Skip to content

Commit bf1412a

Browse files
author
Agaev Huseyn
committed
Fix parsing of negative values
1 parent accee6d commit bf1412a

File tree

3 files changed

+57
-54
lines changed

3 files changed

+57
-54
lines changed

src/parser/mod.rs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7274,22 +7274,6 @@ impl<'a> Parser<'a> {
72747274
let placeholder = tok.to_string() + &ident.value;
72757275
Ok(Value::Placeholder(placeholder))
72767276
}
7277-
tok @ Token::Minus | tok @ Token::Plus => {
7278-
let next_token = self.next_token();
7279-
match next_token.token {
7280-
Token::Number(n, l) => {
7281-
if tok == Token::Minus {
7282-
Ok(Value::Number(
7283-
Self::parse(tok.to_string() + &n, location)?,
7284-
l,
7285-
))
7286-
} else {
7287-
Ok(Value::Number(Self::parse(n, location)?, l))
7288-
}
7289-
}
7290-
_ => self.expected("number", next_token),
7291-
}
7292-
}
72937277
unexpected => self.expected(
72947278
"a value",
72957279
TokenWithLocation {
@@ -7300,6 +7284,7 @@ impl<'a> Parser<'a> {
73007284
}
73017285
}
73027286

7287+
/// Parse an unsigned numeric literal
73037288
pub fn parse_number_value(&mut self) -> Result<Value, ParserError> {
73047289
match self.parse_value()? {
73057290
v @ Value::Number(_, _) => Ok(v),
@@ -7311,6 +7296,26 @@ impl<'a> Parser<'a> {
73117296
}
73127297
}
73137298

7299+
/// Parse a numeric literal and return Expr::UnaryOp if the number is signed,
7300+
/// otherwise return Expr::Value
7301+
pub fn parse_number_value_with_sign(&mut self) -> Result<Expr, ParserError> {
7302+
let next_token = self.next_token();
7303+
match next_token.token {
7304+
Token::Plus => Ok(Expr::UnaryOp {
7305+
op: UnaryOperator::Plus,
7306+
expr: Box::new(Expr::Value(self.parse_number_value()?)),
7307+
}),
7308+
Token::Minus => Ok(Expr::UnaryOp {
7309+
op: UnaryOperator::Minus,
7310+
expr: Box::new(Expr::Value(self.parse_number_value()?)),
7311+
}),
7312+
_ => {
7313+
self.prev_token();
7314+
Ok(Expr::Value(self.parse_number_value()?))
7315+
}
7316+
}
7317+
}
7318+
73147319
fn parse_introduced_string_value(&mut self) -> Result<Value, ParserError> {
73157320
let next_token = self.next_token();
73167321
let location = next_token.location;
@@ -11625,29 +11630,29 @@ impl<'a> Parser<'a> {
1162511630
if self.parse_keywords(&[Keyword::INCREMENT]) {
1162611631
if self.parse_keywords(&[Keyword::BY]) {
1162711632
sequence_options.push(SequenceOptions::IncrementBy(
11628-
Expr::Value(self.parse_number_value()?),
11633+
self.parse_number_value_with_sign()?,
1162911634
true,
1163011635
));
1163111636
} else {
1163211637
sequence_options.push(SequenceOptions::IncrementBy(
11633-
Expr::Value(self.parse_number_value()?),
11638+
self.parse_number_value_with_sign()?,
1163411639
false,
1163511640
));
1163611641
}
1163711642
}
1163811643
//[ MINVALUE minvalue | NO MINVALUE ]
1163911644
if self.parse_keyword(Keyword::MINVALUE) {
11640-
sequence_options.push(SequenceOptions::MinValue(Some(Expr::Value(
11641-
self.parse_number_value()?,
11642-
))));
11645+
sequence_options.push(SequenceOptions::MinValue(Some(
11646+
self.parse_number_value_with_sign()?,
11647+
)));
1164311648
} else if self.parse_keywords(&[Keyword::NO, Keyword::MINVALUE]) {
1164411649
sequence_options.push(SequenceOptions::MinValue(None));
1164511650
}
1164611651
//[ MAXVALUE maxvalue | NO MAXVALUE ]
1164711652
if self.parse_keywords(&[Keyword::MAXVALUE]) {
11648-
sequence_options.push(SequenceOptions::MaxValue(Some(Expr::Value(
11649-
self.parse_number_value()?,
11650-
))));
11653+
sequence_options.push(SequenceOptions::MaxValue(Some(
11654+
self.parse_number_value_with_sign()?,
11655+
)));
1165111656
} else if self.parse_keywords(&[Keyword::NO, Keyword::MAXVALUE]) {
1165211657
sequence_options.push(SequenceOptions::MaxValue(None));
1165311658
}
@@ -11656,21 +11661,19 @@ impl<'a> Parser<'a> {
1165611661
if self.parse_keywords(&[Keyword::START]) {
1165711662
if self.parse_keywords(&[Keyword::WITH]) {
1165811663
sequence_options.push(SequenceOptions::StartWith(
11659-
Expr::Value(self.parse_number_value()?),
11664+
self.parse_number_value_with_sign()?,
1166011665
true,
1166111666
));
1166211667
} else {
1166311668
sequence_options.push(SequenceOptions::StartWith(
11664-
Expr::Value(self.parse_number_value()?),
11669+
self.parse_number_value_with_sign()?,
1166511670
false,
1166611671
));
1166711672
}
1166811673
}
1166911674
//[ CACHE cache ]
1167011675
if self.parse_keywords(&[Keyword::CACHE]) {
11671-
sequence_options.push(SequenceOptions::Cache(Expr::Value(
11672-
self.parse_number_value()?,
11673-
)));
11676+
sequence_options.push(SequenceOptions::Cache(self.parse_number_value_with_sign()?));
1167411677
}
1167511678
// [ [ NO ] CYCLE ]
1167611679
if self.parse_keywords(&[Keyword::NO, Keyword::CYCLE]) {

tests/sqlparser_common.rs

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2820,35 +2820,15 @@ fn parse_window_function_null_treatment_arg() {
28202820
}
28212821

28222822
#[test]
2823-
fn parse_signed_value() {
2824-
let sql1 = "CREATE SEQUENCE name1
2825-
AS BIGINT
2826-
INCREMENT -15
2827-
MINVALUE - 2000 MAXVALUE -50
2828-
START WITH - 60";
2829-
one_statement_parses_to(
2830-
sql1,
2831-
"CREATE SEQUENCE name1 AS BIGINT INCREMENT -15 MINVALUE -2000 MAXVALUE -50 START WITH -60",
2832-
);
2823+
fn parse_negative_value() {
2824+
let sql1 = "SELECT -1";
2825+
one_statement_parses_to(sql1, "SELECT -1");
28332826

2834-
let sql2 = "CREATE SEQUENCE name2
2835-
AS BIGINT
2836-
INCREMENT +10
2837-
MINVALUE + 30 MAXVALUE +5000
2838-
START WITH + 45";
2827+
let sql2 = "CREATE SEQUENCE name INCREMENT -10 MINVALUE -1000 MAXVALUE 15 START -100;";
28392828
one_statement_parses_to(
28402829
sql2,
2841-
"CREATE SEQUENCE name2 AS BIGINT INCREMENT 10 MINVALUE 30 MAXVALUE 5000 START WITH 45",
2842-
);
2843-
2844-
let sql3 = "CREATE SEQUENCE name3 INCREMENT -10 MINVALUE -1000 MAXVALUE 1 START -100;";
2845-
one_statement_parses_to(
2846-
sql3,
2847-
"CREATE SEQUENCE name3 INCREMENT -10 MINVALUE -1000 MAXVALUE 1 START -100",
2830+
"CREATE SEQUENCE name INCREMENT -10 MINVALUE -1000 MAXVALUE 15 START -100",
28482831
);
2849-
2850-
let sql4 = "SELECT -1";
2851-
one_statement_parses_to(sql4, "SELECT -1");
28522832
}
28532833

28542834
#[test]

tests/sqlparser_postgres.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,26 @@ fn parse_create_sequence() {
277277
"CREATE TEMPORARY SEQUENCE IF NOT EXISTS name3 INCREMENT 1 NO MINVALUE MAXVALUE 20 OWNED BY NONE",
278278
);
279279

280+
let sql7 = "CREATE SEQUENCE name4
281+
AS BIGINT
282+
INCREMENT -15
283+
MINVALUE - 2000 MAXVALUE -50
284+
START WITH - 60";
285+
pg().one_statement_parses_to(
286+
sql7,
287+
"CREATE SEQUENCE name4 AS BIGINT INCREMENT -15 MINVALUE -2000 MAXVALUE -50 START WITH -60",
288+
);
289+
290+
let sql8 = "CREATE SEQUENCE name5
291+
AS BIGINT
292+
INCREMENT +10
293+
MINVALUE + 30 MAXVALUE +5000
294+
START WITH + 45";
295+
pg().one_statement_parses_to(
296+
sql8,
297+
"CREATE SEQUENCE name5 AS BIGINT INCREMENT +10 MINVALUE +30 MAXVALUE +5000 START WITH +45",
298+
);
299+
280300
assert!(matches!(
281301
pg().parse_sql_statements("CREATE SEQUENCE foo INCREMENT 1 NO MINVALUE NO"),
282302
Err(ParserError::ParserError(_))

0 commit comments

Comments
 (0)