Skip to content

Commit 62e12f4

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

File tree

3 files changed

+59
-54
lines changed

3 files changed

+59
-54
lines changed

src/parser/mod.rs

+34-29
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 {
@@ -7311,6 +7295,29 @@ impl<'a> Parser<'a> {
73117295
}
73127296
}
73137297

7298+
pub fn parse_number_value_with_sign(&mut self) -> Result<Expr, ParserError> {
7299+
let next_token = self.next_token();
7300+
match next_token.token {
7301+
tok @ Token::Minus | tok @ Token::Plus => {
7302+
if tok == Token::Plus {
7303+
Ok(Expr::UnaryOp {
7304+
op: UnaryOperator::Plus,
7305+
expr: Box::new(Expr::Value(self.parse_number_value()?)),
7306+
})
7307+
} else {
7308+
Ok(Expr::UnaryOp {
7309+
op: UnaryOperator::Minus,
7310+
expr: Box::new(Expr::Value(self.parse_number_value()?)),
7311+
})
7312+
}
7313+
}
7314+
_ => {
7315+
self.prev_token();
7316+
Ok(Expr::Value(self.parse_number_value()?))
7317+
}
7318+
}
7319+
}
7320+
73147321
fn parse_introduced_string_value(&mut self) -> Result<Value, ParserError> {
73157322
let next_token = self.next_token();
73167323
let location = next_token.location;
@@ -11625,29 +11632,29 @@ impl<'a> Parser<'a> {
1162511632
if self.parse_keywords(&[Keyword::INCREMENT]) {
1162611633
if self.parse_keywords(&[Keyword::BY]) {
1162711634
sequence_options.push(SequenceOptions::IncrementBy(
11628-
Expr::Value(self.parse_number_value()?),
11635+
self.parse_number_value_with_sign()?,
1162911636
true,
1163011637
));
1163111638
} else {
1163211639
sequence_options.push(SequenceOptions::IncrementBy(
11633-
Expr::Value(self.parse_number_value()?),
11640+
self.parse_number_value_with_sign()?,
1163411641
false,
1163511642
));
1163611643
}
1163711644
}
1163811645
//[ MINVALUE minvalue | NO MINVALUE ]
1163911646
if self.parse_keyword(Keyword::MINVALUE) {
11640-
sequence_options.push(SequenceOptions::MinValue(Some(Expr::Value(
11641-
self.parse_number_value()?,
11642-
))));
11647+
sequence_options.push(SequenceOptions::MinValue(Some(
11648+
self.parse_number_value_with_sign()?,
11649+
)));
1164311650
} else if self.parse_keywords(&[Keyword::NO, Keyword::MINVALUE]) {
1164411651
sequence_options.push(SequenceOptions::MinValue(None));
1164511652
}
1164611653
//[ MAXVALUE maxvalue | NO MAXVALUE ]
1164711654
if self.parse_keywords(&[Keyword::MAXVALUE]) {
11648-
sequence_options.push(SequenceOptions::MaxValue(Some(Expr::Value(
11649-
self.parse_number_value()?,
11650-
))));
11655+
sequence_options.push(SequenceOptions::MaxValue(Some(
11656+
self.parse_number_value_with_sign()?,
11657+
)));
1165111658
} else if self.parse_keywords(&[Keyword::NO, Keyword::MAXVALUE]) {
1165211659
sequence_options.push(SequenceOptions::MaxValue(None));
1165311660
}
@@ -11656,21 +11663,19 @@ impl<'a> Parser<'a> {
1165611663
if self.parse_keywords(&[Keyword::START]) {
1165711664
if self.parse_keywords(&[Keyword::WITH]) {
1165811665
sequence_options.push(SequenceOptions::StartWith(
11659-
Expr::Value(self.parse_number_value()?),
11666+
self.parse_number_value_with_sign()?,
1166011667
true,
1166111668
));
1166211669
} else {
1166311670
sequence_options.push(SequenceOptions::StartWith(
11664-
Expr::Value(self.parse_number_value()?),
11671+
self.parse_number_value_with_sign()?,
1166511672
false,
1166611673
));
1166711674
}
1166811675
}
1166911676
//[ CACHE cache ]
1167011677
if self.parse_keywords(&[Keyword::CACHE]) {
11671-
sequence_options.push(SequenceOptions::Cache(Expr::Value(
11672-
self.parse_number_value()?,
11673-
)));
11678+
sequence_options.push(SequenceOptions::Cache(self.parse_number_value_with_sign()?));
1167411679
}
1167511680
// [ [ NO ] CYCLE ]
1167611681
if self.parse_keywords(&[Keyword::NO, Keyword::CYCLE]) {

tests/sqlparser_common.rs

+5-25
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

+20
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)