Skip to content

Fix StartsWith filters on dictionary-encoded columns - #4605

Merged
Fokko merged 2 commits into
apache:mainfrom
rahulsmahadev:rahul/starts-with-dictionary
Dec 3, 2026
Merged

Fokko merged 2 commits into
apache:mainfrom
rahulsmahadev:rahul/starts-with-dictionary

Conversation

@rahulsmahadev

Copy link
Copy Markdown
Contributor

Rationale for this change

Bug: StartsWith and NotStartsWith (LIKE 'ap%') raise when a string column is read as an Arrow dictionary. pc.starts_with has no dictionary kernel (no Arrow string kernel has one yet, apache/arrow#40430), while the comparison and is_in kernels decode dictionaries, so =, < and IN work on the same column:

tbl = catalog.create_table("db.fruit", schema=pa.schema([("fruit", pa.string())]))
tbl.append(pa.table({"fruit": ["apple", "banana", "apricot"]}))
tbl.scan(row_filter="fruit LIKE 'ap%'").to_arrow(dictionary_columns=("fruit",))
# pyarrow.lib.ArrowNotImplementedError: Function 'starts_with' has no kernel matching input types (dictionary<values=string, indices=int32, ordered=0>)

The same happens without dictionary_columns for a file written from a pandas categorical or a Polars Enum column (appended, or added with add_files), in delete and overwrite with such a filter, and for struct fields (addr.city LIKE 'ap%').

Fix: _ConvertToArrowExpression reads the field of starts_with as the Arrow type of its Iceberg type, large_string or large_binary (from _ConvertToArrowSchema, as for the literals of the other predicates). Only the predicate input is decoded, like #4340 for the partition transforms (with a cast, because an Arrow expression cannot call dictionary_decode), so scans still return dictionary columns and binary prefixes (#3501) stay bytes. A large_string column is not copied; a string column only gets wider offsets.

Java prunes files and row groups on bounds and dictionaries, then checks rows on decoded values: Evaluator in the generic reader, and the engine on the values the vectorized reader decodes (DictionaryStringAccessor). PyIceberg runs that row check in Arrow, in scans and in the rewrite of delete; this PR fixes that part.

  • Arrow does not prune row groups on a starts_with prefix (only row groups that hold a single value or only nulls), with or without this change; Java's ParquetMetricsRowGroupFilter and ParquetDictionaryRowGroupFilter prune on the prefix.

#4342 (for #3260) decodes unrequested top-level dictionaries after the read, so it does not reach the scan filter, dictionary_columns or struct fields.

Are these changes tested?

Yes: test_starts_with_dictionary_encoded_to_pyarrow (string and binary columns, plain and dictionary arrays) and test_row_filter_starts_with_on_dictionary_encoded_strings (dictionary_columns, a dictionary file with a struct field, delete) fail on main with the error above and pass with this change; the two starts_with repr tests now expect the cast.

Are there any user-facing changes?

Prefix filters, deletes and overwrites work on dictionary-encoded columns, and expression_to_pyarrow casts the starts_with field to large_string or large_binary. Please add the changelog label.

This pull request and its description were written by Isaac.

rahulsmahadev and others added 2 commits December 2, 2026 18:03
pc.starts_with has no kernel for dictionary arrays, so LIKE 'ap%' raised in scans, deletes and
overwrites when a string column was read as a dictionary (dictionary_columns, or a file written
from a pandas categorical). Read the field of StartsWith and NotStartsWith as the Arrow type of its
Iceberg type (large_string or large_binary), which decodes dictionaries and keeps binary prefixes.

Co-authored-by: Isaac <no-reply@databricks.com>
Co-authored-by: Isaac <no-reply@databricks.com>

@Fokko Fokko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice fix, verified the dictionary-encoded string, binary and struct cases pass with the cast. Thanks @rahulsmahadev 🚀

@Fokko
Fokko added this pull request to the merge queue Dec 3, 2026
Merged via the queue into apache:main with commit 64bdf5d Dec 3, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants