Skip to content

Commit f7e80aa

Browse files
author
Emil Ejbyfeldt
authored
Merge cd8a7d5 into be77ce5
2 parents be77ce5 + cd8a7d5 commit f7e80aa

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

src/parser/mod.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ use alloc::{
2020
vec,
2121
vec::Vec,
2222
};
23-
use core::fmt;
23+
use core::{
24+
fmt::{self, Display},
25+
str::FromStr,
26+
};
2427

2528
use log::debug;
2629

@@ -3260,6 +3263,18 @@ impl<'a> Parser<'a> {
32603263
}
32613264
}
32623265

3266+
fn parse<T: FromStr>(s: String, loc: Location) -> Result<T, ParserError>
3267+
where
3268+
<T as FromStr>::Err: Display,
3269+
{
3270+
s.parse::<T>().map_err(|e| {
3271+
ParserError::ParserError(format!(
3272+
"Could not parse '{s}' as {}: {e}{loc}",
3273+
std::any::type_name::<T>()
3274+
))
3275+
})
3276+
}
3277+
32633278
/// Parse a comma-separated list of 1+ SelectItem
32643279
pub fn parse_projection(&mut self) -> Result<Vec<SelectItem>, ParserError> {
32653280
// BigQuery and Snowflake allow trailing commas, but only in project lists
@@ -5281,7 +5296,7 @@ impl<'a> Parser<'a> {
52815296
let _ = self.consume_token(&Token::Eq);
52825297
let next_token = self.next_token();
52835298
match next_token.token {
5284-
Token::Number(s, _) => Some(s.parse::<u32>().expect("literal int")),
5299+
Token::Number(s, _) => Some(Self::parse::<u32>(s, next_token.location)?),
52855300
_ => self.expected("literal int", next_token)?,
52865301
}
52875302
} else {
@@ -6725,10 +6740,7 @@ impl<'a> Parser<'a> {
67256740
// The call to n.parse() returns a bigdecimal when the
67266741
// bigdecimal feature is enabled, and is otherwise a no-op
67276742
// (i.e., it returns the input string).
6728-
Token::Number(ref n, l) => match n.parse() {
6729-
Ok(n) => Ok(Value::Number(n, l)),
6730-
Err(e) => parser_err!(format!("Could not parse '{n}' as number: {e}"), location),
6731-
},
6743+
Token::Number(n, l) => Ok(Value::Number(Self::parse(n, location)?, l)),
67326744
Token::SingleQuotedString(ref s) => Ok(Value::SingleQuotedString(s.to_string())),
67336745
Token::DoubleQuotedString(ref s) => Ok(Value::DoubleQuotedString(s.to_string())),
67346746
Token::TripleSingleQuotedString(ref s) => {
@@ -6820,9 +6832,7 @@ impl<'a> Parser<'a> {
68206832
pub fn parse_literal_uint(&mut self) -> Result<u64, ParserError> {
68216833
let next_token = self.next_token();
68226834
match next_token.token {
6823-
Token::Number(s, _) => s.parse::<u64>().map_err(|e| {
6824-
ParserError::ParserError(format!("Could not parse '{s}' as u64: {e}"))
6825-
}),
6835+
Token::Number(s, _) => Self::parse::<u64>(s, next_token.location),
68266836
_ => self.expected("literal int", next_token),
68276837
}
68286838
}
@@ -9282,20 +9292,20 @@ impl<'a> Parser<'a> {
92829292
return self.expected("literal number", next_token);
92839293
};
92849294
self.expect_token(&Token::RBrace)?;
9285-
RepetitionQuantifier::AtMost(n.parse().expect("literal int"))
9295+
RepetitionQuantifier::AtMost(Self::parse(n, token.location)?)
92869296
}
92879297
Token::Number(n, _) if self.consume_token(&Token::Comma) => {
92889298
let next_token = self.next_token();
92899299
match next_token.token {
92909300
Token::Number(m, _) => {
92919301
self.expect_token(&Token::RBrace)?;
92929302
RepetitionQuantifier::Range(
9293-
n.parse().expect("literal int"),
9294-
m.parse().expect("literal int"),
9303+
Self::parse(n, token.location)?,
9304+
Self::parse(m, token.location)?,
92959305
)
92969306
}
92979307
Token::RBrace => {
9298-
RepetitionQuantifier::AtLeast(n.parse().expect("literal int"))
9308+
RepetitionQuantifier::AtLeast(Self::parse(n, token.location)?)
92999309
}
93009310
_ => {
93019311
return self.expected("} or upper bound", next_token);
@@ -9304,7 +9314,7 @@ impl<'a> Parser<'a> {
93049314
}
93059315
Token::Number(n, _) => {
93069316
self.expect_token(&Token::RBrace)?;
9307-
RepetitionQuantifier::Exactly(n.parse().expect("literal int"))
9317+
RepetitionQuantifier::Exactly(Self::parse(n, token.location)?)
93089318
}
93099319
_ => return self.expected("quantifier range", token),
93109320
}
@@ -10326,7 +10336,7 @@ impl<'a> Parser<'a> {
1032610336
} else {
1032710337
let next_token = self.next_token();
1032810338
let quantity = match next_token.token {
10329-
Token::Number(s, _) => s.parse::<u64>().expect("literal int"),
10339+
Token::Number(s, _) => Self::parse::<u64>(s, next_token.location)?,
1033010340
_ => self.expected("literal int", next_token)?,
1033110341
};
1033210342
Some(TopQuantity::Constant(quantity))

tests/sqlparser_common.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9991,3 +9991,18 @@ fn parse_select_wildcard_with_except() {
99919991
"sql parser error: Expected identifier, found: )"
99929992
);
99939993
}
9994+
9995+
#[test]
9996+
fn parse_auto_increment_too_large() {
9997+
let dialect = GenericDialect {};
9998+
let u64_max = u64::MAX;
9999+
let sql =
10000+
format!("CREATE TABLE foo (bar INT NOT NULL AUTO_INCREMENT) AUTO_INCREMENT=1{u64_max}");
10001+
10002+
let res = Parser::new(&dialect)
10003+
.try_with_sql(&sql)
10004+
.expect("tokenize to work")
10005+
.parse_statements();
10006+
10007+
assert!(res.is_err(), "{res:?}");
10008+
}

0 commit comments

Comments
 (0)