emkornfield commented on code in PR #3390: URL: https://github.com/apache/parquet-java/pull/3390#discussion_r2719722810
########## parquet-column/src/main/java/org/apache/parquet/column/values/alp/AlpValuesReaderForDouble.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.parquet.column.values.alp; + +import static org.apache.parquet.column.values.alp.AlpConstants.*; + +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; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * ALP values reader for DOUBLE type. + * + * <p>Reads ALP-encoded double values from a page and decodes them back to double values. + * + * <p>Page Layout: + * <pre> + * ┌─────────┬────────────────┬────────────────┬─────────────┐ + * │ Header │ AlpInfo Array │ ForInfo Array │ Data Array │ + * │ 8 bytes │ 4B × N vectors │ 9B × N vectors │ Variable │ + * └─────────┴────────────────┴────────────────┴─────────────┘ + * </pre> + */ +public class AlpValuesReaderForDouble extends ValuesReader { + private static final Logger LOG = LoggerFactory.getLogger(AlpValuesReaderForDouble.class); + + // Decoded double values (eagerly decoded) + private double[] decodedValues; + private int currentIndex; + private int totalCount; + + public AlpValuesReaderForDouble() { + this.currentIndex = 0; + this.totalCount = 0; + } + + @Override + public void initFromPage(int valuesCount, ByteBufferInputStream stream) + throws ParquetDecodingException, IOException { + LOG.debug("init from page at offset {} for length {}", stream.position(), stream.available()); + + // Read and validate header + ByteBuffer headerBuf = stream.slice(ALP_HEADER_SIZE).order(ByteOrder.LITTLE_ENDIAN); + int version = headerBuf.get() & 0xFF; + int compressionMode = headerBuf.get() & 0xFF; + int integerEncoding = headerBuf.get() & 0xFF; + int logVectorSize = headerBuf.get() & 0xFF; + int numElements = headerBuf.getInt(); + + if (version != ALP_VERSION) { + throw new ParquetDecodingException("Unsupported ALP version: " + version + ", expected " + ALP_VERSION); + } + 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); + } + + int vectorSize = 1 << logVectorSize; + int numVectors = (numElements + vectorSize - 1) / vectorSize; + + this.totalCount = numElements; + this.decodedValues = new double[numElements]; + this.currentIndex = 0; + + // Read AlpInfo array + ByteBuffer alpInfoBuf = stream.slice(ALP_INFO_SIZE * numVectors).order(ByteOrder.LITTLE_ENDIAN); + int[] exponents = new int[numVectors]; + int[] factors = new int[numVectors]; + int[] numExceptions = new int[numVectors]; + + for (int v = 0; v < numVectors; v++) { + exponents[v] = alpInfoBuf.get() & 0xFF; + factors[v] = alpInfoBuf.get() & 0xFF; + numExceptions[v] = alpInfoBuf.getShort() & 0xFFFF; + } + + // Read ForInfo array + ByteBuffer forInfoBuf = stream.slice(DOUBLE_FOR_INFO_SIZE * numVectors).order(ByteOrder.LITTLE_ENDIAN); + long[] frameOfReference = new long[numVectors]; + int[] bitWidths = new int[numVectors]; + + for (int v = 0; v < numVectors; v++) { + frameOfReference[v] = forInfoBuf.getLong(); + bitWidths[v] = forInfoBuf.get() & 0xFF; + } + + // Decode each vector Review Comment: Do we really want do this all up-front? Don't we only want to decode one vector at a time? -- 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]
