-- 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 max_parallel_workers_per_gather = 0;
-- qual pushdown tests for decompresschunk ---
-- Test qual pushdown with ints
CREATE TABLE meta (device_id INT PRIMARY KEY);
CREATE TABLE hyper(
    time INT NOT NULL,
    device_id INT REFERENCES meta(device_id) ON DELETE CASCADE ON UPDATE CASCADE,
    val INT);
SELECT * FROM create_hypertable('hyper', 'time', chunk_time_interval => 10);
 hypertable_id | schema_name | table_name | created 
---------------+-------------+------------+---------
             1 | public      | hyper      | t

ALTER TABLE hyper SET (
    timescaledb.compress,
    timescaledb.compress_orderby = 'time',
    timescaledb.compress_segmentby = 'device_id');
INSERT INTO meta VALUES (1), (2), (3), (4), (5);
INSERT INTO hyper VALUES (1, 1, 1), (2, 2, 1), (3, 3, 1), (10, 3, 2), (11, 4, 2), (11, 5, 2);
SELECT ch1.table_name AS "CHUNK_NAME", ch1.schema_name|| '.' || ch1.table_name AS "CHUNK_FULL_NAME"
FROM _timescaledb_catalog.chunk ch1, _timescaledb_catalog.hypertable ht
WHERE ch1.hypertable_id = ht.id AND ht.table_name LIKE 'hyper'
ORDER BY ch1.id LIMIT 1 \gset
SELECT compress_chunk(:'CHUNK_FULL_NAME');
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_1_1_chunk

VACUUM FULL ANALYZE hyper;
-- test for qual pushdown
explain (buffers off, costs off, verbose)
SELECT
FROM hyper
WHERE time > 2::bigint and time < 4;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_1_chunk
   Vectorized Filter: ((_hyper_1_1_chunk."time" > '2'::bigint) AND (_hyper_1_1_chunk."time" < 4))
   ->  Seq Scan on _timescaledb_internal.compress_hyper_2_3_chunk
         Output: compress_hyper_2_3_chunk._ts_meta_count, compress_hyper_2_3_chunk.device_id, compress_hyper_2_3_chunk._ts_meta_min_1, compress_hyper_2_3_chunk._ts_meta_max_1, compress_hyper_2_3_chunk."time", compress_hyper_2_3_chunk.val
         Filter: ((compress_hyper_2_3_chunk._ts_meta_max_1 > '2'::bigint) AND (compress_hyper_2_3_chunk._ts_meta_min_1 < 4))

explain (buffers off, costs off, verbose)
SELECT
FROM hyper
WHERE time = 3::bigint;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_1_1_chunk
   Vectorized Filter: (_hyper_1_1_chunk."time" = '3'::bigint)
   ->  Seq Scan on _timescaledb_internal.compress_hyper_2_3_chunk
         Output: compress_hyper_2_3_chunk._ts_meta_count, compress_hyper_2_3_chunk.device_id, compress_hyper_2_3_chunk._ts_meta_min_1, compress_hyper_2_3_chunk._ts_meta_max_1, compress_hyper_2_3_chunk."time", compress_hyper_2_3_chunk.val
         Filter: ((compress_hyper_2_3_chunk._ts_meta_min_1 <= '3'::bigint) AND (compress_hyper_2_3_chunk._ts_meta_max_1 >= '3'::bigint))

SELECT *
FROM hyper
WHERE time > 2::bigint and time < 4;
 time | device_id | val 
------+-----------+-----
    3 |         3 |   1

SELECT *
FROM hyper
WHERE time = 3::bigint;
 time | device_id | val 
------+-----------+-----
    3 |         3 |   1

SELECT * FROM hyper where time = val + 1;
 time | device_id | val 
------+-----------+-----
    2 |         2 |   1

