SET citus.next_shard_id TO 1640000;
CREATE SCHEMA ch_bench_having;
SET search_path = ch_bench_having, public;
CREATE TABLE stock (
  s_w_id int NOT NULL,
  s_i_id int NOT NULL,
  s_order_cnt int NOT NULL
);
SELECT create_distributed_table('stock','s_w_id');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

explain (costs false, summary false, timing false)
select     s_i_id, sum(s_order_cnt) as ordercount
from     stock
where   s_order_cnt > (select sum(s_order_cnt) * .005 as where_query from stock)
group by s_i_id
having   sum(s_order_cnt) > (select max(s_order_cnt) - 3 as having_query from stock)
order by s_i_id;
                                          QUERY PLAN
---------------------------------------------------------------------
 Sort
   Sort Key: remote_scan.s_i_id
   InitPlan 1 (returns $0)
     ->  Function Scan on read_intermediate_result intermediate_result
   ->  HashAggregate
         Group Key: remote_scan.s_i_id
         Filter: ((pg_catalog.sum(remote_scan.worker_column_3))::bigint > $0)
         ->  Custom Scan (Citus Adaptive)
               ->  Distributed Subplan XXX_1
                     ->  Aggregate
                           ->  Custom Scan (Citus Adaptive)
                                 Task Count: 4
                                 Tasks Shown: One of 4
                                 ->  Task
                                       Node: host=localhost port=xxxxx dbname=regression
                                       ->  Aggregate
                                             ->  Seq Scan on stock_1640000 stock
               ->  Distributed Subplan XXX_2
                     ->  Aggregate
                           ->  Custom Scan (Citus Adaptive)
                                 Task Count: 4
                                 Tasks Shown: One of 4
                                 ->  Task
                                       Node: host=localhost port=xxxxx dbname=regression
                                       ->  Aggregate
                                             ->  Seq Scan on stock_1640000 stock
               Task Count: 4
               Tasks Shown: One of 4
               ->  Task
                     Node: host=localhost port=xxxxx dbname=regression
                     ->  HashAggregate
                           Group Key: stock.s_i_id
                           InitPlan 1 (returns $0)
                             ->  Function Scan on read_intermediate_result intermediate_result
                           ->  Seq Scan on stock_1640000 stock
                                 Filter: ((s_order_cnt)::numeric > $0)
(36 rows)

explain (costs false, summary false, timing false)
select     s_i_id, sum(s_order_cnt) as ordercount
from     stock
group by s_i_id
having   sum(s_order_cnt) > (select max(s_order_cnt) - 3 as having_query from stock)
order by s_i_id;
                                       QUERY PLAN
---------------------------------------------------------------------
 Sort
   Sort Key: remote_scan.s_i_id
   InitPlan 1 (returns $0)
     ->  Function Scan on read_intermediate_result intermediate_result
   ->  HashAggregate
         Group Key: remote_scan.s_i_id
         Filter: ((pg_catalog.sum(remote_scan.worker_column_3))::bigint > $0)
         ->  Custom Scan (Citus Adaptive)
               ->  Distributed Subplan XXX_1
                     ->  Aggregate
                           ->  Custom Scan (Citus Adaptive)
                                 Task Count: 4
                                 Tasks Shown: One of 4
                                 ->  Task
                                       Node: host=localhost port=xxxxx dbname=regression
                                       ->  Aggregate
                                             ->  Seq Scan on stock_1640000 stock
               Task Count: 4
               Tasks Shown: One of 4
               ->  Task
                     Node: host=localhost port=xxxxx dbname=regression
                     ->  HashAggregate
                           Group Key: stock.s_i_id
                           ->  Seq Scan on stock_1640000 stock
(24 rows)

explain (costs false, summary false, timing false)
select     s_i_id, sum(s_order_cnt) as ordercount
from     stock
group by s_i_id
having   sum(s_order_cnt) > (select max(s_order_cnt) - 3 as having_query from stock);
                                    QUERY PLAN
