forked from sqlfluff/sqlfluff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AM04.yml
619 lines (529 loc) · 11.1 KB
/
AM04.yml
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
rule: AM04
test_pass_known_number_of_result_columns_1:
pass_str: select a, b from t
test_fail_unknown_number_of_result_columns_1:
fail_str: select * from t
test_pass_known_number_of_result_columns_2:
desc: Columns are specified in CTE so * in final query will return only columns specified earlier.
pass_str: |
with cte as (
select
a, b
from
t
)
select * from cte
test_fail_unknown_number_of_result_columns_2:
fail_str: |
with cte as (
select
*
from
t
)
select * from cte
test_pass_known_number_of_result_columns_3:
pass_str: |
with cte as (
select
*
from
t
)
select a, b from cte
test_pass_known_number_of_result_columns_4:
desc: |
CTE1 has * but columns are specified in final select.
CTE2 has columns specified so cte2.* in final select will return only columns specified in CTE2's body.
pass_str: |
with cte1 as (
select
*
from
t
),
cte2 as (
select
a, b
from
t
)
select
cte1.a,
cte2.*
from cte1
join cte2
using (a)
test_fail_unknown_number_of_result_columns_3:
fail_str: |
with cte1 as (
select
*
from
t
),
cte2 as (
select
a, b
from
t
)
select
cte1.*,
cte2.*
from cte1
join cte2
using (a)
test_pass_known_number_of_result_columns_5:
desc: Columns specified in subquery so * in final select will return only those columns.
pass_str: |
select * from (
select
a, b
from
t
)
test_fail_unknown_number_of_result_columns_4:
desc: Select t.* will return unknown number of columns.
fail_str: |
with cte as (
select
a, b
from
t
)
select
cte.*,
t.*
from cte1
join t
using (a)
test_fail_unknown_number_of_result_columns_5:
desc: Select t_alias.* will return unknown number of columns since the * is used in subquery.
fail_str: |
with cte as (
select
a, b
from
t
)
select
cte.*,
t_alias.*
from cte1
join (select * from t) as t_alias
using (a)
test_pass_known_number_of_result_columns_6:
desc: Select t_alias.* will return known number of columns since they are defined in subquery.
pass_str: |
select
t_alias.*
from cte1
join (select a from t) as t_alias
using (a)
test_fail_unknown_number_of_result_columns_6:
fail_str: |
select
t_alias.*
from t1
join (select * from t) as t_alias
using (a)
test_pass_known_number_of_result_columns_7:
pass_str: |
with cte as (
select
a, b
from
t
)
select
cte.*,
t_alias.a
from cte1
join (select * from t) as t_alias
using (a)
test_fail_unknown_number_of_result_columns_7:
fail_str: select *, t.*, t.a, b from t
test_pass_known_number_of_result_columns_8:
pass_str: |
select a from t1
union all
select b from t2
test_fail_unknown_number_of_result_columns_8:
fail_str: |
select a from t1
union all
select * from t2
test_fail_unknown_number_of_result_columns_9:
fail_str: |
select * from t1
union all
select b from t2
test_fail_unknown_number_of_result_columns_10:
fail_str: |
with cte as (
select * from t1
union all
select b from t2
)
select * from cte
union all
select b from t3
test_pass_known_number_of_result_columns_9:
pass_str: |
with cte as (
select a from t1
union all
select b from t2
)
select * from cte
union all
select b from t3
test_pass_known_number_of_result_columns_10:
desc: Columns are specified in cte_orders's body so cte_orders.* will return only these columns.
pass_str: |
WITH cte_orders AS (
SELECT customer_id, total
FROM orders
)
SELECT customers.name, cte_orders.*
FROM
customers,
cte_orders
WHERE
clients.id = orders.clientId
test_pass_known_number_of_result_columns_11:
desc: Columns are specified in cte_orders's body so cte_orders.* will return only these columns.
pass_str: |
WITH cte_orders AS (
SELECT customer_id, total
FROM orders
)
SELECT *
FROM
cte_orders AS orders
test_fail_unknown_number_of_result_columns_11:
fail_str: |
WITH cte_orders AS (
SELECT *
FROM orders
)
SELECT *
FROM
cte_orders AS orders
test_fail_unknown_number_of_result_columns_12:
desc: CTE is unused. We select * from orders table which means it's unknown what columns will be returned.
fail_str: |
WITH cte_orders AS (
SELECT customer_id, total
FROM orders
)
SELECT *
FROM
orders AS cte_orders
test_fail_unknown_number_of_result_columns_13:
fail_str: SELECT p.* FROM races, UNNEST(participants) AS p
test_pass_known_number_of_result_columns_12:
pass_str: SELECT p FROM races, UNNEST(participants) AS p
test_fail_unknown_number_of_result_columns_14:
fail_str: SELECT * FROM a JOIN b
test_fail_unknown_number_of_result_columns_15:
desc: We know what columns will cte return but we don't know what columns will be returned from joined table b.
fail_str: |
WITH cte AS (
SELECT a
FROM t
)
SELECT *
FROM cte
JOIN b
test_pass_known_number_of_result_columns_13:
desc: Both CTEs define returned columns so * in final select will return known number of columns.
pass_str: |
WITH cte1 AS (
SELECT a
FROM t
),
cte2 AS (
SELECT b
FROM u
)
SELECT *
FROM cte1
JOIN cte2
test_pass_known_number_of_result_columns_14:
pass_str: select a, b from `d.t`
configs:
core:
dialect: bigquery
test_fail_unknown_number_of_result_columns_16:
fail_str: select * from `d.t`
configs:
core:
dialect: bigquery
test_pass_known_number_of_result_columns_15:
# Issue 915: Crash on statements that don't have a SELECT
pass_str: CREATE TABLE my_table (id INTEGER)
test_fail_unknown_number_of_result_columns_17:
# Issue 930: Infinite recursion if CTE queries itself.
fail_str: |
with
hubspot__engagement_calls as (
select * from hubspot__engagement_calls
)
select * from hubspot__engagement_calls
test_fail_unknown_number_of_result_columns_18:
# Another test for issue #930
fail_str: |
with
hubspot__contacts as (
select * from ANALYTICS.PUBLIC_intermediate.hubspot__contacts
),
final as (
select *
from
hubspot__contacts
where not coalesce(_fivetran_deleted, false)
)
select * from final
test_pass_nested_ctes_1:
# Test for issue 1984
pass_str: |
with a as (
with b as (
select 1 from c
)
select * from b
)
select * from a
test_fail_nested_ctes_1:
# Test for issue 1984
fail_str: |
with a as (
with b as (
select * from c
)
select * from b
)
select * from a
test_fail_nested_ctes_2:
# Test for issue 1984
fail_str: |
with a as (
with b as (
select 1 from t1
),
c AS (
SELECT *
FROM u
)
select b.*, c.* from b join c
)
select * from a
test_pass_nested_ctes_3:
# Test for issue 1984
pass_str:
with a as (
with b as (
select * from c
)
select 1 from b
)
select * from a
test_pass_nested_ctes_4:
# Test for issue 1984
pass_str:
with a as (
with b as (
select * from c
)
select * from b
)
select 1 from a
test_cte_reference_outer_5:
pass_str:
with a as (
select 1 from b
)
select * from (
select * from a
)
test_cte_tricky_nesting_6:
pass_str:
with b as (
select 1 from c
)
select * from (
with a as (
select * from b
)
select * from a
)
test_nested_and_same_level_ctes_7:
pass_str:
with a as (
with c as (
select 1 from d
),
b as (
select * from c
)
select * from b
)
select * from a
test_nested_cte_references_outer_8:
pass_str:
with c as (
select 1 from d
),
a as (
with b as (
select * from c
)
select * from b
)
select * from a
test_pass_join_inside_cte_with_unqualified:
pass_str:
with cte as (
select
*
from
t1
inner join t2
)
select a, b from cte;
test_pass_known_number_of_columns_in_two_join_subqueries:
pass_str:
select
*
from (
select a
from foo
) t1
inner join (
select b
from bar
) t2;
test_fail_two_join_subqueries_one_with_unknown_number_of_columns:
fail_str:
select
*
from (
select *
from foo
) t1
inner join (
select b
from bar
) t2;
test_fail_no_source_table:
fail_str: |
SELECT *
test_query_on_snowflake_stage:
pass_str:
select mycolumn1
from @public.mytable1
configs:
core:
dialect: snowflake
test_snowflake_delete_cte:
pass_str: |
DELETE FROM MYTABLE1
USING (
WITH MYCTE AS (SELECT COLUMN2 FROM MYTABLE3)
SELECT COLUMN3 FROM MYTABLE3
) X
WHERE COLUMN1 = X.COLUMN3
configs:
core:
dialect: snowflake
test_pass_exasol_values_clause_cte_1:
pass_str: |
WITH txt AS (
VALUES (1)
AS t (id)
)
SELECT *
FROM txt
configs:
core:
dialect: exasol
test_pass_exasol_values_clause_cte_2:
pass_str: |
WITH txt AS (
VALUES (1, 'foo')
AS t (id, name)
)
SELECT *
FROM txt
configs:
core:
dialect: exasol
test_pass_exasol_values_clause:
pass_str:
SELECT *
FROM (
VALUES (1, 2), (3, 4)
) AS t(c1, c2)
configs:
core:
dialect: exasol
test_exasol_invalid_foreign_key_from:
pass_str: |
SELECT *
WITH INVALID FOREIGN KEY (nr)
FROM T1 REFERENCING T2 (id)
configs:
core:
dialect: exasol
test_pass_cte_no_select_final_statement:
pass_str:
WITH mycte AS (
SELECT
foo,
bar
FROM mytable1
)
UPDATE sometable
SET
sometable.baz = mycte.bar
FROM
mycte;
test_tsql_select_system_as_identifier:
pass_str: |
SELECT @@IDENTITY AS 'Identity'
configs:
core:
dialect: tsql
test_pass_sparksql_values_clause_cte_1:
pass_str: |
WITH txt AS (
VALUES (1)
AS t (id)
)
SELECT *
FROM txt
configs:
core:
dialect: sparksql
test_pass_sparksql_values_clause_cte_2:
pass_str: |
WITH txt AS (
VALUES (1, 'foo')
AS t (id, name)
)
SELECT *
FROM txt
configs:
core:
dialect: sparksql
test_pass_sparksql_values_clause:
pass_str:
SELECT *
FROM (
VALUES (1, 2), (3, 4)
) AS t(c1, c2)
configs:
core:
dialect: sparksql