zhjwpku commented on code in PR #100: URL: https://github.com/apache/iceberg-cpp/pull/100#discussion_r2095119710
########## src/iceberg/avro/avro_schema_util.cc: ########## @@ -0,0 +1,266 @@ +/* + * 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 <format> +#include <mutex> +#include <string_view> + +#include <arrow/type.h> +#include <arrow/util/decimal.h> +#include <avro/CustomAttributes.hh> +#include <avro/LogicalType.hh> +#include <avro/NodeImpl.hh> +#include <avro/Types.hh> + +#include "iceberg/avro/avro_schema_util_internal.h" +#include "iceberg/util/macros.h" +#include "iceberg/util/visit_type.h" + +namespace iceberg::avro { + +namespace { + +constexpr std::string_view kIcebergFieldNameProp = "iceberg-field-name"; +constexpr std::string_view kFieldIdProp = "field-id"; +constexpr std::string_view kKeyIdProp = "key-id"; +constexpr std::string_view kValueIdProp = "value-id"; +constexpr std::string_view kElementIdProp = "element-id"; +constexpr std::string_view kAdjustToUtcProp = "adjust-to-utc"; + +struct MapLogicalType : public ::avro::CustomLogicalType { + MapLogicalType() : ::avro::CustomLogicalType("map") {} +}; + +::avro::LogicalType GetMapLogicalType() { + static std::once_flag flag{}; + std::call_once(flag, []() { + // Register the map logical type with the avro custom logical type registry. + // See https://github.com/apache/avro/pull/3326 for details. + ::avro::CustomLogicalTypeRegistry::instance().registerType( + "map", [](const std::string&) { return std::make_shared<MapLogicalType>(); }); + }); + return ::avro::LogicalType(std::make_shared<MapLogicalType>()); +} + +::avro::CustomAttributes GetAttributesWithFieldId(int32_t field_id) { + ::avro::CustomAttributes attributes; + attributes.addAttribute(std::string(kFieldIdProp), std::to_string(field_id), + /*addQuotes=*/false); + return attributes; +} + +} // namespace + +Status ToAvroNodeVisitor::Visit(const BooleanType& type, ::avro::NodePtr* node) { + *node = std::make_shared<::avro::NodePrimitive>(::avro::AVRO_BOOL); + return {}; +} + +Status ToAvroNodeVisitor::Visit(const IntType& type, ::avro::NodePtr* node) { + *node = std::make_shared<::avro::NodePrimitive>(::avro::AVRO_INT); + return {}; +} + +Status ToAvroNodeVisitor::Visit(const LongType& type, ::avro::NodePtr* node) { + *node = std::make_shared<::avro::NodePrimitive>(::avro::AVRO_LONG); + return {}; +} + +Status ToAvroNodeVisitor::Visit(const FloatType& type, ::avro::NodePtr* node) { + *node = std::make_shared<::avro::NodePrimitive>(::avro::AVRO_FLOAT); + return {}; +} + +Status ToAvroNodeVisitor::Visit(const DoubleType& type, ::avro::NodePtr* node) { + *node = std::make_shared<::avro::NodePrimitive>(::avro::AVRO_DOUBLE); + return {}; +} + +Status ToAvroNodeVisitor::Visit(const DecimalType& type, ::avro::NodePtr* node) { + *node = std::make_shared<::avro::NodeFixed>(); + (*node)->setName( + ::avro::Name(std::format("decimal_{}_{}", type.precision(), type.scale()))); + (*node)->setFixedSize(::arrow::DecimalType::DecimalSize(type.precision())); + + ::avro::LogicalType logical_type(::avro::LogicalType::DECIMAL); + logical_type.setPrecision(type.precision()); + logical_type.setScale(type.scale()); + (*node)->setLogicalType(logical_type); + + return {}; +} + +Status ToAvroNodeVisitor::Visit(const DateType& type, ::avro::NodePtr* node) { + *node = std::make_shared<::avro::NodePrimitive>(::avro::AVRO_INT); + (*node)->setLogicalType(::avro::LogicalType{::avro::LogicalType::DATE}); + return {}; +} + +Status ToAvroNodeVisitor::Visit(const TimeType& type, ::avro::NodePtr* node) { + *node = std::make_shared<::avro::NodePrimitive>(::avro::AVRO_LONG); + (*node)->setLogicalType(::avro::LogicalType{::avro::LogicalType::TIME_MICROS}); + return {}; +} + +Status ToAvroNodeVisitor::Visit(const TimestampType& type, ::avro::NodePtr* node) { + *node = std::make_shared<::avro::NodePrimitive>(::avro::AVRO_LONG); + (*node)->setLogicalType(::avro::LogicalType{::avro::LogicalType::TIMESTAMP_MICROS}); + ::avro::CustomAttributes attributes; + attributes.addAttribute(std::string(kAdjustToUtcProp), "false", /*addQuotes=*/false); + (*node)->addCustomAttributesForField(attributes); + return {}; +} + +Status ToAvroNodeVisitor::Visit(const TimestampTzType& type, ::avro::NodePtr* node) { + *node = std::make_shared<::avro::NodePrimitive>(::avro::AVRO_LONG); + (*node)->setLogicalType(::avro::LogicalType{::avro::LogicalType::TIMESTAMP_MICROS}); + ::avro::CustomAttributes attributes; + attributes.addAttribute(std::string(kAdjustToUtcProp), "true", /*addQuotes=*/false); + (*node)->addCustomAttributesForField(attributes); + return {}; +} + +Status ToAvroNodeVisitor::Visit(const StringType& type, ::avro::NodePtr* node) { + *node = std::make_shared<::avro::NodePrimitive>(::avro::AVRO_STRING); + return {}; +} + +Status ToAvroNodeVisitor::Visit(const UuidType& type, ::avro::NodePtr* node) { + *node = std::make_shared<::avro::NodePrimitive>(::avro::AVRO_FIXED); + (*node)->setLogicalType(::avro::LogicalType{::avro::LogicalType::UUID}); + (*node)->setFixedSize(16); + (*node)->setName(::avro::Name("uuid_fixed")); Review Comment: I'm wondering if the `_fixed` suffix is needed. ########## src/iceberg/avro/avro_schema_util_internal.h: ########## @@ -0,0 +1,58 @@ +/* + * 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 <stack> + +#include <avro/Node.hh> + +#include "iceberg/result.h" +#include "iceberg/type.h" + +namespace iceberg::avro { + +/// \brief A visitor that converts an Iceberg type to an Avro node. +class ToAvroNodeVisitor { + public: + Status Visit(const BooleanType& type, ::avro::NodePtr* node); + Status Visit(const IntType& type, ::avro::NodePtr* node); + Status Visit(const LongType& type, ::avro::NodePtr* node); + Status Visit(const FloatType& type, ::avro::NodePtr* node); + Status Visit(const DoubleType& type, ::avro::NodePtr* node); + Status Visit(const DecimalType& type, ::avro::NodePtr* node); + Status Visit(const DateType& type, ::avro::NodePtr* node); + Status Visit(const TimeType& type, ::avro::NodePtr* node); + Status Visit(const TimestampType& type, ::avro::NodePtr* node); + Status Visit(const TimestampTzType& type, ::avro::NodePtr* node); + Status Visit(const StringType& type, ::avro::NodePtr* node); + Status Visit(const UuidType& type, ::avro::NodePtr* node); + Status Visit(const FixedType& type, ::avro::NodePtr* node); + Status Visit(const BinaryType& type, ::avro::NodePtr* node); + Status Visit(const StructType& type, ::avro::NodePtr* node); + Status Visit(const ListType& type, ::avro::NodePtr* node); + Status Visit(const MapType& type, ::avro::NodePtr* node); + Status Visit(const SchemaField& field, ::avro::NodePtr* node); Review Comment: Should we make this one private, since SchemaField is not a Iceberg type? -- 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: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org