From 99fb633221c48c481edb669cd3b376688c88a5dc Mon Sep 17 00:00:00 2001 From: Nickolay Ponomarev Date: Mon, 5 Oct 2020 05:08:55 +0300 Subject: [PATCH] Move existing SF tests to sqlparser_snowflake.rs Co-authored-by: Eyal Leshem --- src/test_utils.rs | 16 +++++++++ tests/macros/mod.rs | 20 +++++++++++ tests/sqlparser_common.rs | 66 +++--------------------------------- tests/sqlparser_snowflake.rs | 46 ++++++++++++++++++++++++- 4 files changed, 86 insertions(+), 62 deletions(-) create mode 100644 tests/macros/mod.rs diff --git a/src/test_utils.rs b/src/test_utils.rs index 7d963606..0507fa86 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -143,3 +143,19 @@ pub fn expr_from_projection(item: &SelectItem) -> &Expr { pub fn number(n: &'static str) -> Value { Value::Number(n.parse().unwrap()) } + +pub fn table(name: impl Into) -> TableFactor { + TableFactor::Table { + name: ObjectName(vec![Ident::new(name.into())]), + alias: None, + args: vec![], + with_hints: vec![], + } +} + +pub fn join(relation: TableFactor) -> Join { + Join { + relation, + join_operator: JoinOperator::Inner(JoinConstraint::Natural), + } +} diff --git a/tests/macros/mod.rs b/tests/macros/mod.rs new file mode 100644 index 00000000..365024a9 --- /dev/null +++ b/tests/macros/mod.rs @@ -0,0 +1,20 @@ +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +macro_rules! nest { + ($base:expr $(, $join:expr)*) => { + TableFactor::NestedJoin(Box::new(TableWithJoins { + relation: $base, + joins: vec![$(join($join)),*] + })) + }; +} diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 6b032d92..a535fa21 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -18,12 +18,15 @@ //! sqlparser regardless of the chosen dialect (i.e. it doesn't conflict with //! dialect-specific parsing rules). -use matches::assert_matches; +#[macro_use] +#[path = "macros/mod.rs"] +mod macros; +use matches::assert_matches; use sqlparser::ast::*; use sqlparser::dialect::keywords::ALL_KEYWORDS; use sqlparser::parser::ParserError; -use sqlparser::test_utils::{all_dialects, expr_from_projection, number, only}; +use sqlparser::test_utils::{all_dialects, expr_from_projection, join, number, only, table}; #[test] fn parse_insert_values() { @@ -2282,31 +2285,6 @@ fn parse_complex_join() { #[test] fn parse_join_nesting() { - fn table(name: impl Into) -> TableFactor { - TableFactor::Table { - name: ObjectName(vec![Ident::new(name.into())]), - alias: None, - args: vec![], - with_hints: vec![], - } - } - - fn join(relation: TableFactor) -> Join { - Join { - relation, - join_operator: JoinOperator::Inner(JoinConstraint::Natural), - } - } - - macro_rules! nest { - ($base:expr $(, $join:expr)*) => { - TableFactor::NestedJoin(Box::new(TableWithJoins { - relation: $base, - joins: vec![$(join($join)),*] - })) - }; - } - let sql = "SELECT * FROM a NATURAL JOIN (b NATURAL JOIN (c NATURAL JOIN d NATURAL JOIN e)) \ NATURAL JOIN (f NATURAL JOIN (g NATURAL JOIN h))"; assert_eq!( @@ -2337,20 +2315,6 @@ fn parse_join_nesting() { from.joins, vec![join(nest!(nest!(nest!(table("b"), table("c")))))] ); - - // Parenthesized table names are non-standard, but supported in Snowflake SQL - let sql = "SELECT * FROM (a NATURAL JOIN (b))"; - let select = verified_only_select(sql); - let from = only(select.from); - - assert_eq!(from.relation, nest!(table("a"), nest!(table("b")))); - - // Double parentheses around table names are non-standard, but supported in Snowflake SQL - let sql = "SELECT * FROM (a NATURAL JOIN ((b)))"; - let select = verified_only_select(sql); - let from = only(select.from); - - assert_eq!(from.relation, nest!(table("a"), nest!(nest!(table("b"))))); } #[test] @@ -2490,26 +2454,6 @@ fn parse_derived_tables() { }], })) ); - - // Nesting a subquery in parentheses is non-standard, but supported in Snowflake SQL - let sql = "SELECT * FROM ((SELECT 1) AS t)"; - let select = verified_only_select(sql); - let from = only(select.from); - - assert_eq!( - from.relation, - TableFactor::NestedJoin(Box::new(TableWithJoins { - relation: TableFactor::Derived { - lateral: false, - subquery: Box::new(verified_query("SELECT 1")), - alias: Some(TableAlias { - name: "t".into(), - columns: vec![], - }) - }, - joins: Vec::new(), - })) - ); } #[test] diff --git a/tests/sqlparser_snowflake.rs b/tests/sqlparser_snowflake.rs index c1aa41db..5915d123 100644 --- a/tests/sqlparser_snowflake.rs +++ b/tests/sqlparser_snowflake.rs @@ -9,9 +9,14 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + +#[macro_use] +#[path = "macros/mod.rs"] +mod macros; + +use sqlparser::test_utils::*; use sqlparser::ast::*; use sqlparser::dialect::{GenericDialect, SnowflakeDialect}; -use sqlparser::test_utils::*; use sqlparser::tokenizer::*; #[test] @@ -63,6 +68,45 @@ fn test_snowflake_single_line_tokenize() { assert_eq!(expected, tokens); } +#[test] +fn test_sf_derives_single_table_in_parenthesis() { + // Nesting a subquery in parentheses is non-standard, but supported in Snowflake SQL + let sql = "SELECT * FROM ((SELECT 1) AS t)"; + let select = snowflake_and_generic().verified_only_select(sql); + let from = only(select.from); + assert_eq!( + from.relation, + TableFactor::NestedJoin(Box::new(TableWithJoins { + relation: TableFactor::Derived { + lateral: false, + subquery: Box::new(snowflake_and_generic().verified_query("SELECT 1")), + alias: Some(TableAlias { + name: "t".into(), + columns: vec![], + }) + }, + joins: Vec::new(), + })) + ); +} + +#[test] +fn test_single_table_in_parenthesis() { + // Parenthesized table names are non-standard, but supported in Snowflake SQL + let sql = "SELECT * FROM (a NATURAL JOIN (b))"; + let select = snowflake_and_generic().verified_only_select(sql); + let from = only(select.from); + + assert_eq!(from.relation, nest!(table("a"), nest!(table("b")))); + + // Double parentheses around table names are non-standard, but supported in Snowflake SQL + let sql = "SELECT * FROM (a NATURAL JOIN ((b)))"; + let select = snowflake_and_generic().verified_only_select(sql); + let from = only(select.from); + + assert_eq!(from.relation, nest!(table("a"), nest!(nest!(table("b"))))); +} + fn snowflake_and_generic() -> TestedDialects { TestedDialects { dialects: vec![Box::new(SnowflakeDialect {}), Box::new(GenericDialect {})],