-
Notifications
You must be signed in to change notification settings - Fork 217
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
feat(Optimizer): eliminate common sub-expressions #659
Conversation
@st1page PTAL |
impl Rule for ProjectEliminateCSE { | ||
fn apply(&self, plan: PlanRef) -> Result<PlanRef, ()> { | ||
let projection = plan.as_logical_projection()?; | ||
let mut cse_eliminator = CSEEliminator::new(); | ||
let mut proj_exprs = projection.project_expressions().to_vec(); | ||
// Search Phase |
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 might be better if we add the projection merge rule here. and we can merge the nest projection input in AggregateEliminateCSE
too.
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 might be better if we add the projection merge rule here. and we can merge the nest projection input in
AggregateEliminateCSE
too.
Sorry, I don't know what you want to tell me. For simple implementation, it needs multiple consecutive projection operators which play the role of holding temporary variables. In addtition, it seems that consecutive and identical projection operators are produced during column pruning.
Maybe we could:
column pruning --> projection merge(remove identical projection) --> eliminate cse
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 dummy projections (with only InputRef
s) are necessary to support CSE for now. To eliminated them, we have to introduce local reference inside operator, or output_indices
. cc @st1page
exprs_maps: Vec<BoundExpr>, // TODO: HashMap | ||
counts: Vec<(usize, bool)>, |
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.
please add some comments on the fields too.
And why they are not in the same data structure Vec<(BoundExpr, usize, bool)>
? I think even when we use hashmap it should be in the same data structure too, like
struct SubExpr{
count: usize.
new_projectionindex: Option<usize>
}
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.
also some comments on the struct to discribe the general idea of the implementation plz!
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.
also some comments on the struct to discribe the general idea of the implementation plz!
ok 😇
please add some comments on the fields too. And why they are not in the same data structure
Vec<(BoundExpr, usize, bool)>
? I think even when we use hashmap it should be in the same data structure too, likestruct SubExpr{ count: usize. new_projectionindex: Option<usize> }
It looks better
enum EliminatorState { | ||
Search, | ||
Collect, | ||
Replace, | ||
End, | ||
} |
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.
Could you please add some comments here? just similar to the comments on the corresponding functions. and also please remember to use ///
instead //
as rustdoc
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 State
seems not necessaray. What about using different structs to do different phases instead of state transition?
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
State
seems not necessaray. What about using different structs to do different phases instead of state transition?
It seems that expr_maps: HashMap<BoundExpr, SubExpr>
will be accessed during the search phase and collect phase. Using diffrent structs to access and modify expr_maps
maybe look likes wierd? But Replace
state and End
state can be removed, they are just to avoid me doing something wrong.
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 seems that expr_maps: HashMap<BoundExpr, SubExpr> will be accessed during the search phase and collect phase. Using diffrent structs to access and modify expr_maps maybe look likes wierd?
IMHO it makes sense for each phase to process some data and then pass data to the next phase. Using state machine to model the processing pipeline makes the logic of different tasks mixed together and hard to read (And I didn't fully understand it yet) :(
What about:
/// output balabala, modify balabala
fn eliminate(exprs: &mut Vec<BoundExpr>) -> Option<Vec<BoundExpr>> {
let expr_maps = search(exprs.iter())?;
let child_projection_exprs = collect(exprs.iter(), expr_maps);
rewrite(exprs, child_projection_exprs);
Some(child_projection_exprs)
}
/// do balabala, output balabala
fn search(exprs: &Vec<BoundExpr>) -> Option<HashMap<BoundExpr, SubExpr>> {
struct Visitor { expr_maps }
impl ExprVisitor for Visitor {...}
let visitor = ...;
...
visitor.expr_maps
}
/// do balabala
fn collect(exprs: &Vec<BoundExpr>, subexprs: ...) -> Vec<BoundExpr> {
struct Visitor {...}
impl ExprVisitor for Visitor {...}
...
}
/// do balabala
fn rewrite(exprs: &mut Vec<BoundExpr>, child_projection_exprs) {
struct Rewriter {...}
...
}
} | ||
|
||
struct CSEEliminator { | ||
exprs_maps: Vec<BoundExpr>, // TODO: HashMap |
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.
maybe the hash map is necessary because the matching algorithm seems O(n^2) now. we can do it in future PRs 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.
+1 and I think Vec
is not simpler?
enum EliminatorState { | ||
Search, | ||
Collect, | ||
Replace, | ||
End, | ||
} |
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 State
seems not necessaray. What about using different structs to do different phases instead of state transition?
} | ||
|
||
struct CSEEliminator { | ||
exprs_maps: Vec<BoundExpr>, // TODO: HashMap |
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.
+1 and I think Vec
is not simpler?
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 #593! 🥵
Next time for sure! |
I'm working on a new project https://github.com/risinglightdb/sqlplannertest-rs. With planner test, we can merge such optimizer PRs with more confidence! |
Hopefully I can finish it this weekend :) |
It's here! #661 |
Please add planner test cases for affected queries after #661 gets merged :) |
Thanks. 😇😇 |
Signed-off-by: lokax <[email protected]>
Signed-off-by: lokax <[email protected]>
Some things have been modified. |
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.
Please add some planner tests ❤️
Signed-off-by: lokax <[email protected]>
impl Rule for ProjectEliminateCSE { | ||
fn apply(&self, plan: PlanRef) -> Result<PlanRef, ()> { | ||
let projection = plan.as_logical_projection()?; | ||
let mut cse_eliminator = CSEEliminator::new(); | ||
let mut proj_exprs = projection.project_expressions().to_vec(); | ||
// Search Phase |
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 dummy projections (with only InputRef
s) are necessary to support CSE for now. To eliminated them, we have to introduce local reference inside operator, or output_indices
. cc @st1page
|
||
/// This is a helper struct for eliminate common sub-expressions | ||
/// Does not eliminate common aggregate functions | ||
struct CSEEliminator { |
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.
Eliminate CS, not Eliminate CSE 😄
enum EliminatorState { | ||
Search, | ||
Collect, | ||
Replace, | ||
End, | ||
} |
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 seems that expr_maps: HashMap<BoundExpr, SubExpr> will be accessed during the search phase and collect phase. Using diffrent structs to access and modify expr_maps maybe look likes wierd?
IMHO it makes sense for each phase to process some data and then pass data to the next phase. Using state machine to model the processing pipeline makes the logic of different tasks mixed together and hard to read (And I didn't fully understand it yet) :(
What about:
/// output balabala, modify balabala
fn eliminate(exprs: &mut Vec<BoundExpr>) -> Option<Vec<BoundExpr>> {
let expr_maps = search(exprs.iter())?;
let child_projection_exprs = collect(exprs.iter(), expr_maps);
rewrite(exprs, child_projection_exprs);
Some(child_projection_exprs)
}
/// do balabala, output balabala
fn search(exprs: &Vec<BoundExpr>) -> Option<HashMap<BoundExpr, SubExpr>> {
struct Visitor { expr_maps }
impl ExprVisitor for Visitor {...}
let visitor = ...;
...
visitor.expr_maps
}
/// do balabala
fn collect(exprs: &Vec<BoundExpr>, subexprs: ...) -> Vec<BoundExpr> {
struct Visitor {...}
impl ExprVisitor for Visitor {...}
...
}
/// do balabala
fn rewrite(exprs: &mut Vec<BoundExpr>, child_projection_exprs) {
struct Rewriter {...}
...
}
-- keep short circuit | ||
EXPLAIN SELECT x + 5 < y AND x + y < 3, x + y FROM 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.
Could you elaborate a bit what is "short circuit" here? I think x + y
should always be computed, so there's no short circuit?
Thanks for the review. I have no passion to finish it. And the optimizer has been refactored. So I closed this pr. |
Feel free to retry it on the new optimizer if you are interested. It would be easier than this one in my view. |
Signed-off-by: lokax [email protected]
Implement this feature #658 for projection and aggregate.
I removed projection merge rule in LogicalProjection::new(), because we will need a projection below the current projection in some case.
like this:
In the above example, it will evaluate
(1 - l_discount)
2 times andl_extendedprice * A
2 times before optimization. This would not be a problem as long as the expression itself is not expensive. However, evaluating a expensive scalar function multiple times could incur a performance overhead. After optimization, common sub-expressions are evaluated only once.