github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/exec/execbuilder/testdata/catalog (about)

     1  # LogicTest: local
     2  
     3  statement ok
     4  CREATE TABLE xyz (
     5    x INT PRIMARY KEY,
     6    y INT,
     7    z INT,
     8    INDEX foo (z, y),
     9    FAMILY "primary" (x, y, z)
    10  )
    11  
    12  query T
    13  EXPLAIN (OPT, CATALOG) SELECT * from xyz
    14  ----
    15  TABLE xyz
    16   ├── x int not null
    17   ├── y int
    18   ├── z int
    19   ├── INDEX primary
    20   │    └── x int not null
    21   └── INDEX foo
    22        ├── z int
    23        ├── y int
    24        └── x int not null
    25  scan xyz
    26  
    27  # Verify that column qualifications in check constraints and computed columns
    28  # are stripped.
    29  statement ok
    30  CREATE TABLE abcdef (
    31      a INT NOT NULL,
    32      b INT,
    33      c INT DEFAULT (10),
    34      d INT AS (abcdef.b + c + 1) STORED,
    35      e INT AS (a) STORED,
    36      f INT NOT NULL CHECK (test.abcdef.f > 2),
    37      FAMILY "primary" (a, b, c, d, e, f, rowid)
    38  )
    39  
    40  query T
    41  EXPLAIN (OPT, CATALOG) SELECT * from abcdef
    42  ----
    43  TABLE abcdef
    44   ├── a int not null
    45   ├── b int
    46   ├── c int default (10:::INT8)
    47   ├── d int as ((b + c) + 1:::INT8) stored
    48   ├── e int as (a) stored
    49   ├── f int not null
    50   ├── rowid int not null default (unique_rowid()) [hidden]
    51   ├── CHECK (f > 2:::INT8)
    52   └── INDEX primary
    53        └── rowid int not null default (unique_rowid()) [hidden]
    54  scan abcdef
    55   ├── check constraint expressions
    56   │    └── f > 2
    57   └── computed column expressions
    58        ├── d
    59        │    └── (b + c) + 1
    60        └── e
    61             └── a
    62  
    63  statement ok
    64  CREATE TABLE uvwxy (
    65      u INT,
    66      v INT,
    67      w INT,
    68      x INT,
    69      y INT,
    70      PRIMARY KEY (u,v),
    71      FAMILY (u,v,w),
    72      FAMILY (x),
    73      FAMILY (y)
    74  )
    75  
    76  query T
    77  EXPLAIN (OPT, CATALOG) SELECT * from uvwxy
    78  ----
    79  TABLE uvwxy
    80   ├── u int not null
    81   ├── v int not null
    82   ├── w int
    83   ├── x int
    84   ├── y int
    85   ├── FAMILY fam_0_u_v_w (u, v, w)
    86   ├── FAMILY fam_1_x (x)
    87   ├── FAMILY fam_2_y (y)
    88   └── INDEX primary
    89        ├── u int not null
    90        └── v int not null
    91  scan uvwxy
    92  
    93  # Test foreign keys.
    94  statement ok
    95  CREATE TABLE parent (p INT, q INT, r INT, other INT, PRIMARY KEY (p, q, r), FAMILY "primary" (p, q, r, other))
    96  
    97  # Simple FK.
    98  statement ok
    99  CREATE TABLE child  (
   100    c INT PRIMARY KEY,
   101    p INT, q INT, r INT,
   102    CONSTRAINT fk FOREIGN KEY (p,q,r) REFERENCES parent(p,q,r),
   103    FAMILY "primary" (c, p, q, r)
   104  )
   105  
   106  query T
   107  EXPLAIN (OPT, CATALOG) SELECT * from child
   108  ----
   109  TABLE child
   110   ├── c int not null
   111   ├── p int
   112   ├── q int
   113   ├── r int
   114   ├── INDEX primary
   115   │    └── c int not null
   116   ├── INDEX child_auto_index_fk
   117   │    ├── p int
   118   │    ├── q int
   119   │    ├── r int
   120   │    └── c int not null
   121   └── CONSTRAINT fk FOREIGN KEY child (p, q, r) REFERENCES parent (p, q, r)
   122  scan child
   123  
   124  query T
   125  EXPLAIN (OPT, CATALOG) SELECT * from parent
   126  ----
   127  TABLE parent
   128   ├── p int not null
   129   ├── q int not null
   130   ├── r int not null
   131   ├── other int
   132   ├── INDEX primary
   133   │    ├── p int not null
   134   │    ├── q int not null
   135   │    └── r int not null
   136   └── REFERENCED BY CONSTRAINT fk FOREIGN KEY child (p, q, r) REFERENCES parent (p, q, r)
   137  scan parent
   138  
   139  # FK with match and actions.
   140  statement ok
   141  CREATE TABLE child2  (
   142    c INT PRIMARY KEY,
   143    p INT, q INT, r INT,
   144    CONSTRAINT fk FOREIGN KEY (p,q,r) REFERENCES parent(p,q,r) MATCH FULL ON DELETE SET NULL ON UPDATE SET DEFAULT,
   145    FAMILY "primary" (c, p, q, r)
   146  )
   147  
   148  # TODO(radu, justin): we are missing the ON UPDATE part.
   149  query T
   150  EXPLAIN (OPT, CATALOG) SELECT * from child2
   151  ----
   152  TABLE child2
   153   ├── c int not null
   154   ├── p int
   155   ├── q int
   156   ├── r int
   157   ├── INDEX primary
   158   │    └── c int not null
   159   ├── INDEX child2_auto_index_fk
   160   │    ├── p int
   161   │    ├── q int
   162   │    ├── r int
   163   │    └── c int not null
   164   └── CONSTRAINT fk FOREIGN KEY child2 (p, q, r) REFERENCES parent (p, q, r) MATCH FULL ON DELETE SET NULL
   165  scan child2
   166  
   167  query T
   168  EXPLAIN (OPT, CATALOG) SELECT * from parent
   169  ----
   170  TABLE parent
   171   ├── p int not null
   172   ├── q int not null
   173   ├── r int not null
   174   ├── other int
   175   ├── INDEX primary
   176   │    ├── p int not null
   177   │    ├── q int not null
   178   │    └── r int not null
   179   ├── REFERENCED BY CONSTRAINT fk FOREIGN KEY child (p, q, r) REFERENCES parent (p, q, r)
   180   └── REFERENCED BY CONSTRAINT fk FOREIGN KEY child2 (p, q, r) REFERENCES parent (p, q, r) MATCH FULL ON DELETE SET NULL
   181  scan parent
   182  
   183  # Tests with interleaved tables.
   184  statement ok
   185  CREATE TABLE a (a INT PRIMARY KEY, other INT, FAMILY (a,other))
   186  
   187  statement ok
   188  CREATE TABLE ab (
   189    a INT, b INT, PRIMARY KEY (a,b), FAMILY (a,b)
   190  ) INTERLEAVE IN PARENT a(a)
   191  
   192  statement ok
   193  CREATE TABLE abc (k INT PRIMARY KEY, a INT, b INT, c INT, FAMILY (a,b,c));
   194  CREATE INDEX abc_idx ON abc(a,b,c) INTERLEAVE IN PARENT ab(a,b)
   195  
   196  statement ok
   197  CREATE TABLE abx (
   198    a INT, b INT, x INT,
   199    PRIMARY KEY (a,b,x),
   200    FAMILY (a,b,x)
   201  ) INTERLEAVE IN PARENT ab(a,b)
   202  
   203  query T
   204  EXPLAIN (OPT, CATALOG) SELECT * FROM a
   205  ----
   206  TABLE a
   207   ├── a int not null
   208   ├── other int
   209   ├── FAMILY fam_0_a_other (a, other)
   210   └── INDEX primary
   211        ├── a int not null
   212        └── interleaved by
   213             └── table=60 index=1
   214  scan a
   215  
   216  query T
   217  EXPLAIN (OPT, CATALOG) SELECT * FROM ab
   218  ----
   219  TABLE ab
   220   ├── a int not null
   221   ├── b int not null
   222   ├── FAMILY fam_0_a_b (a, b)
   223   └── INDEX primary
   224        ├── a int not null
   225        ├── b int not null
   226        ├── interleave ancestors
   227        │    └── table=59 index=1 (1 key column)
   228        └── interleaved by
   229             ├── table=61 index=2
   230             └── table=62 index=1
   231  scan ab
   232  
   233  query T
   234  EXPLAIN (OPT, CATALOG) SELECT * FROM abc
   235  ----
   236  TABLE abc
   237   ├── k int not null
   238   ├── a int
   239   ├── b int
   240   ├── c int
   241   ├── FAMILY fam_0_a_b_c_k (a, b, c, k)
   242   ├── INDEX primary
   243   │    └── k int not null
   244   └── INDEX abc_idx
   245        ├── a int
   246        ├── b int
   247        ├── c int
   248        ├── k int not null
   249        └── interleave ancestors
   250             ├── table=59 index=1 (1 key column)
   251             └── table=60 index=1 (1 key column)
   252  scan abc
   253  
   254  query T
   255  EXPLAIN (OPT, CATALOG) SELECT * FROM abx
   256  ----
   257  TABLE abx
   258   ├── a int not null
   259   ├── b int not null
   260   ├── x int not null
   261   ├── FAMILY fam_0_a_b_x (a, b, x)
   262   └── INDEX primary
   263        ├── a int not null
   264        ├── b int not null
   265        ├── x int not null
   266        └── interleave ancestors
   267             ├── table=59 index=1 (1 key column)
   268             └── table=60 index=1 (1 key column)
   269  scan abx