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): Settings clause #16669

Merged
merged 6 commits into from
Oct 30, 2024
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
1 change: 1 addition & 0 deletions src/query/ast/src/ast/statements/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub enum SetType {
SettingsSession,
SettingsGlobal,
Variable,
SettingsQuery,
}

#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
Expand Down
1 change: 1 addition & 0 deletions src/query/ast/src/ast/statements/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl Display for Settings {
SetType::SettingsGlobal => write!(f, "GLOBAL ")?,
SetType::SettingsSession => write!(f, "SESSION ")?,
SetType::Variable => write!(f, "VARIABLE ")?,
SetType::SettingsQuery => write!(f, "")?,
}

if self.identifiers.len() > 1 {
Expand Down
23 changes: 22 additions & 1 deletion src/query/ast/src/ast/statements/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use crate::ast::statements::connection::CreateConnectionStmt;
use crate::ast::statements::pipe::CreatePipeStmt;
use crate::ast::statements::settings::Settings;
use crate::ast::statements::task::CreateTaskStmt;
use crate::ast::write_comma_separated_list;
use crate::ast::CreateOption;
use crate::ast::Identifier;
use crate::ast::Query;
Expand All @@ -38,6 +39,10 @@ use crate::ast::Query;
#[derive(Debug, Clone, PartialEq, Drive, DriveMut)]
pub enum Statement {
Query(Box<Query>),
StatementWithSettings {
settings: Option<Settings>,
stmt: Box<Statement>,
},
Explain {
kind: ExplainKind,
options: Vec<ExplainOption>,
Expand Down Expand Up @@ -89,7 +94,6 @@ pub enum Statement {
SetStmt {
settings: Settings,
},

UnSetStmt {
settings: Settings,
},
Expand Down Expand Up @@ -414,6 +418,23 @@ impl Display for Statement {
}
write!(f, " {query}")?;
}
Statement::StatementWithSettings { settings, stmt } => {
if let Some(setting) = settings {
write!(f, "SETTINGS (")?;
let ids = &setting.identifiers;
if let SetValues::Expr(values) = &setting.values {
let mut expr = Vec::with_capacity(ids.len());
for (id, value) in ids.iter().zip(values.iter()) {
expr.push(format!("{} = {}", id, value));
}
write_comma_separated_list(f, expr)?;
} else {
unreachable!();
}
write!(f, ") ")?;
}
write!(f, "{stmt}")?;
}
Statement::ExplainAnalyze {
partial,
graphical,
Expand Down
43 changes: 43 additions & 0 deletions src/query/ast/src/parser/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
})
},
);

let query_setting = map_res(
rule! {
SETTINGS ~ #query_statement_setting? ~ #statement_body
},
|(_, opt_settings, statement)| {
Ok(Statement::StatementWithSettings {
settings: opt_settings,
stmt: Box::new(statement),
})
},
);
let explain_analyze = map(
rule! {
EXPLAIN ~ ANALYZE ~ (PARTIAL|GRAPHICAL)? ~ #statement
Expand Down Expand Up @@ -2375,6 +2387,7 @@ pub fn statement_body(i: Input) -> IResult<Statement> {
rule!(
#set_stmt : "`SET [variable] {<name> = <value> | (<name>, ...) = (<value>, ...)}`"
| #unset_stmt : "`UNSET [variable] {<name> | (<name>, ...)}`"
| #query_setting : "SETTINGS ( {<name> = <value> | (<name>, ...) = (<value>, ...)} ) Statement"
),
// catalog
rule!(
Expand Down Expand Up @@ -2782,6 +2795,36 @@ pub fn hint(i: Input) -> IResult<Hint> {
rule!(#hint|#invalid_hint)(i)
}

pub fn query_setting(i: Input) -> IResult<(Identifier, Expr)> {
map(
rule! {
#ident ~ "=" ~ #subexpr(0)
},
|(id, _, value)| (id, value),
)(i)
}

pub fn query_statement_setting(i: Input) -> IResult<Settings> {
let query_set = map(
rule! {
"(" ~ #comma_separated_list0(query_setting) ~ ")"
},
|(_, query_setting, _)| {
let mut ids = Vec::with_capacity(query_setting.len());
let mut values = Vec::with_capacity(query_setting.len());
for (id, value) in query_setting {
ids.push(id);
values.push(value);
}
Settings {
set_type: SetType::SettingsQuery,
identifiers: ids,
values: SetValues::Expr(values.into_iter().map(|x| x.into()).collect()),
}
},
);
rule!(#query_set: "(SETTING_NAME = VALUE, ...)")(i)
}
pub fn top_n(i: Input) -> IResult<u64> {
map(
rule! {
Expand Down
2 changes: 2 additions & 0 deletions src/query/ast/tests/it/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@ fn test_statement() {
r#"select * from a semi join b on a.a = b.a;"#,
r#"select * from a left anti join b on a.a = b.a;"#,
r#"select * from a anti join b on a.a = b.a;"#,
r#"SETTINGS (max_thread=1, timezone='Asia/Shanghai') select 1;"#,
r#"SETTINGS (max_thread=1) select * from a anti join b on a.a = b.a;"#,
r#"select * from a right semi join b on a.a = b.a;"#,
r#"select * from a right anti join b on a.a = b.a;"#,
r#"select * from a full outer join b on a.a = b.a;"#,
Expand Down
Loading
Loading