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


##########
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 (fieldRequiresAlias(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 fieldRequiresAlias(SqlNode field, String fieldName) {
+    if (!(field instanceof SqlIdentifier)) {
+      return true;
+    }
+    SqlIdentifier identifier = (SqlIdentifier) field;
+    if (identifier.isStar()) {
+      return true;
+    }
+    List<String> names = identifier.names;
+    return !names.get(names.size() - 1).equals(fieldName);

Review Comment:
   I renamed this function to `fieldWasRenamedByJoinDueToNameClash`



-- 
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