-- Test some volatile and stable functions
SET timescaledb.enable_chunk_append TO OFF;
SET timescaledb.enable_constraint_aware_append TO OFF;
EXPLAIN (BUFFERS OFF, COSTS OFF)
SELECT * FROM hyper WHERE time = device_id;
--- QUERY PLAN ---
 Append
   ->  Custom Scan (ColumnarScan) on _hyper_1_1_chunk
         Filter: ("time" = device_id)
         ->  Seq Scan on compress_hyper_2_3_chunk
               Filter: ((_ts_meta_min_1 <= device_id) AND (_ts_meta_max_1 >= device_id))
   ->  Seq Scan on _hyper_1_2_chunk
         Filter: ("time" = device_id)

EXPLAIN (BUFFERS OFF, COSTS OFF)
SELECT * FROM hyper WHERE time = random()::int;
--- QUERY PLAN ---
 Append
   ->  Custom Scan (ColumnarScan) on _hyper_1_1_chunk
         Filter: ("time" = (random())::integer)
         ->  Seq Scan on compress_hyper_2_3_chunk
   ->  Seq Scan on _hyper_1_2_chunk
         Filter: ("time" = (random())::integer)

EXPLAIN (BUFFERS OFF, COSTS OFF)
SELECT * FROM hyper WHERE time
    = CASE WHEN now() > '1970-01-01' THEN random()::int ELSE device_id END;
--- QUERY PLAN ---
 Append
   ->  Custom Scan (ColumnarScan) on _hyper_1_1_chunk
         Filter: ("time" = CASE WHEN (now() > 'Thu Jan 01 00:00:00 1970 PST'::timestamp with time zone) THEN (random())::integer ELSE device_id END)
         ->  Seq Scan on compress_hyper_2_3_chunk
   ->  Seq Scan on _hyper_1_2_chunk
         Filter: ("time" = CASE WHEN (now() > 'Thu Jan 01 00:00:00 1970 PST'::timestamp with time zone) THEN (random())::integer ELSE device_id END)

EXPLAIN (BUFFERS OFF, COSTS OFF)
SELECT * FROM hyper WHERE time
    = CASE WHEN now() > '1970-01-01' THEN 1 ELSE device_id END;
--- QUERY PLAN ---
 Append
   ->  Custom Scan (ColumnarScan) on _hyper_1_1_chunk
         Filter: ("time" = CASE WHEN (now() > 'Thu Jan 01 00:00:00 1970 PST'::timestamp with time zone) THEN 1 ELSE device_id END)
         ->  Seq Scan on compress_hyper_2_3_chunk
               Filter: ((_ts_meta_min_1 <= CASE WHEN (now() > 'Thu Jan 01 00:00:00 1970 PST'::timestamp with time zone) THEN 1 ELSE device_id END) AND (_ts_meta_max_1 >= CASE WHEN (now() > 'Thu Jan 01 00:00:00 1970 PST'::timestamp with time zone) THEN 1 ELSE device_id END))
   ->  Seq Scan on _hyper_1_2_chunk
         Filter: ("time" = CASE WHEN (now() > 'Thu Jan 01 00:00:00 1970 PST'::timestamp with time zone) THEN 1 ELSE device_id END)

RESET timescaledb.enable_chunk_append;
RESET timescaledb.enable_constraint_aware_append;
--- github issue 1855
--- TESTs for meta column pushdown filters on exprs with casts.
CREATE TABLE metaseg_tab (
fmid smallint,
factorid smallint,
start_dt timestamp without time zone,
end_dt timestamp without time zone,
interval_number smallint,
logret double precision,
knowledge_date date NOT NULL
);
SELECT create_hypertable('metaseg_tab', 'end_dt', chunk_time_interval=>interval '1 month', create_default_indexes=>false);
WARNING:  column type "timestamp without time zone" used for "start_dt" does not follow best practices
WARNING:  column type "timestamp without time zone" used for "end_dt" does not follow best practices
    create_hypertable     
--------------------------
 (3,public,metaseg_tab,t)

SELECT add_dimension('metaseg_tab', 'fmid', chunk_time_interval => 1);
         add_dimension         
-------------------------------
 (3,public,metaseg_tab,fmid,t)

