Skip to main content

datafusion_physical_expr/
async_scalar_function.rs

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
18use crate::ScalarFunctionExpr;
19use arrow::array::RecordBatch;
20use arrow::compute::concat;
21use arrow::datatypes::{DataType, Field, FieldRef, Schema};
22use datafusion_common::Result;
23use datafusion_common::config::ConfigOptions;
24use datafusion_common::{internal_err, not_impl_err};
25use datafusion_expr::ScalarFunctionArgs;
26use datafusion_expr::async_udf::AsyncScalarUDF;
27use datafusion_expr_common::columnar_value::ColumnarValue;
28use datafusion_physical_expr_common::physical_expr::PhysicalExpr;
29use std::fmt::Display;
30use std::hash::{Hash, Hasher};
31use std::sync::Arc;
32
33/// Wrapper around a scalar function that can be evaluated asynchronously
34#[derive(Debug, Clone, Eq)]
35pub struct AsyncFuncExpr {
36    /// The name of the output column this function will generate
37    pub name: String,
38    /// The actual function (always `ScalarFunctionExpr`)
39    pub func: Arc<dyn PhysicalExpr>,
40    /// The field that this function will return
41    return_field: FieldRef,
42}
43
44impl Display for AsyncFuncExpr {
45    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
46        write!(f, "async_expr(name={}, expr={})", self.name, self.func)
47    }
48}
49
50impl PartialEq for AsyncFuncExpr {
51    fn eq(&self, other: &Self) -> bool {
52        self.name == other.name && self.func == Arc::clone(&other.func)
53    }
54}
55
56impl Hash for AsyncFuncExpr {
57    fn hash<H: Hasher>(&self, state: &mut H) {
58        self.name.hash(state);
59        self.func.as_ref().hash(state);
60    }
61}
62
63impl AsyncFuncExpr {
64    /// create a new AsyncFuncExpr
65    pub fn try_new(
66        name: impl Into<String>,
67        func: Arc<dyn PhysicalExpr>,
68        schema: &Schema,
69    ) -> Result<Self> {
70        let Some(_) = func.downcast_ref::<ScalarFunctionExpr>() else {
71            return internal_err!(
72                "unexpected function type, expected ScalarFunctionExpr, got: {:?}",
73                func
74            );
75        };
76
77        let return_field = func.return_field(schema)?;
78        Ok(Self {
79            name: name.into(),
80            func,
81            return_field,
82        })
83    }
84
85    /// return the name of the output column
86    pub fn name(&self) -> &str {
87        &self.name
88    }
89
90    /// Return the output field generated by evaluating this function
91    #[deprecated(since = "55.0.0", note = "Use return_field instead")]
92    pub fn field(&self, _input_schema: &Schema) -> Result<Field> {
93        Ok(self.return_field.as_ref().clone().with_name(&self.name))
94    }
95
96    /// Return the ideal batch size for this function
97    pub fn ideal_batch_size(&self) -> Result<Option<usize>> {
98        if let Some(expr) = self.func.downcast_ref::<ScalarFunctionExpr>()
99            && let Some(udf) = expr.fun().inner().downcast_ref::<AsyncScalarUDF>()
100        {
101            return Ok(udf.ideal_batch_size());
102        }
103        not_impl_err!("Can't get ideal_batch_size from {:?}", self.func)
104    }
105
106    /// This (async) function is called for each record batch to evaluate the LLM expressions
107    ///
108    /// The output is the output of evaluating the async expression and the input record batch
109    pub async fn invoke_with_args(
110        &self,
111        batch: &RecordBatch,
112        config_options: Arc<ConfigOptions>,
113    ) -> Result<ColumnarValue> {
114        let Some(scalar_function_expr) = self.func.downcast_ref::<ScalarFunctionExpr>()
115        else {
116            return internal_err!(
117                "unexpected function type, expected ScalarFunctionExpr, got: {:?}",
118                self.func
119            );
120        };
121
122        let Some(async_udf) = scalar_function_expr
123            .fun()
124            .inner()
125            .downcast_ref::<AsyncScalarUDF>()
126        else {
127            return not_impl_err!(
128                "Don't know how to evaluate async function: {:?}",
129                scalar_function_expr
130            );
131        };
132
133        let arg_fields = scalar_function_expr
134            .args()
135            .iter()
136            .map(|e| e.return_field(batch.schema_ref()))
137            .collect::<Result<Vec<_>>>()?;
138
139        let mut result_batches = vec![];
140        if let Some(ideal_batch_size) = self.ideal_batch_size()? {
141            let mut remainder = batch.clone();
142            while remainder.num_rows() > 0 {
143                let size = if ideal_batch_size > remainder.num_rows() {
144                    remainder.num_rows()
145                } else {
146                    ideal_batch_size
147                };
148
149                let current_batch = remainder.slice(0, size); // get next 10 rows
150                remainder = remainder.slice(size, remainder.num_rows() - size);
151                let args = scalar_function_expr
152                    .args()
153                    .iter()
154                    .map(|e| e.evaluate(&current_batch))
155                    .collect::<Result<Vec<_>>>()?;
156                result_batches.push(
157                    async_udf
158                        .invoke_async_with_args(ScalarFunctionArgs {
159                            args,
160                            arg_fields: arg_fields.clone(),
161                            number_rows: current_batch.num_rows(),
162                            return_field: Arc::clone(&self.return_field),
163                            config_options: Arc::clone(&config_options),
164                        })
165                        .await?,
166                );
167            }
168        } else {
169            let args = scalar_function_expr
170                .args()
171                .iter()
172                .map(|e| e.evaluate(batch))
173                .collect::<Result<Vec<_>>>()?;
174
175            result_batches.push(
176                async_udf
177                    .invoke_async_with_args(ScalarFunctionArgs {
178                        args: args.to_vec(),
179                        arg_fields,
180                        number_rows: batch.num_rows(),
181                        return_field: Arc::clone(&self.return_field),
182                        config_options: Arc::clone(&config_options),
183                    })
184                    .await?,
185            );
186        }
187
188        let datas = result_batches
189            .into_iter()
190            .map(|cv| match cv {
191                ColumnarValue::Array(arr) => Ok(arr),
192                ColumnarValue::Scalar(scalar) => Ok(scalar.to_array_of_size(1)?),
193            })
194            .collect::<Result<Vec<_>>>()?;
195
196        // Get references to the arrays as dyn Array to call concat
197        let dyn_arrays = datas
198            .iter()
199            .map(|arr| arr as &dyn arrow::array::Array)
200            .collect::<Vec<_>>();
201        let result_array = concat(&dyn_arrays)?;
202        Ok(ColumnarValue::Array(result_array))
203    }
204}
205
206impl PhysicalExpr for AsyncFuncExpr {
207    fn data_type(&self, input_schema: &Schema) -> Result<DataType> {
208        self.func.data_type(input_schema)
209    }
210
211    fn return_field(&self, _input_schema: &Schema) -> Result<FieldRef> {
212        Ok(Arc::new(
213            self.return_field.as_ref().clone().with_name(&self.name),
214        ))
215    }
216
217    fn nullable(&self, input_schema: &Schema) -> Result<bool> {
218        self.func.nullable(input_schema)
219    }
220
221    fn evaluate(&self, _batch: &RecordBatch) -> Result<ColumnarValue> {
222        // TODO: implement this for scalar value input
223        not_impl_err!("AsyncFuncExpr.evaluate")
224    }
225
226    fn children(&self) -> Vec<&Arc<dyn PhysicalExpr>> {
227        self.func.children()
228    }
229
230    fn with_new_children(
231        self: Arc<Self>,
232        children: Vec<Arc<dyn PhysicalExpr>>,
233    ) -> Result<Arc<dyn PhysicalExpr>> {
234        let new_func = Arc::clone(&self.func).with_new_children(children)?;
235        Ok(Arc::new(AsyncFuncExpr {
236            name: self.name.clone(),
237            func: new_func,
238            return_field: Arc::clone(&self.return_field),
239        }))
240    }
241
242    fn fmt_sql(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
243        write!(f, "{}", self.func)
244    }
245}