Skip to content

Commit 7dcd49f

Browse files
committed
[Coral-Trino] use 'TRY_CAST' instead of 'CAST'
1 parent 15fc504 commit 7dcd49f

File tree

8 files changed

+131
-88
lines changed

8 files changed

+131
-88
lines changed

coral-common/src/main/java/com/linkedin/coral/common/transformers/JsonTransformSqlCallTransformer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ public class JsonTransformSqlCallTransformer extends SourceOperatorMatchSqlCallT
5252
OP_MAP.put("%", SqlStdOperatorTable.MOD);
5353
OP_MAP.put("date", new SqlUserDefinedFunction(new SqlIdentifier("date", SqlParserPos.ZERO), ReturnTypes.DATE, null,
5454
OperandTypes.STRING, null, null));
55-
OP_MAP.put("timestamp", new SqlUserDefinedFunction(new SqlIdentifier("timestamp", SqlParserPos.ZERO),
55+
OP_MAP.put("trino_timestamp", new SqlUserDefinedFunction(new SqlIdentifier("trino_timestamp", SqlParserPos.ZERO),
5656
FunctionReturnTypes.TIMESTAMP, null, OperandTypes.STRING, null, null) {
5757
@Override
5858
public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
59-
// for timestamp operator, we need to construct `CAST(x AS TIMESTAMP)`
59+
// for timestamp operator, we need to construct `TRY_CAST(x AS TIMESTAMP)`
6060
Preconditions.checkState(call.operandCount() == 1);
61-
final SqlWriter.Frame frame = writer.startFunCall("CAST");
61+
final SqlWriter.Frame frame = writer.startFunCall("TRY_CAST");
6262
call.operand(0).unparse(writer, 0, 0);
6363
writer.sep("AS");
6464
writer.literal("TIMESTAMP");

coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/CoralToTrinoSqlCallConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,15 @@ protected SqlCall transform(SqlCall sqlCall) {
9494
"[{\"regex\":\"(?i)('utf-8')\", \"input\":2, \"name\":\"from_utf8\"}]", "[{\"input\":1}]", null, null),
9595
new JsonTransformSqlCallTransformer(hiveToCoralSqlOperator("date_add"), 2, "date_add",
9696
"[{\"value\": 'day'}, {\"input\": 2}, "
97-
+ "{\"op\": \"date\", \"operands\":[{\"op\": \"timestamp\", \"operands\":[{\"input\": 1}]}]}]",
97+
+ "{\"op\": \"date\", \"operands\":[{\"op\": \"trino_timestamp\", \"operands\":[{\"input\": 1}]}]}]",
9898
null, null),
9999
new JsonTransformSqlCallTransformer(hiveToCoralSqlOperator("date_sub"), 2, "date_add",
100100
"[{\"value\": 'day'}, " + "{\"op\": \"*\", \"operands\":[{\"input\": 2}, {\"value\": -1}]}, "
101-
+ "{\"op\": \"date\", \"operands\":[{\"op\": \"timestamp\", \"operands\":[{\"input\": 1}]}]}]",
101+
+ "{\"op\": \"date\", \"operands\":[{\"op\": \"trino_timestamp\", \"operands\":[{\"input\": 1}]}]}]",
102102
null, null),
103103
new JsonTransformSqlCallTransformer(hiveToCoralSqlOperator("datediff"), 2, "date_diff",
104-
"[{\"value\": 'day'}, {\"op\": \"date\", \"operands\":[{\"op\": \"timestamp\", \"operands\":[{\"input\": 2}]}]}, "
105-
+ "{\"op\": \"date\", \"operands\":[{\"op\": \"timestamp\", \"operands\":[{\"input\": 1}]}]}]",
104+
"[{\"value\": 'day'}, {\"op\": \"date\", \"operands\":[{\"op\": \"trino_timestamp\", \"operands\":[{\"input\": 2}]}]}, "
105+
+ "{\"op\": \"date\", \"operands\":[{\"op\": \"trino_timestamp\", \"operands\":[{\"input\": 1}]}]}]",
106106
null, null),
107107
new ToDateOperatorTransformer(configs.getOrDefault(AVOID_TRANSFORM_TO_DATE_UDF, false)),
108108
new CurrentTimestampTransformer(), new FromUnixtimeOperatorTransformer(),

coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/TrinoSqlDialect.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2017-2023 LinkedIn Corporation. All rights reserved.
2+
* Copyright 2017-2024 LinkedIn Corporation. All rights reserved.
33
* Licensed under the BSD-2 Clause license.
44
* See LICENSE in the project root for license information.
55
*/
@@ -14,6 +14,7 @@
1414
import org.apache.calcite.sql.parser.SqlParserPos;
1515

1616
import static com.linkedin.coral.hive.hive2rel.functions.TimestampFromUnixtime.TIMESTAMP_FROM_UNIXTIME;
17+
import static com.linkedin.coral.trino.rel2trino.functions.TrinoTryCastFunction.TRY_CAST;
1718

1819

1920
public class TrinoSqlDialect extends SqlDialect {
@@ -76,8 +77,11 @@ public void unparseCall(SqlWriter writer, SqlCall call, int leftPrec, int rightP
7677
unparseMapValueConstructor(writer, call, leftPrec, rightPrec);
7778
break;
7879
default:
79-
if (call.getOperator().getName().equals("timestamp_from_unixtime")) {
80+
String operateName = call.getOperator().getName();
81+
if (operateName.equals("timestamp_from_unixtime")) {
8082
TIMESTAMP_FROM_UNIXTIME.unparse(writer, call, leftPrec, rightPrec);
83+
} else if (operateName.equalsIgnoreCase("cast")) {
84+
TRY_CAST.unparse(writer, call, leftPrec, rightPrec);
8185
} else {
8286
super.unparseCall(writer, call, leftPrec, rightPrec);
8387
}

coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/functions/TrinoStructCastRowFunction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2019-2023 LinkedIn Corporation. All rights reserved.
2+
* Copyright 2019-2024 LinkedIn Corporation. All rights reserved.
33
* Licensed under the BSD-2 Clause license.
44
* See LICENSE in the project root for license information.
55
*/
@@ -21,6 +21,6 @@
2121
*/
2222
public class TrinoStructCastRowFunction extends GenericTemplateFunction {
2323
public TrinoStructCastRowFunction(RelDataType structDataType) {
24-
super(structDataType, "cast");
24+
super(structDataType, "try_cast");
2525
}
2626
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2019-2024 LinkedIn Corporation. All rights reserved.
3+
* Licensed under the BSD-2 Clause license.
4+
* See LICENSE in the project root for license information.
5+
*/
6+
package com.linkedin.coral.trino.rel2trino.functions;
7+
8+
import org.apache.calcite.sql.SqlCall;
9+
import org.apache.calcite.sql.SqlFunction;
10+
import org.apache.calcite.sql.SqlFunctionCategory;
11+
import org.apache.calcite.sql.SqlIntervalQualifier;
12+
import org.apache.calcite.sql.SqlKind;
13+
import org.apache.calcite.sql.SqlWriter;
14+
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
15+
16+
17+
public class TrinoTryCastFunction extends SqlFunction {
18+
public static final TrinoTryCastFunction TRY_CAST = new TrinoTryCastFunction();
19+
20+
public TrinoTryCastFunction() {
21+
super("try_cast", SqlKind.CAST, SqlStdOperatorTable.CAST::inferReturnType, null, null,
22+
SqlFunctionCategory.USER_DEFINED_FUNCTION);
23+
}
24+
25+
public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
26+
assert call.operandCount() == 2;
27+
28+
SqlWriter.Frame frame = writer.startFunCall(this.getName());
29+
call.operand(0).unparse(writer, 0, 0);
30+
writer.sep("AS");
31+
if (call.operand(1) instanceof SqlIntervalQualifier) {
32+
writer.sep("INTERVAL");
33+
}
34+
35+
call.operand(1).unparse(writer, 0, 0);
36+
writer.endFunCall(frame);
37+
}
38+
}

coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/transformers/GenericProjectTransformer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2023 LinkedIn Corporation. All rights reserved.
2+
* Copyright 2023-2024 LinkedIn Corporation. All rights reserved.
33
* Licensed under the BSD-2 Clause license.
44
* See LICENSE in the project root for license information.
55
*/
@@ -303,7 +303,7 @@ private String arrayDataTypeArgumentString(ArraySqlType fromDataType, ArraySqlTy
303303
*/
304304
private String structDataTypeString(RelRecordType fromDataType, RelRecordType toDataType, String fieldNameReference) {
305305
String structDataTypeArgumentString = structDataTypeArgumentString(fromDataType, toDataType, fieldNameReference);
306-
return (String.format("cast(%s)", structDataTypeArgumentString));
306+
return (String.format("TRY_CAST(%s)", structDataTypeArgumentString));
307307
}
308308

309309
/**

coral-trino/src/main/java/com/linkedin/coral/trino/rel2trino/transformers/ToDateOperatorTransformer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2023 LinkedIn Corporation. All rights reserved.
2+
* Copyright 2023-2024 LinkedIn Corporation. All rights reserved.
33
* Licensed under the BSD-2 Clause license.
44
* See LICENSE in the project root for license information.
55
*/
@@ -37,13 +37,13 @@ public class ToDateOperatorTransformer extends SqlCallTransformer {
3737
private static final String TO_OPERATOR_NAME = "date";
3838
private static final int NUM_OPERANDS = 1;
3939
private static final SqlOperator TIMESTAMP_OPERATOR =
40-
new SqlUserDefinedFunction(new SqlIdentifier("timestamp", SqlParserPos.ZERO), FunctionReturnTypes.TIMESTAMP, null,
41-
OperandTypes.STRING, null, null) {
40+
new SqlUserDefinedFunction(new SqlIdentifier("trino_timestamp", SqlParserPos.ZERO), FunctionReturnTypes.TIMESTAMP,
41+
null, OperandTypes.STRING, null, null) {
4242
@Override
4343
public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) {
44-
// for timestamp operator, we need to construct `CAST(x AS TIMESTAMP)`
44+
// for timestamp operator, we need to construct `TRY_CAST(x AS TIMESTAMP)`
4545
Preconditions.checkState(call.operandCount() == 1);
46-
final SqlWriter.Frame frame = writer.startFunCall("CAST");
46+
final SqlWriter.Frame frame = writer.startFunCall("TRY_CAST");
4747
call.operand(0).unparse(writer, 0, 0);
4848
writer.sep("AS");
4949
writer.literal("TIMESTAMP");

0 commit comments

Comments
 (0)