-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Factorize common AND factors out of OR predicates to support filterPu… #3859
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
Conversation
…shDown as possible Signed-off-by: yangjiang <yangjiang@ebay.com>
| // rewrite to CNF | ||
| // (c = 1 OR c = 1) [can pushDown] AND (c = 1 OR b > 3) AND (b > 2 OR C = 1) AND (b > 2 OR b > 3) | ||
|
|
||
| let expected = "\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It used to get not pushed:
Filter: test.c = Int64(1) AND b > Int64(2) OR test.c = Int64(1) AND b > Int64(3)
Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b) AS b]]
TableScan: test
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alamb @andygrove @Dandandan Do you think this is a reasonable improvement 🤔?
If this ok, i will modify the another test😂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is a good improvement for sure -- thank you
| let expected = "\ | ||
| Filter: test.c = Int64(1) OR b > Int64(3) AND b > Int64(2) OR test.c = Int64(1) AND b > Int64(2) OR b > Int64(3)\ | ||
| \n Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b) AS b]]\ | ||
| \n Filter: test.c = Int64(1) OR test.c = Int64(1)\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Somewhere need simplify this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be a rule in https://github.com/apache/arrow-datafusion/blob/37fe938261636bb7710b26cf26e2bbd010e1dbf0/datafusion/optimizer/src/simplify_expressions.rs#L264
That pass gets run several times
I recommend we file a follow on ticket for that particular rewrite (the same thing applies to a AND a in addition to a OR a
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #3895
|
Seems useful and correct to me - makes sense for predicate pushdown indeed. |
|
Note that one of the tests now fails because of the optimization 👍 |
| LogicalPlan::Filter(filter) => { | ||
| let predicates = utils::split_conjunction(filter.predicate()); | ||
| let filter_cnf = utils::CnfHelper::new().rewrite_to_cnf_impl(filter.predicate()); | ||
| let predicates = utils::split_conjunction(&filter_cnf); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder whether we should do this in filter pushdown or in the expression simplifier optimization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend putting it in SimplifyExpressions -- specifically call it from https://github.com/apache/arrow-datafusion/blob/37fe938261636bb7710b26cf26e2bbd010e1dbf0/datafusion/optimizer/src/simplify_expressions.rs#L264 somehow
You could also reuse the testing framework in that file as well
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alamb @Dandandan I prefer keep this just in filter_push_down, as you seen in test_rewrite_cnf_overflow, this is a heavy operation. It makes the exprs size expansion. IMO, we only need to spilt the filter expr to CNF to push filter down as many as possible, we should use normal expr to pass throuh plan.🤔
alamb
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking great @Ted-Jiang -- thank you.
| } | ||
| } | ||
|
|
||
| /// Converts an expression to conjunctive normal form (CNF). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯 for this comment
| LogicalPlan::Filter(filter) => { | ||
| let predicates = utils::split_conjunction(filter.predicate()); | ||
| let filter_cnf = utils::CnfHelper::new().rewrite_to_cnf_impl(filter.predicate()); | ||
| let predicates = utils::split_conjunction(&filter_cnf); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I recommend putting it in SimplifyExpressions -- specifically call it from https://github.com/apache/arrow-datafusion/blob/37fe938261636bb7710b26cf26e2bbd010e1dbf0/datafusion/optimizer/src/simplify_expressions.rs#L264 somehow
You could also reuse the testing framework in that file as well
| let expected = "\ | ||
| Filter: test.c = Int64(1) OR b > Int64(3) AND b > Int64(2) OR test.c = Int64(1) AND b > Int64(2) OR b > Int64(3)\ | ||
| \n Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b) AS b]]\ | ||
| \n Filter: test.c = Int64(1) OR test.c = Int64(1)\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be a rule in https://github.com/apache/arrow-datafusion/blob/37fe938261636bb7710b26cf26e2bbd010e1dbf0/datafusion/optimizer/src/simplify_expressions.rs#L264
That pass gets run several times
I recommend we file a follow on ticket for that particular rewrite (the same thing applies to a AND a in addition to a OR a
datafusion/optimizer/src/utils.rs
Outdated
| self.current_count >= self.max_count | ||
| } | ||
|
|
||
| pub fn rewrite_to_cnf_impl(&mut self, expr: &Expr) -> Expr { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if you used the ExprRewriter framework here you would avoid many of the clones
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will try this.
| // rewrite to CNF | ||
| // (c = 1 OR c = 1) [can pushDown] AND (c = 1 OR b > 3) AND (b > 2 OR C = 1) AND (b > 2 OR b > 3) | ||
|
|
||
| let expected = "\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it is a good improvement for sure -- thank you
Signed-off-by: yangjiang <yangjiang@ebay.com>
Signed-off-by: yangjiang <yangjiang@ebay.com>
| // Test rewrite on a1_and_b2 and a2_and_b1 -> (((a1 and b2) and a2) and b1) | ||
| let mut helper = CnfHelper::new(); | ||
| let expr1 = and(and(a_1.clone(), b_2.clone()), and(a_2.clone(), b_1.clone())); | ||
| let expect = and(a_1.clone(), b_2.clone()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need improve the Expr fmt: like
let expr1 = and(a_1.clone(), b_2.clone())
.and(a_2.clone())
.and(b_1.clone());
let expr2 = and(and(a_1.clone(), b_2.clone()), and(a_2.clone(), b_1.clone()));
assert_eq!(expr1, expr2);
This is not equal but they have the same string format:
a = Int64(1) AND b = Int64(2) AND a = Int64(2) AND b = Int64(1)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not equal but they have the same string format:
I agree this is not ideal
Signed-off-by: yangjiang <yangjiang@ebay.com>
Signed-off-by: yangjiang <yangjiang@ebay.com>
| /// ``` | ||
| pub fn split_conjunction_owned(expr: Expr) -> Vec<Expr> { | ||
| split_conjunction_owned_impl(expr, vec![]) | ||
| pub fn split_conjunction_owned(expr: Expr, op: Operator) -> Vec<Expr> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alamb I think we could support on split on other types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this is useful -- though I would prefer this function to remain named split_conjunction (as the conjunction implies AND and instead have a new function called split_operator or something. Maybe I can make a quick PR
alamb
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this looks really nice @Ted-Jiang 🏅
thank you
| // Test rewrite on a1_and_b2 and a2_and_b1 -> (((a1 and b2) and a2) and b1) | ||
| let mut helper = CnfHelper::new(); | ||
| let expr1 = and(and(a_1.clone(), b_2.clone()), and(a_2.clone(), b_1.clone())); | ||
| let expect = and(a_1.clone(), b_2.clone()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not equal but they have the same string format:
I agree this is not ideal
| /// ``` | ||
| pub fn split_conjunction_owned(expr: Expr) -> Vec<Expr> { | ||
| split_conjunction_owned_impl(expr, vec![]) | ||
| pub fn split_conjunction_owned(expr: Expr, op: Operator) -> Vec<Expr> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree this is useful -- though I would prefer this function to remain named split_conjunction (as the conjunction implies AND and instead have a new function called split_operator or something. Maybe I can make a quick PR
| } | ||
|
|
||
| #[test] | ||
| fn test_rewrite_cnf_overflow() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
| let expected = "\ | ||
| Filter: test.c = Int64(1) OR b > Int64(3) AND b > Int64(2) OR test.c = Int64(1) AND b > Int64(2) OR b > Int64(3)\ | ||
| \n Aggregate: groupBy=[[test.a]], aggr=[[SUM(test.b) AS b]]\ | ||
| \n Filter: test.c = Int64(1) OR test.c = Int64(1)\ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #3895
|
🤔 this seems to have caused a logical merge conflict: https://github.com/apache/arrow-datafusion/actions/runs/3284319035/jobs/5410351533 I am working on a fix |
|
Actually, I think the regression is pretty bad (there are many filters) -- I am going to revert this PR to get the tests green again. I am really sorry @Ted-Jiang |
…filterPu… (apache#3859)" This reverts commit ddfd052.
|
@Ted-Jiang can you please re-create this PR (or I can do so to) and we can iterate on it some more. I am really sorry about that |
@alamb of course!This is not your fault 😂 |
seems strange it pass in my local. |
…shDown as possible
Signed-off-by: yangjiang yangjiang@ebay.com
Which issue does this PR close?
Closes #3858. Part #3834
Rationale for this change
A predicate is of the from
p AND q1 OR p AND q2it would help to factorize out the AND factor p, to the equivalent expressionp AND (q1 OR q2). This is useful when p in predicates can be pushed down without q.What changes are included in this PR?
Are there any user-facing changes?