Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented CAST from VARBINARY to DATETIME in Babelfish. #3481

Open
wants to merge 19 commits into
base: BABEL_3_X_DEV
Choose a base branch
from

Conversation

Yvinayak07
Copy link
Contributor

Description

Previously, Babelfish did not support casting from VARBINARY to DATETIME. We have now implemented this functionality to align with T-SQL's capabilities. This new feature allows users to cast VARBINARY data types to DATETIME.

Task: BABEL-3070
Signed-off-by: yashneet vinayak yashneet@amazon.com

Test Scenarios Covered

  • **Use case based -**Yes

  • **Boundary conditions -**Yes

  • **Arbitrary inputs -**Yes

  • **Negative test cases -**Yes

  • Minor version upgrade tests -

  • Major version upgrade tests -

  • Performance tests -

  • Tooling impact -

  • **Client tests -**Yes

Check List

  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is under the terms of the Apache 2.0 and PostgreSQL licenses, and grant any person obtaining a copy of the contribution permission to relicense all or a portion of my contribution to the PostgreSQL License solely to contribute all or a portion of my contribution to the PostgreSQL open source project.

For more information on following Developer Certificate of Origin and signing off your commits, please check here.

yashneet vinayak and others added 16 commits February 10, 2025 08:04
Signed-off-by: yashneet vinayak <yashneet@amazon.com>
Signed-off-by: yashneet vinayak <yashneet@amazon.com>
Signed-off-by: yashneet vinayak <yashneet@amazon.com>
Signed-off-by: yashneet vinayak <yashneet@amazon.com>
Signed-off-by: yashneet vinayak <yashneet@amazon.com>
Signed-off-by: yashneet vinayak <yashneet@amazon.com>
Signed-off-by: yashneet vinayak <yashneet@amazon.com>
Signed-off-by: yashneet vinayak <yashneet@amazon.com>
Update the antlr version to 4.13.2. This dramatically improves ANTLR parsing performance for certain queries, reducing their execution times by up to 40%.

Note that this will require developers to have Java 11+ installed in order to develop locally.

Also update the github actions to make it so that previous versions of Babelfish will be compiled using the original antlr-4.9.3 version. This is purely a fix for testing.

Task: BABEL-5371
Signed-off-by: Jason Teng <jasonten@amazon.com>
Signed-off-by: yashneet vinayak <yashneet@amazon.com>
Signed-off-by: yashneet vinayak <yashneet@amazon.com>
Signed-off-by: yashneet vinayak <yashneet@amazon.com>
Signed-off-by: yashneet vinayak <yashneet@amazon.com>
@coveralls
Copy link
Collaborator

coveralls commented Feb 13, 2025

Pull Request Test Coverage Report for Build 13323779292

Details

  • 26 of 26 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.01%) to 73.849%

Totals Coverage Status
Change from base Build 13197036746: 0.01%
Covered Lines: 43356
Relevant Lines: 58709

💛 - Coveralls

@@ -312,6 +312,13 @@ LANGUAGE C VOLATILE STRICT PARALLEL SAFE;
CREATE CAST (TIMESTAMP AS DATETIME)
WITH FUNCTION sys.timestamp2datetime(TIMESTAMP) AS ASSIGNMENT;

CREATE OR REPLACE FUNCTION sys.varbinary2datetime(sys.BBF_VARBINARY)
RETURNS DATETIME
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's prepend proper schema everywhere

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prepended sys where ever require.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we should return sys.DATETIME instead of DATETIME.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appended sys schema to the object.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we thoroughly tested all these functions and error scenarios?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing of all scenarios done thoroughly

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these tests are copied from 5_X. Have we matched the output?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


-- Test Varbinary to Datetime conversion using CAST
-- Min value
SELECT CAST(0xFFFF2E4600000000 AS DateTime);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add tests to test IMPLICIT casting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test for this.

Signed-off-by: yashneet vinayak <yashneet@amazon.com>
AS 'babelfishpg_common', 'varbinary_datetime'
LANGUAGE C IMMUTABLE STRICT PARALLEL SAFE;

CREATE CAST (sys.BBF_VARBINARY AS DATETIME)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here as well we should cast to sys.DATETIME instead of DATETIME.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appended sys schema to the object.

yashneet vinayak added 2 commits February 14, 2025 05:57
Signed-off-by: yashneet vinayak <yashneet@amazon.com>
Signed-off-by: yashneet vinayak <yashneet@amazon.com>
Comment on lines +759 to +764
/* For 4 byte varbinary datetime range from 1900-01-01 00:00:00.000 to 1900-01-01 23:59:59:999*/
bool
is_valid_datetime_for_4byte_varbinary(int64 total_usecs)
{
return (total_usecs >= TSQL_DEFAULT_DATETIME && total_usecs <= MAX_4_BYTE_VARBINARY_DATETIME);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. This should be macro instead of function.
  2. If it is not getting used at multiple places then macro is also not needed.

Timestamp result;
unsigned char *buffer = (unsigned char *)VARDATA_ANY(arg);

/* TSQL datetime is 8 bytes */
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean input should be either 4 or 8 bytes, right? Lets update the comment.

Copy link
Contributor

@KushaalShroff KushaalShroff left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For testing, please make sure to add the below tests:

  • NULLs tests, this is different to 0x00
  • Boundary testing
  • Overflows
  • Coercive tests, eg instead of datetime use date or time which I assume have coercive casts
  • MAX binary type testing

unsigned char *buffer = (unsigned char *)VARDATA_ANY(arg);

/* TSQL datetime is 8 bytes */
if (size != sizeof(int64) && size != sizeof(int32))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As spoken offline, Cast seems to accept multitude of binary lengths, I think MAX ones as well. Lets add support for all lens.

@KushaalShroff
Copy link
Contributor

Are we not supporting binary casts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants