-
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] Cast char fields, if necessary, to varchar type in the …
…set operation In case of dealing with Hive views which make use of set operation (e.g. UNION) ensure that the `char` fields from the inner SELECT statements have the same type as the output field types of the set operation. Due to wrong coercion between `varchar` and `char` in Trino, as described in trinodb/trino#9031 , a work-around needs to be applied in case of translating Hive views which contain a UNION dealing with char and varchar types. The work-around consists in the explicit cast of the field having char type towards varchar type corresponding of the set operation output type.
- Loading branch information
1 parent
8c7bd53
commit 601a398
Showing
6 changed files
with
262 additions
and
7 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
165 changes: 165 additions & 0 deletions
165
...rc/main/java/com/linkedin/coral/trino/rel2trino/transformers/UnionSqlCallTransformer.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,165 @@ | ||
/** | ||
* 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.List; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import org.apache.calcite.rel.type.RelDataType; | ||
import org.apache.calcite.sql.SqlBasicTypeNameSpec; | ||
import org.apache.calcite.sql.SqlCall; | ||
import org.apache.calcite.sql.SqlCallBinding; | ||
import org.apache.calcite.sql.SqlDataTypeSpec; | ||
import org.apache.calcite.sql.SqlIdentifier; | ||
import org.apache.calcite.sql.SqlKind; | ||
import org.apache.calcite.sql.SqlNode; | ||
import org.apache.calcite.sql.SqlNodeList; | ||
import org.apache.calcite.sql.SqlSelect; | ||
import org.apache.calcite.sql.SqlTypeNameSpec; | ||
import org.apache.calcite.sql.fun.SqlCastFunction; | ||
import org.apache.calcite.sql.fun.SqlStdOperatorTable; | ||
import org.apache.calcite.sql.parser.SqlParserPos; | ||
import org.apache.calcite.sql.type.SqlTypeName; | ||
import org.apache.calcite.sql.validate.SqlValidator; | ||
import org.apache.calcite.sql.validate.SqlValidatorScope; | ||
|
||
import com.linkedin.coral.common.transformers.SqlCallTransformer; | ||
import com.linkedin.coral.common.utils.TypeDerivationUtil; | ||
|
||
import static org.apache.calcite.rel.rel2sql.SqlImplementor.POS; | ||
import static org.apache.calcite.sql.parser.SqlParserPos.ZERO; | ||
|
||
|
||
/** | ||
* This transformer class is used to adapt expressions in | ||
* set statements (e.g.: `UNION`, `INTERSECT`, `MINUS`) | ||
* in case that the branches of the set statement contain fields in | ||
* char family which have different types. | ||
* The `char` fields which are differing from the expected `varchar` output | ||
* of the set statement will be adapted through an explicit `CAST` to the `varchar` type. | ||
* | ||
* @see <a href="Change char/varchar coercion in Trino">https://github.com/trinodb/trino/issues/9031</a> | ||
*/ | ||
public class UnionSqlCallTransformer extends SqlCallTransformer { | ||
|
||
public UnionSqlCallTransformer(TypeDerivationUtil typeDerivationUtil) { | ||
super(typeDerivationUtil); | ||
} | ||
|
||
@Override | ||
protected boolean condition(SqlCall sqlCall) { | ||
return sqlCall.getOperator().kind == SqlKind.UNION || sqlCall.getOperator().kind == SqlKind.INTERSECT | ||
|| sqlCall.getOperator().kind == SqlKind.MINUS; | ||
} | ||
|
||
@Override | ||
protected SqlCall transform(SqlCall sqlCall) { | ||
List<SqlNode> operandsList = sqlCall.getOperandList(); | ||
List<List<Optional<RelDataType>>> columnsTypesLists = new ArrayList<>(); | ||
for (SqlNode operand : operandsList) { | ||
if (operand.getKind() == SqlKind.SELECT && ((SqlSelect) operand).getSelectList() != null) { | ||
SqlSelect select = (SqlSelect) operand; | ||
List<SqlNode> selectNodes = select.getSelectList().getList(); | ||
if (columnsTypesLists.isEmpty()) { | ||
for (int i = 0; i < selectNodes.size(); i++) { | ||
columnsTypesLists.add(new ArrayList<>()); | ||
} | ||
} | ||
|
||
for (int i = 0; i < selectNodes.size(); i++) { | ||
SqlNode sqlNode = selectNodes.get(i); | ||
if (sqlNode.getKind() == SqlKind.IDENTIFIER && ((SqlIdentifier) sqlNode).isStar()) { | ||
// The type derivation of * on the SqlNode layer is not supported now. | ||
return sqlCall; | ||
} | ||
|
||
Optional<RelDataType> selectNodeDataType; | ||
try { | ||
selectNodeDataType = Optional.of(deriveRelDatatype(sqlNode)); | ||
} catch (RuntimeException e) { | ||
// The type derivation may fail for complex expressions | ||
selectNodeDataType = Optional.empty(); | ||
} | ||
columnsTypesLists.get(i).add(selectNodeDataType); | ||
} | ||
} else { | ||
return sqlCall; | ||
} | ||
} | ||
|
||
List<SqlNode> updatedOperands = new ArrayList<>(sqlCall.getOperandList().size()); | ||
for (SqlNode operand : operandsList) { | ||
SqlSelect select = (SqlSelect) operand; | ||
|
||
List<SqlNode> selectNodes = select.getSelectList().getList(); | ||
|
||
List<SqlNode> rewrittenSelectNodes = new ArrayList<>(); | ||
boolean useRewrittenSelectNodes = false; | ||
for (int i = 0; i < selectNodes.size(); i++) { | ||
|
||
if (columnsTypesLists.get(i).stream().anyMatch(columnType -> !columnType.isPresent())) { | ||
// Couldn't determine the type for all the expressions | ||
continue; | ||
} | ||
RelDataType inferredColumnType = | ||
leastRestrictive(columnsTypesLists.get(i).stream().map(Optional::get).collect(Collectors.toList())); | ||
|
||
SqlNode selectNode = selectNodes.get(i); | ||
RelDataType selectNodeDataType = deriveRelDatatype(selectNodes.get(i)); | ||
if (!selectNodeDataType.equals(inferredColumnType) && selectNodeDataType.getSqlTypeName() == SqlTypeName.CHAR | ||
&& inferredColumnType.getSqlTypeName() == SqlTypeName.VARCHAR) { | ||
// Work-around for the Trino limitation in dealing UNION statements between `char` and `varchar`. | ||
// See https://github.com/trinodb/trino/issues/9031 | ||
SqlNode rewrittenSelectNode = castNode(selectNode, inferredColumnType); | ||
if (!useRewrittenSelectNodes) { | ||
rewrittenSelectNodes.addAll(selectNodes.subList(0, i)); | ||
useRewrittenSelectNodes = true; | ||
} | ||
rewrittenSelectNodes.add(rewrittenSelectNode); | ||
} else if (useRewrittenSelectNodes) { | ||
rewrittenSelectNodes.add(selectNode); | ||
} | ||
} | ||
|
||
if (useRewrittenSelectNodes) { | ||
select.setSelectList(new SqlNodeList(rewrittenSelectNodes, SqlParserPos.ZERO)); | ||
} | ||
updatedOperands.add(operand); | ||
} | ||
return sqlCall.getOperator().createCall(POS, updatedOperands); | ||
} | ||
|
||
private SqlNode castNode(SqlNode node, RelDataType type) { | ||
if (node.getKind() == SqlKind.AS) { | ||
SqlNode expression = ((SqlCall) node).getOperandList().get(0); | ||
SqlIdentifier identifier = (SqlIdentifier) ((SqlCall) node).getOperandList().get(1); | ||
return SqlStdOperatorTable.AS.createCall(POS, new SqlCastFunction() { | ||
@Override | ||
public RelDataType deriveType(SqlValidator validator, SqlValidatorScope scope, SqlCall call) { | ||
SqlCallBinding opBinding = new SqlCallBinding(validator, scope, call); | ||
return inferReturnType(opBinding); | ||
} | ||
}.createCall(ZERO, expression, getSqlDataTypeSpecForCasting(type)), identifier); | ||
} else { | ||
// If there's no existing alias, just do the cast | ||
return new SqlCastFunction() { | ||
@Override | ||
public RelDataType deriveType(SqlValidator validator, SqlValidatorScope scope, SqlCall call) { | ||
SqlCallBinding opBinding = new SqlCallBinding(validator, scope, call); | ||
return inferReturnType(opBinding); | ||
} | ||
}.createCall(ZERO, node, getSqlDataTypeSpecForCasting(type)); | ||
} | ||
} | ||
|
||
private static SqlDataTypeSpec getSqlDataTypeSpecForCasting(RelDataType relDataType) { | ||
final SqlTypeNameSpec typeNameSpec = new SqlBasicTypeNameSpec(relDataType.getSqlTypeName(), | ||
relDataType.getPrecision(), relDataType.getScale(), null, ZERO); | ||
return new SqlDataTypeSpec(typeNameSpec, ZERO); | ||
} | ||
} |
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