subselect_gp.out 56.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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 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 282 283 284 285 286 287 288 289 290 291 292 293 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 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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 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 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 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 553 554 555 556 557 558 559 560 561 562 563 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 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 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 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 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 939 940 941 942 943 944 945 946 947 948 949 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 976 977 978 979 980 981 982 983 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
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;
                                                            QUERY PLAN                                                             
-----------------------------------------------------------------------------------------------------------------------------------
 Gather Motion 2:1  (slice17; segments: 2)  (cost=20329.25..20331.40 rows=863 width=8)
   Merge Key: x
   ->  Sort  (cost=20329.25..20331.40 rows=432 width=8)
         Sort Key: public.csq_t1.x
         ->  Append  (cost=0.00..20287.16 rows=432 width=8)
               ->  Seq Scan on csq_t1_1_prt_1 csq_t1  (cost=0.00..0.00 rows=1 width=8)
                     Filter: (subplan)
                     SubPlan 1
                       ->  Result  (cost=0.00..23.53 rows=2 width=4)
                             ->  Append  (cost=0.00..23.53 rows=2 width=4)
                                   ->  Result  (cost=0.00..0.01 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=0.00..0.01 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice1; segments: 2)  (cost=0.00..0.00 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_1 csq_t2  (cost=0.00..0.00 rows=1 width=4)
                                   ->  Result  (cost=11.76..11.77 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=11.76..11.77 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice2; segments: 2)  (cost=0.00..11.76 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_2 csq_t2  (cost=0.00..11.76 rows=1 width=4)
                                   ->  Result  (cost=11.76..11.77 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=11.76..11.77 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice3; segments: 2)  (cost=0.00..11.76 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_3 csq_t2  (cost=0.00..11.76 rows=1 width=4)
                                   ->  Result  (cost=0.00..0.01 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=0.00..0.01 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice4; segments: 2)  (cost=0.00..0.00 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_4 csq_t2  (cost=0.00..0.00 rows=1 width=4)
               ->  Seq Scan on csq_t1_1_prt_2 csq_t1  (cost=0.00..10143.58 rows=216 width=8)
                     Filter: (subplan)
                     SubPlan 1
                       ->  Result  (cost=0.00..23.53 rows=2 width=4)
                             ->  Append  (cost=0.00..23.53 rows=2 width=4)
                                   ->  Result  (cost=0.00..0.01 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=0.00..0.01 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice5; segments: 2)  (cost=0.00..0.00 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_1 csq_t2  (cost=0.00..0.00 rows=1 width=4)
                                   ->  Result  (cost=11.76..11.77 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=11.76..11.77 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice6; segments: 2)  (cost=0.00..11.76 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_2 csq_t2  (cost=0.00..11.76 rows=1 width=4)
                                   ->  Result  (cost=11.76..11.77 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=11.76..11.77 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice7; segments: 2)  (cost=0.00..11.76 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_3 csq_t2  (cost=0.00..11.76 rows=1 width=4)
                                   ->  Result  (cost=0.00..0.01 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=0.00..0.01 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice8; segments: 2)  (cost=0.00..0.00 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_4 csq_t2  (cost=0.00..0.00 rows=1 width=4)
               ->  Seq Scan on csq_t1_1_prt_3 csq_t1  (cost=0.00..10143.58 rows=216 width=8)
                     Filter: (subplan)
                     SubPlan 1
                       ->  Result  (cost=0.00..23.53 rows=2 width=4)
                             ->  Append  (cost=0.00..23.53 rows=2 width=4)
                                   ->  Result  (cost=0.00..0.01 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=0.00..0.01 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice9; segments: 2)  (cost=0.00..0.00 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_1 csq_t2  (cost=0.00..0.00 rows=1 width=4)
                                   ->  Result  (cost=11.76..11.77 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=11.76..11.77 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice10; segments: 2)  (cost=0.00..11.76 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_2 csq_t2  (cost=0.00..11.76 rows=1 width=4)
                                   ->  Result  (cost=11.76..11.77 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=11.76..11.77 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice11; segments: 2)  (cost=0.00..11.76 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_3 csq_t2  (cost=0.00..11.76 rows=1 width=4)
                                   ->  Result  (cost=0.00..0.01 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=0.00..0.01 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice12; segments: 2)  (cost=0.00..0.00 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_4 csq_t2  (cost=0.00..0.00 rows=1 width=4)
               ->  Seq Scan on csq_t1_1_prt_4 csq_t1  (cost=0.00..0.00 rows=1 width=8)
                     Filter: (subplan)
                     SubPlan 1
                       ->  Result  (cost=0.00..23.53 rows=2 width=4)
                             ->  Append  (cost=0.00..23.53 rows=2 width=4)
                                   ->  Result  (cost=0.00..0.01 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=0.00..0.01 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice13; segments: 2)  (cost=0.00..0.00 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_1 csq_t2  (cost=0.00..0.00 rows=1 width=4)
                                   ->  Result  (cost=11.76..11.77 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=11.76..11.77 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice14; segments: 2)  (cost=0.00..11.76 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_2 csq_t2  (cost=0.00..11.76 rows=1 width=4)
                                   ->  Result  (cost=11.76..11.77 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=11.76..11.77 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice15; segments: 2)  (cost=0.00..11.76 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_3 csq_t2  (cost=0.00..11.76 rows=1 width=4)
                                   ->  Result  (cost=0.00..0.01 rows=1 width=4)
                                         Filter: public.csq_t2.y = $0
                                         ->  Materialize  (cost=0.00..0.01 rows=1 width=4)
                                               ->  Broadcast Motion 2:2  (slice16; segments: 2)  (cost=0.00..0.00 rows=1 width=4)
                                                     ->  Seq Scan on csq_t2_1_prt_4 csq_t2  (cost=0.00..0.00 rows=1 width=4)
 Settings:  optimizer_segments=3
(106 rows)

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                                           
------------------------------------------------------------------------------------------------
 Gather Motion 2:1  (slice2; segments: 2)  (cost=2.27..4.47 rows=20 width=4)
   ->  Result  (cost=2.27..4.47 rows=10 width=4)
         One-Time Filter: $0
         InitPlan  (slice3)
           ->  Limit  (cost=0.00..2.27 rows=1 width=0)
                 ->  Gather Motion 2:1  (slice1; segments: 2)  (cost=0.00..2.27 rows=1 width=0)
                       ->  Limit  (cost=0.00..2.25 rows=1 width=0)
                             ->  Seq Scan on mrs_t1  (cost=0.00..2.25 rows=1 width=0)
                                   Filter: x < (-1)
         ->  Seq Scan on mrs_t1  (cost=0.00..2.20 rows=10 width=4)
 Settings:  optimizer_segments=3
(11 rows)

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                                           
------------------------------------------------------------------------------------------------
 Gather Motion 2:1  (slice2; segments: 2)  (cost=2.27..4.47 rows=20 width=4)
   ->  Result  (cost=2.27..4.47 rows=10 width=4)
         One-Time Filter: $0
         InitPlan  (slice3)
           ->  Limit  (cost=0.00..2.27 rows=1 width=0)
                 ->  Gather Motion 1:1  (slice1; segments: 1)  (cost=0.00..2.27 rows=1 width=0)
                       ->  Limit  (cost=0.00..2.25 rows=1 width=0)
                             ->  Seq Scan on mrs_t1  (cost=0.00..2.25 rows=1 width=0)
                                   Filter: x = 1
         ->  Seq Scan on mrs_t1  (cost=0.00..2.20 rows=10 width=4)
 Settings:  optimizer_segments=3
(11 rows)

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;
                                             QUERY PLAN                                             
----------------------------------------------------------------------------------------------------
 Gather Motion 2:1  (slice2; segments: 2)  (cost=0.00..25.30 rows=12 width=4)
   ->  Seq Scan on mrs_t1  (cost=0.00..25.30 rows=6 width=4)
         Filter: ((subplan)) OR x < 5
         SubPlan 1
           ->  Materialize  (cost=2.27..2.47 rows=10 width=4)
                 ->  Broadcast Motion 2:2  (slice1; segments: 2)  (cost=0.00..2.25 rows=10 width=4)
                       ->  Seq Scan on mrs_t1  (cost=0.00..2.25 rows=10 width=4)
 Settings:  optimizer_segments=3
(8 rows)

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();
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
     ->  Seq Scan on csq_m1  (cost=0.00..1.01 rows=2 width=4)
 Settings:  optimizer_segments=3
(4 rows)

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)
     ->  Gather Motion 2:1  (slice1; segments: 2)  (cost=0.00..1.01 rows=1 width=4)
           ->  Seq Scan on csq_d1  (cost=0.00..1.01 rows=1 width=4)
 Settings:  optimizer_segments=3
(5 rows)

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

--
-- CSQs involving master-only and distributed tables
--
drop table if exists t3coquicklz;
NOTICE:  table "t3coquicklz" does not exist, skipping
create table t3coquicklz (c1 int , c2 varchar) with (appendonly=true, compresstype=quicklz, orientation=column) distributed by (c1);
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);
insert into pg_attribute_storage values ('t3coquicklz'::regclass, 1, E'{\'something\'}');
insert into pg_attribute_storage values ('t3coquicklz'::regclass, 2, E'{\'something2\'}');
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
WHERE a.attrelid = 't3coquicklz'::regclass AND a.attnum > 0 AND NOT a.attisdropped
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();
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                                        
------------------------------------------------------------------------------------------
 Seq Scan on csq_m1  (cost=0.00..4.08 rows=3 width=4)
   Filter: (NOT ((subplan))) OR x < (-100)
   SubPlan 1
     ->  Materialize  (cost=2.02..2.04 rows=1 width=4)
           ->  Gather Motion 2:1  (slice1; segments: 2)  (cost=0.00..2.02 rows=2 width=4)
                 ->  Seq Scan on csq_d1  (cost=0.00..2.02 rows=1 width=4)
 Settings:  optimizer_segments=3
(7 rows)

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                                      
--------------------------------------------------------------------------------------
 Gather Motion 2:1  (slice2; segments: 2)  (cost=0.00..3.07 rows=1 width=4)
   ->  Seq Scan on csq_d1  (cost=0.00..3.07 rows=1 width=4)
         Filter: (NOT ((subplan))) OR x < (-100)
         SubPlan 1
           ->  Materialize  (cost=1.04..1.10 rows=6 width=4)
                 ->  Broadcast Motion 1:2  (slice1)  (cost=0.00..1.03 rows=6 width=4)
                       ->  Seq Scan on csq_m1  (cost=0.00..1.03 rows=6 width=4)
 Settings:  optimizer_segments=3
(8 rows)

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

--
-- 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
--
CREATE FUNCTION csq_f(a int) RETURNS int AS $$ select $1 $$ LANGUAGE SQL CONTAINS SQL;
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                                 
----------------------------------------------------------------------------
 Gather Motion 2:1  (slice1; segments: 2)  (cost=0.00..1.02 rows=1 width=4)
   ->  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
(6 rows)

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                                 
----------------------------------------------------------------------------
 Gather Motion 2:1  (slice1; segments: 2)  (cost=0.00..1.02 rows=1 width=4)
   ->  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
(6 rows)

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                                 
----------------------------------------------------------------------------
 Gather Motion 2:1  (slice1; segments: 2)  (cost=0.00..1.02 rows=1 width=4)
   ->  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
(6 rows)

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                                 
----------------------------------------------------------------------------
 Gather Motion 2:1  (slice1; segments: 2)  (cost=0.00..1.02 rows=1 width=4)
   ->  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
(6 rows)

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                                  
------------------------------------------------------------------------------
 Gather Motion 2:1  (slice1; segments: 2)  (cost=0.00..1.02 rows=1 width=4)
   ->  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
(7 rows)

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                                 
----------------------------------------------------------------------------
 Gather Motion 2:1  (slice1; segments: 2)  (cost=0.00..1.02 rows=1 width=4)
   ->  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
(6 rows)

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                                 
----------------------------------------------------------------------------
 Gather Motion 2:1  (slice1; segments: 2)  (cost=0.00..1.02 rows=1 width=4)
   ->  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
(6 rows)

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                                         
-------------------------------------------------------------------------------------------
 Gather Motion 2:1  (slice1; segments: 2)  (cost=1.06..2.11 rows=4 width=19)
   ->  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)
                           Filter: 1 = count(*)
                           Group By: t1.t
                           ->  Seq Scan on csq_pullup t1  (cost=0.00..1.01 rows=1 width=4)
 Settings:  optimizer_segments=3
(11 rows)

select * from csq_pullup t0 where 1= (select count(*) from csq_pullup t1 where t0.t=t1.t);
  t  | n | i |  v  
-----+---+---+-----
 xyz | 2 | 3 | def
 def | 3 | 1 | abc
 abc | 1 | 2 | xyz
(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                                                    
-----------------------------------------------------------------------------------------------------------------
 Gather Motion 2:1  (slice2; segments: 2)  (cost=1.09..2.14 rows=4 width=19)
   ->  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)
                           Filter: 1 = count(partial_aggregation.unnamed_attr_2)
                           Group By: "?column1?"
                           ->  Redistribute Motion 2:2  (slice1; segments: 2)  (cost=1.02..1.04 rows=1 width=40)
                                 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
(15 rows)

select * from csq_pullup t0 where 1= (select count(*) from csq_pullup t1 where t0.t=t1.v);
  t  | n | i |  v  
-----+---+---+-----
 abc | 1 | 2 | xyz
 xyz | 2 | 3 | def
 def | 3 | 1 | abc
(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                                                    
-----------------------------------------------------------------------------------------------------------------
 Gather Motion 2:1  (slice3; segments: 2)  (cost=1.09..2.16 rows=4 width=19)
   ->  Hash Join  (cost=1.09..2.16 rows=2 width=19)
         Hash Cond: t0.n = "Expr_SUBQUERY".csq_c0
         ->  Redistribute Motion 2:2  (slice1; segments: 2)  (cost=0.00..1.03 rows=1 width=19)
               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)
                           Filter: 1 = count(partial_aggregation.unnamed_attr_2)
                           Group By: t1.n
                           ->  Redistribute Motion 2:2  (slice2; segments: 2)  (cost=1.02..1.04 rows=1 width=40)
                                 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
(17 rows)

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
 abc | 1 | 2 | xyz
 xyz | 2 | 3 | def
(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                                                    
-----------------------------------------------------------------------------------------------------------------
 Gather Motion 2:1  (slice3; segments: 2)  (cost=1.10..2.17 rows=4 width=19)
   ->  Hash Join  (cost=1.10..2.17 rows=2 width=19)
         Hash Cond: (t0.n + 1::numeric) = "Expr_SUBQUERY".csq_c0
         ->  Redistribute Motion 2:2  (slice1; segments: 2)  (cost=0.00..1.03 rows=1 width=19)
               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)
                           Filter: 1 = count(partial_aggregation.unnamed_attr_2)
                           Group By: "?column1?"
                           ->  Redistribute Motion 2:2  (slice2; segments: 2)  (cost=1.02..1.04 rows=1 width=40)
                                 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
(17 rows)

select * from csq_pullup t0 where 1= (select count(*) from csq_pullup t1 where t0.n + 1=t1.n + 1);
  t  | n | i |  v  
-----+---+---+-----
 xyz | 2 | 3 | def
 def | 3 | 1 | abc
 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                                                    
-----------------------------------------------------------------------------------------------------------------
 Gather Motion 2:1  (slice3; segments: 2)  (cost=1.10..2.18 rows=4 width=19)
   ->  Hash Join  (cost=1.10..2.18 rows=2 width=19)
         Hash Cond: (t0.n + 1::numeric) = "Expr_SUBQUERY".csq_c0
         ->  Redistribute Motion 2:2  (slice1; segments: 2)  (cost=0.00..1.03 rows=1 width=19)
               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)
                           Filter: 1 = count(partial_aggregation.unnamed_attr_2)
                           Group By: "?column1?"
                           ->  Redistribute Motion 2:2  (slice2; segments: 2)  (cost=1.02..1.05 rows=1 width=40)
                                 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
(17 rows)

select * from csq_pullup t0 where 1= (select count(*) from csq_pullup t1 where t0.n + 1=t1.i + 1);
  t  | n | i |  v  
-----+---+---+-----
 def | 3 | 1 | abc
 abc | 1 | 2 | xyz
 xyz | 2 | 3 | def
(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                                   
-------------------------------------------------------------------------------
 Gather Motion 2:1  (slice1; segments: 2)  (cost=1.02..2.07 rows=4 width=19)
   ->  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)
                     Filter: i = 1 AND t IS NOT NULL
 Settings:  optimizer_segments=3
(8 rows)

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                                            
-------------------------------------------------------------------------------------------------
 Gather Motion 2:1  (slice2; segments: 2)  (cost=1.07..2.12 rows=4 width=19)
   ->  Hash Left Anti Semi Join  (cost=1.07..2.12 rows=2 width=19)
         Hash Cond: t0.i = (t1.i + 1)
         ->  Seq Scan on csq_pullup t0  (cost=0.00..1.01 rows=1 width=19)
         ->  Hash  (cost=1.04..1.04 rows=1 width=4)
               ->  Broadcast Motion 2:2  (slice1; segments: 2)  (cost=0.00..1.04 rows=1 width=4)
                     ->  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
(9 rows)

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);
                                 QUERY PLAN                                 
----------------------------------------------------------------------------
 Gather Motion 2:1  (slice1; segments: 2)  (cost=2.07..4.13 rows=4 width=4)
   ->  Hash EXISTS Join  (cost=2.07..4.13 rows=2 width=4)
         Hash Cond: subselect_t1.x = subselect_t2.y
         ->  Seq Scan on subselect_t1  (cost=0.00..2.02 rows=1 width=4)
         ->  Hash  (cost=2.03..2.03 rows=2 width=4)
               ->  Seq Scan on subselect_t2  (cost=0.00..2.03 rows=2 width=4)
 Settings:  optimizer_segments=3
(7 rows)

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                                             
----------------------------------------------------------------------------------------------------
 Gather Motion 2:1  (slice2; segments: 2)  (cost=4.37..6.44 rows=4 width=4)
   ->  Hash EXISTS Join  (cost=4.37..6.44 rows=2 width=4)
         Hash Cond: subselect_t1.x = "IN_subquery".y
         ->  Seq Scan on subselect_t1  (cost=0.00..2.02 rows=1 width=4)
         ->  Hash  (cost=4.30..4.30 rows=3 width=4)
               ->  Redistribute Motion 2:2  (slice1; segments: 2)  (cost=0.00..4.30 rows=3 width=4)
                     Hash Key: "IN_subquery".y
                     ->  Append  (cost=0.00..4.12 rows=3 width=4)
                           ->  Seq Scan on subselect_t2  (cost=0.00..2.03 rows=2 width=4)
                           ->  Seq Scan on subselect_t2  (cost=0.00..2.03 rows=2 width=4)
 Settings:  optimizer_segments=3
(11 rows)

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

explain select count(*) from subselect_t1 where x in (select y from subselect_t2);
                                      QUERY PLAN                                      
--------------------------------------------------------------------------------------
 Aggregate  (cost=4.20..4.21 rows=1 width=8)
   ->  Gather Motion 2:1  (slice1; segments: 2)  (cost=4.14..4.18 rows=1 width=8)
         ->  Aggregate  (cost=4.14..4.15 rows=1 width=8)
               ->  Hash EXISTS Join  (cost=2.07..4.13 rows=2 width=0)
                     Hash Cond: subselect_t1.x = subselect_t2.y
                     ->  Seq Scan on subselect_t1  (cost=0.00..2.02 rows=1 width=4)
                     ->  Hash  (cost=2.03..2.03 rows=2 width=4)
                           ->  Seq Scan on subselect_t2  (cost=0.00..2.03 rows=2 width=4)
 Settings:  optimizer_segments=3
(9 rows)

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                                                   
----------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=6.50..6.51 rows=1 width=8)
   ->  Gather Motion 2:1  (slice2; segments: 2)  (cost=6.45..6.49 rows=1 width=8)
         ->  Aggregate  (cost=6.45..6.46 rows=1 width=8)
               ->  Hash EXISTS Join  (cost=4.37..6.44 rows=2 width=0)
                     Hash Cond: subselect_t1.x = "IN_subquery".y
                     ->  Seq Scan on subselect_t1  (cost=0.00..2.02 rows=1 width=4)
                     ->  Hash  (cost=4.30..4.30 rows=3 width=4)
                           ->  Redistribute Motion 2:2  (slice1; segments: 2)  (cost=0.00..4.30 rows=3 width=4)
                                 Hash Key: "IN_subquery".y
                                 ->  Append  (cost=0.00..4.12 rows=3 width=4)
                                       ->  Seq Scan on subselect_t2  (cost=0.00..2.03 rows=2 width=4)
                                       ->  Seq Scan on subselect_t2  (cost=0.00..2.03 rows=2 width=4)
 Settings:  optimizer_segments=3
(13 rows)

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; 
NOTICE:  table "t1" does not exist, skipping
drop table if exists t2; 
NOTICE:  table "t2" does not exist, skipping
drop table if exists t3; 
NOTICE:  table "t3" does not exist, skipping
drop table if exists t4; 
NOTICE:  table "t4" does not exist, skipping
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);
1029 1030 1031 1032
                QUERY PLAN                
------------------------------------------
 Result  (cost=0.00..0.01 rows=1 width=0)
   One-Time Filter: false
1033
 Settings:  optimizer_segments=3
1034
(3 rows)
1035 1036 1037 1038

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);
1039 1040 1041 1042 1043 1044 1045
                   QUERY PLAN                   
------------------------------------------------
 Append  (cost=0.00..0.04 rows=1 width=0)
   ->  Result  (cost=0.00..0.01 rows=1 width=0)
         One-Time Filter: false
   ->  Result  (cost=0.00..0.01 rows=1 width=0)
         One-Time Filter: false
1046
 Settings:  optimizer_segments=3
1047
(6 rows)
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058

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;
1059 1060 1061 1062
                                        QUERY PLAN                                        
------------------------------------------------------------------------------------------
 Gather Motion 1:1  (slice2; segments: 1)  (cost=0.00..0.01 rows=4 width=8)
   ->  Result  (cost=0.00..0.01 rows=2 width=8)
1063
         One-Time Filter: false
1064 1065 1066 1067 1068 1069 1070 1071
         InitPlan  (slice3)
           ->  Gather Motion 2:1  (slice1; segments: 2)  (cost=0.00..0.00 rows=1 width=4)
                 ->  Seq Scan on t2  (cost=0.00..0.00 rows=1 width=4)
         ->  Nested Loop  (cost=0.00..0.01 rows=2 width=8)
               ->  Seq Scan on t1  (cost=0.00..0.00 rows=1 width=4)
                     Filter: a > $0 AND a = 1
               ->  Seq Scan on t1  (cost=0.00..0.00 rows=1 width=4)
                     Filter: a > $0 AND a = 1
1072
 Settings:  optimizer_segments=3
1073
(12 rows)
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084

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;
--
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
-- 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;
--
1115 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 1163 1164 1165 1166 1167 1168 1169 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 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215
-- 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;
                                              QUERY PLAN
------------------------------------------------------------------------------------------------------
 Window  (cost=1.02..2.10 rows=1 width=8)
   Order By: t2.seq
   ->  Gather Motion 1:1  (slice2; segments: 1)  (cost=1.02..1.05 rows=1 width=8)
         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
     ->  Aggregate  (cost=1.04..1.05 rows=1 width=8)
           ->  Result  (cost=1.01..1.02 rows=1 width=0)
                 Filter: t1.id = $0
                 ->  Materialize  (cost=1.01..1.02 rows=1 width=0)
                       ->  Gather Motion 1:1  (slice1; segments: 1)  (cost=0.00..1.01 rows=1 width=0)
                             ->  Seq Scan on t1_mpp_24563 t1  (cost=0.00..1.01 rows=1 width=0)
 Settings:  optimizer_segments=3
(16 rows)

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;
                                                    QUERY PLAN
------------------------------------------------------------------------------------------------------------------
 Nested Loop  (cost=81.92..12932.78 rows=934 width=28)
   ->  Aggregate  (cost=41.63..41.64 rows=1 width=0)
         ->  Shared Scan (share slice:id 0:0)  (cost=40.29..41.63 rows=934 width=28)
               ->  Materialize  (cost=30.95..40.29 rows=934 width=28)
                     ->  Gather Motion 1:1  (slice3; segments: 1)  (cost=0.00..30.02 rows=934 width=28)
                           ->  Append  (cost=0.00..11.34 rows=934 width=28)
                                 ->  Seq Scan on t_mpp_20470_ptr1 b  (cost=0.00..5.67 rows=467 width=28)
                                 ->  Seq Scan on t_mpp_20470_ptr2 b  (cost=0.00..5.67 rows=467 width=28)
   ->  Subquery Scan coplan  (cost=40.29..50.97 rows=934 width=28)
         ->  Window  (cost=40.29..41.63 rows=934 width=28)
               ->  Shared Scan (share slice:id 0:0)  (cost=40.29..41.63 rows=934 width=28)
   SubPlan 1
     ->  Aggregate  (cost=13.72..13.73 rows=1 width=8)
           ->  Result  (cost=0.00..13.68 rows=2 width=4)
                 ->  Append  (cost=0.00..13.68 rows=2 width=4)
                       ->  Result  (cost=6.84..6.85 rows=1 width=4)
                             Filter: a.col_name::text = $0::text
                             ->  Materialize  (cost=6.84..6.85 rows=1 width=4)
                                   ->  Gather Motion 1:1  (slice1; segments: 1)  (cost=0.00..6.84 rows=1 width=4)
                                         ->  Seq Scan on t_mpp_20470_ptr1 a  (cost=0.00..6.84 rows=1 width=4)
                       ->  Result  (cost=6.84..6.85 rows=1 width=4)
                             Filter: a.col_name::text = $0::text
                             ->  Materialize  (cost=6.84..6.85 rows=1 width=4)
                                   ->  Gather Motion 1:1  (slice2; segments: 1)  (cost=0.00..6.84 rows=1 width=4)
                                         ->  Seq Scan on t_mpp_20470_ptr2 a  (cost=0.00..6.84 rows=1 width=4)
 Settings:  optimizer_segments=3
(26 rows)

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));
 id
----
  3
(1 row)

drop table tbl_25484;
reset optimizer_segments;
reset optimizer_nestloop_factor;
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229
--
-- 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)

1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249
--
-- 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)

1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
--
-- 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;