-- This file and its contents are licensed under the Timescale License.
-- Please see the included NOTICE for copyright information and
-- LICENSE-TIMESCALE for a copy of the license.
-- Test various cases of COPY with decompression and different chunk layouts.
create table cdmlcopy(filler bigint, ts int, value float, metric text);
select create_hypertable('cdmlcopy', 'ts', chunk_time_interval => 1000);
   create_hypertable   
-----------------------
 (1,public,cdmlcopy,t)

alter table cdmlcopy add unique (metric, ts);
alter table cdmlcopy set (timescaledb.compress, timescaledb.compress_segmentby = 'metric');
\copy cdmlcopy from stdin
select count(compress_chunk(x)) from show_chunks('cdmlcopy') x;
 count 
-------
     1

alter table cdmlcopy drop column filler;
\copy cdmlcopy from stdin
select count(compress_chunk(x)) from show_chunks('cdmlcopy') x;
NOTICE:  chunk "_hyper_1_1_chunk" is already converted to columnstore
 count 
-------
     2

\set ON_ERROR_STOP 0
\copy cdmlcopy from stdin
ERROR:  duplicate key value violates unique constraint "1_1_cdmlcopy_metric_ts_key"
\copy cdmlcopy from stdin
ERROR:  duplicate key value violates unique constraint "3_2_cdmlcopy_metric_ts_key"
\set ON_ERROR_STOP 1
-- Also test the code path where the chunk insert state goes out of cache.
set timescaledb.max_open_chunks_per_insert = 1;
select count(compress_chunk(x)) from show_chunks('cdmlcopy') x;
NOTICE:  chunk "_hyper_1_1_chunk" is already converted to columnstore
NOTICE:  chunk "_hyper_1_3_chunk" is already converted to columnstore
 count 
-------
     2

\copy cdmlcopy from stdin
reset timescaledb.max_open_chunks_per_insert;
select count(compress_chunk(x)) from show_chunks('cdmlcopy') x;
 count 
-------
     2

drop table cdmlcopy;
