From d420001c37eeb38aac26a85dd5661755d6ef4228 Mon Sep 17 00:00:00 2001 From: Ziinc Date: Wed, 14 Dec 2022 05:44:45 +0800 Subject: [PATCH] fix: unnest join constraint with alias parsing for BigQuery dialect (#732) * fix: unnest join constraint with alias parsing for BigQuery dialect * chore: fix failing tests --- src/parser.rs | 7 +++++-- tests/sqlparser_bigquery.rs | 28 ++++++++++++++++++++++++++++ tests/sqlparser_common.rs | 19 ------------------- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/parser.rs b/src/parser.rs index 87fb674aa..8e64a6fea 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -5291,12 +5291,15 @@ impl<'a> Parser<'a> { Err(_) => false, }; - let with_offset_alias = + let with_offset_alias = if with_offset { match self.parse_optional_alias(keywords::RESERVED_FOR_COLUMN_ALIAS) { Ok(Some(alias)) => Some(alias), Ok(None) => None, Err(e) => return Err(e), - }; + } + } else { + None + }; Ok(TableFactor::UNNEST { alias, diff --git a/tests/sqlparser_bigquery.rs b/tests/sqlparser_bigquery.rs index 8422245a6..4bf26ba81 100644 --- a/tests/sqlparser_bigquery.rs +++ b/tests/sqlparser_bigquery.rs @@ -94,6 +94,34 @@ fn parse_table_identifiers() { test_table_ident("abc5.GROUP", vec![Ident::new("abc5"), Ident::new("GROUP")]); } +#[test] +fn parse_join_constraint_unnest_alias() { + assert_eq!( + only( + bigquery() + .verified_only_select("SELECT * FROM t1 JOIN UNNEST(t1.a) AS f ON c1 = c2") + .from + ) + .joins, + vec![Join { + relation: TableFactor::UNNEST { + alias: table_alias("f"), + array_expr: Box::new(Expr::CompoundIdentifier(vec![ + Ident::new("t1"), + Ident::new("a") + ])), + with_offset: false, + with_offset_alias: None + }, + join_operator: JoinOperator::Inner(JoinConstraint::On(Expr::BinaryOp { + left: Box::new(Expr::Identifier("c1".into())), + op: BinaryOperator::Eq, + right: Box::new(Expr::Identifier("c2".into())), + })), + }] + ); +} + #[test] fn parse_trailing_comma() { for (sql, canonical) in [ diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 85cde27f5..66557f6a6 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -3683,25 +3683,6 @@ fn parse_unnest() { joins: vec![], }], ); - // 5. WITH OFFSET and WITH OFFSET Alias - chk( - true, - false, - true, - &dialects, - vec![TableWithJoins { - relation: TableFactor::UNNEST { - alias: Some(TableAlias { - name: Ident::new("numbers"), - columns: vec![], - }), - array_expr: Box::new(Expr::Identifier(Ident::new("expr"))), - with_offset: false, - with_offset_alias: Some(Ident::new("with_offset_alias")), - }, - joins: vec![], - }], - ); } #[test]