scovich commented on code in PR #46372:
URL: https://github.com/apache/arrow/pull/46372#discussion_r2100787349


##########
cpp/src/parquet/variant.h:
##########
@@ -0,0 +1,261 @@
+// 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 <vector>
+
+#include <arrow/util/decimal.h>
+#include <arrow/util/small_vector.h>
+
+namespace parquet::variant {
+
+// TODO(mwish): Should I use parquet::ByteArray rather than
+//  std::string_view?
+
+enum class VariantBasicType {
+  /// One of the primitive types
+  Primitive = 0,
+  /// A string with a length less than 64 bytes
+  ShortString = 1,
+  /// A collection of (string-key, variant-value) pairs
+  Object = 2,
+  /// An ordered sequence of variant values
+  Array = 3
+};
+
+std::string variantBasicTypeToString(VariantBasicType type);
+
+enum class VariantPrimitiveType : int8_t {
+  /// Equivalent Parquet Type: UNKNOWN
+  NullType = 0,
+  /// Equivalent Parquet Type: BOOLEAN
+  BooleanTrue = 1,
+  /// Equivalent Parquet Type: BOOLEAN
+  BooleanFalse = 2,
+  /// Equivalent Parquet Type: INT(8, signed)
+  Int8 = 3,
+  /// Equivalent Parquet Type: INT(16, signed)
+  Int16 = 4,
+  /// Equivalent Parquet Type: INT(32, signed)
+  Int32 = 5,
+  /// Equivalent Parquet Type: INT(64, signed)
+  Int64 = 6,
+  /// Equivalent Parquet Type: DOUBLE
+  Double = 7,
+  /// Equivalent Parquet Type: DECIMAL(precision, scale)
+  Decimal4 = 8,
+  /// Equivalent Parquet Type: DECIMAL(precision, scale)
+  Decimal8 = 9,
+  /// Equivalent Parquet Type: DECIMAL(precision, scale)
+  Decimal16 = 10,
+  /// Equivalent Parquet Type: DATE
+  Date = 11,
+  /// Equivalent Parquet Type: TIMESTAMP(isAdjustedToUTC=true, MICROS)
+  Timestamp = 12,
+  /// Equivalent Parquet Type: TIMESTAMP(isAdjustedToUTC=false, MICROS)
+  TimestampNTZ = 13,
+  /// Equivalent Parquet Type: FLOAT
+  Float = 14,
+  /// Equivalent Parquet Type: BINARY
+  Binary = 15,
+  /// Equivalent Parquet Type: STRING
+  String = 16,
+  /// Equivalent Parquet Type: TIME(isAdjustedToUTC=false, MICROS)
+  TimeNTZ = 17,
+  /// Equivalent Parquet Type: TIMESTAMP(isAdjustedToUTC=true, NANOS)
+  TimestampTZ = 18,  // Assuming TZ stands for TimeZone, and follows the 
document's
+                     // 'timestamp with time zone'
+  /// Equivalent Parquet Type: TIMESTAMP(isAdjustedToUTC=false, NANOS)
+  TimestampNTZNanos = 19,  // Differentiating from TimestampNTZ (MICROS)
+  /// Equivalent Parquet Type: UUID
+  Uuid = 20
+};
+
+std::string variantPrimitiveTypeToString(VariantPrimitiveType type);
+
+/// VariantType is from basic type and primitive type.
+enum class VariantType {
+  OBJECT,
+  ARRAY,
+  VARIANT_NULL,
+  BOOLEAN,
+  INT8,
+  INT16,
+  INT32,
+  INT64,
+  STRING,
+  DOUBLE,
+  DECIMAL4,
+  DECIMAL8,
+  DECIMAL16,
+  DATE,
+  TIMESTAMP_TZ,
+  TIMESTAMP_NTZ,
+  FLOAT,
+  BINARY,
+  TIME,
+  TIMESTAMP_NANOS_TZ,
+  TIMESTAMP_NANOS_NTZ,
+  UUID
+};
+
+std::string variantTypeToString(VariantType type);
+
+class VariantMetadata {
+ public:
+  explicit VariantMetadata(std::string_view metadata);
+  /// \brief Get the variant metadata version. Currently, always 1.
+  uint8_t version() const;
+  /// \brief Get the metadata key for a given variant field id.
+  /// \throw ParquetException if the variant_id is out of range(larger than
+  ///        dictionary size).
+  std::string_view getMetadataKey(uint32_t variant_id) const;
+  /// \brief Get the metadata id for a given key.
+  /// From the discussion in ML:
+  /// https://lists.apache.org/thread/b68tjmrjmy64mbv9dknpmqs28vnzjj96 if
+  /// !sortedStrings(), the metadata key is not guaranteed to be unique, so we 
use a
+  /// vector to store all the metadata ids.
+  ::arrow::internal::SmallVector<uint32_t, 1> getMetadataId(std::string_view 
key) const;
+
+  bool sortedStrings() const;
+  uint8_t offsetSize() const;
+  uint32_t dictionarySize() const;
+
+ private:
+  static uint32_t loadDictionarySize(std::string_view metadata, uint8_t 
offset_size);
+
+ private:
+  static constexpr uint8_t VERSION_MASK = 0b1111;
+  static constexpr uint8_t SORTED_STRING_MASK = 0b10000;
+  static constexpr size_t HEADER_SIZE_BYTES = 1;
+  static constexpr size_t MINIMAL_OFFSET_SIZE_BYTES = 1;
+  static constexpr size_t MAXIMUM_OFFSET_SIZE_BYTES = 4;
+  // mask is applied after shift, it's like 0b11000000 before shift.
+  static constexpr uint8_t OFFSET_SIZE_MASK = 0b11;
+  static constexpr uint8_t OFFSET_SIZE_BIT_SHIFT = 6;
+  static constexpr uint8_t SUPPORTED_VERSION = 1;
+
+ private:
+  std::string_view metadata_;
+  uint32_t dictionary_size_{0};
+};
+
+template <typename DecimalType>
+struct DecimalValue {
+  uint8_t scale;
+  DecimalType value;
+};
+
+struct VariantValue {
+  VariantMetadata metadata;
+  std::string_view value;
+
+  VariantBasicType getBasicType() const;
+  VariantType getType() const;
+  std::string typeDebugString() const;
+
+  /// \defgroup ValueAccessors
+  /// @{
+
+  // Note: Null doesn't need visitor.
+  bool getBool() const;
+  int8_t getInt8() const;
+  int16_t getInt16() const;
+  int32_t getInt32() const;
+  int64_t getInt64() const;
+  /// Include short_string optimization and primitive string type
+  std::string_view getString() const;
+  std::string_view getBinary() const;
+  float getFloat() const;
+  double getDouble() const;
+
+  DecimalValue<::arrow::Decimal32> getDecimal4() const;
+  DecimalValue<::arrow::Decimal64> getDecimal8() const;
+  DecimalValue<::arrow::Decimal128> getDecimal16() const;
+
+  int32_t getDate() const;
+  int64_t getTimeNTZ() const;
+  // timestamp with adjusted to UTC
+  int64_t getTimestamp() const;
+  int64_t getTimestampNTZ() const;
+  // 16 bytes UUID
+  std::array<uint8_t, 16> getUuid() const;

Review Comment:
   Also, `string_view` would occupy (at least) the same 16 bytes as this array, 
with a more complex structure to access. And byte-swapping is a single machine 
instruction in most architectures so the conversion should be super cheap.



-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to