Skip to content

[CALCITE-5360] Implement BigQuery TIMESTAMP_ADD#2998

Closed
tanclary wants to merge 5 commits intoapache:mainfrom
tanclary:timestamp_add
Closed

[CALCITE-5360] Implement BigQuery TIMESTAMP_ADD#2998
tanclary wants to merge 5 commits intoapache:mainfrom
tanclary:timestamp_add

Conversation

@tanclary
Copy link
Contributor

@tanclary tanclary commented Dec 5, 2022

Implement BigQuery's TIMESTAMP_ADD. This implementation is similar to that of the standard timestampadd but accounts for the difference in operand number and type. JIRA issue https://issues.apache.org/jira/browse/CALCITE-5360 includes TIMESTAMP_DIFF as well but this PR is being opened primarily for feedback to avoid potential duplicate mistakes as TIMESTAMP_DIFF should follow a similar implementation.

Copy link
Contributor

@julianhyde julianhyde left a comment

Choose a reason for hiding this comment

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

A good start. I suggested some improvements in comments.

I edited the code to check whether my suggestions made sense, so feel free to build on my changes in https://github.com/julianhyde/calcite/tree/5360-timestamp-add.

)
intervalQualifier = IntervalQualifierStart() {
intervalQualifier = TimeUnitOrName() {
if (sign == -1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this change still required?

@LibraryOperator(libraries = {BIG_QUERY})
public static final SqlFunction TIMESTAMP_ADD_BIG_QUERY =
new SqlTimestampAddBigQueryFunction("TIMESTAMP_ADD");

Copy link
Contributor

Choose a reason for hiding this comment

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

Interval is built-in in Calcite, so the syntax description is just "TIMESTAMP_ADD(timestamp, interval)".

I would name the function TIMESTAMP_ADD2 and describe it as 'The 2-argument TIMESTAMP_ADD function, as in BigQuery, as opposed to the 3-argument TIMESTAMPADD JDBC and builtin function"

f.checkNull("timestamp_add(CAST(NULL AS TIMESTAMP), interval 5 minute)");
}

@Test void testDenseRankFunc() {
Copy link
Contributor

Choose a reason for hiding this comment

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

fix the indent of testDenseRankFunc. then the diff will look better, and checkstyle will pass

}

@Test void testDenseRankFunc() {
@Test void testBigQueryTimestampAdd() {
Copy link
Contributor

Choose a reason for hiding this comment

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

In addition to this test, you should remove the !if (false) { ... } around the TIMESTAMP_ADD test in big-query.iq.

* independent of any time zone.*/
@LibraryOperator(libraries = {BIG_QUERY})
public static final SqlFunction TIMESTAMP_ADD_BIG_QUERY =
new SqlTimestampAddBigQueryFunction("TIMESTAMP_ADD");
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think you need class SqlTimestampAddBigQueryFunction at all. Because this function doesn't support custom time frames, you can remove the logic for those, and the type resolution gets simpler. I think

    SqlBasicFunction.create(SqlKind.TIMESTAMP_ADD, ReturnTypes.ARG0_NULLABLE,
          OperandTypes.TIMESTAMP_INTERVAL)
          .withFunctionType(SqlFunctionCategory.TIMEDATE)

is a sufficient definition.

final SqlOperatorFixture f = fixture()
.withLibrary(SqlLibrary.BIG_QUERY)
.setFor(SqlLibraryOperators.TIMESTAMP_ADD_BIG_QUERY);
f.checkScalar("timestamp_add(timestamp '2008-12-25 15:30:00', "
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think Calcite supports NANOSECOND intervals right now (maybe not MICROSECOND either). You should disable this statement with an if and log a Calcite bug to track.

julianhyde and others added 2 commits December 7, 2022 03:24
enable TIMESTAMP_ADD test in big-query.iq

back out unnecessary parser change

lint

use SqlBasicFunction.create, which makes
class SqlTimestampAddBigQueryFunction unnecessary

the convertlet doesn't need to know the library, just
whether the function call had 2 or 3 arguments

disable tests that use INTERVAL MICROSECONDS or NANOSECONDS;
TODO: log bug and change 'if (false)' to reference the bug
number
julianhyde pushed a commit to julianhyde/calcite that referenced this pull request Dec 7, 2022
The JDBC and builtin functions already contain a TIMESTAMPADD
function with three arguments; this change adds a
TIMESTAMP_ADD function with two arguments, consistent with
BigQuery, and enabled if you have 'lib=bigquery' in your
connection options.

Close apache#2998
@julianhyde julianhyde closed this in 1156efb Dec 7, 2022
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.

2 participants