emkornfield commented on code in PR #3390:
URL: https://github.com/apache/parquet-java/pull/3390#discussion_r2719799163


##########
parquet-column/src/main/java/org/apache/parquet/column/values/alp/AlpValuesWriter.java:
##########
@@ -0,0 +1,525 @@
+/*
+ * 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.nio.ByteOrder;
+import org.apache.parquet.bytes.ByteBufferAllocator;
+import org.apache.parquet.bytes.BytesInput;
+import org.apache.parquet.bytes.CapacityByteArrayOutputStream;
+import org.apache.parquet.column.Encoding;
+import org.apache.parquet.column.values.ValuesWriter;
+
+/**
+ * ALP (Adaptive Lossless floating-Point) values writer.
+ *
+ * <p>ALP encoding converts floating-point values to integers using decimal 
scaling,
+ * then applies Frame of Reference (FOR) encoding and bit-packing.
+ * Values that cannot be losslessly converted are stored as exceptions.
+ *
+ * <p>Page Layout:
+ * <pre>
+ * ┌─────────┬────────────────┬────────────────┬─────────────┐
+ * │ Header  │ AlpInfo Array  │ ForInfo Array  │ Data Array  │
+ * │ 8 bytes │ 4B × N vectors │ 5B/9B × N      │ Variable    │
+ * └─────────┴────────────────┴────────────────┴─────────────┘
+ * </pre>
+ */
+public abstract class AlpValuesWriter extends ValuesWriter {
+
+  protected final int initialCapacity;
+  protected final int pageSize;
+  protected final ByteBufferAllocator allocator;
+  protected CapacityByteArrayOutputStream outputStream;
+
+  public AlpValuesWriter(int initialCapacity, int pageSize, 
ByteBufferAllocator allocator) {
+    this.initialCapacity = initialCapacity;
+    this.pageSize = pageSize;
+    this.allocator = allocator;
+    this.outputStream = new CapacityByteArrayOutputStream(initialCapacity, 
pageSize, allocator);
+  }
+
+  @Override
+  public Encoding getEncoding() {
+    return Encoding.ALP;
+  }
+
+  @Override
+  public void close() {
+    outputStream.close();
+  }
+
+  /**
+   * Float-specific ALP values writer.
+   */
+  public static class FloatAlpValuesWriter extends AlpValuesWriter {
+    private float[] buffer;
+    private int count;
+
+    public FloatAlpValuesWriter(int initialCapacity, int pageSize, 
ByteBufferAllocator allocator) {
+      super(initialCapacity, pageSize, allocator);
+      // Initial buffer size - will grow as needed
+      this.buffer = new float[Math.max(ALP_VECTOR_SIZE, initialCapacity / 
Float.BYTES)];
+      this.count = 0;
+    }
+
+    @Override
+    public void writeFloat(float v) {
+      if (count >= buffer.length) {

Review Comment:
   I don't think we should be growing here to all floats.  Once we reach 
vector-size we should be encoding the page.



##########
parquet-column/src/main/java/org/apache/parquet/column/values/alp/AlpValuesWriter.java:
##########
@@ -0,0 +1,525 @@
+/*
+ * 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.nio.ByteOrder;
+import org.apache.parquet.bytes.ByteBufferAllocator;
+import org.apache.parquet.bytes.BytesInput;
+import org.apache.parquet.bytes.CapacityByteArrayOutputStream;
+import org.apache.parquet.column.Encoding;
+import org.apache.parquet.column.values.ValuesWriter;
+
+/**
+ * ALP (Adaptive Lossless floating-Point) values writer.
+ *
+ * <p>ALP encoding converts floating-point values to integers using decimal 
scaling,
+ * then applies Frame of Reference (FOR) encoding and bit-packing.
+ * Values that cannot be losslessly converted are stored as exceptions.
+ *
+ * <p>Page Layout:
+ * <pre>
+ * ┌─────────┬────────────────┬────────────────┬─────────────┐
+ * │ Header  │ AlpInfo Array  │ ForInfo Array  │ Data Array  │
+ * │ 8 bytes │ 4B × N vectors │ 5B/9B × N      │ Variable    │
+ * └─────────┴────────────────┴────────────────┴─────────────┘
+ * </pre>
+ */
+public abstract class AlpValuesWriter extends ValuesWriter {
+
+  protected final int initialCapacity;
+  protected final int pageSize;
+  protected final ByteBufferAllocator allocator;
+  protected CapacityByteArrayOutputStream outputStream;
+
+  public AlpValuesWriter(int initialCapacity, int pageSize, 
ByteBufferAllocator allocator) {
+    this.initialCapacity = initialCapacity;
+    this.pageSize = pageSize;
+    this.allocator = allocator;
+    this.outputStream = new CapacityByteArrayOutputStream(initialCapacity, 
pageSize, allocator);
+  }
+
+  @Override
+  public Encoding getEncoding() {
+    return Encoding.ALP;
+  }
+
+  @Override
+  public void close() {
+    outputStream.close();
+  }
+
+  /**
+   * Float-specific ALP values writer.
+   */
+  public static class FloatAlpValuesWriter extends AlpValuesWriter {
+    private float[] buffer;
+    private int count;
+
+    public FloatAlpValuesWriter(int initialCapacity, int pageSize, 
ByteBufferAllocator allocator) {
+      super(initialCapacity, pageSize, allocator);
+      // Initial buffer size - will grow as needed
+      this.buffer = new float[Math.max(ALP_VECTOR_SIZE, initialCapacity / 
Float.BYTES)];
+      this.count = 0;
+    }
+
+    @Override
+    public void writeFloat(float v) {
+      if (count >= buffer.length) {
+        // Grow buffer
+        float[] newBuffer = new float[buffer.length * 2];
+        System.arraycopy(buffer, 0, newBuffer, 0, count);
+        buffer = newBuffer;
+      }
+      buffer[count++] = v;
+    }
+
+    @Override
+    public long getBufferedSize() {
+      // Estimate: each float value contributes roughly 2-4 bytes after 
compression
+      // (actual size depends on data characteristics)
+      return count * 3L; // Conservative estimate
+    }
+
+    @Override
+    public BytesInput getBytes() {
+      if (count == 0) {
+        return BytesInput.empty();
+      }
+
+      outputStream.reset();
+
+      // Calculate number of vectors
+      int numVectors = (count + ALP_VECTOR_SIZE - 1) / ALP_VECTOR_SIZE;
+
+      // Prepare metadata arrays
+      int[] exponents = new int[numVectors];
+      int[] factors = new int[numVectors];
+      int[] numExceptions = new int[numVectors];
+      int[] frameOfReference = new int[numVectors];
+      int[] bitWidths = new int[numVectors];
+
+      // Prepare encoded data arrays
+      int[][] encodedValues = new int[numVectors][];
+      short[][] exceptionPositions = new short[numVectors][];
+      float[][] exceptionValues = new float[numVectors][];
+
+      // Process each vector
+      for (int v = 0; v < numVectors; v++) {
+        int vectorStart = v * ALP_VECTOR_SIZE;
+        int vectorEnd = Math.min(vectorStart + ALP_VECTOR_SIZE, count);
+        int vectorLen = vectorEnd - vectorStart;
+
+        // Find best encoding parameters
+        AlpEncoderDecoder.EncodingParams params =

Review Comment:
   Don't we want to cache encodings and use those first (and stop searching 
after a while)?



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