Skip to content

Commit 79af31b

Browse files
author
Emil Ejbyfeldt
authored
Return errors, not panic, when integers fail to parse in AUTO_INCREMENT and TOP (#1305)
1 parent 345e209 commit 79af31b

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

src/parser/mod.rs

+25-15
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+
core::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
}
@@ -9273,20 +9283,20 @@ impl<'a> Parser<'a> {
92739283
return self.expected("literal number", next_token);
92749284
};
92759285
self.expect_token(&Token::RBrace)?;
9276-
RepetitionQuantifier::AtMost(n.parse().expect("literal int"))
9286+
RepetitionQuantifier::AtMost(Self::parse(n, token.location)?)
92779287
}
92789288
Token::Number(n, _) if self.consume_token(&Token::Comma) => {
92799289
let next_token = self.next_token();
92809290
match next_token.token {
92819291
Token::Number(m, _) => {
92829292
self.expect_token(&Token::RBrace)?;
92839293
RepetitionQuantifier::Range(
9284-
n.parse().expect("literal int"),
9285-
m.parse().expect("literal int"),
9294+
Self::parse(n, token.location)?,
9295+
Self::parse(m, token.location)?,
92869296
)
92879297
}
92889298
Token::RBrace => {
9289-
RepetitionQuantifier::AtLeast(n.parse().expect("literal int"))
9299+
RepetitionQuantifier::AtLeast(Self::parse(n, token.location)?)
92909300
}
92919301
_ => {
92929302
return self.expected("} or upper bound", next_token);
@@ -9295,7 +9305,7 @@ impl<'a> Parser<'a> {
92959305
}
92969306
Token::Number(n, _) => {
92979307
self.expect_token(&Token::RBrace)?;
9298-
RepetitionQuantifier::Exactly(n.parse().expect("literal int"))
9308+
RepetitionQuantifier::Exactly(Self::parse(n, token.location)?)
92999309
}
93009310
_ => return self.expected("quantifier range", token),
93019311
}
@@ -10329,7 +10339,7 @@ impl<'a> Parser<'a> {
1032910339
} else {
1033010340
let next_token = self.next_token();
1033110341
let quantity = match next_token.token {
10332-
Token::Number(s, _) => s.parse::<u64>().expect("literal int"),
10342+
Token::Number(s, _) => Self::parse::<u64>(s, next_token.location)?,
1033310343
_ => self.expected("literal int", next_token)?,
1033410344
};
1033510345
Some(TopQuantity::Constant(quantity))

tests/sqlparser_common.rs

+15
Original file line numberDiff line numberDiff line change
@@ -10006,3 +10006,18 @@ fn parse_select_wildcard_with_except() {
1000610006
"sql parser error: Expected identifier, found: )"
1000710007
);
1000810008
}
10009+
10010+
#[test]
10011+
fn parse_auto_increment_too_large() {
10012+
let dialect = GenericDialect {};
10013+
let u64_max = u64::MAX;
10014+
let sql =
10015+
format!("CREATE TABLE foo (bar INT NOT NULL AUTO_INCREMENT) AUTO_INCREMENT=1{u64_max}");
10016+
10017+
let res = Parser::new(&dialect)
10018+
.try_with_sql(&sql)
10019+
.expect("tokenize to work")
10020+
.parse_statements();
10021+
10022+
assert!(res.is_err(), "{res:?}");
10023+
}

0 commit comments

Comments
 (0)