-- 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.
-- github issue 5585
create table test (
    time timestamptz not null,
    a varchar(255) not null,
    b int,
    c int
);
SELECT create_hypertable('test', 'time');
WARNING:  column type "character varying" used for "a" does not follow best practices
 create_hypertable 
-------------------
 (1,public,test,t)

insert into test values
('2020-01-01 00:00'::timestamptz, 'lat', 1, 2),
('2020-01-01 00:01'::timestamptz, 'lat', 1, 2),
('2020-01-01 00:01'::timestamptz, 'lat', 2, 2),
('2020-01-01 00:03'::timestamptz, 'lat', 1, 2),
('2020-01-01 00:01'::timestamptz, 'lon', 1, 2);
create table test_copy as select * from test;
-- compress the chunk
alter table test set (timescaledb.compress, timescaledb.compress_segmentby='a, b');
select compress_chunk(show_chunks('test'));
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_1_1_chunk

-- force an index scan
set enable_seqscan = 'off';
-- make some tweaks to avoid flakiness
analyze test;
analyze test_copy;
set enable_hashagg = off;
set enable_sort to off;
set jit = off;
set max_parallel_workers_per_gather = 0;
explain (buffers off, costs off) with query_params as (
	select distinct a, b
	from test_copy
	where test_copy.a IN ('lat', 'lon')
	   and test_copy.b IN (1)
)
select
    test.time,
    test.a = q.a as "this should never be false",
    test.a,
    test.b,
    test.c,
    q.*
from
test inner join query_params q
    on q.a = test.a and q.b = test.b
where test.time between '2020-01-01 00:00' and '2020-01-01 00:02'
order by test.time;
--- QUERY PLAN ---
 Nested Loop
   Join Filter: (((_hyper_1_1_chunk.a)::text = (test_copy.a)::text) AND (_hyper_1_1_chunk.b = test_copy.b))
   ->  Custom Scan (ColumnarScan) on _hyper_1_1_chunk
         Filter: (("time" >= 'Wed Jan 01 00:00:00 2020 PST'::timestamp with time zone) AND ("time" <= 'Wed Jan 01 00:02:00 2020 PST'::timestamp with time zone))
         ->  Sort
               Sort Key: compress_hyper_2_2_chunk._ts_meta_min_1
               ->  Index Scan using compress_hyper_2_2_chunk_a_b__ts_meta_min_1__ts_meta_max_1_idx on compress_hyper_2_2_chunk
                     Index Cond: ((_ts_meta_min_1 <= 'Wed Jan 01 00:02:00 2020 PST'::timestamp with time zone) AND (_ts_meta_max_1 >= 'Wed Jan 01 00:00:00 2020 PST'::timestamp with time zone))
   ->  Materialize
         ->  Unique
               ->  Sort
                     Sort Key: test_copy.a
                     ->  Seq Scan on test_copy
                           Filter: (((a)::text = ANY ('{lat,lon}'::text[])) AND (b = 1))

with query_params as (
	select distinct a, b
	from test_copy
	where test_copy.a IN ('lat', 'lon')
	   and test_copy.b IN (1)
)
select
    test.time,
    test.a = q.a as "this should never be false",
    test.a,
    test.b,
    test.c,
    q.*
from
test inner join query_params q
    on q.a = test.a and q.b = test.b
where test.time between '2020-01-01 00:00' and '2020-01-01 00:02'
order by test.time;
             time             | this should never be false |  a  | b | c |  a  | b 
------------------------------+----------------------------+-----+---+---+-----+---
 Wed Jan 01 00:00:00 2020 PST | t                          | lat | 1 | 2 | lat | 1
 Wed Jan 01 00:01:00 2020 PST | t                          | lat | 1 | 2 | lat | 1
 Wed Jan 01 00:01:00 2020 PST | t                          | lon | 1 | 2 | lon | 1

-- Also test outer join for better coverage of nullability handling.
-- This test case generates a nullable equivalence member for test.b.
explain (buffers off, costs off)
with query_params as (
    select distinct a, b + 1 as b
    from test_copy
    where test_copy.a IN ('lat', 'lon')
)
select test.a, test.b
from test
full join query_params q
    on q.a = test.a
full join query_params q2
    on q2.b = test.b
;
--- QUERY PLAN ---
 Hash Full Join
   Hash Cond: (_hyper_1_1_chunk.b = q2.b)
   CTE query_params
     ->  Unique
           ->  Sort
                 Sort Key: test_copy.a, ((test_copy.b + 1))
                 ->  Seq Scan on test_copy
                       Filter: ((a)::text = ANY ('{lat,lon}'::text[]))
   ->  Hash Full Join
         Hash Cond: ((_hyper_1_1_chunk.a)::text = (q.a)::text)
         ->  Custom Scan (ColumnarScan) on _hyper_1_1_chunk
               ->  Index Scan using compress_hyper_2_2_chunk_a_b__ts_meta_min_1__ts_meta_max_1_idx on compress_hyper_2_2_chunk
         ->  Hash
               ->  CTE Scan on query_params q
   ->  Hash
         ->  CTE Scan on query_params q2

reset enable_seqscan;
reset jit;
drop table test;
drop table test_copy;
