File: join_pushdown_3.out

package info (click to toggle)
postgresql-mysql-fdw 2.9.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,280 kB
  • sloc: ansic: 6,010; sql: 1,994; sh: 84; makefile: 42
file content (1286 lines) | stat: -rw-r--r-- 64,828 bytes parent folder | download
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
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
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
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
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
\set MYSQL_HOST			`echo \'"$MYSQL_HOST"\'`
\set MYSQL_PORT			`echo \'"$MYSQL_PORT"\'`
\set MYSQL_USER_NAME	`echo \'"$MYSQL_USER_NAME"\'`
\set MYSQL_PASS			`echo \'"$MYSQL_PWD"\'`
-- Before running this file User must create database mysql_fdw_regress on
-- mysql with all permission for MYSQL_USER_NAME user with MYSQL_PWD password
-- and ran mysql_init.sh file to create tables.
\c contrib_regression
CREATE EXTENSION IF NOT EXISTS mysql_fdw;
-- FDW-139: Support for JOIN pushdown.
CREATE SERVER mysql_svr FOREIGN DATA WRAPPER mysql_fdw
  OPTIONS (host :MYSQL_HOST, port :MYSQL_PORT);
CREATE USER MAPPING FOR public SERVER mysql_svr
  OPTIONS (username :MYSQL_USER_NAME, password :MYSQL_PASS);
CREATE SERVER mysql_svr1 FOREIGN DATA WRAPPER mysql_fdw
  OPTIONS (host :MYSQL_HOST, port :MYSQL_PORT);
CREATE USER MAPPING FOR public SERVER mysql_svr1
  OPTIONS (username :MYSQL_USER_NAME, password :MYSQL_PASS);
CREATE TYPE user_enum AS ENUM ('foo', 'bar', 'buz');
CREATE FOREIGN TABLE fdw139_t1(c1 int, c2 int, c3 text COLLATE "C", c4 text COLLATE "C")
  SERVER mysql_svr OPTIONS(dbname 'mysql_fdw_regress', table_name 'test1');
CREATE FOREIGN TABLE fdw139_t2(c1 int, c2 int, c3 text COLLATE "C", c4 text COLLATE "C")
  SERVER mysql_svr OPTIONS(dbname 'mysql_fdw_regress', table_name 'test2');
CREATE FOREIGN TABLE fdw139_t3(c1 int, c2 int, c3 text COLLATE "C")
  SERVER mysql_svr OPTIONS(dbname 'mysql_fdw_regress', table_name 'test3');
CREATE FOREIGN TABLE fdw139_t4(c1 int, c2 int, c3 text COLLATE "C")
  SERVER mysql_svr1 OPTIONS(dbname 'mysql_fdw_regress', table_name 'test3');
INSERT INTO fdw139_t1 values(1, 100, 'AAA1', 'foo');
INSERT INTO fdw139_t1 values(2, 100, 'AAA2', 'bar');
INSERT INTO fdw139_t1 values(11, 100, 'AAA11', 'foo');
INSERT INTO fdw139_t2 values(1, 200, 'BBB1', 'foo');
INSERT INTO fdw139_t2 values(2, 200, 'BBB2', 'bar');
INSERT INTO fdw139_t2 values(12, 200, 'BBB12', 'foo');
INSERT INTO fdw139_t3 values(1, 300, 'CCC1');
INSERT INTO fdw139_t3 values(2, 300, 'CCC2');
INSERT INTO fdw139_t3 values(13, 300, 'CCC13');
SET enable_mergejoin TO off;
SET enable_hashjoin TO off;
SET enable_sort TO off;
ALTER FOREIGN TABLE fdw139_t1 ALTER COLUMN c4 type user_enum;
ALTER FOREIGN TABLE fdw139_t2 ALTER COLUMN c4 type user_enum;
-- Join two tables
-- target list order is different for v10.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c3, t1.c1;
                                                                                                           QUERY PLAN                                                                                                            
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1, t1.c3
   Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r1.`c1`, r2.`c1`, r1.`c3` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (((r1.`c1` = r2.`c1`)))) ORDER BY r1.`c3` IS NULL, r1.`c3` ASC, r1.`c1` IS NULL, r1.`c1` ASC
(4 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c3, t1.c1;
 c1 | c1 
----+----
  1 |  1
  2 |  2
(2 rows)

-- INNER JOIN with where condition.  Should execute where condition separately
-- on remote side.
-- target list order is different for v10.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c2 = 100
  ORDER BY t1.c3, t1.c1;
                                                                                                                       QUERY PLAN                                                                                                                        
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1, t1.c3
   Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r1.`c1`, r2.`c1`, r1.`c3` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (((r1.`c1` = r2.`c1`)))) WHERE ((r1.`c2` = 100)) ORDER BY r1.`c3` IS NULL, r1.`c3` ASC, r1.`c1` IS NULL, r1.`c1` ASC
(4 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c2 = 100
  ORDER BY t1.c3, t1.c1;
 c1 | c1 
----+----
  1 |  1
  2 |  2
(2 rows)

-- INNER JOIN in which join clause is not pushable.
-- target list order is different for v10.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (abs(t1.c1) = t2.c1) WHERE t1.c2 = 100
  ORDER BY t1.c3, t1.c1;
                                                                                                               QUERY PLAN                                                                                                               
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1, t1.c3
   Filter: (abs(t1.c1) = t2.c1)
   Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r1.`c1`, r2.`c1`, r1.`c3` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (TRUE)) WHERE ((r1.`c2` = 100)) ORDER BY r1.`c3` IS NULL, r1.`c3` ASC, r1.`c1` IS NULL, r1.`c1` ASC
(5 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (abs(t1.c1) = t2.c1) WHERE t1.c2 = 100
  ORDER BY t1.c3, t1.c1;
 c1 | c1 
----+----
  1 |  1
  2 |  2
(2 rows)

-- Join three tables
-- target list order is different for v10.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c2, t3.c3
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1) JOIN fdw139_t3 t3 ON (t3.c1 = t1.c1)
  ORDER BY t1.c3, t1.c1;
                                                                                                                                                   QUERY PLAN                                                                                                                                                    
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c2, t3.c3, t1.c3
   Relations: ((mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)) INNER JOIN (mysql_fdw_regress.fdw139_t3 t3)
   Remote query: SELECT r1.`c1`, r2.`c2`, r4.`c3`, r1.`c3` FROM ((`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (((r1.`c1` = r2.`c1`)))) INNER JOIN `mysql_fdw_regress`.`test3` r4 ON (((r1.`c1` = r4.`c1`)))) ORDER BY r1.`c3` IS NULL, r1.`c3` ASC, r1.`c1` IS NULL, r1.`c1` ASC
(4 rows)

SELECT t1.c1, t2.c2, t3.c3
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1) JOIN fdw139_t3 t3 ON (t3.c1 = t1.c1)
  ORDER BY t1.c3, t1.c1;
 c1 | c2  |  c3  
----+-----+------
  1 | 200 | CCC1
  2 | 200 | CCC2
(2 rows)

EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1, t3.c1
  FROM fdw139_t1 t1, fdw139_t2 t2, fdw139_t3 t3 WHERE t1.c1 = 11 AND t2.c1 = 12 AND t3.c1 = 13
  ORDER BY t1.c1;
                                                                                                                            QUERY PLAN                                                                                                                             
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1, t3.c1
   Relations: ((mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)) INNER JOIN (mysql_fdw_regress.fdw139_t3 t3)
   Remote query: SELECT r1.`c1`, r2.`c1`, r3.`c1` FROM ((`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (TRUE)) INNER JOIN `mysql_fdw_regress`.`test3` r3 ON (TRUE)) WHERE ((r3.`c1` = 13)) AND ((r2.`c1` = 12)) AND ((r1.`c1` = 11))
(4 rows)

SELECT t1.c1, t2.c1, t3.c1
  FROM fdw139_t1 t1, fdw139_t2 t2, fdw139_t3 t3 WHERE t1.c1 = 11 AND t2.c1 = 12 AND t3.c1 = 13
  ORDER BY t1.c1;
 c1 | c1 | c1 
----+----+----
 11 | 12 | 13
(1 row)

-- LEFT OUTER JOIN
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 LEFT JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c1, t2.c1 NULLS LAST LIMIT 2 OFFSET 2;
                                                                                                               QUERY PLAN                                                                                                               
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1
   Relations: (mysql_fdw_regress.fdw139_t1 t1) LEFT JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r1.`c1`, r2.`c1` FROM (`mysql_fdw_regress`.`test1` r1 LEFT JOIN `mysql_fdw_regress`.`test2` r2 ON (((r1.`c1` = r2.`c1`)))) ORDER BY r1.`c1` IS NULL, r1.`c1` ASC, r2.`c1` IS NULL, r2.`c1` ASC LIMIT 2 OFFSET 2
(4 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 LEFT JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c1, t2.c1 NULLS LAST LIMIT 2 OFFSET 2;
 c1 | c1 
----+----
 11 |   
(1 row)

-- LEFT JOIN evaluating as INNER JOIN, having unsafe join clause.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 LEFT JOIN fdw139_t2 t2 ON (abs(t1.c1) = t2.c1)
  WHERE t2.c1 > 1 ORDER BY t1.c1, t2.c1;
                                                                                                         QUERY PLAN                                                                                                          
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1
   Filter: (abs(t1.c1) = t2.c1)
   Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r1.`c1`, r2.`c1` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (TRUE)) WHERE ((r2.`c1` > 1)) ORDER BY r1.`c1` IS NULL, r1.`c1` ASC, r2.`c1` IS NULL, r2.`c1` ASC
