Skip to content

Commit

Permalink
Coral-Trino: Use cast(xxx as timestamp) for timestamp operator (#143
Browse files Browse the repository at this point in the history
)

* Fix timestamp issue in Coral-Trino

* Add comment

* Modified code to use CAST for TIMESTAMP
  • Loading branch information
ljfgem authored Sep 8, 2021
1 parent eb8382d commit 409703f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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\"" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit 409703f

Please sign in to comment.