File: std_ST05_LT08_test.py

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

from sqlfluff.core import FluffConfig, Linter


def test__rules__std_ST05_LT08_5265() -> None:
    """Tests observed conflict between ST05 & LT08.

    In this case, the moved `oops` and `another` table was created after
    the first usage. The `oops` from the `cte2` is no longer deleted.
    https://github.com/sqlfluff/sqlfluff/issues/5265
    """
    sql = """
WITH
cte1 AS (
    SELECT COUNT(*) AS qty
    FROM some_table AS st
    LEFT JOIN (
        SELECT 'first' AS id
    ) AS oops
    ON st.id = oops.id
),
cte2 AS (
    SELECT COUNT(*) AS other_qty
    FROM other_table AS sot
    LEFT JOIN (
        SELECT 'middle' AS id
    ) AS another
    ON sot.id = another.id
    LEFT JOIN (
        SELECT 'last' AS id
    ) AS oops
    ON sot.id = oops.id
)
SELECT CURRENT_DATE();
"""
    fixed_sql = """
WITH oops AS (
        SELECT 'first' AS id
    ),

cte1 AS (
    SELECT COUNT(*) AS qty
    FROM some_table AS st
    LEFT JOIN oops
    ON st.id = oops.id
),

another AS (
        SELECT 'middle' AS id
    ),

cte2 AS (
    SELECT COUNT(*) AS other_qty
    FROM other_table AS sot
    LEFT JOIN another
    ON sot.id = another.id
    LEFT JOIN (
        SELECT 'last' AS id
    ) AS oops
    ON sot.id = oops.id
)

SELECT CURRENT_DATE();
"""
    cfg = FluffConfig.from_kwargs(
        dialect="ansi",
        rules=["ST05", "LT08"],
    )
    result = Linter(config=cfg).lint_string(sql, fix=True)
    assert result.fix_string()[0] == fixed_sql