kramerul commented on code in PR #3664:
URL: https://github.com/apache/calcite/pull/3664#discussion_r1475710267


##########
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:
   I changed it accordingly.



-- 
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: commits-unsubscr...@calcite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to