Skip to content

Commit 6bc709c

Browse files
Lordwormsccciudatu
authored andcommitted
implement short_circuits function for ScalarUDFImpl trait (apache#10168)
* implement short_circuits function for ScalarUDFImpl trait * finish
1 parent 63f113e commit 6bc709c

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

datafusion/expr/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ impl Expr {
12661266
pub fn short_circuits(&self) -> bool {
12671267
match self {
12681268
Expr::ScalarFunction(ScalarFunction { func_def, .. }) => {
1269-
matches!(func_def, ScalarFunctionDefinition::UDF(fun) if fun.name().eq("coalesce"))
1269+
matches!(func_def, ScalarFunctionDefinition::UDF(fun) if fun.short_circuits())
12701270
}
12711271
Expr::BinaryExpr(BinaryExpr { op, .. }) => {
12721272
matches!(op, Operator::And | Operator::Or)

datafusion/expr/src/udf.rs

+12
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,11 @@ impl ScalarUDF {
193193
pub fn monotonicity(&self) -> Result<Option<FuncMonotonicity>> {
194194
self.inner.monotonicity()
195195
}
196+
197+
/// Get the circuits of inner implementation
198+
pub fn short_circuits(&self) -> bool {
199+
self.inner.short_circuits()
200+
}
196201
}
197202

198203
impl<F> From<F> for ScalarUDF
@@ -376,6 +381,13 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
376381
) -> Result<ExprSimplifyResult> {
377382
Ok(ExprSimplifyResult::Original(args))
378383
}
384+
385+
/// Returns true if some of this `exprs` subexpressions may not be evaluated
386+
/// and thus any side effects (like divide by zero) may not be encountered
387+
/// Setting this to true prevents certain optimizations such as common subexpression elimination
388+
fn short_circuits(&self) -> bool {
389+
false
390+
}
379391
}
380392

381393
/// ScalarUDF that adds an alias to the underlying function. It is better to

datafusion/functions/src/math/coalesce.rs

+4
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ impl ScalarUDFImpl for CoalesceFunc {
120120
Ok(result)
121121
}
122122
}
123+
124+
fn short_circuits(&self) -> bool {
125+
true
126+
}
123127
}
124128

125129
#[cfg(test)]

0 commit comments

Comments
 (0)