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: Support multiple values in SET #36

Merged
merged 1 commit into from
Aug 24, 2023
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
5 changes: 3 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1779,12 +1779,13 @@ impl fmt::Display for Statement {
.iter()
.map(|key_value| {
format!(
"{}{}",
"{}{} = {}",
if key_value.local { "LOCAL " } else { "" },
key_value.key,
key_value
.value
.iter()
.map(|value| format!("{} = {}", key_value.key, value))
.map(|value| format!("{}", value))
.collect::<Vec<String>>()
.join(", ")
)
Expand Down
59 changes: 46 additions & 13 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3684,11 +3684,16 @@ impl<'a> Parser<'a> {
}

let mut key_values: Vec<SetVariableKeyValue> = vec![];
loop {

if dialect_of!(self is PostgreSqlDialect | RedshiftSqlDialect) {
let variable = self.parse_identifier()?;
let mut values = vec![];

if self.consume_token(&Token::Eq) || self.parse_keyword(Keyword::TO) {
if !self.consume_token(&Token::Eq) && !self.parse_keyword(Keyword::TO) {
return self.expected("equals sign or TO", self.peek_token());
}

loop {
let value = if let Ok(expr) = self.parse_expr() {
expr
} else {
Expand All @@ -3697,22 +3702,50 @@ impl<'a> Parser<'a> {

values.push(value);

key_values.push(SetVariableKeyValue {
key: variable,
value: values,
local: modifier == Some(Keyword::LOCAL),
hivevar: false,
});

if self.consume_token(&Token::Comma) {
continue;
if !self.consume_token(&Token::Comma) {
break;
}
}

return Ok(Statement::SetVariable { key_values });
} else {
key_values.push(SetVariableKeyValue {
key: variable,
value: values,
local: modifier == Some(Keyword::LOCAL),
hivevar: false,
});

return Ok(Statement::SetVariable { key_values });
}

loop {
let variable = self.parse_identifier()?;
let mut values = vec![];

if !self.consume_token(&Token::Eq) && !self.parse_keyword(Keyword::TO) {
return self.expected("equals sign or TO", self.peek_token());
}

let value = if let Ok(expr) = self.parse_expr() {
expr
} else {
self.expected("variable value", self.peek_token())?
};

values.push(value);

key_values.push(SetVariableKeyValue {
key: variable,
value: values,
local: modifier == Some(Keyword::LOCAL),
hivevar: false,
});

if !self.consume_token(&Token::Comma) {
break;
}
}

Ok(Statement::SetVariable { key_values })
}

pub fn parse_show(&mut self) -> Result<Statement, ParserError> {
Expand Down
23 changes: 23 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,29 @@ fn parse_set() {
}
);

let stmt = pg().verified_stmt("SET a = b, c");
assert_eq!(
stmt,
Statement::SetVariable {
key_values: [SetVariableKeyValue {
key: "a".into(),
value: vec![
Expr::Identifier(Ident {
value: "b".into(),
quote_style: None
}),
Expr::Identifier(Ident {
value: "c".into(),
quote_style: None
}),
],
local: false,
hivevar: false,
}]
.to_vec()
}
);

let stmt = pg_and_generic().verified_stmt("SET a = 'b'");
assert_eq!(
stmt,
Expand Down
Loading