# LogicTest: default

# Verify information_schema database handles mutation statements correctly.

query error user root does not have DROP privilege on database information_schema
ALTER DATABASE information_schema RENAME TO not_information_schema

statement error user root does not have CREATE privilege on database information_schema
CREATE TABLE information_schema.t (x INT)

query error user root does not have DROP privilege on database information_schema
DROP DATABASE information_schema


# Verify other databases cant be called "information_schema".

statement ok
CREATE DATABASE other_db

statement error the new database name "information_schema" already exists
ALTER DATABASE other_db RENAME TO information_schema

statement error database "information_schema" already exists
CREATE DATABASE information_schema

statement ok
DROP DATABASE other_db


# Verify information_schema tables handle mutation statements correctly.

statement error user root does not have DROP privilege on table tables
ALTER TABLE information_schema.tables RENAME TO information_schema.bad

statement error user root does not have CREATE privilege on table tables
ALTER TABLE information_schema.tables RENAME COLUMN x TO y

statement error user root does not have CREATE privilege on table tables
ALTER TABLE information_schema.tables ADD COLUMN x DECIMAL

statement error user root does not have CREATE privilege on table tables
ALTER TABLE information_schema.tables DROP COLUMN x

statement error user root does not have CREATE privilege on table tables
ALTER TABLE information_schema.tables ADD CONSTRAINT foo UNIQUE (b)

statement error user root does not have CREATE privilege on table tables
ALTER TABLE information_schema.tables DROP CONSTRAINT bar

statement error user root does not have CREATE privilege on table tables
ALTER TABLE information_schema.tables ALTER COLUMN x SET DEFAULT 'foo'

statement error user root does not have CREATE privilege on table tables
ALTER TABLE information_schema.tables ALTER x DROP NOT NULL

statement error user root does not have CREATE privilege on table tables
CREATE INDEX i on information_schema.tables (x)

statement error user root does not have DROP privilege on table tables
DROP TABLE information_schema.tables

statement error user root does not have CREATE privilege on table tables
DROP INDEX information_schema.tables@i

statement error user root does not have GRANT privilege on table tables
GRANT CREATE ON information_schema.tables TO root

statement error user root does not have GRANT privilege on table tables
REVOKE CREATE ON information_schema.tables FROM root


# Verify information_schema tables handles read-only property correctly.

query error user root does not have DELETE privilege on table tables
DELETE FROM information_schema.tables

query error user root does not have INSERT privilege on table tables
INSERT INTO information_schema.tables VALUES ('abc')

statement error user root does not have UPDATE privilege on table tables
UPDATE information_schema.tables SET a = 'abc'

statement error user root does not have DROP privilege on table tables
TRUNCATE TABLE information_schema.tables


# Verify information_schema can be used like a normal database.

statement ok
SET DATABASE = information_schema

statement ok
SET DATABASE = test


# Verify information_schema handles reflection correctly.

query T
SHOW DATABASES
----
crdb_internal
information_schema
pg_catalog
system
test

query T
SHOW TABLES FROM information_schema
----
columns
key_column_usage
schema_privileges
schemata
statistics
table_constraints
table_privileges
tables
user_privileges
views

query TT colnames
SHOW CREATE TABLE information_schema.tables
----
Table                      CreateTable
information_schema.tables  CREATE TABLE tables (
                           table_catalog STRING NOT NULL DEFAULT '':::STRING,
                           table_schema STRING NOT NULL DEFAULT '':::STRING,
                           table_name STRING NOT NULL DEFAULT '':::STRING,
                           table_type STRING NOT NULL DEFAULT '':::STRING,
                           version INT NULL
)

query TTBTT colnames
SHOW COLUMNS FROM information_schema.tables
----
Field            Type       Null   Default     Indices
table_catalog    STRING     false  '':::STRING {}
table_schema     STRING     false  '':::STRING {}
table_name       STRING     false  '':::STRING {}
table_type       STRING     false  '':::STRING {}
version          INT        true   NULL        {}

query TTBITTBB colnames
SHOW INDEXES FROM information_schema.tables
----
Table  Name  Unique  Seq  Column  Direction  Storing  Implicit

query TTTTT colnames
SHOW CONSTRAINTS FROM information_schema.tables
----
Table   Name     Type  Column(s)  Details

