forked from apache/datafusion-sqlparser-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsqlparser_redshift.rs
384 lines (362 loc) · 12.4 KB
/
sqlparser_redshift.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you 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_use]
mod test_utils;
use sqlparser::tokenizer::Span;
use test_utils::*;
use sqlparser::ast::*;
use sqlparser::dialect::GenericDialect;
use sqlparser::dialect::RedshiftSqlDialect;
#[test]
fn test_square_brackets_over_db_schema_table_name() {
let select = redshift().verified_only_select("SELECT [col1] FROM [test_schema].[test_table]");
assert_eq!(
select.projection[0],
SelectItem::UnnamedExpr(Expr::Identifier(Ident {
value: "col1".to_string(),
quote_style: Some('['),
span: Span::empty(),
})),
);
assert_eq!(
select.from[0],
TableWithJoins {
relation: table_from_name(ObjectName(vec![
Ident {
value: "test_schema".to_string(),
quote_style: Some('['),
span: Span::empty(),
},
Ident {
value: "test_table".to_string(),
quote_style: Some('['),
span: Span::empty(),
}
])),
joins: vec![],
}
);
}
#[test]
fn brackets_over_db_schema_table_name_with_whites_paces() {
match redshift().parse_sql_statements("SELECT [ col1 ] FROM [ test_schema].[ test_table]") {
Ok(statements) => {
assert_eq!(statements.len(), 1);
}
_ => unreachable!(),
}
}
#[test]
fn test_double_quotes_over_db_schema_table_name() {
let select =
redshift().verified_only_select("SELECT \"col1\" FROM \"test_schema\".\"test_table\"");
assert_eq!(
select.projection[0],
SelectItem::UnnamedExpr(Expr::Identifier(Ident {
value: "col1".to_string(),
quote_style: Some('"'),
span: Span::empty(),
})),
);
assert_eq!(
select.from[0],
TableWithJoins {
relation: table_from_name(ObjectName(vec![
Ident {
value: "test_schema".to_string(),
quote_style: Some('"'),
span: Span::empty(),
},
Ident {
value: "test_table".to_string(),
quote_style: Some('"'),
span: Span::empty(),
}
])),
joins: vec![],
}
);
}
#[test]
fn parse_delimited_identifiers() {
// check that quoted identifiers in any position remain quoted after serialization
let select = redshift().verified_only_select(
r#"SELECT "alias"."bar baz", "myfun"(), "simple id" AS "column alias" FROM "a table" AS "alias""#,
);
// check FROM
match only(select.from).relation {
TableFactor::Table {
name,
alias,
args,
with_hints,
version,
..
} => {
assert_eq!(vec![Ident::with_quote('"', "a table")], name.0);
assert_eq!(Ident::with_quote('"', "alias"), alias.unwrap().name);
assert!(args.is_none());
assert!(with_hints.is_empty());
assert!(version.is_none());
}
_ => panic!("Expecting TableFactor::Table"),
}
// check SELECT
assert_eq!(3, select.projection.len());
assert_eq!(
&Expr::CompoundIdentifier(vec![
Ident::with_quote('"', "alias"),
Ident::with_quote('"', "bar baz"),
]),
expr_from_projection(&select.projection[0]),
);
assert_eq!(
&Expr::Function(Function {
name: ObjectName(vec![Ident::with_quote('"', "myfun")]),
uses_odbc_syntax: false,
parameters: FunctionArguments::None,
args: FunctionArguments::List(FunctionArgumentList {
duplicate_treatment: None,
args: vec![],
clauses: vec![],
}),
null_treatment: None,
filter: None,
over: None,
within_group: vec![],
}),
expr_from_projection(&select.projection[1]),
);
match &select.projection[2] {
SelectItem::ExprWithAlias { expr, alias } => {
assert_eq!(&Expr::Identifier(Ident::with_quote('"', "simple id")), expr);
assert_eq!(&Ident::with_quote('"', "column alias"), alias);
}
_ => panic!("Expected ExprWithAlias"),
}
redshift().verified_stmt(r#"CREATE TABLE "foo" ("bar" "int")"#);
// An alias starting with a number
redshift().verified_stmt(r#"CREATE TABLE "foo" ("1" INT)"#);
redshift().verified_stmt(r#"ALTER TABLE foo ADD CONSTRAINT "bar" PRIMARY KEY (baz)"#);
//TODO verified_stmt(r#"UPDATE foo SET "bar" = 5"#);
}
fn redshift() -> TestedDialects {
TestedDialects::new(vec![Box::new(RedshiftSqlDialect {})])
}
fn redshift_and_generic() -> TestedDialects {
TestedDialects::new(vec![
Box::new(RedshiftSqlDialect {}),
Box::new(GenericDialect {}),
])
}
#[test]
fn test_sharp() {
let sql = "SELECT #_of_values";
let select = redshift().verified_only_select(sql);
assert_eq!(
SelectItem::UnnamedExpr(Expr::Identifier(Ident::new("#_of_values"))),
select.projection[0]
);
}
#[test]
fn test_create_view_with_no_schema_binding() {
redshift_and_generic()
.verified_stmt("CREATE VIEW myevent AS SELECT eventname FROM event WITH NO SCHEMA BINDING");
}
#[test]
fn test_redshift_json_path() {
let dialects = all_dialects_where(|d| d.supports_partiql());
let sql = "SELECT cust.c_orders[0].o_orderkey FROM customer_orders_lineitem";
let select = dialects.verified_only_select(sql);
assert_eq!(
&Expr::JsonAccess {
value: Box::new(Expr::CompoundIdentifier(vec![
Ident::new("cust"),
Ident::new("c_orders")
])),
path: JsonPath {
path: vec![
JsonPathElem::Bracket {
key: Expr::Value(number("0"))
},
JsonPathElem::Dot {
key: "o_orderkey".to_string(),
quoted: false
}
]
}
},
expr_from_projection(only(&select.projection))
);
let sql = "SELECT cust.c_orders[0]['id'] FROM customer_orders_lineitem";
let select = dialects.verified_only_select(sql);
assert_eq!(
&Expr::JsonAccess {
value: Box::new(Expr::CompoundIdentifier(vec![
Ident::new("cust"),
Ident::new("c_orders")
])),
path: JsonPath {
path: vec![
JsonPathElem::Bracket {
key: Expr::Value(number("0"))
},
JsonPathElem::Bracket {
key: Expr::Value(Value::SingleQuotedString("id".to_owned()))
}
]
}
},
expr_from_projection(only(&select.projection))
);
let sql = "SELECT db1.sc1.tbl1.col1[0]['id'] FROM customer_orders_lineitem";
let select = dialects.verified_only_select(sql);
assert_eq!(
&Expr::JsonAccess {
value: Box::new(Expr::CompoundIdentifier(vec![
Ident::new("db1"),
Ident::new("sc1"),
Ident::new("tbl1"),
Ident::new("col1")
])),
path: JsonPath {
path: vec![
JsonPathElem::Bracket {
key: Expr::Value(number("0"))
},
JsonPathElem::Bracket {
key: Expr::Value(Value::SingleQuotedString("id".to_owned()))
}
]
}
},
expr_from_projection(only(&select.projection))
);
let sql = r#"SELECT db1.sc1.tbl1.col1[0]."id" FROM customer_orders_lineitem"#;
let select = dialects.verified_only_select(sql);
assert_eq!(
&Expr::JsonAccess {
value: Box::new(Expr::CompoundIdentifier(vec![
Ident::new("db1"),
Ident::new("sc1"),
Ident::new("tbl1"),
Ident::new("col1")
])),
path: JsonPath {
path: vec![
JsonPathElem::Bracket {
key: Expr::Value(number("0"))
},
JsonPathElem::Dot {
key: "id".to_string(),
quoted: true,
}
]
}
},
expr_from_projection(only(&select.projection))
);
}
#[test]
fn test_parse_json_path_from() {
let dialects = all_dialects_where(|d| d.supports_partiql());
let select = dialects.verified_only_select("SELECT * FROM src[0].a AS a");
match &select.from[0].relation {
TableFactor::Table {
name, json_path, ..
} => {
assert_eq!(name, &ObjectName(vec![Ident::new("src")]));
assert_eq!(
json_path,
&Some(JsonPath {
path: vec![
JsonPathElem::Bracket {
key: Expr::Value(number("0"))
},
JsonPathElem::Dot {
key: "a".to_string(),
quoted: false
}
]
})
);
}
_ => panic!(),
}
let select = dialects.verified_only_select("SELECT * FROM src[0].a[1].b AS a");
match &select.from[0].relation {
TableFactor::Table {
name, json_path, ..
} => {
assert_eq!(name, &ObjectName(vec![Ident::new("src")]));
assert_eq!(
json_path,
&Some(JsonPath {
path: vec![
JsonPathElem::Bracket {
key: Expr::Value(number("0"))
},
JsonPathElem::Dot {
key: "a".to_string(),
quoted: false
},
JsonPathElem::Bracket {
key: Expr::Value(Value::Number("1".parse().unwrap(), false))
},
JsonPathElem::Dot {
key: "b".to_string(),
quoted: false
},
]
})
);
}
_ => panic!(),
}
let select = dialects.verified_only_select("SELECT * FROM src.a.b");
match &select.from[0].relation {
TableFactor::Table {
name, json_path, ..
} => {
assert_eq!(
name,
&ObjectName(vec![Ident::new("src"), Ident::new("a"), Ident::new("b")])
);
assert_eq!(json_path, &None);
}
_ => panic!(),
}
}
#[test]
fn test_parse_select_numbered_columns() {
// An alias starting with a number
redshift_and_generic().verified_stmt(r#"SELECT 1 AS "1" FROM a"#);
redshift_and_generic().verified_stmt(r#"SELECT 1 AS "1abc" FROM a"#);
}
#[test]
fn test_parse_nested_quoted_identifier() {
redshift().verified_stmt(r#"SELECT 1 AS ["1"] FROM a"#);
redshift().verified_stmt(r#"SELECT 1 AS ["[="] FROM a"#);
redshift().verified_stmt(r#"SELECT 1 AS ["=]"] FROM a"#);
redshift().verified_stmt(r#"SELECT 1 AS ["a[b]"] FROM a"#);
// trim spaces
redshift().one_statement_parses_to(r#"SELECT 1 AS [ " 1 " ]"#, r#"SELECT 1 AS [" 1 "]"#);
// invalid query
assert!(redshift()
.parse_sql_statements(r#"SELECT 1 AS ["1]"#)
.is_err());
}