Skip to content

Commit c64d260

Browse files
committed
[CALCITE-7356] The MARK JOIN generated by TopDownGeneralDecorrelator needs to be adapted to RelFieldTrimmer
1 parent 583e73f commit c64d260

File tree

2 files changed

+68
-3
lines changed

2 files changed

+68
-3
lines changed

core/src/main/java/org/apache/calcite/sql2rel/RelFieldTrimmer.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -957,17 +957,25 @@ public TrimResult trimFields(
957957
switch (join.getJoinType()) {
958958
case SEMI:
959959
case ANTI:
960-
// For SemiJoins and AntiJoins only map fields from the left-side
960+
case LEFT_MARK:
961+
// For SemiJoins, AntiJoins and LeftMarkJoins only map fields from the left-side.
962+
// For LeftMarkJoins, the mark column is also mapped.
961963
if (join.getJoinType() == JoinRelType.SEMI) {
962964
relBuilder.semiJoin(newConditionExpr);
963-
} else {
965+
} else if (join.getJoinType() == JoinRelType.ANTI) {
964966
relBuilder.antiJoin(newConditionExpr);
967+
} else {
968+
relBuilder.join(join.getJoinType(), newConditionExpr, join.getVariablesSet());
965969
}
966970
Mapping inputMapping = inputMappings.get(0);
971+
int targetCount = newSystemFieldCount + inputMapping.getTargetCount();
972+
if (join.getJoinType() == JoinRelType.LEFT_MARK) {
973+
targetCount++;
974+
}
967975
mapping =
968976
Mappings.create(MappingType.INVERSE_SURJECTION,
969977
join.getRowType().getFieldCount(),
970-
newSystemFieldCount + inputMapping.getTargetCount());
978+
targetCount);
971979
for (int i = 0; i < newSystemFieldCount; ++i) {
972980
mapping.set(i, i);
973981
}
@@ -976,6 +984,9 @@ public TrimResult trimFields(
976984
for (IntPair pair : inputMapping) {
977985
mapping.set(pair.source + offset, pair.target + newOffset);
978986
}
987+
if (join.getJoinType() == JoinRelType.LEFT_MARK) {
988+
mapping.set(join.getRowType().getFieldCount() - 1, targetCount - 1);
989+
}
979990
break;
980991
case ASOF:
981992
case LEFT_ASOF:

core/src/test/resources/sql/new-decorr.iq

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,58 @@ SELECT * FROM t0 WHERE t0a <
4242

4343
!ok
4444

45+
# [CALCITE-7356] The MARK JOIN generated by TopDownGeneralDecorrelator needs to be adapted to RelFieldTrimmer
46+
!use blank
47+
CREATE TABLE emps (
48+
empid INTEGER NOT NULL,
49+
deptno INTEGER NOT NULL,
50+
name VARCHAR(10) NOT NULL,
51+
salary DECIMAL(10, 2) NOT NULL,
52+
commission INTEGER);
53+
(0 rows modified)
54+
55+
!update
56+
57+
INSERT INTO emps (empid, deptno, name, salary, commission) VALUES
58+
(100, 10, 'Bill', 10000.00, 1000),
59+
(200, 20, 'Eric', 8000.00, 500),
60+
(150, 10, 'Sebastian', 7000.00, NULL),
61+
(110, 10, 'Theodore', 11500.00, 250),
62+
(170, 30, 'Theodore', 11500.00, 250),
63+
(140, 10, 'Sebastian', 7000.00, NULL);
64+
(6 rows modified)
65+
66+
!update
67+
68+
SELECT *, EXISTS(select * from (
69+
SELECT e2.deptno FROM emps e2 where e1.commission = e2.commission) as table3
70+
where table3.deptno <> e1.deptno)
71+
from emps e1 order by empid;
72+
+-------+--------+-----------+----------+------------+--------+
73+
| EMPID | DEPTNO | NAME | SALARY | COMMISSION | EXPR$5 |
74+
+-------+--------+-----------+----------+------------+--------+
75+
| 100 | 10 | Bill | 10000.00 | 1000 | false |
76+
| 110 | 10 | Theodore | 11500.00 | 250 | true |
77+
| 140 | 10 | Sebastian | 7000.00 | | false |
78+
| 150 | 10 | Sebastian | 7000.00 | | false |
79+
| 170 | 30 | Theodore | 11500.00 | 250 | true |
80+
| 200 | 20 | Eric | 8000.00 | 500 | false |
81+
+-------+--------+-----------+----------+------------+--------+
82+
(6 rows)
83+
84+
!ok
85+
86+
!if (use_new_decorr) {
87+
EnumerableSort(sort0=[$0], dir0=[ASC])
88+
EnumerableHashJoin(condition=[AND(IS NOT DISTINCT FROM($1, $6), IS NOT DISTINCT FROM($4, $7))], joinType=[left_mark])
89+
EnumerableTableScan(table=[[BLANK, EMPS]])
90+
EnumerableCalc(expr#0..3=[{inputs}], DEPTNO=[$t2], DEPTNO0=[$t0], COMMISSION0=[$t1])
91+
EnumerableHashJoin(condition=[AND(=($1, $3), <>($2, $0))], joinType=[inner])
92+
EnumerableAggregate(group=[{1, 4}])
93+
EnumerableTableScan(table=[[BLANK, EMPS]])
94+
EnumerableCalc(expr#0..4=[{inputs}], DEPTNO=[$t1], COMMISSION=[$t4])
95+
EnumerableTableScan(table=[[BLANK, EMPS]])
96+
!plan
97+
!}
98+
4599
# End new-decorr.iq

0 commit comments

Comments
 (0)