notin_optimizer.out 52.2 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
--
-- NOTIN 
-- Test NOTIN clauses
--
create schema notin;
set search_path=notin;
--
-- generate a bunch of tables
--
create table t1 (
	c1 integer
);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'c1' 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 (
	c2 integer
);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'c2' 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 (
	c3 integer
);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'c3' 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 (
	c4 integer
);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'c4' 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 t1n (
	c1n integer
);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'c1n' 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 g1 (
	a integer,
	b integer,
	c integer
);
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 l1 (
	w integer,
	x integer,
	y integer,
	z integer
);
NOTICE:  Table doesn't have 'DISTRIBUTED BY' clause -- Using column named 'w' 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.
--
-- stick in some values
--
insert into t1 values (generate_series (1,10));
insert into t2 values (generate_series (1,5));
insert into t3 values (1), (2), (3);
insert into t4 values (1), (2);
insert into t1n values (1), (2), (3), (null), (5), (6), (7);
insert into g1 values
  (1,1,1),
  (1,1,2),
  (1,2,2),
  (2,2,2),
  (2,2,3),
  (2,3,3),
  (3,3,3),
  (3,3,3),
  (3,3,4),
  (3,4,4),
  (4,4,4);
insert into l1 values (generate_series (1,10), generate_series (1,10), generate_series (1,10), generate_series (1,10));
--
-- queries
--
--
--q1
--
explain select c1 from t1 where c1 not in 
	(select c2 from t2);
                                            QUERY PLAN                                             
---------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..862.00 rows=4 width=4)
82
   ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..862.00 rows=2 width=4)
83
         Hash Cond: t1.c1 = t2.c2
84
         ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
85 86
         ->  Hash  (cost=431.00..431.00 rows=5 width=4)
               ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=5 width=4)
87
                     ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(9 rows)

select c1 from t1 where c1 not in 
	(select c2 from t2);
 c1 
----
  8
  9
 10
  6
  7
(5 rows)

--
--q2
--
explain select c1 from t1 where c1 not in 
	(select c2 from t2 where c2 > 2 and c2 not in 
		(select c3 from t3));
                                                     QUERY PLAN                                                      
---------------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice3; segments: 3)  (cost=0.00..1293.00 rows=4 width=4)
112
   ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..1293.00 rows=2 width=4)
113
         Hash Cond: t1.c1 = t2.c2
114
         ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
115 116
         ->  Hash  (cost=862.00..862.00 rows=1 width=4)
               ->  Broadcast Motion 3:3  (slice2; segments: 3)  (cost=0.00..862.00 rows=1 width=4)
117
                     ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..862.00 rows=1 width=4)
118
                           Hash Cond: t2.c2 = t3.c3
119
                           ->  Seq Scan on t2  (cost=0.00..431.00 rows=1 width=4)
120 121 122
                                 Filter: c2 > 2
                           ->  Hash  (cost=431.00..431.00 rows=3 width=4)
                                 ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=3 width=4)
123
                                       ->  Seq Scan on t3  (cost=0.00..431.00 rows=1 width=4)
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(15 rows)

select c1 from t1 where c1 not in 
	(select c2 from t2 where c2 > 2 and c2 not in 
		(select c3 from t3));
 c1 
----
  3
  6
  7
  1
  2
  8
  9
 10
(8 rows)

	
--
--q3
--
explain select c1 from t1 where c1 not in 
	(select c2 from t2 where c2 not in 
		(select c3 from t3 where c3 not in 
			(select c4 from t4)));
                                                              QUERY PLAN                                                               
---------------------------------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice4; segments: 3)  (cost=0.00..1724.00 rows=4 width=4)
154
   ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..1724.00 rows=2 width=4)
155
         Hash Cond: t1.c1 = t2.c2
156
         ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
157 158
         ->  Hash  (cost=1293.00..1293.00 rows=2 width=4)
               ->  Broadcast Motion 3:3  (slice3; segments: 3)  (cost=0.00..1293.00 rows=2 width=4)
159
                     ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..1293.00 rows=1 width=4)
160
                           Hash Cond: t2.c2 = t3.c3
161
                           ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
162 163
                           ->  Hash  (cost=862.00..862.00 rows=2 width=4)
                                 ->  Broadcast Motion 3:3  (slice2; segments: 3)  (cost=0.00..862.00 rows=2 width=4)
164
                                       ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..862.00 rows=1 width=4)
165
                                             Hash Cond: t3.c3 = t4.c4
166
                                             ->  Seq Scan on t3  (cost=0.00..431.00 rows=1 width=4)
167 168
                                             ->  Hash  (cost=431.00..431.00 rows=2 width=4)
                                                   ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=2 width=4)
169
                                                         ->  Seq Scan on t4  (cost=0.00..431.00 rows=1 width=4)
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(19 rows)

select c1 from t1 where c1 not in 
	(select c2 from t2 where c2 not in 
		(select c3 from t3 where c3 not in 
			(select c4 from t4)));
 c1 
----
  3
  6
  7
  8
  9
 10
(6 rows)

--
--q4
--
explain select c1 from t1, 
(select c2 from t2 where c2 not in 
	(select c3 from t3)) foo 
	where c1 = foo.c2;
                                                  QUERY PLAN                                                   
---------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..1293.00 rows=2 width=4)
   ->  Hash Join  (cost=0.00..1293.00 rows=1 width=4)
         Hash Cond: t1.c1 = t2.c2
200
         ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
201
         ->  Hash  (cost=862.00..862.00 rows=1 width=4)
202
               ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..862.00 rows=1 width=4)
203
                     Hash Cond: t2.c2 = t3.c3
204
                     ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
205 206
                     ->  Hash  (cost=431.00..431.00 rows=3 width=4)
                           ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=3 width=4)