---------------------------------------------------------------------
 HashAggregate
   Group Key: remote_scan.s_i_id
   Filter: ((pg_catalog.sum(remote_scan.worker_column_3))::bigint > $0)
   InitPlan 1 (returns $0)
     ->  Function Scan on read_intermediate_result intermediate_result
   ->  Custom Scan (Citus Adaptive)
         ->  Distributed Subplan XXX_1
               ->  Aggregate
                     ->  Custom Scan (Citus Adaptive)
                           Task Count: 4
                           Tasks Shown: One of 4
                           ->  Task
                                 Node: host=localhost port=xxxxx dbname=regression
                                 ->  Aggregate
                                       ->  Seq Scan on stock_1640000 stock
         Task Count: 4
         Tasks Shown: One of 4
         ->  Task
               Node: host=localhost port=xxxxx dbname=regression
               ->  HashAggregate
                     Group Key: stock.s_i_id
                     ->  Seq Scan on stock_1640000 stock
(22 rows)

explain (costs false)
select     s_i_id, sum(s_order_cnt) as ordercount
from     stock s
group by s_i_id
having   (select true)
order by s_i_id;
                                 QUERY PLAN
---------------------------------------------------------------------
 Sort
   Sort Key: remote_scan.s_i_id
   InitPlan 1 (returns $0)
     ->  Result
   ->  HashAggregate
         Group Key: remote_scan.s_i_id
         ->  Result
               One-Time Filter: $0
               ->  Custom Scan (Citus Adaptive)
                     Filter: $0
                     Task Count: 4
                     Tasks Shown: One of 4
                     ->  Task
                           Node: host=localhost port=xxxxx dbname=regression
                           ->  HashAggregate
                                 Group Key: s.s_i_id
                                 ->  Seq Scan on stock_1640000 s
(17 rows)

explain (costs false)
select     s_i_id, sum(s_order_cnt) as ordercount
from     stock s
group by s_i_id
having   (select true);
                              QUERY PLAN
---------------------------------------------------------------------
 HashAggregate
   Group Key: remote_scan.s_i_id
   InitPlan 1 (returns $0)
     ->  Result
   ->  Result
         One-Time Filter: $0
         ->  Custom Scan (Citus Adaptive)
               Filter: $0
               Task Count: 4
               Tasks Shown: One of 4
               ->  Task
                     Node: host=localhost port=xxxxx dbname=regression
                     ->  HashAggregate
                           Group Key: s.s_i_id
                           ->  Seq Scan on stock_1640000 s
(15 rows)

select     s_i_id, sum(s_order_cnt) as ordercount
from     stock
where   s_order_cnt > (select sum(s_order_cnt) * .005 as where_query from stock)
group by s_i_id
having   sum(s_order_cnt) > (select max(s_order_cnt) - 3 as having_query from stock)
order by s_i_id;
 s_i_id | ordercount
---------------------------------------------------------------------
(0 rows)

INSERT INTO stock SELECT c, c, c FROM generate_series(1, 5) as c;
select     s_i_id, sum(s_order_cnt) as ordercount
from     stock
where   s_order_cnt > (select sum(s_order_cnt) * .005 as where_query from stock)
group by s_i_id
having   sum(s_order_cnt) > (select max(s_order_cnt) - 3 as having_query from stock)
order by s_i_id;
 s_i_id | ordercount
---------------------------------------------------------------------
      3 |          3
      4 |          4
      5 |          5
(3 rows)

select     s_i_id, sum(s_order_cnt) as ordercount
from     stock
group by s_i_id
having   sum(s_order_cnt) > (select max(s_order_cnt) - 3 as having_query from stock)
order by s_i_id;
 s_i_id | ordercount
---------------------------------------------------------------------
      3 |          3
      4 |          4
      5 |          5
(3 rows)

select     s_i_id, sum(s_order_cnt) as ordercount
from     stock s
where   s_order_cnt > (select sum(s_order_cnt) * .005 as where_query from stock)
group by s_i_id
having   (select true)
order by s_i_id;
 s_i_id | ordercount
---------------------------------------------------------------------
      1 |          1
      2 |          2
      3 |          3
      4 |          4
      5 |          5
(5 rows)

select     s_i_id, sum(s_order_cnt) as ordercount
from     stock s
where   s_order_cnt > (select sum(s_order_cnt) * .005 as where_query from stock)
group by s_i_id
having   (select false)
order by s_i_id;
 s_i_id | ordercount
---------------------------------------------------------------------
(0 rows)

select     s_i_id, sum(s_order_cnt) as ordercount
from     stock s
group by s_i_id
having   (select true)
order by s_i_id;
 s_i_id | ordercount
---------------------------------------------------------------------
      1 |          1
      2 |          2
      3 |          3
      4 |          4
      5 |          5
