-
Notifications
You must be signed in to change notification settings - Fork 211
[Coral-Trino] Migrate FROM_UNIXTIME and FROM_UTC_TIMESTAMP #426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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)); | ||
| } | ||
| } |
| 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); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @findinpath If we make this check not simply based on the name so we make sure it only matches the Hive operator and not the Trino one, would we still need
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed offline. We can use
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you @wmoustafa for the hint and help in putting together the PR. |
||
| } | ||
|
|
||
| @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)); | ||
|
yiqiangin marked this conversation as resolved.
|
||
| } | ||
| return sqlCall; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.