Skip to content

Commit

Permalink
[CALCITE-6737] LoptOptimizeJoinRule can not identify selfjoin on uniq…
Browse files Browse the repository at this point in the history
…ue join keys
  • Loading branch information
kasakrisz authored and zabetak committed Dec 18, 2024
1 parent dbf4ff8 commit c1ca226
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,7 @@ > getNumFieldsInJoinFactor(factor2)) {
final Map<Integer, Integer> leftFactorColMapping = new HashMap<>();
for (int i = 0; i < left.getRowType().getFieldCount(); i++) {
final RelColumnOrigin colOrigin = mq.getColumnOrigin(left, i);
if (colOrigin != null && colOrigin.isDerived()) {
if (colOrigin != null && !colOrigin.isDerived()) {
leftFactorColMapping.put(
colOrigin.getOriginColumnOrdinal(),
i);
Expand All @@ -734,7 +734,7 @@ > getNumFieldsInJoinFactor(factor2)) {
RelNode right = getJoinFactor(rightFactor);
for (int i = 0; i < right.getRowType().getFieldCount(); i++) {
final RelColumnOrigin colOrigin = mq.getColumnOrigin(right, i);
if (colOrigin == null || !colOrigin.isDerived()) {
if (colOrigin == null || colOrigin.isDerived()) {
continue;
}
Integer leftOffset =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2067,12 +2067,12 @@ private static boolean areSelfJoinKeysUnique(RelMetadataQuery mq,
for (IntPair pair : joinInfo.pairs()) {
final RelColumnOrigin leftOrigin =
mq.getColumnOrigin(leftRel, pair.source);
if (leftOrigin == null || !leftOrigin.isDerived()) {
if (leftOrigin == null || leftOrigin.isDerived()) {
return false;
}
final RelColumnOrigin rightOrigin =
mq.getColumnOrigin(rightRel, pair.target);
if (rightOrigin == null || !rightOrigin.isDerived()) {
if (rightOrigin == null || rightOrigin.isDerived()) {
return false;
}
if (leftOrigin.getOriginColumnOrdinal()
Expand Down
13 changes: 13 additions & 0 deletions core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9480,4 +9480,17 @@ private void checkJoinAssociateRuleWithTopAlwaysTrueCondition(boolean allowAlway
.check();
}

@Test void testLoptOptimizeJoinRulePrioritizeSelfJoin() {
HepProgram program = new HepProgramBuilder()
.addMatchOrder(HepMatchOrder.BOTTOM_UP)
.addRuleInstance(CoreRules.JOIN_TO_MULTI_JOIN)
.build();

sql("select e.empno from emp e"
+ " inner join dept d on d.deptno = e.deptno"
+ " inner join emp e2 on e.empno = e2.empno")
.withPre(program)
.withRule(CoreRules.MULTI_JOIN_OPTIMIZE)
.check();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6794,6 +6794,31 @@ EnumerableLimitSort(sort0=[$0], dir0=[ASC], offset=[5], fetch=[10])
EnumerableTableScan(table=[[CATALOG, SALES, EMP]])
EnumerableProject(MGR=[$3])
EnumerableTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testLoptOptimizeJoinRulePrioritizeSelfJoin">
<Resource name="sql">
<![CDATA[select e.empno from emp e inner join dept d on d.deptno = e.deptno inner join emp e2 on e.empno = e2.empno]]>
</Resource>
<Resource name="planBefore">
<![CDATA[
LogicalProject(EMPNO=[$0])
MultiJoin(joinFilter=[AND(=($0, $11), =($9, $7))], isFullOuterJoin=[false], joinTypes=[[INNER, INNER, INNER]], outerJoinConditions=[[NULL, NULL, NULL]], projFields=[[ALL, ALL, ALL]])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalProject(EMPNO=[$0])
LogicalProject(EMPNO=[$0], ENAME=[$1], JOB=[$2], MGR=[$3], HIREDATE=[$4], SAL=[$5], COMM=[$6], DEPTNO=[$7], SLACKER=[$8], DEPTNO0=[$18], NAME=[$19], EMPNO0=[$0], ENAME0=[$1], JOB0=[$2], MGR0=[$3], HIREDATE0=[$4], SAL0=[$5], COMM0=[$6], DEPTNO1=[$7], SLACKER0=[$8])
LogicalJoin(condition=[=($18, $7)], joinType=[inner])
LogicalJoin(condition=[=($9, $0)], joinType=[inner])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
]]>
</Resource>
</TestCase>
Expand Down

0 comments on commit c1ca226

Please sign in to comment.