subselect_gp.out 61.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
set optimizer_enable_master_only_queries = on;
set optimizer_segments = 3;
set optimizer_nestloop_factor = 1.0;
--
-- Base tables for CSQ tests
--
drop table if exists csq_t1_base;
NOTICE:  table "csq_t1_base" does not exist, skipping
create table csq_t1_base(x int, y int) distributed by (x);
insert into csq_t1_base values(1,2);
insert into csq_t1_base values(2,1);
insert into csq_t1_base values(4,2);
drop table if exists csq_t2_base;
NOTICE:  table "csq_t2_base" does not exist, skipping
create table csq_t2_base(x int, y int) distributed by (x);
insert into csq_t2_base values(3,2);
insert into csq_t2_base values(3,2);
insert into csq_t2_base values(3,2);
insert into csq_t2_base values(3,2);
insert into csq_t2_base values(3,1);
--
-- Correlated subqueries
--
drop table if exists csq_t1;
NOTICE:  table "csq_t1" does not exist, skipping
drop table if exists csq_t2;
NOTICE:  table "csq_t2" does not exist, skipping
create table csq_t1(x int, y int) distributed by (x);
create table csq_t2(x int, y int) distributed by (x);
insert into csq_t1 select * from csq_t1_base;
insert into csq_t2 select * from csq_t2_base;
select * from csq_t1 where csq_t1.x >ALL (select csq_t2.x from csq_t2 where csq_t2.y=csq_t1.y) order by 1; -- expected (4,2)
 x | y 
---+---
 4 | 2
(1 row)

--
-- correlations in the targetlist
--
select csq_t1.x, (select sum(bar.x) from csq_t1 bar where bar.x >= csq_t1.x) as sum from csq_t1 order by csq_t1.x;
 x | sum 
---+-----
 1 |   7
 2 |   6
 4 |   4
(3 rows)

select csq_t1.x, (select sum(bar.x) from csq_t1 bar where bar.x = csq_t1.x) as sum from csq_t1 order by csq_t1.x;
 x | sum 
---+-----
 1 |   1
 2 |   2
 4 |   4
(3 rows)

select csq_t1.x, (select bar.x from csq_t1 bar where bar.x = csq_t1.x) as sum from csq_t1 order by csq_t1.x;
 x | sum 
---+-----
 1 |   1
 2 |   2
 4 |   4
(3 rows)

--
-- CSQs with partitioned tables
--
drop table if exists csq_t1;
drop table if exists csq_t2;
create table csq_t1(x int, y int) 
distributed by (x)
partition by range (y) ( start (0) end (4) every (1))
;
NOTICE:  CREATE TABLE will create partition "csq_t1_1_prt_1" for table "csq_t1"
NOTICE:  CREATE TABLE will create partition "csq_t1_1_prt_2" for table "csq_t1"
NOTICE:  CREATE TABLE will create partition "csq_t1_1_prt_3" for table "csq_t1"
NOTICE:  CREATE TABLE will create partition "csq_t1_1_prt_4" for table "csq_t1"
create table csq_t2(x int, y int) 
distributed by (x)
partition by range (y) ( start (0) end (4) every (1))
;
NOTICE:  CREATE TABLE will create partition "csq_t2_1_prt_1" for table "csq_t2"
NOTICE:  CREATE TABLE will create partition "csq_t2_1_prt_2" for table "csq_t2"
NOTICE:  CREATE TABLE will create partition "csq_t2_1_prt_3" for table "csq_t2"
NOTICE:  CREATE TABLE will create partition "csq_t2_1_prt_4" for table "csq_t2"
insert into csq_t1 select * from csq_t1_base;
insert into csq_t2 select * from csq_t2_base;
explain select * from csq_t1 where csq_t1.x >ALL (select csq_t2.x from csq_t2 where csq_t2.y=csq_t1.y) order by 1;
89 90 91
                                                              QUERY PLAN                                                              
--------------------------------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice17; segments: 3)  (cost=810368945.20..810369375.70 rows=172200 width=8)
92
   Merge Key: x
93
   ->  Sort  (cost=810368945.20..810369375.70 rows=57400 width=8)
94
         Sort Key: public.csq_t1.x
95 96
         ->  Append  (cost=0.00..810353969.20 rows=57400 width=8)
               ->  Seq Scan on csq_t1_1_prt_1 csq_t1  (cost=0.00..202588492.30 rows=14350 width=8)
97 98
                     Filter: (subplan)
                     SubPlan 1
