forked from sqlfluff/sqlfluff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AL07.yml
491 lines (464 loc) · 11.1 KB
/
AL07.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
rule: AL07
test_pass_allow_self_join_alias:
# AL07 Allow self-joins
pass_str: |
select
x.a,
x_2.b
from x
left join x as x_2 on x.foreign_key = x.foreign_key
configs:
rules:
aliasing.forbid:
force_enable: true
test_fail_avoid_aliases_1:
fail_str: |
SELECT
u.id,
c.first_name,
c.last_name,
COUNT(o.user_id)
FROM users as u
JOIN customers as c on u.id = c.user_id
JOIN orders as o on u.id = o.user_id;
fix_str: |
SELECT
users.id,
customers.first_name,
customers.last_name,
COUNT(orders.user_id)
FROM users
JOIN customers on users.id = customers.user_id
JOIN orders on users.id = orders.user_id;
configs:
rules:
aliasing.forbid:
force_enable: true
test_fail_avoid_aliases_2:
# AL07 order by
fail_str: |
SELECT
u.id,
c.first_name,
c.last_name,
COUNT(o.user_id)
FROM users as u
JOIN customers as c on u.id = c.user_id
JOIN orders as o on u.id = o.user_id
order by o.user_id desc
fix_str: |
SELECT
users.id,
customers.first_name,
customers.last_name,
COUNT(orders.user_id)
FROM users
JOIN customers on users.id = customers.user_id
JOIN orders on users.id = orders.user_id
order by orders.user_id desc
configs:
rules:
aliasing.forbid:
force_enable: true
test_fail_avoid_aliases_3:
# AL07 order by identifier which is the same raw as an alias but refers to a column
fail_str: |
SELECT
u.id,
c.first_name,
c.last_name,
COUNT(o.user_id)
FROM users as u
JOIN customers as c on u.id = c.user_id
JOIN orders as o on u.id = o.user_id
order by o desc
fix_str: |
SELECT
users.id,
customers.first_name,
customers.last_name,
COUNT(orders.user_id)
FROM users
JOIN customers on users.id = customers.user_id
JOIN orders on users.id = orders.user_id
order by o desc
configs:
rules:
aliasing.forbid:
force_enable: true
alias_single_char_identifiers:
fail_str: "select b from tbl as a"
fix_str: "select b from tbl"
configs:
rules:
aliasing.forbid:
force_enable: true
alias_with_wildcard_identifier:
fail_str: "select * from tbl as a"
fix_str: "select * from tbl"
configs:
rules:
aliasing.forbid:
force_enable: true
select_from_values:
pass_str: |
select *
from values(1, 2, 3)
configs:
rules:
aliasing.forbid:
force_enable: true
select_from_table_generator:
pass_str: |
select *
from table(
generator(
rowcount=>10000
)
)
configs:
core:
dialect: snowflake
rules:
aliasing.forbid:
force_enable: true
issue_635:
pass_str: |
select
id::varchar as id,
obj:userid::varchar as user_id,
redemptions.value:awardedreceiptid::varchar as awarded_receipt_id
from
a,
lateral flatten(input => a.obj:redemptions) redemptions
configs:
core:
dialect: snowflake
rules:
aliasing.forbid:
force_enable: true
# This query was causing a runtime error in the rule.
issue_239:
pass_str: |
WITH
confusion_matrix AS (
SELECT
expected_label,
commerce,
digital,
traditional_services
FROM
ML.CONFUSION_MATRIX(MODEL model3,
(
SELECT
*
FROM
table1
WHERE
training = 0 )))
SELECT
*,
commerce pct_commerce
FROM
confusion_matrix
configs:
core:
dialect: bigquery
# The rule was removing the aliases from this query, causing incorrect behavior.
# (Aliases may not only be used in select targets; they also influence whether
# multiple joins to a table are independent or not).
issue_610:
pass_str: |
SELECT aaaaaa.c
FROM aaaaaa
JOIN bbbbbb AS b ON b.a = aaaaaa.id
JOIN bbbbbb AS b2 ON b2.other = b.id
configs:
rules:
aliasing.forbid:
force_enable: true
issue_1589:
pass_str: |
select *
from (select random() as v from (values(1))) t1,
(select max(repl) as m from data) t2,
(select * from data
where repl=t2.m and
rnd>=t.v
order by rnd
limit 1)
configs:
rules:
aliasing.forbid:
force_enable: true
issue_1639:
fail_str: |
DECLARE @VariableE date = GETDATE()
CREATE TABLE #TempTable
AS
(
Select ColumnD
from SchemaA.TableB AliasC
where ColumnD >= @VariableE
)
fix_str: |
DECLARE @VariableE date = GETDATE()
CREATE TABLE #TempTable
AS
(
Select ColumnD
from SchemaA.TableB
where ColumnD >= @VariableE
)
configs:
core:
dialect: tsql
rules:
aliasing.forbid:
force_enable: true
test_fail_no_copy_code_out_of_template:
# The rule wants to replace "t" with "foobar", but
# LintFix.has_template_conflicts() correctly prevents it copying code out
# of the templated region. Hence, the query is not modified.
fail_str: |
SELECT t.repo_id
FROM {{ source_table }} AS t
configs:
templater:
jinja:
context:
source_table: foobar
rules:
aliasing.forbid:
force_enable: true
test_bigquery_skip_multipart_names:
pass_str: |
SELECT t.col1
FROM shema1.table1 AS t
configs:
core:
dialect: bigquery
test_bigquery_force_enable:
fail_str: |
SELECT t.col1
FROM schema1.table1 AS t
# TRICKY: The fix_str does not parse in the real BigQuery, due to backtick
# requirements. That's why the rule is disabled by default.
# TODO (low priority): Update this test to test for a case where the rule
# produces valid SQL.
fix_str: |
SELECT schema1.table1.col1
FROM schema1.table1
configs:
core:
dialect: bigquery
rules:
aliasing.forbid:
force_enable: true
test_violation_locations:
fail_str: |
SELECT
u.id,
c.first_name,
c.last_name,
COUNT(o.user_id)
FROM users as u
JOIN customers as c on u.id = c.user_id
JOIN orders as o on u.id = o.user_id;
fix_str: |
SELECT
users.id,
customers.first_name,
customers.last_name,
COUNT(orders.user_id)
FROM users
JOIN customers on users.id = customers.user_id
JOIN orders on users.id = orders.user_id;
configs:
rules:
aliasing.forbid:
force_enable: true
violations:
- code: AL07
description: Avoid aliases in from clauses and join conditions.
name: aliasing.forbid
warning: false
start_line_no: 6
start_line_pos: 15
start_file_pos: 87
end_line_no: 6
end_line_pos: 16
end_file_pos: 88
fixes:
- edit: ''
end_file_pos: 88
end_line_no: 6
end_line_pos: 16
start_file_pos: 84
start_line_no: 6
start_line_pos: 12
type: delete
- edit: ''
end_file_pos: 84
end_line_no: 6
end_line_pos: 12
start_file_pos: 83
start_line_no: 6
start_line_pos: 11
type: delete
- edit: users
end_file_pos: 88
end_line_no: 6
end_line_pos: 16
start_file_pos: 87
start_line_no: 6
start_line_pos: 15
type: replace
- edit: users
end_file_pos: 12
end_line_no: 2
end_line_pos: 6
start_file_pos: 11
start_line_no: 2
start_line_pos: 5
type: replace
- edit: users
end_file_pos: 113
end_line_no: 7
end_line_pos: 25
start_file_pos: 112
start_line_no: 7
start_line_pos: 24
type: replace
- edit: users
end_file_pos: 150
end_line_no: 8
end_line_pos: 22
start_file_pos: 149
start_line_no: 8
start_line_pos: 21
type: replace
- code: AL07
description: Avoid aliases in from clauses and join conditions.
name: aliasing.forbid
warning: false
start_line_no: 7
start_line_pos: 19
start_file_pos: 107
end_line_no: 7
end_line_pos: 20
end_file_pos: 108
fixes:
- edit: ''
end_file_pos: 108
end_line_no: 7
end_line_pos: 20
start_file_pos: 104
start_line_no: 7
start_line_pos: 16
type: delete
- edit: ''
end_file_pos: 104
end_line_no: 7
end_line_pos: 16
start_file_pos: 103
start_line_no: 7
start_line_pos: 15
type: delete
- edit: customers
end_file_pos: 108
end_line_no: 7
end_line_pos: 20
start_file_pos: 107
start_line_no: 7
start_line_pos: 19
type: replace
- edit: customers
end_file_pos: 22
end_line_no: 3
end_line_pos: 6
start_file_pos: 21
start_line_no: 3
start_line_pos: 5
type: replace
- edit: customers
end_file_pos: 40
end_line_no: 4
end_line_pos: 6
start_file_pos: 39
start_line_no: 4
start_line_pos: 5
type: replace
- edit: customers
end_file_pos: 120
end_line_no: 7
end_line_pos: 32
start_file_pos: 119
start_line_no: 7
start_line_pos: 31
type: replace
- code: AL07
description: Avoid aliases in from clauses and join conditions.
name: aliasing.forbid
warning: false
start_line_no: 8
start_line_pos: 16
start_file_pos: 144
end_line_no: 8
end_line_pos: 17
end_file_pos: 145
fixes:
- edit: ''
end_file_pos: 145
end_line_no: 8
end_line_pos: 17
start_file_pos: 141
start_line_no: 8
start_line_pos: 13
type: delete
- edit: ''
end_file_pos: 141
end_line_no: 8
end_line_pos: 13
start_file_pos: 140
start_line_no: 8
start_line_pos: 12
type: delete
- edit: orders
end_file_pos: 145
end_line_no: 8
end_line_pos: 17
start_file_pos: 144
start_line_no: 8
start_line_pos: 16
type: replace
- edit: orders
end_file_pos: 63
end_line_no: 5
end_line_pos: 12
start_file_pos: 62
start_line_no: 5
start_line_pos: 11
type: replace
- edit: orders
end_file_pos: 157
end_line_no: 8
end_line_pos: 29
start_file_pos: 156
start_line_no: 8
start_line_pos: 28
type: replace
test_fail_fix_command:
# Test originally from commands_test.py
fail_str: |
SELECT u.id, c.first_name, c.last_name, COUNT(o.user_id)
FROM users as u JOIN customers as c on u.id = c.user_id JOIN orders as o
on u.id = o.user_id;
fix_str: |
SELECT users.id, customers.first_name, customers.last_name, COUNT(orders.user_id)
FROM users JOIN customers on users.id = customers.user_id JOIN orders
on users.id = orders.user_id;
configs:
rules:
aliasing.forbid:
force_enable: true