From 2eebf5f874131ffba29d99ba83a44cc08fee457e Mon Sep 17 00:00:00 2001 From: Aleksey Plekhanov Date: Fri, 4 Oct 2024 09:20:49 +0300 Subject: [PATCH] IGNITE-23345 SQL Calcite: Fix correlated nested loop LEFT join failure - Fixes #11565. Signed-off-by: Aleksey Plekhanov --- .../rel/CorrelatedNestedLoopJoinNode.java | 19 +++++++++++++------ .../query/calcite/exec/rel/ExecutionTest.java | 5 ++++- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/CorrelatedNestedLoopJoinNode.java b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/CorrelatedNestedLoopJoinNode.java index c0e2bb2267c02..b1d78912d49e9 100644 --- a/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/CorrelatedNestedLoopJoinNode.java +++ b/modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/CorrelatedNestedLoopJoinNode.java @@ -127,7 +127,7 @@ public CorrelatedNestedLoopJoinNode(ExecutionContext ctx, RelDataType rowTy /** {@inheritDoc} */ @Override public void request(int rowsCnt) throws Exception { assert !F.isEmpty(sources()) && sources().size() == 2; - assert rowsCnt > 0 && requested == 0; + assert rowsCnt > 0 && requested == 0 : "rowsCnt=" + rowsCnt + ", requested=" + requested; checkState(); @@ -430,14 +430,21 @@ private void join() throws Exception { int notMatchedIdx = leftMatched.nextClearBit(0); - while (requested > 0 && notMatchedIdx < leftInBuf.size()) { - downstream().push(handler.concat(leftInBuf.get(notMatchedIdx), rightEmptyRow)); + state = State.IN_LOOP; - requested--; + try { + while (requested > 0 && notMatchedIdx < leftInBuf.size()) { + requested--; + + downstream().push(handler.concat(leftInBuf.get(notMatchedIdx), rightEmptyRow)); - leftMatched.set(notMatchedIdx); + leftMatched.set(notMatchedIdx); - notMatchedIdx = leftMatched.nextClearBit(notMatchedIdx + 1); + notMatchedIdx = leftMatched.nextClearBit(notMatchedIdx + 1); + } + } + finally { + state = State.IDLE; } if (requested == 0 && notMatchedIdx < leftInBuf.size()) diff --git a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/ExecutionTest.java b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/ExecutionTest.java index e201c5cf2f425..6ca45427497c8 100644 --- a/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/ExecutionTest.java +++ b/modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/exec/rel/ExecutionTest.java @@ -515,8 +515,11 @@ public void testCorrelatedNestedLoopJoin() { join.register(Arrays.asList(left, right)); + FilterNode filter = new FilterNode<>(ctx, joinRowType, r -> true); + filter.register(join); + RootNode root = new RootNode<>(ctx, joinRowType); - root.register(join); + root.register(filter); int cnt = 0; while (root.hasNext()) {