Skip to content

Commit

Permalink
fix: handle bigquery offset in map key (#797)
Browse files Browse the repository at this point in the history
* fix: handle bigquery offset in map key

* chore: add in more comments and tests

* chore: fix all linting and compilation warnings
  • Loading branch information
Ziinc authored Jan 21, 2023
1 parent ca93941 commit 4955863
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4183,7 +4183,13 @@ impl<'a> Parser<'a> {
pub fn parse_map_key(&mut self) -> Result<Expr, ParserError> {
let next_token = self.next_token();
match next_token.token {
Token::Word(Word { value, keyword, .. }) if keyword == Keyword::NoKeyword => {
// handle bigquery offset subscript operator which overlaps with OFFSET operator
Token::Word(Word { value, keyword, .. })
if (dialect_of!(self is BigQueryDialect) && keyword == Keyword::OFFSET) =>
{
self.parse_function(ObjectName(vec![Ident::new(value)]))
}
Token::Word(Word { value, keyword, .. }) if (keyword == Keyword::NoKeyword) => {
if self.peek_token() == Token::LParen {
return self.parse_function(ObjectName(vec![Ident::new(value)]));
}
Expand Down
37 changes: 35 additions & 2 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@
#[macro_use]
mod test_utils;

use test_utils::*;

use sqlparser::ast::*;
use sqlparser::dialect::{BigQueryDialect, GenericDialect};
use test_utils::*;

#[test]
fn parse_literal_string() {
Expand Down Expand Up @@ -306,3 +305,37 @@ fn bigquery_and_generic() -> TestedDialects {
dialects: vec![Box::new(BigQueryDialect {}), Box::new(GenericDialect {})],
}
}

#[test]
fn parse_map_access_offset() {
let sql = "SELECT d[offset(0)]";
let _select = bigquery().verified_only_select(sql);
#[cfg(not(feature = "bigdecimal"))]
assert_eq!(
_select.projection[0],
SelectItem::UnnamedExpr(Expr::MapAccess {
column: Box::new(Expr::Identifier(Ident {
value: "d".to_string(),
quote_style: None,
})),
keys: vec![Expr::Function(Function {
name: ObjectName(vec!["offset".into()]),
args: vec![FunctionArg::Unnamed(FunctionArgExpr::Expr(Expr::Value(
Value::Number("0".into(), false)
))),],
over: None,
distinct: false,
special: false,
})],
})
);

// test other operators
for sql in [
"SELECT d[SAFE_OFFSET(0)]",
"SELECT d[ORDINAL(0)]",
"SELECT d[SAFE_ORDINAL(0)]",
] {
bigquery().verified_only_select(sql);
}
}

0 comments on commit 4955863

Please sign in to comment.