From 3c0e0eabc7fdc11b0812a64558961a51bd6ee32d Mon Sep 17 00:00:00 2001 From: yangjie01 Date: Sat, 21 Nov 2026 21:37:11 +0800 Subject: [PATCH 1/2] [FLINK] Keep struct fields aligned with their rows when a row is null --- .../gluten/vectorized/ArrowVectorWriter.java | 27 +++- .../table/runtime/stream/custom/ScanTest.java | 9 ++ .../vectorized/ArrowVectorWriterTest.java | 118 ++++++++++++++++++ 3 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 gluten-flink/ut/src/test/java/org/apache/gluten/vectorized/ArrowVectorWriterTest.java diff --git a/gluten-flink/runtime/src/main/java/org/apache/gluten/vectorized/ArrowVectorWriter.java b/gluten-flink/runtime/src/main/java/org/apache/gluten/vectorized/ArrowVectorWriter.java index d30280077f5..3a480881ee5 100644 --- a/gluten-flink/runtime/src/main/java/org/apache/gluten/vectorized/ArrowVectorWriter.java +++ b/gluten-flink/runtime/src/main/java/org/apache/gluten/vectorized/ArrowVectorWriter.java @@ -128,6 +128,10 @@ public void writeArray(ArrayData arrayData) { throw new UnsupportedOperationException("writeArray is not supported"); } + void writeNull() { + throw new UnsupportedOperationException("writeNull is not supported"); + } + int getValueCount() { return valueCount; } @@ -245,21 +249,31 @@ protected BaseVectorWriter(FieldVector vector) { protected abstract void setValue(int index, V value); + protected void setNull(int index) { + this.typedVector.setNull(index); + } + @Override public void write(int fieldIndex, RowData rowData) { if (rowData.isNullAt(fieldIndex)) { - this.typedVector.setNull(valueCount); + setNull(valueCount); } else { setValue(valueCount, getValue(rowData, fieldIndex)); } valueCount++; } + @Override + void writeNull() { + setNull(valueCount); + valueCount++; + } + @Override public void writeArray(ArrayData arrayData) { for (int i = 0; i < arrayData.size(); i++) { if (arrayData.isNullAt(i)) { - this.typedVector.setNull(valueCount); + setNull(valueCount); } else { setValue(valueCount, getValue(arrayData, i)); } @@ -519,6 +533,15 @@ protected void setValue(int index, RowData value) { } } + // The fields take a null for a null row too, so they stay aligned with the struct's rows. + @Override + protected void setNull(int index) { + this.typedVector.setNull(index); + for (ArrowVectorWriter fieldWriter : fieldWriters) { + fieldWriter.writeNull(); + } + } + @Override public void finish() { this.typedVector.setValueCount(valueCount); diff --git a/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/ScanTest.java b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/ScanTest.java index 7d097763d6f..05675beda32 100644 --- a/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/ScanTest.java +++ b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/ScanTest.java @@ -176,6 +176,15 @@ void testNullScan() { query = "select a, b from nullArrayTbl where a > 0"; runAndCheck(query, Arrays.asList("+I[1, null]", "+I[2, [null, 5, 6]]", "+I[3, [7, 8, null]]")); + rows = + Arrays.asList( + Row.of(1, new Row[] {Row.of(1, 2), null, Row.of(3, 4)}), + Row.of(2, new Row[] {null, Row.of(5, 6)})); + createSimpleBoundedValuesTable("nullStrctArrayTbl", "a int, b array>", rows); + query = "select a, b from nullStrctArrayTbl where a > 0"; + runAndCheck( + query, Arrays.asList("+I[1, [+I[1, 2], null, +I[3, 4]]]", "+I[2, [null, +I[5, 6]]]")); + rows = Arrays.asList( Row.of(1, null), diff --git a/gluten-flink/ut/src/test/java/org/apache/gluten/vectorized/ArrowVectorWriterTest.java b/gluten-flink/ut/src/test/java/org/apache/gluten/vectorized/ArrowVectorWriterTest.java new file mode 100644 index 00000000000..bd00a516324 --- /dev/null +++ b/gluten-flink/ut/src/test/java/org/apache/gluten/vectorized/ArrowVectorWriterTest.java @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.gluten.vectorized; + +import org.apache.gluten.util.LogicalTypeConverter; + +import org.apache.flink.table.data.ArrayData; +import org.apache.flink.table.data.GenericArrayData; +import org.apache.flink.table.data.GenericMapData; +import org.apache.flink.table.data.GenericRowData; +import org.apache.flink.table.data.MapData; +import org.apache.flink.table.data.RowData; +import org.apache.flink.table.data.StringData; +import org.apache.flink.table.types.logical.ArrayType; +import org.apache.flink.table.types.logical.IntType; +import org.apache.flink.table.types.logical.LogicalType; +import org.apache.flink.table.types.logical.MapType; +import org.apache.flink.table.types.logical.RowType; +import org.apache.flink.table.types.logical.VarCharType; + +import org.apache.arrow.memory.BufferAllocator; +import org.apache.arrow.memory.RootAllocator; +import org.apache.arrow.vector.FieldVector; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.LinkedHashMap; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ArrowVectorWriterTest { + private static final RowType ROW_TYPE = + RowType.of( + new LogicalType[] {new IntType(), new VarCharType(VarCharType.MAX_LENGTH)}, + new String[] {"x", "y"}); + + private BufferAllocator allocator; + + @BeforeEach + public void setUp() { + allocator = new RootAllocator(Long.MAX_VALUE); + } + + @AfterEach + public void tearDown() { + allocator.close(); + } + + // Writes `value` as the only row of a column of `type` and reads it back from the vector. + private Object writeAndRead(LogicalType type, Object value) { + ArrowVectorWriter writer = + ArrowVectorWriter.create("c", LogicalTypeConverter.toVLType(type), allocator); + writer.write(0, GenericRowData.of(value)); + writer.finish(); + try (FieldVector vector = writer.getVector()) { + return ArrowVectorAccessor.create(vector).get(0); + } + } + + private static RowData row(int x, String y) { + return GenericRowData.of(x, StringData.fromString(y)); + } + + private static void assertRow(RowData actual, int x, String y) { + assertThat(actual).isNotNull(); + assertThat(actual.isNullAt(0)).isFalse(); + assertThat(actual.getInt(0)).isEqualTo(x); + assertThat(actual.isNullAt(1)).isFalse(); + assertThat(actual.getString(1).toString()).isEqualTo(y); + } + + @Test + public void testArrayWithNullRowElement() { + Object[] elements = {row(1, "a"), null, row(2, "b"), row(3, "c")}; + ArrayData array = + (ArrayData) writeAndRead(new ArrayType(ROW_TYPE), new GenericArrayData(elements)); + + assertThat(array.size()).isEqualTo(4); + assertRow(array.getRow(0, 2), 1, "a"); + assertThat(array.isNullAt(1)).isTrue(); + assertRow(array.getRow(2, 2), 2, "b"); + assertRow(array.getRow(3, 2), 3, "c"); + } + + @Test + public void testMapWithNullRowValue() { + Map entries = new LinkedHashMap<>(); + entries.put(1, row(1, "a")); + entries.put(2, null); + entries.put(3, row(3, "c")); + MapData map = + (MapData) writeAndRead(new MapType(new IntType(), ROW_TYPE), new GenericMapData(entries)); + + ArrayData keys = map.keyArray(); + ArrayData values = map.valueArray(); + assertThat(map.size()).isEqualTo(3); + assertThat(keys.getInt(2)).isEqualTo(3); + assertRow(values.getRow(0, 2), 1, "a"); + assertThat(values.isNullAt(1)).isTrue(); + assertRow(values.getRow(2, 2), 3, "c"); + } +} From 1392c5845de75dfaaef7e27e271cb6f47572ae58 Mon Sep 17 00:00:00 2001 From: yangjie01 Date: Sat, 21 Nov 2026 22:17:27 +0800 Subject: [PATCH 2/2] [FLINK] Test the null-row writer through the Velox round trip and nested fields --- .../table/runtime/stream/custom/ScanTest.java | 9 ---- .../vectorized/ArrowVectorWriterTest.java | 50 +++++++++++++++++++ .../FlinkRowToVLVectorConvertorTest.java | 39 +++++++++++++++ 3 files changed, 89 insertions(+), 9 deletions(-) diff --git a/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/ScanTest.java b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/ScanTest.java index 05675beda32..7d097763d6f 100644 --- a/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/ScanTest.java +++ b/gluten-flink/ut/src/test/java/org/apache/gluten/table/runtime/stream/custom/ScanTest.java @@ -176,15 +176,6 @@ void testNullScan() { query = "select a, b from nullArrayTbl where a > 0"; runAndCheck(query, Arrays.asList("+I[1, null]", "+I[2, [null, 5, 6]]", "+I[3, [7, 8, null]]")); - rows = - Arrays.asList( - Row.of(1, new Row[] {Row.of(1, 2), null, Row.of(3, 4)}), - Row.of(2, new Row[] {null, Row.of(5, 6)})); - createSimpleBoundedValuesTable("nullStrctArrayTbl", "a int, b array>", rows); - query = "select a, b from nullStrctArrayTbl where a > 0"; - runAndCheck( - query, Arrays.asList("+I[1, [+I[1, 2], null, +I[3, 4]]]", "+I[2, [null, +I[5, 6]]]")); - rows = Arrays.asList( Row.of(1, null), diff --git a/gluten-flink/ut/src/test/java/org/apache/gluten/vectorized/ArrowVectorWriterTest.java b/gluten-flink/ut/src/test/java/org/apache/gluten/vectorized/ArrowVectorWriterTest.java index bd00a516324..467d3a769e4 100644 --- a/gluten-flink/ut/src/test/java/org/apache/gluten/vectorized/ArrowVectorWriterTest.java +++ b/gluten-flink/ut/src/test/java/org/apache/gluten/vectorized/ArrowVectorWriterTest.java @@ -115,4 +115,54 @@ public void testMapWithNullRowValue() { assertThat(values.isNullAt(1)).isTrue(); assertRow(values.getRow(2, 2), 3, "c"); } + + @Test + public void testArrayWithNullRowElementOfNestedFields() { + RowType nested = + RowType.of( + new LogicalType[] { + new ArrayType(new IntType()), RowType.of(new LogicalType[] {new IntType()}) + }, + new String[] {"a", "r"}); + Object[] elements = { + GenericRowData.of(new GenericArrayData(new Object[] {1, 2}), GenericRowData.of(10)), + null, + GenericRowData.of(new GenericArrayData(new Object[] {3}), GenericRowData.of(30)) + }; + ArrayData array = + (ArrayData) writeAndRead(new ArrayType(nested), new GenericArrayData(elements)); + + assertThat(array.getRow(0, 2).getRow(1, 1).getInt(0)).isEqualTo(10); + assertThat(array.isNullAt(1)).isTrue(); + RowData last = array.getRow(2, 2); + assertThat(last.getArray(0).size()).isEqualTo(1); + assertThat(last.getArray(0).getInt(0)).isEqualTo(3); + assertThat(last.getRow(1, 1).getInt(0)).isEqualTo(30); + } + + @Test + public void testArrayWithNullRowFieldInElement() { + RowType inner = RowType.of(new LogicalType[] {new IntType()}, new String[] {"x"}); + RowType element = RowType.of(new LogicalType[] {inner}, new String[] {"r"}); + Object[] elements = {GenericRowData.of((Object) null), GenericRowData.of(GenericRowData.of(5))}; + ArrayData array = + (ArrayData) writeAndRead(new ArrayType(element), new GenericArrayData(elements)); + + assertThat(array.getRow(0, 1).isNullAt(0)).isTrue(); + assertThat(array.getRow(1, 1).getRow(0, 1).getInt(0)).isEqualTo(5); + } + + @Test + public void testNullRowKeepsFieldsAsLongAsTheRow() { + ArrowVectorWriter writer = + ArrowVectorWriter.create("c", LogicalTypeConverter.toVLType(ROW_TYPE), allocator); + writer.write(0, GenericRowData.of((Object) null)); + writer.finish(); + try (FieldVector vector = writer.getVector()) { + assertThat(vector.getValueCount()).isEqualTo(1); + for (FieldVector field : vector.getChildrenFromFields()) { + assertThat(field.getValueCount()).isEqualTo(1); + } + } + } } diff --git a/gluten-flink/ut/src/test/java/org/apache/gluten/vectorized/FlinkRowToVLVectorConvertorTest.java b/gluten-flink/ut/src/test/java/org/apache/gluten/vectorized/FlinkRowToVLVectorConvertorTest.java index 36558d94e50..5ccafcf4309 100644 --- a/gluten-flink/ut/src/test/java/org/apache/gluten/vectorized/FlinkRowToVLVectorConvertorTest.java +++ b/gluten-flink/ut/src/test/java/org/apache/gluten/vectorized/FlinkRowToVLVectorConvertorTest.java @@ -24,9 +24,12 @@ import io.github.zhztheplayer.velox4j.memory.MemoryManager; import io.github.zhztheplayer.velox4j.session.Session; +import org.apache.flink.table.data.ArrayData; +import org.apache.flink.table.data.GenericArrayData; import org.apache.flink.table.data.GenericRowData; import org.apache.flink.table.data.RowData; import org.apache.flink.table.data.StringData; +import org.apache.flink.table.types.logical.ArrayType; import org.apache.flink.table.types.logical.IntType; import org.apache.flink.table.types.logical.LogicalType; import org.apache.flink.table.types.logical.RowType; @@ -170,4 +173,40 @@ public void testFromRowDataAppendsRowKindColumnForAllKinds() { } } } + + @Test + public void testRoundTripArrayWithNullRowElement() { + RowType elementType = + RowType.of( + new LogicalType[] {new IntType(), new VarCharType(VarCharType.MAX_LENGTH)}, + new String[] {"x", "y"}); + io.github.zhztheplayer.velox4j.type.RowType type = + (io.github.zhztheplayer.velox4j.type.RowType) + org.apache.gluten.util.LogicalTypeConverter.toVLType( + RowType.of( + new LogicalType[] {new IntType(), new ArrayType(elementType)}, + new String[] {"id", "items"})); + Object[] elements = { + GenericRowData.of(1, StringData.fromString("a")), + null, + GenericRowData.of(2, StringData.fromString("b")), + GenericRowData.of(3, StringData.fromString("c")) + }; + RowData input = GenericRowData.of(7, new GenericArrayData(elements)); + RowVector rv = FlinkRowToVLVectorConvertor.fromRowData(input, allocator, session, type); + try { + List rows = FlinkRowToVLVectorConvertor.toRowData(rv, allocator, type); + assertThat(rows).hasSize(1); + ArrayData items = rows.get(0).getArray(1); + assertThat(items.size()).isEqualTo(4); + assertThat(items.isNullAt(1)).isTrue(); + for (int i : new int[] {0, 2, 3}) { + RowData item = items.getRow(i, 2); + assertThat(item.getInt(0)).isEqualTo(((RowData) elements[i]).getInt(0)); + assertThat(item.getString(1)).isEqualTo(((RowData) elements[i]).getString(1)); + } + } finally { + rv.close(); + } + } }