-
Notifications
You must be signed in to change notification settings - Fork 494
Implement ParquetFormatModel and update write_file to use the format API #3381
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
nssalian
wants to merge
3
commits into
apache:main
Choose a base branch
from
nssalian:file-format-parquet-impl
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.
Open
Changes from 1 commit
Commits
Show all changes
3 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
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,155 @@ | ||
| # 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. | ||
|
|
||
| """Parametrized format writer tests, modeled after Java's BaseFormatModelTests.""" | ||
|
|
||
| from pathlib import Path | ||
|
|
||
| import pyarrow as pa | ||
| import pyarrow.dataset as ds | ||
| import pytest | ||
|
|
||
| from pyiceberg.io.fileformat import FileFormatFactory, FileFormatModel | ||
| from pyiceberg.io.pyarrow import PyArrowFileIO | ||
| from pyiceberg.manifest import FileFormat | ||
| from pyiceberg.schema import Schema | ||
| from pyiceberg.types import LongType, NestedField | ||
|
|
||
|
|
||
| @pytest.fixture(params=FileFormatFactory.available_formats(), ids=lambda f: f.name.lower()) | ||
| def format_model(request: pytest.FixtureRequest) -> FileFormatModel: | ||
| return FileFormatFactory.get(request.param) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def simple_table() -> pa.Table: | ||
|
nssalian marked this conversation as resolved.
Outdated
|
||
| return pa.table( | ||
| { | ||
| "foo": ["a", "b", "c"], | ||
| "bar": pa.array([1, 2, 3], type=pa.int32()), | ||
| "baz": [True, False, True], | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def test_parquet_registered() -> None: | ||
| """ParquetFormatModel is registered in the factory.""" | ||
| model = FileFormatFactory.get(FileFormat.PARQUET) | ||
| assert model.format == FileFormat.PARQUET | ||
| assert model.file_extension() == "parquet" | ||
|
|
||
|
|
||
| def test_round_trip(format_model: FileFormatModel, table_schema_simple: Schema, simple_table: pa.Table, tmp_path: Path) -> None: | ||
| """Write a table and read it back, to verify equality and record count.""" | ||
| file_path = str(tmp_path / f"test.{format_model.file_extension()}") | ||
| writer = format_model.create_writer(PyArrowFileIO().new_output(file_path), table_schema_simple, {}) | ||
| writer.write(simple_table) | ||
| statistics = writer.close() | ||
|
|
||
| result = ds.dataset(file_path).to_table() | ||
| assert result.equals(simple_table) | ||
| assert statistics.record_count == 3 | ||
|
|
||
|
|
||
| def test_statistics_record_count(format_model: FileFormatModel, table_schema_simple: Schema, tmp_path: Path) -> None: | ||
| """close() returns DataFileStatistics with correct record count.""" | ||
| table = pa.table( | ||
|
nssalian marked this conversation as resolved.
Outdated
|
||
| { | ||
| "foo": ["a", "b", "c", "d", "e"], | ||
| "bar": pa.array([10, 20, 30, 40, 50], type=pa.int32()), | ||
| "baz": [True] * 5, | ||
| } | ||
| ) | ||
| file_path = str(tmp_path / f"test.{format_model.file_extension()}") | ||
| writer = format_model.create_writer(PyArrowFileIO().new_output(file_path), table_schema_simple, {}) | ||
| writer.write(table) | ||
| assert writer.close().record_count == 5 | ||
|
|
||
|
|
||
| def test_null_handling(format_model: FileFormatModel, table_schema_simple: Schema, tmp_path: Path) -> None: | ||
| """Nullable columns produce correct null_value_counts in statistics.""" | ||
| table = pa.table( | ||
| { | ||
| "foo": ["a", None, "c"], # field_id=1, optional | ||
| "bar": pa.array([1, 2, 3], type=pa.int32()), # field_id=2, required | ||
| "baz": [True, False, True], # field_id=3, optional | ||
| } | ||
| ) | ||
| file_path = str(tmp_path / f"test.{format_model.file_extension()}") | ||
| writer = format_model.create_writer(PyArrowFileIO().new_output(file_path), table_schema_simple, {}) | ||
| writer.write(table) | ||
| stats = writer.close() | ||
| assert stats.record_count == 3 | ||
| assert stats.null_value_counts.get(1) == 1 | ||
|
|
||
|
|
||
| def test_context_manager_caches_result( | ||
| format_model: FileFormatModel, table_schema_simple: Schema, simple_table: pa.Table, tmp_path: Path | ||
| ) -> None: | ||
| """writer.result() returns cached statistics after context manager exit.""" | ||
| file_path = str(tmp_path / f"test.{format_model.file_extension()}") | ||
| writer = format_model.create_writer(PyArrowFileIO().new_output(file_path), table_schema_simple, {}) | ||
| with writer: | ||
| writer.write(simple_table) | ||
| assert writer.result().record_count == 3 | ||
|
|
||
|
|
||
| def test_close_is_idempotent( | ||
| format_model: FileFormatModel, table_schema_simple: Schema, simple_table: pa.Table, tmp_path: Path | ||
| ) -> None: | ||
| """Calling close() twice returns the same cached statistics object.""" | ||
| file_path = str(tmp_path / f"test.{format_model.file_extension()}") | ||
| writer = format_model.create_writer(PyArrowFileIO().new_output(file_path), table_schema_simple, {}) | ||
| writer.write(simple_table) | ||
| stats1 = writer.close() | ||
| stats2 = writer.close() | ||
| assert stats1 is stats2 | ||
|
|
||
|
|
||
| def test_close_without_write_raises(format_model: FileFormatModel, table_schema_simple: Schema, tmp_path: Path) -> None: | ||
| """Closing a writer that was never written to raises ValueError.""" | ||
| file_path = str(tmp_path / f"test.{format_model.file_extension()}") | ||
| writer = format_model.create_writer(PyArrowFileIO().new_output(file_path), table_schema_simple, {}) | ||
| with pytest.raises(ValueError, match="Cannot close a writer that was never written to"): | ||
| writer.close() | ||
|
|
||
|
|
||
| def test_construct_field_uses_orc_field_id_key() -> None: | ||
| """ArrowProjectionVisitor uses ORC field ID and required keys when file_format is ORC.""" | ||
| from pyiceberg.io.pyarrow import ( | ||
| ORC_FIELD_ID_KEY, | ||
| ORC_FIELD_REQUIRED_KEY, | ||
| PYARROW_PARQUET_FIELD_ID_KEY, | ||
| ArrowProjectionVisitor, | ||
| ) | ||
|
|
||
| schema = Schema(NestedField(field_id=1, name="x", field_type=LongType(), required=True)) | ||
|
|
||
| visitor = ArrowProjectionVisitor(schema, include_field_ids=True, file_format=FileFormat.ORC) | ||
| field = visitor._construct_field(schema.find_field(1), pa.int64()) | ||
| assert field.metadata is not None | ||
| assert ORC_FIELD_ID_KEY in field.metadata | ||
| assert ORC_FIELD_REQUIRED_KEY in field.metadata | ||
| assert field.metadata[ORC_FIELD_REQUIRED_KEY] == b"true" | ||
| assert PYARROW_PARQUET_FIELD_ID_KEY not in field.metadata | ||
|
|
||
| visitor_pq = ArrowProjectionVisitor(schema, include_field_ids=True, file_format=FileFormat.PARQUET) | ||
| field_pq = visitor_pq._construct_field(schema.find_field(1), pa.int64()) | ||
| assert field_pq.metadata is not None | ||
| assert PYARROW_PARQUET_FIELD_ID_KEY in field_pq.metadata | ||
| assert ORC_FIELD_ID_KEY not in field_pq.metadata | ||
| assert ORC_FIELD_REQUIRED_KEY not in field_pq.metadata | ||
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.
Uh oh!
There was an error while loading. Please reload this page.