chunweilei commented on a change in pull request #1655: [CALCITE-3387] Query 
with GROUP BY and JOIN ... USING wrongly fails with "Column DEPTNO is 
ambiguous" error
URL: https://github.com/apache/calcite/pull/1655#discussion_r360357708
 
 

 ##########
 File path: 
core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java
 ##########
 @@ -484,6 +484,65 @@ private boolean expandSelectItem(
     return false;
   }
 
+  private static SqlNode expandExprFromJoin(SqlJoin join, SqlIdentifier 
identifier,
+      SelectScope scope) {
+    if (join.getConditionType() != JoinConditionType.USING) {
+      return identifier;
+    }
+
+    for (SqlNode node : (SqlNodeList) join.getCondition()) {
+      final String name = ((SqlIdentifier) node).getSimple();
+      if (identifier.getSimple().equals(name)) {
+        final List<SqlNode> qualifiedNode = new ArrayList<>();
+        for (ScopeChild child : scope.children) {
+          if (child.namespace.getRowType()
+              .getFieldNames().indexOf(name) >= 0) {
+            final SqlIdentifier exp =
+                new SqlIdentifier(
+                    ImmutableList.of(child.name, name),
+                    identifier.getParserPosition());
+            qualifiedNode.add(exp);
+          }
+        }
+
+        assert qualifiedNode.size() == 2;
+        final SqlNode finalNode =
+            SqlStdOperatorTable.AS.createCall(SqlParserPos.ZERO,
+                SqlStdOperatorTable.COALESCE.createCall(SqlParserPos.ZERO,
+                    qualifiedNode.get(0),
+                    qualifiedNode.get(1)),
+                new SqlIdentifier(name, SqlParserPos.ZERO));
+        return finalNode;
+      }
+    }
+
+    final SqlNode node = join.getLeft();
+    if (node instanceof SqlJoin) {
+      return expandExprFromJoin((SqlJoin) node, identifier, scope);
+    } else {
+      return identifier;
+    }
+  }
+
+  private static SqlNode expandCommonColumn(SqlSelect sqlSelect,
+      SqlNode selectItem, SelectScope scope) {
+    if (!(selectItem instanceof SqlIdentifier)) {
+      return selectItem;
+    }
+
+    final SqlIdentifier identifier = (SqlIdentifier) selectItem;
+    if (!identifier.isSimple()) {
 
 Review comment:
   I have added a test about it.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to