PyArrow: Cast dictionary-encoded arrays to target schema during scan projection (#3260) - #3849
Open
hedger9487 wants to merge 1 commit into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes ArrowScan.to_table() failures when scanning tables that contain a mix of dictionary-encoded and plain string columns across different data files, by ensuring projected batches are cast to the target Iceberg/Arrow schema during scan projection.
Changes:
- Pass
dictionary_columnsthrough the scan projection path so the projector can make correct type decisions. - Update
ArrowProjectionVisitor._cast_if_needed()to cast primitiveDictionaryTypearrays to the target primitive Arrow type unless the column is explicitly requested as dictionary-encoded output. - Add a regression test covering mixed dictionary-encoded and plain string files for the same column.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| pyiceberg/io/pyarrow.py | Ensure scan projection casts dictionary arrays to the requested primitive Arrow type unless opted into dictionary output via dictionary_columns. |
| tests/io/test_pyarrow.py | Add regression test reproducing the mixed dict/plain string scan scenario and asserting successful concatenation with correct output type/values. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Fixes #3260.
When an Iceberg table contains mixed data files where some files contain dictionary-encoded string columns (e.g. written by clients using dictionary encoding) and other files contain plain strings (e.g. after Athena/Trino
OPTIMIZEdata compaction),ArrowScan.to_table()fails during batch concatenation with:This occurs because
ArrowProjectionVisitor._cast_if_needed()did not cast primitive dictionary-encoded arrays to the target schema type when projecting batches unless explicitly requested indictionary_columns.This PR updates
_cast_if_needed()inpyiceberg/io/pyarrow.pyto castDictionaryTypearrays to the target Iceberg primitive schema type when the column is not indictionary_columns.Testing
test_arrow_scan_mixed_dict_encoded_and_plain_stringsintests/io/test_pyarrow.py.