This is an automated email from the ASF dual-hosted git repository.
apitrou pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow.git
The following commit(s) were added to refs/heads/main by this push:
new cf7728121f GH-45739: [C++][Python] Fix crash when calling
hash_pivot_wider without options (#45740)
cf7728121f is described below
commit cf7728121f3267da03406a2a7c4601002222492c
Author: Antoine Pitrou <[email protected]>
AuthorDate: Tue Mar 11 16:51:27 2025 +0100
GH-45739: [C++][Python] Fix crash when calling hash_pivot_wider without
options (#45740)
### Rationale for this change
Calling `hash_pivot_wider` without options is not very useful (it defaults
to `key_names=[]`) but at least it shouldn't crash.
### Are these changes tested?
Yes.
### Are there any user-facing changes?
Just a bugfix.
* GitHub Issue: #45739
Authored-by: Antoine Pitrou <[email protected]>
Signed-off-by: Antoine Pitrou <[email protected]>
---
.../arrow/compute/kernels/hash_aggregate_pivot.cc | 6 +++--
python/pyarrow/tests/test_table.py | 26 ++++++++++++++++++++++
2 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/cpp/src/arrow/compute/kernels/hash_aggregate_pivot.cc
b/cpp/src/arrow/compute/kernels/hash_aggregate_pivot.cc
index e9351e1136..c3dc070e4f 100644
--- a/cpp/src/arrow/compute/kernels/hash_aggregate_pivot.cc
+++ b/cpp/src/arrow/compute/kernels/hash_aggregate_pivot.cc
@@ -452,9 +452,11 @@ const FunctionDoc hash_pivot_doc{
} // namespace
void RegisterHashAggregatePivot(FunctionRegistry* registry) {
+ static const auto default_pivot_options = PivotWiderOptions::Defaults();
+
{
- auto func = std::make_shared<HashAggregateFunction>("hash_pivot_wider",
- Arity::Ternary(),
hash_pivot_doc);
+ auto func = std::make_shared<HashAggregateFunction>(
+ "hash_pivot_wider", Arity::Ternary(), hash_pivot_doc,
&default_pivot_options);
for (auto key_type : BaseBinaryTypes()) {
// Anything that scatter() (i.e. take()) accepts can be passed as values
auto sig = KernelSignature::Make(
diff --git a/python/pyarrow/tests/test_table.py
b/python/pyarrow/tests/test_table.py
index 180ae7b4c1..a9fffc2665 100644
--- a/python/pyarrow/tests/test_table.py
+++ b/python/pyarrow/tests/test_table.py
@@ -2975,6 +2975,32 @@ def test_table_group_by_first():
assert result.equals(expected)
[email protected]
+def test_table_group_by_pivot_wider():
+ table = pa.table({'group': [1, 2, 3, 1, 2, 3],
+ 'key': ['h', 'h', 'h', 'w', 'w', 'w'],
+ 'value': [10, 20, 30, 40, 50, 60]})
+
+ with pytest.raises(ValueError, match='accepts 3 arguments but 2 passed'):
+ table.group_by("group").aggregate([("key", "pivot_wider")])
+
+ # GH-45739: calling hash_pivot_wider without options shouldn't crash
+ # (even though it's not very useful as key_names=[])
+ result = table.group_by("group").aggregate([(("key", "value"),
"pivot_wider")])
+ expected = pa.table({'group': [1, 2, 3],
+ 'key_value_pivot_wider': [{}, {}, {}]})
+ assert result.equals(expected)
+
+ options = pc.PivotWiderOptions(key_names=('h', 'w'))
+ result = table.group_by("group").aggregate(
+ [(("key", "value"), "pivot_wider", options)])
+ expected = pa.table(
+ {'group': [1, 2, 3],
+ 'key_value_pivot_wider': [
+ {'h': 10, 'w': 40}, {'h': 20, 'w': 50}, {'h': 30, 'w': 60}]})
+ assert result.equals(expected)
+
+
def test_table_to_recordbatchreader():
table = pa.Table.from_pydict({'x': [1, 2, 3]})
reader = table.to_reader()