Skip to content

Commit

Permalink
Modify the overriden toSql
Browse files Browse the repository at this point in the history
  • Loading branch information
ljfgem committed Feb 7, 2023
1 parent 394979a commit cc26200
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,14 @@ private SqlNode generateRightChildForSqlJoinWithLateralViews(BiRel e, Result rig
}

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

0 comments on commit cc26200

Please sign in to comment.