set: allow negative ident values (#495)

Signed-off-by: Maciej Obuchowski <obuchowski.maciej@gmail.com>
This commit is contained in:
Maciej Obuchowski 2022-05-22 21:33:07 +02:00 committed by GitHub
parent 85e0e5fd39
commit 74f92079ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 0 deletions

View file

@ -3287,6 +3287,16 @@ impl<'a> Parser<'a> {
let value = match (self.parse_value(), token) {
(Ok(value), _) => SetVariableValue::Literal(value),
(Err(_), Token::Word(ident)) => SetVariableValue::Ident(ident.to_ident()),
(Err(_), Token::Minus) => {
let next_token = self.next_token();
match next_token {
Token::Word(ident) => SetVariableValue::Ident(Ident {
quote_style: ident.quote_style,
value: format!("-{}", ident.value),
}),
_ => self.expected("word", next_token)?,
}
}
(Err(_), unexpected) => self.expected("variable value", unexpected)?,
};
values.push(value);

View file

@ -15,7 +15,9 @@
//! Test SQL syntax specific to Hive. The parser based on the generic dialect
//! is also tested (on the inputs it can handle).
use sqlparser::ast::{Ident, ObjectName, SetVariableValue, Statement};
use sqlparser::dialect::HiveDialect;
use sqlparser::parser::ParserError;
use sqlparser::test_utils::*;
#[test]
@ -205,6 +207,31 @@ fn from_cte() {
println!("{}", hive().verified_stmt(rename));
}
#[test]
fn set_statement_with_minus() {
assert_eq!(
hive().verified_stmt("SET hive.tez.java.opts = -Xmx4g"),
Statement::SetVariable {
local: false,
hivevar: false,
variable: ObjectName(vec![
Ident::new("hive"),
Ident::new("tez"),
Ident::new("java"),
Ident::new("opts")
]),
value: vec![SetVariableValue::Ident("-Xmx4g".into())],
}
);
assert_eq!(
hive().parse_sql_statements("SET hive.tez.java.opts = -"),
Err(ParserError::ParserError(
"Expected word, found: EOF".to_string()
))
)
}
fn hive() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(HiveDialect {})],