github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/logictest/testdata/logic_test/orms (about)

     1  # TODO(mjibson): The fakedist-disk config produces an error. When fixed,
     2  # remove this config line. See #38985.
     3  # LogicTest: local fakedist fakedist-metadata
     4  
     5  ## This test file contains various complex queries that ORMs issue during
     6  ## startup or general use.
     7  
     8  ## 12151
     9  statement ok
    10  CREATE TABLE a (id int UNIQUE, name string)
    11  
    12  query TTTBOI
    13  SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
    14      FROM pg_attribute a
    15      LEFT JOIN pg_attrdef d
    16      ON a.attrelid = d.adrelid
    17      AND a.attnum = d.adnum
    18      WHERE a.attrelid = 'a'::regclass
    19      AND a.attnum > 0 AND NOT a.attisdropped
    20      ORDER BY a.attnum
    21  ----
    22  id     bigint  NULL            false  20  -1
    23  name   text    NULL            false  25  -1
    24  rowid  bigint  unique_rowid()  true   20  -1
    25  
    26  
    27  # Ordered aggregations are possible.
    28  # #12115
    29  query TT
    30  SELECT t.typname enum_name, array_agg(e.enumlabel ORDER BY enumsortorder) enum_value
    31      FROM pg_type t
    32      JOIN pg_enum e ON t.oid = e.enumtypid
    33      JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
    34      WHERE n.nspname = 'public'
    35      GROUP BY 1
    36  ----
    37  
    38  
    39  ## 12207
    40  statement ok
    41  CREATE TABLE customers (
    42      name STRING PRIMARY KEY,
    43      id INT,
    44      INDEX (id)
    45  )
    46  
    47  statement ok
    48  INSERT INTO customers VALUES ('jordan', 12), ('cuong', 13)
    49  
    50  query TBBTTTT colnames
    51  SELECT i.relname AS name,
    52         ix.indisprimary AS PRIMARY,
    53         ix.indisunique AS UNIQUE,
    54         ix.indkey AS indkey,
    55         array_agg(a.attnum) AS column_indexes,
    56         array_agg(a.attname) AS column_names,
    57         pg_get_indexdef(ix.indexrelid) AS definition
    58  FROM pg_class t,
    59       pg_class i,
    60       pg_index ix,
    61       pg_attribute a
    62  WHERE t.oid = ix.indrelid
    63  AND   i.oid = ix.indexrelid
    64  AND   a.attrelid = t.oid
    65  AND   t.relkind = 'r'
    66  AND   t.relname = 'customers' -- this query is run once for each table
    67  GROUP BY i.relname,
    68           ix.indexrelid,
    69           ix.indisprimary,
    70           ix.indisunique,
    71           ix.indkey
    72  ORDER BY i.relname
    73  ----
    74  name              primary  unique  indkey  column_indexes  column_names  definition
    75  customers_id_idx  false    false   2       {1,2}           {name,id}     CREATE INDEX customers_id_idx ON test.public.customers USING btree (id ASC)
    76  primary           true     true    1       {1,2}           {name,id}     CREATE UNIQUE INDEX "primary" ON test.public.customers USING btree (name ASC)
    77  
    78  
    79  query TT colnames
    80  SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type
    81  FROM   pg_index i
    82  JOIN   pg_attribute a ON a.attrelid = i.indrelid
    83                       AND a.attnum = ANY(i.indkey)
    84                       WHERE  i.indrelid = '"a"'::regclass
    85                       AND    i.indisprimary
    86  ----
    87  attname  data_type
    88  rowid    bigint
    89  
    90  statement ok
    91  CREATE TABLE b (id INT, a_id INT, FOREIGN KEY (a_id) REFERENCES a (id))
    92  
    93  # ActiveRecord query for foreign keys
    94  # https://github.com/rails/rails/blob/355a2fcf/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L583
    95  query TTTTTT
    96  SELECT t2.oid::regclass::text AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confupdtype AS on_update, c.confdeltype AS on_delete
    97  FROM pg_constraint c
    98  JOIN pg_class t1 ON c.conrelid = t1.oid
    99  JOIN pg_class t2 ON c.confrelid = t2.oid
   100  JOIN pg_attribute a1 ON a1.attnum = c.conkey[1] AND a1.attrelid = t1.oid
   101  JOIN pg_attribute a2 ON a2.attnum = c.confkey[1] AND a2.attrelid = t2.oid
   102  JOIN pg_namespace t3 ON c.connamespace = t3.oid
   103  WHERE c.contype = 'f'
   104  AND t1.relname ='b'
   105  AND t3.nspname = ANY (current_schemas(false))
   106  ORDER BY c.conname
   107  ----
   108  a  a_id  id  fk_a_id_ref_a  a  a
   109  
   110  # Default value columns in Rails produce these kinds of queries:
   111  query O
   112  SELECT 'decimal(18,2)'::regtype::oid
   113  ----
   114  1700
   115  
   116  # NOTE: Before 19.2, this returned 25 (oid.T_text), but due to updates to the
   117  # type system to more correctly handle OIDs, this now returns 1043
   118  # (oid.T_varchar), which is what PG returns.
   119  query O
   120  SELECT 'character varying'::regtype::oid
   121  ----
   122  1043
   123  
   124  statement ok
   125  CREATE INDEX b_idx ON b(a_id);
   126  
   127  # ActiveRecord 4.2.x query for checking if an index exists
   128  # Relies on OID IN tuple support
   129  query I
   130  SELECT count(*)
   131  FROM pg_class t
   132  INNER JOIN pg_index d ON t.oid = d.indrelid
   133  INNER JOIN pg_class i ON d.indexrelid = i.oid
   134  WHERE i.relkind = 'i'
   135  AND i.relname = 'b_idx'
   136  AND t.relname = 'b'
   137  AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)))
   138  ----
   139  1
   140  
   141  statement ok
   142  CREATE TABLE c (a INT, b INT, PRIMARY KEY (a, b))
   143  
   144  # ActiveRecord query for determining primary key cols.
   145  query T
   146  SELECT
   147      a.attname
   148  FROM
   149      (
   150          SELECT
   151              indrelid, indkey, generate_subscripts(indkey, 1) AS idx
   152          FROM
   153              pg_index
   154          WHERE
   155              indrelid = '"c"'::REGCLASS AND indisprimary
   156      )
   157          AS i
   158      JOIN pg_attribute AS a ON
   159              a.attrelid = i.indrelid AND a.attnum = i.indkey[i.idx]
   160  ORDER BY
   161      i.idx
   162  ----
   163  a
   164  b
   165  
   166  statement ok
   167  CREATE TABLE metatest (a INT PRIMARY KEY)
   168  
   169  # ActiveRecord query that needs apply join.
   170  query TTTBTITT
   171  SELECT a.attname,
   172    format_type(a.atttypid, a.atttypmod),
   173    pg_get_expr(d.adbin, d.adrelid),
   174    a.attnotnull,
   175    a.atttypid,
   176    a.atttypmod,
   177    (SELECT c.collname
   178     FROM pg_collation c, pg_type t
   179     WHERE c.oid = a.attcollation
   180     AND t.oid = a.atttypid
   181     AND a.attcollation <> t.typcollation),
   182     col_description(a.attrelid, a.attnum) AS comment
   183  FROM pg_attribute a LEFT JOIN pg_attrdef d
   184  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
   185  WHERE a.attrelid = '"metatest"'::regclass
   186  AND a.attnum > 0 AND NOT a.attisdropped
   187  ORDER BY a.attnum
   188  ----
   189  a  bigint  NULL  true  20  -1  NULL  NULL
   190  
   191  # Navicat metadata query.
   192  
   193  query TTBBB
   194  SELECT
   195      attname AS name,
   196      attrelid AS tid,
   197      COALESCE(
   198          (
   199              SELECT
   200                  attnum = ANY conkey
   201              FROM
   202                  pg_constraint
   203              WHERE
   204                  contype = 'p' AND conrelid = attrelid
   205          ),
   206          false
   207      )
   208          AS primarykey,
   209      NOT (attnotnull) AS allownull,
   210      (
   211          SELECT
   212              seq.oid
   213          FROM
   214              pg_class AS seq
   215              LEFT JOIN pg_depend AS dep
   216              ON seq.oid = dep.objid
   217          WHERE
   218              (
   219                  seq.relkind = 'S'::CHAR
   220                  AND dep.refobjsubid = attnum
   221              )
   222              AND dep.refobjid = attrelid
   223      )
   224      IS NOT NULL
   225          AS autoincrement
   226  FROM
   227      pg_attribute
   228  WHERE
   229      (
   230          attisdropped = false
   231          AND attrelid
   232              = (
   233                      SELECT
   234                          tbl.oid
   235                      FROM
   236                          pg_class AS tbl
   237                          LEFT JOIN pg_namespace AS sch
   238                          ON tbl.relnamespace = sch.oid
   239                      WHERE
   240                          (
   241                              tbl.relkind = 'r'::"char"
   242                              AND tbl.relname = 'metatest'
   243                          )
   244                          AND sch.nspname = 'public'
   245                  )
   246      )
   247      AND attname = 'a';
   248  ----
   249  a  57  true  false  false
   250  
   251  # Hibernate query.
   252  
   253  query TTTOBIIITTOT
   254  SELECT * FROM (SELECT n.nspname, c.relname, a.attname, a.atttypid, a.attnotnull OR ((t.typtype = 'd') AND t.typnotnull) AS attnotnull, a.atttypmod, a.attlen, row_number() OVER (PARTITION BY a.attrelid ORDER BY a.attnum) AS attnum, pg_get_expr(def.adbin, def.adrelid) AS adsrc, dsc.description, t.typbasetype, t.typtype FROM pg_catalog.pg_namespace AS n JOIN pg_catalog.pg_class AS c ON (c.relnamespace = n.oid) JOIN pg_catalog.pg_attribute AS a ON (a.attrelid = c.oid) JOIN pg_catalog.pg_type AS t ON (a.atttypid = t.oid) LEFT JOIN pg_catalog.pg_attrdef AS def ON ((a.attrelid = def.adrelid) AND (a.attnum = def.adnum)) LEFT JOIN pg_catalog.pg_description AS dsc ON ((c.oid = dsc.objoid) AND (a.attnum = dsc.objsubid)) LEFT JOIN pg_catalog.pg_class AS dc ON ((dc.oid = dsc.classoid) AND (dc.relname = 'pg_class')) LEFT JOIN pg_catalog.pg_namespace AS dn ON ((dc.relnamespace = dn.oid) AND (dn.nspname = 'pg_catalog')) WHERE (((c.relkind IN ('r', 'v', 'f', 'm')) AND (a.attnum > 0)) AND (NOT a.attisdropped)) AND (n.nspname LIKE 'public')) AS c;
   255  ----
   256  public  a          id     20  false  -1  8   1  NULL            NULL  0  b
   257  public  a          name   25  false  -1  -1  2  NULL            NULL  0  b
   258  public  a          rowid  20  true   -1  8   3  unique_rowid()  NULL  0  b
   259  public  customers  name   25  true   -1  -1  1  NULL            NULL  0  b
   260  public  customers  id     20  false  -1  8   2  NULL            NULL  0  b
   261  public  b          id     20  false  -1  8   1  NULL            NULL  0  b
   262  public  b          a_id   20  false  -1  8   2  NULL            NULL  0  b
   263  public  b          rowid  20  true   -1  8   3  unique_rowid()  NULL  0  b
   264  public  c          a      20  true   -1  8   1  NULL            NULL  0  b
   265  public  c          b      20  true   -1  8   2  NULL            NULL  0  b
   266  public  metatest   a      20  true   -1  8   1  NULL            NULL  0  b
   267  
   268  
   269  # Regression test for windower not using EncDatum.Fingerprint.
   270  statement ok
   271  SELECT
   272  	array_agg(t_pk.table_name ORDER BY t_pk.table_name)
   273  FROM
   274  	information_schema.statistics AS i
   275  	LEFT JOIN (
   276  			SELECT
   277  				array_agg(c.column_name) AS table_primary_key_columns,
   278  				c.table_name
   279  			FROM
   280  				information_schema.columns AS c
   281  			GROUP BY
   282  				c.table_name
   283  		)
   284  			AS t_pk ON i.table_name = t_pk.table_name
   285  GROUP BY
   286  	t_pk.table_primary_key_columns