Skip to content

Commit

Permalink
Merge pull request #9406 from sandflee/limit2
Browse files Browse the repository at this point in the history
feat(query): add max_query_row_nums
  • Loading branch information
BohuTANG authored Dec 31, 2022
2 parents 27f63d3 + 0c85285 commit 7c7eb6a
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 1 deletion.
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",
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

1 comment on commit 7c7eb6a

@vercel
Copy link

@vercel vercel bot commented on 7c7eb6a Dec 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

databend – ./

databend-git-main-databend.vercel.app
databend.rs
databend.vercel.app
databend-databend.vercel.app

Please sign in to comment.