8000 UDAF process all state variables by timsaucer · Pull Request #799 · apache/datafusion-python · GitHub
[go: up one dir, main page]

Skip to content

UDAF process all state variables #799

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

Merged
merged 4 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/source/user-guide/common-operations/udf-and-udfa.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ Additionally the :py:func:`~datafusion.udf.AggregateUDF.udaf` function allows yo
import pyarrow.compute
import datafusion
from datafusion import col, udaf, Accumulator
from typing import List

class MyAccumulator(Accumulator):
"""
Expand All @@ -62,9 +63,9 @@ Additionally the :py:func:`~datafusion.udf.AggregateUDF.udaf` function allows yo
# not nice since pyarrow scalars can't be summed yet. This breaks on `None`
self._sum = pyarrow.scalar(self._sum.as_py() + pyarrow.compute.sum(values).as_py())

def merge(self, states: pyarrow.Array) -> None:
def merge(self, states: List[pyarrow.Array]) -> None:
# not nice since pyarrow scalars can't be summed yet. This breaks on `None`
self._sum = pyarrow.scalar(self._sum.as_py() + pyarrow.compute.sum(states).as_py())
self._sum = pyarrow.scalar(self._sum.as_py() + pyarrow.compute.sum(states[0]).as_py())

def state(self) -> pyarrow.Array:
return pyarrow.array([self._sum.as_py()])
Expand Down
4 changes: 2 additions & 2 deletions python/datafusion/tests/test_udaf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def update(self, values: pa.Array) -> None:
# This breaks on `None`
self._sum = pa.scalar(self._sum.as_py() + pc.sum(values).as_py())

def merge(self, states: pa.Array) -> None:
def merge(self, states: List[pa.Array]) -> None:
# Not nice since pyarrow scalars can't be summed yet.
# This breaks on `None`
self._sum = pa.scalar(self._sum.as_py() + pc.sum(states).as_py())
self._sum = pa.scalar(self._sum.as_py() + pc.sum(states[0]).as_py())

def evaluate(self) -> pa.Scalar:
return self._sum
Expand Down
2 changes: 1 addition & 1 deletion python/datafusion/udf.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def update(self, values: pyarrow.Array) -> None:
pass

@abstractmethod
def merge(self, states: pyarrow.Array) -> None:
def merge(self, states: List[pyarrow.Array]) -> None:
"""Merge a set of states."""
pass

Expand Down
19 changes: 11 additions & 8 deletions src/udaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,21 @@ impl Accumulator for RustAccumulator {

fn merge_batch(&mut self, states: &[ArrayRef]) -> Result<()> {
Python::with_gil(|py| {
let state = &states[0];

// 1. cast states to Pyarrow array
let state = state
.into_data()
.to_pyarrow(py)
.map_err(|e| DataFusionError::Execution(format!("{e}")))?;
// // 1. cast states to Pyarrow arrays
let py_states: Result<Vec<PyObject>> = states
.iter()
.map(|state| {
state
.into_data()
.to_pyarrow(py)
.map_err(|e| DataFusionError::Execution(format!("{e}")))
})
.collect();

// 2. call merge
self.accum
.bind(py)
.call_method1("merge", (state,))
.call_method1("merge", (py_states?,))
.map_err(|e| DataFusionError::Execution(format!("{e}")))?;

Ok(())
Expand Down
Loading
0