rahulsmahadev opened a new pull request, #4542:
URL: https://github.com/apache/iceberg-python/pull/4542
# Rationale for this change
Bug: each chunk of a dictionary column can have its own dictionary, e.g.
after `pa.concat_tables` of tables with pandas categoricals. Appending to a
table identity-partitioned on that column fails, because `group_by` in
`_determine_partitions` cannot combine the dictionaries (apache/arrow#14955):
```python
import pandas as pd
import pyarrow as pa
df1 = pd.DataFrame({"region": ["us", "eu"], "label": pd.Categorical(["a",
"b"])})
df2 = pd.DataFrame({"region": ["ap", "eu"], "label": pd.Categorical(["c",
"b"])})
# table is partitioned by identity(label)
table.append(pa.concat_tables([pa.Table.from_pandas(df1),
pa.Table.from_pandas(df2)]))
# pyarrow.lib.ArrowNotImplementedError: Unifying differing dictionaries
```
Whether the chunks share a dictionary is an encoding detail, and the same
rows append fine when they do.
Fix: combine the chunks of the partition key columns before `group_by`,
which unifies their dictionaries. The data columns are not touched, so the
written files keep their dictionary type.
#4340 decodes dictionaries inside the bucket, truncate and time transforms;
identity needs no decode, only unified dictionaries for the grouping.
Combining different dictionaries still fails when their union exceeds 127
entries with int8 indices; index widening needs a separate change. `upsert`
groups its join columns the same way (`has_duplicate_rows` in `upsert_util.py`)
and still fails on such input (#3835); that is a separate change too. As with
other dictionary appends today, scanning a table that mixes these files with
plain string files needs the open #4342 (#3260).
Java computes the partition key from each row's values (`SparkWrite` calls
`PartitionKey.partition` before the fanout or clustered writer), so
dictionaries never reach the grouping; this PR fixes the matching grouping step
in `_determine_partitions`.
## Are these changes tested?
Yes, added `test_append_dictionary_chunks_to_partitioned_table` (with a null
key) and `test_determine_partitions_with_nested_dictionary_chunks` (partition
source in a struct) to `tests/io/test_pyarrow.py`. Both fail on main with the
error above and pass with this change, also on pyarrow 18.1.
`test_determine_partitions_with_shared_dictionary_at_index_limit` passes on
main and keeps a shared dictionary that fills its int8 indices working
(`unify_dictionaries()` would reject it).
## Are there any user-facing changes?
Yes, these appends work now instead of raising.
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]