Skip to content

Commit c18c045

Browse files
committed
[CALCITE-7273] CoreRules.JOIN_REDUCE_EXPRESSIONS throws when applied to an ASOF JOIN
Signed-off-by: Mihai Budiu <mbudiu@feldera.com>
1 parent 18a8aed commit c18c045

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

core/src/main/java/org/apache/calcite/rel/rules/ReduceExpressionsRule.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.calcite.rel.core.Join;
3131
import org.apache.calcite.rel.core.Project;
3232
import org.apache.calcite.rel.core.Window;
33+
import org.apache.calcite.rel.logical.LogicalAsofJoin;
3334
import org.apache.calcite.rel.logical.LogicalCalc;
3435
import org.apache.calcite.rel.logical.LogicalFilter;
3536
import org.apache.calcite.rel.logical.LogicalProject;
@@ -373,6 +374,12 @@ public JoinReduceExpressionsRule(Class<? extends Join> joinClass,
373374

374375
@Override public void onMatch(RelOptRuleCall call) {
375376
final Join join = call.rel(0);
377+
if (join instanceof LogicalAsofJoin) {
378+
// Currently ASOF JOINs are restricted by the validator to use very specific
379+
// conditions, so there isn't much to simplify about them.
380+
// Moreover, calling join.copy() below for an ASOF JOIN will throw.
381+
return;
382+
}
376383
final List<RexNode> expList = Lists.newArrayList(join.getCondition());
377384
final int fieldCount = join.getLeft().getRowType().getFieldCount();
378385
final RelMetadataQuery mq = call.getMetadataQuery();

core/src/test/java/org/apache/calcite/test/RelOptRulesTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3391,6 +3391,19 @@ private void checkPushJoinThroughUnionOnRightDoesNotMatchSemiOrAntiJoin(JoinRelT
33913391
.checkUnchanged();
33923392
}
33933393

3394+
/** Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-7273">[CALCITE-7273]
3395+
* CoreRules.JOIN_REDUCE_EXPRESSIONS throws when applied to an ASOF JOIN</a>. */
3396+
@Test void testAsofOptJoin() {
3397+
// Had to use ROW values to cause the optimization rule to match
3398+
final String sql = "SELECT *\n"
3399+
+ "FROM (VALUES (NULL, ROW(0, 1)), (1, ROW(0, 2))) AS t1(k, t)\n"
3400+
+ "ASOF JOIN (VALUES (2, ROW(0, 3))) AS t2(k, t)\n"
3401+
+ "MATCH_CONDITION t2.t < t1.t\n"
3402+
+ "ON t1.k = t2.k\n";
3403+
sql(sql).withRule(CoreRules.JOIN_REDUCE_EXPRESSIONS)
3404+
.checkUnchanged();
3405+
}
3406+
33943407
/** Tests to see if the final branch of union is missed. */
33953408
@Test void testUnionMergeRule() {
33963409
final String sql = "select * from (\n"

core/src/test/resources/org/apache/calcite/test/RelOptRulesTest.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,6 +1586,30 @@ LogicalProject(K=[$0], T=[$1], K0=[$2], T0=[$3])
15861586
LogicalAsofJoin(condition=[=($0, $2)], joinType=[asof], matchCondition=[<($3, $1)])
15871587
LogicalValues(tuples=[[{ null, 0 }, { 1, null }, { 1, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 }, { 1, 4 }, { 2, 3 }, { 3, 4 }]])
15881588
LogicalValues(tuples=[[{ 1, null }, { 1, 2 }, { 1, 3 }, { 2, 10 }, { 2, 0 }]])
1589+
]]>
1590+
</Resource>
1591+
</TestCase>
1592+
<TestCase name="testAsofOptJoin">
1593+
<Resource name="sql">
1594+
<![CDATA[SELECT *
1595+
FROM (VALUES (NULL, ROW(0, 1)), (1, ROW(0, 2))) AS t1(k, t)
1596+
ASOF JOIN (VALUES (2, ROW(0, 3))) AS t2(k, t)
1597+
MATCH_CONDITION t2.t < t1.t
1598+
ON t1.k = t2.k
1599+
]]>
1600+
</Resource>
1601+
<Resource name="planBefore">
1602+
<![CDATA[
1603+
LogicalProject(K=[$0], T=[$1], K0=[$2], T0=[$3])
1604+
LogicalAsofJoin(condition=[=($0, $2)], joinType=[asof], matchCondition=[<($3, $1)])
1605+
LogicalProject(K=[$0], T=[$1])
1606+
LogicalUnion(all=[true])
1607+
LogicalProject(EXPR$0=[null:INTEGER], EXPR$1=[ROW(0, 1)])
1608+
LogicalValues(tuples=[[{ 0 }]])
1609+
LogicalProject(EXPR$0=[1], EXPR$1=[ROW(0, 2)])
1610+
LogicalValues(tuples=[[{ 0 }]])
1611+
LogicalProject(K=[2], T=[ROW(0, 3)])
1612+
LogicalValues(tuples=[[{ 0 }]])
15891613
]]>
15901614
</Resource>
15911615
</TestCase>

0 commit comments

Comments
 (0)