-
Notifications
You must be signed in to change notification settings - Fork 189
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 FROM_UNIXTIME and FROM_UTC_TIMESTAMP (#426)
* initial commit for timestamp op migrations * refactored transformations * rename SqlShuttle class * enable test and rename var
- Loading branch information
Showing
8 changed files
with
241 additions
and
99 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
41 changes: 41 additions & 0 deletions
41
...ino/src/main/java/com/linkedin/coral/trino/rel2trino/DataTypeDerivedSqlCallConverter.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,41 @@ | ||
/** | ||
* Copyright 2022-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; | ||
|
||
import org.apache.calcite.sql.SqlCall; | ||
import org.apache.calcite.sql.SqlNode; | ||
import org.apache.calcite.sql.util.SqlShuttle; | ||
import org.apache.calcite.sql.validate.SqlValidator; | ||
|
||
import com.linkedin.coral.common.HiveMetastoreClient; | ||
import com.linkedin.coral.common.transformers.SqlCallTransformers; | ||
import com.linkedin.coral.common.utils.TypeDerivationUtil; | ||
import com.linkedin.coral.hive.hive2rel.HiveToRelConverter; | ||
import com.linkedin.coral.trino.rel2trino.transformers.FromUtcTimestampOperatorTransformer; | ||
|
||
|
||
/** | ||
* DataTypeDerivedSqlCallConverter transforms the sqlCalls | ||
* in the input SqlNode representation to be compatible with Trino engine. | ||
* The transformation may involve change in operator, reordering the operands | ||
* or even re-constructing the SqlNode. | ||
* | ||
* All the transformations performed as part of this shuttle require RelDataType derivation. | ||
*/ | ||
public class DataTypeDerivedSqlCallConverter extends SqlShuttle { | ||
private final SqlCallTransformers operatorTransformerList; | ||
|
||
public DataTypeDerivedSqlCallConverter(HiveMetastoreClient mscClient, SqlNode topSqlNode) { | ||
SqlValidator sqlValidator = new HiveToRelConverter(mscClient).getSqlValidator(); | ||
TypeDerivationUtil typeDerivationUtil = new TypeDerivationUtil(sqlValidator, topSqlNode); | ||
operatorTransformerList = SqlCallTransformers.of(new FromUtcTimestampOperatorTransformer(typeDerivationUtil)); | ||
} | ||
|
||
@Override | ||
public SqlNode visit(final SqlCall call) { | ||
return operatorTransformerList.apply((SqlCall) super.visit(call)); | ||
} | ||
} |
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
56 changes: 56 additions & 0 deletions
56
...java/com/linkedin/coral/trino/rel2trino/transformers/FromUnixtimeOperatorTransformer.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,56 @@ | ||
/** | ||
* 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.List; | ||
|
||
import org.apache.calcite.sql.SqlCall; | ||
import org.apache.calcite.sql.SqlLiteral; | ||
import org.apache.calcite.sql.SqlNode; | ||
import org.apache.calcite.sql.SqlOperator; | ||
import org.apache.calcite.sql.parser.SqlParserPos; | ||
|
||
import com.linkedin.coral.common.functions.FunctionReturnTypes; | ||
import com.linkedin.coral.common.transformers.SqlCallTransformer; | ||
|
||
import static org.apache.calcite.sql.type.ReturnTypes.*; | ||
import static org.apache.calcite.sql.type.SqlTypeName.*; | ||
|
||
|
||
/** | ||
* This transformer operates on SqlCalls with 'FROM_UNIXTIME(x)' Coral IR function | ||
* and transforms it to trino engine compatible function - FORMAT_DATETIME(FROM_UNIXTIME(x)). | ||
* For Example: | ||
* A SqlCall of the form: "FROM_UNIXTIME(10000)" is transformed to | ||
* "FORMAT_DATETIME(FROM_UNIXTIME(10000), 'yyyy-MM-dd HH:mm:ss')" | ||
*/ | ||
public class FromUnixtimeOperatorTransformer extends SqlCallTransformer { | ||
|
||
private static final String FORMAT_DATETIME = "format_datetime"; | ||
private static final String FROM_UNIXTIME = "from_unixtime"; | ||
|
||
@Override | ||
protected boolean condition(SqlCall sqlCall) { | ||
return sqlCall.getOperator().getName().equalsIgnoreCase(FROM_UNIXTIME); | ||
} | ||
|
||
@Override | ||
protected SqlCall transform(SqlCall sqlCall) { | ||
SqlOperator formatDatetimeOperator = createSqlOperator(FORMAT_DATETIME, FunctionReturnTypes.STRING); | ||
SqlOperator fromUnixtimeOperator = createSqlOperator(FROM_UNIXTIME, explicit(TIMESTAMP)); | ||
|
||
List<SqlNode> operands = sqlCall.getOperandList(); | ||
if (operands.size() == 1) { | ||
return formatDatetimeOperator.createCall(SqlParserPos.ZERO, | ||
fromUnixtimeOperator.createCall(SqlParserPos.ZERO, operands.get(0)), | ||
SqlLiteral.createCharString("yyyy-MM-dd HH:mm:ss", SqlParserPos.ZERO)); | ||
} else if (operands.size() == 2) { | ||
return formatDatetimeOperator.createCall(SqlParserPos.ZERO, | ||
fromUnixtimeOperator.createCall(SqlParserPos.ZERO, operands.get(0)), operands.get(1)); | ||
} | ||
return sqlCall; | ||
} | ||
} |
Oops, something went wrong.