rdblue commented on code in PR #3397: URL: https://github.com/apache/parquet-java/pull/3397#discussion_r4099751081
########## parquet-column/src/main/java/org/apache/parquet/column/values/alp/AlpValuesReaderForDouble.java: ########## @@ -0,0 +1,149 @@ +/* + * 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.DOUBLE_FOR_INFO_SIZE; +import static org.apache.parquet.column.values.alp.AlpConstants.DOUBLE_MAX_EXPONENT; +import static org.apache.parquet.column.values.alp.AlpConstants.PACK_GROUP_SIZE; + +import java.nio.ByteBuffer; +import java.util.Arrays; +import org.apache.parquet.bytes.BytesUtils; +import org.apache.parquet.column.values.bitpacking.BytePackerForLong; +import org.apache.parquet.column.values.bitpacking.Packer; +import org.apache.parquet.io.ParquetDecodingException; + +/** + * ALP values reader for DOUBLE type with lazy per-vector decoding. + * + * <p>Reads ALP-encoded double values from the interleaved page layout. + * Each vector is decoded on first access using BytePackerForLong-based unpacking. + */ +public class AlpValuesReaderForDouble extends AlpValuesReader { + + private double[] decodedValues; + private long[] deltasBuffer; + private final long[] unpackPadBuf = new long[PACK_GROUP_SIZE]; + private byte[] unpackByteBuf; + + public AlpValuesReaderForDouble() { + super(); + } + + @Override + protected void allocateDecodedBuffer(int capacity) { + this.decodedValues = new double[capacity]; + this.deltasBuffer = new long[capacity]; + this.unpackByteBuf = new byte[Long.SIZE]; // max bit width for long = 64 bytes + } + + @Override + protected int maxExponent() { + return DOUBLE_MAX_EXPONENT; + } + + @Override + protected String typeName() { + return "double"; + } + + @Override + protected int forInfoSize() { + return DOUBLE_FOR_INFO_SIZE; + } + + @Override + protected int exceptionValueSize() { + return Double.BYTES; + } + + @Override + public double readDouble() { + if (pageValueIndex >= totalCount) { + throw new ParquetDecodingException("ALP double data was already exhausted."); + } + ensureVectorDecoded(); + int vectorSlot = pageValueIndex % vectorSize; + pageValueIndex++; + return decodedValues[vectorSlot]; + } + + @Override + protected int decodeBody(int pos, int vectorNumber, int vectorLen, int exponent, int factor) { + long frameOfReference = vectorsData.getLong(pos); + int bitWidth = vectorsData.get(pos + Long.BYTES) & 0xFF; + if (bitWidth > Long.SIZE) { + throw new ParquetDecodingException( + "Invalid ALP double bitWidth " + bitWidth + " > 64 in vector " + vectorNumber); + } + pos += DOUBLE_FOR_INFO_SIZE; + + if (bitWidth > 0) { + pos = unpackLongsWithBytePacker(vectorsData, pos, deltasBuffer, vectorLen, bitWidth); + } else { + Arrays.fill(deltasBuffer, 0, vectorLen, 0L); + } + + for (int i = 0; i < vectorLen; i++) { + long encoded = deltasBuffer[i] + frameOfReference; + decodedValues[i] = AlpCodec.decodeDouble(encoded, exponent, factor); + } + return pos; + } + + @Override + protected void applyExceptionValues(int pos, int numExceptions) { Review Comment: This is another case where I think this would be better if it were static. It's strange to have the number of exceptions passed in as an argument, but the exception positions read directly from the parent's field. I think I would also move reading exception positions into this method (by calling another static helper) so that the dependency is clearer and the logic here is more self-contained. -- 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]
