# LogicTest: default parallel-stmts distsql

statement ok
CREATE TABLE with_no_column_refs (
  a INT,
  b INT,
  c INT AS (3) STORED
)

query TT
SHOW CREATE TABLE with_no_column_refs
----
with_no_column_refs CREATE TABLE with_no_column_refs (
                      a INT NULL,
                      b INT NULL,
                      c INT NULL AS (3) STORED,
                      FAMILY "primary" (a, b, c, rowid)
                    )

statement ok
CREATE TABLE extra_parens (
  a INT,
  b INT,
  c INT AS ((3)) STORED
)

query TT
SHOW CREATE TABLE extra_parens
----
extra_parens CREATE TABLE extra_parens (
                a INT NULL,
                b INT NULL,
                c INT NULL AS ((3)) STORED,
                FAMILY "primary" (a, b, c, rowid)
              )


statement error cannot write directly to computed column "c"
INSERT INTO with_no_column_refs VALUES (1, 2, 3)

statement error cannot write directly to computed column "c"
INSERT INTO with_no_column_refs (SELECT 1, 2, 3)

statement error cannot write directly to computed column "c"
INSERT INTO with_no_column_refs (a, c) (SELECT 1, 3)

statement error cannot write directly to computed column "c"
INSERT INTO with_no_column_refs (c) VALUES (1)

statement ok
INSERT INTO with_no_column_refs (a, b) VALUES (1, 2)

statement ok
INSERT INTO with_no_column_refs VALUES (1, 2)

statement error cannot write directly to computed column "c"
UPDATE with_no_column_refs SET c = 1

statement error cannot write directly to computed column "c"
UPDATE with_no_column_refs SET (a, b, c) = (1, 2, 3)

statement error cannot write directly to computed column "c"
UPDATE with_no_column_refs SET (a, b, c) = (SELECT 1, 2, 3)

query I
SELECT c FROM with_no_column_refs
----
3
3

statement ok
CREATE TABLE x (
  a INT DEFAULT 3,
  b INT DEFAULT 7,
  c INT AS (a) STORED,
  d INT AS (a + b) STORED
)

query TT
SHOW CREATE TABLE x
----
x CREATE TABLE x (
    a INT NULL DEFAULT 3:::INT,
    b INT NULL DEFAULT 7:::INT,
    c INT NULL AS (a) STORED,
    d INT NULL AS (a + b) STORED,
    FAMILY "primary" (a, b, c, d, rowid)
  )

statement error cannot write directly to computed column "c"
INSERT INTO x (c) VALUES (1)

statement ok
INSERT INTO x (a, b) VALUES (1, 2)

query II
SELECT c, d FROM x
----
1 3

statement ok
DELETE FROM x

statement ok
DELETE FROM x

statement ok
DROP TABLE x

# Check with upserts
statement ok
CREATE TABLE x (
  a INT PRIMARY KEY,
  b INT,
  c INT AS (b + 1) STORED,
  d INT AS (b - 1) STORED
)

statement ok
INSERT INTO x (a, b) VALUES (1, 1) ON CONFLICT (a) DO UPDATE SET b = excluded.b + 1

query II
SELECT c, d FROM x
----
2 0

statement ok
INSERT INTO x (a, b) VALUES (1, 1) ON CONFLICT (a) DO UPDATE SET b = excluded.b + 1

query IIII
SELECT a, b, c, d FROM x
----
1 2 3 1

statement ok
INSERT INTO x (a, b) VALUES (1, 1) ON CONFLICT (a) DO UPDATE SET b = x.b + 1

query III
SELECT a, b, c FROM x
----
1 3 4

# Update.

statement ok
UPDATE x SET b = 3

query III
SELECT a, b, c FROM x
----
1 3 4

# Update/self-reference.

statement ok
UPDATE x SET b = c

query III
SELECT a, b, c FROM x
----
1 4 5

# Updating with default is not allowed.

statement error cannot write directly to computed column "c"
UPDATE x SET (b, c) = (1, DEFAULT)

# Upsert using the UPSERT shorthand.

statement ok
UPSERT INTO x (a, b) VALUES (1, 2)

query IIII
SELECT a, b, c, d FROM x
----
1 2 3 1

statement ok
TRUNCATE x

# statement ok
# INSERT INTO x VALUES (2, 3) ON CONFLICT (a) DO UPDATE SET a = 2, b = 3

statement ok
UPSERT INTO x VALUES (2, 3)

query IIII
SELECT a, b, c, d FROM x
----
2 3 4 2

statement ok
TRUNCATE x

statement error cannot write directly to computed column "c"
UPSERT INTO x VALUES (2, 3, 12)

statement ok
UPSERT INTO x (a, b) VALUES (2, 3)

query IIII
SELECT a, b, c, d FROM x
----
2 3 4 2

statement ok
DROP TABLE x

# TODO(justin): #22434
# statement ok
# CREATE TABLE x (
#   b INT AS a STORED,
#   a INT
# )
#
# statement ok
# INSERT INTO x VALUES (DEFAULT, 1)
#
# statement ok
# INSERT INTO x VALUES (DEFAULT, '2')
#
# query I
# SELECT b FROM x ORDER BY b
# ----
# 1
# 2
#
# statement ok
# DROP TABLE x

