11use super :: PreAggregationsCompiler ;
22use super :: * ;
3+ use crate :: logical_plan:: visitor:: { LogicalPlanRewriter , NodeRewriteResult } ;
34use crate :: logical_plan:: * ;
45use crate :: plan:: FilterItem ;
56use crate :: planner:: query_tools:: QueryTools ;
@@ -24,9 +25,7 @@ impl PreAggregationOptimizer {
2425 }
2526
2627 pub fn try_optimize ( & mut self , plan : Rc < Query > ) -> Result < Option < Rc < Query > > , CubeError > {
27- let mut cube_names_collector = CubeNamesCollector :: new ( ) ;
28- cube_names_collector. collect ( & plan) ?;
29- let cube_names = cube_names_collector. result ( ) ;
28+ let cube_names = collect_cube_names_from_node ( & plan) ?;
3029 let mut compiler = PreAggregationsCompiler :: try_new ( self . query_tools . clone ( ) , & cube_names) ?;
3130
3231 let compiled_pre_aggregations = compiler. compile_all_pre_aggregations ( ) ?;
@@ -50,66 +49,43 @@ impl PreAggregationOptimizer {
5049 query : Rc < Query > ,
5150 pre_aggregation : & Rc < CompiledPreAggregation > ,
5251 ) -> Result < Option < Rc < Query > > , CubeError > {
53- match query. as_ref ( ) {
54- Query :: SimpleQuery ( query) => self . try_rewrite_simple_query ( query, pre_aggregation) ,
55- Query :: FullKeyAggregateQuery ( query) => {
56- self . try_rewrite_full_key_aggregate_query ( query, pre_aggregation)
57- }
52+ if query. multistage_members . is_empty ( ) {
53+ self . try_rewrite_simple_query ( & query, pre_aggregation)
54+ } else if !self . allow_multi_stage {
55+ Ok ( None )
56+ } else {
57+ self . try_rewrite_query_with_multistages ( & query, pre_aggregation)
5858 }
5959 }
6060
6161 fn try_rewrite_simple_query (
6262 & mut self ,
63- query : & SimpleQuery ,
63+ query : & Query ,
6464 pre_aggregation : & Rc < CompiledPreAggregation > ,
6565 ) -> Result < Option < Rc < Query > > , CubeError > {
6666 if self . is_schema_and_filters_match ( & query. schema , & query. filter , pre_aggregation) ? {
67- let mut new_query = SimpleQuery :: clone ( & query) ;
68- new_query. source = SimpleQuerySource :: PreAggregation (
69- self . make_pre_aggregation_source ( pre_aggregation) ?,
70- ) ;
71- Ok ( Some ( Rc :: new ( Query :: SimpleQuery ( new_query) ) ) )
67+ let mut new_query = query. clone ( ) ;
68+ new_query. source =
69+ QuerySource :: PreAggregation ( self . make_pre_aggregation_source ( pre_aggregation) ?) ;
70+ Ok ( Some ( Rc :: new ( new_query) ) )
7271 } else {
7372 Ok ( None )
7473 }
7574 }
7675
77- fn try_rewrite_full_key_aggregate_query (
76+ fn try_rewrite_query_with_multistages (
7877 & mut self ,
79- query : & FullKeyAggregateQuery ,
78+ query : & Query ,
8079 pre_aggregation : & Rc < CompiledPreAggregation > ,
8180 ) -> Result < Option < Rc < Query > > , CubeError > {
82- if !self . allow_multi_stage && !query. multistage_members . is_empty ( ) {
83- return Ok ( None ) ;
84- }
85- if self . allow_multi_stage && !query. multistage_members . is_empty ( ) {
86- return self
87- . try_rewrite_full_key_aggregate_query_with_multi_stages ( query, pre_aggregation) ;
88- }
89-
90- if self . is_schema_and_filters_match ( & query. schema , & query. filter , pre_aggregation) ? {
91- let source = SimpleQuerySource :: PreAggregation (
92- self . make_pre_aggregation_source ( pre_aggregation) ?,
93- ) ;
94- let new_query = SimpleQuery {
95- schema : query. schema . clone ( ) ,
96- dimension_subqueries : vec ! [ ] ,
97- filter : query. filter . clone ( ) ,
98- modifers : query. modifers . clone ( ) ,
99- source,
100- } ;
101- Ok ( Some ( Rc :: new ( Query :: SimpleQuery ( new_query) ) ) )
102- } else {
103- Ok ( None )
81+ let rewriter = LogicalPlanRewriter :: new ( ) ;
82+ for multi_stage in & query. multistage_members {
83+ let rewritten = rewriter. rewrite_top_down ( multi_stage. clone ( ) , & mut |& plan_node| {
84+ Ok ( NodeRewriteResult :: stop ( ) )
85+ } ) ?;
10486 }
105- }
10687
107- fn try_rewrite_full_key_aggregate_query_with_multi_stages (
108- & mut self ,
109- query : & FullKeyAggregateQuery ,
110- pre_aggregation : & Rc < CompiledPreAggregation > ,
111- ) -> Result < Option < Rc < Query > > , CubeError > {
112- let used_multi_stage_symbols = self . collect_multi_stage_symbols ( & query. source ) ;
88+ /* let used_multi_stage_symbols = self.collect_multi_stage_symbols(&query.source);
11389 let mut multi_stages_queries = query.multistage_members.clone();
11490 let mut rewrited_multistage = multi_stages_queries
11591 .iter()
@@ -187,11 +163,12 @@ impl PreAggregationOptimizer {
187163 order_by: query.modifers.order_by.clone(),
188164 }),
189165 source,
190- } ;
191- Ok ( Some ( Rc :: new ( Query :: FullKeyAggregateQuery ( result) ) ) )
166+ }; */
167+ //Ok(Some(Rc::new(Query::FullKeyAggregateQuery(result))))
168+ Ok ( None )
192169 }
193170
194- fn try_rewrite_multistage (
171+ /* fn try_rewrite_multistage(
195172 &mut self,
196173 multi_stage_name: &String,
197174 multi_stage_queries: &mut Vec<Rc<LogicalMultiStageMember>>,
@@ -382,17 +359,12 @@ impl PreAggregationOptimizer {
382359 }
383360 }
384361 symbols
385- }
362+ } */
386363
387364 fn make_pre_aggregation_source (
388365 & mut self ,
389366 pre_aggregation : & Rc < CompiledPreAggregation > ,
390367 ) -> Result < Rc < PreAggregation > , CubeError > {
391- /* let pre_aggregation_obj = self.query_tools.base_tools().get_pre_aggregation_by_name(
392- pre_aggregation.cube_name.clone(),
393- pre_aggregation.name.clone(),
394- )?; */
395- //if let Some(table_name) = &pre_aggregation_obj.static_data().table_name {
396368 let schema = LogicalSchema {
397369 time_dimensions : vec ! [ ] ,
398370 dimensions : pre_aggregation
0 commit comments