qzyu999 commented on code in PR #50121:
URL: https://github.com/apache/arrow/pull/50121#discussion_r3485320536


##########
cpp/src/arrow/extension/variant_internal.cc:
##########
@@ -0,0 +1,1020 @@
+// 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/extension/variant_internal.h"
+
+#include <cstring>
+
+#include "arrow/util/endian.h"
+#include "arrow/util/logging_internal.h"
+
+namespace arrow::extension::variant_internal {
+
+namespace {
+
+// ---------------------------------------------------------------------------
+// Helpers for reading little-endian integers of variable size (1-4 bytes)
+// ---------------------------------------------------------------------------
+
+/// \brief Read an unsigned integer of 1-4 bytes in little-endian order.
+///
+/// On big-endian platforms, FromLittleEndian byte-swaps the full 32-bit
+/// word after memcpy; the mask then discards any bytes beyond num_bytes.
+///
+/// \param[in] data Pointer to the bytes (must have at least num_bytes valid)
+/// \param[in] num_bytes Number of bytes to read (1, 2, 3, or 4)
+/// \return The decoded unsigned integer value
+inline uint32_t ReadUnsignedLE(const uint8_t* data, int32_t num_bytes) {
+  uint32_t result = 0;
+  std::memcpy(&result, data, num_bytes);
+  result = bit_util::FromLittleEndian(result);
+  if (num_bytes < 4) {
+    result &= (static_cast<uint32_t>(1) << (num_bytes * 8)) - 1;
+  }
+  return result;
+}
+
+/// \brief Validate that an offset array is monotonically non-decreasing
+///        and within the buffer bounds.
+Status ValidateOffsets(const std::vector<uint32_t>& offsets, int64_t 
data_length) {
+  for (size_t i = 1; i < offsets.size(); ++i) {
+    if (offsets[i] < offsets[i - 1]) {
+      return Status::Invalid(
+          "Variant metadata: string offsets are not monotonically "
+          "non-decreasing at index ",
+          i);
+    }
+  }
+  if (!offsets.empty() && offsets.back() > static_cast<uint32_t>(data_length)) 
{
+    return Status::Invalid("Variant metadata: last string offset ", 
offsets.back(),
+                           " exceeds data length ", data_length);
+  }
+  return Status::OK();
+}
+
+// ---------------------------------------------------------------------------
+// Value decoding helpers
+// ---------------------------------------------------------------------------
+
+/// \brief Decode a single variant value at the given offset and invoke
+///        the visitor. Returns the number of bytes consumed.
+///
+/// This is the core recursive function.
+Status DecodeValueAt(const VariantMetadata& metadata, const uint8_t* data, 
int64_t length,

Review Comment:
   In the refactored design, this use case is covered by 
`VariantView::Make(metadata, data + offset, size)`, you can construct a view at 
any byte offset within a buffer. There's no separate `DecodeValueAt` because 
the view factory IS the decode-at-offset operation.
   
   For object fields specifically, `VariantObjectView::locate(name)` returns an 
`optional<FieldLocation>` with offset + size without constructing the inner 
view, which is useful for zero-copy byte transfer (used by the shredding path).



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