mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-09-22 13:42:31 +00:00
Fix parsing of negative values (#1419)
Co-authored-by: Agaev Huseyn <h.agaev@vkteam.ru>
This commit is contained in:
parent
b9f6784714
commit
246838a69f
3 changed files with 60 additions and 25 deletions
|
@ -7411,6 +7411,7 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parse an unsigned numeric literal
|
||||||
pub fn parse_number_value(&mut self) -> Result<Value, ParserError> {
|
pub fn parse_number_value(&mut self) -> Result<Value, ParserError> {
|
||||||
match self.parse_value()? {
|
match self.parse_value()? {
|
||||||
v @ Value::Number(_, _) => Ok(v),
|
v @ Value::Number(_, _) => Ok(v),
|
||||||
|
@ -7422,6 +7423,26 @@ impl<'a> Parser<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Parse a numeric literal as an expression. Returns a [`Expr::UnaryOp`] if the number is signed,
|
||||||
|
/// otherwise returns a [`Expr::Value`]
|
||||||
|
pub fn parse_number(&mut self) -> Result<Expr, ParserError> {
|
||||||
|
let next_token = self.next_token();
|
||||||
|
match next_token.token {
|
||||||
|
Token::Plus => Ok(Expr::UnaryOp {
|
||||||
|
op: UnaryOperator::Plus,
|
||||||
|
expr: Box::new(Expr::Value(self.parse_number_value()?)),
|
||||||
|
}),
|
||||||
|
Token::Minus => Ok(Expr::UnaryOp {
|
||||||
|
op: UnaryOperator::Minus,
|
||||||
|
expr: Box::new(Expr::Value(self.parse_number_value()?)),
|
||||||
|
}),
|
||||||
|
_ => {
|
||||||
|
self.prev_token();
|
||||||
|
Ok(Expr::Value(self.parse_number_value()?))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn parse_introduced_string_value(&mut self) -> Result<Value, ParserError> {
|
fn parse_introduced_string_value(&mut self) -> Result<Value, ParserError> {
|
||||||
let next_token = self.next_token();
|
let next_token = self.next_token();
|
||||||
let location = next_token.location;
|
let location = next_token.location;
|
||||||
|
@ -11741,30 +11762,20 @@ impl<'a> Parser<'a> {
|
||||||
//[ INCREMENT [ BY ] increment ]
|
//[ INCREMENT [ BY ] increment ]
|
||||||
if self.parse_keywords(&[Keyword::INCREMENT]) {
|
if self.parse_keywords(&[Keyword::INCREMENT]) {
|
||||||
if self.parse_keywords(&[Keyword::BY]) {
|
if self.parse_keywords(&[Keyword::BY]) {
|
||||||
sequence_options.push(SequenceOptions::IncrementBy(
|
sequence_options.push(SequenceOptions::IncrementBy(self.parse_number()?, true));
|
||||||
Expr::Value(self.parse_number_value()?),
|
|
||||||
true,
|
|
||||||
));
|
|
||||||
} else {
|
} else {
|
||||||
sequence_options.push(SequenceOptions::IncrementBy(
|
sequence_options.push(SequenceOptions::IncrementBy(self.parse_number()?, false));
|
||||||
Expr::Value(self.parse_number_value()?),
|
|
||||||
false,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//[ MINVALUE minvalue | NO MINVALUE ]
|
//[ MINVALUE minvalue | NO MINVALUE ]
|
||||||
if self.parse_keyword(Keyword::MINVALUE) {
|
if self.parse_keyword(Keyword::MINVALUE) {
|
||||||
sequence_options.push(SequenceOptions::MinValue(Some(Expr::Value(
|
sequence_options.push(SequenceOptions::MinValue(Some(self.parse_number()?)));
|
||||||
self.parse_number_value()?,
|
|
||||||
))));
|
|
||||||
} else if self.parse_keywords(&[Keyword::NO, Keyword::MINVALUE]) {
|
} else if self.parse_keywords(&[Keyword::NO, Keyword::MINVALUE]) {
|
||||||
sequence_options.push(SequenceOptions::MinValue(None));
|
sequence_options.push(SequenceOptions::MinValue(None));
|
||||||
}
|
}
|
||||||
//[ MAXVALUE maxvalue | NO MAXVALUE ]
|
//[ MAXVALUE maxvalue | NO MAXVALUE ]
|
||||||
if self.parse_keywords(&[Keyword::MAXVALUE]) {
|
if self.parse_keywords(&[Keyword::MAXVALUE]) {
|
||||||
sequence_options.push(SequenceOptions::MaxValue(Some(Expr::Value(
|
sequence_options.push(SequenceOptions::MaxValue(Some(self.parse_number()?)));
|
||||||
self.parse_number_value()?,
|
|
||||||
))));
|
|
||||||
} else if self.parse_keywords(&[Keyword::NO, Keyword::MAXVALUE]) {
|
} else if self.parse_keywords(&[Keyword::NO, Keyword::MAXVALUE]) {
|
||||||
sequence_options.push(SequenceOptions::MaxValue(None));
|
sequence_options.push(SequenceOptions::MaxValue(None));
|
||||||
}
|
}
|
||||||
|
@ -11772,22 +11783,14 @@ impl<'a> Parser<'a> {
|
||||||
//[ START [ WITH ] start ]
|
//[ START [ WITH ] start ]
|
||||||
if self.parse_keywords(&[Keyword::START]) {
|
if self.parse_keywords(&[Keyword::START]) {
|
||||||
if self.parse_keywords(&[Keyword::WITH]) {
|
if self.parse_keywords(&[Keyword::WITH]) {
|
||||||
sequence_options.push(SequenceOptions::StartWith(
|
sequence_options.push(SequenceOptions::StartWith(self.parse_number()?, true));
|
||||||
Expr::Value(self.parse_number_value()?),
|
|
||||||
true,
|
|
||||||
));
|
|
||||||
} else {
|
} else {
|
||||||
sequence_options.push(SequenceOptions::StartWith(
|
sequence_options.push(SequenceOptions::StartWith(self.parse_number()?, false));
|
||||||
Expr::Value(self.parse_number_value()?),
|
|
||||||
false,
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//[ CACHE cache ]
|
//[ CACHE cache ]
|
||||||
if self.parse_keywords(&[Keyword::CACHE]) {
|
if self.parse_keywords(&[Keyword::CACHE]) {
|
||||||
sequence_options.push(SequenceOptions::Cache(Expr::Value(
|
sequence_options.push(SequenceOptions::Cache(self.parse_number()?));
|
||||||
self.parse_number_value()?,
|
|
||||||
)));
|
|
||||||
}
|
}
|
||||||
// [ [ NO ] CYCLE ]
|
// [ [ NO ] CYCLE ]
|
||||||
if self.parse_keywords(&[Keyword::NO, Keyword::CYCLE]) {
|
if self.parse_keywords(&[Keyword::NO, Keyword::CYCLE]) {
|
||||||
|
|
|
@ -2819,6 +2819,18 @@ fn parse_window_function_null_treatment_arg() {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn parse_negative_value() {
|
||||||
|
let sql1 = "SELECT -1";
|
||||||
|
one_statement_parses_to(sql1, "SELECT -1");
|
||||||
|
|
||||||
|
let sql2 = "CREATE SEQUENCE name INCREMENT -10 MINVALUE -1000 MAXVALUE 15 START -100;";
|
||||||
|
one_statement_parses_to(
|
||||||
|
sql2,
|
||||||
|
"CREATE SEQUENCE name INCREMENT -10 MINVALUE -1000 MAXVALUE 15 START -100",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parse_create_table() {
|
fn parse_create_table() {
|
||||||
let sql = "CREATE TABLE uk_cities (\
|
let sql = "CREATE TABLE uk_cities (\
|
||||||
|
|
|
@ -277,6 +277,26 @@ fn parse_create_sequence() {
|
||||||
"CREATE TEMPORARY SEQUENCE IF NOT EXISTS name3 INCREMENT 1 NO MINVALUE MAXVALUE 20 OWNED BY NONE",
|
"CREATE TEMPORARY SEQUENCE IF NOT EXISTS name3 INCREMENT 1 NO MINVALUE MAXVALUE 20 OWNED BY NONE",
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let sql7 = "CREATE SEQUENCE name4
|
||||||
|
AS BIGINT
|
||||||
|
INCREMENT -15
|
||||||
|
MINVALUE - 2000 MAXVALUE -50
|
||||||
|
START WITH - 60";
|
||||||
|
pg().one_statement_parses_to(
|
||||||
|
sql7,
|
||||||
|
"CREATE SEQUENCE name4 AS BIGINT INCREMENT -15 MINVALUE -2000 MAXVALUE -50 START WITH -60",
|
||||||
|
);
|
||||||
|
|
||||||
|
let sql8 = "CREATE SEQUENCE name5
|
||||||
|
AS BIGINT
|
||||||
|
INCREMENT +10
|
||||||
|
MINVALUE + 30 MAXVALUE +5000
|
||||||
|
START WITH + 45";
|
||||||
|
pg().one_statement_parses_to(
|
||||||
|
sql8,
|
||||||
|
"CREATE SEQUENCE name5 AS BIGINT INCREMENT +10 MINVALUE +30 MAXVALUE +5000 START WITH +45",
|
||||||
|
);
|
||||||
|
|
||||||
assert!(matches!(
|
assert!(matches!(
|
||||||
pg().parse_sql_statements("CREATE SEQUENCE foo INCREMENT 1 NO MINVALUE NO"),
|
pg().parse_sql_statements("CREATE SEQUENCE foo INCREMENT 1 NO MINVALUE NO"),
|
||||||
Err(ParserError::ParserError(_))
|
Err(ParserError::ParserError(_))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue