emkornfield commented on code in PR #48345:
URL: https://github.com/apache/arrow/pull/48345#discussion_r3353562396


##########
cpp/src/arrow/util/alp/alp.cc:
##########
@@ -0,0 +1,1031 @@
+// 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.
+
+#include "arrow/util/alp/alp.h"
+
+#include <cmath>
+#include <cstring>
+#include <functional>
+#include <iostream>
+#include <map>
+
+#include "arrow/util/alp/alp_constants.h"
+#include "arrow/util/bit_stream_utils_internal.h"
+#include "arrow/util/bit_util.h"
+#include "arrow/util/endian.h"
+#include "arrow/util/bpacking_internal.h"
+#include "arrow/util/logging.h"
+#include "arrow/util/span.h"
+#include "arrow/util/ubsan.h"
+
+namespace arrow {
+namespace util {
+namespace alp {
+
+// ALP serialization uses memcpy for multi-byte integers (frame_of_reference,
+// num_exceptions, offsets) and assumes little-endian byte order on disk.
+static_assert(ARROW_LITTLE_ENDIAN,
+              "ALP serialization assumes little-endian byte order");
+
+// ----------------------------------------------------------------------
+// AlpEncodedVectorInfo implementation (non-templated, 4 bytes)
+
+void AlpEncodedVectorInfo::Store(arrow::util::span<uint8_t> output_buffer) 
const {
+  ARROW_CHECK(output_buffer.size() >= static_cast<size_t>(GetStoredSize()))
+      << "alp_vector_info_output_too_small: " << output_buffer.size() << " vs "
+      << GetStoredSize();
+
+  uint8_t* ptr = output_buffer.data();
+
+  // exponent, factor: 1 byte each
+  *ptr++ = exponent;
+  *ptr++ = factor;
+
+  // num_exceptions: 2 bytes
+  std::memcpy(ptr, &num_exceptions, sizeof(num_exceptions));
+}
+
+Result<AlpEncodedVectorInfo> AlpEncodedVectorInfo::Load(
+    arrow::util::span<const uint8_t> input_buffer) {
+  if (input_buffer.size() < static_cast<size_t>(GetStoredSize())) {
+    return Status::Invalid("ALP vector info buffer too small: ", 
input_buffer.size(),
+                           " < ", GetStoredSize());
+  }
+
+  AlpEncodedVectorInfo result{};
+  const uint8_t* ptr = input_buffer.data();
+
+  // exponent, factor: 1 byte each
+  result.exponent = *ptr++;
+  result.factor = *ptr++;
+
+  // num_exceptions: 2 bytes
+  std::memcpy(&result.num_exceptions, ptr, sizeof(result.num_exceptions));
+
+  return result;
+}
+
+// ----------------------------------------------------------------------
+// AlpEncodedForVectorInfo implementation (templated, 5/9 bytes)
+
+template <typename T>
+void AlpEncodedForVectorInfo<T>::Store(arrow::util::span<uint8_t> 
output_buffer) const {
+  ARROW_CHECK(output_buffer.size() >= static_cast<size_t>(GetStoredSize()))
+      << "alp_for_vector_info_output_too_small: " << output_buffer.size() << " 
vs "
+      << GetStoredSize();
+
+  uint8_t* ptr = output_buffer.data();
+
+  // frame_of_reference: 4 bytes for float, 8 bytes for double
+  std::memcpy(ptr, &frame_of_reference, sizeof(frame_of_reference));
+  ptr += sizeof(frame_of_reference);
+
+  // bit_width: 1 byte
+  *ptr = bit_width;
+}
+
+template <typename T>
+Result<AlpEncodedForVectorInfo<T>> AlpEncodedForVectorInfo<T>::Load(
+    arrow::util::span<const uint8_t> input_buffer) {
+  if (input_buffer.size() < static_cast<size_t>(GetStoredSize())) {
+    return Status::Invalid("ALP FOR vector info buffer too small: ", 
input_buffer.size(),
+                           " < ", GetStoredSize());
+  }
+
+  AlpEncodedForVectorInfo<T> result{};
+  const uint8_t* ptr = input_buffer.data();
+
+  // frame_of_reference: 4 bytes for float, 8 bytes for double
+  std::memcpy(&result.frame_of_reference, ptr, 
sizeof(result.frame_of_reference));
+  ptr += sizeof(result.frame_of_reference);
+
+  // bit_width: 1 byte
+  result.bit_width = *ptr;
+
+  return result;
+}
+
+// Explicit template instantiations for AlpEncodedForVectorInfo
+template struct AlpEncodedForVectorInfo<float>;
+template struct AlpEncodedForVectorInfo<double>;
+
+// ----------------------------------------------------------------------
+// AlpEncodedVector implementation
+
+template <typename T>
+void AlpEncodedVector<T>::Store(arrow::util::span<uint8_t> output_buffer) 
const {
+  const int64_t overall_size = GetStoredSize();
+  ARROW_CHECK(static_cast<int64_t>(output_buffer.size()) >= overall_size)
+      << "alp_bit_packed_vector_store_output_too_small: " << 
output_buffer.size()
+      << " vs " << overall_size;
+
+  int64_t offset = 0;
+
+  // Store AlpInfo (4 bytes)
+  alp_info.Store({output_buffer.data() + offset, 
AlpEncodedVectorInfo::kStoredSize});
+  offset += AlpEncodedVectorInfo::kStoredSize;
+
+  // Store ForInfo (6/10 bytes)
+  for_info.Store({output_buffer.data() + offset, 
AlpEncodedForVectorInfo<T>::kStoredSize});
+  offset += AlpEncodedForVectorInfo<T>::kStoredSize;
+
+  // Store data section
+  StoreDataOnly({output_buffer.data() + offset, output_buffer.size() - 
offset});
+}
+
+template <typename T>
+void AlpEncodedVector<T>::StoreDataOnly(arrow::util::span<uint8_t> 
output_buffer) const {
+  const int64_t data_size = GetDataStoredSize();
+  ARROW_CHECK(static_cast<int64_t>(output_buffer.size()) >= data_size)
+      << "alp_bit_packed_vector_store_data_output_too_small: " << 
output_buffer.size()
+      << " vs " << data_size;
+
+  ARROW_CHECK(static_cast<size_t>(alp_info.num_exceptions) == 
exceptions.size() &&
+              static_cast<size_t>(alp_info.num_exceptions) == 
exception_positions.size())
+      << "alp_bit_packed_vector_store_num_exceptions_mismatch: "
+      << alp_info.num_exceptions << " vs " << exceptions.size() << " vs "
+      << exception_positions.size();
+
+  int64_t offset = 0;
+
+  // Compute bit_packed_size from num_elements and bit_width
+  const int64_t bit_packed_size =
+      AlpEncodedForVectorInfo<T>::GetBitPackedSize(num_elements, 
for_info.bit_width);
+
+  // Store all successfully compressed values first.
+  std::memcpy(output_buffer.data() + offset, packed_values.data(), 
bit_packed_size);
+  offset += bit_packed_size;
+
+  // Store exception positions.
+  const int64_t exception_position_size =
+      alp_info.num_exceptions * sizeof(AlpConstants::PositionType);
+  std::memcpy(output_buffer.data() + offset, exception_positions.data(),
+              exception_position_size);
+  offset += exception_position_size;
+
+  // Store exception values.
+  const int64_t exception_size = alp_info.num_exceptions * sizeof(T);
+  std::memcpy(output_buffer.data() + offset, exceptions.data(), 
exception_size);
+  offset += exception_size;
+
+  ARROW_CHECK(offset == data_size)
+      << "alp_bit_packed_vector_data_size_mismatch: " << offset << " vs " << 
data_size;
+}
+
+template <typename T>
+Result<AlpEncodedVector<T>> AlpEncodedVector<T>::Load(
+    arrow::util::span<const uint8_t> input_buffer, int32_t num_elements) {
+  if (num_elements > (1 << AlpConstants::kMaxLogVectorSize)) {
+    return Status::Invalid("ALP element count too large: ", num_elements,
+                           " > ", (1 << AlpConstants::kMaxLogVectorSize));
+  }
+
+  AlpEncodedVector<T> result;
+  int64_t input_offset = 0;
+
+  // Load AlpInfo (4 bytes)
+  ARROW_ASSIGN_OR_RAISE(
+      result.alp_info,
+      AlpEncodedVectorInfo::Load(
+          {input_buffer.data() + input_offset, 
AlpEncodedVectorInfo::kStoredSize}));
+  input_offset += AlpEncodedVectorInfo::kStoredSize;
+
+  // Load ForInfo (6/10 bytes)
+  ARROW_ASSIGN_OR_RAISE(
+      result.for_info,
+      AlpEncodedForVectorInfo<T>::Load(
+          {input_buffer.data() + input_offset, 
AlpEncodedForVectorInfo<T>::kStoredSize}));
+  input_offset += AlpEncodedForVectorInfo<T>::kStoredSize;
+
+  result.num_elements = num_elements;
+
+  const int64_t overall_size =
+      GetStoredSize(result.alp_info, result.for_info, num_elements);
+
+  if (static_cast<int64_t>(input_buffer.size()) < overall_size) {
+    return Status::Invalid("ALP compressed vector buffer too small: ",
+                           input_buffer.size(), " < ", overall_size);
+  }
+
+  // Compute bit_packed_size from num_elements and bit_width
+  const int64_t bit_packed_size =
+      AlpEncodedForVectorInfo<T>::GetBitPackedSize(num_elements, 
result.for_info.bit_width);
+
+  result.packed_values.resize(bit_packed_size);

Review Comment:
   is this in the hot path?  did zeroing out values show up in any profiling?  
Maybe we can leave a TODO to re-examine?



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

Reply via email to