Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 4.3k
GH-46629: [Python] Add options to DatasetFactory.inspect#46961
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
Merged
pitrou
merged 16 commits into
apache:main
from
hadrian-reppas:dataset_factory_inspect_params2Jul 15, 2025
+144
−15
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
07cc309
Update DatasetFactory.inspect
9c105e3
Remove constructor call
1a024a6
Fix formatting
534aa61
Define _parse_field_merge_options
1a8e990
Small changes
8507321
Fix everything
08f40e8
Merge branch 'dataset_factory_inspect_params2' into dataset_factory_i…
hadrian-reppas 67d3bcb
Merge pull request #1 from hadrian-reppas/dataset_factory_inspect_par…
hadrian-reppas 92eaa14
Add docstring to _parse_field_merge_options
b9e8b36
Update failing test
069fb15
Add tests
0848c4a
Fix formatting
fd91501
Review comments
8424d64
Create Table in test
5f7578d
Validate table
hadrian-reppas 6dcbc5b
Add comments with expected promotions
hadrian-reppas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -147,6 +147,42 @@ def mockfs(): | ||
| return mockfs | ||
| @pytest.fixture | ||
| def promotable_mockfs(): | ||
| """ | ||
| Creates a _MockFileSystem with two parquet files that have promotable schemas. | ||
| - file1: value: int8, dictionary: dictionary<int8, string> | ||
| - file2: value: uint16, dictionary: dictionary<int16, string> | ||
| """ | ||
| mockfs = fs._MockFileSystem() | ||
| table1 = pa.table({ | ||
| 'value': pa.array([1, 2], type=pa.int8()), | ||
| 'dictionary': pa.DictionaryArray.from_arrays( | ||
| pa.array([0, 1], type=pa.int8()), | ||
| ['a', 'b'], | ||
| ), | ||
| }) | ||
| table2 = pa.table({ | ||
| 'value': pa.array([3, 4], type=pa.uint16()), | ||
| 'dictionary': pa.DictionaryArray.from_arrays( | ||
| pa.array([1, 0], type=pa.int16()), | ||
| ['d', 'c'], | ||
| ), | ||
| }) | ||
| mockfs.create_dir('subdir/zzz') | ||
| path1 = 'subdir/zzz/file1.parquet' | ||
| with mockfs.open_output_stream(path1) as out: | ||
| pq.write_table(table1, out) | ||
| path2 = 'subdir/zzz/file2.parquet' | ||
| with mockfs.open_output_stream(path2) as out: | ||
| pq.write_table(table2, out) | ||
| return mockfs, path1, path2 | ||
| @pytest.fixture | ||
| def open_logging_fs(monkeypatch): | ||
| from pyarrow.fs import LocalFileSystem, PyFileSystem | ||
| @@ -451,6 +487,70 @@ def test_dataset(dataset, dataset_reader): | ||
| assert_dataset_fragment_convenience_methods(dataset) | ||
| def test_dataset_factory_inspect_schema_promotion(promotable_mockfs): | ||
| mockfs, path1, path2 = promotable_mockfs | ||
| factory = ds.FileSystemDatasetFactory( | ||
| mockfs, [path1, path2], ds.ParquetFileFormat() | ||
| ) | ||
| with pytest.raises( | ||
| pa.ArrowTypeError, | ||
| match='Unable to merge: Field value has incompatible types: int8 vs uint16', | ||
| ): | ||
| factory.inspect() | ||
| schema = factory.inspect(promote_options='permissive') | ||
| expected_schema = pa.schema([ | ||
| # Promoted from int8 and uint16 | ||
| pa.field('value', pa.int32()), | ||
| # Dictionary index type promoted from int8 and int16 | ||
| pa.field('dictionary', pa.dictionary(pa.int16(), pa.string())), | ||
| ]) | ||
| assert schema.equals(expected_schema) | ||
| dataset = factory.finish(schema) | ||
| table = dataset.to_table() | ||
| table.validate(full=True) | ||
| expected_table = pa.table({ | ||
| 'value': pa.chunked_array([[1, 2], [3, 4]], type=pa.int32()), | ||
| 'dictionary': pa.chunked_array([ | ||
| pa.DictionaryArray.from_arrays( | ||
| pa.array([0, 1], type=pa.int16()), | ||
| ['a', 'b'] | ||
| ), | ||
| pa.DictionaryArray.from_arrays( | ||
| pa.array([1, 0], type=pa.int16()), | ||
| ['d', 'c'] | ||
| ) | ||
| ]), | ||
| }) | ||
| assert table.equals(expected_table) | ||
| # Inspecting only one fragment should not promote the 'value' field | ||
| inspected_schema_one_frag = factory.inspect( | ||
| promote_options='permissive', fragments=1) | ||
| assert inspected_schema_one_frag.field('value').type != pa.int32() | ||
pitrou marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def test_dataset_factory_inspect_bad_params(promotable_mockfs): | ||
| mockfs, path1, path2 = promotable_mockfs | ||
| factory = ds.FileSystemDatasetFactory( | ||
| mockfs, [path1, path2], ds.ParquetFileFormat() | ||
| ) | ||
| with pytest.raises(ValueError, match='Invalid promote_options: bad_option'): | ||
| factory.inspect(promote_options='bad_option') | ||
| with pytest.raises( | ||
| ValueError, match="Fragment count must be a non-negative int or None; got 'one'" | ||
| ): | ||
| factory.inspect(fragments="one") | ||
| with pytest.raises( | ||
| ValueError, match='Fragment count must be a non-negative int or None; got -1' | ||
| ): | ||
| factory.inspect(fragments=-1) | ||
| @pytest.mark.parquet | ||
| def test_scanner_options(dataset): | ||
| scanner = dataset.to_batches(fragment_readahead=16, batch_readahead=8) | ||
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
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.