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!

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.

1 participant