Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.calcite.rel.logical.LogicalProject;
import org.apache.calcite.rex.RexInputRef;
import org.apache.calcite.rex.RexOver;
import org.apache.calcite.sql.SqlKind;
import org.apache.calcite.tools.RelBuilderFactory;

import org.immutables.value.Value;
Expand Down Expand Up @@ -67,8 +68,10 @@ public ProjectSetOpTransposeRule(
final Project origProject = call.rel(0);
final SetOp setOp = call.rel(1);

// cannot push project past a distinct
if (!setOp.all) {
// INTERSECT ALL and EXCEPT ALL compare rows on all fields,
// so only a permutation of all fields can be pushed past them
if (!setOp.all
|| setOp.kind != SqlKind.UNION && origProject.getPermutation() == null) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10940,17 +10940,16 @@ select name, deptno from dept
<![CDATA[
LogicalMinus(all=[true])
LogicalProject(NAME=[$0], DEPTNO=[$1])
LogicalAggregate(group=[{0, 1}], EXPR$2=[COUNT()])
LogicalProject(NAME=[$1], DEPTNO=[$0], $f2=[1])
LogicalMinus(all=[true])
LogicalAggregate(group=[{0, 1}], EXPR$2=[COUNT()])
LogicalProject(NAME=[$1], DEPTNO=[$0], $f2=[1])
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
LogicalProject(NAME=[$1], DEPTNO=[$0], EXPR$2=[1:BIGINT])
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
LogicalProject(NAME=[$0], DEPTNO=[$1])
LogicalProject(NAME=[$1], DEPTNO=[$0], EXPR$2=[1:BIGINT])
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
LogicalMinus(all=[true])
LogicalProject(NAME=[$0], DEPTNO=[$1])
LogicalMinus(all=[true])
LogicalProject(NAME=[$1], DEPTNO=[$0], EXPR$2=[1:BIGINT])
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
LogicalProject(NAME=[$0], DEPTNO=[$1])
LogicalAggregate(group=[{0, 1}], EXPR$2=[COUNT()])
LogicalProject(NAME=[$1], DEPTNO=[$0], $f2=[1])
LogicalTableScan(table=[[CATALOG, SALES, DEPT]])
Expand Down
30 changes: 30 additions & 0 deletions core/src/test/resources/sql/planner.iq
Original file line number Diff line number Diff line change
Expand Up @@ -728,4 +728,34 @@ EnumerableCalc(expr#0=[{inputs}], expr#1=[null:INTEGER], ID=[$t1], ID0=[$t0])
!plan
!set planner-rules original

# [CALCITE-7909] ProjectSetOpTransposeRule returns wrong results for EXCEPT ALL and INTERSECT ALL.
!set planner-rules "
+CoreRules.PROJECT_SET_OP_TRANSPOSE"

select y from (
select x + 1 as x, y from (values (1, 10)) as a(x, y)
except all
select x + 1 as x, y from (values (2, 10)) as b(x, y));
+----+
| Y |
+----+
| 10 |
+----+
(1 row)

!ok

select y from (
select x + 1 as x, y from (values (1, 10)) as a(x, y)
intersect all
select x + 1 as x, y from (values (2, 10)) as b(x, y));
+---+
| Y |
+---+
+---+
(0 rows)

!ok
!set planner-rules original

# End planner.iq
Loading