ALTER TABLE metaseg_tab SET (timescaledb.compress, timescaledb.compress_orderby= 'end_dt');
INSERT INTO metaseg_tab values (56,0,'2012-12-10 09:45:00','2012-12-10 09:50:00',1,0.1,'2012-12-10');
SELECT compress_chunk(i) from show_chunks('metaseg_tab') i;
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_3_4_chunk

VACUUM FULL ANALYZE metaseg_tab;
select factorid, end_dt, logret
from metaseg_tab
where fmid = 56
and end_dt between '2012-12-10'::date and '2012-12-11'::date
order by factorid, end_dt;
 factorid |          end_dt          | logret 
----------+--------------------------+--------
        0 | Mon Dec 10 09:50:00 2012 |    0.1

explain (buffers off, costs off, verbose)
select factorid, end_dt, logret
from metaseg_tab
where fmid = 56
and end_dt between '2012-12-10'::date and '2012-12-11'::date
order by factorid, end_dt;
--- QUERY PLAN ---
 Sort
   Output: _hyper_3_4_chunk.factorid, _hyper_3_4_chunk.end_dt, _hyper_3_4_chunk.logret
   Sort Key: _hyper_3_4_chunk.factorid, _hyper_3_4_chunk.end_dt
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_3_4_chunk
         Output: _hyper_3_4_chunk.factorid, _hyper_3_4_chunk.end_dt, _hyper_3_4_chunk.logret
         Filter: ((_hyper_3_4_chunk.end_dt >= '12-10-2012'::date) AND (_hyper_3_4_chunk.end_dt <= '12-11-2012'::date))
         Vectorized Filter: (_hyper_3_4_chunk.fmid = 56)
         ->  Seq Scan on _timescaledb_internal.compress_hyper_4_5_chunk
               Output: compress_hyper_4_5_chunk._ts_meta_count, compress_hyper_4_5_chunk.fmid, compress_hyper_4_5_chunk.factorid, compress_hyper_4_5_chunk.start_dt, compress_hyper_4_5_chunk._ts_meta_min_1, compress_hyper_4_5_chunk._ts_meta_max_1, compress_hyper_4_5_chunk.end_dt, compress_hyper_4_5_chunk.interval_number, compress_hyper_4_5_chunk.logret, compress_hyper_4_5_chunk.knowledge_date
               Filter: ((compress_hyper_4_5_chunk._ts_meta_max_1 >= '12-10-2012'::date) AND (compress_hyper_4_5_chunk._ts_meta_min_1 <= '12-11-2012'::date))

--no pushdown here
select factorid, end_dt, logret
from metaseg_tab
where fmid = 56
and end_dt::date between '2012-12-10'::timestamp and '2012-12-11'::date
order by factorid, end_dt;
 factorid |          end_dt          | logret 
----------+--------------------------+--------
        0 | Mon Dec 10 09:50:00 2012 |    0.1

explain (buffers off, costs off, verbose)
select factorid, end_dt, logret
from metaseg_tab
where fmid = 56
and end_dt::date between '2012-12-10'::timestamp and '2012-12-11'::date
order by factorid, end_dt;
--- QUERY PLAN ---
 Sort
   Output: _hyper_3_4_chunk.factorid, _hyper_3_4_chunk.end_dt, _hyper_3_4_chunk.logret
   Sort Key: _hyper_3_4_chunk.factorid, _hyper_3_4_chunk.end_dt
   ->  Custom Scan (ColumnarScan) on _timescaledb_internal._hyper_3_4_chunk
         Output: _hyper_3_4_chunk.factorid, _hyper_3_4_chunk.end_dt, _hyper_3_4_chunk.logret
         Filter: (((_hyper_3_4_chunk.end_dt)::date >= 'Mon Dec 10 00:00:00 2012'::timestamp without time zone) AND ((_hyper_3_4_chunk.end_dt)::date <= '12-11-2012'::date))
         Vectorized Filter: (_hyper_3_4_chunk.fmid = 56)
         ->  Seq Scan on _timescaledb_internal.compress_hyper_4_5_chunk
               Output: compress_hyper_4_5_chunk._ts_meta_count, compress_hyper_4_5_chunk.fmid, compress_hyper_4_5_chunk.factorid, compress_hyper_4_5_chunk.start_dt, compress_hyper_4_5_chunk._ts_meta_min_1, compress_hyper_4_5_chunk._ts_meta_max_1, compress_hyper_4_5_chunk.end_dt, compress_hyper_4_5_chunk.interval_number, compress_hyper_4_5_chunk.logret, compress_hyper_4_5_chunk.knowledge_date

