You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by zs...@apache.org on 2025/07/04 08:35:53 UTC

(ignite-3) branch main updated: IGNITE-25658 Sql. Cannot cancel a query fragment with a sequence of NestedLoopJoin operators (#6172)

This is an automated email from the ASF dual-hosted git repository.

zstan pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git


The following commit(s) were added to refs/heads/main by this push:
     new beb15d5c4c7 IGNITE-25658 Sql. Cannot cancel a query fragment with a sequence of NestedLoopJoin operators (#6172)
beb15d5c4c7 is described below

commit beb15d5c4c760b7baa30c21241dc13bcb8aae13f
Author: Evgeniy Stanilovskiy <st...@gmail.com>
AuthorDate: Fri Jul 4 11:35:48 2025 +0300

    IGNITE-25658 Sql. Cannot cancel a query fragment with a sequence of NestedLoopJoin operators (#6172)
---
 .../ignite/internal/sql/api/ItSqlApiBaseTest.java  | 51 ++++++++++++
 .../sql/engine/exec/rel/NestedLoopJoinNode.java    | 93 +++++++++++-----------
 .../internal/sql/BaseSqlIntegrationTest.java       |  2 +-
 3 files changed, 99 insertions(+), 47 deletions(-)

diff --git a/modules/sql-engine/src/integrationTest/java/org/apache/ignite/internal/sql/api/ItSqlApiBaseTest.java b/modules/sql-engine/src/integrationTest/java/org/apache/ignite/internal/sql/api/ItSqlApiBaseTest.java
index 1c60fa55c19..f92bc0422f4 100644
--- a/modules/sql-engine/src/integrationTest/java/org/apache/ignite/internal/sql/api/ItSqlApiBaseTest.java
+++ b/modules/sql-engine/src/integrationTest/java/org/apache/ignite/internal/sql/api/ItSqlApiBaseTest.java
@@ -49,6 +49,7 @@ import java.util.concurrent.CyclicBarrier;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
+import org.apache.calcite.rel.core.JoinRelType;
 import org.apache.ignite.Ignite;
 import org.apache.ignite.internal.catalog.commands.CatalogUtils;
 import org.apache.ignite.internal.sql.BaseSqlIntegrationTest;
@@ -85,6 +86,8 @@ import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.AssertionFailureBuilder;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.EnumSource;
+import org.junit.jupiter.params.provider.EnumSource.Mode;
 import org.junit.jupiter.params.provider.ValueSource;
 
 /**
@@ -1009,6 +1012,54 @@ public abstract class ItSqlApiBaseTest extends BaseSqlIntegrationTest {
         assertThat(txManager().pending(), is(0));
     }
 
+    @ParameterizedTest
+    @EnumSource(value = JoinRelType.class, mode = Mode.INCLUDE, names = {"INNER", "LEFT", "RIGHT", "FULL"})
+    public void cancelLongRunningJoinStatement(JoinRelType joinType) throws InterruptedException {
+        IgniteSql sql = igniteSql();
+
+        sql.executeScript("CREATE TABLE TEST(ID INT PRIMARY KEY, VAL INT)");
+        for (int i = 0; i < 10; i++) {
+            sql.executeScript("INSERT INTO TEST VALUES (?, ?)", i, i);
+        }
+
+        String join10x = String.format("SELECT count(*) FROM ("
+                + "SELECT "
+                + "t1.ID AS ID1, t2.ID AS ID2, t3.ID AS ID3, t4.ID AS ID4, t5.ID AS ID5, "
+                + "t6.ID AS ID6, t7.ID AS ID7, t8.ID AS ID8, t9.ID AS ID9, t10.ID AS ID10 "
+                + "FROM TEST t1 "
+                + "%s JOIN TEST t2  ON 1 = 1 "
+                + "%s JOIN TEST t3  ON 1 = 1 "
+                + "%s JOIN TEST t4  ON 1 = 1 "
+                + "%s JOIN TEST t5  ON 1 = 1 "
+                + "%s JOIN TEST t6  ON 1 = 1 "
+                + "%s JOIN TEST t7  ON 1 = 1 "
+                + "%s JOIN TEST t8  ON 1 = 1 "
+                + "%s JOIN TEST t9  ON 1 = 1 "
+                + "%s JOIN TEST t10 ON 1 = 1 "
+                + ")", joinType, joinType, joinType, joinType, joinType, joinType, joinType, joinType, joinType);
+
+        {
+            Statement statement = sql.statementBuilder()
+                    .query(join10x)
+                    .build();
+
+            CancelHandle cancelHandle = CancelHandle.create();
+            CompletableFuture<?> fut = sql.executeAsync(null, cancelHandle.token(), statement);
+
+            // Wait until the query starts executing.
+            waitUntilRunningQueriesCount(greaterThan(0));
+            // Wait a bit more to improve failure rate.
+            Thread.sleep(500);
+
+            cancelHandle.cancel();
+
+            // Query was actually cancelled.
+            waitUntilRunningQueriesCount(is(0));
+            expectQueryCancelled(() -> await(fut));
+            assertThat(txManager().pending(), is(0));
+        }
+    }
+
     @Test
     public void testQueryTimeoutIsPropagatedFromTheServer() throws Exception {
         Statement stmt = igniteSql().statementBuilder()
diff --git a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/rel/NestedLoopJoinNode.java b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/rel/NestedLoopJoinNode.java
index d5b69fbac60..8792861c3be 100644
--- a/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/rel/NestedLoopJoinNode.java
+++ b/modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/rel/NestedLoopJoinNode.java
@@ -42,6 +42,8 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
 
     final List<RowT> rightMaterialized = new ArrayList<>(inBufSize);
 
+    int processed = 0;
+
     /**
      * Creates NestedLoopJoinNode.
      *
@@ -173,7 +175,7 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
 
         @Override
         protected void pushLeft(RowT row) throws Exception {
-            // Prefent fetching left if right is empty.
+            // Prevent fetching left if right is empty.
             if (waitingRight == NOT_WAITING && rightMaterialized.isEmpty()) {
                 waitingLeft--;
 
@@ -194,18 +196,14 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
         protected void join() throws Exception {
             if (waitingRight == NOT_WAITING) {
                 inLoop = true;
-                int processed = 0;
                 try {
                     while (requested > 0 && (left != null || !leftInBuf.isEmpty())) {
                         if (left == null) {
                             left = leftInBuf.remove();
                         }
-
                         while (requested > 0 && rightIdx < rightMaterialized.size()) {
-                            if (processed++ > inBufSize) {
+                            if (rescheduleJoin()) {
                                 // Allow others to do their job.
-                                execute(this::join);
-
                                 return;
                             }
 
@@ -222,6 +220,11 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                             left = null;
                             rightIdx = 0;
                         }
+
+                        if (rightMaterialized.isEmpty() && rescheduleJoin()) {
+                            // Allow others to do their job.
+                            return;
+                        }
                     }
                 } finally {
                     inLoop = false;
@@ -276,7 +279,6 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
         protected void join() throws Exception {
             if (waitingRight == NOT_WAITING) {
                 inLoop = true;
-                int processed = 0;
                 try {
                     while (requested > 0 && (left != null || !leftInBuf.isEmpty())) {
                         if (left == null) {
@@ -286,10 +288,8 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                         }
 
                         while (requested > 0 && rightIdx < rightMaterialized.size()) {
-                            if (processed++ > inBufSize) {
+                            if (rescheduleJoin()) {
                                 // Allow others to do their job.
-                                execute(this::join);
-
                                 return;
                             }
 
@@ -312,8 +312,6 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                                 wasPushed = true;
 
                                 downstream().push(outputProjection.project(context(), left, rightRowFactory.create()));
-
-                                processed++;
                             }
 
                             if (matched || wasPushed) {
@@ -322,10 +320,8 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                             }
                         }
 
-                        if (processed > inBufSize) {
+                        if (rightMaterialized.isEmpty() && rescheduleJoin()) {
                             // Allow others to do their job.
-                            execute(this::join);
-
                             return;
                         }
                     }
@@ -381,7 +377,7 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
 
         @Override
         protected void pushLeft(RowT row) throws Exception {
-            // Prefent fetching left if right is empty.
+            // Prevent fetching left if right is empty.
             if (waitingRight == NOT_WAITING && rightMaterialized.isEmpty()) {
                 waitingLeft--;
 
@@ -409,7 +405,6 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                 }
 
                 inLoop = true;
-                int processed = 0;
                 try {
                     while (requested > 0 && (left != null || !leftInBuf.isEmpty())) {
                         if (left == null) {
@@ -417,10 +412,8 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                         }
 
                         while (requested > 0 && rightIdx < rightMaterialized.size()) {
-                            if (processed++ > inBufSize) {
+                            if (rescheduleJoin()) {
                                 // Allow others to do their job.
-                                execute(this::join);
-
                                 return;
                             }
 
@@ -441,6 +434,12 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                             left = null;
                             rightIdx = 0;
                         }
+
+                        // Allow others to do their job.
+                        if (rightMaterialized.isEmpty() && rescheduleJoin()) {
+                            // Allow others to do their job.
+                            return;
+                        }
                     }
                 } finally {
                     inLoop = false;
@@ -454,10 +453,8 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                     inLoop = true;
                     try {
                         while (requested > 0 && rightNotMatchedIt.hasNext()) {
-                            if (processed++ > inBufSize) {
+                            if (rescheduleJoin()) {
                                 // Allow others to do their job.
-                                execute(this::join);
-
                                 return;
                             }
 
@@ -544,7 +541,6 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                 }
 
                 inLoop = true;
-                int processed = 0;
                 try {
                     while (requested > 0 && (left != null || !leftInBuf.isEmpty())) {
                         if (left == null) {
@@ -554,10 +550,8 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                         }
 
                         while (requested > 0 && rightIdx < rightMaterialized.size()) {
-                            if (processed++ > inBufSize) {
+                            if (rescheduleJoin()) {
                                 // Allow others to do their job.
-                                execute(this::join);
-
                                 return;
                             }
 
@@ -583,7 +577,6 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                                 wasPushed = true;
 
                                 downstream().push(outputProjection.project(context(), left, rightRowFactory.create()));
-                                processed++;
                             }
 
                             if (leftMatched || wasPushed) {
@@ -592,10 +585,8 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                             }
                         }
 
-                        if (processed >= inBufSize) {
+                        if (rightMaterialized.isEmpty() && rescheduleJoin()) {
                             // Allow others to do their job.
-                            execute(this::join);
-
                             return;
                         }
                     }
@@ -617,10 +608,8 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                             requested--;
                             downstream().push(row);
 
-                            if (processed++ >= inBufSize) {
+                            if (rescheduleJoin()) {
                                 // Allow others to do their job.
-                                execute(this::join);
-
                                 return;
                             }
                         }
@@ -662,7 +651,7 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
 
         @Override
         protected void pushLeft(RowT row) throws Exception {
-            // Prefent fetching left if right is empty.
+            // Prevent fetching left if right is empty.
             if (waitingRight == NOT_WAITING && rightMaterialized.isEmpty()) {
                 waitingLeft--;
 
@@ -690,12 +679,9 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
 
                     boolean matched = false;
 
-                    int processed = 0;
                     while (!matched && requested > 0 && rightIdx < rightMaterialized.size()) {
-                        if (processed++ > inBufSize) {
+                        if (rescheduleJoin()) {
                             // Allow others to do their job.
-                            execute(this::join);
-
                             return;
                         }
 
@@ -713,6 +699,11 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                         left = null;
                         rightIdx = 0;
                     }
+
+                    if (rightMaterialized.isEmpty() && rescheduleJoin()) {
+                        // Allow others to do their job.
+                        return;
+                    }
                 }
             }
 
@@ -745,7 +736,6 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
         protected void join() throws Exception {
             if (waitingRight == NOT_WAITING) {
                 inLoop = true;
-                int processed = 0;
                 try {
                     while (requested > 0 && (left != null || !leftInBuf.isEmpty())) {
                         if (left == null) {
@@ -755,10 +745,8 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                         boolean matched = false;
 
                         while (rightIdx < rightMaterialized.size()) {
-                            if (processed++ > inBufSize) {
+                            if (rescheduleJoin()) {
                                 // Allow others to do their job.
-                                execute(this::join);
-
                                 return;
                             }
 
@@ -776,10 +764,8 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
                         left = null;
                         rightIdx = 0;
 
-                        if (rightMaterialized.isEmpty() && processed++ >= inBufSize) {
+                        if (rightMaterialized.isEmpty() && rescheduleJoin()) {
                             // Allow others to do their job.
-                            execute(this::join);
-
                             return;
                         }
                     }
@@ -811,4 +797,19 @@ public abstract class NestedLoopJoinNode<RowT> extends AbstractRightMaterialized
     protected boolean endOfRight() {
         return waitingRight == NOT_WAITING;
     }
+
+    /**
+     * The method is called to break down the execution of the long join process into smaller chunks, to allow other tasks do their job.
+     *
+     * @return {@code true} if the next {@link #join()} task was enqueued to executor, {@code false} otherwise.
+     */
+    protected boolean rescheduleJoin() {
+        if (processed++ > inBufSize) {
+            execute(this::join);
+            processed = 0;
+
+            return true;
+        }
+        return false;
+    }
 }
diff --git a/modules/sql-engine/src/testFixtures/java/org/apache/ignite/internal/sql/BaseSqlIntegrationTest.java b/modules/sql-engine/src/testFixtures/java/org/apache/ignite/internal/sql/BaseSqlIntegrationTest.java
index fec952eda39..4febb9446cd 100644
--- a/modules/sql-engine/src/testFixtures/java/org/apache/ignite/internal/sql/BaseSqlIntegrationTest.java
+++ b/modules/sql-engine/src/testFixtures/java/org/apache/ignite/internal/sql/BaseSqlIntegrationTest.java
@@ -116,7 +116,7 @@ public abstract class BaseSqlIntegrationTest extends ClusterPerClassIntegrationT
     }
 
     /**
-     * Used for query with aggregates checks, disables other aggregate rules for executing exact agregate algo.
+     * Used for query with aggregates checks, disables other aggregate rules for executing exact aggregate algo.
      *
      * @param qry Query for check.
      * @param aggregateType Type of aggregate algo.