Skip to content

Commit 4fac70d

Browse files
authored
Minor: Disallow async function in lambdas (apache#22097)
## Which issue does this PR close? Part of apache#22091. ## Rationale for this change Current async udfs in lambdas fail with generic errors ## What changes are included in this PR? Report an explicit error when trying to create a lambda with async functions ## Are these changes tested? One sqllogictest added to assert the friendly error ## Are there any user-facing changes? What failed before still fail but with a better error
1 parent 7f2f78d commit 4fac70d

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

‎datafusion/physical-expr/src/expressions/lambda.rs‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use std::hash::Hash;
2121
use std::sync::Arc;
2222

2323
use crate::{
24+
ScalarFunctionExpr,
2425
expressions::{Column, LambdaVariable},
2526
physical_expr::PhysicalExpr,
2627
};
@@ -61,11 +62,16 @@ impl Hash for LambdaExpr {
6162
impl LambdaExpr {
6263
/// Create a new lambda expression with the given parameters and body
6364
pub fn try_new(params: Vec<String>, body: Arc<dyn PhysicalExpr>) -> Result<Self> {
64-
if all_unique(&params) {
65-
Ok(Self::new(params, body))
66-
} else {
67-
plan_err!("lambda params must be unique, got ({})", params.join(", "))
65+
if !all_unique(&params) {
66+
return plan_err!(
67+
"lambda params must be unique, got ({})",
68+
params.join(", ")
69+
);
6870
}
71+
72+
check_async_udf(&body)?;
73+
74+
Ok(Self::new(params, body))
6975
}
7076

7177
fn new(params: Vec<String>, body: Arc<dyn PhysicalExpr>) -> Self {
@@ -179,6 +185,8 @@ impl PhysicalExpr for LambdaExpr {
179185
);
180186
};
181187

188+
check_async_udf(body)?;
189+
182190
Ok(Arc::new(Self::new(self.params.clone(), Arc::clone(body))))
183191
}
184192

@@ -210,6 +218,20 @@ fn all_unique(params: &[String]) -> bool {
210218
}
211219
}
212220

221+
fn check_async_udf(body: &Arc<dyn PhysicalExpr>) -> Result<()> {
222+
if body.exists(|expr| {
223+
Ok(expr
224+
.downcast_ref::<ScalarFunctionExpr>()
225+
.is_some_and(|udf| udf.fun().as_async().is_some()))
226+
})? {
227+
return plan_err!(
228+
"Async functions in lambdas aren't supported, see https://github.com/apache/datafusion/issues/22091"
229+
);
230+
}
231+
232+
Ok(())
233+
}
234+
213235
#[cfg(test)]
214236
mod tests {
215237
use crate::expressions::{NoOp, lambda::lambda};

‎datafusion/sqllogictest/test_files/async_udf.slt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,13 @@ physical_plan
9999
01)ProjectionExec: expr=[__async_fn_0@1 as async_abs(data.x)]
100100
02)--AsyncFuncExec: async_expr=[async_expr(name=__async_fn_0, expr=async_abs(x@0))]
101101
03)----DataSourceExec: partitions=1, partition_sizes=[1]
102+
103+
statement ok
104+
set datafusion.sql_parser.dialect = databricks;
105+
106+
# Async udf can't be used in lambdas
107+
query error DataFusion error: Error during planning: Async functions in lambdas aren't supported, see https://github\.com/apache/datafusion/issues/22091
108+
select array_transform([1], v -> async_abs(v));
109+
110+
statement ok
111+
set datafusion.sql_parser.dialect = generic;

0 commit comments

Comments
 (0)