Skip to content

Commit

Permalink
[CALCITE-4941] SemiJoinRule loses hints
Browse files Browse the repository at this point in the history
  • Loading branch information
rubenada committed Dec 15, 2021
1 parent 250dfb7 commit 7d342b3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ protected void perform(RelOptRuleCall call, @Nullable Project project,
RelOptUtil.createEquiJoinCondition(relBuilder.peek(2, 0),
joinInfo.leftKeys, relBuilder.peek(2, 1), newRightKeys,
rexBuilder);
relBuilder.semiJoin(newCondition);
relBuilder.semiJoin(newCondition).hints(join.getHints());
break;

case LEFT:
Expand Down
46 changes: 46 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 @@ -58,6 +58,9 @@
import org.apache.calcite.rel.core.Minus;
import org.apache.calcite.rel.core.Project;
import org.apache.calcite.rel.core.Union;
import org.apache.calcite.rel.hint.HintPredicates;
import org.apache.calcite.rel.hint.HintStrategyTable;
import org.apache.calcite.rel.hint.RelHint;
import org.apache.calcite.rel.logical.LogicalAggregate;
import org.apache.calcite.rel.logical.LogicalCorrelate;
import org.apache.calcite.rel.logical.LogicalFilter;
Expand Down Expand Up @@ -147,6 +150,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
Expand Down Expand Up @@ -1077,6 +1081,48 @@ private void checkSemiOrAntiJoinProjectTranspose(JoinRelType type) {
.check();
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-4941">[CALCITE-4941]
* SemiJoinRule loses hints</a>. */
@Test void testSemiJoinRuleWithHint() {
final RelHint noHashJoinHint = RelHint.builder("no_hash_join").build();
final Function<RelBuilder, RelNode> relFn = b -> {
b.getCluster().setHintStrategies(
HintStrategyTable.builder()
.hintStrategy("no_hash_join", HintPredicates.JOIN)
.build());
return b
.scan("DEPT")
.scan("EMP")
.project(b.field("DEPTNO"))
.distinct()
.join(
JoinRelType.INNER,
b.equals(
b.field(2, 0, "DEPTNO"),
b.field(2, 1, "DEPTNO"))).hints(noHashJoinHint)
.project(b.field("DNAME"))
.build();
};

// verify plan
relFn(relFn)
.withRule(CoreRules.PROJECT_TO_SEMI_JOIN)
.check();

// verify hint
final RelBuilder relBuilder = RelBuilder.create(RelBuilderTest.config().build());
final RelNode input = relFn.apply(relBuilder);
final HepProgram program = new HepProgramBuilder()
.addRuleInstance(CoreRules.PROJECT_TO_SEMI_JOIN)
.build();
final HepPlanner hepPlanner = new HepPlanner(program);
hepPlanner.setRoot(input);
final RelNode output = hepPlanner.findBestExp();
final Join join = (Join) output.getInput(0);
assertTrue(join.getHints().contains(noHashJoinHint));
}

/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-438">[CALCITE-438]
* Push predicates through SemiJoin</a>. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11447,6 +11447,27 @@ LogicalProject(DEPTNO=[$0], NAME=[$1])
LogicalProject(DEPTNO=[$7])
LogicalFilter(condition=[>($5, 100)])
LogicalTableScan(table=[[CATALOG, SALES, EMP]])
]]>
</Resource>
</TestCase>
<TestCase name="testSemiJoinRuleWithHint">
<Resource name="planBefore">
<![CDATA[
LogicalProject(DNAME=[$1])
LogicalJoin(condition=[=($0, $3)], joinType=[inner])
LogicalTableScan(table=[[scott, DEPT]])
LogicalAggregate(group=[{0}])
LogicalProject(DEPTNO=[$7])
LogicalTableScan(table=[[scott, EMP]])
]]>
</Resource>
<Resource name="planAfter">
<![CDATA[
LogicalProject(DNAME=[$1])
LogicalJoin(condition=[=($0, $3)], joinType=[semi])
LogicalTableScan(table=[[scott, DEPT]])
LogicalProject(DEPTNO=[$7])
LogicalTableScan(table=[[scott, EMP]])
]]>
</Resource>
</TestCase>
Expand Down

0 comments on commit 7d342b3

Please sign in to comment.