Skip to content

Commit c5c9d3f

Browse files
authored
Minor: improve documentation for CommonSubexprEliminate (#9700)
1 parent edaf235 commit c5c9d3f

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

datafusion/optimizer/src/common_subexpr_eliminate.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,32 @@ type ExprSet = HashMap<Identifier, (Expr, usize, DataType)>;
5353
/// here is not such a good choose.
5454
type Identifier = String;
5555

56-
/// Perform Common Sub-expression Elimination optimization.
56+
/// Performs Common Sub-expression Elimination optimization.
5757
///
58-
/// Currently only common sub-expressions within one logical plan will
58+
/// This optimization improves query performance by computing expressions that
59+
/// appear more than once and reusing those results rather than re-computing the
60+
/// same value
61+
///
62+
/// Currently only common sub-expressions within a single `LogicalPlan` are
5963
/// be eliminated.
64+
///
65+
/// # Example
66+
///
67+
/// Given a projection that computes the same expensive expression
68+
/// multiple times such as parsing as string as a date with `to_date` twice:
69+
///
70+
/// ```text
71+
/// ProjectionExec(expr=[extract (day from to_date(c1)), extract (year from to_date(c1))])
72+
/// ```
73+
///
74+
/// This optimization will rewrite the plan to compute the common expression once
75+
/// using a new `ProjectionExec` and then rewrite the original expressions to
76+
/// refer to that new column.
77+
///
78+
/// ```text
79+
/// ProjectionExec(exprs=[extract (day from new_col), extract (year from new_col)]) <-- reuse here
80+
/// ProjectionExec(exprs=[to_date(c1) as new_col]) <-- compute to_date once
81+
/// ```
6082
pub struct CommonSubexprEliminate {}
6183

6284
impl CommonSubexprEliminate {

0 commit comments

Comments
 (0)