-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Coral-Trino] Migrate SUBSTR() operator from RexShuttle to SqlShuttle (…
…#432) * [Coral-Trino] Initial commit for migrating SUBSTR() operator * rebase * register Coral IR functions * add UTs
- Loading branch information
Showing
5 changed files
with
118 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
.../main/java/com/linkedin/coral/trino/rel2trino/transformers/SubstrOperatorTransformer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* 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 java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.apache.calcite.rel.type.RelDataType; | ||
import org.apache.calcite.sql.SqlBasicTypeNameSpec; | ||
import org.apache.calcite.sql.SqlCall; | ||
import org.apache.calcite.sql.SqlDataTypeSpec; | ||
import org.apache.calcite.sql.SqlNode; | ||
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.HiveTypeSystem; | ||
import com.linkedin.coral.common.transformers.SqlCallTransformer; | ||
import com.linkedin.coral.common.utils.TypeDerivationUtil; | ||
|
||
import static org.apache.calcite.sql.parser.SqlParserPos.*; | ||
import static org.apache.calcite.sql.type.SqlTypeName.*; | ||
|
||
|
||
/** | ||
* This class implements the transformation of SqlCalls with Coral IR function `SUBSTR` | ||
* to their corresponding Trino-compatible versions. | ||
* | ||
* For example: | ||
* Given table: | ||
* t1(int_col INTEGER, time_col timestamp) | ||
* and a Coral IR SqlCall: | ||
* `SUBSTR(time_col, 12, 8)` | ||
* | ||
* The transformed SqlCall would be: | ||
* `SUBSTR(CAST(time_col AS VARCHAR(65535)), 12, 8)` | ||
*/ | ||
public class SubstrOperatorTransformer extends SqlCallTransformer { | ||
|
||
private static final int DEFAULT_VARCHAR_PRECISION = new HiveTypeSystem().getDefaultPrecision(SqlTypeName.VARCHAR); | ||
private static final String SUBSTR_OPERATOR_NAME = "substr"; | ||
private static final Set<SqlTypeName> OPERAND_SQL_TYPE_NAMES = | ||
new HashSet<>(Arrays.asList(SqlTypeName.VARCHAR, SqlTypeName.CHAR)); | ||
private static final SqlDataTypeSpec VARCHAR_SQL_DATA_TYPE_SPEC = | ||
new SqlDataTypeSpec(new SqlBasicTypeNameSpec(SqlTypeName.VARCHAR, DEFAULT_VARCHAR_PRECISION, ZERO), ZERO); | ||
|
||
public SubstrOperatorTransformer(TypeDerivationUtil typeDerivationUtil) { | ||
super(typeDerivationUtil); | ||
} | ||
|
||
@Override | ||
protected boolean condition(SqlCall sqlCall) { | ||
return sqlCall.getOperator().getName().equalsIgnoreCase(SUBSTR_OPERATOR_NAME); | ||
} | ||
|
||
@Override | ||
protected SqlCall transform(SqlCall sqlCall) { | ||
List<SqlNode> operands = sqlCall.getOperandList(); | ||
RelDataType relDataTypeOfOperand = deriveRelDatatype(operands.get(0)); | ||
|
||
// Coral IR accepts a byte array or String as an input for the `substr` operator. | ||
// This behavior is emulated by casting non-String input to String in this transformer | ||
// https://cwiki.apache.org/confluence/display/hive/languagemanual+udf | ||
if (!OPERAND_SQL_TYPE_NAMES.contains(relDataTypeOfOperand.getSqlTypeName())) { | ||
List<SqlNode> modifiedOperands = new ArrayList<>(); | ||
|
||
modifiedOperands.add(SqlStdOperatorTable.CAST.createCall(ZERO, operands.get(0), VARCHAR_SQL_DATA_TYPE_SPEC)); | ||
modifiedOperands.addAll(operands.subList(1, operands.size())); | ||
|
||
return sqlCall.getOperator().createCall(SqlParserPos.ZERO, modifiedOperands); | ||
} | ||
return sqlCall; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters