-- 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.
-- test error handling _timescaledb_functions.calculate_chunk_interval
\set ON_ERROR_STOP 0
SELECT _timescaledb_functions.calculate_chunk_interval(0,0,-0);
ERROR:  could not find a matching hypertable for dimension 0
SELECT _timescaledb_functions.calculate_chunk_interval(1,0,-1);
ERROR:  chunk_target_size must be positive
\set ON_ERROR_STOP 1
-- Valid chunk sizing function for testing
CREATE OR REPLACE FUNCTION calculate_chunk_interval(
        dimension_id INTEGER,
        dimension_coord BIGINT,
        chunk_target_size BIGINT
)
    RETURNS BIGINT LANGUAGE PLPGSQL AS
$BODY$
DECLARE
BEGIN
    RETURN -1;
END
$BODY$;
-- Chunk sizing function with bad signature
CREATE OR REPLACE FUNCTION bad_calculate_chunk_interval(
        dimension_id INTEGER
)
    RETURNS BIGINT LANGUAGE PLPGSQL AS
$BODY$
DECLARE
BEGIN
    RETURN -1;
END
$BODY$;
-- Set a fixed memory cache size to make tests determinstic
-- (independent of available machine memory)
SELECT * FROM test.set_memory_cache_size('2GB');
 set_memory_cache_size 
-----------------------
            2147483648

-- test NULL handling
\set ON_ERROR_STOP 0
SELECT * FROM set_adaptive_chunking(NULL,NULL);
ERROR:  invalid hypertable: cannot be NULL
\set ON_ERROR_STOP 1
CREATE TABLE test_adaptive(time timestamptz, temp float, location int);
\set ON_ERROR_STOP 0
-- Bad signature of sizing func should fail
SELECT create_hypertable('test_adaptive', 'time',
                         chunk_target_size => '1MB',
                         chunk_sizing_func => 'bad_calculate_chunk_interval');
ERROR:  invalid function signature
\set ON_ERROR_STOP 1
-- Setting sizing func with correct signature should work
SELECT create_hypertable('test_adaptive', 'time',
                         chunk_target_size => '1MB',
                         chunk_sizing_func => 'calculate_chunk_interval');
WARNING:  target chunk size for adaptive chunking is less than 10 MB
NOTICE:  adaptive chunking is a BETA feature and is not recommended for production deployments
     create_hypertable      
----------------------------
 (1,public,test_adaptive,t)

DROP TABLE test_adaptive;
CREATE TABLE test_adaptive(time timestamptz, temp float, location int);
-- Size but no explicit func should use default func
SELECT create_hypertable('test_adaptive', 'time',
                         chunk_target_size => '1MB',
                         create_default_indexes => true);
WARNING:  target chunk size for adaptive chunking is less than 10 MB
NOTICE:  adaptive chunking is a BETA feature and is not recommended for production deployments
     create_hypertable      
----------------------------
 (2,public,test_adaptive,t)

SELECT table_name, chunk_sizing_func_schema, chunk_sizing_func_name, chunk_target_size
FROM _timescaledb_catalog.hypertable;
  table_name   | chunk_sizing_func_schema |  chunk_sizing_func_name  | chunk_target_size 
---------------+--------------------------+--------------------------+-------------------
 test_adaptive | _timescaledb_functions   | calculate_chunk_interval |           1048576

-- Check that adaptive chunking sets a 1 day default chunk time
-- interval => 86400000000 microseconds
SELECT * FROM _timescaledb_catalog.dimension;
 id | hypertable_id | column_name |       column_type        | aligned | num_slices | partitioning_func_schema | partitioning_func | interval_length | compress_interval_length | integer_now_func_schema | integer_now_func 
----+---------------+-------------+--------------------------+---------+------------+--------------------------+-------------------+-----------------+--------------------------+-------------------------+------------------
  2 |             2 | time        | timestamp with time zone | t       |            |                          |                   |     86400000000 |                          |                         | 

-- Change the target size
SELECT * FROM set_adaptive_chunking('test_adaptive', '2MB');
WARNING:  target chunk size for adaptive chunking is less than 10 MB
                chunk_sizing_func                | chunk_target_size 
-------------------------------------------------+-------------------
 _timescaledb_functions.calculate_chunk_interval |           2097152

SELECT table_name, chunk_sizing_func_schema, chunk_sizing_func_name, chunk_target_size
FROM _timescaledb_catalog.hypertable;
  table_name   | chunk_sizing_func_schema |  chunk_sizing_func_name  | chunk_target_size 
---------------+--------------------------+--------------------------+-------------------
 test_adaptive | _timescaledb_functions   | calculate_chunk_interval |           2097152

\set ON_ERROR_STOP 0
-- Setting NULL func should fail
SELECT * FROM set_adaptive_chunking('test_adaptive', '1MB', NULL);
ERROR:  invalid chunk sizing function
\set ON_ERROR_STOP 1
-- Setting NULL size disables adaptive chunking
SELECT * FROM set_adaptive_chunking('test_adaptive', NULL);
                chunk_sizing_func                | chunk_target_size 
-------------------------------------------------+-------------------
 _timescaledb_functions.calculate_chunk_interval |                 0

SELECT table_name, chunk_sizing_func_schema, chunk_sizing_func_name, chunk_target_size
FROM _timescaledb_catalog.hypertable;
  table_name   | chunk_sizing_func_schema |  chunk_sizing_func_name  | chunk_target_size 
---------------+--------------------------+--------------------------+-------------------
 test_adaptive | _timescaledb_functions   | calculate_chunk_interval |                 0

SELECT * FROM set_adaptive_chunking('test_adaptive', '1MB');
WARNING:  target chunk size for adaptive chunking is less than 10 MB
                chunk_sizing_func                | chunk_target_size 
-------------------------------------------------+-------------------
 _timescaledb_functions.calculate_chunk_interval |           1048576

-- Setting size to 'off' should also disable
SELECT * FROM set_adaptive_chunking('test_adaptive', 'off');
                chunk_sizing_func                | chunk_target_size 
-------------------------------------------------+-------------------
 _timescaledb_functions.calculate_chunk_interval |                 0

SELECT table_name, chunk_sizing_func_schema, chunk_sizing_func_name, chunk_target_size
FROM _timescaledb_catalog.hypertable;
  table_name   | chunk_sizing_func_schema |  chunk_sizing_func_name  | chunk_target_size 
---------------+--------------------------+--------------------------+-------------------
 test_adaptive | _timescaledb_functions   | calculate_chunk_interval |                 0

-- Setting 0 size should also disable
SELECT * FROM set_adaptive_chunking('test_adaptive', '0MB');
                chunk_sizing_func                | chunk_target_size 
-------------------------------------------------+-------------------
 _timescaledb_functions.calculate_chunk_interval |                 0

SELECT table_name, chunk_sizing_func_schema, chunk_sizing_func_name, chunk_target_size
FROM _timescaledb_catalog.hypertable;
  table_name   | chunk_sizing_func_schema |  chunk_sizing_func_name  | chunk_target_size 
---------------+--------------------------+--------------------------+-------------------
 test_adaptive | _timescaledb_functions   | calculate_chunk_interval |                 0

SELECT * FROM set_adaptive_chunking('test_adaptive', '1MB');
WARNING:  target chunk size for adaptive chunking is less than 10 MB
                chunk_sizing_func                | chunk_target_size 
-------------------------------------------------+-------------------
 _timescaledb_functions.calculate_chunk_interval |           1048576

-- No warning about small target size if > 10MB
SELECT * FROM set_adaptive_chunking('test_adaptive', '11MB');
                chunk_sizing_func                | chunk_target_size 
-------------------------------------------------+-------------------
 _timescaledb_functions.calculate_chunk_interval |          11534336

-- Setting size to 'estimate' should also estimate size
SELECT * FROM set_adaptive_chunking('test_adaptive', 'estimate');
                chunk_sizing_func                | chunk_target_size 
-------------------------------------------------+-------------------
 _timescaledb_functions.calculate_chunk_interval |        1932735283

SELECT table_name, chunk_sizing_func_schema, chunk_sizing_func_name, chunk_target_size
FROM _timescaledb_catalog.hypertable;
  table_name   | chunk_sizing_func_schema |  chunk_sizing_func_name  | chunk_target_size 
---------------+--------------------------+--------------------------+-------------------
 test_adaptive | _timescaledb_functions   | calculate_chunk_interval |        1932735283

-- Use a lower memory setting to test that the calculated chunk_target_size is reduced
SELECT * FROM test.set_memory_cache_size('512MB');
 set_memory_cache_size 
-----------------------
             536870912

SELECT * FROM set_adaptive_chunking('test_adaptive', 'estimate');
                chunk_sizing_func                | chunk_target_size 
-------------------------------------------------+-------------------
 _timescaledb_functions.calculate_chunk_interval |         483183820

SELECT table_name, chunk_sizing_func_schema, chunk_sizing_func_name, chunk_target_size
FROM _timescaledb_catalog.hypertable;
  table_name   | chunk_sizing_func_schema |  chunk_sizing_func_name  | chunk_target_size 
---------------+--------------------------+--------------------------+-------------------
 test_adaptive | _timescaledb_functions   | calculate_chunk_interval |         483183820

-- Reset memory settings
SELECT * FROM test.set_memory_cache_size('2GB');
 set_memory_cache_size 
-----------------------
            2147483648

-- Set a reasonable test value
SELECT * FROM set_adaptive_chunking('test_adaptive', '1MB');
WARNING:  target chunk size for adaptive chunking is less than 10 MB
                chunk_sizing_func                | chunk_target_size 
-------------------------------------------------+-------------------
 _timescaledb_functions.calculate_chunk_interval |           1048576

-- Show the interval length before and after adaptation
SELECT id, hypertable_id, interval_length FROM _timescaledb_catalog.dimension;
 id | hypertable_id | interval_length 
----+---------------+-----------------
  2 |             2 |     86400000000

-- Generate data to create chunks. We use the hash of the time value
-- to get determinstic location IDs so that we always spread these
-- values the same way across space partitions
INSERT INTO test_adaptive
SELECT time, random() * 35, _timescaledb_functions.get_partition_hash(time) FROM
generate_series('2017-03-07T18:18:03+00'::timestamptz - interval '175 days',
                '2017-03-07T18:18:03+00'::timestamptz,
                '2 minutes') as time;
SELECT chunk_name, primary_dimension, range_start, range_end
FROM  timescaledb_information.chunks
WHERE hypertable_name = 'test_adaptive' ORDER BY chunk_name;
    chunk_name     | primary_dimension |             range_start             |              range_end              
-------------------+-------------------+-------------------------------------+-------------------------------------
 _hyper_2_10_chunk | time              | Fri Sep 23 22:08:15.728855 2016 PDT | Sat Oct 01 13:16:09.024252 2016 PDT
 _hyper_2_11_chunk | time              | Sat Oct 01 13:16:09.024252 2016 PDT | Fri Oct 14 03:19:44.231212 2016 PDT
 _hyper_2_12_chunk | time              | Fri Oct 14 03:19:44.231212 2016 PDT | Wed Oct 26 19:20:54.4938 2016 PDT
 _hyper_2_13_chunk | time              | Wed Oct 26 19:20:54.4938 2016 PDT   | Fri Nov 04 04:03:56.248528 2016 PDT
 _hyper_2_14_chunk | time              | Fri Nov 04 04:03:56.248528 2016 PDT | Fri Nov 18 21:58:20.411232 2016 PST
 _hyper_2_15_chunk | time              | Fri Nov 18 21:58:20.411232 2016 PST | Sat Dec 03 16:52:44.573936 2016 PST
 _hyper_2_16_chunk | time              | Sat Dec 03 16:52:44.573936 2016 PST | Sun Dec 18 11:47:08.73664 2016 PST
 _hyper_2_17_chunk | time              | Sun Dec 18 11:47:08.73664 2016 PST  | Mon Jan 02 06:41:32.899344 2017 PST
 _hyper_2_18_chunk | time              | Mon Jan 02 06:41:32.899344 2017 PST | Tue Jan 17 01:35:57.062048 2017 PST
 _hyper_2_19_chunk | time              | Tue Jan 17 01:35:57.062048 2017 PST | Tue Jan 31 20:30:21.224752 2017 PST
 _hyper_2_1_chunk  | time              | Mon Sep 12 17:00:00 2016 PDT        | Tue Sep 13 17:00:00 2016 PDT
 _hyper_2_20_chunk | time              | Tue Jan 31 20:30:21.224752 2017 PST | Wed Feb 15 15:24:45.387456 2017 PST
 _hyper_2_21_chunk | time              | Wed Feb 15 15:24:45.387456 2017 PST | Thu Mar 02 10:19:09.55016 2017 PST
 _hyper_2_22_chunk | time              | Thu Mar 02 10:19:09.55016 2017 PST  | Fri Mar 17 06:13:33.712864 2017 PDT
 _hyper_2_2_chunk  | time              | Tue Sep 13 17:00:00 2016 PDT        | Wed Sep 14 17:00:00 2016 PDT
 _hyper_2_3_chunk  | time              | Wed Sep 14 17:00:00 2016 PDT        | Thu Sep 15 17:00:00 2016 PDT
 _hyper_2_4_chunk  | time              | Thu Sep 15 17:00:00 2016 PDT        | Fri Sep 16 15:02:54.2208 2016 PDT
 _hyper_2_5_chunk  | time              | Fri Sep 16 15:02:54.2208 2016 PDT   | Sun Sep 18 03:12:14.342144 2016 PDT
 _hyper_2_6_chunk  | time              | Sun Sep 18 03:12:14.342144 2016 PDT | Mon Sep 19 15:21:34.463488 2016 PDT
 _hyper_2_7_chunk  | time              | Mon Sep 19 15:21:34.463488 2016 PDT | Wed Sep 21 03:30:54.584832 2016 PDT
 _hyper_2_8_chunk  | time              | Wed Sep 21 03:30:54.584832 2016 PDT | Thu Sep 22 03:45:14.901568 2016 PDT
 _hyper_2_9_chunk  | time              | Thu Sep 22 03:45:14.901568 2016 PDT | Fri Sep 23 22:08:15.728855 2016 PDT

-- Do same thing without an index on the time column. This affects
-- both the calculation of fill-factor of the chunk and its size
CREATE TABLE test_adaptive_no_index(time timestamptz, temp float, location int);
-- Size but no explicit func should use default func
-- No default indexes should warn and use heap scan for min and max
SELECT create_hypertable('test_adaptive_no_index', 'time',
                         chunk_target_size => '1MB',
                         create_default_indexes => false);
WARNING:  target chunk size for adaptive chunking is less than 10 MB
WARNING:  no index on "time" found for adaptive chunking on hypertable "test_adaptive_no_index"
NOTICE:  adaptive chunking is a BETA feature and is not recommended for production deployments
          create_hypertable          
-------------------------------------
 (3,public,test_adaptive_no_index,t)

SELECT id, hypertable_id, interval_length FROM _timescaledb_catalog.dimension;
 id | hypertable_id | interval_length 
----+---------------+-----------------
  2 |             2 |   1277664162704
  3 |             3 |     86400000000