207
                                 ->  Seq Scan on t3  (cost=0.00..431.00 rows=1 width=4)
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(13 rows)

select c1 from t1, 
(select c2 from t2 where c2 not in 
	(select c3 from t3)) foo 
	where c1 = foo.c2;
 c1 
----
  4
  5
(2 rows)

--
--q5
--
explain select c1 from t1, 
(select c2 from t2 where c2 not in 
	(select c3 from t3) and c2 > 4) foo 
	where c1 = foo.c2;
                                        QUERY PLAN                                        
------------------------------------------------------------------------------------------
231
 Hash Left Anti Semi (Not-In) Join  (cost=0.00..1293.00 rows=1 width=4)
232 233 234 235
   Hash Cond: t2.c2 = t3.c3
   ->  Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..862.00 rows=1 width=8)
         ->  Hash Join  (cost=0.00..862.00 rows=1 width=8)
               Hash Cond: t1.c1 = t2.c2
236
               ->  Seq Scan on t1  (cost=0.00..431.00 rows=2 width=4)
237 238
                     Filter: c1 > 4
               ->  Hash  (cost=431.00..431.00 rows=1 width=4)
239
                     ->  Seq Scan on t2  (cost=0.00..431.00 rows=1 width=4)
240 241 242
                           Filter: c2 > 4
   ->  Hash  (cost=431.00..431.00 rows=1 width=4)
         ->  Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..431.00 rows=3 width=4)
243
               ->  Seq Scan on t3  (cost=0.00..431.00 rows=1 width=4)
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(15 rows)

select c1 from t1, 
(select c2 from t2 where c2 not in 
	(select c3 from t3) and c2 > 4) foo 
	where c1 = foo.c2;
 c1 
----
  5
(1 row)

--
--q6
--
explain select c1 from t1 where c1 not in 
	(select c2 from t2) and c1 > 1;
                                            QUERY PLAN                                             
---------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..862.00 rows=4 width=4)
265
   ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..862.00 rows=2 width=4)
266
         Hash Cond: t1.c1 = t2.c2
267
         ->  Seq Scan on t1  (cost=0.00..431.00 rows=3 width=4)
268 269 270
               Filter: c1 > 1
         ->  Hash  (cost=431.00..431.00 rows=5 width=4)
               ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=5 width=4)
271
                     ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(10 rows)

select c1 from t1 where c1 not in 
	(select c2 from t2) and c1 > 1;
 c1 
----
  8
  9
 10
  6
  7
(5 rows)

--
--q7
--
explain select c1 from t1 where c1 > 6 and c1 not in 
	(select c2 from t2) and c1 < 10;
                                            QUERY PLAN                                             
---------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..862.00 rows=2 width=4)
295
   ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..862.00 rows=1 width=4)
296
         Hash Cond: t1.c1 = t2.c2
297
         ->  Seq Scan on t1  (cost=0.00..431.00 rows=2 width=4)
298 299 300
               Filter: c1 > 6 AND c1 < 10
         ->  Hash  (cost=431.00..431.00 rows=5 width=4)
               ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=5 width=4)
301
                     ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(10 rows)

select c1 from t1 where c1 > 6 and c1 not in 
	(select c2 from t2) and c1 < 10;
 c1 
----
  7
  8
  9
(3 rows)

--
--q8 introduce join
--
explain select c1 from t1,t2 where c1 not in 
	(select c3 from t3) and c1 = c2;
                                                  QUERY PLAN                                                   
---------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..1293.00 rows=2 width=4)
   ->  Hash Join  (cost=0.00..1293.00 rows=1 width=4)
         Hash Cond: t2.c2 = t1.c1
325
         ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
326
         ->  Hash  (cost=862.00..862.00 rows=2 width=4)
327
               ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..862.00 rows=2 width=4)
328
                     Hash Cond: t1.c1 = t3.c3
329
                     ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
330 331
                     ->  Hash  (cost=431.00..431.00 rows=3 width=4)
                           ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=3 width=4)
332
                                 ->  Seq Scan on t3  (cost=0.00..431.00 rows=1 width=4)
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(13 rows)

select c1 from t1,t2 where c1 not in 
	(select c3 from t3) and c1 = c2;
 c1 
----
  4
  5
(2 rows)

--
--q9
--
select c1 from t1 where c1 not in 
	(select c2 from t2 where c2 > 2 and c2 < 5);
 c1 
----
  1
  2
  5
  6
  7
  8
  9
 10
(8 rows)

--
--q10
--
select count(c1) from t1 where c1 not in 
	(select sum(c2) from t2);
 count 
-------
    10
(1 row)

--
--q11
--
select c1 from t1 where c1 not in 
	(select count(*) from t1);
 c1 
----
  3
  4
  5
  6
  7
  1
  2
  8
  9
(9 rows)

--
--q12
--
select a,b from g1 where (a,b) not in
	(select a,b from g1);
 a | b 
---+---
(0 rows)

--
--q13
--
explain select x,y from l1 where (x,y) not in
	(select distinct y, sum(x) from l1 group by y having y < 4 order by y) order by 1,2;
404 405
                                                         QUERY PLAN                                                          
-----------------------------------------------------------------------------------------------------------------------------
406 407 408 409 410 411
 Gather Motion 3:1  (slice3; segments: 3)  (cost=10000000008.02..10000000008.03 rows=4 width=8)
   Merge Key: l1.x, l1.y
   ->  Sort  (cost=10000000008.02..10000000008.03 rows=2 width=8)
         Sort Key: l1.x, l1.y
         ->  Nested Loop Left Anti Semi (Not-In) Join  (cost=10000000003.25..10000000007.99 rows=2 width=8)
               Join Filter: l1.x = "NotIn_SUBQUERY".y AND l1.y = "NotIn_SUBQUERY".sum
412
               ->  Seq Scan on l1  (cost=0.00..3.10 rows=4 width=8)