(5 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 LEFT JOIN fdw139_t2 t2 ON (abs(t1.c1) = t2.c1)
  WHERE t2.c1 > 1 ORDER BY t1.c1, t2.c1;
 c1 | c1 
----+----
  2 |  2
(1 row)

-- LEFT OUTER JOIN in which join clause is not pushable.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 LEFT JOIN fdw139_t2 t2 ON (abs(t1.c1) = t2.c1)
  ORDER BY t1.c1, t2.c1;
                                                   QUERY PLAN                                                   
----------------------------------------------------------------------------------------------------------------
 Sort
   Output: t1.c1, t2.c1
   Sort Key: t1.c1, t2.c1
   ->  Nested Loop Left Join
         Output: t1.c1, t2.c1
         Join Filter: (abs(t1.c1) = t2.c1)
         ->  Foreign Scan on public.fdw139_t1 t1
               Output: t1.c1, t1.c2, t1.c3, t1.c4
               Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1`
         ->  Materialize
               Output: t2.c1
               ->  Foreign Scan on public.fdw139_t2 t2
                     Output: t2.c1
                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test2` ORDER BY `c1` IS NULL, `c1` ASC
(14 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 LEFT JOIN fdw139_t2 t2 ON (abs(t1.c1) = t2.c1)
  ORDER BY t1.c1, t2.c1;
 c1 | c1 
----+----
  1 |  1
  2 |  2
 11 |   
(3 rows)

-- LEFT OUTER JOIN + placement of clauses.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t1.c2, t2.c1, t2.c2
  FROM fdw139_t1 t1 LEFT JOIN (SELECT * FROM fdw139_t2 WHERE c1 < 10) t2 ON (t1.c1 = t2.c1)
  WHERE t1.c1 < 10;
                                                                                                   QUERY PLAN                                                                                                    
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t1.c2, fdw139_t2.c1, fdw139_t2.c2
   Relations: (mysql_fdw_regress.fdw139_t1 t1) LEFT JOIN (mysql_fdw_regress.fdw139_t2)
   Remote query: SELECT r1.`c1`, r1.`c2`, r4.`c1`, r4.`c2` FROM (`mysql_fdw_regress`.`test1` r1 LEFT JOIN `mysql_fdw_regress`.`test2` r4 ON (((r1.`c1` = r4.`c1`)) AND ((r4.`c1` < 10)))) WHERE ((r1.`c1` < 10))
(4 rows)

SELECT t1.c1, t1.c2, t2.c1, t2.c2
  FROM fdw139_t1 t1 LEFT JOIN (SELECT * FROM fdw139_t2 WHERE c1 < 10) t2 ON (t1.c1 = t2.c1)
  WHERE t1.c1 < 10;
 c1 | c2  | c1 | c2  
----+-----+----+-----
  1 | 100 |  1 | 200
  2 | 100 |  2 | 200
(2 rows)

-- Clauses within the nullable side are not pulled up, but the top level clause
-- on nullable side is not pushed down into nullable side
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t1.c2, t2.c1, t2.c2
  FROM fdw139_t1 t1 LEFT JOIN (SELECT * FROM fdw139_t2 WHERE c1 < 10) t2 ON (t1.c1 = t2.c1)
  WHERE (t2.c1 < 10 OR t2.c1 IS NULL) AND t1.c1 < 10;
                                                                                                                         QUERY PLAN                                                                                                                          
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t1.c2, fdw139_t2.c1, fdw139_t2.c2
   Relations: (mysql_fdw_regress.fdw139_t1 t1) LEFT JOIN (mysql_fdw_regress.fdw139_t2)
   Remote query: SELECT r1.`c1`, r1.`c2`, r4.`c1`, r4.`c2` FROM (`mysql_fdw_regress`.`test1` r1 LEFT JOIN `mysql_fdw_regress`.`test2` r4 ON (((r1.`c1` = r4.`c1`)) AND ((r4.`c1` < 10)))) WHERE (((r4.`c1` < 10) OR (r4.`c1` IS NULL))) AND ((r1.`c1` < 10))
(4 rows)

SELECT t1.c1, t1.c2, t2.c1, t2.c2
  FROM fdw139_t1 t1 LEFT JOIN (SELECT * FROM fdw139_t2 WHERE c1 < 10) t2 ON (t1.c1 = t2.c1)
  WHERE (t2.c1 < 10 OR t2.c1 IS NULL) AND t1.c1 < 10;
 c1 | c2  | c1 | c2  
----+-----+----+-----
  1 | 100 |  1 | 200
  2 | 100 |  2 | 200
(2 rows)

-- RIGHT OUTER JOIN
-- target list order is different for v10.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 RIGHT JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t2.c1, t1.c1 NULLS LAST;
                                                                                                      QUERY PLAN                                                                                                       
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1
   Relations: (mysql_fdw_regress.fdw139_t2 t2) LEFT JOIN (mysql_fdw_regress.fdw139_t1 t1)
   Remote query: SELECT r1.`c1`, r2.`c1` FROM (`mysql_fdw_regress`.`test2` r2 LEFT JOIN `mysql_fdw_regress`.`test1` r1 ON (((r1.`c1` = r2.`c1`)))) ORDER BY r2.`c1` IS NULL, r2.`c1` ASC, r1.`c1` IS NULL, r1.`c1` ASC
(4 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 RIGHT JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t2.c1, t1.c1 NULLS LAST;
 c1 | c1 
----+----
  1 |  1
  2 |  2
    | 12
(3 rows)

-- Combinations of various joins
-- INNER JOIN + RIGHT JOIN
-- target list order is different for v10.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c2, t3.c3
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1) RIGHT JOIN fdw139_t3 t3 ON (t1.c1 = t3.c1)
  ORDER BY t1.c1 NULLS LAST, t1.c3, t1.c1;
                                                                                                                                                   QUERY PLAN                                                                                                                                                   
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c2, t3.c3, t1.c3
   Relations: (mysql_fdw_regress.fdw139_t3 t3) LEFT JOIN ((mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2))
   Remote query: SELECT r1.`c1`, r2.`c2`, r4.`c3`, r1.`c3` FROM (`mysql_fdw_regress`.`test3` r4 LEFT JOIN (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (((r1.`c1` = r2.`c1`)))) ON (((r1.`c1` = r4.`c1`)))) ORDER BY r1.`c1` IS NULL, r1.`c1` ASC, r1.`c3` IS NULL, r1.`c3` ASC
(4 rows)

SELECT t1.c1, t2.c2, t3.c3
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1) RIGHT JOIN fdw139_t3 t3 ON (t1.c1 = t3.c1)
  ORDER BY t1.c1 NULLS LAST, t1.c3, t1.c1;
 c1 | c2  |  c3   
----+-----+-------
  1 | 200 | CCC1
  2 | 200 | CCC2
    |     | CCC13
(3 rows)

-- FULL OUTER JOIN, should not be pushdown as target database doesn't support
-- it.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 FULL JOIN fdw139_t1 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c1, t2.c1;
                                                   QUERY PLAN                                                   
----------------------------------------------------------------------------------------------------------------
 Sort
   Output: t1.c1, t2.c1
   Sort Key: t1.c1, t2.c1
   ->  Merge Full Join
         Output: t1.c1, t2.c1
         Merge Cond: (t1.c1 = t2.c1)
         ->  Foreign Scan on public.fdw139_t1 t1
               Output: t1.c1, t1.c2, t1.c3, t1.c4
               Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` ORDER BY `c1` IS NULL, `c1` ASC
         ->  Materialize
               Output: t2.c1, t2.c2, t2.c3, t2.c4
               ->  Foreign Scan on public.fdw139_t1 t2
                     Output: t2.c1, t2.c2, t2.c3, t2.c4
                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` ORDER BY `c1` IS NULL, `c1` ASC
(14 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 FULL JOIN fdw139_t1 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c1, t2.c1;
 c1 | c1 
----+----
  1 |  1
  2 |  2
 11 | 11
(3 rows)

-- Join two tables with FOR UPDATE clause
-- tests whole-row reference for row marks
-- target list order is different for v10.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c3, t1.c1 FOR UPDATE OF t1;
                                                                                                                                  QUERY PLAN                                                                                                                                  
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1, t1.c3, t1.*, t2.*
   Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r1.`c1`, r2.`c1`, r1.`c3`, r1.`c2`, r1.`c4`, r2.`c2`, r2.`c3`, r2.`c4` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (((r1.`c1` = r2.`c1`)))) ORDER BY r1.`c3` IS NULL, r1.`c3` ASC, r1.`c1` IS NULL, r1.`c1` ASC
(4 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c3, t1.c1 FOR UPDATE OF t1;
 c1 | c1 
----+----
  1 |  1
  2 |  2
(2 rows)

-- target list order is different for v10.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c3, t1.c1 FOR UPDATE;
                                                                                                                                  QUERY PLAN                                                                                                                                  
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1, t1.c3, t1.*, t2.*
   Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r1.`c1`, r2.`c1`, r1.`c3`, r1.`c2`, r1.`c4`, r2.`c2`, r2.`c3`, r2.`c4` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (((r1.`c1` = r2.`c1`)))) ORDER BY r1.`c3` IS NULL, r1.`c3` ASC, r1.`c1` IS NULL, r1.`c1` ASC
(4 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c3, t1.c1 FOR UPDATE;
 c1 | c1 
----+----
  1 |  1
  2 |  2
(2 rows)

-- Join two tables with FOR SHARE clause
-- target list order is different for v10.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c3, t1.c1 FOR SHARE OF t1;
                                                                                                                                  QUERY PLAN                                                                                                                                  
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1, t1.c3, t1.*, t2.*
   Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r1.`c1`, r2.`c1`, r1.`c3`, r1.`c2`, r1.`c4`, r2.`c2`, r2.`c3`, r2.`c4` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (((r1.`c1` = r2.`c1`)))) ORDER BY r1.`c3` IS NULL, r1.`c3` ASC, r1.`c1` IS NULL, r1.`c1` ASC
(4 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c3, t1.c1 FOR SHARE OF t1;
 c1 | c1 
----+----
  1 |  1
  2 |  2
(2 rows)

-- target list order is different for v10.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c3, t1.c1 FOR SHARE;
                                                                                                                                  QUERY PLAN                                                                                                                                  
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1, t1.c3, t1.*, t2.*
   Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r1.`c1`, r2.`c1`, r1.`c3`, r1.`c2`, r1.`c4`, r2.`c2`, r2.`c3`, r2.`c4` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (((r1.`c1` = r2.`c1`)))) ORDER BY r1.`c3` IS NULL, r1.`c3` ASC, r1.`c1` IS NULL, r1.`c1` ASC
(4 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c3, t1.c1 FOR SHARE;
 c1 | c1 
----+----
  1 |  1
  2 |  2
(2 rows)

-- Join in CTE.
-- Explain plan difference between v11 (or pre) and later.
EXPLAIN (COSTS false, VERBOSE)
WITH t (c1_1, c1_3, c2_1) AS (
  SELECT t1.c1, t1.c3, t2.c1
    FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
) SELECT c1_1, c2_1 FROM t ORDER BY c1_3, c1_1;
                                                                                                           QUERY PLAN                                                                                                            
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1, t1.c3
   Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r2.`c1`, r3.`c1`, r2.`c3` FROM (`mysql_fdw_regress`.`test1` r2 INNER JOIN `mysql_fdw_regress`.`test2` r3 ON (((r2.`c1` = r3.`c1`)))) ORDER BY r2.`c3` IS NULL, r2.`c3` ASC, r2.`c1` IS NULL, r2.`c1` ASC
(4 rows)

WITH t (c1_1, c1_3, c2_1) AS (
  SELECT t1.c1, t1.c3, t2.c1
    FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
) SELECT c1_1, c2_1 FROM t ORDER BY c1_3, c1_1;
 c1_1 | c2_1 
------+------
    1 |    1
    2 |    2
(2 rows)

-- Whole-row reference
EXPLAIN (COSTS false, VERBOSE)
SELECT t1, t2, t1.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c3, t1.c1;
                                                                                                                                  QUERY PLAN                                                                                                                                  
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.*, t2.*, t1.c1, t1.c3
   Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r1.`c1`, r1.`c2`, r1.`c3`, r1.`c4`, r2.`c1`, r2.`c2`, r2.`c3`, r2.`c4` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (((r1.`c1` = r2.`c1`)))) ORDER BY r1.`c3` IS NULL, r1.`c3` ASC, r1.`c1` IS NULL, r1.`c1` ASC
(4 rows)

SELECT t1, t2, t1.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c3, t1.c1;
        t1        |        t2        | c1 
------------------+------------------+----
 (1,100,AAA1,foo) | (1,200,BBB1,foo) |  1
 (2,100,AAA2,bar) | (2,200,BBB2,bar) |  2
(2 rows)

-- SEMI JOIN, not pushed down
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1
  FROM fdw139_t1 t1 WHERE EXISTS (SELECT 1 FROM fdw139_t2 t2 WHERE t1.c1 = t2.c1)
  ORDER BY t1.c1;
                                                QUERY PLAN                                                
----------------------------------------------------------------------------------------------------------
 Nested Loop Semi Join
   Output: t1.c1
   Join Filter: (t1.c1 = t2.c1)
   ->  Foreign Scan on public.fdw139_t1 t1
         Output: t1.c1, t1.c2, t1.c3, t1.c4
         Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` ORDER BY `c1` IS NULL, `c1` ASC
   ->  Materialize
         Output: t2.c1
         ->  Foreign Scan on public.fdw139_t2 t2
               Output: t2.c1
               Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test2` ORDER BY `c1` IS NULL, `c1` ASC
(11 rows)

SELECT t1.c1
  FROM fdw139_t1 t1 WHERE EXISTS (SELECT 1 FROM fdw139_t2 t2 WHERE t1.c1 = t2.c1)
  ORDER BY t1.c1;
 c1 
----
  1
  2
(2 rows)

-- ANTI JOIN, not pushed down
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1
  FROM fdw139_t1 t1 WHERE NOT EXISTS (SELECT 1 FROM fdw139_t2 t2 WHERE t1.c1 = t2.c2)
  ORDER BY t1.c1 LIMIT 2;
                                                   QUERY PLAN                                                   
----------------------------------------------------------------------------------------------------------------
 Limit
   Output: t1.c1
   ->  Nested Loop Anti Join
         Output: t1.c1
         Join Filter: (t1.c1 = t2.c2)
         ->  Foreign Scan on public.fdw139_t1 t1
               Output: t1.c1, t1.c2, t1.c3, t1.c4
               Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` ORDER BY `c1` IS NULL, `c1` ASC
         ->  Materialize
               Output: t2.c2
               ->  Foreign Scan on public.fdw139_t2 t2
                     Output: t2.c2
                     Remote query: SELECT `c2` FROM `mysql_fdw_regress`.`test2` ORDER BY `c2` IS NULL, `c2` ASC
(13 rows)

SELECT t1.c1
  FROM fdw139_t1 t1 WHERE NOT EXISTS (SELECT 1 FROM fdw139_t2 t2 WHERE t1.c1 = t2.c2)
  ORDER BY t1.c1 LIMIT 2;
 c1 
----
  1
  2
(2 rows)

-- CROSS JOIN can be pushed down
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 CROSS JOIN fdw139_t2 t2
  ORDER BY t1.c1, t2.c1 LIMIT round(5.4) OFFSET 2;
                                                                                                       QUERY PLAN                                                                                                       
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1
   Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r1.`c1`, r2.`c1` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (TRUE)) ORDER BY r1.`c1` IS NULL, r1.`c1` ASC, r2.`c1` IS NULL, r2.`c1` ASC LIMIT 5 OFFSET 2
(4 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 CROSS JOIN fdw139_t2 t2
  ORDER BY t1.c1, t2.c1 LIMIT round(5.4) OFFSET 2;
 c1 | c1 
----+----
  1 | 12
  2 |  1
  2 |  2
  2 | 12
 11 |  1
(5 rows)

-- CROSS JOIN combined with local table.
CREATE TABLE local_t1(c1 int);
INSERT INTO local_t1 VALUES (1), (2);
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1, l1.c1
  FROM fdw139_t1 t1 CROSS JOIN fdw139_t2 t2 CROSS JOIN local_t1 l1
  ORDER BY t1.c1, t2.c1, l1.c1 LIMIT 8 OFFSET round(2.2);
                                                                     QUERY PLAN                                                                      
-----------------------------------------------------------------------------------------------------------------------------------------------------
 Limit
   Output: t1.c1, t2.c1, l1.c1
   ->  Sort
         Output: t1.c1, t2.c1, l1.c1
         Sort Key: t1.c1, t2.c1, l1.c1
         ->  Nested Loop
               Output: t1.c1, t2.c1, l1.c1
               ->  Seq Scan on public.local_t1 l1
                     Output: l1.c1
               ->  Foreign Scan
                     Output: t1.c1, t2.c1
                     Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
                     Remote query: SELECT r1.`c1`, r2.`c1` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (TRUE))
(13 rows)

SELECT t1.c1, t2.c1, l1.c1
  FROM fdw139_t1 t1 CROSS JOIN fdw139_t2 t2 CROSS JOIN local_t1 l1
  ORDER BY t1.c1, t2.c1, l1.c1 LIMIT 8 OFFSET round(2.2);
 c1 | c1 | c1 
----+----+----
  1 |  2 |  1
  1 |  2 |  2
  1 | 12 |  1
  1 | 12 |  2
  2 |  1 |  1
  2 |  1 |  2
  2 |  2 |  1
  2 |  2 |  2
(8 rows)

SELECT count(t1.c1)
  FROM fdw139_t1 t1 CROSS JOIN fdw139_t2 t2 CROSS JOIN local_t1 l1;
 count 
-------
    18
(1 row)

-- Join two tables from two different foreign table
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t4 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c3, t1.c1;
                                                            QUERY PLAN                                                            
----------------------------------------------------------------------------------------------------------------------------------
 Nested Loop
   Output: t1.c1, t2.c1, t1.c3
   Join Filter: (t1.c1 = t2.c1)
   ->  Foreign Scan on public.fdw139_t1 t1
         Output: t1.c1, t1.c2, t1.c3, t1.c4
         Remote query: SELECT `c1`, `c3` FROM `mysql_fdw_regress`.`test1` ORDER BY `c3` IS NULL, `c3` ASC, `c1` IS NULL, `c1` ASC
   ->  Materialize
         Output: t2.c1
         ->  Foreign Scan on public.fdw139_t4 t2
               Output: t2.c1
               Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test3` ORDER BY `c1` IS NULL, `c1` ASC
(11 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t4 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c3, t1.c1;
 c1 | c1 
----+----
  1 |  1
  2 |  2
(2 rows)

-- Unsafe join conditions (c4 has a UDT), not pushed down.
EXPLAIN (VERBOSE, COSTS OFF)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 LEFT JOIN fdw139_t2 t2 ON (t1.c4 = t2.c4)
  ORDER BY t1.c1, t2.c1;
                                      QUERY PLAN                                      
--------------------------------------------------------------------------------------
 Sort
   Output: t1.c1, t2.c1
   Sort Key: t1.c1, t2.c1
   ->  Nested Loop Left Join
         Output: t1.c1, t2.c1
         Join Filter: (t1.c4 = t2.c4)
         ->  Foreign Scan on public.fdw139_t1 t1
               Output: t1.c1, t1.c2, t1.c3, t1.c4
               Remote query: SELECT `c1`, `c4` FROM `mysql_fdw_regress`.`test1`
         ->  Materialize
               Output: t2.c1, t2.c4
               ->  Foreign Scan on public.fdw139_t2 t2
                     Output: t2.c1, t2.c4
                     Remote query: SELECT `c1`, `c4` FROM `mysql_fdw_regress`.`test2`
(14 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 LEFT JOIN fdw139_t2 t2 ON (t1.c4 = t2.c4)
  ORDER BY t1.c1, t2.c1;
 c1 | c1 
----+----
  1 |  1
  1 | 12
  2 |  2
 11 |  1
 11 | 12
(5 rows)

-- Unsafe conditions on one side (c4 has a UDT), not pushed down.
EXPLAIN (VERBOSE, COSTS OFF)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 LEFT JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c4 = 'foo'
  ORDER BY t1.c1, t2.c1 NULLS LAST;
                                                   QUERY PLAN                                                   
----------------------------------------------------------------------------------------------------------------
 Incremental Sort
   Output: t1.c1, t2.c1
   Sort Key: t1.c1, t2.c1
   Presorted Key: t1.c1
   ->  Nested Loop Left Join
         Output: t1.c1, t2.c1
         Join Filter: (t1.c1 = t2.c1)
         ->  Foreign Scan on public.fdw139_t1 t1
               Output: t1.c1, t1.c2, t1.c3, t1.c4
               Filter: (t1.c4 = 'foo'::user_enum)
               Remote query: SELECT `c1`, `c4` FROM `mysql_fdw_regress`.`test1` ORDER BY `c1` IS NULL, `c1` ASC
         ->  Materialize
               Output: t2.c1
               ->  Foreign Scan on public.fdw139_t2 t2
                     Output: t2.c1
                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test2` ORDER BY `c1` IS NULL, `c1` ASC
(16 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 LEFT JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c4 = 'foo'
  ORDER BY t1.c1, t2.c1 NULLS LAST;
 c1 | c1 
----+----
  1 |  1
 11 |   
(2 rows)

-- Join where unsafe to pushdown condition in WHERE clause has a column not
-- in the SELECT clause.  In this test unsafe clause needs to have column
-- references from both joining sides so that the clause is not pushed down
-- into one of the joining sides.
-- target list order is different for v10.
EXPLAIN (VERBOSE, COSTS OFF)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c4 = t2.c4
  ORDER BY t1.c3, t1.c1;
                                                                                                                    QUERY PLAN                                                                                                                     
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1, t1.c3
   Filter: (t1.c4 = t2.c4)
   Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r1.`c1`, r2.`c1`, r1.`c3`, r1.`c4`, r2.`c4` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (((r1.`c1` = r2.`c1`)))) ORDER BY r1.`c3` IS NULL, r1.`c3` ASC, r1.`c1` IS NULL, r1.`c1` ASC
(5 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1) WHERE t1.c4 = t2.c4
  ORDER BY t1.c3, t1.c1;
 c1 | c1 
----+----
  1 |  1
  2 |  2
(2 rows)

-- Check join pushdown in situations where multiple userids are involved
CREATE ROLE regress_view_owner SUPERUSER;
CREATE USER MAPPING FOR regress_view_owner
  SERVER mysql_svr OPTIONS (username :MYSQL_USER_NAME, password :MYSQL_PASS);
GRANT SELECT ON fdw139_t1 TO regress_view_owner;
GRANT SELECT ON fdw139_t2 TO regress_view_owner;
CREATE VIEW v1 AS SELECT * FROM fdw139_t1;
CREATE VIEW v2 AS SELECT * FROM fdw139_t2;
ALTER VIEW v2 OWNER TO regress_view_owner;
EXPLAIN (VERBOSE, COSTS OFF)
SELECT t1.c1, t2.c2
  FROM v1 t1 LEFT JOIN v2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c1, t2.c1, t2.c2 NULLS LAST;  -- not pushed down, different view owners
                                                      QUERY PLAN                                                      
----------------------------------------------------------------------------------------------------------------------
 Incremental Sort
   Output: fdw139_t1.c1, fdw139_t2.c2, fdw139_t2.c1
   Sort Key: fdw139_t1.c1, fdw139_t2.c1, fdw139_t2.c2
   Presorted Key: fdw139_t1.c1
   ->  Nested Loop Left Join
         Output: fdw139_t1.c1, fdw139_t2.c2, fdw139_t2.c1
         Join Filter: (fdw139_t1.c1 = fdw139_t2.c1)
         ->  Foreign Scan on public.fdw139_t1
               Output: fdw139_t1.c1, fdw139_t1.c2, fdw139_t1.c3, fdw139_t1.c4
               Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` ORDER BY `c1` IS NULL, `c1` ASC
         ->  Materialize
               Output: fdw139_t2.c2, fdw139_t2.c1
               ->  Foreign Scan on public.fdw139_t2
                     Output: fdw139_t2.c2, fdw139_t2.c1
                     Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test2` ORDER BY `c1` IS NULL, `c1` ASC
(15 rows)

SELECT t1.c1, t2.c2
  FROM v1 t1 LEFT JOIN v2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c1, t2.c1, t2.c2 NULLS LAST;
 c1 | c2  
----+-----
  1 | 200
  2 | 200
 11 |    
(3 rows)

ALTER VIEW v1 OWNER TO regress_view_owner;
EXPLAIN (VERBOSE, COSTS OFF)
SELECT t1.c1, t2.c2
  FROM v1 t1 LEFT JOIN v2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c1, t2.c1, t2.c2 NULLS LAST;  -- pushed down
                                                                                                                          QUERY PLAN                                                                                                                          
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: fdw139_t1.c1, fdw139_t2.c2, fdw139_t2.c1
   Relations: (mysql_fdw_regress.fdw139_t1) LEFT JOIN (mysql_fdw_regress.fdw139_t2)
   Remote query: SELECT r4.`c1`, r5.`c2`, r5.`c1` FROM (`mysql_fdw_regress`.`test1` r4 LEFT JOIN `mysql_fdw_regress`.`test2` r5 ON (((r4.`c1` = r5.`c1`)))) ORDER BY r4.`c1` IS NULL, r4.`c1` ASC, r5.`c1` IS NULL, r5.`c1` ASC, r5.`c2` IS NULL, r5.`c2` ASC
(4 rows)

SELECT t1.c1, t2.c2
  FROM v1 t1 LEFT JOIN v2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c1, t2.c1, t2.c2 NULLS LAST;
 c1 | c2  
----+-----
  1 | 200
  2 | 200
 11 |    
(3 rows)

EXPLAIN (VERBOSE, COSTS OFF)
SELECT t1.c1, t2.c2
  FROM v1 t1 LEFT JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c1, t2.c1, t2.c2 NULLS LAST;  -- not pushed down, view owner not current user
                                                      QUERY PLAN                                                      
----------------------------------------------------------------------------------------------------------------------
 Incremental Sort
   Output: fdw139_t1.c1, t2.c2, t2.c1
   Sort Key: fdw139_t1.c1, t2.c1, t2.c2
   Presorted Key: fdw139_t1.c1
   ->  Nested Loop Left Join
         Output: fdw139_t1.c1, t2.c2, t2.c1
         Join Filter: (fdw139_t1.c1 = t2.c1)
         ->  Foreign Scan on public.fdw139_t1
               Output: fdw139_t1.c1, fdw139_t1.c2, fdw139_t1.c3, fdw139_t1.c4
               Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` ORDER BY `c1` IS NULL, `c1` ASC
         ->  Materialize
               Output: t2.c2, t2.c1
               ->  Foreign Scan on public.fdw139_t2 t2
                     Output: t2.c2, t2.c1
                     Remote query: SELECT `c1`, `c2` FROM `mysql_fdw_regress`.`test2` ORDER BY `c1` IS NULL, `c1` ASC
(15 rows)

SELECT t1.c1, t2.c2
  FROM v1 t1 LEFT JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c1, t2.c1, t2.c2 NULLS LAST;
 c1 | c2  
----+-----
  1 | 200
  2 | 200
 11 |    
(3 rows)

ALTER VIEW v1 OWNER TO CURRENT_USER;
EXPLAIN (VERBOSE, COSTS OFF)
SELECT t1.c1, t2.c2
  FROM v1 t1 LEFT JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c1, t2.c1, t2.c2 NULLS LAST;  -- pushed down
                                                                                                                          QUERY PLAN                                                                                                                          
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: fdw139_t1.c1, t2.c2, t2.c1
   Relations: (mysql_fdw_regress.fdw139_t1) LEFT JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r4.`c1`, r2.`c2`, r2.`c1` FROM (`mysql_fdw_regress`.`test1` r4 LEFT JOIN `mysql_fdw_regress`.`test2` r2 ON (((r4.`c1` = r2.`c1`)))) ORDER BY r4.`c1` IS NULL, r4.`c1` ASC, r2.`c1` IS NULL, r2.`c1` ASC, r2.`c2` IS NULL, r2.`c2` ASC
(4 rows)

SELECT t1.c1, t2.c2
  FROM v1 t1 LEFT JOIN fdw139_t2 t2 ON (t1.c1 = t2.c1)
  ORDER BY t1.c1, t2.c1, t2.c2 NULLS LAST;
 c1 | c2  
----+-----
  1 | 200
  2 | 200
 11 |    
(3 rows)

ALTER VIEW v1 OWNER TO regress_view_owner;
-- Non-Var items in targetlist of the nullable rel of a join preventing
-- push-down in some cases
-- Unable to push {fdw139_t1, fdw139_t2}
EXPLAIN (VERBOSE, COSTS OFF)
SELECT q.a, fdw139_t2.c1
  FROM (SELECT 13 FROM fdw139_t1 WHERE c1 = 13) q(a) RIGHT JOIN fdw139_t2 ON (q.a = fdw139_t2.c1)
  WHERE fdw139_t2.c1 BETWEEN 10 AND 15;
                                                                 QUERY PLAN                                                                 
--------------------------------------------------------------------------------------------------------------------------------------------
 Nested Loop Left Join
   Output: (13), fdw139_t2.c1
   Join Filter: (13 = fdw139_t2.c1)
   ->  Foreign Scan on public.fdw139_t2
         Output: fdw139_t2.c1, fdw139_t2.c2, fdw139_t2.c3, fdw139_t2.c4
         Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test2` WHERE ((`c1` >= 10)) AND ((`c1` <= 15)) ORDER BY `c1` IS NULL, `c1` ASC
   ->  Materialize
         Output: (13)
         ->  Foreign Scan on public.fdw139_t1
               Output: 13
               Remote query: SELECT NULL FROM `mysql_fdw_regress`.`test1` WHERE ((`c1` = 13))
(11 rows)

SELECT q.a, fdw139_t2.c1
  FROM (SELECT 13 FROM fdw139_t1 WHERE c1 = 13) q(a) RIGHT JOIN fdw139_t2 ON (q.a = fdw139_t2.c1)
  WHERE fdw139_t2.c1 BETWEEN 10 AND 15;
 a | c1 
---+----
   | 12
(1 row)

-- Ok to push {fdw139_t1, fdw139_t2 but not {fdw139_t1, fdw139_t2, fdw139_t3}
EXPLAIN (VERBOSE, COSTS OFF)
SELECT fdw139_t3.c1, q.*
  FROM fdw139_t3 LEFT JOIN (
    SELECT 13, fdw139_t1.c1, fdw139_t2.c1
    FROM fdw139_t1 RIGHT JOIN fdw139_t2 ON (fdw139_t1.c1 = fdw139_t2.c1)
    WHERE fdw139_t1.c1 = 11
  ) q(a, b, c) ON (fdw139_t3.c1 = q.b)
  WHERE fdw139_t3.c1 BETWEEN 10 AND 15;
                                                                                                        QUERY PLAN                                                                                                         
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Nested Loop Left Join
   Output: fdw139_t3.c1, (13), fdw139_t1.c1, fdw139_t2.c1
   Join Filter: (fdw139_t3.c1 = fdw139_t1.c1)
   ->  Foreign Scan on public.fdw139_t3
         Output: fdw139_t3.c1, fdw139_t3.c2, fdw139_t3.c3
         Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test3` WHERE ((`c1` >= 10)) AND ((`c1` <= 15)) ORDER BY `c1` IS NULL, `c1` ASC
   ->  Foreign Scan
         Output: fdw139_t1.c1, fdw139_t2.c1, 13
         Relations: (mysql_fdw_regress.fdw139_t1) INNER JOIN (mysql_fdw_regress.fdw139_t2)
         Remote query: SELECT r4.`c1`, r5.`c1` FROM (`mysql_fdw_regress`.`test1` r4 INNER JOIN `mysql_fdw_regress`.`test2` r5 ON (TRUE)) WHERE ((r5.`c1` = 11)) AND ((r4.`c1` = 11)) ORDER BY r4.`c1` IS NULL, r4.`c1` ASC
(10 rows)

SELECT fdw139_t3.c1, q.*
  FROM fdw139_t3 LEFT JOIN (
    SELECT 13, fdw139_t1.c1, fdw139_t2.c1
    FROM fdw139_t1 RIGHT JOIN fdw139_t2 ON (fdw139_t1.c1 = fdw139_t2.c1)
    WHERE fdw139_t1.c1 = 11
  ) q(a, b, c) ON (fdw139_t3.c1 = q.b)
  WHERE fdw139_t3.c1 BETWEEN 10 AND 15;
 c1 | a | b | c 
----+---+---+---
 13 |   |   |  
(1 row)

-- FDW-129: Limit and offset pushdown with join pushdown.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (TRUE)
  ORDER BY t1.c1, t2.c1 LIMIT round(2.2) OFFSET 2;
                                                                                                       QUERY PLAN                                                                                                       
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1
   Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r1.`c1`, r2.`c1` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (TRUE)) ORDER BY r1.`c1` IS NULL, r1.`c1` ASC, r2.`c1` IS NULL, r2.`c1` ASC LIMIT 2 OFFSET 2
(4 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (TRUE)
  ORDER BY t1.c1, t2.c1 LIMIT round(2.2) OFFSET 2;
 c1 | c1 
----+----
  1 | 12
  2 |  1
(2 rows)

-- Limit as NULL, no LIMIT/OFFSET pushdown.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (TRUE)
  ORDER BY t1.c1, t2.c1 LIMIT NULL OFFSET 1;
                                                                                                 QUERY PLAN                                                                                                  
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit
   Output: t1.c1, t2.c1
   ->  Foreign Scan
         Output: t1.c1, t2.c1
         Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
         Remote query: SELECT r1.`c1`, r2.`c1` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (TRUE)) ORDER BY r1.`c1` IS NULL, r1.`c1` ASC, r2.`c1` IS NULL, r2.`c1` ASC
(6 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (TRUE)
  ORDER BY t1.c1, t2.c1 LIMIT NULL OFFSET 1;
 c1 | c1 
----+----
  1 |  2
  1 | 12
  2 |  1
  2 |  2
  2 | 12
 11 |  1
 11 |  2
 11 | 12
(8 rows)

-- Limit as ALL, no LIMIT/OFFSET pushdown.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (TRUE)
  ORDER BY t1.c1, t2.c1 LIMIT ALL OFFSET 1;
                                                                                                 QUERY PLAN                                                                                                  
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Limit
   Output: t1.c1, t2.c1
   ->  Foreign Scan
         Output: t1.c1, t2.c1
         Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
         Remote query: SELECT r1.`c1`, r2.`c1` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (TRUE)) ORDER BY r1.`c1` IS NULL, r1.`c1` ASC, r2.`c1` IS NULL, r2.`c1` ASC
(6 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (TRUE)
  ORDER BY t1.c1, t2.c1 LIMIT ALL OFFSET 1;
 c1 | c1 
----+----
  1 |  2
  1 | 12
  2 |  1
  2 |  2
  2 | 12
 11 |  1
 11 |  2
 11 | 12
(8 rows)

-- Offset as NULL, no LIMIT/OFFSET pushdown.
EXPLAIN (COSTS false, VERBOSE)
SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (TRUE)
  ORDER BY t1.c1, t2.c1 LIMIT 3 OFFSET NULL;
                                                                                                  QUERY PLAN                                                                                                   
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Foreign Scan
   Output: t1.c1, t2.c1
   Relations: (mysql_fdw_regress.fdw139_t1 t1) INNER JOIN (mysql_fdw_regress.fdw139_t2 t2)
   Remote query: SELECT r1.`c1`, r2.`c1` FROM (`mysql_fdw_regress`.`test1` r1 INNER JOIN `mysql_fdw_regress`.`test2` r2 ON (TRUE)) ORDER BY r1.`c1` IS NULL, r1.`c1` ASC, r2.`c1` IS NULL, r2.`c1` ASC LIMIT 3
(4 rows)

SELECT t1.c1, t2.c1
  FROM fdw139_t1 t1 JOIN fdw139_t2 t2 ON (TRUE)
  ORDER BY t1.c1, t2.c1 LIMIT 3 OFFSET NULL;
 c1 | c1 
----+----
  1 |  1
  1 |  2
  1 | 12
(3 rows)

-- Delete existing data and load new data for partition-wise join test cases.
DROP OWNED BY regress_view_owner;
DROP ROLE regress_view_owner;
DELETE FROM fdw139_t1;
DELETE FROM fdw139_t2;
DELETE FROM fdw139_t3;
INSERT INTO fdw139_t1 values(1, 1, 'AAA1', 'foo');
INSERT INTO fdw139_t1 values(2, 2, 'AAA2', 'bar');
INSERT INTO fdw139_t1 values(3, 3, 'AAA11', 'foo');
INSERT INTO fdw139_t1 values(4, 4, 'AAA12', 'foo');
INSERT INTO fdw139_t2 values(5, 5, 'BBB1', 'foo');
INSERT INTO fdw139_t2 values(6, 6, 'BBB2', 'bar');
INSERT INTO fdw139_t2 values(7, 7, 'BBB11', 'foo');
INSERT INTO fdw139_t2 values(8, 8, 'BBB12', 'foo');
INSERT INTO fdw139_t3 values(1, 1, 'CCC1');
INSERT INTO fdw139_t3 values(2, 2, 'CCC2');
INSERT INTO fdw139_t3 values(3, 3, 'CCC13');
INSERT INTO fdw139_t3 values(4, 4, 'CCC14');
DROP FOREIGN TABLE fdw139_t4;
CREATE FOREIGN TABLE tmp_t4(c1 int, c2 int, c3 text)
  SERVER mysql_svr1 OPTIONS(dbname 'mysql_fdw_regress', table_name 'test4');
INSERT INTO tmp_t4 values(5, 5, 'CCC1');
INSERT INTO tmp_t4 values(6, 6, 'CCC2');
INSERT INTO tmp_t4 values(7, 7, 'CCC13');
INSERT INTO tmp_t4 values(8, 8, 'CCC13');
-- Test partition-wise join
SET enable_partitionwise_join TO on;
-- Create the partition table.
CREATE TABLE fprt1 (c1 int, c2 int, c3 varchar, c4 varchar) PARTITION BY RANGE(c1);
CREATE FOREIGN TABLE ftprt1_p1 PARTITION OF fprt1 FOR VALUES FROM (1) TO (4)
  SERVER mysql_svr OPTIONS (dbname 'mysql_fdw_regress', table_name 'test1');
CREATE FOREIGN TABLE ftprt1_p2 PARTITION OF fprt1 FOR VALUES FROM (5) TO (8)
  SERVER mysql_svr OPTIONS (dbname 'mysql_fdw_regress', TABLE_NAME 'test2');
CREATE TABLE fprt2 (c1 int, c2 int, c3 varchar) PARTITION BY RANGE(c2);
CREATE FOREIGN TABLE ftprt2_p1 PARTITION OF fprt2 FOR VALUES FROM (1) TO (4)
  SERVER mysql_svr OPTIONS (dbname 'mysql_fdw_regress', table_name 'test3');
CREATE FOREIGN TABLE ftprt2_p2 PARTITION OF fprt2 FOR VALUES FROM (5) TO (8)
  SERVER mysql_svr OPTIONS (dbname 'mysql_fdw_regress', TABLE_NAME 'test4');
-- Inner join three tables
-- Different explain plan on v10 as partition-wise join is not supported there.
EXPLAIN (VERBOSE, COSTS OFF)
SELECT t1.c1,t2.c2,t3.c3
  FROM fprt1 t1 INNER JOIN fprt2 t2 ON (t1.c1 = t2.c2) INNER JOIN fprt1 t3 ON (t2.c2 = t3.c1)
  WHERE t1.c1 % 2 =0 ORDER BY 1,2,3;
                                                                                                                                                                  QUERY PLAN                                                                                                                                                                   
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Merge Append
   Sort Key: t1.c1, t3.c3
   ->  Foreign Scan
         Output: t1_1.c1, t2_1.c2, t3_1.c3
         Relations: ((mysql_fdw_regress.ftprt1_p1 t1) INNER JOIN (mysql_fdw_regress.ftprt2_p1 t2)) INNER JOIN (mysql_fdw_regress.ftprt1_p1 t3)
         Remote query: SELECT r6.`c1`, r8.`c2`, r10.`c3` FROM ((`mysql_fdw_regress`.`test1` r6 INNER JOIN `mysql_fdw_regress`.`test3` r8 ON (((r6.`c1` = r8.`c2`)))) INNER JOIN `mysql_fdw_regress`.`test1` r10 ON (((r6.`c1` = r10.`c1`)))) WHERE (((r6.`c1` % 2) = 0)) ORDER BY r6.`c1` IS NULL, r6.`c1` ASC, r10.`c3` IS NULL, r10.`c3` ASC
   ->  Foreign Scan
         Output: t1_2.c1, t2_2.c2, t3_2.c3
         Relations: ((mysql_fdw_regress.ftprt1_p2 t1) INNER JOIN (mysql_fdw_regress.ftprt2_p2 t2)) INNER JOIN (mysql_fdw_regress.ftprt1_p2 t3)
         Remote query: SELECT r7.`c1`, r9.`c2`, r11.`c3` FROM ((`mysql_fdw_regress`.`test2` r7 INNER JOIN `mysql_fdw_regress`.`test4` r9 ON (((r7.`c1` = r9.`c2`)))) INNER JOIN `mysql_fdw_regress`.`test2` r11 ON (((r7.`c1` = r11.`c1`)))) WHERE (((r7.`c1` % 2) = 0)) ORDER BY r7.`c1` IS NULL, r7.`c1` ASC, r11.`c3` IS NULL, r11.`c3` ASC
(10 rows)

SELECT t1.c1,t2.c2,t3.c3
  FROM fprt1 t1 INNER JOIN fprt2 t2 ON (t1.c1 = t2.c2) INNER JOIN fprt1 t3 ON (t2.c2 = t3.c1)
  WHERE t1.c1 % 2 =0 ORDER BY 1,2,3;
 c1 | c2 |  c3   
----+----+-------
  2 |  2 | AAA2
  4 |  4 | AAA12
  6 |  6 | BBB2
  8 |  8 | BBB12
(4 rows)

-- With whole-row reference; partitionwise join does not apply
EXPLAIN (VERBOSE, COSTS false)
SELECT t1, t2, t1.c1
  FROM fprt1 t1 JOIN fprt2 t2 ON (t1.c1 = t2.c2)
  ORDER BY t1.c3, t1.c1;
                                                                     QUERY PLAN                                                                     
----------------------------------------------------------------------------------------------------------------------------------------------------
 Nested Loop
   Output: ((t1.*)::fprt1), ((t2.*)::fprt2), t1.c1, t1.c3
   Join Filter: (t1.c1 = t2.c2)
   ->  Merge Append
         Sort Key: t1.c3, t1.c1
         ->  Foreign Scan on public.ftprt1_p1 t1_1
               Output: t1_1.*, t1_1.c1, t1_1.c3
               Remote query: SELECT `c1`, `c2`, `c3`, `c4` FROM `mysql_fdw_regress`.`test1` ORDER BY `c3` IS NULL, `c3` ASC, `c1` IS NULL, `c1` ASC
         ->  Foreign Scan on public.ftprt1_p2 t1_2
               Output: t1_2.*, t1_2.c1, t1_2.c3
               Remote query: SELECT `c1`, `c2`, `c3`, `c4` FROM `mysql_fdw_regress`.`test2` ORDER BY `c3` IS NULL, `c3` ASC, `c1` IS NULL, `c1` ASC
   ->  Materialize
         Output: ((t2.*)::fprt2), t2.c2
         ->  Append
               ->  Foreign Scan on public.ftprt2_p1 t2_1
                     Output: t2_1.*, t2_1.c2
                     Remote query: SELECT `c1`, `c2`, `c3` FROM `mysql_fdw_regress`.`test3` ORDER BY `c2` IS NULL, `c2` ASC
               ->  Foreign Scan on public.ftprt2_p2 t2_2
                     Output: t2_2.*, t2_2.c2
                     Remote query: SELECT `c1`, `c2`, `c3` FROM `mysql_fdw_regress`.`test4` ORDER BY `c2` IS NULL, `c2` ASC
(20 rows)

SELECT t1, t2, t1.c1
  FROM fprt1 t1 JOIN fprt2 t2 ON (t1.c1 = t2.c2)
  ORDER BY t1.c3, t1.c1;
       t1        |     t2      | c1 
-----------------+-------------+----
 (1,1,AAA1,foo)  | (1,1,CCC1)  |  1
 (3,3,AAA11,foo) | (3,3,CCC13) |  3
 (4,4,AAA12,foo) | (4,4,CCC14) |  4
 (2,2,AAA2,bar)  | (2,2,CCC2)  |  2
 (5,5,BBB1,foo)  | (5,5,CCC1)  |  5
 (7,7,BBB11,foo) | (7,7,CCC13) |  7
 (8,8,BBB12,foo) | (8,8,CCC13) |  8
 (6,6,BBB2,bar)  | (6,6,CCC2)  |  6
(8 rows)

-- Join with lateral reference
-- Different explain plan on v10 as partition-wise join is not supported there.
EXPLAIN (VERBOSE, COSTS OFF)
SELECT t1.c1,t1.c2
  FROM fprt1 t1, LATERAL (SELECT t2.c1, t2.c2 FROM fprt2 t2
  WHERE t1.c1 = t2.c2 AND t1.c2 = t2.c1) q WHERE t1.c1 % 2 = 0 ORDER BY 1,2;
                                                                                                                                     QUERY PLAN                                                                                                                                     
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Merge Append
   Sort Key: t1.c1, t1.c2
   ->  Foreign Scan
         Output: t1_1.c1, t1_1.c2
         Relations: (mysql_fdw_regress.ftprt1_p1 t1) INNER JOIN (mysql_fdw_regress.ftprt2_p1 t2)
         Remote query: SELECT r4.`c1`, r4.`c2` FROM (`mysql_fdw_regress`.`test1` r4 INNER JOIN `mysql_fdw_regress`.`test3` r6 ON (((r4.`c1` = r6.`c2`)) AND ((r4.`c2` = r6.`c1`)))) WHERE (((r4.`c1` % 2) = 0)) ORDER BY r4.`c1` IS NULL, r4.`c1` ASC, r4.`c2` IS NULL, r4.`c2` ASC
   ->  Foreign Scan
         Output: t1_2.c1, t1_2.c2
         Relations: (mysql_fdw_regress.ftprt1_p2 t1) INNER JOIN (mysql_fdw_regress.ftprt2_p2 t2)
         Remote query: SELECT r5.`c1`, r5.`c2` FROM (`mysql_fdw_regress`.`test2` r5 INNER JOIN `mysql_fdw_regress`.`test4` r7 ON (((r5.`c1` = r7.`c2`)) AND ((r5.`c2` = r7.`c1`)))) WHERE (((r5.`c1` % 2) = 0)) ORDER BY r5.`c1` IS NULL, r5.`c1` ASC, r5.`c2` IS NULL, r5.`c2` ASC
(10 rows)

SELECT t1.c1,t1.c2
  FROM fprt1 t1, LATERAL (SELECT t2.c1, t2.c2 FROM fprt2 t2
  WHERE t1.c1 = t2.c2 AND t1.c2 = t2.c1) q WHERE t1.c1 % 2 = 0 ORDER BY 1,2;
 c1 | c2 
----+----
  2 |  2
  4 |  4
  6 |  6
  8 |  8
(4 rows)

-- With PHVs, partitionwise join selected but no join pushdown
EXPLAIN (VERBOSE, COSTS OFF)
SELECT t1.c1, t1.phv, t2.c2, t2.phv
  FROM (SELECT 't1_phv' phv, * FROM fprt1 WHERE c1 % 2 = 0) t1 LEFT JOIN
    (SELECT 't2_phv' phv, * FROM fprt2 WHERE c2 % 2 = 0) t2 ON (t1.c1 = t2.c2)
  ORDER BY t1.c1, t2.c2;
                                                                  QUERY PLAN                                                                   
-----------------------------------------------------------------------------------------------------------------------------------------------
 Incremental Sort
   Output: fprt1.c1, 't1_phv'::text, fprt2.c2, ('t2_phv'::text)
   Sort Key: fprt1.c1, fprt2.c2
   Presorted Key: fprt1.c1
   ->  Merge Append
         Sort Key: fprt1.c1
         ->  Nested Loop Left Join
               Output: fprt1_1.c1, 't1_phv'::text, fprt2_1.c2, ('t2_phv'::text)
               Join Filter: (fprt1_1.c1 = fprt2_1.c2)
               ->  Foreign Scan on public.ftprt1_p1 fprt1_1
                     Output: fprt1_1.c1
                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test1` WHERE (((`c1` % 2) = 0)) ORDER BY `c1` IS NULL, `c1` ASC
               ->  Materialize
                     Output: fprt2_1.c2, ('t2_phv'::text)
                     ->  Foreign Scan on public.ftprt2_p1 fprt2_1
                           Output: fprt2_1.c2, 't2_phv'::text
                           Remote query: SELECT `c2` FROM `mysql_fdw_regress`.`test3` WHERE (((`c2` % 2) = 0)) ORDER BY `c2` IS NULL, `c2` ASC
         ->  Nested Loop Left Join
               Output: fprt1_2.c1, 't1_phv'::text, fprt2_2.c2, ('t2_phv'::text)
               Join Filter: (fprt1_2.c1 = fprt2_2.c2)
               ->  Foreign Scan on public.ftprt1_p2 fprt1_2
                     Output: fprt1_2.c1
                     Remote query: SELECT `c1` FROM `mysql_fdw_regress`.`test2` WHERE (((`c1` % 2) = 0)) ORDER BY `c1` IS NULL, `c1` ASC
               ->  Materialize
                     Output: fprt2_2.c2, ('t2_phv'::text)
                     ->  Foreign Scan on public.ftprt2_p2 fprt2_2
                           Output: fprt2_2.c2, 't2_phv'::text
                           Remote query: SELECT `c2` FROM `mysql_fdw_regress`.`test4` WHERE (((`c2` % 2) = 0)) ORDER BY `c2` IS NULL, `c2` ASC
(28 rows)

SELECT t1.c1, t1.phv, t2.c2, t2.phv
  FROM (SELECT 't1_phv' phv, * FROM fprt1 WHERE c1 % 2 = 0) t1 LEFT JOIN
    (SELECT 't2_phv' phv, * FROM fprt2 WHERE c2 % 2 = 0) t2 ON (t1.c1 = t2.c2)
  ORDER BY t1.c1, t2.c2;
 c1 |  phv   | c2 |  phv   
----+--------+----+--------
  2 | t1_phv |  2 | t2_phv
  4 | t1_phv |  4 | t2_phv
  6 | t1_phv |  6 | t2_phv
  8 | t1_phv |  8 | t2_phv
(4 rows)

SET enable_partitionwise_join TO off;
-- FDW-679: Whole-row reference in left join should work correctly.
EXPLAIN (COSTS false, VERBOSE)
SELECT t3 FROM fdw139_t1 t1
    LEFT JOIN fdw139_t1 t2 ON (t1.c1 = t2.c1)
    LEFT JOIN fdw139_t1 t3 ON (t3.c1 = t2.c1)
  ORDER BY 1;
                                                                                                                   QUERY PLAN                                                                                                                    
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 Sort
   Output: t3.*
   Sort Key: t3.*
   ->  Foreign Scan
         Output: t3.*
         Relations: ((mysql_fdw_regress.fdw139_t1 t1) LEFT JOIN (mysql_fdw_regress.fdw139_t1 t2)) LEFT JOIN (mysql_fdw_regress.fdw139_t1 t3)
         Remote query: SELECT r4.`c1`, r4.`c2`, r4.`c3`, r4.`c4` FROM ((`mysql_fdw_regress`.`test1` r1 LEFT JOIN `mysql_fdw_regress`.`test1` r2 ON (((r1.`c1` = r2.`c1`)))) LEFT JOIN `mysql_fdw_regress`.`test1` r4 ON (((r4.`c1` = r2.`c1`))))
(7 rows)

SELECT t3 FROM fdw139_t1 t1
    LEFT JOIN fdw139_t1 t2 ON (t1.c1 = t2.c1)
    LEFT JOIN fdw139_t1 t3 ON (t3.c1 = t2.c1)
  ORDER BY 1;
       t3        
-----------------
 (1,1,AAA1,foo)
 (2,2,AAA2,bar)
 (3,3,AAA11,foo)
 (4,4,AAA12,foo)
(4 rows)

-- Cleanup
DELETE FROM fdw139_t1;
DELETE FROM fdw139_t2;
DELETE FROM fdw139_t3;
DELETE FROM tmp_t4;
DROP FOREIGN TABLE fdw139_t1;
DROP FOREIGN TABLE fdw139_t2;
DROP FOREIGN TABLE fdw139_t3;
DROP FOREIGN TABLE tmp_t4;
DROP TABLE IF EXISTS fprt1;
DROP TABLE IF EXISTS fprt2;
DROP TYPE user_enum;
DROP USER MAPPING FOR public SERVER mysql_svr;
DROP USER MAPPING FOR public SERVER mysql_svr1;
DROP SERVER mysql_svr;
DROP SERVER mysql_svr1;
DROP EXTENSION mysql_fdw;