INSERT INTO test_adaptive_no_index
SELECT time, random() * 35, _timescaledb_functions.get_partition_hash(time) FROM
generate_series('2017-03-07T18:18:03+00'::timestamptz - interval '175 days',
                '2017-03-07T18:18:03+00'::timestamptz,
                '2 minutes') as time;
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_23_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_23_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_24_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_23_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_24_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_25_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_24_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_25_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_26_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_25_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_26_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_27_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_26_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_27_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_28_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_27_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_28_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_29_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_28_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_29_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_30_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_29_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_30_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_31_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_30_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_31_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_32_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_31_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_32_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_33_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_32_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_33_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_34_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_33_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_34_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_35_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_34_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_35_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_36_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_35_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_36_chunk"
WARNING:  no index on "time" found for adaptive chunking on chunk "_hyper_3_37_chunk"
SELECT chunk_name, primary_dimension, range_start, range_end
FROM  timescaledb_information.chunks
WHERE hypertable_name = 'test_adaptive_no_index' ORDER BY chunk_name;
    chunk_name     | primary_dimension |             range_start             |              range_end              
-------------------+-------------------+-------------------------------------+-------------------------------------
 _hyper_3_23_chunk | time              | Mon Sep 12 17:00:00 2016 PDT        | Tue Sep 13 17:00:00 2016 PDT
 _hyper_3_24_chunk | time              | Tue Sep 13 17:00:00 2016 PDT        | Wed Sep 14 17:00:00 2016 PDT
 _hyper_3_25_chunk | time              | Wed Sep 14 17:00:00 2016 PDT        | Thu Sep 15 17:00:00 2016 PDT
 _hyper_3_26_chunk | time              | Thu Sep 15 17:00:00 2016 PDT        | Sun Sep 18 02:18:45.310968 2016 PDT
 _hyper_3_27_chunk | time              | Sun Sep 18 02:18:45.310968 2016 PDT | Sun Sep 18 06:20:21.359312 2016 PDT
 _hyper_3_28_chunk | time              | Sun Sep 18 06:20:21.359312 2016 PDT | Wed Sep 21 08:25:00.957966 2016 PDT
 _hyper_3_29_chunk | time              | Wed Sep 21 08:25:00.957966 2016 PDT | Thu Sep 22 03:26:42.599807 2016 PDT
 _hyper_3_30_chunk | time              | Thu Sep 22 03:26:42.599807 2016 PDT | Sun Sep 25 18:03:30.59359 2016 PDT
 _hyper_3_31_chunk | time              | Sun Sep 25 18:03:30.59359 2016 PDT  | Sat Oct 08 05:32:02.75732 2016 PDT
 _hyper_3_32_chunk | time              | Sat Oct 08 05:32:02.75732 2016 PDT  | Mon Oct 31 07:33:42.652938 2016 PDT
 _hyper_3_33_chunk | time              | Mon Oct 31 07:33:42.652938 2016 PDT | Wed Nov 23 08:35:22.548556 2016 PST
 _hyper_3_34_chunk | time              | Wed Nov 23 08:35:22.548556 2016 PST | Thu Dec 15 09:48:28.1888 2016 PST
 _hyper_3_35_chunk | time              | Thu Dec 15 09:48:28.1888 2016 PST   | Wed Jan 11 04:57:38.357845 2017 PST
 _hyper_3_36_chunk | time              | Wed Jan 11 04:57:38.357845 2017 PST | Tue Feb 07 00:06:48.52689 2017 PST
 _hyper_3_37_chunk | time              | Tue Feb 07 00:06:48.52689 2017 PST  | Sun Mar 05 19:15:58.695935 2017 PST
 _hyper_3_38_chunk | time              | Sun Mar 05 19:15:58.695935 2017 PST | Sat Apr 01 15:25:08.86498 2017 PDT

-- Test added to check that the correct index (i.e. time index) is being used
-- to find the min and max. Previously a bug selected the first index listed,
-- which in this case is location rather than time and therefore could return
-- the wrong min and max if items at the start and end of the index did not have
-- the correct min and max timestamps.
--
-- In this test, we create chunks with a lot of locations with only one reading
-- that is at the beginning of the time frame, and then one location in the middle
-- of the range that has two readings, one that is the same as the others and one
-- that is larger. The algorithm should use these two readings for min & max; however,
-- if it's broken (as it was before), it would choose just the reading that is common
-- to all the locations.
CREATE TABLE test_adaptive_correct_index(time timestamptz, temp float, location int);
SELECT create_hypertable('test_adaptive_correct_index', 'time',
                         chunk_target_size => '100MB',
                         chunk_time_interval => 86400000000,
                         create_default_indexes => false);
WARNING:  no index on "time" found for adaptive chunking on hypertable "test_adaptive_correct_index"
NOTICE:  adaptive chunking is a BETA feature and is not recommended for production deployments
            create_hypertable             
------------------------------------------
 (4,public,test_adaptive_correct_index,t)

CREATE INDEX ON test_adaptive_correct_index(location);
CREATE INDEX ON test_adaptive_correct_index(time DESC);
-- First chunk
INSERT INTO test_adaptive_correct_index
SELECT '2018-01-01T00:00:00+00'::timestamptz, val, val + 1 FROM
generate_series(1, 1000) as val;
INSERT INTO test_adaptive_correct_index
SELECT time, 0.0, '1500' FROM
generate_series('2018-01-01T00:00:00+00'::timestamptz,
                '2018-01-01T20:00:00+00'::timestamptz,
                '10 hours') as time;
