Skip to content

Commit 9ae9035

Browse files
authored
refactor(cubesql): Move all wrapper replacers params to separate enode (#9105)
Move all replacer parameters from `WrapperPushdownReplacer` and `WrapperPullupReplacer` to separate node variant `WrapperReplacerContext`. Because of the way our current `plan_to_language!` works, for every non-recusive param in input enum it generates separate wrapper struct on Rust side. So, before this we had separate `WrapperPushdownReplacerPushToCube` and `WrapperPullupReplacerPushToCube`. And, because they are separate terminal variants, one can't just copy those in patterns, and had to do it in Rust code, like in `transforming_rewrite`. This change allows: * Copy context parts between push-down and pull-up replacers directly in patterns * Completely ignore context in patterns, like `wrapper_pushdown_replacer(cast_expr("?expr", "?data_type"), "?context")` => `cast_expr(wrapper_pushdown_replacer("?expr", "?context"), "?data_type")` * Share same eclass with context for a whole replacer stage, instead of one for push-down and one for pull-up All of this simplifies adding new parameters, and have measurable performance boost. On my laptop I measured from about 1 to 11% boost, depending on a benchmark. For example: ``` power_bi_wrap time: [38.386 ms 38.482 ms 38.624 ms] change: [-4.7016% -4.3391% -3.9957%] (p = 0.00 < 0.05) Performance has improved. ... large_model_1000_select_all_dimensions_with_filter time: [190.47 ms 190.88 ms 191.17 ms] change: [-12.353% -11.824% -11.374%] (p = 0.00 < 0.05) Performance has improved. ```
1 parent fc5efb2 commit 9ae9035

31 files changed

+1426
-2706
lines changed

rust/cubesql/cubesql/src/compile/rewrite/mod.rs

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,10 @@ crate::plan_to_language! {
454454
members: Vec<LogicalPlan>,
455455
alias_to_cube: Vec<(String, String)>,
456456
},
457-
WrapperPushdownReplacer {
458-
member: Arc<LogicalPlan>,
457+
WrapperReplacerContext {
459458
alias_to_cube: Vec<(String, String)>,
460-
// This means that result of this replacer would be used as member expression in load query to Cube.
461-
// This flag should be passed from top, by the rule that starts wrapping new logical plan node.
459+
// When `member` is expression this means that result of this replacer should be used as member expression in load query to Cube.
460+
// When `member` is logical plan node this means that logical plan inside allows to push to Cube
462461
// Important caveat: it means that result would be used for push to cube *and only there*.
463462
// So it's more like "must push to Cube" than "can push to Cube"
464463
// This part is important for rewrites like SUM(sumMeasure) => sumMeasure
@@ -471,23 +470,19 @@ crate::plan_to_language! {
471470
// Used to allow to rewrite columns from them even with push to Cube enabled
472471
grouped_subqueries: Vec<String>,
473472
},
473+
WrapperPushdownReplacer {
474+
member: Arc<LogicalPlan>,
475+
// Only WrapperReplacerContext should be allowed here
476+
// Context be passed from top, by the rule that starts wrapping new logical plan node,
477+
// and should make roundtrip from top to bottom and back.
478+
context: Arc<LogicalPlan>,
479+
},
474480
WrapperPullupReplacer {
475481
member: Arc<LogicalPlan>,
476-
alias_to_cube: Vec<(String, String)>,
477-
// When `member` is expression this means that result of this replacer should be used as member expression in load query to Cube.
478-
// When `member` is logical plan node this means that logical plan inside allows to push to Cube
479-
// This flag should make roundtrip from top to bottom and back.
480-
// Important caveat: it means that result should be used for push to cube *and only there*.
481-
// So it's more like "must push to Cube" than "can push to Cube"
482-
// This part is important for rewrites like SUM(sumMeasure) => sumMeasure
483-
// We can use sumMeasure instead of SUM(sumMeasure) ONLY in with push to Cube
484-
// An vice versa, we can't use SUM(sumMeasure) in grouped query to Cube, so it can be allowed ONLY without push to grouped Cube query
485-
push_to_cube: bool,
486-
in_projection: bool,
487-
cube_members: Vec<LogicalPlan>,
488-
// Known qualifiers of grouped subqueries
489-
// Used to allow to rewrite columns from them even with push to Cube enabled
490-
grouped_subqueries: Vec<String>,
482+
// Only WrapperReplacerContext should be allowed here
483+
// Context be passed from top, by the rule that starts wrapping new logical plan node,
484+
// and should make roundtrip from top to bottom and back.
485+
context: Arc<LogicalPlan>,
491486
},
492487
FlattenPushdownReplacer {
493488
expr: Arc<Expr>,
@@ -1973,30 +1968,24 @@ fn case_expr_replacer(members: impl Display, alias_to_cube: impl Display) -> Str
19731968
format!("(CaseExprReplacer {} {})", members, alias_to_cube)
19741969
}
19751970

1976-
fn wrapper_pushdown_replacer(
1977-
members: impl Display,
1971+
fn wrapper_replacer_context(
19781972
alias_to_cube: impl Display,
19791973
push_to_cube: impl Display,
19801974
in_projection: impl Display,
19811975
cube_members: impl Display,
19821976
grouped_subqueries: impl Display,
19831977
) -> String {
19841978
format!(
1985-
"(WrapperPushdownReplacer {members} {alias_to_cube} {push_to_cube} {in_projection} {cube_members} {grouped_subqueries})",
1979+
"(WrapperReplacerContext {alias_to_cube} {push_to_cube} {in_projection} {cube_members} {grouped_subqueries})",
19861980
)
19871981
}
19881982

1989-
fn wrapper_pullup_replacer(
1990-
members: impl Display,
1991-
alias_to_cube: impl Display,
1992-
push_to_cube: impl Display,
1993-
in_projection: impl Display,
1994-
cube_members: impl Display,
1995-
grouped_subqueries: impl Display,
1996-
) -> String {
1997-
format!(
1998-
"(WrapperPullupReplacer {members} {alias_to_cube} {push_to_cube} {in_projection} {cube_members} {grouped_subqueries})",
1999-
)
1983+
fn wrapper_pushdown_replacer(members: impl Display, context: impl Display) -> String {
1984+
format!("(WrapperPushdownReplacer {members} {context})",)
1985+
}
1986+
1987+
fn wrapper_pullup_replacer(members: impl Display, context: impl Display) -> String {
1988+
format!("(WrapperPullupReplacer {members} {context})",)
20001989
}
20011990

20021991
fn flatten_pushdown_replacer(

0 commit comments

Comments
 (0)