statement error use AS \( <expr> \) STORED
CREATE TABLE y (
  a INT AS 3 STORED
)

statement error use AS \( <expr> \) STORED
CREATE TABLE y (
  a INT AS (3)
)

statement error unimplemented at or near "virtual"
CREATE TABLE y (
  a INT AS (3) VIRTUAL
)

statement error expected computed column expression to have type int, but .* has type string
CREATE TABLE y (
  a INT AS ('not an integer!'::STRING) STORED
)

# We utilize the types from other columns.

statement error expected computed column expression to have type int, but 'a' has type string
CREATE TABLE y (
  a STRING,
  b INT AS (a) STORED
)

statement error computed column a contains impure functions: now\(\)
CREATE TABLE y (
  a TIMESTAMP AS (now()) STORED
)

statement error computed column a contains impure functions: now\(\), uuid_v4\(\)
CREATE TABLE y (
  a STRING AS (CONCAT(now()::STRING, uuid_v4()::STRING)) STORED
)

statement error computed columns cannot reference other computed columns
CREATE TABLE y (
  a INT AS (3) STORED,
  b INT AS (a) STORED
)

statement error column name "a" not found
CREATE TABLE y (
  b INT AS (a) STORED
)

statement error aggregate functions are not allowed in computed column expressions
CREATE TABLE y (
  b INT AS (COUNT(1)) STORED
)

statement error computed columns cannot have default values
CREATE TABLE y (
  a INT AS (3) STORED DEFAULT 4
)

# TODO(justin,bram): this should be allowed.
statement ok
CREATE TABLE x (a INT PRIMARY KEY)

statement error computed columns cannot reference non-restricted FK columns
CREATE TABLE y (
  q INT REFERENCES x (a) ON UPDATE CASCADE,
  r INT AS (q) STORED
)

statement error computed columns cannot reference non-restricted FK columns
CREATE TABLE y (
  q INT REFERENCES x (a) ON DELETE CASCADE,
  r INT AS (q) STORED
)

statement error computed column "r" cannot be a foreign key reference
CREATE TABLE y (
  r INT AS (1) STORED REFERENCES x (a)
)

statement error computed column "r" cannot be a foreign key reference
CREATE TABLE y (
  r INT AS (1) STORED REFERENCES x
)

statement error computed column "r" cannot be a foreign key reference
CREATE TABLE y (
  a INT,
  r INT AS (1) STORED REFERENCES x
)

# Composite FK.

statement ok
CREATE TABLE xx (
  a INT,
  b INT,
  UNIQUE (a, b)
)

statement error computed column "y" cannot be a foreign key reference
CREATE TABLE yy (
  x INT,
  y INT AS (3) STORED,
  FOREIGN KEY (x, y) REFERENCES xx (a, b)
)

statement error computed column "y" cannot be a foreign key reference
CREATE TABLE yy (
  x INT,
  y INT AS (3) STORED,
  FOREIGN KEY (y, x) REFERENCES xx (a, b)
)

statement ok
DROP TABLE xx

statement ok
CREATE TABLE y (
  r INT AS (1) STORED,
  INDEX (r)
)

statement error computed column "r" cannot be a foreign key reference
ALTER TABLE y ADD FOREIGN KEY (r) REFERENCES x (a)

statement ok
DROP TABLE y

statement error computed column expression '\(SELECT 1\)' may not contain variable sub-expressions
CREATE TABLE y (
  r INT AS ((SELECT 1)) STORED
)

statement error no data source matches prefix: x
CREATE TABLE y (
  r INT AS (x.a) STORED
)

statement error no data source matches prefix: x
CREATE TABLE y (
  q INT,
  r INT AS (x.q) STORED
)

statement ok
CREATE TABLE y (
  q INT,
  r INT AS (y.q) STORED
)

statement ok
DROP TABLE y

# It's ok if they exist and we don't reference them.
statement ok
CREATE TABLE y (
  q INT REFERENCES x (a) ON UPDATE CASCADE,
  r INT AS (3) STORED
)

statement ok
DROP TABLE y

statement ok
DROP TABLE x

# Indexes on computed columns
statement ok
CREATE TABLE x (
  k INT PRIMARY KEY,
  a JSON,
  b TEXT AS (a->>'q') STORED,
  INDEX (b) STORING (k)
)

query TTT
EXPLAIN SELECT b FROM x ORDER BY b
----
render     ·      ·
 └── scan  ·      ·
·          table  x@x_b_idx
·          spans  ALL

statement error cannot write directly to computed column
INSERT INTO x (k, a, b) VALUES (1, '{"q":"xyz"}', 'not allowed!'), (2, '{"q":"abc"}', 'also not allowed')

statement error cannot write directly to computed column
UPDATE x SET (k, a, b) = (1, '{"q":"xyz"}', 'not allowed!')

statement ok
INSERT INTO x (k, a) VALUES (1, '{"q":"xyz"}'), (2, '{"q":"abc"}')

query IT
SELECT k, b FROM x ORDER BY b
----
2 abc
1 xyz