INSERT INTO test_adaptive_correct_index
SELECT '2018-01-01T00:00:00+00'::timestamptz, val, val + 1 FROM
generate_series(2001, 3000) as val;
-- Second chunk
INSERT INTO test_adaptive_correct_index
SELECT '2018-01-02T00:00:00+00'::timestamptz, val, val + 1 FROM
generate_series(1, 1000) as val;
INSERT INTO test_adaptive_correct_index
SELECT time, 0.0, '1500' FROM
generate_series('2018-01-02T00:00:00+00'::timestamptz,
                '2018-01-02T20:00:00+00'::timestamptz,
                '10 hours') as time;
INSERT INTO test_adaptive_correct_index
SELECT '2018-01-02T00:00:00+00'::timestamptz, val, val + 1 FROM
generate_series(2001, 3000) as val;
-- Third chunk
INSERT INTO test_adaptive_correct_index
SELECT '2018-01-03T00:00:00+00'::timestamptz, val, val + 1 FROM
generate_series(1, 1000) as val;
INSERT INTO test_adaptive_correct_index
SELECT time, 0.0, '1500' FROM
generate_series('2018-01-03T00:00:00+00'::timestamptz,
                '2018-01-03T20:00:00+00'::timestamptz,
                '10 hours') as time;
INSERT INTO test_adaptive_correct_index
SELECT '2018-01-03T00:00:00+00'::timestamptz, val, val + 1 FROM
generate_series(2001, 3000) as val;
-- This should be the start of the fourth chunk
INSERT INTO test_adaptive_correct_index
SELECT '2018-01-04T00:00:00+00'::timestamptz, val, val + 1 FROM
generate_series(1, 1000) as val;
INSERT INTO test_adaptive_correct_index
SELECT time, 0.0, '1500' FROM
generate_series('2018-01-04T00:00:00+00'::timestamptz,
                '2018-01-04T20:00:00+00'::timestamptz,
                '10 hours') as time;
INSERT INTO test_adaptive_correct_index
SELECT '2018-01-04T00:00:00+00'::timestamptz, val, val + 1 FROM
generate_series(2001, 3000) as val;
-- If working correctly, this goes in the 4th chunk, otherwise its a separate 5th chunk
INSERT INTO test_adaptive_correct_index
SELECT '2018-01-05T00:00:00+00'::timestamptz, val, val + 1 FROM
generate_series(1, 1000) as val;
INSERT INTO test_adaptive_correct_index
SELECT time, 0.0, '1500' FROM
generate_series('2018-01-05T00:00:00+00'::timestamptz,
                '2018-01-05T20:00:00+00'::timestamptz,
                '10 hours') as time;
INSERT INTO test_adaptive_correct_index
SELECT '2018-01-05T00:00:00+00'::timestamptz, val, val + 1 FROM
generate_series(2001, 3000) as val;
-- This should show 4 chunks, rather than 5
SELECT count(*)
FROM  timescaledb_information.chunks
WHERE hypertable_name = 'test_adaptive_correct_index';
 count 
-------
     4

-- The interval_length should no longer be 86400000000 for our hypertable, so 3rd column so be true.
-- Note: the exact interval_length is non-deterministic, so we can't use its actual value for tests
SELECT id, hypertable_id, interval_length > 86400000000 FROM _timescaledb_catalog.dimension;
 id | hypertable_id | ?column? 
----+---------------+----------
  2 |             2 | t
  3 |             3 | t
  4 |             4 | t

-- Drop because it's size and estimated chunk_interval is non-deterministic so
-- we don't want to make other tests flaky.
DROP TABLE test_adaptive_correct_index;
-- Test with space partitioning. This might affect the estimation
-- since there are more chunks in the same time interval and space
-- chunks might be unevenly filled.
CREATE TABLE test_adaptive_space(time timestamptz, temp float, location int);
SELECT create_hypertable('test_adaptive_space', 'time', 'location', 2,
                         chunk_target_size => '1MB',
                         create_default_indexes => true);
WARNING:  target chunk size for adaptive chunking is less than 10 MB
NOTICE:  adaptive chunking is a BETA feature and is not recommended for production deployments
        create_hypertable         
----------------------------------
 (5,public,test_adaptive_space,t)

SELECT id, hypertable_id, interval_length FROM _timescaledb_catalog.dimension;
 id | hypertable_id | interval_length 
----+---------------+-----------------
  2 |             2 |   1277664162704
  3 |             3 |   2315350169045
  5 |             5 |     86400000000
  6 |             5 |                

INSERT INTO test_adaptive_space
SELECT time, random() * 35, _timescaledb_functions.get_partition_hash(time) FROM
generate_series('2017-03-07T18:18:03+00'::timestamptz - interval '175 days',
                '2017-03-07T18:18:03+00'::timestamptz,
                '2 minutes') as time;