query TTT colnames
SHOW GRANTS ON information_schema.tables
----
Table  User  Privileges


# Verify selecting from information_schema.

## information_schema.schemata

query TTTT colnames
SELECT * FROM information_schema.schemata
----
catalog_name  schema_name         default_character_set_name  sql_path
def           crdb_internal       NULL                        NULL
def           information_schema  NULL                        NULL
def           pg_catalog          NULL                        NULL
def           system              NULL                        NULL
def           test                NULL                        NULL

query TTTT colnames
SELECT * FROM INFormaTION_SCHEMa.schemata
----
catalog_name  schema_name         default_character_set_name  sql_path
def           crdb_internal       NULL                        NULL
def           information_schema  NULL                        NULL
def           pg_catalog          NULL                        NULL
def           system              NULL                        NULL
def           test                NULL                        NULL

statement ok
CREATE DATABASE other_db

query TTTT colnames
SELECT * FROM information_schema.schemata
----
catalog_name  schema_name         default_character_set_name  sql_path
def           crdb_internal       NULL                        NULL
def           information_schema  NULL                        NULL
def           other_db            NULL                        NULL
def           pg_catalog          NULL                        NULL
def           system              NULL                        NULL
def           test                NULL                        NULL

statement ok
DROP DATABASE other_db

## information_schema.tables

statement ok
CREATE DATABASE other_db

statement ok
CREATE TABLE other_db.xyz (i INT)

statement ok
CREATE VIEW other_db.abc AS SELECT i from other_db.xyz

query T
SELECT table_name FROM information_schema.tables
----
jobs
leases
node_build_info
node_statement_statistics
schema_changes
tables
columns
key_column_usage
schema_privileges
schemata
statistics
table_constraints
table_privileges
tables
user_privileges
views
abc
xyz
pg_am
pg_attrdef
pg_attribute
pg_class
pg_collation
pg_constraint
pg_database
pg_depend
pg_description
pg_enum
pg_extension
pg_foreign_server
pg_foreign_table
pg_index
pg_indexes
pg_inherits
pg_namespace
pg_proc
pg_range
pg_roles
pg_settings
pg_tables
pg_type
pg_views
descriptor
eventlog
jobs
lease
namespace
rangelog
settings
ui
users
zones

query T
SELECT table_name FROM information_schema.tables WHERE table_name > 'n' ORDER BY 1 DESC
----
zones
xyz
views
users
user_privileges
ui
tables
tables
table_privileges
table_constraints
statistics
settings
schemata
schema_privileges
schema_changes
rangelog
pg_views
pg_type
pg_tables
pg_settings
pg_roles
pg_range
pg_proc
pg_namespace
pg_inherits
pg_indexes
pg_index
pg_foreign_table
pg_foreign_server
pg_extension
pg_enum
pg_description
pg_depend
pg_database
pg_constraint
pg_collation
pg_class
pg_attribute
pg_attrdef
pg_am
node_statement_statistics
node_build_info
namespace

query TTTTI colnames
SELECT * FROM information_schema.tables
----
table_catalog  table_schema        table_name                 table_type   version
def            crdb_internal       jobs                       SYSTEM VIEW  1
def            crdb_internal       leases                     SYSTEM VIEW  1
def            crdb_internal       node_build_info            SYSTEM VIEW  1
def            crdb_internal       node_statement_statistics  SYSTEM VIEW  1
def            crdb_internal       schema_changes             SYSTEM VIEW  1
def            crdb_internal       tables                     SYSTEM VIEW  1
def            information_schema  columns                    SYSTEM VIEW  1
def            information_schema  key_column_usage           SYSTEM VIEW  1
def            information_schema  schema_privileges          SYSTEM VIEW  1
def            information_schema  schemata                   SYSTEM VIEW  1
def            information_schema  statistics                 SYSTEM VIEW  1
def            information_schema  table_constraints          SYSTEM VIEW  1
def            information_schema  table_privileges           SYSTEM VIEW  1
def            information_schema  tables                     SYSTEM VIEW  1
def            information_schema  user_privileges            SYSTEM VIEW  1
def            information_schema  views                      SYSTEM VIEW  1
def            other_db            abc                        VIEW         1
def            other_db            xyz                        BASE TABLE   2
def            pg_catalog          pg_am                      SYSTEM VIEW  1
def            pg_catalog          pg_attrdef                 SYSTEM VIEW  1
def            pg_catalog          pg_attribute               SYSTEM VIEW  1
def            pg_catalog          pg_class                   SYSTEM VIEW  1
def            pg_catalog          pg_collation               SYSTEM VIEW  1
def            pg_catalog          pg_constraint              SYSTEM VIEW  1
def            pg_catalog          pg_database                SYSTEM VIEW  1
def            pg_catalog          pg_depend                  SYSTEM VIEW  1
def            pg_catalog          pg_description             SYSTEM VIEW  1
def            pg_catalog          pg_enum                    SYSTEM VIEW  1
def            pg_catalog          pg_extension               SYSTEM VIEW  1
def            pg_catalog          pg_foreign_server          SYSTEM VIEW  1
def            pg_catalog          pg_foreign_table           SYSTEM VIEW  1
def            pg_catalog          pg_index                   SYSTEM VIEW  1
def            pg_catalog          pg_indexes                 SYSTEM VIEW  1
def            pg_catalog          pg_inherits                SYSTEM VIEW  1
def            pg_catalog          pg_namespace               SYSTEM VIEW  1
def            pg_catalog          pg_proc                    SYSTEM VIEW  1
def            pg_catalog          pg_range                   SYSTEM VIEW  1
def            pg_catalog          pg_roles                   SYSTEM VIEW  1
def            pg_catalog          pg_settings                SYSTEM VIEW  1
def            pg_catalog          pg_tables                  SYSTEM VIEW  1
def            pg_catalog          pg_type                    SYSTEM VIEW  1
def            pg_catalog          pg_views                   SYSTEM VIEW  1
def            system              descriptor                 BASE TABLE   1
def            system              eventlog                   BASE TABLE   2
def            system              jobs                       BASE TABLE   1
def            system              lease                      BASE TABLE   1
def            system              namespace                  BASE TABLE   1
def            system              rangelog                   BASE TABLE   1
def            system              settings                   BASE TABLE   1
def            system              ui                         BASE TABLE   1
def            system              users                      BASE TABLE   1
def            system              zones                      BASE TABLE   1

statement ok
ALTER TABLE other_db.xyz ADD COLUMN j INT

query TTI colnames
SELECT TABLE_SCHEMA, TABLE_NAME, VERSION FROM information_schema.tables WHERE version > 1
----
table_schema  table_name  version
other_db      xyz         5
system        eventlog    2

user testuser

query TTTTI colnames
SELECT * FROM information_schema.tables
----
table_catalog  table_schema        table_name         table_type   version
def            information_schema  columns            SYSTEM VIEW  1
def            information_schema  key_column_usage   SYSTEM VIEW  1
def            information_schema  schema_privileges  SYSTEM VIEW  1
def            information_schema  schemata           SYSTEM VIEW  1
def            information_schema  statistics         SYSTEM VIEW  1
def            information_schema  table_constraints  SYSTEM VIEW  1
def            information_schema  table_privileges   SYSTEM VIEW  1
def            information_schema  tables             SYSTEM VIEW  1
def            information_schema  user_privileges    SYSTEM VIEW  1
def            information_schema  views              SYSTEM VIEW  1
def            pg_catalog          pg_am              SYSTEM VIEW  1
def            pg_catalog          pg_attrdef         SYSTEM VIEW  1
def            pg_catalog          pg_attribute       SYSTEM VIEW  1
def            pg_catalog          pg_class           SYSTEM VIEW  1
def            pg_catalog          pg_collation       SYSTEM VIEW  1
def            pg_catalog          pg_constraint      SYSTEM VIEW  1
def            pg_catalog          pg_database        SYSTEM VIEW  1
def            pg_catalog          pg_depend          SYSTEM VIEW  1
def            pg_catalog          pg_description     SYSTEM VIEW  1
def            pg_catalog          pg_enum            SYSTEM VIEW  1
def            pg_catalog          pg_extension       SYSTEM VIEW  1
def            pg_catalog          pg_foreign_server  SYSTEM VIEW  1
def            pg_catalog          pg_foreign_table   SYSTEM VIEW  1
def            pg_catalog          pg_index           SYSTEM VIEW  1
def            pg_catalog          pg_indexes         SYSTEM VIEW  1
def            pg_catalog          pg_inherits        SYSTEM VIEW  1
def            pg_catalog          pg_namespace       SYSTEM VIEW  1
def            pg_catalog          pg_proc            SYSTEM VIEW  1
def            pg_catalog          pg_range           SYSTEM VIEW  1
def            pg_catalog          pg_roles           SYSTEM VIEW  1
def            pg_catalog          pg_settings        SYSTEM VIEW  1
def            pg_catalog          pg_tables          SYSTEM VIEW  1
def            pg_catalog          pg_type            SYSTEM VIEW  1
def            pg_catalog          pg_views           SYSTEM VIEW  1

user root

statement ok
GRANT SELECT ON other_db.xyz TO testuser

user testuser

statement ok
SET DATABASE = other_db

query TTTTI colnames
SELECT * FROM information_schema.tables
----
table_catalog  table_schema        table_name         table_type   version
def            information_schema  columns            SYSTEM VIEW  1
def            information_schema  key_column_usage   SYSTEM VIEW  1
def            information_schema  schema_privileges  SYSTEM VIEW  1
def            information_schema  schemata           SYSTEM VIEW  1
def            information_schema  statistics         SYSTEM VIEW  1
def            information_schema  table_constraints  SYSTEM VIEW  1
def            information_schema  table_privileges   SYSTEM VIEW  1
def            information_schema  tables             SYSTEM VIEW  1
def            information_schema  user_privileges    SYSTEM VIEW  1
def            information_schema  views              SYSTEM VIEW  1
def            other_db            xyz                BASE TABLE   6
def            pg_catalog          pg_am              SYSTEM VIEW  1
def            pg_catalog          pg_attrdef         SYSTEM VIEW  1
def            pg_catalog          pg_attribute       SYSTEM VIEW  1
def            pg_catalog          pg_class           SYSTEM VIEW  1
def            pg_catalog          pg_collation       SYSTEM VIEW  1
def            pg_catalog          pg_constraint      SYSTEM VIEW  1
def            pg_catalog          pg_database        SYSTEM VIEW  1
def            pg_catalog          pg_depend          SYSTEM VIEW  1
def            pg_catalog          pg_description     SYSTEM VIEW  1
def            pg_catalog          pg_enum            SYSTEM VIEW  1
def            pg_catalog          pg_extension       SYSTEM VIEW  1
def            pg_catalog          pg_foreign_server  SYSTEM VIEW  1
def            pg_catalog          pg_foreign_table   SYSTEM VIEW  1
def            pg_catalog          pg_index           SYSTEM VIEW  1
def            pg_catalog          pg_indexes         SYSTEM VIEW  1
def            pg_catalog          pg_inherits        SYSTEM VIEW  1
def            pg_catalog          pg_namespace       SYSTEM VIEW  1
def            pg_catalog          pg_proc            SYSTEM VIEW  1
def            pg_catalog          pg_range           SYSTEM VIEW  1
def            pg_catalog          pg_roles           SYSTEM VIEW  1
def            pg_catalog          pg_settings        SYSTEM VIEW  1
def            pg_catalog          pg_tables          SYSTEM VIEW  1
def            pg_catalog          pg_type            SYSTEM VIEW  1
def            pg_catalog          pg_views           SYSTEM VIEW  1

user root

statement ok
DROP DATABASE other_db

statement ok
SET DATABASE = test

## information_schema.table_constraints

query TTTTTT colnames
SELECT *
FROM information_schema.table_constraints
ORDER BY TABLE_NAME, CONSTRAINT_TYPE, CONSTRAINT_NAME
----
constraint_catalog  constraint_schema  constraint_name  table_schema  table_name  constraint_type
def                 system             primary          system        descriptor  PRIMARY KEY
def                 system             primary          system        eventlog    PRIMARY KEY
def                 system             primary          system        jobs        PRIMARY KEY
def                 system             primary          system        lease       PRIMARY KEY
def                 system             primary          system        namespace   PRIMARY KEY
def                 system             primary          system        rangelog    PRIMARY KEY
def                 system             primary          system        settings    PRIMARY KEY
def                 system             primary          system        ui          PRIMARY KEY
def                 system             primary          system        users       PRIMARY KEY
def                 system             primary          system        zones       PRIMARY KEY

statement ok
CREATE DATABASE constraint_db

statement ok
CREATE TABLE constraint_db.t1 (
  p FLOAT PRIMARY KEY,
  a INT UNIQUE CHECK (a > 4),
  CONSTRAINT c2 CHECK (a < 99)
)

statement ok
CREATE TABLE constraint_db.t2 (
    t1_ID INT,
    CONSTRAINT fk FOREIGN KEY (t1_ID) REFERENCES constraint_db.t1(a),
    INDEX (t1_ID)
)

query TTTTTT colnames
SELECT *
FROM information_schema.table_constraints
WHERE constraint_schema = 'constraint_db'
ORDER BY TABLE_NAME, CONSTRAINT_TYPE, CONSTRAINT_NAME
----
constraint_catalog  constraint_schema  constraint_name  table_schema   table_name  constraint_type
def                 constraint_db      c2               constraint_db  t1          CHECK
def                 constraint_db      check_a          constraint_db  t1          CHECK
def                 constraint_db      primary          constraint_db  t1          PRIMARY KEY
def                 constraint_db      t1_a_key         constraint_db  t1          UNIQUE
def                 constraint_db      fk               constraint_db  t2          FOREIGN KEY

statement ok
DROP DATABASE constraint_db

## information_schema.columns

query TTTTI colnames
SELECT table_catalog, table_schema, table_name, column_name, ordinal_position
FROM information_schema.columns
WHERE table_schema != 'information_schema' AND table_schema != 'pg_catalog' AND table_schema != 'crdb_internal'
----
table_catalog  table_schema  table_name  column_name     ordinal_position
def            system        descriptor  id              1
def            system        descriptor  descriptor      2
def            system        eventlog    timestamp       1
def            system        eventlog    eventType       2
def            system        eventlog    targetID        3
def            system        eventlog    reportingID     4
def            system        eventlog    info            5
def            system        eventlog    uniqueID        6
def            system        jobs        id              1
def            system        jobs        status          2
def            system        jobs        created         3
def            system        jobs        payload         4
def            system        lease       descID          1
def            system        lease       version         2
def            system        lease       nodeID          3
def            system        lease       expiration      4
def            system        namespace   parentID        1
def            system        namespace   name            2
def            system        namespace   id              3
def            system        rangelog    timestamp       1
def            system        rangelog    rangeID         2
def            system        rangelog    storeID         3
def            system        rangelog    eventType       4
def            system        rangelog    otherRangeID    5
def            system        rangelog    info            6
def            system        rangelog    uniqueID        7
def            system        settings    name            1
def            system        settings    value           2
def            system        settings    lastUpdated     3
def            system        settings    valueType       4
def            system        ui          key             1
def            system        ui          value           2
def            system        ui          lastUpdated     3
def            system        users       username        1
def            system        users       hashedPassword  2
def            system        zones       id              1
def            system        zones       config          2

statement ok
CREATE TABLE with_defaults (a INT DEFAULT 9, b STRING DEFAULT 'default', c INT, d STRING)

query TTT colnames
SELECT table_name, column_name, column_default
FROM information_schema.columns
WHERE table_schema = 'test' AND table_name = 'with_defaults'
----
table_name     column_name  column_default
with_defaults  a            9:::INT
with_defaults  b            'default':::STRING
with_defaults  c            NULL
with_defaults  d            NULL

statement ok
DROP TABLE with_defaults

statement ok
CREATE TABLE nullability (a INT NOT NULL, b STRING NOT NULL, c INT, d STRING)

query TTT colnames
SELECT table_name, column_name, is_nullable
FROM information_schema.columns
WHERE table_schema = 'test' AND table_name = 'nullability'
----
table_name   column_name  is_nullable
nullability  a            NO
nullability  b            NO
nullability  c            YES
nullability  d            YES

statement ok
DROP TABLE nullability

statement ok
CREATE TABLE data_types (a INT, b FLOAT, c DECIMAL, d STRING, e BYTES, f TIMESTAMP, g TIMESTAMPTZ)

