Skip to content

Commit

Permalink
Support for single-quoted identifiers (apache#1021)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Lamb <[email protected]>
  • Loading branch information
2 people authored and serprex committed Nov 6, 2023
1 parent bb53ee7 commit 07e867c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -624,18 +624,29 @@ impl<'a> Parser<'a> {

let next_token = self.next_token();
match next_token.token {
Token::Word(w) if self.peek_token().token == Token::Period => {
let mut id_parts: Vec<Ident> = vec![w.to_ident()];

while self.consume_token(&Token::Period) {
let next_token = self.next_token();
match next_token.token {
Token::Word(w) => id_parts.push(w.to_ident()),
Token::Mul => {
return Ok(WildcardExpr::QualifiedWildcard(ObjectName(id_parts)));
}
_ => {
return self.expected("an identifier or a '*' after '.'", next_token);
t @ (Token::Word(_) | Token::SingleQuotedString(_)) => {
if self.peek_token().token == Token::Period {
let mut id_parts: Vec<Ident> = vec![match t {
Token::Word(w) => w.to_ident(),
Token::SingleQuotedString(s) => Ident::with_quote('\'', s),
_ => unreachable!(), // We matched above
}];

while self.consume_token(&Token::Period) {
let next_token = self.next_token();
match next_token.token {
Token::Word(w) => id_parts.push(w.to_ident()),
Token::SingleQuotedString(s) => {
// SQLite has single-quoted identifiers
id_parts.push(Ident::with_quote('\'', s))
}
Token::Mul => {
return Ok(WildcardExpr::QualifiedWildcard(ObjectName(id_parts)));
}
_ => {
return self
.expected("an identifier or a '*' after '.'", next_token);
}
}
}
}
Expand Down Expand Up @@ -834,6 +845,9 @@ impl<'a> Parser<'a> {
let next_token = self.next_token();
match next_token.token {
Token::Word(w) => id_parts.push(w.to_ident()),
Token::SingleQuotedString(s) => {
id_parts.push(Ident::with_quote('\'', s))
}
_ => {
return self
.expected("an identifier or a '*' after '.'", next_token);
Expand Down
5 changes: 5 additions & 0 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ fn parse_create_table_with_strict() {
}
}

#[test]
fn parse_single_quoted_identified() {
sqlite().verified_only_select("SELECT 't'.*, t.'x' FROM 't'");
// TODO: add support for select 't'.x
}
#[test]
fn parse_window_function_with_filter() {
for func_name in [
Expand Down

0 comments on commit 07e867c

Please sign in to comment.