Skip to content

Commit 7720978

Browse files
authored
feat(query): Support SHOW VARIABLES (#16409)
* feat(query): support SHOW VARIABLES * show variables not support mysql handlers but can use table function show_variables() to query all vars in session
1 parent 390b9c1 commit 7720978

File tree

16 files changed

+313
-2
lines changed

16 files changed

+313
-2
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/query/ast/src/ast/statements/statement.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ pub enum Statement {
9696
identifiers: Vec<Identifier>,
9797
},
9898

99+
ShowVariables {
100+
show_options: Option<ShowOptions>,
101+
},
102+
99103
SetRole {
100104
is_default: bool,
101105
role_name: String,
@@ -431,6 +435,12 @@ impl Display for Statement {
431435
write!(f, " {show_options}")?;
432436
}
433437
}
438+
Statement::ShowVariables { show_options } => {
439+
write!(f, "SHOW VARIABLES")?;
440+
if let Some(show_options) = show_options {
441+
write!(f, " {show_options}")?;
442+
}
443+
}
434444
Statement::ShowProcessList { show_options } => {
435445
write!(f, "SHOW PROCESSLIST")?;
436446
if let Some(show_options) = show_options {

src/query/ast/src/parser/statement.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
267267
},
268268
|(_, _, show_options)| Statement::ShowSettings { show_options },
269269
);
270+
let show_variables = map(
271+
rule! {
272+
SHOW ~ VARIABLES ~ #show_options?
273+
},
274+
|(_, _, show_options)| Statement::ShowVariables { show_options },
275+
);
270276
let show_stages = value(Statement::ShowStages, rule! { SHOW ~ STAGES });
271277
let show_process_list = map(
272278
rule! {
@@ -2170,6 +2176,7 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
21702176
| #explain : "`EXPLAIN [PIPELINE | GRAPH] <statement>`"
21712177
| #explain_analyze : "`EXPLAIN ANALYZE <statement>`"
21722178
| #show_settings : "`SHOW SETTINGS [<show_limit>]`"
2179+
| #show_variables : "`SHOW VARIABLES [<show_limit>]`"
21732180
| #show_stages : "`SHOW STAGES`"
21742181
| #show_engines : "`SHOW ENGINES`"
21752182
| #show_process_list : "`SHOW PROCESSLIST`"

src/query/ast/src/parser/token.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,8 @@ pub enum TokenKind {
10211021
SESSION,
10221022
#[token("SETTINGS", ignore(ascii_case))]
10231023
SETTINGS,
1024+
#[token("VARIABLES", ignore(ascii_case))]
1025+
VARIABLES,
10241026
#[token("STAGES", ignore(ascii_case))]
10251027
STAGES,
10261028
#[token("STATISTIC", ignore(ascii_case))]

src/query/ast/tests/it/parser.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,8 @@ fn test_statement() {
571571
r#"UNSET (max_threads, sql_dialect);"#,
572572
r#"UNSET session (max_threads, sql_dialect);"#,
573573
r#"SET variable a = 3"#,
574+
r#"show variables"#,
575+
r#"show variables like 'v%'"#,
574576
r#"SET variable a = select 3"#,
575577
r#"SET variable a = (select max(number) from numbers(10))"#,
576578
r#"select $1 FROM '@my_stage/my data/'"#,

src/query/ast/tests/it/testdata/stmt-error.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ error:
238238
--> SQL:1:6
239239
|
240240
1 | SHOW GRANT FOR ROLE 'role1';
241-
| ^^^^^ unexpected `GRANT`, expecting `GRANTS`, `CREATE`, `NETWORK`, `VIRTUAL`, `CATALOGS`, `STREAMS`, `FUNCTIONS`, `DATABASES`, `CONNECTIONS`, `TABLE_FUNCTIONS`, `DROP`, `TABLE`, `ROLES`, `TASKS`, `INDEXES`, `COLUMNS`, `PASSWORD`, `PROCEDURES`, `PROCESSLIST`, `STAGES`, `TABLES`, `DICTIONARIES`, `ENGINES`, `METRICS`, `SETTINGS`, `LOCKS`, `SCHEMAS`, `FIELDS`, `VIEWS`, `USERS`, `USER`, `FILE`, or `FULL`
241+
| ^^^^^ unexpected `GRANT`, expecting `GRANTS`, `CREATE`, `NETWORK`, `VIRTUAL`, `CATALOGS`, `STREAMS`, `FUNCTIONS`, `DATABASES`, `CONNECTIONS`, `TABLE_FUNCTIONS`, `DROP`, `TABLE`, `ROLES`, `TASKS`, `INDEXES`, `COLUMNS`, `PASSWORD`, `PROCEDURES`, `PROCESSLIST`, `STAGES`, `TABLES`, `DICTIONARIES`, `ENGINES`, `METRICS`, `SETTINGS`, `VARIABLES`, `LOCKS`, `SCHEMAS`, `FIELDS`, `VIEWS`, `USERS`, `USER`, `FILE`, or `FULL`
242242

243243

244244
---------- Input ----------

src/query/ast/tests/it/testdata/stmt.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16199,6 +16199,40 @@ SetStmt {
1619916199
}
1620016200

1620116201

16202+
---------- Input ----------
16203+
show variables
16204+
---------- Output ---------
16205+
SHOW VARIABLES
16206+
---------- AST ------------
16207+
ShowVariables {
16208+
show_options: Some(
16209+
ShowOptions {
16210+
show_limit: None,
16211+
limit: None,
16212+
},
16213+
),
16214+
}
16215+
16216+
16217+
---------- Input ----------
16218+
show variables like 'v%'
16219+
---------- Output ---------
16220+
SHOW VARIABLES LIKE 'v%'
16221+
---------- AST ------------
16222+
ShowVariables {
16223+
show_options: Some(
16224+
ShowOptions {
16225+
show_limit: Some(
16226+
Like {
16227+
pattern: "v%",
16228+
},
16229+
),
16230+
limit: None,
16231+
},
16232+
),
16233+
}
16234+
16235+
1620216236
---------- Input ----------
1620316237
SET variable a = select 3
1620416238
---------- Output ---------

src/query/service/src/interpreters/access/management_mode_access.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl AccessChecker for ManagementModeAccess {
4949
| RewriteKind::ShowColumns(_, _, _)
5050
| RewriteKind::ShowEngines
5151
| RewriteKind::ShowSettings
52+
| RewriteKind::ShowVariables
5253
| RewriteKind::ShowFunctions
5354
| RewriteKind::ShowUserFunctions
5455
| RewriteKind::ShowTableFunctions

src/query/service/src/table_functions/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mod numbers;
2121
mod openai;
2222
mod others;
2323
mod show_grants;
24+
mod show_variables;
2425
mod srf;
2526
mod sync_crash_me;
2627
mod table_function;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2021 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+
mod show_variables_table;
16+
pub use show_variables_table::ShowVariables;

0 commit comments

Comments
 (0)