-- 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.
SET enable_memoize TO false;
SET enable_indexscan TO false;
SET max_parallel_workers_per_gather = 0;
-- test join on compressed time column
-- #3079, #4465
CREATE TABLE compressed_join_temp AS SELECT * FROM metrics ORDER BY time DESC LIMIT 10;
ANALYZE compressed_join_temp;
EXPLAIN (analyze,buffers off, costs off,timing off,summary off) SELECT *
FROM compressed_join_temp t
INNER JOIN metrics_compressed m ON t.time = m.time AND t.device_id = m.device_id
LIMIT 1;
--- QUERY PLAN ---
 Limit (actual rows=1.00 loops=1)
   ->  Hash Join (actual rows=1.00 loops=1)
         Hash Cond: ((m."time" = t."time") AND (m.device_id = t.device_id))
         ->  Append (actual rows=43181.00 loops=1)
               ->  Custom Scan (ColumnarScan) on _hyper_X_X_chunk m_1 (actual rows=16392.00 loops=1)
                     ->  Seq Scan on compress_hyper_X_X_chunk (actual rows=18.00 loops=1)
               ->  Seq Scan on _hyper_X_X_chunk m_1 (actual rows=1598.00 loops=1)
               ->  Custom Scan (ColumnarScan) on _hyper_X_X_chunk m_2 (actual rows=25190.00 loops=1)
                     ->  Seq Scan on compress_hyper_X_X_chunk (actual rows=30.00 loops=1)
               ->  Custom Scan (ColumnarScan) on _hyper_X_X_chunk m_3 (actual rows=1.00 loops=1)
                     ->  Seq Scan on compress_hyper_X_X_chunk (actual rows=1.00 loops=1)
         ->  Hash (actual rows=10.00 loops=1)
               ->  Seq Scan on compressed_join_temp t (actual rows=10.00 loops=1)

DROP TABLE compressed_join_temp;
-- test join with partially compressed chunks
CREATE TABLE partial_join(time timestamptz,device text);
SELECT table_name FROM create_hypertable('partial_join','time');
  table_name  
--------------
 partial_join

ALTER TABLE partial_join set(timescaledb.compress,timescaledb.compress_segmentby='device');
INSERT INTO partial_join SELECT '2000-01-01','d1';
SELECT count(*) FROM (SELECT compress_chunk(show_chunks('partial_join'),true)) compress;
 count 
-------
     1

-- make chunk partially compressed
INSERT INTO partial_join SELECT '2000-01-01','d1';
SELECT * FROM partial_join m1 INNER JOIN partial_join m2 ON m1.device = m2.device;
             time             | device |             time             | device 
------------------------------+--------+------------------------------+--------
 Sat Jan 01 00:00:00 2000 PST | d1     | Sat Jan 01 00:00:00 2000 PST | d1
 Sat Jan 01 00:00:00 2000 PST | d1     | Sat Jan 01 00:00:00 2000 PST | d1
 Sat Jan 01 00:00:00 2000 PST | d1     | Sat Jan 01 00:00:00 2000 PST | d1
 Sat Jan 01 00:00:00 2000 PST | d1     | Sat Jan 01 00:00:00 2000 PST | d1

DROP TABLE partial_join;
-- This resulted in a recursion in the join planner code on PG16
SELECT * FROM metrics_tstz as m
INNER JOIN metrics_space as ms on (true)
INNER JOIN metrics_space_compressed as msc on (true)
WHERE CASE WHEN m.device_id is not NULL and ms.v2 is not NULL THEN NULL::int2 end = msc.device_id;
 time | device_id | v1 | v2 | time | device_id | v0 | v1 | v2 | v3 | time | device_id | v0 | v1 | v2 | v3 
------+-----------+----+----+------+-----------+----+----+----+----+------+-----------+----+----+----+----