--should fail
\set ON_ERROR_STOP 0
select factorid, end_dt, logret
from metaseg_tab
where fmid = 56
and end_dt::date = 10;
ERROR:  operator does not exist: date = integer at character 83
select factorid, end_dt, logret
from metaseg_tab
where fmid = 56
and end_dt::date = 'dec 2010'::date;
ERROR:  invalid input syntax for type date: "dec 2010" at character 85
-- test casts between different char types are pushed down
CREATE TABLE pushdown_relabel(time timestamptz NOT NULL, dev_vc varchar(10), dev_c char(10));
SELECT table_name FROM create_hypertable('pushdown_relabel', 'time');
WARNING:  column type "character varying" used for "dev_vc" does not follow best practices
    table_name    
------------------
 pushdown_relabel

ALTER TABLE pushdown_relabel SET (timescaledb.compress, timescaledb.compress_segmentby='dev_vc,dev_c');
INSERT INTO pushdown_relabel SELECT '2000-01-01','varchar','char';
SELECT compress_chunk(i) from show_chunks('pushdown_relabel') i;
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_5_6_chunk

VACUUM FULL ANALYZE pushdown_relabel;
EXPLAIN (buffers off, costs off) SELECT * FROM pushdown_relabel WHERE dev_vc = 'varchar';
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_5_6_chunk
   ->  Seq Scan on compress_hyper_6_7_chunk
         Filter: ((dev_vc)::text = 'varchar'::text)

EXPLAIN (buffers off, costs off) SELECT * FROM pushdown_relabel WHERE dev_c = 'char';
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_5_6_chunk
   ->  Seq Scan on compress_hyper_6_7_chunk
         Filter: (dev_c = 'char'::bpchar)

EXPLAIN (buffers off, costs off) SELECT * FROM pushdown_relabel WHERE dev_vc = 'varchar' AND dev_c = 'char';
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_5_6_chunk
   ->  Seq Scan on compress_hyper_6_7_chunk
         Filter: (((dev_vc)::text = 'varchar'::text) AND (dev_c = 'char'::bpchar))

EXPLAIN (buffers off, costs off) SELECT * FROM pushdown_relabel WHERE dev_vc = 'varchar'::char(10) AND dev_c = 'char'::varchar;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_5_6_chunk
   ->  Seq Scan on compress_hyper_6_7_chunk
         Filter: (((dev_vc)::bpchar = 'varchar   '::character(10)) AND (dev_c = 'char'::bpchar))

-- test again with index scans
SET enable_seqscan TO false;
EXPLAIN (buffers off, costs off) SELECT * FROM pushdown_relabel WHERE dev_vc = 'varchar';
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_5_6_chunk
   ->  Index Scan using compress_hyper_6_7_chunk_dev_vc_dev_c__ts_meta_min_1__ts_me_idx on compress_hyper_6_7_chunk
         Index Cond: ((dev_vc)::text = 'varchar'::text)

EXPLAIN (buffers off, costs off) SELECT * FROM pushdown_relabel WHERE dev_c = 'char';
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_5_6_chunk
   ->  Index Scan using compress_hyper_6_7_chunk_dev_vc_dev_c__ts_meta_min_1__ts_me_idx on compress_hyper_6_7_chunk
         Index Cond: (dev_c = 'char'::bpchar)

EXPLAIN (buffers off, costs off) SELECT * FROM pushdown_relabel WHERE dev_vc = 'varchar' AND dev_c = 'char';
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_5_6_chunk
   ->  Index Scan using compress_hyper_6_7_chunk_dev_vc_dev_c__ts_meta_min_1__ts_me_idx on compress_hyper_6_7_chunk
         Index Cond: (((dev_vc)::text = 'varchar'::text) AND (dev_c = 'char'::bpchar))

