Skip to content

Commit

Permalink
[Coral-Trino] Migrate cast transformation from RelNode layer to SqlNo…
Browse files Browse the repository at this point in the history
…de layer (#491)

* Initial commit for migrating Cast

* add type derivation changes

* type derivation enhance and test debug

* remove testing files

* spotlessapply + comment out failing unit test

* update unit tests

* fix regression by TypeDerivationUtil on casts with join on simple aliases

* generalize case of casting with join on simple aliases

* remove extraneous input to dummy select

* temporary removal of type util postprocessor for i-test

* revert

* spotless checks

* update calcite version for linkedin-calcite #98/#99

* remove Calcite2TrinoUDFConverter

* clean up tests

* unused imports

* spotless

* improve javadoc/documentation

* fix javadoc

* improve javadoc

* grammar

* refactor + clean up

---------

Co-authored-by: aastha25 <[email protected]>
  • Loading branch information
KevinGe00 and aastha25 authored Apr 11, 2024
1 parent 3ffebec commit e09015e
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 228 deletions.
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
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 Down Expand Up @@ -59,6 +59,11 @@ public RelDataType getRelDataType(SqlNode sqlNode) {
throw new RuntimeException("SqlValidator does not exist to derive the RelDataType for SqlNode: " + sqlNode);
}

RelDataType cachedDataType = sqlValidator.getValidatedNodeTypeIfKnown(sqlNode);
if (cachedDataType != null) {
return cachedDataType;
}

for (SqlSelect topSqlSelectNode : topSelectNodes) {
final SqlSelect dummySqlSelect = new SqlSelect(topSqlSelectNode.getParserPosition(), null,
SqlNodeList.of(sqlNode), topSqlSelectNode.getFrom(), topSqlSelectNode.getWhere(), topSqlSelectNode.getGroup(),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2022-2023 LinkedIn Corporation. All rights reserved.
* Copyright 2022-2024 LinkedIn Corporation. All rights reserved.
* Licensed under the BSD-2 Clause license.
* See LICENSE in the project root for license information.
*/
Expand All @@ -16,6 +16,7 @@
import com.linkedin.coral.common.utils.TypeDerivationUtil;
import com.linkedin.coral.hive.hive2rel.HiveToRelConverter;
import com.linkedin.coral.hive.hive2rel.functions.VersionedSqlUserDefinedFunction;
import com.linkedin.coral.trino.rel2trino.transformers.CastOperatorTransformer;
import com.linkedin.coral.trino.rel2trino.transformers.ConcatOperatorTransformer;
import com.linkedin.coral.trino.rel2trino.transformers.FromUtcTimestampOperatorTransformer;
import com.linkedin.coral.trino.rel2trino.transformers.GenericProjectTransformer;
Expand Down Expand Up @@ -44,7 +45,7 @@ public DataTypeDerivedSqlCallConverter(HiveMetastoreClient mscClient, SqlNode to
operatorTransformerList = SqlCallTransformers.of(new FromUtcTimestampOperatorTransformer(typeDerivationUtil),
new GenericProjectTransformer(typeDerivationUtil), new NamedStructToCastTransformer(typeDerivationUtil),
new ConcatOperatorTransformer(typeDerivationUtil), new SubstrOperatorTransformer(typeDerivationUtil),
new UnionSqlCallTransformer(typeDerivationUtil));
new CastOperatorTransformer(typeDerivationUtil), new UnionSqlCallTransformer(typeDerivationUtil));
}

@Override
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 All @@ -19,7 +19,6 @@
import org.apache.calcite.rel.rel2sql.RelToSqlConverter;
import org.apache.calcite.rel.type.RelDataType;
import org.apache.calcite.rel.type.RelDataTypeField;
import org.apache.calcite.rex.RexCall;
import org.apache.calcite.rex.RexFieldAccess;
import org.apache.calcite.rex.RexLiteral;
import org.apache.calcite.rex.RexNode;
Expand All @@ -35,7 +34,6 @@
import com.linkedin.coral.common.functions.FunctionFieldReferenceOperator;

import static com.google.common.base.Preconditions.*;
import static com.linkedin.coral.trino.rel2trino.Calcite2TrinoUDFConverter.convertRel;
import static com.linkedin.coral.trino.rel2trino.CoralTrinoConfigKeys.*;


Expand All @@ -48,7 +46,7 @@ public class RelToTrinoConverter extends RelToSqlConverter {
* wrapping in {@link RelToTrinoConverter#visit(Uncollect)}
* (2) Some internally registered UDFs which should not be converted, like `to_date`.
* If the value of key {@link CoralTrinoConfigKeys#AVOID_TRANSFORM_TO_DATE_UDF} is set to true, we don't transform `to_date` UDF
* in {@link com.linkedin.coral.trino.rel2trino.Calcite2TrinoUDFConverter.TrinoRexConverter#visitCall(RexCall)}
* in {@link com.linkedin.coral.trino.rel2trino.transformers.ToDateOperatorTransformer}
* (3) We need to adjust the return type for some functions using cast, since the converted Trino function's return type is not
* aligned with the Hive function's return type. For example, if the value of key {@link CoralTrinoConfigKeys#CAST_DATEADD_TO_STRING}
* is set to true, we would cast the converted RexCall to `varchar` type (date_add(xxx) -> cast(date_add(xxx) as varchar))
Expand Down Expand Up @@ -84,8 +82,7 @@ public RelToTrinoConverter(HiveMetastoreClient mscClient, Map<String, Boolean> c
* @return SQL string
*/
public String convert(RelNode relNode) {
RelNode rel = convertRel(relNode, configs);
SqlNode sqlNode = convertToSqlNode(rel);
SqlNode sqlNode = convertToSqlNode(relNode);

SqlNode sqlNodeWithRelDataTypeDerivedConversions =
sqlNode.accept(new DataTypeDerivedSqlCallConverter(_hiveMetastoreClient, sqlNode));
Expand Down
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 Down
Loading

0 comments on commit e09015e

Please sign in to comment.