Skip to content

fix: parsing JsonOperator #913

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

Merged
merged 2 commits into from
Jul 17, 2023
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
111 changes: 77 additions & 34 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ use serde::{Deserialize, Serialize};
use sqlparser_derive::{Visit, VisitMut};

use crate::ast::DollarQuotedString;
use crate::dialect::{BigQueryDialect, DuckDbDialect, GenericDialect, SnowflakeDialect};
use crate::dialect::{
BigQueryDialect, DuckDbDialect, GenericDialect, HiveDialect, SnowflakeDialect,
};
use crate::dialect::{Dialect, MySqlDialect};
use crate::keywords::{Keyword, ALL_KEYWORDS, ALL_KEYWORDS_INDEX};

Expand Down Expand Up @@ -495,9 +497,32 @@ impl<'a> Tokenizer<'a> {
Ok(tokens)
}

fn tokenize_identifier_or_keyword(
&self,
ch: String,
chars: &mut State,
) -> Result<Option<Token>, TokenizerError> {
chars.next(); // consume the first char
let word = self.tokenize_word(ch, chars);

// TODO: implement parsing of exponent here
if word.chars().all(|x| x.is_ascii_digit() || x == '.') {
let mut inner_state = State {
peekable: word.chars().peekable(),
line: 0,
col: 0,
};
let mut s = peeking_take_while(&mut inner_state, |ch| matches!(ch, '0'..='9' | '.'));
let s2 = peeking_take_while(chars, |ch| matches!(ch, '0'..='9' | '.'));
s += s2.as_str();
return Ok(Some(Token::Number(s, false)));
}

Ok(Some(Token::make_word(&word, None)))
}

/// Get the next token or return None
fn next_token(&self, chars: &mut State) -> Result<Option<Token>, TokenizerError> {
//println!("next_token: {:?}", chars.peek());
match chars.peek() {
Some(&ch) => match ch {
' ' => self.consume_and_return(chars, Token::Whitespace(Whitespace::Space)),
Expand Down Expand Up @@ -525,7 +550,7 @@ impl<'a> Tokenizer<'a> {
}
_ => {
// regular identifier starting with an "b" or "B"
let s = self.tokenize_word(b, chars);
let s = self.tokenize_word(b.to_string(), chars);
Ok(Some(Token::make_word(&s, None)))
}
}
Expand All @@ -544,7 +569,7 @@ impl<'a> Tokenizer<'a> {
}
_ => {
// regular identifier starting with an "r" or "R"
let s = self.tokenize_word(b, chars);
let s = self.tokenize_word(b.to_string(), chars);
Ok(Some(Token::make_word(&s, None)))
}
}
Expand All @@ -560,7 +585,7 @@ impl<'a> Tokenizer<'a> {
}
_ => {
// regular identifier starting with an "N"
let s = self.tokenize_word(n, chars);
let s = self.tokenize_word(n.to_string(), chars);
Ok(Some(Token::make_word(&s, None)))
}
}
Expand All @@ -577,7 +602,7 @@ impl<'a> Tokenizer<'a> {
}
_ => {
// regular identifier starting with an "E" or "e"
let s = self.tokenize_word(x, chars);
let s = self.tokenize_word(x.to_string(), chars);
Ok(Some(Token::make_word(&s, None)))
}
}
Expand All @@ -594,33 +619,11 @@ impl<'a> Tokenizer<'a> {
}
_ => {
// regular identifier starting with an "X"
let s = self.tokenize_word(x, chars);
let s = self.tokenize_word(x.to_string(), chars);
Ok(Some(Token::make_word(&s, None)))
}
}
}
// identifier or keyword
ch if self.dialect.is_identifier_start(ch) => {
chars.next(); // consume the first char
let word = self.tokenize_word(ch, chars);

// TODO: implement parsing of exponent here
if word.chars().all(|x| x.is_ascii_digit() || x == '.') {
let mut inner_state = State {
peekable: word.chars().peekable(),
line: 0,
col: 0,
};
let mut s = peeking_take_while(&mut inner_state, |ch| {
matches!(ch, '0'..='9' | '.')
});
let s2 = peeking_take_while(chars, |ch| matches!(ch, '0'..='9' | '.'));
s += s2.as_str();
return Ok(Some(Token::Number(s, false)));
}

Ok(Some(Token::make_word(&word, None)))
}
// single quoted string
'\'' => {
let s = self.tokenize_quoted_string(chars, '\'')?;
Expand Down Expand Up @@ -714,7 +717,7 @@ impl<'a> Tokenizer<'a> {

// mysql dialect supports identifiers that start with a numeric prefix,
// as long as they aren't an exponent number.
if dialect_of!(self is MySqlDialect) && exponent_part.is_empty() {
if dialect_of!(self is MySqlDialect | HiveDialect) && exponent_part.is_empty() {
let word =
peeking_take_while(chars, |ch| self.dialect.is_identifier_part(ch));

Expand Down Expand Up @@ -786,7 +789,18 @@ impl<'a> Tokenizer<'a> {
}
'+' => self.consume_and_return(chars, Token::Plus),
'*' => self.consume_and_return(chars, Token::Mul),
'%' => self.consume_and_return(chars, Token::Mod),
'%' => {
chars.next();
match chars.peek() {
Some(' ') => self.consume_and_return(chars, Token::Mod),
Some(sch) if self.dialect.is_identifier_start('%') => {
let mut s = ch.to_string();
s.push_str(&sch.to_string());
self.tokenize_identifier_or_keyword(s, chars)
}
_ => self.consume_and_return(chars, Token::Mod),
}
}
'|' => {
chars.next(); // consume the '|'
match chars.peek() {
Expand Down Expand Up @@ -901,6 +915,12 @@ impl<'a> Tokenizer<'a> {
_ => Ok(Some(Token::HashArrow)),
}
}
Some(' ') => Ok(Some(Token::Sharp)),
Some(sch) if self.dialect.is_identifier_start('#') => {
let mut s = ch.to_string();
s.push_str(&sch.to_string());
self.tokenize_identifier_or_keyword(s, chars)
}
_ => Ok(Some(Token::Sharp)),
}
}
Expand All @@ -909,7 +929,25 @@ impl<'a> Tokenizer<'a> {
match chars.peek() {
Some('>') => self.consume_and_return(chars, Token::AtArrow),
Some('?') => self.consume_and_return(chars, Token::AtQuestion),
Some('@') => self.consume_and_return(chars, Token::AtAt),
Some('@') => {
chars.next();
match chars.peek() {
Some(' ') => Ok(Some(Token::AtAt)),
Some(tch) if self.dialect.is_identifier_start('@') => {
let mut s = ch.to_string();
s.push('@');
s.push_str(&tch.to_string());
self.tokenize_identifier_or_keyword(s, chars)
}
_ => Ok(Some(Token::AtAt)),
}
}
Some(' ') => Ok(Some(Token::AtSign)),
Some(sch) if self.dialect.is_identifier_start('@') => {
let mut s = ch.to_string();
s.push_str(&sch.to_string());
self.tokenize_identifier_or_keyword(s, chars)
}
_ => Ok(Some(Token::AtSign)),
}
}
Expand All @@ -918,6 +956,11 @@ impl<'a> Tokenizer<'a> {
let s = peeking_take_while(chars, |ch| ch.is_numeric());
Ok(Some(Token::Placeholder(String::from("?") + &s)))
}

// identifier or keyword
ch if self.dialect.is_identifier_start(ch) => {
self.tokenize_identifier_or_keyword(ch.to_string(), chars)
}
'$' => Ok(Some(self.tokenize_dollar_preceded_value(chars)?)),

//whitespace check (including unicode chars) should be last as it covers some of the chars above
Expand Down Expand Up @@ -1043,8 +1086,8 @@ impl<'a> Tokenizer<'a> {
}

/// Tokenize an identifier or keyword, after the first char is already consumed.
fn tokenize_word(&self, first_char: char, chars: &mut State) -> String {
let mut s = first_char.to_string();
fn tokenize_word(&self, first_chars: String, chars: &mut State) -> String {
let mut s = first_chars;
s.push_str(&peeking_take_while(chars, |ch| {
self.dialect.is_identifier_part(ch)
}));
Expand Down
35 changes: 35 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,41 @@ fn parse_unary_math_with_multiply() {
);
}

fn pg_and_generic() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(PostgreSqlDialect {}), Box::new(GenericDialect {})],
options: None,
}
}

#[test]
fn parse_json_ops_without_colon() {
use self::JsonOperator;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I verified this test covers the changes in this PR by running this test without the code in this PR and it failed (as expected):

---- parse_json_ops_without_colon stdout ----
thread 'parse_json_ops_without_colon' panicked at 'assertion failed: `(left == right)`
  left: `Ok([Query(Query { with: None, body: Select(Select { distinct: None, top: None, projection: [UnnamedExpr(JsonAccess { left: Identifier(Ident { value: "a", quote_style: None }), operator: HashArrow, right: Identifier(Ident { value: "b", quote_style: None }) })], into: None, from: [], lateral_views: [], selection: None, group_by: [], cluster_by: [], distribute_by: [], sort_by: [], having: None, named_window: [], qualify: None }), order_by: [], limit: None, offset: None, fetch: None, locks: [] })])`,
 right: `Err(ParserError("Expected end of statement, found: >"))`: Parse results with PostgreSqlDialect are different from GenericDialect', src/test_utils.rs:60:21
stack backtrace:

let binary_ops = &[
("->", JsonOperator::Arrow, all_dialects()),
("->>", JsonOperator::LongArrow, all_dialects()),
("#>", JsonOperator::HashArrow, pg_and_generic()),
("#>>", JsonOperator::HashLongArrow, pg_and_generic()),
("@>", JsonOperator::AtArrow, all_dialects()),
("<@", JsonOperator::ArrowAt, all_dialects()),
("#-", JsonOperator::HashMinus, pg_and_generic()),
("@?", JsonOperator::AtQuestion, all_dialects()),
("@@", JsonOperator::AtAt, all_dialects()),
];

for (str_op, op, dialects) in binary_ops {
let select = dialects.verified_only_select(&format!("SELECT a {} b", &str_op));
assert_eq!(
SelectItem::UnnamedExpr(Expr::JsonAccess {
left: Box::new(Expr::Identifier(Ident::new("a"))),
operator: *op,
right: Box::new(Expr::Identifier(Ident::new("b"))),
}),
select.projection[0]
);
}
}

#[test]
fn parse_is_null() {
use self::Expr::*;
Expand Down