-
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] Avoid accidental translation of Trino
from_unixtime
S…
…QL call
- Loading branch information
1 parent
a7e33e6
commit 07ac623
Showing
4 changed files
with
96 additions
and
6 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
88 changes: 88 additions & 0 deletions
88
...-hive/src/main/java/com/linkedin/coral/hive/hive2rel/functions/TimestampFromUnixtime.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,88 @@ | ||
/** | ||
* 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.hive.hive2rel.functions; | ||
|
||
import org.apache.calcite.sql.SqlCall; | ||
import org.apache.calcite.sql.SqlCallBinding; | ||
import org.apache.calcite.sql.SqlFunction; | ||
import org.apache.calcite.sql.SqlFunctionCategory; | ||
import org.apache.calcite.sql.SqlIdentifier; | ||
import org.apache.calcite.sql.SqlKind; | ||
import org.apache.calcite.sql.SqlLiteral; | ||
import org.apache.calcite.sql.SqlNode; | ||
import org.apache.calcite.sql.SqlOperandCountRange; | ||
import org.apache.calcite.sql.SqlUtil; | ||
import org.apache.calcite.sql.SqlWriter; | ||
import org.apache.calcite.sql.parser.SqlParserPos; | ||
import org.apache.calcite.sql.type.OperandTypes; | ||
import org.apache.calcite.sql.type.SqlOperandCountRanges; | ||
import org.apache.calcite.sql.type.SqlTypeFamily; | ||
|
||
import com.linkedin.coral.common.functions.FunctionReturnTypes; | ||
|
||
|
||
public class TimestampFromUnixtime extends SqlFunction { | ||
|
||
public static final TimestampFromUnixtime TIMESTAMP_FROM_UNIXTIME = new TimestampFromUnixtime(); | ||
|
||
public TimestampFromUnixtime() { | ||
super("from_unixtime", new SqlIdentifier("timestamp_from_unixtime", SqlParserPos.ZERO), SqlKind.OTHER_FUNCTION, | ||
FunctionReturnTypes.TIMESTAMP, null, null, null, SqlFunctionCategory.USER_DEFINED_FUNCTION); | ||
} | ||
|
||
@Override | ||
public SqlOperandCountRange getOperandCountRange() { | ||
return SqlOperandCountRanges.between(1, 3); | ||
} | ||
|
||
@Override | ||
public boolean checkOperandTypes(SqlCallBinding callBinding, boolean throwOnFailure) { | ||
final SqlNode firstOperand = callBinding.operand(0); | ||
if (!OperandTypes.family(SqlTypeFamily.NUMERIC).checkSingleOperandType(callBinding, firstOperand, 0, | ||
throwOnFailure)) { | ||
return false; | ||
} | ||
|
||
if (callBinding.getOperandCount() == 2) { | ||
final SqlNode secondOperand = callBinding.operand(1); | ||
if (!OperandTypes.family(SqlTypeFamily.STRING).checkSingleOperandType(callBinding, secondOperand, 0, | ||
throwOnFailure)) { | ||
return false; | ||
} | ||
} | ||
|
||
if (callBinding.getOperandCount() == 3) { | ||
final SqlNode secondOperand = callBinding.operand(1); | ||
final SqlNode thirdOperand = callBinding.operand(2); | ||
if (!OperandTypes.family(SqlTypeFamily.NUMERIC).checkSingleOperandType(callBinding, secondOperand, 0, | ||
throwOnFailure)) { | ||
return false; | ||
} | ||
if (!OperandTypes.family(SqlTypeFamily.NUMERIC).checkSingleOperandType(callBinding, thirdOperand, 0, | ||
throwOnFailure)) { | ||
return false; | ||
} | ||
} | ||
|
||
// unknown function signature | ||
return false; | ||
} | ||
|
||
@Override | ||
public void unparse(SqlWriter writer, SqlCall call, int leftPrec, int rightPrec) { | ||
SqlUtil.unparseSqlIdentifierSyntax(writer, new SqlIdentifier("from_unixtime", SqlParserPos.ZERO), true); | ||
final SqlWriter.Frame frame = writer.startList(SqlWriter.FrameTypeEnum.FUN_CALL, "(", ")"); | ||
final SqlLiteral quantifier = call.getFunctionQuantifier(); | ||
if (quantifier != null) { | ||
quantifier.unparse(writer, 0, 0); | ||
} | ||
for (SqlNode operand : call.getOperandList()) { | ||
writer.sep(","); | ||
operand.unparse(writer, 0, 0); | ||
} | ||
writer.endList(frame); | ||
} | ||
} |
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