mapleFU commented on code in PR #46372: URL: https://github.com/apache/arrow/pull/46372#discussion_r2086632875
########## cpp/src/parquet/variant.h: ########## @@ -0,0 +1,212 @@ +// 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 <string_view> +#include <vector> + +#include <arrow/util/decimal.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 +}; + +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 +}; + +/// VariantType is from basic type and primitive type. +enum class VariantType { + OBJECT, + ARRAY, + VARIANT_NULL, + BOOLEAN, + BYTE, + SHORT, + INT, + LONG, + STRING, + DOUBLE, + DECIMAL4, + DECIMAL8, + DECIMAL16, + DATE, + TIMESTAMP_TZ, + TIMESTAMP_NTZ, + FLOAT, + BINARY, + TIME, + TIMESTAMP_NANOS_TZ, + TIMESTAMP_NANOS_NTZ, + UUID +}; + +class VariantMetadata { + public: + explicit VariantMetadata(std::string_view metadata); + /// \brief Get the variant metadata version. Currently, always 1. + int8_t version() const; + /// \brief Get the metadata key for a given variant field id. + std::string_view getMetadataKey(int32_t variantId) const; + + private: + bool sortedStrings() const; + uint8_t offsetSize() const; + uint32_t dictionarySize() const; + + private: + std::string_view metadata_; +}; + +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; + + int64_t timeNTZ() const; + // timestamp with adjusted to UTC + int64_t getTimestamp() const; + int64_t getTimestampNTZ() const; + // 16 bytes UUID + const uint8_t* getUuid() const; + + /// }@ + + struct ObjectInfo { + uint32_t num_elements; + uint32_t id_size; + uint32_t offset_size; + uint32_t id_start_offset; + uint32_t offset_start_offset; + uint32_t data_start_offset; + + std::string toDebugString() const; + }; + ObjectInfo getObjectInfo() const; + std::optional<VariantValue> getObjectValueByKey(std::string_view key) const; + std::optional<VariantValue> getObjectFieldByFieldId(uint32_t variantId, + std::string_view* key) const; + + struct ArrayInfo { + uint32_t num_elements; + uint32_t offset_size; + uint32_t offset_start_offset; + uint32_t data_start_offset; + }; + ArrayInfo getArrayInfo() const; + // Would throw ParquetException if index is out of range. + VariantValue getArrayValueByIndex(uint32_t index) const; + + static constexpr uint8_t BASIC_TYPE_MASK = 0b00000011; Review Comment: This code needs to cleanup ########## cpp/src/parquet/variant.h: ########## @@ -0,0 +1,212 @@ +// 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 <string_view> +#include <vector> + +#include <arrow/util/decimal.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 +}; + +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 +}; + +/// VariantType is from basic type and primitive type. +enum class VariantType { + OBJECT, + ARRAY, + VARIANT_NULL, + BOOLEAN, + BYTE, + SHORT, + INT, + LONG, + STRING, + DOUBLE, + DECIMAL4, + DECIMAL8, + DECIMAL16, + DATE, + TIMESTAMP_TZ, + TIMESTAMP_NTZ, + FLOAT, + BINARY, + TIME, + TIMESTAMP_NANOS_TZ, + TIMESTAMP_NANOS_NTZ, + UUID +}; + +class VariantMetadata { + public: + explicit VariantMetadata(std::string_view metadata); + /// \brief Get the variant metadata version. Currently, always 1. + int8_t version() const; + /// \brief Get the metadata key for a given variant field id. + std::string_view getMetadataKey(int32_t variantId) const; + + private: + bool sortedStrings() const; + uint8_t offsetSize() const; + uint32_t dictionarySize() const; + + private: + std::string_view metadata_; +}; + +template <typename DecimalType> Review Comment: This is because decimal data is with a byte's precision. So use an extra "scale" here ########## cpp/src/parquet/variant.h: ########## @@ -0,0 +1,212 @@ +// 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 <string_view> +#include <vector> + +#include <arrow/util/decimal.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 +}; + +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 +}; + +/// VariantType is from basic type and primitive type. +enum class VariantType { + OBJECT, + ARRAY, + VARIANT_NULL, + BOOLEAN, + BYTE, + SHORT, + INT, + LONG, + STRING, + DOUBLE, + DECIMAL4, + DECIMAL8, + DECIMAL16, + DATE, + TIMESTAMP_TZ, + TIMESTAMP_NTZ, + FLOAT, + BINARY, + TIME, + TIMESTAMP_NANOS_TZ, + TIMESTAMP_NANOS_NTZ, + UUID +}; + +class VariantMetadata { + public: + explicit VariantMetadata(std::string_view metadata); + /// \brief Get the variant metadata version. Currently, always 1. + int8_t version() const; + /// \brief Get the metadata key for a given variant field id. + std::string_view getMetadataKey(int32_t variantId) const; + + private: + bool sortedStrings() const; + uint8_t offsetSize() const; + uint32_t dictionarySize() const; + + private: + std::string_view metadata_; +}; + +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. Review Comment: I don't know should we just return an arrow's `Scalar`, it would be easy to use but in-efficient. ########## cpp/src/parquet/variant.cc: ########## @@ -0,0 +1,592 @@ +// 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 "parquet/variant.h" + +#include <cstdint> +#include <iostream> +#include <string_view> + +#include "arrow/util/endian.h" +#include "parquet/exception.h" + +namespace parquet::variant { + +VariantMetadata::VariantMetadata(std::string_view metadata) : metadata_(metadata) { + if (metadata.size() < 2) { + throw ParquetException("Invalid Variant metadata: too short: " + + std::to_string(metadata.size())); + } +} + +int8_t VariantMetadata::version() const { + return static_cast<int8_t>(metadata_[0]) & 0x0F; +} + +bool VariantMetadata::sortedStrings() const { return (metadata_[0] & 0b10000) != 0; } + +uint8_t VariantMetadata::offsetSize() const { return ((metadata_[0] >> 6) & 0x3) + 1; } + +uint32_t VariantMetadata::dictionarySize() const { + uint8_t length = offsetSize(); + if (length > 4) { + throw ParquetException("Invalid offset size: " + std::to_string(length)); + } + if (length + 1 > metadata_.size()) { + throw ParquetException("Invalid Variant metadata: too short for dictionary size"); + } + uint32_t dict_size = 0; + memcpy(&dict_size, metadata_.data() + 1, length); + dict_size = arrow::bit_util::FromLittleEndian(dict_size); + return dict_size; +} + +std::string_view VariantMetadata::getMetadataKey(int32_t variantId) const { + uint32_t offset_size = offsetSize(); + uint32_t dict_size = dictionarySize(); + + if (variantId < 0 || variantId >= static_cast<int32_t>(dict_size)) { + throw ParquetException("Invalid Variant metadata: variantId out of range"); + } + + if ((dict_size + 1) * offset_size > metadata_.size()) { + throw ParquetException("Invalid Variant metadata: offset out of range"); + } + + size_t offset_start_pos = 1 + offset_size + (variantId * offset_size); + + uint32_t variant_offset = 0; + uint32_t variant_next_offset = 0; + memcpy(&variant_offset, metadata_.data() + offset_start_pos, offset_size); + variant_offset = arrow::bit_util::FromLittleEndian(variant_offset); + memcpy(&variant_next_offset, metadata_.data() + offset_start_pos + offset_size, + offset_size); + variant_next_offset = arrow::bit_util::FromLittleEndian(variant_next_offset); + + uint32_t key_size = variant_next_offset - variant_offset; + + size_t string_start = 1 + offset_size * (dict_size + 2) + variant_offset; + if (string_start + key_size > metadata_.size()) { + throw ParquetException("Invalid Variant metadata: string data out of range"); + } + return std::string_view(metadata_.data() + string_start, key_size); +} + +VariantBasicType VariantValue::getBasicType() const { + if (value.empty()) { + throw ParquetException("Empty variant value"); + } + return static_cast<VariantBasicType>(value[0] & BASIC_TYPE_MASK); +} + +VariantType VariantValue::getType() const { + VariantBasicType basic_type = getBasicType(); + // std::cout << "Variant first byte:" << static_cast<int>(value[0] >> 2) << ", " Review Comment: todo: remove these debug messages... -- 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]
