summaryrefslogtreecommitdiffstats
path: root/tests/fixtures/optimizer/eliminate_ctes.sql
blob: f1f78f47c92cf9cb14383402c3cb909b67544703 (plain)
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
# title: CTE
WITH q AS (
  SELECT
    a
  FROM x
)
SELECT
  a
FROM x;
SELECT
  a
FROM x;

# title: Nested CTE
SELECT
  a
FROM (
  WITH q AS (
    SELECT
      a
    FROM x
  )
  SELECT a FROM x
);
SELECT
  a
FROM (
  SELECT
    a
  FROM x
);

# title: Chained CTE
WITH q AS (
  SELECT
    a
  FROM x
), r AS (
  SELECT
    a
  FROM q
)
SELECT
  a
FROM x;
SELECT
  a
FROM x;

# title: CTE reference in subquery where alias matches outer table name
WITH q AS (
  SELECT
    a
  FROM y
)
SELECT
  a
FROM x AS q
WHERE
  a IN (
    SELECT
      a
    FROM q
  );
WITH q AS (
  SELECT
    a
  FROM y
)
SELECT
  a
FROM x AS q
WHERE
  a IN (
    SELECT
      a
    FROM q
  );

# title: CTE reference in subquery where alias matches outer table name and outer alias is also CTE
WITH q AS (
  SELECT
    a
  FROM y
), q2 AS (
  SELECT
    a
  FROM y
)
SELECT
  a
FROM q2 AS q
WHERE
  a IN (
    SELECT
      a
    FROM q
  );
WITH q AS (
  SELECT
    a
  FROM y
), q2 AS (
  SELECT
    a
  FROM y
)
SELECT
  a
FROM q2 AS q
WHERE
  a IN (
    SELECT
      a
    FROM q
  );