-- 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.
create table sparse(ts int, value float);
select create_hypertable('sparse', 'ts');
  create_hypertable  
---------------------
 (1,public,sparse,t)

insert into sparse select x, x from generate_series(1, 10000) x;
alter table sparse set (timescaledb.compress, timescaledb.order_by='ts desc');
-- When the chunks are compressed, minmax metadata are created for columns that
-- have btree indexes.
create index ii on sparse(value);
select count(compress_chunk(x)) from show_chunks('sparse') x;
 count 
-------
     1

vacuum analyze sparse;
explain (buffers off, costs off) select * from sparse where value = 1;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk
   Vectorized Filter: (value = '1'::double precision)
   ->  Seq Scan on compress_hyper_2_2_chunk
         Filter: ((_ts_meta_v2_min_value <= '1'::double precision) AND (_ts_meta_v2_max_value >= '1'::double precision))

-- Should be disabled with the GUC
set timescaledb.auto_sparse_indexes to off;
select count(compress_chunk(x, recompress:=true)) from show_chunks('sparse') x;
 count 
-------
     1

vacuum analyze sparse;
explain (buffers off, costs off) select * from sparse where value = 1;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk
   Vectorized Filter: (value = '1'::double precision)
   ->  Seq Scan on compress_hyper_2_3_chunk

reset timescaledb.auto_sparse_indexes;
select count(compress_chunk(x, recompress:=true)) from show_chunks('sparse') x;
 count 
-------
     1

vacuum analyze sparse;
explain (buffers off, costs off) select * from sparse where value = 1;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk
   Vectorized Filter: (value = '1'::double precision)
   ->  Seq Scan on compress_hyper_2_4_chunk
         Filter: ((_ts_meta_v2_min_value <= '1'::double precision) AND (_ts_meta_v2_max_value >= '1'::double precision))

-- Should survive renames.
alter table sparse rename column value to wert;
explain (buffers off, costs off) select * from sparse where wert = 1;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk
   Vectorized Filter: (wert = '1'::double precision)
   ->  Seq Scan on compress_hyper_2_4_chunk
         Filter: ((_ts_meta_v2_min_wert <= '1'::double precision) AND (_ts_meta_v2_max_wert >= '1'::double precision))

alter table sparse rename column wert to value;
explain (buffers off, costs off) select * from sparse where value = 1;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk
   Vectorized Filter: (value = '1'::double precision)
   ->  Seq Scan on compress_hyper_2_4_chunk
         Filter: ((_ts_meta_v2_min_value <= '1'::double precision) AND (_ts_meta_v2_max_value >= '1'::double precision))

-- Not for expression indexes.
drop index ii;
create index ii on sparse((value + 1));
select count(compress_chunk(x, recompress:=true)) from show_chunks('sparse') x;
 count 
-------
     1

vacuum analyze sparse;
explain (buffers off, costs off) select * from sparse where value = 1;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk
   Vectorized Filter: (value = '1'::double precision)
   ->  Seq Scan on compress_hyper_2_5_chunk

-- Not for other index types.
drop index ii;
create index ii on sparse using hash(value);
select count(compress_chunk(x, recompress:=true)) from show_chunks('sparse') x;
 count 
-------
     1

vacuum analyze sparse;
explain (buffers off, costs off) select * from sparse where value = 1;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk
   Vectorized Filter: (value = '1'::double precision)
   ->  Seq Scan on compress_hyper_2_6_chunk
         Filter: ((_ts_meta_v2_min_value <= '1'::double precision) AND (_ts_meta_v2_max_value >= '1'::double precision))

-- When the chunk is recompressed without index, no sparse index is created.
drop index ii;
select count(compress_chunk(x, recompress:=true)) from show_chunks('sparse') x;
 count 
-------
     1

vacuum analyze sparse;
explain (buffers off, costs off) select * from sparse where value = 1;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk
   Vectorized Filter: (value = '1'::double precision)
   ->  Seq Scan on compress_hyper_2_7_chunk

-- Long column names.
select count(decompress_chunk(x)) from show_chunks('sparse') x;
 count 
-------
     1

\set ECHO none
select count(compress_chunk(x)) from show_chunks('sparse') x;
 count 
-------
     1

vacuum analyze sparse;
explain (buffers off, costs off) select * from sparse where Abcdef012345678_Bbcdef012345678_Cbcdef012345678_Dbcdef0 = 1;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk
   Vectorized Filter: (abcdef012345678_bbcdef012345678_cbcdef012345678_dbcdef0 = 1)
   ->  Seq Scan on compress_hyper_2_8_chunk
         Filter: _timescaledb_functions.bloom1_contains(regress-test-bloom_9218_abcdef012345678_bbcdef012345678_cbcdef0, 1)

