-- should not able to create pg_catalog
create database pg_catalog;

Error: 1004(InvalidArguments), Schema pg_catalog already exists

-- session_user because session_user is based on the current user so is not null is for test
-- SQLNESS PROTOCOL POSTGRES
SELECT session_user is not null;

+----------------------------+
| session_user() IS NOT NULL |
+----------------------------+
| t                          |
+----------------------------+

-- session_user and current_schema
-- SQLNESS PROTOCOL POSTGRES
select current_schema();

+------------------+
| current_schema() |
+------------------+
| public           |
+------------------+

-- search_path for pg using schema for now FIXME when support real search_path
-- SQLNESS PROTOCOL POSTGRES
show search_path;

+-------------+
| search_path |
+-------------+
| public      |
+-------------+

-- set search_path for pg using schema for now FIXME when support real search_path
create database test;

Affected Rows: 1

-- SQLNESS PROTOCOL POSTGRES
set search_path to 'test';

Affected Rows: 0

drop database test;

Affected Rows: 0

-- SQLNESS PROTOCOL POSTGRES
set search_path to 'public';

Affected Rows: 0

-- SQLNESS PROTOCOL POSTGRES
set search_path = public;

Affected Rows: 0

-- SQLNESS PROTOCOL POSTGRES
select current_schema();

+------------------+
| current_schema() |
+------------------+
| public           |
+------------------+

-- make sure all the pg_catalog tables are only visible to postgres
select * from pg_catalog.pg_class;

Error: 4001(TableNotFound), Failed to plan SQL: Table not found: greptime.pg_catalog.pg_class

select * from pg_catalog.pg_namespace;

Error: 4001(TableNotFound), Failed to plan SQL: Table not found: greptime.pg_catalog.pg_namespace

select * from pg_catalog.pg_type;

Error: 4001(TableNotFound), Failed to plan SQL: Table not found: greptime.pg_catalog.pg_type

select * from pg_catalog.pg_database;

Error: 4001(TableNotFound), Failed to plan SQL: Table not found: greptime.pg_catalog.pg_database

-- SQLNESS PROTOCOL POSTGRES
select * from pg_catalog.pg_type order by oid;

+-----+-----------+--------+
| oid | typname   | typlen |
+-----+-----------+--------+
| 1   | String    | -1     |
| 2   | Binary    | -1     |
| 3   | Int8      | 1      |
| 4   | Int16     | 2      |
| 5   | Int32     | 4      |
| 6   | Int64     | 8      |
| 7   | UInt8     | 1      |
| 8   | UInt16    | 2      |
| 9   | UInt32    | 4      |
| 10  | UInt64    | 8      |
| 11  | Float32   | 4      |
| 12  | Float64   | 8      |
| 13  | Decimal   | 16     |
| 14  | Date      | 4      |
| 15  | DateTime  | 8      |
| 16  | Timestamp | 8      |
| 17  | Time      | 8      |
| 18  | Duration  | 8      |
| 19  | Interval  | 16     |
| 20  | List      | -1     |
+-----+-----------+--------+

-- SQLNESS PROTOCOL POSTGRES
-- SQLNESS REPLACE (\d+\s*) OID
select * from pg_catalog.pg_database where datname = 'public';

+------------+---------+
| oid        | datname |
+------------+---------+
| OID| public  |
+------------+---------+

-- \d
-- SQLNESS PROTOCOL POSTGRES
SELECT n.nspname as "Schema",
  c.relname as "Name",
  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 't' THEN 'TOAST table' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' END as "Type",
  pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','p','v','m','S','f','')
      AND n.nspname <> 'pg_catalog'
      AND n.nspname !~ '^pg_toast'
      AND n.nspname <> 'information_schema'
  AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;

+--------+---------+-------+-------+
| Schema | Name    | Type  | Owner |
+--------+---------+-------+-------+
| public | numbers | table |       |
+--------+---------+-------+-------+

-- \dt
-- SQLNESS PROTOCOL POSTGRES
SELECT n.nspname as "Schema",
  c.relname as "Name",
  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 't' THEN 'TOAST table' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' END as "Type",
  pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','p','')
      AND n.nspname <> 'pg_catalog'
      AND n.nspname !~ '^pg_toast'
      AND n.nspname <> 'information_schema'
  AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;

+--------+---------+-------+-------+
| Schema | Name    | Type  | Owner |
+--------+---------+-------+-------+
| public | numbers | table |       |
+--------+---------+-------+-------+

-- make sure oid of namespace keep stable
-- SQLNESS PROTOCOL POSTGRES
-- SQLNESS REPLACE (\d+\s*) OID
SELECT * FROM pg_namespace ORDER BY nspname;

