@@ -17,6 +17,7 @@ mod macros;
17
17
use sqlparser:: test_utils:: * ;
18
18
use sqlparser:: ast:: * ;
19
19
use sqlparser:: dialect:: { GenericDialect , SnowflakeDialect } ;
20
+ use sqlparser:: parser:: ParserError ;
20
21
use sqlparser:: tokenizer:: * ;
21
22
22
23
#[ test]
@@ -68,43 +69,124 @@ fn test_snowflake_single_line_tokenize() {
68
69
assert_eq ! ( expected, tokens) ;
69
70
}
70
71
72
+ fn get_from_section_from_select_query ( query : & str ) -> Vec < TableWithJoins > {
73
+ let statement = snowflake ( ) . parse_sql_statements ( query) . unwrap ( ) [ 0 ] . clone ( ) ;
74
+
75
+ let query = match statement {
76
+ Statement :: Query ( query) => query,
77
+ _ => panic ! ( "Not a query" ) ,
78
+ } ;
79
+
80
+ let select = match query. body {
81
+ SetExpr :: Select ( select) => select,
82
+ _ => panic ! ( "not a select query" ) ,
83
+ } ;
84
+
85
+ select. from . clone ( )
86
+ }
87
+
71
88
#[ test]
72
89
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 ) ;
90
+ let from = get_from_section_from_select_query ( "SELECT * FROM ((SELECT 1) AS t)" ) ;
91
+ assert_eq ! (
92
+ only( from) . relation,
93
+ TableFactor :: Derived {
94
+ lateral: false ,
95
+ subquery: Box :: new( snowflake( ) . verified_query( "SELECT 1" ) ) ,
96
+ alias: Some ( TableAlias {
97
+ name: "t" . into( ) ,
98
+ columns: vec![ ] ,
99
+ } )
100
+ }
101
+ ) ;
102
+
103
+ let from = get_from_section_from_select_query ( "SELECT * FROM (((SELECT 1) AS t))" ) ;
104
+
77
105
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
- } ) )
106
+ only( from) . relation,
107
+ TableFactor :: Derived {
108
+ lateral: false ,
109
+ subquery: Box :: new( snowflake( ) . verified_query( "SELECT 1" ) ) ,
110
+ alias: Some ( TableAlias {
111
+ name: "t" . into( ) ,
112
+ columns: vec![ ] ,
113
+ } )
114
+ }
90
115
) ;
91
116
}
92
117
93
118
#[ test]
94
119
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 ) ;
120
+ //Parenthesized table names are non-standard, but supported in Snowflake SQL
121
+
122
+ let from = get_from_section_from_select_query ( "SELECT * FROM (a NATURAL JOIN (b))" ) ;
123
+ assert_eq ! ( only( from) . relation, nest!( table( "a" ) , table( "b" ) ) ) ;
124
+
125
+ let from = get_from_section_from_select_query ( "SELECT * FROM (a NATURAL JOIN ((b)))" ) ;
126
+ assert_eq ! ( only( from) . relation, nest!( table( "a" ) , table( "b" ) ) ) ;
127
+ }
128
+
129
+ #[ test]
130
+ fn test_single_table_in_parenthesis_with_alias ( ) {
131
+ let sql = "SELECT * FROM (a NATURAL JOIN (b) c )" ;
132
+ let table_with_joins = only ( get_from_section_from_select_query ( sql) ) ;
133
+ assert_eq ! (
134
+ table_with_joins. relation,
135
+ nest!( table( "a" ) , table_with_alias( "b" , "c" ) )
136
+ ) ;
137
+
138
+ let sql = "SELECT * FROM (a NATURAL JOIN ((b)) c )" ;
139
+ let table_with_joins = only ( get_from_section_from_select_query ( sql) ) ;
140
+ assert_eq ! (
141
+ table_with_joins. relation,
142
+ nest!( table( "a" ) , table_with_alias( "b" , "c" ) )
143
+ ) ;
144
+
145
+ let sql = "SELECT * FROM (a NATURAL JOIN ( (b) c ) )" ;
146
+ let table_with_joins = only ( get_from_section_from_select_query ( sql) ) ;
147
+ assert_eq ! (
148
+ table_with_joins. relation,
149
+ nest!( table( "a" ) , table_with_alias( "b" , "c" ) )
150
+ ) ;
151
+
152
+ let sql = "SELECT * FROM (a NATURAL JOIN ( (b) as c ) )" ;
153
+ let table_with_joins = only ( get_from_section_from_select_query ( sql) ) ;
154
+ assert_eq ! (
155
+ table_with_joins. relation,
156
+ nest!( table( "a" ) , table_with_alias( "b" , "c" ) )
157
+ ) ;
158
+
159
+ let sql = "SELECT * FROM (a alias1 NATURAL JOIN ( (b) c ) )" ;
160
+ let table_with_joins = only ( get_from_section_from_select_query ( sql) ) ;
161
+ assert_eq ! (
162
+ table_with_joins. relation,
163
+ nest!( table_with_alias( "a" , "alias1" ) , table_with_alias( "b" , "c" ) )
164
+ ) ;
99
165
100
- assert_eq ! ( from. relation, nest!( table( "a" ) , nest!( table( "b" ) ) ) ) ;
166
+ let sql = "SELECT * FROM (a as alias1 NATURAL JOIN ( (b) as c ) )" ;
167
+ let table_with_joins = only ( get_from_section_from_select_query ( sql) ) ;
168
+ assert_eq ! (
169
+ table_with_joins. relation,
170
+ nest!( table_with_alias( "a" , "alias1" ) , table_with_alias( "b" , "c" ) )
171
+ ) ;
172
+
173
+ let res = snowflake ( ) . parse_sql_statements ( "SELECT * FROM (a NATURAL JOIN b) c" ) ;
174
+ assert_eq ! (
175
+ ParserError :: ParserError ( "Expected end of statement, found: c" . to_string( ) ) ,
176
+ res. unwrap_err( )
177
+ ) ;
101
178
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 ) ;
179
+ let res = snowflake ( ) . parse_sql_statements ( "SELECT * FROM (a b) c" ) ;
180
+ assert_eq ! (
181
+ ParserError :: ParserError ( "duplicate alias b" . to_string( ) ) ,
182
+ res. unwrap_err( )
183
+ ) ;
184
+ }
106
185
107
- assert_eq ! ( from. relation, nest!( table( "a" ) , nest!( nest!( table( "b" ) ) ) ) ) ;
186
+ fn snowflake ( ) -> TestedDialects {
187
+ TestedDialects {
188
+ dialects : vec ! [ Box :: new( SnowflakeDialect { } ) ] ,
189
+ }
108
190
}
109
191
110
192
fn snowflake_and_generic ( ) -> TestedDialects {
0 commit comments