# LogicTest: local

statement ok
CREATE TABLE x(a INT)

statement ok
CREATE TABLE y(a INT)

query T
EXPLAIN (VERBOSE)
  WITH t AS (SELECT a FROM y) SELECT * FROM t JOIN t AS q ON true
----
distribution: local
vectorized: true
·
• root
│ columns: (a, a)
│
├── • cross join (inner)
│   │ columns: (a, a)
│   │ estimated row count: 1,000,000 (missing stats)
│   │
│   ├── • scan buffer
│   │     columns: (a)
│   │     estimated row count: 1,000 (missing stats)
│   │     label: buffer 1 (t)
│   │
│   └── • scan buffer
│         columns: (a)
│         estimated row count: 1,000 (missing stats)
│         label: buffer 1 (t)
│
└── • subquery
    │ id: @S1
    │ original sql: SELECT a FROM y
    │ exec mode: discard all rows
    │
    └── • buffer
        │ columns: (a)
        │ label: buffer 1 (t)
        │
        └── • scan
              columns: (a)
              estimated row count: 1,000 (missing stats)
              table: y@y_pkey
              spans: FULL SCAN

query T
EXPLAIN (VERBOSE)
  WITH t AS (SELECT a FROM y) SELECT * FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (a)
│ render a: a
│
└── • scan
      columns: (a)
      estimated row count: 1,000 (missing stats)
      table: y@y_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE)
  WITH t AS (INSERT INTO x VALUES (1) RETURNING a) SELECT * FROM t
----
distribution: local
vectorized: true
·
• root
│ columns: (a)
│
├── • scan buffer
│     columns: (a)
│     estimated row count: 1
│     label: buffer 1 (t)
│
└── • subquery
    │ id: @S1
    │ original sql: INSERT INTO x VALUES (1) RETURNING a
    │ exec mode: discard all rows
    │
    └── • buffer
        │ columns: (a)
        │ label: buffer 1 (t)
        │
        └── • project
            │ columns: (a)
            │
            └── • insert
                │ columns: (a, rowid)
                │ estimated row count: 1
                │ into: x(a, rowid)
                │
                └── • values
                      columns: (column1, rowid_default)
                      size: 2 columns, 1 row
                      row 0, expr 0: 1
                      row 0, expr 1: unique_rowid()

# Regression test for #39010.

statement ok
CREATE TABLE table39010 (col NAME)

query T
EXPLAIN (VERBOSE)
  WITH
    w AS (SELECT NULL, NULL FROM table39010)
  SELECT
    col
  FROM
    w, table39010
----
distribution: local
vectorized: true
·
• cross join (inner)
│ columns: (col)
│ estimated row count: 1,000,000 (missing stats)
│
├── • scan
│     columns: ()
│     estimated row count: 1,000 (missing stats)
│     table: table39010@table39010_pkey
│     spans: FULL SCAN
│
└── • scan
      columns: (col)
      estimated row count: 1,000 (missing stats)
      table: table39010@table39010_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE)
  WITH RECURSIVE t(n) AS (
      VALUES (1)
    UNION ALL
      SELECT n+1 FROM t WHERE n < 100
  )
  SELECT sum(n) FROM t
----
distribution: local
vectorized: true
·
• group (scalar)
│ columns: (sum)
│ estimated row count: 1 (missing stats)
│ aggregate 0: sum(n)
│
└── • render
    │ columns: (n)
    │ render n: column1
    │
    └── • recursive cte
        │ columns: (column1)
        │ estimated row count: 10 (missing stats)
        │
        └── • values
              columns: (column1)
              size: 1 column, 1 row
              row 0, expr 0: 1

query T
EXPLAIN (VERBOSE)
  WITH RECURSIVE t(n) AS (
      VALUES (1)
    UNION
      SELECT n+1 FROM t WHERE n < 100
  )
  SELECT sum(n) FROM t