+------------+--------------------+
| oid        | nspname            |
+------------+--------------------+
| OID| greptime_private   |
| OID| information_schema |
| OID| pg_catalog         |
| OID| public             |
+------------+--------------------+

-- SQLNESS PROTOCOL POSTGRES
create database my_db;

Affected Rows: 1

-- SQLNESS PROTOCOL POSTGRES
use my_db;



-- SQLNESS PROTOCOL POSTGRES
create table foo
(
    ts TIMESTAMP TIME INDEX
);

Affected Rows: 0

-- show tables in `my_db`
-- SQLNESS PROTOCOL POSTGRES
select relname
from pg_catalog.pg_class
where relnamespace = (
    select oid
    from pg_catalog.pg_namespace
    where nspname = 'my_db'
);

+---------+
| relname |
+---------+
| foo     |
+---------+

-- \dt
-- SQLNESS PROTOCOL POSTGRES
SELECT n.nspname as "Schema",
  c.relname as "Name",
  CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 't' THEN 'TOAST table' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' END as "Type",
  pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
FROM pg_catalog.pg_class c
     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind IN ('r','p','')
      AND n.nspname <> 'pg_catalog'
      AND n.nspname !~ '^pg_toast'
      AND n.nspname <> 'information_schema'
  AND pg_catalog.pg_table_is_visible(c.oid)
ORDER BY 1,2;

+--------+---------+-------+-------+
| Schema | Name    | Type  | Owner |
+--------+---------+-------+-------+
| my_db  | foo     | table |       |
| public | numbers | table |       |
+--------+---------+-------+-------+

-- show tables in `my_db`, `public`
-- SQLNESS PROTOCOL POSTGRES
select relname
from pg_catalog.pg_class
where relnamespace in (
    select oid
    from pg_catalog.pg_namespace
    where nspname = 'my_db' or nspname = 'public'
)
order by relname;

+---------+
| relname |
+---------+
| foo     |
| numbers |
+---------+

-- SQLNESS PROTOCOL POSTGRES
select relname
from pg_catalog.pg_class
where relnamespace in (
    select oid
    from pg_catalog.pg_namespace
    where nspname like 'my%'
);

+---------+
| relname |
+---------+
| foo     |
+---------+

-- SQLNESS PROTOCOL POSTGRES
-- SQLNESS REPLACE (\d+\s*) OID
select relnamespace, relname, relkind
from pg_catalog.pg_class
where relnamespace in (
    select oid
    from pg_catalog.pg_namespace
    where nspname <> 'public'
      and nspname <> 'information_schema'
      and nspname <> 'pg_catalog'
)
order by relnamespace, relname;

+--------------+---------+---------+
| relnamespace | relname | relkind |
+--------------+---------+---------+
| OID| foo     | r       |
+--------------+---------+---------+

-- SQLNESS PROTOCOL POSTGRES
use public;



-- SQLNESS PROTOCOL POSTGRES
drop schema my_db;

Affected Rows: 0

-- SQLNESS PROTOCOL POSTGRES
use pg_catalog;



-- pg_class
-- SQLNESS PROTOCOL POSTGRES
desc table pg_class;

+--------------+--------+-----+------+---------+---------------+
| Column       | Type   | Key | Null | Default | Semantic Type |
+--------------+--------+-----+------+---------+---------------+
| oid          | UInt32 |     | NO   |         | FIELD         |
| relname      | String |     | NO   |         | FIELD         |
| relnamespace | UInt32 |     | NO   |         | FIELD         |
| relkind      | String |     | NO   |         | FIELD         |
| relowner     | UInt32 |     | NO   |         | FIELD         |
+--------------+--------+-----+------+---------+---------------+

-- SQLNESS PROTOCOL POSTGRES
desc table pg_namespace;

+---------+--------+-----+------+---------+---------------+
| Column  | Type   | Key | Null | Default | Semantic Type |
+---------+--------+-----+------+---------+---------------+
| oid     | UInt32 |     | NO   |         | FIELD         |
| nspname | String |     | NO   |         | FIELD         |
+---------+--------+-----+------+---------+---------------+

-- SQLNESS PROTOCOL POSTGRES
drop table my_db.foo;

Failed to execute query, encountered: Error { kind: Db, cause: Some(DbError { severity: "ERROR", parsed_severity: None, code: SqlState(E42P01), message: "Table not found: greptime.my_db.foo", detail: None, hint: None, position: None, where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: None, line: None, routine: None }) }

-- SQLNESS PROTOCOL POSTGRES
use public;



