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


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

Review Comment:
   5 or 9? maybe just remove the detail?
   ```suggestion
     // Store ForInfo 
   ```



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