diff --git a/src/iceberg/parquet/parquet_schema_util.cc b/src/iceberg/parquet/parquet_schema_util.cc index fa76f49c3..ac3388626 100644 --- a/src/iceberg/parquet/parquet_schema_util.cc +++ b/src/iceberg/parquet/parquet_schema_util.cc @@ -293,6 +293,43 @@ void SelectAnchorColumnIfEmpty( } } +// Lists and maps are matched to the file by the field IDs of their element, key and +// value. Returns false when the Parquet field leaves any of them without an ID, for +// example when a partial name mapping does not cover them. Like Java's PruneColumns, +// the caller then reads the container as if it were missing from the file. A Parquet +// field of the wrong shape returns true so that projection reports the mismatch. +bool HasNestedFieldIds(const Type& expected_type, + const ::parquet::arrow::SchemaField& parquet_field) { + const auto arrow_type_id = parquet_field.field->type()->id(); + switch (expected_type.type_id()) { + case TypeId::kList: { + if ((arrow_type_id != ::arrow::Type::LIST && + arrow_type_id != ::arrow::Type::LARGE_LIST) || + parquet_field.children.size() != 1) { + return true; + } + const auto& element = parquet_field.children[0]; + const auto& list_type = internal::checked_cast(expected_type); + return GetFieldId(element).has_value() && + HasNestedFieldIds(*list_type.element().type(), element); + } + case TypeId::kMap: { + if (arrow_type_id != ::arrow::Type::MAP || parquet_field.children.size() != 1 || + parquet_field.children[0].children.size() != 2) { + return true; + } + const auto& key = parquet_field.children[0].children[0]; + const auto& value = parquet_field.children[0].children[1]; + const auto& map_type = internal::checked_cast(expected_type); + return GetFieldId(key).has_value() && GetFieldId(value).has_value() && + HasNestedFieldIds(*map_type.key().type(), key) && + HasNestedFieldIds(*map_type.value().type(), value); + } + default: + return true; + } +} + template Status ValidateGeospatialParameters(const IcebergType& expected, const ParquetType& actual, @@ -560,6 +597,23 @@ class ProjectionBuilder { return projection; } + // Project a field that cannot be read from the file. + static Result ProjectMissingField(const SchemaField& field) { + FieldProjection projection; + if (MetadataColumns::IsMetadataColumn(field.field_id())) { + projection.kind = FieldProjection::Kind::kMetadata; + } else if (field.initial_default() != nullptr) { + // Rows written before the field existed assume its `initial-default` value. + projection.kind = FieldProjection::Kind::kDefault; + projection.from = *field.initial_default(); + } else if (field.optional()) { + projection.kind = FieldProjection::Kind::kNull; + } else { + return InvalidSchema("Missing required field with id: {}", field.field_id()); + } + return projection; + } + Result ProjectStruct( const StructType& struct_type, const std::vector<::parquet::arrow::SchemaField>& parquet_fields) { @@ -593,28 +647,20 @@ class ProjectionBuilder { result.children.reserve(struct_type.fields().size()); for (const auto& field : struct_type.fields()) { - int32_t field_id = field.field_id(); FieldProjection child_projection; - if (auto iter = field_context_map.find(field_id); - iter != field_context_map.cend()) { + if (auto iter = field_context_map.find(field.field_id()); + iter != field_context_map.cend() && + HasNestedFieldIds(*field.type(), iter->second.parquet_field)) { const auto& parquet_field = iter->second.parquet_field; ICEBERG_ASSIGN_OR_RAISE(child_projection, ProjectField(field, parquet_field, iter->second.local_index)); - } else if (auto constant = id_to_constant_.find(field_id); + } else if (auto constant = id_to_constant_.find(field.field_id()); constant != id_to_constant_.end()) { child_projection.kind = FieldProjection::Kind::kConstant; child_projection.from = constant->second; - } else if (MetadataColumns::IsMetadataColumn(field_id)) { - child_projection.kind = FieldProjection::Kind::kMetadata; - } else if (field.initial_default() != nullptr) { - // Rows written before the field existed assume its `initial-default` value. - child_projection.kind = FieldProjection::Kind::kDefault; - child_projection.from = *field.initial_default(); - } else if (field.optional()) { - child_projection.kind = FieldProjection::Kind::kNull; } else { - return InvalidSchema("Missing required field with id: {}", field_id); + ICEBERG_ASSIGN_OR_RAISE(child_projection, ProjectMissingField(field)); } result.children.emplace_back(std::move(child_projection)); @@ -750,51 +796,89 @@ void CollectColumnIds(const FieldProjection& field_projection, } } -// Forward declaration -void ApplyNameMappingToField(const NameMapping& mapping, std::vector& names, - ::parquet::arrow::SchemaField* parquet_field); +// Assigns field IDs from a name mapping to a Parquet schema without field IDs, like +// Java's ApplyNameMapping. List and map children are looked up as "element", "key" and +// "value", whatever their physical names in the file. +class NameMappingApplier { + public: + explicit NameMappingApplier(const NameMapping& mapping) : mapping_(mapping) {} -void ApplyNameMappingToChild(const NameMapping& mapping, std::vector& names, - std::string name, ::parquet::arrow::SchemaField* child) { - names.push_back(std::move(name)); - ApplyNameMappingToField(mapping, names, child); - names.pop_back(); -} + void ApplyToStruct(std::vector<::parquet::arrow::SchemaField>& fields) { + struct MappedSibling { + size_t index; + std::shared_ptr<::arrow::Field> unmapped_field; + }; + std::unordered_map mapped_siblings; + for (size_t i = 0; i < fields.size(); ++i) { + auto unmapped_field = fields[i].field; + auto field_id = ApplyToChild(unmapped_field->name(), &fields[i]); + if (field_id.has_value()) { + auto [iter, inserted] = mapped_siblings.try_emplace( + field_id.value(), + MappedSibling{.index = i, .unmapped_field = unmapped_field}); + if (!inserted) { + // Aliases can map two sibling columns to the same ID. Java's reader keeps the + // last one, so drop the ID from the earlier column. + fields[iter->second.index].field = std::move(iter->second.unmapped_field); + iter->second = MappedSibling{.index = i, .unmapped_field = unmapped_field}; + } + } + } + } -void ApplyNameMappingToField(const NameMapping& mapping, std::vector& names, - ::parquet::arrow::SchemaField* parquet_field) { - if (auto mapped_field = mapping.Find(names); - mapped_field.has_value() && mapped_field->get().field_id.has_value()) { - parquet_field->field = - parquet_field->field->WithMergedMetadata(::arrow::key_value_metadata( - {std::string(kParquetFieldIdKey)}, - {std::to_string(mapped_field->get().field_id.value())})); + private: + // Returns the field ID assigned to `field`, if any. + std::optional ApplyToChild(std::string_view name, + ::parquet::arrow::SchemaField* field) { + const size_t parent_size = path_.size(); + if (!path_.empty()) { + path_.push_back('.'); + } + path_.append(name); + auto field_id = Apply(field); + path_.resize(parent_size); + return field_id; } - auto& children = parquet_field->children; - switch (parquet_field->field->type()->id()) { - case ::arrow::Type::STRUCT: - for (auto& child : children) { - ApplyNameMappingToChild(mapping, names, child.field->name(), &child); - } - break; - case ::arrow::Type::LIST: - case ::arrow::Type::LARGE_LIST: - if (children.size() == 1) { - ApplyNameMappingToChild(mapping, names, "element", &children[0]); - } - break; - case ::arrow::Type::MAP: - // The key and value sit under the entries struct, which the name path skips. - if (children.size() == 1 && children[0].children.size() == 2) { - ApplyNameMappingToChild(mapping, names, "key", &children[0].children[0]); - ApplyNameMappingToChild(mapping, names, "value", &children[0].children[1]); - } - break; - default: - break; + std::optional Apply(::parquet::arrow::SchemaField* parquet_field) { + std::optional field_id; + if (auto mapped_field = mapping_.Find(path_); mapped_field.has_value()) { + field_id = mapped_field->get().field_id; + } + if (field_id.has_value()) { + parquet_field->field = + parquet_field->field->WithMergedMetadata(::arrow::key_value_metadata( + {std::string(kParquetFieldIdKey)}, {std::to_string(field_id.value())})); + } + + auto& children = parquet_field->children; + switch (parquet_field->field->type()->id()) { + case ::arrow::Type::STRUCT: + ApplyToStruct(children); + break; + case ::arrow::Type::LIST: + case ::arrow::Type::LARGE_LIST: + if (children.size() == 1) { + ApplyToChild(ListType::kElementName, &children[0]); + } + break; + case ::arrow::Type::MAP: + // The key and value sit under the entries struct, which the name path skips. + if (children.size() == 1 && children[0].children.size() == 2) { + ApplyToChild(MapType::kKeyName, &children[0].children[0]); + ApplyToChild(MapType::kValueName, &children[0].children[1]); + } + break; + default: + break; + } + return field_id; } -} + + const NameMapping& mapping_; + // Dot-joined names of the field being visited, the key used by NameMapping::Find. + std::string path_; +}; } // namespace @@ -837,10 +921,7 @@ bool HasFieldIds(const ::parquet::schema::NodePtr& node) { void ApplyNameMapping(const NameMapping& mapping, ::parquet::arrow::SchemaManifest* parquet_schema) { - std::vector names; - for (auto& parquet_field : parquet_schema->schema_fields) { - ApplyNameMappingToChild(mapping, names, parquet_field.field->name(), &parquet_field); - } + NameMappingApplier(mapping).ApplyToStruct(parquet_schema->schema_fields); } Result> ToParquetSchema( diff --git a/src/iceberg/parquet/parquet_schema_util_internal.h b/src/iceberg/parquet/parquet_schema_util_internal.h index be209ed36..933a460bc 100644 --- a/src/iceberg/parquet/parquet_schema_util_internal.h +++ b/src/iceberg/parquet/parquet_schema_util_internal.h @@ -80,7 +80,8 @@ bool HasFieldIds(const ::parquet::schema::NodePtr& root_node); /// \brief Assign field IDs from a name mapping to a Parquet schema without field IDs. /// /// List elements use "element"; map keys and values use "key" and "value". -/// Fields without a mapped ID remain unassigned. +/// Fields without a mapped ID remain unassigned. When aliases map several sibling +/// fields to the same ID, only the last of them keeps it, matching Java's reader. /// /// \param mapping The name mapping to apply. /// \param parquet_schema The Parquet schema to update in place. diff --git a/src/iceberg/test/parquet_schema_test.cc b/src/iceberg/test/parquet_schema_test.cc index 52709aa66..266725e53 100644 --- a/src/iceberg/test/parquet_schema_test.cc +++ b/src/iceberg/test/parquet_schema_test.cc @@ -356,6 +356,106 @@ TEST(ParquetNameMappingTest, ApplyNameMappingWithUnmappedFields) { ASSERT_EQ(SelectedColumnIndices(projection), std::vector({0})); } +TEST(ParquetNameMappingTest, ApplyNameMappingKeepsLastAliasedSibling) { + Schema expected_schema({ + SchemaField::MakeOptional(/*field_id=*/2, "full_name", string()), + }); + + auto parquet_schema = MakeGroupNode( + "iceberg_schema", {MakeStringNode("name"), MakeStringNode("full_name")}); + auto schema_manifest = MakeSchemaManifest(parquet_schema); + auto mapping = NameMapping::Make({ + MappedField{.names = {"name", "full_name"}, .field_id = 2}, + }); + + ApplyNameMapping(*mapping, &schema_manifest); + const auto& first_metadata = schema_manifest.schema_fields[0].field->metadata(); + EXPECT_TRUE(first_metadata == nullptr || + first_metadata->FindKey(std::string(kParquetFieldIdKey)) < 0); + EXPECT_EQ(schema_manifest.schema_fields[1].field->metadata()->Get( + std::string(kParquetFieldIdKey)), + "2"); + + auto projection_result = Project(expected_schema, schema_manifest); + ASSERT_THAT(projection_result, IsOk()); + + // `from` indexes the selected columns, which only hold the "full_name" column. + const auto& projection = *projection_result; + ASSERT_EQ(projection.fields.size(), 1); + ASSERT_PROJECTED_FIELD(projection.fields[0], 0); + ASSERT_EQ(SelectedColumnIndices(projection), std::vector({1})); +} + +TEST(ParquetNameMappingTest, ProjectContainersWithUnmappedChildrenAsMissing) { + Schema expected_schema({ + SchemaField::MakeOptional(/*field_id=*/1, "id", int64()), + SchemaField::MakeOptional( + /*field_id=*/2, "numbers", + std::make_shared(SchemaField::MakeOptional( + /*field_id=*/201, "element", int32()))), + SchemaField::MakeOptional( + /*field_id=*/3, "counts", + std::make_shared( + SchemaField::MakeRequired(/*field_id=*/301, "key", string()), + SchemaField::MakeOptional(/*field_id=*/302, "value", int32()))), + SchemaField::MakeOptional( + /*field_id=*/4, "nested", + std::make_shared(SchemaField::MakeOptional( + /*field_id=*/401, "element", + std::make_shared(SchemaField::MakeOptional( + /*field_id=*/402, "element", int32()))))), + }); + + auto parquet_schema = MakeGroupNode( + "iceberg_schema", + { + MakeInt64Node("id"), + MakeListNode("numbers", MakeInt32Node("element")), + MakeMapNode("counts", + MakeStringNode("key", /*field_id=*/-1, /*optional=*/false), + MakeInt32Node("value")), + MakeListNode("nested", MakeListNode("element", MakeInt32Node("element"))), + }); + // Each container is mapped, but some of its children are not. + auto mapping = NameMapping::Make({ + MappedField{.names = {"id"}, .field_id = 1}, + MappedField{.names = {"numbers"}, .field_id = 2}, + MappedField{.names = {"counts"}, + .field_id = 3, + .nested_mapping = MappedFields::Make({ + MappedField{.names = {"value"}, .field_id = 302}, + })}, + MappedField{.names = {"nested"}, + .field_id = 4, + .nested_mapping = MappedFields::Make({ + MappedField{.names = {"element"}, .field_id = 401}, + })}, + }); + + auto schema_manifest = MakeSchemaManifest(parquet_schema); + ApplyNameMapping(*mapping, &schema_manifest); + auto projection_result = Project(expected_schema, schema_manifest); + ASSERT_THAT(projection_result, IsOk()); + + const auto& projection = *projection_result; + ASSERT_EQ(projection.fields.size(), 4); + ASSERT_PROJECTED_FIELD(projection.fields[0], 0); + ASSERT_PROJECTED_NULL_FIELD(projection.fields[1]); + ASSERT_PROJECTED_NULL_FIELD(projection.fields[2]); + ASSERT_PROJECTED_NULL_FIELD(projection.fields[3]); + ASSERT_EQ(SelectedColumnIndices(projection), std::vector({0})); + + // A required container that cannot be read is still an error. + Schema required_schema({ + SchemaField::MakeRequired( + /*field_id=*/2, "numbers", + std::make_shared(SchemaField::MakeOptional( + /*field_id=*/201, "element", int32()))), + }); + EXPECT_THAT(Project(required_schema, schema_manifest), + HasErrorMessage("Missing required field with id: 2")); +} + TEST(ParquetSchemaProjectionTest, ProjectIdenticalSchemas) { Schema expected_schema({ SchemaField::MakeRequired(/*field_id=*/1, "id", iceberg::int64()), diff --git a/src/iceberg/test/parquet_test.cc b/src/iceberg/test/parquet_test.cc index cb59b9099..5d1133537 100644 --- a/src/iceberg/test/parquet_test.cc +++ b/src/iceberg/test/parquet_test.cc @@ -276,12 +276,29 @@ class ParquetReaderTest : public TempFileTestBase { auto io = internal::checked_cast(*file_io_); auto outfile = io.fs()->OpenOutputStream(temp_parquet_file_).ValueOrDie(); - ASSERT_TRUE(::parquet::arrow::WriteTable(*table, ::arrow::default_memory_pool(), - outfile, table->num_rows()) + // Keep the Arrow names of list elements (such as "item") instead of the standard + // "element", so that reads exercise the name mapping's path normalization. + auto arrow_writer_props = ::parquet::ArrowWriterProperties::Builder() + .disable_compliant_nested_types() + ->build(); + ASSERT_TRUE(::parquet::arrow::WriteTable( + *table, ::arrow::default_memory_pool(), outfile, table->num_rows(), + ::parquet::default_writer_properties(), arrow_writer_props) .ok()); ASSERT_TRUE(outfile->Close().ok()); } + std::vector ParquetColumnPaths() { + auto& io = internal::checked_cast(*file_io_); + auto input_file = io.fs()->OpenInputFile(temp_parquet_file_).ValueOrDie(); + auto metadata = ::parquet::ReadMetaData(input_file); + std::vector paths; + for (int i = 0; i < metadata->schema()->num_columns(); ++i) { + paths.push_back(metadata->schema()->Column(i)->path()->ToDotString()); + } + return paths; + } + void CreateListParquetFile() { auto schema = std::make_shared(std::vector{ SchemaField::MakeRequired(1, "id", int32()), @@ -632,6 +649,7 @@ TEST_F(ParquetReaderTest, ReadNestedFieldsWithNameMapping) { [[null], null, null] ])"; ASSERT_NO_FATAL_FAILURE(CreateParquetFileWithoutFieldIds(type, json)); + EXPECT_THAT(ParquetColumnPaths(), ::testing::Contains("numbers.list.item.value")); auto schema = std::make_shared(std::vector{ SchemaField::MakeOptional(1, "profile", @@ -673,6 +691,69 @@ TEST_F(ParquetReaderTest, ReadNestedFieldsWithNameMapping) { } } +TEST_F(ParquetReaderTest, ReadListWithUnmappedElementAsMissing) { + ASSERT_NO_FATAL_FAILURE(CreateParquetFileWithoutFieldIds( + ::arrow::struct_({ + ::arrow::field("id", ::arrow::int32()), + ::arrow::field("tags", ::arrow::list(::arrow::utf8())), + ::arrow::field("counts", ::arrow::map(::arrow::utf8(), ::arrow::int32())), + }), + R"([[1, ["a"], [["x", 1]]], [2, null, null]])")); + auto schema = std::make_shared(std::vector{ + SchemaField::MakeOptional(1, "id", int32()), + SchemaField::MakeOptional(2, "tags", + std::make_shared( + SchemaField::MakeOptional(201, "element", string()))), + SchemaField::MakeOptional( + 3, "counts", + std::make_shared(SchemaField::MakeRequired(301, "key", string()), + SchemaField::MakeOptional(302, "value", int32()))), + }); + // The mapping covers the list and the map but not their children, so they are read + // as if they were missing from the file. + auto mapping = NameMapping::Make({ + MappedField{.names = {"id"}, .field_id = 1}, + MappedField{.names = {"tags"}, .field_id = 2}, + MappedField{.names = {"counts"}, + .field_id = 3, + .nested_mapping = MappedFields::Make({ + MappedField{.names = {"key"}, .field_id = 301}, + })}, + }); + + ICEBERG_UNWRAP_OR_FAIL( + auto reader, ReaderFactoryRegistry::Open(FileFormatType::kParquet, + {.path = temp_parquet_file_, + .io = file_io_, + .projection = schema, + .name_mapping = std::move(mapping)})); + ASSERT_NO_FATAL_FAILURE( + VerifyNextBatch(*reader, R"([[1, null, null], [2, null, null]])")); + ASSERT_NO_FATAL_FAILURE(VerifyExhausted(*reader)); +} + +TEST_F(ParquetReaderTest, ReadWithAliasMappedToTwoColumns) { + ASSERT_NO_FATAL_FAILURE(CreateParquetFileWithoutFieldIds( + ::arrow::struct_({::arrow::field("name", ::arrow::utf8()), + ::arrow::field("full_name", ::arrow::utf8())}), + R"([["a", "Alice"], ["b", "Bob"]])")); + auto schema = std::make_shared( + std::vector{SchemaField::MakeOptional(2, "full_name", string())}); + auto mapping = NameMapping::Make({ + MappedField{.names = {"name", "full_name"}, .field_id = 2}, + }); + + // Like Java, the last column mapped to the ID is read. + ICEBERG_UNWRAP_OR_FAIL( + auto reader, ReaderFactoryRegistry::Open(FileFormatType::kParquet, + {.path = temp_parquet_file_, + .io = file_io_, + .projection = schema, + .name_mapping = std::move(mapping)})); + ASSERT_NO_FATAL_FAILURE(VerifyNextBatch(*reader, R"([["Alice"], ["Bob"]])")); + ASSERT_NO_FATAL_FAILURE(VerifyExhausted(*reader)); +} + TEST_F(ParquetReaderTest, ReadMissingFieldsWithDefaults) { // The file contains only fields 1 and 2; the projected schema adds fields 3 and 4 // with initial-defaults, which are filled for all rows written before the columns