-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Improve InListSImplifier
-- add test, commend and avoid clones
#8971
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
85ac332
Add new test and issue link
alamb 9a1c62d
Avoid cloning in InListSimplifier
alamb 24bb8ab
Remove another clone
alamb 24ceddc
Merge remote-tracking branch 'apache/main' into alamb/inlist_simplify
alamb 87e284b
fmt
alamb b6173f4
Merge remote-tracking branch 'apache/main' into alamb/inlist_simplify
alamb f4adf4a
fix clippy, simplify more
alamb 6d74a53
Merge remote-tracking branch 'apache/main' into alamb/inlist_simplify
alamb 54a9a0d
Merge remote-tracking branch 'apache/main' into alamb/inlist_simplify
alamb 4110926
Merge remote-tracking branch 'apache/main' into alamb/inlist_simplify
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,85 +52,92 @@ impl TreeNodeRewriter for InListSimplifier { | |
type N = Expr; | ||
|
||
fn mutate(&mut self, expr: Expr) -> Result<Expr> { | ||
if let Expr::BinaryExpr(BinaryExpr { left, op, right }) = &expr { | ||
if let (Expr::InList(l1), Operator::And, Expr::InList(l2)) = | ||
(left.as_ref(), op, right.as_ref()) | ||
{ | ||
if l1.expr == l2.expr && !l1.negated && !l2.negated { | ||
return inlist_intersection(l1, l2, false); | ||
} else if l1.expr == l2.expr && l1.negated && l2.negated { | ||
return inlist_union(l1, l2, true); | ||
} else if l1.expr == l2.expr && !l1.negated && l2.negated { | ||
return inlist_except(l1, l2); | ||
} else if l1.expr == l2.expr && l1.negated && !l2.negated { | ||
return inlist_except(l2, l1); | ||
if let Expr::BinaryExpr(BinaryExpr { left, op, right }) = expr { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This pattern allows getting an owned |
||
match (*left, op, *right) { | ||
(Expr::InList(l1), Operator::And, Expr::InList(l2)) | ||
if l1.expr == l2.expr && !l1.negated && !l2.negated => | ||
{ | ||
inlist_intersection(l1, l2, false) | ||
} | ||
} else if let (Expr::InList(l1), Operator::Or, Expr::InList(l2)) = | ||
(left.as_ref(), op, right.as_ref()) | ||
{ | ||
if l1.expr == l2.expr && l1.negated && l2.negated { | ||
return inlist_intersection(l1, l2, true); | ||
(Expr::InList(l1), Operator::And, Expr::InList(l2)) | ||
if l1.expr == l2.expr && l1.negated && l2.negated => | ||
{ | ||
inlist_union(l1, l2, true) | ||
} | ||
(Expr::InList(l1), Operator::And, Expr::InList(l2)) | ||
if l1.expr == l2.expr && !l1.negated && l2.negated => | ||
{ | ||
inlist_except(l1, l2) | ||
} | ||
(Expr::InList(l1), Operator::And, Expr::InList(l2)) | ||
if l1.expr == l2.expr && l1.negated && !l2.negated => | ||
{ | ||
inlist_except(l2, l1) | ||
} | ||
(Expr::InList(l1), Operator::Or, Expr::InList(l2)) | ||
if l1.expr == l2.expr && l1.negated && l2.negated => | ||
{ | ||
inlist_intersection(l1, l2, true) | ||
} | ||
(left, op, right) => { | ||
// put the expression back together | ||
Ok(Expr::BinaryExpr(BinaryExpr { | ||
left: Box::new(left), | ||
op, | ||
right: Box::new(right), | ||
})) | ||
} | ||
} | ||
} else { | ||
Ok(expr) | ||
} | ||
|
||
Ok(expr) | ||
} | ||
} | ||
|
||
fn inlist_union(l1: &InList, l2: &InList, negated: bool) -> Result<Expr> { | ||
let mut seen: HashSet<Expr> = HashSet::new(); | ||
let list = l1 | ||
.list | ||
.iter() | ||
.chain(l2.list.iter()) | ||
.filter(|&e| seen.insert(e.to_owned())) | ||
.cloned() | ||
.collect::<Vec<_>>(); | ||
let merged_inlist = InList { | ||
expr: l1.expr.clone(), | ||
list, | ||
negated, | ||
}; | ||
Ok(Expr::InList(merged_inlist)) | ||
} | ||
/// Return the union of two inlist expressions | ||
/// maintaining the order of the elements in the two lists | ||
fn inlist_union(mut l1: InList, l2: InList, negated: bool) -> Result<Expr> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These functions now take owned |
||
// extend the list in l1 with the elements in l2 that are not already in l1 | ||
let l1_items: HashSet<_> = l1.list.iter().collect(); | ||
|
||
fn inlist_intersection(l1: &InList, l2: &InList, negated: bool) -> Result<Expr> { | ||
let l1_set: HashSet<Expr> = l1.list.iter().cloned().collect(); | ||
let intersect_list: Vec<Expr> = l2 | ||
// keep all l2 items that do not also appear in l1 | ||
let keep_l2: Vec<_> = l2 | ||
.list | ||
.iter() | ||
.filter(|x| l1_set.contains(x)) | ||
.cloned() | ||
.into_iter() | ||
.filter_map(|e| if l1_items.contains(&e) { None } else { Some(e) }) | ||
.collect(); | ||
|
||
l1.list.extend(keep_l2); | ||
l1.negated = negated; | ||
Ok(Expr::InList(l1)) | ||
} | ||
|
||
/// Return the intersection of two inlist expressions | ||
/// maintaining the order of the elements in the two lists | ||
fn inlist_intersection(mut l1: InList, l2: InList, negated: bool) -> Result<Expr> { | ||
let l2_items = l2.list.iter().collect::<HashSet<_>>(); | ||
|
||
// remove all items from l1 that are not in l2 | ||
l1.list.retain(|e| l2_items.contains(e)); | ||
|
||
// e in () is always false | ||
// e not in () is always true | ||
if intersect_list.is_empty() { | ||
if l1.list.is_empty() { | ||
return Ok(lit(negated)); | ||
} | ||
let merged_inlist = InList { | ||
expr: l1.expr.clone(), | ||
list: intersect_list, | ||
negated, | ||
}; | ||
Ok(Expr::InList(merged_inlist)) | ||
Ok(Expr::InList(l1)) | ||
} | ||
|
||
fn inlist_except(l1: &InList, l2: &InList) -> Result<Expr> { | ||
let l2_set: HashSet<Expr> = l2.list.iter().cloned().collect(); | ||
let except_list: Vec<Expr> = l1 | ||
.list | ||
.iter() | ||
.filter(|x| !l2_set.contains(x)) | ||
.cloned() | ||
.collect(); | ||
if except_list.is_empty() { | ||
/// Return the all items in l1 that are not in l2 | ||
/// maintaining the order of the elements in the two lists | ||
fn inlist_except(mut l1: InList, l2: InList) -> Result<Expr> { | ||
let l2_items = l2.list.iter().collect::<HashSet<_>>(); | ||
|
||
// keep only items from l1 that are not in l2 | ||
l1.list.retain(|e| !l2_items.contains(e)); | ||
|
||
if l1.list.is_empty() { | ||
return Ok(lit(false)); | ||
} | ||
let merged_inlist = InList { | ||
expr: l1.expr.clone(), | ||
list: except_list, | ||
negated: false, | ||
}; | ||
Ok(Expr::InList(merged_inlist)) | ||
Ok(Expr::InList(l1)) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 case shocks me (again) on how powerful this rule is
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.
@jayzhan211 gets all the credit for implementing it in #8949 (🏆)