-
Notifications
You must be signed in to change notification settings - Fork 75
feat: assign fresh field ids for new schema #430
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
1
commit into
apache:main
Choose a base branch
from
WZhuo:id_assigner
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.
+449
−21
Open
Changes from all commits
Commits
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
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 | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -49,7 +49,19 @@ class ICEBERG_EXPORT Schema : public StructType { | |||||||||||||||
| static constexpr int32_t kInvalidColumnId = -1; | ||||||||||||||||
|
|
||||||||||||||||
| explicit Schema(std::vector<SchemaField> fields, | ||||||||||||||||
| std::optional<int32_t> schema_id = std::nullopt); | ||||||||||||||||
| std::optional<int32_t> schema_id = std::nullopt, | ||||||||||||||||
| std::vector<int32_t> identifier_field_ids = {}); | ||||||||||||||||
|
|
||||||||||||||||
| /// \brief Create a schema. | ||||||||||||||||
| /// | ||||||||||||||||
| /// \param fields The fields that make up the schema. | ||||||||||||||||
| /// \param schema_id The unique identifier for this schema (default: kInitialSchemaId). | ||||||||||||||||
| /// \param identifier_field_names Full names of fields that uniquely identify rows in | ||||||||||||||||
| /// the table (default: empty). | ||||||||||||||||
| /// \return A new Schema instance or Status if failed. | ||||||||||||||||
| static Result<std::unique_ptr<Schema>> Make( | ||||||||||||||||
| std::vector<SchemaField> fields, int32_t schema_id = kInitialSchemaId, | ||||||||||||||||
| const std::vector<std::string>& identifier_field_names = {}); | ||||||||||||||||
|
|
||||||||||||||||
| /// \brief Get the schema ID. | ||||||||||||||||
| /// | ||||||||||||||||
|
|
@@ -78,6 +90,12 @@ class ICEBERG_EXPORT Schema : public StructType { | |||||||||||||||
| Result<std::optional<std::reference_wrapper<const SchemaField>>> FindFieldById( | ||||||||||||||||
| int32_t field_id) const; | ||||||||||||||||
|
|
||||||||||||||||
| /// \brief Returns the full column name for the given id. | ||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
| /// | ||||||||||||||||
| /// \param field_id The id of the field to get the full name for. | ||||||||||||||||
| /// \return The full name of the field with the given id, or std::nullopt if not found. | ||||||||||||||||
| Result<std::optional<std::string_view>> FindColumnNameById(int32_t field_id) const; | ||||||||||||||||
|
|
||||||||||||||||
| /// \brief Get the accessor to access the field by field id. | ||||||||||||||||
| /// | ||||||||||||||||
| /// \param field_id The id of the field to get the accessor for. | ||||||||||||||||
|
|
@@ -103,26 +121,45 @@ class ICEBERG_EXPORT Schema : public StructType { | |||||||||||||||
| Result<std::unique_ptr<Schema>> Project( | ||||||||||||||||
| const std::unordered_set<int32_t>& field_ids) const; | ||||||||||||||||
|
|
||||||||||||||||
| const std::vector<int32_t>& IdentifierFieldIds() const; | ||||||||||||||||
| Result<std::vector<std::string>> IdentifierFieldNames() const; | ||||||||||||||||
|
Comment on lines
+124
to
+125
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
|
||||||||||||||||
| friend bool operator==(const Schema& lhs, const Schema& rhs) { return lhs.Equals(rhs); } | ||||||||||||||||
|
|
||||||||||||||||
| private: | ||||||||||||||||
| /// \brief Compare two schemas for equality. | ||||||||||||||||
| bool Equals(const Schema& other) const; | ||||||||||||||||
|
|
||||||||||||||||
| struct NameIdMap { | ||||||||||||||||
| /// \brief Mapping from full field name to ID | ||||||||||||||||
| /// | ||||||||||||||||
| /// \note Short names for maps and lists are included for any name that does not | ||||||||||||||||
| /// conflict with a canonical name. For example, a list, 'l', of structs with field | ||||||||||||||||
| /// 'x' will produce short name 'l.x' in addition to canonical name 'l.element.x'. | ||||||||||||||||
| std::unordered_map<std::string, int32_t, StringHash, std::equal_to<>> name_to_id; | ||||||||||||||||
|
|
||||||||||||||||
| /// \brief Mapping from field ID to full name | ||||||||||||||||
| /// | ||||||||||||||||
| /// \note Canonical names, but not short names are set, for example | ||||||||||||||||
| /// 'list.element.field' instead of 'list.field'. | ||||||||||||||||
| std::unordered_map<int32_t, std::string> id_to_name; | ||||||||||||||||
| }; | ||||||||||||||||
|
|
||||||||||||||||
| static Result<std::unordered_map<int32_t, std::reference_wrapper<const SchemaField>>> | ||||||||||||||||
| InitIdToFieldMap(const Schema&); | ||||||||||||||||
| static Result<std::unordered_map<std::string, int32_t, StringHash, std::equal_to<>>> | ||||||||||||||||
| InitNameToIdMap(const Schema&); | ||||||||||||||||
| static Result<NameIdMap> InitNameIdMap(const Schema&); | ||||||||||||||||
| static Result<std::unordered_map<std::string, int32_t, StringHash, std::equal_to<>>> | ||||||||||||||||
| InitLowerCaseNameToIdMap(const Schema&); | ||||||||||||||||
| static Result<std::unordered_map<int32_t, std::vector<size_t>>> InitIdToPositionPath( | ||||||||||||||||
| const Schema&); | ||||||||||||||||
|
|
||||||||||||||||
| const std::optional<int32_t> schema_id_; | ||||||||||||||||
| /// Field IDs that uniquely identify rows in the table. | ||||||||||||||||
| std::vector<int32_t> identifier_field_ids_; | ||||||||||||||||
| /// Mapping from field id to field. | ||||||||||||||||
| Lazy<InitIdToFieldMap> id_to_field_; | ||||||||||||||||
| /// Mapping from field name to field id. | ||||||||||||||||
| Lazy<InitNameToIdMap> name_to_id_; | ||||||||||||||||
| Lazy<InitNameIdMap> name_id_map_; | ||||||||||||||||
| /// Mapping from lowercased field name to field id | ||||||||||||||||
| Lazy<InitLowerCaseNameToIdMap> lowercase_name_to_id_; | ||||||||||||||||
| /// Mapping from field id to (nested) position path to access the field. | ||||||||||||||||
|
|
||||||||||||||||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| /* | ||
| * 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 <memory> | ||
|
|
||
| #include <gmock/gmock.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "iceberg/schema.h" | ||
| #include "iceberg/schema_field.h" | ||
| #include "iceberg/test/matchers.h" | ||
| #include "iceberg/type.h" | ||
| #include "iceberg/util/type_util.h" | ||
|
|
||
| namespace iceberg { | ||
|
|
||
| namespace { | ||
|
|
||
| Schema CreateFlatSchema() { | ||
| return Schema({ | ||
| SchemaField::MakeRequired(/*field_id=*/10, "id", iceberg::int64()), | ||
| SchemaField::MakeOptional(/*field_id=*/20, "name", iceberg::string()), | ||
| SchemaField::MakeOptional(/*field_id=*/30, "age", iceberg::int32()), | ||
| SchemaField::MakeRequired(/*field_id=*/40, "data", iceberg::float64()), | ||
| }); | ||
| } | ||
|
|
||
| std::shared_ptr<Type> CreateListOfStruct() { | ||
| return std::make_shared<ListType>(SchemaField::MakeOptional( | ||
| /*field_id=*/101, "element", | ||
| std::make_shared<StructType>(std::vector<SchemaField>{ | ||
| SchemaField::MakeOptional(/*field_id=*/102, "x", iceberg::int32()), | ||
| SchemaField::MakeRequired(/*field_id=*/103, "y", iceberg::string()), | ||
| }))); | ||
| } | ||
|
|
||
| std::shared_ptr<Type> CreateMapWithStructValue() { | ||
| return std::make_shared<MapType>( | ||
| SchemaField::MakeRequired(/*field_id=*/201, "key", iceberg::string()), | ||
| SchemaField::MakeRequired( | ||
| /*field_id=*/202, "value", | ||
| std::make_shared<StructType>(std::vector<SchemaField>{ | ||
| SchemaField::MakeRequired(/*field_id=*/203, "id", iceberg::int64()), | ||
| SchemaField::MakeOptional(/*field_id=*/204, "name", iceberg::string()), | ||
| }))); | ||
| } | ||
|
|
||
| std::shared_ptr<Type> CreateNestedStruct() { | ||
| return std::make_shared<StructType>(std::vector<SchemaField>{ | ||
| SchemaField::MakeRequired(/*field_id=*/301, "outer_id", iceberg::int64()), | ||
| SchemaField::MakeRequired( | ||
| /*field_id=*/302, "nested", | ||
| std::make_shared<StructType>(std::vector<SchemaField>{ | ||
| SchemaField::MakeOptional(/*field_id=*/303, "inner_id", iceberg::int32()), | ||
| SchemaField::MakeRequired(/*field_id=*/304, "inner_name", | ||
| iceberg::string()), | ||
| })), | ||
| }); | ||
| } | ||
|
|
||
| Schema CreateNestedSchema(std::vector<int32_t> identifier_field_ids = {}) { | ||
| return Schema( | ||
| { | ||
| SchemaField::MakeRequired(/*field_id=*/10, "id", iceberg::int64()), | ||
| SchemaField::MakeOptional(/*field_id=*/20, "list", CreateListOfStruct()), | ||
| SchemaField::MakeOptional(/*field_id=*/30, "map", CreateMapWithStructValue()), | ||
| SchemaField::MakeRequired(/*field_id=*/40, "struct", CreateNestedStruct()), | ||
| }, | ||
| Schema::kInitialSchemaId, std::move(identifier_field_ids)); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| TEST(AssignFreshIdVisitorTest, FlatSchema) { | ||
| Schema schema = CreateFlatSchema(); | ||
|
|
||
| std::atomic<int32_t> id = 0; | ||
| auto next_id = [&id]() { return ++id; }; | ||
| ICEBERG_UNWRAP_OR_FAIL(auto fresh_schema, | ||
| AssignFreshIds(Schema::kInitialSchemaId, schema, next_id)); | ||
|
|
||
| ASSERT_EQ(fresh_schema->fields().size(), schema.fields().size()); | ||
| EXPECT_EQ(Schema( | ||
| { | ||
| SchemaField::MakeRequired(/*field_id=*/1, "id", iceberg::int64()), | ||
| SchemaField::MakeOptional(/*field_id=*/2, "name", iceberg::string()), | ||
| SchemaField::MakeOptional(/*field_id=*/3, "age", iceberg::int32()), | ||
| SchemaField::MakeRequired(/*field_id=*/4, "data", iceberg::float64()), | ||
| }, | ||
| Schema::kInitialSchemaId), | ||
| *fresh_schema); | ||
| } | ||
|
|
||
| TEST(AssignFreshIdVisitorTest, NestedSchema) { | ||
| Schema schema = CreateNestedSchema(); | ||
| std::atomic<int32_t> id = 0; | ||
| auto next_id = [&id]() { return ++id; }; | ||
| ICEBERG_UNWRAP_OR_FAIL(auto fresh_schema, | ||
| AssignFreshIds(Schema::kInitialSchemaId, schema, next_id)); | ||
|
|
||
| ASSERT_EQ(4, fresh_schema->fields().size()); | ||
| for (int32_t i = 0; i < fresh_schema->fields().size(); ++i) { | ||
| EXPECT_EQ(i + 1, fresh_schema->fields()[i].field_id()); | ||
| } | ||
|
|
||
| auto list_field = fresh_schema->fields()[1]; | ||
| auto list_type = std::dynamic_pointer_cast<ListType>(list_field.type()); | ||
| ASSERT_TRUE(list_type); | ||
| auto list_element_field = list_type->fields()[0]; | ||
| EXPECT_EQ(5, list_element_field.field_id()); | ||
| auto list_element_type = | ||
| std::dynamic_pointer_cast<StructType>(list_element_field.type()); | ||
| ASSERT_TRUE(list_element_type); | ||
| EXPECT_EQ(StructType(std::vector<SchemaField>{ | ||
| SchemaField::MakeOptional(/*field_id=*/6, "x", iceberg::int32()), | ||
| SchemaField::MakeRequired(/*field_id=*/7, "y", iceberg::string()), | ||
| }), | ||
| *list_element_type); | ||
|
|
||
| auto map_field = fresh_schema->fields()[2]; | ||
| auto map_type = std::dynamic_pointer_cast<MapType>(map_field.type()); | ||
| ASSERT_TRUE(map_type); | ||
| EXPECT_EQ(8, map_type->fields()[0].field_id()); | ||
| auto map_value_field = map_type->fields()[1]; | ||
| EXPECT_EQ(9, map_value_field.field_id()); | ||
| auto map_value_type = std::dynamic_pointer_cast<StructType>(map_value_field.type()); | ||
| ASSERT_TRUE(map_value_type); | ||
| EXPECT_EQ(StructType(std::vector<SchemaField>{ | ||
| SchemaField::MakeRequired(/*field_id=*/10, "id", iceberg::int64()), | ||
| SchemaField::MakeOptional(/*field_id=*/11, "name", iceberg::string()), | ||
| }), | ||
| *map_value_type); | ||
|
|
||
| auto struct_field = fresh_schema->fields()[3]; | ||
| auto struct_type = std::dynamic_pointer_cast<StructType>(struct_field.type()); | ||
| ASSERT_TRUE(struct_type); | ||
|
|
||
| auto expect_nested_struct_type = std::make_shared<StructType>(std::vector<SchemaField>{ | ||
| SchemaField::MakeOptional(/*field_id=*/14, "inner_id", iceberg::int32()), | ||
| SchemaField::MakeRequired(/*field_id=*/15, "inner_name", iceberg::string()), | ||
| }); | ||
| EXPECT_EQ(StructType(std::vector<SchemaField>{ | ||
| SchemaField::MakeRequired(/*field_id=*/12, "outer_id", iceberg::int64()), | ||
| SchemaField::MakeRequired( | ||
| /*field_id=*/13, "nested", expect_nested_struct_type)}), | ||
| *struct_type); | ||
|
|
||
| auto nested_struct_field = struct_type->fields()[1]; | ||
| auto nested_struct_type = | ||
| std::dynamic_pointer_cast<StructType>(nested_struct_field.type()); | ||
| ASSERT_TRUE(nested_struct_type); | ||
| EXPECT_EQ(*expect_nested_struct_type, *nested_struct_type); | ||
| } | ||
|
|
||
| TEST(AssignFreshIdVisitorTest, RefreshIdentifierId) { | ||
| std::atomic<int32_t> id = 0; | ||
| auto next_id = [&id]() { return ++id; }; | ||
|
|
||
| Schema invalid_schema = CreateNestedSchema({10, 400}); | ||
| // Invalid identified field id | ||
| auto result = AssignFreshIds(Schema::kInitialSchemaId, invalid_schema, next_id); | ||
| EXPECT_THAT(result, IsError(ErrorKind::kInvalidSchema)); | ||
| EXPECT_THAT(result, HasErrorMessage("Cannot find")); | ||
|
|
||
| id = 0; | ||
| Schema schema = CreateNestedSchema({10, 301}); | ||
| ICEBERG_UNWRAP_OR_FAIL(auto fresh_schema, | ||
| AssignFreshIds(Schema::kInitialSchemaId, schema, next_id)); | ||
| EXPECT_THAT(fresh_schema->IdentifierFieldIds(), testing::ElementsAre(1, 12)); | ||
| } | ||
|
|
||
| } // namespace iceberg |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is better to keep
std::nulloptas the default or remove the default value ofschema_id. Otherwise users may accidentally usekInitialSchemaIdas the schema_id.