haohuaijin commented on code in PR #11197:
URL: https://github.com/apache/datafusion/pull/11197#discussion_r1662621500
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -1012,19 +1013,22 @@ impl TreeNodeRewriter for CommonSubexprRewriter<'_, '_>
{
self.alias_counter += 1;
}
- // The `CommonSubexprRewriter` relies on `ExprIdentifierVisitor` to
generate
- // the `id_array`, which records the expr's identifier used to rewrite
expr. So if we
+ // The `CommonSubexprRewriter` relies on `ExprIdentifierVisitor` to
generate the
+ // `id_array`, which records the expr's identifier used to rewrite
expr. So if we
// skip an expr in `ExprIdentifierVisitor`, we should skip it here,
too.
- if expr.short_circuits() {
- return Ok(Transformed::new(expr, false, TreeNodeRecursion::Jump));
- }
+ let is_tree = expr.short_circuits();
+ let tnr = if is_tree {
+ TreeNodeRecursion::Jump
Review Comment:
maybe we can rename `is_tree` to `is_short_circuits`
##########
datafusion/optimizer/src/common_subexpr_eliminate.rs:
##########
@@ -1799,4 +1803,34 @@ mod test {
assert!(result.len() == 1);
Ok(())
}
+
+ #[test]
+ fn test_short_circuits() -> Result<()> {
+ let table_scan = test_table_scan()?;
+
+ let extracted_short_circuit =
col("a").eq(lit(0)).or(col("b").eq(lit(0)));
+ let not_extracted_short_circuit_leg = (col("a") + col("b")).eq(lit(0));
+ let plan = LogicalPlanBuilder::from(table_scan.clone())
+ .project(vec![
+ extracted_short_circuit.clone().alias("c1"),
+ extracted_short_circuit.alias("c2"),
+ col("c")
+ .gt(lit(0))
+ .or(not_extracted_short_circuit_leg.clone())
+ .alias("c3"),
+ col("c")
+ .gt(lit(1))
+ .or(not_extracted_short_circuit_leg)
+ .alias("c4"),
+ ])?
+ .build()?;
+
+ let expected = "Projection: __common_expr_1 AS c1, __common_expr_1 AS
c2, test.c > Int32(0) OR test.a + test.b = Int32(0) AS c3, test.c > Int32(1) OR
test.a + test.b = Int32(0) AS c4\
+ \n Projection: test.a = Int32(0) OR test.b = Int32(0) AS
__common_expr_1, test.a, test.b, test.c\
+ \n TableScan: test";
+
+ assert_optimized_plan_eq(expected, plan, None);
+
+ Ok(())
+ }
Review Comment:
Can we add a test case like the one below to check if (a or b) can be
extracted as a common subexpr?
```
select ((a or b) or d) as f1, ((a or b) or c) as f2 from t;
```
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]