\x
SELECT chunk_name, range_start, range_end
FROM  timescaledb_information.chunks
WHERE hypertable_name = 'test_adaptive_space' ORDER BY chunk_name;
-[ RECORD 1 ]------------------------------------
chunk_name  | _hyper_5_43_chunk
range_start | Mon Sep 12 17:00:00 2016 PDT
range_end   | Tue Sep 13 17:00:00 2016 PDT
-[ RECORD 2 ]------------------------------------
chunk_name  | _hyper_5_44_chunk
range_start | Mon Sep 12 17:00:00 2016 PDT
range_end   | Tue Sep 13 17:00:00 2016 PDT
-[ RECORD 3 ]------------------------------------
chunk_name  | _hyper_5_45_chunk
range_start | Tue Sep 13 17:00:00 2016 PDT
range_end   | Wed Sep 14 17:00:00 2016 PDT
-[ RECORD 4 ]------------------------------------
chunk_name  | _hyper_5_46_chunk
range_start | Tue Sep 13 17:00:00 2016 PDT
range_end   | Wed Sep 14 17:00:00 2016 PDT
-[ RECORD 5 ]------------------------------------
chunk_name  | _hyper_5_47_chunk
range_start | Wed Sep 14 17:00:00 2016 PDT
range_end   | Thu Sep 15 11:47:51.47376 2016 PDT
-[ RECORD 6 ]------------------------------------
chunk_name  | _hyper_5_48_chunk
range_start | Wed Sep 14 17:00:00 2016 PDT
range_end   | Thu Sep 15 11:47:51.47376 2016 PDT
-[ RECORD 7 ]------------------------------------
chunk_name  | _hyper_5_49_chunk
range_start | Thu Sep 15 11:47:51.47376 2016 PDT
range_end   | Sat Sep 17 02:40:49.182352 2016 PDT
-[ RECORD 8 ]------------------------------------
chunk_name  | _hyper_5_50_chunk
range_start | Thu Sep 15 11:47:51.47376 2016 PDT
range_end   | Sat Sep 17 02:40:49.182352 2016 PDT
-[ RECORD 9 ]------------------------------------
chunk_name  | _hyper_5_51_chunk
range_start | Sat Sep 17 02:40:49.182352 2016 PDT
range_end   | Sun Sep 18 17:33:46.890944 2016 PDT
-[ RECORD 10 ]-----------------------------------
chunk_name  | _hyper_5_52_chunk
range_start | Sat Sep 17 02:40:49.182352 2016 PDT
range_end   | Sun Sep 18 17:33:46.890944 2016 PDT
-[ RECORD 11 ]-----------------------------------
chunk_name  | _hyper_5_53_chunk
range_start | Sun Sep 18 17:33:46.890944 2016 PDT
range_end   | Sun Sep 18 20:35:55.67676 2016 PDT
-[ RECORD 12 ]-----------------------------------
chunk_name  | _hyper_5_54_chunk
range_start | Sun Sep 18 17:33:46.890944 2016 PDT
range_end   | Sun Sep 18 20:35:55.67676 2016 PDT
-[ RECORD 13 ]-----------------------------------
chunk_name  | _hyper_5_55_chunk
range_start | Sun Sep 18 20:35:55.67676 2016 PDT
range_end   | Tue Sep 20 18:46:40.16883 2016 PDT
-[ RECORD 14 ]-----------------------------------
chunk_name  | _hyper_5_56_chunk
range_start | Sun Sep 18 20:35:55.67676 2016 PDT
range_end   | Tue Sep 20 18:46:40.16883 2016 PDT
-[ RECORD 15 ]-----------------------------------
chunk_name  | _hyper_5_57_chunk
range_start | Tue Sep 20 18:46:40.16883 2016 PDT
range_end   | Sun Oct 02 16:44:29.071032 2016 PDT
-[ RECORD 16 ]-----------------------------------
chunk_name  | _hyper_5_58_chunk
range_start | Tue Sep 20 18:46:40.16883 2016 PDT
range_end   | Sun Oct 02 16:44:29.071032 2016 PDT
-[ RECORD 17 ]-----------------------------------
chunk_name  | _hyper_5_59_chunk
range_start | Sun Oct 02 16:44:29.071032 2016 PDT
range_end   | Tue Oct 11 00:37:03.738979 2016 PDT
-[ RECORD 18 ]-----------------------------------
chunk_name  | _hyper_5_60_chunk
range_start | Sun Oct 02 16:44:29.071032 2016 PDT
range_end   | Tue Oct 11 00:37:03.738979 2016 PDT
-[ RECORD 19 ]-----------------------------------
chunk_name  | _hyper_5_61_chunk
range_start | Tue Oct 11 00:37:03.738979 2016 PDT
range_end   | Thu Oct 27 03:05:25.740618 2016 PDT
-[ RECORD 20 ]-----------------------------------
chunk_name  | _hyper_5_62_chunk
range_start | Tue Oct 11 00:37:03.738979 2016 PDT
range_end   | Thu Oct 27 03:05:25.740618 2016 PDT
-[ RECORD 21 ]-----------------------------------
chunk_name  | _hyper_5_63_chunk
range_start | Thu Oct 27 03:05:25.740618 2016 PDT
range_end   | Sun Nov 13 12:38:49.541703 2016 PST
-[ RECORD 22 ]-----------------------------------
chunk_name  | _hyper_5_64_chunk
range_start | Thu Oct 27 03:05:25.740618 2016 PDT
range_end   | Sun Nov 13 12:38:49.541703 2016 PST
-[ RECORD 23 ]-----------------------------------
chunk_name  | _hyper_5_65_chunk
range_start | Sun Nov 13 12:38:49.541703 2016 PST
range_end   | Fri Dec 02 17:45:40.237036 2016 PST
-[ RECORD 24 ]-----------------------------------
chunk_name  | _hyper_5_66_chunk
range_start | Sun Nov 13 12:38:49.541703 2016 PST
range_end   | Fri Dec 02 17:45:40.237036 2016 PST
-[ RECORD 25 ]-----------------------------------
chunk_name  | _hyper_5_67_chunk
range_start | Fri Dec 02 17:45:40.237036 2016 PST
range_end   | Wed Dec 21 22:52:30.932369 2016 PST
-[ RECORD 26 ]-----------------------------------
chunk_name  | _hyper_5_68_chunk
range_start | Fri Dec 02 17:45:40.237036 2016 PST
range_end   | Wed Dec 21 22:52:30.932369 2016 PST
-[ RECORD 27 ]-----------------------------------
chunk_name  | _hyper_5_69_chunk
range_start | Wed Dec 21 22:52:30.932369 2016 PST
range_end   | Tue Jan 10 03:59:21.627702 2017 PST
-[ RECORD 28 ]-----------------------------------
chunk_name  | _hyper_5_70_chunk
range_start | Wed Dec 21 22:52:30.932369 2016 PST
range_end   | Tue Jan 10 03:59:21.627702 2017 PST
-[ RECORD 29 ]-----------------------------------
chunk_name  | _hyper_5_71_chunk
range_start | Tue Jan 10 03:59:21.627702 2017 PST
range_end   | Sun Jan 29 09:06:12.323035 2017 PST
-[ RECORD 30 ]-----------------------------------
chunk_name  | _hyper_5_72_chunk
range_start | Tue Jan 10 03:59:21.627702 2017 PST
range_end   | Sun Jan 29 09:06:12.323035 2017 PST
-[ RECORD 31 ]-----------------------------------
chunk_name  | _hyper_5_73_chunk
range_start | Sun Jan 29 09:06:12.323035 2017 PST
range_end   | Fri Feb 17 14:13:03.018368 2017 PST
-[ RECORD 32 ]-----------------------------------
chunk_name  | _hyper_5_74_chunk
range_start | Sun Jan 29 09:06:12.323035 2017 PST
range_end   | Fri Feb 17 14:13:03.018368 2017 PST
-[ RECORD 33 ]-----------------------------------
chunk_name  | _hyper_5_75_chunk
range_start | Fri Feb 17 14:13:03.018368 2017 PST
range_end   | Wed Mar 08 19:19:53.713701 2017 PST
-[ RECORD 34 ]-----------------------------------
chunk_name  | _hyper_5_76_chunk
range_start | Fri Feb 17 14:13:03.018368 2017 PST
range_end   | Wed Mar 08 19:19:53.713701 2017 PST