query TTT colnames
SELECT table_name, column_name, data_type
FROM information_schema.columns
WHERE table_schema = 'test' AND table_name = 'data_types'
----
table_name  column_name  data_type
data_types  a            INT
data_types  b            FLOAT
data_types  c            DECIMAL
data_types  d            STRING
data_types  e            BYTES
data_types  f            TIMESTAMP
data_types  g            TIMESTAMP WITH TIME ZONE

statement ok
DROP TABLE data_types

statement ok
CREATE TABLE char_len (a INT, b BIT, c BIT(12), d STRING, e STRING(12), f FLOAT)

query TTII colnames
SELECT table_name, column_name, character_maximum_length, character_octet_length
FROM information_schema.columns
WHERE table_schema = 'test' AND table_name = 'char_len'
----
table_name  column_name  character_maximum_length  character_octet_length
char_len    a            NULL                      NULL
char_len    b            1                         NULL
char_len    c            12                        NULL
char_len    d            NULL                      NULL
char_len    e            12                        48
char_len    f            NULL                      NULL

statement ok
DROP TABLE char_len

statement ok
CREATE TABLE num_prec (a INT, b FLOAT, c FLOAT(23), d DECIMAL, e DECIMAL(12), f DECIMAL(12, 6), g BOOLEAN)

query TTIII colnames
SELECT table_name, column_name, numeric_precision, numeric_scale, datetime_precision
FROM information_schema.columns
WHERE table_schema = 'test' AND table_name = 'num_prec'
----
table_name  column_name  numeric_precision  numeric_scale  datetime_precision
num_prec    a            64                 0              NULL
num_prec    b            53                 NULL           NULL
num_prec    c            23                 NULL           NULL
num_prec    d            NULL               NULL           NULL
num_prec    e            12                 0              NULL
num_prec    f            12                 6              NULL
num_prec    g            NULL               NULL           NULL

statement ok
DROP TABLE num_prec

## information_schema.key_column_usage

statement ok
CREATE DATABASE constraint_column

statement ok
CREATE TABLE constraint_column.t1 (
  p FLOAT PRIMARY KEY,
  a INT UNIQUE,
  b INT,
  c INT CHECK(c > 0),
  UNIQUE INDEX index_key(b, c)
)

statement ok
CREATE TABLE constraint_column.t2 (
    t1_ID INT PRIMARY KEY,
    CONSTRAINT fk FOREIGN KEY (t1_ID) REFERENCES constraint_column.t1(a)
)

statement ok
CREATE TABLE constraint_column.t3 (
    a INT,
    b INT,
    CONSTRAINT fk FOREIGN KEY (a, b) REFERENCES constraint_column.t1(b, c),
    INDEX (a, b)
)

query TTTTTTTII colnames
SELECT * FROM information_schema.key_column_usage WHERE constraint_schema = 'constraint_column' ORDER BY TABLE_NAME, CONSTRAINT_NAME, ORDINAL_POSITION
----
constraint_catalog  constraint_schema  constraint_name  table_catalog  table_schema       table_name  column_name  ordinal_position  position_in_unique_constraint
def                 constraint_column  index_key        def            constraint_column  t1          b            1                 NULL
def                 constraint_column  index_key        def            constraint_column  t1          c            2                 NULL
def                 constraint_column  primary          def            constraint_column  t1          p            1                 NULL
def                 constraint_column  t1_a_key         def            constraint_column  t1          a            1                 NULL
def                 constraint_column  fk               def            constraint_column  t2          t1_id        1                 1
def                 constraint_column  primary          def            constraint_column  t2          t1_id        1                 NULL
def                 constraint_column  fk               def            constraint_column  t3          a            1                 1
def                 constraint_column  fk               def            constraint_column  t3          b            2                 2

statement ok
DROP DATABASE constraint_column

## information_schema.schema_privileges

statement ok
CREATE DATABASE other_db

query TTTTT colnames
SELECT * FROM information_schema.schema_privileges
----
grantee  table_catalog  table_schema  privilege_type  is_grantable
root     def            other_db      ALL             NULL
root     def            system        GRANT           NULL
root     def            system        SELECT          NULL
root     def            test          ALL             NULL

