Skip to content

Commit 3fce658

Browse files
racevedoomihaibudiu
authored andcommitted
[CALCITE-6786] ANY/SOME operator yields multiple rows in correlated queries
1 parent 2a89ec9 commit 3fce658

File tree

4 files changed

+94
-45
lines changed

4 files changed

+94
-45
lines changed

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -705,16 +705,15 @@ private static RexNode rewriteIn(RexSubQuery e, Set<CorrelationId> variablesSet,
705705
if (variablesSet.isEmpty()) {
706706
builder.aggregate(builder.groupKey(builder.field("cs")),
707707
builder.count(false, "c"));
708-
709-
// sorts input with desc order since we are interested
710-
// only in the case when one of the values is true.
711-
// When true value is absent then we are interested
712-
// only in false value.
713-
builder.sortLimit(0, 1,
714-
ImmutableList.of(builder.desc(builder.field("cs"))));
715708
} else {
716709
builder.distinct();
717710
}
711+
// sorts input with desc order since we are interested
712+
// only in the case when one of the values is true.
713+
// When true value is absent then we are interested
714+
// only in false value.
715+
builder.sortLimit(0, 1,
716+
ImmutableList.of(builder.desc(builder.field("cs"))));
718717
}
719718
// clears expressionOperands and fields lists since
720719
// all expressions were used in the filter

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

+10-8
Original file line numberDiff line numberDiff line change
@@ -3662,18 +3662,20 @@ LogicalProject(EMPNO=[$0])
36623662
LogicalCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{1}])
36633663
LogicalProject(EMPNO=[$0], ENAME=[$1])
36643664
LogicalTableScan(table=[[CATALOG, SALES, EMPNULLABLES]])
3665+
LogicalSort(sort0=[$0], dir0=[DESC], fetch=[1])
3666+
LogicalAggregate(group=[{0}])
3667+
LogicalProject(cs=[IS NOT NULL($0)])
3668+
LogicalFilter(condition=[OR(=(1, $0), IS NULL($0))])
3669+
LogicalProject(DEPTNO=[$0])
3670+
LogicalFilter(condition=[AND(=($cor0.ENAME, CAST($1):VARCHAR(20)), >($0, 10))])
3671+
LogicalTableScan(table=[[CATALOG, SALES, DEPTNULLABLES]])
3672+
LogicalSort(sort0=[$0], dir0=[DESC], fetch=[1])
36653673
LogicalAggregate(group=[{0}])
36663674
LogicalProject(cs=[IS NOT NULL($0)])
3667-
LogicalFilter(condition=[OR(=(1, $0), IS NULL($0))])
3675+
LogicalFilter(condition=[OR(=(2, $0), IS NULL($0))])
36683676
LogicalProject(DEPTNO=[$0])
3669-
LogicalFilter(condition=[AND(=($cor0.ENAME, CAST($1):VARCHAR(20)), >($0, 10))])
3677+
LogicalFilter(condition=[AND(=($cor0.ENAME, CAST($1):VARCHAR(20)), <($0, 20))])
36703678
LogicalTableScan(table=[[CATALOG, SALES, DEPTNULLABLES]])
3671-
LogicalAggregate(group=[{0}])
3672-
LogicalProject(cs=[IS NOT NULL($0)])
3673-
LogicalFilter(condition=[OR(=(2, $0), IS NULL($0))])
3674-
LogicalProject(DEPTNO=[$0])
3675-
LogicalFilter(condition=[AND(=($cor0.ENAME, CAST($1):VARCHAR(20)), <($0, 20))])
3676-
LogicalTableScan(table=[[CATALOG, SALES, DEPTNULLABLES]])
36773679
]]>
36783680
</Resource>
36793681
</TestCase>

core/src/test/resources/sql/some.iq

+35
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,41 @@ where sal > some (4000, 2000);
895895

896896
!ok
897897

898+
# CALCITE-6786: ANY/SOME operator yields multiple rows in correlated queries
899+
900+
WITH tb as (select array(SELECT * FROM (VALUES (TRUE), (NULL)) as x(a)) as a)
901+
SELECT TRUE IN (SELECT b FROM UNNEST(a) AS x1(b)) AS test FROM tb;
902+
+------+
903+
| TEST |
904+
+------+
905+
| true |
906+
+------+
907+
(1 row)
908+
909+
!ok
910+
911+
WITH tb as (select array(SELECT * FROM (VALUES (FALSE), (NULL)) as x(a)) as a)
912+
SELECT TRUE IN (SELECT b FROM UNNEST(a) AS x1(b)) AS test FROM tb;
913+
+------+
914+
| TEST |
915+
+------+
916+
| |
917+
+------+
918+
(1 row)
919+
920+
!ok
921+
922+
WITH tb as (select array(SELECT * FROM (VALUES (FALSE), (TRUE)) as x(a)) as a)
923+
SELECT TRUE IN (SELECT b FROM UNNEST(a) AS x1(b)) AS test FROM tb;
924+
+------+
925+
| TEST |
926+
+------+
927+
| true |
928+
+------+
929+
(1 row)
930+
931+
!ok
932+
898933
select sal, sal > some (4000, 2000, null) from "scott".emp;
899934
+---------+--------+
900935
| SAL | EXPR$1 |

