Skip to content

Commit ad98949

Browse files
committed
Modify the overriden toSql
1 parent 5340dee commit ad98949

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

coral-hive/src/main/java/com/linkedin/coral/transformers/CoralRelToSqlNodeConverter.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -350,15 +350,14 @@ private SqlNode generateRightChildForSqlJoinWithLateralViews(BiRel e, Result rig
350350
}
351351

352352
/**
353-
* Override this method to handle the conversion for RelNode `f(x).y.z` where `f` is an UDF, which
353+
* Override this method to handle the conversion for RelNode `f(x).y.z` where `f` is an operator, which
354354
* returns a struct containing field `y`, `y` is also a struct containing field `z`.
355355
*
356356
* Calcite will convert this RelNode to a SqlIdentifier directly (check
357357
* {@link org.apache.calcite.rel.rel2sql.SqlImplementor.Context#toSql(RexProgram, RexNode)}),
358358
* which is not aligned with our expectation since we want to apply transformations on `f(x)` with
359359
* {@link com.linkedin.coral.common.transformers.SqlCallTransformer}. Therefore, we override this
360360
* method to convert `f(x)` to SqlCall, `.` to {@link com.linkedin.coral.common.functions.FunctionFieldReferenceOperator#DOT}
361-
* and `y.z` to SqlIdentifier.
362361
*/
363362
@Override
364363
public Context aliasContext(Map<String, RelDataType> aliases, boolean qualified) {
@@ -374,11 +373,14 @@ public SqlNode toSql(RexProgram program, RexNode rex) {
374373
accessNames.add(((RexFieldAccess) referencedExpr).getField().getName());
375374
referencedExpr = ((RexFieldAccess) referencedExpr).getReferenceExpr();
376375
}
377-
if (referencedExpr.getKind() == SqlKind.OTHER_FUNCTION) {
376+
if (referencedExpr.getKind() == SqlKind.OTHER_FUNCTION || referencedExpr.getKind() == SqlKind.CAST) {
378377
SqlNode functionCall = toSql(program, referencedExpr);
379378
Collections.reverse(accessNames);
380-
return FunctionFieldReferenceOperator.DOT.createCall(SqlParserPos.ZERO, functionCall,
381-
new SqlIdentifier(String.join(".", accessNames), POS));
379+
for (String accessName : accessNames) {
380+
functionCall = FunctionFieldReferenceOperator.DOT.createCall(SqlParserPos.ZERO, functionCall,
381+
new SqlIdentifier(accessName, POS));
382+
}
383+
return functionCall;
382384
}
383385
}
384386
return super.toSql(program, rex);

0 commit comments

Comments
 (0)