statement ok
GRANT SELECT ON DATABASE other_db TO testuser

query TTTTT colnames
SELECT * FROM information_schema.schema_privileges
----
grantee   table_catalog  table_schema  privilege_type  is_grantable
root      def            other_db      ALL             NULL
testuser  def            other_db      SELECT          NULL
root      def            system        GRANT           NULL
root      def            system        SELECT          NULL
root      def            test          ALL             NULL

## information_schema.table_privileges

# root can see everything
query TTTTTTTT colnames
SELECT * FROM information_schema.table_privileges
----
grantor  grantee  table_catalog  table_schema  table_name  privilege_type  is_grantable  with_hierarchy
NULL     root     def            system        descriptor  GRANT           NULL          NULL
NULL     root     def            system        descriptor  SELECT          NULL          NULL
NULL     root     def            system        eventlog    DELETE          NULL          NULL
NULL     root     def            system        eventlog    GRANT           NULL          NULL
NULL     root     def            system        eventlog    INSERT          NULL          NULL
NULL     root     def            system        eventlog    SELECT          NULL          NULL
NULL     root     def            system        eventlog    UPDATE          NULL          NULL
NULL     root     def            system        jobs        DELETE          NULL          NULL
NULL     root     def            system        jobs        GRANT           NULL          NULL
NULL     root     def            system        jobs        INSERT          NULL          NULL
NULL     root     def            system        jobs        SELECT          NULL          NULL
NULL     root     def            system        jobs        UPDATE          NULL          NULL
NULL     root     def            system        lease       DELETE          NULL          NULL
NULL     root     def            system        lease       GRANT           NULL          NULL
NULL     root     def            system        lease       INSERT          NULL          NULL
NULL     root     def            system        lease       SELECT          NULL          NULL
NULL     root     def            system        lease       UPDATE          NULL          NULL
NULL     root     def            system        namespace   GRANT           NULL          NULL
NULL     root     def            system        namespace   SELECT          NULL          NULL
NULL     root     def            system        rangelog    DELETE          NULL          NULL
NULL     root     def            system        rangelog    GRANT           NULL          NULL
NULL     root     def            system        rangelog    INSERT          NULL          NULL
NULL     root     def            system        rangelog    SELECT          NULL          NULL
NULL     root     def            system        rangelog    UPDATE          NULL          NULL
NULL     root     def            system        settings    DELETE          NULL          NULL
NULL     root     def            system        settings    GRANT           NULL          NULL
NULL     root     def            system        settings    INSERT          NULL          NULL
NULL     root     def            system        settings    SELECT          NULL          NULL
NULL     root     def            system        settings    UPDATE          NULL          NULL
NULL     root     def            system        ui          DELETE          NULL          NULL
NULL     root     def            system        ui          GRANT           NULL          NULL
NULL     root     def            system        ui          INSERT          NULL          NULL
NULL     root     def            system        ui          SELECT          NULL          NULL
NULL     root     def            system        ui          UPDATE          NULL          NULL
NULL     root     def            system        users       DELETE          NULL          NULL
NULL     root     def            system        users       GRANT           NULL          NULL
NULL     root     def            system        users       INSERT          NULL          NULL
NULL     root     def            system        users       SELECT          NULL          NULL
NULL     root     def            system        users       UPDATE          NULL          NULL
NULL     root     def            system        zones       DELETE          NULL          NULL
NULL     root     def            system        zones       GRANT           NULL          NULL
NULL     root     def            system        zones       INSERT          NULL          NULL
NULL     root     def            system        zones       SELECT          NULL          NULL
NULL     root     def            system        zones       UPDATE          NULL          NULL

statement ok
CREATE TABLE other_db.xyz (i INT)

statement ok
CREATE VIEW other_db.abc AS SELECT i from other_db.xyz

query TTTTTTTT colnames
SELECT * FROM information_schema.table_privileges WHERE TABLE_SCHEMA = 'other_db'
----
grantor  grantee   table_catalog  table_schema  table_name  privilege_type  is_grantable  with_hierarchy
NULL     root      def            other_db      abc         ALL             NULL          NULL
NULL     testuser  def            other_db      abc         SELECT          NULL          NULL
NULL     root      def            other_db      xyz         ALL             NULL          NULL
NULL     testuser  def            other_db      xyz         SELECT          NULL          NULL