99 100 101
                       ->  Result  (cost=0.00..4705.00 rows=115 width=4)
                             ->  Append  (cost=0.00..4705.00 rows=115 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
102
                                         Filter: public.csq_t2.y = $0
103 104 105 106
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_1 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
107
                                         Filter: public.csq_t2.y = $0
108 109 110 111
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice2; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_2 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
112
                                         Filter: public.csq_t2.y = $0
113 114 115 116
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice3; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_3 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
117
                                         Filter: public.csq_t2.y = $0
118 119 120 121
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice4; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_4 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
               ->  Seq Scan on csq_t1_1_prt_2 csq_t1  (cost=0.00..202588492.30 rows=14350 width=8)
122 123
                     Filter: (subplan)
                     SubPlan 1
124 125 126
                       ->  Result  (cost=0.00..4705.00 rows=115 width=4)
                             ->  Append  (cost=0.00..4705.00 rows=115 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
127
                                         Filter: public.csq_t2.y = $0
128 129 130 131
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice5; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_1 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
132
                                         Filter: public.csq_t2.y = $0
133 134 135 136
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice6; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_2 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
137
                                         Filter: public.csq_t2.y = $0
138 139 140 141
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice7; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_3 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
142
                                         Filter: public.csq_t2.y = $0
143 144 145 146
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice8; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_4 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
               ->  Seq Scan on csq_t1_1_prt_3 csq_t1  (cost=0.00..202588492.30 rows=14350 width=8)
147 148
                     Filter: (subplan)
                     SubPlan 1
149 150 151
                       ->  Result  (cost=0.00..4705.00 rows=115 width=4)
                             ->  Append  (cost=0.00..4705.00 rows=115 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
152
                                         Filter: public.csq_t2.y = $0
153 154 155 156
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice9; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_1 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
157
                                         Filter: public.csq_t2.y = $0
158 159 160 161
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice10; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_2 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
162
                                         Filter: public.csq_t2.y = $0
163 164 165 166
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice11; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_3 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
167
                                         Filter: public.csq_t2.y = $0
168 169 170 171
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice12; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_4 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
               ->  Seq Scan on csq_t1_1_prt_4 csq_t1  (cost=0.00..202588492.30 rows=14350 width=8)
172 173
                     Filter: (subplan)
                     SubPlan 1
174 175 176
                       ->  Result  (cost=0.00..4705.00 rows=115 width=4)
                             ->  Append  (cost=0.00..4705.00 rows=115 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
177
                                         Filter: public.csq_t2.y = $0
178 179 180 181
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice13; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_1 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
182
                                         Filter: public.csq_t2.y = $0
183 184 185 186
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice14; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_2 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
187
                                         Filter: public.csq_t2.y = $0
188 189 190 191
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice15; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_3 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
                                   ->  Result  (cost=1176.34..1177.20 rows=29 width=4)
192
                                         Filter: public.csq_t2.y = $0
193 194 195
                                         ->  Materialize  (cost=1176.34..1177.20 rows=29 width=4)
                                               ->  Broadcast Motion 3:3  (slice16; segments: 3)  (cost=0.00..1176.25 rows=29 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_4 csq_t2  (cost=0.00..1176.25 rows=29 width=4)
196
 Settings:  optimizer_segments=3
197 198
 Optimizer status: legacy query optimizer
(107 rows)
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

select * from csq_t1 where csq_t1.x >ALL (select csq_t2.x from csq_t2 where csq_t2.y=csq_t1.y) order by 1; -- expected (4,2)
 x | y 
---+---
 4 | 2
(1 row)

drop table if exists csq_t1;
drop table if exists csq_t2;
drop table if exists csq_t1_base;
drop table if exists csq_t2_base;
--
-- Multi-row subqueries
--
drop table if exists mrs_t1;
NOTICE:  table "mrs_t1" does not exist, skipping
create table mrs_t1(x int) distributed by (x);
insert into mrs_t1 select generate_series(1,20);
explain select * from mrs_t1 where exists (select x from mrs_t1 where x < -1);
                                           QUERY PLAN                                           
------------------------------------------------------------------------------------------------
220 221
 Gather Motion 3:1  (slice2; segments: 3)  (cost=3.27..6.47 rows=20 width=4)
   ->  Result  (cost=3.27..6.47 rows=7 width=4)
222 223
         One-Time Filter: $0
         InitPlan  (slice3)
224 225 226 227
           ->  Limit  (cost=0.00..3.27 rows=1 width=0)
                 ->  Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..3.27 rows=1 width=0)
                       ->  Limit  (cost=0.00..3.25 rows=1 width=0)
                             ->  Seq Scan on mrs_t1  (cost=0.00..3.25 rows=1 width=0)
228
                                   Filter: x < (-1)
229
         ->  Seq Scan on mrs_t1  (cost=0.00..3.20 rows=7 width=4)
230
 Settings:  optimizer_segments=3
231 232
 Optimizer status: legacy query optimizer
(12 rows)
233 234 235 236 237 238 239 240 241

select * from mrs_t1 where exists (select x from mrs_t1 where x < -1) order by 1;
 x 
---
(0 rows)

explain select * from mrs_t1 where exists (select x from mrs_t1 where x = 1);
                                           QUERY PLAN                                           
------------------------------------------------------------------------------------------------
242 243
 Gather Motion 3:1  (slice2; segments: 3)  (cost=3.27..6.47 rows=20 width=4)
   ->  Result  (cost=3.27..6.47 rows=7 width=4)
244 245
         One-Time Filter: $0
         InitPlan  (slice3)
246 247 248 249
           ->  Limit  (cost=0.00..3.27 rows=1 width=0)
                 ->  Gather Motion 1:1  (slice1; segments: 1)  (cost=0.00..3.27 rows=1 width=0)
                       ->  Limit  (cost=0.00..3.25 rows=1 width=0)
                             ->  Seq Scan on mrs_t1  (cost=0.00..3.25 rows=1 width=0)
250
                                   Filter: x = 1
251
         ->  Seq Scan on mrs_t1  (cost=0.00..3.20 rows=7 width=4)
252
 Settings:  optimizer_segments=3
253 254
 Optimizer status: legacy query optimizer
(12 rows)
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

select * from mrs_t1 where exists (select x from mrs_t1 where x = 1) order by 1;
 x  
----
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
(20 rows)

explain select * from mrs_t1 where x in (select x-95 from mrs_t1) or x < 5;
282 283 284 285
                                            QUERY PLAN                                             
---------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..36.30 rows=13 width=4)
   ->  Seq Scan on mrs_t1  (cost=0.00..36.30 rows=5 width=4)
286 287
         Filter: ((subplan)) OR x < 5
         SubPlan 1
288 289 290
           ->  Materialize  (cost=3.27..3.47 rows=7 width=4)
                 ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..3.25 rows=7 width=4)
                       ->  Seq Scan on mrs_t1  (cost=0.00..3.25 rows=7 width=4)
291
 Settings:  optimizer_segments=3
292 293
 Optimizer status: legacy query optimizer
(9 rows)
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334

select * from mrs_t1 where x in (select x-95 from mrs_t1) or x < 5 order by 1;
 x 
---
 1
 2
 3
 4
(4 rows)

drop table if exists mrs_t1;
--
-- Multi-row subquery from MSTR
--
drop table if exists mrs_u1;
NOTICE:  table "mrs_u1" does not exist, skipping
drop table if exists mrs_u2;
NOTICE:  table "mrs_u2" does not exist, skipping
create TABLE mrs_u1 (a int, b int);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'a' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
create TABLE mrs_u2 (a int, b int);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'a' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
insert into mrs_u1 values (1,2),(11,22);
insert into mrs_u2 values (1,2),(11,22),(33,44);
select * from mrs_u1 join mrs_u2 on mrs_u1.a=mrs_u2.a where mrs_u1.a in (1,11) or mrs_u2.a in (select a from mrs_u1 where a=1) order by 1;
 a  | b  | a  | b  
----+----+----+----
  1 |  2 |  1 |  2
 11 | 22 | 11 | 22
(2 rows)

drop table if exists mrs_u1;
drop table if exists mrs_u2;
--
-- MPP-13758
--
drop table if exists csq_m1;
NOTICE:  table "csq_m1" does not exist, skipping
create table csq_m1();
335
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause, and no column type is suitable for a distribution key. Creating a NULL policy entry.
336 337 338 339 340 341 342 343 344 345 346 347 348 349
set allow_system_table_mods='DML';
delete from gp_distribution_policy where localoid='csq_m1'::regclass;
reset allow_system_table_mods;
alter table csq_m1 add column x int;
insert into csq_m1 values(1);
drop table if exists csq_d1;
NOTICE:  table "csq_d1" does not exist, skipping
create table csq_d1(x int) distributed by (x);
insert into csq_d1 select * from csq_m1;
explain select array(select x from csq_m1); -- no initplan
                          QUERY PLAN                          
--------------------------------------------------------------
 Result  (cost=1.01..1.02 rows=1 width=0)
   InitPlan
350
     ->  Seq Scan on csq_m1  (cost=0.00..1.01 rows=1 width=4)
351
 Settings:  optimizer_segments=3
352 353
 Optimizer status: legacy query optimizer
(5 rows)
354 355 356 357 358 359 360 361 362 363 364 365

select array(select x from csq_m1); -- {1}
 ?column? 
----------
 {1}
(1 row)

explain select array(select x from csq_d1); -- initplan
                                     QUERY PLAN                                     
------------------------------------------------------------------------------------
 Result  (cost=1.01..1.02 rows=1 width=0)
   InitPlan  (slice2)
366
     ->  Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..1.01 rows=1 width=4)
367 368
           ->  Seq Scan on csq_d1  (cost=0.00..1.01 rows=1 width=4)
 Settings:  optimizer_segments=3
369 370
 Optimizer status: legacy query optimizer
(6 rows)
371 372 373 374 375 376 377 378 379 380

select array(select x from csq_d1); -- {1}
 ?column? 
----------
 {1}
(1 row)

--
-- CSQs involving master-only and distributed tables
--
381 382 383
drop table if exists t3cozlib;
NOTICE:  table "t3cozlib" does not exist, skipping
create table t3cozlib (c1 int , c2 varchar) with (appendonly=true, compresstype=zlib, orientation=column) distributed by (c1);
384 385 386
drop table if exists pg_attribute_storage;
NOTICE:  table "pg_attribute_storage" does not exist, skipping
create table pg_attribute_storage (attrelid int, attnum int, attoptions text[]) distributed by (attrelid);
387 388
insert into pg_attribute_storage values ('t3cozlib'::regclass, 1, E'{\'something\'}');
insert into pg_attribute_storage values ('t3cozlib'::regclass, 2, E'{\'something2\'}');
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
SELECT a.attname
, pg_catalog.format_type(a.atttypid, a.atttypmod)
, ( SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
 FROM pg_catalog.pg_attrdef d
WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef
)
, a.attnotnull
, a.attnum
, a.attstorage
, pg_catalog.col_description(a.attrelid, a.attnum)
, ( SELECT s.attoptions
FROM pg_attribute_storage s
WHERE s.attrelid = a.attrelid AND s.attnum = a.attnum
) newcolumn
FROM pg_catalog.pg_attribute a
404
WHERE a.attrelid = 't3cozlib'::regclass AND a.attnum > 0 AND NOT a.attisdropped
405 406 407 408 409 410 411 412 413 414 415 416 417
ORDER BY a.attnum
; -- expect to see 2 rows
 attname |    format_type    | ?column? | attnotnull | attnum | attstorage | col_description |   newcolumn    
---------+-------------------+----------+------------+--------+------------+-----------------+----------------
 c1      | integer           |          | f          |      1 | p          |                 | {'something'}
 c2      | character varying |          | f          |      2 | x          |                 | {'something2'}
(2 rows)

--
-- More CSQs involving master-only and distributed relations
--
drop table if exists csq_m1;
create table csq_m1();
418
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause, and no column type is suitable for a distribution key. Creating a NULL policy entry.
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
set allow_system_table_mods='DML';
delete from gp_distribution_policy where localoid='csq_m1'::regclass;
reset allow_system_table_mods;
alter table csq_m1 add column x int;
insert into csq_m1 values(1),(2),(3);
drop table if exists csq_d1;
create table csq_d1(x int) distributed by (x);
insert into csq_d1 select * from csq_m1 where x < 3;
insert into csq_d1 values(4);
select * from csq_m1;
 x 
---
 1
 2
 3
(3 rows)

select * from csq_d1;
 x 
---
 1
 2
 4
(3 rows)

--
-- outer plan node is master-only and CSQ has distributed relation
--
explain select * from csq_m1 where x not in (select x from csq_d1) or x < -100; -- gather motion
                                        QUERY PLAN                                        
------------------------------------------------------------------------------------------
450
 Seq Scan on csq_m1  (cost=0.00..2.58 rows=2 width=4)
451 452
   Filter: (NOT ((subplan))) OR x < (-100)
   SubPlan 1
453 454 455
     ->  Materialize  (cost=1.02..1.04 rows=1 width=4)
           ->  Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..1.02 rows=2 width=4)
                 ->  Seq Scan on csq_d1  (cost=0.00..1.02 rows=1 width=4)
