Skip to content

Commit e5c4c84

Browse files
committed
sql/planner/optimizer/mod.rs -> sql/planner/optimizer/optimizer.rs
1 parent 4ee35cd commit e5c4c84

File tree

2 files changed

+140
-118
lines changed

2 files changed

+140
-118
lines changed
Lines changed: 8 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 Datafuse Labs.
1+
// Copyright 2022 Datafuse Labs.
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -20,21 +20,21 @@ mod group;
2020
mod heuristic;
2121
mod m_expr;
2222
mod memo;
23+
#[allow(clippy::module_inception)]
24+
mod optimizer;
2325
mod pattern_extractor;
2426
mod property;
2527
mod rule;
2628
mod s_expr;
2729
mod util;
2830

29-
use std::sync::Arc;
30-
31-
use common_ast::ast::ExplainKind;
32-
use common_exception::Result;
33-
use common_planner::MetadataRef;
3431
pub use heuristic::HeuristicOptimizer;
3532
pub use heuristic::DEFAULT_REWRITE_RULES;
3633
pub use m_expr::MExpr;
3734
pub use memo::Memo;
35+
pub use optimizer::optimize;
36+
pub use optimizer::OptimizerConfig;
37+
pub use optimizer::OptimizerContext;
3838
pub use pattern_extractor::PatternExtractor;
3939
pub use property::ColumnSet;
4040
pub use property::Distribution;
@@ -43,116 +43,6 @@ pub use property::RelExpr;
4343
pub use property::RelationalProperty;
4444
pub use property::RequiredProperty;
4545
pub use rule::RuleFactory;
46+
pub use rule::RuleID;
47+
pub use rule::RuleSet;
4648
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-
}
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright 2022 Datafuse Labs.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use std::sync::Arc;
16+
17+
use common_ast::ast::ExplainKind;
18+
use common_catalog::table_context::TableContext;
19+
use common_exception::Result;
20+
use common_planner::MetadataRef;
21+
22+
use crate::sql::optimizer::cascades::CascadesOptimizer;
23+
use crate::sql::optimizer::distributed::optimize_distributed_query;
24+
use crate::sql::optimizer::heuristic::RuleList;
25+
use crate::sql::optimizer::util::contains_local_table_scan;
26+
use crate::sql::optimizer::util::validate_distributed_query;
27+
use crate::sql::optimizer::HeuristicOptimizer;
28+
use crate::sql::optimizer::SExpr;
29+
use crate::sql::optimizer::DEFAULT_REWRITE_RULES;
30+
use crate::sql::plans::CopyPlanV2;
31+
use crate::sql::plans::Plan;
32+
use crate::sql::BindContext;
33+
34+
#[derive(Debug, Clone, Default)]
35+
pub struct OptimizerConfig {
36+
pub enable_distributed_optimization: bool,
37+
}
38+
39+
#[derive(Debug)]
40+
pub struct OptimizerContext {
41+
pub config: OptimizerConfig,
42+
}
43+
44+
impl OptimizerContext {
45+
pub fn new(config: OptimizerConfig) -> Self {
46+
Self { config }
47+
}
48+
}
49+
50+
pub fn optimize(
51+
ctx: Arc<dyn TableContext>,
52+
opt_ctx: Arc<OptimizerContext>,
53+
plan: Plan,
54+
) -> Result<Plan> {
55+
match plan {
56+
Plan::Query {
57+
s_expr,
58+
bind_context,
59+
metadata,
60+
rewrite_kind,
61+
} => Ok(Plan::Query {
62+
s_expr: Box::new(optimize_query(
63+
ctx,
64+
opt_ctx,
65+
metadata.clone(),
66+
bind_context.clone(),
67+
*s_expr,
68+
)?),
69+
bind_context,
70+
metadata,
71+
rewrite_kind,
72+
}),
73+
Plan::Explain { kind, plan } => match kind {
74+
ExplainKind::Raw | ExplainKind::Ast(_) | ExplainKind::Syntax(_) => {
75+
Ok(Plan::Explain { kind, plan })
76+
}
77+
_ => Ok(Plan::Explain {
78+
kind,
79+
plan: Box::new(optimize(ctx, opt_ctx, *plan)?),
80+
}),
81+
},
82+
Plan::Copy(v) => {
83+
Ok(Plan::Copy(Box::new(match *v {
84+
CopyPlanV2::IntoStage {
85+
stage,
86+
path,
87+
validation_mode,
88+
from,
89+
} => {
90+
CopyPlanV2::IntoStage {
91+
stage,
92+
path,
93+
validation_mode,
94+
// Make sure the subquery has been optimized.
95+
from: Box::new(optimize(ctx, opt_ctx, *from)?),
96+
}
97+
}
98+
into_table => into_table,
99+
})))
100+
}
101+
// Passthrough statements
102+
_ => Ok(plan),
103+
}
104+
}
105+
106+
pub fn optimize_query(
107+
ctx: Arc<dyn TableContext>,
108+
opt_ctx: Arc<OptimizerContext>,
109+
metadata: MetadataRef,
110+
bind_context: Box<BindContext>,
111+
s_expr: SExpr,
112+
) -> Result<SExpr> {
113+
let rules = RuleList::create(DEFAULT_REWRITE_RULES.clone())?;
114+
115+
let contains_local_table_scan = contains_local_table_scan(&s_expr, &metadata);
116+
117+
let mut heuristic = HeuristicOptimizer::new(ctx.clone(), bind_context, metadata, rules);
118+
let mut result = heuristic.optimize(s_expr)?;
119+
120+
let cascades = CascadesOptimizer::create(ctx)?;
121+
result = cascades.optimize(result)?;
122+
123+
// So far, we don't have ability to execute distributed query
124+
// with reading data from local tales(e.g. system tables).
125+
let enable_distributed_query =
126+
opt_ctx.config.enable_distributed_optimization && !contains_local_table_scan;
127+
if enable_distributed_query && validate_distributed_query(&result) {
128+
result = optimize_distributed_query(&result)?;
129+
}
130+
131+
Ok(result)
132+
}

0 commit comments

Comments
 (0)