statement ok
GRANT UPDATE ON other_db.xyz TO testuser

query TTTTTTTT colnames
SELECT * FROM information_schema.table_privileges WHERE TABLE_SCHEMA = 'other_db'
----
grantor  grantee   table_catalog  table_schema  table_name  privilege_type  is_grantable  with_hierarchy
NULL     root      def            other_db      abc         ALL             NULL          NULL
NULL     testuser  def            other_db      abc         SELECT          NULL          NULL
NULL     root      def            other_db      xyz         ALL             NULL          NULL
NULL     testuser  def            other_db      xyz         SELECT          NULL          NULL
NULL     testuser  def            other_db      xyz         UPDATE          NULL          NULL

# testuser can read permissions as well
user testuser

statement ok
SET DATABASE = other_db

query TTTTTTTT colnames
SELECT * FROM information_schema.table_privileges WHERE TABLE_SCHEMA = 'other_db'
----
grantor  grantee   table_catalog  table_schema  table_name  privilege_type  is_grantable  with_hierarchy
NULL     root      def            other_db      abc         ALL             NULL          NULL
NULL     testuser  def            other_db      abc         SELECT          NULL          NULL
NULL     root      def            other_db      xyz         ALL             NULL          NULL
NULL     testuser  def            other_db      xyz         SELECT          NULL          NULL
NULL     testuser  def            other_db      xyz         UPDATE          NULL          NULL

user root

## information_schema.statistics

statement ok
CREATE TABLE other_db.teststatics(id INT PRIMARY KEY, c INT, d INT, e STRING, INDEX idx_c(c), UNIQUE INDEX idx_cd(c,d))

query TTTBTTITIITBB colnames
SELECT * FROM information_schema.statistics WHERE table_schema='other_db' AND table_name='teststatics' ORDER BY INDEX_SCHEMA,INDEX_NAME,SEQ_IN_INDEX
----
table_catalog  table_schema  table_name   non_unique  index_schema  index_name  seq_in_index  column_name  COLLATION  cardinality  direction  storing  implicit
def            other_db      teststatics  true        other_db      idx_c       1             c            NULL       NULL         ASC        false    false
def            other_db      teststatics  true        other_db      idx_c       2             id           NULL       NULL         ASC        false    true
def            other_db      teststatics  false       other_db      idx_cd      1             c            NULL       NULL         ASC        false    false
def            other_db      teststatics  false       other_db      idx_cd      2             d            NULL       NULL         ASC        false    false
def            other_db      teststatics  false       other_db      idx_cd      3             id           NULL       NULL         ASC        false    true
def            other_db      teststatics  false       other_db      primary     1             id           NULL       NULL         ASC        false    false

# Verify information_schema.views
statement ok
CREATE VIEW other_db.v_xyz AS SELECT i FROM other_db.xyz

query TTTTT colnames
SELECT TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME, VIEW_DEFINITION, CHECK_OPTION
FROM information_schema.views
WHERE TABLE_NAME='v_xyz'
----
table_catalog  table_schema  table_name  view_definition             check_option
def            other_db      v_xyz       SELECT i FROM other_db.xyz  NULL

query BBBBB colnames
SELECT IS_UPDATABLE, IS_INSERTABLE_INTO, IS_TRIGGER_UPDATABLE, IS_TRIGGER_DELETABLE, IS_TRIGGER_INSERTABLE_INTO
FROM information_schema.views
WHERE TABLE_NAME='v_xyz'
----
is_updatable  is_insertable_into  is_trigger_updatable  is_trigger_deletable  is_trigger_insertable_into
NULL          NULL                NULL                  NULL                  NULL

statement ok
DROP DATABASE other_db


#Verify  information_schema.user_privileges

query TTTT colnames
SELECT * FROM information_schema.user_privileges ORDER BY privilege_type
----
grantee  table_catalog  privilege_type  is_grantable
root     def            ALL             NULL
root     def            CREATE          NULL
root     def            DELETE          NULL
root     def            DROP            NULL
root     def            GRANT           NULL
root     def            INSERT          NULL
root     def            SELECT          NULL
root     def            UPDATE          NULL
