vinooganesh commented on code in PR #3397:
URL: https://github.com/apache/parquet-java/pull/3397#discussion_r3567844556


##########
parquet-column/src/main/java/org/apache/parquet/column/values/alp/AlpValuesReaderForFloat.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.nio.ByteBuffer;
+import java.util.Arrays;
+import org.apache.parquet.column.values.bitpacking.BytePacker;
+import org.apache.parquet.column.values.bitpacking.Packer;
+import org.apache.parquet.io.ParquetDecodingException;
+
+/**
+ * ALP values reader for FLOAT type with lazy per-vector decoding.
+ *
+ * <p>Reads ALP-encoded float values from the interleaved page layout.
+ * Each vector is decoded on first access using BytePacker-based unpacking.
+ */
+public class AlpValuesReaderForFloat extends AlpValuesReader {
+
+  private float[] decodedValues;
+  private int[] deltasBuffer;
+  private int[] excPositionsBuffer;
+  private final int[] unpackPadBuf = new int[8];
+  private byte[] unpackByteBuf;
+
+  public AlpValuesReaderForFloat() {
+    super();
+  }
+
+  @Override
+  protected void allocateDecodedBuffer(int capacity) {
+    this.decodedValues = new float[capacity];
+    this.deltasBuffer = new int[capacity];
+    this.excPositionsBuffer = new int[capacity];
+    this.unpackByteBuf = new byte[Integer.SIZE]; // max bit width for int = 32 
bytes
+  }
+
+  @Override
+  public float readFloat() {
+    if (currentIndex >= totalCount) {
+      throw new ParquetDecodingException("ALP float data was already 
exhausted.");
+    }
+    ensureVectorDecoded();
+    int indexInVector = currentIndex % vectorSize;
+    currentIndex++;
+    return decodedValues[indexInVector];
+  }
+
+  @Override
+  protected void decodeVector(int vectorIdx) {
+    int vectorLen = getVectorLength(vectorIdx);
+    int pos = getVectorDataPosition(vectorIdx);
+
+    int exponent = vectorsData.get(pos) & 0xFF;
+    int factor = vectorsData.get(pos + 1) & 0xFF;
+    int numExceptions = getShortLE(vectorsData, pos + 2) & 0xFFFF;
+    pos += ALP_INFO_SIZE;
+
+    if (exponent > FLOAT_MAX_EXPONENT) {
+      throw new ParquetDecodingException(
+          "Invalid ALP float exponent " + exponent + " in vector " + vectorIdx 
+ ", max is " + FLOAT_MAX_EXPONENT);
+    }
+    if (factor > exponent) {
+      throw new ParquetDecodingException(
+          "Invalid ALP float factor " + factor + " > exponent " + exponent + " 
in vector " + vectorIdx);
+    }
+    if (numExceptions > vectorLen) {
+      throw new ParquetDecodingException(
+          "Invalid ALP numExceptions " + numExceptions + " > vectorLen " + 
vectorLen + " in vector " + vectorIdx);
+    }
+
+    int frameOfReference = getIntLE(vectorsData, pos);
+    int bitWidth = vectorsData.get(pos + 4) & 0xFF;
+    pos += FLOAT_FOR_INFO_SIZE;
+
+    if (bitWidth > 0) {

Review Comment:
   Added a bounds check. decodeBody now throws if bitWidth is greater than 32 
for the float reader.



##########
parquet-column/src/main/java/org/apache/parquet/column/values/alp/AlpValuesReader.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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;
+
+/**
+ * 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 &times; 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 currentIndex;
+  protected int currentVectorIndex;
+
+  protected int[] vectorOffsets;
+  protected ByteBuffer vectorsData;
+  protected int offsetArraySize;
+
+  AlpValuesReader() {
+    this.currentIndex = 0;
+    this.totalCount = 0;
+    this.currentVectorIndex = -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;
+    this.numVectors = (numElements + vectorSize - 1) / vectorSize;
+    this.currentIndex = 0;
+    this.currentVectorIndex = -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);
+
+    allocateDecodedBuffer(vectorSize);
+  }
+
+  protected int getVectorLength(int vectorIdx) {
+    if (vectorIdx < numVectors - 1) {
+      return vectorSize;
+    }
+    // Last vector may be partial
+    int lastVectorLen = totalCount % vectorSize;
+    return lastVectorLen == 0 ? vectorSize : lastVectorLen;
+  }
+
+  // Offsets in the page are relative to the compression body (after header),
+  // but vectorsData starts after the offset array, so adjust.
+  protected int getVectorDataPosition(int vectorIdx) {
+    return vectorOffsets[vectorIdx] - offsetArraySize;
+  }
+
+  @Override
+  public void skip() {
+    skip(1);
+  }
+
+  @Override
+  public void skip(int n) {
+    if (n < 0 || currentIndex + n > totalCount) {
+      throw new ParquetDecodingException(String.format(
+          "Cannot skip this many elements. Current index: %d. Skip %d. Total 
count: %d",
+          currentIndex, n, totalCount));
+    }
+    currentIndex += n;
+  }
+
+  protected void ensureVectorDecoded() {
+    int vectorIdx = currentIndex / vectorSize;
+    if (vectorIdx != currentVectorIndex) {
+      decodeVector(vectorIdx);
+      currentVectorIndex = vectorIdx;
+    }
+  }
+
+  protected abstract void allocateDecodedBuffer(int capacity);
+
+  protected abstract void decodeVector(int vectorIdx);
+
+  // Explicit little-endian reads using absolute get(), since absolute get() 
ignores ByteBuffer order.

Review Comment:
   Switched to the little-endian typed getters (getInt/getLong on a 
LITTLE_ENDIAN-ordered buffer) instead of the manual byte assembly, so the local 
LE helpers are gone.



##########
parquet-column/src/main/java/org/apache/parquet/column/values/alp/AlpValuesReader.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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;
+
+/**
+ * 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 &times; 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 currentIndex;
+  protected int currentVectorIndex;
+
+  protected int[] vectorOffsets;
+  protected ByteBuffer vectorsData;
+  protected int offsetArraySize;
+
+  AlpValuesReader() {
+    this.currentIndex = 0;

Review Comment:
   Renamed these to make the coordinate systems clearer: pageValueIndex 
(position in the page), vectorNumber (which vector), and vectorSlot (position 
within the vector).



##########
parquet-column/src/main/java/org/apache/parquet/column/values/alp/AlpValuesReaderForDouble.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.nio.ByteBuffer;
+import java.util.Arrays;
+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 int[] excPositionsBuffer;
+  private final long[] unpackPadBuf = new long[8];
+  private byte[] unpackByteBuf;
+
+  public AlpValuesReaderForDouble() {
+    super();
+  }
+
+  @Override
+  protected void allocateDecodedBuffer(int capacity) {
+    this.decodedValues = new double[capacity];
+    this.deltasBuffer = new long[capacity];
+    this.excPositionsBuffer = new int[capacity];
+    this.unpackByteBuf = new byte[Long.SIZE]; // max bit width for long = 64 
bytes
+  }
+
+  @Override
+  public double readDouble() {
+    if (currentIndex >= totalCount) {
+      throw new ParquetDecodingException("ALP double data was already 
exhausted.");
+    }
+    ensureVectorDecoded();
+    int indexInVector = currentIndex % vectorSize;
+    currentIndex++;
+    return decodedValues[indexInVector];
+  }
+
+  @Override
+  protected void decodeVector(int vectorIdx) {

Review Comment:
   Renamed to vectorNumber here too, matching the base reader.



##########
parquet-column/src/main/java/org/apache/parquet/column/values/alp/AlpValuesReaderForDouble.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.nio.ByteBuffer;
+import java.util.Arrays;
+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 int[] excPositionsBuffer;
+  private final long[] unpackPadBuf = new long[8];
+  private byte[] unpackByteBuf;
+
+  public AlpValuesReaderForDouble() {
+    super();
+  }
+
+  @Override
+  protected void allocateDecodedBuffer(int capacity) {
+    this.decodedValues = new double[capacity];
+    this.deltasBuffer = new long[capacity];
+    this.excPositionsBuffer = new int[capacity];
+    this.unpackByteBuf = new byte[Long.SIZE]; // max bit width for long = 64 
bytes
+  }
+
+  @Override
+  public double readDouble() {
+    if (currentIndex >= totalCount) {
+      throw new ParquetDecodingException("ALP double data was already 
exhausted.");
+    }
+    ensureVectorDecoded();
+    int indexInVector = currentIndex % vectorSize;
+    currentIndex++;
+    return decodedValues[indexInVector];
+  }
+
+  @Override
+  protected void decodeVector(int vectorIdx) {
+    int vectorLen = getVectorLength(vectorIdx);
+    int pos = getVectorDataPosition(vectorIdx);
+
+    int exponent = vectorsData.get(pos) & 0xFF;
+    int factor = vectorsData.get(pos + 1) & 0xFF;
+    int numExceptions = getShortLE(vectorsData, pos + 2) & 0xFFFF;

Review Comment:
   Right, it's moot now that we read through the typed getters, so the 0xFFFF 
masking is gone.



##########
parquet-column/src/main/java/org/apache/parquet/column/values/alp/AlpValuesReaderForDouble.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.nio.ByteBuffer;
+import java.util.Arrays;
+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 int[] excPositionsBuffer;
+  private final long[] unpackPadBuf = new long[8];
+  private byte[] unpackByteBuf;
+
+  public AlpValuesReaderForDouble() {
+    super();
+  }
+
+  @Override
+  protected void allocateDecodedBuffer(int capacity) {
+    this.decodedValues = new double[capacity];
+    this.deltasBuffer = new long[capacity];
+    this.excPositionsBuffer = new int[capacity];
+    this.unpackByteBuf = new byte[Long.SIZE]; // max bit width for long = 64 
bytes
+  }
+
+  @Override
+  public double readDouble() {
+    if (currentIndex >= totalCount) {
+      throw new ParquetDecodingException("ALP double data was already 
exhausted.");
+    }
+    ensureVectorDecoded();
+    int indexInVector = currentIndex % vectorSize;
+    currentIndex++;
+    return decodedValues[indexInVector];
+  }
+
+  @Override
+  protected void decodeVector(int vectorIdx) {
+    int vectorLen = getVectorLength(vectorIdx);
+    int pos = getVectorDataPosition(vectorIdx);
+
+    int exponent = vectorsData.get(pos) & 0xFF;
+    int factor = vectorsData.get(pos + 1) & 0xFF;
+    int numExceptions = getShortLE(vectorsData, pos + 2) & 0xFFFF;
+    pos += ALP_INFO_SIZE;
+
+    if (exponent > DOUBLE_MAX_EXPONENT) {
+      throw new ParquetDecodingException(
+          "Invalid ALP double exponent " + exponent + " in vector " + 
vectorIdx + ", max is " + DOUBLE_MAX_EXPONENT);
+    }
+    if (factor > exponent) {
+      throw new ParquetDecodingException(
+          "Invalid ALP double factor " + factor + " > exponent " + exponent + 
" in vector " + vectorIdx);
+    }
+    if (numExceptions > vectorLen) {
+      throw new ParquetDecodingException(
+          "Invalid ALP numExceptions " + numExceptions + " > vectorLen " + 
vectorLen + " in vector " + vectorIdx);
+    }
+
+    long frameOfReference = getLongLE(vectorsData, pos);
+    int bitWidth = vectorsData.get(pos + 8) & 0xFF;

Review Comment:
   Using Long.BYTES now.



##########
parquet-column/src/main/java/org/apache/parquet/column/values/alp/AlpValuesReaderForDouble.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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.nio.ByteBuffer;
+import java.util.Arrays;
+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 int[] excPositionsBuffer;
+  private final long[] unpackPadBuf = new long[8];
+  private byte[] unpackByteBuf;
+
+  public AlpValuesReaderForDouble() {
+    super();
+  }
+
+  @Override
+  protected void allocateDecodedBuffer(int capacity) {
+    this.decodedValues = new double[capacity];
+    this.deltasBuffer = new long[capacity];
+    this.excPositionsBuffer = new int[capacity];
+    this.unpackByteBuf = new byte[Long.SIZE]; // max bit width for long = 64 
bytes
+  }
+
+  @Override
+  public double readDouble() {
+    if (currentIndex >= totalCount) {
+      throw new ParquetDecodingException("ALP double data was already 
exhausted.");
+    }
+    ensureVectorDecoded();
+    int indexInVector = currentIndex % vectorSize;
+    currentIndex++;
+    return decodedValues[indexInVector];
+  }
+
+  @Override
+  protected void decodeVector(int vectorIdx) {
+    int vectorLen = getVectorLength(vectorIdx);
+    int pos = getVectorDataPosition(vectorIdx);
+
+    int exponent = vectorsData.get(pos) & 0xFF;
+    int factor = vectorsData.get(pos + 1) & 0xFF;
+    int numExceptions = getShortLE(vectorsData, pos + 2) & 0xFFFF;
+    pos += ALP_INFO_SIZE;
+
+    if (exponent > DOUBLE_MAX_EXPONENT) {
+      throw new ParquetDecodingException(
+          "Invalid ALP double exponent " + exponent + " in vector " + 
vectorIdx + ", max is " + DOUBLE_MAX_EXPONENT);
+    }
+    if (factor > exponent) {
+      throw new ParquetDecodingException(
+          "Invalid ALP double factor " + factor + " > exponent " + exponent + 
" in vector " + vectorIdx);
+    }
+    if (numExceptions > vectorLen) {
+      throw new ParquetDecodingException(
+          "Invalid ALP numExceptions " + numExceptions + " > vectorLen " + 
vectorLen + " in vector " + vectorIdx);
+    }
+
+    long frameOfReference = getLongLE(vectorsData, pos);
+    int bitWidth = vectorsData.get(pos + 8) & 0xFF;
+    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] = AlpEncoderDecoder.decodeDouble(encoded, exponent, 
factor);
+    }
+
+    if (numExceptions > 0) {
+      for (int e = 0; e < numExceptions; e++) {
+        excPositionsBuffer[e] = getShortLE(vectorsData, pos) & 0xFFFF;
+        if (excPositionsBuffer[e] >= vectorLen) {
+          throw new ParquetDecodingException(
+              "ALP exception position " + excPositionsBuffer[e] + " out of 
bounds for vectorLen " + vectorLen);
+        }
+        pos += Short.BYTES;
+      }
+      for (int e = 0; e < numExceptions; e++) {
+        decodedValues[excPositionsBuffer[e]] = getDoubleLE(vectorsData, pos);
+        pos += Double.BYTES;
+      }
+    }
+  }
+
+  private int unpackLongsWithBytePacker(ByteBuffer buf, int pos, long[] 
output, int count, int bitWidth) {
+    BytePackerForLong packer = 
Packer.LITTLE_ENDIAN.newBytePackerForLong(bitWidth);
+    int numFullGroups = count / 8;
+    int remaining = count % 8;
+
+    for (int g = 0; g < numFullGroups; g++) {
+      packer.unpack8Values(buf, pos, output, g * 8);
+      pos += bitWidth;
+    }
+
+    // Last group might have fewer than 8 values; zero-pad and unpack,
+    // but only advance pos by the actual bytes in the page.
+    if (remaining > 0) {
+      int totalPackedBytes = (count * bitWidth + 7) / 8;

Review Comment:
   Switched to BytesUtils.paddedByteCountFromBits.



-- 
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]

Reply via email to