This is an automated email from the ASF dual-hosted git repository. mbudiu pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/calcite.git
The following commit(s) were added to refs/heads/main by this push: new 4252e0ca2a [CALCITE-6540] RelOptUtil.pushDownJoinConditions does not correctly adjust ASOF joins match conditions 4252e0ca2a is described below commit 4252e0ca2a7c57edf5ea8f13d4cf70fe0c2913c7 Author: Mihai Budiu <mbu...@feldera.com> AuthorDate: Tue Aug 20 13:20:09 2024 -0700 [CALCITE-6540] RelOptUtil.pushDownJoinConditions does not correctly adjust ASOF joins match conditions Signed-off-by: Mihai Budiu <mbu...@feldera.com> --- .../main/java/org/apache/calcite/plan/RelOptUtil.java | 4 +++- .../org/apache/calcite/test/SqlToRelConverterTest.java | 12 ++++++++++++ .../org/apache/calcite/test/SqlToRelConverterTest.xml | 18 ++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java b/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java index 5042bc04ca..cb644ccf1d 100644 --- a/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java +++ b/core/src/main/java/org/apache/calcite/plan/RelOptUtil.java @@ -3851,8 +3851,10 @@ public abstract class RelOptUtil { final RelNode left = relBuilder.build(); if (joinType == JoinRelType.ASOF || joinType == JoinRelType.LEFT_ASOF) { LogicalAsofJoin ljoin = (LogicalAsofJoin) originalJoin; + RexNode match = + RexUtil.shift(ljoin.getMatchCondition(), leftCount, extraLeftExprs.size()); RelNode copy = - ljoin.copy(originalJoin.getTraitSet(), joinCond, ljoin.getMatchCondition(), left, right); + ljoin.copy(originalJoin.getTraitSet(), joinCond, match, left, right); relBuilder.push(copy); } else { relBuilder.push( diff --git a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java index aba9bb1da2..02980adc35 100644 --- a/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java +++ b/core/src/test/java/org/apache/calcite/test/SqlToRelConverterTest.java @@ -285,6 +285,18 @@ class SqlToRelConverterTest extends SqlToRelTestBase { sql(sql).ok(); } + /** Test case for <a href="https://issues.apache.org/jira/browse/CALCITE-6540"> + * RelOptUtil.pushDownJoinConditions does not correctly adjust ASOF joins match conditions</a>. + */ + @Test void testAsOfCast() { + final String sql = "SELECT * " + + "FROM (SELECT deptno % 10 as m, CAST(deptno AS BIGINT) as deptno FROM dept) D\n" + + "LEFT ASOF JOIN (SELECT CAST(empno as BIGINT) as empno, CAST(deptno AS BIGINT) AS deptno FROM emp) E\n" + + "MATCH_CONDITION D.deptno >= E.deptno\n" + + "ON D.m = E.empno"; + sql(sql).withConformance(SqlConformanceEnum.LENIENT).ok(); + } + @Test void testJoinOnInSubQuery() { final String sql = "select * from emp left join dept\n" + "on emp.empno = 1\n" diff --git a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml index d97cf8c12e..60b1ce73e6 100644 --- a/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml +++ b/core/src/test/resources/org/apache/calcite/test/SqlToRelConverterTest.xml @@ -521,6 +521,24 @@ LogicalProject(EXPR$0=[ROW(ITEM(ITEM(ITEM(ITEM($3, 0), 'detail'), 'skills'), 0). <![CDATA[ LogicalProject(EXPR$0=[ITEM(ITEM($3, 1).DETAIL.SKILLS, +(2, 3)).DESC]) LogicalTableScan(table=[[CATALOG, SALES, DEPT_NESTED]]) +]]> + </Resource> + </TestCase> + <TestCase name="testAsOfCast"> + <Resource name="sql"> + <![CDATA[SELECT * FROM (SELECT deptno % 10 as m, CAST(deptno AS BIGINT) as deptno FROM dept) D +LEFT ASOF JOIN (SELECT CAST(empno as BIGINT) as empno, CAST(deptno AS BIGINT) AS deptno FROM emp) E +MATCH_CONDITION D.deptno >= E.deptno +ON D.m = E.empno]]> + </Resource> + <Resource name="plan"> + <![CDATA[ +LogicalProject(M=[$0], DEPTNO=[$1], EMPNO=[$3], DEPTNO0=[$4]) + LogicalAsofJoin(condition=[=($2, $3)], joinType=[left_asof], matchCondition=[>=($1, $4)]) + LogicalProject(M=[MOD($0, 10)], DEPTNO=[CAST($0):BIGINT NOT NULL], $f2=[CAST(MOD($0, 10)):BIGINT NOT NULL]) + LogicalTableScan(table=[[CATALOG, SALES, DEPT]]) + LogicalProject(EMPNO=[CAST($0):BIGINT NOT NULL], DEPTNO=[CAST($7):BIGINT NOT NULL]) + LogicalTableScan(table=[[CATALOG, SALES, EMP]]) ]]> </Resource> </TestCase>