413
               ->  Materialize  (cost=3.25..3.47 rows=3 width=12)
414
                     ->  Broadcast Motion 3:3  (slice2; segments: 3)  (cost=3.25..3.43 rows=3 width=12)
415
                           ->  Subquery Scan on "NotIn_SUBQUERY"  (cost=3.25..3.31 rows=1 width=12)
416
                                 ->  HashAggregate  (cost=3.25..3.28 rows=1 width=16)
417
                                       Group Key: l1_1.y
418
                                       ->  Redistribute Motion 3:3  (slice1; segments: 3)  (cost=3.14..3.20 rows=1 width=12)
419
                                             Hash Key: l1_1.y
420
                                             ->  HashAggregate  (cost=3.14..3.14 rows=1 width=12)
421 422
                                                   Group Key: l1_1.y
                                                   ->  Seq Scan on l1 l1_1  (cost=0.00..3.12 rows=2 width=8)
423
                                                         Filter: y < 4
424
 Optimizer: Postgres query optimizer
425
(19 rows)
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

select x,y from l1 where (x,y) not in
	(select distinct y, sum(x) from l1 group by y having y < 4 order by y) order by 1,2;
 x  | y  
----+----
  4 |  4
  5 |  5
  6 |  6
  7 |  7
  8 |  8
  9 |  9
 10 | 10
(7 rows)

--
--q14
--
explain select * from g1 where (a,b,c) not in 
	(select x,y,z from l1);
445 446 447
                                            QUERY PLAN                                             
---------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=3.53..14.71 rows=11 width=12)
448
   ->  Nested Loop Left Anti Semi (Not-In) Join  (cost=3.53..14.71 rows=4 width=12)
449
         Join Filter: g1.a = l1.x AND g1.b = l1.y AND g1.c = l1.z
450
         ->  Seq Scan on g1  (cost=0.00..2.11 rows=4 width=12)
451 452 453
         ->  Materialize  (cost=3.53..3.83 rows=10 width=12)
               ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..3.50 rows=10 width=12)
                     ->  Seq Scan on l1  (cost=0.00..3.10 rows=4 width=12)
454
 Optimizer status: Postgres query optimizer
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
(10 rows)

select * from g1 where (a,b,c) not in 
	(select x,y,z from l1);
 a | b | c 
---+---+---
 3 | 3 | 4
 3 | 4 | 4
 1 | 1 | 2
 1 | 2 | 2
 2 | 2 | 3
 2 | 3 | 3
(6 rows)

--
--q15
--
explain select c1 from t1, t2 where c1 not in 
	(select c3 from t3 where c3 = c1) and c1 = c2;
                                                                                                                                                                  QUERY PLAN                                                                                                                                                                  
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..1324465.54 rows=6 width=4)
   ->  Hash Join  (cost=0.00..1324465.54 rows=2 width=4)
         Hash Cond: t1.c1 = t2.c2
479
         ->  Seq Scan on t1  (cost=0.00..1324034.54 rows=4 width=4)
480 481 482 483 484 485 486 487 488 489 490 491
               Filter: (subplan)
               SubPlan 1
                 ->  Result  (cost=0.00..431.00 rows=1 width=1)
                       ->  Result  (cost=0.00..431.00 rows=1 width=1)
                             Filter: (CASE WHEN (sum((CASE WHEN $0 = t3.c3 THEN 1 ELSE 0 END))) IS NULL THEN true WHEN (sum((CASE WHEN t3.c3 IS NULL THEN 1 ELSE 0 END))) > 0::bigint THEN NULL::boolean WHEN $0 IS NULL THEN NULL::boolean WHEN (sum((CASE WHEN $0 = t3.c3 THEN 1 ELSE 0 END))) = 0::bigint THEN true ELSE false END) = true
                             ->  Result  (cost=0.00..431.00 rows=1 width=1)
                                   ->  Aggregate  (cost=0.00..431.00 rows=1 width=16)
                                         ->  Result  (cost=0.00..431.00 rows=1 width=8)
                                               ->  Result  (cost=0.00..431.00 rows=1 width=4)
                                                     Filter: t3.c3 = $0
                                                     ->  Materialize  (cost=0.00..431.00 rows=3 width=4)
                                                           ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=3 width=4)
492
                                                                 ->  Seq Scan on t3  (cost=0.00..431.00 rows=1 width=4)
493
         ->  Hash  (cost=431.00..431.00 rows=2 width=4)
494
               ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(21 rows)

select c1 from t1, t2 where c1 not in 
	(select c3 from t3 where c3 = c1) and c1 = c2;
 c1 
----
  4
  5
(2 rows)

--
--q17
-- null test
--
select c1 from t1 where c1 not in 
	(select c1n from t1n);
 c1 
----
(0 rows)

--
--q18
-- null test
--
select c1 from t1 where c1 not in 
	(select c2 from t2 where c2 not in 
		(select c3 from t3 where c3 not in 
			(select c1n from t1n)));
 c1 
----
  8
  9
 10
  6
  7
(5 rows)

--
--q19
--
select c1 from t1 join t2 on c1 = c2 where c1 not in 
	(select c3 from t3);
 c1 
----
  4
  5
(2 rows)

--
--q20
--
explain select c1 from t1 where c1 not in 
	(select sum(c2) as s from t2 where c2 > 2 group by c2 having c2 > 3);
                                            QUERY PLAN                                             
---------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..862.00 rows=1 width=4)
553
   ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..862.00 rows=1 width=4)
554
         Hash Cond: t1.c1::bigint = (sum(t2.c2))
555
         ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
556 557 558 559 560 561 562
         ->  Hash  (cost=431.00..431.00 rows=2 width=8)
               ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=2 width=8)
                     ->  Result  (cost=0.00..431.00 rows=1 width=8)
                           ->  GroupAggregate  (cost=0.00..431.00 rows=1 width=8)
                                 Group By: t2.c2
                                 ->  Sort  (cost=0.00..431.00 rows=1 width=4)
                                       Sort Key: t2.c2
563
                                       ->  Seq Scan on t2  (cost=0.00..431.00 rows=1 width=4)
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
                                             Filter: c2 > 2 AND c2 > 3
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(15 rows)

select c1 from t1 where c1 not in 
	(select sum(c2) as s from t2 where c2 > 2 group by c2 having c2 > 3);
 c1 
----
  1
  2
  3
  6
  7
  8
  9
 10
(8 rows)

--
--q21
-- multiple not in in where clause
--
select c1 from t1 where c1 not in 
	(select c2 from t2) and c1 not in 
	(select c3 from t3);
 c1 
----
  6
  7
  8
  9
 10
(5 rows)

--
--q22
-- coexist with joins
--
select c1 from t1,t3,t2 where c1 not in 
	(select c4 from t4) and c1 = c3 and c1 = c2;
 c1 
----
  3
(1 row)

--
--q23
-- union in subselect
--
select c1 from t1 where c1 not in 
	(select c2 from t2 union select c3 from t3);
 c1 
----
  8
  9
 10
  6
  7
(5 rows)

--
--q24
--
select c1 from t1 where c1 not in 
	(select c2 from t2 union all select c3 from t3);
 c1 
----
  6
  7
  8
  9
 10
(5 rows)

--
--q25
--
select c1 from t1 where c1 not in 
	(select (case when c1n is null then 1 else c1n end) as c1n from t1n);
 c1 
----
  8
  9
 10
  4
(4 rows)

--
--q26
--
explain select (case when c1%2 = 0 
 then (select sum(c2) from t2 where c2 not in (select c3 from t3)) 
 else (select sum(c3) from t3 where c3 not in (select c4 from t4)) end) as foo from t1;
                                                                    QUERY PLAN                                                                     
---------------------------------------------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice7; segments: 3)  (cost=0.00..1809071284.19 rows=10 width=8)
   ->  Result  (cost=0.00..1809071284.19 rows=4 width=8)
         ->  Nested Loop Left Join  (cost=0.00..1809071284.19 rows=14 width=20)
               Join Filter: true
               ->  Nested Loop Left Join  (cost=0.00..1765378.17 rows=7 width=12)
                     Join Filter: true
666
                     ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
667 668 669 670 671
                     ->  Materialize  (cost=0.00..862.00 rows=1 width=8)
                           ->  Broadcast Motion 1:3  (slice6)  (cost=0.00..862.00 rows=3 width=8)
                                 ->  Aggregate  (cost=0.00..862.00 rows=1 width=8)
                                       ->  Gather Motion 3:1  (slice5; segments: 3)  (cost=0.00..862.00 rows=1 width=8)
                                             ->  Aggregate  (cost=0.00..862.00 rows=1 width=8)
672
                                                   ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..862.00 rows=1 width=4)
673
                                                         Hash Cond: t2.c2 = t3_1.c3
674
                                                         ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
675 676
                                                         ->  Hash  (cost=431.00..431.00 rows=3 width=4)
                                                               ->  Broadcast Motion 3:3  (slice4; segments: 3)  (cost=0.00..431.00 rows=3 width=4)
677
                                                                     ->  Seq Scan on t3 t3_1  (cost=0.00..431.00 rows=1 width=4)
678 679 680 681 682
               ->  Materialize  (cost=0.00..862.00 rows=1 width=8)
                     ->  Broadcast Motion 1:3  (slice3)  (cost=0.00..862.00 rows=3 width=8)
                           ->  Aggregate  (cost=0.00..862.00 rows=1 width=8)
                                 ->  Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..862.00 rows=1 width=8)
                                       ->  Aggregate  (cost=0.00..862.00 rows=1 width=8)
683
                                             ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..862.00 rows=1 width=4)
684
                                                   Hash Cond: t3.c3 = t4.c4
685
                                                   ->  Seq Scan on t3  (cost=0.00..431.00 rows=1 width=4)
686 687
                                                   ->  Hash  (cost=431.00..431.00 rows=2 width=4)
                                                         ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=2 width=4)
688
                                                               ->  Seq Scan on t4  (cost=0.00..431.00 rows=1 width=4)
689 690
 Optimizer: PQO version 2.74.0
(30 rows)
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717

select (case when c1%2 = 0 
 then (select sum(c2) from t2 where c2 not in (select c3 from t3)) 
 else (select sum(c3) from t3 where c3 not in (select c4 from t4)) end) as foo from t1;
 foo 
-----
   3
   9
   9
   3
   9
   3
   9
   3
   9
   3
(10 rows)

--
--q27
--
explain select c1 from t1 where not c1 >= some (select c2 from t2);
                                                                                                                                                             QUERY PLAN                                                                                                                                                             
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Result  (cost=0.00..1324032.88 rows=2 width=4)
   Filter: (subplan)
   ->  Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..431.00 rows=10 width=4)
718
         ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
719 720 721 722 723 724 725 726 727
   SubPlan 1
     ->  Result  (cost=0.00..431.00 rows=1 width=1)
           ->  Result  (cost=0.00..431.00 rows=1 width=1)
                 Filter: (CASE WHEN (sum((CASE WHEN $0 >= t2.c2 THEN 1 ELSE 0 END))) IS NULL THEN true WHEN (sum((CASE WHEN t2.c2 IS NULL THEN 1 ELSE 0 END))) > 0::bigint THEN NULL::boolean WHEN $0 IS NULL THEN NULL::boolean WHEN (sum((CASE WHEN $0 >= t2.c2 THEN 1 ELSE 0 END))) = 0::bigint THEN true ELSE false END) = true
                 ->  Result  (cost=0.00..431.00 rows=1 width=1)
                       ->  Aggregate  (cost=0.00..431.00 rows=1 width=16)
                             ->  Result  (cost=0.00..431.00 rows=2 width=8)
                                   ->  Materialize  (cost=0.00..431.00 rows=2 width=4)
                                         ->  Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..431.00 rows=5 width=4)
728
                                               ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(16 rows)

select c1 from t1 where not c1 >= some (select c2 from t2);
 c1 
----
(0 rows)

--
--q28
--
explain select c2 from t2 where not c2 < all (select c2 from t2);
                                             QUERY PLAN                                              
-----------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..1324033.39 rows=5 width=4)
745
   ->  Seq Scan on t2  (cost=0.00..1324033.39 rows=2 width=4)
746 747
         Filter: (SubPlan 1)
         SubPlan 1  (slice2; segments: 3)
748 749
           ->  Materialize  (cost=0.00..431.00 rows=5 width=4)
                 ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=5 width=4)
750
                       ->  Seq Scan on t2 t2_1  (cost=0.00..431.00 rows=2 width=4)
751 752
 Optimizer: PQO version 2.74.0
(8 rows)
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772

select c2 from t2 where not c2 < all (select c2 from t2);
 c2 
----
  1
  2
  3
  4
  5
(5 rows)

--
--q29
--
explain select c3 from t3 where not c3 <> any (select c4 from t4);
                                                                                                                                                             QUERY PLAN                                                                                                                                                             
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Result  (cost=0.00..1324032.31 rows=1 width=4)
   Filter: (subplan)
   ->  Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..431.00 rows=3 width=4)
773
         ->  Seq Scan on t3  (cost=0.00..431.00 rows=1 width=4)
774 775 776 777 778 779 780 781 782
   SubPlan 1
     ->  Result  (cost=0.00..431.00 rows=1 width=1)
           ->  Result  (cost=0.00..431.00 rows=1 width=1)
                 Filter: (CASE WHEN (sum((CASE WHEN $0 <> t4.c4 THEN 1 ELSE 0 END))) IS NULL THEN true WHEN (sum((CASE WHEN t4.c4 IS NULL THEN 1 ELSE 0 END))) > 0::bigint THEN NULL::boolean WHEN $0 IS NULL THEN NULL::boolean WHEN (sum((CASE WHEN $0 <> t4.c4 THEN 1 ELSE 0 END))) = 0::bigint THEN true ELSE false END) = true
                 ->  Result  (cost=0.00..431.00 rows=1 width=1)
                       ->  Aggregate  (cost=0.00..431.00 rows=1 width=16)
                             ->  Result  (cost=0.00..431.00 rows=1 width=8)
                                   ->  Materialize  (cost=0.00..431.00 rows=1 width=4)
                                         ->  Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..431.00 rows=2 width=4)
783
                                               ->  Seq Scan on t4  (cost=0.00..431.00 rows=1 width=4)
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(16 rows)

select c3 from t3 where not c3 <> any (select c4 from t4);
 c3 
----
(0 rows)

--
--q31
--
explain select c1 from t1 where c1 not in (select c2 from t2 order by c2 limit 3) order by c1;
                                                    QUERY PLAN                                                    
------------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice3; segments: 3)  (cost=0.00..862.00 rows=4 width=4)
   Merge Key: t1.c1
   ->  Sort  (cost=0.00..862.00 rows=2 width=4)
         Sort Key: t1.c1
803
         ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..862.00 rows=2 width=4)
804
               Hash Cond: t1.c1 = t2.c2
805
               ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
806 807 808 809 810 811 812 813
               ->  Hash  (cost=431.00..431.00 rows=3 width=4)
                     ->  Broadcast Motion 1:3  (slice2)  (cost=0.00..431.00 rows=9 width=4)
                           ->  Limit  (cost=0.00..431.00 rows=1 width=4)
                                 ->  Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..431.00 rows=3 width=4)
                                       Merge Key: t2.c2
                                       ->  Limit  (cost=0.00..431.00 rows=1 width=4)
                                             ->  Sort  (cost=0.00..431.00 rows=2 width=4)
                                                   Sort Key: t2.c2
814
                                                   ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(18 rows)

select c1 from t1 where c1 not in (select c2 from t2 order by c2 limit 3) order by c1;
 c1 
----
  4
  5
  6
  7
  8
  9
 10
(7 rows)

--quantified/correlated subqueries
--
--q32
--
explain select c1 from t1 where c1 =all (select c2 from t2 where c2 > -1 and c2 <= 1);
                                                                                                                                                                QUERY PLAN                                                                                                                                                                
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..1324032.79 rows=4 width=4)
839
   ->  Seq Scan on t1  (cost=0.00..1324032.79 rows=2 width=4)
840 841 842 843 844 845 846 847 848 849
         Filter: (subplan)
         SubPlan 1
           ->  Result  (cost=0.00..431.00 rows=1 width=1)
                 ->  Result  (cost=0.00..431.00 rows=1 width=1)
                       Filter: (CASE WHEN (sum((CASE WHEN $0 <> t2.c2 THEN 1 ELSE 0 END))) IS NULL THEN true WHEN (sum((CASE WHEN t2.c2 IS NULL THEN 1 ELSE 0 END))) > 0::bigint THEN NULL::boolean WHEN $0 IS NULL THEN NULL::boolean WHEN (sum((CASE WHEN $0 <> t2.c2 THEN 1 ELSE 0 END))) = 0::bigint THEN true ELSE false END) = true
                       ->  Result  (cost=0.00..431.00 rows=1 width=1)
                             ->  Aggregate  (cost=0.00..431.00 rows=1 width=16)
                                   ->  Result  (cost=0.00..431.00 rows=1 width=8)
                                         ->  Materialize  (cost=0.00..431.00 rows=1 width=4)
                                               ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=1 width=4)
850
                                                     ->  Seq Scan on t2  (cost=0.00..431.00 rows=1 width=4)
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868
                                                           Filter: c2 > (-1) AND c2 <= 1
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(16 rows)

select c1 from t1 where c1 =all (select c2 from t2 where c2 > -1 and c2 <= 1);
 c1 
----
  1
(1 row)

--
--q33
--
explain select c1 from t1 where c1 <>all (select c2 from t2);
                                            QUERY PLAN                                             
---------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..862.00 rows=4 width=4)
869
   ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..862.00 rows=2 width=4)
870
         Hash Cond: t1.c1 = t2.c2
871
         ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
872 873
         ->  Hash  (cost=431.00..431.00 rows=5 width=4)
               ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=5 width=4)
874
                     ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(9 rows)

select c1 from t1 where c1 <>all (select c2 from t2);
 c1 
----
  6
  7
  8
  9
 10
(5 rows)

--
--q34
--
explain select c1 from t1 where c1 <=all (select c2 from t2 where c2 not in (select c1n from t1n));
                                                                                                                                                               QUERY PLAN                                                                                                                                                               
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice3; segments: 3)  (cost=0.00..1765379.01 rows=4 width=4)
896
   ->  Seq Scan on t1  (cost=0.00..1765379.01 rows=2 width=4)
897 898 899 900 901 902 903 904 905 906
         Filter: (subplan)
         SubPlan 1
           ->  Result  (cost=0.00..862.00 rows=1 width=1)
                 ->  Result  (cost=0.00..862.00 rows=1 width=1)
                       Filter: (CASE WHEN (sum((CASE WHEN $0 > t2.c2 THEN 1 ELSE 0 END))) IS NULL THEN true WHEN (sum((CASE WHEN t2.c2 IS NULL THEN 1 ELSE 0 END))) > 0::bigint THEN NULL::boolean WHEN $0 IS NULL THEN NULL::boolean WHEN (sum((CASE WHEN $0 > t2.c2 THEN 1 ELSE 0 END))) = 0::bigint THEN true ELSE false END) = true
                       ->  Result  (cost=0.00..862.00 rows=1 width=1)
                             ->  Aggregate  (cost=0.00..862.00 rows=1 width=16)
                                   ->  Result  (cost=0.00..862.00 rows=2 width=8)
                                         ->  Materialize  (cost=0.00..862.00 rows=2 width=4)
                                               ->  Broadcast Motion 3:3  (slice2; segments: 3)  (cost=0.00..862.00 rows=2 width=4)
907
                                                     ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..862.00 rows=1 width=4)
908
                                                           Hash Cond: t2.c2 = t1n.c1n
909
                                                           ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
910 911
                                                           ->  Hash  (cost=431.00..431.00 rows=7 width=4)
                                                                 ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=7 width=4)
912
                                                                       ->  Seq Scan on t1n  (cost=0.00..431.00 rows=3 width=4)
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(20 rows)

select c1 from t1 where c1 <=all (select c2 from t2 where c2 not in (select c1n from t1n));
 c1 
----
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
(10 rows)

--
--q35
--
explain select c1 from t1 where not c1 =all (select c2 from t2 where not c2 >all (select c3 from t3));
                                                       QUERY PLAN                                                        
-------------------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice3; segments: 3)  (cost=0.00..1356692471.20 rows=10 width=4)
939
   ->  Seq Scan on t1  (cost=0.00..1356692471.20 rows=4 width=4)
940 941 942 943
         Filter: (subplan)
         SubPlan 2
           ->  Materialize  (cost=0.00..1324032.99 rows=5 width=4)
                 ->  Broadcast Motion 3:3  (slice2; segments: 3)  (cost=0.00..1324032.99 rows=5 width=4)
944
                       ->  Seq Scan on t2  (cost=0.00..1324032.99 rows=2 width=4)
945 946 947 948
                             Filter: (subplan)
                             SubPlan 1
                               ->  Materialize  (cost=0.00..431.00 rows=3 width=4)
                                     ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=3 width=4)
949
                                           ->  Seq Scan on t3  (cost=0.00..431.00 rows=1 width=4)
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(14 rows)

select c1 from t1 where not c1 =all (select c2 from t2 where not c2 >all (select c3 from t3));
 c1 
----
  3
  4
  5
  6
  7
  1
  2
  8
  9
 10
(10 rows)

--
--q36
--
explain select c1 from t1 where not c1 <>all (select c1n from t1n where c1n <all (select c3 from t3 where c3 = c1n));
                                                                                                                                                                      QUERY PLAN                                                                                                                                                                      
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..1324464.88 rows=8 width=4)
976
   ->  Hash Semi Join  (cost=0.00..1324464.88 rows=3 width=4)
977
         Hash Cond: t1.c1 = t1n.c1n
978
         ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
979
         ->  Hash  (cost=1324033.87..1324033.87 rows=3 width=4)
980
               ->  Seq Scan on t1n  (cost=0.00..1324033.87 rows=3 width=4)
981 982 983 984 985 986 987 988 989 990 991 992
                     Filter: (subplan)
                     SubPlan 1
                       ->  Result  (cost=0.00..431.00 rows=1 width=1)
                             ->  Result  (cost=0.00..431.00 rows=1 width=1)
                                   Filter: (CASE WHEN (sum((CASE WHEN $0 >= t3.c3 THEN 1 ELSE 0 END))) IS NULL THEN true WHEN (sum((CASE WHEN t3.c3 IS NULL THEN 1 ELSE 0 END))) > 0::bigint THEN NULL::boolean WHEN $0 IS NULL THEN NULL::boolean WHEN (sum((CASE WHEN $0 >= t3.c3 THEN 1 ELSE 0 END))) = 0::bigint THEN true ELSE false END) = true
                                   ->  Result  (cost=0.00..431.00 rows=1 width=1)
                                         ->  Aggregate  (cost=0.00..431.00 rows=1 width=16)
                                               ->  Result  (cost=0.00..431.00 rows=1 width=8)
                                                     ->  Result  (cost=0.00..431.00 rows=1 width=4)
                                                           Filter: t3.c3 = $0
                                                           ->  Materialize  (cost=0.00..431.00 rows=3 width=4)
                                                                 ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=3 width=4)
993
                                                                       ->  Seq Scan on t3  (cost=0.00..431.00 rows=1 width=4)
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(21 rows)

select c1 from t1 where not c1 <>all (select c1n from t1n where c1n <all (select c3 from t3 where c3 = c1n));
 c1 
----
  5
  6
  7
(3 rows)

--
--q37
--
explain select c1 from t1 where not c1 >=all (select c2 from t2 where c2 = c1);
                                  QUERY PLAN                                  
------------------------------------------------------------------------------
 Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..862.00 rows=6 width=4)
1013
   ->  Hash Semi Join  (cost=0.00..862.00 rows=2 width=4)
1014 1015
         Hash Cond: t1.c1 = t2.c2
         Join Filter: t1.c1 < t2.c2
1016
         ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
1017
         ->  Hash  (cost=431.00..431.00 rows=2 width=4)
1018
               ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(9 rows)

select c1 from t1 where not c1 >=all (select c2 from t2 where c2 = c1);
 c1 
----
(0 rows)

--
--q38
--
explain select c1 from t1 where not exists (select c2 from t2 where c2 = c1);
                                  QUERY PLAN                                  
------------------------------------------------------------------------------
 Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..862.00 rows=4 width=4)
1035
   ->  Hash Anti Join  (cost=0.00..862.00 rows=2 width=4)
1036
         Hash Cond: t1.c1 = t2.c2
1037
         ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
1038
         ->  Hash  (cost=431.00..431.00 rows=2 width=4)
1039
               ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(8 rows)

select c1 from t1 where not exists (select c2 from t2 where c2 = c1);
 c1 
----
  6
  7
  8
  9
 10
(5 rows)

--
--q39
--
explain select c1 from t1 where not exists (select c2 from t2 where c2 not in (select c3 from t3) and c2 = c1);
                                                  QUERY PLAN                                                   
---------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..1293.00 rows=4 width=4)
1061
   ->  Hash Anti Join  (cost=0.00..1293.00 rows=2 width=4)
1062
         Hash Cond: t1.c1 = t2.c2
1063
         ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
1064
         ->  Hash  (cost=862.00..862.00 rows=1 width=4)
1065
               ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..862.00 rows=1 width=4)
1066
                     Hash Cond: t2.c2 = t3.c3
1067
                     ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
1068 1069
                     ->  Hash  (cost=431.00..431.00 rows=3 width=4)
                           ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=3 width=4)
1070
                                 ->  Seq Scan on t3  (cost=0.00..431.00 rows=1 width=4)
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(13 rows)

select c1 from t1 where not exists (select c2 from t2 where c2 not in (select c3 from t3) and c2 = c1);
 c1 
----
  8
  9
 10
  3
  6
  7
  1
  2
(8 rows)

--
--q40
1090
-- GPDB_90_MERGE_FIXME: We should be able to push down join filter on param $0 to a result node on top of LASJ (Not in)
1091 1092
--
explain select c1 from t1 where not exists (select c2 from t2 where exists (select c3 from t3) and c2 <>all (select c3 from t3) and c2 = c1);
1093 1094 1095
                                                          QUERY PLAN                                                          
------------------------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice4; segments: 3)  (cost=0.00..1324894.16 rows=4 width=4)
1096
   ->  Hash Anti Join  (cost=0.00..1324894.16 rows=2 width=4)
1097
         Hash Cond: t1.c1 = t2.c2
1098
         ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
1099
         ->  Hash  (cost=1324463.16..1324463.16 rows=1 width=4)
1100
               ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..1324463.16 rows=1 width=4)
1101
                     Hash Cond: t2.c2 = t3_1.c3
1102
                     ->  Nested Loop Semi Join  (cost=0.00..1324032.16 rows=2 width=4)
1103
                           Join Filter: true
1104
                           ->  Seq Scan on t2  (cost=0.00..431.00 rows=2 width=4)
V
Venkatesh Raghavan 已提交
1105 1106 1107 1108 1109
                           ->  Materialize  (cost=0.00..431.00 rows=1 width=1)
                                 ->  Broadcast Motion 1:3  (slice2)  (cost=0.00..431.00 rows=3 width=1)
                                       ->  Limit  (cost=0.00..431.00 rows=1 width=1)
                                             ->  Gather Motion 3:1  (slice1; segments: 3)  (cost=0.00..431.00 rows=1 width=1)
                                                   ->  Limit  (cost=0.00..431.00 rows=1 width=1)
1110
                                                         ->  Seq Scan on t3  (cost=0.00..431.00 rows=1 width=1)
1111
                     ->  Hash  (cost=431.00..431.00 rows=3 width=4)
V
Venkatesh Raghavan 已提交
1112
                           ->  Broadcast Motion 3:3  (slice3; segments: 3)  (cost=0.00..431.00 rows=3 width=4)
1113
                                 ->  Seq Scan on t3 t3_1  (cost=0.00..431.00 rows=1 width=4)
1114
 Optimizer: PQO version 2.74.0
V
Venkatesh Raghavan 已提交
1115
(20 rows)
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 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162

select c1 from t1 where not exists (select c2 from t2 where exists (select c3 from t3) and c2 <>all (select c3 from t3) and c2 = c1);
 c1 
----
  8
  9
 10
  1
  2
  3
  6
  7
(8 rows)

--
--q41
--
select c1 from t1 where c1 not in (select c2 from t2) or c1 = 49;
 c1 
----
  8
  9
 10
  6
  7
(5 rows)

--
--q42
--
select c1 from t1 where not not not c1 in (select c2 from t2);
 c1 
----
  6
  7
  8
  9
 10
(5 rows)

--
--q43
--
explain select c1 from t1 where c1 not in (select c2 from t2 where c2 > 4) and c1 is not null;
                                            QUERY PLAN                                             
---------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice2; segments: 3)  (cost=0.00..862.00 rows=4 width=4)
1163
   ->  Hash Left Anti Semi (Not-In) Join  (cost=0.00..862.00 rows=2 width=4)
1164
         Hash Cond: t1.c1 = t2.c2
1165
         ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=4)
1166 1167 1168
               Filter: NOT c1 IS NULL
         ->  Hash  (cost=431.00..431.00 rows=1 width=4)
               ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=1 width=4)
1169
                     ->  Seq Scan on t2  (cost=0.00..431.00 rows=1 width=4)
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
                           Filter: c2 > 4
 Settings:  optimizer=on
 Optimizer status: PQO version 2.34.0
(11 rows)

select c1 from t1 where c1 not in (select c2 from t2 where c2 > 4) and c1 is not null;
 c1 
----
  3
  4
  6
  7
  8
  9
 10
  1
  2
(9 rows)

--
--q44
--
select c1 from t1 where c1 not in (select c2 from t2 where c2 > 4) and c1 > 2;
 c1 
----
  8
  9
 10
  3
  4
  6
  7
(7 rows)

R
Richard Guo 已提交
1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
-- Test if the equality operator is implemented by a SQL function
--
--q45
--
create domain absint as int4;
create function iszero(absint) returns bool as $$ begin return $1::int4 = 0; end; $$ language plpgsql immutable strict;
create or replace function abseq (absint, absint) returns bool as $$ select iszero(abs($1) - abs($2)); $$ language sql immutable strict;
create operator = (PROCEDURE = abseq, leftarg=absint, rightarg=absint);
explain select c1 from t1 where c1::absint not in
	(select c1n::absint from t1n);
1214 1215 1216 1217 1218 1219
                                                                                                                                              QUERY PLAN                                                                                                                                               
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Gather Motion 3:1  (slice3; segments: 3)  (cost=0.00..1324035.89 rows=10 width=4)
   ->  Result  (cost=0.00..1324035.89 rows=4 width=4)
         Filter: (NOT CASE WHEN ((count((count("inner".ColRef_0017)))) > 0::bigint) THEN CASE WHEN ((pg_catalog.sum((sum((CASE WHEN (((t1.c1)::absint = ((t1n.c1n)::absint)) IS NULL) THEN 1 ELSE 0 END))))) = (count((count("inner".ColRef_0017))))) THEN NULL::boolean ELSE true END ELSE false END)
         ->  GroupAggregate  (cost=0.00..1324035.89 rows=4 width=20)
R
Richard Guo 已提交
1220
               Group Key: t1.c1, t1.ctid, t1.gp_segment_id
1221
               ->  Sort  (cost=0.00..1324035.89 rows=4 width=30)
R
Richard Guo 已提交
1222
                     Sort Key: t1.ctid, t1.gp_segment_id
1223
                     ->  Redistribute Motion 3:3  (slice2; segments: 3)  (cost=0.00..1324035.89 rows=4 width=30)
R
Richard Guo 已提交
1224
                           Hash Key: t1.ctid, t1.gp_segment_id
1225 1226
                           ->  Result  (cost=0.00..1324035.89 rows=4 width=30)
                                 ->  HashAggregate  (cost=0.00..1324035.89 rows=4 width=30)
R
Richard Guo 已提交
1227
                                       Group Key: t1.c1, t1.ctid, t1.gp_segment_id
1228 1229 1230 1231 1232 1233 1234
                                       ->  Result  (cost=0.00..1324035.88 rows=11 width=19)
                                             ->  Nested Loop Left Join  (cost=0.00..1324035.88 rows=11 width=19)
                                                   Join Filter: (((t1.c1)::absint = ((t1n.c1n)::absint)) IS NOT FALSE)
                                                   ->  Seq Scan on t1  (cost=0.00..431.00 rows=4 width=14)
                                                   ->  Result  (cost=0.00..431.00 rows=7 width=5)
                                                         ->  Materialize  (cost=0.00..431.00 rows=7 width=4)
                                                               ->  Broadcast Motion 3:3  (slice1; segments: 3)  (cost=0.00..431.00 rows=7 width=4)
R
Richard Guo 已提交
1235
                                                                     ->  Seq Scan on t1n  (cost=0.00..431.00 rows=3 width=4)
1236 1237
 Optimizer: PQO version 3.27.0
(21 rows)
R
Richard Guo 已提交
1238 1239 1240 1241 1242 1243 1244

select c1 from t1 where c1::absint not in
	(select c1n::absint from t1n);
 c1 
----
(0 rows)

1245 1246
reset search_path;
drop schema notin cascade;
R
Richard Guo 已提交
1247
NOTICE:  drop cascades to 11 other objects
1248 1249 1250 1251 1252 1253 1254
DETAIL:  drop cascades to table notin.t1
drop cascades to table notin.t2
drop cascades to table notin.t3
drop cascades to table notin.t4
drop cascades to table notin.t1n
drop cascades to table notin.g1
drop cascades to table notin.l1
R
Richard Guo 已提交
1255 1256 1257 1258
drop cascades to type notin.absint
drop cascades to function notin.iszero(notin.absint)
drop cascades to function notin.abseq(notin.absint,notin.absint)
drop cascades to operator notin.=(notin.absint,notin.absint)