# LogicTest: local

# ------------------------------------------------------------------------------
# ALTER TABLE tests.
# ------------------------------------------------------------------------------
statement ok
CREATE TABLE other (b INT PRIMARY KEY)

statement ok
INSERT INTO other VALUES (9)

statement ok
CREATE TABLE t (a INT PRIMARY KEY CHECK(a > 0), f INT REFERENCES other, INDEX (f), FAMILY (a, f))

statement ok
INSERT INTO t VALUES (1, 9)

statement ok
SET tracing = on,kv,results; SELECT * FROM t; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
 ORDER BY message LIKE 'fetched:%' DESC, ordinality ASC
----
fetched: /t/t_pkey/1/f -> /9
output row: [1 9]

statement ok
ALTER TABLE t ADD b INT

statement ok
ALTER TABLE t ADD CONSTRAINT foo UNIQUE (b)

statement ok
SET tracing = on,kv,results; SELECT b FROM t@foo; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
 ORDER BY message LIKE 'fetched:%' DESC, ordinality ASC
----
fetched: /t/foo/NULL -> /?
output row: [NULL]

statement ok
ALTER TABLE t ADD c INT

statement ok
INSERT INTO t VALUES (2, 9, 1, 1), (3, 9, 2, 1)

statement ok
ALTER TABLE t DROP CONSTRAINT check_a

statement ok
DROP INDEX foo CASCADE

statement ok
SET tracing = on,kv,results; SELECT * FROM t; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
 ORDER BY message LIKE 'fetched:%' DESC, ordinality ASC
----
fetched: /t/t_pkey/1/f -> /9
fetched: /t/t_pkey/2/f/b/c -> /9/1/1
fetched: /t/t_pkey/3/f/b/c -> /9/2/1
output row: [1 9 NULL NULL]
output row: [2 9 1 1]
output row: [3 9 2 1]

statement ok
ALTER TABLE t DROP b, DROP c

statement ok
SET tracing = on,kv,results; SELECT * FROM t; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
 ORDER BY message LIKE 'fetched:%' DESC, ordinality ASC
----
fetched: /t/t_pkey/1/f -> /9
fetched: /t/t_pkey/2/f -> /9
fetched: /t/t_pkey/3/f -> /9
output row: [1 9]
output row: [2 9]
output row: [3 9]

# Verify limits and orderings are propagated correctly to the select.
statement ok
CREATE TABLE s (k1 INT, k2 INT, v INT, PRIMARY KEY (k1,k2))

query T
EXPLAIN (VERBOSE) ALTER TABLE s SPLIT AT SELECT k1,k2 FROM s ORDER BY k1 LIMIT 3
----
distribution: local
vectorized: true
·
• split
│ columns: (key, pretty, split_enforced_until)
│ estimated row count: 10 (missing stats)
│ index: s@s_pkey
│ expiry: CAST(NULL AS STRING)
│
└── • scan
      columns: (k1, k2)
      ordering: +k1
      estimated row count: 3 (missing stats)
      table: s@s_pkey
      spans: LIMITED SCAN
      limit: 3

statement ok
DROP TABLE t; DROP TABLE other

# ------------------------------------------------------------------------------
# CREATE INDEX tests.
# ------------------------------------------------------------------------------
statement ok
CREATE TABLE t (
  a INT PRIMARY KEY,
  b INT,
  FAMILY (a),
  FAMILY (b)
)

statement ok
INSERT INTO t VALUES (1,1)

statement ok
SET tracing = on,kv,results; SELECT * FROM t; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
 ORDER BY message LIKE 'fetched:%' DESC, ordinality ASC
----
fetched: /t/t_pkey/1 -> <undecoded>
fetched: /t/t_pkey/1/b -> 1
output row: [1 1]

user root

statement ok
CREATE INDEX foo ON t (b)

statement error index with name \"foo\" already exists
CREATE INDEX foo ON t (a)

statement error column "c" does not exist
CREATE INDEX bar ON t (c)

statement error index \"bar\" contains duplicate column \"b\"
CREATE INDEX bar ON t (b, b);

statement ok
SET tracing = on,kv,results; SELECT * FROM t@foo; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
 ORDER BY message LIKE 'fetched:%' DESC, ordinality ASC
----
fetched: /t/foo/1/1 -> <undecoded>
output row: [1 1]

statement ok
INSERT INTO t VALUES (2,1)

statement ok
SET tracing = on,kv,results; SELECT * FROM t@foo; SET tracing = off

query T
SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
 ORDER BY message LIKE 'fetched:%' DESC, ordinality ASC
----
fetched: /t/foo/1/1 -> <undecoded>
fetched: /t/foo/1/2 -> <undecoded>
output row: [1 1]
output row: [2 1]

# test for DESC index

statement ok
DROP TABLE t

statement ok
CREATE TABLE t (
  a INT PRIMARY KEY,
  b INT,
  c INT,
  FAMILY "primary" (a, b, c)
)

statement ok
INSERT INTO t VALUES (1,1,1), (2,2,2)

statement ok
CREATE INDEX b_desc ON t (b DESC)

statement ok
CREATE INDEX b_asc ON t (b ASC, c DESC)

# index b_desc is well formed and column c is scanned using the primary index.
statement ok
SET tracing = on,kv,results; SELECT * FROM t@b_desc; SET tracing = off

query T rowsort
SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
 WHERE message LIKE 'fetched:%' OR message LIKE 'output row%'
 ORDER BY message LIKE 'fetched:%' DESC, ordinality ASC
----
fetched: /t/b_desc/2/2 -> <undecoded>
fetched: /t/b_desc/1/1 -> <undecoded>
fetched: /t/t_pkey/1/b/c -> /1/1
fetched: /t/t_pkey/2/b/c -> /2/2
output row: [1 1 1]
output row: [2 2 2]

statement ok
DROP table t

# ------------------------------------------------------------------------------
# CREATE VIEW tests.
# ------------------------------------------------------------------------------
statement ok
CREATE TABLE test_kv(k INT PRIMARY KEY, v INT, w DECIMAL);
  CREATE UNIQUE INDEX test_v_idx ON test_kv(v);
  CREATE INDEX test_v_idx2 ON test_kv(v DESC) STORING(w);
  CREATE INDEX test_v_idx3 ON test_kv(w) STORING(v);
  CREATE TABLE test_kvr1(k INT PRIMARY KEY REFERENCES test_kv(k));
  CREATE TABLE test_kvr2(k INT, v INT UNIQUE REFERENCES test_kv(k));
  CREATE TABLE test_kvr3(k INT, v INT UNIQUE REFERENCES test_kv(v));
  CREATE TABLE test_kvi2(k INT PRIMARY KEY, v INT);
  CREATE VIEW test_v1 AS SELECT v FROM test_kv;
  CREATE VIEW test_v2 AS SELECT v FROM test_v1;