SELECT *
FROM  timescaledb_information.dimensions
WHERE hypertable_name = 'test_adaptive_space' ORDER BY dimension_number;
-[ RECORD 1 ]-----+----------------------------------------
hypertable_schema | public
hypertable_name   | test_adaptive_space
dimension_number  | 1
column_name       | time
column_type       | timestamp with time zone
dimension_type    | Time
time_interval     | @ 19 days 5 hours 6 mins 50.695333 secs
integer_interval  | 
integer_now_func  | 
num_partitions    | 
-[ RECORD 2 ]-----+----------------------------------------
hypertable_schema | public
hypertable_name   | test_adaptive_space
dimension_number  | 2
column_name       | location
column_type       | integer
dimension_type    | Space
time_interval     | 
integer_interval  | 
integer_now_func  | 
num_partitions    | 2

\x
SELECT *
FROM chunks_detailed_size('test_adaptive_space') ORDER BY chunk_name;
     chunk_schema      |    chunk_name     | table_bytes | index_bytes | toast_bytes | total_bytes | node_name 
-----------------------+-------------------+-------------+-------------+-------------+-------------+-----------
 _timescaledb_internal | _hyper_5_43_chunk |        8192 |       32768 |           0 |       40960 | 
 _timescaledb_internal | _hyper_5_44_chunk |        8192 |       32768 |           0 |       40960 | 
 _timescaledb_internal | _hyper_5_45_chunk |       49152 |       57344 |           0 |      106496 | 
 _timescaledb_internal | _hyper_5_46_chunk |       49152 |       57344 |           0 |      106496 | 
 _timescaledb_internal | _hyper_5_47_chunk |       40960 |       49152 |           0 |       90112 | 
 _timescaledb_internal | _hyper_5_48_chunk |       40960 |       32768 |           0 |       73728 | 
 _timescaledb_internal | _hyper_5_49_chunk |       57344 |       81920 |           0 |      139264 | 
 _timescaledb_internal | _hyper_5_50_chunk |       57344 |       81920 |           0 |      139264 | 
 _timescaledb_internal | _hyper_5_51_chunk |       57344 |       81920 |           0 |      139264 | 
 _timescaledb_internal | _hyper_5_52_chunk |       57344 |       81920 |           0 |      139264 | 
 _timescaledb_internal | _hyper_5_53_chunk |        8192 |       32768 |           0 |       40960 | 
 _timescaledb_internal | _hyper_5_54_chunk |        8192 |       32768 |           0 |       40960 | 
 _timescaledb_internal | _hyper_5_55_chunk |       65536 |      106496 |           0 |      172032 | 
 _timescaledb_internal | _hyper_5_56_chunk |       65536 |       98304 |           0 |      163840 | 
 _timescaledb_internal | _hyper_5_57_chunk |      253952 |      360448 |           0 |      614400 | 
 _timescaledb_internal | _hyper_5_58_chunk |      253952 |      368640 |           0 |      622592 | 
 _timescaledb_internal | _hyper_5_59_chunk |      180224 |      303104 |           0 |      483328 | 
 _timescaledb_internal | _hyper_5_60_chunk |      188416 |      303104 |           0 |      491520 | 
 _timescaledb_internal | _hyper_5_61_chunk |      327680 |      540672 |           0 |      868352 | 
 _timescaledb_internal | _hyper_5_62_chunk |      327680 |      532480 |           0 |      860160 | 
 _timescaledb_internal | _hyper_5_63_chunk |      360448 |      581632 |           0 |      942080 | 
 _timescaledb_internal | _hyper_5_64_chunk |      352256 |      589824 |           0 |      942080 | 
 _timescaledb_internal | _hyper_5_65_chunk |      385024 |      598016 |           0 |      983040 | 
 _timescaledb_internal | _hyper_5_66_chunk |      393216 |      614400 |           0 |     1007616 | 
 _timescaledb_internal | _hyper_5_67_chunk |      385024 |      598016 |           0 |      983040 | 
 _timescaledb_internal | _hyper_5_68_chunk |      393216 |      598016 |           0 |      991232 | 
 _timescaledb_internal | _hyper_5_69_chunk |      393216 |      622592 |           0 |     1015808 | 
 _timescaledb_internal | _hyper_5_70_chunk |      385024 |      606208 |           0 |      991232 | 
 _timescaledb_internal | _hyper_5_71_chunk |      385024 |      614400 |           0 |      999424 | 
 _timescaledb_internal | _hyper_5_72_chunk |      393216 |      622592 |           0 |     1015808 | 
 _timescaledb_internal | _hyper_5_73_chunk |      393216 |      614400 |           0 |     1007616 | 
 _timescaledb_internal | _hyper_5_74_chunk |      385024 |      614400 |           0 |      999424 | 
 _timescaledb_internal | _hyper_5_75_chunk |      360448 |      581632 |           0 |      942080 | 
 _timescaledb_internal | _hyper_5_76_chunk |      368640 |      598016 |           0 |      966656 | 

