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

Factorize common AND factors out of OR predicates to support filterPu… #3859

Merged
merged 6 commits into from
Oct 19, 2022

Conversation

Ted-Jiang
Copy link
Member

@Ted-Jiang Ted-Jiang commented Oct 17, 2022

…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 q2 it would help to factorize out the AND factor p, to the equivalent expression p 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?

…shDown as possible

Signed-off-by: yangjiang <yangjiang@ebay.com>
@github-actions github-actions bot added the optimizer Optimizer rules label Oct 17, 2022
// 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 = "\
Copy link
Member Author

@Ted-Jiang Ted-Jiang Oct 17, 2022

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

Copy link
Member Author

@Ted-Jiang Ted-Jiang Oct 17, 2022

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😂

Copy link
Contributor

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)\
Copy link
Member Author

Choose a reason for hiding this comment

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

Somewhere need simplify this

Copy link
Contributor

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Filed #3895

@Dandandan
Copy link
Contributor

Seems useful and correct to me - makes sense for predicate pushdown indeed.

@Dandandan
Copy link
Contributor

Note that one of the tests now fails because of the optimization 👍

@@ -529,7 +529,8 @@ fn optimize(plan: &LogicalPlan, mut state: State) -> Result<LogicalPlan> {
}
LogicalPlan::Analyze { .. } => push_down(&state, plan),
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);
Copy link
Contributor

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?

Copy link
Contributor

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

Copy link
Member Author

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.🤔

Copy link
Contributor

@alamb alamb left a 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.

@@ -119,6 +119,121 @@ fn split_conjunction_owned_impl(expr: Expr, mut exprs: Vec<Expr>) -> Vec<Expr> {
}
}

/// Converts an expression to conjunctive normal form (CNF).
Copy link
Contributor

Choose a reason for hiding this comment

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

💯 for this comment

@@ -529,7 +529,8 @@ fn optimize(plan: &LogicalPlan, mut state: State) -> Result<LogicalPlan> {
}
LogicalPlan::Analyze { .. } => push_down(&state, plan),
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);
Copy link
Contributor

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)\
Copy link
Contributor

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

self.current_count >= self.max_count
}

pub fn rewrite_to_cnf_impl(&mut self, expr: &Expr) -> Expr {
Copy link
Contributor

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

Copy link
Member Author

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 = "\
Copy link
Contributor

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())
Copy link
Member Author

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)

Copy link
Contributor

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> {
Copy link
Member Author

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.

Copy link
Contributor

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

@Ted-Jiang Ted-Jiang requested a review from alamb October 19, 2022 07:15
@github-actions github-actions bot added the core Core DataFusion crate label Oct 19, 2022
@Ted-Jiang Ted-Jiang requested review from Dandandan and alamb and removed request for alamb and Dandandan October 19, 2022 08:09
Copy link
Contributor

@alamb alamb left a 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())
Copy link
Contributor

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> {
Copy link
Contributor

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() {
Copy link
Contributor

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)\
Copy link
Contributor

Choose a reason for hiding this comment

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

Filed #3895

@alamb alamb merged commit ddfd052 into apache:master Oct 19, 2022
@alamb
Copy link
Contributor

alamb commented Oct 19, 2022

🤔 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

@alamb
Copy link
Contributor

alamb commented Oct 19, 2022

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

alamb added a commit to alamb/datafusion that referenced this pull request Oct 19, 2022
@alamb
Copy link
Contributor

alamb commented Oct 19, 2022

@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

@Ted-Jiang
Copy link
Member Author

@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 😂

@Ted-Jiang
Copy link
Member Author

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

seems strange it pass in my local.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Core DataFusion crate optimizer Optimizer rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Factorize common AND factors out of OR predicates to support filterPushDown as possible
3 participants