diff --git a/rust/cubesql/cubesql/src/compile/rewrite/cost.rs b/rust/cubesql/cubesql/src/compile/rewrite/cost.rs index 383a01a357622..2d46d8dfc5bff 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/cost.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/cost.rs @@ -255,8 +255,8 @@ pub struct CubePlanCost { non_pushed_down_limit_sort: i64, joins: usize, wrapper_nodes: i64, - wrapped_select_ungrouped_scan: usize, ast_size_outside_wrapper: usize, + wrapped_select_ungrouped_scan: usize, filters: i64, structure_points: i64, filter_members: i64, diff --git a/rust/cubesql/cubesql/src/compile/rewrite/mod.rs b/rust/cubesql/cubesql/src/compile/rewrite/mod.rs index cef3544c46e0f..cffb58f07151a 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/mod.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/mod.rs @@ -469,6 +469,13 @@ crate::plan_to_language! { // Known qualifiers of grouped subqueries // Used to allow to rewrite columns from them even with push to Cube enabled grouped_subqueries: Vec, + // When `member` is logical plan this means it is actually ungrouped, even when push_to_cube is disabled. + // When `member` is expression it just acts as a pull-through from pushdown. + // It will be filled by every wrapper replacer producer rule, essentially same way as + // ungrouped_scan flag in wrapped_select is filled: + // fixed false for aggregation, copy inner value for projection. + // This flag should make roundtrip from top to bottom and back. + ungrouped_scan: bool, }, WrapperPushdownReplacer { member: Arc, @@ -1974,9 +1981,10 @@ fn wrapper_replacer_context( in_projection: impl Display, cube_members: impl Display, grouped_subqueries: impl Display, + ungrouped_scan: impl Display, ) -> String { format!( - "(WrapperReplacerContext {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} {ungrouped_scan})", ) } 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 08b26e133e010..d1cff7bfc5844 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/aggregate.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/aggregate.rs @@ -38,6 +38,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "CubeScanWrapperFinalized:false", @@ -57,6 +58,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pullup_replacer( @@ -67,6 +69,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pushdown_replacer( @@ -77,6 +80,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pushdown_replacer( @@ -87,6 +91,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pullup_replacer( @@ -97,6 +102,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pullup_replacer( @@ -107,6 +113,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pullup_replacer( @@ -117,6 +124,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pullup_replacer( @@ -127,6 +135,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapped_select_having_expr_empty_tail(), @@ -140,6 +149,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), "WrappedSelectAlias:None", @@ -166,6 +176,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), grouping_set_expr( @@ -177,6 +188,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "?type", @@ -194,6 +206,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "?type", @@ -206,6 +219,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), ), @@ -231,6 +245,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), vec![("?aggr_expr", aggr_expr)], @@ -242,6 +257,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.pushdown_measure( @@ -314,6 +330,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "CubeScanWrapperFinalized:false", @@ -336,6 +353,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pushdown_replacer( @@ -346,6 +364,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pushdown_replacer( @@ -356,6 +375,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pushdown_replacer( @@ -366,6 +386,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pullup_replacer( @@ -376,6 +397,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pullup_replacer( @@ -386,6 +408,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pullup_replacer( @@ -396,6 +419,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pullup_replacer( @@ -406,6 +430,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapped_select_having_expr_empty_tail(), @@ -419,6 +444,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), "WrappedSelectAlias:None", @@ -457,6 +483,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -467,6 +494,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -477,6 +505,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -487,6 +516,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -497,6 +527,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -507,6 +538,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -517,6 +549,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -527,6 +560,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapped_select_having_expr_empty_tail(), @@ -541,6 +575,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "WrappedSelectAlias:None", @@ -565,6 +600,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pullup_replacer( @@ -575,6 +611,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pushdown_replacer( @@ -585,6 +622,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pushdown_replacer( @@ -595,6 +633,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pullup_replacer( @@ -605,6 +644,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pullup_replacer( @@ -615,6 +655,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pullup_replacer( @@ -625,6 +666,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapper_pullup_replacer( @@ -635,6 +677,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), wrapped_select_having_expr_empty_tail(), @@ -648,6 +691,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?cube_members", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), "WrappedSelectAlias:None", 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 50cc573d4c54e..3bf986b4d066d 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 @@ -39,6 +39,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), )], "?distinct", @@ -51,6 +52,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.transform_agg_fun_expr("?fun", "?distinct", "?alias_to_cube"), 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 31c7454238abb..282410cb43aa7 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 @@ -33,6 +33,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "?op", @@ -44,6 +45,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), ), @@ -55,6 +57,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.transform_binary_expr("?op", "?alias_to_cube"), 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 e7c2adda0bc42..ced6eac68cfef 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/case.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/case.rs @@ -33,6 +33,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -43,6 +44,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -53,6 +55,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), ), @@ -64,6 +67,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.transform_case_expr("?alias_to_cube"), 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 922322c82bc3d..a3ce49c8b9c28 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/column.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/column.rs @@ -25,6 +25,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -35,6 +36,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), ), @@ -50,6 +52,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -60,6 +63,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.pushdown_simple_measure("?name", "?cube_members"), @@ -75,6 +79,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -85,6 +90,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.pushdown_dimension( 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 9077ab32f15c2..c170a20db7885 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 @@ -6,9 +6,9 @@ use crate::{ transforming_rewrite, wrapper_pullup_replacer, wrapper_replacer_context, CubeScanAliasToCube, CubeScanLimit, CubeScanOffset, CubeScanUngrouped, LogicalPlanLanguage, WrapperReplacerContextAliasToCube, WrapperReplacerContextGroupedSubqueries, - WrapperReplacerContextPushToCube, + WrapperReplacerContextPushToCube, WrapperReplacerContextUngroupedScan, }, - var, var_iter, + copy_flag, var, var_iter, }; use egg::Subst; @@ -49,6 +49,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:false", "?members", "?grouped_subqueries_out", + "?ungrouped_scan_out", ), ), "CubeScanWrapperFinalized:false", @@ -62,6 +63,7 @@ impl WrapperRules { "?alias_to_cube_out", "?push_to_cube_out", "?grouped_subqueries_out", + "?ungrouped_scan_out", ), ), rewrite( @@ -85,6 +87,7 @@ impl WrapperRules { alias_to_cube_var_out: &'static str, push_to_cube_out_var: &'static str, grouped_subqueries_out_var: &'static str, + ungrouped_scan_out_var: &'static str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let members_var = var!(members_var); let alias_to_cube_var = var!(alias_to_cube_var); @@ -94,6 +97,7 @@ impl WrapperRules { let alias_to_cube_var_out = var!(alias_to_cube_var_out); let push_to_cube_out_var = var!(push_to_cube_out_var); let grouped_subqueries_out_var = var!(grouped_subqueries_out_var); + let ungrouped_scan_out_var = var!(ungrouped_scan_out_var); move |egraph, subst| { let mut has_no_limit_or_offset = true; for limit in var_iter!(egraph[subst[limit_var]], CubeScanLimit).cloned() { @@ -103,6 +107,17 @@ impl WrapperRules { has_no_limit_or_offset &= offset.is_none(); } + if !copy_flag!( + egraph, + subst, + ungrouped_cube_var, + CubeScanUngrouped, + ungrouped_scan_out_var, + WrapperReplacerContextUngroupedScan + ) { + return false; + } + if let Some(_) = egraph[subst[members_var]].data.member_name_to_expr { for alias_to_cube in var_iter!(egraph[subst[alias_to_cube_var]], CubeScanAliasToCube).cloned() 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 db9af90f8e03d..1607b4de43030 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/distinct.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/distinct.rs @@ -51,7 +51,7 @@ impl WrapperRules { "?select_alias", "WrappedSelectDistinct:true", "WrappedSelectPushToCube:false", - "?select_ungrouped_scan", + "WrappedSelectUngroupedScan:false", ), "?context", ), 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 22e484d73065c..f03ef8593e9b4 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/extract.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/extract.rs @@ -25,6 +25,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -35,6 +36,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), ], @@ -50,6 +52,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.transform_date_part_expr("?alias_to_cube"), 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 7fecfda0a1892..b2bc06f96d951 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/filter.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/filter.rs @@ -9,10 +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, - wrapper_replacer_context, LogicalPlanLanguage, WrappedSelectPushToCube, - WrappedSelectUngroupedScan, WrapperReplacerContextPushToCube, + wrapper_replacer_context, WrappedSelectPushToCube, WrappedSelectUngroupedScan, + WrapperReplacerContextPushToCube, WrapperReplacerContextUngroupedScan, }, - var, var_iter, + copy_flag, var, }; use egg::{Subst, Var}; @@ -125,6 +125,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "CubeScanWrapperFinalized:false", @@ -141,6 +142,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -151,6 +153,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -161,6 +164,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -171,6 +175,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -181,6 +186,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -191,6 +197,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -201,6 +208,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapped_select_filter_expr( @@ -212,6 +220,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -222,6 +231,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), ), @@ -236,6 +246,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "WrappedSelectAlias:None", @@ -247,6 +258,7 @@ impl WrapperRules { ), self.transform_filter( "?push_to_cube", + "?ungrouped_scan", "?select_push_to_cube", "?select_ungrouped_scan", ), @@ -275,6 +287,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "CubeScanWrapperFinalized:false", @@ -294,6 +307,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pushdown_replacer( @@ -304,6 +318,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -314,6 +329,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -324,6 +340,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -334,6 +351,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -344,6 +362,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -354,6 +373,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapped_select_filter_expr( @@ -365,6 +385,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -375,6 +396,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), ), @@ -389,6 +411,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "WrappedSelectAlias:None", @@ -401,6 +424,7 @@ impl WrapperRules { self.transform_filter_subquery( "?alias_to_cube", "?push_to_cube", + "?ungrouped_scan", "?select_push_to_cube", "?select_ungrouped_scan", ), @@ -410,10 +434,12 @@ impl WrapperRules { fn transform_filter( &self, push_to_cube_var: &'static str, + ungrouped_scan_var: &'static str, select_push_to_cube_var: &'static str, select_ungrouped_scan_var: &'static str, ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let push_to_cube_var = var!(push_to_cube_var); + let ungrouped_scan_var = var!(ungrouped_scan_var); let select_push_to_cube_var = var!(select_push_to_cube_var); let select_ungrouped_scan_var = var!(select_ungrouped_scan_var); move |egraph, subst| { @@ -421,6 +447,7 @@ impl WrapperRules { egraph, subst, push_to_cube_var, + ungrouped_scan_var, select_push_to_cube_var, select_ungrouped_scan_var, ) @@ -431,11 +458,13 @@ impl WrapperRules { &self, alias_to_cube_var: &'static str, push_to_cube_var: &'static str, + ungrouped_scan_var: &'static str, select_push_to_cube_var: &'static str, select_ungrouped_scan_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 ungrouped_scan_var = var!(ungrouped_scan_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(); @@ -450,6 +479,7 @@ impl WrapperRules { egraph, subst, push_to_cube_var, + ungrouped_scan_var, select_push_to_cube_var, select_ungrouped_scan_var, ) @@ -463,30 +493,32 @@ impl WrapperRules { egraph: &mut CubeEGraph, subst: &mut Subst, push_to_cube_var: Var, + ungrouped_scan_var: Var, select_push_to_cube_var: Var, select_ungrouped_scan_var: Var, ) -> bool { - for push_to_cube in var_iter!( - egraph[subst[push_to_cube_var]], - WrapperReplacerContextPushToCube - ) - .cloned() - { - subst.insert( - select_push_to_cube_var, - egraph.add(LogicalPlanLanguage::WrappedSelectPushToCube( - WrappedSelectPushToCube(push_to_cube), - )), - ); + if !copy_flag!( + egraph, + subst, + push_to_cube_var, + WrapperReplacerContextPushToCube, + select_push_to_cube_var, + WrappedSelectPushToCube + ) { + return false; + } - subst.insert( - select_ungrouped_scan_var, - egraph.add(LogicalPlanLanguage::WrappedSelectUngroupedScan( - WrappedSelectUngroupedScan(push_to_cube), - )), - ); - return true; + if !copy_flag!( + egraph, + subst, + ungrouped_scan_var, + WrapperReplacerContextUngroupedScan, + select_ungrouped_scan_var, + WrappedSelectUngroupedScan + ) { + return false; } - false + + true } } 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 1e4530b29094b..3076d6c8cf4b6 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 @@ -43,6 +43,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -53,6 +54,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "?negated", @@ -65,6 +67,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.transform_in_list_expr("?alias_to_cube"), 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 d92160e09dab1..7722991af221f 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 @@ -28,6 +28,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), )), wrapper_pullup_replacer( @@ -38,6 +39,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.transform_is_null_expr("?alias_to_cube"), @@ -57,6 +59,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), )), wrapper_pullup_replacer( @@ -67,6 +70,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.transform_is_null_expr("?alias_to_cube"), 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 fc193258b9daa..8bc53d5e66bed 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/join.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/join.rs @@ -53,6 +53,7 @@ impl WrapperRules { // RHS of join is grouped, so it shouldn't have any cubes or members "?left_cube_members", "?left_grouped_subqueries", + "?left_ungrouped_scan", ), ), "CubeScanWrapperFinalized:false", @@ -63,12 +64,12 @@ impl WrapperRules { 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", + "?right_push_to_cube", "?in_projection", // Going to ignore this "?right_cube_members", "?right_grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), "CubeScanWrapperFinalized:false", @@ -90,6 +91,9 @@ impl WrapperRules { "?in_projection", "?left_cube_members", "?out_grouped_subqueries", + // Can use it, because we've checked that left input allows push-to-Cube, + // so it must be ungrouped, making this whole plan ungrouped + "WrapperReplacerContextUngroupedScan:true", ), ), wrapper_pullup_replacer( @@ -100,6 +104,7 @@ impl WrapperRules { "?in_projection", "?left_cube_members", "?out_grouped_subqueries", + "WrapperReplacerContextUngroupedScan:true", ), ), wrapper_pullup_replacer( @@ -110,6 +115,7 @@ impl WrapperRules { "?in_projection", "?left_cube_members", "?out_grouped_subqueries", + "WrapperReplacerContextUngroupedScan:true", ), ), wrapper_pullup_replacer( @@ -120,6 +126,7 @@ impl WrapperRules { "?in_projection", "?left_cube_members", "?out_grouped_subqueries", + "WrapperReplacerContextUngroupedScan:true", ), ), wrapper_pullup_replacer( @@ -130,6 +137,7 @@ impl WrapperRules { "?in_projection", "?left_cube_members", "?out_grouped_subqueries", + "WrapperReplacerContextUngroupedScan:true", ), ), wrapper_pullup_replacer( @@ -142,6 +150,7 @@ impl WrapperRules { "?in_projection", "?left_cube_members", "?out_grouped_subqueries", + "WrapperReplacerContextUngroupedScan:true", ), ), // We don't want to use list rules here, because ?right_input is already done @@ -155,6 +164,7 @@ impl WrapperRules { "?in_projection", "?left_cube_members", "?out_grouped_subqueries", + "WrapperReplacerContextUngroupedScan:true", ), ), wrapper_pushdown_replacer( @@ -168,6 +178,7 @@ impl WrapperRules { "?in_projection", "?left_cube_members", "?out_grouped_subqueries", + "WrapperReplacerContextUngroupedScan:true", ), ), "?out_join_type", @@ -181,6 +192,7 @@ impl WrapperRules { "?in_projection", "?left_cube_members", "?out_grouped_subqueries", + "WrapperReplacerContextUngroupedScan:true", ), ), ), @@ -192,6 +204,7 @@ impl WrapperRules { "?in_projection", "?left_cube_members", "?out_grouped_subqueries", + "WrapperReplacerContextUngroupedScan:true", ), ), wrapped_select_having_expr_empty_tail(), @@ -205,6 +218,7 @@ impl WrapperRules { "?in_projection", "?left_cube_members", "?out_grouped_subqueries", + "WrapperReplacerContextUngroupedScan:true", ), ), "WrappedSelectAlias:None", 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 3092309d3304c..cef5b86b4a0e0 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 @@ -47,6 +47,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -57,6 +58,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "?escape_char", @@ -75,6 +77,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.transform_like_expr("?alias_to_cube", "?like_type", "?escape_char"), 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 e1e55317ae091..fcc1f12c40649 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/literal.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/literal.rs @@ -28,6 +28,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -38,6 +39,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.transform_literal("?alias_to_cube", "?value"), @@ -52,6 +54,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -62,6 +65,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.transform_interval_literal("?alias_to_cube", "?value", "?new_value"), 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 d7b312e4b5a47..f0fd4b670aeac 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 @@ -28,6 +28,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), )), wrapper_pullup_replacer( @@ -38,6 +39,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.transform_negative_expr("?alias_to_cube"), 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 3195fb10c6a1b..1c08a27133260 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 @@ -28,6 +28,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), )), wrapper_pullup_replacer( @@ -38,6 +39,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.transform_not_expr("?alias_to_cube"), 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 fd8bf67316cea..65f80e07b64b6 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/projection.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/projection.rs @@ -10,9 +10,9 @@ use crate::{ wrapped_select_window_expr_empty_tail, wrapper_pullup_replacer, wrapper_pushdown_replacer, wrapper_replacer_context, ListType, LogicalPlanLanguage, ProjectionAlias, WrappedSelectAlias, WrappedSelectPushToCube, WrappedSelectUngroupedScan, - WrapperReplacerContextPushToCube, + WrapperReplacerContextPushToCube, WrapperReplacerContextUngroupedScan, }, - var, var_iter, + copy_flag, var, var_iter, }; use egg::{Subst, Var}; @@ -31,6 +31,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "CubeScanWrapperFinalized:false", @@ -49,6 +50,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -59,6 +61,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -69,6 +72,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -79,6 +83,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -89,6 +94,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -99,6 +105,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -109,6 +116,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -119,6 +127,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapped_select_having_expr_empty_tail(), @@ -132,6 +141,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "?select_alias", @@ -145,6 +155,7 @@ impl WrapperRules { "?expr", "?projection_alias", "?push_to_cube", + "?ungrouped_scan", "?select_alias", "?select_push_to_cube", "?select_ungrouped_scan", @@ -183,6 +194,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "CubeScanWrapperFinalized:false", @@ -204,6 +216,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pushdown_replacer( @@ -214,6 +227,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -224,6 +238,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -234,6 +249,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -244,6 +260,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -254,6 +271,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -264,6 +282,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -274,6 +293,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapped_select_having_expr_empty_tail(), @@ -287,6 +307,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "?select_alias", @@ -301,6 +322,7 @@ impl WrapperRules { "?expr", "?projection_alias", "?push_to_cube", + "?ungrouped_scan", "?select_alias", "?select_push_to_cube", "?select_ungrouped_scan", @@ -312,6 +334,7 @@ impl WrapperRules { expr_var: &'static str, projection_alias_var: &'static str, push_to_cube_var: &'static str, + ungrouped_scan_var: &'static str, select_alias_var: &'static str, select_push_to_cube_var: &'static str, select_ungrouped_scan_var: &'static str, @@ -319,6 +342,7 @@ impl WrapperRules { 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 ungrouped_scan_var = var!(ungrouped_scan_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); @@ -329,6 +353,7 @@ impl WrapperRules { expr_var, projection_alias_var, push_to_cube_var, + ungrouped_scan_var, select_alias_var, select_push_to_cube_var, select_ungrouped_scan_var, @@ -342,6 +367,7 @@ impl WrapperRules { expr_var: &'static str, projection_alias_var: &'static str, push_to_cube_var: &'static str, + ungrouped_scan_var: &'static str, select_alias_var: &'static str, select_push_to_cube_var: &'static str, select_ungrouped_scan_var: &'static str, @@ -350,6 +376,7 @@ impl WrapperRules { 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 ungrouped_scan_var = var!(ungrouped_scan_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); @@ -367,6 +394,7 @@ impl WrapperRules { expr_var, projection_alias_var, push_to_cube_var, + ungrouped_scan_var, select_alias_var, select_push_to_cube_var, select_ungrouped_scan_var, @@ -383,40 +411,44 @@ impl WrapperRules { expr_var: Var, projection_alias_var: Var, push_to_cube_var: Var, + ungrouped_scan_var: Var, select_alias_var: Var, select_push_to_cube_var: Var, select_ungrouped_scan_var: Var, ) -> bool { if let Some(_) = &egraph[subst[expr_var]].data.referenced_expr { + if !copy_flag!( + egraph, + subst, + push_to_cube_var, + WrapperReplacerContextPushToCube, + select_push_to_cube_var, + WrappedSelectPushToCube + ) { + return false; + } + + if !copy_flag!( + egraph, + subst, + ungrouped_scan_var, + WrapperReplacerContextUngroupedScan, + select_ungrouped_scan_var, + WrappedSelectUngroupedScan + ) { + 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]], - WrapperReplacerContextPushToCube - ) - .cloned() - { - subst.insert( - select_push_to_cube_var, - egraph.add(LogicalPlanLanguage::WrappedSelectPushToCube( - WrappedSelectPushToCube(push_to_cube), - )), - ); - subst.insert( - select_ungrouped_scan_var, - egraph.add(LogicalPlanLanguage::WrappedSelectUngroupedScan( - WrappedSelectUngroupedScan(push_to_cube), - )), - ); - subst.insert( - select_alias_var, - egraph.add(LogicalPlanLanguage::WrappedSelectAlias(WrappedSelectAlias( - projection_alias, - ))), - ); - return true; - } + subst.insert( + select_alias_var, + egraph.add(LogicalPlanLanguage::WrappedSelectAlias(WrappedSelectAlias( + projection_alias, + ))), + ); + return true; } } 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 f9ad941b5e7cf..462e9bfab2f53 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 @@ -31,6 +31,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), ), @@ -42,6 +43,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.transform_fun_expr("?fun", "?alias_to_cube"), 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 563fafa6a106f..5e9844aea7b73 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/subquery.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/subquery.rs @@ -48,6 +48,7 @@ impl WrapperRules { "WrapperReplacerContextInProjection:true", "CubeScanMembers", "?grouped_subqueries", + "WrapperReplacerContextUngroupedScan:false", ), ), "CubeScanWrapperFinalized:false", 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 f87f717188333..7fabf890a19b9 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 @@ -31,6 +31,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), ), @@ -42,6 +43,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.transform_udf_expr("?fun", "?alias_to_cube"), 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 6d96d53501758..1d020a4dc6e19 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 @@ -47,6 +47,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -57,6 +58,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -67,6 +69,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "?window_frame", @@ -85,6 +88,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), self.transform_window_fun_expr("?fun", "?alias_to_cube"), 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 962bd949752f4..6030fd7574a05 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 @@ -27,6 +27,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -37,6 +38,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -47,6 +49,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -57,6 +60,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -67,6 +71,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -77,6 +82,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -87,6 +93,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -97,6 +104,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapped_select_having_expr_empty_tail(), @@ -110,6 +118,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "?select_alias", @@ -142,13 +151,13 @@ impl WrapperRules { ), 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", + "?ungrouped_scan", ), ), "CubeScanWrapperFinalized:false", @@ -168,6 +177,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -178,6 +188,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -188,6 +199,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -198,6 +210,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -208,6 +221,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -236,6 +250,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -247,6 +262,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapper_pullup_replacer( @@ -257,6 +273,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), wrapped_select_having_expr_empty_tail(), @@ -270,6 +287,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "?select_alias", @@ -327,6 +345,7 @@ impl WrapperRules { "?in_projection", "?cube_members", "?grouped_subqueries", + "?ungrouped_scan", ), ), "CubeScanWrapperFinalized:false",