This is an automated email from the ASF dual-hosted git repository.

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new f5ab312165 implement short_circuits function for ScalarUDFImpl trait 
(#10168)
f5ab312165 is described below

commit f5ab3121651b1b1090abb1b038572588a8cf7789
Author: Lordworms <[email protected]>
AuthorDate: Mon Apr 22 15:20:01 2024 -0500

    implement short_circuits function for ScalarUDFImpl trait (#10168)
    
    * implement short_circuits function for ScalarUDFImpl trait
    
    * finish
---
 datafusion/expr/src/expr.rs               |  2 +-
 datafusion/expr/src/udf.rs                | 12 ++++++++++++
 datafusion/functions/src/math/coalesce.rs |  4 ++++
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/datafusion/expr/src/expr.rs b/datafusion/expr/src/expr.rs
index b2357e77b1..fb75a3cc7a 100644
--- a/datafusion/expr/src/expr.rs
+++ b/datafusion/expr/src/expr.rs
@@ -1266,7 +1266,7 @@ impl Expr {
     pub fn short_circuits(&self) -> bool {
         match self {
             Expr::ScalarFunction(ScalarFunction { func_def, .. }) => {
-                matches!(func_def, ScalarFunctionDefinition::UDF(fun) if 
fun.name().eq("coalesce"))
+                matches!(func_def, ScalarFunctionDefinition::UDF(fun) if 
fun.short_circuits())
             }
             Expr::BinaryExpr(BinaryExpr { op, .. }) => {
                 matches!(op, Operator::And | Operator::Or)
diff --git a/datafusion/expr/src/udf.rs b/datafusion/expr/src/udf.rs
index 069ac078a1..4557fe60a4 100644
--- a/datafusion/expr/src/udf.rs
+++ b/datafusion/expr/src/udf.rs
@@ -193,6 +193,11 @@ impl ScalarUDF {
     pub fn monotonicity(&self) -> Result<Option<FuncMonotonicity>> {
         self.inner.monotonicity()
     }
+
+    /// Get the circuits of inner implementation
+    pub fn short_circuits(&self) -> bool {
+        self.inner.short_circuits()
+    }
 }
 
 impl<F> From<F> for ScalarUDF
@@ -376,6 +381,13 @@ pub trait ScalarUDFImpl: Debug + Send + Sync {
     ) -> Result<ExprSimplifyResult> {
         Ok(ExprSimplifyResult::Original(args))
     }
+
+    /// Returns true if some of this `exprs` subexpressions may not be 
evaluated
+    /// and thus any side effects (like divide by zero) may not be encountered
+    /// Setting this to true prevents certain optimizations such as common 
subexpression elimination
+    fn short_circuits(&self) -> bool {
+        false
+    }
 }
 
 /// ScalarUDF that adds an alias to the underlying function. It is better to
diff --git a/datafusion/functions/src/math/coalesce.rs 
b/datafusion/functions/src/math/coalesce.rs
index 3e16113bbd..cc4a921c75 100644
--- a/datafusion/functions/src/math/coalesce.rs
+++ b/datafusion/functions/src/math/coalesce.rs
@@ -120,6 +120,10 @@ impl ScalarUDFImpl for CoalesceFunc {
             Ok(result)
         }
     }
+
+    fn short_circuits(&self) -> bool {
+        true
+    }
 }
 
 #[cfg(test)]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to