1
- // Copyright 2021 Datafuse Labs.
1
+ // Copyright 2022 Datafuse Labs.
2
2
//
3
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
4
// you may not use this file except in compliance with the License.
@@ -20,21 +20,21 @@ mod group;
20
20
mod heuristic;
21
21
mod m_expr;
22
22
mod memo;
23
+ #[ allow( clippy:: module_inception) ]
24
+ mod optimizer;
23
25
mod pattern_extractor;
24
26
mod property;
25
27
mod rule;
26
28
mod s_expr;
27
29
mod util;
28
30
29
- use std:: sync:: Arc ;
30
-
31
- use common_ast:: ast:: ExplainKind ;
32
- use common_exception:: Result ;
33
- use common_planner:: MetadataRef ;
34
31
pub use heuristic:: HeuristicOptimizer ;
35
32
pub use heuristic:: DEFAULT_REWRITE_RULES ;
36
33
pub use m_expr:: MExpr ;
37
34
pub use memo:: Memo ;
35
+ pub use optimizer:: optimize;
36
+ pub use optimizer:: OptimizerConfig ;
37
+ pub use optimizer:: OptimizerContext ;
38
38
pub use pattern_extractor:: PatternExtractor ;
39
39
pub use property:: ColumnSet ;
40
40
pub use property:: Distribution ;
@@ -43,116 +43,6 @@ pub use property::RelExpr;
43
43
pub use property:: RelationalProperty ;
44
44
pub use property:: RequiredProperty ;
45
45
pub use rule:: RuleFactory ;
46
+ pub use rule:: RuleID ;
47
+ pub use rule:: RuleSet ;
46
48
pub use s_expr:: SExpr ;
47
-
48
- use self :: cascades:: CascadesOptimizer ;
49
- use self :: distributed:: optimize_distributed_query;
50
- use self :: util:: contains_local_table_scan;
51
- use self :: util:: validate_distributed_query;
52
- use super :: plans:: Plan ;
53
- use super :: BindContext ;
54
- use crate :: sessions:: TableContext ;
55
- pub use crate :: sql:: optimizer:: heuristic:: RuleList ;
56
- pub use crate :: sql:: optimizer:: rule:: RuleID ;
57
- use crate :: sql:: optimizer:: rule:: RuleSet ;
58
- use crate :: sql:: plans:: CopyPlanV2 ;
59
-
60
- #[ derive( Debug , Clone , Default ) ]
61
- pub struct OptimizerConfig {
62
- pub enable_distributed_optimization : bool ,
63
- }
64
-
65
- #[ derive( Debug ) ]
66
- pub struct OptimizerContext {
67
- pub config : OptimizerConfig ,
68
- }
69
-
70
- impl OptimizerContext {
71
- pub fn new ( config : OptimizerConfig ) -> Self {
72
- Self { config }
73
- }
74
- }
75
-
76
- pub fn optimize (
77
- ctx : Arc < dyn TableContext > ,
78
- opt_ctx : Arc < OptimizerContext > ,
79
- plan : Plan ,
80
- ) -> Result < Plan > {
81
- match plan {
82
- Plan :: Query {
83
- s_expr,
84
- bind_context,
85
- metadata,
86
- rewrite_kind,
87
- } => Ok ( Plan :: Query {
88
- s_expr : Box :: new ( optimize_query (
89
- ctx,
90
- opt_ctx,
91
- metadata. clone ( ) ,
92
- bind_context. clone ( ) ,
93
- * s_expr,
94
- ) ?) ,
95
- bind_context,
96
- metadata,
97
- rewrite_kind,
98
- } ) ,
99
- Plan :: Explain { kind, plan } => match kind {
100
- ExplainKind :: Raw | ExplainKind :: Ast ( _) | ExplainKind :: Syntax ( _) => {
101
- Ok ( Plan :: Explain { kind, plan } )
102
- }
103
- _ => Ok ( Plan :: Explain {
104
- kind,
105
- plan : Box :: new ( optimize ( ctx, opt_ctx, * plan) ?) ,
106
- } ) ,
107
- } ,
108
- Plan :: Copy ( v) => {
109
- Ok ( Plan :: Copy ( Box :: new ( match * v {
110
- CopyPlanV2 :: IntoStage {
111
- stage,
112
- path,
113
- validation_mode,
114
- from,
115
- } => {
116
- CopyPlanV2 :: IntoStage {
117
- stage,
118
- path,
119
- validation_mode,
120
- // Make sure the subquery has been optimized.
121
- from : Box :: new ( optimize ( ctx, opt_ctx, * from) ?) ,
122
- }
123
- }
124
- into_table => into_table,
125
- } ) ) )
126
- }
127
- // Passthrough statements
128
- _ => Ok ( plan) ,
129
- }
130
- }
131
-
132
- pub fn optimize_query (
133
- ctx : Arc < dyn TableContext > ,
134
- opt_ctx : Arc < OptimizerContext > ,
135
- metadata : MetadataRef ,
136
- bind_context : Box < BindContext > ,
137
- s_expr : SExpr ,
138
- ) -> Result < SExpr > {
139
- let rules = RuleList :: create ( DEFAULT_REWRITE_RULES . clone ( ) ) ?;
140
-
141
- let contains_local_table_scan = contains_local_table_scan ( & s_expr, & metadata) ;
142
-
143
- let mut heuristic = HeuristicOptimizer :: new ( ctx. clone ( ) , bind_context, metadata, rules) ;
144
- let mut result = heuristic. optimize ( s_expr) ?;
145
-
146
- let cascades = CascadesOptimizer :: create ( ctx) ?;
147
- result = cascades. optimize ( result) ?;
148
-
149
- // So far, we don't have ability to execute distributed query
150
- // with reading data from local tales(e.g. system tables).
151
- let enable_distributed_query =
152
- opt_ctx. config . enable_distributed_optimization && !contains_local_table_scan;
153
- if enable_distributed_query && validate_distributed_query ( & result) {
154
- result = optimize_distributed_query ( & result) ?;
155
- }
156
-
157
- Ok ( result)
158
- }
0 commit comments