You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "wiedld (via GitHub)" <gi...@apache.org> on 2024/03/18 22:47:02 UTC
[PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
wiedld opened a new pull request, #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685
## Which issue does this PR close?
Closes #9678
## Rationale for this change
After the TreeNode refactor, the [common_subexpr_eliminate was producing an error](https://github.com/apache/arrow-datafusion/issues/9678) in a very specific use case.
First commit is the reproducer of this issue. It was specifically occurring with short-circuited expressions. It occurred in case/when expressions (without an alias), and coalesce (but only when also used with an alias?).
## What changes are included in this PR?
Second commit is a proposed fix; although I'm soliciting feedback for what is the proper fix.
The stack of visited nodes was not populated for common expressions that were short-circuited. The change made in the TreeNode refactor did two things:
* returned after `self.pop_enter_mark()` without populating the visited stack
* no longer detecting the unpopulated stack, but the removal of the panic `unreachable!("Enter mark should paired with node number")`
## Are these changes tested?
Yes. First commit has reproducer tests.
## Are there any user-facing changes?
No API changes. Only removes a bug in query execution.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531566365
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -719,23 +741,10 @@ impl TreeNodeRewriter for CommonSubexprRewriter<'_> {
));
}
- let (series_number, id) = &self.id_array[self.curr_index];
+ // incr idx for id_array, when not jumping
self.curr_index += 1;
- // Skip sub-node of a replaced tree, or without identifier, or is not repeated expr.
- let expr_set_item = self.expr_set.get(id).ok_or_else(|| {
- internal_datafusion_err!("expr_set invalid state")
- })?;
- if *series_number < self.max_series_number
Review Comment:
These checks are already done earlier. Remove.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#issuecomment-2011795733
I plan to merge this later today unless someone else would like more time to 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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531567569
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -719,23 +741,10 @@ impl TreeNodeRewriter for CommonSubexprRewriter<'_> {
));
}
- let (series_number, id) = &self.id_array[self.curr_index];
Review Comment:
This was one of the bugs. It uses an already incremented `self.curr_index`. Instead, we grab all of the values once (see new line 713), before all the `self.curr_index` shenanigans.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531581836
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -42,6 +41,15 @@ use datafusion_expr::{col, Expr, ExprSchemable};
/// - DataType of this expression.
type ExprSet = HashMap<Identifier, (Expr, usize, DataType)>;
+/// An ordered map of Identifiers encountered during visitation.
+///
+/// Is created in the ExprIdentifierVisitor, which identifies the common expressions.
+/// Is consumed in the CommonSubexprRewriter, which performs mutations.
+///
+/// - series_number. increased in fn_up, start from 1.
+/// - Identifier. is empty ("") if expr should not be considered for common elimation.
+type IdArray = Vec<(usize, Identifier)>;
Review Comment:
This abstraction really helps to understand how the 2 visitors work together.
The first visitor sets the vector idx used on f_down, whereas the series_number is set on f_up. An empty string for the expr_id means that this expression is not considered for rewrite (is skipped) in the second visitor.
e.g. `[(3, expr_id),(2, expr_id),(1, expr_id)]`
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531581836
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -42,6 +41,15 @@ use datafusion_expr::{col, Expr, ExprSchemable};
/// - DataType of this expression.
type ExprSet = HashMap<Identifier, (Expr, usize, DataType)>;
+/// An ordered map of Identifiers encountered during visitation.
+///
+/// Is created in the ExprIdentifierVisitor, which identifies the common expressions.
+/// Is consumed in the CommonSubexprRewriter, which performs mutations.
+///
+/// - series_number. increased in fn_up, start from 1.
+/// - Identifier. is empty ("") if expr should not be considered for common elimation.
+type IdArray = Vec<(usize, Identifier)>;
Review Comment:
This abstraction really helps to understand how the 2 visitors work together.
The first visitor builds the `IdArray` and `ExprSet`. The first visitor sets the vector idx used on f_down, whereas the series_number is set on f_up. An empty string for the expr_id means that this expression is not considered for rewrite (is skipped) in the second visitor.
e.g. `[(3, expr_id),(2, expr_id),(1, expr_id)]`
The second visitor then performs the mutation, based upon the `IdArray` and `ExprSet`.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531573732
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +580,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
+ JumpMark(usize),
/// Accumulated identifier of sub expression.
ExprItem(Identifier),
}
impl ExprIdentifierVisitor<'_> {
- fn desc_expr(expr: &Expr) -> String {
+ fn node_identifier(expr: &Expr) -> String {
format!("{expr}")
}
/// Find the first `EnterMark` in the stack, and accumulates every `ExprItem`
/// before it.
- fn pop_enter_mark(&mut self) -> Option<(usize, Identifier)> {
+ fn pop_enter_mark(&mut self) -> (usize, Identifier) {
let mut desc = String::new();
while let Some(item) = self.visit_stack.pop() {
match item {
- VisitRecord::EnterMark(idx) => {
- return Some((idx, desc));
+ VisitRecord::EnterMark(idx) | VisitRecord::JumpMark(idx) => {
+ return (idx, desc);
}
- VisitRecord::ExprItem(s) => {
- desc.push_str(&s);
+ VisitRecord::ExprItem(id) => {
+ desc.push_str(&id);
}
}
}
- None
+ unreachable!("Enter mark should paired with node number");
}
}
impl TreeNodeVisitor for ExprIdentifierVisitor<'_> {
type Node = Expr;
fn f_down(&mut self, expr: &Expr) -> Result<TreeNodeRecursion> {
+ // put placeholder, sets the proper array length
+ self.id_array.push((0, "".to_string()));
+
// related to https://github.com/apache/arrow-datafusion/issues/8814
// If the expr contain volatile expression or is a short-circuit expression, skip it.
if expr.short_circuits() || is_volatile_expression(expr)? {
- return Ok(TreeNodeRecursion::Jump);
+ self.visit_stack
+ .push(VisitRecord::JumpMark(self.node_count));
+ return Ok(TreeNodeRecursion::Jump); // go to f_up
}
+
self.visit_stack
.push(VisitRecord::EnterMark(self.node_count));
self.node_count += 1;
- // put placeholder
- self.id_array.push((0, "".to_string()));
+
Ok(TreeNodeRecursion::Continue)
}
fn f_up(&mut self, expr: &Expr) -> Result<TreeNodeRecursion> {
self.series_number += 1;
- let Some((idx, sub_expr_desc)) = self.pop_enter_mark() else {
- return Ok(TreeNodeRecursion::Continue);
- };
Review Comment:
This was one of the bugs, which occurred for short-circuited expr. The behavior was changed with the TreeNode refactor. Previously, it was always returning a `sub_expr_desc` => then executing the remaining function block.
The changes I made here was to (1) bring it back closer to the original code, while (2) incorporating the jump contract from the consolidated visitor.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531575684
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +580,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
+ JumpMark(usize),
/// Accumulated identifier of sub expression.
ExprItem(Identifier),
}
impl ExprIdentifierVisitor<'_> {
- fn desc_expr(expr: &Expr) -> String {
+ fn node_identifier(expr: &Expr) -> String {
format!("{expr}")
}
/// Find the first `EnterMark` in the stack, and accumulates every `ExprItem`
/// before it.
- fn pop_enter_mark(&mut self) -> Option<(usize, Identifier)> {
+ fn pop_enter_mark(&mut self) -> (usize, Identifier) {
let mut desc = String::new();
while let Some(item) = self.visit_stack.pop() {
match item {
- VisitRecord::EnterMark(idx) => {
- return Some((idx, desc));
+ VisitRecord::EnterMark(idx) | VisitRecord::JumpMark(idx) => {
+ return (idx, desc);
}
- VisitRecord::ExprItem(s) => {
- desc.push_str(&s);
+ VisitRecord::ExprItem(id) => {
+ desc.push_str(&id);
}
}
}
- None
+ unreachable!("Enter mark should paired with node number");
Review Comment:
Bring back the pop_enter_mark() contract to the original, while also adding in a new JumpMark.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531581836
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -42,6 +41,15 @@ use datafusion_expr::{col, Expr, ExprSchemable};
/// - DataType of this expression.
type ExprSet = HashMap<Identifier, (Expr, usize, DataType)>;
+/// An ordered map of Identifiers encountered during visitation.
+///
+/// Is created in the ExprIdentifierVisitor, which identifies the common expressions.
+/// Is consumed in the CommonSubexprRewriter, which performs mutations.
+///
+/// - series_number. increased in fn_up, start from 1.
+/// - Identifier. is empty ("") if expr should not be considered for common elimation.
+type IdArray = Vec<(usize, Identifier)>;
Review Comment:
This abstraction really helps to understand how the 2 visitors work together.
The first visitor builds the `IdArray` and `ExprSet`. The first visitor sets the vector idx used on f_down, whereas the series_number is set on f_up. An empty string for the expr_id means that this expression is not considered for rewrite (is skipped) in the second visitor.
e.g. `[(3, expr_id),(2, expr_id),(1, expr_id)]`
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531573732
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +580,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
+ JumpMark(usize),
/// Accumulated identifier of sub expression.
ExprItem(Identifier),
}
impl ExprIdentifierVisitor<'_> {
- fn desc_expr(expr: &Expr) -> String {
+ fn node_identifier(expr: &Expr) -> String {
format!("{expr}")
}
/// Find the first `EnterMark` in the stack, and accumulates every `ExprItem`
/// before it.
- fn pop_enter_mark(&mut self) -> Option<(usize, Identifier)> {
+ fn pop_enter_mark(&mut self) -> (usize, Identifier) {
let mut desc = String::new();
while let Some(item) = self.visit_stack.pop() {
match item {
- VisitRecord::EnterMark(idx) => {
- return Some((idx, desc));
+ VisitRecord::EnterMark(idx) | VisitRecord::JumpMark(idx) => {
+ return (idx, desc);
}
- VisitRecord::ExprItem(s) => {
- desc.push_str(&s);
+ VisitRecord::ExprItem(id) => {
+ desc.push_str(&id);
}
}
}
- None
+ unreachable!("Enter mark should paired with node number");
}
}
impl TreeNodeVisitor for ExprIdentifierVisitor<'_> {
type Node = Expr;
fn f_down(&mut self, expr: &Expr) -> Result<TreeNodeRecursion> {
+ // put placeholder, sets the proper array length
+ self.id_array.push((0, "".to_string()));
+
// related to https://github.com/apache/arrow-datafusion/issues/8814
// If the expr contain volatile expression or is a short-circuit expression, skip it.
if expr.short_circuits() || is_volatile_expression(expr)? {
- return Ok(TreeNodeRecursion::Jump);
+ self.visit_stack
+ .push(VisitRecord::JumpMark(self.node_count));
+ return Ok(TreeNodeRecursion::Jump); // go to f_up
}
+
self.visit_stack
.push(VisitRecord::EnterMark(self.node_count));
self.node_count += 1;
- // put placeholder
- self.id_array.push((0, "".to_string()));
+
Ok(TreeNodeRecursion::Continue)
}
fn f_up(&mut self, expr: &Expr) -> Result<TreeNodeRecursion> {
self.series_number += 1;
- let Some((idx, sub_expr_desc)) = self.pop_enter_mark() else {
- return Ok(TreeNodeRecursion::Continue);
- };
Review Comment:
This was one of the bugs. The behavior was changed with the TreeNode refactor. Previously, it was always returning a `sub_expr_desc` => then executing the remaining function block.
The changes I made here was to (1) bring it back closer to the original code, while (2) incorporating the jump contract from the consolidated visitor.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531567569
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -719,23 +741,10 @@ impl TreeNodeRewriter for CommonSubexprRewriter<'_> {
));
}
- let (series_number, id) = &self.id_array[self.curr_index];
Review Comment:
This was one of the bugs (see the regression test). It uses an already incremented `self.curr_index`. Instead, we grab all of the values once (see new line 713), before all the `self.curr_index` shenanigans.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1532640935
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +584,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
Review Comment:
For my understanding (please correct here 🙏🏼 ), prior to refactoring the 2 different visitors (`TreeNodeVisitor` and `TreeNodeRewriter`) had different transversal rules, and the semantics of skip vs stop mapped in weird ways (as [nicely described here](https://github.com/apache/arrow-datafusion/pull/8891#issue-2085713028) in the TreeNode refactor's "Rationale for Change").
Prior to the refactor:
* the first visitor used skips (`VisitRecursion::Skip`), which never calls f_up afterwards.
* the second visitor used `RewriteRecursion::Stop` which is functionally the same as `VisitRecursion::Skip`
### Fix for the first visitor, for the short-circuited nodes:
Prior to the refactor, the first tree walk did not add short-circuited nodes to the stack:
1. short-circuited nodes were [skipped](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L242-L243), without adding to the stack
1. the old semantics meant [skipping the post-visit (up) call](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L26-L27)
1. it never looked in the stack (since no post-visit on the old semantics)
Whereas with the new TreeNode refactor the jump became a skip-children-but-call-f_up, which meant:
1. prior to the fix in this PR, it was using the old control flow. So it still didn't add to visited_stack.
1. refactor means that it now does perform the f_up on jump
1. f_up checks the stack (in `self.pop_enter_mark()`) and would not find it
1. the original refactor got around this by removing the panic and adding an option
For the changes done in the `TreeNodeVisitor` for the short-circuited nodes, I basically made it work with a jump that does the post-visit f_up by:
* add a placeholder to the id_array so that it exists on f_up
* push into the stack so it exists on the f_up
* before this bugfix, it was occasionally matching an entry in the stack, but it could be the wrong match (such as in our test case).
* make a `VisitRecord::JumpMark` to be used in the stack
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531573732
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +580,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
+ JumpMark(usize),
/// Accumulated identifier of sub expression.
ExprItem(Identifier),
}
impl ExprIdentifierVisitor<'_> {
- fn desc_expr(expr: &Expr) -> String {
+ fn node_identifier(expr: &Expr) -> String {
format!("{expr}")
}
/// Find the first `EnterMark` in the stack, and accumulates every `ExprItem`
/// before it.
- fn pop_enter_mark(&mut self) -> Option<(usize, Identifier)> {
+ fn pop_enter_mark(&mut self) -> (usize, Identifier) {
let mut desc = String::new();
while let Some(item) = self.visit_stack.pop() {
match item {
- VisitRecord::EnterMark(idx) => {
- return Some((idx, desc));
+ VisitRecord::EnterMark(idx) | VisitRecord::JumpMark(idx) => {
+ return (idx, desc);
}
- VisitRecord::ExprItem(s) => {
- desc.push_str(&s);
+ VisitRecord::ExprItem(id) => {
+ desc.push_str(&id);
}
}
}
- None
+ unreachable!("Enter mark should paired with node number");
}
}
impl TreeNodeVisitor for ExprIdentifierVisitor<'_> {
type Node = Expr;
fn f_down(&mut self, expr: &Expr) -> Result<TreeNodeRecursion> {
+ // put placeholder, sets the proper array length
+ self.id_array.push((0, "".to_string()));
+
// related to https://github.com/apache/arrow-datafusion/issues/8814
// If the expr contain volatile expression or is a short-circuit expression, skip it.
if expr.short_circuits() || is_volatile_expression(expr)? {
- return Ok(TreeNodeRecursion::Jump);
+ self.visit_stack
+ .push(VisitRecord::JumpMark(self.node_count));
+ return Ok(TreeNodeRecursion::Jump); // go to f_up
}
+
self.visit_stack
.push(VisitRecord::EnterMark(self.node_count));
self.node_count += 1;
- // put placeholder
- self.id_array.push((0, "".to_string()));
+
Ok(TreeNodeRecursion::Continue)
}
fn f_up(&mut self, expr: &Expr) -> Result<TreeNodeRecursion> {
self.series_number += 1;
- let Some((idx, sub_expr_desc)) = self.pop_enter_mark() else {
- return Ok(TreeNodeRecursion::Continue);
- };
Review Comment:
This was one of the bugs, which occurred for short-circuited expr. The behavior was changed with the TreeNode refactor. Previously, it was always returning a `sub_expr_desc` => then executing the remaining function block.
The changes I made here was to (1) bring it back closer to the original code, while (2) incorporating the jump contract from the consolidated visitor.
The actual process to figure this out was done over several commits. The bug was fixed by [build the proper visited_stack](https://github.com/apache/arrow-datafusion/pull/9685/commits/ff466149231a3c72dcceebf54d43d59f00d9c415), then later I [tweaked the fix](https://github.com/apache/arrow-datafusion/pull/9685/commits/1931b3b439e6386ec1bdfe5616d89cfe3c7e3300) as I figured out a bit more.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531575684
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +580,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
+ JumpMark(usize),
/// Accumulated identifier of sub expression.
ExprItem(Identifier),
}
impl ExprIdentifierVisitor<'_> {
- fn desc_expr(expr: &Expr) -> String {
+ fn node_identifier(expr: &Expr) -> String {
format!("{expr}")
}
/// Find the first `EnterMark` in the stack, and accumulates every `ExprItem`
/// before it.
- fn pop_enter_mark(&mut self) -> Option<(usize, Identifier)> {
+ fn pop_enter_mark(&mut self) -> (usize, Identifier) {
let mut desc = String::new();
while let Some(item) = self.visit_stack.pop() {
match item {
- VisitRecord::EnterMark(idx) => {
- return Some((idx, desc));
+ VisitRecord::EnterMark(idx) | VisitRecord::JumpMark(idx) => {
+ return (idx, desc);
}
- VisitRecord::ExprItem(s) => {
- desc.push_str(&s);
+ VisitRecord::ExprItem(id) => {
+ desc.push_str(&id);
}
}
}
- None
+ unreachable!("Enter mark should paired with node number");
Review Comment:
Bring back the pop_enter_mark() contract to the original (before the [TreeNode refactor](https://github.com/apache/arrow-datafusion/pull/8891)), while also adding in a new JumpMark.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1532520089
##########
datafusion/sqllogictest/test_files/common_subexpr_eliminate.slt:
##########
@@ -0,0 +1,106 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+#############
+## Common Subexpr Eliminate Tests
+#############
+
+statement ok
+CREATE TABLE doubles (
+ f64 DOUBLE
+) as VALUES
+ (10.1)
+;
+
+# common subexpr with alias
+query RRR rowsort
+select f64, round(1.0 / f64) as i64_1, acos(round(1.0 / f64)) from doubles;
+----
+10.1 0 1.570796326795
+
+# common subexpr with coalesce (short-circuited)
+query RRR rowsort
+select f64, coalesce(1.0 / f64, 0.0), acos(coalesce(1.0 / f64, 0.0)) from doubles;
+----
+10.1 0.09900990099 1.471623942989
+
+# common subexpr with coalesce (short-circuited) and alias
+query RRR rowsort
+select f64, coalesce(1.0 / f64, 0.0) as f64_1, acos(coalesce(1.0 / f64, 0.0)) from doubles;
+----
+10.1 0.09900990099 1.471623942989
+
+# common subexpr with case (short-circuited)
+query RRR rowsort
+select f64, case when f64 > 0 then 1.0 / f64 else null end, acos(case when f64 > 0 then 1.0 / f64 else null end) from doubles;
+----
+10.1 0.09900990099 1.471623942989
+
+
+statement ok
Review Comment:
done.
##########
datafusion/sqllogictest/test_files/common_subexpr_eliminate.slt:
##########
@@ -0,0 +1,106 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+#############
+## Common Subexpr Eliminate Tests
+#############
+
+statement ok
Review Comment:
done.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1532640935
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +584,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
Review Comment:
For my understanding (please correct here 🙏🏼 ), prior to refactoring the 2 different visitors (`TreeNodeVisitor` and `TreeNodeRewriter`) had different transversal rules, and the semantics of skip vs stop mapped in weird ways (as [nicely described here](https://github.com/apache/arrow-datafusion/pull/8891#issue-2085713028) in the TreeNode refactor's "Rationale for Change").
Prior to the refactor, during the first tree walk (which builds the `visited_stack`) the short-circuited nodes:
1. were [skipped](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L242-L243), without adding to the stack
1. the old semantics meant [skipping the post-visit (up) call](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L26-L27)
1. it never looked in the stack (since no post-visit on the old semantics)
Whereas with the new TreeNode refactor the jump became a skip-children-but-call-f_up, which meant:
1. prior to the fix in this PR, it was using the old control flow. So it still didn't add to visited_stack.
1. refactor means that it now does perform the f_up on jump
1. f_up checks the stack (in `self.pop_enter_mark()`) and would not find it
1. the original refactor got around this by removing the panic and adding an option
For the changes done in the `TreeNodeVisitor` for the short-circuited nodes, I basically made it work with a jump that does the post-visit f_up by:
* add a placeholder to the id_array so that it exists on f_up
* push into the stack so it exists on the f_up
* make a `VisitRecord::JumpMark` to be used in the stack
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#issuecomment-2013224631
I merged up to main and pushed a commit to this branch
> I plan to give it a review later tomorrow. We can merge this first as there is one approval if the release progress is blocked.
Given I would like to unblock the release I do plan to merge it in later today. We'll plan to address any comments as follow on PRs.
Thank you very much @waynexia
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531573732
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +580,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
+ JumpMark(usize),
/// Accumulated identifier of sub expression.
ExprItem(Identifier),
}
impl ExprIdentifierVisitor<'_> {
- fn desc_expr(expr: &Expr) -> String {
+ fn node_identifier(expr: &Expr) -> String {
format!("{expr}")
}
/// Find the first `EnterMark` in the stack, and accumulates every `ExprItem`
/// before it.
- fn pop_enter_mark(&mut self) -> Option<(usize, Identifier)> {
+ fn pop_enter_mark(&mut self) -> (usize, Identifier) {
let mut desc = String::new();
while let Some(item) = self.visit_stack.pop() {
match item {
- VisitRecord::EnterMark(idx) => {
- return Some((idx, desc));
+ VisitRecord::EnterMark(idx) | VisitRecord::JumpMark(idx) => {
+ return (idx, desc);
}
- VisitRecord::ExprItem(s) => {
- desc.push_str(&s);
+ VisitRecord::ExprItem(id) => {
+ desc.push_str(&id);
}
}
}
- None
+ unreachable!("Enter mark should paired with node number");
}
}
impl TreeNodeVisitor for ExprIdentifierVisitor<'_> {
type Node = Expr;
fn f_down(&mut self, expr: &Expr) -> Result<TreeNodeRecursion> {
+ // put placeholder, sets the proper array length
+ self.id_array.push((0, "".to_string()));
+
// related to https://github.com/apache/arrow-datafusion/issues/8814
// If the expr contain volatile expression or is a short-circuit expression, skip it.
if expr.short_circuits() || is_volatile_expression(expr)? {
- return Ok(TreeNodeRecursion::Jump);
+ self.visit_stack
+ .push(VisitRecord::JumpMark(self.node_count));
+ return Ok(TreeNodeRecursion::Jump); // go to f_up
}
+
self.visit_stack
.push(VisitRecord::EnterMark(self.node_count));
self.node_count += 1;
- // put placeholder
- self.id_array.push((0, "".to_string()));
+
Ok(TreeNodeRecursion::Continue)
}
fn f_up(&mut self, expr: &Expr) -> Result<TreeNodeRecursion> {
self.series_number += 1;
- let Some((idx, sub_expr_desc)) = self.pop_enter_mark() else {
- return Ok(TreeNodeRecursion::Continue);
- };
Review Comment:
This was one of the bugs. The behavior was changed with the TreeNode refactor. Previously, it was always returning a `sub_expr_desc` => then executing the remaining function block.
The changes I made here was to (1) bring it back closer to the original code, while (2) incorporating the new Jump contract from the consolidated visitor.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531567569
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -719,23 +741,10 @@ impl TreeNodeRewriter for CommonSubexprRewriter<'_> {
));
}
- let (series_number, id) = &self.id_array[self.curr_index];
Review Comment:
This was one of the bugs (see the regression test). It uses a `self.curr_index` which gets incr, when it really needs the current expr node being visited. The fix is to grab all of the `IdArray[self.curr_index]` values once (see new line 717), before all the `self.curr_index` incr shenanigans.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1532640935
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +584,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
Review Comment:
For my understanding (please correct here 🙏🏼 ), prior to refactoring the 2 different visitors (`TreeNodeVisitor` and `TreeNodeRewriter`) had different transversal rules, and the semantics of skip vs stop mapped in weird ways (as [nicely described here](https://github.com/apache/arrow-datafusion/pull/8891#issue-2085713028) in the TreeNode refactor's "Rationale for Change").
Prior to the refactor:
* the first visitor used skips (`VisitRecursion::Skip`), which never calls f_up afterwards.
* the second visitor used `RewriteRecursion::Stop` which is functionally the same as `VisitRecursion::Skip`
### Fix for the first visitor, for the short-circuited nodes:
Prior to the refactor, the first tree walk did not add short-circuited nodes to the stack:
1. short-circuited nodes were [skipped](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L242-L243), without adding to the stack
1. the old semantics meant [skipping the post-visit (up) call](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L26-L27)
1. it never looked in the stack (since no post-visit on the old semantics)
Whereas with the new TreeNode refactor the jump became a skip-children-but-call-f_up, which meant:
1. prior to the fix in this PR, it was using the old control flow. So it still didn't add to visited_stack.
1. refactor means that it now does perform the f_up on jump
1. f_up checks the stack (in `self.pop_enter_mark()`) and would not find it
1. the original refactor got around this by removing the panic and adding an option
**Edited:** For the changes done in the `TreeNodeVisitor` for the short-circuited nodes, I basically made it work with a jump that does the post-visit f_up by:
* add a placeholder to the id_array so that it exists on f_up
* keep the idx counts correct, for that IdArray abstraction
* fixing the stack management:
* push into the stack so it exists on the f_up
* before this bugfix, it was either not finding it in the stack (None), or occasionally matching another/wrong entry in the stack (such as in our bug test case).
* make a `VisitRecord::JumpMark` to be used in the stack
* therefore the stack entry should always exist, including on short-circuited jumps.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "waynexia (via GitHub)" <gi...@apache.org>.
waynexia commented on PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#issuecomment-2012466583
I plan to give it a review later tomorrow. We can merge this first as there is one approval if the release progress is blocked.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1533498046
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +584,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
Review Comment:
Yes, that's how the API worked before and after https://github.com/apache/arrow-datafusion/pull/8891. I think the issue came in with this https://github.com/peter-toth/arrow-datafusion/pull/1 adjustment.
Thanks @wiedld for fixing it!
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1532640935
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +584,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
Review Comment:
For my understanding (please correct here 🙏🏼 ), prior to refactoring the 2 different visitors (`TreeNodeVisitor` and `TreeNodeRewriter`) had different transversal rules, and the semantics of skip vs stop mapped in weird ways (as [nicely described here](https://github.com/apache/arrow-datafusion/pull/8891#issue-2085713028) in the TreeNode refactor's "Rationale for Change").
Prior to the refactor:
* the first visitor used skips (`VisitRecursion::Skip`), which never calls f_up afterwards.
* the second visitor used `RewriteRecursion::Stop` which is functionally the same as `VisitRecursion::Skip`
### Fix for the first visitor, for the short-circuited nodes:
Prior to the refactor, the first tree walk did not add short-circuited nodes to the stack:
1. short-circuited nodes were [skipped](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L242-L243), without adding to the stack
1. the old semantics meant [skipping the post-visit (up) call](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L26-L27)
1. it never looked in the stack (since no post-visit on the old semantics)
Whereas with the new TreeNode refactor the jump became a skip-children-but-call-f_up, which meant:
1. prior to the fix in this PR, it was using the old control flow. So it still didn't add to visited_stack.
1. refactor means that it now does perform the f_up on jump
1. f_up checks the stack (in `self.pop_enter_mark()`) and would not find it
1. the original refactor got around this by removing the panic and adding an option
For the changes done in the `TreeNodeVisitor` for the short-circuited nodes, I basically made it work with a jump that does the post-visit f_up by:
* add a placeholder to the id_array so that it exists on f_up
* push into the stack so it exists on the f_up
* make a `VisitRecord::JumpMark` to be used in the stack
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1532640935
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +584,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
Review Comment:
For my understanding (please correct here 🙏🏼 ), prior to refactoring the 2 different visitors (`TreeNodeVisitor` and `TreeNodeRewriter`) had different transversal rules, and the semantics of skip vs stop mapped in weird ways (as [nicely described here](https://github.com/apache/arrow-datafusion/pull/8891#issue-2085713028) in the TreeNode refactor's "Rationale for Change").
Prior to the refactor:
* the first visitor used skips (`VisitRecursion::Skip`), which never calls f_up afterwards.
* the second visitor used `RewriteRecursion::Stop` which is functionally the same as `VisitRecursion::Skip`
### Fix for the first visitor, for the short-circuited nodes:
Prior to the refactor, the first tree walk did not add short-circuited nodes to the stack:
1. short-circuited nodes were [skipped](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L242-L243), without adding to the stack
1. the old semantics meant [skipping the post-visit (up) call](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L26-L27)
1. it never looked in the stack (since no post-visit on the old semantics)
Whereas with the new TreeNode refactor the jump became a skip-children-but-call-f_up, which meant:
1. prior to the fix in this PR, it was using the old control flow. So it still didn't add to visited_stack.
1. refactor means that it now does perform the f_up on jump
1. f_up checks the stack (in `self.pop_enter_mark()`) and would not find it
1. the original refactor got around this by removing the panic and adding an option
For the changes done in the `TreeNodeVisitor` for the short-circuited nodes, I basically made it work with a jump that does the post-visit f_up by:
* add a placeholder to the id_array so that it exists on f_up
* push into the stack so it exists on the f_up
* before this bugfix, it was finding the last entry into the stack => which was it's prior sibling!
* make a `VisitRecord::JumpMark` to be used in the stack
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531590056
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +580,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
+ JumpMark(usize),
/// Accumulated identifier of sub expression.
ExprItem(Identifier),
}
impl ExprIdentifierVisitor<'_> {
- fn desc_expr(expr: &Expr) -> String {
+ fn node_identifier(expr: &Expr) -> String {
Review Comment:
I renamed this, to make it more explicit that this is the identifier used in the `IdArray` and `ExprSet`.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531575684
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +580,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
+ JumpMark(usize),
/// Accumulated identifier of sub expression.
ExprItem(Identifier),
}
impl ExprIdentifierVisitor<'_> {
- fn desc_expr(expr: &Expr) -> String {
+ fn node_identifier(expr: &Expr) -> String {
format!("{expr}")
}
/// Find the first `EnterMark` in the stack, and accumulates every `ExprItem`
/// before it.
- fn pop_enter_mark(&mut self) -> Option<(usize, Identifier)> {
+ fn pop_enter_mark(&mut self) -> (usize, Identifier) {
let mut desc = String::new();
while let Some(item) = self.visit_stack.pop() {
match item {
- VisitRecord::EnterMark(idx) => {
- return Some((idx, desc));
+ VisitRecord::EnterMark(idx) | VisitRecord::JumpMark(idx) => {
+ return (idx, desc);
}
- VisitRecord::ExprItem(s) => {
- desc.push_str(&s);
+ VisitRecord::ExprItem(id) => {
+ desc.push_str(&id);
}
}
}
- None
+ unreachable!("Enter mark should paired with node number");
Review Comment:
Bring back the pop_enter_mark() contract to the original (before the TreeNode refactor), while also adding in a new JumpMark.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1532640935
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +584,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
Review Comment:
For my understanding (please correct here 🙏🏼 ), prior to refactoring the 2 different visitors (`TreeNodeVisitor` and `TreeNodeRewriter`) had different transversal rules, and the semantics of skip vs stop mapped in weird ways (as [nicely described here](https://github.com/apache/arrow-datafusion/pull/8891#issue-2085713028) in the TreeNode refactor's "Rationale for Change").
Prior to the refactor:
* the first visitor used skips (`VisitRecursion::Skip`), which never calls f_up afterwards.
* the second visitor used `RewriteRecursion::Stop` which is functionally the same as `VisitRecursion::Skip`
### Fix for the first visitor, for the short-circuited nodes:
Prior to the refactor, the first tree walk did not add short-circuited nodes to the stack:
1. short-circuited nodes were [skipped](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L242-L243), without adding to the stack
1. the old semantics meant [skipping the post-visit (up) call](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L26-L27)
1. it never looked in the stack (since no post-visit on the old semantics)
Whereas with the new TreeNode refactor the jump became a skip-children-but-call-f_up, which meant:
1. prior to the fix in this PR, it was using the old control flow. So it still didn't add to visited_stack.
1. refactor means that it now does perform the f_up on jump
1. f_up checks the stack (in `self.pop_enter_mark()`) and would not find it
1. the original refactor got around this by removing the panic and adding an option
For the changes done in the `TreeNodeVisitor` for the short-circuited nodes, I basically made it work with a jump that does the post-visit f_up by:
* add a placeholder to the id_array so that it exists on f_up
* keep the idx counts correct, as needed for that IdArray abstraction
* push into the stack so it exists on the f_up
* before this bugfix, it was occasionally matching an entry in the stack, but it could be the wrong match (such as in our test case).
* make a `VisitRecord::JumpMark` to be used in the stack
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531573732
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +580,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
+ JumpMark(usize),
/// Accumulated identifier of sub expression.
ExprItem(Identifier),
}
impl ExprIdentifierVisitor<'_> {
- fn desc_expr(expr: &Expr) -> String {
+ fn node_identifier(expr: &Expr) -> String {
format!("{expr}")
}
/// Find the first `EnterMark` in the stack, and accumulates every `ExprItem`
/// before it.
- fn pop_enter_mark(&mut self) -> Option<(usize, Identifier)> {
+ fn pop_enter_mark(&mut self) -> (usize, Identifier) {
let mut desc = String::new();
while let Some(item) = self.visit_stack.pop() {
match item {
- VisitRecord::EnterMark(idx) => {
- return Some((idx, desc));
+ VisitRecord::EnterMark(idx) | VisitRecord::JumpMark(idx) => {
+ return (idx, desc);
}
- VisitRecord::ExprItem(s) => {
- desc.push_str(&s);
+ VisitRecord::ExprItem(id) => {
+ desc.push_str(&id);
}
}
}
- None
+ unreachable!("Enter mark should paired with node number");
}
}
impl TreeNodeVisitor for ExprIdentifierVisitor<'_> {
type Node = Expr;
fn f_down(&mut self, expr: &Expr) -> Result<TreeNodeRecursion> {
+ // put placeholder, sets the proper array length
+ self.id_array.push((0, "".to_string()));
+
// related to https://github.com/apache/arrow-datafusion/issues/8814
// If the expr contain volatile expression or is a short-circuit expression, skip it.
if expr.short_circuits() || is_volatile_expression(expr)? {
- return Ok(TreeNodeRecursion::Jump);
+ self.visit_stack
+ .push(VisitRecord::JumpMark(self.node_count));
+ return Ok(TreeNodeRecursion::Jump); // go to f_up
}
+
self.visit_stack
.push(VisitRecord::EnterMark(self.node_count));
self.node_count += 1;
- // put placeholder
- self.id_array.push((0, "".to_string()));
+
Ok(TreeNodeRecursion::Continue)
}
fn f_up(&mut self, expr: &Expr) -> Result<TreeNodeRecursion> {
self.series_number += 1;
- let Some((idx, sub_expr_desc)) = self.pop_enter_mark() else {
- return Ok(TreeNodeRecursion::Continue);
- };
Review Comment:
This was one of the bugs. The behavior was changes with the TreeNode visitor. Previously, it was always returning a `sub_expr_desc` => then executing the remaining function code.
The changes I made here was to (1) bring it back closer to the original code, while (2) halting the new Jump contract from the consolidated visitor.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531590056
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +580,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
+ JumpMark(usize),
/// Accumulated identifier of sub expression.
ExprItem(Identifier),
}
impl ExprIdentifierVisitor<'_> {
- fn desc_expr(expr: &Expr) -> String {
+ fn node_identifier(expr: &Expr) -> String {
Review Comment:
I renamed this, to make it more explicit that this is the identifier used in the `IdArray` and `ExprSet`.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531567569
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -719,23 +741,10 @@ impl TreeNodeRewriter for CommonSubexprRewriter<'_> {
));
}
- let (series_number, id) = &self.id_array[self.curr_index];
Review Comment:
This was one of the bugs (see the regression test). It uses a `self.curr_index` which has already been incr, when it really needs the original `self.curr_index` for the current expr node being visited. The fix is to grab all of the `IdArray[self.curr_index]` values once (see new line 717), before all the `self.curr_index` incr shenanigans.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1532048878
##########
datafusion/sqllogictest/test_files/common_subexpr_eliminate.slt:
##########
@@ -0,0 +1,106 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+#############
+## Common Subexpr Eliminate Tests
+#############
+
+statement ok
Review Comment:
I recommend we move this test out of its own .slt file and into https://github.com/apache/arrow-datafusion/blob/main/datafusion/sqllogictest/test_files/expr.slt so it is eaiser to find
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -532,18 +545,18 @@ impl ExprMask {
/// Go through an expression tree and generate identifier.
///
/// An identifier contains information of the expression itself and its sub-expression.
-/// This visitor implementation use a stack `visit_stack` to track traversal, which
+/// This visitor implementation use a stack `f_down()` to track traversal, which
Review Comment:
I think the original was correct (the stack was called `visit_stack`) -- it seems like the stack itself is created on `f_down`
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -693,20 +713,26 @@ impl TreeNodeRewriter for CommonSubexprRewriter<'_> {
if expr.short_circuits() || is_volatile_expression(&expr)? {
return Ok(Transformed::new(expr, false, TreeNodeRecursion::Jump));
}
+
+ let (series_number, curr_id) = &self.id_array[self.curr_index];
Review Comment:
❤️
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +584,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
Review Comment:
This is the key fix, as I understand it -- the TreeNodeVisitor rewrite removed the notion of skipping sibling nodes during recursion, so this notion must be explicitly encoded in the subexpression rewrite pass.
##########
datafusion/sqllogictest/test_files/common_subexpr_eliminate.slt:
##########
@@ -0,0 +1,106 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+#############
+## Common Subexpr Eliminate Tests
+#############
+
+statement ok
+CREATE TABLE doubles (
+ f64 DOUBLE
+) as VALUES
+ (10.1)
+;
+
+# common subexpr with alias
+query RRR rowsort
+select f64, round(1.0 / f64) as i64_1, acos(round(1.0 / f64)) from doubles;
+----
+10.1 0 1.570796326795
+
+# common subexpr with coalesce (short-circuited)
+query RRR rowsort
+select f64, coalesce(1.0 / f64, 0.0), acos(coalesce(1.0 / f64, 0.0)) from doubles;
+----
+10.1 0.09900990099 1.471623942989
+
+# common subexpr with coalesce (short-circuited) and alias
+query RRR rowsort
+select f64, coalesce(1.0 / f64, 0.0) as f64_1, acos(coalesce(1.0 / f64, 0.0)) from doubles;
+----
+10.1 0.09900990099 1.471623942989
+
+# common subexpr with case (short-circuited)
+query RRR rowsort
+select f64, case when f64 > 0 then 1.0 / f64 else null end, acos(case when f64 > 0 then 1.0 / f64 else null end) from doubles;
+----
+10.1 0.09900990099 1.471623942989
+
+
+statement ok
Review Comment:
I believe this test is a duplicate of something elsewhere. While it was valuable for debugging I think we should remove the duplication to make the code easier to maintain over the long run
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -42,6 +41,15 @@ use datafusion_expr::{col, Expr, ExprSchemable};
/// - DataType of this expression.
type ExprSet = HashMap<Identifier, (Expr, usize, DataType)>;
+/// An ordered map of Identifiers encountered during visitation.
+///
+/// Is created in the ExprIdentifierVisitor, which identifies the common expressions.
+/// Is consumed in the CommonSubexprRewriter, which performs mutations.
+///
+/// - series_number. increased in fn_up, start from 1.
+/// - Identifier. is empty ("") if expr should not be considered for common elimation.
+type IdArray = Vec<(usize, Identifier)>;
Review Comment:
THank you -- I agree introducing a new typedef helps to make the code eaiser to understand
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1531567569
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -719,23 +741,10 @@ impl TreeNodeRewriter for CommonSubexprRewriter<'_> {
));
}
- let (series_number, id) = &self.id_array[self.curr_index];
Review Comment:
This was one of the bugs (see the regression test). It uses an already incremented `self.curr_index`. The fix is to grab all of the values once (see new line 717), before all the `self.curr_index` shenanigans.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1532509914
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -532,18 +545,18 @@ impl ExprMask {
/// Go through an expression tree and generate identifier.
///
/// An identifier contains information of the expression itself and its sub-expression.
-/// This visitor implementation use a stack `visit_stack` to track traversal, which
+/// This visitor implementation use a stack `f_down()` to track traversal, which
Review Comment:
nice catch.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1532640935
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +584,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
Review Comment:
For my understanding (please correct here 🙏🏼 ), prior to refactoring the 2 different visitors (`TreeNodeVisitor` and `TreeNodeRewriter`) had different transversal rules, and the semantics of skip vs stop mapped in weird ways (as [nicely described here](https://github.com/apache/arrow-datafusion/pull/8891#issue-2085713028) in the TreeNode refactor's "Rationale for Change").
Prior to the refactor:
* the first visitor used skips (which never calls f_up afterwards)
* the second visitor used stops (which can skip siblings)
### Fix for the first visitor, for the short-circuited nodes:
Prior to the refactor, the first tree walk did not add short-circuited nodes to the stack:
1. short-circuited nodes were [skipped](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L242-L243), without adding to the stack
1. the old semantics meant [skipping the post-visit (up) call](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L26-L27)
1. it never looked in the stack (since no post-visit on the old semantics)
Whereas with the new TreeNode refactor the jump became a skip-children-but-call-f_up, which meant:
1. prior to the fix in this PR, it was using the old control flow. So it still didn't add to visited_stack.
1. refactor means that it now does perform the f_up on jump
1. f_up checks the stack (in `self.pop_enter_mark()`) and would not find it
1. the original refactor got around this by removing the panic and adding an option
For the changes done in the `TreeNodeVisitor` for the short-circuited nodes, I basically made it work with a jump that does the post-visit f_up by:
* add a placeholder to the id_array so that it exists on f_up
* push into the stack so it exists on the f_up
* make a `VisitRecord::JumpMark` to be used in the stack
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +584,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
Review Comment:
For my understanding (please correct here 🙏🏼 ), prior to refactoring the 2 different visitors (`TreeNodeVisitor` and `TreeNodeRewriter`) had different transversal rules, and the semantics of skip vs stop mapped in weird ways (as [nicely described here](https://github.com/apache/arrow-datafusion/pull/8891#issue-2085713028) in the TreeNode refactor's "Rationale for Change").
Prior to the refactor:
* the first visitor used skips (which never calls f_up afterwards)
* the second visitor used stops (which can miss siblings)
### Fix for the first visitor, for the short-circuited nodes:
Prior to the refactor, the first tree walk did not add short-circuited nodes to the stack:
1. short-circuited nodes were [skipped](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L242-L243), without adding to the stack
1. the old semantics meant [skipping the post-visit (up) call](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L26-L27)
1. it never looked in the stack (since no post-visit on the old semantics)
Whereas with the new TreeNode refactor the jump became a skip-children-but-call-f_up, which meant:
1. prior to the fix in this PR, it was using the old control flow. So it still didn't add to visited_stack.
1. refactor means that it now does perform the f_up on jump
1. f_up checks the stack (in `self.pop_enter_mark()`) and would not find it
1. the original refactor got around this by removing the panic and adding an option
For the changes done in the `TreeNodeVisitor` for the short-circuited nodes, I basically made it work with a jump that does the post-visit f_up by:
* add a placeholder to the id_array so that it exists on f_up
* push into the stack so it exists on the f_up
* make a `VisitRecord::JumpMark` to be used in the stack
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1532640935
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +584,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
Review Comment:
For my understanding (please correct here 🙏🏼 ), prior to refactoring the 2 different visitors (`TreeNodeVisitor` and `TreeNodeRewriter`) had different transversal rules, and the semantics of skip vs stop mapped in weird ways (as [nicely described here](https://github.com/apache/arrow-datafusion/pull/8891#issue-2085713028) in the TreeNode refactor's "Rationale for Change").
Prior to the refactor:
* the first visitor used skips (`VisitRecursion::Skip`), which never calls f_up afterwards.
* the second visitor used `RewriteRecursion::Stop` which is functionally the same as `VisitRecursion::Skip`
### Fix for the first visitor, for the short-circuited nodes:
Prior to the refactor, the first tree walk did not add short-circuited nodes to the stack:
1. short-circuited nodes were [skipped](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L242-L243), without adding to the stack
1. the old semantics meant [skipping the post-visit (up) call](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L26-L27)
1. it never looked in the stack (since no post-visit on the old semantics)
Whereas with the new TreeNode refactor the jump became a skip-children-but-call-f_up, which meant:
1. prior to the fix in this PR, it was using the old control flow. So it still didn't add to visited_stack.
1. refactor means that it now does perform the f_up on jump
1. f_up checks the stack (in `self.pop_enter_mark()`) and would not find it
1. the original refactor got around this by removing the panic and adding an option
For the changes done in the `TreeNodeVisitor` for the short-circuited nodes, I basically made it work with a jump that does the post-visit f_up by:
* add a placeholder to the id_array so that it exists on f_up
* push into the stack so it exists on the f_up
* before this bugfix, it was occasionally (e.g. our test case) finding the last entry into the stack => which was it's prior sibling!
* make a `VisitRecord::JumpMark` to be used in the stack
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1532640935
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +584,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
Review Comment:
For my understanding (please correct here 🙏🏼 ), prior to refactoring the 2 different visitors (`TreeNodeVisitor` and `TreeNodeRewriter`) had different transversal rules, and the semantics of skip vs stop mapped in weird ways (as [nicely described here](https://github.com/apache/arrow-datafusion/pull/8891#issue-2085713028) in the TreeNode refactor's "Rationale for Change").
Prior to the refactor:
* the first visitor used skips (`VisitRecursion::Skip`), which never calls f_up afterwards.
* the second visitor used `RewriteRecursion::Stop` which is functionally the same as `VisitRecursion::Skip`
### Fix for the first visitor, for the short-circuited nodes:
Prior to the refactor, the first tree walk did not add short-circuited nodes to the stack:
1. short-circuited nodes were [skipped](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L242-L243), without adding to the stack
1. the old semantics meant [skipping the post-visit (up) call](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L26-L27)
1. it never looked in the stack (since no post-visit on the old semantics)
Whereas with the new TreeNode refactor the jump became a skip-children-but-call-f_up, which meant:
1. prior to the fix in this PR, it was using the old control flow. So it still didn't add to visited_stack.
1. refactor means that it now does perform the f_up on jump
1. f_up checks the stack (in `self.pop_enter_mark()`) and would not find it
1. the original refactor got around this by removing the panic and adding an option
**Edited:** For the changes done in the `TreeNodeVisitor` for the short-circuited nodes, I basically made it work with a jump that does the post-visit f_up:
* add a placeholder to the id_array so that it exists on f_up
* keep the idx counts correct, for that IdArray abstraction
* fixing the stack management:
* push into the stack so it exists on the f_up
* before this bugfix, it was either not finding it in the stack (None), or occasionally matching another/wrong entry in the stack (such as in our bug test case).
* make a `VisitRecord::JumpMark` to be used in the stack
* therefore the stack entry should always exist, including on short-circuited jumps.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1532640935
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +584,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
Review Comment:
For my understanding (please correct here 🙏🏼 ), prior to refactoring the 2 different visitors (`TreeNodeVisitor` and `TreeNodeRewriter`) had different transversal rules, and the semantics of skip vs stop mapped in weird ways (as [nicely described here](https://github.com/apache/arrow-datafusion/pull/8891#issue-2085713028) in the TreeNode refactor's "Rationale for Change").
Prior to the refactor:
* the first visitor used skips (`VisitRecursion::Skip`), which never calls f_up afterwards.
* the second visitor used `RewriteRecursion::Stop` which is functionally the same as `VisitRecursion::Skip`
### Fix for the first visitor, for the short-circuited nodes:
Prior to the refactor, the first tree walk did not add short-circuited nodes to the stack:
1. short-circuited nodes were [skipped](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L242-L243), without adding to the stack
1. the old semantics meant [skipping the post-visit (up) call](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L26-L27)
1. it never looked in the stack (since no post-visit on the old semantics)
Whereas with the new TreeNode refactor the jump became a skip-children-but-call-f_up, which meant:
1. prior to the fix in this PR, it was using the old control flow. So it still didn't add to visited_stack.
1. refactor means that it now does perform the f_up on jump
1. f_up checks the stack (in `self.pop_enter_mark()`) and would not find it
1. the original refactor got around this by removing the panic and adding an option
**Edited:** For the changes done in the `TreeNodeVisitor` for the short-circuited nodes, I basically made it work with a jump that does the post-visit f_up by:
* add a placeholder to the id_array so that it exists on f_up
* keep the idx counts correct, for that IdArray abstraction
* fixing the stack management:
* push into the stack so it exists on the f_up
* before this bugfix, it was either not finding it in the stack (None), or occasionally matching another/wrong entry in the stack (such as in our bug test case).
* make a `VisitRecord::JumpMark` to be used in the stack
* therefore the stack entry should always exist, including on jump.
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] fix(9678): short circuiting prevented population of visited stack, for common subexpr elimination optimization [arrow-datafusion]
Posted by "wiedld (via GitHub)" <gi...@apache.org>.
wiedld commented on code in PR #9685:
URL: https://github.com/apache/arrow-datafusion/pull/9685#discussion_r1532640935
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -571,66 +584,73 @@ enum VisitRecord {
/// `usize` is the monotone increasing series number assigned in pre_visit().
/// Starts from 0. Is used to index the identifier array `id_array` in post_visit().
EnterMark(usize),
+ /// the node's children were skipped => jump to f_up on same node
Review Comment:
For my understanding (please correct here 🙏🏼 ), prior to refactoring the 2 different visitors (`TreeNodeVisitor` and `TreeNodeRewriter`) had different transversal rules, and the semantics of skip vs stop mapped in weird ways (as [nicely described here](https://github.com/apache/arrow-datafusion/pull/8891#issue-2085713028) in the TreeNode refactor's "Rationale for Change").
Prior to the refactor:
* the first visitor used skips (`VisitRecursion::Skip`), which never calls f_up afterwards.
* the second visitor used `RewriteRecursion::Stop` which is functionally the same as `VisitRecursion::Skip`
### Fix for the first visitor, for the short-circuited nodes:
Prior to the refactor, the first tree walk did not add short-circuited nodes to the stack:
1. short-circuited nodes were [skipped](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L242-L243), without adding to the stack
1. the old semantics meant [skipping the post-visit (up) call](https://github.com/apache/arrow-datafusion/blob/684b4fab75841672aabc4f3c2475e8c232acea20/datafusion/common/src/tree_node.rs#L26-L27)
1. it never looked in the stack (since no post-visit on the old semantics)
Whereas with the new TreeNode refactor the jump became a skip-children-but-call-f_up, which meant:
1. prior to the fix in this PR, it was using the old control flow. So it still didn't add to visited_stack.
1. refactor means that it now does perform the f_up on jump
1. f_up checks the stack (in `self.pop_enter_mark()`) and would not find it
1. the original refactor got around this by removing the panic and adding an option
For the changes done in the `TreeNodeVisitor` for the short-circuited nodes, I basically made it work with a jump that does the post-visit f_up by:
* add a placeholder to the id_array so that it exists on f_up
* keep the idx counts correct, as needed for that IdArray abstraction
* push into the stack so it exists on the f_up
* before this bugfix, it was returning None but also occasionally matching another entry in the stack (but it could be the wrong match, such as in our test case).
* make a `VisitRecord::JumpMark` to be used in the stack
--
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@arrow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org