-- This file and its contents are licensed under the Apache License 2.0.
-- Please see the included NOTICE for copyright information and
-- LICENSE-APACHE for a copy of the license.
-- our user needs permission to create schema for the schema tests
\c :TEST_DBNAME :ROLE_SUPERUSER
GRANT CREATE ON DATABASE :TEST_DBNAME TO :ROLE_DEFAULT_PERM_USER;
\c :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
-- create table with non-tsdb option should not be affected
CREATE TABLE t1(time timestamptz, device text, value float) WITH (autovacuum_enabled);
DROP TABLE t1;
-- test error cases
\set ON_ERROR_STOP 0
\set VERBOSITY default
CREATE TABLE t2(time float, device text, value float) WITH (tsdb.hypertable);
ERROR:  partition column could not be determined
HINT:  Use "timescaledb.partition_column" to specify the column to use as partitioning column.
CREATE TABLE t2(time float, device text, value float) WITH (timescaledb.hypertable);
ERROR:  partition column could not be determined
HINT:  Use "timescaledb.partition_column" to specify the column to use as partitioning column.
CREATE TABLE t2(time timestamptz, device text, value float) WITH (tsdb.hypertable,tsdb.partition_column=NULL);
ERROR:  column "null" does not exist
CREATE TABLE t2(time timestamptz, device text, value float) WITH (tsdb.hypertable,tsdb.partition_column='');
ERROR:  column "" does not exist
CREATE TABLE t2(time timestamptz, device text, value float) WITH (tsdb.hypertable,tsdb.partition_column='foo');
ERROR:  column "foo" does not exist
CREATE TABLE t2(time timestamptz, device text, value float) WITH (tsdb.partition_column='time');
ERROR:  timescaledb options requires hypertable option
HINT:  Use "timescaledb.hypertable" to enable creating a hypertable.
CREATE TABLE t2(time timestamptz, device text, value float) WITH (timescaledb.partition_column='time');
ERROR:  timescaledb options requires hypertable option
HINT:  Use "timescaledb.hypertable" to enable creating a hypertable.
CREATE TABLE t2(time timestamptz , device text, value float) WITH (tsdb.hypertable,tsdb.partition_column='time',tsdb.chunk_interval='foo');
ERROR:  invalid input syntax for type interval: "foo"
CREATE TABLE t2(time int2 NOT NULL, device text, value float) WITH (tsdb.hypertable,tsdb.partition_column='time',tsdb.chunk_interval='3 months');
ERROR:  invalid input syntax for type smallint: "3 months"
CREATE TABLE t2(time timestamptz, device text, value float) WITH (tsdb.create_default_indexes='time');
ERROR:  invalid value for tsdb.create_default_indexes 'time'
HINT:  tsdb.create_default_indexes must be a valid bool
CREATE TABLE t2(time timestamptz, device text, value float) WITH (tsdb.create_default_indexes=2);
ERROR:  invalid value for tsdb.create_default_indexes '2'
HINT:  tsdb.create_default_indexes must be a valid bool
CREATE TABLE t2(time timestamptz, device text, value float) WITH (tsdb.create_default_indexes=-1);
ERROR:  invalid value for tsdb.create_default_indexes '-1'
HINT:  tsdb.create_default_indexes must be a valid bool
CREATE TABLE t2(time timestamptz NOT NULL, device text, value float) WITH (tsdb.hypertable,tsdb.partition_column='time',tsdb.columnstore=true);
ERROR:  functionality not supported under the current "apache" license. Learn more at https://tsdb.co/pdbir1r3
HINT:  To access all features and the best time-series experience, try out Timescale Cloud.
CREATE TABLE t2(time timestamptz NOT NULL, device text, value float) WITH (tsdb.columnstore,tsdb.hypertable,tsdb.partition_column='time');
ERROR:  functionality not supported under the current "apache" license. Learn more at https://tsdb.co/pdbir1r3
HINT:  To access all features and the best time-series experience, try out Timescale Cloud.
-- Test error hint for invalid timescaledb options during CREATE TABLE
CREATE TABLE t2(time timestamptz, device text, value float) WITH (tsdb.invalid_option = true);
ERROR:  unrecognized parameter "tsdb.invalid_option"
HINT:  Valid timescaledb parameters are: hypertable, columnstore, partition_column, chunk_interval, create_default_indexes, associated_schema, associated_table_prefix, orderby, segmentby, compress_index
CREATE TABLE t2(time timestamptz, device text, value float) WITH (timescaledb.nonexistent_param = false);
ERROR:  unrecognized parameter "timescaledb.nonexistent_param"
HINT:  Valid timescaledb parameters are: hypertable, columnstore, partition_column, chunk_interval, create_default_indexes, associated_schema, associated_table_prefix, orderby, segmentby, compress_index
\set ON_ERROR_STOP 1
\set VERBOSITY terse
BEGIN;
CREATE TABLE t3(time timestamptz NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time');
CREATE TABLE t4(time timestamp, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,timescaledb.partition_column='time');
CREATE TABLE t5(time date, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time',autovacuum_enabled);
CREATE TABLE t6(time timestamptz NOT NULL, device text, value float) WITH (tsdb.columnstore=false,timescaledb.hypertable,tsdb.partition_column='time');
CREATE TABLE t7(time timestamptz, device text, value float) WITH (timescaledb.hypertable,tsdb.partition_column='time');
SELECT hypertable_name FROM timescaledb_information.hypertables ORDER BY 1;
 hypertable_name 
-----------------
 t3
 t4
 t5
 t6
 t7

ROLLBACK;
-- IF NOT EXISTS
BEGIN;
CREATE TABLE IF NOT EXISTS t7(time timestamptz NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time');
CREATE TABLE IF NOT EXISTS t7(time timestamptz NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time');
NOTICE:  relation "t7" already exists, skipping
CREATE TABLE IF NOT EXISTS t7(time timestamptz NOT NULL, device text, value float);
NOTICE:  relation "t7" already exists, skipping
SELECT hypertable_name FROM timescaledb_information.hypertables ORDER BY 1;
 hypertable_name 
-----------------
 t7

ROLLBACK;
-- table won't be converted to hypertable unless it is in the initial CREATE TABLE
BEGIN;
CREATE TABLE IF NOT EXISTS t8(time timestamptz NOT NULL, device text, value float);
CREATE TABLE IF NOT EXISTS t8(time timestamptz NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time');
NOTICE:  relation "t8" already exists, skipping
CREATE TABLE IF NOT EXISTS t8(time timestamptz NOT NULL, device text, value float);
NOTICE:  relation "t8" already exists, skipping
SELECT hypertable_name FROM timescaledb_information.hypertables ORDER BY 1;
 hypertable_name 
-----------------

ROLLBACK;
-- chunk_interval
BEGIN;
CREATE TABLE IF NOT EXISTS t9(time timestamptz NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time',tsdb.chunk_interval='8weeks');
SELECT hypertable_name, column_name, column_type, time_interval FROM timescaledb_information.dimensions;
 hypertable_name | column_name |       column_type        | time_interval 
-----------------+-------------+--------------------------+---------------
 t9              | time        | timestamp with time zone | @ 56 days

ROLLBACK;
BEGIN;
CREATE TABLE IF NOT EXISTS t9(time timestamp NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time',tsdb.chunk_interval='23 days');
SELECT hypertable_name, column_name, column_type, time_interval FROM timescaledb_information.dimensions;
 hypertable_name | column_name |         column_type         | time_interval 
-----------------+-------------+-----------------------------+---------------
 t9              | time        | timestamp without time zone | @ 23 days

ROLLBACK;
BEGIN;
CREATE TABLE IF NOT EXISTS t9(time date NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time',tsdb.chunk_interval='3 months');
SELECT hypertable_name, column_name, column_type, time_interval FROM timescaledb_information.dimensions;
 hypertable_name | column_name | column_type | time_interval 
-----------------+-------------+-------------+---------------
 t9              | time        | date        | @ 90 days

ROLLBACK;
BEGIN;
CREATE TABLE IF NOT EXISTS t9(time int2 NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time',tsdb.chunk_interval=12);
SELECT hypertable_name, column_name, column_type, integer_interval FROM timescaledb_information.dimensions;
 hypertable_name | column_name | column_type | integer_interval 
-----------------+-------------+-------------+------------------
 t9              | time        | smallint    |               12

ROLLBACK;
BEGIN;
CREATE TABLE IF NOT EXISTS t9(time int4 NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time',tsdb.chunk_interval=3453);
SELECT hypertable_name, column_name, column_type, integer_interval FROM timescaledb_information.dimensions;
 hypertable_name | column_name | column_type | integer_interval 
-----------------+-------------+-------------+------------------
 t9              | time        | integer     |             3453

ROLLBACK;
BEGIN;
CREATE TABLE IF NOT EXISTS t9(time int8 NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time',tsdb.chunk_interval=32768);
SELECT hypertable_name, column_name, column_type, integer_interval FROM timescaledb_information.dimensions;
 hypertable_name | column_name | column_type | integer_interval 
-----------------+-------------+-------------+------------------
 t9              | time        | bigint      |            32768

ROLLBACK;
-- create_default_indexes
BEGIN;
CREATE TABLE t10(time timestamptz NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time');
SELECT indexrelid::regclass from pg_index where indrelid='t10'::regclass ORDER BY indexrelid::regclass::text;
  indexrelid  
--------------
 t10_time_idx

ROLLBACK;
BEGIN;
CREATE TABLE t10(time timestamptz NOT NULL PRIMARY KEY, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time');
SELECT indexrelid::regclass from pg_index where indrelid='t10'::regclass ORDER BY indexrelid::regclass::text;
 indexrelid 
------------
 t10_pkey

ROLLBACK;
BEGIN;
CREATE TABLE t10(time timestamptz NOT NULL UNIQUE, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time');
SELECT indexrelid::regclass from pg_index where indrelid='t10'::regclass ORDER BY indexrelid::regclass::text;
  indexrelid  
--------------
 t10_time_key

ROLLBACK;
BEGIN;
CREATE TABLE t10(time timestamptz NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time',tsdb.create_default_indexes=true);
SELECT indexrelid::regclass from pg_index where indrelid='t10'::regclass ORDER BY indexrelid::regclass::text;
  indexrelid  
--------------
 t10_time_idx

ROLLBACK;
BEGIN;
CREATE TABLE t10(time timestamptz NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time',tsdb.create_default_indexes=false);
SELECT indexrelid::regclass from pg_index where indrelid='t10'::regclass ORDER BY indexrelid::regclass::text;
 indexrelid 
------------

ROLLBACK;
-- associated_schema
BEGIN;
CREATE TABLE t11(time timestamptz NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time');
SELECT associated_schema_name FROM _timescaledb_catalog.hypertable WHERE table_name = 't11';
 associated_schema_name 
------------------------
 _timescaledb_internal

ROLLBACK;
BEGIN;
CREATE TABLE t11(time timestamptz NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time', tsdb.associated_schema='abc');
SELECT associated_schema_name FROM _timescaledb_catalog.hypertable WHERE table_name = 't11';
 associated_schema_name 
------------------------
 abc

INSERT INTO t11 SELECT '2025-01-01', 'd1', 0.1;
SELECT relname from pg_class where relnamespace = 'abc'::regnamespace ORDER BY 1;
            relname             
--------------------------------
 _hyper_21_1_chunk
 _hyper_21_1_chunk_t11_time_idx

ROLLBACK;
BEGIN;
CREATE SCHEMA abc2;
CREATE TABLE t11(time timestamptz NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time', tsdb.associated_schema='abc2');
SELECT associated_schema_name FROM _timescaledb_catalog.hypertable WHERE table_name = 't11';
 associated_schema_name 
------------------------
 abc2

INSERT INTO t11 SELECT '2025-01-01', 'd1', 0.1;
SELECT relname from pg_class where relnamespace = 'abc2'::regnamespace ORDER BY 1;
            relname             
--------------------------------
 _hyper_22_2_chunk
 _hyper_22_2_chunk_t11_time_idx

ROLLBACK;
-- associated_table_prefix
BEGIN;
CREATE TABLE t12(time timestamptz NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time');
SELECT associated_table_prefix FROM _timescaledb_catalog.hypertable WHERE table_name = 't12';
 associated_table_prefix 
-------------------------
 _hyper_23

ROLLBACK;
BEGIN;
CREATE TABLE t12(time timestamptz NOT NULL, device text, value float) WITH (tsdb.columnstore=false,tsdb.hypertable,tsdb.partition_column='time', tsdb.associated_schema='abc', tsdb.associated_table_prefix='tbl_prefix');
SELECT associated_table_prefix FROM _timescaledb_catalog.hypertable WHERE table_name = 't12';
 associated_table_prefix 
-------------------------
 tbl_prefix

INSERT INTO t12 SELECT '2025-01-01', 'd1', 0.1;
SELECT relname from pg_class where relnamespace = 'abc'::regnamespace ORDER BY 1;
             relname             
---------------------------------
 tbl_prefix_3_chunk
 tbl_prefix_3_chunk_t12_time_idx

ROLLBACK;
-- default partition column
BEGIN;
CREATE TABLE t13(time timestamptz, device text, value float) WITH (tsdb.hypertable);
NOTICE:  using column "time" as partitioning column
CREATE TABLE t14("TiMe" timestamptz, device text, value float) WITH (tsdb.hypertable);
NOTICE:  using column "TiMe" as partitioning column
SELECT hypertable_name, column_name FROM timescaledb_information.dimensions WHERE hypertable_name IN ('t13','t14') ORDER BY 1;
 hypertable_name | column_name 
-----------------+-------------
 t13             | time
 t14             | TiMe

ROLLBACK;