456
 Settings:  optimizer_segments=3
457 458
 Optimizer status: legacy query optimizer
(8 rows)
459 460 461 462 463 464 465 466 467 468 469 470 471

select * from csq_m1 where x not in (select x from csq_d1) or x < -100; -- (3)
 x 
---
 3
(1 row)

--
-- outer plan node is master-only and CSQ has distributed relation
--
explain select * from csq_d1 where x not in (select x from csq_m1) or x < -100; -- broadcast motion
                                      QUERY PLAN                                      
--------------------------------------------------------------------------------------
472 473
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..2.07 rows=2 width=4)
   ->  Seq Scan on csq_d1  (cost=0.00..2.07 rows=1 width=4)
474 475
         Filter: (NOT ((subplan))) OR x < (-100)
         SubPlan 1
476 477 478
           ->  Materialize  (cost=1.03..1.06 rows=3 width=4)
                 ->  Broadcast Motion 1:3  (slice1)  (cost=0.00..1.03 rows=3 width=4)
                       ->  Seq Scan on csq_m1  (cost=0.00..1.03 rows=3 width=4)
479
 Settings:  optimizer_segments=3
480 481
 Optimizer status: legacy query optimizer
(9 rows)
482 483 484 485 486 487 488

select * from csq_d1 where x not in (select x from csq_m1) or x < -100; -- (4)
 x 
---
 4
(1 row)

489 490
-- drop csq_m1 since we deleted its gp_distribution_policy entry
drop table csq_m1;
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
--
-- MPP-14441 Don't lose track of initplans
--
drop table if exists csq_t1;
NOTICE:  table "csq_t1" does not exist, skipping
CREATE TABLE csq_t1 (a int, b int, c int, d int, e text) DISTRIBUTED BY (a);
INSERT INTO csq_t1 SELECT i, i/3, i%2, 100-i, 'text'||i  FROM generate_series(1,100) i;
select count(*) from csq_t1 t1 where a > (SELECT x.b FROM ( select avg(a)::int as b,'haha'::text from csq_t1 t2 where t2.a=t1.d) x ) ;
 count 
-------
    49
(1 row)

select count(*) from csq_t1 t1 where a > ( select avg(a)::int from csq_t1 t2 where t2.a=t1.d) ;
 count 
-------
    49
(1 row)

--
-- correlation in a func expr
--
513 514
CREATE OR REPLACE FUNCTION csq_f(a int) RETURNS int AS $$ select $1 $$ LANGUAGE SQL CONTAINS SQL;
DROP TABLE IF EXISTS csq_r;
515 516 517 518 519 520 521 522
CREATE TABLE csq_r(a int) distributed by (a);
INSERT INTO csq_r VALUES (1);
-- subqueries shouldn't be pulled into a join if the from clause has a function call
-- with a correlated argument
-- force_explain
explain SELECT * FROM csq_r WHERE a IN (SELECT * FROM csq_f(csq_r.a));
                                 QUERY PLAN                                 
----------------------------------------------------------------------------
523
 Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..1.02 rows=1 width=4)
524 525 526 527 528
   ->  Seq Scan on csq_r  (cost=0.00..1.02 rows=1 width=4)
         Filter: (subplan)
         SubPlan 1
           ->  Function Scan on csq_f  (cost=0.00..0.01 rows=1 width=4)
 Settings:  optimizer_segments=3
529 530
 Optimizer status: legacy query optimizer
(7 rows)
531 532 533 534 535 536 537 538 539 540 541

SELECT * FROM csq_r WHERE a IN (SELECT * FROM csq_f(csq_r.a));
 a 
---
 1
(1 row)

-- force_explain
explain SELECT * FROM csq_r WHERE a not IN (SELECT * FROM csq_f(csq_r.a));
                                 QUERY PLAN                                 
----------------------------------------------------------------------------
542
 Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..1.02 rows=1 width=4)
543 544 545 546 547
   ->  Seq Scan on csq_r  (cost=0.00..1.02 rows=1 width=4)
         Filter: (subplan)
         SubPlan 1
           ->  Function Scan on csq_f  (cost=0.00..0.01 rows=1 width=4)
 Settings:  optimizer_segments=3
548 549
 Optimizer status: legacy query optimizer
(7 rows)
550 551 552 553 554 555 556 557 558 559

SELECT * FROM csq_r WHERE a not IN (SELECT * FROM csq_f(csq_r.a));
 a 
---
(0 rows)

-- force_explain
explain SELECT * FROM csq_r WHERE exists (SELECT * FROM csq_f(csq_r.a));
                                 QUERY PLAN                                 
----------------------------------------------------------------------------
560
 Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..1.02 rows=1 width=4)
561 562 563 564 565
   ->  Seq Scan on csq_r  (cost=0.00..1.02 rows=1 width=4)
         Filter: (subplan)
         SubPlan 1
           ->  Function Scan on csq_f  (cost=0.00..0.01 rows=1 width=4)
 Settings:  optimizer_segments=3
566 567
 Optimizer status: legacy query optimizer
(7 rows)
568 569 570 571 572 573 574 575 576 577 578

SELECT * FROM csq_r WHERE exists (SELECT * FROM csq_f(csq_r.a));
 a 
---
 1
(1 row)

-- force_explain
explain SELECT * FROM csq_r WHERE not exists (SELECT * FROM csq_f(csq_r.a));
                                 QUERY PLAN                                 
----------------------------------------------------------------------------
579
 Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..1.02 rows=1 width=4)
