You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@datafusion.apache.org by "Lordworms (via GitHub)" <gi...@apache.org> on 2024/10/26 20:55:06 UTC
[PR] consider volatile function in simply_expression [datafusion]
Lordworms opened a new pull request, #13128:
URL: https://github.com/apache/datafusion/pull/13128
## Which issue does this PR close?
<!--
We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
-->
Closes #13060
## Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
-->
## What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
-->
## Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
-->
## Are there any user-facing changes?
<!--
If there are user-facing changes then we may require documentation to be updated before approving the PR.
-->
<!--
If there are any breaking changes to public APIs, please add the `api change` label.
-->
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1818561682
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -835,40 +835,38 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
left,
op: Or,
right,
- }) if expr_contains(&left, &right, Or) => Transformed::yes(*left),
+ }) if expr_contains(&left, &right, Or)? => Transformed::yes(*left),
// A OR (..A..) --> (..A..)
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if expr_contains(&right, &left, Or) => Transformed::yes(*right),
+ }) if expr_contains(&right, &left, Or)? => Transformed::yes(*right),
// A OR (A AND B) --> A
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if is_op_with(And, &right, &left) => Transformed::yes(*left),
+ }) if is_op_with(And, &right, &left)? => Transformed::yes(*left),
// (A AND B) OR A --> A
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if is_op_with(And, &left, &right) => Transformed::yes(*right),
+ }) if is_op_with(And, &left, &right)? => Transformed::yes(*right),
// Eliminate common factors in conjunctions e.g
// (A AND B) OR (A AND C) -> A AND (B OR C)
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if has_common_conjunction(&left, &right) => {
+ }) if has_common_conjunction(&left, &right)? => {
Review Comment:
That's weird and seems incorrect to me. BTW, this is what Spark does:
```
scala> sql("select * from t where column1 = 2 AND rand() = 0 OR column1 = 2 AND rand() = 0").explain(true);
== Parsed Logical Plan ==
'Project [*]
+- 'Filter ((('column1 = 2) AND ('rand() = 0)) OR (('column1 = 2) AND ('rand() = 0)))
+- 'UnresolvedRelation [t], [], false
== Analyzed Logical Plan ==
column1: int, column2: int
Project [column1#0, column2#1]
+- Filter (((column1#0 = 2) AND (rand(-1409433140814249342) = cast(0 as double))) OR ((column1#0 = 2) AND (rand(2383542950853957003) = cast(0 as double))))
+- SubqueryAlias spark_catalog.default.t
+- HiveTableRelation [`spark_catalog`.`default`.`t`, org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe, Data Cols: [column1#0, column2#1], Partition Cols: []]
== Optimized Logical Plan ==
Filter (isnotnull(column1#0) AND ((column1#0 = 2) AND ((rand(-1409433140814249342) = 0.0) OR (rand(2383542950853957003) = 0.0))))
+- HiveTableRelation [`spark_catalog`.`default`.`t`, org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe, Data Cols: [column1#0, column2#1], Partition Cols: []]
== Physical Plan ==
*(1) Filter ((isnotnull(column1#0) AND (column1#0 = 2)) AND ((rand(-1409433140814249342) = 0.0) OR (rand(2383542950853957003) = 0.0)))
+- Scan hive spark_catalog.default.t [column1#0, column2#1], HiveTableRelation [`spark_catalog`.`default`.`t`, org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe, Data Cols: [column1#0, column2#1], Partition Cols: []]
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#issuecomment-2452351373
Thanks again @Lordworms and @eejbyfeldt
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "eejbyfeldt (via GitHub)" <gi...@apache.org>.
eejbyfeldt commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1822606751
##########
datafusion/sqllogictest/test_files/explain.slt:
##########
@@ -411,3 +411,40 @@ logical_plan
physical_plan
01)ProjectionExec: expr=[{c0:1,c1:2.3,c2:abc} as struct(Int64(1),Float64(2.3),Utf8("abc"))]
02)--PlaceholderRowExec
+
+
+
+
+query TT
Review Comment:
Any reason for not adding these as unit tests in the `expr_simplifier` module instead?
They feel quite output place in file called `explain.slt`.
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1681,9 +1679,15 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
}
}
-fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> bool {
+fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> Result<bool, DataFusionError> {
let lhs: HashSet<&Expr> = iter_conjunction(lhs).collect();
- iter_conjunction(rhs).any(|e| lhs.contains(&e))
+ iter_conjunction(rhs).try_fold(false, |acc, e| {
Review Comment:
> Maybe we can consider changing the return type of `Expr::is_volatile()` in a follow-up PR.
I think this PR would be much cleaner if we fixed the signature of that first. But I guess the important thing is that we do fix it before it spreads keeps spreading.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "Lordworms (via GitHub)" <gi...@apache.org>.
Lordworms commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1825290324
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -3980,4 +3994,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)));
Review Comment:
Also Happy Halloween!
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "Lordworms (via GitHub)" <gi...@apache.org>.
Lordworms commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1825276620
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -3980,4 +3994,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)));
Review Comment:
Could be something wrong here, let me double check
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1818015781
##########
datafusion/optimizer/src/simplify_expressions/utils.rs:
##########
@@ -341,3 +355,49 @@ pub fn distribute_negation(expr: Expr) -> Expr {
_ => Expr::Negative(Box::new(expr)),
}
}
+
Review Comment:
Do we need this? We already have an `Expr::is_volatile()` API.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#issuecomment-2452223556
I merged up from main to resolve a logical conflict with this PR
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1826098143
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -3980,4 +3994,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)));
Review Comment:
> Also Happy Halloween!

--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "Lordworms (via GitHub)" <gi...@apache.org>.
Lordworms commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1825280278
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -3980,4 +3994,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)));
Review Comment:
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....
<img width="898" alt="image" src="https://github.com/user-attachments/assets/3b7c9175-b1e3-4901-b22a-6cedca231bab">
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1818577627
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1681,9 +1679,15 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
}
}
-fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> bool {
+fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> Result<bool, DataFusionError> {
let lhs: HashSet<&Expr> = iter_conjunction(lhs).collect();
- iter_conjunction(rhs).any(|e| lhs.contains(&e))
+ iter_conjunction(rhs).try_fold(false, |acc, e| {
Review Comment:
This still looks a bit ugly. I mean, we just look for the first non-volatile expression and don't want to iterate over all expressions.
Actually, I think I made a mistake when I added `is_volatile()` with return type `Result<bool>`:
https://github.com/apache/datafusion/blob/e22d23113f549a90483bbf161add84eba08510fd/datafusion/expr/src/expr.rs#L1581-L1583
As the closure we provide to `exists()` can't return an error it would be safe to unwrap the result and return only `bool`.
@alamb, do you think we can fix `Expr::is_volatile()`, it would be an API breaking change though, to make it simpler and so we can keep using `any()` here?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1820598604
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1681,9 +1679,15 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
}
}
-fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> bool {
+fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> Result<bool, DataFusionError> {
let lhs: HashSet<&Expr> = iter_conjunction(lhs).collect();
- iter_conjunction(rhs).any(|e| lhs.contains(&e))
+ iter_conjunction(rhs).try_fold(false, |acc, e| {
Review Comment:
Looks good to me, thanks for the fix.
Maybe we can consider changing the return type of `Expr::is_volatile()` in a follow-up PR.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "Lordworms (via GitHub)" <gi...@apache.org>.
Lordworms commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1823358071
##########
datafusion/sqllogictest/test_files/explain.slt:
##########
@@ -411,3 +411,40 @@ logical_plan
physical_plan
01)ProjectionExec: expr=[{c0:1,c1:2.3,c2:abc} as struct(Int64(1),Float64(2.3),Utf8("abc"))]
02)--PlaceholderRowExec
+
+
+
+
+query TT
Review Comment:
Sure
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#issuecomment-2452211702
I double checked that this PR compiles after we merge up from main, so let's get it in
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #13128:
URL: https://github.com/apache/datafusion/pull/13128
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1825040266
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1681,9 +1690,14 @@ 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))
+fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> Result<bool, DataFusionError> {
+ let lhs_set: HashSet<&Expr> = iter_conjunction(lhs).collect();
+ for e in iter_conjunction(rhs) {
+ if lhs_set.contains(&e) && !e.is_volatile()? {
Review Comment:
I looked a little more at `Expr::is_volatile` and I actually think it can't return any errors. If we changed the signature I think this PR would be significantly simpler
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1826063930
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1681,9 +1679,15 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
}
}
-fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> bool {
+fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> Result<bool, DataFusionError> {
let lhs: HashSet<&Expr> = iter_conjunction(lhs).collect();
- iter_conjunction(rhs).any(|e| lhs.contains(&e))
+ iter_conjunction(rhs).try_fold(false, |acc, e| {
Review Comment:
I changed the signature in https://github.com/apache/datafusion/pull/13206 which caused a logical conflict in this PR (failed to compile) and then I updated this PR to use the new signature
As @eejbyfeldt predicated, the PR became much cleaner
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1818015688
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -835,40 +835,38 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
left,
op: Or,
right,
- }) if expr_contains(&left, &right, Or) => Transformed::yes(*left),
+ }) if expr_contains(&left, &right, Or)? => Transformed::yes(*left),
// A OR (..A..) --> (..A..)
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if expr_contains(&right, &left, Or) => Transformed::yes(*right),
+ }) if expr_contains(&right, &left, Or)? => Transformed::yes(*right),
// A OR (A AND B) --> A
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if is_op_with(And, &right, &left) => Transformed::yes(*left),
+ }) if is_op_with(And, &right, &left)? => Transformed::yes(*left),
// (A AND B) OR A --> A
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if is_op_with(And, &left, &right) => Transformed::yes(*right),
+ }) if is_op_with(And, &left, &right)? => Transformed::yes(*right),
// Eliminate common factors in conjunctions e.g
// (A AND B) OR (A AND C) -> A AND (B OR C)
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if has_common_conjunction(&left, &right) => {
+ }) if has_common_conjunction(&left, &right)? => {
Review Comment:
This doesn't seem correct. It is ok to just check the presence of common non-volatile expressions in `has_common_conjunction()` to trigger simplification, but you need to deal with volatile expressions during simplification too and don't extract them as common.
E.g. can you please add a test like `column1 = 2 AND random() = 0 OR column1 = 2 AND random() = 0`?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "Lordworms (via GitHub)" <gi...@apache.org>.
Lordworms commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1819837343
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1681,9 +1679,15 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
}
}
-fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> bool {
+fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> Result<bool, DataFusionError> {
let lhs: HashSet<&Expr> = iter_conjunction(lhs).collect();
- iter_conjunction(rhs).any(|e| lhs.contains(&e))
+ iter_conjunction(rhs).try_fold(false, |acc, e| {
Review Comment:
I have refactored it, Thanks for your review
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1820598604
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1681,9 +1679,15 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
}
}
-fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> bool {
+fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> Result<bool, DataFusionError> {
let lhs: HashSet<&Expr> = iter_conjunction(lhs).collect();
- iter_conjunction(rhs).any(|e| lhs.contains(&e))
+ iter_conjunction(rhs).try_fold(false, |acc, e| {
Review Comment:
Looks good to me, thanks for the fix.
Maybe we can consider changing the return type of `Expr::is_volatile()` in a follow-up.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "Lordworms (via GitHub)" <gi...@apache.org>.
Lordworms commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1818194880
##########
datafusion/optimizer/src/simplify_expressions/utils.rs:
##########
@@ -341,3 +355,49 @@ pub fn distribute_negation(expr: Expr) -> Expr {
_ => Expr::Negative(Box::new(expr)),
}
}
+
Review Comment:
I'll refactor that
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -835,40 +835,38 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
left,
op: Or,
right,
- }) if expr_contains(&left, &right, Or) => Transformed::yes(*left),
+ }) if expr_contains(&left, &right, Or)? => Transformed::yes(*left),
// A OR (..A..) --> (..A..)
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if expr_contains(&right, &left, Or) => Transformed::yes(*right),
+ }) if expr_contains(&right, &left, Or)? => Transformed::yes(*right),
// A OR (A AND B) --> A
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if is_op_with(And, &right, &left) => Transformed::yes(*left),
+ }) if is_op_with(And, &right, &left)? => Transformed::yes(*left),
// (A AND B) OR A --> A
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if is_op_with(And, &left, &right) => Transformed::yes(*right),
+ }) if is_op_with(And, &left, &right)? => Transformed::yes(*right),
// Eliminate common factors in conjunctions e.g
// (A AND B) OR (A AND C) -> A AND (B OR C)
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if has_common_conjunction(&left, &right) => {
+ }) if has_common_conjunction(&left, &right)? => {
Review Comment:
I checked postgresql and duckdb, they seems to merge the volatile expressions

<img width="945" alt="image" src="https://github.com/user-attachments/assets/1b838b93-04e2-40ed-8bf5-4e225df0c7a1">
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -835,40 +835,38 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
left,
op: Or,
right,
- }) if expr_contains(&left, &right, Or) => Transformed::yes(*left),
+ }) if expr_contains(&left, &right, Or)? => Transformed::yes(*left),
// A OR (..A..) --> (..A..)
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if expr_contains(&right, &left, Or) => Transformed::yes(*right),
+ }) if expr_contains(&right, &left, Or)? => Transformed::yes(*right),
// A OR (A AND B) --> A
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if is_op_with(And, &right, &left) => Transformed::yes(*left),
+ }) if is_op_with(And, &right, &left)? => Transformed::yes(*left),
// (A AND B) OR A --> A
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if is_op_with(And, &left, &right) => Transformed::yes(*right),
+ }) if is_op_with(And, &left, &right)? => Transformed::yes(*right),
// Eliminate common factors in conjunctions e.g
// (A AND B) OR (A AND C) -> A AND (B OR C)
Expr::BinaryExpr(BinaryExpr {
left,
op: Or,
right,
- }) if has_common_conjunction(&left, &right) => {
+ }) if has_common_conjunction(&left, &right)? => {
Review Comment:
Sure
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1825266578
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -3980,4 +3994,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)));
Review Comment:
It would seem to me this should still be the same thing -- namely that we shouldn't remove the `rand() = 0` condition 🤔
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1825267047
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -3980,4 +3994,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)));
Review Comment:
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
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org
Re: [PR] consider volatile function in simply_expression [datafusion]
Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #13128:
URL: https://github.com/apache/datafusion/pull/13128#discussion_r1818577627
##########
datafusion/optimizer/src/simplify_expressions/expr_simplifier.rs:
##########
@@ -1681,9 +1679,15 @@ impl<'a, S: SimplifyInfo> TreeNodeRewriter for Simplifier<'a, S> {
}
}
-fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> bool {
+fn has_common_conjunction(lhs: &Expr, rhs: &Expr) -> Result<bool, DataFusionError> {
let lhs: HashSet<&Expr> = iter_conjunction(lhs).collect();
- iter_conjunction(rhs).any(|e| lhs.contains(&e))
+ iter_conjunction(rhs).try_fold(false, |acc, e| {
Review Comment:
This still looks a bit ugly. I mean, we just look for the first non-volatile expression and don't want to iterate over all expressions.
Actually, I think I made a mistake when I added `is_volatile()` with return type `Result<bool>`:
https://github.com/apache/datafusion/blob/e22d23113f549a90483bbf161add84eba08510fd/datafusion/expr/src/expr.rs#L1581-L1583
As the closure we provide to `exists()` can't return any error, it would be safe to unwrap the result and return only `bool`.
@alamb, do you think we can fix `Expr::is_volatile()`, it would be an API breaking change though, to make it simpler and so we can keep using `any()` here?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: github-unsubscribe@datafusion.apache.org
For additional commands, e-mail: github-help@datafusion.apache.org