Skip to content
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

consider volatile function in simply_expression #13128

Merged
merged 10 commits into from
Nov 1, 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
73 changes: 69 additions & 4 deletions datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,8 +862,8 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
right,
}) if has_common_conjunction(&left, &right) => {
let lhs: IndexSet<Expr> = iter_conjunction_owned(*left).collect();
let (common, rhs): (Vec<_>, Vec<_>) =
iter_conjunction_owned(*right).partition(|e| lhs.contains(e));
let (common, rhs): (Vec<_>, Vec<_>) = iter_conjunction_owned(*right)
.partition(|e| lhs.contains(e) && !e.is_volatile());

let new_rhs = rhs.into_iter().reduce(and);
let new_lhs = lhs.into_iter().filter(|e| !common.contains(e)).reduce(and);
Expand Down Expand Up @@ -1682,8 +1682,8 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
}

fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> bool {
let lhs: HashSet<&Expr> = iter_conjunction(lhs).collect();
iter_conjunction(rhs).any(|e| lhs.contains(&e))
let lhs_set: HashSet<&Expr> = iter_conjunction(lhs).collect();
iter_conjunction(rhs).any(|e| lhs_set.contains(&e) && !e.is_volatile())
}

// TODO: We might not need this after defer pattern for Box is stabilized. https://github.com/rust-lang/rust/issues/87121
Expand Down Expand Up @@ -3978,4 +3978,69 @@ mod tests {
unimplemented!("not needed for tests")
}
}
#[derive(Debug)]
struct VolatileUdf {
signature: Signature,
}

impl VolatileUdf {
pub fn new() -> Self {
Self {
signature: Signature::exact(vec![], Volatility::Volatile),
}
}
}
impl ScalarUDFImpl for VolatileUdf {
fn as_any(&self) -> &dyn std::any::Any {
self
}

fn name(&self) -> &str {
"VolatileUdf"
}

fn signature(&self) -> &Signature {
&self.signature
}

fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {
Ok(DataType::Int16)
}
}
#[test]
fn test_optimize_volatile_conditions() {
let fun = Arc::new(ScalarUDF::new_from_impl(VolatileUdf::new()));
let rand = Expr::ScalarFunction(ScalarFunction::new_udf(fun, vec![]));
{
let expr = rand
.clone()
.eq(lit(0))
.or(col("column1").eq(lit(2)).and(rand.clone().eq(lit(0))));

assert_eq!(simplify(expr.clone()), expr);
}

{
let expr = col("column1")
.eq(lit(2))
.or(col("column1").eq(lit(2)).and(rand.clone().eq(lit(0))));

assert_eq!(simplify(expr), col("column1").eq(lit(2)));
Copy link
Contributor

Choose a reason for hiding this comment

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

It would seem to me this should still be the same thing -- namely that we shouldn't remove the rand() = 0 condition 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

However, this PR still seems like it is an improvement over what is on main so we could potentially fix this in a follow on PR

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could be something wrong here, let me double check

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think here should remove the rand() = 0 since we must fulfill the column1 = 2 first? according to the description in the issue? Don't know if I got it wrong....
image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also Happy Halloween!

Copy link
Contributor

Choose a reason for hiding this comment

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

Also Happy Halloween!

image

}

{
let expr = (col("column1").eq(lit(2)).and(rand.clone().eq(lit(0)))).or(col(
"column1",
)
.eq(lit(2))
.and(rand.clone().eq(lit(0))));

assert_eq!(
simplify(expr),
col("column1")
.eq(lit(2))
.and((rand.clone().eq(lit(0))).or(rand.clone().eq(lit(0))))
);
}
}
}
13 changes: 9 additions & 4 deletions datafusion/optimizer/src/simplify_expressions/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,21 @@ pub static POWS_OF_TEN: [i128; 38] = [

/// returns true if `needle` is found in a chain of search_op
/// expressions. Such as: (A AND B) AND C
pub fn expr_contains(expr: &Expr, needle: &Expr, search_op: Operator) -> bool {
fn expr_contains_inner(expr: &Expr, needle: &Expr, search_op: Operator) -> bool {
match expr {
Expr::BinaryExpr(BinaryExpr { left, op, right }) if *op == search_op => {
expr_contains(left, needle, search_op)
|| expr_contains(right, needle, search_op)
expr_contains_inner(left, needle, search_op)
|| expr_contains_inner(right, needle, search_op)
}
_ => expr == needle,
}
}

/// check volatile calls and return if expr contains needle
pub fn expr_contains(expr: &Expr, needle: &Expr, search_op: Operator) -> bool {
expr_contains_inner(expr, needle, search_op) && !needle.is_volatile()
}

/// Deletes all 'needles' or remains one 'needle' that are found in a chain of xor
/// expressions. Such as: A ^ (A ^ (B ^ A))
pub fn delete_xor_in_complex_expr(expr: &Expr, needle: &Expr, is_left: bool) -> Expr {
Expand Down Expand Up @@ -206,7 +211,7 @@ pub fn is_false(expr: &Expr) -> bool {

/// returns true if `haystack` looks like (needle OP X) or (X OP needle)
pub fn is_op_with(target_op: Operator, haystack: &Expr, needle: &Expr) -> bool {
matches!(haystack, Expr::BinaryExpr(BinaryExpr { left, op, right }) if op == &target_op && (needle == left.as_ref() || needle == right.as_ref()))
matches!(haystack, Expr::BinaryExpr(BinaryExpr { left, op, right }) if op == &target_op && (needle == left.as_ref() || needle == right.as_ref()) && !needle.is_volatile())
}

/// returns true if `not_expr` is !`expr` (not)
Expand Down
Loading