Skip to content

Commit 1fbe5bc

Browse files
committed
Reuse parquet_path_to_id_mapping
1 parent efd56e8 commit 1fbe5bc

2 files changed

Lines changed: 14 additions & 22 deletions

File tree

pyiceberg/io/pyarrow.py

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,19 +2479,6 @@ def parquet_path_to_id_mapping(
24792479
return result
24802480

24812481

2482-
def id_to_parquet_path_mapping(schema: Schema) -> dict[int, str]:
2483-
"""
2484-
Compute the mapping of Iceberg column ID to parquet column path.
2485-
2486-
Args:
2487-
schema (pyiceberg.schema.Schema): The current table schema.
2488-
"""
2489-
result: dict[int, str] = {}
2490-
for pair in pre_order_visit(schema, ID2ParquetPathVisitor()):
2491-
result[pair.field_id] = pair.parquet_path
2492-
return result
2493-
2494-
24952482
@dataclass(frozen=True)
24962483
class BloomFilterOptions:
24972484
parquet_path: str
@@ -2573,17 +2560,17 @@ def primitive(self, primitive: PrimitiveType) -> builtins.list[BloomFilterOption
25732560
def get_bloom_filter_options(
25742561
schema: Schema,
25752562
table_properties: dict[str, str],
2563+
id_to_parquet_path: dict[int, str],
25762564
) -> dict[str, dict[str, Any]]:
25772565
"""
25782566
Get the bloom filter options from the table properties.
25792567
25802568
Args:
25812569
schema (pyiceberg.schema.Schema): The current table schema.
25822570
table_properties (dict[str, str]): The table properties.
2571+
id_to_parquet_path (Dict[int, str]): The mapping of the field ID to the parquet file name.
25832572
"""
2584-
bloom_filter_options = pre_order_visit(
2585-
schema, BloomFilterOptionsCollector(schema, table_properties, id_to_parquet_path_mapping(schema))
2586-
)
2573+
bloom_filter_options = pre_order_visit(schema, BloomFilterOptionsCollector(schema, table_properties, id_to_parquet_path))
25872574
result: dict[str, dict[str, Any]] = {}
25882575
for bf_opts in bloom_filter_options:
25892576
result[bf_opts.parquet_path] = {
@@ -2731,7 +2718,8 @@ def write_parquet(task: WriteTask) -> DataFile:
27312718
else:
27322719
file_schema = table_schema
27332720

2734-
parquet_writer_kwargs = _get_parquet_writer_kwargs(table_metadata.properties, file_schema)
2721+
parquet_column_mapping = parquet_path_to_id_mapping(file_schema)
2722+
parquet_writer_kwargs = _get_parquet_writer_kwargs(table_metadata.properties, file_schema, parquet_column_mapping)
27352723

27362724
downcast_ns_timestamp_to_us = Config().get_bool(DOWNCAST_NS_TIMESTAMP_TO_US_ON_WRITE) or False
27372725
batches = [
@@ -2758,7 +2746,7 @@ def write_parquet(task: WriteTask) -> DataFile:
27582746
statistics = data_file_statistics_from_parquet_metadata(
27592747
parquet_metadata=writer.writer.metadata,
27602748
stats_columns=compute_statistics_plan(file_schema, table_metadata.properties),
2761-
parquet_column_mapping=parquet_path_to_id_mapping(file_schema),
2749+
parquet_column_mapping=parquet_column_mapping,
27622750
)
27632751
data_file = DataFile.from_args(
27642752
content=DataFileContent.DATA,
@@ -2877,7 +2865,9 @@ def parquet_file_to_data_file(io: FileIO, table_metadata: TableMetadata, file_pa
28772865
PYARROW_UNCOMPRESSED_CODEC = "none"
28782866

28792867

2880-
def _get_parquet_writer_kwargs(table_properties: Properties, file_schema: Schema) -> dict[str, Any]:
2868+
def _get_parquet_writer_kwargs(
2869+
table_properties: Properties, file_schema: Schema, parquet_column_mapping: dict[str, int]
2870+
) -> dict[str, Any]:
28812871
from pyiceberg.table import TableProperties
28822872

28832873
unsupported_key_patterns = [
@@ -2908,7 +2898,8 @@ def _get_parquet_writer_kwargs(table_properties: Properties, file_schema: Schema
29082898
if compression_codec == ICEBERG_UNCOMPRESSED_CODEC:
29092899
compression_codec = PYARROW_UNCOMPRESSED_CODEC
29102900

2911-
bloom_filter_options = get_bloom_filter_options(file_schema, table_properties)
2901+
id_to_parquet_path = {field_id: parquet_path for parquet_path, field_id in parquet_column_mapping.items()}
2902+
bloom_filter_options = get_bloom_filter_options(file_schema, table_properties, id_to_parquet_path)
29122903

29132904
return {
29142905
"compression": compression_codec,

tests/table/test_metadata.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import pytest
2727

2828
from pyiceberg.exceptions import ValidationError
29-
from pyiceberg.io.pyarrow import get_bloom_filter_options
29+
from pyiceberg.io.pyarrow import get_bloom_filter_options, parquet_path_to_id_mapping
3030
from pyiceberg.partitioning import PartitionField, PartitionSpec
3131
from pyiceberg.schema import Schema
3232
from pyiceberg.serializers import FromByteStream
@@ -907,7 +907,8 @@ def test_get_bloom_filter_options() -> None:
907907
"write.parquet.bloom-filter-ndv.column.qux.quux": "3000",
908908
}
909909

910-
bloom_filter_options = get_bloom_filter_options(schema, table_properties)
910+
id_to_parquet_path = {field_id: parquet_path for parquet_path, field_id in parquet_path_to_id_mapping(schema).items()}
911+
bloom_filter_options = get_bloom_filter_options(schema, table_properties, id_to_parquet_path)
911912
assert bloom_filter_options == {
912913
"foo": {"fpp": 0.01, "ndv": 1000},
913914
"qux.quux": {"fpp": 0.03, "ndv": 3000},

0 commit comments

Comments
 (0)