diff --git a/coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/UDFTransformer.java b/coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/UDFTransformer.java index b23b0531f..e36f68e79 100644 --- a/coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/UDFTransformer.java +++ b/coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/UDFTransformer.java @@ -32,8 +32,11 @@ import org.apache.calcite.sql.type.OperandTypes; import org.apache.calcite.sql.validate.SqlUserDefinedFunction; +import com.linkedin.coral.com.google.common.base.Preconditions; import com.linkedin.coral.hive.hive2rel.functions.HiveReturnTypes; +import static org.apache.calcite.sql.type.SqlTypeName.*; + /** * Object for transforming UDF from one SQL language to another SQL language. @@ -133,8 +136,13 @@ public class UDFTransformer { HiveReturnTypes.TIMESTAMP, null, OperandTypes.STRING, null, null) { @Override public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { - writer.keyword(call.getOperator().getName()); - call.getOperandList().get(0).unparse(writer, 0, 0); //Just one operand + // for timestamp operator, we need to construct `CAST(x AS TIMESTAMP)` + Preconditions.checkState(call.operandCount() == 1); + final SqlWriter.Frame frame = writer.startFunCall("CAST"); + call.operand(0).unparse(writer, 0, 0); + writer.sep("AS"); + writer.literal("TIMESTAMP"); + writer.endFunCall(frame); } }); OP_MAP.put("hive_pattern_to_trino", diff --git a/coral-trino/src/test/java/com/linkedin/coral/trino/rel2trino/HiveToTrinoConverterTest.java b/coral-trino/src/test/java/com/linkedin/coral/trino/rel2trino/HiveToTrinoConverterTest.java index 4f88f1ca4..dff1c9282 100644 --- a/coral-trino/src/test/java/com/linkedin/coral/trino/rel2trino/HiveToTrinoConverterTest.java +++ b/coral-trino/src/test/java/com/linkedin/coral/trino/rel2trino/HiveToTrinoConverterTest.java @@ -163,15 +163,7 @@ public Object[][] viewTestCasesProvider() { + "CAST(\"at_timezone\"(\"from_unixtime\"(\"to_unixtime\"(\"with_timezone\"(\"a_date\", 'UTC'))), \"$canonicalize_hive_timezone_id\"('America/Los_Angeles')) AS TIMESTAMP(3))\n" + "FROM \"test\".\"table_from_utc_timestamp\"" }, - { "test", "date_calculation_view", "SELECT " - + "\"date\"(TIMESTAMP '2021-08-20'), \"date\"(TIMESTAMP '2021-08-20 00:00:00'), " - + "\"date_add\"('day', 1, \"date\"(TIMESTAMP '2021-08-20')), " - + "\"date_add\"('day', 1, \"date\"(TIMESTAMP '2021-08-20 00:00:00')), " - + "\"date_add\"('day', 1 * -1, \"date\"(TIMESTAMP '2021-08-20')), " - + "\"date_add\"('day', 1 * -1, \"date\"(TIMESTAMP '2021-08-20 00:00:00')), " - + "\"date_diff\"('day', \"date\"(TIMESTAMP '2021-08-21'), \"date\"(TIMESTAMP '2021-08-20')), " - + "\"date_diff\"('day', \"date\"(TIMESTAMP '2021-08-19'), \"date\"(TIMESTAMP '2021-08-20')), " - + "\"date_diff\"('day', \"date\"(TIMESTAMP '2021-08-19 23:59:59'), \"date\"(TIMESTAMP '2021-08-20 00:00:00'))\n" + { "test", "date_calculation_view", "SELECT \"date\"(CAST(\"SUBSTR\"('2021-08-20', 1, 10) AS TIMESTAMP)), \"date\"(CAST('2021-08-20' AS TIMESTAMP)), \"date\"(CAST('2021-08-20 00:00:00' AS TIMESTAMP)), \"date_add\"('day', 1, \"date\"(CAST('2021-08-20' AS TIMESTAMP))), \"date_add\"('day', 1, \"date\"(CAST('2021-08-20 00:00:00' AS TIMESTAMP))), \"date_add\"('day', 1 * -1, \"date\"(CAST('2021-08-20' AS TIMESTAMP))), \"date_add\"('day', 1 * -1, \"date\"(CAST('2021-08-20 00:00:00' AS TIMESTAMP))), \"date_diff\"('day', \"date\"(CAST('2021-08-21' AS TIMESTAMP)), \"date\"(CAST('2021-08-20' AS TIMESTAMP))), \"date_diff\"('day', \"date\"(CAST('2021-08-19' AS TIMESTAMP)), \"date\"(CAST('2021-08-20' AS TIMESTAMP))), \"date_diff\"('day', \"date\"(CAST('2021-08-19 23:59:59' AS TIMESTAMP)), \"date\"(CAST('2021-08-20 00:00:00' AS TIMESTAMP)))\n" + "FROM \"test\".\"tablea\"" }, { "test", "pmod_view", "SELECT MOD(MOD(- 9, 4) + 4, 4)\nFROM \"test\".\"tablea\"" }, diff --git a/coral-trino/src/test/java/com/linkedin/coral/trino/rel2trino/TestUtils.java b/coral-trino/src/test/java/com/linkedin/coral/trino/rel2trino/TestUtils.java index 10430ca29..fc79e0bd2 100644 --- a/coral-trino/src/test/java/com/linkedin/coral/trino/rel2trino/TestUtils.java +++ b/coral-trino/src/test/java/com/linkedin/coral/trino/rel2trino/TestUtils.java @@ -320,11 +320,12 @@ public static void initializeViews(Path metastoreDbDirectory) throws HiveExcepti + "from_utc_timestamp(a_timestamp, 'America/Los_Angeles'), " + "from_utc_timestamp(a_date, 'America/Los_Angeles')" + "FROM test.table_from_utc_timestamp"); - run(driver, "CREATE VIEW IF NOT EXISTS test.date_calculation_view AS \n" + "SELECT to_date('2021-08-20'), " - + "to_date('2021-08-20 00:00:00'), " + "date_add('2021-08-20', 1), " + "date_add('2021-08-20 00:00:00', 1), " - + "date_sub('2021-08-20', 1), " + "date_sub('2021-08-20 00:00:00', 1), " - + "datediff('2021-08-20', '2021-08-21'), " + "datediff('2021-08-20', '2021-08-19'), " - + "datediff('2021-08-20 00:00:00', '2021-08-19 23:59:59')" + "FROM test.tableA"); + run(driver, "CREATE VIEW IF NOT EXISTS test.date_calculation_view AS \n" + + "SELECT to_date(substr('2021-08-20', 1, 10)), to_date('2021-08-20'), " + "to_date('2021-08-20 00:00:00'), " + + "date_add('2021-08-20', 1), " + "date_add('2021-08-20 00:00:00', 1), " + "date_sub('2021-08-20', 1), " + + "date_sub('2021-08-20 00:00:00', 1), " + "datediff('2021-08-20', '2021-08-21'), " + + "datediff('2021-08-20', '2021-08-19'), " + "datediff('2021-08-20 00:00:00', '2021-08-19 23:59:59')" + + "FROM test.tableA"); run(driver, "CREATE VIEW IF NOT EXISTS test.pmod_view AS \n" + "SELECT pmod(-9, 4) FROM test.tableA");