You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@calcite.apache.org by GitBox <gi...@apache.org> on 2020/09/03 18:45:54 UTC
[GitHub] [calcite] jamesstarr opened a new pull request #2138: CALCITE-4210 Initial tests
jamesstarr opened a new pull request #2138:
URL: https://github.com/apache/calcite/pull/2138
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [calcite] danny0405 commented on a change in pull request #2138: CALCITE-4210 Replaying subquery from ON clause on to right node
Posted by GitBox <gi...@apache.org>.
danny0405 commented on a change in pull request #2138:
URL: https://github.com/apache/calcite/pull/2138#discussion_r497193375
##########
File path: core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
##########
@@ -2807,34 +2756,109 @@ private boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb) {
return Collections.emptyList();
}
- private RexNode convertJoinCondition(Blackboard bb,
+ private void convertJoin(Blackboard bb, SqlJoin join) {
+ final SqlValidatorScope scope = validator.getJoinScope(join);
+ final Blackboard fromBlackboard = createBlackboard(scope, null, false);
+ SqlNode left = join.getLeft();
+ SqlNode right = join.getRight();
+ final SqlValidatorScope leftScope =
+ Util.first(validator.getJoinScope(left),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard leftBlackboard =
+ createBlackboard(leftScope, null, false);
+ final SqlValidatorScope rightScope =
+ Util.first(validator.getJoinScope(right),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard rightBlackboard =
+ createBlackboard(rightScope, null, false);
+ convertFrom(leftBlackboard, left);
+ final RelNode leftRel = leftBlackboard.root;
+ convertFrom(rightBlackboard, right);
+ final RelNode tempRightRel = rightBlackboard.root;
+
+ final JoinConditionType conditionType = join.getConditionType();
+ final RexNode condition;
+ final RelNode rightRel;
+ if (join.isNatural()) {
+ condition = createNaturalJoinCondition(validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.NONE) {
+ condition = rexBuilder.makeLiteral(true);
+ rightRel = tempRightRel;
Review comment:
`natural should be added as a conditionType` i think it should.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [calcite] danny0405 commented on a change in pull request #2138: CALCITE-4210 Replaying subquery from ON clause on to right node
Posted by GitBox <gi...@apache.org>.
danny0405 commented on a change in pull request #2138:
URL: https://github.com/apache/calcite/pull/2138#discussion_r496432135
##########
File path: core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
##########
@@ -2807,34 +2756,109 @@ private boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb) {
return Collections.emptyList();
}
- private RexNode convertJoinCondition(Blackboard bb,
+ private void convertJoin(Blackboard bb, SqlJoin join) {
+ final SqlValidatorScope scope = validator.getJoinScope(join);
+ final Blackboard fromBlackboard = createBlackboard(scope, null, false);
+ SqlNode left = join.getLeft();
+ SqlNode right = join.getRight();
+ final SqlValidatorScope leftScope =
+ Util.first(validator.getJoinScope(left),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard leftBlackboard =
+ createBlackboard(leftScope, null, false);
+ final SqlValidatorScope rightScope =
+ Util.first(validator.getJoinScope(right),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard rightBlackboard =
+ createBlackboard(rightScope, null, false);
+ convertFrom(leftBlackboard, left);
+ final RelNode leftRel = leftBlackboard.root;
+ convertFrom(rightBlackboard, right);
+ final RelNode tempRightRel = rightBlackboard.root;
+
+ final JoinConditionType conditionType = join.getConditionType();
+ final RexNode condition;
+ final RelNode rightRel;
+ if (join.isNatural()) {
+ condition = createNaturalJoinCondition(validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.NONE) {
+ condition = rexBuilder.makeLiteral(true);
+ rightRel = tempRightRel;
Review comment:
i would prefer switch.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [calcite] danny0405 commented on a change in pull request #2138: CALCITE-4210 Replaying subquery from ON clause on to right node
Posted by GitBox <gi...@apache.org>.
danny0405 commented on a change in pull request #2138:
URL: https://github.com/apache/calcite/pull/2138#discussion_r496340344
##########
File path: core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
##########
@@ -2807,34 +2756,109 @@ private boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb) {
return Collections.emptyList();
}
- private RexNode convertJoinCondition(Blackboard bb,
+ private void convertJoin(Blackboard bb, SqlJoin join) {
+ final SqlValidatorScope scope = validator.getJoinScope(join);
+ final Blackboard fromBlackboard = createBlackboard(scope, null, false);
+ SqlNode left = join.getLeft();
+ SqlNode right = join.getRight();
+ final SqlValidatorScope leftScope =
+ Util.first(validator.getJoinScope(left),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard leftBlackboard =
+ createBlackboard(leftScope, null, false);
+ final SqlValidatorScope rightScope =
+ Util.first(validator.getJoinScope(right),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard rightBlackboard =
+ createBlackboard(rightScope, null, false);
+ convertFrom(leftBlackboard, left);
+ final RelNode leftRel = leftBlackboard.root;
+ convertFrom(rightBlackboard, right);
+ final RelNode tempRightRel = rightBlackboard.root;
+
+ final JoinConditionType conditionType = join.getConditionType();
+ final RexNode condition;
+ final RelNode rightRel;
+ if (join.isNatural()) {
+ condition = createNaturalJoinCondition(validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.NONE) {
+ condition = rexBuilder.makeLiteral(true);
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.USING) {
+ condition = createJoinUsingCondition(join,
+ validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.ON) {
+ Pair<RexNode, RelNode> conditionAndRightNode = createJoinOnCondition(fromBlackboard,
+ join,
+ leftRel,
+ tempRightRel);
+ condition = conditionAndRightNode.left;
+ rightRel = conditionAndRightNode.right;
+ } else {
+ throw Util.unexpected(conditionType);
+ }
+ final RelNode joinRel = createJoin(
+ fromBlackboard,
+ leftRel,
+ rightRel,
+ condition,
+ convertJoinType(join.getJoinType()));
+ bb.setRoot(joinRel, false);
+ }
+
+ private RexNode createNaturalJoinCondition(SqlValidatorNamespace leftNamespace,
+ SqlValidatorNamespace rightNamespace) {
+ final List<String> columnList =
+ SqlValidatorUtil.deriveNaturalJoinColumnList(
+ catalogReader.nameMatcher(),
+ leftNamespace.getRowType(),
+ rightNamespace.getRowType());
+ return convertUsing(leftNamespace, rightNamespace, columnList);
+ }
+
+ private RexNode createJoinUsingCondition(SqlJoin join,
SqlValidatorNamespace leftNamespace,
- SqlValidatorNamespace rightNamespace,
- SqlNode condition,
- JoinConditionType conditionType,
+ SqlValidatorNamespace rightNamespace) {
+ SqlNode condition = join.getCondition();
+
+ final SqlNodeList list = (SqlNodeList) condition;
+ final List<String> nameList = new ArrayList<>();
+ for (SqlNode columnName : list) {
+ final SqlIdentifier id = (SqlIdentifier) columnName;
+ String name = id.getSimple();
+ nameList.add(name);
+ }
+ return convertUsing(leftNamespace, rightNamespace, nameList);
+ }
+
+ private Pair<RexNode, RelNode> createJoinOnCondition(Blackboard bb,
+ SqlJoin join,
RelNode leftRel,
RelNode rightRel) {
- if (condition == null) {
- return rexBuilder.makeLiteral(true);
- }
+ SqlNode condition = join.getCondition();
+
bb.setRoot(ImmutableList.of(leftRel, rightRel));
replaceSubQueries(bb, condition, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
- switch (conditionType) {
- case ON:
- bb.setRoot(ImmutableList.of(leftRel, rightRel));
- return bb.convertExpression(condition);
- case USING:
- final SqlNodeList list = (SqlNodeList) condition;
- final List<String> nameList = new ArrayList<>();
- for (SqlNode columnName : list) {
- final SqlIdentifier id = (SqlIdentifier) columnName;
- String name = id.getSimple();
- nameList.add(name);
- }
- return convertUsing(leftNamespace, rightNamespace, nameList);
- default:
- throw Util.unexpected(conditionType);
- }
+ final RelNode newRightRel;
+ if (bb.root == null) {
+ newRightRel = rightRel;
+ } else {
+ bb.setRoot(rightRel, false);
+ List<BlackboardRegisterArgs> blackboardRegisterArgsList =
Review comment:
What multiplicity issue, can you elaborate for that ?
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [calcite] jamesstarr commented on a change in pull request #2138: CALCITE-4210 Replaying subquery from ON clause on to right node
Posted by GitBox <gi...@apache.org>.
jamesstarr commented on a change in pull request #2138:
URL: https://github.com/apache/calcite/pull/2138#discussion_r496347171
##########
File path: core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
##########
@@ -2807,34 +2756,109 @@ private boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb) {
return Collections.emptyList();
}
- private RexNode convertJoinCondition(Blackboard bb,
+ private void convertJoin(Blackboard bb, SqlJoin join) {
+ final SqlValidatorScope scope = validator.getJoinScope(join);
+ final Blackboard fromBlackboard = createBlackboard(scope, null, false);
+ SqlNode left = join.getLeft();
+ SqlNode right = join.getRight();
+ final SqlValidatorScope leftScope =
+ Util.first(validator.getJoinScope(left),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard leftBlackboard =
+ createBlackboard(leftScope, null, false);
+ final SqlValidatorScope rightScope =
+ Util.first(validator.getJoinScope(right),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard rightBlackboard =
+ createBlackboard(rightScope, null, false);
+ convertFrom(leftBlackboard, left);
+ final RelNode leftRel = leftBlackboard.root;
+ convertFrom(rightBlackboard, right);
+ final RelNode tempRightRel = rightBlackboard.root;
+
+ final JoinConditionType conditionType = join.getConditionType();
+ final RexNode condition;
+ final RelNode rightRel;
+ if (join.isNatural()) {
+ condition = createNaturalJoinCondition(validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.NONE) {
+ condition = rexBuilder.makeLiteral(true);
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.USING) {
+ condition = createJoinUsingCondition(join,
+ validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.ON) {
+ Pair<RexNode, RelNode> conditionAndRightNode = createJoinOnCondition(fromBlackboard,
+ join,
+ leftRel,
+ tempRightRel);
+ condition = conditionAndRightNode.left;
+ rightRel = conditionAndRightNode.right;
+ } else {
+ throw Util.unexpected(conditionType);
+ }
+ final RelNode joinRel = createJoin(
+ fromBlackboard,
+ leftRel,
+ rightRel,
+ condition,
+ convertJoinType(join.getJoinType()));
+ bb.setRoot(joinRel, false);
+ }
+
+ private RexNode createNaturalJoinCondition(SqlValidatorNamespace leftNamespace,
+ SqlValidatorNamespace rightNamespace) {
+ final List<String> columnList =
+ SqlValidatorUtil.deriveNaturalJoinColumnList(
+ catalogReader.nameMatcher(),
+ leftNamespace.getRowType(),
+ rightNamespace.getRowType());
+ return convertUsing(leftNamespace, rightNamespace, columnList);
+ }
+
+ private RexNode createJoinUsingCondition(SqlJoin join,
SqlValidatorNamespace leftNamespace,
- SqlValidatorNamespace rightNamespace,
- SqlNode condition,
- JoinConditionType conditionType,
+ SqlValidatorNamespace rightNamespace) {
+ SqlNode condition = join.getCondition();
+
+ final SqlNodeList list = (SqlNodeList) condition;
+ final List<String> nameList = new ArrayList<>();
+ for (SqlNode columnName : list) {
+ final SqlIdentifier id = (SqlIdentifier) columnName;
+ String name = id.getSimple();
+ nameList.add(name);
+ }
+ return convertUsing(leftNamespace, rightNamespace, nameList);
+ }
+
+ private Pair<RexNode, RelNode> createJoinOnCondition(Blackboard bb,
+ SqlJoin join,
RelNode leftRel,
RelNode rightRel) {
- if (condition == null) {
- return rexBuilder.makeLiteral(true);
- }
+ SqlNode condition = join.getCondition();
+
bb.setRoot(ImmutableList.of(leftRel, rightRel));
replaceSubQueries(bb, condition, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
- switch (conditionType) {
- case ON:
- bb.setRoot(ImmutableList.of(leftRel, rightRel));
- return bb.convertExpression(condition);
- case USING:
- final SqlNodeList list = (SqlNodeList) condition;
- final List<String> nameList = new ArrayList<>();
- for (SqlNode columnName : list) {
- final SqlIdentifier id = (SqlIdentifier) columnName;
- String name = id.getSimple();
- nameList.add(name);
- }
- return convertUsing(leftNamespace, rightNamespace, nameList);
- default:
- throw Util.unexpected(conditionType);
- }
+ final RelNode newRightRel;
+ if (bb.root == null) {
+ newRightRel = rightRel;
+ } else {
+ bb.setRoot(rightRel, false);
+ List<BlackboardRegisterArgs> blackboardRegisterArgsList =
Review comment:
If another part of the clause would filter out the particular rows, then you would end up with extra rows.
For instance give query:
`
SELECT * FROM left_side
LEFT JOIN right_side ON exist(sub_query) AND some_other_clause
`
If the sub query cause multiple rows to produced on the left side, then some_other_clause cause those rows to not joined, you will end up with repeated rows. This is why a full outer join can produce erroneous results. The solution would be to generate a decorrelated query that joins on all columns columns used in the ON clause regardless if they are used in the subquery.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [calcite] jamesstarr commented on a change in pull request #2138: CALCITE-4210 Replaying subquery from ON clause on to right node
Posted by GitBox <gi...@apache.org>.
jamesstarr commented on a change in pull request #2138:
URL: https://github.com/apache/calcite/pull/2138#discussion_r496934988
##########
File path: core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
##########
@@ -2807,34 +2756,109 @@ private boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb) {
return Collections.emptyList();
}
- private RexNode convertJoinCondition(Blackboard bb,
+ private void convertJoin(Blackboard bb, SqlJoin join) {
+ final SqlValidatorScope scope = validator.getJoinScope(join);
+ final Blackboard fromBlackboard = createBlackboard(scope, null, false);
+ SqlNode left = join.getLeft();
+ SqlNode right = join.getRight();
+ final SqlValidatorScope leftScope =
+ Util.first(validator.getJoinScope(left),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard leftBlackboard =
+ createBlackboard(leftScope, null, false);
+ final SqlValidatorScope rightScope =
+ Util.first(validator.getJoinScope(right),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard rightBlackboard =
+ createBlackboard(rightScope, null, false);
+ convertFrom(leftBlackboard, left);
+ final RelNode leftRel = leftBlackboard.root;
+ convertFrom(rightBlackboard, right);
+ final RelNode tempRightRel = rightBlackboard.root;
+
+ final JoinConditionType conditionType = join.getConditionType();
+ final RexNode condition;
+ final RelNode rightRel;
+ if (join.isNatural()) {
+ condition = createNaturalJoinCondition(validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.NONE) {
+ condition = rexBuilder.makeLiteral(true);
+ rightRel = tempRightRel;
Review comment:
I changed it to the switch, but I if calcite standard insist on splitting conditional to use a case statement when ever possible, then, natural should be added as a conditionType, which again flatten the control structure.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [calcite] jamesstarr commented on a change in pull request #2138: CALCITE-4210 Replaying subquery from ON clause on to right node
Posted by GitBox <gi...@apache.org>.
jamesstarr commented on a change in pull request #2138:
URL: https://github.com/apache/calcite/pull/2138#discussion_r496339094
##########
File path: core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
##########
@@ -2807,34 +2756,109 @@ private boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb) {
return Collections.emptyList();
}
- private RexNode convertJoinCondition(Blackboard bb,
+ private void convertJoin(Blackboard bb, SqlJoin join) {
+ final SqlValidatorScope scope = validator.getJoinScope(join);
+ final Blackboard fromBlackboard = createBlackboard(scope, null, false);
+ SqlNode left = join.getLeft();
+ SqlNode right = join.getRight();
+ final SqlValidatorScope leftScope =
+ Util.first(validator.getJoinScope(left),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard leftBlackboard =
+ createBlackboard(leftScope, null, false);
+ final SqlValidatorScope rightScope =
+ Util.first(validator.getJoinScope(right),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard rightBlackboard =
+ createBlackboard(rightScope, null, false);
+ convertFrom(leftBlackboard, left);
+ final RelNode leftRel = leftBlackboard.root;
+ convertFrom(rightBlackboard, right);
+ final RelNode tempRightRel = rightBlackboard.root;
+
+ final JoinConditionType conditionType = join.getConditionType();
+ final RexNode condition;
+ final RelNode rightRel;
+ if (join.isNatural()) {
+ condition = createNaturalJoinCondition(validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.NONE) {
+ condition = rexBuilder.makeLiteral(true);
+ rightRel = tempRightRel;
Review comment:
Naturals does not support a switch statement, so it is if (isNatural) {} else {switch{...}}. Double nesting the control statements looks ugly. But I can change the it to switch if you really want to.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [calcite] danny0405 commented on a change in pull request #2138: CALCITE-4210 Replaying subquery from ON clause on to right node
Posted by GitBox <gi...@apache.org>.
danny0405 commented on a change in pull request #2138:
URL: https://github.com/apache/calcite/pull/2138#discussion_r496432979
##########
File path: core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
##########
@@ -2807,34 +2756,109 @@ private boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb) {
return Collections.emptyList();
}
- private RexNode convertJoinCondition(Blackboard bb,
+ private void convertJoin(Blackboard bb, SqlJoin join) {
+ final SqlValidatorScope scope = validator.getJoinScope(join);
+ final Blackboard fromBlackboard = createBlackboard(scope, null, false);
+ SqlNode left = join.getLeft();
+ SqlNode right = join.getRight();
+ final SqlValidatorScope leftScope =
+ Util.first(validator.getJoinScope(left),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard leftBlackboard =
+ createBlackboard(leftScope, null, false);
+ final SqlValidatorScope rightScope =
+ Util.first(validator.getJoinScope(right),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard rightBlackboard =
+ createBlackboard(rightScope, null, false);
+ convertFrom(leftBlackboard, left);
+ final RelNode leftRel = leftBlackboard.root;
+ convertFrom(rightBlackboard, right);
+ final RelNode tempRightRel = rightBlackboard.root;
+
+ final JoinConditionType conditionType = join.getConditionType();
+ final RexNode condition;
+ final RelNode rightRel;
+ if (join.isNatural()) {
+ condition = createNaturalJoinCondition(validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.NONE) {
+ condition = rexBuilder.makeLiteral(true);
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.USING) {
+ condition = createJoinUsingCondition(join,
+ validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.ON) {
+ Pair<RexNode, RelNode> conditionAndRightNode = createJoinOnCondition(fromBlackboard,
+ join,
+ leftRel,
+ tempRightRel);
+ condition = conditionAndRightNode.left;
+ rightRel = conditionAndRightNode.right;
+ } else {
+ throw Util.unexpected(conditionType);
+ }
+ final RelNode joinRel = createJoin(
+ fromBlackboard,
+ leftRel,
+ rightRel,
+ condition,
+ convertJoinType(join.getJoinType()));
+ bb.setRoot(joinRel, false);
+ }
+
+ private RexNode createNaturalJoinCondition(SqlValidatorNamespace leftNamespace,
+ SqlValidatorNamespace rightNamespace) {
+ final List<String> columnList =
+ SqlValidatorUtil.deriveNaturalJoinColumnList(
+ catalogReader.nameMatcher(),
+ leftNamespace.getRowType(),
+ rightNamespace.getRowType());
+ return convertUsing(leftNamespace, rightNamespace, columnList);
+ }
+
+ private RexNode createJoinUsingCondition(SqlJoin join,
SqlValidatorNamespace leftNamespace,
- SqlValidatorNamespace rightNamespace,
- SqlNode condition,
- JoinConditionType conditionType,
+ SqlValidatorNamespace rightNamespace) {
+ SqlNode condition = join.getCondition();
+
+ final SqlNodeList list = (SqlNodeList) condition;
+ final List<String> nameList = new ArrayList<>();
+ for (SqlNode columnName : list) {
+ final SqlIdentifier id = (SqlIdentifier) columnName;
+ String name = id.getSimple();
+ nameList.add(name);
+ }
+ return convertUsing(leftNamespace, rightNamespace, nameList);
+ }
+
+ private Pair<RexNode, RelNode> createJoinOnCondition(Blackboard bb,
+ SqlJoin join,
RelNode leftRel,
RelNode rightRel) {
- if (condition == null) {
- return rexBuilder.makeLiteral(true);
- }
+ SqlNode condition = join.getCondition();
+
bb.setRoot(ImmutableList.of(leftRel, rightRel));
replaceSubQueries(bb, condition, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
- switch (conditionType) {
- case ON:
- bb.setRoot(ImmutableList.of(leftRel, rightRel));
- return bb.convertExpression(condition);
- case USING:
- final SqlNodeList list = (SqlNodeList) condition;
- final List<String> nameList = new ArrayList<>();
- for (SqlNode columnName : list) {
- final SqlIdentifier id = (SqlIdentifier) columnName;
- String name = id.getSimple();
- nameList.add(name);
- }
- return convertUsing(leftNamespace, rightNamespace, nameList);
- default:
- throw Util.unexpected(conditionType);
- }
+ final RelNode newRightRel;
+ if (bb.root == null) {
+ newRightRel = rightRel;
+ } else {
+ bb.setRoot(rightRel, false);
+ List<BlackboardRegisterArgs> blackboardRegisterArgsList =
Review comment:
Thanks, can you put the explanation into the code comment, that would help people understand your code.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [calcite] jamesstarr commented on a change in pull request #2138: CALCITE-4210 Replaying subquery from ON clause on to right node
Posted by GitBox <gi...@apache.org>.
jamesstarr commented on a change in pull request #2138:
URL: https://github.com/apache/calcite/pull/2138#discussion_r496339354
##########
File path: core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
##########
@@ -2807,34 +2756,109 @@ private boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb) {
return Collections.emptyList();
}
- private RexNode convertJoinCondition(Blackboard bb,
+ private void convertJoin(Blackboard bb, SqlJoin join) {
+ final SqlValidatorScope scope = validator.getJoinScope(join);
+ final Blackboard fromBlackboard = createBlackboard(scope, null, false);
+ SqlNode left = join.getLeft();
+ SqlNode right = join.getRight();
+ final SqlValidatorScope leftScope =
+ Util.first(validator.getJoinScope(left),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard leftBlackboard =
+ createBlackboard(leftScope, null, false);
+ final SqlValidatorScope rightScope =
+ Util.first(validator.getJoinScope(right),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard rightBlackboard =
+ createBlackboard(rightScope, null, false);
+ convertFrom(leftBlackboard, left);
+ final RelNode leftRel = leftBlackboard.root;
+ convertFrom(rightBlackboard, right);
+ final RelNode tempRightRel = rightBlackboard.root;
+
+ final JoinConditionType conditionType = join.getConditionType();
+ final RexNode condition;
+ final RelNode rightRel;
+ if (join.isNatural()) {
+ condition = createNaturalJoinCondition(validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.NONE) {
+ condition = rexBuilder.makeLiteral(true);
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.USING) {
+ condition = createJoinUsingCondition(join,
+ validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.ON) {
+ Pair<RexNode, RelNode> conditionAndRightNode = createJoinOnCondition(fromBlackboard,
+ join,
+ leftRel,
+ tempRightRel);
+ condition = conditionAndRightNode.left;
+ rightRel = conditionAndRightNode.right;
+ } else {
+ throw Util.unexpected(conditionType);
+ }
+ final RelNode joinRel = createJoin(
+ fromBlackboard,
+ leftRel,
+ rightRel,
+ condition,
+ convertJoinType(join.getJoinType()));
+ bb.setRoot(joinRel, false);
+ }
+
+ private RexNode createNaturalJoinCondition(SqlValidatorNamespace leftNamespace,
+ SqlValidatorNamespace rightNamespace) {
+ final List<String> columnList =
+ SqlValidatorUtil.deriveNaturalJoinColumnList(
+ catalogReader.nameMatcher(),
+ leftNamespace.getRowType(),
+ rightNamespace.getRowType());
+ return convertUsing(leftNamespace, rightNamespace, columnList);
+ }
+
+ private RexNode createJoinUsingCondition(SqlJoin join,
SqlValidatorNamespace leftNamespace,
- SqlValidatorNamespace rightNamespace,
- SqlNode condition,
- JoinConditionType conditionType,
+ SqlValidatorNamespace rightNamespace) {
+ SqlNode condition = join.getCondition();
+
+ final SqlNodeList list = (SqlNodeList) condition;
+ final List<String> nameList = new ArrayList<>();
+ for (SqlNode columnName : list) {
+ final SqlIdentifier id = (SqlIdentifier) columnName;
+ String name = id.getSimple();
+ nameList.add(name);
+ }
+ return convertUsing(leftNamespace, rightNamespace, nameList);
+ }
+
+ private Pair<RexNode, RelNode> createJoinOnCondition(Blackboard bb,
+ SqlJoin join,
RelNode leftRel,
RelNode rightRel) {
- if (condition == null) {
- return rexBuilder.makeLiteral(true);
- }
+ SqlNode condition = join.getCondition();
+
bb.setRoot(ImmutableList.of(leftRel, rightRel));
replaceSubQueries(bb, condition, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
- switch (conditionType) {
- case ON:
- bb.setRoot(ImmutableList.of(leftRel, rightRel));
- return bb.convertExpression(condition);
- case USING:
- final SqlNodeList list = (SqlNodeList) condition;
- final List<String> nameList = new ArrayList<>();
- for (SqlNode columnName : list) {
- final SqlIdentifier id = (SqlIdentifier) columnName;
- String name = id.getSimple();
- nameList.add(name);
- }
- return convertUsing(leftNamespace, rightNamespace, nameList);
- default:
- throw Util.unexpected(conditionType);
- }
+ final RelNode newRightRel;
+ if (bb.root == null) {
+ newRightRel = rightRel;
+ } else {
+ bb.setRoot(rightRel, false);
+ List<BlackboardRegisterArgs> blackboardRegisterArgsList =
Review comment:
Because there could be a multiplicity issue if it is joined to left node.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [calcite] jamesstarr commented on pull request #2138: CALCITE-4210 Replaying subquery from ON clause on to right node
Posted by GitBox <gi...@apache.org>.
jamesstarr commented on pull request #2138:
URL: https://github.com/apache/calcite/pull/2138#issuecomment-690715885
Could some comment on what I need to do get this reviewed?
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [calcite] danny0405 commented on a change in pull request #2138: CALCITE-4210 Replaying subquery from ON clause on to right node
Posted by GitBox <gi...@apache.org>.
danny0405 commented on a change in pull request #2138:
URL: https://github.com/apache/calcite/pull/2138#discussion_r495677904
##########
File path: core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
##########
@@ -2807,34 +2756,109 @@ private boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb) {
return Collections.emptyList();
}
- private RexNode convertJoinCondition(Blackboard bb,
+ private void convertJoin(Blackboard bb, SqlJoin join) {
+ final SqlValidatorScope scope = validator.getJoinScope(join);
+ final Blackboard fromBlackboard = createBlackboard(scope, null, false);
+ SqlNode left = join.getLeft();
+ SqlNode right = join.getRight();
+ final SqlValidatorScope leftScope =
+ Util.first(validator.getJoinScope(left),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard leftBlackboard =
+ createBlackboard(leftScope, null, false);
+ final SqlValidatorScope rightScope =
+ Util.first(validator.getJoinScope(right),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard rightBlackboard =
+ createBlackboard(rightScope, null, false);
+ convertFrom(leftBlackboard, left);
+ final RelNode leftRel = leftBlackboard.root;
+ convertFrom(rightBlackboard, right);
+ final RelNode tempRightRel = rightBlackboard.root;
+
+ final JoinConditionType conditionType = join.getConditionType();
+ final RexNode condition;
+ final RelNode rightRel;
+ if (join.isNatural()) {
+ condition = createNaturalJoinCondition(validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.NONE) {
+ condition = rexBuilder.makeLiteral(true);
+ rightRel = tempRightRel;
Review comment:
Replace the if else with `switch case`.
##########
File path: core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
##########
@@ -2807,34 +2756,109 @@ private boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb) {
return Collections.emptyList();
}
- private RexNode convertJoinCondition(Blackboard bb,
+ private void convertJoin(Blackboard bb, SqlJoin join) {
+ final SqlValidatorScope scope = validator.getJoinScope(join);
+ final Blackboard fromBlackboard = createBlackboard(scope, null, false);
+ SqlNode left = join.getLeft();
+ SqlNode right = join.getRight();
+ final SqlValidatorScope leftScope =
+ Util.first(validator.getJoinScope(left),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard leftBlackboard =
+ createBlackboard(leftScope, null, false);
+ final SqlValidatorScope rightScope =
+ Util.first(validator.getJoinScope(right),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard rightBlackboard =
+ createBlackboard(rightScope, null, false);
+ convertFrom(leftBlackboard, left);
+ final RelNode leftRel = leftBlackboard.root;
+ convertFrom(rightBlackboard, right);
+ final RelNode tempRightRel = rightBlackboard.root;
+
+ final JoinConditionType conditionType = join.getConditionType();
+ final RexNode condition;
+ final RelNode rightRel;
+ if (join.isNatural()) {
+ condition = createNaturalJoinCondition(validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.NONE) {
+ condition = rexBuilder.makeLiteral(true);
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.USING) {
+ condition = createJoinUsingCondition(join,
+ validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.ON) {
+ Pair<RexNode, RelNode> conditionAndRightNode = createJoinOnCondition(fromBlackboard,
+ join,
+ leftRel,
+ tempRightRel);
+ condition = conditionAndRightNode.left;
+ rightRel = conditionAndRightNode.right;
+ } else {
+ throw Util.unexpected(conditionType);
+ }
+ final RelNode joinRel = createJoin(
+ fromBlackboard,
+ leftRel,
+ rightRel,
+ condition,
+ convertJoinType(join.getJoinType()));
+ bb.setRoot(joinRel, false);
+ }
+
+ private RexNode createNaturalJoinCondition(SqlValidatorNamespace leftNamespace,
+ SqlValidatorNamespace rightNamespace) {
+ final List<String> columnList =
+ SqlValidatorUtil.deriveNaturalJoinColumnList(
+ catalogReader.nameMatcher(),
+ leftNamespace.getRowType(),
+ rightNamespace.getRowType());
+ return convertUsing(leftNamespace, rightNamespace, columnList);
+ }
+
+ private RexNode createJoinUsingCondition(SqlJoin join,
SqlValidatorNamespace leftNamespace,
- SqlValidatorNamespace rightNamespace,
- SqlNode condition,
- JoinConditionType conditionType,
+ SqlValidatorNamespace rightNamespace) {
+ SqlNode condition = join.getCondition();
+
+ final SqlNodeList list = (SqlNodeList) condition;
+ final List<String> nameList = new ArrayList<>();
+ for (SqlNode columnName : list) {
+ final SqlIdentifier id = (SqlIdentifier) columnName;
+ String name = id.getSimple();
+ nameList.add(name);
+ }
+ return convertUsing(leftNamespace, rightNamespace, nameList);
+ }
+
+ private Pair<RexNode, RelNode> createJoinOnCondition(Blackboard bb,
+ SqlJoin join,
RelNode leftRel,
RelNode rightRel) {
- if (condition == null) {
- return rexBuilder.makeLiteral(true);
- }
+ SqlNode condition = join.getCondition();
+
bb.setRoot(ImmutableList.of(leftRel, rightRel));
replaceSubQueries(bb, condition, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
- switch (conditionType) {
- case ON:
- bb.setRoot(ImmutableList.of(leftRel, rightRel));
- return bb.convertExpression(condition);
- case USING:
- final SqlNodeList list = (SqlNodeList) condition;
- final List<String> nameList = new ArrayList<>();
- for (SqlNode columnName : list) {
- final SqlIdentifier id = (SqlIdentifier) columnName;
- String name = id.getSimple();
- nameList.add(name);
- }
- return convertUsing(leftNamespace, rightNamespace, nameList);
- default:
- throw Util.unexpected(conditionType);
- }
+ final RelNode newRightRel;
+ if (bb.root == null) {
+ newRightRel = rightRel;
+ } else {
+ bb.setRoot(rightRel, false);
+ List<BlackboardRegisterArgs> blackboardRegisterArgsList =
Review comment:
Why we set the `rightRel` as root not the `leftRel` ?
##########
File path: core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
##########
@@ -4029,6 +4029,88 @@ private Tester getExtendedTester() {
sql(sql).trim(true).ok();
}
+ @Test void testJoinExpandAndDecorrelation() {
+ String sql = ""
+ + "SELECT emp.deptno, emp.sal\n"
+ + "FROM dept\n"
+ + "JOIN emp ON emp.deptno = dept.deptno AND emp.sal < (\n"
+ + " SELECT AVG(emp.sal)\n"
+ + " FROM emp\n"
+ + " WHERE emp.deptno = dept.deptno\n"
+ + ")";
+ sql(sql)
+ .withConfig(configBuilder -> configBuilder
+ .withExpand(true)
+ .withDecorrelationEnabled(true))
+ .convertsTo("\n"
+ + "LogicalProject(DEPTNO=[$9], SAL=[$7])\n"
+ + " LogicalJoin(condition=[AND(=($9, $0), <($7, $0))], joinType=[inner])\n"
+ + " LogicalTableScan(table=[[CATALOG, SALES, DEPT]])\n"
+ + " LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], "
+ + "SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8], EXPR$0=[$10])\n"
+ + " LogicalJoin(condition=[=($0, $9)], joinType=[left])\n"
+ + " LogicalTableScan(table=[[CATALOG, SALES, EMP]])\n"
+ + " LogicalAggregate(group=[{0}], EXPR$0=[AVG($1)])\n"
Review comment:
Can we test the plan in the xml ?
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [calcite] jamesstarr commented on a change in pull request #2138: CALCITE-4210 Replaying subquery from ON clause on to right node
Posted by GitBox <gi...@apache.org>.
jamesstarr commented on a change in pull request #2138:
URL: https://github.com/apache/calcite/pull/2138#discussion_r496353320
##########
File path: core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
##########
@@ -4029,6 +4029,88 @@ private Tester getExtendedTester() {
sql(sql).trim(true).ok();
}
+ @Test void testJoinExpandAndDecorrelation() {
+ String sql = ""
+ + "SELECT emp.deptno, emp.sal\n"
+ + "FROM dept\n"
+ + "JOIN emp ON emp.deptno = dept.deptno AND emp.sal < (\n"
+ + " SELECT AVG(emp.sal)\n"
+ + " FROM emp\n"
+ + " WHERE emp.deptno = dept.deptno\n"
+ + ")";
+ sql(sql)
+ .withConfig(configBuilder -> configBuilder
+ .withExpand(true)
+ .withDecorrelationEnabled(true))
+ .convertsTo("\n"
+ + "LogicalProject(DEPTNO=[$9], SAL=[$7])\n"
+ + " LogicalJoin(condition=[AND(=($9, $0), <($7, $0))], joinType=[inner])\n"
+ + " LogicalTableScan(table=[[CATALOG, SALES, DEPT]])\n"
+ + " LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], "
+ + "SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8], EXPR$0=[$10])\n"
+ + " LogicalJoin(condition=[=($0, $9)], joinType=[left])\n"
+ + " LogicalTableScan(table=[[CATALOG, SALES, EMP]])\n"
+ + " LogicalAggregate(group=[{0}], EXPR$0=[AVG($1)])\n"
Review comment:
Done.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [calcite] jamesstarr commented on a change in pull request #2138: CALCITE-4210 Replaying subquery from ON clause on to right node
Posted by GitBox <gi...@apache.org>.
jamesstarr commented on a change in pull request #2138:
URL: https://github.com/apache/calcite/pull/2138#discussion_r496353535
##########
File path: core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
##########
@@ -2807,34 +2756,109 @@ private boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb) {
return Collections.emptyList();
}
- private RexNode convertJoinCondition(Blackboard bb,
+ private void convertJoin(Blackboard bb, SqlJoin join) {
+ final SqlValidatorScope scope = validator.getJoinScope(join);
+ final Blackboard fromBlackboard = createBlackboard(scope, null, false);
+ SqlNode left = join.getLeft();
+ SqlNode right = join.getRight();
+ final SqlValidatorScope leftScope =
+ Util.first(validator.getJoinScope(left),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard leftBlackboard =
+ createBlackboard(leftScope, null, false);
+ final SqlValidatorScope rightScope =
+ Util.first(validator.getJoinScope(right),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard rightBlackboard =
+ createBlackboard(rightScope, null, false);
+ convertFrom(leftBlackboard, left);
+ final RelNode leftRel = leftBlackboard.root;
+ convertFrom(rightBlackboard, right);
+ final RelNode tempRightRel = rightBlackboard.root;
+
+ final JoinConditionType conditionType = join.getConditionType();
+ final RexNode condition;
+ final RelNode rightRel;
+ if (join.isNatural()) {
+ condition = createNaturalJoinCondition(validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.NONE) {
+ condition = rexBuilder.makeLiteral(true);
+ rightRel = tempRightRel;
Review comment:
Would you still like to me to replace it with an if {} else { switch } statement?
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [calcite] jamesstarr commented on a change in pull request #2138: CALCITE-4210 Replaying subquery from ON clause on to right node
Posted by GitBox <gi...@apache.org>.
jamesstarr commented on a change in pull request #2138:
URL: https://github.com/apache/calcite/pull/2138#discussion_r496934798
##########
File path: core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java
##########
@@ -4029,6 +4029,88 @@ private Tester getExtendedTester() {
sql(sql).trim(true).ok();
}
+ @Test void testJoinExpandAndDecorrelation() {
+ String sql = ""
+ + "SELECT emp.deptno, emp.sal\n"
+ + "FROM dept\n"
+ + "JOIN emp ON emp.deptno = dept.deptno AND emp.sal < (\n"
+ + " SELECT AVG(emp.sal)\n"
+ + " FROM emp\n"
+ + " WHERE emp.deptno = dept.deptno\n"
+ + ")";
+ sql(sql)
+ .withConfig(configBuilder -> configBuilder
+ .withExpand(true)
+ .withDecorrelationEnabled(true))
+ .convertsTo("\n"
+ + "LogicalProject(DEPTNO=[$9], SAL=[$7])\n"
+ + " LogicalJoin(condition=[AND(=($9, $0), <($7, $0))], joinType=[inner])\n"
+ + " LogicalTableScan(table=[[CATALOG, SALES, DEPT]])\n"
+ + " LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], "
+ + "SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8], EXPR$0=[$10])\n"
+ + " LogicalJoin(condition=[=($0, $9)], joinType=[left])\n"
+ + " LogicalTableScan(table=[[CATALOG, SALES, EMP]])\n"
+ + " LogicalAggregate(group=[{0}], EXPR$0=[AVG($1)])\n"
Review comment:
I changed it to the switch, but I if calcite standard insist on splitting conditional to use a case statement when ever possible, then, natural should be added as a conditionType, which again flatten the control structure.
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [calcite] jamesstarr commented on a change in pull request #2138: CALCITE-4210 Replaying subquery from ON clause on to right node
Posted by GitBox <gi...@apache.org>.
jamesstarr commented on a change in pull request #2138:
URL: https://github.com/apache/calcite/pull/2138#discussion_r496935235
##########
File path: core/src/main/java/org/apache/calcite/sql2rel/SqlToRelConverter.java
##########
@@ -2807,34 +2756,109 @@ private boolean isSubQueryNonCorrelated(RelNode subq, Blackboard bb) {
return Collections.emptyList();
}
- private RexNode convertJoinCondition(Blackboard bb,
+ private void convertJoin(Blackboard bb, SqlJoin join) {
+ final SqlValidatorScope scope = validator.getJoinScope(join);
+ final Blackboard fromBlackboard = createBlackboard(scope, null, false);
+ SqlNode left = join.getLeft();
+ SqlNode right = join.getRight();
+ final SqlValidatorScope leftScope =
+ Util.first(validator.getJoinScope(left),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard leftBlackboard =
+ createBlackboard(leftScope, null, false);
+ final SqlValidatorScope rightScope =
+ Util.first(validator.getJoinScope(right),
+ ((DelegatingScope) bb.scope).getParent());
+ final Blackboard rightBlackboard =
+ createBlackboard(rightScope, null, false);
+ convertFrom(leftBlackboard, left);
+ final RelNode leftRel = leftBlackboard.root;
+ convertFrom(rightBlackboard, right);
+ final RelNode tempRightRel = rightBlackboard.root;
+
+ final JoinConditionType conditionType = join.getConditionType();
+ final RexNode condition;
+ final RelNode rightRel;
+ if (join.isNatural()) {
+ condition = createNaturalJoinCondition(validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.NONE) {
+ condition = rexBuilder.makeLiteral(true);
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.USING) {
+ condition = createJoinUsingCondition(join,
+ validator.getNamespace(left),
+ validator.getNamespace(right));
+ rightRel = tempRightRel;
+ } else if (conditionType == JoinConditionType.ON) {
+ Pair<RexNode, RelNode> conditionAndRightNode = createJoinOnCondition(fromBlackboard,
+ join,
+ leftRel,
+ tempRightRel);
+ condition = conditionAndRightNode.left;
+ rightRel = conditionAndRightNode.right;
+ } else {
+ throw Util.unexpected(conditionType);
+ }
+ final RelNode joinRel = createJoin(
+ fromBlackboard,
+ leftRel,
+ rightRel,
+ condition,
+ convertJoinType(join.getJoinType()));
+ bb.setRoot(joinRel, false);
+ }
+
+ private RexNode createNaturalJoinCondition(SqlValidatorNamespace leftNamespace,
+ SqlValidatorNamespace rightNamespace) {
+ final List<String> columnList =
+ SqlValidatorUtil.deriveNaturalJoinColumnList(
+ catalogReader.nameMatcher(),
+ leftNamespace.getRowType(),
+ rightNamespace.getRowType());
+ return convertUsing(leftNamespace, rightNamespace, columnList);
+ }
+
+ private RexNode createJoinUsingCondition(SqlJoin join,
SqlValidatorNamespace leftNamespace,
- SqlValidatorNamespace rightNamespace,
- SqlNode condition,
- JoinConditionType conditionType,
+ SqlValidatorNamespace rightNamespace) {
+ SqlNode condition = join.getCondition();
+
+ final SqlNodeList list = (SqlNodeList) condition;
+ final List<String> nameList = new ArrayList<>();
+ for (SqlNode columnName : list) {
+ final SqlIdentifier id = (SqlIdentifier) columnName;
+ String name = id.getSimple();
+ nameList.add(name);
+ }
+ return convertUsing(leftNamespace, rightNamespace, nameList);
+ }
+
+ private Pair<RexNode, RelNode> createJoinOnCondition(Blackboard bb,
+ SqlJoin join,
RelNode leftRel,
RelNode rightRel) {
- if (condition == null) {
- return rexBuilder.makeLiteral(true);
- }
+ SqlNode condition = join.getCondition();
+
bb.setRoot(ImmutableList.of(leftRel, rightRel));
replaceSubQueries(bb, condition, RelOptUtil.Logic.UNKNOWN_AS_FALSE);
- switch (conditionType) {
- case ON:
- bb.setRoot(ImmutableList.of(leftRel, rightRel));
- return bb.convertExpression(condition);
- case USING:
- final SqlNodeList list = (SqlNodeList) condition;
- final List<String> nameList = new ArrayList<>();
- for (SqlNode columnName : list) {
- final SqlIdentifier id = (SqlIdentifier) columnName;
- String name = id.getSimple();
- nameList.add(name);
- }
- return convertUsing(leftNamespace, rightNamespace, nameList);
- default:
- throw Util.unexpected(conditionType);
- }
+ final RelNode newRightRel;
+ if (bb.root == null) {
+ newRightRel = rightRel;
+ } else {
+ bb.setRoot(rightRel, false);
+ List<BlackboardRegisterArgs> blackboardRegisterArgsList =
Review comment:
Added a comment
----------------------------------------------------------------
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:
users@infra.apache.org
[GitHub] [calcite] danny0405 closed pull request #2138: CALCITE-4210 Replaying subquery from ON clause on to right node
Posted by GitBox <gi...@apache.org>.
danny0405 closed pull request #2138:
URL: https://github.com/apache/calcite/pull/2138
----------------------------------------------------------------
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:
users@infra.apache.org