# LogicTest: local

statement ok
CREATE TABLE xyz (
  x INT PRIMARY KEY,
  y INT,
  z INT,
  INDEX foo (z, y),
  FAMILY "primary" (x, y, z)
)

query T
EXPLAIN (OPT, CATALOG) SELECT * from xyz
----
TABLE xyz
 ├── x int not null
 ├── y int
 ├── z int
 ├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
 ├── tableoid oid [hidden] [system]
 ├── crdb_internal_origin_id int4 [hidden] [system]
 ├── crdb_internal_origin_timestamp decimal [hidden] [system]
 ├── PRIMARY INDEX xyz_pkey
 │    └── x int not null
 └── INDEX foo
      ├── z int
      ├── y int
      └── x int not null
scan xyz

# Verify that column qualifications in check constraints and computed columns
# are stripped.
statement ok
CREATE TABLE abcdef (
    a INT NOT NULL,
    b INT,
    c INT DEFAULT (10),
    d INT AS (abcdef.b + c + 1) STORED,
    e INT AS (a) STORED,
    f INT NOT NULL CHECK (test.abcdef.f > 2),
    FAMILY "primary" (a, b, c, d, e, f, rowid)
)

query T
EXPLAIN (OPT, CATALOG) SELECT * from abcdef
----
TABLE abcdef
 ├── a int not null
 ├── b int
 ├── c int default (10:::INT8)
 ├── d int as ((b + c) + 1:::INT8) stored
 ├── e int as (a) stored
 ├── f int not null
 ├── rowid int not null default (unique_rowid()) [hidden]
 ├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
 ├── tableoid oid [hidden] [system]
 ├── crdb_internal_origin_id int4 [hidden] [system]
 ├── crdb_internal_origin_timestamp decimal [hidden] [system]
 ├── CHECK (f > 2:::INT8)
 └── PRIMARY INDEX abcdef_pkey
      └── rowid int not null default (unique_rowid()) [hidden]
scan abcdef
 ├── check constraint expressions
 │    └── f > 2
 └── computed column expressions
      ├── d
      │    └── (b + c) + 1
      └── e
           └── a

statement ok
CREATE TABLE uvwxy (
    u INT,
    v INT,
    w INT,
    x INT,
    y INT,
    PRIMARY KEY (u,v),
    FAMILY (u,v,w),
    FAMILY (x),
    FAMILY (y)
)

query T
EXPLAIN (OPT, CATALOG) SELECT * from uvwxy
----
TABLE uvwxy
 ├── u int not null
 ├── v int not null
 ├── w int
 ├── x int
 ├── y int
 ├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
 ├── tableoid oid [hidden] [system]
 ├── crdb_internal_origin_id int4 [hidden] [system]
 ├── crdb_internal_origin_timestamp decimal [hidden] [system]
 ├── FAMILY fam_0_u_v_w (u, v, w)
 ├── FAMILY fam_1_x (x)
 ├── FAMILY fam_2_y (y)
 └── PRIMARY INDEX uvwxy_pkey
      ├── u int not null
      └── v int not null
scan uvwxy

# Test foreign keys.
statement ok
CREATE TABLE parent (p INT, q INT, r INT, other INT, PRIMARY KEY (p, q, r), FAMILY "primary" (p, q, r, other))

# Simple FK.
statement ok
CREATE TABLE child  (
  c INT PRIMARY KEY,
  p INT, q INT, r INT,
  CONSTRAINT fk FOREIGN KEY (p,q,r) REFERENCES parent(p,q,r),
  FAMILY "primary" (c, p, q, r)
)

query T
EXPLAIN (OPT, CATALOG) SELECT * from child
----
TABLE child
 ├── c int not null
 ├── p int
 ├── q int
 ├── r int
 ├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
 ├── tableoid oid [hidden] [system]
 ├── crdb_internal_origin_id int4 [hidden] [system]
 ├── crdb_internal_origin_timestamp decimal [hidden] [system]
 ├── PRIMARY INDEX child_pkey
 │    └── c int not null
 └── CONSTRAINT fk FOREIGN KEY child (p, q, r) REFERENCES parent (p, q, r)
scan child

query T
EXPLAIN (OPT, CATALOG) SELECT * from parent
----
TABLE parent
 ├── p int not null
 ├── q int not null
 ├── r int not null
 ├── other int
 ├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
 ├── tableoid oid [hidden] [system]
 ├── crdb_internal_origin_id int4 [hidden] [system]
 ├── crdb_internal_origin_timestamp decimal [hidden] [system]
 ├── PRIMARY INDEX parent_pkey
 │    ├── p int not null
 │    ├── q int not null
 │    └── r int not null
 └── REFERENCED BY CONSTRAINT fk FOREIGN KEY child (p, q, r) REFERENCES parent (p, q, r)
scan parent

# FK with match and actions.
statement ok
CREATE TABLE child2  (
  c INT PRIMARY KEY,
  p INT, q INT, r INT,
  CONSTRAINT fk FOREIGN KEY (p,q,r) REFERENCES parent(p,q,r) MATCH FULL ON DELETE SET NULL ON UPDATE SET DEFAULT,
  FAMILY "primary" (c, p, q, r)
)

# TODO(radu, justin): we are missing the ON UPDATE part.
query T
EXPLAIN (OPT, CATALOG) SELECT * from child2
----
TABLE child2
 ├── c int not null
 ├── p int
 ├── q int
 ├── r int
 ├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
 ├── tableoid oid [hidden] [system]
 ├── crdb_internal_origin_id int4 [hidden] [system]
 ├── crdb_internal_origin_timestamp decimal [hidden] [system]
 ├── PRIMARY INDEX child2_pkey
 │    └── c int not null
 └── CONSTRAINT fk FOREIGN KEY child2 (p, q, r) REFERENCES parent (p, q, r) MATCH FULL ON DELETE SET NULL
scan child2

query T
EXPLAIN (OPT, CATALOG) SELECT * from parent
----
TABLE parent
 ├── p int not null
 ├── q int not null
 ├── r int not null
 ├── other int
 ├── crdb_internal_mvcc_timestamp decimal [hidden] [system]
 ├── tableoid oid [hidden] [system]
 ├── crdb_internal_origin_id int4 [hidden] [system]
 ├── crdb_internal_origin_timestamp decimal [hidden] [system]
 ├── PRIMARY INDEX parent_pkey
 │    ├── p int not null
 │    ├── q int not null
 │    └── r int not null
 ├── REFERENCED BY CONSTRAINT fk FOREIGN KEY child (p, q, r) REFERENCES parent (p, q, r)
 └── REFERENCED BY CONSTRAINT fk FOREIGN KEY child2 (p, q, r) REFERENCES parent (p, q, r) MATCH FULL ON DELETE SET NULL
scan parent
