Repository navigation
test: run the partition-transforms surface of iceberg-verification #1173
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
WZhuo
wants to merge
2
commits into
apache:main
Choose a base branch
from
WZhuo:feat/iceberg-verification-phase2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+231
−20
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
214 changes: 214 additions & 0 deletions
214
src/iceberg/test/verification/partition_transforms_verification_test.cc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
|
|
||
| // Runs the `table-spec/partition-transforms` surface: binding `input.transform` to | ||
| // `input.source_type` yields one of `decoded.result_types`, and applying it to each | ||
| // of `input.values` yields the value at the same index of `decoded.values`. | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <cstdint> | ||
| #include <exception> | ||
| #include <format> | ||
| #include <memory> | ||
| #include <optional> | ||
| #include <string> | ||
| #include <string_view> | ||
| #include <vector> | ||
|
|
||
| #include <gtest/gtest.h> | ||
| #include <nlohmann/json.hpp> | ||
|
|
||
| #include "iceberg/expression/json_serde_internal.h" | ||
| #include "iceberg/expression/literal.h" | ||
| #include "iceberg/json_serde_internal.h" | ||
| #include "iceberg/result.h" | ||
| #include "iceberg/test/verification/verification_util.h" | ||
| #include "iceberg/transform.h" | ||
| #include "iceberg/type.h" | ||
| #include "iceberg/util/string_util.h" | ||
|
|
||
| namespace iceberg::verification { | ||
|
|
||
| namespace { | ||
|
|
||
| constexpr std::array<std::string_view, 8> kSubdirs = { | ||
| "identity", "bucket", "truncate", "year", "month", "day", "hour", "void"}; | ||
|
|
||
| constexpr std::string_view kTransform = "transform"; | ||
| constexpr std::string_view kSourceType = "source_type"; | ||
| constexpr std::string_view kValues = "values"; | ||
| constexpr std::string_view kResultTypes = "result_types"; | ||
|
|
||
| /// \brief Decode a language-neutral value of this surface into a literal of `type`. | ||
| Result<Literal> DecodeValue(const nlohmann::json& json, | ||
| const std::shared_ptr<PrimitiveType>& type) { | ||
| if (json.is_null()) { | ||
| return Literal::Null(type); | ||
| } | ||
| switch (type->type_id()) { | ||
| case TypeId::kLong: { | ||
| // partition-transforms/README.md: "`long` values are decimal strings so JSON | ||
| // number handling cannot lose 64-bit precision." | ||
| if (!json.is_string()) { | ||
| return JsonParseError("long value {} is not a decimal string", json.dump()); | ||
| } | ||
| ICEBERG_ASSIGN_OR_RAISE(auto value, | ||
| StringUtils::ParseNumber<int64_t>(json.get<std::string>())); | ||
| return Literal::Long(value); | ||
| } | ||
| default: | ||
| return LiteralFromJson(json, type.get()); | ||
| } | ||
| } | ||
|
|
||
| std::string Describe(const Literal& literal) { | ||
| if (literal.IsNull()) { | ||
| return "null"; | ||
| } | ||
| auto json = ToJson(literal); | ||
| return json.has_value() ? json->dump() : literal.ToString(); | ||
| } | ||
|
|
||
| /// \brief Give a `day` result its declared `date` type. | ||
| Literal NormalizeResult(const Literal& literal, | ||
| const std::shared_ptr<PrimitiveType>& result_type) { | ||
| if (result_type->type_id() != TypeId::kDate || | ||
| literal.type()->type_id() != TypeId::kInt) { | ||
| return literal; | ||
| } | ||
| if (literal.IsNull()) { | ||
| return Literal::Null(result_type); | ||
| } | ||
| return Literal::Date(std::get<int32_t>(literal.value())); | ||
| } | ||
|
|
||
| Result<std::shared_ptr<PrimitiveType>> ParsePrimitiveType(const nlohmann::json& json) { | ||
| ICEBERG_ASSIGN_OR_RAISE(std::shared_ptr<Type> type, TypeFromJson(json)); | ||
| if (!type->is_primitive()) { | ||
| return InvalidArgument("{} is not a primitive type", json.dump()); | ||
| } | ||
| return std::static_pointer_cast<PrimitiveType>(type); | ||
| } | ||
|
|
||
| /// \brief Apply `transform` to `input` and compare the result with `expected`. | ||
| std::optional<std::string> CheckValue(TransformFunction& transform, | ||
| const std::shared_ptr<PrimitiveType>& source_type, | ||
| const std::shared_ptr<PrimitiveType>& result_type, | ||
| const nlohmann::json& input, | ||
| const nlohmann::json& expected_json) { | ||
| auto value = DecodeValue(input, source_type); | ||
| if (!value.has_value()) { | ||
| return std::format("cannot decode input {}: {}", input.dump(), value.error().message); | ||
| } | ||
| Result<Literal> transformed = [&]() -> Result<Literal> { | ||
| try { | ||
| return transform.Transform(*value); | ||
| } catch (const std::exception& e) { | ||
| return Invalid("transform threw: {}", e.what()); | ||
| } | ||
| }(); | ||
| if (!transformed.has_value()) { | ||
| return std::format("transform failed: {}", transformed.error().message); | ||
| } | ||
| Literal actual = NormalizeResult(*transformed, result_type); | ||
| if (expected_json.is_null()) { | ||
| if (!actual.IsNull()) { | ||
| return std::format("expected null, got {}", Describe(actual)); | ||
| } | ||
| return std::nullopt; | ||
| } | ||
| auto expected = DecodeValue(expected_json, result_type); | ||
| if (!expected.has_value()) { | ||
| return std::format("cannot decode expected {} as {}: {}", expected_json.dump(), | ||
| result_type->ToString(), expected.error().message); | ||
| } | ||
| if (actual.IsNull() || *actual.type() != *expected->type() || actual != *expected) { | ||
| return std::format("expected {} of type {}, got {} of type {}", expected_json.dump(), | ||
| expected->type()->ToString(), Describe(actual), | ||
| actual.type()->ToString()); | ||
| } | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| std::vector<std::string> CheckCase(const nlohmann::json& c) { | ||
| const auto& input = c.at(kInput); | ||
| const auto& decoded = c.at(kDecoded); | ||
|
|
||
| auto source_type = ParsePrimitiveType(input.at(kSourceType)); | ||
| if (!source_type.has_value()) { | ||
| return {std::format("source type: {}", source_type.error().message)}; | ||
| } | ||
| auto transform = TransformFromString(input.at(kTransform).get<std::string>()); | ||
| if (!transform.has_value()) { | ||
| return {std::format("transform: {}", transform.error().message)}; | ||
| } | ||
| auto bound = (*transform)->Bind(*source_type); | ||
| if (!bound.has_value()) { | ||
| return {std::format("bind failed: {}", bound.error().message)}; | ||
| } | ||
|
|
||
| std::vector<std::string> diffs; | ||
| auto result_type = (*bound)->ResultType(); | ||
| const auto& allowed = decoded.at(kResultTypes); | ||
| bool result_type_allowed = std::ranges::any_of(allowed, [&](const auto& json) { | ||
| auto type = TypeFromJson(json); | ||
| return type.has_value() && **type == *result_type; | ||
| }); | ||
| if (!result_type_allowed) { | ||
| diffs.push_back(std::format("result_type: expected one of {}, got {}", allowed.dump(), | ||
| result_type->ToString())); | ||
| } | ||
| if (!result_type->is_primitive()) { | ||
| diffs.push_back( | ||
| std::format("result_type: {} is not primitive", result_type->ToString())); | ||
| return diffs; | ||
| } | ||
| auto result_primitive = std::static_pointer_cast<PrimitiveType>(result_type); | ||
|
|
||
| const auto& inputs = input.at(kValues); | ||
| const auto& expected_values = decoded.at(kValues); | ||
| if (inputs.size() != expected_values.size()) { | ||
| diffs.push_back(std::format("values: {} inputs but {} expected values", inputs.size(), | ||
| expected_values.size())); | ||
| return diffs; | ||
| } | ||
| for (size_t i = 0; i < inputs.size(); ++i) { | ||
|
WZhuo marked this conversation as resolved.
Resolved
|
||
| auto diff = CheckValue(**bound, *source_type, result_primitive, inputs[i], | ||
| expected_values[i]); | ||
| if (diff.has_value()) { | ||
| diffs.push_back(std::format("values[{}]: {}", i, *diff)); | ||
| } | ||
| } | ||
| return diffs; | ||
| } | ||
|
|
||
| class PartitionTransformsVerificationTest | ||
| : public ::testing::TestWithParam<VerificationCase> {}; | ||
|
|
||
| TEST_P(PartitionTransformsVerificationTest, Apply) { RunCase(GetParam(), CheckCase); } | ||
|
|
||
| INSTANTIATE_TEST_SUITE_P(PartitionTransforms, PartitionTransformsVerificationTest, | ||
| ::testing::ValuesIn(LoadCases("partition-transforms", kSubdirs)), | ||
| CaseName); | ||
| GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PartitionTransformsVerificationTest); | ||
|
|
||
| } // namespace | ||
|
|
||
| } // namespace iceberg::verification | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.