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

mihaibudiu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new 052a35015c [CALCITE-5998] The SAFE_OFFSET operator can cause an index 
out of bounds exception
052a35015c is described below

commit 052a35015c56db2927727afd7e08ed7a18a26ace
Author: Mihai Budiu <[email protected]>
AuthorDate: Wed Aug 5 21:15:25 2026 -0700

    [CALCITE-5998] The SAFE_OFFSET operator can cause an index out of bounds 
exception
    
    Signed-off-by: Mihai Budiu <[email protected]>
---
 .../calcite/test/SqlOperatorUnparseTest.java       | 11 -----------
 .../org/apache/calcite/test/SqlOperatorTest.java   | 22 ++++++++++++++++++++++
 2 files changed, 22 insertions(+), 11 deletions(-)

diff --git 
a/core/src/test/java/org/apache/calcite/test/SqlOperatorUnparseTest.java 
b/core/src/test/java/org/apache/calcite/test/SqlOperatorUnparseTest.java
index b4e4e66769..f7c5f2e9a6 100644
--- a/core/src/test/java/org/apache/calcite/test/SqlOperatorUnparseTest.java
+++ b/core/src/test/java/org/apache/calcite/test/SqlOperatorUnparseTest.java
@@ -24,8 +24,6 @@
 import org.apache.calcite.sql.test.SqlOperatorFixture;
 import org.apache.calcite.sql.test.SqlTestFactory;
 
-import org.junit.jupiter.api.Disabled;
-
 import java.util.function.Consumer;
 import java.util.function.UnaryOperator;
 
@@ -105,13 +103,4 @@ String rewrite(StringAndPos sap) throws SqlParseException {
       }
     }
   }
-
-  // Every test that is Disabled below corresponds to a bug.
-  // These tests should just be deleted when the corresponding bugs are fixed.
-
-  @Override @Disabled("https://issues.apache.org/jira/browse/CALCITE-5998 "
-      + "The SAFE_OFFSET operator can cause an index out of bounds exception")
-  void testSafeOffsetOperator() {
-    super.testSafeOffsetOperator();
-  }
 }
diff --git a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java 
b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
index 89a5ad1dad..7db6ab298d 100644
--- a/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
+++ b/testkit/src/main/java/org/apache/calcite/test/SqlOperatorTest.java
@@ -13851,6 +13851,17 @@ private static void 
checkArrayConcatAggFuncFails(SqlOperatorFixture t) {
     f.checkScalar("ARRAY[2,4,6][SAFE_OFFSET(-1)]", isNullValue(), "INTEGER");
     f.checkScalar("ARRAY[2,4,6][SAFE_OFFSET(5)]", isNullValue(), "INTEGER");
     f.checkNull("ARRAY[2,4,6][SAFE_OFFSET(null)]");
+    // Test case for [CALCITE-5998] The SAFE_OFFSET operator can cause
+    // an index out of bounds exception; the index is not constant
+    f.check("select ARRAY[p3,p2,p1][SAFE_OFFSET(p0)]\n"
+            + "from (values (-1, 6, 4, 2)) as t(p0, p1, p2, p3)",
+        "INTEGER", isNullValue());
+    f.check("select ARRAY[p3,p2,p1][SAFE_OFFSET(p1)]\n"
+            + "from (values (-1, 6, 4, 2)) as t(p0, p1, p2, p3)",
+        "INTEGER", isNullValue());
+    f.check("select ARRAY[p3,p2,p1][SAFE_OFFSET(p3)]\n"
+            + "from (values (-1, 6, 4, 2)) as t(p0, p1, p2, p3)",
+        "INTEGER", 6);
     f.checkFails("^map['foo', 3, 'bar', 7][safe_offset('bar')]^",
         "Cannot apply 'SAFE_OFFSET' to arguments of type 
'SAFE_OFFSET\\(<MAP<CHAR\\(3\\)"
             + ", INTEGER>>, <CHAR\\(3\\)>\\)'\\. Supported form\\(s\\): "
@@ -13869,6 +13880,17 @@ private static void 
checkArrayConcatAggFuncFails(SqlOperatorFixture t) {
     f.checkScalar("ARRAY[2,4,6][SAFE_ORDINAL(-1)]", isNullValue(), "INTEGER");
     f.checkScalar("ARRAY[2,4,6][SAFE_ORDINAL(5)]", isNullValue(), "INTEGER");
     f.checkNull("ARRAY[2,4,6][SAFE_ORDINAL(null)]");
+    // Test case for [CALCITE-5998] The SAFE_OFFSET operator can cause
+    // an index out of bounds exception; the index is not constant
+    f.check("select ARRAY[p3,p2,p1][SAFE_ORDINAL(p0)]\n"
+            + "from (values (-1, 6, 4, 2)) as t(p0, p1, p2, p3)",
+        "INTEGER", isNullValue());
+    f.check("select ARRAY[p3,p2,p1][SAFE_ORDINAL(p1)]\n"
+            + "from (values (-1, 6, 4, 2)) as t(p0, p1, p2, p3)",
+        "INTEGER", isNullValue());
+    f.check("select ARRAY[p3,p2,p1][SAFE_ORDINAL(p3)]\n"
+            + "from (values (-1, 6, 4, 2)) as t(p0, p1, p2, p3)",
+        "INTEGER", 4);
     f.checkFails("^map['foo', 3, 'bar', 7][safe_ordinal('bar')]^",
         "Cannot apply 'SAFE_ORDINAL' to arguments of type 
'SAFE_ORDINAL\\(<MAP<CHAR\\(3\\)"
             + ", INTEGER>>, <CHAR\\(3\\)>\\)'\\. Supported form\\(s\\): "

Reply via email to