Skip to content

Commit

Permalink
[Coral-Trino] Migrate 'CurrentTimestampTransformer' from RelNode to S…
Browse files Browse the repository at this point in the history
…qlNode layer (#384)
  • Loading branch information
ljfgem authored Apr 5, 2023
1 parent 7d7f962 commit 825943f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,6 @@ public RexNode visitCall(RexCall call) {
}
}

if (operatorName.equalsIgnoreCase("current_timestamp")) {
Optional<RexNode> modifiedCall = visitCurrentTimestamp(call);
if (modifiedCall.isPresent()) {
return modifiedCall.get();
}
}

if (operatorName.equalsIgnoreCase("concat")) {
Optional<RexNode> modifiedCall = visitConcat(call);
if (modifiedCall.isPresent()) {
Expand Down Expand Up @@ -333,13 +326,6 @@ private Optional<RexNode> visitSubstring(RexCall call) {
return Optional.empty();
}

private Optional<RexNode> visitCurrentTimestamp(RexCall call) {
final SqlOperator op = call.getOperator();
// We may want to extend this to allow current_timestamp(n) in case of Trino <-> Trino conversion, to make
// intermediate representation more complete.
return Optional.of(rexBuilder.makeCast(typeFactory.createSqlType(TIMESTAMP, 3), rexBuilder.makeCall(op)));
}

private Optional<RexNode> visitCast(RexCall call) {
final SqlOperator op = call.getOperator();
if (op.getKind() != SqlKind.CAST) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.linkedin.coral.trino.rel2trino.functions.TrinoElementAtFunction;
import com.linkedin.coral.trino.rel2trino.transformers.CollectListOrSetFunctionTransformer;
import com.linkedin.coral.trino.rel2trino.transformers.CoralRegistryOperatorRenameSqlCallTransformer;
import com.linkedin.coral.trino.rel2trino.transformers.CurrentTimestampTransformer;
import com.linkedin.coral.trino.rel2trino.transformers.GenericCoralRegistryOperatorRenameSqlCallTransformer;
import com.linkedin.coral.trino.rel2trino.transformers.MapValueConstructorTransformer;
import com.linkedin.coral.trino.rel2trino.transformers.ToDateOperatorTransformer;
Expand Down Expand Up @@ -98,6 +99,7 @@ protected SqlCall transform(SqlCall sqlCall) {
+ "{\"op\": \"date\", \"operands\":[{\"op\": \"timestamp\", \"operands\":[{\"input\": 1}]}]}]",
null, null),
new ToDateOperatorTransformer(configs.getOrDefault(AVOID_TRANSFORM_TO_DATE_UDF, false)),
new CurrentTimestampTransformer(),

// LinkedIn specific functions
new CoralRegistryOperatorRenameSqlCallTransformer(
Expand All @@ -124,7 +126,6 @@ private SqlOperator hiveToCoralSqlOperator(String functionName) {

@Override
public SqlNode visit(SqlCall call) {
SqlCall transformedCall = sqlCallTransformers.apply(call);
return super.visit(transformedCall);
return sqlCallTransformers.apply((SqlCall) super.visit(call));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright 2023 LinkedIn Corporation. All rights reserved.
* Licensed under the BSD-2 Clause license.
* See LICENSE in the project root for license information.
*/
package com.linkedin.coral.trino.rel2trino.transformers;

import org.apache.calcite.sql.SqlBasicTypeNameSpec;
import org.apache.calcite.sql.SqlCall;
import org.apache.calcite.sql.SqlDataTypeSpec;
import org.apache.calcite.sql.fun.SqlStdOperatorTable;
import org.apache.calcite.sql.parser.SqlParserPos;
import org.apache.calcite.sql.type.SqlTypeName;

import com.linkedin.coral.common.transformers.SqlCallTransformer;


/**
* 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.
*/
public class CurrentTimestampTransformer extends SqlCallTransformer {

private static final String CURRENT_TIMESTAMP_FUNCTION_NAME = "CURRENT_TIMESTAMP";

@Override
protected boolean condition(SqlCall sqlCall) {
return sqlCall.getOperator().getName().equalsIgnoreCase(CURRENT_TIMESTAMP_FUNCTION_NAME);
}

@Override
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);
}
}

0 comments on commit 825943f

Please sign in to comment.