forked from sqlfluff/sqlfluff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LT05.yml
711 lines (653 loc) · 21 KB
/
LT05.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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
rule: LT05
test_pass_line_too_long_config_override:
# Long lines (with config override)
pass_str: "SELECT COUNT(*) FROM tbl\n"
configs:
core:
max_line_length: 30
test_fail_line_too_long_with_comments_1:
# Check we move comments correctly
fail_str: "SELECT 1 -- Some Comment\n"
fix_str: "-- Some Comment\nSELECT 1\n"
configs:
core:
max_line_length: 18
test_fail_line_too_long_with_comments_1_after:
# Check we move comments correctly
fail_str: "SELECT 1 -- Some Comment\n"
fix_str: "SELECT 1\n-- Some Comment\n"
configs:
core:
max_line_length: 18
indentation:
trailing_comments: after
test_fail_line_too_long_with_comments_1_no_newline:
# Check we move comments correctly, and that it
# still works when there isn't a trailing newline.
# https://github.com/sqlfluff/sqlfluff/issues/4386
fail_str: "SELECT 1 -- Some Comment"
fix_str: "-- Some Comment\nSELECT 1"
configs:
core:
max_line_length: 18
test_fail_line_too_long_with_comments_2:
# Check we can add newlines after dedents (with an indent).
# NOTE: That for LT05, we don't repair the initial indent
# but that the following lines will be fixed as though it
# has been corrected. Ideally LT02 would have been run _first_
# on this file.
fail_str: " SELECT COUNT(*) FROM tbl\n"
fix_str: " SELECT COUNT(*)\nFROM tbl\n"
configs:
core:
max_line_length: 20
test_fail_line_too_long_with_comments_3:
# Check priority of fixes
fail_str: "SELECT COUNT(*) FROM tbl -- Some Comment\n"
fix_str: "-- Some Comment\nSELECT COUNT(*)\nFROM tbl\n"
configs:
core:
max_line_length: 18
test_fail_line_too_long_with_comments_4:
# In this case, the inline comment is NOT on a line by itself (note the
# leading comma), but even if we move it onto a line by itself, it's still
# too long. In this case, the rule should do nothing, otherwise it triggers
# an endless cycle of "fixes" that simply keeps adding blank lines.
fail_str: |
SELECT
c1
,-- the "y variable" and uses_small_subject_line to be the "x variable" in terms of the regression line.
c2
configs:
core:
max_line_length: 80
test_pass_line_too_long_with_comments_ignore_comment_lines:
# Same case as above, but should pass as ignore_comment_lines is set to true
pass_str: |
SELECT
c1
,-- the "y variable" and uses_small_subject_line to be the "x variable" in terms of the regression line.
c2
configs:
core:
max_line_length: 80
rules:
layout.long_lines:
ignore_comment_lines: true
test_fail_line_too_long_only_comments:
# Check long lines that are only comments are linted correctly
fail_str: "-- Some really long comments on their own line\n\nSELECT 1"
configs:
core:
max_line_length: 18
test_fail_line_too_long_handling_indents:
# Check we handle indents nicely
fail_str: "SELECT 12345\n"
fix_str: "SELECT\n 12345\n"
configs:
core:
max_line_length: 10
test_pass_line_too_long_ignore_comments_true:
# Check we can ignore comments if we want
pass_str: "SELECT 1\n-- Some long comment over 10 characters\n"
configs:
core:
max_line_length: 10
rules:
layout.long_lines:
ignore_comment_lines: true
test_pass_line_too_long_ignore_comments_false:
# Check we still pick up long comments if we don't want to ignore
fail_str: "SELECT 1\n-- Some long comment over 10 characters\n"
configs:
core:
max_line_length: 10
rules:
layout.long_lines:
ignore_comment_lines: false
test_compute_line_length_before_template_expansion_1:
# Line 3 is fine before expansion. Too long after expansion is NOT considered
# a violation.
pass_str: |
SELECT user_id
FROM
`{{bi_ecommerce_orders}}` {{table_at_job_start}}
configs:
core:
dialect: bigquery
templater:
jinja:
context:
table_at_job_start: FOR SYSTEM_TIME AS OF CAST('2021-03-02T01:22:59+00:00' AS TIMESTAMP)
bi_ecommerce_orders: bq-business-intelligence.user.ecommerce_orders
test_compute_line_length_before_template_expansion_2:
# Line 3 is too long before expansion. It's fine after expansion, but the rule
# does not look at that.
fail_str: |
SELECT user_id
FROM
`{{bi_ecommerce_orders_bi_ecommerce_orders}}` AS {{table_alias_table_alias_table_alias_table_alias_table_alias_table_alias}}
fix_str: |
SELECT user_id
FROM
`{{bi_ecommerce_orders_bi_ecommerce_orders}}`
AS {{table_alias_table_alias_table_alias_table_alias_table_alias_table_alias}}
violations_after_fix:
# Even after fixing, the final line is still too long.
- description: Line is too long (86 > 80).
line_no: 4
line_pos: 9
name: layout.long_lines
configs:
core:
dialect: bigquery
templater:
jinja:
context:
bi_ecommerce_orders_bi_ecommerce_orders: bq-business-intelligence.user.ecommerce_orders
table_alias_table_alias_table_alias_table_alias_table_alias_table_alias: t
test_long_jinja_comment:
fail_str: |
SELECT *
{# comment #}
{# ........................................................................... #}
FROM table
configs:
core:
max_line_length: 80
rules:
layout.long_lines:
ignore_comment_lines: false
test_long_jinja_comment_ignore:
# A Jinja comment is a comment.
pass_str: |
SELECT *
{# comment #}
{# ........................................................................... #}
FROM table
configs:
core:
max_line_length: 80
rules:
layout.long_lines:
ignore_comment_lines: true
test_for_loop:
# A Jinja for loop
pass_str: |
{% for elem in 'foo' %}
SELECT '{{ elem }}' FROM table1;
SELECT '{{ elem }}' FROM table2;
{% endfor %}
test_for_loop_repeating_elements_starts_with_literal:
# A Jinja for loop with repeating elements (that are difficult to match)
# but starting with a literal that can be used to match
pass_str: |
{% set elements = 'foo' %}
SELECT
CASE
{% for elem in elements %}
WHEN '{{ elem }}' = '' THEN 1
WHEN '{{ elem }}' = '' THEN 1
{% endfor %}
END
test_for_loop_starting_with_templated_piece:
# A Jinja for loop starting with non-literals
# But unique parts can be used to match
pass_str: |
{% set elements = 'foo' %}
{% set when = 'WHEN' %}
SELECT
CASE
{% for elem in elements %}
{{ when }} '{{ elem }}' = '' THEN 1
{{ when }} '{{ elem }}' = '' THEN 2
{% endfor %}
END
test_for_loop_fail_complex_match:
# A Jinja for loop starting with non-literals
# But non-unique parts which therefore cannot
# be used to match
pass_str: |
{% set elements = 'foo' %}
{% set when = 'WHEN' %}
SELECT
CASE
{% for elem in elements %}
{{ when }} '{{ elem }}' = '' THEN 1
{{ when }} '{{ elem }}' = '' THEN 1
{% endfor %}
END
test_for_loop_fail_simple_match:
# If for loop only contains literals it should still pass
pass_str: |
{% set elements = 'foo' %}
SELECT
CASE
{% for elem in elements %}
WHEN 'f' THEN a
{% endfor %}
END
test_set_statement:
# A Jinja set statement
pass_str: |
{% set statement = "SELECT 1 from table1;" %}
{{ statement }}{{ statement }}
configs:
core:
max_line_length: 80
test_issue_1666_line_too_long_unfixable_jinja:
# Note the trailing space at the end of line 1. This is a necessary part of
# the test, because the space (which is passed through to the output) was
# "tricking" LT05 into trying to split the line, then encountering an internal
# error.
fail_str: "{{ config (schema='bronze', materialized='view', sort =['id','number'], dist = 'all', tags =['longlonglonglonglong']) }} \n\nselect 1\n"
test_fail_ignore_comment_clauses_1:
# Too long, comment clause not ignored
fail_str: |
CREATE OR REPLACE TABLE mytable (
col1 NUMBER COMMENT 'col1 comment',
col2 BOOLEAN COMMENT 'very long comment line, exceeding max_line_lengthvery long comment line, exceeding max_line_length very long comment line, exceeding max_line_length very long comment line, exceeding max_line_length',
col3 NUMBER COMMENT 'col3 comment'
)
test_fail_ignore_comment_clauses_2:
# Too long even after ignoring comment clause
fail_str: |
CREATE OR REPLACE TABLE mytable (
col1 NUMBER COMMENT 'col1 comment',
colaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbcccccccccccccccddddddddddddddddeeeeeeeeeeeeeee2 BOOLEAN COMMENT 'very long comment line, exceeding max_line_lengthvery long comment line, exceeding max_line_length very long comment line, exceeding max_line_length very long comment line, exceeding max_line_length',
col3 NUMBER COMMENT 'col3 comment'
)
configs:
rules:
layout.long_lines:
ignore_comment_clauses: true
test_pass_ignore_comment_clauses:
pass_str: |
CREATE OR REPLACE TABLE mytable (
col1 NUMBER COMMENT 'col1 comment',
col2 BOOLEAN COMMENT 'very long comment line, exceeding max_line_lengthvery long comment line, exceeding max_line_length very long comment line, exceeding max_line_length very long comment line, exceeding max_line_length',
col3 NUMBER COMMENT 'col3 comment'
)
configs:
rules:
layout.long_lines:
ignore_comment_clauses: true
test_pass_ignore_comment_clauses_teradata:
pass_str: |
comment on table sandbox_db.Org_Descendant is 'very long comment line, exceeding max_line_lengthvery long comment line, exceeding max_line_length very long comment line, exceeding max_line_length very long comment line, exceeding max_line_length';
configs:
core:
dialect: teradata
rules:
layout.long_lines:
ignore_comment_clauses: true
test_pass_ignore_comment_clauses_exasol:
pass_str: |
CREATE TABLE IF NOT EXISTS SCHEM.TAB (
ID DECIMAL(18, 0) IDENTITY CONSTRAINT PRIMARY KEY DISABLE COMMENT IS 'without constraint name'
) COMMENT IS 'very long comment line, exceeding max_line_lengthvery long comment line, exceeding max_line_length very long comment line, exceeding max_line_length very long comment line, exceeding max_line_length';
configs:
core:
dialect: exasol
rules:
layout.long_lines:
ignore_comment_clauses: true
test_pass_ignore_comment_clauses_snowflake:
pass_str: |
CREATE TABLE foo_table (bar INTEGER) COMMENT = 'very long comment line, exceeding max_line_lengthvery long comment line, exceeding max_line_length very long comment line, exceeding max_line_length very long comment line, exceeding max_line_length'
configs:
core:
dialect: snowflake
rules:
layout.long_lines:
ignore_comment_clauses: true
test_pass_ignore_comment_clauses_postgres:
pass_str: |
CREATE TABLE IF NOT EXISTS foo
( id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
name TEXT NOT NULL
);
COMMENT ON TABLE foo IS 'Windows Phone 8, however, was never able to overcome a long string of disappointments for Microsoft. ';
configs:
core:
dialect: postgres
rules:
layout.long_lines:
ignore_comment_clauses: true
test_fail_templated_comment_line:
fail_str: |
SELECT *
{# ........................................................................... #}
FROM table
configs:
templater:
jinja:
context: {}
test_pass_ignore_templated_comment_lines:
# NOTE: This is potentially a behaviour change in 2.0.0.
# This was erroneously using the `ignore_comment_clauses`
# config when this query contains no comment clauses.
pass_str: |
SELECT *
{# ........................................................................... #}
FROM table
configs:
rules:
layout.long_lines:
ignore_comment_lines: true
templater:
jinja:
context: {}
test_fail_operator_precedence_1:
# Make sure we split at the + operator.
fail_str: |
select
ISNULL(count, '0') * 10000 + ISNULL(planned, 100)
from blah
fix_str: |
select
ISNULL(count, '0') * 10000
+ ISNULL(planned, 100)
from blah
configs:
core:
max_line_length: 30
test_fail_operator_precedence_2:
# Make sure we split at the AND operator.
fail_str: |
select
recommendation_list[ORDINAL(1)] = 'uses_small_subject_line' AND uses_small_subject_line != CAST(effect_size_list[ORDINAL(1)] AS FLOAT64)
from blah
fix_str: |
select
recommendation_list[ORDINAL(1)] = 'uses_small_subject_line'
AND uses_small_subject_line != CAST(effect_size_list[ORDINAL(1)] AS FLOAT64)
from blah
configs:
core:
max_line_length: 120
test_fail_operator_precedence_3:
# Stretching cases for operators and comma
fail_str: |
select
a, b + c, long_name + long_name * long_name - long_name as foo, long_name AND long_name OR long_name OR long_name as bar
from blah
fix_str: |
select
a,
b + c,
long_name
+ long_name * long_name
- long_name as foo,
long_name AND long_name
OR long_name
OR long_name as bar
from blah
configs:
core:
max_line_length: 30
test_pass_long_multiline_jinja:
# None of the lines are longer than 30
# but the whole tag is. It shouldn't
# cause issues.
pass_str: |
select
{{
1 + 2 + 3 + 4 + 5
+ 6 + 7 + 8 + 9 + 10
}}
from blah
configs:
core:
max_line_length: 30
test_fail_long_inline_statement:
# Tests that breaks happen between clauses properly
fail_str: |
select distinct a + b from c join d using (e) where f = g and h = i order by j
fix_str: |
select distinct a + b
from c
join d using (e)
where f = g and h = i
order by j
configs:
core:
max_line_length: 50
test_pass_check_off_1:
# Tests that we can disable the check (using 0).
pass_str: |
select my_really_really_really_really_really_really_really_really_really_really_really_long_var from tbl
configs:
core:
max_line_length: 0
test_pass_check_off_2:
# Tests that we can disable the check (using -1).
pass_str: |
select my_really_really_really_really_really_really_really_really_really_really_really_long_var from tbl
configs:
core:
max_line_length: -1
test_comment_move_mid_query:
fail_str: |
select
my_long_long_line as foo -- with some comment
from foo
fix_str: |
select
-- with some comment
my_long_long_line as foo
from foo
configs:
core:
max_line_length: 40
test_fix_implicit_indent:
# Test for ImplicitIndent.
# The theoretical indent between WHERE and "a" is implicit.
fail_str: |
SELECT
CASE
WHEN longer_and_longer AND much_much_much_longer
THEN longer_and_longer AND much_much_much_longer
ELSE longer_and_longer AND much_much_much_longer
END as foobar,
CASE WHEN a THEN b END as bar
FROM foo
WHERE a_really_long_field AND a_nother_really_long_field
HAVING a_really_long_field AND a_nother_really_long_field
fix_str: |
SELECT
CASE
WHEN longer_and_longer
AND much_much_much_longer
THEN longer_and_longer
AND much_much_much_longer
ELSE longer_and_longer
AND much_much_much_longer
END as foobar,
CASE WHEN a THEN b END as bar
FROM foo
WHERE a_really_long_field
AND a_nother_really_long_field
HAVING a_really_long_field
AND a_nother_really_long_field
configs:
core:
max_line_length: 45
indentation:
allow_implicit_indents: true
test_fix_no_implicit_indent:
# Test explicitly preventing implicit indents.
fail_str: |
SELECT
CASE
WHEN longer_and_longer AND much_much_much_longer
THEN longer_and_longer AND much_much_much_longer
ELSE longer_and_longer AND much_much_much_longer
END as foobar,
CASE WHEN a THEN b END as bar
FROM foo
WHERE a_really_long_field AND a_nother_really_long_field
HAVING a_really_long_field AND a_nother_really_long_field
fix_str: |
SELECT
CASE
WHEN
longer_and_longer
AND much_much_much_longer
THEN
longer_and_longer
AND much_much_much_longer
ELSE
longer_and_longer
AND much_much_much_longer
END as foobar,
CASE WHEN a THEN b END as bar
FROM foo
WHERE
a_really_long_field
AND a_nother_really_long_field
HAVING
a_really_long_field
AND a_nother_really_long_field
configs:
core:
max_line_length: 45
indentation:
allow_implicit_indents: false
test_fix_window_function:
# https://github.com/sqlfluff/sqlfluff/issues/4292
fail_str: |
select *
from t
qualify a = coalesce(
first_value(iff(b = 'none', null, a)) ignore nulls over (partition by c order by d desc),
first_value(a) respect nulls over (partition by c order by d desc)
)
fix_str: |
select *
from t
qualify a = coalesce(
first_value(
iff(b = 'none', null, a)
) ignore nulls
over (partition by c order by d desc),
first_value(a) respect nulls
over (partition by c order by d desc)
)
configs:
core:
max_line_length: 50
dialect: snowflake
test_fail_do_not_fix_noqa:
# https://github.com/sqlfluff/sqlfluff/issues/4248
# NOTE: No fix_str, because this should be unfixable.
fail_str: |
SELECT
col1,
col2,
col3
FROM
really_really_really_really_really_really_long_schema_name.TABLE1 -- noqa: L014
test_operator_precedence:
fail_str: |
SELECT *
FROM foo
left join abcdef_abcd_details
on foo.abcdefgh_id = abcdef_abcd_details.abcdefgh_id and abcdef_abcd_details.abcdef_abcdef_abcdef_abcdef = 1
fix_str: |
SELECT *
FROM foo
left join abcdef_abcd_details
on
foo.abcdefgh_id = abcdef_abcd_details.abcdefgh_id
and abcdef_abcd_details.abcdef_abcdef_abcdef_abcdef = 1
configs:
core:
max_line_length: 100
dialect: snowflake
test_long_functions_and_aliases:
# https://github.com/sqlfluff/sqlfluff/issues/4033
fail_str: |
SELECT
my_function(col1 + col2, arg2, arg3) over (partition by col3, col4 order by col5 rows between unbounded preceding and current row) as my_relatively_long_alias,
my_other_function(col6, col7 + col8, arg4) as my_other_relatively_long_alias,
my_expression_function(col6, col7 + col8, arg4) = col9 + col10 as another_relatively_long_alias
FROM my_table
fix_str: |
SELECT
my_function(col1 + col2, arg2, arg3)
over (
partition by col3, col4
order by col5 rows between unbounded preceding and current row
)
as my_relatively_long_alias,
my_other_function(col6, col7 + col8, arg4)
as my_other_relatively_long_alias,
my_expression_function(col6, col7 + col8, arg4)
= col9 + col10 as another_relatively_long_alias
FROM my_table
test_order_by_rebreak_span:
# This tests that we can correctly rebreak an "order by" expressions.
fail_str: |
select * from
(
select
tbl1.*,
row_number() over (
partition by tbl1.the_name, {{ ['a', 'b', 'c', 'd'] | join(', ') }} order by created_at desc
) rnk
from foo
inner join tbl2
on tbl1.the_name = tbl2.the_name
)
fix_str: |
select * from
(
select
tbl1.*,
row_number() over (
partition by
tbl1.the_name, {{ ['a', 'b', 'c', 'd'] | join(', ') }}
order by created_at desc
) rnk
from foo
inner join tbl2
on tbl1.the_name = tbl2.the_name
)
test_trailing_semicolon_moves:
# The checks that we don't move the semicolon or the comma.
fail_str: |
SELECT my_very_long_field,
FROM foo
ORDER BY my_very_long_field;
fix_str: |
SELECT
my_very_long_field,
FROM foo
ORDER BY
my_very_long_field;
configs:
core:
dialect: bigquery
max_line_length: 20
# After fixing there are still issues, but we're still keeping
# the comma and semicolon where they are.
violations_after_fix:
- description: Line is too long (23 > 20).
line_no: 2
line_pos: 5
name: layout.long_lines
- description: Line is too long (23 > 20).
line_no: 5
line_pos: 5
name: layout.long_lines
test_pass_window_function:
# Test that we don't flag too eagerly on window functions.
pass_str: |
select
col,
rank() over (
partition by a, b, c
order by d desc
) as rnk
from foo