wgtmac commented on code in PR #50252:
URL: https://github.com/apache/arrow/pull/50252#discussion_r3479593632


##########
cpp/src/arrow/extension/variant/encoding.h:
##########
@@ -0,0 +1,166 @@
+// 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.
+
+#pragma once
+
+#include <cstdint>
+#include <optional>
+#include <string_view>
+#include <utility>
+#include <variant>
+#include <vector>
+
+#include "arrow/result.h"
+#include "arrow/status.h"
+#include "arrow/util/visibility.h"
+
+namespace arrow::extension {
+
+enum class VariantBasicType : uint8_t {
+  Primitive = 0,
+  ShortString = 1,
+  Object = 2,
+  Array = 3,
+};
+
+enum class VariantPrimitiveType : uint8_t {
+  Null = 0,
+  BooleanTrue = 1,
+  BooleanFalse = 2,
+  Int8 = 3,
+  Int16 = 4,
+  Int32 = 5,
+  Int64 = 6,
+  Double = 7,
+  Decimal4 = 8,
+  Decimal8 = 9,
+  Decimal16 = 10,
+  Date = 11,
+  TimestampMicros = 12,
+  TimestampNTZMicros = 13,
+  Float = 14,
+  Binary = 15,
+  String = 16,
+  TimeNTZMicros = 17,
+  TimestampNanos = 18,
+  TimestampNTZNanos = 19,
+  Uuid = 20,
+};
+
+struct ARROW_EXPORT VariantObjectField {
+  std::string_view name;
+  uint32_t field_id = 0;

Review Comment:
   Use int32_t



##########
cpp/src/arrow/extension/variant/encoding.h:
##########
@@ -0,0 +1,166 @@
+// 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.
+
+#pragma once
+
+#include <cstdint>
+#include <optional>
+#include <string_view>
+#include <utility>
+#include <variant>
+#include <vector>
+
+#include "arrow/result.h"
+#include "arrow/status.h"
+#include "arrow/util/visibility.h"
+
+namespace arrow::extension {
+
+enum class VariantBasicType : uint8_t {
+  Primitive = 0,
+  ShortString = 1,
+  Object = 2,
+  Array = 3,
+};
+
+enum class VariantPrimitiveType : uint8_t {
+  Null = 0,
+  BooleanTrue = 1,
+  BooleanFalse = 2,
+  Int8 = 3,
+  Int16 = 4,
+  Int32 = 5,
+  Int64 = 6,
+  Double = 7,
+  Decimal4 = 8,
+  Decimal8 = 9,
+  Decimal16 = 10,
+  Date = 11,
+  TimestampMicros = 12,
+  TimestampNTZMicros = 13,
+  Float = 14,
+  Binary = 15,
+  String = 16,
+  TimeNTZMicros = 17,
+  TimestampNanos = 18,
+  TimestampNTZNanos = 19,
+  Uuid = 20,
+};
+
+struct ARROW_EXPORT VariantObjectField {
+  std::string_view name;
+  uint32_t field_id = 0;
+  std::string_view value;
+};
+
+class ARROW_EXPORT VariantMetadataView {
+ public:
+  /// Parse Variant metadata bytes without copying them.
+  ///
+  /// The returned view and all string views borrowed from it are valid only 
while the
+  /// input metadata bytes remain alive and unchanged.
+  static Result<VariantMetadataView> Make(std::string_view metadata);
+
+  std::string_view metadata() const { return metadata_; }
+  bool sorted_strings() const { return sorted_strings_; }
+  uint8_t offset_size() const { return offset_size_; }
+  uint32_t dictionary_size() const { return 
static_cast<uint32_t>(strings_.size()); }
+
+  std::string_view string(uint32_t id) const;

Review Comment:
   Let's add some comment to help understand that this is the dictionary.



##########
cpp/src/arrow/extension/variant/encoding.cc:
##########
@@ -0,0 +1,465 @@
+// 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/encoding.h"
+
+#include <algorithm>
+#include <cstring>
+#include <utility>
+
+#include "arrow/extension/variant/encoding_internal.h"
+#include "arrow/util/endian.h"
+#include "arrow/util/logging_internal.h"
+
+namespace arrow::extension {
+
+namespace {
+
+using ::arrow::Status;
+
+uint32_t ReadLittleEndian(std::string_view data, size_t offset, size_t width) {
+  DCHECK_LE(width, sizeof(uint32_t));
+  uint32_t value = 0;
+  std::memcpy(&value, data.data() + offset, width);
+  return bit_util::FromLittleEndian(value);
+}
+
+Status CheckAvailable(std::string_view data, size_t offset, size_t size,
+                      std::string_view context) {
+  if (offset > data.size() || data.size() - offset < size) {
+    return Status::Invalid("Invalid Variant encoding: truncated ", context);
+  }
+  return Status::OK();
+}
+
+Status PrimitivePayloadSize(std::string_view value, size_t offset,
+                            VariantPrimitiveType primitive, size_t* size) {

Review Comment:
   BTW, if we decide to move to parquet folder, we should use 
`ParquetException` instead of `arrow::Result/Status`.



##########
cpp/src/arrow/extension/variant/encoding.h:
##########
@@ -0,0 +1,166 @@
+// 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.
+
+#pragma once
+
+#include <cstdint>
+#include <optional>
+#include <string_view>
+#include <utility>
+#include <variant>
+#include <vector>
+
+#include "arrow/result.h"
+#include "arrow/status.h"
+#include "arrow/util/visibility.h"
+
+namespace arrow::extension {
+
+enum class VariantBasicType : uint8_t {
+  Primitive = 0,
+  ShortString = 1,
+  Object = 2,
+  Array = 3,
+};
+
+enum class VariantPrimitiveType : uint8_t {
+  Null = 0,
+  BooleanTrue = 1,
+  BooleanFalse = 2,
+  Int8 = 3,
+  Int16 = 4,
+  Int32 = 5,
+  Int64 = 6,
+  Double = 7,
+  Decimal4 = 8,
+  Decimal8 = 9,
+  Decimal16 = 10,
+  Date = 11,
+  TimestampMicros = 12,
+  TimestampNTZMicros = 13,
+  Float = 14,
+  Binary = 15,
+  String = 16,
+  TimeNTZMicros = 17,
+  TimestampNanos = 18,
+  TimestampNTZNanos = 19,
+  Uuid = 20,
+};
+
+struct ARROW_EXPORT VariantObjectField {
+  std::string_view name;
+  uint32_t field_id = 0;
+  std::string_view value;
+};
+
+class ARROW_EXPORT VariantMetadataView {

Review Comment:
   We need add comment about lifetime promise of these objects.



##########
cpp/src/arrow/extension/variant/encoding.cc:
##########
@@ -0,0 +1,465 @@
+// 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/encoding.h"
+
+#include <algorithm>
+#include <cstring>
+#include <utility>
+
+#include "arrow/extension/variant/encoding_internal.h"
+#include "arrow/util/endian.h"
+#include "arrow/util/logging_internal.h"
+
+namespace arrow::extension {
+
+namespace {
+
+using ::arrow::Status;
+
+uint32_t ReadLittleEndian(std::string_view data, size_t offset, size_t width) {
+  DCHECK_LE(width, sizeof(uint32_t));
+  uint32_t value = 0;
+  std::memcpy(&value, data.data() + offset, width);
+  return bit_util::FromLittleEndian(value);
+}
+
+Status CheckAvailable(std::string_view data, size_t offset, size_t size,
+                      std::string_view context) {
+  if (offset > data.size() || data.size() - offset < size) {
+    return Status::Invalid("Invalid Variant encoding: truncated ", context);
+  }
+  return Status::OK();
+}
+
+Status PrimitivePayloadSize(std::string_view value, size_t offset,
+                            VariantPrimitiveType primitive, size_t* size) {

Review Comment:
   Why not returning `Result<size_t>`?



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