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

[Coral-Trino] Modify transformation for CURRENT_TIMESTAMP #483

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2023 LinkedIn Corporation. All rights reserved.
* Copyright 2023-2024 LinkedIn Corporation. All rights reserved.
* Licensed under the BSD-2 Clause license.
* See LICENSE in the project root for license information.
*/
Expand All @@ -8,6 +8,10 @@
import org.apache.calcite.sql.SqlBasicTypeNameSpec;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlDataTypeSpec;
import org.apache.calcite.sql.SqlFunction;
import org.apache.calcite.sql.SqlFunctionCategory;
import org.apache.calcite.sql.SqlOperator;
import org.apache.calcite.sql.SqlSyntax;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.SqlTypeName;
Expand All @@ -17,12 +21,16 @@

/**
* This class implements the transformation for the CURRENT_TIMESTAMP function.
* For example, "SELECT CURRENT_TIMESTAMP" is transformed into "SELECT CAST(CURRENT_TIMESTAMP AS TIMESTAMP(3))".
* This transformation ensures compatibility with Trino.
* For example, "SELECT CURRENT_TIMESTAMP" is transformed into "SELECT CAST(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' AS TIMESTAMP(3))".
* This transformation ensures compatibility with Trino due to the following reasons:
* (1) In Hive, CURRENT_TIMESTAMP returns a UTC timestamp, whereas in Trino, it returns a timestamp based on the session's timezone.
* (2) In Hive, the return type of CURRENT_TIMESTAMP is timestamp, whereas in Trino, the return type is timestamp with time zone,
* so we need to add an explicit CAST.
*/
public class CurrentTimestampTransformer extends SqlCallTransformer {

private static final String CURRENT_TIMESTAMP_FUNCTION_NAME = "CURRENT_TIMESTAMP";
private static final String CURRENT_TIMESTAMP_AT_TIMEZONE_UTC = "CURRENT_TIMESTAMP AT TIME ZONE 'UTC'";

@Override
protected boolean condition(SqlCall sqlCall) {
Expand All @@ -33,6 +41,17 @@ protected boolean condition(SqlCall sqlCall) {
protected SqlCall transform(SqlCall sqlCall) {
SqlDataTypeSpec timestampType =
new SqlDataTypeSpec(new SqlBasicTypeNameSpec(SqlTypeName.TIMESTAMP, 3, SqlParserPos.ZERO), SqlParserPos.ZERO);
return SqlStdOperatorTable.CAST.createCall(SqlParserPos.ZERO, sqlCall, timestampType);
final SqlOperator currentTimestampOperator = sqlCall.getOperator();
final SqlFunction currentTimestampAtTimeZoneUTCOperator =
new SqlFunction(CURRENT_TIMESTAMP_AT_TIMEZONE_UTC, currentTimestampOperator.kind,
currentTimestampOperator.getReturnTypeInference(), currentTimestampOperator.getOperandTypeInference(),
currentTimestampOperator.getOperandTypeChecker(), SqlFunctionCategory.TIMEDATE) {
@Override
public SqlSyntax getSyntax() {
return SqlSyntax.FUNCTION_ID;
}
};
return SqlStdOperatorTable.CAST.createCall(SqlParserPos.ZERO,
currentTimestampAtTimeZoneUTCOperator.createCall(SqlParserPos.ZERO, sqlCall.getOperandList()), timestampType);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2023 LinkedIn Corporation. All rights reserved.
* Copyright 2017-2024 LinkedIn Corporation. All rights reserved.
* Licensed under the BSD-2 Clause license.
* See LICENSE in the project root for license information.
*/
Expand Down Expand Up @@ -136,7 +136,7 @@ public Object[][] viewTestCasesProvider() {
{ "test", "map_array_view", "SELECT MAP (ARRAY['key1', 'key2'], ARRAY['value1', 'value2']) AS \"simple_map_col\", MAP (ARRAY['key1', 'key2'], ARRAY[MAP (ARRAY['a', 'c'], ARRAY['b', 'd']), MAP (ARRAY['a', 'c'], ARRAY['b', 'd'])]) AS \"nested_map_col\"\n"
+ "FROM \"test\".\"tablea\" AS \"tablea\"" },

{ "test", "current_date_and_timestamp_view", "SELECT CAST(CURRENT_TIMESTAMP AS TIMESTAMP(3)), TRIM(CAST(CAST(CURRENT_TIMESTAMP AS TIMESTAMP(3)) AS VARCHAR(65535))) AS \"ct\", CURRENT_DATE, CURRENT_DATE AS \"cd\", \"tablea\".\"a\" AS \"a\"\n"
{ "test", "current_date_and_timestamp_view", "SELECT CAST(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' AS TIMESTAMP(3)), TRIM(CAST(CAST(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' AS TIMESTAMP(3)) AS VARCHAR(65535))) AS \"ct\", CURRENT_DATE, CURRENT_DATE AS \"cd\", \"tablea\".\"a\" AS \"a\"\n"
+ "FROM \"test\".\"tablea\" AS \"tablea\"" },

{ "test", "date_function_view", "SELECT \"date\"('2021-01-02') AS \"a\"\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2017-2023 LinkedIn Corporation. All rights reserved.
* Copyright 2017-2024 LinkedIn Corporation. All rights reserved.
* Licensed under the BSD-2 Clause license.
* See LICENSE in the project root for license information.
*/
Expand Down Expand Up @@ -510,8 +510,8 @@ public void testCurrentUser() {
@Test
public void testCurrentTimestamp() {
String sql = "SELECT current_timestamp";
String expected =
formatSql("SELECT CAST(CURRENT_TIMESTAMP AS TIMESTAMP(3))\nFROM (VALUES (0)) AS \"t\" (\"ZERO\")");
String expected = formatSql(
"SELECT CAST(CURRENT_TIMESTAMP AT TIME ZONE 'UTC' AS TIMESTAMP(3))\nFROM (VALUES (0)) AS \"t\" (\"ZERO\")");
testConversion(sql, expected);
}

Expand Down
Loading