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

JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new eb89868850 [hive] Return null for array subscripts outside bounds 
(#11360)
eb89868850 is described below

commit eb89868850e86d6b7c641e891403698b0b8bc67c
Author: YueZhang <[email protected]>
AuthorDate: Mon Nov 23 02:39:15 2026 -0500

    [hive] Return null for array subscripts outside bounds (#11360)
---
 .../objectinspector/PaimonListObjectInspector.java |  6 ++-
 .../org/apache/paimon/hive/HiveReadITCaseBase.java | 35 ++++++++++++++
 .../PaimonListObjectInspectorTest.java             | 56 ++++++++++++++++++++++
 3 files changed, 96 insertions(+), 1 deletion(-)

diff --git 
a/paimon-hive/paimon-hive-connector-common/src/main/java/org/apache/paimon/hive/objectinspector/PaimonListObjectInspector.java
 
b/paimon-hive/paimon-hive-connector-common/src/main/java/org/apache/paimon/hive/objectinspector/PaimonListObjectInspector.java
index c65fa74307..edd04484d8 100644
--- 
a/paimon-hive/paimon-hive-connector-common/src/main/java/org/apache/paimon/hive/objectinspector/PaimonListObjectInspector.java
+++ 
b/paimon-hive/paimon-hive-connector-common/src/main/java/org/apache/paimon/hive/objectinspector/PaimonListObjectInspector.java
@@ -54,7 +54,11 @@ public class PaimonListObjectInspector implements 
ListObjectInspector {
         if (o == null) {
             return null;
         }
-        return elementGetter.getElementOrNull((InternalArray) o, i);
+        InternalArray array = (InternalArray) o;
+        if (i < 0 || i >= array.size()) {
+            return null;
+        }
+        return elementGetter.getElementOrNull(array, i);
     }
 
     @Override
diff --git 
a/paimon-hive/paimon-hive-connector-common/src/test/java/org/apache/paimon/hive/HiveReadITCaseBase.java
 
b/paimon-hive/paimon-hive-connector-common/src/test/java/org/apache/paimon/hive/HiveReadITCaseBase.java
index 6c225bafe3..c97039923b 100644
--- 
a/paimon-hive/paimon-hive-connector-common/src/test/java/org/apache/paimon/hive/HiveReadITCaseBase.java
+++ 
b/paimon-hive/paimon-hive-connector-common/src/test/java/org/apache/paimon/hive/HiveReadITCaseBase.java
@@ -23,6 +23,7 @@ import org.apache.paimon.catalog.AbstractCatalog;
 import org.apache.paimon.catalog.Identifier;
 import org.apache.paimon.data.BinaryString;
 import org.apache.paimon.data.Decimal;
+import org.apache.paimon.data.GenericArray;
 import org.apache.paimon.data.GenericMap;
 import org.apache.paimon.data.GenericRow;
 import org.apache.paimon.data.InternalRow;
@@ -500,6 +501,40 @@ public abstract class HiveReadITCaseBase extends 
HiveTestBase {
         assertThat(actual).isEqualTo(expected);
     }
 
+    @Test
+    public void testArraySubscriptOutsideBounds() throws Exception {
+        Options conf = getBasicConf();
+        conf.set(CoreOptions.FILE_FORMAT, CoreOptions.FILE_FORMAT_AVRO);
+        conf.set(CoreOptions.BUCKET, -1);
+        RowType rowType =
+                RowType.of(
+                        new DataType[] {DataTypes.INT(), 
DataTypes.ARRAY(DataTypes.INT())},
+                        new String[] {"id", "arr"});
+        Table table =
+                FileStoreTestUtils.createFileStoreTable(
+                        conf,
+                        rowType,
+                        Collections.emptyList(),
+                        Collections.emptyList(),
+                        identifier);
+        writeData(
+                table,
+                Arrays.asList(
+                        GenericRow.of(1, new GenericArray(new Integer[] {10, 
null, 30})),
+                        GenericRow.of(2, new GenericArray(new int[0])),
+                        GenericRow.of(3, null)));
+        createExternalTable();
+
+        assertThat(
+                        hiveShell.executeQuery(
+                                "SELECT id, arr[-1], arr[0], arr[1], arr[2], 
arr[3], arr[4] FROM "
+                                        + externalTable))
+                .containsExactlyInAnyOrder(
+                        "1\tNULL\t10\tNULL\t30\tNULL\tNULL",
+                        "2\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL",
+                        "3\tNULL\tNULL\tNULL\tNULL\tNULL\tNULL");
+    }
+
     private void writeData(Table table, List<InternalRow> data) throws 
Exception {
         StreamWriteBuilder streamWriteBuilder = table.newStreamWriteBuilder();
         StreamTableWrite write = streamWriteBuilder.newWrite();
diff --git 
a/paimon-hive/paimon-hive-connector-common/src/test/java/org/apache/paimon/hive/objectinspector/PaimonListObjectInspectorTest.java
 
b/paimon-hive/paimon-hive-connector-common/src/test/java/org/apache/paimon/hive/objectinspector/PaimonListObjectInspectorTest.java
index 9c376ce9c5..541cd82a43 100644
--- 
a/paimon-hive/paimon-hive-connector-common/src/test/java/org/apache/paimon/hive/objectinspector/PaimonListObjectInspectorTest.java
+++ 
b/paimon-hive/paimon-hive-connector-common/src/test/java/org/apache/paimon/hive/objectinspector/PaimonListObjectInspectorTest.java
@@ -18,12 +18,20 @@
 
 package org.apache.paimon.hive.objectinspector;
 
+import org.apache.paimon.data.BinaryArray;
 import org.apache.paimon.data.BinaryString;
 import org.apache.paimon.data.GenericArray;
+import org.apache.paimon.data.InternalArray;
 import org.apache.paimon.types.DataTypes;
 
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDF;
+import org.apache.hadoop.hive.ql.udf.generic.GenericUDFIndex;
 import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspector;
+import org.apache.hadoop.hive.serde2.objectinspector.ObjectInspectorFactory;
+import 
org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
 
 import java.util.Arrays;
 
@@ -32,6 +40,54 @@ import static org.assertj.core.api.Assertions.assertThat;
 /** Tests for {@link PaimonListObjectInspector}. */
 public class PaimonListObjectInspectorTest {
 
+    @ParameterizedTest
+    @ValueSource(ints = {Integer.MIN_VALUE, -1, 3, 4, Integer.MAX_VALUE})
+    public void testHiveArraySubscriptOutsideBounds(int index) throws 
Exception {
+        PaimonListObjectInspector inspector = new 
PaimonListObjectInspector(DataTypes.INT());
+        Integer[] values = new Integer[] {10, null, 30};
+        assertThat(
+                        ObjectInspectorFactory.getStandardListObjectInspector(
+                                        
PrimitiveObjectInspectorFactory.javaIntObjectInspector)
+                                .getListElement(Arrays.asList(values), index))
+                .isNull();
+        try (GenericUDFIndex udf = new GenericUDFIndex()) {
+            udf.initialize(
+                    new ObjectInspector[] {
+                        inspector, 
PrimitiveObjectInspectorFactory.javaIntObjectInspector
+                    });
+            GenericArray array = new GenericArray(values);
+            assertThat(evaluate(udf, array, 0)).isEqualTo(10);
+            assertThat(evaluate(udf, array, 1)).isNull();
+            assertThat(evaluate(udf, array, 2)).isEqualTo(30);
+            assertThat(evaluate(udf, array, index)).isNull();
+            assertThat(evaluate(udf, BinaryArray.fromPrimitiveArray(new int[] 
{10, 20, 30}), index))
+                    .isNull();
+            assertThat(evaluate(udf, null, index)).isNull();
+        }
+    }
+
+    @Test
+    public void testHiveSubscriptOfEmptyArray() throws Exception {
+        try (GenericUDFIndex udf = new GenericUDFIndex()) {
+            udf.initialize(
+                    new ObjectInspector[] {
+                        new PaimonListObjectInspector(DataTypes.INT()),
+                        PrimitiveObjectInspectorFactory.javaIntObjectInspector
+                    });
+            assertThat(evaluate(udf, new GenericArray(new int[0]), 
0)).isNull();
+            assertThat(evaluate(udf, BinaryArray.fromPrimitiveArray(new 
int[0]), 0)).isNull();
+        }
+    }
+
+    private static Object evaluate(GenericUDFIndex udf, InternalArray array, 
int index)
+            throws Exception {
+        return udf.evaluate(
+                new GenericUDF.DeferredObject[] {
+                    new GenericUDF.DeferredJavaObject(array),
+                    new GenericUDF.DeferredJavaObject(index)
+                });
+    }
+
     @Test
     public void testCategoryAndTypeName() {
         PaimonListObjectInspector oi = new 
PaimonListObjectInspector(DataTypes.STRING());

Reply via email to