Skip to content

GH-45269: [C++][Compute] Add "pivot_wider" and "hash_pivot_wider" functions - #45562

Merged
pitrou merged 7 commits into
apache:mainfrom
pitrou:pivot_kernel
Mar 4, 2025
Merged

pitrou merged 7 commits into
apache:mainfrom
pitrou:pivot_kernel

Conversation

@pitrou

@pitrou pitrou commented Feb 18, 2025 •

Copy link
Copy Markdown
Member

Rationale for this change

Add "pivot wider" functionality such as in Pandas, through two dedicated functions:

  1. a "pivot_wider" scalar aggregate function returning a Struct scalar
  2. a "hash_pivot_wider" grouped aggregate function returning a Struct array

Both functions take two arguments (the column of pivot keys and the column of pivot values) and require passing a PivotWiderOptions structure with the expected pivot keys, so as to determine the output Struct type.

Are these changes tested?

Yes, by dedicated unit tests.

Are there any user-facing changes?

No, just new APIs.

@pitrou
pitrou marked this pull request as ready for review February 18, 2025 12:52
@pitrou
pitrou requested a review from westonpace as a code owner February 18, 2025 12:52
@pitrou
pitrou requested review from zanmato1984 and removed request for westonpace February 18, 2025 12:52
@pitrou

ghost commented Feb 18, 2025

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit -g cpp

@github-actions

This comment was marked as outdated.

Comment on lines 297 to 303

ghost Feb 18, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Without this change, any error in MergeAll below could lead to a crash.

ghost Feb 25, 2025

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.

Not sure I understand this - shouldn't error in MergeAll be bubbled up and stop execution? (Why does it lead to crash?)

ghost Feb 25, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I suppose it does not actually stop execution. @zanmato1984 might know more about this.

ghost Feb 26, 2025

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.

I don't know... why a new scalar agg function makes an existing acero node having to change the internal logic?

Is there any test failing w/o this part of change? Maybe I can take a look and then tell if this change is right.

ghost Feb 26, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Any test where the merge step returns an error could be affected. I guess other hash-aggregate functions never fail in their merge step, so this was not exercised before.

ghost Feb 26, 2025

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.

I tried, no luck, sorry. And from the code, the case PivotDuplicateValues is using group by thus shouldn't be executing this code path. I can't speak for the validity of this change w/o truly understanding what the problem is.

I do have a suspect though: this might be race related. And if, a big if, this is the case, I'm afraid your change doesn't fix the race. However there are still many things unclear so please don't take this seriously until I have new findings.

Do you still hit the problem if you revert this part of the change? Or shall we try reverting it (maybe temporarily) to see if CI is good?

ghost Feb 26, 2025

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.

And from the code, the case PivotDuplicateValues is using group by thus shouldn't be executing this code path.

Update: I was wrong about this part - the test is indeed doing the scalar agg.

