Skip to content

Commit e76e768

Browse files
IGNITE-24730 SQL Calcite: Fix correlated distribution restoring failure - Fixes #11947.
Signed-off-by: Aleksey Plekhanov <plehanov.alex@gmail.com>
1 parent bfffcc1 commit e76e768

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/rel/IgniteFilter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ public IgniteFilter(RelInput input) {
171171
// Remap correlate fields to input fields.
172172
int corrFieldsCnt = ((RexNode)corr.getNode()).getType().getFieldCount();
173173
int inputFieldsCnt = getRowType().getFieldCount();
174+
175+
// Calcite during planning sometimes can produce hash distribution for correlated nested loop joins
176+
// on keys which are not part of RexCorrelVariable type.
177+
// These keys can't be refered by RexFieldAccess and original hash distribution can't be restored,
178+
// so, just ignore correlated distribution with these keys.
179+
for (int i : corrDistr.getKeys()) {
180+
if (i >= corrFieldsCnt)
181+
return null;
182+
}
183+
174184
Mapping mapping = Mappings.create(MappingType.PARTIAL_FUNCTION, corrFieldsCnt, inputFieldsCnt);
175185

176186
List<RexNode> conds = RelOptUtil.conjunctions(RexUtil.toCnf(getCluster().getRexBuilder(), condition));

modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/tpch/TpchQ20Test.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package org.apache.ignite.internal.processors.query.calcite.integration.tpch;
1919

2020
import org.apache.ignite.internal.processors.query.calcite.integration.AbstractBasicIntegrationTest;
21-
import org.junit.Ignore;
2221
import org.junit.Test;
2322

2423
/** */
@@ -27,7 +26,6 @@ public class TpchQ20Test extends AbstractBasicIntegrationTest {
2726
* Test the Q20 TPC-H query can be planned and executed.
2827
*/
2928
@Test
30-
@Ignore("https://issues.apache.org/jira/browse/IGNITE-24730")
3129
public void testQ20() throws Exception {
3230
TpchHelper.createTables(client);
3331

modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/CorrelatedSubqueryPlannerTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.ignite.internal.processors.query.calcite.prepare.IgnitePlanner;
3636
import org.apache.ignite.internal.processors.query.calcite.prepare.PlannerPhase;
3737
import org.apache.ignite.internal.processors.query.calcite.prepare.PlanningContext;
38+
import org.apache.ignite.internal.processors.query.calcite.rel.AbstractIgniteJoin;
3839
import org.apache.ignite.internal.processors.query.calcite.rel.IgniteCorrelatedNestedLoopJoin;
3940
import org.apache.ignite.internal.processors.query.calcite.rel.IgniteExchange;
4041
import org.apache.ignite.internal.processors.query.calcite.rel.IgniteFilter;
@@ -281,6 +282,14 @@ public void testCorrelatedDistribution() throws Exception {
281282
")) FROM th1";
282283
assertPlan(sql, schema, colocatedPredicate);
283284

285+
// Correlate on top of another join.
286+
sql = "SELECT a FROM ta1 WHERE ta1.a IN (SELECT a FROM th1) AND EXISTS (SELECT 1 FROM ta2 WHERE ta2.b = ta1.a)";
287+
assertPlan(sql, schema, hasChildThat(isInstanceOf(IgniteCorrelatedNestedLoopJoin.class)
288+
.and(input(0, isInstanceOf(AbstractIgniteJoin.class)))
289+
.and(input(1, isInstanceOf(IgniteColocatedAggregateBase.class)
290+
.and(hasChildThat(isInstanceOf(IgniteExchange.class)).negate())
291+
))));
292+
284293
// Condition on not colocated column.
285294
sql = "SELECT a FROM ta1 WHERE EXISTS (SELECT a FROM ta2 WHERE ta2.a = ta1.a)";
286295
assertPlan(sql, schema, colocatedPredicate.negate());

0 commit comments

Comments
 (0)