rahulsmahadev opened a new pull request, #4605:
URL: https://github.com/apache/iceberg-python/pull/4605

   # 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:
   
   ```python
   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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to