Move existing SF tests to sqlparser_snowflake.rs

Co-authored-by: Eyal Leshem <eyal@satoricyber.com>
This commit is contained in:
Nickolay Ponomarev 2020-10-05 05:08:55 +03:00
parent 54be3912a9
commit 99fb633221
4 changed files with 86 additions and 62 deletions

View file

@ -143,3 +143,19 @@ pub fn expr_from_projection(item: &SelectItem) -> &Expr {
pub fn number(n: &'static str) -> Value { pub fn number(n: &'static str) -> Value {
Value::Number(n.parse().unwrap()) Value::Number(n.parse().unwrap())
} }
pub fn table(name: impl Into<String>) -> 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),
}
}

20
tests/macros/mod.rs Normal file
View file

@ -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)),*]
}))
};
}

View file

@ -18,12 +18,15 @@
//! sqlparser regardless of the chosen dialect (i.e. it doesn't conflict with //! sqlparser regardless of the chosen dialect (i.e. it doesn't conflict with
//! dialect-specific parsing rules). //! 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::ast::*;
use sqlparser::dialect::keywords::ALL_KEYWORDS; use sqlparser::dialect::keywords::ALL_KEYWORDS;
use sqlparser::parser::ParserError; 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] #[test]
fn parse_insert_values() { fn parse_insert_values() {
@ -2282,31 +2285,6 @@ fn parse_complex_join() {
#[test] #[test]
fn parse_join_nesting() { fn parse_join_nesting() {
fn table(name: impl Into<String>) -> 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)) \ 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))"; NATURAL JOIN (f NATURAL JOIN (g NATURAL JOIN h))";
assert_eq!( assert_eq!(
@ -2337,20 +2315,6 @@ fn parse_join_nesting() {
from.joins, from.joins,
vec![join(nest!(nest!(nest!(table("b"), table("c")))))] 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] #[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] #[test]

View file

@ -9,9 +9,14 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
#[macro_use]
#[path = "macros/mod.rs"]
mod macros;
use sqlparser::test_utils::*;
use sqlparser::ast::*; use sqlparser::ast::*;
use sqlparser::dialect::{GenericDialect, SnowflakeDialect}; use sqlparser::dialect::{GenericDialect, SnowflakeDialect};
use sqlparser::test_utils::*;
use sqlparser::tokenizer::*; use sqlparser::tokenizer::*;
#[test] #[test]
@ -63,6 +68,45 @@ fn test_snowflake_single_line_tokenize() {
assert_eq!(expected, tokens); 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 { fn snowflake_and_generic() -> TestedDialects {
TestedDialects { TestedDialects {
dialects: vec![Box::new(SnowflakeDialect {}), Box::new(GenericDialect {})], dialects: vec![Box::new(SnowflakeDialect {}), Box::new(GenericDialect {})],