Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 99fb633

Browse files
nickolayeyalsatori
andcommittedOct 5, 2020
Move existing SF tests to sqlparser_snowflake.rs
Co-authored-by: Eyal Leshem <[email protected]>
1 parent 54be391 commit 99fb633

File tree

4 files changed

+86
-62
lines changed

4 files changed

+86
-62
lines changed
 

‎src/test_utils.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,3 +143,19 @@ pub fn expr_from_projection(item: &SelectItem) -> &Expr {
143143
pub fn number(n: &'static str) -> Value {
144144
Value::Number(n.parse().unwrap())
145145
}
146+
147+
pub fn table(name: impl Into<String>) -> TableFactor {
148+
TableFactor::Table {
149+
name: ObjectName(vec![Ident::new(name.into())]),
150+
alias: None,
151+
args: vec![],
152+
with_hints: vec![],
153+
}
154+
}
155+
156+
pub fn join(relation: TableFactor) -> Join {
157+
Join {
158+
relation,
159+
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
160+
}
161+
}

‎tests/macros/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Licensed under the Apache License, Version 2.0 (the "License");
2+
// you may not use this file except in compliance with the License.
3+
// You may obtain a copy of the License at
4+
//
5+
// http://www.apache.org/licenses/LICENSE-2.0
6+
//
7+
// Unless required by applicable law or agreed to in writing, software
8+
// distributed under the License is distributed on an "AS IS" BASIS,
9+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
// See the License for the specific language governing permissions and
11+
// limitations under the License.
12+
13+
macro_rules! nest {
14+
($base:expr $(, $join:expr)*) => {
15+
TableFactor::NestedJoin(Box::new(TableWithJoins {
16+
relation: $base,
17+
joins: vec![$(join($join)),*]
18+
}))
19+
};
20+
}

‎tests/sqlparser_common.rs

Lines changed: 5 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
//! sqlparser regardless of the chosen dialect (i.e. it doesn't conflict with
1919
//! dialect-specific parsing rules).
2020
21-
use matches::assert_matches;
21+
#[macro_use]
22+
#[path = "macros/mod.rs"]
23+
mod macros;
2224

25+
use matches::assert_matches;
2326
use sqlparser::ast::*;
2427
use sqlparser::dialect::keywords::ALL_KEYWORDS;
2528
use sqlparser::parser::ParserError;
26-
use sqlparser::test_utils::{all_dialects, expr_from_projection, number, only};
29+
use sqlparser::test_utils::{all_dialects, expr_from_projection, join, number, only, table};
2730

2831
#[test]
2932
fn parse_insert_values() {
@@ -2282,31 +2285,6 @@ fn parse_complex_join() {
22822285

22832286
#[test]
22842287
fn parse_join_nesting() {
2285-
fn table(name: impl Into<String>) -> TableFactor {
2286-
TableFactor::Table {
2287-
name: ObjectName(vec![Ident::new(name.into())]),
2288-
alias: None,
2289-
args: vec![],
2290-
with_hints: vec![],
2291-
}
2292-
}
2293-
2294-
fn join(relation: TableFactor) -> Join {
2295-
Join {
2296-
relation,
2297-
join_operator: JoinOperator::Inner(JoinConstraint::Natural),
2298-
}
2299-
}
2300-
2301-
macro_rules! nest {
2302-
($base:expr $(, $join:expr)*) => {
2303-
TableFactor::NestedJoin(Box::new(TableWithJoins {
2304-
relation: $base,
2305-
joins: vec![$(join($join)),*]
2306-
}))
2307-
};
2308-
}
2309-
23102288
let sql = "SELECT * FROM a NATURAL JOIN (b NATURAL JOIN (c NATURAL JOIN d NATURAL JOIN e)) \
23112289
NATURAL JOIN (f NATURAL JOIN (g NATURAL JOIN h))";
23122290
assert_eq!(
@@ -2337,20 +2315,6 @@ fn parse_join_nesting() {
23372315
from.joins,
23382316
vec![join(nest!(nest!(nest!(table("b"), table("c")))))]
23392317
);
2340-
2341-
// Parenthesized table names are non-standard, but supported in Snowflake SQL
2342-
let sql = "SELECT * FROM (a NATURAL JOIN (b))";
2343-
let select = verified_only_select(sql);
2344-
let from = only(select.from);
2345-
2346-
assert_eq!(from.relation, nest!(table("a"), nest!(table("b"))));
2347-
2348-
// Double parentheses around table names are non-standard, but supported in Snowflake SQL
2349-
let sql = "SELECT * FROM (a NATURAL JOIN ((b)))";
2350-
let select = verified_only_select(sql);
2351-
let from = only(select.from);
2352-
2353-
assert_eq!(from.relation, nest!(table("a"), nest!(nest!(table("b")))));
23542318
}
23552319

23562320
#[test]
@@ -2490,26 +2454,6 @@ fn parse_derived_tables() {
24902454
}],
24912455
}))
24922456
);
2493-
2494-
// Nesting a subquery in parentheses is non-standard, but supported in Snowflake SQL
2495-
let sql = "SELECT * FROM ((SELECT 1) AS t)";
2496-
let select = verified_only_select(sql);
2497-
let from = only(select.from);
2498-
2499-
assert_eq!(
2500-
from.relation,
2501-
TableFactor::NestedJoin(Box::new(TableWithJoins {
2502-
relation: TableFactor::Derived {
2503-
lateral: false,
2504-
subquery: Box::new(verified_query("SELECT 1")),
2505-
alias: Some(TableAlias {
2506-
name: "t".into(),
2507-
columns: vec![],
2508-
})
2509-
},
2510-
joins: Vec::new(),
2511-
}))
2512-
);
25132457
}
25142458