core/src/test/resources/sql/sub-query.iq

+43-30
Original file line numberDiff line numberDiff line change
@@ -1871,13 +1871,15 @@ select sal from "scott".emp e
18711871
(0 rows)
18721872

18731873
!ok
1874-
EnumerableCalc(expr#0..4=[{inputs}], expr#5=[RAND()], expr#6=[CAST($t5):INTEGER NOT NULL], expr#7=[2], expr#8=[MOD($t6, $t7)], expr#9=[3], expr#10=[=($t8, $t9)], expr#11=[OR($t10, $t4)], SAL=[$t1], $condition=[$t11])
1875-
EnumerableMergeJoin(condition=[=($2, $3)], joinType=[left])
1876-
EnumerableSort(sort0=[$2], dir0=[ASC])
1877-
EnumerableCalc(expr#0..7=[{inputs}], EMPNO=[$t0], SAL=[$t5], DEPTNO=[$t7])
1878-
EnumerableTableScan(table=[[scott, EMP]])
1879-
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[false], DEPTNO=[$t0], $f1=[$t3])
1880-
EnumerableTableScan(table=[[scott, DEPT]])
1874+
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[RAND()], expr#4=[CAST($t3):INTEGER NOT NULL], expr#5=[2], expr#6=[MOD($t4, $t5)], expr#7=[3], expr#8=[=($t6, $t7)], expr#9=[OR($t8, $t2)], SAL=[$t0], $condition=[$t9])
1875+
EnumerableCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{1}])
1876+
EnumerableCalc(expr#0..7=[{inputs}], SAL=[$t5], DEPTNO=[$t7])
1877+
EnumerableTableScan(table=[[scott, EMP]])
1878+
EnumerableLimit(fetch=[1])
1879+
EnumerableSort(sort0=[$0], dir0=[DESC])
1880+
EnumerableAggregate(group=[{0}])
1881+
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[false], expr#4=[$cor0], expr#5=[$t4.DEPTNO], expr#6=[=($t0, $t5)], cs=[$t3], $condition=[$t6])
1882+
EnumerableTableScan(table=[[scott, DEPT]])
18811883
!plan
18821884

18831885
# Test filter null IN nullable correlated
@@ -1958,13 +1960,15 @@ select sal from "scott".emp e
19581960
(0 rows)
19591961

19601962
!ok
1961-
EnumerableCalc(expr#0..4=[{inputs}], expr#5=[NOT($t4)], expr#6=[IS NOT NULL($t4)], expr#7=[OR($t5, $t6)], expr#8=[IS NOT TRUE($t7)], SAL=[$t1], $condition=[$t8])
1962-
EnumerableMergeJoin(condition=[=($2, $3)], joinType=[left])
1963-
EnumerableSort(sort0=[$2], dir0=[ASC])
1964-
EnumerableCalc(expr#0..7=[{inputs}], EMPNO=[$t0], SAL=[$t5], DEPTNO=[$t7])
1965-
EnumerableTableScan(table=[[scott, EMP]])
1966-
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[false], DEPTNO=[$t0], $f1=[$t3])
1967-
EnumerableTableScan(table=[[scott, DEPT]])
1963+
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[NOT($t2)], expr#4=[IS NOT NULL($t2)], expr#5=[OR($t3, $t4)], expr#6=[IS NOT TRUE($t5)], SAL=[$t0], $condition=[$t6])
1964+
EnumerableCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{1}])
1965+
EnumerableCalc(expr#0..7=[{inputs}], SAL=[$t5], DEPTNO=[$t7])
1966+
EnumerableTableScan(table=[[scott, EMP]])
1967+
EnumerableLimit(fetch=[1])
1968+
EnumerableSort(sort0=[$0], dir0=[DESC])
1969+
EnumerableAggregate(group=[{0}])
1970+
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[false], expr#4=[$cor0], expr#5=[$t4.DEPTNO], expr#6=[=($t5, $t0)], cs=[$t3], $condition=[$t6])
1971+
EnumerableTableScan(table=[[scott, DEPT]])
19681972
!plan
19691973

19701974
# Test filter null NOT IN literal correlated
@@ -2027,13 +2031,14 @@ select sal from "scott".emp e
20272031
(11 rows)
20282032

20292033
!ok
2030-
EnumerableCalc(expr#0..4=[{inputs}], expr#5=[NOT($t4)], expr#6=[IS NOT NULL($t4)], expr#7=[OR($t5, $t6)], expr#8=[IS NOT TRUE($t7)], SAL=[$t1], $condition=[$t8])
2031-
EnumerableMergeJoin(condition=[=($2, $3)], joinType=[left])
2032-
EnumerableSort(sort0=[$2], dir0=[ASC])
2033-
EnumerableCalc(expr#0..7=[{inputs}], EMPNO=[$t0], SAL=[$t5], DEPTNO=[$t7])
2034-
EnumerableTableScan(table=[[scott, EMP]])
2035-
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[true], expr#4=[10], expr#5=[CAST($t0):INTEGER NOT NULL], expr#6=[=($t4, $t5)], DEPTNO1=[$t0], $f1=[$t3], $condition=[$t6])
2036-
EnumerableTableScan(table=[[scott, DEPT]])
2034+
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[NOT($t2)], expr#4=[IS NOT NULL($t2)], expr#5=[OR($t3, $t4)], expr#6=[IS NOT TRUE($t5)], SAL=[$t0], $condition=[$t6])
2035+
EnumerableCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{1}])
2036+
EnumerableCalc(expr#0..7=[{inputs}], SAL=[$t5], DEPTNO=[$t7])
2037+
EnumerableTableScan(table=[[scott, EMP]])
2038+
EnumerableLimit(fetch=[1])
2039+
EnumerableSort(sort0=[$0], dir0=[DESC])
2040+
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[true], expr#4=[$cor0], expr#5=[$t4.DEPTNO], expr#6=[=($t5, $t0)], expr#7=[10], expr#8=[CAST($t0):INTEGER NOT NULL], expr#9=[=($t7, $t8)], expr#10=[AND($t6, $t9)], cs=[$t3], $condition=[$t10])
2041+
EnumerableTableScan(table=[[scott, DEPT]])
20372042
!plan
20382043

20392044
# Test filter literal NOT IN nullable correlated
@@ -2057,13 +2062,14 @@ select sal from "scott".emp e
20572062
(11 rows)
20582063

20592064
!ok
2060-
EnumerableCalc(expr#0..4=[{inputs}], expr#5=[NOT($t3)], expr#6=[IS NOT NULL($t3)], expr#7=[OR($t5, $t6)], expr#8=[IS NOT TRUE($t7)], SAL=[$t1], $condition=[$t8])
2061-
EnumerableMergeJoin(condition=[=($2, $4)], joinType=[left])
2062-
EnumerableSort(sort0=[$2], dir0=[ASC])
2063-
EnumerableCalc(expr#0..7=[{inputs}], EMPNO=[$t0], SAL=[$t5], DEPTNO=[$t7])
2064-
EnumerableTableScan(table=[[scott, EMP]])
2065-
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[true], expr#4=[CAST($t0):INTEGER], expr#5=[10], expr#6=[=($t4, $t5)], cs=[$t3], DEPTNO=[$t0], $condition=[$t6])
2066-
EnumerableTableScan(table=[[scott, DEPT]])
2065+
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[NOT($t2)], expr#4=[IS NOT NULL($t2)], expr#5=[OR($t3, $t4)], expr#6=[IS NOT TRUE($t5)], SAL=[$t0], $condition=[$t6])
2066+
EnumerableCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{1}])
2067+
EnumerableCalc(expr#0..7=[{inputs}], SAL=[$t5], DEPTNO=[$t7])
2068+
EnumerableTableScan(table=[[scott, EMP]])
2069+
EnumerableLimit(fetch=[1])
2070+
EnumerableSort(sort0=[$0], dir0=[DESC])
2071+
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[IS NOT NULL($t0)], expr#4=[$cor0], expr#5=[$t4.DEPTNO], expr#6=[=($t5, $t0)], expr#7=[CAST($t0):INTEGER], expr#8=[10], expr#9=[=($t7, $t8)], expr#10=[AND($t6, $t9)], cs=[$t3], $condition=[$t10])
2072+
EnumerableTableScan(table=[[scott, DEPT]])
20672073
!plan
20682074

20692075
# Test filter null IN required is unknown correlated
@@ -2090,8 +2096,15 @@ select sal from "scott".emp e
20902096
(14 rows)
20912097

20922098
!ok
2093-
EnumerableCalc(expr#0..7=[{inputs}], SAL=[$t5])
2094-
EnumerableTableScan(table=[[scott, EMP]])
2099+
EnumerableCalc(expr#0..2=[{inputs}], SAL=[$t0])
2100+
EnumerableCorrelate(correlation=[$cor0], joinType=[left], requiredColumns=[{1}])
2101+
EnumerableCalc(expr#0..7=[{inputs}], SAL=[$t5], DEPTNO=[$t7])
2102+
EnumerableTableScan(table=[[scott, EMP]])
2103+
EnumerableLimit(fetch=[1])
2104+
EnumerableSort(sort0=[$0], dir0=[DESC])
2105+
EnumerableAggregate(group=[{0}])
2106+
EnumerableCalc(expr#0..2=[{inputs}], expr#3=[true], expr#4=[$cor0], expr#5=[$t4.DEPTNO], expr#6=[=($t5, $t0)], cs=[$t3], $condition=[$t6])
2107+
EnumerableTableScan(table=[[scott, DEPT]])
20952108
!plan
20962109

20972110

0 commit comments

Comments
 (0)