Skip to content

implement short_circuits function for ScalarUDFImpl trait #10168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion datafusion/expr/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions datafusion/expr/src/udf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// Returns true if some of this `exprs` subexpressions may not be evaluated
/// Returns true if some of this `exprs` subexpressions may not be evaluated

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blank line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

got it

/// 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
Expand Down
4 changes: 4 additions & 0 deletions datafusion/functions/src/math/coalesce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ impl ScalarUDFImpl for CoalesceFunc {
Ok(result)
}
}

fn short_circuits(&self) -> bool {
true
}
}

#[cfg(test)]
Expand Down