Repository navigation
Unify partition key dictionaries before grouping - #4542
rahulsmahadev wants to merge 1 commit into
Conversation
Each chunk of a dictionary column can carry its own dictionary, e.g. after pa.concat_tables of pandas categoricals, and group_by cannot combine them, so an append identity-partitioned on such a column failed with "Unifying differing dictionaries". Combine the chunks of the partition key columns before grouping, which unifies their dictionaries; unify_dictionaries() would reject a shared dictionary that fills its index type, which appends today. Co-authored-by: Isaac <no-reply@databricks.com>
|
@Anurag-S1ngh, this follows up on your #4340 for identity partitions: identity needs no decode, but |
|
Reviewed. The failure reproduces on main and the change fixes it on pyarrow 18.1 and 21. |
|
Thanks @Anurag-S1ngh for checking it on both versions. I kept identity undecoded because of Fokko's comment on #4340 ("We don't always have to decode the dictionary, for example, for an identity or void transform, this isn't needed"), so this only combines the chunks of the identity key column before |
|
The chunk combine is fine, no need to switch. Decoding the identity key would also fix it but is not needed for this change, and keeping it scoped to the grouping is cleaner. The index-widening case can stay in #3835. |
Areesha-Rizwan412
left a comment
There was a problem hiding this comment.
Verified the grouping step and the dictionary edge cases before approving.
- Reproduced the failure standalone:
group_byon a chunked dictionary column with per-chunk dictionaries raisesArrowNotImplementedError: Unifying differing dictionaries, andcombine_chunks().group_by(...)returns the four keys including the null partition. That matches thetest_append_dictionary_chunks_to_partitioned_tablesetup. combine_chunksoverunify_dictionariesis the right call: with a shared 128-value dictionary on int8 indices,unify_dictionaries()raisesArrowInvalid(the unified dictionary requires a larger index type) whilecombine_chunksgroups fine - which is whattest_determine_partitions_with_shared_dictionary_at_index_limitpins.- The combine is scoped to
arrow_table.select(partition_fields), so only the synthetic_partition_*columns are unified;filtered_tablestill slices the originalarrow_table, so written files keep their dictionary encoding as the tests assert. - Index widening past 127 and the upsert path in #3835 stay scoped out and documented; identity needs no decode per #4340.
CI green on 53530f6b.
Rationale for this change
Bug: each chunk of a dictionary column can have its own dictionary, e.g. after
pa.concat_tablesof tables with pandas categoricals. Appending to a table identity-partitioned on that column fails, becausegroup_byin_determine_partitionscannot combine the dictionaries (apache/arrow#14955):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.
upsertgroups its join columns the same way (has_duplicate_rowsinupsert_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 (
SparkWritecallsPartitionKey.partitionbefore 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) andtest_determine_partitions_with_nested_dictionary_chunks(partition source in a struct) totests/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_limitpasses 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
changeloglabel.This pull request and its description were written by Isaac.