Skip to content

Commit

Permalink
[CALCITE-5508] Allow no-op versions of DATETIME and TIMESTAMP
Browse files Browse the repository at this point in the history
  • Loading branch information
wnob committed Nov 14, 2023
1 parent 7629568 commit 5e7a2c4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
12 changes: 12 additions & 0 deletions babel/src/test/resources/sql/big-query.iq
Original file line number Diff line number Diff line change
Expand Up @@ -2515,6 +2515,18 @@ SELECT TIMESTAMP(DATE "2008-12-25") AS timestamp_date;

!ok

# This tests the no-op TIMESTAMP and DATETIME functions.
select TIMESTAMP(TIMESTAMP "2008-01-01 01:03:05") as ts,
DATETIME(DATETIME "2008-01-01 01:03:05") as dt;
+---------------------+---------------------+
| ts | dt |
+---------------------+---------------------+
| 2008-01-01 01:03:05 | 2008-01-01 01:03:05 |
+---------------------+---------------------+
(1 row)

!ok

# All these timestamps should be equal.
# This tests the BQ timestamp literal string formatter
# (optional 'T', optional leading zeros, optional offset with conversion).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,9 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
// DATETIME(timestampLtz, timeZone)
OperandTypes.sequence(
"DATETIME(TIMESTAMP WITH LOCAL TIME ZONE, VARCHAR)",
OperandTypes.TIMESTAMP_LTZ, OperandTypes.CHARACTER)),
OperandTypes.TIMESTAMP_LTZ, OperandTypes.CHARACTER),
// DATETIME(timestamp) -- This is a no-op.
OperandTypes.TIMESTAMP_NTZ),
SqlFunctionCategory.TIMEDATE);

/** The "TIME" function. It has the following overloads:
Expand Down Expand Up @@ -788,7 +790,9 @@ static RelDataType deriveTypeSplit(SqlOperatorBinding operatorBinding,
OperandTypes.TIMESTAMP_NTZ,
// TIMESTAMP(timestamp, timeZone)
OperandTypes.sequence("TIMESTAMP(TIMESTAMP, VARCHAR)",
OperandTypes.TIMESTAMP_NTZ, OperandTypes.CHARACTER)),
OperandTypes.TIMESTAMP_NTZ, OperandTypes.CHARACTER),
// TIMESTAMP(timestampLtz) -- This is a no-op.
OperandTypes.TIMESTAMP_LTZ),
SqlFunctionCategory.TIMEDATE);

/** The "CURRENT_DATETIME([timezone])" function. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5045,4 +5045,24 @@ void checkUserDefinedOrderByOver(NullCollation nullCollation) {
String sql = "SELECT CAST(CAST(? AS INTEGER) AS CHAR)";
sql(sql).ok();
}

/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-5508">DATETIME and TIMESTAMP constructor
* functions</a> that take single arguments with the same type as the function's return type.
*
* <p>The function counts as a no-op in this case.
*/
@Test void testNoOpTimeDateFunctions() {
String sql = "SELECT TIMESTAMP(TIMESTAMP WITH LOCAL TIME ZONE '2023-12-21 12:34:56'), "
+ "DATETIME(TIMESTAMP '2023-12-21 12:34:56') "
+ "FROM emp";

fixture()
.withFactory(c ->
c.withOperatorTable(t ->
SqlValidatorTest.operatorTableFor(SqlLibrary.BIG_QUERY)))
.withSql(sql)
.ok();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4883,6 +4883,17 @@ LogicalProject(SKILL=[ROW($0)])
LogicalFilter(condition=[=($0, '')])
LogicalProject(TYPE=[$0.TYPE])
LogicalTableScan(table=[[CATALOG, SALES, DEPT_SINGLE]])
]]>
</Resource>
</TestCase>
<TestCase name="testNoOpTimeDateFunctions">
<Resource name="sql">
<![CDATA[SELECT TIMESTAMP(TIMESTAMP WITH LOCAL TIME ZONE '2023-12-21 12:34:56'), DATETIME(TIMESTAMP '2023-12-21 12:34:56') FROM emp]]>
</Resource>
<Resource name="plan">
<![CDATA[
LogicalProject(EXPR$0=[TIMESTAMP(2023-12-21 12:34:56:TIMESTAMP_WITH_LOCAL_TIME_ZONE(0))], EXPR$1=[DATETIME(2023-12-21 12:34:56)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
Expand Down

0 comments on commit 5e7a2c4

Please sign in to comment.