mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-09-01 03:37:21 +00:00
Optionally parse numbers into BigDecimals
With `--features bigdecimal`, parse numbers into BigDecimals instead of leaving them as strings.
This commit is contained in:
parent
b5621c0fe8
commit
a0aca824e8
7 changed files with 95 additions and 57 deletions
|
@ -1183,7 +1183,13 @@ impl Parser {
|
|||
return parser_err!(format!("No value parser for keyword {}", k.keyword));
|
||||
}
|
||||
},
|
||||
Token::Number(ref n) => Ok(Value::Number(n.to_string())),
|
||||
// The call to n.parse() returns a bigdecimal when the
|
||||
// bigdecimal feature is enabled, and is otherwise a no-op
|
||||
// (i.e., it returns the input string).
|
||||
Token::Number(ref n) => match n.parse() {
|
||||
Ok(n) => Ok(Value::Number(n)),
|
||||
Err(e) => parser_err!(format!("Could not parse '{}' as number: {}", n, e)),
|
||||
},
|
||||
Token::SingleQuotedString(ref s) => Ok(Value::SingleQuotedString(s.to_string())),
|
||||
Token::NationalStringLiteral(ref s) => {
|
||||
Ok(Value::NationalStringLiteral(s.to_string()))
|
||||
|
@ -1195,6 +1201,16 @@ impl Parser {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn parse_number_value(&mut self) -> Result<Value, ParserError> {
|
||||
match self.parse_value()? {
|
||||
v @ Value::Number(_) => Ok(v),
|
||||
_ => {
|
||||
self.prev_token();
|
||||
self.expected("literal number", self.peek_token())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse an unsigned literal integer/long
|
||||
pub fn parse_literal_uint(&mut self) -> Result<u64, ParserError> {
|
||||
match self.next_token() {
|
||||
|
@ -1856,16 +1872,13 @@ impl Parser {
|
|||
if self.parse_keyword("ALL") {
|
||||
Ok(None)
|
||||
} else {
|
||||
self.parse_literal_uint()
|
||||
.map(|n| Some(Expr::Value(Value::Number(n.to_string()))))
|
||||
Ok(Some(Expr::Value(self.parse_number_value()?)))
|
||||
}
|
||||
}
|
||||
|
||||
/// Parse an OFFSET clause
|
||||
pub fn parse_offset(&mut self) -> Result<Expr, ParserError> {
|
||||
let value = self
|
||||
.parse_literal_uint()
|
||||
.map(|n| Expr::Value(Value::Number(n.to_string())))?;
|
||||
let value = Expr::Value(self.parse_number_value()?);
|
||||
self.expect_one_of_keywords(&["ROW", "ROWS"])?;
|
||||
Ok(value)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue