Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(query): add max_query_row_nums #9406

Merged
merged 4 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ DB.Table: 'system'.'settings', Table: settings-table_id:1, ver:0, Engine: System
| max_block_size | 65536 | 65536 | SESSION | Maximum block size for reading, default value: 65536. | UInt64 |
| max_execute_time | 0 | 0 | SESSION | The maximum query execution time. it means no limit if the value is zero. default value: 0. | UInt64 |
| max_inlist_to_or | 3 | 3 | SESSION | Max size in inlist expression that will convert to or combinator, default value: 3. | UInt64 |
| max_result_rows | 0 | 0 | SESSION | Auto limit max result rows if user not specify the limit, default is 0 means no limit | UInt64 |
| parquet_uncompressed_buffer_size | 2097152 | 2097152 | SESSION | Parquet decompresses buffer size. default: 2MB | UInt64 |
| prefer_broadcast_join | 0 | 0 | SESSION | If enable broadcast join, default value: 0 | UInt64 |
| quoted_ident_case_sensitive | 1 | 1 | SESSION | Case sensitivity of quoted identifiers, default value: 1 (aka case-sensitive). | UInt64 |
Expand Down
13 changes: 13 additions & 0 deletions src/query/settings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,13 @@ impl Settings {
desc: "the max number of rows each read from parquet to databend processor",
possible_values: None,
},
SettingValue {
default_value: UserSettingValue::UInt64(0),
user_setting: UserSetting::create("max_result_rows", UserSettingValue::UInt64(0)),
level: ScopeLevel::Session,
desc: "Auto limit max result rows if user not specify the limit, default is 0 means no limit",
BohuTANG marked this conversation as resolved.
Show resolved Hide resolved
possible_values: None,
},
SettingValue {
default_value: UserSettingValue::UInt64(1),
user_setting: UserSetting::create(
Expand Down Expand Up @@ -685,6 +692,12 @@ impl Settings {
Ok(v != 0)
}

pub fn get_max_result_rows(&self) -> Result<u64> {
static KEY: &str = "max_result_rows";
let v = self.try_get_u64(KEY)?;
Ok(v)
}

pub fn set_enable_distributed_eval_index(&self, val: bool) -> Result<()> {
static KEY: &str = "enable_distributed_eval_index";
let v = u64::from(val);
Expand Down
16 changes: 15 additions & 1 deletion src/query/sql/src/planner/binder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use common_ast::ast::Expr;
use common_ast::ast::Join;
use common_ast::ast::JoinCondition;
use common_ast::ast::JoinOperator;
use common_ast::ast::Literal;
use common_ast::ast::OrderByExpr;
use common_ast::ast::Query;
use common_ast::ast::SelectStmt;
Expand Down Expand Up @@ -227,6 +228,15 @@ impl<'a> Binder {
}
};

let default_limit = if self.ctx.get_settings().get_max_result_rows()? > 0 {
Some(Expr::Literal {
span: &[],
lit: Literal::Integer(self.ctx.get_settings().get_max_result_rows()?),
})
} else {
None
};

if !query.limit.is_empty() {
if query.limit.len() == 1 {
s_expr = self
Expand All @@ -244,7 +254,11 @@ impl<'a> Binder {
}
} else if query.offset.is_some() {
s_expr = self
.bind_limit(&bind_context, s_expr, None, &query.offset)
.bind_limit(&bind_context, s_expr, default_limit.as_ref(), &query.offset)
.await?;
} else if let Some(l) = default_limit {
s_expr = self
.bind_limit(&bind_context, s_expr, Some(&l), &None)
.await?;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
statement ok
DROP DATABASE IF EXISTS db1

statement ok
CREATE DATABASE db1

statement ok
USE db1

statement ok
CREATE TABLE IF NOT EXISTS t1(a INT) Engine = fuse

statement ok
INSERT INTO t1 (a) values (1), (2), (3)

query I
SELECT COUNT() FROM (SELECT * FROM t1)
----
3

statement ok
SET max_result_rows=1

query I
SELECT COUNT() FROM (SELECT * FROM t1)
----
1

query I
SELECT COUNT() FROM (SELECT * FROM t1 limit 2)
----
2

statement ok
DROP TABLE t1

statement ok
DROP DATABASE db1