SELECT id, hypertable_id, interval_length FROM _timescaledb_catalog.dimension;
 id | hypertable_id | interval_length 
----+---------------+-----------------
  2 |             2 |   1277664162704
  3 |             3 |   2315350169045
  6 |             5 |                
  5 |             5 |   1660010695333

-- A previous version stopped working as soon as hypertable_id stopped being
-- equal to dimension_id (i.e., there was a hypertable with more than 1 dimension).
-- This test comes after test_adaptive_space, which has 2 dimensions, and makes
-- sure that it still works.
CREATE TABLE test_adaptive_after_multiple_dims(time timestamptz, temp float, location int);
SELECT create_hypertable('test_adaptive_after_multiple_dims', 'time',
                         chunk_target_size => '100MB',
                         create_default_indexes => true);
NOTICE:  adaptive chunking is a BETA feature and is not recommended for production deployments
               create_hypertable                
------------------------------------------------
 (6,public,test_adaptive_after_multiple_dims,t)

INSERT INTO test_adaptive_after_multiple_dims VALUES('2018-01-01T00:00:00+00'::timestamptz, 0.0, 5);
\c  :TEST_DBNAME :ROLE_DEFAULT_PERM_USER_2
\set ON_ERROR_STOP 0
SELECT * FROM set_adaptive_chunking('test_adaptive', '2MB');
ERROR:  must be owner of hypertable "test_adaptive"
\set ON_ERROR_STOP 1
\c  :TEST_DBNAME :ROLE_DEFAULT_PERM_USER
-- Now make sure renaming schema gets propagated to the func_schema
DROP TABLE test_adaptive;
\c :TEST_DBNAME :ROLE_SUPERUSER
CREATE SCHEMA IF NOT EXISTS my_chunk_func_schema;
CREATE OR REPLACE FUNCTION my_chunk_func_schema.calculate_chunk_interval(
        dimension_id INTEGER,
        dimension_coord BIGINT,
        chunk_target_size BIGINT
)
    RETURNS BIGINT LANGUAGE PLPGSQL AS
$BODY$
DECLARE
BEGIN
    RETURN 2;
END
$BODY$;
CREATE TABLE test_adaptive(time timestamptz, temp float, location int);
SELECT create_hypertable('test_adaptive', 'time',
                         chunk_target_size => '1MB',
                         chunk_sizing_func => 'my_chunk_func_schema.calculate_chunk_interval');
WARNING:  target chunk size for adaptive chunking is less than 10 MB
NOTICE:  adaptive chunking is a BETA feature and is not recommended for production deployments
     create_hypertable      
----------------------------
 (7,public,test_adaptive,t)

ALTER SCHEMA my_chunk_func_schema RENAME TO new_chunk_func_schema;
INSERT INTO test_adaptive VALUES (now(), 1.0, 1);
