File: std_ST05_LT09_test.py

package info (click to toggle)
sqlfluff 3.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 34,000 kB
  • sloc: python: 106,131; sql: 34,188; makefile: 52; sh: 8
file content (129 lines) | stat: -rw-r--r-- 1,936 bytes parent folder | download | duplicates (2)
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
"""Tests observed conflict between ST05 & LT09."""

from sqlfluff.core import FluffConfig, Linter


def test__rules__std_ST05_LT09_4137() -> None:
    """Tests observed conflict between ST05 & LT09.

    In this case, the moved `t2` table was created after the first usage.
    https://github.com/sqlfluff/sqlfluff/issues/4137
    """
    sql = """
with

cte1 as (
    select t1.x, t2.y
    from tbl1 t1
    join (select x, y from tbl2) t2
        on t1.x = t2.x
)

, cte2 as (
    select x, y from tbl2 t2
)

select x, y from cte1
union all
select x, y from cte2
;
"""
    fixed_sql = """
with t2 as (select
x,
y
from tbl2),
cte1 as (
    select
t1.x,
t2.y
    from tbl1 t1
    join t2
        on t1.x = t2.x
),
cte2 as (
    select
x,
y
from tbl2 t2
)
select
x,
y
from cte1
union all
select
x,
y
from cte2
;
"""
    cfg = FluffConfig.from_kwargs(
        dialect="ansi",
        rules=["ST05", "LT09"],
    )
    result = Linter(config=cfg).lint_string(sql, fix=True)
    assert result.fix_string()[0] == fixed_sql


def test__rules__std_ST05_LT09_5265() -> None:
    """Tests observed conflict between ST05 & LT09.

    In this case, the moved `t2` table was created after the first usage.
    https://github.com/sqlfluff/sqlfluff/issues/4137
    """
    sql = """
with

cte1 as (
    select t1.x, t2.y
    from tbl1 t1
    join (select x, y from tbl2) t2
        on t1.x = t2.x
)

, cte2 as (
    select x, y from tbl2 t2
)

select x, y from cte1
union all
select x, y from cte2
;
"""
    fixed_sql = """
with t2 as (select
x,
y
from tbl2),
cte1 as (
    select
t1.x,
t2.y
    from tbl1 t1
    join t2
        on t1.x = t2.x
),
cte2 as (
    select
x,
y
from tbl2 t2
)
select
x,
y
from cte1
union all
select
x,
y
from cte2
;
"""
    cfg = FluffConfig.from_kwargs(
        dialect="ansi",
        rules=["ST05", "LT09"],
    )
    result = Linter(config=cfg).lint_string(sql, fix=True)
    assert result.fix_string()[0] == fixed_sql