Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 51 additions & 33 deletions src/sql-parser/src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,38 +394,7 @@ impl<'a> Tokenizer<'a> {
}
}
// numbers
'0'..='9' => {
let mut seen_decimal = false;
let mut s = peeking_take_while(chars, |ch| match ch {
'0'..='9' => true,
'.' if !seen_decimal => {
seen_decimal = true;
true
}
_ => false,
});
// If in e-notation, parse the e-notation with special care given to negative exponents.
match chars.peek() {
Some('e') | Some('E') => {
s.push('E');
// Consume the e-notation signifier.
chars.next();
if let Some('-') = chars.peek() {
s.push('-');
// Consume the negative sign.
chars.next();
}
let e = peeking_take_while(chars, |ch| match ch {
'0'..='9' => true,
_ => false,
});
s.push_str(&e);
}
_ => {}
}

Ok(Some(Token::Number(s)))
}
'0'..='9' => self.tokenize_number(chars, false),
// punctuation
'(' => self.consume_and_return(chars, Token::LParen),
')' => self.consume_and_return(chars, Token::RParen),
Expand Down Expand Up @@ -518,7 +487,13 @@ impl<'a> Tokenizer<'a> {
}
}
'=' => self.consume_and_return(chars, Token::Eq),
'.' => self.consume_and_return(chars, Token::Period),
'.' => {
chars.next(); // consume '.'
match chars.peek() {
Some('0'..='9') => self.tokenize_number(chars, true),
_ => Ok(Some(Token::Period)),
}
}
'!' => {
chars.next(); // consume
match chars.peek() {
Expand Down Expand Up @@ -660,6 +635,49 @@ impl<'a> Tokenizer<'a> {
Ok(Some(Token::Parameter(n)))
}

fn tokenize_number(
&self,
chars: &mut Peekable<Chars<'_>>,
seen_decimal: bool,
) -> Result<Option<Token>, TokenizerError> {
let mut seen_decimal = seen_decimal;
let mut s = if seen_decimal {
".".to_string()
} else {
String::default()
};

s.push_str(&peeking_take_while(chars, |ch| match ch {
'0'..='9' => true,
'.' if !seen_decimal => {
seen_decimal = true;
true
}
_ => false,
}));
// If in e-notation, parse the e-notation with special care given to negative exponents.
match chars.peek() {
Some('e') | Some('E') => {
s.push('E');
// Consume the e-notation signifier.
chars.next();
if let Some('-') = chars.peek() {
s.push('-');
// Consume the negative sign.
chars.next();
}
let e = peeking_take_while(chars, |ch| match ch {
'0'..='9' => true,
_ => false,
});
s.push_str(&e);
}
_ => {}
}

Ok(Some(Token::Number(s)))
}

fn consume_and_return(
&self,
chars: &mut Peekable<Chars<'_>>,
Expand Down
6 changes: 6 additions & 0 deletions src/sql-parser/tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,12 @@ fn parse_number() {
assert_eq!(expr, Expr::Value(Value::Number("1.0".into())));
}

#[test]
fn parse_numeric_begin_with_decimal() {
let expr = verified_expr(".1");
assert_eq!(expr, Expr::Value(Value::Number(".1".into())));
}

#[test]
fn parse_approximate_numeric_literal() {
let expr = verified_expr("1.0E2");
Expand Down
13 changes: 13 additions & 0 deletions test/decimal.slt
Original file line number Diff line number Diff line change
Expand Up @@ -294,3 +294,16 @@ query R
SELECT CAST (null::float AS decimal(38, 7))
----
NULL

### No leading integer ###

query R
SELECT .123
----
0.123

statement error
SELECT ..123

statement error
SELECT .1.23