Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix BigQuery hyphenated ObjectName with numbers #1598

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8747,7 +8747,9 @@ impl<'a> Parser<'a> {
}
Token::Number(s, false) if s.chars().all(|c| c.is_ascii_digit()) => {
ident.value.push_str(&s);
true
// If next token is period, then it is part of an ObjectName and we don't expect whitespace
// after the number.
!matches!(self.peek_token().token, Token::Period)
}
_ => {
return self
Expand Down
45 changes: 38 additions & 7 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1108,15 +1108,29 @@ impl<'a> Tokenizer<'a> {

// match one period
if let Some('.') = chars.peek() {
s.push('.');
chars.next();
// Check if this actually is a float point number
let mut char_clone = chars.peekable.clone();
char_clone.next();
// Next char should be a digit, otherwise, it is not a float point number
if char_clone
.peek()
.map(|c| c.is_ascii_digit())
.unwrap_or(false)
{
s.push('.');
chars.next();
} else if !s.is_empty() {
// Number might be part of period separated construct. Keep the period for next token
// e.g. a-12.b
return Ok(Some(Token::Number(s, false)));
} else {
// No number -> Token::Period
chars.next();
return Ok(Some(Token::Period));
}
}
s += &peeking_take_while(chars, |ch| ch.is_ascii_digit());

// No number -> Token::Period
if s == "." {
return Ok(Some(Token::Period));
}
s += &peeking_take_while(chars, |ch| ch.is_ascii_digit());

let mut exponent_part = String::new();
// Parse exponent as number
Expand Down Expand Up @@ -2128,6 +2142,23 @@ mod tests {
compare(expected, tokens);
}

#[test]
fn tokenize_select_float_hyphenated_identifier() {
let sql = String::from("SELECT a-12.b");
let dialect = GenericDialect {};
let tokens = Tokenizer::new(&dialect, &sql).tokenize().unwrap();
let expected = vec![
Token::make_keyword("SELECT"),
Token::Whitespace(Whitespace::Space),
Token::make_word("a", None),
Token::Minus,
Token::Number(String::from("12"), false),
Token::Period,
Token::make_word("b", None),
];
compare(expected, tokens);
}

#[test]
fn tokenize_clickhouse_double_equal() {
let sql = String::from("SELECT foo=='1'");
Expand Down
20 changes: 20 additions & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,26 @@ fn parse_hyphenated_table_identifiers() {
"SELECT * FROM foo-bar AS f JOIN baz-qux AS b ON f.id = b.id",
);

assert_eq!(
bigquery()
.verified_only_select_with_canonical(
"select * from foo-123.bar",
"SELECT * FROM foo-123.bar"
)
.from[0]
.relation,
TableFactor::Table {
name: ObjectName(vec![Ident::new("foo-123"), Ident::new("bar")]),
alias: None,
args: None,
with_hints: vec![],
version: None,
partitions: vec![],
with_ordinality: false,
json_path: None,
}
);

assert_eq!(
bigquery()
.verified_only_select_with_canonical(
Expand Down
Loading