25152459
#[test]

‎tests/sqlparser_snowflake.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@
99
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1010
// See the License for the specific language governing permissions and
1111
// limitations under the License.
12+
13+
#[macro_use]
14+
#[path = "macros/mod.rs"]
15+
mod macros;
16+
17+
use sqlparser::test_utils::*;
1218
use sqlparser::ast::*;
1319
use sqlparser::dialect::{GenericDialect, SnowflakeDialect};
14-
use sqlparser::test_utils::*;
1520
use sqlparser::tokenizer::*;
1621

1722
#[test]
@@ -63,6 +68,45 @@ fn test_snowflake_single_line_tokenize() {
6368
assert_eq!(expected, tokens);
6469
}
6570

71+
#[test]
72+
fn test_sf_derives_single_table_in_parenthesis() {
73+
// Nesting a subquery in parentheses is non-standard, but supported in Snowflake SQL
74+
let sql = "SELECT * FROM ((SELECT 1) AS t)";
75+
let select = snowflake_and_generic().verified_only_select(sql);
76+
let from = only(select.from);
77+
assert_eq!(
78+
from.relation,
79+
TableFactor::NestedJoin(Box::new(TableWithJoins {
80+
relation: TableFactor::Derived {
81+
lateral: false,
82+
subquery: Box::new(snowflake_and_generic().verified_query("SELECT 1")),
83+
alias: Some(TableAlias {
84+
name: "t".into(),
85+
columns: vec![],
86+
})
87+
},
88+
joins: Vec::new(),
89+
}))
90+
);
91+
}
92+
93+
#[test]
94+
fn test_single_table_in_parenthesis() {
95+
// Parenthesized table names are non-standard, but supported in Snowflake SQL
96+
let sql = "SELECT * FROM (a NATURAL JOIN (b))";
97+
let select = snowflake_and_generic().verified_only_select(sql);
98+
let from = only(select.from);
99+
100+
assert_eq!(from.relation, nest!(table("a"), nest!(table("b"))));
101+
102+
// Double parentheses around table names are non-standard, but supported in Snowflake SQL
103+
let sql = "SELECT * FROM (a NATURAL JOIN ((b)))";
104+
let select = snowflake_and_generic().verified_only_select(sql);
105+
let from = only(select.from);
106+
107+
assert_eq!(from.relation, nest!(table("a"), nest!(nest!(table("b")))));
108+
}
109+
66110
fn snowflake_and_generic() -> TestedDialects {
67111
TestedDialects {
68112
dialects: vec![Box::new(SnowflakeDialect {}), Box::new(GenericDialect {})],

0 commit comments

Comments
 (0)
Please sign in to comment.