EXPLAIN (buffers off, costs off) SELECT * FROM pushdown_relabel WHERE dev_vc = 'varchar'::char(10) AND dev_c = 'char'::varchar;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_5_6_chunk
   ->  Index Scan using compress_hyper_6_7_chunk_dev_vc_dev_c__ts_meta_min_1__ts_me_idx on compress_hyper_6_7_chunk
         Index Cond: (dev_c = 'char'::bpchar)
         Filter: ((dev_vc)::bpchar = 'varchar   '::character(10))

RESET enable_seqscan;
-- github issue #5286
CREATE TABLE deleteme AS
    SELECT generate_series AS timestamp, 1 AS segment, 0 AS data
        FROM generate_series('2008-03-01 00:00'::timestamp with time zone,
                             '2008-03-04 12:00', '1 second');
SELECT create_hypertable('deleteme', 'timestamp', migrate_data => true);
NOTICE:  migrating data to chunks
   create_hypertable   
-----------------------
 (7,public,deleteme,t)

ALTER TABLE deleteme SET (
  timescaledb.compress,
  timescaledb.compress_segmentby = 'segment'
);
SELECT compress_chunk(i) FROM show_chunks('deleteme') i;
             compress_chunk             
----------------------------------------
 _timescaledb_internal._hyper_7_8_chunk

VACUUM FULL ANALYZE deleteme;
EXPLAIN (buffers off, costs off) SELECT sum(data) FROM deleteme WHERE segment::text like '%4%';
--- QUERY PLAN ---
 Aggregate
   ->  Custom Scan (ColumnarScan) on _hyper_7_8_chunk
         ->  Seq Scan on compress_hyper_8_9_chunk
               Filter: ((segment)::text ~~ '%4%'::text)

EXPLAIN (buffers off, costs off) SELECT sum(data) FROM deleteme WHERE '4' = segment::text;
--- QUERY PLAN ---
 Aggregate
   ->  Custom Scan (ColumnarScan) on _hyper_7_8_chunk
         ->  Seq Scan on compress_hyper_8_9_chunk
               Filter: ('4'::text = (segment)::text)

CREATE TABLE deleteme_with_bytea(time bigint NOT NULL, bdata bytea);
SELECT create_hypertable('deleteme_with_bytea', 'time', chunk_time_interval => 1000000);
        create_hypertable         
----------------------------------
 (9,public,deleteme_with_bytea,t)

INSERT INTO deleteme_with_bytea(time, bdata) VALUES (1001, E'\\x');
INSERT INTO deleteme_with_bytea(time, bdata) VALUES (1001, NULL);
ALTER TABLE deleteme_with_bytea SET (
  timescaledb.compress,
  timescaledb.compress_segmentby = 'bdata'
);
SELECT compress_chunk(i) FROM show_chunks('deleteme_with_bytea') i;
             compress_chunk              
-----------------------------------------
 _timescaledb_internal._hyper_9_10_chunk

VACUUM FULL ANALYZE deleteme_with_bytea;
EXPLAIN (buffers off, costs off) SELECT '1' FROM deleteme_with_bytea WHERE bdata = E'\\x';
--- QUERY PLAN ---
 Result
   ->  Custom Scan (ColumnarScan) on _hyper_9_10_chunk
         ->  Seq Scan on compress_hyper_10_11_chunk
               Filter: (bdata = '\x'::bytea)

EXPLAIN (buffers off, costs off) SELECT '1' FROM deleteme_with_bytea WHERE bdata::text = '123';
--- QUERY PLAN ---
 Result
   ->  Custom Scan (ColumnarScan) on _hyper_9_10_chunk
         ->  Seq Scan on compress_hyper_10_11_chunk
               Filter: ((bdata)::text = '123'::text)