(5 rows)

select     s_i_id, sum(s_order_cnt) as ordercount
from     stock s
group by s_i_id
having   (select false)
order by s_i_id;
 s_i_id | ordercount
---------------------------------------------------------------------
(0 rows)

select     s_i_id, sum(s_order_cnt) as ordercount
from     stock s
group by s_i_id
having   (select true)
order by s_i_id;
 s_i_id | ordercount
---------------------------------------------------------------------
      1 |          1
      2 |          2
      3 |          3
      4 |          4
      5 |          5
(5 rows)

-- We don't support correlated subqueries in having
select     s_i_id, sum(s_order_cnt) as ordercount
from     stock s
where   s_order_cnt > (select sum(s_order_cnt) * .005 as where_query from stock)
group by s_i_id
having   (select max(s_order_cnt) > 2 as having_query from stock where s_i_id = s.s_i_id)
order by s_i_id;
ERROR:  complex joins are only supported when all distributed tables are co-located and joined on their distribution columns
-- We don't support correlated subqueries in having
select     s_i_id, sum(s_order_cnt) as ordercount
from     stock s
group by s_i_id
having   (select max(s_order_cnt) > 2 as having_query from stock where s_i_id = s.s_i_id)
order by s_i_id;
ERROR:  complex joins are only supported when all distributed tables are co-located and joined on their distribution columns
DROP TABLE stock;
CREATE TABLE stock (
  s_w_id int NOT NULL,
  s_i_id int NOT NULL,
  s_quantity decimal(4,0) NOT NULL,
  s_ytd decimal(8,2) NOT NULL,
  s_order_cnt int NOT NULL,
  s_remote_cnt int NOT NULL,
  s_data varchar(50) NOT NULL,
  s_dist_01 char(24) NOT NULL,
  s_dist_02 char(24) NOT NULL,
  s_dist_03 char(24) NOT NULL,
  s_dist_04 char(24) NOT NULL,
  s_dist_05 char(24) NOT NULL,
  s_dist_06 char(24) NOT NULL,
  s_dist_07 char(24) NOT NULL,
  s_dist_08 char(24) NOT NULL,
  s_dist_09 char(24) NOT NULL,
  s_dist_10 char(24) NOT NULL,
  PRIMARY KEY (s_w_id,s_i_id)
);
insert into stock VALUES
(1, 33, 1, 1, 1, 1, '', '','','','','','','','','',''),
(33, 1, 1, 1, 1, 1, '', '','','','','','','','','',''),
(32, 1, 1, 1, 1, 1, '', '','','','','','','','','','');
SELECT create_distributed_table('stock','s_w_id');
NOTICE:  Copying data from local table...
 create_distributed_table
---------------------------------------------------------------------

(1 row)

select     s_i_id, sum(s_order_cnt) as ordercount
from     stock, supplier, nation
where     mod((s_w_id * s_i_id),10000) = s_suppkey
     and s_nationkey = n_nationkey
     and n_name = 'GERMANY'
group by s_i_id
having   sum(s_order_cnt) >
        (select sum(s_order_cnt) * .005
        from stock, supplier, nation
        where mod((s_w_id * s_i_id),10000) = s_suppkey
        and s_nationkey = n_nationkey
        and n_name = 'GERMANY')
order by ordercount desc;
 s_i_id | ordercount
---------------------------------------------------------------------
     33 |          1
      1 |          1
(2 rows)

insert into stock VALUES
(10033, 1, 1, 1, 100000, 1, '', '','','','','','','','','','');
select     s_i_id, sum(s_order_cnt) as ordercount
from     stock, supplier, nation
where     mod((s_w_id * s_i_id),10000) = s_suppkey
     and s_nationkey = n_nationkey
     and n_name = 'GERMANY'
group by s_i_id
having   sum(s_order_cnt) >
        (select sum(s_order_cnt) * .005
        from stock, supplier, nation
        where mod((s_w_id * s_i_id),10000) = s_suppkey
        and s_nationkey = n_nationkey
        and n_name = 'GERMANY')
order by ordercount desc;
 s_i_id | ordercount
---------------------------------------------------------------------
      1 |     100001
(1 row)

BEGIN;
SET LOCAL client_min_messages TO WARNING;
DROP SCHEMA ch_bench_having CASCADE;
COMMIT;
