jbewing commented on code in PR #15410: URL: https://github.com/apache/iceberg/pull/15410#discussion_r2978720972
########## arrow/src/main/java/org/apache/iceberg/arrow/vectorized/parquet/VectorizedRleBooleanValuesReader.java: ########## @@ -0,0 +1,139 @@ +/* + * 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.IOException; +import java.nio.ByteBuffer; +import org.apache.arrow.vector.BitVector; +import org.apache.arrow.vector.FieldVector; +import org.apache.parquet.bytes.ByteBufferInputStream; +import org.apache.parquet.bytes.BytesUtils; +import org.apache.parquet.column.values.ValuesReader; +import org.apache.parquet.column.values.bitpacking.BytePacker; +import org.apache.parquet.column.values.bitpacking.Packer; +import org.apache.parquet.io.ParquetDecodingException; + +/** + * A {@link VectorizedValuesReader} for RLE-encoded boolean data pages. + * + * @see <a + * href="https://parquet.apache.org/docs/file-format/data-pages/encodings/#run-length-encoding--bit-packing-hybrid-rle--3"> + * Parquet format encodings: RLE</a> + */ +public class VectorizedRleBooleanValuesReader extends ValuesReader + implements VectorizedValuesReader { + + private enum Mode { + RLE, + PACKED + } + + private static final int BIT_WIDTH = 1; + private static final BytePacker PACKER = Packer.LITTLE_ENDIAN.newBytePacker(BIT_WIDTH); + + private ByteBufferInputStream inputStream; + private Mode mode; + private int currentCount; + private int currentValue; + private int[] packedValuesBuffer = new int[16]; + private int packedValuesBufferIdx = 0; + + @Override + public void initFromPage(int valueCount, ByteBufferInputStream in) throws IOException { + int length = BytesUtils.readIntLittleEndian(in); + this.inputStream = in.sliceStream(length); + this.currentCount = 0; + } + + @Override + public boolean readBoolean() { + return nextValue() != 0; + } + + @Override + public void readBooleans(int total, FieldVector vec, int rowId) { + BitVector bitVector = (BitVector) vec; + for (int i = 0; i < total; i++) { + bitVector.setSafe(rowId + i, readBoolean() ? 1 : 0); + } + } + + private int nextValue() { + if (currentCount == 0) { + readNextGroup(); + } + + currentCount--; + switch (mode) { + case RLE: + return currentValue; + case PACKED: + return packedValuesBuffer[packedValuesBufferIdx++]; Review Comment: > 1: How common is it for RLE and PACKED modes to be mixed? Very common with real-world data. It's mostly a function of your data distribution though. To illustrate this, I wrote quick a Python script to analyze our golden test files and synthetic boolean data. The golden files (random booleans) happen to be 100% PACKED since random data has no runs. But for realistic patterns like a "95% true" column (think is_active, is_enabled flags), the encoder produces a heavily interleaved mix — 24 RLE groups and 23 PACKED groups in 1000 values, alternating between runs of true and packed regions around sparse false values. Here's what my script ultimately output (and happy to share my setup w/ you if you'd like to replicate things) | Pattern | RLE Groups | Packed Groups | | - | - | - | | All true (1000) | 1 (1000 vals) | 0 | | All false (1000) | 1 (1000 vals) | 0 | | Random 50/50 | 1 (8 vals) | 3 (992 vals) | | Mixed: runs & random | 4 (800 vals) | 1 (200 vals) | | 95% true (most real-world case imo) | 24 (712 vals) | 23 (288 vals) | > 2. Are there cases where only RLE or only PACKED is defined? No. The [RLE/bit-packing hybrid spec always permits both modes](https://parquet.apache.org/docs/file-format/data-pages/encodings/#RLE). There is no variant that restricts to one mode. A decoder must always handle both since encoders freely choose per-group based on the data. Constant data (all true/all false) produces pure RLE; fully random data produces mostly PACKED; and real-world data falls anywhere on the spectrum. > 3. Is there any measurable performance benefit from skipping unused code paths in the boolean reader? I haven't done any benchmarking myself, but given that really only relevant code runs on `readBoolean` & such and more of the "expensive" looking code lives at row group boundaries, I can't imagine its a massive boundary. In fact, I think my expanding the inheritance hierarchy further I'd imagine we probably make things worse vtable wise. We're _fine_ on our immediate `VectorizedRLEAndBitPackingParquetValuesReader` hierarchy since it's still biomorphic If performance is your primary concern, given that this is the only path left over for non-nested parquet primitives that can't be used with a vectorized reader (thus preventing vectorization on flat, parquet v2 written data), I've anecdotally found that the vectorized read path is ~40-70% faster than the non-vectorized read path (depends on what you're reading for the most part). -- 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]
