From 264f8da9e315884e5e6ec7c4529dc897261d84d0 Mon Sep 17 00:00:00 2001 From: Mikhail Cheshkov Date: Tue, 22 Oct 2024 18:41:04 +0300 Subject: [PATCH] refactor(cubesql): Add CubeEGraph and CubeRewrite type aliases (#8848) --- rust/cubesql/cubesql/src/compile/qtrace.rs | 20 +--- .../cubesql/src/compile/rewrite/analysis.rs | 2 +- .../cubesql/src/compile/rewrite/converter.rs | 36 +++---- .../cubesql/src/compile/rewrite/mod.rs | 86 +++++++---------- .../cubesql/src/compile/rewrite/rewriter.rs | 31 +++--- .../cubesql/src/compile/rewrite/rules/case.rs | 11 ++- .../src/compile/rewrite/rules/common.rs | 11 +-- .../src/compile/rewrite/rules/dates.rs | 21 ++--- .../src/compile/rewrite/rules/filters.rs | 94 +++++++++---------- .../compile/rewrite/rules/flatten/column.rs | 17 ++-- .../src/compile/rewrite/rules/flatten/mod.rs | 7 +- .../rewrite/rules/flatten/pass_through.rs | 14 +-- .../rewrite/rules/flatten/top_level.rs | 30 ++---- .../src/compile/rewrite/rules/members.rs | 75 +++++++-------- .../cubesql/src/compile/rewrite/rules/mod.rs | 18 ++-- .../src/compile/rewrite/rules/old_split.rs | 94 +++++++++---------- .../src/compile/rewrite/rules/order.rs | 12 +-- .../rewrite/rules/split/aggregate_function.rs | 56 +++-------- .../src/compile/rewrite/rules/split/alias.rs | 7 +- .../rewrite/rules/split/binary_expr.rs | 11 +-- .../src/compile/rewrite/rules/split/case.rs | 8 +- .../src/compile/rewrite/rules/split/cast.rs | 9 +- .../src/compile/rewrite/rules/split/column.rs | 9 +- .../src/compile/rewrite/rules/split/dates.rs | 33 ++----- .../compile/rewrite/rules/split/functions.rs | 10 +- .../rewrite/rules/split/granularity.rs | 22 ++--- .../src/compile/rewrite/rules/split/mod.rs | 68 ++++---------- .../compile/rewrite/rules/split/top_level.rs | 28 +++--- .../src/compile/rewrite/rules/utils.rs | 22 ++--- .../rewrite/rules/wrapper/aggregate.rs | 25 ++--- .../rules/wrapper/aggregate_function.rs | 13 ++- .../compile/rewrite/rules/wrapper/alias.rs | 7 +- .../rewrite/rules/wrapper/binary_expr.rs | 13 ++- .../src/compile/rewrite/rules/wrapper/case.rs | 10 +- .../src/compile/rewrite/rules/wrapper/cast.rs | 7 +- .../compile/rewrite/rules/wrapper/column.rs | 11 ++- .../rules/wrapper/cube_scan_wrapper.rs | 18 ++-- .../compile/rewrite/rules/wrapper/distinct.rs | 10 +- .../compile/rewrite/rules/wrapper/extract.rs | 13 ++- .../compile/rewrite/rules/wrapper/filter.rs | 19 ++-- .../rewrite/rules/wrapper/in_list_expr.rs | 17 ++-- .../rewrite/rules/wrapper/in_subquery_expr.rs | 10 +- .../rewrite/rules/wrapper/is_null_expr.rs | 17 ++-- .../rewrite/rules/wrapper/like_expr.rs | 13 ++- .../compile/rewrite/rules/wrapper/limit.rs | 10 +- .../compile/rewrite/rules/wrapper/literal.rs | 22 ++--- .../src/compile/rewrite/rules/wrapper/mod.rs | 14 ++- .../rewrite/rules/wrapper/negative_expr.rs | 13 ++- .../compile/rewrite/rules/wrapper/not_expr.rs | 13 ++- .../compile/rewrite/rules/wrapper/order.rs | 7 +- .../rewrite/rules/wrapper/projection.rs | 22 ++--- .../rewrite/rules/wrapper/scalar_function.rs | 20 ++-- .../rewrite/rules/wrapper/sort_expr.rs | 10 +- .../compile/rewrite/rules/wrapper/subquery.rs | 22 ++--- .../rewrite/rules/wrapper/udf_function.rs | 17 ++-- .../compile/rewrite/rules/wrapper/window.rs | 9 +- .../rewrite/rules/wrapper/window_function.rs | 18 ++-- .../rewrite/rules/wrapper/wrapper_pull_up.rs | 15 ++- .../src/compile/test/rewrite_engine.rs | 9 +- .../cubesql/cubesql/src/sql/compiler_cache.rs | 31 +++--- rust/cubesql/cubesql/src/utils/mod.rs | 8 +- 61 files changed, 538 insertions(+), 787 deletions(-) diff --git a/rust/cubesql/cubesql/src/compile/qtrace.rs b/rust/cubesql/cubesql/src/compile/qtrace.rs index aa8cf92509b8b..ca6fe276fbe5a 100644 --- a/rust/cubesql/cubesql/src/compile/qtrace.rs +++ b/rust/cubesql/cubesql/src/compile/qtrace.rs @@ -1,14 +1,10 @@ use std::{env, fs, sync::Arc}; -use super::rewrite::{ - analysis::{LogicalPlanAnalysis, LogicalPlanData}, - rewriter::IterInfo, - LogicalPlanLanguage, -}; -use crate::compile::test::find_cube_scans_deep_search; +use super::rewrite::{analysis::LogicalPlanData, rewriter::IterInfo, LogicalPlanLanguage}; +use crate::compile::{rewrite::rewriter::CubeEGraph, test::find_cube_scans_deep_search}; use cubeclient::models::V1LoadRequestQuery; use datafusion::logical_plan::LogicalPlan; -use egg::{EClass, EGraph, Iteration, Language}; +use egg::{EClass, Iteration, Language}; use serde::Serialize; use sqlparser::ast::Statement; use uuid::Uuid; @@ -82,10 +78,7 @@ impl Qtrace { self.statement(|stmt| stmt.set_optimized_plan(plan)); } - pub fn set_original_graph( - &mut self, - egraph: &EGraph, - ) { + pub fn set_original_graph(&mut self, egraph: &CubeEGraph) { self.statement(|stmt| stmt.set_original_graph(egraph)); } @@ -189,10 +182,7 @@ impl QtraceStatement { self.optimized_plan = Some(format!("{:?}", plan)); } - pub fn set_original_graph( - &mut self, - egraph: &EGraph, - ) { + pub fn set_original_graph(&mut self, egraph: &CubeEGraph) { self.original_graph = egraph .classes() .map(|eclass| QtraceEclass::make(eclass)) diff --git a/rust/cubesql/cubesql/src/compile/rewrite/analysis.rs b/rust/cubesql/cubesql/src/compile/rewrite/analysis.rs index 910f24ec4c723..f3646ea5f3b4a 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/analysis.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/analysis.rs @@ -1150,7 +1150,7 @@ impl LogicalPlanAnalysis { } fn eval_constant_expr( - egraph: &EGraph, + egraph: &EGraph, expr: &Expr, ) -> Option { let schema = DFSchema::empty(); diff --git a/rust/cubesql/cubesql/src/compile/rewrite/converter.rs b/rust/cubesql/cubesql/src/compile/rewrite/converter.rs index fd908d44d21a9..fe091d9527239 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/converter.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/converter.rs @@ -1,3 +1,4 @@ +pub use super::rewriter::CubeRunner; use crate::{ compile::{ engine::df::{ @@ -5,7 +6,9 @@ use crate::{ wrapper::CubeScanWrapperNode, }, rewrite::{ - analysis::LogicalPlanAnalysis, extract_exprlist_from_groupping_set, rewriter::Rewriter, + analysis::LogicalPlanAnalysis, + extract_exprlist_from_groupping_set, + rewriter::{CubeEGraph, Rewriter}, AggregateFunctionExprDistinct, AggregateFunctionExprFun, AggregateSplit, AggregateUDFExprFun, AliasExprAlias, AnyExprAll, AnyExprOp, BetweenExprNegated, BinaryExprOp, CastExprDataType, ChangeUserMemberValue, ColumnExprColumn, @@ -52,7 +55,7 @@ use datafusion::{ scalar::ScalarValue, sql::planner::ContextProvider, }; -use egg::{EGraph, Id, RecExpr}; +use egg::{Id, RecExpr}; use itertools::Itertools; use serde_json::json; use std::{ @@ -62,8 +65,6 @@ use std::{ sync::{Arc, LazyLock}, }; -pub use super::rewriter::CubeRunner; - macro_rules! add_data_node { ($converter:expr, $value_expr:expr, $field_variant:ident) => { $converter @@ -125,10 +126,7 @@ macro_rules! add_binary_expr_list_node { $flat_list ) } else { - fn to_binary_tree( - graph: &mut EGraph, - list: &[Id], - ) -> Id { + fn to_binary_tree(graph: &mut CubeEGraph, list: &[Id]) -> Id { if list.len() == 0 { graph.add(LogicalPlanLanguage::$field_variant(Vec::new())) } else if list.len() == 1 { @@ -186,7 +184,7 @@ static EXCLUDED_PARAM_VALUES: LazyLock> = LazyLock::new(|| }); pub struct LogicalPlanToLanguageConverter { - graph: EGraph, + graph: CubeEGraph, cube_context: Arc, flat_list: bool, } @@ -199,28 +197,22 @@ pub struct LogicalPlanToLanguageContext { impl LogicalPlanToLanguageConverter { pub fn new(cube_context: Arc, flat_list: bool) -> Self { Self { - graph: EGraph::::new( - LogicalPlanAnalysis::new( - cube_context.clone(), - Arc::new(DefaultPhysicalPlanner::default()), - ), - ), + graph: CubeEGraph::new(LogicalPlanAnalysis::new( + cube_context.clone(), + Arc::new(DefaultPhysicalPlanner::default()), + )), cube_context, flat_list, } } - pub fn add_expr( - graph: &mut EGraph, - expr: &Expr, - flat_list: bool, - ) -> Result { + pub fn add_expr(graph: &mut CubeEGraph, expr: &Expr, flat_list: bool) -> Result { // TODO: reference self? Self::add_expr_replace_params(graph, expr, &mut None, flat_list) } pub fn add_expr_replace_params( - graph: &mut EGraph, + graph: &mut CubeEGraph, expr: &Expr, query_params: &mut Option>, flat_list: bool, @@ -833,7 +825,7 @@ impl LogicalPlanToLanguageConverter { }) } - pub fn take_egraph(self) -> EGraph { + pub fn take_egraph(self) -> CubeEGraph { self.graph } diff --git a/rust/cubesql/cubesql/src/compile/rewrite/mod.rs b/rust/cubesql/cubesql/src/compile/rewrite/mod.rs index a63b4d97da4a0..2a355ccf3e62f 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/mod.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/mod.rs @@ -5,8 +5,12 @@ pub mod language; pub mod rewriter; pub mod rules; +use self::analysis::{LogicalPlanData, MemberNameToExpr}; use crate::{ - compile::rewrite::analysis::{LogicalPlanAnalysis, OriginalExpr}, + compile::rewrite::{ + analysis::{LogicalPlanAnalysis, OriginalExpr}, + rewriter::{CubeEGraph, CubeRewrite}, + }, CubeError, }; use analysis::MemberNamesToExpr; @@ -23,8 +27,8 @@ use datafusion::{ scalar::ScalarValue, }; use egg::{ - rewrite, Applier, EGraph, Id, Language, Pattern, PatternAst, Rewrite, SearchMatches, Searcher, - Subst, Symbol, Var, + rewrite, Applier, Id, Language, Pattern, PatternAst, Rewrite, SearchMatches, Searcher, Subst, + Symbol, Var, }; use itertools::Itertools; use std::{ @@ -33,9 +37,6 @@ use std::{ str::FromStr, sync::Arc, }; - -use self::analysis::{LogicalPlanData, MemberNameToExpr}; - // trace_macros!(true); #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Hash)] @@ -656,11 +657,7 @@ fn expr_column_name(expr: &Expr, cube: &Option) -> String { } } -pub fn rewrite( - name: &str, - searcher: String, - applier: String, -) -> Rewrite { +pub fn rewrite(name: &str, searcher: String, applier: String) -> CubeRewrite { Rewrite::new( name.to_string(), searcher.parse::>().unwrap(), @@ -674,12 +671,9 @@ pub fn transforming_rewrite( searcher: String, applier: String, transform_fn: T, -) -> Rewrite +) -> CubeRewrite where - T: Fn(&mut EGraph, &mut Subst) -> bool - + Sync - + Send - + 'static, + T: Fn(&mut CubeEGraph, &mut Subst) -> bool + Sync + Send + 'static, { Rewrite::new( name.to_string(), @@ -696,12 +690,9 @@ pub fn transforming_rewrite_with_root( searcher: String, applier: String, transform_fn: T, -) -> Rewrite +) -> CubeRewrite where - T: Fn(&mut EGraph, Id, &mut Subst) -> bool - + Sync - + Send - + 'static, + T: Fn(&mut CubeEGraph, Id, &mut Subst) -> bool + Sync + Send + 'static, { Rewrite::new( name.to_string(), @@ -717,12 +708,9 @@ pub fn transforming_chain_rewrite( chain: Vec<(&str, String)>, applier: String, transform_fn: T, -) -> Rewrite +) -> CubeRewrite where - T: Fn(&mut EGraph, &mut Subst) -> bool - + Sync - + Send - + 'static, + T: Fn(&mut CubeEGraph, &mut Subst) -> bool + Sync + Send + 'static, { Rewrite::new( name.to_string(), @@ -746,12 +734,9 @@ pub fn transforming_chain_rewrite_with_root( chain: Vec<(&str, String)>, applier: String, transform_fn: T, -) -> Rewrite +) -> CubeRewrite where - T: Fn(&mut EGraph, Id, &mut Subst) -> bool - + Sync - + Send - + 'static, + T: Fn(&mut CubeEGraph, Id, &mut Subst) -> bool + Sync + Send + 'static, { Rewrite::new( name.to_string(), @@ -904,7 +889,7 @@ impl ListNodeSearcher { fn search_from_list_matches<'a>( &'a self, - egraph: &EGraph, + egraph: &CubeEGraph, limit: usize, list_subst: &Subst, output: &mut Vec, @@ -968,7 +953,7 @@ impl ListNodeSearcher { impl Searcher for ListNodeSearcher { fn search_eclass_with_limit( &self, - egraph: &EGraph, + egraph: &CubeEGraph, eclass: Id, limit: usize, ) -> Option> { @@ -989,7 +974,7 @@ impl Searcher for ListNodeSearcher { fn search_eclasses_with_limit( &self, - egraph: &EGraph, + egraph: &CubeEGraph, eclasses: &mut dyn Iterator, limit: usize, ) -> Vec> { @@ -1109,7 +1094,7 @@ impl ListNodeApplier { impl Applier for ListNodeApplier { fn apply_one( &self, - egraph: &mut EGraph, + egraph: &mut CubeEGraph, mut eclass: Id, subst: &Subst, _searcher_ast: Option<&PatternAst>, @@ -1169,7 +1154,7 @@ pub fn list_rewrite( list_type: ListType, searcher: ListPattern, applier: ListPattern, -) -> Rewrite { +) -> CubeRewrite { let searcher = ListNodeSearcher::new( list_type.clone(), &searcher.list_var, @@ -1191,7 +1176,7 @@ pub fn list_rewrite_with_lists( searcher: ListPattern, applier_pattern: &str, lists: impl IntoIterator, -) -> Rewrite { +) -> CubeRewrite { let searcher = ListNodeSearcher::new( list_type.clone(), &searcher.list_var, @@ -1208,7 +1193,7 @@ pub fn list_rewrite_with_vars( searcher: ListPattern, applier: ListPattern, top_level_elem_vars: &[&str], -) -> Rewrite { +) -> CubeRewrite { let searcher = ListNodeSearcher::new( list_type.clone(), &searcher.list_var, @@ -1232,7 +1217,7 @@ pub fn list_rewrite_with_lists_and_vars( applier_pattern: &str, lists: impl IntoIterator, top_level_elem_vars: &[&str], -) -> Rewrite { +) -> CubeRewrite { let searcher = ListNodeSearcher::new( list_type.clone(), &searcher.list_var, @@ -2097,10 +2082,7 @@ fn distinct(input: impl Display) -> String { format!("(Distinct {})", input) } -pub fn original_expr_name( - egraph: &EGraph, - id: Id, -) -> Option { +pub fn original_expr_name(egraph: &CubeEGraph, id: Id) -> Option { egraph[id] .data .original_expr @@ -2123,7 +2105,7 @@ pub struct ChainSearcher { impl Searcher for ChainSearcher { fn search_eclasses_with_limit( &self, - egraph: &EGraph, + egraph: &CubeEGraph, eclasses: &mut dyn Iterator, limit: usize, ) -> Vec> { @@ -2141,7 +2123,7 @@ impl Searcher for ChainSearcher { fn search_eclass_with_limit( &self, - egraph: &EGraph, + egraph: &CubeEGraph, eclass: Id, limit: usize, ) -> Option> { @@ -2164,7 +2146,7 @@ impl Searcher for ChainSearcher { impl ChainSearcher { fn search_match_chained<'a>( &self, - egraph: &EGraph, + egraph: &CubeEGraph, mut cur_match: SearchMatches<'a, LogicalPlanLanguage>, ) -> Option> { let mut new_substs = vec![]; @@ -2195,7 +2177,7 @@ impl ChainSearcher { pub struct TransformingPattern where - T: Fn(&mut EGraph, Id, &mut Subst) -> bool, + T: Fn(&mut CubeEGraph, Id, &mut Subst) -> bool, { pattern: Pattern, vars_to_substitute: T, @@ -2203,7 +2185,7 @@ where impl TransformingPattern where - T: Fn(&mut EGraph, Id, &mut Subst) -> bool, + T: Fn(&mut CubeEGraph, Id, &mut Subst) -> bool, { pub fn new(pattern: &str, vars_to_substitute: T) -> Self { Self { @@ -2215,11 +2197,11 @@ where impl Applier for TransformingPattern where - T: Fn(&mut EGraph, Id, &mut Subst) -> bool, + T: Fn(&mut CubeEGraph, Id, &mut Subst) -> bool, { fn apply_one( &self, - egraph: &mut EGraph, + egraph: &mut CubeEGraph, eclass: Id, subst: &Subst, searcher_ast: Option<&PatternAst>, @@ -2237,13 +2219,13 @@ where pub fn transform_original_expr_to_alias( alias_expr_var: &'static str, -) -> impl Fn(&mut EGraph, Id, &mut Subst) -> bool { +) -> impl Fn(&mut CubeEGraph, Id, &mut Subst) -> bool { let alias_expr_var = var!(alias_expr_var); move |egraph, root, subst| add_root_original_expr_alias(egraph, root, subst, alias_expr_var) } pub fn add_root_original_expr_alias( - egraph: &mut EGraph, + egraph: &mut CubeEGraph, root: Id, subst: &mut Subst, alias_expr_var: Var, diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rewriter.rs b/rust/cubesql/cubesql/src/compile/rewrite/rewriter.rs index d7cf6cf591a95..06b65df62ef61 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rewriter.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rewriter.rs @@ -32,8 +32,11 @@ use std::{ time::Duration, }; +pub type CubeRewrite = Rewrite; +pub type CubeEGraph = EGraph; + pub struct Rewriter { - graph: EGraph, + graph: CubeEGraph, cube_context: Arc, } @@ -80,9 +83,7 @@ pub struct IterDebugInfo { } impl IterDebugInfo { - pub fn prepare_debug_data( - graph: &EGraph, - ) -> DebugData { + pub fn prepare_debug_data(graph: &CubeEGraph) -> DebugData { DebugData { applied_rules: None, nodes: graph @@ -185,20 +186,14 @@ impl IterationData for IterInfo { } impl Rewriter { - pub fn new( - graph: EGraph, - cube_context: Arc, - ) -> Self { + pub fn new(graph: CubeEGraph, cube_context: Arc) -> Self { Self { graph, cube_context, } } - pub fn rewrite_runner( - cube_context: Arc, - egraph: EGraph, - ) -> CubeRunner { + pub fn rewrite_runner(cube_context: Arc, egraph: CubeEGraph) -> CubeRunner { CubeRunner::new(LogicalPlanAnalysis::new( cube_context, Arc::new(DefaultPhysicalPlanner::default()), @@ -231,7 +226,7 @@ impl Rewriter { &mut self, auth_context: AuthContextRef, qtrace: &mut Option, - ) -> Result, CubeError> { + ) -> Result { let cube_context = self.cube_context.clone(); let egraph = self.graph.clone(); if let Some(qtrace) = qtrace { @@ -396,8 +391,8 @@ impl Rewriter { fn run_rewrites( cube_context: &Arc, - egraph: EGraph, - rules: Arc>>, + egraph: CubeEGraph, + rules: Arc>, stage: &str, ) -> Result<(CubeRunner, Vec), CubeError> { let runner = Self::rewrite_runner(cube_context.clone(), egraph); @@ -528,7 +523,7 @@ impl Rewriter { meta_context: Arc, config_obj: Arc, eval_stable_functions: bool, - ) -> Vec> { + ) -> Vec { let sql_push_down = Self::sql_push_down_enabled(); let rules: Vec> = vec![ Box::new(MemberRules::new( @@ -580,7 +575,7 @@ impl Rewriter { } pub trait RewriteRules { - fn rewrite_rules(&self) -> Vec>; + fn rewrite_rules(&self) -> Vec; } struct IncrementalScheduler { @@ -601,7 +596,7 @@ impl egg::RewriteScheduler for Increme fn search_rewrite<'a>( &mut self, iteration: usize, - egraph: &EGraph, + egraph: &CubeEGraph, rewrite: &'a Rewrite, ) -> Vec> { if iteration != self.current_iter { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/case.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/case.rs index 2febf3d910be8..ca2318a4fa327 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/case.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/case.rs @@ -1,4 +1,4 @@ -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; use crate::{ compile::rewrite::{ @@ -7,9 +7,10 @@ use crate::{ case_expr_replacer, case_expr_var_arg, case_expr_when_then_expr, case_expr_when_then_expr_empty_tail, column_expr, group_aggregate_split_replacer, group_expr_split_replacer, inner_aggregate_split_replacer, is_not_null_expr, is_null_expr, - literal_expr, outer_aggregate_split_replacer, rewrite, rewriter::RewriteRules, + literal_expr, outer_aggregate_split_replacer, rewrite, + rewriter::{CubeEGraph, CubeRewrite, RewriteRules}, transforming_rewrite, CaseExprReplacerAliasToCube, InnerAggregateSplitReplacerAliasToCube, - LogicalPlanAnalysis, LogicalPlanLanguage, + LogicalPlanLanguage, }, var, var_iter, }; @@ -17,7 +18,7 @@ use crate::{ pub struct CaseRules {} impl RewriteRules for CaseRules { - fn rewrite_rules(&self) -> Vec> { + fn rewrite_rules(&self) -> Vec { vec![ // Case replacer takes place of inner replacer transforming_rewrite( @@ -172,7 +173,7 @@ impl CaseRules { &self, inner_alias_to_cube_var: &'static str, case_alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let inner_alias_to_cube_var = var!(inner_alias_to_cube_var); let case_alias_to_cube_var = var!(case_alias_to_cube_var); move |egraph, subst| { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/common.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/common.rs index 7860e92635edc..ed45d3094c81e 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/common.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/common.rs @@ -1,9 +1,9 @@ use crate::{ compile::rewrite::{ agg_fun_expr, alias_expr, - analysis::{ConstantFolding, LogicalPlanAnalysis, OriginalExpr}, + analysis::{ConstantFolding, OriginalExpr}, binary_expr, column_expr, fun_expr, - rewriter::{RewriteRules, Rewriter}, + rewriter::{CubeEGraph, CubeRewrite, RewriteRules, Rewriter}, transform_original_expr_to_alias, transforming_rewrite_with_root, udf_expr, AliasExprAlias, LogicalPlanLanguage, }, @@ -11,7 +11,7 @@ use crate::{ var, }; use datafusion::{logical_plan::DFSchema, scalar::ScalarValue}; -use egg::{EGraph, Id, Rewrite, Subst}; +use egg::{Id, Subst}; use std::{fmt::Display, sync::Arc}; pub struct CommonRules { @@ -19,7 +19,7 @@ pub struct CommonRules { } impl RewriteRules for CommonRules { - fn rewrite_rules(&self) -> Vec> { + fn rewrite_rules(&self) -> Vec { let mut rules = vec![]; if Rewriter::sql_push_down_enabled() { @@ -89,8 +89,7 @@ impl CommonRules { &self, literal_var: &'static str, alias_var: &'static str, - ) -> impl Fn(&mut EGraph, Id, &mut Subst) -> bool - { + ) -> impl Fn(&mut CubeEGraph, Id, &mut Subst) -> bool { let literal_var = var!(literal_var); let alias_var = var!(alias_var); diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/dates.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/dates.rs index 05e67f4cb791c..b97ecb321c9c8 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/dates.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/dates.rs @@ -2,10 +2,10 @@ use super::utils; use crate::{ compile::rewrite::{ alias_expr, - analysis::{ConstantFolding, LogicalPlanAnalysis, OriginalExpr}, + analysis::{ConstantFolding, OriginalExpr}, binary_expr, cast_expr, cast_expr_explicit, column_expr, fun_expr, literal_expr, literal_int, literal_string, negative_expr, original_expr_name, rewrite, - rewriter::RewriteRules, + rewriter::{CubeEGraph, CubeRewrite, RewriteRules}, rules::utils::DeltaTimeUnitToken, to_day_interval_expr, transform_original_expr_to_alias, transforming_rewrite, transforming_rewrite_with_root, udf_expr, AliasExprAlias, CastExprDataType, @@ -19,7 +19,7 @@ use datafusion::{ logical_plan::DFSchema, scalar::ScalarValue, }; -use egg::{EGraph, Id, Rewrite, Subst}; +use egg::{Id, Subst}; use std::{convert::TryFrom, fmt::Display, sync::Arc}; pub struct DateRules { @@ -27,7 +27,7 @@ pub struct DateRules { } impl RewriteRules for DateRules { - fn rewrite_rules(&self) -> Vec> { + fn rewrite_rules(&self) -> Vec { vec![ // TODO ?interval rewrite( @@ -432,8 +432,7 @@ impl DateRules { interval_int_var: &'static str, interval_var: &'static str, alias_var: &'static str, - ) -> impl Fn(&mut EGraph, Id, &mut Subst) -> bool - { + ) -> impl Fn(&mut CubeEGraph, Id, &mut Subst) -> bool { let datepart_var = var!(datepart_var); let interval_int_var = var!(interval_int_var); let interval_var = var!(interval_var); @@ -501,7 +500,7 @@ impl DateRules { fn transform_to_date_to_timestamp( &self, format_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let format_var = var!(format_var); move |egraph, subst| { for format in var_iter!(egraph[subst[format_var]], LiteralExprValue) { @@ -523,7 +522,7 @@ impl DateRules { outer_granularity_var: &'static str, inner_granularity_var: &'static str, new_granularity_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let outer_granularity_var = var!(outer_granularity_var); let inner_granularity_var = var!(inner_granularity_var); let new_granularity_var = var!(new_granularity_var); @@ -576,8 +575,7 @@ impl DateRules { data_type_var: &'static str, granularity_var: &'static str, alias_var: &'static str, - ) -> impl Fn(&mut EGraph, Id, &mut Subst) -> bool - { + ) -> impl Fn(&mut CubeEGraph, Id, &mut Subst) -> bool { let data_type_var = var!(data_type_var); let granularity_var = var!(granularity_var); let alias_var = var!(alias_var); @@ -633,8 +631,7 @@ impl DateRules { pub fn transform_root_alias( &self, alias_var: &'static str, - ) -> impl Fn(&mut EGraph, Id, &mut Subst) -> bool - { + ) -> impl Fn(&mut CubeEGraph, Id, &mut Subst) -> bool { let alias_var = var!(alias_var); move |egraph, root, subst| { if let Some(OriginalExpr::Expr(original_expr)) = diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/filters.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/filters.rs index acd20572df886..0246487be65ed 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/filters.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/filters.rs @@ -2,7 +2,7 @@ use super::utils; use crate::{ compile::rewrite::{ alias_expr, - analysis::{ConstantFolding, LogicalPlanAnalysis, Member, OriginalExpr}, + analysis::{ConstantFolding, Member, OriginalExpr}, between_expr, binary_expr, case_expr, case_expr_var_arg, cast_expr, change_user_member, column_expr, cube_scan, cube_scan_filters, cube_scan_filters_empty_tail, cube_scan_members, dimension_expr, expr_column_name, filter, filter_member, filter_op, filter_op_filters, @@ -10,7 +10,7 @@ use crate::{ fun_expr_args_legacy, fun_expr_var_arg, inlist_expr, inlist_expr_list, is_not_null_expr, is_null_expr, like_expr, limit, list_rewrite, literal_bool, literal_expr, literal_int, literal_string, measure_expr, negative_expr, not_expr, projection, rewrite, - rewriter::RewriteRules, + rewriter::{CubeEGraph, CubeRewrite, RewriteRules}, scalar_fun_expr_args_empty_tail, segment_member, time_dimension_date_range_replacer, time_dimension_expr, transform_original_expr_to_alias, transforming_chain_rewrite, transforming_rewrite, transforming_rewrite_with_root, udf_expr, udf_expr_var_arg, @@ -45,7 +45,7 @@ use datafusion::{ logical_plan::{Column, Expr, Operator}, scalar::ScalarValue, }; -use egg::{EGraph, Rewrite, Subst, Var}; +use egg::{Subst, Var}; use std::{ collections::HashSet, fmt::Display, @@ -66,7 +66,7 @@ impl FilterRules { } impl RewriteRules for FilterRules { - fn rewrite_rules(&self) -> Vec> { + fn rewrite_rules(&self) -> Vec { let mut rules = vec![ transforming_rewrite( "push-down-filter", @@ -2519,7 +2519,7 @@ impl FilterRules { exp_var: &'static str, filter_alias_to_cube_var: &'static str, filter_aliases_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let exp_var = var!(exp_var); let filter_aliases_var = var!(filter_aliases_var); @@ -2556,7 +2556,7 @@ impl FilterRules { new_limit_var: &'static str, new_limit_skip_var: &'static str, new_limit_fetch_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let literal_var = var!(literal_var); let new_limit_var = var!(new_limit_var); let new_limit_skip_var = var!(new_limit_skip_var); @@ -2588,7 +2588,7 @@ impl FilterRules { fn transform_literal_true( &self, literal_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let literal_var = var!(literal_var); move |egraph, subst| { if let Some(ConstantFolding::Scalar(literal)) = @@ -2605,7 +2605,7 @@ impl FilterRules { fn push_down_limit_projection( &self, input_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let input_var = var!(input_var); move |egraph, subst| { for node in egraph[subst[input_var]].nodes.iter() { @@ -2621,7 +2621,7 @@ impl FilterRules { fn unwrap_lower_or_upper( &self, op_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let op_var = op_var.parse().unwrap(); move |egraph, subst| { @@ -2651,7 +2651,7 @@ impl FilterRules { filter_op_var: &'static str, filter_values_var: &'static str, filter_aliases_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let column_var = column_var.parse().unwrap(); let op_var = op_var.parse().unwrap(); let constant_var = var!(constant_var); @@ -2889,7 +2889,7 @@ impl FilterRules { filter_op_var: &'static str, filter_values_var: &'static str, filter_aliases_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let column_var = var!(column_var); let literal_var = var!(literal_var); let alias_to_cube_var = var!(alias_to_cube_var); @@ -2991,7 +2991,7 @@ impl FilterRules { literal_var: &'static str, op_var: &'static str, new_op_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let literal_var = var!(literal_var); let op_var = var!(op_var); let new_op_var = var!(new_op_var); @@ -3049,7 +3049,7 @@ impl FilterRules { alias_to_cube_var: &'static str, members_var: &'static str, filter_aliases_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let column_left_var = var!(column_left_var); let column_right_var = var!(column_right_var); let alias_to_cube_var = var!(alias_to_cube_var); @@ -3099,7 +3099,7 @@ impl FilterRules { alias_to_cube_var: &'static str, members_var: &'static str, filter_aliases_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let column_var = var!(column_var); let alias_to_cube_var = var!(alias_to_cube_var); let members_var = var!(members_var); @@ -3136,7 +3136,7 @@ impl FilterRules { output_op_var: &'static str, literal_var: &'static str, new_literal_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let op_var = var!(op_var); let output_op_var = var!(output_op_var); let literal_var = var!(literal_var); @@ -3194,7 +3194,7 @@ impl FilterRules { member_var: &'static str, values_var: &'static str, filter_aliases_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let year_var = var!(year_var); let column_var = var!(column_var); let alias_to_cube_var = var!(alias_to_cube_var); @@ -3271,7 +3271,7 @@ impl FilterRules { members_var: &'static str, segment_member_var: &'static str, filter_aliases_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let column_var = column_var.parse().unwrap(); let op_var = op_var.parse().unwrap(); let literal_var = literal_var.parse().unwrap(); @@ -3347,7 +3347,7 @@ impl FilterRules { members_var: &'static str, filter_aliases_var: &'static str, change_user_member_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let column_var = var!(column_var); let literal_var = var!(literal_var); let alias_to_cube_var = var!(alias_to_cube_var); @@ -3406,7 +3406,7 @@ impl FilterRules { alias_to_cube_var: &'static str, members_var: &'static str, filter_aliases_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let column_var = var!(column_var); let alias_to_cube_var = var!(alias_to_cube_var); let members_var = var!(members_var); @@ -3445,7 +3445,7 @@ impl FilterRules { &self, negated_var: &'static str, op_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let negated_var = var!(negated_var); let op_var = var!(op_var); @@ -3479,7 +3479,7 @@ impl FilterRules { filter_op_var: &'static str, filter_values_var: &'static str, filter_aliases_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let column_var = var!(column_var); let list_var = var!(list_var); let negated_var = var!(negated_var); @@ -3607,7 +3607,7 @@ impl FilterRules { filter_values_var: &'static str, filter_aliases_var: &'static str, is_null_op: bool, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let column_var = var!(column_var); let alias_to_cube_var = var!(alias_to_cube_var); let members_var = var!(members_var); @@ -3670,7 +3670,7 @@ impl FilterRules { &self, negated_var: &'static str, new_negated_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let negated_var = var!(negated_var); let new_negated_var = var!(new_negated_var); move |egraph, subst| { @@ -3693,7 +3693,7 @@ impl FilterRules { &self, negated_var: &'static str, new_negated_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let negated_var = var!(negated_var); let new_negated_var = var!(new_negated_var); move |egraph, subst| { @@ -3713,7 +3713,7 @@ impl FilterRules { } fn filter_member_name( - egraph: &mut EGraph, + egraph: &mut CubeEGraph, subst: &Subst, meta_context: &Arc, alias_to_cube_var: Var, @@ -3734,7 +3734,7 @@ impl FilterRules { } fn filter_member_name_with_granularity( - egraph: &mut EGraph, + egraph: &mut CubeEGraph, subst: &Subst, meta_context: &Arc, alias_to_cube_var: Var, @@ -3822,7 +3822,7 @@ impl FilterRules { filter_op_var: &'static str, filter_values_var: &'static str, filter_aliases_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let column_var = var!(column_var); let negated_var = var!(negated_var); let low_var = var!(low_var); @@ -3914,7 +3914,7 @@ impl FilterRules { members_var: &'static str, filter_aliases_var: &'static str, is_negated: bool, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let column_var = var!(column_var); let negated_var = var!(negated_var); let alias_to_cube_var = var!(alias_to_cube_var); @@ -3957,7 +3957,7 @@ impl FilterRules { &self, granularity_var: &'static str, interval_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let granularity_var = var!(granularity_var); let interval_var = var!(interval_var); move |egraph, subst| { @@ -4029,7 +4029,7 @@ impl FilterRules { new_op_var: &'static str, date_add_interval_var: &'static str, date_sub_interval_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let granularity_var = var!(granularity_var); let op_var = var!(op_var); let new_op_var = var!(new_op_var); @@ -4091,7 +4091,7 @@ impl FilterRules { date_var: &'static str, start_date_var: &'static str, end_date_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let granularity_var = var!(granularity_var); let date_var = var!(date_var); let start_date_var = var!(start_date_var); @@ -4142,7 +4142,7 @@ impl FilterRules { fn is_empty_filter_ops_filters( &self, filter_ops_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let filter_ops_var = var!(filter_ops_var); move |egraph, subst| { if let Some(true) = egraph[subst[filter_ops_var]].data.is_empty_list.clone() { @@ -4160,7 +4160,7 @@ impl FilterRules { date_range_var: &'static str, date_range_start_op_var: &'static str, date_range_end_op_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let date_range_start_var = date_range_start_var.parse().unwrap(); let date_range_end_var = date_range_end_var.parse().unwrap(); let date_range_var = date_range_var.parse().unwrap(); @@ -4269,7 +4269,7 @@ impl FilterRules { granularity_var: &'static str, date_range_var: &'static str, expr_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let members_var = var!(members_var); let time_dimension_member_var = var!(time_dimension_member_var); let time_dimension_date_range_var = var!(time_dimension_date_range_var); @@ -4345,7 +4345,7 @@ impl FilterRules { &self, members_var: &'static str, time_dimension_member_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let members_var = var!(members_var); let time_dimension_member_var = var!(time_dimension_member_var); move |egraph, subst| { @@ -4380,7 +4380,7 @@ impl FilterRules { time_dimension_member_var: &'static str, time_dimension_date_range_var: &'static str, output_date_range_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let member_var = var!(member_var); let date_range_var = var!(date_range_var); let time_dimension_member_var = var!(time_dimension_member_var); @@ -4426,7 +4426,7 @@ impl FilterRules { &self, granularity_var: &'static str, target_granularity: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let granularity_var = var!(granularity_var); move |egraph, subst| { for granularity in var_iter!(egraph[subst[granularity_var]], LiteralExprValue) { @@ -4457,7 +4457,7 @@ impl FilterRules { filter_member_var: &'static str, filter_op_var: &'static str, filter_values_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let negated_var = var!(negated_var); let column_var = var!(column_var); let literal_var = var!(literal_var); @@ -4560,7 +4560,7 @@ impl FilterRules { expr_var: &'static str, data_type_var: &'static str, negative: bool, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let expr_var = var!(expr_var); let data_type_var = var!(data_type_var); move |egraph, subst| { @@ -4590,7 +4590,7 @@ impl FilterRules { &self, op_var: &'static str, literal_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let op_var = var!(op_var); let literal_var = var!(literal_var); move |egraph, subst| { @@ -4619,7 +4619,7 @@ impl FilterRules { &self, literal_var: &'static str, one_day_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let literal_var = var!(literal_var); let one_day_var = var!(one_day_var); move |egraph, subst| { @@ -4651,7 +4651,7 @@ impl FilterRules { members_var: &'static str, filter_aliases_var: &'static str, new_filter_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let granularity_var = var!(granularity_var); let column_var = var!(column_var); let list_var = var!(list_var); @@ -4911,7 +4911,7 @@ impl FilterRules { pull_up_member_var: &'static str, left_out_var: &'static str, right_out_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let time_dimension_filter_var = var!(time_dimension_filter_var); let time_dimension_member_var = var!(time_dimension_member_var); let time_dimension_op_var = var!(time_dimension_op_var); @@ -4992,9 +4992,7 @@ impl FilterRules { } } -fn filter_simplify_push_down( - node_type: impl Display, -) -> Rewrite { +fn filter_simplify_push_down(node_type: impl Display) -> CubeRewrite { rewrite( &format!("filter-simplify-{}-push-down", node_type), filter_simplify_replacer(format!("({} ?left ?right)", node_type)), @@ -5007,9 +5005,7 @@ fn filter_simplify_push_down( ) } -fn filter_simplify_push_down_tail( - node_type: impl Display, -) -> Rewrite { +fn filter_simplify_push_down_tail(node_type: impl Display) -> CubeRewrite { rewrite( &format!("filter-simplify-{}-empty-tail-push-down", node_type), filter_simplify_replacer(node_type.to_string()), diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/column.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/column.rs index 3f3ffc717af48..d67712c17e661 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/column.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/column.rs @@ -1,16 +1,18 @@ use crate::{ compile::rewrite::{ - alias_expr, analysis::LogicalPlanAnalysis, column_expr, - converter::LogicalPlanToLanguageConverter, flatten_pushdown_replacer, literal_expr, - rewrite, rules::flatten::FlattenRules, transforming_rewrite, AliasExprAlias, - ColumnExprColumn, FlattenPushdownReplacerTopLevel, LogicalPlanLanguage, + alias_expr, column_expr, + converter::LogicalPlanToLanguageConverter, + flatten_pushdown_replacer, literal_expr, rewrite, + rewriter::{CubeEGraph, CubeRewrite}, + rules::flatten::FlattenRules, + transforming_rewrite, AliasExprAlias, ColumnExprColumn, FlattenPushdownReplacerTopLevel, + LogicalPlanLanguage, }, var, var_iter, CubeError, }; -use egg::Rewrite; impl FlattenRules { - pub fn column_rules(&self, rules: &mut Vec>) { + pub fn column_rules(&self, rules: &mut Vec) { rules.extend(vec![ transforming_rewrite( "flatten-column-pushdown", @@ -51,8 +53,7 @@ impl FlattenRules { _inner_alias_var: &str, column_alias_var: &str, out_expr_var: &str, - ) -> impl Fn(&mut egg::EGraph, &mut egg::Subst) -> bool - { + ) -> impl Fn(&mut CubeEGraph, &mut egg::Subst) -> bool { let column_var = var!(column_var); let top_level_var = var!(top_level_var); let inner_expr_var = var!(inner_expr_var); diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/mod.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/mod.rs index a59794824ab72..a547ec12e8024 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/mod.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/mod.rs @@ -3,12 +3,9 @@ mod pass_through; mod top_level; use crate::{ - compile::rewrite::{ - analysis::LogicalPlanAnalysis, rewriter::RewriteRules, LogicalPlanLanguage, - }, + compile::rewrite::rewriter::{CubeRewrite, RewriteRules}, config::ConfigObj, }; -use egg::Rewrite; use std::sync::Arc; pub struct FlattenRules { @@ -16,7 +13,7 @@ pub struct FlattenRules { } impl RewriteRules for FlattenRules { - fn rewrite_rules(&self) -> Vec> { + fn rewrite_rules(&self) -> Vec { let mut rules = vec![]; self.top_level_rules(&mut rules); diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/pass_through.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/pass_through.rs index 26e560a1a3350..8018106243603 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/pass_through.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/pass_through.rs @@ -1,15 +1,11 @@ use crate::compile::rewrite::{ - agg_fun_expr, alias_expr, analysis::LogicalPlanAnalysis, binary_expr, cast_expr, - flatten_pushdown_replacer, fun_expr_var_arg, is_not_null_expr, is_null_expr, rewrite, - rules::flatten::FlattenRules, udf_expr_var_arg, LogicalPlanLanguage, + agg_fun_expr, alias_expr, binary_expr, cast_expr, flatten_pushdown_replacer, fun_expr_var_arg, + is_not_null_expr, is_null_expr, rewrite, rewriter::CubeRewrite, rules::flatten::FlattenRules, + udf_expr_var_arg, }; -use egg::Rewrite; impl FlattenRules { - pub fn pass_through_rules( - &self, - rules: &mut Vec>, - ) { + pub fn pass_through_rules(&self, rules: &mut Vec) { self.single_arg_pass_through_rules("alias", |expr| alias_expr(expr, "?alias"), rules); self.single_arg_pass_through_rules("cast", |expr| cast_expr(expr, "?data_type"), rules); self.single_arg_pass_through_rules( @@ -59,7 +55,7 @@ impl FlattenRules { &self, name: &str, node: impl Fn(String) -> String, - rules: &mut Vec>, + rules: &mut Vec, ) { rules.extend(vec![rewrite( &format!("flatten-{}-push-down", name), diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/top_level.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/top_level.rs index db7521d60479d..2951663ace473 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/top_level.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/flatten/top_level.rs @@ -1,21 +1,17 @@ use crate::{ compile::rewrite::{ - aggregate, - analysis::LogicalPlanAnalysis, - cube_scan, flatten_pushdown_replacer, projection, + aggregate, cube_scan, flatten_pushdown_replacer, projection, + rewriter::{CubeEGraph, CubeRewrite}, rules::{flatten::FlattenRules, replacer_flat_push_down_node, replacer_push_down_node}, transforming_chain_rewrite_with_root, FlattenPushdownReplacerInnerAlias, ListType, LogicalPlanLanguage, ProjectionAlias, }, var, var_iter, }; -use egg::{Id, Rewrite}; +use egg::Id; impl FlattenRules { - pub fn top_level_rules( - &self, - rules: &mut Vec>, - ) { + pub fn top_level_rules(&self, rules: &mut Vec) { // TODO use root instead for performance rules.extend(vec![transforming_chain_rewrite_with_root( "flatten-projection-pushdown", @@ -193,8 +189,7 @@ impl FlattenRules { outer_projection_alias_var: &'static str, new_projection_alias_var: &'static str, inner_alias_var: &'static str, - ) -> impl Fn(&mut egg::EGraph, Id, &mut egg::Subst) -> bool - { + ) -> impl Fn(&mut CubeEGraph, Id, &mut egg::Subst) -> bool { let inner_projection_var = var!(inner_projection_var); let cube_scan_var = var!(cube_scan_var); let members_var = var!(members_var); @@ -256,8 +251,7 @@ impl FlattenRules { outer_aggregate_expr_var: &'static str, inner_projection_alias_var: &'static str, inner_alias_var: &'static str, - ) -> impl Fn(&mut egg::EGraph, Id, &mut egg::Subst) -> bool - { + ) -> impl Fn(&mut CubeEGraph, Id, &mut egg::Subst) -> bool { let inner_projection_var = var!(inner_projection_var); let cube_scan_var = var!(cube_scan_var); let members_var = var!(members_var); @@ -306,11 +300,7 @@ impl FlattenRules { } } - fn list_pushdown_rules( - name: &str, - list_node: &str, - rules: &mut Vec>, - ) { + fn list_pushdown_rules(name: &str, list_node: &str, rules: &mut Vec) { rules.extend(replacer_push_down_node( name, list_node, @@ -319,11 +309,7 @@ impl FlattenRules { )); } - fn flat_list_pushdown_rules( - name: &str, - list_type: ListType, - rules: &mut Vec>, - ) { + fn flat_list_pushdown_rules(name: &str, list_type: ListType, rules: &mut Vec) { rules.extend(replacer_flat_push_down_node( name, list_type, diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/members.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/members.rs index 5ddcc8a3d68b1..8ff4f85807546 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/members.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/members.rs @@ -1,16 +1,14 @@ use crate::{ compile::rewrite::{ agg_fun_expr, aggregate, alias_expr, all_members, - analysis::{ - ConstantFolding, LogicalPlanAnalysis, LogicalPlanData, MemberNamesToExpr, OriginalExpr, - }, + analysis::{ConstantFolding, LogicalPlanData, MemberNamesToExpr, OriginalExpr}, binary_expr, cast_expr, change_user_expr, column_expr, cross_join, cube_scan, cube_scan_filters_empty_tail, cube_scan_members, cube_scan_members_empty_tail, cube_scan_order_empty_tail, dimension_expr, expr_column_name, fun_expr, join, like_expr, limit, list_concat_pushdown_replacer, list_concat_pushup_replacer, literal_expr, literal_member, measure_expr, member_pushdown_replacer, member_replacer, merged_members_replacer, original_expr_name, projection, referenced_columns, rewrite, - rewriter::RewriteRules, + rewriter::{CubeEGraph, CubeRewrite, RewriteRules}, rules::{ replacer_flat_push_down_node_substitute_rules, replacer_push_down_node, replacer_push_down_node_substitute_rules, utils, @@ -40,7 +38,7 @@ use datafusion::{ physical_plan::aggregates::AggregateFunction, scalar::ScalarValue, }; -use egg::{EGraph, Id, Rewrite, Subst, Var}; +use egg::{Id, Subst, Var}; use itertools::{EitherOrBoth, Itertools}; use std::{ collections::{HashMap, HashSet}, @@ -56,7 +54,7 @@ pub struct MemberRules { } impl RewriteRules for MemberRules { - fn rewrite_rules(&self) -> Vec> { + fn rewrite_rules(&self) -> Vec { let mut rules = vec![ transforming_rewrite( "cube-scan", @@ -442,7 +440,7 @@ impl MemberRules { name: &str, member_fn: impl Fn(&str) -> String, relation: Option<&'static str>, - ) -> Vec> { + ) -> Vec { vec![ transforming_rewrite( &format!("member-pushdown-replacer-column-{}", name), @@ -477,7 +475,7 @@ impl MemberRules { ] } - fn member_pushdown_rules(&self) -> Vec> { + fn member_pushdown_rules(&self) -> Vec { let mut rules = Vec::new(); let member_replacer_fn = |members| { member_pushdown_replacer( @@ -931,10 +929,7 @@ impl MemberRules { ), )); - fn list_concat_terminal( - name: &str, - member_fn: String, - ) -> Rewrite { + fn list_concat_terminal(name: &str, member_fn: String) -> CubeRewrite { rewrite( &format!("list-concat-terminal-{}", name), list_concat_pushdown_replacer(member_fn.to_string()), @@ -1001,7 +996,7 @@ impl MemberRules { } pub fn measure_rewrites( - rules: &mut Vec>, + rules: &mut Vec, pushdown_measure_rewrite: impl Fn( &'static str, String, @@ -1010,7 +1005,7 @@ impl MemberRules { Option<&'static str>, Option<&'static str>, Option<&'static str>, - ) -> Rewrite, + ) -> CubeRewrite, ) { rules.push(pushdown_measure_rewrite( "member-pushdown-replacer-agg-fun", @@ -1101,7 +1096,7 @@ impl MemberRules { left_var: &'static str, right_var: &'static str, concat_output_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let left_var = var!(left_var); let right_var = var!(right_var); let concat_output_var = var!(concat_output_var); @@ -1141,7 +1136,7 @@ impl MemberRules { table_name_var: &'static str, alias_to_cube_var: &'static str, cube_scan_members_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let source_table_name_var = var!(source_table_name_var); let table_name_var = var!(table_name_var); let alias_to_cube_var = var!(alias_to_cube_var); @@ -1205,8 +1200,7 @@ impl MemberRules { column_expr_var: &'static str, alias_expr_var: Option<&'static str>, inner_replacer: bool, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool + Clone - { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool + Clone { let original_expr_var = var!(original_expr_var); let outer_granularity_var = var!(outer_granularity_var); let inner_granularity_var = var!(inner_granularity_var); @@ -1300,8 +1294,7 @@ impl MemberRules { column_expr_var: &'static str, alias_expr_var: Option<&'static str>, inner_replacer: bool, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool + Clone - { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool + Clone { Self::transform_original_expr_nested_date_trunc( original_expr_var, granularity_var, @@ -1321,7 +1314,7 @@ impl MemberRules { alias_to_cube_var: &'static str, new_alias_to_cube_var: &'static str, member_pushdown_replacer_alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let projection_expr_var = var!(projection_expr_var); let members_var = var!(members_var); let alias_var = var!(alias_var); @@ -1455,7 +1448,7 @@ impl MemberRules { new_pushdown_join_var: &'static str, ungrouped_var: &'static str, filters_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> 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 aggregate_expr_var = var!(aggregate_expr_var); @@ -1565,7 +1558,7 @@ impl MemberRules { fetch_var: &'static str, new_skip_var: &'static str, new_fetch_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let skip_var = var!(skip_var); let fetch_var = var!(fetch_var); let new_skip_var = var!(new_skip_var); @@ -1613,7 +1606,7 @@ impl MemberRules { literal_member_value_var: &'static str, alias_to_cube_var: &'static str, relation_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let literal_value_var = var!(literal_value_var); let literal_member_value_var = var!(literal_member_value_var); let alias_to_cube_var = var!(alias_to_cube_var); @@ -1666,7 +1659,7 @@ impl MemberRules { date_range_var: &'static str, original_expr_var: &'static str, alias_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let cube_var = cube_var.parse().unwrap(); let dimension_var = dimension_var.parse().unwrap(); let time_dimension_name_var = time_dimension_name_var.parse().unwrap(); @@ -1741,7 +1734,7 @@ impl MemberRules { } pub fn add_alias_column( - egraph: &mut EGraph, + egraph: &mut CubeEGraph, alias: String, cube_alias: Option, ) -> Id { @@ -1762,7 +1755,7 @@ impl MemberRules { distinct_var: Option<&'static str>, fun_var: Option<&'static str>, cast_data_type_var: Option<&'static str>, - ) -> Rewrite { + ) -> CubeRewrite { transforming_chain_rewrite( &format!("measure-{}", name), member_replacer("?aggr_expr", "?alias_to_cube", "?aliases"), @@ -1788,7 +1781,7 @@ impl MemberRules { terminal_member: &'static str, filtered_member_pushdown_replacer_alias_to_cube_var: &'static str, default_count: bool, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let member_pushdown_replacer_alias_to_cube_var = var!(member_pushdown_replacer_alias_to_cube_var); let column_var = var!(column_var); @@ -1867,7 +1860,7 @@ impl MemberRules { column_var: &'static str, member_var: &'static str, default_count: bool, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let cube_var = var!(cube_var); let member_pushdown_replacer_alias_to_cube_var = var!(member_pushdown_replacer_alias_to_cube_var); @@ -1990,7 +1983,7 @@ impl MemberRules { original_expr_var: &'static str, cast_data_type_var: Option<&'static str>, measure_out_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let member_pushdown_replacer_alias_to_cube_var = var!(member_pushdown_replacer_alias_to_cube_var); let measure_name_var = var!(measure_name_var); @@ -2106,7 +2099,7 @@ impl MemberRules { cast_data_type_var: Option<&'static str>, aggr_expr_var: &'static str, measure_out_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let distinct_var = distinct_var.map(|var| var.parse().unwrap()); let fun_var = fun_var.map(|var| var.parse().unwrap()); @@ -2212,7 +2205,7 @@ impl MemberRules { } fn measure_output( - egraph: &mut EGraph, + egraph: &mut CubeEGraph, subst: &mut Subst, measure: &V1CubeMetaMeasure, call_agg_type: Option, @@ -2270,7 +2263,7 @@ impl MemberRules { column_var: &'static str, output_column_var: &'static str, relation_var: Option<&'static str>, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let member_pushdown_replacer_alias_to_cube_var = var!(member_pushdown_replacer_alias_to_cube_var); let column_var = var!(column_var); @@ -2314,7 +2307,7 @@ impl MemberRules { new_alias_var: &'static str, new_relation_var: &'static str, is_alias: bool, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let column_var = var!(column_var); let new_alias_var = var!(new_alias_var); @@ -2373,7 +2366,7 @@ impl MemberRules { alias_var: &'static str, output_column_var: &'static str, relation_var: Option<&'static str>, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let member_pushdown_replacer_alias_to_cube_var = var!(member_pushdown_replacer_alias_to_cube_var); let alias_var = var!(alias_var); @@ -2415,7 +2408,7 @@ impl MemberRules { negated_var: &'static str, escape_char_var: &'static str, op_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let like_type_var = var!(like_type_var); let negated_var = var!(negated_var); let escape_char_var = var!(escape_char_var); @@ -2481,7 +2474,7 @@ impl MemberRules { left_alias_to_cube_var: &'static str, right_alias_to_cube_var: &'static str, joined_alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let left_alias_to_cube_var = var!(left_alias_to_cube_var); let right_alias_to_cube_var = var!(right_alias_to_cube_var); let joined_alias_to_cube_var = var!(joined_alias_to_cube_var); @@ -2530,7 +2523,7 @@ impl MemberRules { left_ungrouped_var: &'static str, right_ungrouped_var: &'static str, new_ungrouped_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let left_alias_to_cube_var = var!(left_alias_to_cube_var); let right_alias_to_cube_var = var!(right_alias_to_cube_var); let joined_alias_to_cube_var = var!(joined_alias_to_cube_var); @@ -2676,7 +2669,7 @@ impl MemberRules { right_on_var: &'static str, left_aliases_var: &'static str, right_aliases_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let left_on_var = var!(left_on_var); let right_on_var = var!(right_on_var); let left_aliases_var = var!(left_aliases_var); @@ -2763,7 +2756,7 @@ impl MemberRules { right_members_expr: String, left_members: &'static str, right_members: &'static str, - ) -> Rewrite { + ) -> CubeRewrite { transforming_rewrite( &format!("push-down-cross-join-to-cube-scan-{}", name), cross_join( @@ -2829,7 +2822,7 @@ impl MemberRules { } pub fn add_member_error( - egraph: &mut EGraph, + egraph: &mut CubeEGraph, member_error: String, priority: usize, expr: Id, diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/mod.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/mod.rs index b6bf20863aefa..373e4ba67fe91 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/mod.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/mod.rs @@ -1,9 +1,7 @@ use crate::compile::rewrite::{ - analysis::LogicalPlanAnalysis, list_rewrite, list_rewrite_with_lists, - list_rewrite_with_lists_and_vars, rewrite, ListApplierListPattern, ListPattern, ListType, - LogicalPlanLanguage, + list_rewrite, list_rewrite_with_lists, list_rewrite_with_lists_and_vars, rewrite, + rewriter::CubeRewrite, ListApplierListPattern, ListPattern, ListType, }; -use egg::Rewrite; pub mod case; pub mod common; @@ -22,7 +20,7 @@ pub fn replacer_push_down_node( list_node: &str, replacer_node: impl Fn(String) -> String, include_tail: bool, -) -> Vec> { +) -> Vec { let push_down_rule = rewrite( &format!("{}-push-down", name), replacer_node(format!("({} ?left ?right)", list_node)), @@ -52,7 +50,7 @@ pub fn replacer_flat_push_down_node( list_type: ListType, replacer_node: impl Fn(String) -> String, include_tail: bool, -) -> Vec> { +) -> Vec { let push_down_rule = list_rewrite( &format!("{}-push-down", name), list_type.clone(), @@ -86,7 +84,7 @@ pub fn replacer_pull_up_node( list_node: &str, substitute_list_node: &str, replacer_node: impl Fn(String) -> String, -) -> Vec> { +) -> Vec { let pull_up_rule = rewrite( &format!("{}-pull-up", name), format!( @@ -106,7 +104,7 @@ pub fn replacer_flat_pull_up_node( substitute_list_type: ListType, replacer_node: impl Fn(String) -> String, top_level_elem_vars: &[&str], -) -> Vec> { +) -> Vec { let pull_up_rule = list_rewrite_with_lists_and_vars( &format!("{}-pull-up", name), list_type, @@ -131,7 +129,7 @@ pub fn replacer_push_down_node_substitute_rules( list_node: &str, substitute_node: &str, replacer_node: impl Fn(String) -> String, -) -> Vec> { +) -> Vec { vec![ rewrite( &format!("{}-push-down", name), @@ -156,7 +154,7 @@ pub fn replacer_flat_push_down_node_substitute_rules( list_type: ListType, substitute_type: ListType, replacer_node: impl Fn(String) -> String, -) -> Vec> { +) -> Vec { vec![ list_rewrite_with_lists( &format!("{}-push-down", name), diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/old_split.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/old_split.rs index 2dcb93b0f93bb..b79d5325f8467 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/old_split.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/old_split.rs @@ -4,14 +4,14 @@ use crate::{ agg_fun_expr, aggr_aggr_expr_empty_tail, aggr_aggr_expr_legacy as aggr_aggr_expr, aggr_group_expr_empty_tail, aggr_group_expr_legacy as aggr_group_expr, aggregate, alias_expr, - analysis::{ConstantFolding, LogicalPlanAnalysis, OriginalExpr}, + analysis::{ConstantFolding, OriginalExpr}, binary_expr, cast_expr, cast_expr_explicit, column_expr, cube_scan, event_notification, fun_expr, group_aggregate_split_replacer, group_expr_split_replacer, inner_aggregate_split_replacer, is_not_null_expr, is_null_expr, literal_expr, literal_float, literal_int, literal_string, original_expr_name, outer_aggregate_split_replacer, outer_projection_split_replacer, projection, projection_expr_empty_tail, projection_expr_legacy as projection_expr, rewrite, - rewriter::RewriteRules, + rewriter::{CubeEGraph, CubeRewrite, RewriteRules}, rules::members::MemberRules, transforming_chain_rewrite, transforming_rewrite, udf_expr, AggregateFunctionExprDistinct, AggregateFunctionExprFun, AliasExprAlias, BinaryExprOp, CastExprDataType, ColumnExprColumn, @@ -30,7 +30,7 @@ use datafusion::{ physical_plan::{aggregates::AggregateFunction, functions::BuiltinScalarFunction}, scalar::ScalarValue, }; -use egg::{EGraph, Id, Rewrite, Subst, Var}; +use egg::{Id, Subst, Var}; use std::{fmt::Display, ops::Index, sync::Arc}; pub struct OldSplitRules { @@ -39,7 +39,7 @@ pub struct OldSplitRules { } impl RewriteRules for OldSplitRules { - fn rewrite_rules(&self) -> Vec> { + fn rewrite_rules(&self) -> Vec { let mut rules = vec![ transforming_rewrite( "split-projection-aggregate", @@ -4667,16 +4667,12 @@ impl OldSplitRules { is_measure: bool, is_dimension: bool, unwrap_agg_chain: Option>, - ) -> Vec> + ) -> Vec where M: Fn(fn(D, DD) -> String) -> String, C: Fn(fn(D, DD) -> String) -> Vec<(&'a str, String)>, A: Fn(fn(D, DD) -> String) -> String, - T: Fn(&mut EGraph, &mut Subst) -> bool - + Sync - + Send - + Clone - + 'static, + T: Fn(&mut CubeEGraph, &mut Subst) -> bool + Sync + Send + Clone + 'static, { let mut rules = vec![transforming_chain_rewrite( &format!("{}-outer-aggr", base_name), @@ -4827,7 +4823,7 @@ impl OldSplitRules { original_expr_var: &'static str, out_alias_expr_var: &'static str, out_column_expr_var: Option<&'static str>, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { self.transform_original_expr_to_alias_and_column_with_chain_transform( original_expr_var, out_alias_expr_var, @@ -4842,9 +4838,9 @@ impl OldSplitRules { out_alias_expr_var: &'static str, out_column_expr_var: Option<&'static str>, chain_transform_fn: T, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool where - T: Fn(&mut EGraph, &mut Subst) -> bool, + T: Fn(&mut CubeEGraph, &mut Subst) -> bool, { let original_expr_var = var!(original_expr_var); let out_alias_expr_var = var!(out_alias_expr_var); @@ -4895,16 +4891,12 @@ impl OldSplitRules { pub fn transform_original_expr_alias( &self, - alias_to_cube_fn: fn( - &EGraph, - Id, - ) -> Vec>, + alias_to_cube_fn: fn(&CubeEGraph, Id) -> Vec>, original_expr_var: &'static str, column_var: &'static str, alias_to_cube_var: &'static str, alias_expr_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool + Clone - { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool + Clone { let original_expr_var = var!(original_expr_var); let column_var = var!(column_var); let alias_to_cube_var = var!(alias_to_cube_var); @@ -4959,7 +4951,7 @@ impl OldSplitRules { left_column_var: &'static str, right_column_var: &'static str, alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let left_column_var = var!(left_column_var); let right_column_var = var!(right_column_var); let alias_to_cube_var = var!(alias_to_cube_var); @@ -5005,7 +4997,7 @@ impl OldSplitRules { right_granularity_var: &'static str, out_granularity_var: &'static str, is_max: bool, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let left_granularity_var = var!(left_granularity_var); let right_granularity_var = var!(right_granularity_var); let out_granularity_var = var!(out_granularity_var); @@ -5043,7 +5035,7 @@ impl OldSplitRules { column_var: &'static str, alias_var: &'static str, is_time_dimension: bool, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let cube_expr_var = var!(cube_expr_var); let fun_expr_var = var!(fun_expr_var); let arg_expr_var = var!(arg_expr_var); @@ -5099,7 +5091,7 @@ impl OldSplitRules { fun_var: Option<&'static str>, distinct_var: Option<&'static str>, out_expr_var: Option<&'static str>, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let cube_expr_var = var!(cube_expr_var); let column_var = column_var.map(|column_var| var!(column_var)); let aggr_expr_var = aggr_expr_var.map(|v| var!(v)); @@ -5235,7 +5227,7 @@ impl OldSplitRules { &self, alias_to_cube_var: &'static str, column_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let column_var = var!(column_var); let meta = self.meta_context.clone(); @@ -5259,7 +5251,7 @@ impl OldSplitRules { fn transform_inner_measure_missing_count( &self, cube_expr_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let cube_expr_var = var!(cube_expr_var); let meta = self.meta_context.clone(); move |egraph, subst| { @@ -5290,9 +5282,9 @@ impl OldSplitRules { alias_to_cube_var: &'static str, column_var: Option<&'static str>, original_transform_fn: T, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool where - T: Fn(&mut EGraph, &mut Subst) -> bool, + T: Fn(&mut CubeEGraph, &mut Subst) -> bool, { let alias_to_cube_var = var!(alias_to_cube_var); let column_var = column_var.map(|column_var| var!(column_var)); @@ -5326,9 +5318,9 @@ impl OldSplitRules { alias_to_cube_var: &'static str, column_var: Option<&'static str>, original_transform_fn: T, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool where - T: Fn(&mut EGraph, &mut Subst) -> bool, + T: Fn(&mut CubeEGraph, &mut Subst) -> bool, { let alias_to_cube_var = var!(alias_to_cube_var); let column_var = column_var.map(|column_var| var!(column_var)); @@ -5372,9 +5364,9 @@ impl OldSplitRules { output_fun_var: Option<&'static str>, distinct_var: Option<&'static str>, original_transform_fn: T, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool where - T: Fn(&mut EGraph, &mut Subst) -> bool, + T: Fn(&mut CubeEGraph, &mut Subst) -> bool, { let alias_to_cube_var = var!(alias_to_cube_var); let column_var = column_var.map(|column_var| var!(column_var)); @@ -5436,9 +5428,9 @@ impl OldSplitRules { alias_to_cube_var: &'static str, column_var: Option<&'static str>, original_transform_fn: T, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool where - T: Fn(&mut EGraph, &mut Subst) -> bool, + T: Fn(&mut CubeEGraph, &mut Subst) -> bool, { let alias_to_cube_var = var!(alias_to_cube_var); let column_var = column_var.map(|column_var| var!(column_var)); @@ -5480,7 +5472,7 @@ impl OldSplitRules { group_aggregate_cube_var: &'static str, new_expr_var: &'static str, inner_projection_alias_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let projection_expr_var = var!(projection_expr_var); let alias_to_cube_var = var!(alias_to_cube_var); let inner_aggregate_cube_var = var!(inner_aggregate_cube_var); @@ -5615,7 +5607,7 @@ impl OldSplitRules { inner_aggregate_cube_expr_var: &'static str, outer_projection_cube_expr_var: &'static str, projection_alias_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let projection_expr_var = projection_expr_var.map(|v| var!(v)); let aggr_expr_var = aggr_expr_var.map(|v| var!(v)); let group_expr_var = group_expr_var.map(|v| var!(v)); @@ -5672,7 +5664,7 @@ impl OldSplitRules { &self, granularity_var: &'static str, out_granularity_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let granularity_var = var!(granularity_var); let out_granularity_var = var!(out_granularity_var); @@ -5702,7 +5694,7 @@ impl OldSplitRules { binary_op_var: &'static str, literal_expr_var: &'static str, is_outer_projection: bool, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let binary_op_var = var!(binary_op_var); let literal_expr_var = var!(literal_expr_var); @@ -5800,7 +5792,7 @@ impl OldSplitRules { alias_to_cube_var: &'static str, inner_aggregate_cube_expr_var: &'static str, outer_aggregate_cube_expr_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let aggr_expr_var = var!(aggr_expr_var); let group_expr_var = var!(group_expr_var); @@ -5844,7 +5836,7 @@ impl OldSplitRules { &self, alias_to_cube_var: &'static str, column_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let column_var = var!(column_var); let meta = self.meta_context.clone(); @@ -5878,7 +5870,7 @@ impl OldSplitRules { distinct_var: &'static str, allow_count_distinct: bool, output_distinct_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let cube_var = var!(cube_var); let original_expr_var = var!(original_expr_var); let fun_expr_var = var!(fun_expr_var); @@ -5985,7 +5977,7 @@ impl OldSplitRules { &self, expr_var: &'static str, alias_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let expr_var = var!(expr_var); let alias_var = var!(alias_var); move |egraph, subst| { @@ -6010,7 +6002,7 @@ impl OldSplitRules { alias_expr_var: &'static str, outer_alias_expr_var: &'static str, meta_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let cube_var = var!(cube_var); let original_expr_var = var!(original_expr_var); let arg_var = var!(arg_var); @@ -6089,7 +6081,7 @@ impl OldSplitRules { &self, cube_var: &'static str, fun_expr_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let cube_var = var!(cube_var); let fun_expr_var = var!(fun_expr_var); let meta = self.meta_context.clone(); @@ -6126,7 +6118,7 @@ impl OldSplitRules { original_expr_var: &'static str, column_var: Option<&'static str>, alias_expr_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let cube_var = var!(cube_var); let original_expr_var = var!(original_expr_var); let column_var = column_var.map(|v| var!(v)); @@ -6173,7 +6165,7 @@ impl OldSplitRules { &self, fun_expr_var: &'static str, expr_val: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let fun_expr_var = var!(fun_expr_var); let expr_val = var!(expr_val); move |egraph, subst| { @@ -6199,7 +6191,7 @@ impl OldSplitRules { &self, expr_val: &'static str, alias_val: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_val = var!(alias_val); let expr_val = var!(expr_val); move |egraph, subst| { @@ -6232,7 +6224,7 @@ impl OldSplitRules { literal_strings: Vec<&'static str>, metas: Vec<&'static str>, keys: Vec<&'static str>, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let literal_strings = literal_strings .into_iter() .map(|l| var!(l)) @@ -6307,7 +6299,7 @@ impl OldSplitRules { &self, column_var: &'static str, meta_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let column_var = var!(column_var); let meta_var = var!(meta_var); move |egraph, subst| { @@ -6336,7 +6328,7 @@ impl OldSplitRules { inner_expr_var: &'static str, data_type_var: &'static str, new_expr_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let expr_var = var!(expr_var); let alias_to_cube_var = var!(alias_to_cube_var); let inner_expr_var = var!(inner_expr_var); @@ -6443,7 +6435,7 @@ impl OldSplitRules { data_type_var: &'static str, new_expr_var: &'static str, is_outer_projection: bool, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let expr_var = var!(expr_var); let alias_to_cube_var = var!(alias_to_cube_var); let inner_expr_var = var!(inner_expr_var); @@ -6539,7 +6531,7 @@ impl OldSplitRules { &self, interval_var: &'static str, granularity: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let interval_var = var!(interval_var); move |egraph, subst| { if let Some(expected_interval) = utils::granularity_str_to_interval(granularity) { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/order.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/order.rs index b5d891efb0203..bccc2cfbba892 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/order.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/order.rs @@ -1,21 +1,21 @@ use crate::{ compile::rewrite::{ - analysis::{LogicalPlanAnalysis, OriginalExpr}, + analysis::OriginalExpr, column_name_to_member_vec, cube_scan, cube_scan_order, cube_scan_order_empty_tail, expr_column_name, order, order_replacer, referenced_columns, rewrite, - rewriter::RewriteRules, + rewriter::{CubeEGraph, CubeRewrite, RewriteRules}, sort, sort_exp, sort_exp_empty_tail, sort_expr, transforming_rewrite, LogicalPlanLanguage, OrderAsc, OrderMember, OrderReplacerColumnNameToMember, SortExprAsc, }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; use std::ops::{Index, IndexMut}; pub struct OrderRules {} impl RewriteRules for OrderRules { - fn rewrite_rules(&self) -> Vec> { + fn rewrite_rules(&self) -> Vec { vec![ transforming_rewrite( "push-down-sort", @@ -82,7 +82,7 @@ impl OrderRules { sort_exp_var: &'static str, members_var: &'static str, aliases_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let sort_exp_var = var!(sort_exp_var); let members_var = var!(members_var); let aliases_var = var!(aliases_var); @@ -129,7 +129,7 @@ impl OrderRules { column_name_to_member_var: &'static str, order_member_var: &'static str, order_asc_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let expr_var = expr_var.parse().unwrap(); let asc_var = asc_var.parse().unwrap(); let column_name_to_member_var = column_name_to_member_var.parse().unwrap(); diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/aggregate_function.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/aggregate_function.rs index ca01488d6e278..844cc610ae1d5 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/aggregate_function.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/aggregate_function.rs @@ -1,8 +1,9 @@ use crate::{ compile::rewrite::{ agg_fun_expr, alias_expr, - analysis::{ConstantFolding, LogicalPlanAnalysis}, + analysis::ConstantFolding, case_expr, cast_expr, column_expr, is_null_expr, literal_expr, literal_int, + rewriter::{CubeEGraph, CubeRewrite}, rules::{members::MemberRules, split::SplitRules}, AggregateFunctionExprDistinct, AggregateFunctionExprFun, AggregateSplitPushDownReplacerAliasToCube, ColumnExprColumn, LogicalPlanLanguage, @@ -12,13 +13,9 @@ use crate::{ var, var_iter, }; use datafusion::{logical_plan::Column, physical_plan::aggregates::AggregateFunction}; -use egg::Rewrite; impl SplitRules { - pub fn aggregate_function_rules( - &self, - rules: &mut Vec>, - ) { + pub fn aggregate_function_rules(&self, rules: &mut Vec) { self.single_arg_split_point_rules_aggregate_function( "aggregate-function", || agg_fun_expr("?fun_name", vec![column_expr("?column")], "?distinct"), @@ -211,25 +208,17 @@ impl SplitRules { inner_rule: impl Fn() -> String + Clone, outer_aggr_rule: impl Fn(String) -> String, outer_proj_rule: impl Fn(String) -> String, - transform_fn_aggr: impl Fn( - bool, - &mut egg::EGraph, - &mut egg::Subst, - ) -> bool + transform_fn_aggr: impl Fn(bool, &mut CubeEGraph, &mut egg::Subst) -> bool + Sync + Send + Clone + 'static, - transform_fn_proj: impl Fn( - bool, - &mut egg::EGraph, - &mut egg::Subst, - ) -> bool + transform_fn_proj: impl Fn(bool, &mut CubeEGraph, &mut egg::Subst) -> bool + Sync + Send + Clone + 'static, - rules: &mut Vec>, + rules: &mut Vec, ) { self.single_arg_split_point_rules_aggregate( name, @@ -257,15 +246,8 @@ impl SplitRules { output_fun_var: Option<&str>, alias_to_cube_var: &str, should_match_agg_fun: bool, - ) -> impl Fn( - bool, - &mut egg::EGraph, - &mut egg::Subst, - ) -> bool - + Sync - + Send - + Clone - + 'static { + ) -> impl Fn(bool, &mut CubeEGraph, &mut egg::Subst) -> bool + Sync + Send + Clone + 'static + { let fun_name_var = fun_name_var.map(|fun_name_var| var!(fun_name_var)); let column_var = column_var.map(|column_var| var!(column_var)); let distinct_var = distinct_var.map(|distinct_var| var!(distinct_var)); @@ -367,15 +349,8 @@ impl SplitRules { fun_name_var: &str, distinct_var: &str, constant_var: &str, - ) -> impl Fn( - bool, - &mut egg::EGraph, - &mut egg::Subst, - ) -> bool - + Sync - + Send - + Clone - + 'static { + ) -> impl Fn(bool, &mut CubeEGraph, &mut egg::Subst) -> bool + Sync + Send + Clone + 'static + { let fun_name_var = var!(fun_name_var); let distinct_var = var!(distinct_var); let constant_var = var!(constant_var); @@ -407,15 +382,8 @@ impl SplitRules { &self, column_var: &str, alias_to_cube_var: &str, - ) -> impl Fn( - bool, - &mut egg::EGraph, - &mut egg::Subst, - ) -> bool - + Sync - + Send - + Clone - + 'static { + ) -> impl Fn(bool, &mut CubeEGraph, &mut egg::Subst) -> bool + Sync + Send + Clone + 'static + { let column_var = var!(column_var); let alias_to_cube_var = var!(alias_to_cube_var); let meta_context = self.meta_context.clone(); diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/alias.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/alias.rs index 88738cc0789cd..95bf493338874 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/alias.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/alias.rs @@ -1,12 +1,11 @@ use crate::compile::rewrite::{ aggregate_split_pullup_replacer, aggregate_split_pushdown_replacer, alias_expr, - analysis::LogicalPlanAnalysis, projection_split_pullup_replacer, - projection_split_pushdown_replacer, rewrite, rules::split::SplitRules, LogicalPlanLanguage, + projection_split_pullup_replacer, projection_split_pushdown_replacer, rewrite, + rewriter::CubeRewrite, rules::split::SplitRules, }; -use egg::Rewrite; impl SplitRules { - pub fn alias_rules(&self, rules: &mut Vec>) { + pub fn alias_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "split-alias-push-down-aggregate", diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/binary_expr.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/binary_expr.rs index fc861c764a5bf..f55b3cebe56d0 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/binary_expr.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/binary_expr.rs @@ -1,15 +1,10 @@ use crate::compile::rewrite::{ - aggregate_split_pullup_replacer, aggregate_split_pushdown_replacer, - analysis::LogicalPlanAnalysis, binary_expr, rewrite, rules::split::SplitRules, - LogicalPlanLanguage, + aggregate_split_pullup_replacer, aggregate_split_pushdown_replacer, binary_expr, rewrite, + rewriter::CubeRewrite, rules::split::SplitRules, }; -use egg::Rewrite; impl SplitRules { - pub fn binary_expr_rules( - &self, - rules: &mut Vec>, - ) { + pub fn binary_expr_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "split-binary-push-down-aggregate", diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/case.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/case.rs index 28e09b632491a..b19451cb83ba8 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/case.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/case.rs @@ -1,12 +1,10 @@ use crate::compile::rewrite::{ - aggregate_split_pullup_replacer, aggregate_split_pushdown_replacer, - analysis::LogicalPlanAnalysis, case_expr_var_arg, rewrite, rules::split::SplitRules, - LogicalPlanLanguage, + aggregate_split_pullup_replacer, aggregate_split_pushdown_replacer, case_expr_var_arg, rewrite, + rewriter::CubeRewrite, rules::split::SplitRules, }; -use egg::Rewrite; impl SplitRules { - pub fn case_rules(&self, rules: &mut Vec>) { + pub fn case_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "split-case-push-down-aggregate", diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/cast.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/cast.rs index 22f7fc2364af6..a057349dad2bb 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/cast.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/cast.rs @@ -1,8 +1,9 @@ use crate::{ compile::rewrite::{ aggregate_split_pullup_replacer, aggregate_split_pushdown_replacer, - analysis::{LogicalPlanAnalysis, OriginalExpr}, + analysis::OriginalExpr, cast_expr, projection_split_pullup_replacer, projection_split_pushdown_replacer, rewrite, + rewriter::{CubeEGraph, CubeRewrite}, rules::split::SplitRules, transforming_rewrite, AliasExprAlias, CastExprDataType, LiteralExprValue, LogicalPlanLanguage, ScalarFunctionExprFun, @@ -15,10 +16,10 @@ use datafusion::{ physical_plan::functions::BuiltinScalarFunction, scalar::ScalarValue, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl SplitRules { - pub fn cast_rules(&self, rules: &mut Vec>) { + pub fn cast_rules(&self, rules: &mut Vec) { rules.extend([ transforming_rewrite( "split-cast-push-down-aggregate", @@ -90,7 +91,7 @@ impl SplitRules { expr_var: &'static str, data_type_var: &'static str, new_expr_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let expr_var = var!(expr_var); let data_type_var = var!(data_type_var); let new_expr_var = var!(new_expr_var); diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/column.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/column.rs index 1ff054217ee1c..bff16ed07122e 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/column.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/column.rs @@ -1,12 +1,11 @@ use crate::compile::rewrite::{ - aggregate_split_pullup_replacer, aggregate_split_pushdown_replacer, - analysis::LogicalPlanAnalysis, column_expr, projection_split_pullup_replacer, - projection_split_pushdown_replacer, rewrite, rules::split::SplitRules, LogicalPlanLanguage, + aggregate_split_pullup_replacer, aggregate_split_pushdown_replacer, column_expr, + projection_split_pullup_replacer, projection_split_pushdown_replacer, rewrite, + rewriter::CubeRewrite, rules::split::SplitRules, }; -use egg::Rewrite; impl SplitRules { - pub fn column_rules(&self, rules: &mut Vec>) { + pub fn column_rules(&self, rules: &mut Vec) { // TODO check for measures? rules.push(rewrite( "split-column-point-aggregate", diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/dates.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/dates.rs index d1e1077971688..ee536dd294849 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/dates.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/dates.rs @@ -1,7 +1,8 @@ use crate::{ compile::rewrite::{ - analysis::{ConstantFolding, LogicalPlanAnalysis}, + analysis::ConstantFolding, cast_expr, literal_expr, + rewriter::{CubeEGraph, CubeRewrite}, rules::{ members::min_granularity, split::SplitRules, @@ -12,10 +13,9 @@ use crate::{ var, }; use datafusion::scalar::ScalarValue; -use egg::Rewrite; impl SplitRules { - pub fn date_rules(&self, rules: &mut Vec>) { + pub fn date_rules(&self, rules: &mut Vec) { // TODO check for time dimension before push down to optimize performance // TODO use pass-through instead point rules for epoch self.single_arg_split_point_rules( @@ -149,14 +149,7 @@ impl SplitRules { &self, date_part_var: &str, new_date_part_var: &str, - ) -> impl Fn( - bool, - &mut egg::EGraph, - &mut egg::Subst, - ) -> bool - + Sync - + Send - + Clone { + ) -> impl Fn(bool, &mut CubeEGraph, &mut egg::Subst) -> bool + Sync + Send + Clone { let date_part_var = var!(date_part_var); let new_date_part_var = var!(new_date_part_var); move |_, egraph, subst| { @@ -193,14 +186,7 @@ impl SplitRules { date_part_var: &str, new_date_part_var: &str, new_trunc_granularity_var: &str, - ) -> impl Fn( - bool, - &mut egg::EGraph, - &mut egg::Subst, - ) -> bool - + Sync - + Send - + Clone { + ) -> impl Fn(bool, &mut CubeEGraph, &mut egg::Subst) -> bool + Sync + Send + Clone { let date_part_var = var!(date_part_var); let new_date_part_var = var!(new_date_part_var); let new_trunc_granularity_var = var!(new_trunc_granularity_var); @@ -242,14 +228,7 @@ impl SplitRules { inner_var: &str, new_outer_var: &str, new_inner_var: &str, - ) -> impl Fn( - bool, - &mut egg::EGraph, - &mut egg::Subst, - ) -> bool - + Sync - + Send - + Clone { + ) -> impl Fn(bool, &mut CubeEGraph, &mut egg::Subst) -> bool + Sync + Send + Clone { let outer_var = var!(outer_var); let inner_var = var!(inner_var); let new_outer_var = var!(new_outer_var); diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/functions.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/functions.rs index 5cb631b9b2d57..6d09247433b97 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/functions.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/functions.rs @@ -1,14 +1,10 @@ use crate::compile::rewrite::{ - analysis::LogicalPlanAnalysis, is_not_null_expr, is_null_expr, literal_expr, negative_expr, - rules::split::SplitRules, udf_expr, ListType, LogicalPlanLanguage, + is_not_null_expr, is_null_expr, literal_expr, negative_expr, rewriter::CubeRewrite, + rules::split::SplitRules, udf_expr, ListType, }; -use egg::Rewrite; impl SplitRules { - pub fn functions_rules( - &self, - rules: &mut Vec>, - ) { + pub fn functions_rules(&self, rules: &mut Vec) { // Universal rule to traverse any number of function arguments // PushDown(ScalarFunctionExprArgs(..., arg, ...)) => ScalarFunctionExprArgs(..., PushDown(arg), ...) // ScalarFunctionExprArgs(..., PullUp(arg), ...) => PullUp(ScalarFunctionExprArgs(..., arg, ...)) diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/granularity.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/granularity.rs index ef3e22fcc0f37..805e13b73b86c 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/granularity.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/granularity.rs @@ -1,22 +1,19 @@ use crate::{ compile::rewrite::{ alias_expr, - analysis::{ConstantFolding, LogicalPlanAnalysis}, + analysis::ConstantFolding, binary_expr, cast_expr, cast_expr_explicit, column_expr, literal_expr, literal_float, literal_int, literal_string, + rewriter::{CubeEGraph, CubeRewrite}, rules::split::SplitRules, - udf_expr, LogicalPlanLanguage, + udf_expr, }, var, }; use datafusion::{arrow::datatypes::DataType as ArrowDataType, scalar::ScalarValue}; -use egg::Rewrite; impl SplitRules { - pub fn granularity_rules( - &self, - rules: &mut Vec>, - ) { + pub fn granularity_rules(&self, rules: &mut Vec) { // CAST(CAST(((((EXTRACT(YEAR FROM "ta_1"."LO_COMMITDATE") * 100) + 1) * 100) + 1) AS varchar) AS date) self.single_arg_split_point_rules( "thoughtspot-year", @@ -666,15 +663,8 @@ impl SplitRules { &self, day_interval_var: &str, neg_day_interval_var: &str, - ) -> impl Fn( - bool, - &mut egg::EGraph, - &mut egg::Subst, - ) -> bool - + Sync - + Send - + Clone - + 'static { + ) -> impl Fn(bool, &mut CubeEGraph, &mut egg::Subst) -> bool + Sync + Send + Clone + 'static + { let day_interval = var!(day_interval_var); let neg_day_interval = var!(neg_day_interval_var); move |_, egraph, subst| { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/mod.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/mod.rs index 94070ad08c9e1..2bbb2c7d1371a 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/mod.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/mod.rs @@ -11,11 +11,10 @@ pub mod top_level; use crate::{ compile::rewrite::{ - aggregate_split_pullup_replacer, aggregate_split_pushdown_replacer, alias_expr, - analysis::LogicalPlanAnalysis, - fun_expr, fun_expr_var_arg, list_rewrite_with_lists_and_vars, original_expr_name, + aggregate_split_pullup_replacer, aggregate_split_pushdown_replacer, alias_expr, fun_expr, + fun_expr_var_arg, list_rewrite_with_lists_and_vars, original_expr_name, projection_split_pullup_replacer, projection_split_pushdown_replacer, rewrite, - rewriter::RewriteRules, + rewriter::{CubeEGraph, CubeRewrite, RewriteRules}, rules::{members::MemberRules, replacer_flat_push_down_node, replacer_push_down_node}, transforming_chain_rewrite, AliasExprAlias, ListApplierListPattern, ListPattern, ListType, LogicalPlanLanguage, @@ -24,7 +23,6 @@ use crate::{ transport::MetaContext, var, }; -use egg::Rewrite; use std::{fmt::Display, sync::Arc}; pub struct SplitRules { @@ -34,7 +32,7 @@ pub struct SplitRules { } impl RewriteRules for SplitRules { - fn rewrite_rules(&self) -> Vec> { + fn rewrite_rules(&self) -> Vec { let mut rules = Vec::new(); self.top_level_rules(&mut rules); @@ -69,7 +67,7 @@ impl SplitRules { name: &str, node: impl Fn(String) -> String, projection_rules: bool, - rules: &mut Vec>, + rules: &mut Vec, ) { rules.extend(vec![ rewrite( @@ -140,7 +138,7 @@ impl SplitRules { &self, fun_name: &(impl Display + ?Sized), projection_rules: bool, - rules: &mut Vec>, + rules: &mut Vec, ) { rules.extend(vec![ rewrite( @@ -217,17 +215,13 @@ impl SplitRules { match_rule: impl Fn() -> String + Clone, inner_rule: impl Fn() -> String + Clone, outer_rule: impl Fn(String) -> String + Clone, - transform_fn: impl Fn( - bool, - &mut egg::EGraph, - &mut egg::Subst, - ) -> bool + transform_fn: impl Fn(bool, &mut CubeEGraph, &mut egg::Subst) -> bool + Sync + Send + Clone + 'static, projection_rules: bool, - rules: &mut Vec>, + rules: &mut Vec, ) { if projection_rules { self.single_arg_split_point_rules_projection( @@ -255,16 +249,12 @@ impl SplitRules { match_rule: impl Fn() -> String, inner_rule: impl Fn() -> String, outer_rule: impl Fn(String) -> String, - transform_fn: impl Fn( - bool, - &mut egg::EGraph, - &mut egg::Subst, - ) -> bool + transform_fn: impl Fn(bool, &mut CubeEGraph, &mut egg::Subst) -> bool + Sync + Send + Clone + 'static, - rules: &mut Vec>, + rules: &mut Vec, ) { rules.push(transforming_chain_rewrite( &format!("split-{}-point-aggregate", name), @@ -295,16 +285,12 @@ impl SplitRules { match_rule: impl Fn() -> String, inner_rule: impl Fn() -> String, outer_rule: impl Fn(String) -> String, - transform_fn: impl Fn( - bool, - &mut egg::EGraph, - &mut egg::Subst, - ) -> bool + transform_fn: impl Fn(bool, &mut CubeEGraph, &mut egg::Subst) -> bool + Sync + Send + Clone + 'static, - rules: &mut Vec>, + rules: &mut Vec, ) { rules.push(transforming_chain_rewrite( &format!("split-{}-point-projection", name), @@ -335,18 +321,8 @@ impl SplitRules { inner_alias_var: &str, outer_alias_column_var: &str, is_projection: bool, - transform_fn: impl Fn( - bool, - &mut egg::EGraph, - &mut egg::Subst, - ) -> bool - + Clone - + Send - + Sync, - ) -> impl Fn(&mut egg::EGraph, &mut egg::Subst) -> bool - + Sync - + Send - + Clone { + transform_fn: impl Fn(bool, &mut CubeEGraph, &mut egg::Subst) -> bool + Clone + Send + Sync, + ) -> impl Fn(&mut CubeEGraph, &mut egg::Subst) -> bool + Sync + Send + Clone { let match_expr_var = var!(match_expr_var); let inner_alias_var = var!(inner_alias_var); let outer_alias_column_var = var!(outer_alias_column_var); @@ -371,11 +347,7 @@ impl SplitRules { } } - fn list_pushdown_pullup_rules( - name: &str, - list_node: &str, - rules: &mut Vec>, - ) { + fn list_pushdown_pullup_rules(name: &str, list_node: &str, rules: &mut Vec) { let possible_inner_list_nodes = Self::possible_inner_list_nodes(); // Aggregate split replacer @@ -450,7 +422,7 @@ impl SplitRules { fn flat_list_pushdown_pullup_rules( name: &str, list_type: ListType, - rules: &mut Vec>, + rules: &mut Vec, ) { let possible_inner_list_types = Self::possible_inner_flat_list_types(); @@ -547,7 +519,7 @@ impl SplitRules { substitute_list_node: &str, replacer_node: impl Fn(String, String, String) -> String, possible_inner_list_nodes: &Vec, - ) -> Vec> { + ) -> Vec { possible_inner_list_nodes .iter() .map(|inner_list_node| { @@ -584,7 +556,7 @@ impl SplitRules { replacer_node: impl Fn(String, String, String) -> String, possible_inner_list_types: &Vec, top_level_elem_vars: &[&str], - ) -> Vec> { + ) -> Vec { possible_inner_list_types .iter() .map(|inner_list_type| { @@ -630,7 +602,7 @@ impl SplitRules { push_down_replacer_node: impl Fn(String, String) -> String, pull_up_replacer_node: impl Fn(String, String, String) -> String, possible_inner_list_nodes: &Vec, - ) -> Vec> { + ) -> Vec { possible_inner_list_nodes .iter() .map(|inner_list_node| { @@ -654,7 +626,7 @@ impl SplitRules { push_down_replacer_node: impl Fn(String, String) -> String, pull_up_replacer_node: impl Fn(String, String, String) -> String, possible_inner_list_types: &Vec, - ) -> Vec> { + ) -> Vec { possible_inner_list_types .iter() .map(|inner_list_type| { diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/top_level.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/top_level.rs index 2d79599509f9a..a040c6dc21009 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/split/top_level.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/split/top_level.rs @@ -1,22 +1,20 @@ use crate::{ compile::rewrite::{ aggr_aggr_expr_empty_tail, aggr_group_expr_empty_tail, aggregate, - aggregate_split_pullup_replacer, aggregate_split_pushdown_replacer, - analysis::LogicalPlanAnalysis, cube_scan, projection, projection_expr_empty_tail, - projection_split_pullup_replacer, projection_split_pushdown_replacer, rewrite, - rules::split::SplitRules, transforming_rewrite, AggregateSplitPushDownReplacerAliasToCube, - CubeScanAliasToCube, ListType, LogicalPlanLanguage, ProjectionAlias, - ProjectionSplitPushDownReplacerAliasToCube, + aggregate_split_pullup_replacer, aggregate_split_pushdown_replacer, cube_scan, projection, + projection_expr_empty_tail, projection_split_pullup_replacer, + projection_split_pushdown_replacer, rewrite, + rewriter::{CubeEGraph, CubeRewrite}, + rules::split::SplitRules, + transforming_rewrite, AggregateSplitPushDownReplacerAliasToCube, CubeScanAliasToCube, + ListType, LogicalPlanLanguage, ProjectionAlias, ProjectionSplitPushDownReplacerAliasToCube, }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl SplitRules { - pub fn top_level_rules( - &self, - rules: &mut Vec>, - ) { + pub fn top_level_rules(&self, rules: &mut Vec) { rules.push(transforming_rewrite( "split-projection-aggregate", aggregate( @@ -348,7 +346,7 @@ impl SplitRules { group_expr_var: Option<&str>, aggr_expr_var: Option<&str>, projection_expr_var: Option<&str>, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let split_alias_to_cube_var = var!(split_alias_to_cube_var); let group_expr_var = group_expr_var.map(|group_expr_var| var!(group_expr_var)); @@ -411,7 +409,7 @@ impl SplitRules { split_alias_to_cube_var: &str, group_expr_var: &str, aggr_expr_var: &str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let split_alias_to_cube_var = var!(split_alias_to_cube_var); let group_expr_var = var!(group_expr_var); @@ -452,7 +450,7 @@ impl SplitRules { fn transform_projection_projection_ungrouped_pull_up( &self, projection_alias_var: &str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let projection_alias_var = var!(projection_alias_var); move |egraph, subst| { subst.insert( @@ -470,7 +468,7 @@ impl SplitRules { outer_group_expr_var: &str, outer_aggr_expr_var: &str, projection_expr_var: &str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let projection_alias_var = var!(projection_alias_var); let outer_group_expr_var = var!(outer_group_expr_var); let outer_aggr_expr_var = var!(outer_aggr_expr_var); diff --git a/rust/cubesql/cubesql/src/compile/rewrite/rules/utils.rs b/rust/cubesql/cubesql/src/compile/rewrite/rules/utils.rs index 72fd8233b8d3a..a941bc7cdc6ca 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/utils.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/utils.rs @@ -12,12 +12,10 @@ use datafusion::{ physical_plan::aggregates::AggregateFunction, scalar::ScalarValue, }; -use egg::{EGraph, Id}; +use egg::Id; use crate::{ - compile::rewrite::{ - analysis::LogicalPlanAnalysis, BinaryExprOp, LiteralExprValue, LogicalPlanLanguage, - }, + compile::rewrite::{rewriter::CubeEGraph, BinaryExprOp, LiteralExprValue, LogicalPlanLanguage}, transport::SqlTemplates, CubeError, }; @@ -524,11 +522,8 @@ impl DecomposedDayTime { } } - pub fn add_decomposed_to_egraph( - self, - egraph: &mut EGraph, - ) -> Id { - let add_literal = |egraph: &mut EGraph<_, _>, scalar| { + pub fn add_decomposed_to_egraph(self, egraph: &mut CubeEGraph) -> Id { + let add_literal = |egraph: &mut CubeEGraph, scalar| { let id = egraph.add(LogicalPlanLanguage::LiteralExprValue(LiteralExprValue( scalar, ))); @@ -662,17 +657,14 @@ impl DecomposedMonthDayNano { } } - pub fn add_decomposed_to_egraph( - self, - egraph: &mut EGraph, - ) -> Id { - let add_literal = |egraph: &mut EGraph<_, _>, scalar| { + pub fn add_decomposed_to_egraph(self, egraph: &mut CubeEGraph) -> Id { + let add_literal = |egraph: &mut CubeEGraph, scalar| { let id = egraph.add(LogicalPlanLanguage::LiteralExprValue(LiteralExprValue( scalar, ))); egraph.add(LogicalPlanLanguage::LiteralExpr([id])) }; - let add_binary = |egraph: &mut EGraph<_, _>, l, r| { + let add_binary = |egraph: &mut CubeEGraph, l, r| { let op = Operator::Plus; let op = egraph.add(LogicalPlanLanguage::BinaryExprOp(BinaryExprOp(op))); let left = add_literal(egraph, l); 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 c0cc602f25686..dea6d2b1a9564 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/aggregate.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/aggregate.rs @@ -1,8 +1,9 @@ use crate::{ compile::rewrite::{ aggregate, - analysis::{LogicalPlanAnalysis, LogicalPlanData}, + analysis::LogicalPlanData, cube_scan_wrapper, grouping_set_expr, original_expr_name, rewrite, + rewriter::{CubeEGraph, CubeRewrite}, rules::{members::MemberRules, wrapper::WrapperRules}, subquery, transforming_chain_rewrite, transforming_rewrite, wrapped_select, wrapped_select_filter_expr_empty_tail, wrapped_select_having_expr_empty_tail, @@ -17,14 +18,11 @@ use crate::{ var, var_iter, }; use datafusion::logical_plan::Column; -use egg::{EGraph, Rewrite, Subst, Var}; +use egg::{Subst, Var}; use std::ops::IndexMut; impl WrapperRules { - pub fn aggregate_rules( - &self, - rules: &mut Vec>, - ) { + pub fn aggregate_rules(&self, rules: &mut Vec) { rules.extend(vec![ transforming_rewrite( "wrapper-push-down-aggregate-to-cube-scan", @@ -246,10 +244,7 @@ impl WrapperRules { } } - pub fn aggregate_rules_subquery( - &self, - rules: &mut Vec>, - ) { + pub fn aggregate_rules_subquery(&self, rules: &mut Vec) { rules.extend(vec![transforming_rewrite( "wrapper-push-down-aggregate-and-subquery-to-cube-scan", aggregate( @@ -357,7 +352,7 @@ impl WrapperRules { aggr_expr_var: &'static str, ungrouped_var: &'static str, select_ungrouped_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let group_expr_var = var!(group_expr_var); let aggr_expr_var = var!(aggr_expr_var); let ungrouped_var = var!(ungrouped_var); @@ -381,7 +376,7 @@ impl WrapperRules { aggr_expr_var: &'static str, ungrouped_var: &'static str, select_ungrouped_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> 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); @@ -410,7 +405,7 @@ impl WrapperRules { } fn transform_aggregate_impl( - egraph: &mut EGraph, + egraph: &mut CubeEGraph, subst: &mut Subst, group_expr_var: Var, aggr_expr_var: Var, @@ -440,7 +435,7 @@ impl WrapperRules { fn check_rollup_allowed( &self, alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let meta = self.meta_context.clone(); move |egraph, subst| { @@ -474,7 +469,7 @@ impl WrapperRules { _cast_data_type_var: Option<&'static str>, cube_members_var: &'static str, measure_out_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let original_expr_var = var!(original_expr_var); let column_var = column_var.map(|v| var!(v)); let fun_name_var = fun_name_var.map(|v| var!(v)); 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 3d5280f017912..b398f77221d94 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 @@ -1,6 +1,8 @@ use crate::{ compile::rewrite::{ - agg_fun_expr, analysis::LogicalPlanAnalysis, rewrite, rules::wrapper::WrapperRules, + agg_fun_expr, rewrite, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, AggregateFunctionExprDistinct, AggregateFunctionExprFun, LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, @@ -8,13 +10,10 @@ use crate::{ var, var_iter, }; use datafusion::physical_plan::aggregates::AggregateFunction; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn window_function_rules( - &self, - rules: &mut Vec>, - ) { + pub fn window_function_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "wrapper-push-down-aggregate-function", @@ -67,7 +66,7 @@ impl WrapperRules { fun_var: &'static str, distinct_var: &'static str, alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let fun_var = var!(fun_var); let distinct_var = var!(distinct_var); let alias_to_cube_var = var!(alias_to_cube_var); 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 a3354764fb463..61914129452af 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/alias.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/alias.rs @@ -1,11 +1,10 @@ use crate::compile::rewrite::{ - alias_expr, analysis::LogicalPlanAnalysis, rewrite, rules::wrapper::WrapperRules, - wrapper_pullup_replacer, wrapper_pushdown_replacer, LogicalPlanLanguage, + alias_expr, rewrite, rewriter::CubeRewrite, rules::wrapper::WrapperRules, + wrapper_pullup_replacer, wrapper_pushdown_replacer, }; -use egg::Rewrite; impl WrapperRules { - pub fn alias_rules(&self, rules: &mut Vec>) { + pub fn alias_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "wrapper-push-down-alias", 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 a11687459e591..d39c897fa76aa 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 @@ -1,18 +1,17 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, binary_expr, rewrite, rules::wrapper::WrapperRules, + binary_expr, rewrite, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn binary_expr_rules( - &self, - rules: &mut Vec>, - ) { + pub fn binary_expr_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "wrapper-push-down-binary-expr", @@ -76,7 +75,7 @@ impl WrapperRules { &self, _operator_var: &'static str, alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); // let operator_var = var!(operator_var); let meta = self.meta_context.clone(); 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 2ad45eb7561e9..4ffa6de10d6dc 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/case.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/case.rs @@ -1,15 +1,17 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, case_expr_var_arg, rewrite, rules::wrapper::WrapperRules, + case_expr_var_arg, rewrite, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn case_rules(&self, rules: &mut Vec>) { + pub fn case_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "wrapper-push-down-case", @@ -94,7 +96,7 @@ impl WrapperRules { fn transform_case_expr( &self, alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let meta = self.meta_context.clone(); move |egraph, subst| { 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 ffb8493ebc622..8aabbe4e4f944 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/cast.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/cast.rs @@ -1,11 +1,10 @@ use crate::compile::rewrite::{ - analysis::LogicalPlanAnalysis, cast_expr, rewrite, rules::wrapper::WrapperRules, - wrapper_pullup_replacer, wrapper_pushdown_replacer, LogicalPlanLanguage, + cast_expr, rewrite, rewriter::CubeRewrite, rules::wrapper::WrapperRules, + wrapper_pullup_replacer, wrapper_pushdown_replacer, }; -use egg::Rewrite; impl WrapperRules { - pub fn cast_rules(&self, rules: &mut Vec>) { + pub fn cast_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "wrapper-push-down-cast", 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 159c690d5170f..822cf1e9fc510 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/column.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/column.rs @@ -1,17 +1,18 @@ use crate::{ compile::rewrite::{ - analysis::{LogicalPlanAnalysis, Member}, + analysis::Member, column_expr, rewrite, + rewriter::{CubeEGraph, CubeRewrite}, rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, ColumnExprColumn, LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn column_rules(&self, rules: &mut Vec>) { + pub fn column_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "wrapper-push-down-column", @@ -78,7 +79,7 @@ impl WrapperRules { column_name_var: &'static str, members_var: &'static str, dimension_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> 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); @@ -143,7 +144,7 @@ impl WrapperRules { &self, column_name_var: &'static str, members_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let column_name_var = var!(column_name_var); let members_var = var!(members_var); let meta = self.meta_context.clone(); 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 20b13b9cb4492..b439f71f191fe 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 @@ -1,19 +1,17 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, cube_scan, cube_scan_wrapper, rewrite, - rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, - CubeScanAliasToCube, CubeScanUngrouped, LogicalPlanLanguage, - WrapperPullupReplacerAliasToCube, WrapperPullupReplacerUngrouped, + cube_scan, cube_scan_wrapper, rewrite, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, + transforming_rewrite, wrapper_pullup_replacer, CubeScanAliasToCube, CubeScanUngrouped, + LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, WrapperPullupReplacerUngrouped, }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn cube_scan_wrapper_rules( - &self, - rules: &mut Vec>, - ) { + pub fn cube_scan_wrapper_rules(&self, rules: &mut Vec) { rules.extend(vec![ transforming_rewrite( "wrapper-cube-scan-wrap", @@ -82,7 +80,7 @@ impl WrapperRules { ungrouped_cube_var: &'static str, alias_to_cube_var_out: &'static str, ungrouped_cube_var_out: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let members_var = var!(members_var); let alias_to_cube_var = var!(alias_to_cube_var); let ungrouped_cube_var = var!(ungrouped_cube_var); 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 7e5e3caf575ed..2dc8707697014 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/distinct.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/distinct.rs @@ -1,14 +1,10 @@ use crate::compile::rewrite::{ - analysis::LogicalPlanAnalysis, cube_scan_wrapper, distinct, rewrite, - rules::wrapper::WrapperRules, wrapped_select, wrapper_pullup_replacer, LogicalPlanLanguage, + cube_scan_wrapper, distinct, rewrite, rewriter::CubeRewrite, rules::wrapper::WrapperRules, + wrapped_select, wrapper_pullup_replacer, }; -use egg::Rewrite; impl WrapperRules { - pub fn distinct_rules( - &self, - rules: &mut Vec>, - ) { + pub fn distinct_rules(&self, rules: &mut Vec) { rules.extend(vec![rewrite( "wrapper-push-down-distinct-to-cube-scan", distinct(cube_scan_wrapper( 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 e70a95894bade..c1d3072cd13f1 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/extract.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/extract.rs @@ -1,18 +1,17 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, literal_expr, rules::wrapper::WrapperRules, + literal_expr, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn extract_rules( - &self, - rules: &mut Vec>, - ) { + pub fn extract_rules(&self, rules: &mut Vec) { rules.extend(vec![transforming_rewrite( "wrapper-pull-up-extract", self.fun_expr( @@ -51,7 +50,7 @@ impl WrapperRules { fn transform_date_part_expr( &self, alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let meta = self.meta_context.clone(); move |egraph, subst| { 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 00e8e69a42ea0..2af376fadfdcc 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/filter.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/filter.rs @@ -1,6 +1,8 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, cube_scan_wrapper, filter, rules::wrapper::WrapperRules, + cube_scan_wrapper, filter, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, subquery, transforming_rewrite, wrapped_select, wrapped_select_aggr_expr_empty_tail, wrapped_select_filter_expr, wrapped_select_filter_expr_empty_tail, wrapped_select_group_expr_empty_tail, wrapped_select_having_expr_empty_tail, @@ -12,10 +14,10 @@ use crate::{ }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst, Var}; +use egg::{Subst, Var}; impl WrapperRules { - pub fn filter_rules(&self, rules: &mut Vec>) { + pub fn filter_rules(&self, rules: &mut Vec) { // TODO respect having filter for push down to wrapped select // rules.extend(vec![rewrite( // "wrapper-push-down-filter-to-wrapped-select", @@ -215,10 +217,7 @@ impl WrapperRules { ); } - pub fn filter_rules_subquery( - &self, - rules: &mut Vec>, - ) { + pub fn filter_rules_subquery(&self, rules: &mut Vec) { rules.extend(vec![transforming_rewrite( "wrapper-push-down-filter-and-subquery-to-cube-scan", filter( @@ -331,7 +330,7 @@ impl WrapperRules { ungrouped_var: &'static str, select_ungrouped_var: &'static str, select_ungrouped_scan_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let ungrouped_var = var!(ungrouped_var); let select_ungrouped_var = var!(select_ungrouped_var); let select_ungrouped_scan_var = var!(select_ungrouped_scan_var); @@ -352,7 +351,7 @@ impl WrapperRules { ungrouped_var: &'static str, select_ungrouped_var: &'static str, select_ungrouped_scan_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let ungrouped_var = var!(ungrouped_var); let select_ungrouped_var = var!(select_ungrouped_var); @@ -379,7 +378,7 @@ impl WrapperRules { } fn transform_filter_impl( - egraph: &mut EGraph, + egraph: &mut CubeEGraph, subst: &mut Subst, ungrouped_var: Var, select_ungrouped_var: Var, 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 e4981bda96a99..01a9c42957789 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 @@ -1,18 +1,17 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, inlist_expr, rewrite, rules::wrapper::WrapperRules, + inlist_expr, rewrite, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn in_list_expr_rules( - &self, - rules: &mut Vec>, - ) { + pub fn in_list_expr_rules(&self, rules: &mut Vec) { rules.extend(vec![ transforming_rewrite( "wrapper-in-list-only-consts-push-down", @@ -106,7 +105,7 @@ impl WrapperRules { fn transform_in_list_expr( &self, alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let meta = self.meta_context.clone(); move |egraph, subst| { @@ -133,9 +132,9 @@ impl WrapperRules { fn transform_in_list_only_consts( &self, list_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let list_var = var!(list_var); - move |egraph: &mut EGraph<_, LogicalPlanAnalysis>, subst| { + move |egraph: &mut CubeEGraph, subst| { 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 6f966de1f8e5a..5c577c4706d3b 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,14 +1,10 @@ use crate::compile::rewrite::{ - analysis::LogicalPlanAnalysis, insubquery_expr, rewrite, rules::wrapper::WrapperRules, - wrapper_pullup_replacer, wrapper_pushdown_replacer, LogicalPlanLanguage, + insubquery_expr, rewrite, rewriter::CubeRewrite, rules::wrapper::WrapperRules, + wrapper_pullup_replacer, wrapper_pushdown_replacer, }; -use egg::Rewrite; impl WrapperRules { - pub fn in_subquery_expr_rules( - &self, - rules: &mut Vec>, - ) { + pub fn in_subquery_expr_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "wrapper-in-subquery-push-down", 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 bfa77149f1489..6c84953ddc211 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 @@ -1,18 +1,17 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, is_not_null_expr, is_null_expr, rewrite, - rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, - wrapper_pushdown_replacer, LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, + is_not_null_expr, is_null_expr, rewrite, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, + transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, + LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn is_null_expr_rules( - &self, - rules: &mut Vec>, - ) { + pub fn is_null_expr_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "wrapper-push-down-is-null-expr", @@ -90,7 +89,7 @@ impl WrapperRules { fn transform_is_null_expr( &self, alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let meta = self.meta_context.clone(); move |egraph, subst| { 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 64f89af867070..41b38376b8f2f 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 @@ -1,19 +1,18 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, like_expr, rewrite, rules::wrapper::WrapperRules, + like_expr, rewrite, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, LikeExprEscapeChar, LikeExprLikeType, LikeType, LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn like_expr_rules( - &self, - rules: &mut Vec>, - ) { + pub fn like_expr_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "wrapper-push-down-like-expr", @@ -94,7 +93,7 @@ impl WrapperRules { alias_to_cube_var: &'static str, like_type_var: &'static str, escape_char_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let like_type_var = var!(like_type_var); let escape_char_var = var!(escape_char_var); 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 a76b8e41b53f0..b4404bc9232ed 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/limit.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/limit.rs @@ -1,15 +1,17 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, cube_scan_wrapper, limit, rules::wrapper::WrapperRules, + cube_scan_wrapper, limit, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, transforming_rewrite, wrapped_select, wrapper_pullup_replacer, LimitFetch, LimitSkip, LogicalPlanLanguage, WrappedSelectLimit, WrappedSelectOffset, }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn limit_rules(&self, rules: &mut Vec>) { + pub fn limit_rules(&self, rules: &mut Vec) { rules.extend(vec![transforming_rewrite( "wrapper-push-down-limit-to-cube-scan", limit( @@ -87,7 +89,7 @@ impl WrapperRules { offset_var: &'static str, wrapped_select_limit_var: &'static str, wrapped_select_offset_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let limit_var = var!(limit_var); let offset_var = var!(offset_var); let wrapped_select_limit_var = var!(wrapped_select_limit_var); 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 481a973caf2f5..4f2d7531e3ac0 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/literal.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/literal.rs @@ -1,21 +1,21 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, literal_expr, rules::wrapper::WrapperRules, - transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, LiteralExprValue, - LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, + literal_expr, rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, + wrapper_pushdown_replacer, LiteralExprValue, LogicalPlanLanguage, + WrapperPullupReplacerAliasToCube, }, var, var_iter, }; -use crate::compile::rewrite::rules::utils::{DecomposedDayTime, DecomposedMonthDayNano}; +use crate::compile::rewrite::{ + rewriter::{CubeEGraph, CubeRewrite}, + rules::utils::{DecomposedDayTime, DecomposedMonthDayNano}, +}; use datafusion::scalar::ScalarValue; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn literal_rules( - &self, - rules: &mut Vec>, - ) { + pub fn literal_rules(&self, rules: &mut Vec) { rules.extend(vec![ transforming_rewrite( "wrapper-push-down-literal", @@ -60,7 +60,7 @@ impl WrapperRules { &self, alias_to_cube_var: &str, value_var: &str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let value_var = var!(value_var); let meta = self.meta_context.clone(); @@ -100,7 +100,7 @@ impl WrapperRules { alias_to_cube_var: &str, value_var: &str, new_value_var: &str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> 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); 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 54f6f26933fe2..65966c4b9ae14 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/mod.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/mod.rs @@ -29,19 +29,17 @@ mod wrapper_pull_up; use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, fun_expr, rewrite, - rewriter::RewriteRules, + rewriter::{CubeRewrite, RewriteRules}, rules::{ replacer_flat_pull_up_node, replacer_flat_push_down_node, replacer_pull_up_node, replacer_push_down_node, }, - wrapper_pullup_replacer, wrapper_pushdown_replacer, ListType, LogicalPlanLanguage, + wrapper_pullup_replacer, wrapper_pushdown_replacer, ListType, }, config::ConfigObj, transport::MetaContext, }; -use egg::Rewrite; use std::{fmt::Display, sync::Arc}; pub struct WrapperRules { @@ -50,7 +48,7 @@ pub struct WrapperRules { } impl RewriteRules for WrapperRules { - fn rewrite_rules(&self) -> Vec> { + fn rewrite_rules(&self) -> Vec { let mut rules = Vec::new(); self.cube_scan_wrapper_rules(&mut rules); @@ -102,7 +100,7 @@ impl WrapperRules { } fn list_pushdown_pullup_rules( - rules: &mut Vec>, + rules: &mut Vec, rule_name: &str, list_node: &str, substitute_list_node: &str, @@ -157,7 +155,7 @@ impl WrapperRules { } fn flat_list_pushdown_pullup_rules( - rules: &mut Vec>, + rules: &mut Vec, rule_name: &str, list_type: ListType, substitute_list_type: ListType, @@ -218,7 +216,7 @@ impl WrapperRules { } fn expr_list_pushdown_pullup_rules( - rules: &mut Vec>, + rules: &mut Vec, rule_name: &str, list_node: &str, ) { 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 748d3bc865bb2..20ef82810d1ce 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 @@ -1,18 +1,17 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, negative_expr, rewrite, rules::wrapper::WrapperRules, + negative_expr, rewrite, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn negative_expr_rules( - &self, - rules: &mut Vec>, - ) { + pub fn negative_expr_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "wrapper-negative-push-down", @@ -55,7 +54,7 @@ impl WrapperRules { fn transform_negative_expr( &self, alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let meta = self.meta_context.clone(); move |egraph, subst| { 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 87cc50d2b5efb..79d1b114fcb90 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 @@ -1,18 +1,17 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, not_expr, rewrite, rules::wrapper::WrapperRules, + not_expr, rewrite, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn not_expr_rules( - &self, - rules: &mut Vec>, - ) { + pub fn not_expr_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "wrapper-not-push-down", @@ -55,7 +54,7 @@ impl WrapperRules { fn transform_not_expr( &self, alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let alias_to_cube_var = var!(alias_to_cube_var); let meta = self.meta_context.clone(); move |egraph, subst| { 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 5beeed8696dac..35b58940d28d3 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/order.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/order.rs @@ -1,12 +1,11 @@ use crate::compile::rewrite::{ - analysis::LogicalPlanAnalysis, cube_scan_wrapper, rewrite, rules::wrapper::WrapperRules, sort, + cube_scan_wrapper, rewrite, rewriter::CubeRewrite, rules::wrapper::WrapperRules, sort, wrapped_select, wrapped_select_order_expr_empty_tail, wrapper_pullup_replacer, - wrapper_pushdown_replacer, LogicalPlanLanguage, + wrapper_pushdown_replacer, }; -use egg::Rewrite; impl WrapperRules { - pub fn order_rules(&self, rules: &mut Vec>) { + pub fn order_rules(&self, rules: &mut Vec) { rules.extend(vec![rewrite( "wrapper-push-down-order-to-cube-scan", sort( 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 d1666b82a78ce..42aaee98d8498 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/projection.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/projection.rs @@ -1,6 +1,8 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, cube_scan_wrapper, projection, rules::wrapper::WrapperRules, + cube_scan_wrapper, projection, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, subquery, transforming_rewrite, wrapped_select, wrapped_select_aggr_expr_empty_tail, wrapped_select_filter_expr_empty_tail, wrapped_select_group_expr_empty_tail, wrapped_select_having_expr_empty_tail, wrapped_select_joins_empty_tail, @@ -11,13 +13,10 @@ use crate::{ }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst, Var}; +use egg::{Subst, Var}; impl WrapperRules { - pub fn projection_rules( - &self, - rules: &mut Vec>, - ) { + pub fn projection_rules(&self, rules: &mut Vec) { rules.extend(vec![transforming_rewrite( "wrapper-push-down-projection-to-cube-scan", projection( @@ -132,10 +131,7 @@ impl WrapperRules { } } - pub fn projection_rules_subquery( - &self, - rules: &mut Vec>, - ) { + pub fn projection_rules_subquery(&self, rules: &mut Vec) { rules.extend(vec![transforming_rewrite( "wrapper-push-down-projection-and-subquery-to-cube-scan", projection( @@ -246,7 +242,7 @@ impl WrapperRules { select_alias_var: &'static str, select_ungrouped_var: &'static str, select_ungrouped_scan_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let expr_var = var!(expr_var); let projection_alias_var = var!(projection_alias_var); let ungrouped_var = var!(ungrouped_var); @@ -276,7 +272,7 @@ impl WrapperRules { select_alias_var: &'static str, select_ungrouped_var: &'static str, select_ungrouped_scan_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> 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); @@ -309,7 +305,7 @@ impl WrapperRules { } fn transform_projection_impl( - egraph: &mut EGraph, + egraph: &mut CubeEGraph, subst: &mut Subst, expr_var: Var, projection_alias_var: Var, 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 e5ea83f884a64..b6cd4883c8c54 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 @@ -1,20 +1,18 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, fun_expr_var_arg, list_rewrite, list_rewrite_with_vars, - rewrite, rules::wrapper::WrapperRules, scalar_fun_expr_args_empty_tail, - scalar_fun_expr_args_legacy, transforming_rewrite, wrapper_pullup_replacer, - wrapper_pushdown_replacer, ListPattern, ListType, LogicalPlanLanguage, - ScalarFunctionExprFun, WrapperPullupReplacerAliasToCube, + fun_expr_var_arg, list_rewrite, list_rewrite_with_vars, rewrite, + 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, + LogicalPlanLanguage, ScalarFunctionExprFun, WrapperPullupReplacerAliasToCube, }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn scalar_function_rules( - &self, - rules: &mut Vec>, - ) { + pub fn scalar_function_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "wrapper-push-down-function", @@ -199,7 +197,7 @@ impl WrapperRules { &self, fun_var: &'static str, alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let fun_var = var!(fun_var); let alias_to_cube_var = var!(alias_to_cube_var); let meta = self.meta_context.clone(); 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 e378f93208f5b..1c4a8d652354e 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 @@ -1,14 +1,10 @@ use crate::compile::rewrite::{ - analysis::LogicalPlanAnalysis, rewrite, rules::wrapper::WrapperRules, sort_expr, - wrapper_pullup_replacer, wrapper_pushdown_replacer, LogicalPlanLanguage, + rewrite, rewriter::CubeRewrite, rules::wrapper::WrapperRules, sort_expr, + wrapper_pullup_replacer, wrapper_pushdown_replacer, }; -use egg::Rewrite; impl WrapperRules { - pub fn sort_expr_rules( - &self, - rules: &mut Vec>, - ) { + pub fn sort_expr_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "wrapper-push-down-sort-expr", 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 796a7f9a48493..48629fdbab7b6 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/subquery.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/subquery.rs @@ -1,21 +1,19 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, cube_scan_wrapper, empty_relation, - rules::wrapper::WrapperRules, transforming_rewrite, wrapper_pullup_replacer, - wrapper_pushdown_replacer, EmptyRelationDerivedSourceTableName, LogicalPlanLanguage, - WrapperPullupReplacerAliasToCube, + cube_scan_wrapper, empty_relation, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, + transforming_rewrite, wrapper_pullup_replacer, wrapper_pushdown_replacer, + EmptyRelationDerivedSourceTableName, LogicalPlanLanguage, WrapperPullupReplacerAliasToCube, }, transport::MetaContext, var, var_iter, var_list_iter, }; -use egg::{EGraph, Rewrite, Subst, Var}; +use egg::{Subst, Var}; use std::sync::Arc; impl WrapperRules { - pub fn subquery_rules( - &self, - rules: &mut Vec>, - ) { + pub fn subquery_rules(&self, rules: &mut Vec) { rules.extend(vec![ transforming_rewrite( "wrapper-subqueries-wrapped-scan-to-pull", @@ -79,7 +77,7 @@ impl WrapperRules { &self, source_table_name_var: &'static str, alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let source_table_name_var = var!(source_table_name_var); let alias_to_cube_var = var!(alias_to_cube_var); let meta_context = self.meta_context.clone(); @@ -113,7 +111,7 @@ impl WrapperRules { } pub fn transform_check_subquery_allowed( - egraph: &mut EGraph, + egraph: &mut CubeEGraph, subst: &mut Subst, meta: Arc, alias_to_cube_var: Var, @@ -140,7 +138,7 @@ impl WrapperRules { fn transform_check_subquery_wrapped( &self, cube_scan_input_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let cube_scan_input_var = var!(cube_scan_input_var); move |egraph, subst| { for _ in var_list_iter!(egraph[subst[cube_scan_input_var]], WrappedSelect).cloned() { 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 d50f64b8f42c0..25bd9f7aa4e48 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 @@ -1,19 +1,18 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, rewrite, 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, LogicalPlanLanguage, ScalarUDFExprFun, + rewrite, + 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, LogicalPlanLanguage, ScalarUDFExprFun, WrapperPullupReplacerAliasToCube, }, var, var_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn udf_function_rules( - &self, - rules: &mut Vec>, - ) { + pub fn udf_function_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "wrapper-push-down-udf", @@ -132,7 +131,7 @@ impl WrapperRules { &self, fun_var: &'static str, alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let fun_var = var!(fun_var); let alias_to_cube_var = var!(alias_to_cube_var); let meta = self.meta_context.clone(); 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 dd6f65f178b13..9ca83c4807dea 100644 --- a/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/window.rs +++ b/rust/cubesql/cubesql/src/compile/rewrite/rules/wrapper/window.rs @@ -1,12 +1,11 @@ use crate::compile::rewrite::{ - analysis::LogicalPlanAnalysis, cube_scan_wrapper, rewrite, rules::wrapper::WrapperRules, - window, wrapped_select, wrapped_select_window_expr_empty_tail, wrapper_pullup_replacer, - wrapper_pushdown_replacer, ListType, LogicalPlanLanguage, + 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::Rewrite; impl WrapperRules { - pub fn window_rules(&self, rules: &mut Vec>) { + pub fn window_rules(&self, rules: &mut Vec) { rules.extend(vec![rewrite( "wrapper-push-down-window-to-cube-scan", window( 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 73bb990bc4b4c..de89203ff4321 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 @@ -1,19 +1,19 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, rewrite, rules::wrapper::WrapperRules, transforming_rewrite, - window_fun_expr_var_arg, wrapper_pullup_replacer, wrapper_pushdown_replacer, - LogicalPlanLanguage, WindowFunctionExprFun, WrapperPullupReplacerAliasToCube, + rewrite, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, + transforming_rewrite, window_fun_expr_var_arg, wrapper_pullup_replacer, + wrapper_pushdown_replacer, LogicalPlanLanguage, WindowFunctionExprFun, + WrapperPullupReplacerAliasToCube, }, var, var_iter, }; use datafusion::physical_plan::windows::WindowFunction; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn aggregate_function_rules( - &self, - rules: &mut Vec>, - ) { + pub fn aggregate_function_rules(&self, rules: &mut Vec) { rules.extend(vec![ rewrite( "wrapper-push-down-window-function", @@ -123,7 +123,7 @@ impl WrapperRules { &self, fun_var: &'static str, alias_to_cube_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let fun_var = var!(fun_var); let alias_to_cube_var = var!(alias_to_cube_var); let meta = self.meta_context.clone(); 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 0e6574350f210..cb59a308fc48f 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 @@ -1,19 +1,18 @@ use crate::{ compile::rewrite::{ - analysis::LogicalPlanAnalysis, cube_scan_wrapper, rules::wrapper::WrapperRules, + cube_scan_wrapper, + rewriter::{CubeEGraph, CubeRewrite}, + rules::wrapper::WrapperRules, transforming_rewrite, wrapped_select, wrapped_select_having_expr_empty_tail, wrapped_select_joins_empty_tail, wrapper_pullup_replacer, LogicalPlanLanguage, WrappedSelectSelectType, WrappedSelectType, }, var, var_iter, var_list_iter, }; -use egg::{EGraph, Rewrite, Subst}; +use egg::Subst; impl WrapperRules { - pub fn wrapper_pull_up_rules( - &self, - rules: &mut Vec>, - ) { + pub fn wrapper_pull_up_rules(&self, rules: &mut Vec) { rules.extend(vec![ transforming_rewrite( "wrapper-pull-up-to-cube-scan-non-wrapped-select", @@ -271,7 +270,7 @@ impl WrapperRules { fn transform_pull_up_wrapper_select( &self, cube_scan_input_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let cube_scan_input_var = var!(cube_scan_input_var); move |egraph, subst| { for _ in var_list_iter!(egraph[subst[cube_scan_input_var]], WrappedSelect).cloned() { @@ -291,7 +290,7 @@ impl WrapperRules { inner_projection_expr_var: &'static str, _inner_group_expr_var: &'static str, _inner_aggr_expr_var: &'static str, - ) -> impl Fn(&mut EGraph, &mut Subst) -> bool { + ) -> impl Fn(&mut CubeEGraph, &mut Subst) -> bool { let select_type_var = var!(select_type_var); let projection_expr_var = var!(projection_expr_var); let inner_select_type_var = var!(inner_select_type_var); diff --git a/rust/cubesql/cubesql/src/compile/test/rewrite_engine.rs b/rust/cubesql/cubesql/src/compile/test/rewrite_engine.rs index 870be963ffad5..2ae1aefd114e2 100644 --- a/rust/cubesql/cubesql/src/compile/test/rewrite_engine.rs +++ b/rust/cubesql/cubesql/src/compile/test/rewrite_engine.rs @@ -4,17 +4,14 @@ use datafusion::{ logical_plan::LogicalPlan, sql::{parser::Statement, planner::SqlToRel}, }; -use egg::Rewrite; use super::get_test_session; use crate::{ compile::{ parser::parse_sql_to_statement, rewrite::{ - analysis::LogicalPlanAnalysis, converter::{CubeRunner, LogicalPlanToLanguageConverter}, - rewriter::Rewriter, - LogicalPlanLanguage, + rewriter::{CubeRewrite, Rewriter}, }, rewrite_statement, CompilationError, CubeContext, DatabaseProtocol, QueryEngine, SqlQueryEngine, @@ -55,9 +52,7 @@ pub fn rewrite_runner(plan: LogicalPlan, context: Arc) -> CubeRunne converter.take_runner() } -pub fn rewrite_rules( - cube_context: Arc, -) -> Vec> { +pub fn rewrite_rules(cube_context: Arc) -> Vec { Rewriter::rewrite_rules( cube_context.meta.clone(), cube_context.sessions.server.config_obj.clone(), diff --git a/rust/cubesql/cubesql/src/sql/compiler_cache.rs b/rust/cubesql/cubesql/src/sql/compiler_cache.rs index 220d62cd713ad..80131a5192e9a 100644 --- a/rust/cubesql/cubesql/src/sql/compiler_cache.rs +++ b/rust/cubesql/cubesql/src/sql/compiler_cache.rs @@ -2,7 +2,7 @@ use crate::{ compile::{ engine::CubeContext, qtrace::Qtrace, - rewrite::{analysis::LogicalPlanAnalysis, rewriter::Rewriter, LogicalPlanLanguage}, + rewrite::rewriter::{CubeEGraph, CubeRewrite, Rewriter}, DatabaseProtocol, }, config::ConfigObj, @@ -13,7 +13,6 @@ use crate::{ }; use async_trait::async_trait; use datafusion::scalar::ScalarValue; -use egg::{EGraph, Rewrite}; use lru::LruCache; use std::{collections::HashMap, fmt::Debug, num::NonZeroUsize, sync::Arc}; use uuid::Uuid; @@ -25,7 +24,7 @@ pub trait CompilerCache: Send + Sync + Debug { ctx: AuthContextRef, protocol: DatabaseProtocol, eval_stable_functions: bool, - ) -> Result>>, CubeError>; + ) -> Result>, CubeError>; async fn meta( &self, @@ -37,18 +36,18 @@ pub trait CompilerCache: Send + Sync + Debug { &self, ctx: AuthContextRef, cube_context: Arc, - input_plan: EGraph, + input_plan: CubeEGraph, qtrace: &mut Option, - ) -> Result, CubeError>; + ) -> Result; async fn rewrite( &self, ctx: AuthContextRef, cube_context: Arc, - input_plan: EGraph, + input_plan: CubeEGraph, param_values: &HashMap, qtrace: &mut Option, - ) -> Result, CubeError>; + ) -> Result; } #[derive(Debug)] @@ -60,11 +59,9 @@ pub struct CompilerCacheImpl { pub struct CompilerCacheEntry { meta_context: Arc, - rewrite_rules: - RWLockAsync>>>>, - parameterized_cache: - MutexAsync>>, - queries_cache: MutexAsync>>, + rewrite_rules: RWLockAsync>>>, + parameterized_cache: MutexAsync>, + queries_cache: MutexAsync>, } crate::di_service!(CompilerCacheImpl, [CompilerCache]); @@ -76,7 +73,7 @@ impl CompilerCache for CompilerCacheImpl { ctx: AuthContextRef, protocol: DatabaseProtocol, eval_stable_functions: bool, - ) -> Result>>, CubeError> { + ) -> Result>, CubeError> { let cache_entry = self.get_cache_entry(ctx.clone(), protocol).await?; let rewrite_rules = { @@ -119,9 +116,9 @@ impl CompilerCache for CompilerCacheImpl { &self, ctx: AuthContextRef, cube_context: Arc, - parameterized_graph: EGraph, + parameterized_graph: CubeEGraph, qtrace: &mut Option, - ) -> Result, CubeError> { + ) -> Result { let cache_entry = self .get_cache_entry(ctx.clone(), cube_context.session_state.protocol.clone()) .await?; @@ -145,10 +142,10 @@ impl CompilerCache for CompilerCacheImpl { &self, ctx: AuthContextRef, cube_context: Arc, - input_plan: EGraph, + input_plan: CubeEGraph, param_values: &HashMap, qtrace: &mut Option, - ) -> Result, CubeError> { + ) -> Result { if !self.config_obj.enable_rewrite_cache() { let mut rewriter = Rewriter::new(input_plan, cube_context); rewriter.add_param_values(param_values)?; diff --git a/rust/cubesql/cubesql/src/utils/mod.rs b/rust/cubesql/cubesql/src/utils/mod.rs index c56024d2e2e48..8d3b2cc28246d 100644 --- a/rust/cubesql/cubesql/src/utils/mod.rs +++ b/rust/cubesql/cubesql/src/utils/mod.rs @@ -1,6 +1,5 @@ -use crate::compile::rewrite::{analysis::LogicalPlanAnalysis, LogicalPlanLanguage}; +use crate::compile::rewrite::rewriter::CubeEGraph; use datafusion::scalar::ScalarValue; -use egg::EGraph; use sha2::{Digest, Sha256}; use std::{ collections::HashMap, @@ -38,10 +37,7 @@ impl ShaHasher { } } -pub fn egraph_hash( - egraph: &EGraph, - params: Option<&HashMap>, -) -> [u8; 32] { +pub fn egraph_hash(egraph: &CubeEGraph, params: Option<&HashMap>) -> [u8; 32] { let mut hasher = ShaHasher::new(); for class in egraph.classes() { class.id.hash(&mut hasher);