580 581 582 583 584
   ->  Seq Scan on csq_r  (cost=0.00..1.02 rows=1 width=4)
         Filter: NOT ((subplan))
         SubPlan 1
           ->  Function Scan on csq_f  (cost=0.00..0.01 rows=1 width=4)
 Settings:  optimizer_segments=3
585 586
 Optimizer status: legacy query optimizer
(7 rows)
587 588 589 590 591 592 593 594 595 596

SELECT * FROM csq_r WHERE not exists (SELECT * FROM csq_f(csq_r.a));
 a 
---
(0 rows)

-- force_explain
explain SELECT * FROM csq_r WHERE a > (SELECT csq_f FROM csq_f(csq_r.a) limit 1);
                                  QUERY PLAN                                  
------------------------------------------------------------------------------
597
 Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..1.02 rows=1 width=4)
598 599 600 601 602 603
   ->  Seq Scan on csq_r  (cost=0.00..1.02 rows=1 width=4)
         Filter: a > ((subplan))
         SubPlan 1
           ->  Limit  (cost=0.00..0.01 rows=1 width=4)
                 ->  Function Scan on csq_f  (cost=0.00..0.01 rows=1 width=4)
 Settings:  optimizer_segments=3
604 605
 Optimizer status: legacy query optimizer
(8 rows)
606 607 608 609 610 611 612 613 614 615

SELECT * FROM csq_r WHERE a > (SELECT csq_f FROM csq_f(csq_r.a) limit 1);
 a 
---
(0 rows)

-- force_explain
explain SELECT * FROM csq_r WHERE a < ANY (SELECT csq_f FROM csq_f(csq_r.a));
                                 QUERY PLAN                                 
----------------------------------------------------------------------------
616
 Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..1.02 rows=1 width=4)
617 618 619 620 621
   ->  Seq Scan on csq_r  (cost=0.00..1.02 rows=1 width=4)
         Filter: (subplan)
         SubPlan 1
           ->  Function Scan on csq_f  (cost=0.00..0.01 rows=1 width=4)
 Settings:  optimizer_segments=3
622 623
 Optimizer status: legacy query optimizer
(7 rows)
624 625 626 627 628 629 630 631 632 633

SELECT * FROM csq_r WHERE a < ANY (SELECT csq_f FROM csq_f(csq_r.a));
 a 
---
(0 rows)

-- force_explain
explain SELECT * FROM csq_r WHERE a <= ALL (SELECT csq_f FROM csq_f(csq_r.a));
                                 QUERY PLAN                                 
----------------------------------------------------------------------------
634
 Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..1.02 rows=1 width=4)
635 636 637 638 639
   ->  Seq Scan on csq_r  (cost=0.00..1.02 rows=1 width=4)
         Filter: (subplan)
         SubPlan 1
           ->  Function Scan on csq_f  (cost=0.00..0.01 rows=1 width=4)
 Settings:  optimizer_segments=3
640 641
 Optimizer status: legacy query optimizer
(7 rows)
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674

SELECT * FROM csq_r WHERE a <= ALL (SELECT csq_f FROM csq_f(csq_r.a));
 a 
---
 1
(1 row)

-- fails: correlation in distributed subplan
-- force_explain
explain SELECT * FROM csq_r WHERE a IN (SELECT csq_f FROM csq_f(csq_r.a),csq_r);
ERROR:  Cannot parallelize that query yet.
DETAIL:  In a subquery FROM clause, a function invocation cannot contain a correlated reference.
--
-- Test pullup of expr CSQs to joins
--
--
-- Test data
--
drop table if exists csq_pullup;
NOTICE:  table "csq_pullup" does not exist, skipping
create table csq_pullup(t text, n numeric, i int, v varchar(10)) distributed by (t);
insert into csq_pullup values ('abc',1, 2, 'xyz');
insert into csq_pullup values ('xyz',2, 3, 'def');  
insert into csq_pullup values ('def',3, 1, 'abc'); 
--
-- Expr CSQs to joins
--
--
-- text, text
--
explain select * from csq_pullup t0 where 1= (select count(*) from csq_pullup t1 where t0.t=t1.t);
                                        QUERY PLAN                                         
-------------------------------------------------------------------------------------------
675
 Gather Motion 3:1  (slice1; segments: 3)  (cost=1.06..2.11 rows=4 width=19)
676 677 678 679 680 681
   ->  Hash Join  (cost=1.06..2.11 rows=2 width=19)
         Hash Cond: t0.t = "Expr_SUBQUERY".csq_c0
         ->  Seq Scan on csq_pullup t0  (cost=0.00..1.01 rows=1 width=19)
         ->  Hash  (cost=1.04..1.04 rows=1 width=32)
               ->  Subquery Scan "Expr_SUBQUERY"  (cost=1.02..1.04 rows=1 width=32)
                     ->  HashAggregate  (cost=1.02..1.03 rows=1 width=40)
682
                           Filter: count(*) = 1
683 684 685
                           Group By: t1.t
                           ->  Seq Scan on csq_pullup t1  (cost=0.00..1.01 rows=1 width=4)
 Settings:  optimizer_segments=3
686 687
 Optimizer status: legacy query optimizer
(12 rows)
688 689 690 691 692 693

select * from csq_pullup t0 where 1= (select count(*) from csq_pullup t1 where t0.t=t1.t);
  t  | n | i |  v  
-----+---+---+-----
 def | 3 | 1 | abc
 abc | 1 | 2 | xyz
694
 xyz | 2 | 3 | def
695 696 697 698 699 700 701 702
(3 rows)

--
-- text, varchar
--
explain select * from csq_pullup t0 where 1= (select count(*) from csq_pullup t1 where t0.t=t1.v);
                                                   QUERY PLAN                                                    
-----------------------------------------------------------------------------------------------------------------
703
 Gather Motion 3:1  (slice2; segments: 3)  (cost=1.09..2.14 rows=4 width=19)
704 705 706 707 708 709
   ->  Hash Join  (cost=1.09..2.14 rows=2 width=19)
         Hash Cond: t0.t = "Expr_SUBQUERY".csq_c0
         ->  Seq Scan on csq_pullup t0  (cost=0.00..1.01 rows=1 width=19)
         ->  Hash  (cost=1.08..1.08 rows=1 width=32)
               ->  Subquery Scan "Expr_SUBQUERY"  (cost=1.05..1.08 rows=1 width=32)
                     ->  HashAggregate  (cost=1.05..1.07 rows=1 width=40)
710
                           Filter: count(partial_aggregation.unnamed_attr_2) = 1
711
                           Group By: "?column1?"
712
                           ->  Redistribute Motion 3:3  (slice1; segments: 3)  (cost=1.02..1.04 rows=1 width=40)
713 714 715 716 717
                                 Hash Key: unnamed_attr_1
                                 ->  HashAggregate  (cost=1.02..1.02 rows=1 width=40)
                                       Group By: t1.v::text
                                       ->  Seq Scan on csq_pullup t1  (cost=0.00..1.01 rows=1 width=4)
 Settings:  optimizer_segments=3
718 719
 Optimizer status: legacy query optimizer
(16 rows)
720 721 722 723

select * from csq_pullup t0 where 1= (select count(*) from csq_pullup t1 where t0.t=t1.v);
  t  | n | i |  v  
-----+---+---+-----
724
 def | 3 | 1 | abc
725 726 727 728 729 730 731 732 733 734
 abc | 1 | 2 | xyz
 xyz | 2 | 3 | def
(3 rows)

--
-- numeric, numeric
--
explain select * from csq_pullup t0 where 1= (select count(*) from csq_pullup t1 where t0.n=t1.n);
                                                   QUERY PLAN                                                    
-----------------------------------------------------------------------------------------------------------------
735
 Gather Motion 3:1  (slice3; segments: 3)  (cost=1.09..2.16 rows=4 width=19)
