YiwenWu commented on code in PR #3664:
URL: https://github.com/apache/calcite/pull/3664#discussion_r1475566245
##########
core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java:
##########
@@ -437,11 +437,43 @@ public Result visit(Filter e) {
final Result x = visitInput(e, 0, Clause.WHERE);
parseCorrelTable(e, x);
final Builder builder = x.builder(e);
+ if (input instanceof Join) {
+ final Context context = x.qualifiedContext();
+ final ImmutableList.Builder<SqlNode> selectList =
ImmutableList.builder();
+ // Fieldnames are unique since they are created by
SqlValidatorUtil.deriveJoinRowType()
+ final List<String> uniqueFieldNames =
input.getRowType().getFieldNames();
+ boolean selectListRequired = false;
+ for (int i = 0; i < context.fieldCount; i++) {
+ final SqlNode field = context.field(i);
+ final String fieldName = uniqueFieldNames.get(i);
+ if (fieldWasRenamedByJoinDueToNameClash(field, fieldName)) {
+ selectListRequired = true;
+ }
+ selectList.add(
+ SqlStdOperatorTable.AS.createCall(POS, field,
+ new SqlIdentifier(fieldName, POS)));
+ }
+ if (selectListRequired) {
+ builder.setSelect(new SqlNodeList(selectList.build(), POS));
+ }
+ }
builder.setWhere(builder.context.toSql(null, e.getCondition()));
return builder.result();
}
}
+ private static boolean fieldWasRenamedByJoinDueToNameClash(SqlNode field,
String fieldName) {
+ if (!(field instanceof SqlIdentifier)) {
Review Comment:
context(AliasContext) field is only allowed to return SqlIdentifier.
and Is there any other simpler way to determine whether rename is needed?
can we identify the Duplicate name by only
Result.qualifiedContext().fieldList()
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]