DROP table deleteme;
DROP table deleteme_with_bytea;
-- test sqlvaluefunction pushdown
CREATE TABLE svf_pushdown(time timestamptz, c_date date, c_time time, c_timetz timetz, c_timestamp timestamptz, c_name text, c_bool bool);
SELECT table_name FROM create_hypertable('svf_pushdown', 'time');
  table_name  
--------------
 svf_pushdown

ALTER TABLE svf_pushdown SET (timescaledb.compress,timescaledb.compress_segmentby='c_date,c_time, c_timetz,c_timestamp,c_name,c_bool');
INSERT INTO svf_pushdown SELECT '2020-01-01';
SELECT compress_chunk(show_chunks('svf_pushdown'));
              compress_chunk              
------------------------------------------
 _timescaledb_internal._hyper_11_12_chunk

VACUUM FULL ANALYZE svf_pushdown;
-- constraints should be pushed down into scan below decompresschunk in all cases
EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_date = CURRENT_DATE;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         ->  Seq Scan on compress_hyper_12_13_chunk
               Filter: (c_date = CURRENT_DATE)

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_timetz = CURRENT_TIME;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         ->  Seq Scan on compress_hyper_12_13_chunk
               Filter: (c_timetz = CURRENT_TIME)

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_timetz = CURRENT_TIME(1);
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         ->  Seq Scan on compress_hyper_12_13_chunk
               Filter: (c_timetz = CURRENT_TIME(1))

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_timestamp = CURRENT_TIMESTAMP;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         ->  Seq Scan on compress_hyper_12_13_chunk
               Filter: (c_timestamp = CURRENT_TIMESTAMP)

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_timestamp = CURRENT_TIMESTAMP(1);
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         ->  Seq Scan on compress_hyper_12_13_chunk
               Filter: (c_timestamp = CURRENT_TIMESTAMP(1))

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_time = LOCALTIME;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         ->  Seq Scan on compress_hyper_12_13_chunk
               Filter: (c_time = LOCALTIME)

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_time = LOCALTIME(1);
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         ->  Seq Scan on compress_hyper_12_13_chunk
               Filter: (c_time = LOCALTIME(1))

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_timestamp = LOCALTIMESTAMP;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         ->  Seq Scan on compress_hyper_12_13_chunk
               Filter: (c_timestamp = LOCALTIMESTAMP)

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_timestamp = LOCALTIMESTAMP(1);
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         ->  Seq Scan on compress_hyper_12_13_chunk
               Filter: (c_timestamp = LOCALTIMESTAMP(1))

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_name = USER;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         ->  Seq Scan on compress_hyper_12_13_chunk
               Filter: (c_name = USER)

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_name = CURRENT_USER;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         ->  Seq Scan on compress_hyper_12_13_chunk
               Filter: (c_name = CURRENT_USER)

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_name = SESSION_USER;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         ->  Seq Scan on compress_hyper_12_13_chunk
               Filter: (c_name = SESSION_USER)

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_name = CURRENT_USER OR c_name = SESSION_USER;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         ->  Seq Scan on compress_hyper_12_13_chunk
               Filter: ((c_name = CURRENT_USER) OR (c_name = SESSION_USER))

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_name = CURRENT_CATALOG;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         ->  Seq Scan on compress_hyper_12_13_chunk
               Filter: (c_name = CURRENT_CATALOG)

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_name = CURRENT_SCHEMA;
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         ->  Seq Scan on compress_hyper_12_13_chunk
               Filter: (c_name = CURRENT_SCHEMA)

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_bool;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_11_12_chunk
   ->  Seq Scan on compress_hyper_12_13_chunk
         Filter: c_bool

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_bool = true;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_11_12_chunk
   ->  Seq Scan on compress_hyper_12_13_chunk
         Filter: c_bool

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_bool = false;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_11_12_chunk
   ->  Seq Scan on compress_hyper_12_13_chunk
         Filter: (NOT c_bool)

EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE NOT c_bool;
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_11_12_chunk
   ->  Seq Scan on compress_hyper_12_13_chunk
         Filter: (NOT c_bool)