736 737
   ->  Hash Join  (cost=1.09..2.16 rows=2 width=19)
         Hash Cond: t0.n = "Expr_SUBQUERY".csq_c0
738
         ->  Redistribute Motion 3:3  (slice1; segments: 3)  (cost=0.00..1.03 rows=1 width=19)
739 740 741 742 743
               Hash Key: t0.n
               ->  Seq Scan on csq_pullup t0  (cost=0.00..1.01 rows=1 width=19)
         ->  Hash  (cost=1.08..1.08 rows=1 width=32)
               ->  Subquery Scan "Expr_SUBQUERY"  (cost=1.05..1.08 rows=1 width=32)
                     ->  HashAggregate  (cost=1.05..1.07 rows=1 width=40)
744
                           Filter: count(partial_aggregation.unnamed_attr_2) = 1
745
                           Group By: t1.n
746
                           ->  Redistribute Motion 3:3  (slice2; segments: 3)  (cost=1.02..1.04 rows=1 width=40)
747 748 749 750 751
                                 Hash Key: t1.n
                                 ->  HashAggregate  (cost=1.02..1.02 rows=1 width=40)
                                       Group By: t1.n
                                       ->  Seq Scan on csq_pullup t1  (cost=0.00..1.01 rows=1 width=7)
 Settings:  optimizer_segments=3
752 753
 Optimizer status: legacy query optimizer
(18 rows)
754 755 756 757 758 759

select * from csq_pullup t0 where 1= (select count(*) from csq_pullup t1 where t0.n=t1.n);
  t  | n | i |  v  
-----+---+---+-----
 def | 3 | 1 | abc
 xyz | 2 | 3 | def
760
 abc | 1 | 2 | xyz
761 762 763 764 765 766 767 768
(3 rows)

--
-- function(numeric), function(numeric)
--
explain select * from csq_pullup t0 where 1= (select count(*) from csq_pullup t1 where t0.n + 1=t1.n + 1);
                                                   QUERY PLAN                                                    
-----------------------------------------------------------------------------------------------------------------
769
 Gather Motion 3:1  (slice3; segments: 3)  (cost=1.10..2.17 rows=4 width=19)
770 771
   ->  Hash Join  (cost=1.10..2.17 rows=2 width=19)
         Hash Cond: (t0.n + 1::numeric) = "Expr_SUBQUERY".csq_c0
772
         ->  Redistribute Motion 3:3  (slice1; segments: 3)  (cost=0.00..1.03 rows=1 width=19)
773 774 775 776 777
               Hash Key: t0.n + 1::numeric
               ->  Seq Scan on csq_pullup t0  (cost=0.00..1.01 rows=1 width=19)
         ->  Hash  (cost=1.09..1.09 rows=1 width=32)
               ->  Subquery Scan "Expr_SUBQUERY"  (cost=1.06..1.09 rows=1 width=32)
                     ->  HashAggregate  (cost=1.06..1.08 rows=1 width=40)
778
                           Filter: count(partial_aggregation.unnamed_attr_2) = 1
779
                           Group By: "?column1?"
780
                           ->  Redistribute Motion 3:3  (slice2; segments: 3)  (cost=1.02..1.04 rows=1 width=40)
781 782 783 784 785
                                 Hash Key: unnamed_attr_1
                                 ->  HashAggregate  (cost=1.02..1.02 rows=1 width=40)
                                       Group By: t1.n + 1::numeric
                                       ->  Seq Scan on csq_pullup t1  (cost=0.00..1.01 rows=1 width=7)
 Settings:  optimizer_segments=3
786 787
 Optimizer status: legacy query optimizer
(18 rows)
788 789 790 791 792

select * from csq_pullup t0 where 1= (select count(*) from csq_pullup t1 where t0.n + 1=t1.n + 1);
  t  | n | i |  v  
-----+---+---+-----
 def | 3 | 1 | abc
793
 xyz | 2 | 3 | def
794 795 796 797 798 799 800 801 802
 abc | 1 | 2 | xyz
(3 rows)

--
-- function(numeric), function(int)
--
explain select * from csq_pullup t0 where 1= (select count(*) from csq_pullup t1 where t0.n + 1=t1.i + 1);
                                                   QUERY PLAN                                                    
-----------------------------------------------------------------------------------------------------------------
803
 Gather Motion 3:1  (slice3; segments: 3)  (cost=1.10..2.18 rows=4 width=19)
804 805
   ->  Hash Join  (cost=1.10..2.18 rows=2 width=19)
         Hash Cond: (t0.n + 1::numeric) = "Expr_SUBQUERY".csq_c0
806
         ->  Redistribute Motion 3:3  (slice1; segments: 3)  (cost=0.00..1.03 rows=1 width=19)
807 808 809 810 811
               Hash Key: t0.n + 1::numeric
               ->  Seq Scan on csq_pullup t0  (cost=0.00..1.01 rows=1 width=19)
         ->  Hash  (cost=1.09..1.09 rows=1 width=32)
               ->  Subquery Scan "Expr_SUBQUERY"  (cost=1.06..1.09 rows=1 width=32)
                     ->  HashAggregate  (cost=1.06..1.08 rows=1 width=40)
812
                           Filter: count(partial_aggregation.unnamed_attr_2) = 1
813
                           Group By: "?column1?"
814
                           ->  Redistribute Motion 3:3  (slice2; segments: 3)  (cost=1.02..1.05 rows=1 width=40)
815 816 817 818 819
                                 Hash Key: unnamed_attr_1
                                 ->  HashAggregate  (cost=1.02..1.03 rows=1 width=40)
                                       Group By: (t1.i + 1)::numeric
                                       ->  Seq Scan on csq_pullup t1  (cost=0.00..1.01 rows=1 width=4)
 Settings:  optimizer_segments=3
820 821
 Optimizer status: legacy query optimizer
(18 rows)
822 823 824 825

select * from csq_pullup t0 where 1= (select count(*) from csq_pullup t1 where t0.n + 1=t1.i + 1);
  t  | n | i |  v  
-----+---+---+-----
826
 xyz | 2 | 3 | def
827 828 829 830 831 832 833 834 835 836 837 838 839
 def | 3 | 1 | abc
 abc | 1 | 2 | xyz
(3 rows)

--
-- NOT EXISTS CSQs to joins
--
--
-- text, text
--
explain select * from csq_pullup t0 where not exists (select 1 from csq_pullup t1 where t0.t=t1.t and t1.i = 1);
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
840
 Gather Motion 3:1  (slice1; segments: 3)  (cost=1.02..2.07 rows=4 width=19)
841 842 843 844 845
   ->  Hash Left Anti Semi Join  (cost=1.02..2.07 rows=2 width=19)
         Hash Cond: t0.t = t1.t
         ->  Seq Scan on csq_pullup t0  (cost=0.00..1.01 rows=1 width=19)
         ->  Hash  (cost=1.01..1.01 rows=1 width=4)
               ->  Seq Scan on csq_pullup t1  (cost=0.00..1.01 rows=1 width=4)
846
                     Filter: t IS NOT NULL AND i = 1
847
 Settings:  optimizer_segments=3
848 849
 Optimizer status: legacy query optimizer
(9 rows)
850 851 852 853 854 855 856 857 858 859 860 861 862 863

select * from csq_pullup t0 where not exists (select 1 from csq_pullup t1 where t0.t=t1.t and t1.i = 1);
  t  | n | i |  v  
-----+---+---+-----
 abc | 1 | 2 | xyz
 xyz | 2 | 3 | def
(2 rows)

--
-- int, function(int)
--
explain select * from csq_pullup t0 where not exists (select 1 from csq_pullup t1 where t0.i=t1.i + 1);
                                           QUERY PLAN                                            
