|
| 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 | +import pyarrow as pa |
| 19 | +import pytest |
| 20 | + |
| 21 | +from datafusion import SessionContext, column, udwf, lit, functions as f |
| 22 | +from datafusion.udf import WindowEvaluator |
| 23 | + |
| 24 | + |
| 25 | +class ExponentialSmooth(WindowEvaluator): |
| 26 | + """Interface of a user-defined accumulation.""" |
| 27 | + |
| 28 | + def __init__(self) -> None: |
| 29 | + self.alpha = 0.9 |
| 30 | + |
| 31 | + def evaluate_all(self, values: pa.Array, num_rows: int) -> pa.Array: |
| 32 | + results = [] |
| 33 | + curr_value = 0.0 |
| 34 | + for idx in range(num_rows): |
| 35 | + if idx == 0: |
| 36 | + curr_value = values[idx].as_py() |
| 37 | + else: |
| 38 | + curr_value = values[idx].as_py() * self.alpha + curr_value * ( |
| 39 | + 1.0 - self.alpha |
| 40 | + ) |
| 41 | + results.append(curr_value) |
| 42 | + |
| 43 | + return pa.array(results) |
| 44 | + |
| 45 | + |
| 46 | +class NotSubclassOfWindowEvaluator: |
| 47 | + pass |
| 48 | + |
| 49 | + |
| 50 | +@pytest.fixture |
| 51 | +def df(): |
| 52 | + ctx = SessionContext() |
| 53 | + |
| 54 | + # create a RecordBatch and a new DataFrame from it |
| 55 | + batch = pa.RecordBatch.from_arrays( |
| 56 | + [ |
| 57 | + pa.array([0, 1, 2, 3, 4, 5, 6]), |
| 58 | + pa.array([7, 4, 3, 8, 9, 1, 6]), |
| 59 | + pa.array(["A", "A", "A", "A", "B", "B", "B"]), |
| 60 | + ], |
| 61 | + names=["a", "b", "c"], |
| 62 | + ) |
| 63 | + return ctx.create_dataframe([[batch]]) |
| 64 | + |
| 65 | + |
| 66 | +def test_udwf_errors(df): |
| 67 | + with pytest.raises(TypeError): |
| 68 | + udwf( |
| 69 | + NotSubclassOfWindowEvaluator, |
| 70 | + pa.float64(), |
| 71 | + pa.float64(), |
| 72 | + volatility="immutable", |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +smooth = udwf( |
| 77 | + ExponentialSmooth, |
| 78 | + pa.float64(), |
| 79 | + pa.float64(), |
| 80 | + volatility="immutable", |
| 81 | +) |
| 82 | + |
| 83 | +data_test_udwf_functions = [ |
| 84 | + ("smooth_udwf", smooth(column("a")), [0, 0.9, 1.89, 2.889, 3.889, 4.889, 5.889]), |
| 85 | + ( |
| 86 | + "partitioned_udwf", |
| 87 | + smooth(column("a")).partition_by(column("c")).build(), |
| 88 | + [0, 0.9, 1.89, 2.889, 4.0, 4.9, 5.89], |
| 89 | + ), |
| 90 | + ( |
| 91 | + "ordered_udwf", |
| 92 | + smooth(column("a")).order_by(column("b")).build(), |
| 93 | + [0.551, 1.13, 2.3, 2.755, 3.876, 5.0, 5.513], |
| 94 | + ), |
| 95 | +] |
| 96 | + |
| 97 | + |
| 98 | +@pytest.mark.parametrize("name,expr,expected", data_test_udwf_functions) |
| 99 | +def test_udwf_functions(df, name, expr, expected): |
| 100 | + df = df.select("a", f.round(expr, lit(3)).alias(name)) |
| 101 | + |
| 102 | + # execute and collect the first (and only) batch |
| 103 | + result = df.sort(column("a")).select(column(name)).collect()[0] |
| 104 | + |
| 105 | + assert result.column(0) == pa.array(expected) |
0 commit comments