(But unfortunately this doesn't change rest of the my comment.)

ghost Feb 26, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Hmm, I can't seem to reproduce locally right now, though I did have to change this to have the tests pass at the time. I'm gonna revert temporarily so that we can check CI.

ghost Feb 26, 2025

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.

I do have a suspect though: this might be race related. And if, a big if, this is the case, I'm afraid your change doesn't fix the race. However there are still many things unclear so please don't take this seriously until I have new findings.

Update: Turns out my suspect of race won't happen actually.

So I'm at my wits end by now.

ghost Feb 26, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Okay, even with Valgrind I can't reproduce anymore.

@icexelloss

ghost commented Feb 25, 2025

Copy link
Copy Markdown
Contributor

Should we also add python binding for the option and call via pyarrow.compute?

@pitrou

ghost commented Feb 25, 2025

Copy link
Copy Markdown
Member Author

Should we also add python binding for the option and call via pyarrow.compute?

We can indeed, for the aggregate function that is.

@zanmato1984

ghost commented Feb 26, 2025

Copy link
Copy Markdown
Contributor

@github-actions crossbow submit -g cpp

@github-actions

This comment was marked as outdated.

@pitrou

ghost commented Feb 26, 2025

Copy link
Copy Markdown
Member Author

@github-actions crossbow submit -g cpp

@github-actions

ghost commented Feb 26, 2025

Copy link
Copy Markdown

Revision: d97a9a7d5b0abdc9b42faffaccfaed12bbd3362e

Submitted crossbow builds: ursacomputing/crossbow @ actions-e51f9aecb5

Task Status
example-cpp-minimal-build-static GitHub Actions
example-cpp-minimal-build-static-system-dependency GitHub Actions
example-cpp-tutorial GitHub Actions
test-alpine-linux-cpp GitHub Actions
test-build-cpp-fuzz GitHub Actions
test-conda-cpp GitHub Actions
test-conda-cpp-meson GitHub Actions
test-conda-cpp-valgrind GitHub Actions
test-cuda-cpp-ubuntu-22.04-cuda-11.7.1 GitHub Actions
test-debian-12-cpp-amd64 GitHub Actions
test-debian-12-cpp-i386 GitHub Actions
test-fedora-39-cpp GitHub Actions
test-ubuntu-22.04-cpp GitHub Actions
test-ubuntu-22.04-cpp-20 GitHub Actions
test-ubuntu-22.04-cpp-bundled GitHub Actions
test-ubuntu-22.04-cpp-emscripten GitHub Actions
test-ubuntu-22.04-cpp-no-threading GitHub Actions
test-ubuntu-24.04-cpp GitHub Actions
test-ubuntu-24.04-cpp-bundled-offline GitHub Actions
test-ubuntu-24.04-cpp-gcc-13-bundled GitHub Actions
test-ubuntu-24.04-cpp-gcc-14 GitHub Actions
test-ubuntu-24.04-cpp-minimal-with-formats GitHub Actions
test-ubuntu-24.04-cpp-thread-sanitizer GitHub Actions

@pitrou

ghost commented Feb 26, 2025

Copy link
Copy Markdown
Member Author

I've addressed your review comments and also added Python bindings. @icexelloss @zanmato1984

ghost 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.

Still looking at the implantation. Just a few questions about the function prototype so far.

Comment thread cpp/src/arrow/compute/api_aggregate.h Resolved
Comment thread cpp/src/arrow/compute/api_aggregate.h Resolved

ghost 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.

Still looking at the detailed code. Some comments so far.

ghost Feb 28, 2025

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.

We may need to add some description about "duplicate values".

ghost Mar 3, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think this is covered by "If a pivot key appears twice, KeyError is raised"?

ghost Mar 3, 2025

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.

Well, that's what I originally expected, until I saw Invalid not KeyError is actually thrown in DuplicateValue, so I think this description is not accurate enough.

ghost Mar 4, 2025

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.

At least we should either change the error being thrown or refine the wording here.

ghost Mar 4, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Well, that's what I originally expected, until I saw Invalid not KeyError is actually thrown in DuplicateValue, so I think this description is not accurate enough.

Good point, I'll correct this.

ghost Mar 4, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Can you take a look at the updated wording @zanmato1984 ?

ghost Mar 4, 2025

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.

Hmm, seems this time KeyError for specifying duplicate key names is not mentioned?

ghost Mar 4, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yes, should we mention it? It's not really likely to occur. But we can still be explicit about it.

ghost Mar 4, 2025

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.

Well, not a big deal. The docstring looks good now.

Comment thread cpp/src/arrow/compute/api_aggregate.h Resolved

ghost Mar 3, 2025

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.

Does it make sense to support scalar here as well? (similar to the non hashed version)

ghost Mar 3, 2025

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good question. It might, depending on whether that is easily testable.

@icexelloss

ghost commented Mar 3, 2025

Copy link
Copy Markdown
Contributor

Thanks @pitrou - left a question on whether we want to handle scalar case for the hash kernel as well - otherwise LGTM.

I don't think the scalar case for hash kernel is blocking - we can probably do as a follow up if you want to merge this first.

@pitrou

ghost commented Mar 3, 2025

Copy link
Copy Markdown
Member Author

I don't think the scalar case for hash kernel is blocking - we can probably do as a follow up if you want to merge this first.

I'll wait for @zanmato1984 's opinion before moving forward on this.

ghost 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 with two nits.

ghost Mar 3, 2025

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.

Well, that's what I originally expected, until I saw Invalid not KeyError is actually thrown in DuplicateValue, so I think this description is not accurate enough.

@pitrou

ghost commented Mar 4, 2025

Copy link
Copy Markdown
Member Author

Thanks a lot for the reviews @zanmato1984 @icexelloss ! I will merge this PR now.

@pitrou
pitrou merged commit b41386d into apache:main Mar 4, 2025
@pitrou
pitrou deleted the pivot_kernel branch March 4, 2025 16:21
@conbench-apache-arrow

ghost commented Mar 5, 2025

Copy link
Copy Markdown

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

There were no benchmark performance regressions. 🎉

The full Conbench report has more details. It also includes information about 48 possible false positives for unstable benchmarks that are known to sometimes produce them.

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.

3 participants