rdblue commented on code in PR #3397: URL: https://github.com/apache/parquet-java/pull/3397#discussion_r4099313761
########## parquet-column/src/main/java/org/apache/parquet/column/values/alp/AlpValuesReader.java: ########## @@ -0,0 +1,306 @@ +/* + * 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.parquet.column.values.alp; + +import static org.apache.parquet.column.values.alp.AlpConstants.ALP_COMPRESSION_MODE; +import static org.apache.parquet.column.values.alp.AlpConstants.ALP_HEADER_SIZE; +import static org.apache.parquet.column.values.alp.AlpConstants.ALP_INFO_SIZE; +import static org.apache.parquet.column.values.alp.AlpConstants.ALP_INTEGER_ENCODING_FOR; +import static org.apache.parquet.column.values.alp.AlpConstants.MAX_LOG_VECTOR_SIZE; +import static org.apache.parquet.column.values.alp.AlpConstants.MIN_LOG_VECTOR_SIZE; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import org.apache.parquet.bytes.ByteBufferInputStream; +import org.apache.parquet.column.values.ValuesReader; +import org.apache.parquet.io.ParquetDecodingException; + +/** + * Abstract base class for ALP values readers with lazy per-vector decoding. + * + * <p>Reads ALP-encoded values from the interleaved page layout: + * <pre> + * ┌─────────┬──────────────────────┬──────────────┬──────────────┬─────┐ + * │ Header │ Offset Array │ Vector 0 │ Vector 1 │ ... │ + * │ 7 bytes │ 4B × numVectors │ (interleaved)│ (interleaved)│ │ + * └─────────┴──────────────────────┴──────────────┴──────────────┴─────┘ + * </pre> + * + * <p>Each vector is decoded lazily on first access. Skipping values does not + * trigger decoding of intermediate vectors. + */ +abstract class AlpValuesReader extends ValuesReader { + + protected int vectorSize; + protected int totalCount; + protected int numVectors; + protected int pageValueIndex; + protected int currentVectorNumber; + + protected int[] vectorOffsets; + protected ByteBuffer vectorsData; + protected int offsetArraySize; + + // Scratch buffer for exception positions within a vector; shared by both readers (int[] in each). + protected int[] excPositionsBuffer; + + AlpValuesReader() { + this.pageValueIndex = 0; + this.totalCount = 0; + this.currentVectorNumber = -1; + } + + @Override + public void initFromPage(int valuesCount, ByteBufferInputStream stream) + throws ParquetDecodingException, IOException { + ByteBuffer headerBuf = stream.slice(ALP_HEADER_SIZE).order(ByteOrder.LITTLE_ENDIAN); + int compressionMode = headerBuf.get() & 0xFF; + int integerEncoding = headerBuf.get() & 0xFF; + int logVectorSize = headerBuf.get() & 0xFF; + int numElements = headerBuf.getInt(); + + if (compressionMode != ALP_COMPRESSION_MODE) { + throw new ParquetDecodingException("Unsupported ALP compression mode: " + compressionMode); + } + if (integerEncoding != ALP_INTEGER_ENCODING_FOR) { + throw new ParquetDecodingException("Unsupported ALP integer encoding: " + integerEncoding); + } + if (logVectorSize < MIN_LOG_VECTOR_SIZE || logVectorSize > MAX_LOG_VECTOR_SIZE) { + throw new ParquetDecodingException("Invalid ALP log vector size: " + logVectorSize + ", must be between " + + MIN_LOG_VECTOR_SIZE + " and " + MAX_LOG_VECTOR_SIZE); + } + if (numElements < 0) { + throw new ParquetDecodingException("Invalid ALP element count: " + numElements); + } + // ALP's num_elements is the count of non-null values that went through encoding; + // valuesCount is the page row count, which is larger when the column has nulls. + // The two are equal only for required (non-null) columns. + if (numElements > valuesCount) { + throw new ParquetDecodingException( + "ALP header element count " + numElements + " exceeds page valuesCount " + valuesCount); + } + + this.vectorSize = 1 << logVectorSize; + this.totalCount = numElements; + + // Bound the vector count against the bytes actually present before allocating anything. + // numElements is only capped by valuesCount, which a forged page can set as high as + // Integer.MAX_VALUE, so computing this in int would overflow to a negative count, and even + // without overflow a large count would drive a huge int[] and a huge stream.slice allocation. + // Every vector costs a 4-byte offset entry plus at least its ALP and FOR headers, which gives + // a hard ceiling on how many vectors the remaining bytes can possibly describe. + int minVectorBytes = ALP_INFO_SIZE + forInfoSize(); + long bodyBytes = stream.available(); + long numVectorsLong = ((long) numElements + vectorSize - 1) / vectorSize; + long minBodyBytes = numVectorsLong * (Integer.BYTES + minVectorBytes); + if (minBodyBytes > bodyBytes) { + throw new ParquetDecodingException("ALP header element count " + numElements + " requires at least " + + minBodyBytes + " bytes but only " + bodyBytes + " remain in the page"); + } + this.numVectors = (int) numVectorsLong; + this.pageValueIndex = 0; + this.currentVectorNumber = -1; + + this.offsetArraySize = numVectors * Integer.BYTES; + ByteBuffer offsetBuf = stream.slice(offsetArraySize).order(ByteOrder.LITTLE_ENDIAN); + this.vectorOffsets = new int[numVectors]; + for (int v = 0; v < numVectors; v++) { + vectorOffsets[v] = offsetBuf.getInt(); + } + + // Slice remaining bytes into a 0-based view so decodeVector can use + // absolute get methods (vectorsData.get(pos)) directly. + int remainingBytes = (int) stream.available(); + ByteBuffer rawSlice = stream.slice(remainingBytes); + this.vectorsData = rawSlice.slice().order(ByteOrder.LITTLE_ENDIAN); + + validateVectorOffsets(minVectorBytes); + + allocateDecodedBuffer(vectorSize); + this.excPositionsBuffer = new int[vectorSize]; Review Comment: It would be nice to reuse these allocations since we don't expect `vectorSize` to change between pages in the same column. -- 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]
