-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-12728: [C++] Implement count_distinct/distinct hash aggregate kernels #10876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
9053eeb
ARROW-12728: [C++] Add count_distinct hash aggregate kernel
lidavidm 53e807b
ARROW-12728: [C++] Add distinct hash aggregate kernel
lidavidm b29f378
ARROW-12728: [C++] Fix type
lidavidm 9d0ceaf
ARROW-12728: [C++] Assemble results of distinct via Take
lidavidm 7403fdf
ARROW-12728: [C++] Add cast for MSVC
lidavidm 566bf0c
ARROW-12728: [C++] Clean up tests
lidavidm 35e30c8
ARROW-12728: [C++] Reuse MakeGroupings/ApplyGroupings
lidavidm 991c339
ARROW-12728: [C++] Eagerly initialize grouper
lidavidm f5f4397
ARROW-12728: [C++] Add cast for MSVC
lidavidm bf9ce07
ARROW-12728: [C++] Allocate new array for remapped group IDs
lidavidm 785bac7
ARROW-12728: [C++] Fix comment wording
lidavidm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1239,6 +1239,184 @@ TEST(GroupBy, AnyAndAll) { | |
| } | ||
| } | ||
|
|
||
| TEST(GroupBy, CountDistinct) { | ||
| for (bool use_threads : {true, false}) { | ||
| SCOPED_TRACE(use_threads ? "parallel/merged" : "serial"); | ||
|
|
||
| auto table = | ||
| TableFromJSON(schema({field("argument", float64()), field("key", int64())}), {R"([ | ||
| [1, 1], | ||
| [1, 1] | ||
| ])", | ||
| R"([ | ||
| [0, 2], | ||
| [null, 3] | ||
| ])", | ||
| R"([ | ||
| [4, null], | ||
| [1, 3] | ||
| ])", | ||
| R"([ | ||
| [0, 2], | ||
| [-1, 2] | ||
| ])", | ||
| R"([ | ||
| [1, null], | ||
| [NaN, 3] | ||
| ])", | ||
| R"([ | ||
| [2, null], | ||
| [3, null] | ||
| ])"}); | ||
|
|
||
| ASSERT_OK_AND_ASSIGN(Datum aggregated_and_grouped, | ||
| internal::GroupBy( | ||
| { | ||
| table->GetColumnByName("argument"), | ||
| }, | ||
| { | ||
| table->GetColumnByName("key"), | ||
| }, | ||
| { | ||
| {"hash_count_distinct", nullptr}, | ||
| }, | ||
| use_threads)); | ||
| SortBy({"key_0"}, &aggregated_and_grouped); | ||
| ValidateOutput(aggregated_and_grouped); | ||
|
|
||
| AssertDatumsEqual(ArrayFromJSON(struct_({ | ||
| field("hash_count_distinct", int64()), | ||
| field("key_0", int64()), | ||
| }), | ||
| R"([ | ||
| [1, 1], | ||
| [2, 2], | ||
| [3, 3], | ||
| [4, null] | ||
| ])"), | ||
| aggregated_and_grouped, | ||
| /*verbose=*/true); | ||
|
|
||
| table = | ||
| TableFromJSON(schema({field("argument", utf8()), field("key", int64())}), {R"([ | ||
| ["foo", 1], | ||
| ["foo", 1] | ||
| ])", | ||
| R"([ | ||
| ["bar", 2], | ||
| [null, 3] | ||
| ])", | ||
| R"([ | ||
| ["baz", null], | ||
| ["foo", 3] | ||
| ])", | ||
| R"([ | ||
| ["bar", 2], | ||
| ["spam", 2] | ||
| ])", | ||
| R"([ | ||
| ["eggs", null], | ||
| ["ham", 3] | ||
| ])", | ||
| R"([ | ||
| ["a", null], | ||
| ["b", null] | ||
| ])"}); | ||
|
|
||
| ASSERT_OK_AND_ASSIGN(aggregated_and_grouped, | ||
| internal::GroupBy( | ||
| { | ||
| table->GetColumnByName("argument"), | ||
| }, | ||
| { | ||
| table->GetColumnByName("key"), | ||
| }, | ||
| { | ||
| {"hash_count_distinct", nullptr}, | ||
| }, | ||
| use_threads)); | ||
| ValidateOutput(aggregated_and_grouped); | ||
| SortBy({"key_0"}, &aggregated_and_grouped); | ||
|
|
||
| AssertDatumsEqual(ArrayFromJSON(struct_({ | ||
| field("hash_count_distinct", int64()), | ||
| field("key_0", int64()), | ||
| }), | ||
| R"([ | ||
| [1, 1], | ||
| [2, 2], | ||
| [3, 3], | ||
| [4, null] | ||
| ])"), | ||
| aggregated_and_grouped, | ||
| /*verbose=*/true); | ||
| } | ||
| } | ||
|
|
||
| TEST(GroupBy, Distinct) { | ||
| for (bool use_threads : {true, false}) { | ||
| SCOPED_TRACE(use_threads ? "parallel/merged" : "serial"); | ||
|
|
||
| auto table = | ||
| TableFromJSON(schema({field("argument", utf8()), field("key", int64())}), {R"([ | ||
| ["foo", 1], | ||
| ["foo", 1] | ||
| ])", | ||
| R"([ | ||
| ["bar", 2], | ||
| [null, 3] | ||
| ])", | ||
| R"([ | ||
| ["baz", null], | ||
| ["foo", 3] | ||
| ])", | ||
| R"([ | ||
| ["bar", 2], | ||
| ["spam", 2] | ||
| ])", | ||
| R"([ | ||
| ["eggs", null], | ||
| ["ham", 3] | ||
| ])", | ||
| R"([ | ||
| ["a", null], | ||
| ["b", null] | ||
| ])"}); | ||
|
|
||
| ASSERT_OK_AND_ASSIGN(auto aggregated_and_grouped, | ||
| internal::GroupBy( | ||
| { | ||
| table->GetColumnByName("argument"), | ||
| }, | ||
| { | ||
| table->GetColumnByName("key"), | ||
| }, | ||
| { | ||
| {"hash_distinct", nullptr}, | ||
| }, | ||
| use_threads)); | ||
| ValidateOutput(aggregated_and_grouped); | ||
| SortBy({"key_0"}, &aggregated_and_grouped); | ||
|
|
||
| // Order of sub-arrays is not stable | ||
|
||
| auto struct_arr = aggregated_and_grouped.array_as<StructArray>(); | ||
| auto distinct_arr = checked_pointer_cast<ListArray>(struct_arr->field(0)); | ||
| auto sort = [](const Array& arr) -> std::shared_ptr<Array> { | ||
| EXPECT_OK_AND_ASSIGN(auto indices, SortIndices(arr)); | ||
| EXPECT_OK_AND_ASSIGN(auto sorted, Take(arr, indices)); | ||
| return sorted.make_array(); | ||
| }; | ||
| AssertDatumsEqual(ArrayFromJSON(utf8(), R"(["foo"])"), | ||
| sort(*distinct_arr->value_slice(0)), /*verbose=*/true); | ||
| AssertDatumsEqual(ArrayFromJSON(utf8(), R"(["bar", "spam"])"), | ||
| sort(*distinct_arr->value_slice(1)), /*verbose=*/true); | ||
| AssertDatumsEqual(ArrayFromJSON(utf8(), R"(["foo", "ham", null])"), | ||
| sort(*distinct_arr->value_slice(2)), /*verbose=*/true); | ||
| AssertDatumsEqual(ArrayFromJSON(utf8(), R"(["a", "b", "baz", "eggs"])"), | ||
| sort(*distinct_arr->value_slice(3)), /*verbose=*/true); | ||
| } | ||
| } | ||
|
|
||
| TEST(GroupBy, CountAndSum) { | ||
| auto batch = RecordBatchFromJSON( | ||
| schema({field("argument", float64()), field("key", int64())}), R"([ | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michalursa what do you think of adding
Grouper::Reserve(additional_capacity_hint)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The keys here are pairs of
[value, group_id]so just having the number of groups doesn't give much of a capacity hint.