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


##########
core/src/main/java/org/apache/calcite/rel/rel2sql/RelToSqlConverter.java:
##########
@@ -257,6 +257,48 @@ public Result visit(Join e) {
     return result(join, leftResult, rightResult);
   }
 
+  private Result maybeFixRenamedFields(Result rightResult, Join e) {
+    Frame last = stack.peekLast();
+    if (last != null && last.r instanceof TableModify) {
+      return rightResult;
+    }
+    List<String> rightFieldNames = e.getRight().getRowType().getFieldNames();
+    List<String> fieldNames = e.getRowType().getFieldNames();
+    int offset = e.getLeft().getRowType().getFieldCount();
+    boolean hasFieldNameCollision = false;
+    for (int i = 0; i < rightFieldNames.size(); i++) {
+      if (!rightFieldNames.get(i).equals(fieldNames.get(offset + i))) {
+        hasFieldNameCollision = true;
+      }
+    }
+    if (!hasFieldNameCollision) {
+      return rightResult;
+    }
+    Builder builder = rightResult.builder(e);
+    List<SqlNode> oldSelectList = new ArrayList<>();
+    if (builder.select.getSelectList() == SqlNodeList.SINGLETON_STAR) {
+      for (int i = 0; i < rightFieldNames.size(); i++) {
+        oldSelectList.add(new SqlIdentifier(rightFieldNames.get(i), POS));
+      }
+    } else {
+      for (SqlNode node : builder.select.getSelectList().getList()) {
+        oldSelectList.add(requireNonNull(node, "node"));
+      }
+    }
+    List<SqlNode> selectList = new ArrayList<>();
+    for (int i = 0; i < rightFieldNames.size(); i++) {
+      SqlNode column = oldSelectList.get(i);
+      if (!rightFieldNames.get(i).equals(fieldNames.get(offset + i))) {
+        column =
+            SqlStdOperatorTable.AS.createCall(POS, SqlUtil.stripAs(column),

Review Comment:
   The uniqueness is currently guaranteed by the function 
`SqlValidatorUtil.deriveJoinRowType` which creates the unique field names for 
the `Join` relation. But this is not really visible here. Therefore, I will add 
a check.



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