-- 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 test(ts int, tag text) with (tsdb.hypertable,
    tsdb.partition_column = 'ts', tsdb.compress_orderby = 'ts');
insert into test values (1, '1');
create index on test(tag);
select compress_chunk(x) from show_chunks('test') x;
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_1_1_chunk

vacuum analyze test;
-- Normal case: bloom indexes use the newest version.
explain (analyze, costs off, buffers off, timing off, summary off)
select * from test where tag = '1';
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk (actual rows=1.00 loops=1)
   Vectorized Filter: (tag = '1'::text)
   ->  Seq Scan on compress_hyper_2_2_chunk (actual rows=1.00 loops=1)
         Filter: _timescaledb_functions.bloom1_contains(regress-test-bloom_tag, '1'::text)

-- Create a bloom index with the old column name, should be disabled.
set timescaledb.bloom1_column_prefix = 'bloom1';
select compress_chunk(decompress_chunk(x)) from show_chunks('test') x;
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_1_1_chunk

vacuum analyze test;
reset timescaledb.bloom1_column_prefix;
explain (analyze, costs off, buffers off, timing off, summary off)
select * from test where tag = '1';
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk (actual rows=1.00 loops=1)
   Vectorized Filter: (tag = '1'::text)
   ->  Seq Scan on compress_hyper_2_3_chunk (actual rows=1.00 loops=1)

-- Try to enable it with the GUC.
set timescaledb.read_legacy_bloom1_v1 to on;
explain (analyze, costs off, buffers off, timing off, summary off)
select * from test where tag = '1';
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk (actual rows=1.00 loops=1)
   Vectorized Filter: (tag = '1'::text)
   ->  Seq Scan on compress_hyper_2_3_chunk (actual rows=1.00 loops=1)
         Filter: _timescaledb_functions.bloom1_contains(regress-test-bloom_tag, '1'::text)

reset timescaledb.read_legacy_bloom1_v1;
-- Test that recompression of a partial chunk with old indexes produces a
-- sensible result.
insert into test values (2, '2');
vacuum analyze test;
explain (analyze, costs off, buffers off, timing off, summary off)
select * from test where tag = '2';
--- QUERY PLAN ---
 Append (actual rows=1.00 loops=1)
   ->  Custom Scan (ColumnarScan) on _hyper_1_1_chunk (actual rows=0.00 loops=1)
         Vectorized Filter: (tag = '2'::text)
         Rows Removed by Filter: 1
         ->  Seq Scan on compress_hyper_2_3_chunk (actual rows=1.00 loops=1)
   ->  Seq Scan on _hyper_1_1_chunk (actual rows=1.00 loops=1)
         Filter: (tag = '2'::text)

select compress_chunk(x) from show_chunks('test') x;
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_1_1_chunk

vacuum analyze test;
explain (analyze, costs off, buffers off, timing off, summary off)
select * from test where tag = '1';
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk (actual rows=1.00 loops=1)
   Vectorized Filter: (tag = '1'::text)
   Rows Removed by Filter: 1
   ->  Seq Scan on compress_hyper_2_3_chunk (actual rows=2.00 loops=1)

explain (analyze, costs off, buffers off, timing off, summary off)
select * from test where tag = '2';
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk (actual rows=1.00 loops=1)
   Vectorized Filter: (tag = '2'::text)
   Rows Removed by Filter: 1
   ->  Seq Scan on compress_hyper_2_3_chunk (actual rows=2.00 loops=1)

set timescaledb.read_legacy_bloom1_v1 to on;
explain (analyze, costs off, buffers off, timing off, summary off)
select * from test where tag = '1';
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk (actual rows=1.00 loops=1)
   Vectorized Filter: (tag = '1'::text)
   Rows Removed by Filter: 1
   ->  Seq Scan on compress_hyper_2_3_chunk (actual rows=2.00 loops=1)
         Filter: _timescaledb_functions.bloom1_contains(regress-test-bloom_tag, '1'::text)

explain (analyze, costs off, buffers off, timing off, summary off)
select * from test where tag = '2';
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_1_1_chunk (actual rows=1.00 loops=1)
   Vectorized Filter: (tag = '2'::text)
   ->  Seq Scan on compress_hyper_2_3_chunk (actual rows=1.00 loops=1)
         Filter: _timescaledb_functions.bloom1_contains(regress-test-bloom_tag, '2'::text)
         Rows Removed by Filter: 1

reset timescaledb.read_legacy_bloom1_v1;
