Skip to content

Unify partition key dictionaries before grouping - #4542

Open
rahulsmahadev wants to merge 1 commit into
apache:mainfrom
rahulsmahadev:rahul/partition-dictionary-chunks
Open

rahulsmahadev wants to merge 1 commit into
apache:mainfrom
rahulsmahadev:rahul/partition-dictionary-chunks

Conversation

@rahulsmahadev

Copy link
Copy Markdown
Contributor

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):

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.

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>
@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

@Anurag-S1ngh, this follows up on your #4340 for identity partitions: identity needs no decode, but group_by cannot combine chunks with different dictionaries, so this combines the partition key chunks before grouping, which unifies their dictionaries. Could you take a look when you have a chance? Thanks!

@Anurag-S1ngh

Copy link
Copy Markdown
Contributor

Reviewed. The failure reproduces on main and the change fixes it on pyarrow 18.1 and 21. combine_chunks is the right call: unify_dictionaries raises on a shared dictionary that fills the int8 index range, and the test covers that. The remaining index-widening failure is documented and fine to scope out. One question: did you consider decoding identity dictionaries like #4340, which would also cover it? Either way, no blockers from me.

@rahulsmahadev

Copy link
Copy Markdown
Contributor Author

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 group_by. Decoding that column in the identity transform would also fix it. Happy to switch to that if you and Fokko prefer it.

@Anurag-S1ngh

Copy link
Copy Markdown
Contributor

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 Areesha-Rizwan412 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Verified the grouping step and the dictionary edge cases before approving.

  • Reproduced the failure standalone: group_by on a chunked dictionary column with per-chunk dictionaries raises ArrowNotImplementedError: Unifying differing dictionaries, and combine_chunks().group_by(...) returns the four keys including the null partition. That matches the test_append_dictionary_chunks_to_partitioned_table setup.
  • combine_chunks over unify_dictionaries is the right call: with a shared 128-value dictionary on int8 indices, unify_dictionaries() raises ArrowInvalid (the unified dictionary requires a larger index type) while combine_chunks groups fine - which is what test_determine_partitions_with_shared_dictionary_at_index_limit pins.
  • The combine is scoped to arrow_table.select(partition_fields), so only the synthetic _partition_* columns are unified; filtered_table still slices the original arrow_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.

This branch has not been deployed

No deployments
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.

3 participants