----
distribution: local
vectorized: true
·
• group (scalar)
│ columns: (sum)
│ estimated row count: 1 (missing stats)
│ aggregate 0: sum(n)
│
└── • render
    │ columns: (n)
    │ render n: column1
    │
    └── • recursive cte
        │ columns: (column1)
        │ estimated row count: 10 (missing stats)
        │ deduplicate
        │
        └── • values
              columns: (column1)
              size: 1 column, 1 row
              row 0, expr 0: 1

# Tests with correlated CTEs.
query T
EXPLAIN
  SELECT (
    WITH foo AS MATERIALIZED (SELECT x.a FROM x WHERE x.a = y.a)
    SELECT * FROM foo
  ) FROM y
----
distribution: local
vectorized: true
·
• render
│
└── • distinct
    │ distinct on: rowid
    │ error on duplicate
    │
    └── • apply join (left outer)
        │
        └── • scan
              missing stats
              table: y@y_pkey
              spans: FULL SCAN

query T
EXPLAIN
  SELECT * FROM
    (VALUES (1), (2), (10)) AS v(x),
    LATERAL (WITH foo AS MATERIALIZED (SELECT a FROM y WHERE y.a <= x) SELECT * FROM foo)
----
distribution: local
vectorized: true
·
• apply join
│
└── • values
      size: 1 column, 3 rows

# Regression tests for #93370. Do not convert a non-recursive CTE
# that uses UNION ALL and WITH RECURSIVE to UNION.
query T
EXPLAIN WITH RECURSIVE
   x(id) AS
     (SELECT 1 UNION ALL SELECT id+1 FROM x WHERE id < 3 ),
   y(id) AS
     (SELECT * FROM x UNION ALL SELECT * FROM x)
 SELECT * FROM y
----
distribution: local
vectorized: true
·
• root
│
├── • render
│   │
│   └── • union all
│       │
│       ├── • scan buffer
│       │     label: buffer 3 (x)
│       │
│       └── • scan buffer
│             label: buffer 3 (x)
│
└── • subquery
    │ id: @S1
    │ original sql: SELECT 1 UNION ALL SELECT id + 1 FROM x WHERE id < 3
    │ exec mode: discard all rows
    │
    └── • buffer
        │ label: buffer 3 (x)
        │
        └── • recursive cte
            │
            └── • values
                  size: 1 column, 1 row

statement ok
CREATE TABLE t93370 (i INT)

query T
EXPLAIN WITH RECURSIVE
   y(id) AS (SELECT * FROM t93370 UNION ALL SELECT * FROM t93370)
 SELECT * FROM y
----
distribution: local
vectorized: true
·
• render
│
└── • union all
    │
    ├── • scan
    │     missing stats
    │     table: t93370@t93370_pkey
    │     spans: FULL SCAN
    │
    └── • scan
          missing stats
          table: t93370@t93370_pkey
          spans: FULL SCAN

statement ok
CREATE TABLE t97362 (i INT, j INT)

statement error pq: WITH clause "alias_0" does not return any columns
SHOW CHANGEFEED JOBS WITH alias_0 AS MATERIALIZED (CREATE TYPE "int" AS ENUM ()) SELECT * FROM alias_0

statement error pq: WITH clause "alias_0" does not return any columns
WITH alias_0 AS MATERIALIZED (DELETE FROM t97362 WHERE i=1) SELECT * FROM alias_0

query I
WITH alias_0 AS MATERIALIZED (DELETE FROM t97362 WHERE i=1 RETURNING j) SELECT * FROM alias_0
----

statement error pq: WITH clause "alias_0" does not return any columns
WITH alias_0 AS MATERIALIZED (CREATE TABLE t97362b ("i" INT, "j" INT)) SELECT * FROM alias_0

statement error pq:
WITH alias_0 AS MATERIALIZED (CREATE OR REPLACE FUNCTION sq(a INT) RETURNS INT AS 'SELECT a*a' LANGUAGE SQL) SELECT * FROM alias_0;
