-
Notifications
You must be signed in to change notification settings - Fork 210
[AURON #1850] Add ArrowFieldWriters for temporal and composite types #2086
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* | ||
| * 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.auron.flink.arrow.writers; | ||
|
|
||
| import java.util.Objects; | ||
| import org.apache.arrow.vector.complex.ListVector; | ||
| import org.apache.flink.table.data.ArrayData; | ||
| import org.apache.flink.table.data.RowData; | ||
|
|
||
| /** | ||
| * {@link ArrowFieldWriter} for arrays ({@link ListVector}). | ||
| * | ||
| * <p>Holds an {@code elementWriter} that writes each array element. The element writer operates on | ||
| * {@link ArrayData} since array elements are accessed via {@link ArrayData} interface. | ||
| * | ||
| * @param <T> the input data type | ||
| */ | ||
| public abstract class ArrayWriter<T> extends ArrowFieldWriter<T> { | ||
|
|
||
| /** Creates an ArrayWriter that reads from {@link RowData}. */ | ||
| public static ArrayWriter<RowData> forRow(ListVector listVector, ArrowFieldWriter<ArrayData> elementWriter) { | ||
| return new ArrayWriterForRow(listVector, elementWriter); | ||
| } | ||
|
|
||
| /** Creates an ArrayWriter that reads from {@link ArrayData}. */ | ||
| public static ArrayWriter<ArrayData> forArray(ListVector listVector, ArrowFieldWriter<ArrayData> elementWriter) { | ||
| return new ArrayWriterForArray(listVector, elementWriter); | ||
| } | ||
|
|
||
| // ------------------------------------------------------------------------------------------ | ||
|
|
||
| private final ArrowFieldWriter<ArrayData> elementWriter; | ||
|
|
||
| private ArrayWriter(ListVector listVector, ArrowFieldWriter<ArrayData> elementWriter) { | ||
| super(listVector); | ||
| this.elementWriter = Objects.requireNonNull(elementWriter); | ||
| } | ||
|
|
||
| abstract boolean isNullAt(T in, int ordinal); | ||
|
|
||
| abstract ArrayData readArray(T in, int ordinal); | ||
|
|
||
| @Override | ||
| public void doWrite(T in, int ordinal) { | ||
| if (!isNullAt(in, ordinal)) { | ||
| ((ListVector) getValueVector()).startNewValue(getCount()); | ||
| ArrayData array = readArray(in, ordinal); | ||
| for (int i = 0; i < array.size(); i++) { | ||
| elementWriter.write(array, i); | ||
| } | ||
| ((ListVector) getValueVector()).endValue(getCount(), array.size()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void finish() { | ||
| super.finish(); | ||
| elementWriter.finish(); | ||
| } | ||
|
|
||
| @Override | ||
| public void reset() { | ||
| super.reset(); | ||
| elementWriter.reset(); | ||
| } | ||
|
|
||
| // ---- Inner classes for RowData and ArrayData ---- | ||
|
|
||
| public static final class ArrayWriterForRow extends ArrayWriter<RowData> { | ||
| private ArrayWriterForRow(ListVector listVector, ArrowFieldWriter<ArrayData> elementWriter) { | ||
| super(listVector, elementWriter); | ||
| } | ||
|
|
||
| @Override | ||
| boolean isNullAt(RowData in, int ordinal) { | ||
| return in.isNullAt(ordinal); | ||
| } | ||
|
|
||
| @Override | ||
| ArrayData readArray(RowData in, int ordinal) { | ||
| return in.getArray(ordinal); | ||
| } | ||
| } | ||
|
|
||
| public static final class ArrayWriterForArray extends ArrayWriter<ArrayData> { | ||
| private ArrayWriterForArray(ListVector listVector, ArrowFieldWriter<ArrayData> elementWriter) { | ||
| super(listVector, elementWriter); | ||
| } | ||
|
|
||
| @Override | ||
| boolean isNullAt(ArrayData in, int ordinal) { | ||
| return in.isNullAt(ordinal); | ||
| } | ||
|
|
||
| @Override | ||
| ArrayData readArray(ArrayData in, int ordinal) { | ||
| return in.getArray(ordinal); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,133 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * 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.auron.flink.arrow.writers; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.Objects; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.arrow.vector.complex.MapVector; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.arrow.vector.complex.StructVector; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.flink.table.data.ArrayData; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.flink.table.data.MapData; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.apache.flink.table.data.RowData; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * {@link ArrowFieldWriter} for maps ({@link MapVector}). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * <p>Arrow represents maps as {@code List<Struct{key, value}>}. This writer holds separate key and | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * value writers that operate on {@link ArrayData} (from {@link MapData#keyArray()} and {@link | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * MapData#valueArray()}). | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @param <T> the input data type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public abstract class MapWriter<T> extends ArrowFieldWriter<T> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Creates a MapWriter that reads from {@link RowData}. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static MapWriter<RowData> forRow( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| MapVector mapVector, ArrowFieldWriter<ArrayData> keyWriter, ArrowFieldWriter<ArrayData> valueWriter) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new MapWriterForRow(mapVector, keyWriter, valueWriter); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** Creates a MapWriter that reads from {@link ArrayData}. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static MapWriter<ArrayData> forArray( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| MapVector mapVector, ArrowFieldWriter<ArrayData> keyWriter, ArrowFieldWriter<ArrayData> valueWriter) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new MapWriterForArray(mapVector, keyWriter, valueWriter); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // ------------------------------------------------------------------------------------------ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final ArrowFieldWriter<ArrayData> keyWriter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final ArrowFieldWriter<ArrayData> valueWriter; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private MapWriter( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| MapVector mapVector, ArrowFieldWriter<ArrayData> keyWriter, ArrowFieldWriter<ArrayData> valueWriter) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| super(mapVector); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.keyWriter = Objects.requireNonNull(keyWriter); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.valueWriter = Objects.requireNonNull(valueWriter); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abstract boolean isNullAt(T in, int ordinal); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| abstract MapData readMap(T in, int ordinal); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void doWrite(T in, int ordinal) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!isNullAt(in, ordinal)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ((MapVector) getValueVector()).startNewValue(getCount()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| StructVector structVector = (StructVector) ((MapVector) getValueVector()).getDataVector(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| MapData map = readMap(in, ordinal); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ArrayData keys = map.keyArray(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ArrayData values = map.valueArray(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| for (int i = 0; i < map.size(); i++) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| structVector.setIndexDefined(keyWriter.getCount()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| keyWriter.write(keys, i); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| valueWriter.write(values, i); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ((MapVector) getValueVector()).endValue(getCount(), map.size()); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+67
to
+81
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!isNullAt(in, ordinal)) { | |
| ((MapVector) getValueVector()).startNewValue(getCount()); | |
| StructVector structVector = (StructVector) ((MapVector) getValueVector()).getDataVector(); | |
| MapData map = readMap(in, ordinal); | |
| ArrayData keys = map.keyArray(); | |
| ArrayData values = map.valueArray(); | |
| for (int i = 0; i < map.size(); i++) { | |
| structVector.setIndexDefined(keyWriter.getCount()); | |
| keyWriter.write(keys, i); | |
| valueWriter.write(values, i); | |
| } | |
| ((MapVector) getValueVector()).endValue(getCount(), map.size()); | |
| } | |
| MapVector mapVector = (MapVector) getValueVector(); | |
| // Explicitly mark null entries to avoid leaving stale validity/offset state. | |
| if (isNullAt(in, ordinal)) { | |
| mapVector.setNull(getCount()); | |
| return; | |
| } | |
| mapVector.startNewValue(getCount()); | |
| StructVector structVector = (StructVector) mapVector.getDataVector(); | |
| MapData map = readMap(in, ordinal); | |
| ArrayData keys = map.keyArray(); | |
| ArrayData values = map.valueArray(); | |
| for (int i = 0; i < map.size(); i++) { | |
| structVector.setIndexDefined(keyWriter.getCount()); | |
| keyWriter.write(keys, i); | |
| valueWriter.write(values, i); | |
| } | |
| mapVector.endValue(getCount(), map.size()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same rationale as the ArrayWriter comment above — this is intentional and consistent with the Flink official flink-python implementation, which also omits explicit setNull for null map entries.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ArrayWriter.doWrite() does nothing when the input value is null. For ListVector this is unsafe: you still need to explicitly mark the current index as null (and ensure offsets for index+1 are updated), otherwise old validity/offset buffer contents from a previous batch can leak after reset(), producing incorrect non-null reads or invalid offsets. Add an explicit null branch (e.g., listVector.setNull(getCount()) / setSafeNull-style handling) so every written position is defined.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is intentional and consistent with the Flink official implementation in
flink-python(ArrowArrayWriter.java). The upstreamArrayWriteralso omits explicitsetNullfor null entries.