-------------------------------------------------------------------------------------------------
864 865
 Gather Motion 3:1  (slice2; segments: 3)  (cost=1.09..2.15 rows=4 width=19)
   ->  Hash Left Anti Semi Join  (cost=1.09..2.15 rows=2 width=19)
866 867
         Hash Cond: t0.i = (t1.i + 1)
         ->  Seq Scan on csq_pullup t0  (cost=0.00..1.01 rows=1 width=19)
868 869
         ->  Hash  (cost=1.05..1.05 rows=1 width=4)
               ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..1.05 rows=1 width=4)
870 871 872
                     ->  Seq Scan on csq_pullup t1  (cost=0.00..1.01 rows=1 width=4)
                           Filter: (i + 1) IS NOT NULL
 Settings:  optimizer_segments=3
873 874
 Optimizer status: legacy query optimizer
(10 rows)
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895

select * from csq_pullup t0 where not exists (select 1 from csq_pullup t1 where t0.i=t1.i + 1);
  t  | n | i |  v  
-----+---+---+-----
 def | 3 | 1 | abc
(1 row)

--
-- wrong results bug MPP-16477
--
drop table if exists subselect_t1;
NOTICE:  table "subselect_t1" does not exist, skipping
drop table if exists subselect_t2;
NOTICE:  table "subselect_t2" does not exist, skipping
create table subselect_t1(x int) distributed by (x);
insert into subselect_t1 values(1),(2);
create table subselect_t2(y int) distributed by (y);
insert into subselect_t2 values(1),(2),(2);
analyze subselect_t1;
analyze subselect_t2;
explain select * from subselect_t1 where x in (select y from subselect_t2);
896 897 898 899
                                  QUERY PLAN                                  
------------------------------------------------------------------------------
 Gather Motion 3:1  (slice1; segments: 3)  (cost=1.07..2.13 rows=4 width=4)
   ->  Hash EXISTS Join  (cost=1.07..2.13 rows=2 width=4)
900
         Hash Cond: subselect_t1.x = subselect_t2.y
901 902 903
         ->  Seq Scan on subselect_t1  (cost=0.00..1.02 rows=1 width=4)
         ->  Hash  (cost=1.03..1.03 rows=1 width=4)
               ->  Seq Scan on subselect_t2  (cost=0.00..1.03 rows=1 width=4)
904
 Settings:  optimizer_segments=3
905 906
 Optimizer status: legacy query optimizer
(8 rows)
907 908 909 910 911 912 913 914 915 916 917 918 919 920

select * from subselect_t1 where x in (select y from subselect_t2);
 x 
---
 1
 2
(2 rows)

-- start_ignore
-- Known_opt_diff: MPP-21351
-- end_ignore
explain select * from subselect_t1 where x in (select y from subselect_t2 union all select y from subselect_t2);
                                             QUERY PLAN                                             
----------------------------------------------------------------------------------------------------
921 922
 Gather Motion 3:1  (slice2; segments: 3)  (cost=2.38..3.44 rows=4 width=4)
   ->  Hash EXISTS Join  (cost=2.38..3.44 rows=2 width=4)
923
         Hash Cond: subselect_t1.x = "IN_subquery".y
924 925 926
         ->  Seq Scan on subselect_t1  (cost=0.00..1.02 rows=1 width=4)
         ->  Hash  (cost=2.30..2.30 rows=2 width=4)
               ->  Redistribute Motion 3:3  (slice1; segments: 3)  (cost=0.00..2.30 rows=2 width=4)
927
                     Hash Key: "IN_subquery".y
928 929 930
                     ->  Append  (cost=0.00..2.12 rows=2 width=4)
                           ->  Seq Scan on subselect_t2  (cost=0.00..1.03 rows=1 width=4)
                           ->  Seq Scan on subselect_t2  (cost=0.00..1.03 rows=1 width=4)
931
 Settings:  optimizer_segments=3
932 933
 Optimizer status: legacy query optimizer
(12 rows)
934 935 936 937 938

select * from subselect_t1 where x in (select y from subselect_t2 union all select y from subselect_t2);
 x 
---
 1
939
 2
940 941 942
(2 rows)

explain select count(*) from subselect_t1 where x in (select y from subselect_t2);
943 944 945 946 947 948
                                        QUERY PLAN                                        
------------------------------------------------------------------------------------------
 Aggregate  (cost=2.21..2.22 rows=1 width=8)
   ->  Gather Motion 3:1  (slice1; segments: 3)  (cost=2.14..2.19 rows=1 width=8)
         ->  Aggregate  (cost=2.14..2.15 rows=1 width=8)
               ->  Hash EXISTS Join  (cost=1.07..2.13 rows=2 width=0)
949
                     Hash Cond: subselect_t1.x = subselect_t2.y
950 951 952
                     ->  Seq Scan on subselect_t1  (cost=0.00..1.02 rows=1 width=4)
                     ->  Hash  (cost=1.03..1.03 rows=1 width=4)
                           ->  Seq Scan on subselect_t2  (cost=0.00..1.03 rows=1 width=4)
953
 Settings:  optimizer_segments=3
954 955
 Optimizer status: legacy query optimizer
(10 rows)
956 957 958 959 960 961 962 963 964 965 966 967 968

select count(*) from subselect_t1 where x in (select y from subselect_t2);
 count 
-------
     2
(1 row)

-- start_ignore
-- Known_opt_diff: MPP-21351
-- end_ignore
explain select count(*) from subselect_t1 where x in (select y from subselect_t2 union all select y from subselect_t2);
                                                   QUERY PLAN                                                   
----------------------------------------------------------------------------------------------------------------
969 970 971 972
 Aggregate  (cost=3.51..3.52 rows=1 width=8)
   ->  Gather Motion 3:1  (slice2; segments: 3)  (cost=3.45..3.50 rows=1 width=8)
         ->  Aggregate  (cost=3.45..3.46 rows=1 width=8)
               ->  Hash EXISTS Join  (cost=2.38..3.44 rows=2 width=0)
973
                     Hash Cond: subselect_t1.x = "IN_subquery".y
974 975 976
                     ->  Seq Scan on subselect_t1  (cost=0.00..1.02 rows=1 width=4)
                     ->  Hash  (cost=2.30..2.30 rows=2 width=4)
                           ->  Redistribute Motion 3:3  (slice1; segments: 3)  (cost=0.00..2.30 rows=2 width=4)
977
                                 Hash Key: "IN_subquery".y
978 979 980
                                 ->  Append  (cost=0.00..2.12 rows=2 width=4)
                                       ->  Seq Scan on subselect_t2  (cost=0.00..1.03 rows=1 width=4)
                                       ->  Seq Scan on subselect_t2  (cost=0.00..1.03 rows=1 width=4)
981
 Settings:  optimizer_segments=3
982 983
 Optimizer status: legacy query optimizer
(14 rows)
984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055

select count(*) from subselect_t1 where x in (select y from subselect_t2 union all select y from subselect_t2);
 count 
-------
     2
(1 row)

select count(*) from 
       ( select 1 as FIELD_1 union all select 2 as FIELD_1 ) TABLE_1 
       where FIELD_1 in ( select 1 as FIELD_1 union all select 1 as FIELD_1 union all select 1 as FIELD_1 );
 count 
-------
     1
(1 row)

       
---
--- Query was deadlocking because of not squelching subplans (MPP-18936)
---
drop table if exists t1; 
drop table if exists t2; 
drop table if exists t3; 
drop table if exists t4; 
CREATE TABLE t1 AS (SELECT generate_series(1, 5000) AS i, generate_series(5001, 10000) AS j);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column(s) named 'i' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
CREATE TABLE t2 AS (SELECT * FROM t1 WHERE gp_segment_id = 0);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column(s) named 'i' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
CREATE TABLE t3 AS (SELECT * FROM t1 WHERE gp_segment_id = 1);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column(s) named 'i' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
CREATE TABLE t4 (i1 int, i2 int); 
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'i1' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
set gp_interconnect_queue_depth=1;
-- This query was deadlocking on a 2P system
INSERT INTO t4 
(
SELECT t1.i, (SELECT t3.i FROM t3 WHERE t3.i + 1 = t1.i + 1)
FROM t1, t3
WHERE t1.i = t3.i
)
UNION
(
SELECT t1.i, (SELECT t2.i FROM t2 WHERE t2.i + 1 = t1.i + 1)
FROM t1, t2
WHERE t1.i = t2.i
);
drop table if exists t1; 
drop table if exists t2; 
drop table if exists t3; 
drop table if exists t4; 
--
-- Initplans with no corresponding params should be removed MPP-20600
--
drop table if exists t1;
NOTICE:  table "t1" does not exist, skipping
drop table if exists t2;
NOTICE:  table "t2" does not exist, skipping
create table t1(a int);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'a' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
create table t2(b int);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'b' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
select * from t1 where a=1 and a=2 and a > (select t2.b from t2);
 a 
---
(0 rows)

explain select * from t1 where a=1 and a=2 and a > (select t2.b from t2);
1056 1057 1058
                   QUERY PLAN                   
------------------------------------------------
 Result  (cost=1063.00..1063.01 rows=1 width=0)
1059
   One-Time Filter: false
1060
 Settings:  optimizer_segments=3
1061 1062
 Optimizer status: legacy query optimizer
(4 rows)
1063 1064 1065 1066

explain select * from t1 where a=1 and a=2 and a > (select t2.b from t2)
union all
select * from t1 where a=1 and a=2 and a > (select t2.b from t2);
1067 1068 1069 1070
                      QUERY PLAN                      
------------------------------------------------------
 Append  (cost=1063.00..2126.04 rows=1 width=0)
   ->  Result  (cost=1063.00..1063.01 rows=1 width=0)
1071
         One-Time Filter: false
1072
   ->  Result  (cost=1063.00..1063.01 rows=1 width=0)
1073
         One-Time Filter: false
1074
 Settings:  optimizer_segments=3
1075 1076
 Optimizer status: legacy query optimizer
(7 rows)
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087

select * from t1 where a=1 and a=2 and a > (select t2.b from t2)
union all
select * from t1 where a=1 and a=2 and a > (select t2.b from t2);
 a 
---
(0 rows)

explain select * from t1,
(select * from t1 where a=1 and a=2 and a > (select t2.b from t2)) foo
where t1.a = foo.a;
1088 1089 1090 1091
                                           QUERY PLAN                                            
-------------------------------------------------------------------------------------------------
 Gather Motion 1:1  (slice2; segments: 1)  (cost=2607.56..4213.88 rows=6 width=8)
   ->  Result  (cost=2607.56..4213.88 rows=2 width=8)
1092
         One-Time Filter: false
1093
         InitPlan  (slice3)
1094 1095 1096 1097
           ->  Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..1063.00 rows=96300 width=4)
                 ->  Seq Scan on t2  (cost=0.00..1063.00 rows=32100 width=4)
         ->  Nested Loop  (cost=1544.56..3150.88 rows=2 width=8)
               ->  Seq Scan on t1  (cost=0.00..1544.50 rows=19 width=4)
1098
                     Filter: a > $0 AND a = 1
1099 1100 1101
               ->  Materialize  (cost=1544.56..1545.11 rows=19 width=4)
                     ->  Seq Scan on t1  (cost=0.00..1544.50 rows=19 width=4)
                           Filter: a > $0 AND a = 1
1102
 Settings:  optimizer_segments=3
1103 1104
 Optimizer status: legacy query optimizer
(14 rows)
1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115

select * from t1,
(select * from t1 where a=1 and a=2 and a > (select t2.b from t2)) foo
where t1.a = foo.a;
 a | a 
---+---
(0 rows)

drop table if exists t1;
drop table if exists t2;
--
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
-- Test for a bug we used to have with eliminating InitPlans. The subplan,
-- (select max(content) from y), was eliminated when it shouldn't have been.
-- The query is supposed to return 0 rows, but returned > 0 when the bug was
-- present.
--
CREATE TABLE initplan_x (i int4, t text);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'i' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
insert into initplan_x values
 (1, 'foobar1'),
 (2, 'foobar2'),
 (3, 'foobar3'),
 (4, 'foobar4'),
 (5, 'foobar5');
CREATE TABLE initplan_y (content int4);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'content' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
insert into initplan_y values (5);
select i, t from initplan_x
except
select g, t from initplan_x,
                 generate_series(0, (select max(content) from initplan_y)) g
order by 1;
 i | t 
---+---
(0 rows)

drop table if exists initplan_x;
drop table if exists initplan_y;
--
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
-- Test Initplans that return multiple params.
--
create table initplan_test(i int, j int, m int);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'i' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
insert into initplan_test values (1,1,1);
select * from initplan_test where row(j, m) = (select j, m from initplan_test where i = 1);
 i | j | m 
---+---+---
 1 | 1 | 1
(1 row)

drop table initplan_test;
--
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
-- apply parallelization for subplan MPP-24563
--
create table t1_mpp_24563 (id int, value int) distributed by (id);
insert into t1_mpp_24563 values (1, 3);
create table t2_mpp_24563 (id int, value int, seq int) distributed by (id);
insert into t2_mpp_24563 values (1, 7, 5);
explain select row_number() over (order by seq asc) as id, foo.cnt
from
(select seq, (select count(*) from t1_mpp_24563 t1 where t1.id = t2.id) cnt from
	t2_mpp_24563 t2 where value = 7) foo;
1170
                                              QUERY PLAN                                              
1171
------------------------------------------------------------------------------------------------------
1172
 Window  (cost=1.02..2.12 rows=1 width=8)
1173
   Order By: t2.seq
1174
   ->  Gather Motion 3:1  (slice2; segments: 3)  (cost=1.02..1.05 rows=1 width=8)
1175 1176 1177 1178 1179 1180
         Merge Key: t2.seq
         ->  Sort  (cost=1.02..1.03 rows=1 width=8)
               Sort Key: t2.seq
               ->  Seq Scan on t2_mpp_24563 t2  (cost=0.00..1.01 rows=1 width=8)
                     Filter: value = 7
   SubPlan 1
1181
     ->  Aggregate  (cost=1.06..1.07 rows=1 width=8)
1182 1183 1184
           ->  Result  (cost=1.01..1.02 rows=1 width=0)
                 Filter: t1.id = $0
                 ->  Materialize  (cost=1.01..1.02 rows=1 width=0)
1185
                       ->  Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..1.01 rows=1 width=0)
1186 1187
                             ->  Seq Scan on t1_mpp_24563 t1  (cost=0.00..1.01 rows=1 width=0)
 Settings:  optimizer_segments=3
1188 1189
 Optimizer status: legacy query optimizer
(17 rows)
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218

drop table t1_mpp_24563;
drop table t2_mpp_24563;
--
-- MPP-20470 update the flow of node after parallelizing subplan.
--
CREATE TABLE t_mpp_20470 (
    col_date timestamp without time zone,
    col_name character varying(6),
    col_expiry date
) DISTRIBUTED BY (col_date) PARTITION BY RANGE(col_date)
(
START ('2013-05-10 00:00:00'::timestamp without time zone) END ('2013-05-11
	00:00:00'::timestamp without time zone) WITH (tablename='t_mpp_20470_ptr1', appendonly=false ),
START ('2013-05-24 00:00:00'::timestamp without time zone) END ('2013-05-25
	00:00:00'::timestamp without time zone) WITH (tablename='t_mpp_20470_ptr2', appendonly=false )
);
NOTICE:  CREATE TABLE will create partition "t_mpp_20470_ptr1" for table "t_mpp_20470"
NOTICE:  CREATE TABLE will create partition "t_mpp_20470_ptr2" for table "t_mpp_20470"
COPY t_mpp_20470 from STDIN delimiter '|' null '';
create view v1_mpp_20470 as
SELECT
CASE
	WHEN  b.col_name::text = 'FUTCUR'::text
	THEN  ( SELECT count(a.col_expiry) AS count FROM t_mpp_20470 a WHERE
		a.col_name::text = b.col_name::text)::text
	ELSE 'Q2'::text END  AS  cc,  1 AS nn
FROM t_mpp_20470 b;
explain SELECT  cc, sum(nn) over() FROM v1_mpp_20470;
1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231
                                                     QUERY PLAN                                                      
---------------------------------------------------------------------------------------------------------------------
 Nested Loop  (cost=8184.20..128107063.42 rows=93400 width=28)
   ->  Aggregate  (cost=4154.80..4154.81 rows=1 width=0)
         ->  Shared Scan (share slice:id 0:0)  (cost=4029.40..4154.80 rows=93400 width=28)
               ->  Materialize  (cost=3095.40..4029.40 rows=93400 width=28)
                     ->  Gather Motion 3:1  (slice3; segments: 3)  (cost=0.00..3002.00 rows=93400 width=28)
                           ->  Append  (cost=0.00..1134.00 rows=31134 width=28)
                                 ->  Seq Scan on t_mpp_20470_ptr1 b  (cost=0.00..567.00 rows=15567 width=28)
                                 ->  Seq Scan on t_mpp_20470_ptr2 b  (cost=0.00..567.00 rows=15567 width=28)
   ->  Subquery Scan coplan  (cost=4029.40..5088.80 rows=93400 width=28)
         ->  Window  (cost=4029.40..4154.80 rows=93400 width=28)
               ->  Shared Scan (share slice:id 0:0)  (cost=4029.40..4154.80 rows=93400 width=28)
1232
   SubPlan 1
1233 1234 1235 1236
     ->  Aggregate  (cost=1371.47..1371.48 rows=1 width=8)
           ->  Result  (cost=0.00..1367.50 rows=32 width=4)
                 ->  Append  (cost=0.00..1367.50 rows=32 width=4)
                       ->  Result  (cost=683.80..684.26 rows=16 width=4)
1237
                             Filter: a.col_name::text = $0::text
1238 1239 1240 1241
                             ->  Materialize  (cost=683.80..684.26 rows=16 width=4)
                                   ->  Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..683.75 rows=47 width=4)
                                         ->  Seq Scan on t_mpp_20470_ptr1 a  (cost=0.00..683.75 rows=16 width=4)
                       ->  Result  (cost=683.80..684.26 rows=16 width=4)
1242
                             Filter: a.col_name::text = $0::text
1243 1244 1245
                             ->  Materialize  (cost=683.80..684.26 rows=16 width=4)
                                   ->  Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..683.75 rows=47 width=4)
                                         ->  Seq Scan on t_mpp_20470_ptr2 a  (cost=0.00..683.75 rows=16 width=4)
1246
 Settings:  optimizer_segments=3
1247 1248
 Optimizer status: legacy query optimizer
(27 rows)
1249 1250 1251 1252 1253 1254

drop view v1_mpp_20470;
drop table t_mpp_20470;
create table tbl_25484(id int, num int) distributed by (id);
insert into tbl_25484 values(1, 1), (2, 2), (3, 3);
select id from tbl_25484 where 3 = (select 3 where 3 = (select num));
1255
 id 
1256 1257 1258 1259 1260 1261 1262
----
  3
(1 row)

drop table tbl_25484;
reset optimizer_segments;
reset optimizer_nestloop_factor;
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276
--
-- Test case that once triggered a bug in the IN-clause pull-up code.
--
SELECT p.id
    FROM (SELECT * FROM generate_series(1,10) id
          WHERE id IN (
              SELECT 1
              UNION ALL
              SELECT 0)) p;
 id 
----
  1
(1 row)

1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
--
-- Verify another bug in the IN-clause pull-up code. This returned some
-- rows from xsupplier twice, because of a bug in detecting whether a
-- Redistribute node was needed.
--
CREATE TABLE xlineitem (l_orderkey int4, l_suppkey int4) distributed by (l_orderkey);
insert into xlineitem select g+3, g from generate_series(10,100) g;
insert into xlineitem select g+1, g from generate_series(10,100) g;
insert into xlineitem select g, g from generate_series(10,100) g;
CREATE TABLE xsupplier (s_suppkey int4, s_name text) distributed by (s_suppkey);
insert into xsupplier select g, 'foo' || g from generate_series(1,10) g;
select s_name from xsupplier
where s_suppkey in (
  select g.l_suppkey from xlineitem g
) ;
 s_name 
--------
 foo10
(1 row)

1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
--
-- Another case that failed at one point. (A planner bug in pulling up a
-- subquery with constant distribution key, 1, in the outer queries.)
--
create table nested_in_tbl(tc1 int, tc2 int) distributed by (tc1);
select * from nested_in_tbl t1  where tc1 in
  (select 1 from nested_in_tbl t2 where tc1 in
    (select 1 from nested_in_tbl t3 where t3.tc2 = t2.tc2));
 tc1 | tc2 
-----+-----
(0 rows)

drop table nested_in_tbl;
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319
--
-- Window query with a function scan that has non-correlated subquery.
--
SELECT rank() over (partition by min(c) order by min(c)) AS p_rank FROM (SELECT d AS c FROM (values(1)) d1, generate_series(0,(SELECT 2)) AS d) tt GROUP BY c;
 p_rank 
--------
      1
      1
      1
(3 rows)
H
Haisheng Yuan 已提交
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334

--
-- Remove unused subplans
--
create table foo(a int, b int) distributed by (a) partition by range(b) (start(1) end(3) every(1));
NOTICE:  CREATE TABLE will create partition "foo_1_prt_1" for table "foo"
NOTICE:  CREATE TABLE will create partition "foo_1_prt_2" for table "foo"
create table bar(a int, b int) distributed by (a);
with CT as (select a from foo except select a from bar)
select * from foo
where exists (select 1 from CT where CT.a = foo.a);
 a | b 
---+---
(0 rows)

1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
--
-- Multiple SUBPLAN nodes must not refer to same plan_id
--
CREATE TABLE bar_s (c integer, d character varying(10));
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'c' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
INSERT INTO bar_s VALUES (9,9);
SELECT * FROM bar_s T1 WHERE c = (SELECT max(c) FROM bar_s T2 WHERE T2.d = T1.d GROUP BY c) AND c < 10;
 c | d
---+---
 9 | 9
(1 row)

1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362
CREATE TABLE foo_s (a integer, b integer)  PARTITION BY RANGE(b)
    (PARTITION sub_one START (1) END (10),
     PARTITION sub_two START (11) END (22));
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'a' as the Greenplum Database data distribution key for this table.
HINT:  The 'DISTRIBUTED BY' clause determines the distribution of data. Make sure column(s) chosen are the optimal data distribution key to minimize skew.
NOTICE:  CREATE TABLE will create partition "foo_s_1_prt_sub_one" for table "foo_s"
NOTICE:  CREATE TABLE will create partition "foo_s_1_prt_sub_two" for table "foo_s"
INSERT INTO foo_s VALUES (9,9);
INSERT INTO foo_s VALUES (2,9);
SELECT bar_s.c from bar_s, foo_s WHERE foo_s.a=2 AND foo_s.b = (SELECT max(b) FROM foo_s WHERE bar_s.c = 9);
 c 
---
 9
(1 row)

1363
DROP TABLE bar_s;
1364
DROP TABLE foo_s;