nastra commented on code in PR #14800: URL: https://github.com/apache/iceberg/pull/14800#discussion_r2792855795
########## arrow/src/main/java/org/apache/iceberg/arrow/vectorized/parquet/VectorizedDeltaLengthByteArrayValuesReader.java: ########## @@ -0,0 +1,202 @@ +/* + * 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.iceberg.arrow.vectorized.parquet; + +import java.io.EOFException; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.function.IntUnaryOperator; +import org.apache.arrow.vector.BaseVariableWidthVector; +import org.apache.arrow.vector.FieldVector; +import org.apache.parquet.bytes.ByteBufferInputStream; +import org.apache.parquet.column.values.ValuesReader; +import org.apache.parquet.io.ParquetDecodingException; +import org.apache.parquet.io.api.Binary; + +/** + * A {@link VectorizedValuesReader} implementation for the encoding type DELTA_LENGTH_BYTE_ARRAY. + * This is adapted from Spark's VectorizedDeltaLengthByteArrayReader. + * + * @see <a + * href="https://github.com/apache/parquet-format/blob/master/Encodings.md#delta-length-byte-array-delta_length_byte_array--6"> + * Parquet format encodings: DELTA_LENGTH_BYTE_ARRAY</a> + */ +public class VectorizedDeltaLengthByteArrayValuesReader extends ValuesReader + implements VectorizedValuesReader { + + private final VectorizedDeltaEncodedValuesReader lengthReader; + + private ByteBufferInputStream in; + private int[] lengths; + private ByteBuffer byteBuffer; + private int currentIndex; + + VectorizedDeltaLengthByteArrayValuesReader() { + lengthReader = new VectorizedDeltaEncodedValuesReader(); + } + + @Override + public void initFromPage(int valueCount, ByteBufferInputStream inputStream) throws IOException { + lengthReader.initFromPage(valueCount, inputStream); + // actual number of elements in the page may be less than the passed valueCount here due to + // nulls + lengths = lengthReader.readIntegers(lengthReader.getTotalValueCount(), 0); + + in = inputStream.remainingStream(); + currentIndex = 0; + } + + @Override + public Binary readBinary(int len) { + readValues(1, null, 0, x -> len, (f, i, v) -> byteBuffer = v); + return Binary.fromReusedByteBuffer(byteBuffer); + } + + Binary readBinaryForRow(int rowId) { + if (lengths[currentIndex] == 0) { + currentIndex++; + return Binary.EMPTY; + } + readValues(1, null, rowId, ignored -> lengths[currentIndex], (f, i, v) -> byteBuffer = v); + return Binary.fromReusedByteBuffer(byteBuffer); + } + + @Override + public void readBinary(int total, FieldVector vec, int rowId, boolean setArrowValidityVector) { + readValues( + total, + vec, + rowId, + x -> lengths[currentIndex], + (f, i, v) -> + ((BaseVariableWidthVector) vec) + .setSafe( + (int) i, v.array(), v.position() + v.arrayOffset(), v.limit() - v.position())); + } + + @SuppressWarnings("UnusedVariable") + private void readValues( + int total, + FieldVector vec, + int rowId, + IntUnaryOperator getLength, + BinaryOutputWriter outputWriter) { + ByteBuffer buffer; + for (int i = 0; i < total; i++) { + int length = getLength.applyAsInt(rowId + i); + try { + if (length < 0) { + throw new IllegalStateException("Invalid length: " + length); + } + buffer = in.slice(length); + } catch (EOFException e) { + throw new ParquetDecodingException("Failed to read " + length + " bytes"); + } + outputWriter.write(vec, rowId + i, buffer); + currentIndex++; + } + } + + /** DELTA_LENGTH_BYTE_ARRAY only supports BINARY */ + @Override + public boolean readBoolean() { + throw new UnsupportedOperationException("readBoolean is not supported"); + } + + /** DELTA_LENGTH_BYTE_ARRAY only supports BINARY */ + @Override + public byte readByte() { + throw new UnsupportedOperationException("readByte is not supported"); + } + + /** DELTA_LENGTH_BYTE_ARRAY only supports BINARY */ + @Override + public short readShort() { + throw new UnsupportedOperationException("readShort is not supported"); + } + + /** DELTA_LENGTH_BYTE_ARRAY only supports BINARY */ + @Override + public int readInteger() { + throw new UnsupportedOperationException("readInteger is not supported"); + } + + /** DELTA_LENGTH_BYTE_ARRAY only supports BINARY */ + @Override + public long readLong() { + throw new UnsupportedOperationException("readLong is not supported"); + } + + /** DELTA_LENGTH_BYTE_ARRAY only supports BINARY */ + @Override + public float readFloat() { + throw new UnsupportedOperationException("readFloat is not supported"); + } + + /** DELTA_LENGTH_BYTE_ARRAY only supports BINARY */ + @Override + public double readDouble() { + throw new UnsupportedOperationException("readDouble is not supported"); + } + + /** DELTA_LENGTH_BYTE_ARRAY only supports BINARY */ + @Override + public void readIntegers(int total, FieldVector vec, int rowId) { + throw new UnsupportedOperationException("readIntegers is not supported"); + } + + /** DELTA_LENGTH_BYTE_ARRAY only supports BINARY */ + @Override + public void readLongs(int total, FieldVector vec, int rowId) { + throw new UnsupportedOperationException("readLongs is not supported"); Review Comment: rather than having to throw UOE in each subclass we should probably instead do this in one single place -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
