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_r360340302
##########
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:
Now I understand what you mean. We should support sql like:
```
CREATE TYPE complex AS (
x integer,
y integer
);
create table test (name complex);
select (name).x from test t1 join test t2 using (name);
```
as PostgreSQL supports.
----------------------------------------------------------------
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