gty404 commented on code in PR #54: URL: https://github.com/apache/iceberg-cpp/pull/54#discussion_r2020974086
########## src/iceberg/transform.h: ########## @@ -0,0 +1,81 @@ +/* + * 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 + +/// \file iceberg/transform.h + +#include <cstdint> +#include <memory> + +#include "iceberg/arrow_c_data_internal.h" +#include "iceberg/error.h" +#include "iceberg/expected.h" +#include "iceberg/iceberg_export.h" +#include "iceberg/type_fwd.h" +#include "iceberg/util/formattable.h" + +namespace iceberg { + +/// \brief Transform types used for partitioning +enum class TransformType : uint8_t { + /// Equal to source value, unmodified + kIdentity, + /// Hash of value, mod `N` + kBucket, + /// Value truncated to width `W` + kTruncate, + /// Extract a date or timestamp year, as years from 1970 + kYear, + /// Extract a date or timestamp month, as months from 1970-01-01 + kMonth, + /// Extract a date or timestamp day, as days from 1970-01-01 + kDay, + /// Extract a timestamp hour, as hours from 1970-01-01 00:00:00 + kHour, + /// Always produces `null` + kVoid, + /// Used to represent some customized transform that can't be recognized or supported now. + kUnknown, +}; + +/// \brief Get the relative transform name +constexpr std::string_view ToString(TransformType type) { + /// Transform names used for formatting + constexpr std::string_view TransformTypeNames[] = { + "Identity", "Bucket", "Truncate", "Year", "Month", "Day", "Hour", "Void", "Unknown" + }; + return TransformTypeNames[static_cast<size_t>(type)]; Review Comment: As long as the order of TransformTypes is not modified, there is no undefined behavior ########## test/transform_test.cc: ########## @@ -0,0 +1,66 @@ +/* + * 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 "iceberg/transform.h" + +#include <format> +#include <memory> + +#include <gmock/gmock.h> +#include <gtest/gtest.h> + +#include "iceberg/util/formatter.h" + +TEST(TransformTest, TransformType) { + { + EXPECT_EQ("Identity", iceberg::ToString(iceberg::TransformType::kIdentity)); + EXPECT_EQ("Bucket", iceberg::ToString(iceberg::TransformType::kBucket)); + EXPECT_EQ("Truncate", iceberg::ToString(iceberg::TransformType::kTruncate)); + EXPECT_EQ("Year", iceberg::ToString(iceberg::TransformType::kYear)); + EXPECT_EQ("Month", iceberg::ToString(iceberg::TransformType::kMonth)); + EXPECT_EQ("Day", iceberg::ToString(iceberg::TransformType::kDay)); + EXPECT_EQ("Hour", iceberg::ToString(iceberg::TransformType::kHour)); + EXPECT_EQ("Void", iceberg::ToString(iceberg::TransformType::kVoid)); + EXPECT_EQ("Unknown", iceberg::ToString(iceberg::TransformType::kUnknown)); + } +} + +TEST(TransformTest, TransformFunction) { + class TestTransformFunction : public iceberg::TransformFunction { + public: + TestTransformFunction() : TransformFunction(iceberg::TransformType::kUnknown) {} + iceberg::expected<std::unique_ptr<ArrowArray>, iceberg::Error> Transform( + const ArrowArray& input) override { + return iceberg::unexpected(iceberg::Error{.kind = iceberg::ErrorKind::kNotSupported, + .message = "test transform function"}); + } + }; + + TestTransformFunction transform; + EXPECT_EQ(iceberg::TransformType::kUnknown, transform.transform_type()); + EXPECT_EQ("Unknown", transform.ToString()); + EXPECT_EQ("Unknown", std::format("{}", transform)); + + auto [_, arrow_array] = + iceberg::internal::CreateExampleArrowSchemaAndArrayByNanoarrow(); Review Comment: Okay, I'll build a simple arrow array to test -- 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