MCPcopy Create free account
hub / github.com/apache/datafusion / invoke_async_with_args

Method invoke_async_with_args

datafusion-examples/examples/udf/async_udf.rs:194–233  ·  view source on GitHub ↗

The `invoke_async_with_args` method is similar to `invoke_with_args`, but it returns a `Future` that resolves to the result. Since this signature is `async`, it can do any `async` operations, such as network requests. This method is run on the same tokio `Runtime` that is processing the query, so you may wish to make actual network requests on a different `Runtime`, as explained in the `thread_po

(
        &self,
        args: ScalarFunctionArgs,
    )

Source from the content-addressed store, hash-verified

192 /// on a different `Runtime`, as explained in the `thread_pools.rs` example
193 /// in this directory.
194 async fn invoke_async_with_args(
195 &self,
196 args: ScalarFunctionArgs,
197 ) -> Result<ColumnarValue> {
198 // in a real UDF you would likely want to special case constant
199 // arguments to improve performance, but this example converts the
200 // arguments to arrays for simplicity.
201 let args = ColumnarValue::values_to_arrays(&args.args)?;
202 let [content_column, question_column] = take_function_args(self.name(), args)?;
203
204 // In a real function, you would use a library such as `reqwest` here to
205 // make an async HTTP request. Credentials and other configurations can
206 // be supplied via the `ConfigOptions` parameter.
207
208 // In this example, we will simulate the LLM response by comparing the two
209 // input arguments using some static strings
210 let content_column = as_string_view_array(&content_column)?;
211 let question_column = as_string_view_array(&question_column)?;
212
213 let result_array: BooleanArray = content_column
214 .iter()
215 .zip(question_column.iter())
216 .map(|(a, b)| {
217 // If either value is null, return None
218 let a = a?;
219 let b = b?;
220 // Simulate an LLM response by checking the arguments to some
221 // hardcoded conditions.
222 if a.contains("cat") && b.contains("furry")
223 || a.contains("dog") && b.contains("furry")
224 {
225 Some(true)
226 } else {
227 Some(false)
228 }
229 })
230 .collect();
231
232 Ok(ColumnarValue::from(Arc::new(result_array) as ArrayRef))
233 }
234}

Callers

nothing calls this directly

Calls 9

values_to_arraysFunction · 0.85
take_function_argsFunction · 0.85
as_string_view_arrayFunction · 0.85
newFunction · 0.85
collectMethod · 0.80
nameMethod · 0.45
mapMethod · 0.45
iterMethod · 0.45
containsMethod · 0.45

Tested by

no test coverage detected