statement ok
DROP TABLE x

statement ok
CREATE TABLE x (
  k INT AS ((data->>'id')::INT) STORED PRIMARY KEY,
  data JSON
)

statement ok
INSERT INTO x (data) VALUES
 ('{"id": 1, "name": "lucky"}'),
 ('{"id": 2, "name": "rascal"}'),
 ('{"id": 3, "name": "captain"}'),
 ('{"id": 4, "name": "lola"}')

# ON CONFLICT that modifies a PK.
statement ok
INSERT INTO x (data) VALUES ('{"id": 1, "name": "ernie"}')
ON CONFLICT (k) DO UPDATE SET data = '{"id": 5, "name": "ernie"}'

# ON CONFLICT that modifies a PK which then also conflicts.
statement error duplicate key value
INSERT INTO x (data) VALUES ('{"id": 5, "name": "oliver"}')
ON CONFLICT (k) DO UPDATE SET data = '{"id": 2, "name": "rascal"}'

# Updating a non-PK column.
statement ok
UPDATE x SET data = data || '{"name": "carl"}' WHERE k = 2

query T
SELECT data->>'name' FROM x WHERE k = 2
----
carl

query T
SELECT data->>'name' FROM x WHERE k = 5
----
ernie

# Referencing a computed column.
statement ok
create table y (
  a INT REFERENCES x (k)
)

statement ok
INSERT INTO y VALUES (5)

statement error foreign key violation
INSERT INTO y VALUES (100)

statement ok
DROP TABLE x CASCADE

statement ok
CREATE TABLE x (
  a INT,
  b INT,
  c INT,
  d INT[] AS (ARRAY[a, b, c]) STORED
)

statement ok
INSERT INTO x (a, b, c) VALUES (1, 2, 3)

query T
SELECT d FROM x
----
{1,2,3}

statement ok
TRUNCATE x

# Make sure we get the permutation on the inserts correct.

statement ok
INSERT INTO x (b, a, c) VALUES (1, 2, 3)

query T
SELECT d FROM x
----
{2,1,3}

# Make sure we get the permutation on the updates correct.
statement ok
UPDATE x SET (c, a, b) = (1, 2, 3)

query T
SELECT d FROM x
----
{2,3,1}

statement ok
UPDATE x SET (a, c) = (1, 2)

query T
SELECT d FROM x
----
{1,3,2}

statement ok
UPDATE x SET c = 2, a = 3, b = 1

query T
SELECT d FROM x
----
{3,1,2}

# Make sure we get the permutation on upserts correct.
statement ok
INSERT INTO x (rowid) VALUES ((SELECT rowid FROM x)) ON CONFLICT(rowid) DO UPDATE SET (a, b, c) = (1, 2, 3)

query T
SELECT d FROM x
----
{1,2,3}

statement ok
INSERT INTO x (rowid) VALUES ((SELECT rowid FROM x)) ON CONFLICT(rowid) DO UPDATE SET (c, a, b) = (1, 2, 3)

query T
SELECT d FROM x
----
{2,3,1}

statement ok
INSERT INTO x (rowid) VALUES ((SELECT rowid FROM x)) ON CONFLICT(rowid) DO UPDATE SET (c, a) = (1, 2)

query T
SELECT d FROM x
----
{2,3,1}

statement ok
DROP TABLE x

statement ok
CREATE TABLE x (
  a INT,
  b INT as (x.a) STORED
)

query TT
SHOW CREATE TABLE x
----
x CREATE TABLE x (
    a INT NULL,
    b INT NULL AS (a) STORED,
    FAMILY "primary" (a, b, rowid)
  )

statement ok
DROP TABLE x

# Check that computed columns are resilient to column renames.
statement ok
CREATE TABLE x (
  a INT,
  b INT AS (a) STORED
)

statement ok
ALTER TABLE x RENAME COLUMN a TO c

query TT
SHOW CREATE TABLE x
----
x CREATE TABLE x (
    c INT NULL,
    b INT NULL AS (c) STORED,
    FAMILY "primary" (c, b, rowid)
  )

statement ok
DROP TABLE x

statement ok
CREATE TABLE x (
  a INT,
  b INT AS (a * 2) STORED
)

query T colnames
SELECT generation_expression FROM information_schema.columns
WHERE table_name = 'x' and column_name = 'b'
----
generation_expression
a * 2

query I
SELECT COUNT(*) FROM information_schema.columns
WHERE table_name = 'x' and generation_expression = ''
----
1

# TODO(justin): #22652
statement error adding a computed column via ALTER not supported
ALTER TABLE x ADD COLUMN c INT AS (4) STORED

statement ok
DROP TABLE x

# Regression test for #23109
statement ok
CREATE TABLE x (
  a INT DEFAULT 1,
  b INT AS (2) STORED
)

statement ok
INSERT INTO x (a) SELECT 1

statement ok
DROP TABLE x

statement ok
CREATE TABLE x (
  b INT AS (2) STORED,
  a INT DEFAULT 1
)

statement ok
INSERT INTO x (a) SELECT 1

statement ok
DROP TABLE x
