|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +from datafusion import udaf, SessionContext, Accumulator |
| 19 | +import pyarrow as pa |
| 20 | + |
| 21 | + |
| 22 | +# Define a user-defined aggregation function (UDAF) |
| 23 | +class MyAccumulator(Accumulator): |
| 24 | + """ |
| 25 | + Interface of a user-defined accumulation. |
| 26 | + """ |
| 27 | + |
| 28 | + def __init__(self): |
| 29 | + self._sum = pa.scalar(0.0) |
| 30 | + |
| 31 | + def update(self, values: pa.Array) -> None: |
| 32 | + # not nice since pyarrow scalars can't be summed yet. This breaks on `None` |
| 33 | + self._sum = pa.scalar( |
| 34 | + self._sum.as_py() + pa.compute.sum(values).as_py() |
| 35 | + ) |
| 36 | + |
| 37 | + def merge(self, states: pa.Array) -> None: |
| 38 | + # not nice since pyarrow scalars can't be summed yet. This breaks on `None` |
| 39 | + self._sum = pa.scalar( |
| 40 | + self._sum.as_py() + pa.compute.sum(states).as_py() |
| 41 | + ) |
| 42 | + |
| 43 | + def state(self) -> pa.Array: |
| 44 | + return pa.array([self._sum.as_py()]) |
| 45 | + |
| 46 | + def evaluate(self) -> pa.Scalar: |
| 47 | + return self._sum |
| 48 | + |
| 49 | + |
| 50 | +my_udaf = udaf( |
| 51 | + MyAccumulator, |
| 52 | + pa.float64(), |
| 53 | + pa.float64(), |
| 54 | + [pa.float64()], |
| 55 | + "stable", |
| 56 | + # This will be the name of the UDAF in SQL |
| 57 | + # If not specified it will by default the same as accumulator class name |
| 58 | + name="my_accumulator", |
| 59 | +) |
| 60 | + |
| 61 | +# Create a context |
| 62 | +ctx = SessionContext() |
| 63 | + |
| 64 | +# Create a datafusion DataFrame from a Python dictionary |
| 65 | +source_df = ctx.from_pydict({"a": [1, 1, 3], "b": [4, 5, 6]}) |
| 66 | +# Dataframe: |
| 67 | +# +---+---+ |
| 68 | +# | a | b | |
| 69 | +# +---+---+ |
| 70 | +# | 1 | 4 | |
| 71 | +# | 1 | 5 | |
| 72 | +# | 3 | 6 | |
| 73 | +# +---+---+ |
| 74 | + |
| 75 | +# Register UDF for use in SQL |
| 76 | +ctx.register_udaf(my_udaf) |
| 77 | + |
| 78 | +# Query the DataFrame using SQL |
| 79 | +table_name = ctx.catalog().database().names().pop() |
| 80 | +result_df = ctx.sql( |
| 81 | + f"select a, my_accumulator(b) as b_aggregated from {table_name} group by a order by a" |
| 82 | +) |
| 83 | +# Dataframe: |
| 84 | +# +---+--------------+ |
| 85 | +# | a | b_aggregated | |
| 86 | +# +---+--------------+ |
| 87 | +# | 1 | 9 | |
| 88 | +# | 3 | 6 | |
| 89 | +# +---+--------------+ |
| 90 | +assert result_df.to_pydict()["a"] == [1, 3] |
| 91 | +assert result_df.to_pydict()["b_aggregated"] == [9, 6] |
0 commit comments