-- current_query() is not a sqlvaluefunction and volatile so should not be pushed down
EXPLAIN (buffers off, costs off) SELECT * FROM svf_pushdown WHERE c_name = current_query();
--- QUERY PLAN ---
 Custom Scan (ChunkAppend) on svf_pushdown
   Chunks excluded during startup: 0
   ->  Custom Scan (ColumnarScan) on _hyper_11_12_chunk
         Filter: (c_name = current_query())
         ->  Seq Scan on compress_hyper_12_13_chunk

-- test or constraints in lateral query #6912
SELECT FROM svf_pushdown m1,
LATERAL(
  SELECT FROM svf_pushdown m2
  WHERE
    m1.time = m2.time AND
    EXISTS (SELECT random()) OR
    EXISTS (SELECT FROM meta) LIMIT 1
) l;
--

DROP TABLE svf_pushdown;
-- Test bool segmentby column to cover bool-returning operator deeper in the
-- expression tree.
create table booltab(ts int, segmentby bool, flag bool, value int);
select create_hypertable('booltab', 'ts');
   create_hypertable   
-----------------------
 (13,public,booltab,t)

alter table booltab set (timescaledb.compress, timescaledb.compress_segmentby = 'segmentby',
    timescaledb.compress_orderby = 'ts, flag');
insert into booltab values
    (  1,  true,  true,   1),
    ( 99, false, false,  99),
    (100, false, false, 100),
    (101, false, false, 101)
;
select count(compress_chunk(x)) from show_chunks('booltab') x;
 count 
-------
     1

vacuum analyze booltab;
explain (analyze, buffers off, costs off, timing off, summary off)
select * from booltab where segmentby = (value = 1);
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_13_14_chunk (actual rows=4.00 loops=1)
   Filter: (segmentby = (value = 1))
   ->  Seq Scan on compress_hyper_14_15_chunk (actual rows=2.00 loops=1)

explain (analyze, buffers off, costs off, timing off, summary off)
select * from booltab where segmentby = (ts = 1);
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_13_14_chunk (actual rows=4.00 loops=1)
   Filter: (segmentby = (ts = 1))
   ->  Seq Scan on compress_hyper_14_15_chunk (actual rows=2.00 loops=1)

explain (analyze, buffers off, costs off, timing off, summary off)
select * from booltab where segmentby != (ts = 1);
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_13_14_chunk (actual rows=0.00 loops=1)
   Filter: (segmentby <> (ts = 1))
   Rows Removed by Filter: 4
   ->  Seq Scan on compress_hyper_14_15_chunk (actual rows=2.00 loops=1)

explain (analyze, buffers off, costs off, timing off, summary off)
select * from booltab where (value = 1) = (ts = 1);
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_13_14_chunk (actual rows=4.00 loops=1)
   Filter: ((value = 1) = (ts = 1))
   ->  Seq Scan on compress_hyper_14_15_chunk (actual rows=2.00 loops=1)

explain (analyze, buffers off, costs off, timing off, summary off)
select * from booltab where flag = (ts = 1);
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_13_14_chunk (actual rows=4.00 loops=1)
   Filter: (flag = (ts = 1))
   ->  Seq Scan on compress_hyper_14_15_chunk (actual rows=2.00 loops=1)

explain (analyze, buffers off, costs off, timing off, summary off)
select * from booltab where flag = (ts = 99);
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_13_14_chunk (actual rows=2.00 loops=1)
   Filter: (flag = (ts = 99))
   Rows Removed by Filter: 2
   ->  Seq Scan on compress_hyper_14_15_chunk (actual rows=2.00 loops=1)

explain (analyze, buffers off, costs off, timing off, summary off)
select * from booltab where flag = (ts = 99);
--- QUERY PLAN ---
 Custom Scan (ColumnarScan) on _hyper_13_14_chunk (actual rows=2.00 loops=1)
   Filter: (flag = (ts = 99))
   Rows Removed by Filter: 2
   ->  Seq Scan on compress_hyper_14_15_chunk (actual rows=2.00 loops=1)

drop table booltab;
