Skip to content

GH-48679: [C++] Fix pivot_wider with non-monotonic group ids - #50423

Merged
pitrou merged 2 commits into
apache:mainfrom
pitrou:gh48679-pivot-wider-bug
Jul 22, 2026
Merged

GH-48679: [C++] Fix pivot_wider with non-monotonic group ids#50423
pitrou merged 2 commits into
apache:mainfrom
pitrou:gh48679-pivot-wider-bug

Conversation

@pitrou

@pitrou pitrou commented Jul 8, 2026

Copy link
Copy Markdown
Member

Rationale for this change

The pivot_wider implementation was assuming that the Grouper produces monotonic group ids for the pivot key names.

However, that turns out to not always be true when the FastGrouperImpl is involved (perhaps when hash collisions are involved?).

What changes are included in this PR?

Compute and apply a mapping from pivot key group ids back to pivot key indices.

Are these changes tested?

Yes, by additional tests.

Are there any user-facing changes?

Only a bugfix.

@pitrou pitrou changed the title GH-48679: [C++] Fix pivot_wider bug with non-monotonic group ids GH-48679: [C++] Fix pivot_wider with non-monotonic group ids Jul 8, 2026
@github-actions github-actions Bot added the awaiting review Awaiting review label Jul 8, 2026
@pitrou

pitrou commented Jul 8, 2026

Copy link
Copy Markdown
Member Author

@zanmato1984 Do you think this is ok, or should FastGrouperImpl be fixed to always produce monotonic group ids?

(by the way, I was looking for a way to force group ids to always be non-monotonic in debug mode, but that seems quite non-trivial)

@zanmato1984

Copy link
Copy Markdown
Contributor

@pitrou Although the current Grouper API contract does not explicitly specify the ordering of GetUniques(), the existing tests strongly suggest that it is expected to preserve first-seen order.

If we agree that this should be part of the contract, then this issue should probably be treated as a FastGrouperImpl bug, and the pivot_wider fix would not be strictly necessary. That said, keeping the explicit mapping here as a safeguard seems reasonable.

I am fine with keeping this PR for now. I will keep looking into whether we should make the ordering guarantee explicit, how to fix FastGrouperImpl accordingly, and whether that would have a significant performance impact. If the performance impact turns out to be unacceptable, then we may need to reconsider whether order preservation should be promoted to an API contract.

@pitrou
pitrou marked this pull request as ready for review July 20, 2026 10:00
@pitrou
pitrou requested a review from zanmato1984 as a code owner July 20, 2026 10:00
@pitrou

pitrou commented Jul 20, 2026

Copy link
Copy Markdown
Member Author

@zanmato1984 Would you like to give this a review?

@zanmato1984 zanmato1984 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM, thanks @pitrou.

One note: this change removes what looks like the only production call site of Grouper::Populate(), replacing it with Consume() so we can build the key-index/group-id mapping. I don't think this is a problem: Populate() is still a reasonable public API ("consume without materializing ids") and may have future uses.

If/when we make Grouper's first-seen ordering a public contract, we can revisit this path and see whether pivot_wider can use Populate() again. Perhaps a TODO here would be useful, but I don't think this should block the PR.

Comment thread cpp/src/arrow/compute/kernels/pivot_internal.cc Outdated
Comment thread cpp/src/arrow/compute/kernels/pivot_internal.cc Outdated
@github-actions github-actions Bot added awaiting committer review Awaiting committer review and removed awaiting review Awaiting review labels Jul 21, 2026
@pitrou
pitrou force-pushed the gh48679-pivot-wider-bug branch from afa0d42 to 4ceff4d Compare July 22, 2026 08:31
@pitrou

pitrou commented Jul 22, 2026

Copy link
Copy Markdown
Member Author

I'll merge now. Thanks for the review @zanmato1984

@pitrou
pitrou merged commit 34647d2 into apache:main Jul 22, 2026
58 checks passed
@pitrou pitrou removed the awaiting committer review Awaiting committer review label Jul 22, 2026
@pitrou
pitrou deleted the gh48679-pivot-wider-bug branch July 22, 2026 09:08
@conbench-apache-arrow

Copy link
Copy Markdown

After merging your PR, Conbench analyzed the 4 benchmarking runs that have been run so far on merge-commit 34647d2.

There were no benchmark performance regressions. 🎉

The full Conbench report has more details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants