vibhatha commented on code in PR #12590: URL: https://github.com/apache/arrow/pull/12590#discussion_r852577029
########## python/pyarrow/tests/test_udf.py: ########## @@ -0,0 +1,447 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + + +import pytest + +import pyarrow as pa +from pyarrow import compute as pc +from pyarrow.compute import register_scalar_function +from pyarrow.compute import InputType + + +unary_doc = {"summary": "add function", + "description": "test add function"} + + +def unary_function(scalar1): + return pc.call_function("add", [scalar1, 1]) + + +binary_doc = {"summary": "y=mx", + "description": "find y from y = mx"} + + +def binary_function(m, x): + return pc.call_function("multiply", [m, x]) + + +ternary_doc = {"summary": "y=mx+c", + "description": "find y from y = mx + c"} + + +def ternary_function(m, x, c): + mx = pc.call_function("multiply", [m, x]) + return pc.call_function("add", [mx, c]) + + +varargs_doc = {"summary": "z=ax+by+c", + "description": "find z from z = ax + by + c" + } + + +def varargs_function(*values): + base_val = values[:2] + res = pc.call_function("add", base_val) + for other_val in values[2:]: + res = pc.call_function("add", [res, other_val]) + return res + + +def test_scalar_udf_function_with_scalar_valued_functions(): + function_names = [ + "scalar_y=x+k", + "scalar_y=mx", + "scalar_y=mx+c", + "scalar_z=ax+by+c", + ] + + function_input_types = [ + { + "scalar": InputType.scalar(pa.int64()), + }, + { + "scalar1": InputType.scalar(pa.int64()), + "scalar2": InputType.scalar(pa.int64()), + }, + { + "scalar1": InputType.scalar(pa.int64()), + "scalar2": InputType.scalar(pa.int64()), + "scalar3": InputType.scalar(pa.int64()), + }, + { + "scalar1": InputType.scalar(pa.int64()), + "scalar2": InputType.scalar(pa.int64()), + "scalar3": InputType.scalar(pa.int64()), + "scalar4": InputType.scalar(pa.int64()), + "scalar5": InputType.scalar(pa.int64()), + }, + ] + + function_output_types = [ + pa.int64(), + pa.int64(), + pa.int64(), + pa.int64(), + ] + + function_docs = [ + unary_doc, + binary_doc, + ternary_doc, + varargs_doc + ] + + functions = [ + unary_function, + binary_function, + ternary_function, + varargs_function + ] + + function_inputs = [ + [ + pa.scalar(10, pa.int64()) + ], + [ + pa.scalar(10, pa.int64()), + pa.scalar(2, pa.int64()) + ], + [ + pa.scalar(10, pa.int64()), + pa.scalar(2, pa.int64()), + pa.scalar(5, pa.int64()) + ], + [ + pa.scalar(2, pa.int64()), + pa.scalar(10, pa.int64()), + pa.scalar(3, pa.int64()), + pa.scalar(20, pa.int64()), + pa.scalar(5, pa.int64()) + ], + ] + + for name, \ + in_types, \ + out_type, \ + doc, \ + function, \ + input in zip(function_names, + function_input_types, + function_output_types, + function_docs, + functions, + function_inputs): + expected_output = function(*input) + register_scalar_function( + name, doc, in_types, out_type, function) + + func = pc.get_function(name) + assert func.name == name + + result = pc.call_function(name, input) + assert result == expected_output + + +def test_scalar_udf_with_array_data_functions(): + function_names = [ + "array_y=x+k", + "array_y=mx", + "array_y=mx+c", + "array_z=ax+by+c" + ] + + function_input_types = [ + { + "array": InputType.array(pa.int64()), + }, + { + "array1": InputType.array(pa.int64()), + "array2": InputType.array(pa.int64()), + }, + { + "array1": InputType.array(pa.int64()), + "array2": InputType.array(pa.int64()), + "array3": InputType.array(pa.int64()), + }, + { + "array1": InputType.array(pa.int64()), + "array2": InputType.array(pa.int64()), + "array3": InputType.array(pa.int64()), + "array4": InputType.array(pa.int64()), + "array5": InputType.array(pa.int64()), + }, + ] + + function_output_types = [ + pa.int64(), + pa.int64(), + pa.int64(), + pa.int64(), + ] + + function_docs = [ + unary_doc, + binary_doc, + ternary_doc, + varargs_doc + ] + + functions = [ + unary_function, + binary_function, + ternary_function, + varargs_function + ] + + function_inputs = [ + [ + pa.array([10, 20], pa.int64()) + ], + [ + pa.array([10, 20], pa.int64()), + pa.array([2, 4], pa.int64()) + ], + [ + pa.array([10, 20], pa.int64()), + pa.array([2, 4], pa.int64()), + pa.array([5, 10], pa.int64()) + ], + [ + pa.array([2, 3], pa.int64()), + pa.array([10, 20], pa.int64()), + pa.array([3, 7], pa.int64()), + pa.array([20, 30], pa.int64()), + pa.array([5, 10], pa.int64()) + ] + ] + + for name, \ + in_types, \ + out_type, \ + doc, \ + function, \ + input in zip(function_names, + function_input_types, + function_output_types, + function_docs, + functions, + function_inputs): + expected_output = function(*input) + register_scalar_function( + name, doc, in_types, out_type, function) + + func = pc.get_function(name) + assert func.name == name + + result = pc.call_function(name, input) + assert result == expected_output + + +def test_udf_input(): + def unary_scalar_function(scalar): + return pc.call_function("add", [scalar, 1]) + + # validate function name + doc = { + "summary": "test udf input", + "description": "parameters are validated" + } + in_types = {"scalar": InputType.scalar(pa.int64())} + out_type = pa.int64() + with pytest.raises(TypeError): + register_scalar_function(None, doc, in_types, + out_type, unary_scalar_function) + + # validate function + with pytest.raises(TypeError, match="Object must be a callable"): + register_scalar_function("none_function", doc, in_types, + out_type, None) + + # validate output type + expected_expr = "out_type must be a DataType, not None" + with pytest.raises(TypeError, match=expected_expr): + register_scalar_function("output_function", doc, in_types, + None, unary_scalar_function) + + # validate input type + expected_expr = r'in_types must be a dictionary of InputType' + with pytest.raises(TypeError, match=expected_expr): + register_scalar_function("input_function", doc, None, + out_type, unary_scalar_function) Review Comment: I guess you're suggesting a test case for that too? Is it? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@arrow.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org