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

[CALCITE-5757] BigQuery DATE_TRUNC return type should be ARG0 and TIMESTAMP_TRUNC/DATETIME_TRUNC should return TIMESTAMP for DATE/TIMESTAMPs and TIMESTAMP_LTZ for TIMESTAMP_LTZ #3248

Merged
merged 1 commit into from Jun 8, 2023

Conversation

tanclary
Copy link
Contributor

@tanclary tanclary commented Jun 6, 2023

BigQuery TRUNC functions follow the return type behavior described in CALCITE-5757. This PR adjusts the return type for those functions such that it matches the described behavior.

Notable changes include converting a DATE to a TIMESTAMP for the TIMESTAMP_TRUNC and DATETIME_TRUNC functions. It also ensures that DATE_TRUNC return type is equivalent to the operand type (aka ReturnTypes.ARG0).

Prior to this PR, discrepancies included DATETIME_TRUNC incorrectly returning a TIMESTAMP_LTZ if given a TIMESTAMP argument and DATE_TRUNC having a DATE return type even if the argument was a TIMESTAMP or TIMESTAMP_LTZ.

@julianhyde I opted to just include the TRUNC functions in this PR to keep the review and scope fairly easy (hopefully).

@tanclary tanclary force-pushed the stripped-trunc-cast branch 4 times, most recently from 34723b2 to e6f0e3f Compare June 7, 2023 18:49
@tanclary tanclary changed the title [CALCITE-5757] Align return types for TRUNC functions for BigQuery [CALCITE-5757] Correct DATE_TRUNC return type and convert DATEs to TIMESTAMPs for DATETIME_TRUNC and TIMESTAMP_TRUNC Jun 8, 2023
@tanclary tanclary changed the title [CALCITE-5757] Correct DATE_TRUNC return type and convert DATEs to TIMESTAMPs for DATETIME_TRUNC and TIMESTAMP_TRUNC [CALCITE-5757] Correct DATE_TRUNC return type and convert DATEs to TIMESTAMPs for DATETIME_TRUNC and TIMESTAMP_TRUNC for BigQuery Jun 8, 2023
DATE_TRUNC(DATETIME "2008-12-25 15:30:00", MONTH) AS datetime_result;
timestamp_result TIMESTAMP_WITH_LOCAL_TIME_ZONE NOT NULL
datetime_result TIMESTAMP NOT NULL
!type
Copy link
Contributor

Choose a reason for hiding this comment

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

neat, I didnt know about !type in quidem tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah super useful!!

@julianhyde julianhyde force-pushed the main branch 2 times, most recently from 8a5cf83 to cf7f71b Compare June 8, 2023 21:21
* Type-inference strategy that returns the type of the first operand,
* unless it is a DATE, in which case the return type is TIMESTAMP.
*/
public static final SqlReturnTypeInference ARG0_EXCEPT_DATE = opBinding -> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Might be nice to add a comment referencing [CALCITE-5757] to remind that this is for BQ typing behavior

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated, let me know if it's clear enough

final RexBuilder rexBuilder = cx.getRexBuilder();
RexNode op1 = cx.convertExpression(call.operand(0));
RexNode op2 = cx.convertExpression(call.operand(1));
if (op1.getType().getSqlTypeName() == SqlTypeName.DATE) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe also a comment here discussion why DATE is handled separately

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same as above

@julianhyde
Copy link
Contributor

The commit message is currently as follows:

Correct DATE_TRUNC return type and convert DATEs to TIMESTAMPs for DATETIME_TRUNC and TIMESTAMP_TRUNC for BigQuery

That contains 'correct', which is a synonym for 'fix', and is superfluous. Just state the desired behavior. Would the following be better?

BigQuery functions DATETIME_TRUNC and TIMESTAMP_TRUNC should return same type as their first argument, and DATE_TRUNC should return DATE

@tanclary tanclary changed the title [CALCITE-5757] Correct DATE_TRUNC return type and convert DATEs to TIMESTAMPs for DATETIME_TRUNC and TIMESTAMP_TRUNC for BigQuery [CALCITE-5757] BigQuery functions DATETIME_TRUNC and TIMESTAMP_TRUNC should return same type as their first argument, and DATE_TRUNC should return DATE Jun 8, 2023
@tanclary tanclary changed the title [CALCITE-5757] BigQuery functions DATETIME_TRUNC and TIMESTAMP_TRUNC should return same type as their first argument, and DATE_TRUNC should return DATE [CALCITE-5757] BigQuery DATE_TRUNC return type should be ARG0 and TIMESTAMP_TRUNC/DATETIME_TRUNC should return TIMESTAMP for DATE/TIMESTAMPs and TIMESTAMP_LTZ for TIMESTAMP_LTZ Jun 8, 2023
@julianhyde
Copy link
Contributor

After you've removed & inlined ARG0_EXCEPT_DATE_NULLABLE, +1, go ahead and merge.

…ESTAMP_TRUNC/DATETIME_TRUNC should return TIMESTAMP for DATE/TIMESTAMPs and TIMESTAMP_LTZ for TIMESTAMP_LTZ
@tanclary
Copy link
Contributor Author

tanclary commented Jun 8, 2023

@julianhyde I updated it, also, DATE_TRUNC /should/ return ARG0 and not DATE (in case you wonder why commit message differs from your suggestion)

@sonarcloud
Copy link

sonarcloud bot commented Jun 8, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 7 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@tanclary tanclary merged commit 7dc94e3 into apache:main Jun 8, 2023
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants