Skip to content

Commit cf0b77c

Browse files
authored
Merge pull request #7606 from xudong963/cbo_switch
chore: add the switch of enable_cbo
2 parents 1a6f2c0 + 9802327 commit cf0b77c

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

src/query/service/src/sql/planner/optimizer/cascades/mod.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ mod implement_rules;
1717

1818
use std::collections::hash_map::Entry;
1919
use std::collections::HashMap;
20+
use std::sync::Arc;
2021

22+
use common_catalog::table_context::TableContext;
2123
use common_exception::ErrorCode;
2224
use common_exception::Result;
2325

@@ -47,17 +49,24 @@ pub struct CascadesOptimizer {
4749

4850
/// group index -> best cost context
4951
best_cost_map: HashMap<IndexType, CostContext>,
52+
_ctx: Arc<dyn TableContext>,
5053
}
5154

5255
impl CascadesOptimizer {
53-
pub fn create() -> Self {
54-
CascadesOptimizer {
56+
pub fn create(ctx: Arc<dyn TableContext>) -> Result<Self> {
57+
let explore_rules = if ctx.get_settings().get_enable_cbo()? {
58+
get_explore_rule_set()
59+
} else {
60+
RuleSet::create_with_ids(vec![]).unwrap()
61+
};
62+
Ok(CascadesOptimizer {
5563
memo: Memo::create(),
56-
explore_rules: get_explore_rule_set(),
64+
explore_rules,
5765
implement_rules: get_implement_rule_set(),
5866
cost_model: Box::new(DefaultCostModel),
5967
best_cost_map: HashMap::new(),
60-
}
68+
_ctx: ctx,
69+
})
6170
}
6271

6372
fn init(&mut self, expression: SExpr) -> Result<()> {

src/query/service/src/sql/planner/optimizer/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ pub fn optimize_query(
140140

141141
let contains_local_table_scan = contains_local_table_scan(&s_expr, &metadata);
142142

143-
let mut heuristic = HeuristicOptimizer::new(ctx, bind_context, metadata, rules);
143+
let mut heuristic = HeuristicOptimizer::new(ctx.clone(), bind_context, metadata, rules);
144144
let mut result = heuristic.optimize(s_expr)?;
145145

146-
let cascades = CascadesOptimizer::create();
146+
let cascades = CascadesOptimizer::create(ctx)?;
147147
result = cascades.optimize(result)?;
148148

149149
// So far, we don't have ability to execute distributed query

src/query/service/tests/it/storages/system/settings_table.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ async fn test_settings_table() -> Result<()> {
3737
"| compression | None | None | SESSION | Format compression, default value: None | String |",
3838
"| empty_as_default | 1 | 1 | SESSION | Format empty_as_default, default value: 1 | UInt64 |",
3939
"| enable_async_insert | 0 | 0 | SESSION | Whether the client open async insert mode, default value: 0 | UInt64 |",
40+
"| enable_cbo | 1 | 1 | SESSION | If enable cost based optimization, default value: 1 | UInt64 |",
4041
"| enable_new_processor_framework | 1 | 1 | SESSION | Enable new processor framework if value != 0, default value: 1 | UInt64 |",
4142
"| enable_planner_v2 | 1 | 1 | SESSION | Enable planner v2 by setting this variable to 1, default value: 1 | UInt64 |",
4243
"| field_delimiter | , | , | SESSION | Format field delimiter, default value: , | String |",

src/query/settings/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,13 @@ impl Settings {
281281
desc: "SQL dialect, support \"PostgreSQL\" and \"MySQL\", default value: \"PostgreSQL\"",
282282
possible_values: Some(vec!["PostgreSQL", "MySQL"]),
283283
},
284+
SettingValue {
285+
default_value: DataValue::UInt64(1),
286+
user_setting: UserSetting::create("enable_cbo", DataValue::UInt64(1)),
287+
level: ScopeLevel::Session,
288+
desc: "If enable cost based optimization, default value: 1",
289+
possible_values: None,
290+
},
284291
// max_execute_time
285292
SettingValue {
286293
default_value: DataValue::UInt64(0),
@@ -459,6 +466,18 @@ impl Settings {
459466
self.try_set_u64(KEY, v, false)
460467
}
461468

469+
pub fn get_enable_cbo(&self) -> Result<bool> {
470+
static KEY: &str = "enable_cbo";
471+
let v = self.try_get_u64(KEY)?;
472+
Ok(v != 0)
473+
}
474+
475+
pub fn set_enable_cbo(&self, val: bool) -> Result<()> {
476+
static KEY: &str = "enable_cbo";
477+
let v = if val { 1 } else { 0 };
478+
self.try_set_u64(KEY, v, false)
479+
}
480+
462481
pub fn get_sql_dialect(&self) -> Result<Dialect> {
463482
let key = "sql_dialect";
464483
self.check_and_get_setting_value(key)

tests/logictest/suites/base/06_show/06_0003_show_settings

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ SHOW SETTINGS;
1111
compression None None SESSION Format compression, default value: None String
1212
empty_as_default 1 1 SESSION Format empty_as_default, default value: 1 UInt64
1313
enable_async_insert 0 0 SESSION Whether the client open async insert mode, default value: 0 UInt64
14+
enable_cbo 1 1 SESSION If enable cost based optimization, default value: 1 UInt64
1415
enable_new_processor_framework 1 1 SESSION Enable new processor framework if value != 0, default value: 1 UInt64
1516
enable_planner_v2 1 1 SESSION Enable planner v2 by setting this variable to 1, default value: 1 UInt64
1617
field_delimiter , , SESSION Format field delimiter, default value: , String
@@ -35,6 +36,7 @@ SHOW SETTINGS LIKE 'enable%';
3536

3637
----
3738
enable_async_insert 0 0 SESSION Whether the client open async insert mode, default value: 0 UInt64
39+
enable_cbo 1 1 SESSION If enable cost based optimization, default value: 1 UInt64
3840
enable_new_processor_framework 1 1 SESSION Enable new processor framework if value != 0, default value: 1 UInt64
3941
enable_planner_v2 1 1 SESSION Enable planner v2 by setting this variable to 1, default value: 1 UInt64
4042

0 commit comments

Comments
 (0)