From 4772d355e1c0b4952c95f974c370b61ee50be262 Mon Sep 17 00:00:00 2001 From: Mikhail Cheshkov Date: Tue, 10 Dec 2024 02:31:08 +0200 Subject: [PATCH] refactor(cubesql): Move all wrapper replacers params to separate enode --- .../cubesql/src/compile/rewrite/mod.rs | 55 +- .../rewrite/rules/wrapper/aggregate.rs | 645 +++++++++--------- .../rules/wrapper/aggregate_function.rs | 44 +- .../compile/rewrite/rules/wrapper/alias.rs | 42 +- .../rewrite/rules/wrapper/binary_expr.rs | 63 +- .../src/compile/rewrite/rules/wrapper/case.rs | 84 +-- .../src/compile/rewrite/rules/wrapper/cast.rs | 42 +- .../compile/rewrite/rules/wrapper/column.rs | 151 ++-- .../rules/wrapper/cube_scan_wrapper.rs | 40 +- .../compile/rewrite/rules/wrapper/distinct.rs | 12 +- .../compile/rewrite/rules/wrapper/extract.rs | 41 +- .../compile/rewrite/rules/wrapper/filter.rs | 323 +++++---- .../rewrite/rules/wrapper/in_list_expr.rs | 133 +--- .../rewrite/rules/wrapper/in_subquery_expr.rs | 107 +-- .../rewrite/rules/wrapper/is_null_expr.rs | 88 +-- .../src/compile/rewrite/rules/wrapper/join.rs | 247 ++++--- .../rewrite/rules/wrapper/like_expr.rs | 65 +- .../compile/rewrite/rules/wrapper/limit.rs | 12 +- .../compile/rewrite/rules/wrapper/literal.rs | 138 +--- .../src/compile/rewrite/rules/wrapper/mod.rs | 271 +------- .../rewrite/rules/wrapper/negative_expr.rs | 46 +- .../compile/rewrite/rules/wrapper/not_expr.rs | 46 +- .../compile/rewrite/rules/wrapper/order.rs | 148 +--- .../rewrite/rules/wrapper/projection.rs | 300 ++++---- .../rewrite/rules/wrapper/scalar_function.rs | 206 +----- .../rewrite/rules/wrapper/sort_expr.rs | 36 +- .../compile/rewrite/rules/wrapper/subquery.rs | 91 +-- .../rewrite/rules/wrapper/udf_function.rs | 168 +---- .../compile/rewrite/rules/wrapper/window.rs | 148 +--- .../rewrite/rules/wrapper/window_function.rs | 86 +-- .../rewrite/rules/wrapper/wrapper_pull_up.rs | 254 ++++--- 31 files changed, 1426 insertions(+), 2706 deletions(-) diff --git a/rust/cubesql/cubesql/src/compile/rewrite/mod.rs b/rust/cubesql/cubesql/src/compile/rewrite/mod.rs index 61c96bc63e3e0..cef3544c46e0f 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/mod.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/mod.rs @@ -454,11 +454,10 @@ crate::plan_to_language! { members: Vec, alias_to_cube: Vec<(String, String)>, }, - WrapperPushdownReplacer { - member: Arc, + WrapperReplacerContext { alias_to_cube: Vec<(String, String)>, - // This means that result of this replacer would be used as member expression in load query to Cube. - // This flag should be passed from top, by the rule that starts wrapping new logical plan node. + // When `member` is expression this means that result of this replacer should be used as member expression in load query to Cube. + // When `member` is logical plan node this means that logical plan inside allows to push to Cube // Important caveat: it means that result would be used for push to cube *and only there*. // So it's more like "must push to Cube" than "can push to Cube" // This part is important for rewrites like SUM(sumMeasure) => sumMeasure @@ -471,23 +470,19 @@ crate::plan_to_language! { // Used to allow to rewrite columns from them even with push to Cube enabled grouped_subqueries: Vec, }, + WrapperPushdownReplacer { + member: Arc, + // Only WrapperReplacerContext should be allowed here + // Context be passed from top, by the rule that starts wrapping new logical plan node, + // and should make roundtrip from top to bottom and back. + context: Arc, + }, WrapperPullupReplacer { member: Arc, - alias_to_cube: Vec<(String, String)>, - // When `member` is expression this means that result of this replacer should be used as member expression in load query to Cube. - // When `member` is logical plan node this means that logical plan inside allows to push to Cube - // This flag should make roundtrip from top to bottom and back. - // Important caveat: it means that result should be used for push to cube *and only there*. - // So it's more like "must push to Cube" than "can push to Cube" - // This part is important for rewrites like SUM(sumMeasure) => sumMeasure - // We can use sumMeasure instead of SUM(sumMeasure) ONLY in with push to Cube - // 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 - push_to_cube: bool, - in_projection: bool, - cube_members: Vec, - // Known qualifiers of grouped subqueries - // Used to allow to rewrite columns from them even with push to Cube enabled - grouped_subqueries: Vec, + // Only WrapperReplacerContext should be allowed here + // Context be passed from top, by the rule that starts wrapping new logical plan node, + // and should make roundtrip from top to bottom and back. + context: Arc, }, FlattenPushdownReplacer { expr: Arc, @@ -1973,8 +1968,7 @@ fn case_expr_replacer(members: impl Display, alias_to_cube: impl Display) -> Str format!("(CaseExprReplacer {} {})", members, alias_to_cube) } -fn wrapper_pushdown_replacer( - members: impl Display, +fn wrapper_replacer_context( alias_to_cube: impl Display, push_to_cube: impl Display, in_projection: impl Display, @@ -1982,21 +1976,16 @@ fn wrapper_pushdown_replacer( grouped_subqueries: impl Display, ) -> String { format!( - "(WrapperPushdownReplacer {members} {alias_to_cube} {push_to_cube} {in_projection} {cube_members} {grouped_subqueries})", + "(WrapperReplacerContext {alias_to_cube} {push_to_cube} {in_projection} {cube_members} {grouped_subqueries})", ) } -fn wrapper_pullup_replacer( - members: impl Display, - alias_to_cube: impl Display, - push_to_cube: impl Display, - in_projection: impl Display, - cube_members: impl Display, - grouped_subqueries: impl Display, -) -> String { - format!( - "(WrapperPullupReplacer {members} {alias_to_cube} {push_to_cube} {in_projection} {cube_members} {grouped_subqueries})", - ) +fn wrapper_pushdown_replacer(members: impl Display, context: impl Display) -> String { + format!("(WrapperPushdownReplacer {members} {context})",) +} + +fn wrapper_pullup_replacer(members: impl Display, context: impl Display) -> String { + format!("(WrapperPullupReplacer {members} {context})",) } fn flatten_pushdown_replacer( diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/aggregate.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/aggregate.rs index f9cf9f6f13061..08b26e133e010 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/aggregate.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/aggregate.rs @@ -11,12 +11,11 @@ use crate::{ wrapped_select_joins_empty_tail, wrapped_select_order_expr_empty_tail, wrapped_select_projection_expr_empty_tail, wrapped_select_subqueries_empty_tail, wrapped_select_window_expr_empty_tail, wrapper_pullup_replacer, wrapper_pushdown_replacer, - AggregateFunctionExprDistinct, AggregateFunctionExprFun, AliasExprAlias, ColumnExprColumn, - ListType, LogicalPlanLanguage, WrappedSelectPushToCube, WrapperPullupReplacerAliasToCube, - WrapperPullupReplacerGroupedSubqueries, WrapperPullupReplacerPushToCube, - WrapperPushdownReplacerGroupedSubqueries, WrapperPushdownReplacerPushToCube, + wrapper_replacer_context, AggregateFunctionExprDistinct, AggregateFunctionExprFun, + AliasExprAlias, ColumnExprColumn, ListType, LogicalPlanLanguage, WrappedSelectPushToCube, + WrapperReplacerContextAliasToCube, WrapperReplacerContextPushToCube, }, - copy_flag, copy_value, + copy_flag, transport::V1CubeMetaMeasureExt, var, var_iter, }; @@ -33,11 +32,13 @@ impl WrapperRules { cube_scan_wrapper( wrapper_pullup_replacer( "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), "CubeScanWrapperFinalized:false", ), @@ -50,78 +51,96 @@ impl WrapperRules { "WrappedSelectSelectType:Aggregate", wrapper_pullup_replacer( wrapped_select_projection_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_subqueries_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pushdown_replacer( "?group_expr", - "?alias_to_cube", - "?pushdown_push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?pushdown_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pushdown_replacer( "?aggr_expr", - "?alias_to_cube", - "?pushdown_push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?pushdown_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_window_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_joins_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_filter_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapped_select_having_expr_empty_tail(), "WrappedSelectLimit:None", "WrappedSelectOffset:None", wrapper_pullup_replacer( wrapped_select_order_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), "WrappedSelectAlias:None", "WrappedSelectDistinct:false", @@ -134,31 +153,32 @@ impl WrapperRules { "?group_expr", "?aggr_expr", "?push_to_cube", - "?pushdown_push_to_cube", "?select_push_to_cube", - "?grouped_subqueries", - "?pushdown_grouped_subqueries", ), ), transforming_rewrite( "wrapper-groupping-set-push-down", wrapper_pushdown_replacer( grouping_set_expr("?rollout_members", "?type"), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", - ), - grouping_set_expr( - wrapper_pushdown_replacer( - "?rollout_members", + wrapper_replacer_context( "?alias_to_cube", "?push_to_cube", - "WrapperPullupReplacerInProjection:false", + "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", ), + ), + grouping_set_expr( + wrapper_pushdown_replacer( + "?rollout_members", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), + ), "?type", ), self.check_rollup_allowed("?alias_to_cube"), @@ -168,21 +188,25 @@ impl WrapperRules { grouping_set_expr( wrapper_pullup_replacer( "?rollout_members", - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), "?type", ), wrapper_pullup_replacer( grouping_set_expr("?rollout_members", "?type"), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), ), ]); @@ -201,20 +225,24 @@ impl WrapperRules { &format!("wrapper-{}", name), wrapper_pushdown_replacer( "?aggr_expr", - "?alias_to_cube", - "WrapperPushdownReplacerPushToCube:true", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), vec![("?aggr_expr", aggr_expr)], wrapper_pullup_replacer( "?measure", - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "?in_projection", - "?cube_members", - "?pullup_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), self.pushdown_measure( "?aggr_expr", @@ -224,8 +252,6 @@ impl WrapperRules { cast_data_type, "?cube_members", "?measure", - "?grouped_subqueries", - "?pullup_grouped_subqueries", ), ) }, @@ -282,11 +308,13 @@ impl WrapperRules { cube_scan_wrapper( wrapper_pullup_replacer( "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), "CubeScanWrapperFinalized:false", ), @@ -302,78 +330,96 @@ impl WrapperRules { "WrappedSelectSelectType:Aggregate", wrapper_pullup_replacer( wrapped_select_projection_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pushdown_replacer( "?subqueries", - "?alias_to_cube", - "?pushdown_push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?pushdown_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pushdown_replacer( "?group_expr", - "?alias_to_cube", - "?pushdown_push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?pushdown_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pushdown_replacer( "?aggr_expr", - "?alias_to_cube", - "?pushdown_push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?pushdown_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_window_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_joins_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_filter_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapped_select_having_expr_empty_tail(), "WrappedSelectLimit:None", "WrappedSelectOffset:None", wrapper_pullup_replacer( wrapped_select_order_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), "WrappedSelectAlias:None", "WrappedSelectDistinct:false", @@ -387,16 +433,13 @@ impl WrapperRules { "?group_expr", "?aggr_expr", "?push_to_cube", - "?pushdown_push_to_cube", "?select_push_to_cube", - "?grouped_subqueries", - "?pushdown_grouped_subqueries", ), )]); } pub fn aggregate_merge_rules(&self, rules: &mut Vec) { - rules.extend(vec![transforming_rewrite( + rules.extend(vec![rewrite( "wrapper-merge-aggregation-with-inner-wrapped-select", // Input is not a finished wrapper_pullup_replacer, but WrappedSelect just before pullup // After pullup replacer would disable push to cube, because any node on top would have WrappedSelect in `from` @@ -408,67 +451,83 @@ impl WrapperRules { "WrappedSelectSelectType:Projection", wrapper_pullup_replacer( wrapped_select_projection_expr_empty_tail(), - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_subqueries_empty_tail(), - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_group_expr_empty_tail(), - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_aggr_expr_empty_tail(), - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_window_expr_empty_tail(), - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?inner_from", - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?inner_joins", - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?inner_filters", - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapped_select_having_expr_empty_tail(), // Inner must not have limit and offset, because they are not commutative with aggregation @@ -476,11 +535,13 @@ impl WrapperRules { "WrappedSelectOffset:None", wrapper_pullup_replacer( wrapped_select_order_expr_empty_tail(), - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), "WrappedSelectAlias:None", "WrappedSelectDistinct:false", @@ -498,78 +559,96 @@ impl WrapperRules { "WrappedSelectSelectType:Aggregate", wrapper_pullup_replacer( wrapped_select_projection_expr_empty_tail(), - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_subqueries_empty_tail(), - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pushdown_replacer( "?group_expr", - "?alias_to_cube", - "WrapperPushdownReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?pushdown_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pushdown_replacer( "?aggr_expr", - "?alias_to_cube", - "WrapperPushdownReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?pushdown_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_window_expr_empty_tail(), - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?inner_from", - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?inner_joins", - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?inner_filters", - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), wrapped_select_having_expr_empty_tail(), "WrappedSelectLimit:None", "WrappedSelectOffset:None", wrapper_pullup_replacer( wrapped_select_order_expr_empty_tail(), - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:false", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:false", + "?cube_members", + "?grouped_subqueries", + ), ), "WrappedSelectAlias:None", "WrappedSelectDistinct:false", @@ -578,7 +657,6 @@ impl WrapperRules { ), "CubeScanWrapperFinalized:false", ), - self.transform_merge_aggregate("?grouped_subqueries", "?pushdown_grouped_subqueries"), )]); } @@ -587,18 +665,12 @@ impl WrapperRules { group_expr_var: &'static str, aggr_expr_var: &'static str, push_to_cube_var: &'static str, - pushdown_push_to_cube_var: &'static str, select_push_to_cube_var: &'static str, - grouped_subqueries_var: &'static str, - pushdown_grouped_subqueries_var: &'static str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let group_expr_var = var!(group_expr_var); let aggr_expr_var = var!(aggr_expr_var); let push_to_cube_var = var!(push_to_cube_var); - let pushdown_push_to_cube_var = var!(pushdown_push_to_cube_var); let select_push_to_cube_var = var!(select_push_to_cube_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pushdown_grouped_subqueries_var = var!(pushdown_grouped_subqueries_var); move |egraph, subst| { Self::transform_aggregate_impl( egraph, @@ -606,10 +678,7 @@ impl WrapperRules { group_expr_var, aggr_expr_var, push_to_cube_var, - pushdown_push_to_cube_var, select_push_to_cube_var, - grouped_subqueries_var, - pushdown_grouped_subqueries_var, ) } } @@ -620,19 +689,13 @@ impl WrapperRules { group_expr_var: &'static str, aggr_expr_var: &'static str, push_to_cube_var: &'static str, - pushdown_push_to_cube_var: &'static str, select_push_to_cube_var: &'static str, - grouped_subqueries_var: &'static str, - pushdown_grouped_subqueries_var: &'static str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let group_expr_var = var!(group_expr_var); let aggr_expr_var = var!(aggr_expr_var); let push_to_cube_var = var!(push_to_cube_var); - let pushdown_push_to_cube_var = var!(pushdown_push_to_cube_var); let select_push_to_cube_var = var!(select_push_to_cube_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pushdown_grouped_subqueries_var = var!(pushdown_grouped_subqueries_var); let meta = self.meta_context.clone(); move |egraph, subst| { if Self::transform_check_subquery_allowed( @@ -647,10 +710,7 @@ impl WrapperRules { group_expr_var, aggr_expr_var, push_to_cube_var, - pushdown_push_to_cube_var, select_push_to_cube_var, - grouped_subqueries_var, - pushdown_grouped_subqueries_var, ) } else { false @@ -664,10 +724,7 @@ impl WrapperRules { group_expr_var: Var, aggr_expr_var: Var, push_to_cube_var: Var, - pushdown_push_to_cube_var: Var, select_push_to_cube_var: Var, - grouped_subqueries_var: Var, - pushdown_grouped_subqueries_var: Var, ) -> bool { if egraph[subst[group_expr_var]].data.referenced_expr.is_none() { return false; @@ -680,64 +737,14 @@ impl WrapperRules { egraph, subst, push_to_cube_var, - WrapperPullupReplacerPushToCube, - pushdown_push_to_cube_var, - WrapperPushdownReplacerPushToCube - ) { - return false; - } - - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries, - pushdown_grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries + WrapperReplacerContextPushToCube, + select_push_to_cube_var, + WrappedSelectPushToCube ) { return false; } - for push_to_cube in var_iter!( - egraph[subst[push_to_cube_var]], - WrapperPullupReplacerPushToCube - ) - .cloned() - { - subst.insert( - select_push_to_cube_var, - egraph.add(LogicalPlanLanguage::WrappedSelectPushToCube( - WrappedSelectPushToCube(push_to_cube), - )), - ); - return true; - } - false - } - - fn transform_merge_aggregate( - &self, - grouped_subqueries_var: &'static str, - pushdown_grouped_subqueries_var: &'static str, - ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pushdown_grouped_subqueries_var = var!(pushdown_grouped_subqueries_var); - move |egraph, subst| { - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries, - pushdown_grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries - ) { - return false; - } - - true - } + true } fn check_rollup_allowed( @@ -749,7 +756,7 @@ impl WrapperRules { move |egraph, subst| { for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) .cloned() { @@ -777,8 +784,6 @@ impl WrapperRules { _cast_data_type_var: Option<&'static str>, cube_members_var: &'static str, measure_out_var: &'static str, - grouped_subqueries_var: &'static str, - pullup_grouped_subqueries_var: &'static str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let original_expr_var = var!(original_expr_var); let column_var = column_var.map(|v| var!(v)); @@ -787,8 +792,6 @@ impl WrapperRules { // let cast_data_type_var = cast_data_type_var.map(|v| var!(v)); let cube_members_var = var!(cube_members_var); let measure_out_var = var!(measure_out_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pullup_grouped_subqueries_var = var!(pullup_grouped_subqueries_var); let meta = self.meta_context.clone(); let disable_strict_agg_type_match = self.config_obj.disable_strict_agg_type_match(); move |egraph, subst| { @@ -840,18 +843,6 @@ impl WrapperRules { disable_strict_agg_type_match, ) { - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries, - pullup_grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries - ) { - return false; - } - let column_expr_column = egraph.add(LogicalPlanLanguage::ColumnExprColumn( ColumnExprColumn(column.clone()), diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/aggregate_function.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/aggregate_function.rs index aadcddbce8a93..50cc573d4c54e 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/aggregate_function.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/aggregate_function.rs @@ -4,7 +4,8 @@ use crate::{ rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, - AggregateFunctionExprDistinct, AggregateFunctionExprFun, WrapperPullupReplacerAliasToCube, + wrapper_replacer_context, AggregateFunctionExprDistinct, AggregateFunctionExprFun, + WrapperReplacerContextAliasToCube, }, var, var_iter, }; @@ -18,22 +19,11 @@ impl WrapperRules { "wrapper-push-down-aggregate-function", wrapper_pushdown_replacer( agg_fun_expr("?fun", vec!["?expr"], "?distinct"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + "?context", ), agg_fun_expr( "?fun", - vec![wrapper_pushdown_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - )], + vec![wrapper_pushdown_replacer("?expr", "?context")], "?distinct", ), ), @@ -43,21 +33,25 @@ impl WrapperRules { "?fun", vec![wrapper_pullup_replacer( "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), )], "?distinct", ), wrapper_pullup_replacer( agg_fun_expr("?fun", vec!["?expr"], "?distinct"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), self.transform_agg_fun_expr("?fun", "?distinct", "?alias_to_cube"), ), @@ -77,7 +71,7 @@ impl WrapperRules { move |egraph, subst| { for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) .cloned() { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/alias.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/alias.rs index 8c1268806256a..751cb5beeda6c 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/alias.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/alias.rs @@ -8,47 +8,13 @@ impl WrapperRules { rules.extend(vec![ rewrite( "wrapper-push-down-alias", - wrapper_pushdown_replacer( - alias_expr("?expr", "?alias"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - alias_expr( - wrapper_pushdown_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - "?alias", - ), + wrapper_pushdown_replacer(alias_expr("?expr", "?alias"), "?context"), + alias_expr(wrapper_pushdown_replacer("?expr", "?context"), "?alias"), ), rewrite( "wrapper-pull-up-alias", - alias_expr( - wrapper_pullup_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - "?alias", - ), - wrapper_pullup_replacer( - alias_expr("?expr", "?alias"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + alias_expr(wrapper_pullup_replacer("?expr", "?context"), "?alias"), + wrapper_pullup_replacer(alias_expr("?expr", "?alias"), "?context"), ), ]); } diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/binary_expr.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/binary_expr.rs index 9ab802f261ac0..31c7454238abb 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/binary_expr.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/binary_expr.rs @@ -4,7 +4,7 @@ use crate::{ rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, - WrapperPullupReplacerAliasToCube, + wrapper_replacer_context, WrapperReplacerContextAliasToCube, }, var, var_iter, }; @@ -15,32 +15,11 @@ impl WrapperRules { rules.extend(vec![ rewrite( "wrapper-push-down-binary-expr", - wrapper_pushdown_replacer( - binary_expr("?left", "?op", "?right"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pushdown_replacer(binary_expr("?left", "?op", "?right"), "?context"), binary_expr( - wrapper_pushdown_replacer( - "?left", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pushdown_replacer("?left", "?context"), "?op", - wrapper_pushdown_replacer( - "?right", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pushdown_replacer("?right", "?context"), ), ), transforming_rewrite( @@ -48,15 +27,29 @@ impl WrapperRules { binary_expr( wrapper_pullup_replacer( "?left", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), "?op", wrapper_pullup_replacer( "?right", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), + ), + ), + wrapper_pullup_replacer( + binary_expr("?left", "?op", "?right"), + wrapper_replacer_context( "?alias_to_cube", "?push_to_cube", "?in_projection", @@ -64,14 +57,6 @@ impl WrapperRules { "?grouped_subqueries", ), ), - wrapper_pullup_replacer( - binary_expr("?left", "?op", "?right"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), self.transform_binary_expr("?op", "?alias_to_cube"), ), ]); @@ -88,7 +73,7 @@ impl WrapperRules { move |egraph, subst| { for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) .cloned() { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/case.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/case.rs index 9eca3354b25e0..e7c2adda0bc42 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/case.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/case.rs @@ -4,7 +4,7 @@ use crate::{ rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, - WrapperPullupReplacerAliasToCube, + wrapper_replacer_context, WrapperReplacerContextAliasToCube, }, var, var_iter, }; @@ -15,39 +15,11 @@ impl WrapperRules { rules.extend(vec![ rewrite( "wrapper-push-down-case", - wrapper_pushdown_replacer( - case_expr_var_arg("?when", "?then", "?else"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pushdown_replacer(case_expr_var_arg("?when", "?then", "?else"), "?context"), case_expr_var_arg( - wrapper_pushdown_replacer( - "?when", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pushdown_replacer( - "?then", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pushdown_replacer( - "?else", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pushdown_replacer("?when", "?context"), + wrapper_pushdown_replacer("?then", "?context"), + wrapper_pushdown_replacer("?else", "?context"), ), ), transforming_rewrite( @@ -55,22 +27,38 @@ impl WrapperRules { case_expr_var_arg( wrapper_pullup_replacer( "?when", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?then", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?else", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), + ), + ), + wrapper_pullup_replacer( + case_expr_var_arg("?when", "?then", "?else"), + wrapper_replacer_context( "?alias_to_cube", "?push_to_cube", "?in_projection", @@ -78,14 +66,6 @@ impl WrapperRules { "?grouped_subqueries", ), ), - wrapper_pullup_replacer( - case_expr_var_arg("?when", "?then", "?else"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), self.transform_case_expr("?alias_to_cube"), ), ]); @@ -110,7 +90,7 @@ impl WrapperRules { move |egraph, subst| { for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) .cloned() { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/cast.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/cast.rs index 119b79bc0a7df..222822db79de5 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/cast.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/cast.rs @@ -8,47 +8,13 @@ impl WrapperRules { rules.extend(vec![ rewrite( "wrapper-push-down-cast", - wrapper_pushdown_replacer( - cast_expr("?expr", "?data_type"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - cast_expr( - wrapper_pushdown_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - "?data_type", - ), + wrapper_pushdown_replacer(cast_expr("?expr", "?data_type"), "?context"), + cast_expr(wrapper_pushdown_replacer("?expr", "?context"), "?data_type"), ), rewrite( "wrapper-pull-up-cast", - cast_expr( - wrapper_pullup_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - "?data_type", - ), - wrapper_pullup_replacer( - cast_expr("?expr", "?data_type"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + cast_expr(wrapper_pullup_replacer("?expr", "?context"), "?data_type"), + wrapper_pullup_replacer(cast_expr("?expr", "?data_type"), "?context"), ), ]); } diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/column.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/column.rs index ed685fabf614a..922322c82bc3d 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/column.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/column.rs @@ -1,39 +1,42 @@ use crate::{ compile::rewrite::{ analysis::Member, - column_expr, + column_expr, rewrite, rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, - transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, ColumnExprColumn, - LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, - WrapperPullupReplacerGroupedSubqueries, WrapperPushdownReplacerGroupedSubqueries, + transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, + wrapper_replacer_context, ColumnExprColumn, LogicalPlanLanguage, + WrapperReplacerContextAliasToCube, WrapperReplacerContextGroupedSubqueries, }, - copy_value, var, var_iter, + var, var_iter, }; use egg::Subst; impl WrapperRules { pub fn column_rules(&self, rules: &mut Vec) { rules.extend(vec![ - transforming_rewrite( + rewrite( "wrapper-push-down-column", wrapper_pushdown_replacer( column_expr("?name"), - "?alias_to_cube", - "WrapperPushdownReplacerPushToCube:false", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:false", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( column_expr("?name"), - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:false", - "?in_projection", - "?cube_members", - "?pullup_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:false", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), - self.pushdown_column("?grouped_subqueries", "?pullup_grouped_subqueries"), ), // TODO This is half measure implementation to propagate ungrouped simple measure towards aggregate node that easily allow replacement of aggregation functions // We need to support it for complex aka `number` measures @@ -41,45 +44,48 @@ impl WrapperRules { "wrapper-push-down-column-simple-measure-in-projection", wrapper_pushdown_replacer( column_expr("?name"), - "?alias_to_cube", - "WrapperPushdownReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( column_expr("?name"), - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?pullup_grouped_subqueries", - ), - self.pushdown_simple_measure( - "?name", - "?cube_members", - "?grouped_subqueries", - "?pullup_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), + self.pushdown_simple_measure("?name", "?cube_members"), ), // TODO time dimension support transforming_rewrite( "wrapper-push-down-dimension", wrapper_pushdown_replacer( column_expr("?name"), - "?alias_to_cube", - "WrapperPushdownReplacerPushToCube:true", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?dimension", - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "?in_projection", - "?cube_members", - "?pullup_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), self.pushdown_dimension( "?alias_to_cube", @@ -87,36 +93,11 @@ impl WrapperRules { "?cube_members", "?dimension", "?grouped_subqueries", - "?pullup_grouped_subqueries", ), ), ]); } - fn pushdown_column( - &self, - grouped_subqueries_var: &'static str, - pullup_grouped_subqueries_var: &'static str, - ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pullup_grouped_subqueries_var = var!(pullup_grouped_subqueries_var); - move |egraph, subst| { - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries, - pullup_grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries - ) { - return false; - } - - true - } - } - fn pushdown_dimension( &self, alias_to_cube_var: &'static str, @@ -124,34 +105,20 @@ impl WrapperRules { members_var: &'static str, dimension_var: &'static str, grouped_subqueries_var: &'static str, - pullup_grouped_subqueries_var: &'static str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let column_name_var = var!(column_name_var); let members_var = var!(members_var); let dimension_var = var!(dimension_var); let grouped_subqueries_var = var!(grouped_subqueries_var); - let pullup_grouped_subqueries_var = var!(pullup_grouped_subqueries_var); move |egraph, subst| { - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries, - pullup_grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries - ) { - return false; - } - let columns: Vec<_> = var_iter!(egraph[subst[column_name_var]], ColumnExprColumn) .cloned() .collect(); for column in columns.iter() { for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) .cloned() { @@ -179,7 +146,7 @@ impl WrapperRules { if let Some(col_relation) = &column.relation { for grouped_subqueries in var_iter!( egraph[subst[grouped_subqueries_var]], - WrapperPushdownReplacerGroupedSubqueries + WrapperReplacerContextGroupedSubqueries ) { if grouped_subqueries.iter().any(|subq| subq == col_relation) { // Found grouped subquery, can "replace" column with itself @@ -228,13 +195,9 @@ impl WrapperRules { &self, column_name_var: &'static str, members_var: &'static str, - grouped_subqueries_var: &'static str, - pullup_grouped_subqueries_var: &'static str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let column_name_var = var!(column_name_var); let members_var = var!(members_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pullup_grouped_subqueries_var = var!(pullup_grouped_subqueries_var); let meta = self.meta_context.clone(); move |egraph, subst| { let columns: Vec<_> = var_iter!(egraph[subst[column_name_var]], ColumnExprColumn) @@ -247,18 +210,6 @@ impl WrapperRules { { if let Some(measure) = meta.find_measure_with_name(member.to_string()) { if measure.agg_type != Some("number".to_string()) { - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries, - pullup_grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries - ) { - return false; - } - return true; } } diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/cube_scan_wrapper.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/cube_scan_wrapper.rs index d516b62212bb8..9077ab32f15c2 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/cube_scan_wrapper.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/cube_scan_wrapper.rs @@ -3,9 +3,10 @@ use crate::{ cube_scan, cube_scan_wrapper, rewrite, rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, - transforming_rewrite, wrapper_pullup_replacer, CubeScanAliasToCube, CubeScanLimit, - CubeScanOffset, CubeScanUngrouped, LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, - WrapperPullupReplacerGroupedSubqueries, WrapperPullupReplacerPushToCube, + transforming_rewrite, wrapper_pullup_replacer, wrapper_replacer_context, + CubeScanAliasToCube, CubeScanLimit, CubeScanOffset, CubeScanUngrouped, LogicalPlanLanguage, + WrapperReplacerContextAliasToCube, WrapperReplacerContextGroupedSubqueries, + WrapperReplacerContextPushToCube, }, var, var_iter, }; @@ -42,11 +43,13 @@ impl WrapperRules { "CubeScanWrapped:true", "?ungrouped", ), - "?alias_to_cube_out", - "?push_to_cube_out", - "WrapperPullupReplacerInProjection:false", - "?members", - "?grouped_subqueries_out", + wrapper_replacer_context( + "?alias_to_cube_out", + "?push_to_cube_out", + "WrapperReplacerContextInProjection:false", + "?members", + "?grouped_subqueries_out", + ), ), "CubeScanWrapperFinalized:false", ), @@ -64,14 +67,7 @@ impl WrapperRules { rewrite( "wrapper-finalize-pull-up-replacer", cube_scan_wrapper( - wrapper_pullup_replacer( - "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pullup_replacer("?cube_scan_input", "?context"), "CubeScanWrapperFinalized:false", ), cube_scan_wrapper("?cube_scan_input", "CubeScanWrapperFinalized:true"), @@ -121,21 +117,21 @@ impl WrapperRules { let push_to_cube_out = ungrouped && has_no_limit_or_offset; subst.insert( push_to_cube_out_var, - egraph.add(LogicalPlanLanguage::WrapperPullupReplacerPushToCube( - WrapperPullupReplacerPushToCube(push_to_cube_out), + egraph.add(LogicalPlanLanguage::WrapperReplacerContextPushToCube( + WrapperReplacerContextPushToCube(push_to_cube_out), )), ); subst.insert( alias_to_cube_var_out, - egraph.add(LogicalPlanLanguage::WrapperPullupReplacerAliasToCube( - WrapperPullupReplacerAliasToCube(alias_to_cube), + egraph.add(LogicalPlanLanguage::WrapperReplacerContextAliasToCube( + WrapperReplacerContextAliasToCube(alias_to_cube), )), ); subst.insert( grouped_subqueries_out_var, egraph.add( - LogicalPlanLanguage::WrapperPullupReplacerGroupedSubqueries( - WrapperPullupReplacerGroupedSubqueries(vec![]), + LogicalPlanLanguage::WrapperReplacerContextGroupedSubqueries( + WrapperReplacerContextGroupedSubqueries(vec![]), ), ), ); diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/distinct.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/distinct.rs index b9af37fa16c38..db9af90f8e03d 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/distinct.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/distinct.rs @@ -28,11 +28,7 @@ impl WrapperRules { "WrappedSelectPushToCube:false", "?select_ungrouped_scan", ), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + "?context", ), "CubeScanWrapperFinalized:false".to_string(), )), @@ -57,11 +53,7 @@ impl WrapperRules { "WrappedSelectPushToCube:false", "?select_ungrouped_scan", ), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + "?context", ), "CubeScanWrapperFinalized:false", ), diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/extract.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/extract.rs index 9f1b46eff2089..22e484d73065c 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/extract.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/extract.rs @@ -3,7 +3,8 @@ use crate::{ literal_expr, rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, - transforming_rewrite, wrapper_pullup_replacer, WrapperPullupReplacerAliasToCube, + transforming_rewrite, wrapper_pullup_replacer, wrapper_replacer_context, + WrapperReplacerContextAliasToCube, }, var, var_iter, }; @@ -18,19 +19,23 @@ impl WrapperRules { vec![ wrapper_pullup_replacer( literal_expr("?date_part"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?date", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), ], ), @@ -39,11 +44,13 @@ impl WrapperRules { "DatePart", vec![literal_expr("?date_part"), "?date".to_string()], ), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), self.transform_date_part_expr("?alias_to_cube"), )]); @@ -58,7 +65,7 @@ impl WrapperRules { move |egraph, subst| { for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) .cloned() { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/filter.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/filter.rs index 6ac99d2193c74..7fecfda0a1892 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/filter.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/filter.rs @@ -9,11 +9,10 @@ use crate::{ wrapped_select_joins_empty_tail, wrapped_select_order_expr_empty_tail, wrapped_select_projection_expr_empty_tail, wrapped_select_subqueries_empty_tail, wrapped_select_window_expr_empty_tail, wrapper_pullup_replacer, wrapper_pushdown_replacer, - LogicalPlanLanguage, WrappedSelectPushToCube, WrappedSelectUngroupedScan, - WrapperPullupReplacerGroupedSubqueries, WrapperPullupReplacerPushToCube, - WrapperPushdownReplacerGroupedSubqueries, WrapperPushdownReplacerPushToCube, + wrapper_replacer_context, LogicalPlanLanguage, WrappedSelectPushToCube, + WrappedSelectUngroupedScan, WrapperReplacerContextPushToCube, }, - copy_flag, copy_value, var, var_iter, + var, var_iter, }; use egg::{Subst, Var}; @@ -120,11 +119,13 @@ impl WrapperRules { cube_scan_wrapper( wrapper_pullup_replacer( "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), "CubeScanWrapperFinalized:false", ), @@ -134,76 +135,94 @@ impl WrapperRules { "WrappedSelectSelectType:Projection", wrapper_pullup_replacer( wrapped_select_projection_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_subqueries_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_group_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_aggr_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_window_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_joins_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapped_select_filter_expr( wrapper_pushdown_replacer( "?filter_expr", - "?alias_to_cube", - "?pushdown_push_to_cube", - "?in_projection", - "?cube_members", - "?pushdown_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_filter_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), ), wrapped_select_having_expr_empty_tail(), @@ -211,11 +230,13 @@ impl WrapperRules { "WrappedSelectOffset:None", wrapper_pullup_replacer( wrapped_select_order_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), "WrappedSelectAlias:None", "WrappedSelectDistinct:false", @@ -226,11 +247,8 @@ impl WrapperRules { ), self.transform_filter( "?push_to_cube", - "?pushdown_push_to_cube", "?select_push_to_cube", "?select_ungrouped_scan", - "?grouped_subqueries", - "?pushdown_grouped_subqueries", ), )]); @@ -251,11 +269,13 @@ impl WrapperRules { cube_scan_wrapper( wrapper_pullup_replacer( "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), "CubeScanWrapperFinalized:false", ), @@ -268,76 +288,94 @@ impl WrapperRules { "WrappedSelectSelectType:Projection", wrapper_pullup_replacer( wrapped_select_projection_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pushdown_replacer( "?subqueries", - "?alias_to_cube", - "?pushdown_push_to_cube", - "?in_projection", - "?cube_members", - "?pushdown_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_group_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_aggr_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_window_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_joins_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapped_select_filter_expr( wrapper_pushdown_replacer( "?filter_expr", - "?alias_to_cube", - "?pushdown_push_to_cube", - "?in_projection", - "?cube_members", - "?pushdown_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_filter_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), ), wrapped_select_having_expr_empty_tail(), @@ -345,11 +383,13 @@ impl WrapperRules { "WrappedSelectOffset:None", wrapper_pullup_replacer( wrapped_select_order_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), "WrappedSelectAlias:None", "WrappedSelectDistinct:false", @@ -361,11 +401,8 @@ impl WrapperRules { self.transform_filter_subquery( "?alias_to_cube", "?push_to_cube", - "?pushdown_push_to_cube", "?select_push_to_cube", "?select_ungrouped_scan", - "?grouped_subqueries", - "?pushdown_grouped_subqueries", ), )]); } @@ -373,28 +410,19 @@ impl WrapperRules { fn transform_filter( &self, push_to_cube_var: &'static str, - pushdown_push_to_cube_var: &'static str, select_push_to_cube_var: &'static str, select_ungrouped_scan_var: &'static str, - grouped_subqueries_var: &'static str, - pushdown_grouped_subqueries_var: &'static str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let push_to_cube_var = var!(push_to_cube_var); - let pushdown_push_to_cube_var = var!(pushdown_push_to_cube_var); let select_push_to_cube_var = var!(select_push_to_cube_var); let select_ungrouped_scan_var = var!(select_ungrouped_scan_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pushdown_grouped_subqueries_var = var!(pushdown_grouped_subqueries_var); move |egraph, subst| { Self::transform_filter_impl( egraph, subst, push_to_cube_var, - pushdown_push_to_cube_var, select_push_to_cube_var, select_ungrouped_scan_var, - grouped_subqueries_var, - pushdown_grouped_subqueries_var, ) } } @@ -403,19 +431,13 @@ impl WrapperRules { &self, alias_to_cube_var: &'static str, push_to_cube_var: &'static str, - pushdown_push_to_cube_var: &'static str, select_push_to_cube_var: &'static str, select_ungrouped_scan_var: &'static str, - grouped_subqueries_var: &'static str, - pushdown_grouped_subqueries_var: &'static str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let push_to_cube_var = var!(push_to_cube_var); - let pushdown_push_to_cube_var = var!(pushdown_push_to_cube_var); let select_push_to_cube_var = var!(select_push_to_cube_var); let select_ungrouped_scan_var = var!(select_ungrouped_scan_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pushdown_grouped_subqueries_var = var!(pushdown_grouped_subqueries_var); let meta = self.meta_context.clone(); move |egraph, subst| { if Self::transform_check_subquery_allowed( @@ -428,11 +450,8 @@ impl WrapperRules { egraph, subst, push_to_cube_var, - pushdown_push_to_cube_var, select_push_to_cube_var, select_ungrouped_scan_var, - grouped_subqueries_var, - pushdown_grouped_subqueries_var, ) } else { false @@ -444,38 +463,12 @@ impl WrapperRules { egraph: &mut CubeEGraph, subst: &mut Subst, push_to_cube_var: Var, - pushdown_push_to_cube_var: Var, select_push_to_cube_var: Var, select_ungrouped_scan_var: Var, - grouped_subqueries_var: Var, - pushdown_grouped_subqueries_var: Var, ) -> bool { - if !copy_flag!( - egraph, - subst, - push_to_cube_var, - WrapperPullupReplacerPushToCube, - pushdown_push_to_cube_var, - WrapperPushdownReplacerPushToCube - ) { - return false; - } - - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries, - pushdown_grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries - ) { - return false; - } - for push_to_cube in var_iter!( egraph[subst[push_to_cube_var]], - WrapperPullupReplacerPushToCube + WrapperReplacerContextPushToCube ) .cloned() { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/in_list_expr.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/in_list_expr.rs index ff94d71f0f5c8..1e4530b29094b 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/in_list_expr.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/in_list_expr.rs @@ -4,11 +4,9 @@ use crate::{ rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, - WrapperPullupReplacerAliasToCube, WrapperPullupReplacerGroupedSubqueries, - WrapperPullupReplacerPushToCube, WrapperPushdownReplacerGroupedSubqueries, - WrapperPushdownReplacerPushToCube, + wrapper_replacer_context, WrapperReplacerContextAliasToCube, }, - copy_flag, copy_value, var, var_iter, + var, var_iter, }; use egg::Subst; @@ -17,68 +15,20 @@ impl WrapperRules { rules.extend(vec![ transforming_rewrite( "wrapper-in-list-only-consts-push-down", - wrapper_pushdown_replacer( - inlist_expr("?expr", "?list", "?negated"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pushdown_replacer(inlist_expr("?expr", "?list", "?negated"), "?context"), inlist_expr( - wrapper_pushdown_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?list", - "?alias_to_cube", - "?pullup_push_to_cube", - "?in_projection", - "?cube_members", - "?pullup_grouped_subqueries", - ), + wrapper_pushdown_replacer("?expr", "?context"), + wrapper_pullup_replacer("?list", "?context"), "?negated", ), - self.transform_in_list_only_consts( - "?list", - "?push_to_cube", - "?pullup_push_to_cube", - "?grouped_subqueries", - "?pullup_grouped_subqueries", - ), + self.transform_in_list_only_consts("?list"), ), rewrite( "wrapper-in-list-push-down", - wrapper_pushdown_replacer( - inlist_expr("?expr", "?list", "?negated"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pushdown_replacer(inlist_expr("?expr", "?list", "?negated"), "?context"), inlist_expr( - wrapper_pushdown_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pushdown_replacer( - "?list", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pushdown_replacer("?expr", "?context"), + wrapper_pushdown_replacer("?list", "?context"), "?negated", ), ), @@ -87,29 +37,35 @@ impl WrapperRules { inlist_expr( wrapper_pullup_replacer( "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?list", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), + ), + "?negated", + ), + wrapper_pullup_replacer( + inlist_expr("?expr", "?list", "?negated"), + wrapper_replacer_context( "?alias_to_cube", "?push_to_cube", "?in_projection", "?cube_members", "?grouped_subqueries", ), - "?negated", - ), - wrapper_pullup_replacer( - inlist_expr("?expr", "?list", "?negated"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", ), self.transform_in_list_expr("?alias_to_cube"), ), @@ -128,7 +84,7 @@ impl WrapperRules { move |egraph, subst| { for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) .cloned() { @@ -149,38 +105,9 @@ impl WrapperRules { fn transform_in_list_only_consts( &self, list_var: &'static str, - push_to_cube_var: &'static str, - pullup_push_to_cube_var: &'static str, - grouped_subqueries_var: &'static str, - pullup_grouped_subqueries_var: &'static str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let list_var = var!(list_var); - let push_to_cube_var = var!(push_to_cube_var); - let pullup_push_to_cube_var = var!(pullup_push_to_cube_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pullup_grouped_subqueries_var = var!(pullup_grouped_subqueries_var); move |egraph: &mut CubeEGraph, subst| { - if !copy_flag!( - egraph, - subst, - push_to_cube_var, - WrapperPushdownReplacerPushToCube, - pullup_push_to_cube_var, - WrapperPullupReplacerPushToCube - ) { - return false; - } - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries, - pullup_grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries - ) { - return false; - } return egraph[subst[list_var]].data.constant_in_list.is_some(); } } diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/in_subquery_expr.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/in_subquery_expr.rs index 02fe0e26e975e..56c1ba0724518 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/in_subquery_expr.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/in_subquery_expr.rs @@ -1,122 +1,35 @@ -use crate::{ - compile::rewrite::{ - insubquery_expr, rewrite, - rewriter::{CubeEGraph, CubeRewrite}, - rules::wrapper::WrapperRules, - transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, - WrapperPullupReplacerGroupedSubqueries, WrapperPullupReplacerPushToCube, - WrapperPushdownReplacerGroupedSubqueries, WrapperPushdownReplacerPushToCube, - }, - copy_flag, copy_value, var, +use crate::compile::rewrite::{ + insubquery_expr, rewrite, rewriter::CubeRewrite, rules::wrapper::WrapperRules, + wrapper_pullup_replacer, wrapper_pushdown_replacer, }; -use egg::Subst; impl WrapperRules { pub fn in_subquery_expr_rules(&self, rules: &mut Vec) { rules.extend(vec![ - transforming_rewrite( + rewrite( "wrapper-in-subquery-push-down", wrapper_pushdown_replacer( insubquery_expr("?expr", "?subquery", "?negated"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + "?context", ), insubquery_expr( - wrapper_pushdown_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?subquery", - "?alias_to_cube", - "?pullup_push_to_cube", - "?in_projection", - "?cube_members", - "?pullup_grouped_subqueries", - ), + wrapper_pushdown_replacer("?expr", "?context"), + wrapper_pullup_replacer("?subquery", "?context"), "?negated", ), - self.transform_in_subquery_pushdown( - "?push_to_cube", - "?pullup_push_to_cube", - "?grouped_subqueries", - "?pullup_grouped_subqueries", - ), ), rewrite( "wrapper-in-subquery-pull-up", insubquery_expr( - wrapper_pullup_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?subquery", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pullup_replacer("?expr", "?context"), + wrapper_pullup_replacer("?subquery", "?context"), "?negated", ), wrapper_pullup_replacer( insubquery_expr("?expr", "?subquery", "?negated"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + "?context", ), ), ]); } - - fn transform_in_subquery_pushdown( - &self, - push_to_cube_var: &'static str, - pullup_push_to_cube_var: &'static str, - grouped_subqueries_var: &'static str, - pullup_grouped_subqueries_var: &'static str, - ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { - let push_to_cube_var = var!(push_to_cube_var); - let pullup_push_to_cube_var = var!(pullup_push_to_cube_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pullup_grouped_subqueries_var = var!(pullup_grouped_subqueries_var); - move |egraph: &mut CubeEGraph, subst| { - if !copy_flag!( - egraph, - subst, - push_to_cube_var, - WrapperPushdownReplacerPushToCube, - pullup_push_to_cube_var, - WrapperPullupReplacerPushToCube - ) { - return false; - } - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries, - pullup_grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries - ) { - return false; - } - true - } - } } diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/is_null_expr.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/is_null_expr.rs index 55da4b2ecbaec..d92160e09dab1 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/is_null_expr.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/is_null_expr.rs @@ -4,7 +4,7 @@ use crate::{ rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, - WrapperPullupReplacerAliasToCube, + wrapper_replacer_context, WrapperReplacerContextAliasToCube, }, var, var_iter, }; @@ -15,79 +15,59 @@ impl WrapperRules { rules.extend(vec![ rewrite( "wrapper-push-down-is-null-expr", - wrapper_pushdown_replacer( - is_null_expr("?expr"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - is_null_expr(wrapper_pushdown_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - )), + wrapper_pushdown_replacer(is_null_expr("?expr"), "?context"), + is_null_expr(wrapper_pushdown_replacer("?expr", "?context")), ), transforming_rewrite( "wrapper-pull-up-is-null-expr", is_null_expr(wrapper_pullup_replacer( "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), )), wrapper_pullup_replacer( is_null_expr("?expr"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), self.transform_is_null_expr("?alias_to_cube"), ), rewrite( "wrapper-push-down-is-not-null-expr", - wrapper_pushdown_replacer( - is_not_null_expr("?expr"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - is_not_null_expr(wrapper_pushdown_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - )), + wrapper_pushdown_replacer(is_not_null_expr("?expr"), "?context"), + is_not_null_expr(wrapper_pushdown_replacer("?expr", "?context")), ), transforming_rewrite( "wrapper-pull-up-is-not-null-expr", is_not_null_expr(wrapper_pullup_replacer( "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), )), wrapper_pullup_replacer( is_not_null_expr("?expr"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), self.transform_is_null_expr("?alias_to_cube"), ), @@ -103,7 +83,7 @@ impl WrapperRules { move |egraph, subst| { for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) .cloned() { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/join.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/join.rs index daf1b997f1b24..fc193258b9daa 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/join.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/join.rs @@ -7,9 +7,9 @@ use crate::{ wrapped_select_joins_empty_tail, wrapped_select_order_expr_empty_tail, wrapped_select_projection_expr_empty_tail, wrapped_select_subqueries_empty_tail, wrapped_select_window_expr_empty_tail, wrapper_pullup_replacer, wrapper_pushdown_replacer, - BinaryExprOp, ColumnExprColumn, CubeEGraph, JoinLeftOn, JoinRightOn, LogicalPlanLanguage, - WrappedSelectJoinJoinType, WrapperPullupReplacerGroupedSubqueries, - WrapperPushdownReplacerGroupedSubqueries, + wrapper_replacer_context, BinaryExprOp, ColumnExprColumn, CubeEGraph, JoinLeftOn, + JoinRightOn, LogicalPlanLanguage, WrappedSelectJoinJoinType, + WrapperReplacerContextGroupedSubqueries, }, var, var_iter, var_list_iter, }; @@ -24,31 +24,13 @@ impl WrapperRules { rewrite( "wrapper-pull-up-single-select-join", wrapped_select_join( - wrapper_pullup_replacer( - "?input", - "?alias_to_cube", - "?ungrouped", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?join_expr", - "?alias_to_cube", - "?ungrouped", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pullup_replacer("?input", "?context"), + wrapper_pullup_replacer("?join_expr", "?context"), "?out_join_type", ), wrapper_pullup_replacer( wrapped_select_join("?input", "?join_expr", "?out_join_type"), - "?alias_to_cube", - "?ungrouped", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + "?context", ), ), // TODO handle CrossJoin and Filter(CrossJoin) as well @@ -58,32 +40,36 @@ impl WrapperRules { cube_scan_wrapper( wrapper_pullup_replacer( "?left_cube_scan_input", - // Going to use this in RHS of rule - // RHS of join is grouped, so it shouldn't have any cubes or members - "?left_alias_to_cube", - // This check is important - // Rule would place ?left_cube_scan_input to `from` position of WrappedSelect(WrappedSelectPushToCube:true) - // So it need to support push-to-Cube - "WrapperPullupReplacerPushToCube:true", - "?in_projection", - // Going to use this in RHS of rule - // RHS of join is grouped, so it shouldn't have any cubes or members - "?left_cube_members", - "?left_grouped_subqueries", + wrapper_replacer_context( + // Going to use this in RHS of rule + // RHS of join is grouped, so it shouldn't have any cubes or members + "?left_alias_to_cube", + // This check is important + // Rule would place ?left_cube_scan_input to `from` position of WrappedSelect(WrappedSelectPushToCube:true) + // So it need to support push-to-Cube + "WrapperReplacerContextPushToCube:true", + "?in_projection", + // Going to use this in RHS of rule + // RHS of join is grouped, so it shouldn't have any cubes or members + "?left_cube_members", + "?left_grouped_subqueries", + ), ), "CubeScanWrapperFinalized:false", ), cube_scan_wrapper( wrapper_pullup_replacer( "?right_input", - // Going to ignore this - "?right_alias_to_cube", - // TODO depend on proper "ungrouped scan" flag (that is not a push-to-cube) - "WrapperPullupReplacerPushToCube:false", - "?in_projection", - // Going to ignore this - "?right_cube_members", - "?right_grouped_subqueries", + wrapper_replacer_context( + // Going to ignore this + "?right_alias_to_cube", + // TODO depend on proper "ungrouped scan" flag (that is not a push-to-cube) + "WrapperReplacerContextPushToCube:false", + "?in_projection", + // Going to ignore this + "?right_cube_members", + "?right_grouped_subqueries", + ), ), "CubeScanWrapperFinalized:false", ), @@ -98,116 +84,136 @@ impl WrapperRules { "WrappedSelectSelectType:Projection", wrapper_pullup_replacer( wrapped_select_projection_expr_empty_tail(), - "?left_alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "?in_projection", - "?left_cube_members", - "?out_pullup_grouped_subqueries", + wrapper_replacer_context( + "?left_alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "?in_projection", + "?left_cube_members", + "?out_grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_subqueries_empty_tail(), - "?left_alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "?in_projection", - "?left_cube_members", - "?out_pullup_grouped_subqueries", + wrapper_replacer_context( + "?left_alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "?in_projection", + "?left_cube_members", + "?out_grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_group_expr_empty_tail(), - "?left_alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "?in_projection", - "?left_cube_members", - "?out_pullup_grouped_subqueries", + wrapper_replacer_context( + "?left_alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "?in_projection", + "?left_cube_members", + "?out_grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_aggr_expr_empty_tail(), - "?left_alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "?in_projection", - "?left_cube_members", - "?out_pullup_grouped_subqueries", + wrapper_replacer_context( + "?left_alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "?in_projection", + "?left_cube_members", + "?out_grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_window_expr_empty_tail(), - "?left_alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "?in_projection", - "?left_cube_members", - "?out_pullup_grouped_subqueries", + wrapper_replacer_context( + "?left_alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "?in_projection", + "?left_cube_members", + "?out_grouped_subqueries", + ), ), wrapper_pullup_replacer( // Can move left_cube_scan_input here without checking if it's actually CubeScan - // Check for WrapperPullupReplacerPushToCube:true should be enough + // Check for WrapperReplacerContextPushToCube:true should be enough "?left_cube_scan_input", - "?left_alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "?in_projection", - "?left_cube_members", - "?out_pullup_grouped_subqueries", + wrapper_replacer_context( + "?left_alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "?in_projection", + "?left_cube_members", + "?out_grouped_subqueries", + ), ), // We don't want to use list rules here, because ?right_input is already done wrapped_select_joins( wrapped_select_join( wrapper_pullup_replacer( "?right_input", - "?left_alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "?in_projection", - "?left_cube_members", - "?out_pullup_grouped_subqueries", + wrapper_replacer_context( + "?left_alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "?in_projection", + "?left_cube_members", + "?out_grouped_subqueries", + ), ), wrapper_pushdown_replacer( "?out_join_expr", - // TODO pullup field in pushdown replacer - "?left_alias_to_cube", - // On one hand, this should be PushToCube:true, so we would only join on dimensions - // On other: RHS is grouped, so any column is just a column - // Right now, it is relying on grouped_subqueries + PushToCube:true, to allow both dimensions and grouped columns - "WrapperPushdownReplacerPushToCube:true", - // TODO pullup flag in pushdown replacer - "?in_projection", - "?left_cube_members", - "?out_pushdown_grouped_subqueries", + wrapper_replacer_context( + "?left_alias_to_cube", + // On one hand, this should be PushToCube:true, so we would only join on dimensions + // On other: RHS is grouped, so any column is just a column + // Right now, it is relying on grouped_subqueries + PushToCube:true, to allow both dimensions and grouped columns + "WrapperReplacerContextPushToCube:true", + "?in_projection", + "?left_cube_members", + "?out_grouped_subqueries", + ), ), "?out_join_type", ), // pullup(tail) just so it could be easily picked up by pullup rules wrapper_pullup_replacer( wrapped_select_joins_empty_tail(), - "?left_alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "?in_projection", - "?left_cube_members", - "?out_pullup_grouped_subqueries", + wrapper_replacer_context( + "?left_alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "?in_projection", + "?left_cube_members", + "?out_grouped_subqueries", + ), ), ), wrapper_pullup_replacer( wrapped_select_filter_expr_empty_tail(), - "?left_alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "?in_projection", - "?left_cube_members", - "?out_pullup_grouped_subqueries", + wrapper_replacer_context( + "?left_alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "?in_projection", + "?left_cube_members", + "?out_grouped_subqueries", + ), ), wrapped_select_having_expr_empty_tail(), "WrappedSelectLimit:None", "WrappedSelectOffset:None", wrapper_pullup_replacer( wrapped_select_order_expr_empty_tail(), - "?left_alias_to_cube", - "WrapperPullupReplacerPushToCube:true", - "?in_projection", - "?left_cube_members", - "?out_pullup_grouped_subqueries", + wrapper_replacer_context( + "?left_alias_to_cube", + "WrapperReplacerContextPushToCube:true", + "?in_projection", + "?left_cube_members", + "?out_grouped_subqueries", + ), ), "WrappedSelectAlias:None", "WrappedSelectDistinct:false", - // left input has WrapperPullupReplacerPushToCube:true + // left input has WrapperReplacerContextPushToCube:true // Meaning that left input itself is ungrouped CubeScan // Keep it in result, rely on pull-up rules to drop it, and on flattening rules to pick it up "WrappedSelectPushToCube:true", - // left input is WrapperPullupReplacerPushToCube:true, so result must be ungrouped + // left input is WrapperReplacerContextPushToCube:true, so result must be ungrouped "WrappedSelectUngroupedScan:true", ), "CubeScanWrapperFinalized:false", @@ -219,8 +225,7 @@ impl WrapperRules { "?in_join_type", "?out_join_expr", "?out_join_type", - "?out_pullup_grouped_subqueries", - "?out_pushdown_grouped_subqueries", + "?out_grouped_subqueries", ), ), ]); @@ -314,8 +319,7 @@ impl WrapperRules { in_join_type_var: &'static str, out_join_expr_var: &'static str, out_join_type_var: &'static str, - out_pullup_grouped_subqueries_var: &'static str, - out_pushdown_grouped_subqueries_var: &'static str, + out_grouped_subqueries_var: &'static str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let left_members_var = var!(left_members_var); let left_on_var = var!(left_on_var); @@ -326,8 +330,7 @@ impl WrapperRules { let out_join_expr_var = var!(out_join_expr_var); let out_join_type_var = var!(out_join_type_var); - let out_pullup_grouped_subqueries_var = var!(out_pullup_grouped_subqueries_var); - let out_pushdown_grouped_subqueries_var = var!(out_pushdown_grouped_subqueries_var); + let out_grouped_subqueries_var = var!(out_grouped_subqueries_var); // Only left is allowed to be ungrouped query, so right would be a subquery join for left ungrouped CubeScan // It means we don't care about just a "single cube" in LHS, and there's essentially no cubes by this moment in RHS @@ -382,22 +385,10 @@ impl WrapperRules { )), ); subst.insert( - out_pullup_grouped_subqueries_var, + out_grouped_subqueries_var, egraph.add( - LogicalPlanLanguage::WrapperPullupReplacerGroupedSubqueries( - WrapperPullupReplacerGroupedSubqueries( - out_grouped_subqueries.clone(), - ), - ), - ), - ); - subst.insert( - out_pushdown_grouped_subqueries_var, - egraph.add( - LogicalPlanLanguage::WrapperPushdownReplacerGroupedSubqueries( - WrapperPushdownReplacerGroupedSubqueries( - out_grouped_subqueries, - ), + LogicalPlanLanguage::WrapperReplacerContextGroupedSubqueries( + WrapperReplacerContextGroupedSubqueries(out_grouped_subqueries), ), ), ); diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/like_expr.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/like_expr.rs index cb466e9e99bb0..3092309d3304c 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/like_expr.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/like_expr.rs @@ -4,7 +4,8 @@ use crate::{ rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, - LikeExprEscapeChar, LikeExprLikeType, LikeType, WrapperPullupReplacerAliasToCube, + wrapper_replacer_context, LikeExprEscapeChar, LikeExprLikeType, LikeType, + WrapperReplacerContextAliasToCube, }, var, var_iter, }; @@ -23,31 +24,13 @@ impl WrapperRules { "?pattern", "?escape_char", ), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + "?context", ), like_expr( "?like_type", "?negated", - wrapper_pushdown_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pushdown_replacer( - "?pattern", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pushdown_replacer("?expr", "?context"), + wrapper_pushdown_replacer("?pattern", "?context"), "?escape_char", ), ), @@ -58,19 +41,23 @@ impl WrapperRules { "?negated", wrapper_pullup_replacer( "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?pattern", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), "?escape_char", ), @@ -82,11 +69,13 @@ impl WrapperRules { "?pattern", "?escape_char", ), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), self.transform_like_expr("?alias_to_cube", "?like_type", "?escape_char"), ), @@ -106,7 +95,7 @@ impl WrapperRules { move |egraph, subst| { for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) { let Some(sql_generator) = meta.sql_generator_by_alias_to_cube(&alias_to_cube) else { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/limit.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/limit.rs index 1b31a8a6cbbd2..0362ac1dc68b7 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/limit.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/limit.rs @@ -38,11 +38,7 @@ impl WrapperRules { "?select_push_to_cube", "?select_ungrouped_scan", ), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + "?context", ), "CubeScanWrapperFinalized:false".to_string(), ), @@ -68,11 +64,7 @@ impl WrapperRules { "?select_push_to_cube", "?select_ungrouped_scan", ), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + "?context", ), "CubeScanWrapperFinalized:false", ), diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/literal.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/literal.rs index 96142f024e264..e1e55317ae091 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/literal.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/literal.rs @@ -2,16 +2,15 @@ use crate::{ compile::rewrite::{ literal_expr, rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, LiteralExprValue, LogicalPlanLanguage, - WrapperPullupReplacerAliasToCube, WrapperPullupReplacerGroupedSubqueries, - WrapperPullupReplacerPushToCube, WrapperPushdownReplacerGroupedSubqueries, - WrapperPushdownReplacerPushToCube, + WrapperReplacerContextAliasToCube, }, - copy_flag, copy_value, var, var_iter, + var, var_iter, }; use crate::compile::rewrite::{ rewriter::{CubeEGraph, CubeRewrite}, rules::utils::{DecomposedDayTime, DecomposedMonthDayNano}, + wrapper_replacer_context, }; use datafusion::scalar::ScalarValue; use egg::Subst; @@ -23,56 +22,49 @@ impl WrapperRules { "wrapper-push-down-literal", wrapper_pushdown_replacer( literal_expr("?value"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( literal_expr("?value"), - "?alias_to_cube", - "?pullup_push_to_cube", - "?in_projection", - "?cube_members", - "?pullup_grouped_subqueries", - ), - self.transform_literal( - "?alias_to_cube", - "?value", - "?push_to_cube", - "?pullup_push_to_cube", - "?grouped_subqueries", - "?pullup_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), + self.transform_literal("?alias_to_cube", "?value"), ), transforming_rewrite( "wrapper-push-down-interval-literal", wrapper_pushdown_replacer( literal_expr("?value"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?new_value", - "?alias_to_cube", - "?pullup_push_to_cube", - "?in_projection", - "?cube_members", - "?pullup_grouped_subqueries", - ), - self.transform_interval_literal( - "?alias_to_cube", - "?value", - "?new_value", - "?push_to_cube", - "?pullup_push_to_cube", - "?grouped_subqueries", - "?pullup_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), + self.transform_interval_literal("?alias_to_cube", "?value", "?new_value"), ), ]); } @@ -81,44 +73,14 @@ impl WrapperRules { &self, alias_to_cube_var: &str, value_var: &str, - push_to_cube_var: &str, - pullup_push_to_cube_var: &str, - grouped_subqueries_var: &str, - pullup_grouped_subqueries_var: &str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let value_var = var!(value_var); - let push_to_cube_var = var!(push_to_cube_var); - let pullup_push_to_cube_var = var!(pullup_push_to_cube_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pullup_grouped_subqueries_var = var!(pullup_grouped_subqueries_var); let meta = self.meta_context.clone(); move |egraph, subst| { - if !copy_flag!( - egraph, - subst, - push_to_cube_var, - WrapperPushdownReplacerPushToCube, - pullup_push_to_cube_var, - WrapperPullupReplacerPushToCube - ) { - return false; - } - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries, - pullup_grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries - ) { - return false; - } - for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) { if let Some(sql_generator) = meta.sql_generator_by_alias_to_cube(alias_to_cube) { for literal in var_iter!(egraph[subst[value_var]], LiteralExprValue) { @@ -151,45 +113,15 @@ impl WrapperRules { alias_to_cube_var: &str, value_var: &str, new_value_var: &str, - push_to_cube_var: &str, - pullup_push_to_cube_var: &str, - grouped_subqueries_var: &str, - pullup_grouped_subqueries_var: &str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let value_var = var!(value_var); let new_value_var = var!(new_value_var); - let push_to_cube_var = var!(push_to_cube_var); - let pullup_push_to_cube_var = var!(pullup_push_to_cube_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pullup_grouped_subqueries_var = var!(pullup_grouped_subqueries_var); let meta = self.meta_context.clone(); move |egraph, subst| { - if !copy_flag!( - egraph, - subst, - push_to_cube_var, - WrapperPushdownReplacerPushToCube, - pullup_push_to_cube_var, - WrapperPullupReplacerPushToCube - ) { - return false; - } - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries, - pullup_grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries - ) { - return false; - } - for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) { if let Some(sql_generator) = meta.sql_generator_by_alias_to_cube(alias_to_cube) { let contains_template = diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/mod.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/mod.rs index 974a05623d072..60a18fc41b4a8 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/mod.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/mod.rs @@ -30,22 +30,17 @@ mod wrapper_pull_up; use crate::{ compile::rewrite::{ - fun_expr, - rewriter::{CubeEGraph, CubeRewrite, RewriteRules}, + fun_expr, rewrite, + rewriter::{CubeRewrite, RewriteRules}, rules::{ replacer_flat_pull_up_node, replacer_flat_push_down_node, replacer_pull_up_node, replacer_push_down_node, }, - transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, ListType, - WrapperPullupReplacerGroupedSubqueries, WrapperPullupReplacerPushToCube, - WrapperPushdownReplacerGroupedSubqueries, WrapperPushdownReplacerPushToCube, + wrapper_pullup_replacer, wrapper_pushdown_replacer, ListType, }, config::ConfigObj, - copy_flag, copy_value, transport::MetaContext, - var, }; -use egg::Subst; use std::{fmt::Display, sync::Arc}; pub struct WrapperRules { @@ -116,16 +111,7 @@ impl WrapperRules { rules.extend(replacer_push_down_node( rule_name, list_node, - |node| { - wrapper_pushdown_replacer( - node, - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ) - }, + |node| wrapper_pushdown_replacer(node, "?context"), false, )); @@ -133,81 +119,16 @@ impl WrapperRules { rule_name, list_node, substitute_list_node, - |node| { - wrapper_pullup_replacer( - node, - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ) - }, + |node| wrapper_pullup_replacer(node, "?context"), )); - rules.extend(vec![transforming_rewrite( + rules.extend(vec![rewrite( &format!("{}-tail", rule_name), - wrapper_pushdown_replacer( - list_node, - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - substitute_list_node, - "?alias_to_cube", - "?pullup_push_to_cube", - "?in_projection", - "?cube_members", - "?pullup_grouped_subqueries", - ), - Self::transform_list_tail( - "?push_to_cube", - "?pullup_push_to_cube", - "?grouped_subqueries", - "?pullup_grouped_subqueries", - ), + wrapper_pushdown_replacer(list_node, "?context"), + wrapper_pullup_replacer(substitute_list_node, "?context"), )]); } - fn transform_list_tail( - push_to_cube_var: &str, - pullup_push_to_cube_var: &str, - grouped_subqueries_var: &str, - pullup_grouped_subqueries_var: &str, - ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { - let push_to_cube_var = var!(push_to_cube_var); - let pullup_push_to_cube_var = var!(pullup_push_to_cube_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pullup_grouped_subqueries_var = var!(pullup_grouped_subqueries_var); - move |egraph, subst| { - if !copy_flag!( - egraph, - subst, - push_to_cube_var, - WrapperPushdownReplacerPushToCube, - pullup_push_to_cube_var, - WrapperPullupReplacerPushToCube - ) { - return false; - } - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries, - pullup_grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries - ) { - return false; - } - true - } - } - fn flat_list_pushdown_pullup_rules( rules: &mut Vec, rule_name: &str, @@ -217,16 +138,7 @@ impl WrapperRules { rules.extend(replacer_flat_push_down_node( rule_name, list_type.clone(), - |node| { - wrapper_pushdown_replacer( - node, - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ) - }, + |node| wrapper_pushdown_replacer(node, "?context"), false, )); @@ -234,88 +146,17 @@ impl WrapperRules { rule_name, list_type.clone(), substitute_list_type.clone(), - |node| { - wrapper_pullup_replacer( - node, - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ) - }, - &[ - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ], + |node| wrapper_pullup_replacer(node, "?context"), + &["?context"], )); - rules.extend(vec![transforming_rewrite( + rules.extend(vec![rewrite( &format!("{}-tail", rule_name), - wrapper_pushdown_replacer( - list_type.empty_list(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - substitute_list_type.empty_list(), - "?alias_to_cube", - "?pullup_push_to_cube", - "?in_projection", - "?cube_members", - "?pullup_grouped_subqueries", - ), - Self::transform_flat_list_tail( - "?push_to_cube", - "?pullup_push_to_cube", - "?grouped_subqueries", - "?pullup_grouped_subqueries", - ), + wrapper_pushdown_replacer(list_type.empty_list(), "?context"), + wrapper_pullup_replacer(substitute_list_type.empty_list(), "?context"), )]); } - fn transform_flat_list_tail( - push_to_cube_var: &str, - pullup_push_to_cube_var: &str, - grouped_subqueries_var: &str, - pullup_grouped_subqueries_var: &str, - ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { - let push_to_cube_var = var!(push_to_cube_var); - let pullup_push_to_cube_var = var!(pullup_push_to_cube_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pullup_grouped_subqueries_var = var!(pullup_grouped_subqueries_var); - move |egraph, subst| { - if !copy_flag!( - egraph, - subst, - push_to_cube_var, - WrapperPushdownReplacerPushToCube, - pullup_push_to_cube_var, - WrapperPullupReplacerPushToCube - ) { - return false; - } - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries, - pullup_grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries - ) { - return false; - } - true - } - } - fn expr_list_pushdown_pullup_rules( rules: &mut Vec, rule_name: &str, @@ -324,16 +165,7 @@ impl WrapperRules { rules.extend(replacer_push_down_node( rule_name, list_node, - |node| { - wrapper_pushdown_replacer( - node, - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ) - }, + |node| wrapper_pushdown_replacer(node, "?context"), false, )); @@ -341,78 +173,13 @@ impl WrapperRules { rule_name, list_node, list_node, - |node| { - wrapper_pullup_replacer( - node, - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ) - }, + |node| wrapper_pullup_replacer(node, "?context"), )); - rules.extend(vec![transforming_rewrite( + rules.extend(vec![rewrite( rule_name, - wrapper_pushdown_replacer( - list_node, - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - list_node, - "?alias_to_cube", - "?pullup_push_to_cube", - "?in_projection", - "?cube_members", - "?pullup_grouped_subqueries", - ), - Self::transform_expr_list_tail( - "?push_to_cube", - "?pullup_push_to_cube", - "?grouped_subqueries", - "?pullup_grouped_subqueries", - ), + wrapper_pushdown_replacer(list_node, "?context"), + wrapper_pullup_replacer(list_node, "?context"), )]); } - - fn transform_expr_list_tail( - push_to_cube_var: &str, - pullup_push_to_cube_var: &str, - grouped_subqueries_var: &str, - pullup_grouped_subqueries_var: &str, - ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { - let push_to_cube_var = var!(push_to_cube_var); - let pullup_push_to_cube_var = var!(pullup_push_to_cube_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pullup_grouped_subqueries_var = var!(pullup_grouped_subqueries_var); - move |egraph, subst| { - if !copy_flag!( - egraph, - subst, - push_to_cube_var, - WrapperPushdownReplacerPushToCube, - pullup_push_to_cube_var, - WrapperPullupReplacerPushToCube - ) { - return false; - } - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries, - pullup_grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries - ) { - return false; - } - true - } - } } diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/negative_expr.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/negative_expr.rs index d0fcaafd0ec8d..d7b312e4b5a47 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/negative_expr.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/negative_expr.rs @@ -4,7 +4,7 @@ use crate::{ rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, - WrapperPullupReplacerAliasToCube, + wrapper_replacer_context, WrapperReplacerContextAliasToCube, }, var, var_iter, }; @@ -15,40 +15,30 @@ impl WrapperRules { rules.extend(vec![ rewrite( "wrapper-negative-push-down", - wrapper_pushdown_replacer( - negative_expr("?expr"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - negative_expr(wrapper_pushdown_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - )), + wrapper_pushdown_replacer(negative_expr("?expr"), "?context"), + negative_expr(wrapper_pushdown_replacer("?expr", "?context")), ), transforming_rewrite( "wrapper-negative-pull-up", negative_expr(wrapper_pullup_replacer( "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), )), wrapper_pullup_replacer( negative_expr("?expr"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), self.transform_negative_expr("?alias_to_cube"), ), @@ -64,7 +54,7 @@ impl WrapperRules { move |egraph, subst| { for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) .cloned() { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/not_expr.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/not_expr.rs index 6c1673d040e73..3195fb10c6a1b 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/not_expr.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/not_expr.rs @@ -4,7 +4,7 @@ use crate::{ rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, - WrapperPullupReplacerAliasToCube, + wrapper_replacer_context, WrapperReplacerContextAliasToCube, }, var, var_iter, }; @@ -15,40 +15,30 @@ impl WrapperRules { rules.extend(vec![ rewrite( "wrapper-not-push-down", - wrapper_pushdown_replacer( - not_expr("?expr"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - not_expr(wrapper_pushdown_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - )), + wrapper_pushdown_replacer(not_expr("?expr"), "?context"), + not_expr(wrapper_pushdown_replacer("?expr", "?context")), ), transforming_rewrite( "wrapper-not-pull-up", not_expr(wrapper_pullup_replacer( "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), )), wrapper_pullup_replacer( not_expr("?expr"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), self.transform_not_expr("?alias_to_cube"), ), @@ -64,7 +54,7 @@ impl WrapperRules { move |egraph, subst| { for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) .cloned() { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/order.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/order.rs index 9c634c46d7134..dc9fda0132b10 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/order.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/order.rs @@ -1,20 +1,12 @@ -use crate::{ - compile::rewrite::{ - cube_scan_wrapper, - rewriter::{CubeEGraph, CubeRewrite}, - rules::wrapper::WrapperRules, - sort, transforming_rewrite, wrapped_select, wrapped_select_order_expr_empty_tail, - wrapper_pullup_replacer, wrapper_pushdown_replacer, WrapperPullupReplacerGroupedSubqueries, - WrapperPullupReplacerPushToCube, WrapperPushdownReplacerGroupedSubqueries, - WrapperPushdownReplacerPushToCube, - }, - copy_flag, copy_value, var, +use crate::compile::rewrite::{ + cube_scan_wrapper, rewrite, rewriter::CubeRewrite, rules::wrapper::WrapperRules, sort, + wrapped_select, wrapped_select_order_expr_empty_tail, wrapper_pullup_replacer, + wrapper_pushdown_replacer, }; -use egg::Subst; impl WrapperRules { pub fn order_rules(&self, rules: &mut Vec) { - rules.extend(vec![transforming_rewrite( + rules.extend(vec![rewrite( "wrapper-push-down-order-to-cube-scan", sort( "?order_expr", @@ -39,11 +31,7 @@ impl WrapperRules { "?select_push_to_cube", "?select_ungrouped_scan", ), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + "?context", ), "CubeScanWrapperFinalized:false", ), @@ -51,81 +39,18 @@ impl WrapperRules { cube_scan_wrapper( wrapped_select( "?select_type", - wrapper_pullup_replacer( - "?projection_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?subqueries", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?group_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?aggr_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?window_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?joins", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?filter_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pullup_replacer("?projection_expr", "?context"), + wrapper_pullup_replacer("?subqueries", "?context"), + wrapper_pullup_replacer("?group_expr", "?context"), + wrapper_pullup_replacer("?aggr_expr", "?context"), + wrapper_pullup_replacer("?window_expr", "?context"), + wrapper_pullup_replacer("?cube_scan_input", "?context"), + wrapper_pullup_replacer("?joins", "?context"), + wrapper_pullup_replacer("?filter_expr", "?context"), "?having_expr", "?limit", "?offset", - wrapper_pushdown_replacer( - "?order_expr", - "?alias_to_cube", - "?pushdown_push_to_cube", - "?in_projection", - "?cube_members", - "?pushdown_grouped_subqueries", - ), + wrapper_pushdown_replacer("?order_expr", "?context"), "?select_alias", "?select_distinct", "?select_push_to_cube", @@ -133,12 +58,6 @@ impl WrapperRules { ), "CubeScanWrapperFinalized:false", ), - self.transform_order( - "?push_to_cube", - "?pushdown_push_to_cube", - "?grouped_subqueries", - "?pushdown_grouped_subqueries", - ), )]); Self::list_pushdown_pullup_rules( @@ -148,41 +67,4 @@ impl WrapperRules { "WrappedSelectOrderExpr", ); } - - fn transform_order( - &self, - push_to_cube_var: &'static str, - pushdown_push_to_cube_var: &'static str, - grouped_subqueries_var: &'static str, - pushdown_grouped_subqueries_var: &'static str, - ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { - let push_to_cube_var = var!(push_to_cube_var); - let pushdown_push_to_cube_var = var!(pushdown_push_to_cube_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pushdown_grouped_subqueries_var = var!(pushdown_grouped_subqueries_var); - move |egraph, subst| { - if !copy_flag!( - egraph, - subst, - push_to_cube_var, - WrapperPullupReplacerPushToCube, - pushdown_push_to_cube_var, - WrapperPushdownReplacerPushToCube - ) { - return false; - } - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries, - pushdown_grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries - ) { - return false; - } - true - } - } } diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/projection.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/projection.rs index bc8c0cf7e337e..fd8bf67316cea 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/projection.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/projection.rs @@ -8,12 +8,11 @@ use crate::{ wrapped_select_having_expr_empty_tail, wrapped_select_joins_empty_tail, wrapped_select_order_expr_empty_tail, wrapped_select_subqueries_empty_tail, wrapped_select_window_expr_empty_tail, wrapper_pullup_replacer, wrapper_pushdown_replacer, - ListType, LogicalPlanLanguage, ProjectionAlias, WrappedSelectAlias, - WrappedSelectPushToCube, WrappedSelectUngroupedScan, - WrapperPullupReplacerGroupedSubqueries, WrapperPullupReplacerPushToCube, - WrapperPushdownReplacerGroupedSubqueries, WrapperPushdownReplacerPushToCube, + wrapper_replacer_context, ListType, LogicalPlanLanguage, ProjectionAlias, + WrappedSelectAlias, WrappedSelectPushToCube, WrappedSelectUngroupedScan, + WrapperReplacerContextPushToCube, }, - copy_flag, copy_value, var, var_iter, + var, var_iter, }; use egg::{Subst, Var}; @@ -26,11 +25,13 @@ impl WrapperRules { cube_scan_wrapper( wrapper_pullup_replacer( "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), "CubeScanWrapperFinalized:false", ), @@ -42,78 +43,96 @@ impl WrapperRules { "WrappedSelectSelectType:Projection", wrapper_pushdown_replacer( "?expr", - "?alias_to_cube", - "?pushdown_push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?pushdown_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_subqueries_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_group_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_aggr_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_window_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_joins_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_filter_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapped_select_having_expr_empty_tail(), "WrappedSelectLimit:None", "WrappedSelectOffset:None", wrapper_pullup_replacer( wrapped_select_order_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), "?select_alias", "WrappedSelectDistinct:false", @@ -126,12 +145,9 @@ impl WrapperRules { "?expr", "?projection_alias", "?push_to_cube", - "?pushdown_push_to_cube", "?select_alias", "?select_push_to_cube", "?select_ungrouped_scan", - "?grouped_subqueries", - "?pushdown_grouped_subqueries", ), )]); @@ -161,11 +177,13 @@ impl WrapperRules { cube_scan_wrapper( wrapper_pullup_replacer( "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), "CubeScanWrapperFinalized:false", ), @@ -180,78 +198,96 @@ impl WrapperRules { "WrappedSelectSelectType:Projection", wrapper_pushdown_replacer( "?expr", - "?alias_to_cube", - "?pushdown_push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?pushdown_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pushdown_replacer( "?subqueries", - "?alias_to_cube", - "?pushdown_push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?pushdown_grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_group_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_aggr_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_window_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_joins_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select_filter_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), wrapped_select_having_expr_empty_tail(), "WrappedSelectLimit:None", "WrappedSelectOffset:None", wrapper_pullup_replacer( wrapped_select_order_expr_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "WrapperPullupReplacerInProjection:true", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "WrapperReplacerContextInProjection:true", + "?cube_members", + "?grouped_subqueries", + ), ), "?select_alias", "WrappedSelectDistinct:false", @@ -265,12 +301,9 @@ impl WrapperRules { "?expr", "?projection_alias", "?push_to_cube", - "?pushdown_push_to_cube", "?select_alias", "?select_push_to_cube", "?select_ungrouped_scan", - "?grouped_subqueries", - "?pushdown_grouped_subqueries", ), )]); } @@ -279,22 +312,16 @@ impl WrapperRules { expr_var: &'static str, projection_alias_var: &'static str, push_to_cube_var: &'static str, - pushdown_push_to_cube_var: &'static str, select_alias_var: &'static str, select_push_to_cube_var: &'static str, select_ungrouped_scan_var: &'static str, - grouped_subqueries_var: &'static str, - pushdown_grouped_subqueries_var: &'static str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let expr_var = var!(expr_var); let projection_alias_var = var!(projection_alias_var); let push_to_cube_var = var!(push_to_cube_var); - let pushdown_push_to_cube_var = var!(pushdown_push_to_cube_var); let select_alias_var = var!(select_alias_var); let select_push_to_cube_var = var!(select_push_to_cube_var); let select_ungrouped_scan_var = var!(select_ungrouped_scan_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pushdown_grouped_subqueries_var = var!(pushdown_grouped_subqueries_var); move |egraph, subst| { Self::transform_projection_impl( egraph, @@ -302,12 +329,9 @@ impl WrapperRules { expr_var, projection_alias_var, push_to_cube_var, - pushdown_push_to_cube_var, select_alias_var, select_push_to_cube_var, select_ungrouped_scan_var, - grouped_subqueries_var, - pushdown_grouped_subqueries_var, ) } } @@ -318,24 +342,18 @@ impl WrapperRules { expr_var: &'static str, projection_alias_var: &'static str, push_to_cube_var: &'static str, - pushdown_push_to_cube_var: &'static str, select_alias_var: &'static str, select_push_to_cube_var: &'static str, select_ungrouped_scan_var: &'static str, - grouped_subqueries_var: &'static str, - pushdown_grouped_subqueries_var: &'static str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let expr_var = var!(expr_var); let projection_alias_var = var!(projection_alias_var); let push_to_cube_var = var!(push_to_cube_var); - let pushdown_push_to_cube_var = var!(pushdown_push_to_cube_var); let select_alias_var = var!(select_alias_var); let select_push_to_cube_var = var!(select_push_to_cube_var); let select_ungrouped_scan_var = var!(select_ungrouped_scan_var); let meta = self.meta_context.clone(); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pushdown_grouped_subqueries_var = var!(pushdown_grouped_subqueries_var); move |egraph, subst| { if Self::transform_check_subquery_allowed( egraph, @@ -349,12 +367,9 @@ impl WrapperRules { expr_var, projection_alias_var, push_to_cube_var, - pushdown_push_to_cube_var, select_alias_var, select_push_to_cube_var, select_ungrouped_scan_var, - grouped_subqueries_var, - pushdown_grouped_subqueries_var, ) } else { false @@ -368,42 +383,17 @@ impl WrapperRules { expr_var: Var, projection_alias_var: Var, push_to_cube_var: Var, - pushdown_push_to_cube_var: Var, select_alias_var: Var, select_push_to_cube_var: Var, select_ungrouped_scan_var: Var, - grouped_subqueries_var: Var, - pushdown_grouped_subqueries_var: Var, ) -> bool { if let Some(_) = &egraph[subst[expr_var]].data.referenced_expr { - if !copy_flag!( - egraph, - subst, - push_to_cube_var, - WrapperPullupReplacerPushToCube, - pushdown_push_to_cube_var, - WrapperPushdownReplacerPushToCube - ) { - return false; - } - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries, - pushdown_grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries - ) { - return false; - } - for projection_alias in var_iter!(egraph[subst[projection_alias_var]], ProjectionAlias).cloned() { for push_to_cube in var_iter!( egraph[subst[push_to_cube_var]], - WrapperPullupReplacerPushToCube + WrapperReplacerContextPushToCube ) .cloned() { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/scalar_function.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/scalar_function.rs index b18ad1f035ec1..f9ad941b5e7cf 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/scalar_function.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/scalar_function.rs @@ -4,12 +4,10 @@ use crate::{ rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, scalar_fun_expr_args_empty_tail, scalar_fun_expr_args_legacy, transforming_rewrite, - wrapper_pullup_replacer, wrapper_pushdown_replacer, ListPattern, ListType, - ScalarFunctionExprFun, WrapperPullupReplacerAliasToCube, - WrapperPullupReplacerGroupedSubqueries, WrapperPullupReplacerPushToCube, - WrapperPushdownReplacerGroupedSubqueries, WrapperPushdownReplacerPushToCube, + wrapper_pullup_replacer, wrapper_pushdown_replacer, wrapper_replacer_context, ListPattern, + ListType, ScalarFunctionExprFun, WrapperReplacerContextAliasToCube, }, - copy_flag, copy_value, var, var_iter, + var, var_iter, }; use egg::Subst; @@ -18,25 +16,8 @@ impl WrapperRules { rules.extend(vec![ rewrite( "wrapper-push-down-function", - wrapper_pushdown_replacer( - fun_expr_var_arg("?fun", "?args"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - fun_expr_var_arg( - "?fun", - wrapper_pushdown_replacer( - "?args", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - ), + wrapper_pushdown_replacer(fun_expr_var_arg("?fun", "?args"), "?context"), + fun_expr_var_arg("?fun", wrapper_pushdown_replacer("?args", "?context")), ), transforming_rewrite( "wrapper-pull-up-function", @@ -44,6 +25,18 @@ impl WrapperRules { "?fun", wrapper_pullup_replacer( "?args", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), + ), + ), + wrapper_pullup_replacer( + fun_expr_var_arg("?fun", "?args"), + wrapper_replacer_context( "?alias_to_cube", "?push_to_cube", "?in_projection", @@ -51,40 +44,12 @@ impl WrapperRules { "?grouped_subqueries", ), ), - wrapper_pullup_replacer( - fun_expr_var_arg("?fun", "?args"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), self.transform_fun_expr("?fun", "?alias_to_cube"), ), - transforming_rewrite( + rewrite( "wrapper-push-down-scalar-function-empty-tail", - wrapper_pushdown_replacer( - scalar_fun_expr_args_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - scalar_fun_expr_args_empty_tail(), - "?alias_to_cube", - "?pullup_push_to_cube", - "?in_projection", - "?cube_members", - "?pullup_grouped_subqueries", - ), - self.transform_scalar_function_empty_tail( - "?push_to_cube", - "?pullup_push_to_cube", - "?grouped_subqueries", - "?pullup_grouped_subqueries", - ), + wrapper_pushdown_replacer(scalar_fun_expr_args_empty_tail(), "?context"), + wrapper_pullup_replacer(scalar_fun_expr_args_empty_tail(), "?context"), ), ]); @@ -94,28 +59,14 @@ impl WrapperRules { "wrapper-push-down-scalar-function-args", ListType::ScalarFunctionExprArgs, ListPattern { - pattern: wrapper_pushdown_replacer( - "?args", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + pattern: wrapper_pushdown_replacer("?args", "?context"), list_var: "?args".to_string(), elem: "?arg".to_string(), }, ListPattern { pattern: "?new_args".to_string(), list_var: "?new_args".to_string(), - elem: wrapper_pushdown_replacer( - "?arg", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + elem: wrapper_pushdown_replacer("?arg", "?context"), }, ), list_rewrite_with_vars( @@ -124,33 +75,14 @@ impl WrapperRules { ListPattern { pattern: "?args".to_string(), list_var: "?args".to_string(), - elem: wrapper_pullup_replacer( - "?arg", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + elem: wrapper_pullup_replacer("?arg", "?context"), }, ListPattern { - pattern: wrapper_pullup_replacer( - "?new_args", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + pattern: wrapper_pullup_replacer("?new_args", "?context"), list_var: "?new_args".to_string(), elem: "?arg".to_string(), }, - &[ - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - ], + &["?context"], ), ]); } else { @@ -159,102 +91,28 @@ impl WrapperRules { "wrapper-push-down-scalar-function-args", wrapper_pushdown_replacer( scalar_fun_expr_args_legacy("?left", "?right"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + "?context", ), scalar_fun_expr_args_legacy( - wrapper_pushdown_replacer( - "?left", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pushdown_replacer( - "?right", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pushdown_replacer("?left", "?context"), + wrapper_pushdown_replacer("?right", "?context"), ), ), rewrite( "wrapper-pull-up-scalar-function-args", scalar_fun_expr_args_legacy( - wrapper_pullup_replacer( - "?left", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?right", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pullup_replacer("?left", "?context"), + wrapper_pullup_replacer("?right", "?context"), ), wrapper_pullup_replacer( scalar_fun_expr_args_legacy("?left", "?right"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + "?context", ), ), ]); } } - fn transform_scalar_function_empty_tail( - &self, - push_to_cube_var: &'static str, - pullup_push_to_cube_var: &'static str, - grouped_subqueries_var: &'static str, - pullup_grouped_subqueries_var: &'static str, - ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { - let push_to_cube_var = var!(push_to_cube_var); - let pullup_push_to_cube_var = var!(pullup_push_to_cube_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pullup_grouped_subqueries_var = var!(pullup_grouped_subqueries_var); - move |egraph, subst| { - if !copy_flag!( - egraph, - subst, - push_to_cube_var, - WrapperPushdownReplacerPushToCube, - pullup_push_to_cube_var, - WrapperPullupReplacerPushToCube - ) { - return false; - } - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries, - pullup_grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries - ) { - return false; - } - - true - } - } - fn transform_fun_expr( &self, fun_var: &'static str, @@ -266,7 +124,7 @@ impl WrapperRules { move |egraph, subst| { for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) .cloned() { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/sort_expr.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/sort_expr.rs index 28c6ecf4270da..55cd9d983f212 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/sort_expr.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/sort_expr.rs @@ -8,23 +8,9 @@ impl WrapperRules { rules.extend(vec![ rewrite( "wrapper-push-down-sort-expr", - wrapper_pushdown_replacer( - sort_expr("?expr", "?asc", "?nulls_first"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pushdown_replacer(sort_expr("?expr", "?asc", "?nulls_first"), "?context"), sort_expr( - wrapper_pushdown_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pushdown_replacer("?expr", "?context"), "?asc", "?nulls_first", ), @@ -32,25 +18,11 @@ impl WrapperRules { rewrite( "wrapper-pull-up-sort-expr", sort_expr( - wrapper_pullup_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pullup_replacer("?expr", "?context"), "?asc", "?nulls_first", ), - wrapper_pullup_replacer( - sort_expr("?expr", "?asc", "?nulls_first"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pullup_replacer(sort_expr("?expr", "?asc", "?nulls_first"), "?context"), ), ]); } diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/subquery.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/subquery.rs index c53723235da4f..563fafa6a106f 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/subquery.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/subquery.rs @@ -4,11 +4,9 @@ use crate::{ rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, - EmptyRelationDerivedSourceTableName, LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, - WrapperPullupReplacerGroupedSubqueries, WrapperPullupReplacerPushToCube, - WrapperPushdownReplacerGroupedSubqueries, WrapperPushdownReplacerPushToCube, + wrapper_replacer_context, EmptyRelationDerivedSourceTableName, LogicalPlanLanguage, + WrapperReplacerContextAliasToCube, WrapperReplacerContextGroupedSubqueries, }, - copy_flag, copy_value, transport::MetaContext, var, var_iter, var_list_iter, }; @@ -22,37 +20,13 @@ impl WrapperRules { "wrapper-subqueries-wrapped-scan-to-pull", wrapper_pushdown_replacer( cube_scan_wrapper( - wrapper_pullup_replacer( - "?cube_scan_input", - "?inner_alias_to_cube", - "?nner_push_to_cube", - "?inner_in_projection", - "?inner_cube_members", - "?inner_grouped_subqueries", - ), + wrapper_pullup_replacer("?cube_scan_input", "?inner_context"), "CubeScanWrapperFinalized:false", ), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?cube_scan_input", - "?alias_to_cube", - "?pullup_push_to_cube", - "?in_projection", - "?cube_members", - "?pullup_grouped_subqueries", - ), - self.transform_check_subquery_wrapped( - "?cube_scan_input", - "?push_to_cube", - "?pullup_push_to_cube", - "?grouped_subqueries", - "?pullup_grouped_subqueries", + "?context", ), + wrapper_pullup_replacer("?cube_scan_input", "?context"), + self.transform_check_subquery_wrapped("?cube_scan_input"), ), transforming_rewrite( "wrapper-subqueries-wrap-empty-rel", @@ -68,11 +42,13 @@ impl WrapperRules { "?derived_source_table_name", "EmptyRelationIsWrappable:true", ), - "?alias_to_cube", - "WrapperPullupReplacerPushToCube:false", - "WrapperPullupReplacerInProjection:true", - "CubeScanMembers", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "WrapperReplacerContextPushToCube:false", + "WrapperReplacerContextInProjection:true", + "CubeScanMembers", + "?grouped_subqueries", + ), ), "CubeScanWrapperFinalized:false", ), @@ -113,8 +89,8 @@ impl WrapperRules { { subst.insert( alias_to_cube_var, - egraph.add(LogicalPlanLanguage::WrapperPullupReplacerAliasToCube( - WrapperPullupReplacerAliasToCube(vec![( + egraph.add(LogicalPlanLanguage::WrapperReplacerContextAliasToCube( + WrapperReplacerContextAliasToCube(vec![( "".to_string(), cube.name.to_string(), )]), @@ -127,8 +103,8 @@ impl WrapperRules { subst.insert( grouped_subqueries_out_var, egraph.add( - LogicalPlanLanguage::WrapperPullupReplacerGroupedSubqueries( - WrapperPullupReplacerGroupedSubqueries(vec![]), + LogicalPlanLanguage::WrapperReplacerContextGroupedSubqueries( + WrapperReplacerContextGroupedSubqueries(vec![]), ), ), ); @@ -149,7 +125,7 @@ impl WrapperRules { ) -> bool { for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) .cloned() { @@ -169,40 +145,9 @@ impl WrapperRules { fn transform_check_subquery_wrapped( &self, cube_scan_input_var: &'static str, - push_to_cube_var: &'static str, - pullup_push_to_cube_var: &'static str, - grouped_subqueries_var: &'static str, - pullup_grouped_subqueries_var: &'static str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let cube_scan_input_var = var!(cube_scan_input_var); - let push_to_cube_var = var!(push_to_cube_var); - let pullup_push_to_cube_var = var!(pullup_push_to_cube_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pullup_grouped_subqueries_var = var!(pullup_grouped_subqueries_var); move |egraph, subst| { - if !copy_flag!( - egraph, - subst, - push_to_cube_var, - WrapperPushdownReplacerPushToCube, - pullup_push_to_cube_var, - WrapperPullupReplacerPushToCube - ) { - return false; - } - - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries, - pullup_grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries - ) { - return false; - } - for _ in var_list_iter!(egraph[subst[cube_scan_input_var]], WrappedSelect).cloned() { return true; } diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/udf_function.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/udf_function.rs index 418ff722764c6..f87f717188333 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/udf_function.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/udf_function.rs @@ -4,12 +4,10 @@ use crate::{ rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, transforming_rewrite, udf_expr_var_arg, udf_fun_expr_args, udf_fun_expr_args_empty_tail, - wrapper_pullup_replacer, wrapper_pushdown_replacer, ScalarUDFExprFun, - WrapperPullupReplacerAliasToCube, WrapperPullupReplacerGroupedSubqueries, - WrapperPullupReplacerPushToCube, WrapperPushdownReplacerGroupedSubqueries, - WrapperPushdownReplacerPushToCube, + wrapper_pullup_replacer, wrapper_pushdown_replacer, wrapper_replacer_context, + ScalarUDFExprFun, WrapperReplacerContextAliasToCube, }, - copy_flag, copy_value, var, var_iter, + var, var_iter, }; use egg::Subst; @@ -18,25 +16,8 @@ impl WrapperRules { rules.extend(vec![ rewrite( "wrapper-push-down-udf", - wrapper_pushdown_replacer( - udf_expr_var_arg("?fun", "?args"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - udf_expr_var_arg( - "?fun", - wrapper_pushdown_replacer( - "?args", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - ), + wrapper_pushdown_replacer(udf_expr_var_arg("?fun", "?args"), "?context"), + udf_expr_var_arg("?fun", wrapper_pushdown_replacer("?args", "?context")), ), transforming_rewrite( "wrapper-pull-up-udf", @@ -44,6 +25,18 @@ impl WrapperRules { "?fun", wrapper_pullup_replacer( "?args", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), + ), + ), + wrapper_pullup_replacer( + udf_expr_var_arg("?fun", "?args"), + wrapper_replacer_context( "?alias_to_cube", "?push_to_cube", "?in_projection", @@ -51,98 +44,28 @@ impl WrapperRules { "?grouped_subqueries", ), ), - wrapper_pullup_replacer( - udf_expr_var_arg("?fun", "?args"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), self.transform_udf_expr("?fun", "?alias_to_cube"), ), rewrite( "wrapper-push-down-udf-args", - wrapper_pushdown_replacer( - udf_fun_expr_args("?left", "?right"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pushdown_replacer(udf_fun_expr_args("?left", "?right"), "?context"), udf_fun_expr_args( - wrapper_pushdown_replacer( - "?left", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pushdown_replacer( - "?right", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pushdown_replacer("?left", "?context"), + wrapper_pushdown_replacer("?right", "?context"), ), ), rewrite( "wrapper-pull-up-udf-args", udf_fun_expr_args( - wrapper_pullup_replacer( - "?left", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?right", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - ), - wrapper_pullup_replacer( - udf_fun_expr_args("?left", "?right"), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_pullup_replacer("?left", "?context"), + wrapper_pullup_replacer("?right", "?context"), ), + wrapper_pullup_replacer(udf_fun_expr_args("?left", "?right"), "?context"), ), - transforming_rewrite( + rewrite( "wrapper-push-down-udf-empty-tail", - wrapper_pushdown_replacer( - udf_fun_expr_args_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - udf_fun_expr_args_empty_tail(), - "?alias_to_cube", - "?pullup_push_to_cube", - "?in_projection", - "?cube_members", - "?pullup_grouped_subqueries", - ), - self.transform_udf_expr_tail( - "?push_to_cube", - "?pullup_push_to_cube", - "?grouped_subqueries", - "?pullup_grouped_subqueries", - ), + wrapper_pushdown_replacer(udf_fun_expr_args_empty_tail(), "?context"), + wrapper_pullup_replacer(udf_fun_expr_args_empty_tail(), "?context"), ), ]); } @@ -158,7 +81,7 @@ impl WrapperRules { move |egraph, subst| { for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) .cloned() { @@ -177,41 +100,4 @@ impl WrapperRules { false } } - - fn transform_udf_expr_tail( - &self, - push_to_cube_var: &'static str, - pullup_push_to_cube_var: &'static str, - grouped_subqueries_var: &'static str, - pullup_grouped_subqueries_var: &'static str, - ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { - let push_to_cube_var = var!(push_to_cube_var); - let pullup_push_to_cube_var = var!(pullup_push_to_cube_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pullup_grouped_subqueries_var = var!(pullup_grouped_subqueries_var); - move |egraph, subst| { - if !copy_flag!( - egraph, - subst, - push_to_cube_var, - WrapperPushdownReplacerPushToCube, - pullup_push_to_cube_var, - WrapperPullupReplacerPushToCube - ) { - return false; - } - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries, - pullup_grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries - ) { - return false; - } - true - } - } } diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/window.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/window.rs index 444e40413ffec..ae829dc807304 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/window.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/window.rs @@ -1,20 +1,12 @@ -use crate::{ - compile::rewrite::{ - cube_scan_wrapper, - rewriter::{CubeEGraph, CubeRewrite}, - rules::wrapper::WrapperRules, - transforming_rewrite, window, wrapped_select, wrapped_select_window_expr_empty_tail, - wrapper_pullup_replacer, wrapper_pushdown_replacer, ListType, - WrapperPullupReplacerGroupedSubqueries, WrapperPullupReplacerPushToCube, - WrapperPushdownReplacerGroupedSubqueries, WrapperPushdownReplacerPushToCube, - }, - copy_flag, copy_value, var, +use crate::compile::rewrite::{ + cube_scan_wrapper, rewrite, rewriter::CubeRewrite, rules::wrapper::WrapperRules, window, + wrapped_select, wrapped_select_window_expr_empty_tail, wrapper_pullup_replacer, + wrapper_pushdown_replacer, ListType, }; -use egg::Subst; impl WrapperRules { pub fn window_rules(&self, rules: &mut Vec) { - rules.extend(vec![transforming_rewrite( + rules.extend(vec![rewrite( "wrapper-push-down-window-to-cube-scan", window( cube_scan_wrapper( @@ -38,11 +30,7 @@ impl WrapperRules { "?select_push_to_cube", "?select_ungrouped_scan", ), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + "?context", ), "CubeScanWrapperFinalized:false", ), @@ -51,81 +39,18 @@ impl WrapperRules { cube_scan_wrapper( wrapped_select( "?select_type", - wrapper_pullup_replacer( - "?projection_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?subqueries", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?group_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?aggr_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pushdown_replacer( - "?window_expr", - "?alias_to_cube", - "?pushdown_push_to_cube", - "?in_projection", - "?cube_members", - "?pushdown_grouped_subqueries", - ), - wrapper_pullup_replacer( - "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?joins", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pullup_replacer( - "?filter_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pullup_replacer("?projection_expr", "?context"), + wrapper_pullup_replacer("?subqueries", "?context"), + wrapper_pullup_replacer("?group_expr", "?context"), + wrapper_pullup_replacer("?aggr_expr", "?context"), + wrapper_pushdown_replacer("?window_expr", "?context"), + wrapper_pullup_replacer("?cube_scan_input", "?context"), + wrapper_pullup_replacer("?joins", "?context"), + wrapper_pullup_replacer("?filter_expr", "?context"), "?having_expr", "?limit", "?offset", - wrapper_pullup_replacer( - "?order_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pullup_replacer("?order_expr", "?context"), "?select_alias", "?select_distinct", "?select_push_to_cube", @@ -133,12 +58,6 @@ impl WrapperRules { ), "CubeScanWrapperFinalized:false", ), - self.transform_window_pushdown( - "?push_to_cube", - "?pushdown_push_to_cube", - "?grouped_subqueries", - "?pushdown_grouped_subqueries", - ), )]); if self.config_obj.push_down_pull_up_split() { @@ -157,41 +76,4 @@ impl WrapperRules { ); } } - - fn transform_window_pushdown( - &self, - push_to_cube_var: &'static str, - pushdown_push_to_cube_var: &'static str, - grouped_subqueries_var: &'static str, - pushdown_grouped_subqueries_var: &'static str, - ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { - let push_to_cube_var = var!(push_to_cube_var); - let pushdown_push_to_cube_var = var!(pushdown_push_to_cube_var); - let grouped_subqueries_var = var!(grouped_subqueries_var); - let pushdown_grouped_subqueries_var = var!(pushdown_grouped_subqueries_var); - move |egraph, subst| { - if !copy_flag!( - egraph, - subst, - push_to_cube_var, - WrapperPullupReplacerPushToCube, - pushdown_push_to_cube_var, - WrapperPushdownReplacerPushToCube - ) { - return false; - } - if !copy_value!( - egraph, - subst, - Vec, - grouped_subqueries_var, - WrapperPullupReplacerGroupedSubqueries, - pushdown_grouped_subqueries_var, - WrapperPushdownReplacerGroupedSubqueries - ) { - return false; - } - true - } - } } diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/window_function.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/window_function.rs index af5bf45c3343c..6d96d53501758 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/window_function.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/window_function.rs @@ -4,7 +4,8 @@ use crate::{ rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, transforming_rewrite, window_fun_expr_var_arg, wrapper_pullup_replacer, - wrapper_pushdown_replacer, WindowFunctionExprFun, WrapperPullupReplacerAliasToCube, + wrapper_pushdown_replacer, wrapper_replacer_context, WindowFunctionExprFun, + WrapperReplacerContextAliasToCube, }, var, var_iter, }; @@ -24,38 +25,13 @@ impl WrapperRules { "?order_by", "?window_frame", ), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + "?context", ), window_fun_expr_var_arg( "?fun", - wrapper_pushdown_replacer( - "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pushdown_replacer( - "?partition_by", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), - wrapper_pushdown_replacer( - "?order_by", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", - ), + wrapper_pushdown_replacer("?expr", "?context"), + wrapper_pushdown_replacer("?partition_by", "?context"), + wrapper_pushdown_replacer("?order_by", "?context"), "?window_frame", ), ), @@ -65,27 +41,33 @@ impl WrapperRules { "?fun", wrapper_pullup_replacer( "?expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?partition_by", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?order_by", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), "?window_frame", ), @@ -97,11 +79,13 @@ impl WrapperRules { "?order_by", "?window_frame", ), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), self.transform_window_fun_expr("?fun", "?alias_to_cube"), ), @@ -137,7 +121,7 @@ impl WrapperRules { move |egraph, subst| { for alias_to_cube in var_iter!( egraph[subst[alias_to_cube_var]], - WrapperPullupReplacerAliasToCube + WrapperReplacerContextAliasToCube ) .cloned() { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/wrapper_pull_up.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/wrapper_pull_up.rs index 64eaa28bc9a76..962bd949752f4 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/wrapper_pull_up.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/wrapper_pull_up.rs @@ -4,8 +4,8 @@ use crate::{ rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, transforming_rewrite, wrapped_select, wrapped_select_having_expr_empty_tail, - wrapped_select_joins_empty_tail, wrapper_pullup_replacer, WrappedSelectSelectType, - WrappedSelectType, + wrapped_select_joins_empty_tail, wrapper_pullup_replacer, wrapper_replacer_context, + WrappedSelectSelectType, WrappedSelectType, }, var, var_iter, var_list_iter, }; @@ -21,78 +21,96 @@ impl WrapperRules { "?select_type", wrapper_pullup_replacer( "?projection_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?subqueries", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?group_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?aggr_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?window_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?cube_scan_input", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?joins", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?filter_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapped_select_having_expr_empty_tail(), "WrappedSelectLimit:None", "WrappedSelectOffset:None", wrapper_pullup_replacer( "?order_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), "?select_alias", "?select_distinct", @@ -122,14 +140,16 @@ impl WrapperRules { "?select_push_to_cube", "?select_ungrouped_scan", ), - "?alias_to_cube", - // TODO in fact ungrouped flag is being used not only to indicate that underlying query is ungrouped however to indicate that WrappedSelect won't push down Cube members. Do we need separate flags? - // This is fixed to false for any LHS because we should only allow to push to Cube when from is ungrouped CubeScan - // And after pulling replacer over this node it will be WrappedSelect(from=CubeScan), so it should not allow to push for whatever LP is on top of it - "WrapperPullupReplacerPushToCube:false", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + // TODO in fact ungrouped flag is being used not only to indicate that underlying query is ungrouped however to indicate that WrappedSelect won't push down Cube members. Do we need separate flags? + // This is fixed to false for any LHS because we should only allow to push to Cube when from is ungrouped CubeScan + // And after pulling replacer over this node it will be WrappedSelect(from=CubeScan), so it should not allow to push for whatever LP is on top of it + "WrapperReplacerContextPushToCube:false", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), "CubeScanWrapperFinalized:false", ), @@ -142,43 +162,53 @@ impl WrapperRules { "?select_type", wrapper_pullup_replacer( "?projection_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?subqueries", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?group_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?aggr_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?window_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( wrapped_select( @@ -200,39 +230,47 @@ impl WrapperRules { "?inner_push_to_cube", "?inner_ungrouped_scan", ), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( // TODO handle non-empty joins wrapped_select_joins_empty_tail(), - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapper_pullup_replacer( "?filter_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), wrapped_select_having_expr_empty_tail(), "WrappedSelectLimit:None", "WrappedSelectOffset:None", wrapper_pullup_replacer( "?order_expr", - "?alias_to_cube", - "?push_to_cube", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + "?push_to_cube", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), "?select_alias", "?select_distinct", @@ -281,13 +319,15 @@ impl WrapperRules { "WrappedSelectPushToCube:false", "?select_ungrouped_scan", ), - "?alias_to_cube", - // This is fixed to false for any LHS because we should only allow to push to Cube when from is ungrouped CubeSCan - // And after pulling replacer over this node it will be WrappedSelect(from=WrappedSelect), so it should not allow to push for whatever LP is on top of it - "WrapperPullupReplacerPushToCube:false", - "?in_projection", - "?cube_members", - "?grouped_subqueries", + wrapper_replacer_context( + "?alias_to_cube", + // This is fixed to false for any LHS because we should only allow to push to Cube when from is ungrouped CubeSCan + // And after pulling replacer over this node it will be WrappedSelect(from=WrappedSelect), so it should not allow to push for whatever LP is on top of it + "WrapperReplacerContextPushToCube:false", + "?in_projection", + "?cube_members", + "?grouped_subqueries", + ), ), "CubeScanWrapperFinalized:false", ),