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
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
|
/*-------------------------------------------------------------------------
*
* execExpr.c
* Expression evaluation infrastructure.
*
* During executor startup, we compile each expression tree (which has
* previously been processed by the parser and planner) into an ExprState,
* using ExecInitExpr() et al. This converts the tree into a flat array
* of ExprEvalSteps, which may be thought of as instructions in a program.
* At runtime, we'll execute steps, starting with the first, until we reach
* an EEOP_DONE opcode.
*
* This file contains the "compilation" logic. It is independent of the
* specific execution technology we use (switch statement, computed goto,
* JIT compilation, etc).
*
* See src/backend/executor/README for some background, specifically the
* "Expression Trees and ExprState nodes", "Expression Initialization",
* and "Expression Evaluation" sections.
*
*
* Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
*
* IDENTIFICATION
* src/backend/executor/execExpr.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/nbtree.h"
#include "catalog/objectaccess.h"
#include "catalog/pg_type.h"
#include "executor/execExpr.h"
#include "executor/nodeSubplan.h"
#include "funcapi.h"
#include "jit/jit.h"
#include "miscadmin.h"
#include "nodes/makefuncs.h"
#include "nodes/nodeFuncs.h"
#include "nodes/subscripting.h"
#include "optimizer/optimizer.h"
#include "pgstat.h"
#include "utils/acl.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/datum.h"
#include "utils/lsyscache.h"
#include "utils/typcache.h"
typedef struct ExprSetupInfo
{
/* Highest attribute numbers fetched from inner/outer/scan tuple slots: */
AttrNumber last_inner;
AttrNumber last_outer;
AttrNumber last_scan;
/* MULTIEXPR SubPlan nodes appearing in the expression: */
List *multiexpr_subplans;
} ExprSetupInfo;
static void ExecReadyExpr(ExprState *state);
static void ExecInitExprRec(Expr *node, ExprState *state,
Datum *resv, bool *resnull);
static void ExecInitFunc(ExprEvalStep *scratch, Expr *node, List *args,
Oid funcid, Oid inputcollid,
ExprState *state);
static void ExecCreateExprSetupSteps(ExprState *state, Node *node);
static void ExecPushExprSetupSteps(ExprState *state, ExprSetupInfo *info);
static bool expr_setup_walker(Node *node, ExprSetupInfo *info);
static bool ExecComputeSlotInfo(ExprState *state, ExprEvalStep *op);
static void ExecInitWholeRowVar(ExprEvalStep *scratch, Var *variable,
ExprState *state);
static void ExecInitSubscriptingRef(ExprEvalStep *scratch,
SubscriptingRef *sbsref,
ExprState *state,
Datum *resv, bool *resnull);
static bool isAssignmentIndirectionExpr(Expr *expr);
static void ExecInitCoerceToDomain(ExprEvalStep *scratch, CoerceToDomain *ctest,
ExprState *state,
Datum *resv, bool *resnull);
static void ExecBuildAggTransCall(ExprState *state, AggState *aggstate,
ExprEvalStep *scratch,
FunctionCallInfo fcinfo, AggStatePerTrans pertrans,
int transno, int setno, int setoff, bool ishash,
bool nullcheck);
/*
* ExecInitExpr: prepare an expression tree for execution
*
* This function builds and returns an ExprState implementing the given
* Expr node tree. The return ExprState can then be handed to ExecEvalExpr
* for execution. Because the Expr tree itself is read-only as far as
* ExecInitExpr and ExecEvalExpr are concerned, several different executions
* of the same plan tree can occur concurrently. (But note that an ExprState
* does mutate at runtime, so it can't be re-used concurrently.)
*
* This must be called in a memory context that will last as long as repeated
* executions of the expression are needed. Typically the context will be
* the same as the per-query context of the associated ExprContext.
*
* Any Aggref, WindowFunc, or SubPlan nodes found in the tree are added to
* the lists of such nodes held by the parent PlanState.
*
* Note: there is no ExecEndExpr function; we assume that any resource
* cleanup needed will be handled by just releasing the memory context
* in which the state tree is built. Functions that require additional
* cleanup work can register a shutdown callback in the ExprContext.
*
* 'node' is the root of the expression tree to compile.
* 'parent' is the PlanState node that owns the expression.
*
* 'parent' may be NULL if we are preparing an expression that is not
* associated with a plan tree. (If so, it can't have aggs or subplans.)
* Such cases should usually come through ExecPrepareExpr, not directly here.
*
* Also, if 'node' is NULL, we just return NULL. This is convenient for some
* callers that may or may not have an expression that needs to be compiled.
* Note that a NULL ExprState pointer *cannot* be handed to ExecEvalExpr,
* although ExecQual and ExecCheck will accept one (and treat it as "true").
*/
ExprState *
ExecInitExpr(Expr *node, PlanState *parent)
{
ExprState *state;
ExprEvalStep scratch = {0};
/* Special case: NULL expression produces a NULL ExprState pointer */
if (node == NULL)
return NULL;
/* Initialize ExprState with empty step list */
state = makeNode(ExprState);
state->expr = node;
state->parent = parent;
state->ext_params = NULL;
/* Insert setup steps as needed */
ExecCreateExprSetupSteps(state, (Node *) node);
/* Compile the expression proper */
ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
/* Finally, append a DONE step */
scratch.opcode = EEOP_DONE;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
return state;
}
/*
* ExecInitExprWithParams: prepare a standalone expression tree for execution
*
* This is the same as ExecInitExpr, except that there is no parent PlanState,
* and instead we may have a ParamListInfo describing PARAM_EXTERN Params.
*/
ExprState *
ExecInitExprWithParams(Expr *node, ParamListInfo ext_params)
{
ExprState *state;
ExprEvalStep scratch = {0};
/* Special case: NULL expression produces a NULL ExprState pointer */
if (node == NULL)
return NULL;
/* Initialize ExprState with empty step list */
state = makeNode(ExprState);
state->expr = node;
state->parent = NULL;
state->ext_params = ext_params;
/* Insert setup steps as needed */
ExecCreateExprSetupSteps(state, (Node *) node);
/* Compile the expression proper */
ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
/* Finally, append a DONE step */
scratch.opcode = EEOP_DONE;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
return state;
}
/*
* ExecInitQual: prepare a qual for execution by ExecQual
*
* Prepares for the evaluation of a conjunctive boolean expression (qual list
* with implicit AND semantics) that returns true if none of the
* subexpressions are false.
*
* We must return true if the list is empty. Since that's a very common case,
* we optimize it a bit further by translating to a NULL ExprState pointer
* rather than setting up an ExprState that computes constant TRUE. (Some
* especially hot-spot callers of ExecQual detect this and avoid calling
* ExecQual at all.)
*
* If any of the subexpressions yield NULL, then the result of the conjunction
* is false. This makes ExecQual primarily useful for evaluating WHERE
* clauses, since SQL specifies that tuples with null WHERE results do not
* get selected.
*/
ExprState *
ExecInitQual(List *qual, PlanState *parent)
{
ExprState *state;
ExprEvalStep scratch = {0};
List *adjust_jumps = NIL;
ListCell *lc;
/* short-circuit (here and in ExecQual) for empty restriction list */
if (qual == NIL)
return NULL;
Assert(IsA(qual, List));
state = makeNode(ExprState);
state->expr = (Expr *) qual;
state->parent = parent;
state->ext_params = NULL;
/* mark expression as to be used with ExecQual() */
state->flags = EEO_FLAG_IS_QUAL;
/* Insert setup steps as needed */
ExecCreateExprSetupSteps(state, (Node *) qual);
/*
* ExecQual() needs to return false for an expression returning NULL. That
* allows us to short-circuit the evaluation the first time a NULL is
* encountered. As qual evaluation is a hot-path this warrants using a
* special opcode for qual evaluation that's simpler than BOOL_AND (which
* has more complex NULL handling).
*/
scratch.opcode = EEOP_QUAL;
/*
* We can use ExprState's resvalue/resnull as target for each qual expr.
*/
scratch.resvalue = &state->resvalue;
scratch.resnull = &state->resnull;
foreach(lc, qual)
{
Expr *node = (Expr *) lfirst(lc);
/* first evaluate expression */
ExecInitExprRec(node, state, &state->resvalue, &state->resnull);
/* then emit EEOP_QUAL to detect if it's false (or null) */
scratch.d.qualexpr.jumpdone = -1;
ExprEvalPushStep(state, &scratch);
adjust_jumps = lappend_int(adjust_jumps,
state->steps_len - 1);
}
/* adjust jump targets */
foreach(lc, adjust_jumps)
{
ExprEvalStep *as = &state->steps[lfirst_int(lc)];
Assert(as->opcode == EEOP_QUAL);
Assert(as->d.qualexpr.jumpdone == -1);
as->d.qualexpr.jumpdone = state->steps_len;
}
/*
* At the end, we don't need to do anything more. The last qual expr must
* have yielded TRUE, and since its result is stored in the desired output
* location, we're done.
*/
scratch.opcode = EEOP_DONE;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
return state;
}
/*
* ExecInitCheck: prepare a check constraint for execution by ExecCheck
*
* This is much like ExecInitQual/ExecQual, except that a null result from
* the conjunction is treated as TRUE. This behavior is appropriate for
* evaluating CHECK constraints, since SQL specifies that NULL constraint
* conditions are not failures.
*
* Note that like ExecInitQual, this expects input in implicit-AND format.
* Users of ExecCheck that have expressions in normal explicit-AND format
* can just apply ExecInitExpr to produce suitable input for ExecCheck.
*/
ExprState *
ExecInitCheck(List *qual, PlanState *parent)
{
/* short-circuit (here and in ExecCheck) for empty restriction list */
if (qual == NIL)
return NULL;
Assert(IsA(qual, List));
/*
* Just convert the implicit-AND list to an explicit AND (if there's more
* than one entry), and compile normally. Unlike ExecQual, we can't
* short-circuit on NULL results, so the regular AND behavior is needed.
*/
return ExecInitExpr(make_ands_explicit(qual), parent);
}
/*
* Call ExecInitExpr() on a list of expressions, return a list of ExprStates.
*/
List *
ExecInitExprList(List *nodes, PlanState *parent)
{
List *result = NIL;
ListCell *lc;
foreach(lc, nodes)
{
Expr *e = lfirst(lc);
result = lappend(result, ExecInitExpr(e, parent));
}
return result;
}
/*
* ExecBuildProjectionInfo
*
* Build a ProjectionInfo node for evaluating the given tlist in the given
* econtext, and storing the result into the tuple slot. (Caller must have
* ensured that tuple slot has a descriptor matching the tlist!)
*
* inputDesc can be NULL, but if it is not, we check to see whether simple
* Vars in the tlist match the descriptor. It is important to provide
* inputDesc for relation-scan plan nodes, as a cross check that the relation
* hasn't been changed since the plan was made. At higher levels of a plan,
* there is no need to recheck.
*
* This is implemented by internally building an ExprState that performs the
* whole projection in one go.
*
* Caution: before PG v10, the targetList was a list of ExprStates; now it
* should be the planner-created targetlist, since we do the compilation here.
*/
ProjectionInfo *
ExecBuildProjectionInfo(List *targetList,
ExprContext *econtext,
TupleTableSlot *slot,
PlanState *parent,
TupleDesc inputDesc)
{
ProjectionInfo *projInfo = makeNode(ProjectionInfo);
ExprState *state;
ExprEvalStep scratch = {0};
ListCell *lc;
projInfo->pi_exprContext = econtext;
/* We embed ExprState into ProjectionInfo instead of doing extra palloc */
projInfo->pi_state.type = T_ExprState;
state = &projInfo->pi_state;
state->expr = (Expr *) targetList;
state->parent = parent;
state->ext_params = NULL;
state->resultslot = slot;
/* Insert setup steps as needed */
ExecCreateExprSetupSteps(state, (Node *) targetList);
/* Now compile each tlist column */
foreach(lc, targetList)
{
TargetEntry *tle = lfirst_node(TargetEntry, lc);
Var *variable = NULL;
AttrNumber attnum = 0;
bool isSafeVar = false;
/*
* If tlist expression is a safe non-system Var, use the fast-path
* ASSIGN_*_VAR opcodes. "Safe" means that we don't need to apply
* CheckVarSlotCompatibility() during plan startup. If a source slot
* was provided, we make the equivalent tests here; if a slot was not
* provided, we assume that no check is needed because we're dealing
* with a non-relation-scan-level expression.
*/
if (tle->expr != NULL &&
IsA(tle->expr, Var) &&
((Var *) tle->expr)->varattno > 0)
{
/* Non-system Var, but how safe is it? */
variable = (Var *) tle->expr;
attnum = variable->varattno;
if (inputDesc == NULL)
isSafeVar = true; /* can't check, just assume OK */
else if (attnum <= inputDesc->natts)
{
Form_pg_attribute attr = TupleDescAttr(inputDesc, attnum - 1);
/*
* If user attribute is dropped or has a type mismatch, don't
* use ASSIGN_*_VAR. Instead let the normal expression
* machinery handle it (which'll possibly error out).
*/
if (!attr->attisdropped && variable->vartype == attr->atttypid)
{
isSafeVar = true;
}
}
}
if (isSafeVar)
{
/* Fast-path: just generate an EEOP_ASSIGN_*_VAR step */
switch (variable->varno)
{
case INNER_VAR:
/* get the tuple from the inner node */
scratch.opcode = EEOP_ASSIGN_INNER_VAR;
break;
case OUTER_VAR:
/* get the tuple from the outer node */
scratch.opcode = EEOP_ASSIGN_OUTER_VAR;
break;
/* INDEX_VAR is handled by default case */
default:
/* get the tuple from the relation being scanned */
scratch.opcode = EEOP_ASSIGN_SCAN_VAR;
break;
}
scratch.d.assign_var.attnum = attnum - 1;
scratch.d.assign_var.resultnum = tle->resno - 1;
ExprEvalPushStep(state, &scratch);
}
else
{
/*
* Otherwise, compile the column expression normally.
*
* We can't tell the expression to evaluate directly into the
* result slot, as the result slot (and the exprstate for that
* matter) can change between executions. We instead evaluate
* into the ExprState's resvalue/resnull and then move.
*/
ExecInitExprRec(tle->expr, state,
&state->resvalue, &state->resnull);
/*
* Column might be referenced multiple times in upper nodes, so
* force value to R/O - but only if it could be an expanded datum.
*/
if (get_typlen(exprType((Node *) tle->expr)) == -1)
scratch.opcode = EEOP_ASSIGN_TMP_MAKE_RO;
else
scratch.opcode = EEOP_ASSIGN_TMP;
scratch.d.assign_tmp.resultnum = tle->resno - 1;
ExprEvalPushStep(state, &scratch);
}
}
scratch.opcode = EEOP_DONE;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
return projInfo;
}
/*
* ExecBuildUpdateProjection
*
* Build a ProjectionInfo node for constructing a new tuple during UPDATE.
* The projection will be executed in the given econtext and the result will
* be stored into the given tuple slot. (Caller must have ensured that tuple
* slot has a descriptor matching the target rel!)
*
* When evalTargetList is false, targetList contains the UPDATE ... SET
* expressions that have already been computed by a subplan node; the values
* from this tlist are assumed to be available in the "outer" tuple slot.
* When evalTargetList is true, targetList contains the UPDATE ... SET
* expressions that must be computed (which could contain references to
* the outer, inner, or scan tuple slots).
*
* In either case, targetColnos contains a list of the target column numbers
* corresponding to the non-resjunk entries of targetList. The tlist values
* are assigned into these columns of the result tuple slot. Target columns
* not listed in targetColnos are filled from the UPDATE's old tuple, which
* is assumed to be available in the "scan" tuple slot.
*
* targetList can also contain resjunk columns. These must be evaluated
* if evalTargetList is true, but their values are discarded.
*
* relDesc must describe the relation we intend to update.
*
* This is basically a specialized variant of ExecBuildProjectionInfo.
* However, it also performs sanity checks equivalent to ExecCheckPlanOutput.
* Since we never make a normal tlist equivalent to the whole
* tuple-to-be-assigned, there is no convenient way to apply
* ExecCheckPlanOutput, so we must do our safety checks here.
*/
ProjectionInfo *
ExecBuildUpdateProjection(List *targetList,
bool evalTargetList,
List *targetColnos,
TupleDesc relDesc,
ExprContext *econtext,
TupleTableSlot *slot,
PlanState *parent)
{
ProjectionInfo *projInfo = makeNode(ProjectionInfo);
ExprState *state;
int nAssignableCols;
bool sawJunk;
Bitmapset *assignedCols;
ExprSetupInfo deform = {0, 0, 0, NIL};
ExprEvalStep scratch = {0};
int outerattnum;
ListCell *lc,
*lc2;
projInfo->pi_exprContext = econtext;
/* We embed ExprState into ProjectionInfo instead of doing extra palloc */
projInfo->pi_state.type = T_ExprState;
state = &projInfo->pi_state;
if (evalTargetList)
state->expr = (Expr *) targetList;
else
state->expr = NULL; /* not used */
state->parent = parent;
state->ext_params = NULL;
state->resultslot = slot;
/*
* Examine the targetList to see how many non-junk columns there are, and
* to verify that the non-junk columns come before the junk ones.
*/
nAssignableCols = 0;
sawJunk = false;
foreach(lc, targetList)
{
TargetEntry *tle = lfirst_node(TargetEntry, lc);
if (tle->resjunk)
sawJunk = true;
else
{
if (sawJunk)
elog(ERROR, "subplan target list is out of order");
nAssignableCols++;
}
}
/* We should have one targetColnos entry per non-junk column */
if (nAssignableCols != list_length(targetColnos))
elog(ERROR, "targetColnos does not match subplan target list");
/*
* Build a bitmapset of the columns in targetColnos. (We could just use
* list_member_int() tests, but that risks O(N^2) behavior with many
* columns.)
*/
assignedCols = NULL;
foreach(lc, targetColnos)
{
AttrNumber targetattnum = lfirst_int(lc);
assignedCols = bms_add_member(assignedCols, targetattnum);
}
/*
* We need to insert EEOP_*_FETCHSOME steps to ensure the input tuples are
* sufficiently deconstructed. The scan tuple must be deconstructed at
* least as far as the last old column we need.
*/
for (int attnum = relDesc->natts; attnum > 0; attnum--)
{
Form_pg_attribute attr = TupleDescAttr(relDesc, attnum - 1);
if (attr->attisdropped)
continue;
if (bms_is_member(attnum, assignedCols))
continue;
deform.last_scan = attnum;
break;
}
/*
* If we're actually evaluating the tlist, incorporate its input
* requirements too; otherwise, we'll just need to fetch the appropriate
* number of columns of the "outer" tuple.
*/
if (evalTargetList)
expr_setup_walker((Node *) targetList, &deform);
else
deform.last_outer = nAssignableCols;
ExecPushExprSetupSteps(state, &deform);
/*
* Now generate code to evaluate the tlist's assignable expressions or
* fetch them from the outer tuple, incidentally validating that they'll
* be of the right data type. The checks above ensure that the forboth()
* will iterate over exactly the non-junk columns. Note that we don't
* bother evaluating any remaining resjunk columns.
*/
outerattnum = 0;
forboth(lc, targetList, lc2, targetColnos)
{
TargetEntry *tle = lfirst_node(TargetEntry, lc);
AttrNumber targetattnum = lfirst_int(lc2);
Form_pg_attribute attr;
Assert(!tle->resjunk);
/*
* Apply sanity checks comparable to ExecCheckPlanOutput().
*/
if (targetattnum <= 0 || targetattnum > relDesc->natts)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("table row type and query-specified row type do not match"),
errdetail("Query has too many columns.")));
attr = TupleDescAttr(relDesc, targetattnum - 1);
if (attr->attisdropped)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("table row type and query-specified row type do not match"),
errdetail("Query provides a value for a dropped column at ordinal position %d.",
targetattnum)));
if (exprType((Node *) tle->expr) != attr->atttypid)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("table row type and query-specified row type do not match"),
errdetail("Table has type %s at ordinal position %d, but query expects %s.",
format_type_be(attr->atttypid),
targetattnum,
format_type_be(exprType((Node *) tle->expr)))));
/* OK, generate code to perform the assignment. */
if (evalTargetList)
{
/*
* We must evaluate the TLE's expression and assign it. We do not
* bother jumping through hoops for "safe" Vars like
* ExecBuildProjectionInfo does; this is a relatively less-used
* path and it doesn't seem worth expending code for that.
*/
ExecInitExprRec(tle->expr, state,
&state->resvalue, &state->resnull);
/* Needn't worry about read-only-ness here, either. */
scratch.opcode = EEOP_ASSIGN_TMP;
scratch.d.assign_tmp.resultnum = targetattnum - 1;
ExprEvalPushStep(state, &scratch);
}
else
{
/* Just assign from the outer tuple. */
scratch.opcode = EEOP_ASSIGN_OUTER_VAR;
scratch.d.assign_var.attnum = outerattnum;
scratch.d.assign_var.resultnum = targetattnum - 1;
ExprEvalPushStep(state, &scratch);
}
outerattnum++;
}
/*
* Now generate code to copy over any old columns that were not assigned
* to, and to ensure that dropped columns are set to NULL.
*/
for (int attnum = 1; attnum <= relDesc->natts; attnum++)
{
Form_pg_attribute attr = TupleDescAttr(relDesc, attnum - 1);
if (attr->attisdropped)
{
/* Put a null into the ExprState's resvalue/resnull ... */
scratch.opcode = EEOP_CONST;
scratch.resvalue = &state->resvalue;
scratch.resnull = &state->resnull;
scratch.d.constval.value = (Datum) 0;
scratch.d.constval.isnull = true;
ExprEvalPushStep(state, &scratch);
/* ... then assign it to the result slot */
scratch.opcode = EEOP_ASSIGN_TMP;
scratch.d.assign_tmp.resultnum = attnum - 1;
ExprEvalPushStep(state, &scratch);
}
else if (!bms_is_member(attnum, assignedCols))
{
/* Certainly the right type, so needn't check */
scratch.opcode = EEOP_ASSIGN_SCAN_VAR;
scratch.d.assign_var.attnum = attnum - 1;
scratch.d.assign_var.resultnum = attnum - 1;
ExprEvalPushStep(state, &scratch);
}
}
scratch.opcode = EEOP_DONE;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
return projInfo;
}
/*
* ExecPrepareExpr --- initialize for expression execution outside a normal
* Plan tree context.
*
* This differs from ExecInitExpr in that we don't assume the caller is
* already running in the EState's per-query context. Also, we run the
* passed expression tree through expression_planner() to prepare it for
* execution. (In ordinary Plan trees the regular planning process will have
* made the appropriate transformations on expressions, but for standalone
* expressions this won't have happened.)
*/
ExprState *
ExecPrepareExpr(Expr *node, EState *estate)
{
ExprState *result;
MemoryContext oldcontext;
oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
node = expression_planner(node);
result = ExecInitExpr(node, NULL);
MemoryContextSwitchTo(oldcontext);
return result;
}
/*
* ExecPrepareQual --- initialize for qual execution outside a normal
* Plan tree context.
*
* This differs from ExecInitQual in that we don't assume the caller is
* already running in the EState's per-query context. Also, we run the
* passed expression tree through expression_planner() to prepare it for
* execution. (In ordinary Plan trees the regular planning process will have
* made the appropriate transformations on expressions, but for standalone
* expressions this won't have happened.)
*/
ExprState *
ExecPrepareQual(List *qual, EState *estate)
{
ExprState *result;
MemoryContext oldcontext;
oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
qual = (List *) expression_planner((Expr *) qual);
result = ExecInitQual(qual, NULL);
MemoryContextSwitchTo(oldcontext);
return result;
}
/*
* ExecPrepareCheck -- initialize check constraint for execution outside a
* normal Plan tree context.
*
* See ExecPrepareExpr() and ExecInitCheck() for details.
*/
ExprState *
ExecPrepareCheck(List *qual, EState *estate)
{
ExprState *result;
MemoryContext oldcontext;
oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
qual = (List *) expression_planner((Expr *) qual);
result = ExecInitCheck(qual, NULL);
MemoryContextSwitchTo(oldcontext);
return result;
}
/*
* Call ExecPrepareExpr() on each member of a list of Exprs, and return
* a list of ExprStates.
*
* See ExecPrepareExpr() for details.
*/
List *
ExecPrepareExprList(List *nodes, EState *estate)
{
List *result = NIL;
MemoryContext oldcontext;
ListCell *lc;
/* Ensure that the list cell nodes are in the right context too */
oldcontext = MemoryContextSwitchTo(estate->es_query_cxt);
foreach(lc, nodes)
{
Expr *e = (Expr *) lfirst(lc);
result = lappend(result, ExecPrepareExpr(e, estate));
}
MemoryContextSwitchTo(oldcontext);
return result;
}
/*
* ExecCheck - evaluate a check constraint
*
* For check constraints, a null result is taken as TRUE, ie the constraint
* passes.
*
* The check constraint may have been prepared with ExecInitCheck
* (possibly via ExecPrepareCheck) if the caller had it in implicit-AND
* format, but a regular boolean expression prepared with ExecInitExpr or
* ExecPrepareExpr works too.
*/
bool
ExecCheck(ExprState *state, ExprContext *econtext)
{
Datum ret;
bool isnull;
/* short-circuit (here and in ExecInitCheck) for empty restriction list */
if (state == NULL)
return true;
/* verify that expression was not compiled using ExecInitQual */
Assert(!(state->flags & EEO_FLAG_IS_QUAL));
ret = ExecEvalExprSwitchContext(state, econtext, &isnull);
if (isnull)
return true;
return DatumGetBool(ret);
}
/*
* Prepare a compiled expression for execution. This has to be called for
* every ExprState before it can be executed.
*
* NB: While this currently only calls ExecReadyInterpretedExpr(),
* this will likely get extended to further expression evaluation methods.
* Therefore this should be used instead of directly calling
* ExecReadyInterpretedExpr().
*/
static void
ExecReadyExpr(ExprState *state)
{
if (jit_compile_expr(state))
return;
ExecReadyInterpretedExpr(state);
}
/*
* Append the steps necessary for the evaluation of node to ExprState->steps,
* possibly recursing into sub-expressions of node.
*
* node - expression to evaluate
* state - ExprState to whose ->steps to append the necessary operations
* resv / resnull - where to store the result of the node into
*/
static void
ExecInitExprRec(Expr *node, ExprState *state,
Datum *resv, bool *resnull)
{
ExprEvalStep scratch = {0};
/* Guard against stack overflow due to overly complex expressions */
check_stack_depth();
/* Step's output location is always what the caller gave us */
Assert(resv != NULL && resnull != NULL);
scratch.resvalue = resv;
scratch.resnull = resnull;
/* cases should be ordered as they are in enum NodeTag */
switch (nodeTag(node))
{
case T_Var:
{
Var *variable = (Var *) node;
if (variable->varattno == InvalidAttrNumber)
{
/* whole-row Var */
ExecInitWholeRowVar(&scratch, variable, state);
}
else if (variable->varattno <= 0)
{
/* system column */
scratch.d.var.attnum = variable->varattno;
scratch.d.var.vartype = variable->vartype;
switch (variable->varno)
{
case INNER_VAR:
scratch.opcode = EEOP_INNER_SYSVAR;
break;
case OUTER_VAR:
scratch.opcode = EEOP_OUTER_SYSVAR;
break;
/* INDEX_VAR is handled by default case */
default:
scratch.opcode = EEOP_SCAN_SYSVAR;
break;
}
}
else
{
/* regular user column */
scratch.d.var.attnum = variable->varattno - 1;
scratch.d.var.vartype = variable->vartype;
switch (variable->varno)
{
case INNER_VAR:
scratch.opcode = EEOP_INNER_VAR;
break;
case OUTER_VAR:
scratch.opcode = EEOP_OUTER_VAR;
break;
/* INDEX_VAR is handled by default case */
default:
scratch.opcode = EEOP_SCAN_VAR;
break;
}
}
ExprEvalPushStep(state, &scratch);
break;
}
case T_Const:
{
Const *con = (Const *) node;
scratch.opcode = EEOP_CONST;
scratch.d.constval.value = con->constvalue;
scratch.d.constval.isnull = con->constisnull;
ExprEvalPushStep(state, &scratch);
break;
}
case T_Param:
{
Param *param = (Param *) node;
ParamListInfo params;
switch (param->paramkind)
{
case PARAM_EXEC:
scratch.opcode = EEOP_PARAM_EXEC;
scratch.d.param.paramid = param->paramid;
scratch.d.param.paramtype = param->paramtype;
ExprEvalPushStep(state, &scratch);
break;
case PARAM_EXTERN:
/*
* If we have a relevant ParamCompileHook, use it;
* otherwise compile a standard EEOP_PARAM_EXTERN
* step. ext_params, if supplied, takes precedence
* over info from the parent node's EState (if any).
*/
if (state->ext_params)
params = state->ext_params;
else if (state->parent &&
state->parent->state)
params = state->parent->state->es_param_list_info;
else
params = NULL;
if (params && params->paramCompile)
{
params->paramCompile(params, param, state,
resv, resnull);
}
else
{
scratch.opcode = EEOP_PARAM_EXTERN;
scratch.d.param.paramid = param->paramid;
scratch.d.param.paramtype = param->paramtype;
ExprEvalPushStep(state, &scratch);
}
break;
default:
elog(ERROR, "unrecognized paramkind: %d",
(int) param->paramkind);
break;
}
break;
}
case T_Aggref:
{
Aggref *aggref = (Aggref *) node;
scratch.opcode = EEOP_AGGREF;
scratch.d.aggref.aggno = aggref->aggno;
if (state->parent && IsA(state->parent, AggState))
{
AggState *aggstate = (AggState *) state->parent;
aggstate->aggs = lappend(aggstate->aggs, aggref);
}
else
{
/* planner messed up */
elog(ERROR, "Aggref found in non-Agg plan node");
}
ExprEvalPushStep(state, &scratch);
break;
}
case T_GroupingFunc:
{
GroupingFunc *grp_node = (GroupingFunc *) node;
Agg *agg;
if (!state->parent || !IsA(state->parent, AggState) ||
!IsA(state->parent->plan, Agg))
elog(ERROR, "GroupingFunc found in non-Agg plan node");
scratch.opcode = EEOP_GROUPING_FUNC;
agg = (Agg *) (state->parent->plan);
if (agg->groupingSets)
scratch.d.grouping_func.clauses = grp_node->cols;
else
scratch.d.grouping_func.clauses = NIL;
ExprEvalPushStep(state, &scratch);
break;
}
case T_WindowFunc:
{
WindowFunc *wfunc = (WindowFunc *) node;
WindowFuncExprState *wfstate = makeNode(WindowFuncExprState);
wfstate->wfunc = wfunc;
if (state->parent && IsA(state->parent, WindowAggState))
{
WindowAggState *winstate = (WindowAggState *) state->parent;
int nfuncs;
winstate->funcs = lappend(winstate->funcs, wfstate);
nfuncs = ++winstate->numfuncs;
if (wfunc->winagg)
winstate->numaggs++;
/* for now initialize agg using old style expressions */
wfstate->args = ExecInitExprList(wfunc->args,
state->parent);
wfstate->aggfilter = ExecInitExpr(wfunc->aggfilter,
state->parent);
/*
* Complain if the windowfunc's arguments contain any
* windowfuncs; nested window functions are semantically
* nonsensical. (This should have been caught earlier,
* but we defend against it here anyway.)
*/
if (nfuncs != winstate->numfuncs)
ereport(ERROR,
(errcode(ERRCODE_WINDOWING_ERROR),
errmsg("window function calls cannot be nested")));
}
else
{
/* planner messed up */
elog(ERROR, "WindowFunc found in non-WindowAgg plan node");
}
scratch.opcode = EEOP_WINDOW_FUNC;
scratch.d.window_func.wfstate = wfstate;
ExprEvalPushStep(state, &scratch);
break;
}
case T_SubscriptingRef:
{
SubscriptingRef *sbsref = (SubscriptingRef *) node;
ExecInitSubscriptingRef(&scratch, sbsref, state, resv, resnull);
break;
}
case T_FuncExpr:
{
FuncExpr *func = (FuncExpr *) node;
ExecInitFunc(&scratch, node,
func->args, func->funcid, func->inputcollid,
state);
ExprEvalPushStep(state, &scratch);
break;
}
case T_OpExpr:
{
OpExpr *op = (OpExpr *) node;
ExecInitFunc(&scratch, node,
op->args, op->opfuncid, op->inputcollid,
state);
ExprEvalPushStep(state, &scratch);
break;
}
case T_DistinctExpr:
{
DistinctExpr *op = (DistinctExpr *) node;
ExecInitFunc(&scratch, node,
op->args, op->opfuncid, op->inputcollid,
state);
/*
* Change opcode of call instruction to EEOP_DISTINCT.
*
* XXX: historically we've not called the function usage
* pgstat infrastructure - that seems inconsistent given that
* we do so for normal function *and* operator evaluation. If
* we decided to do that here, we'd probably want separate
* opcodes for FUSAGE or not.
*/
scratch.opcode = EEOP_DISTINCT;
ExprEvalPushStep(state, &scratch);
break;
}
case T_NullIfExpr:
{
NullIfExpr *op = (NullIfExpr *) node;
ExecInitFunc(&scratch, node,
op->args, op->opfuncid, op->inputcollid,
state);
/*
* Change opcode of call instruction to EEOP_NULLIF.
*
* XXX: historically we've not called the function usage
* pgstat infrastructure - that seems inconsistent given that
* we do so for normal function *and* operator evaluation. If
* we decided to do that here, we'd probably want separate
* opcodes for FUSAGE or not.
*/
scratch.opcode = EEOP_NULLIF;
ExprEvalPushStep(state, &scratch);
break;
}
case T_ScalarArrayOpExpr:
{
ScalarArrayOpExpr *opexpr = (ScalarArrayOpExpr *) node;
Expr *scalararg;
Expr *arrayarg;
FmgrInfo *finfo;
FunctionCallInfo fcinfo;
AclResult aclresult;
Oid cmpfuncid;
/*
* Select the correct comparison function. When we do hashed
* NOT IN clauses, the opfuncid will be the inequality
* comparison function and negfuncid will be set to equality.
* We need to use the equality function for hash probes.
*/
if (OidIsValid(opexpr->negfuncid))
{
Assert(OidIsValid(opexpr->hashfuncid));
cmpfuncid = opexpr->negfuncid;
}
else
cmpfuncid = opexpr->opfuncid;
Assert(list_length(opexpr->args) == 2);
scalararg = (Expr *) linitial(opexpr->args);
arrayarg = (Expr *) lsecond(opexpr->args);
/* Check permission to call function */
aclresult = pg_proc_aclcheck(cmpfuncid,
GetUserId(),
ACL_EXECUTE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, OBJECT_FUNCTION,
get_func_name(cmpfuncid));
InvokeFunctionExecuteHook(cmpfuncid);
if (OidIsValid(opexpr->hashfuncid))
{
aclresult = pg_proc_aclcheck(opexpr->hashfuncid,
GetUserId(),
ACL_EXECUTE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, OBJECT_FUNCTION,
get_func_name(opexpr->hashfuncid));
InvokeFunctionExecuteHook(opexpr->hashfuncid);
}
/* Set up the primary fmgr lookup information */
finfo = palloc0(sizeof(FmgrInfo));
fcinfo = palloc0(SizeForFunctionCallInfo(2));
fmgr_info(cmpfuncid, finfo);
fmgr_info_set_expr((Node *) node, finfo);
InitFunctionCallInfoData(*fcinfo, finfo, 2,
opexpr->inputcollid, NULL, NULL);
/*
* If hashfuncid is set, we create a EEOP_HASHED_SCALARARRAYOP
* step instead of a EEOP_SCALARARRAYOP. This provides much
* faster lookup performance than the normal linear search
* when the number of items in the array is anything but very
* small.
*/
if (OidIsValid(opexpr->hashfuncid))
{
/* Evaluate scalar directly into left function argument */
ExecInitExprRec(scalararg, state,
&fcinfo->args[0].value, &fcinfo->args[0].isnull);
/*
* Evaluate array argument into our return value. There's
* no danger in that, because the return value is
* guaranteed to be overwritten by
* EEOP_HASHED_SCALARARRAYOP, and will not be passed to
* any other expression.
*/
ExecInitExprRec(arrayarg, state, resv, resnull);
/* And perform the operation */
scratch.opcode = EEOP_HASHED_SCALARARRAYOP;
scratch.d.hashedscalararrayop.inclause = opexpr->useOr;
scratch.d.hashedscalararrayop.finfo = finfo;
scratch.d.hashedscalararrayop.fcinfo_data = fcinfo;
scratch.d.hashedscalararrayop.saop = opexpr;
ExprEvalPushStep(state, &scratch);
}
else
{
/* Evaluate scalar directly into left function argument */
ExecInitExprRec(scalararg, state,
&fcinfo->args[0].value,
&fcinfo->args[0].isnull);
/*
* Evaluate array argument into our return value. There's
* no danger in that, because the return value is
* guaranteed to be overwritten by EEOP_SCALARARRAYOP, and
* will not be passed to any other expression.
*/
ExecInitExprRec(arrayarg, state, resv, resnull);
/* And perform the operation */
scratch.opcode = EEOP_SCALARARRAYOP;
scratch.d.scalararrayop.element_type = InvalidOid;
scratch.d.scalararrayop.useOr = opexpr->useOr;
scratch.d.scalararrayop.finfo = finfo;
scratch.d.scalararrayop.fcinfo_data = fcinfo;
scratch.d.scalararrayop.fn_addr = finfo->fn_addr;
ExprEvalPushStep(state, &scratch);
}
break;
}
case T_BoolExpr:
{
BoolExpr *boolexpr = (BoolExpr *) node;
int nargs = list_length(boolexpr->args);
List *adjust_jumps = NIL;
int off;
ListCell *lc;
/* allocate scratch memory used by all steps of AND/OR */
if (boolexpr->boolop != NOT_EXPR)
scratch.d.boolexpr.anynull = (bool *) palloc(sizeof(bool));
/*
* For each argument evaluate the argument itself, then
* perform the bool operation's appropriate handling.
*
* We can evaluate each argument into our result area, since
* the short-circuiting logic means we only need to remember
* previous NULL values.
*
* AND/OR is split into separate STEP_FIRST (one) / STEP (zero
* or more) / STEP_LAST (one) steps, as each of those has to
* perform different work. The FIRST/LAST split is valid
* because AND/OR have at least two arguments.
*/
off = 0;
foreach(lc, boolexpr->args)
{
Expr *arg = (Expr *) lfirst(lc);
/* Evaluate argument into our output variable */
ExecInitExprRec(arg, state, resv, resnull);
/* Perform the appropriate step type */
switch (boolexpr->boolop)
{
case AND_EXPR:
Assert(nargs >= 2);
if (off == 0)
scratch.opcode = EEOP_BOOL_AND_STEP_FIRST;
else if (off + 1 == nargs)
scratch.opcode = EEOP_BOOL_AND_STEP_LAST;
else
scratch.opcode = EEOP_BOOL_AND_STEP;
break;
case OR_EXPR:
Assert(nargs >= 2);
if (off == 0)
scratch.opcode = EEOP_BOOL_OR_STEP_FIRST;
else if (off + 1 == nargs)
scratch.opcode = EEOP_BOOL_OR_STEP_LAST;
else
scratch.opcode = EEOP_BOOL_OR_STEP;
break;
case NOT_EXPR:
Assert(nargs == 1);
scratch.opcode = EEOP_BOOL_NOT_STEP;
break;
default:
elog(ERROR, "unrecognized boolop: %d",
(int) boolexpr->boolop);
break;
}
scratch.d.boolexpr.jumpdone = -1;
ExprEvalPushStep(state, &scratch);
adjust_jumps = lappend_int(adjust_jumps,
state->steps_len - 1);
off++;
}
/* adjust jump targets */
foreach(lc, adjust_jumps)
{
ExprEvalStep *as = &state->steps[lfirst_int(lc)];
Assert(as->d.boolexpr.jumpdone == -1);
as->d.boolexpr.jumpdone = state->steps_len;
}
break;
}
case T_SubPlan:
{
SubPlan *subplan = (SubPlan *) node;
SubPlanState *sstate;
/*
* Real execution of a MULTIEXPR SubPlan has already been
* done. What we have to do here is return a dummy NULL record
* value in case this targetlist element is assigned
* someplace.
*/
if (subplan->subLinkType == MULTIEXPR_SUBLINK)
{
scratch.opcode = EEOP_CONST;
scratch.d.constval.value = (Datum) 0;
scratch.d.constval.isnull = true;
ExprEvalPushStep(state, &scratch);
break;
}
if (!state->parent)
elog(ERROR, "SubPlan found with no parent plan");
sstate = ExecInitSubPlan(subplan, state->parent);
/* add SubPlanState nodes to state->parent->subPlan */
state->parent->subPlan = lappend(state->parent->subPlan,
sstate);
scratch.opcode = EEOP_SUBPLAN;
scratch.d.subplan.sstate = sstate;
ExprEvalPushStep(state, &scratch);
break;
}
case T_FieldSelect:
{
FieldSelect *fselect = (FieldSelect *) node;
/* evaluate row/record argument into result area */
ExecInitExprRec(fselect->arg, state, resv, resnull);
/* and extract field */
scratch.opcode = EEOP_FIELDSELECT;
scratch.d.fieldselect.fieldnum = fselect->fieldnum;
scratch.d.fieldselect.resulttype = fselect->resulttype;
scratch.d.fieldselect.rowcache.cacheptr = NULL;
ExprEvalPushStep(state, &scratch);
break;
}
case T_FieldStore:
{
FieldStore *fstore = (FieldStore *) node;
TupleDesc tupDesc;
ExprEvalRowtypeCache *rowcachep;
Datum *values;
bool *nulls;
int ncolumns;
ListCell *l1,
*l2;
/* find out the number of columns in the composite type */
tupDesc = lookup_rowtype_tupdesc(fstore->resulttype, -1);
ncolumns = tupDesc->natts;
ReleaseTupleDesc(tupDesc);
/* create workspace for column values */
values = (Datum *) palloc(sizeof(Datum) * ncolumns);
nulls = (bool *) palloc(sizeof(bool) * ncolumns);
/* create shared composite-type-lookup cache struct */
rowcachep = palloc(sizeof(ExprEvalRowtypeCache));
rowcachep->cacheptr = NULL;
/* emit code to evaluate the composite input value */
ExecInitExprRec(fstore->arg, state, resv, resnull);
/* next, deform the input tuple into our workspace */
scratch.opcode = EEOP_FIELDSTORE_DEFORM;
scratch.d.fieldstore.fstore = fstore;
scratch.d.fieldstore.rowcache = rowcachep;
scratch.d.fieldstore.values = values;
scratch.d.fieldstore.nulls = nulls;
scratch.d.fieldstore.ncolumns = ncolumns;
ExprEvalPushStep(state, &scratch);
/* evaluate new field values, store in workspace columns */
forboth(l1, fstore->newvals, l2, fstore->fieldnums)
{
Expr *e = (Expr *) lfirst(l1);
AttrNumber fieldnum = lfirst_int(l2);
Datum *save_innermost_caseval;
bool *save_innermost_casenull;
if (fieldnum <= 0 || fieldnum > ncolumns)
elog(ERROR, "field number %d is out of range in FieldStore",
fieldnum);
/*
* Use the CaseTestExpr mechanism to pass down the old
* value of the field being replaced; this is needed in
* case the newval is itself a FieldStore or
* SubscriptingRef that has to obtain and modify the old
* value. It's safe to reuse the CASE mechanism because
* there cannot be a CASE between here and where the value
* would be needed, and a field assignment can't be within
* a CASE either. (So saving and restoring
* innermost_caseval is just paranoia, but let's do it
* anyway.)
*
* Another non-obvious point is that it's safe to use the
* field's values[]/nulls[] entries as both the caseval
* source and the result address for this subexpression.
* That's okay only because (1) both FieldStore and
* SubscriptingRef evaluate their arg or refexpr inputs
* first, and (2) any such CaseTestExpr is directly the
* arg or refexpr input. So any read of the caseval will
* occur before there's a chance to overwrite it. Also,
* if multiple entries in the newvals/fieldnums lists
* target the same field, they'll effectively be applied
* left-to-right which is what we want.
*/
save_innermost_caseval = state->innermost_caseval;
save_innermost_casenull = state->innermost_casenull;
state->innermost_caseval = &values[fieldnum - 1];
state->innermost_casenull = &nulls[fieldnum - 1];
ExecInitExprRec(e, state,
&values[fieldnum - 1],
&nulls[fieldnum - 1]);
state->innermost_caseval = save_innermost_caseval;
state->innermost_casenull = save_innermost_casenull;
}
/* finally, form result tuple */
scratch.opcode = EEOP_FIELDSTORE_FORM;
scratch.d.fieldstore.fstore = fstore;
scratch.d.fieldstore.rowcache = rowcachep;
scratch.d.fieldstore.values = values;
scratch.d.fieldstore.nulls = nulls;
scratch.d.fieldstore.ncolumns = ncolumns;
ExprEvalPushStep(state, &scratch);
break;
}
case T_RelabelType:
{
/* relabel doesn't need to do anything at runtime */
RelabelType *relabel = (RelabelType *) node;
ExecInitExprRec(relabel->arg, state, resv, resnull);
break;
}
case T_CoerceViaIO:
{
CoerceViaIO *iocoerce = (CoerceViaIO *) node;
Oid iofunc;
bool typisvarlena;
Oid typioparam;
FunctionCallInfo fcinfo_in;
/* evaluate argument into step's result area */
ExecInitExprRec(iocoerce->arg, state, resv, resnull);
/*
* Prepare both output and input function calls, to be
* evaluated inside a single evaluation step for speed - this
* can be a very common operation.
*
* We don't check permissions here as a type's input/output
* function are assumed to be executable by everyone.
*/
scratch.opcode = EEOP_IOCOERCE;
/* lookup the source type's output function */
scratch.d.iocoerce.finfo_out = palloc0(sizeof(FmgrInfo));
scratch.d.iocoerce.fcinfo_data_out = palloc0(SizeForFunctionCallInfo(1));
getTypeOutputInfo(exprType((Node *) iocoerce->arg),
&iofunc, &typisvarlena);
fmgr_info(iofunc, scratch.d.iocoerce.finfo_out);
fmgr_info_set_expr((Node *) node, scratch.d.iocoerce.finfo_out);
InitFunctionCallInfoData(*scratch.d.iocoerce.fcinfo_data_out,
scratch.d.iocoerce.finfo_out,
1, InvalidOid, NULL, NULL);
/* lookup the result type's input function */
scratch.d.iocoerce.finfo_in = palloc0(sizeof(FmgrInfo));
scratch.d.iocoerce.fcinfo_data_in = palloc0(SizeForFunctionCallInfo(3));
getTypeInputInfo(iocoerce->resulttype,
&iofunc, &typioparam);
fmgr_info(iofunc, scratch.d.iocoerce.finfo_in);
fmgr_info_set_expr((Node *) node, scratch.d.iocoerce.finfo_in);
InitFunctionCallInfoData(*scratch.d.iocoerce.fcinfo_data_in,
scratch.d.iocoerce.finfo_in,
3, InvalidOid, NULL, NULL);
/*
* We can preload the second and third arguments for the input
* function, since they're constants.
*/
fcinfo_in = scratch.d.iocoerce.fcinfo_data_in;
fcinfo_in->args[1].value = ObjectIdGetDatum(typioparam);
fcinfo_in->args[1].isnull = false;
fcinfo_in->args[2].value = Int32GetDatum(-1);
fcinfo_in->args[2].isnull = false;
ExprEvalPushStep(state, &scratch);
break;
}
case T_ArrayCoerceExpr:
{
ArrayCoerceExpr *acoerce = (ArrayCoerceExpr *) node;
Oid resultelemtype;
ExprState *elemstate;
/* evaluate argument into step's result area */
ExecInitExprRec(acoerce->arg, state, resv, resnull);
resultelemtype = get_element_type(acoerce->resulttype);
if (!OidIsValid(resultelemtype))
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("target type is not an array")));
/*
* Construct a sub-expression for the per-element expression;
* but don't ready it until after we check it for triviality.
* We assume it hasn't any Var references, but does have a
* CaseTestExpr representing the source array element values.
*/
elemstate = makeNode(ExprState);
elemstate->expr = acoerce->elemexpr;
elemstate->parent = state->parent;
elemstate->ext_params = state->ext_params;
elemstate->innermost_caseval = (Datum *) palloc(sizeof(Datum));
elemstate->innermost_casenull = (bool *) palloc(sizeof(bool));
ExecInitExprRec(acoerce->elemexpr, elemstate,
&elemstate->resvalue, &elemstate->resnull);
if (elemstate->steps_len == 1 &&
elemstate->steps[0].opcode == EEOP_CASE_TESTVAL)
{
/* Trivial, so we need no per-element work at runtime */
elemstate = NULL;
}
else
{
/* Not trivial, so append a DONE step */
scratch.opcode = EEOP_DONE;
ExprEvalPushStep(elemstate, &scratch);
/* and ready the subexpression */
ExecReadyExpr(elemstate);
}
scratch.opcode = EEOP_ARRAYCOERCE;
scratch.d.arraycoerce.elemexprstate = elemstate;
scratch.d.arraycoerce.resultelemtype = resultelemtype;
if (elemstate)
{
/* Set up workspace for array_map */
scratch.d.arraycoerce.amstate =
(ArrayMapState *) palloc0(sizeof(ArrayMapState));
}
else
{
/* Don't need workspace if there's no subexpression */
scratch.d.arraycoerce.amstate = NULL;
}
ExprEvalPushStep(state, &scratch);
break;
}
case T_ConvertRowtypeExpr:
{
ConvertRowtypeExpr *convert = (ConvertRowtypeExpr *) node;
ExprEvalRowtypeCache *rowcachep;
/* cache structs must be out-of-line for space reasons */
rowcachep = palloc(2 * sizeof(ExprEvalRowtypeCache));
rowcachep[0].cacheptr = NULL;
rowcachep[1].cacheptr = NULL;
/* evaluate argument into step's result area */
ExecInitExprRec(convert->arg, state, resv, resnull);
/* and push conversion step */
scratch.opcode = EEOP_CONVERT_ROWTYPE;
scratch.d.convert_rowtype.inputtype =
exprType((Node *) convert->arg);
scratch.d.convert_rowtype.outputtype = convert->resulttype;
scratch.d.convert_rowtype.incache = &rowcachep[0];
scratch.d.convert_rowtype.outcache = &rowcachep[1];
scratch.d.convert_rowtype.map = NULL;
ExprEvalPushStep(state, &scratch);
break;
}
/* note that CaseWhen expressions are handled within this block */
case T_CaseExpr:
{
CaseExpr *caseExpr = (CaseExpr *) node;
List *adjust_jumps = NIL;
Datum *caseval = NULL;
bool *casenull = NULL;
ListCell *lc;
/*
* If there's a test expression, we have to evaluate it and
* save the value where the CaseTestExpr placeholders can find
* it.
*/
if (caseExpr->arg != NULL)
{
/* Evaluate testexpr into caseval/casenull workspace */
caseval = palloc(sizeof(Datum));
casenull = palloc(sizeof(bool));
ExecInitExprRec(caseExpr->arg, state,
caseval, casenull);
/*
* Since value might be read multiple times, force to R/O
* - but only if it could be an expanded datum.
*/
if (get_typlen(exprType((Node *) caseExpr->arg)) == -1)
{
/* change caseval in-place */
scratch.opcode = EEOP_MAKE_READONLY;
scratch.resvalue = caseval;
scratch.resnull = casenull;
scratch.d.make_readonly.value = caseval;
scratch.d.make_readonly.isnull = casenull;
ExprEvalPushStep(state, &scratch);
/* restore normal settings of scratch fields */
scratch.resvalue = resv;
scratch.resnull = resnull;
}
}
/*
* Prepare to evaluate each of the WHEN clauses in turn; as
* soon as one is true we return the value of the
* corresponding THEN clause. If none are true then we return
* the value of the ELSE clause, or NULL if there is none.
*/
foreach(lc, caseExpr->args)
{
CaseWhen *when = (CaseWhen *) lfirst(lc);
Datum *save_innermost_caseval;
bool *save_innermost_casenull;
int whenstep;
/*
* Make testexpr result available to CaseTestExpr nodes
* within the condition. We must save and restore prior
* setting of innermost_caseval fields, in case this node
* is itself within a larger CASE.
*
* If there's no test expression, we don't actually need
* to save and restore these fields; but it's less code to
* just do so unconditionally.
*/
save_innermost_caseval = state->innermost_caseval;
save_innermost_casenull = state->innermost_casenull;
state->innermost_caseval = caseval;
state->innermost_casenull = casenull;
/* evaluate condition into CASE's result variables */
ExecInitExprRec(when->expr, state, resv, resnull);
state->innermost_caseval = save_innermost_caseval;
state->innermost_casenull = save_innermost_casenull;
/* If WHEN result isn't true, jump to next CASE arm */
scratch.opcode = EEOP_JUMP_IF_NOT_TRUE;
scratch.d.jump.jumpdone = -1; /* computed later */
ExprEvalPushStep(state, &scratch);
whenstep = state->steps_len - 1;
/*
* If WHEN result is true, evaluate THEN result, storing
* it into the CASE's result variables.
*/
ExecInitExprRec(when->result, state, resv, resnull);
/* Emit JUMP step to jump to end of CASE's code */
scratch.opcode = EEOP_JUMP;
scratch.d.jump.jumpdone = -1; /* computed later */
ExprEvalPushStep(state, &scratch);
/*
* Don't know address for that jump yet, compute once the
* whole CASE expression is built.
*/
adjust_jumps = lappend_int(adjust_jumps,
state->steps_len - 1);
/*
* But we can set WHEN test's jump target now, to make it
* jump to the next WHEN subexpression or the ELSE.
*/
state->steps[whenstep].d.jump.jumpdone = state->steps_len;
}
/* transformCaseExpr always adds a default */
Assert(caseExpr->defresult);
/* evaluate ELSE expr into CASE's result variables */
ExecInitExprRec(caseExpr->defresult, state,
resv, resnull);
/* adjust jump targets */
foreach(lc, adjust_jumps)
{
ExprEvalStep *as = &state->steps[lfirst_int(lc)];
Assert(as->opcode == EEOP_JUMP);
Assert(as->d.jump.jumpdone == -1);
as->d.jump.jumpdone = state->steps_len;
}
break;
}
case T_CaseTestExpr:
{
/*
* Read from location identified by innermost_caseval. Note
* that innermost_caseval could be NULL, if this node isn't
* actually within a CaseExpr, ArrayCoerceExpr, etc structure.
* That can happen because some parts of the system abuse
* CaseTestExpr to cause a read of a value externally supplied
* in econtext->caseValue_datum. We'll take care of that
* scenario at runtime.
*/
scratch.opcode = EEOP_CASE_TESTVAL;
scratch.d.casetest.value = state->innermost_caseval;
scratch.d.casetest.isnull = state->innermost_casenull;
ExprEvalPushStep(state, &scratch);
break;
}
case T_ArrayExpr:
{
ArrayExpr *arrayexpr = (ArrayExpr *) node;
int nelems = list_length(arrayexpr->elements);
ListCell *lc;
int elemoff;
/*
* Evaluate by computing each element, and then forming the
* array. Elements are computed into scratch arrays
* associated with the ARRAYEXPR step.
*/
scratch.opcode = EEOP_ARRAYEXPR;
scratch.d.arrayexpr.elemvalues =
(Datum *) palloc(sizeof(Datum) * nelems);
scratch.d.arrayexpr.elemnulls =
(bool *) palloc(sizeof(bool) * nelems);
scratch.d.arrayexpr.nelems = nelems;
/* fill remaining fields of step */
scratch.d.arrayexpr.multidims = arrayexpr->multidims;
scratch.d.arrayexpr.elemtype = arrayexpr->element_typeid;
/* do one-time catalog lookup for type info */
get_typlenbyvalalign(arrayexpr->element_typeid,
&scratch.d.arrayexpr.elemlength,
&scratch.d.arrayexpr.elembyval,
&scratch.d.arrayexpr.elemalign);
/* prepare to evaluate all arguments */
elemoff = 0;
foreach(lc, arrayexpr->elements)
{
Expr *e = (Expr *) lfirst(lc);
ExecInitExprRec(e, state,
&scratch.d.arrayexpr.elemvalues[elemoff],
&scratch.d.arrayexpr.elemnulls[elemoff]);
elemoff++;
}
/* and then collect all into an array */
ExprEvalPushStep(state, &scratch);
break;
}
case T_RowExpr:
{
RowExpr *rowexpr = (RowExpr *) node;
int nelems = list_length(rowexpr->args);
TupleDesc tupdesc;
int i;
ListCell *l;
/* Build tupdesc to describe result tuples */
if (rowexpr->row_typeid == RECORDOID)
{
/* generic record, use types of given expressions */
tupdesc = ExecTypeFromExprList(rowexpr->args);
/* ... but adopt RowExpr's column aliases */
ExecTypeSetColNames(tupdesc, rowexpr->colnames);
/* Bless the tupdesc so it can be looked up later */
BlessTupleDesc(tupdesc);
}
else
{
/* it's been cast to a named type, use that */
tupdesc = lookup_rowtype_tupdesc_copy(rowexpr->row_typeid, -1);
}
/*
* In the named-type case, the tupdesc could have more columns
* than are in the args list, since the type might have had
* columns added since the ROW() was parsed. We want those
* extra columns to go to nulls, so we make sure that the
* workspace arrays are large enough and then initialize any
* extra columns to read as NULLs.
*/
Assert(nelems <= tupdesc->natts);
nelems = Max(nelems, tupdesc->natts);
/*
* Evaluate by first building datums for each field, and then
* a final step forming the composite datum.
*/
scratch.opcode = EEOP_ROW;
scratch.d.row.tupdesc = tupdesc;
/* space for the individual field datums */
scratch.d.row.elemvalues =
(Datum *) palloc(sizeof(Datum) * nelems);
scratch.d.row.elemnulls =
(bool *) palloc(sizeof(bool) * nelems);
/* as explained above, make sure any extra columns are null */
memset(scratch.d.row.elemnulls, true, sizeof(bool) * nelems);
/* Set up evaluation, skipping any deleted columns */
i = 0;
foreach(l, rowexpr->args)
{
Form_pg_attribute att = TupleDescAttr(tupdesc, i);
Expr *e = (Expr *) lfirst(l);
if (!att->attisdropped)
{
/*
* Guard against ALTER COLUMN TYPE on rowtype since
* the RowExpr was created. XXX should we check
* typmod too? Not sure we can be sure it'll be the
* same.
*/
if (exprType((Node *) e) != att->atttypid)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("ROW() column has type %s instead of type %s",
format_type_be(exprType((Node *) e)),
format_type_be(att->atttypid))));
}
else
{
/*
* Ignore original expression and insert a NULL. We
* don't really care what type of NULL it is, so
* always make an int4 NULL.
*/
e = (Expr *) makeNullConst(INT4OID, -1, InvalidOid);
}
/* Evaluate column expr into appropriate workspace slot */
ExecInitExprRec(e, state,
&scratch.d.row.elemvalues[i],
&scratch.d.row.elemnulls[i]);
i++;
}
/* And finally build the row value */
ExprEvalPushStep(state, &scratch);
break;
}
case T_RowCompareExpr:
{
RowCompareExpr *rcexpr = (RowCompareExpr *) node;
int nopers = list_length(rcexpr->opnos);
List *adjust_jumps = NIL;
ListCell *l_left_expr,
*l_right_expr,
*l_opno,
*l_opfamily,
*l_inputcollid;
ListCell *lc;
/*
* Iterate over each field, prepare comparisons. To handle
* NULL results, prepare jumps to after the expression. If a
* comparison yields a != 0 result, jump to the final step.
*/
Assert(list_length(rcexpr->largs) == nopers);
Assert(list_length(rcexpr->rargs) == nopers);
Assert(list_length(rcexpr->opfamilies) == nopers);
Assert(list_length(rcexpr->inputcollids) == nopers);
forfive(l_left_expr, rcexpr->largs,
l_right_expr, rcexpr->rargs,
l_opno, rcexpr->opnos,
l_opfamily, rcexpr->opfamilies,
l_inputcollid, rcexpr->inputcollids)
{
Expr *left_expr = (Expr *) lfirst(l_left_expr);
Expr *right_expr = (Expr *) lfirst(l_right_expr);
Oid opno = lfirst_oid(l_opno);
Oid opfamily = lfirst_oid(l_opfamily);
Oid inputcollid = lfirst_oid(l_inputcollid);
int strategy;
Oid lefttype;
Oid righttype;
Oid proc;
FmgrInfo *finfo;
FunctionCallInfo fcinfo;
get_op_opfamily_properties(opno, opfamily, false,
&strategy,
&lefttype,
&righttype);
proc = get_opfamily_proc(opfamily,
lefttype,
righttype,
BTORDER_PROC);
if (!OidIsValid(proc))
elog(ERROR, "missing support function %d(%u,%u) in opfamily %u",
BTORDER_PROC, lefttype, righttype, opfamily);
/* Set up the primary fmgr lookup information */
finfo = palloc0(sizeof(FmgrInfo));
fcinfo = palloc0(SizeForFunctionCallInfo(2));
fmgr_info(proc, finfo);
fmgr_info_set_expr((Node *) node, finfo);
InitFunctionCallInfoData(*fcinfo, finfo, 2,
inputcollid, NULL, NULL);
/*
* If we enforced permissions checks on index support
* functions, we'd need to make a check here. But the
* index support machinery doesn't do that, and thus
* neither does this code.
*/
/* evaluate left and right args directly into fcinfo */
ExecInitExprRec(left_expr, state,
&fcinfo->args[0].value, &fcinfo->args[0].isnull);
ExecInitExprRec(right_expr, state,
&fcinfo->args[1].value, &fcinfo->args[1].isnull);
scratch.opcode = EEOP_ROWCOMPARE_STEP;
scratch.d.rowcompare_step.finfo = finfo;
scratch.d.rowcompare_step.fcinfo_data = fcinfo;
scratch.d.rowcompare_step.fn_addr = finfo->fn_addr;
/* jump targets filled below */
scratch.d.rowcompare_step.jumpnull = -1;
scratch.d.rowcompare_step.jumpdone = -1;
ExprEvalPushStep(state, &scratch);
adjust_jumps = lappend_int(adjust_jumps,
state->steps_len - 1);
}
/*
* We could have a zero-column rowtype, in which case the rows
* necessarily compare equal.
*/
if (nopers == 0)
{
scratch.opcode = EEOP_CONST;
scratch.d.constval.value = Int32GetDatum(0);
scratch.d.constval.isnull = false;
ExprEvalPushStep(state, &scratch);
}
/* Finally, examine the last comparison result */
scratch.opcode = EEOP_ROWCOMPARE_FINAL;
scratch.d.rowcompare_final.rctype = rcexpr->rctype;
ExprEvalPushStep(state, &scratch);
/* adjust jump targets */
foreach(lc, adjust_jumps)
{
ExprEvalStep *as = &state->steps[lfirst_int(lc)];
Assert(as->opcode == EEOP_ROWCOMPARE_STEP);
Assert(as->d.rowcompare_step.jumpdone == -1);
Assert(as->d.rowcompare_step.jumpnull == -1);
/* jump to comparison evaluation */
as->d.rowcompare_step.jumpdone = state->steps_len - 1;
/* jump to the following expression */
as->d.rowcompare_step.jumpnull = state->steps_len;
}
break;
}
case T_CoalesceExpr:
{
CoalesceExpr *coalesce = (CoalesceExpr *) node;
List *adjust_jumps = NIL;
ListCell *lc;
/* We assume there's at least one arg */
Assert(coalesce->args != NIL);
/*
* Prepare evaluation of all coalesced arguments, after each
* one push a step that short-circuits if not null.
*/
foreach(lc, coalesce->args)
{
Expr *e = (Expr *) lfirst(lc);
/* evaluate argument, directly into result datum */
ExecInitExprRec(e, state, resv, resnull);
/* if it's not null, skip to end of COALESCE expr */
scratch.opcode = EEOP_JUMP_IF_NOT_NULL;
scratch.d.jump.jumpdone = -1; /* adjust later */
ExprEvalPushStep(state, &scratch);
adjust_jumps = lappend_int(adjust_jumps,
state->steps_len - 1);
}
/*
* No need to add a constant NULL return - we only can get to
* the end of the expression if a NULL already is being
* returned.
*/
/* adjust jump targets */
foreach(lc, adjust_jumps)
{
ExprEvalStep *as = &state->steps[lfirst_int(lc)];
Assert(as->opcode == EEOP_JUMP_IF_NOT_NULL);
Assert(as->d.jump.jumpdone == -1);
as->d.jump.jumpdone = state->steps_len;
}
break;
}
case T_MinMaxExpr:
{
MinMaxExpr *minmaxexpr = (MinMaxExpr *) node;
int nelems = list_length(minmaxexpr->args);
TypeCacheEntry *typentry;
FmgrInfo *finfo;
FunctionCallInfo fcinfo;
ListCell *lc;
int off;
/* Look up the btree comparison function for the datatype */
typentry = lookup_type_cache(minmaxexpr->minmaxtype,
TYPECACHE_CMP_PROC);
if (!OidIsValid(typentry->cmp_proc))
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_FUNCTION),
errmsg("could not identify a comparison function for type %s",
format_type_be(minmaxexpr->minmaxtype))));
/*
* If we enforced permissions checks on index support
* functions, we'd need to make a check here. But the index
* support machinery doesn't do that, and thus neither does
* this code.
*/
/* Perform function lookup */
finfo = palloc0(sizeof(FmgrInfo));
fcinfo = palloc0(SizeForFunctionCallInfo(2));
fmgr_info(typentry->cmp_proc, finfo);
fmgr_info_set_expr((Node *) node, finfo);
InitFunctionCallInfoData(*fcinfo, finfo, 2,
minmaxexpr->inputcollid, NULL, NULL);
scratch.opcode = EEOP_MINMAX;
/* allocate space to store arguments */
scratch.d.minmax.values =
(Datum *) palloc(sizeof(Datum) * nelems);
scratch.d.minmax.nulls =
(bool *) palloc(sizeof(bool) * nelems);
scratch.d.minmax.nelems = nelems;
scratch.d.minmax.op = minmaxexpr->op;
scratch.d.minmax.finfo = finfo;
scratch.d.minmax.fcinfo_data = fcinfo;
/* evaluate expressions into minmax->values/nulls */
off = 0;
foreach(lc, minmaxexpr->args)
{
Expr *e = (Expr *) lfirst(lc);
ExecInitExprRec(e, state,
&scratch.d.minmax.values[off],
&scratch.d.minmax.nulls[off]);
off++;
}
/* and push the final comparison */
ExprEvalPushStep(state, &scratch);
break;
}
case T_SQLValueFunction:
{
SQLValueFunction *svf = (SQLValueFunction *) node;
scratch.opcode = EEOP_SQLVALUEFUNCTION;
scratch.d.sqlvaluefunction.svf = svf;
ExprEvalPushStep(state, &scratch);
break;
}
case T_XmlExpr:
{
XmlExpr *xexpr = (XmlExpr *) node;
int nnamed = list_length(xexpr->named_args);
int nargs = list_length(xexpr->args);
int off;
ListCell *arg;
scratch.opcode = EEOP_XMLEXPR;
scratch.d.xmlexpr.xexpr = xexpr;
/* allocate space for storing all the arguments */
if (nnamed)
{
scratch.d.xmlexpr.named_argvalue =
(Datum *) palloc(sizeof(Datum) * nnamed);
scratch.d.xmlexpr.named_argnull =
(bool *) palloc(sizeof(bool) * nnamed);
}
else
{
scratch.d.xmlexpr.named_argvalue = NULL;
scratch.d.xmlexpr.named_argnull = NULL;
}
if (nargs)
{
scratch.d.xmlexpr.argvalue =
(Datum *) palloc(sizeof(Datum) * nargs);
scratch.d.xmlexpr.argnull =
(bool *) palloc(sizeof(bool) * nargs);
}
else
{
scratch.d.xmlexpr.argvalue = NULL;
scratch.d.xmlexpr.argnull = NULL;
}
/* prepare argument execution */
off = 0;
foreach(arg, xexpr->named_args)
{
Expr *e = (Expr *) lfirst(arg);
ExecInitExprRec(e, state,
&scratch.d.xmlexpr.named_argvalue[off],
&scratch.d.xmlexpr.named_argnull[off]);
off++;
}
off = 0;
foreach(arg, xexpr->args)
{
Expr *e = (Expr *) lfirst(arg);
ExecInitExprRec(e, state,
&scratch.d.xmlexpr.argvalue[off],
&scratch.d.xmlexpr.argnull[off]);
off++;
}
/* and evaluate the actual XML expression */
ExprEvalPushStep(state, &scratch);
break;
}
case T_NullTest:
{
NullTest *ntest = (NullTest *) node;
if (ntest->nulltesttype == IS_NULL)
{
if (ntest->argisrow)
scratch.opcode = EEOP_NULLTEST_ROWISNULL;
else
scratch.opcode = EEOP_NULLTEST_ISNULL;
}
else if (ntest->nulltesttype == IS_NOT_NULL)
{
if (ntest->argisrow)
scratch.opcode = EEOP_NULLTEST_ROWISNOTNULL;
else
scratch.opcode = EEOP_NULLTEST_ISNOTNULL;
}
else
{
elog(ERROR, "unrecognized nulltesttype: %d",
(int) ntest->nulltesttype);
}
/* initialize cache in case it's a row test */
scratch.d.nulltest_row.rowcache.cacheptr = NULL;
/* first evaluate argument into result variable */
ExecInitExprRec(ntest->arg, state,
resv, resnull);
/* then push the test of that argument */
ExprEvalPushStep(state, &scratch);
break;
}
case T_BooleanTest:
{
BooleanTest *btest = (BooleanTest *) node;
/*
* Evaluate argument, directly into result datum. That's ok,
* because resv/resnull is definitely not used anywhere else,
* and will get overwritten by the below EEOP_BOOLTEST_IS_*
* step.
*/
ExecInitExprRec(btest->arg, state, resv, resnull);
switch (btest->booltesttype)
{
case IS_TRUE:
scratch.opcode = EEOP_BOOLTEST_IS_TRUE;
break;
case IS_NOT_TRUE:
scratch.opcode = EEOP_BOOLTEST_IS_NOT_TRUE;
break;
case IS_FALSE:
scratch.opcode = EEOP_BOOLTEST_IS_FALSE;
break;
case IS_NOT_FALSE:
scratch.opcode = EEOP_BOOLTEST_IS_NOT_FALSE;
break;
case IS_UNKNOWN:
/* Same as scalar IS NULL test */
scratch.opcode = EEOP_NULLTEST_ISNULL;
break;
case IS_NOT_UNKNOWN:
/* Same as scalar IS NOT NULL test */
scratch.opcode = EEOP_NULLTEST_ISNOTNULL;
break;
default:
elog(ERROR, "unrecognized booltesttype: %d",
(int) btest->booltesttype);
}
ExprEvalPushStep(state, &scratch);
break;
}
case T_CoerceToDomain:
{
CoerceToDomain *ctest = (CoerceToDomain *) node;
ExecInitCoerceToDomain(&scratch, ctest, state,
resv, resnull);
break;
}
case T_CoerceToDomainValue:
{
/*
* Read from location identified by innermost_domainval. Note
* that innermost_domainval could be NULL, if we're compiling
* a standalone domain check rather than one embedded in a
* larger expression. In that case we must read from
* econtext->domainValue_datum. We'll take care of that
* scenario at runtime.
*/
scratch.opcode = EEOP_DOMAIN_TESTVAL;
/* we share instruction union variant with case testval */
scratch.d.casetest.value = state->innermost_domainval;
scratch.d.casetest.isnull = state->innermost_domainnull;
ExprEvalPushStep(state, &scratch);
break;
}
case T_CurrentOfExpr:
{
scratch.opcode = EEOP_CURRENTOFEXPR;
ExprEvalPushStep(state, &scratch);
break;
}
case T_NextValueExpr:
{
NextValueExpr *nve = (NextValueExpr *) node;
scratch.opcode = EEOP_NEXTVALUEEXPR;
scratch.d.nextvalueexpr.seqid = nve->seqid;
scratch.d.nextvalueexpr.seqtypid = nve->typeId;
ExprEvalPushStep(state, &scratch);
break;
}
default:
elog(ERROR, "unrecognized node type: %d",
(int) nodeTag(node));
break;
}
}
/*
* Add another expression evaluation step to ExprState->steps.
*
* Note that this potentially re-allocates es->steps, therefore no pointer
* into that array may be used while the expression is still being built.
*/
void
ExprEvalPushStep(ExprState *es, const ExprEvalStep *s)
{
if (es->steps_alloc == 0)
{
es->steps_alloc = 16;
es->steps = palloc(sizeof(ExprEvalStep) * es->steps_alloc);
}
else if (es->steps_alloc == es->steps_len)
{
es->steps_alloc *= 2;
es->steps = repalloc(es->steps,
sizeof(ExprEvalStep) * es->steps_alloc);
}
memcpy(&es->steps[es->steps_len++], s, sizeof(ExprEvalStep));
}
/*
* Perform setup necessary for the evaluation of a function-like expression,
* appending argument evaluation steps to the steps list in *state, and
* setting up *scratch so it is ready to be pushed.
*
* *scratch is not pushed here, so that callers may override the opcode,
* which is useful for function-like cases like DISTINCT.
*/
static void
ExecInitFunc(ExprEvalStep *scratch, Expr *node, List *args, Oid funcid,
Oid inputcollid, ExprState *state)
{
int nargs = list_length(args);
AclResult aclresult;
FmgrInfo *flinfo;
FunctionCallInfo fcinfo;
int argno;
ListCell *lc;
/* Check permission to call function */
aclresult = pg_proc_aclcheck(funcid, GetUserId(), ACL_EXECUTE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(funcid));
InvokeFunctionExecuteHook(funcid);
/*
* Safety check on nargs. Under normal circumstances this should never
* fail, as parser should check sooner. But possibly it might fail if
* server has been compiled with FUNC_MAX_ARGS smaller than some functions
* declared in pg_proc?
*/
if (nargs > FUNC_MAX_ARGS)
ereport(ERROR,
(errcode(ERRCODE_TOO_MANY_ARGUMENTS),
errmsg_plural("cannot pass more than %d argument to a function",
"cannot pass more than %d arguments to a function",
FUNC_MAX_ARGS,
FUNC_MAX_ARGS)));
/* Allocate function lookup data and parameter workspace for this call */
scratch->d.func.finfo = palloc0(sizeof(FmgrInfo));
scratch->d.func.fcinfo_data = palloc0(SizeForFunctionCallInfo(nargs));
flinfo = scratch->d.func.finfo;
fcinfo = scratch->d.func.fcinfo_data;
/* Set up the primary fmgr lookup information */
fmgr_info(funcid, flinfo);
fmgr_info_set_expr((Node *) node, flinfo);
/* Initialize function call parameter structure too */
InitFunctionCallInfoData(*fcinfo, flinfo,
nargs, inputcollid, NULL, NULL);
/* Keep extra copies of this info to save an indirection at runtime */
scratch->d.func.fn_addr = flinfo->fn_addr;
scratch->d.func.nargs = nargs;
/* We only support non-set functions here */
if (flinfo->fn_retset)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("set-valued function called in context that cannot accept a set"),
state->parent ?
executor_errposition(state->parent->state,
exprLocation((Node *) node)) : 0));
/* Build code to evaluate arguments directly into the fcinfo struct */
argno = 0;
foreach(lc, args)
{
Expr *arg = (Expr *) lfirst(lc);
if (IsA(arg, Const))
{
/*
* Don't evaluate const arguments every round; especially
* interesting for constants in comparisons.
*/
Const *con = (Const *) arg;
fcinfo->args[argno].value = con->constvalue;
fcinfo->args[argno].isnull = con->constisnull;
}
else
{
ExecInitExprRec(arg, state,
&fcinfo->args[argno].value,
&fcinfo->args[argno].isnull);
}
argno++;
}
/* Insert appropriate opcode depending on strictness and stats level */
if (pgstat_track_functions <= flinfo->fn_stats)
{
if (flinfo->fn_strict && nargs > 0)
scratch->opcode = EEOP_FUNCEXPR_STRICT;
else
scratch->opcode = EEOP_FUNCEXPR;
}
else
{
if (flinfo->fn_strict && nargs > 0)
scratch->opcode = EEOP_FUNCEXPR_STRICT_FUSAGE;
else
scratch->opcode = EEOP_FUNCEXPR_FUSAGE;
}
}
/*
* Add expression steps performing setup that's needed before any of the
* main execution of the expression.
*/
static void
ExecCreateExprSetupSteps(ExprState *state, Node *node)
{
ExprSetupInfo info = {0, 0, 0, NIL};
/* Prescan to find out what we need. */
expr_setup_walker(node, &info);
/* And generate those steps. */
ExecPushExprSetupSteps(state, &info);
}
/*
* Add steps performing expression setup as indicated by "info".
* This is useful when building an ExprState covering more than one expression.
*/
static void
ExecPushExprSetupSteps(ExprState *state, ExprSetupInfo *info)
{
ExprEvalStep scratch = {0};
ListCell *lc;
scratch.resvalue = NULL;
scratch.resnull = NULL;
/*
* Add steps deforming the ExprState's inner/outer/scan slots as much as
* required by any Vars appearing in the expression.
*/
if (info->last_inner > 0)
{
scratch.opcode = EEOP_INNER_FETCHSOME;
scratch.d.fetch.last_var = info->last_inner;
scratch.d.fetch.fixed = false;
scratch.d.fetch.kind = NULL;
scratch.d.fetch.known_desc = NULL;
if (ExecComputeSlotInfo(state, &scratch))
ExprEvalPushStep(state, &scratch);
}
if (info->last_outer > 0)
{
scratch.opcode = EEOP_OUTER_FETCHSOME;
scratch.d.fetch.last_var = info->last_outer;
scratch.d.fetch.fixed = false;
scratch.d.fetch.kind = NULL;
scratch.d.fetch.known_desc = NULL;
if (ExecComputeSlotInfo(state, &scratch))
ExprEvalPushStep(state, &scratch);
}
if (info->last_scan > 0)
{
scratch.opcode = EEOP_SCAN_FETCHSOME;
scratch.d.fetch.last_var = info->last_scan;
scratch.d.fetch.fixed = false;
scratch.d.fetch.kind = NULL;
scratch.d.fetch.known_desc = NULL;
if (ExecComputeSlotInfo(state, &scratch))
ExprEvalPushStep(state, &scratch);
}
/*
* Add steps to execute any MULTIEXPR SubPlans appearing in the
* expression. We need to evaluate these before any of the Params
* referencing their outputs are used, but after we've prepared for any
* Var references they may contain. (There cannot be cross-references
* between MULTIEXPR SubPlans, so we needn't worry about their order.)
*/
foreach(lc, info->multiexpr_subplans)
{
SubPlan *subplan = (SubPlan *) lfirst(lc);
SubPlanState *sstate;
Assert(subplan->subLinkType == MULTIEXPR_SUBLINK);
/* This should match what ExecInitExprRec does for other SubPlans: */
if (!state->parent)
elog(ERROR, "SubPlan found with no parent plan");
sstate = ExecInitSubPlan(subplan, state->parent);
/* add SubPlanState nodes to state->parent->subPlan */
state->parent->subPlan = lappend(state->parent->subPlan,
sstate);
scratch.opcode = EEOP_SUBPLAN;
scratch.d.subplan.sstate = sstate;
/* The result can be ignored, but we better put it somewhere */
scratch.resvalue = &state->resvalue;
scratch.resnull = &state->resnull;
ExprEvalPushStep(state, &scratch);
}
}
/*
* expr_setup_walker: expression walker for ExecCreateExprSetupSteps
*/
static bool
expr_setup_walker(Node *node, ExprSetupInfo *info)
{
if (node == NULL)
return false;
if (IsA(node, Var))
{
Var *variable = (Var *) node;
AttrNumber attnum = variable->varattno;
switch (variable->varno)
{
case INNER_VAR:
info->last_inner = Max(info->last_inner, attnum);
break;
case OUTER_VAR:
info->last_outer = Max(info->last_outer, attnum);
break;
/* INDEX_VAR is handled by default case */
default:
info->last_scan = Max(info->last_scan, attnum);
break;
}
return false;
}
/* Collect all MULTIEXPR SubPlans, too */
if (IsA(node, SubPlan))
{
SubPlan *subplan = (SubPlan *) node;
if (subplan->subLinkType == MULTIEXPR_SUBLINK)
info->multiexpr_subplans = lappend(info->multiexpr_subplans,
subplan);
}
/*
* Don't examine the arguments or filters of Aggrefs or WindowFuncs,
* because those do not represent expressions to be evaluated within the
* calling expression's econtext. GroupingFunc arguments are never
* evaluated at all.
*/
if (IsA(node, Aggref))
return false;
if (IsA(node, WindowFunc))
return false;
if (IsA(node, GroupingFunc))
return false;
return expression_tree_walker(node, expr_setup_walker,
(void *) info);
}
/*
* Compute additional information for EEOP_*_FETCHSOME ops.
*
* The goal is to determine whether a slot is 'fixed', that is, every
* evaluation of the expression will have the same type of slot, with an
* equivalent descriptor.
*
* Returns true if the deforming step is required, false otherwise.
*/
static bool
ExecComputeSlotInfo(ExprState *state, ExprEvalStep *op)
{
PlanState *parent = state->parent;
TupleDesc desc = NULL;
const TupleTableSlotOps *tts_ops = NULL;
bool isfixed = false;
ExprEvalOp opcode = op->opcode;
Assert(opcode == EEOP_INNER_FETCHSOME ||
opcode == EEOP_OUTER_FETCHSOME ||
opcode == EEOP_SCAN_FETCHSOME);
if (op->d.fetch.known_desc != NULL)
{
desc = op->d.fetch.known_desc;
tts_ops = op->d.fetch.kind;
isfixed = op->d.fetch.kind != NULL;
}
else if (!parent)
{
isfixed = false;
}
else if (opcode == EEOP_INNER_FETCHSOME)
{
PlanState *is = innerPlanState(parent);
if (parent->inneropsset && !parent->inneropsfixed)
{
isfixed = false;
}
else if (parent->inneropsset && parent->innerops)
{
isfixed = true;
tts_ops = parent->innerops;
desc = ExecGetResultType(is);
}
else if (is)
{
tts_ops = ExecGetResultSlotOps(is, &isfixed);
desc = ExecGetResultType(is);
}
}
else if (opcode == EEOP_OUTER_FETCHSOME)
{
PlanState *os = outerPlanState(parent);
if (parent->outeropsset && !parent->outeropsfixed)
{
isfixed = false;
}
else if (parent->outeropsset && parent->outerops)
{
isfixed = true;
tts_ops = parent->outerops;
desc = ExecGetResultType(os);
}
else if (os)
{
tts_ops = ExecGetResultSlotOps(os, &isfixed);
desc = ExecGetResultType(os);
}
}
else if (opcode == EEOP_SCAN_FETCHSOME)
{
desc = parent->scandesc;
if (parent->scanops)
tts_ops = parent->scanops;
if (parent->scanopsset)
isfixed = parent->scanopsfixed;
}
if (isfixed && desc != NULL && tts_ops != NULL)
{
op->d.fetch.fixed = true;
op->d.fetch.kind = tts_ops;
op->d.fetch.known_desc = desc;
}
else
{
op->d.fetch.fixed = false;
op->d.fetch.kind = NULL;
op->d.fetch.known_desc = NULL;
}
/* if the slot is known to always virtual we never need to deform */
if (op->d.fetch.fixed && op->d.fetch.kind == &TTSOpsVirtual)
return false;
return true;
}
/*
* Prepare step for the evaluation of a whole-row variable.
* The caller still has to push the step.
*/
static void
ExecInitWholeRowVar(ExprEvalStep *scratch, Var *variable, ExprState *state)
{
PlanState *parent = state->parent;
/* fill in all but the target */
scratch->opcode = EEOP_WHOLEROW;
scratch->d.wholerow.var = variable;
scratch->d.wholerow.first = true;
scratch->d.wholerow.slow = false;
scratch->d.wholerow.tupdesc = NULL; /* filled at runtime */
scratch->d.wholerow.junkFilter = NULL;
/*
* If the input tuple came from a subquery, it might contain "resjunk"
* columns (such as GROUP BY or ORDER BY columns), which we don't want to
* keep in the whole-row result. We can get rid of such columns by
* passing the tuple through a JunkFilter --- but to make one, we have to
* lay our hands on the subquery's targetlist. Fortunately, there are not
* very many cases where this can happen, and we can identify all of them
* by examining our parent PlanState. We assume this is not an issue in
* standalone expressions that don't have parent plans. (Whole-row Vars
* can occur in such expressions, but they will always be referencing
* table rows.)
*/
if (parent)
{
PlanState *subplan = NULL;
switch (nodeTag(parent))
{
case T_SubqueryScanState:
subplan = ((SubqueryScanState *) parent)->subplan;
break;
case T_CteScanState:
subplan = ((CteScanState *) parent)->cteplanstate;
break;
default:
break;
}
if (subplan)
{
bool junk_filter_needed = false;
ListCell *tlist;
/* Detect whether subplan tlist actually has any junk columns */
foreach(tlist, subplan->plan->targetlist)
{
TargetEntry *tle = (TargetEntry *) lfirst(tlist);
if (tle->resjunk)
{
junk_filter_needed = true;
break;
}
}
/* If so, build the junkfilter now */
if (junk_filter_needed)
{
scratch->d.wholerow.junkFilter =
ExecInitJunkFilter(subplan->plan->targetlist,
ExecInitExtraTupleSlot(parent->state, NULL,
&TTSOpsVirtual));
}
}
}
}
/*
* Prepare evaluation of a SubscriptingRef expression.
*/
static void
ExecInitSubscriptingRef(ExprEvalStep *scratch, SubscriptingRef *sbsref,
ExprState *state, Datum *resv, bool *resnull)
{
bool isAssignment = (sbsref->refassgnexpr != NULL);
int nupper = list_length(sbsref->refupperindexpr);
int nlower = list_length(sbsref->reflowerindexpr);
const SubscriptRoutines *sbsroutines;
SubscriptingRefState *sbsrefstate;
SubscriptExecSteps methods;
char *ptr;
List *adjust_jumps = NIL;
ListCell *lc;
int i;
/* Look up the subscripting support methods */
sbsroutines = getSubscriptingRoutines(sbsref->refcontainertype, NULL);
if (!sbsroutines)
ereport(ERROR,
(errcode(ERRCODE_DATATYPE_MISMATCH),
errmsg("cannot subscript type %s because it does not support subscripting",
format_type_be(sbsref->refcontainertype)),
state->parent ?
executor_errposition(state->parent->state,
exprLocation((Node *) sbsref)) : 0));
/* Allocate sbsrefstate, with enough space for per-subscript arrays too */
sbsrefstate = palloc0(MAXALIGN(sizeof(SubscriptingRefState)) +
(nupper + nlower) * (sizeof(Datum) +
2 * sizeof(bool)));
/* Fill constant fields of SubscriptingRefState */
sbsrefstate->isassignment = isAssignment;
sbsrefstate->numupper = nupper;
sbsrefstate->numlower = nlower;
/* Set up per-subscript arrays */
ptr = ((char *) sbsrefstate) + MAXALIGN(sizeof(SubscriptingRefState));
sbsrefstate->upperindex = (Datum *) ptr;
ptr += nupper * sizeof(Datum);
sbsrefstate->lowerindex = (Datum *) ptr;
ptr += nlower * sizeof(Datum);
sbsrefstate->upperprovided = (bool *) ptr;
ptr += nupper * sizeof(bool);
sbsrefstate->lowerprovided = (bool *) ptr;
ptr += nlower * sizeof(bool);
sbsrefstate->upperindexnull = (bool *) ptr;
ptr += nupper * sizeof(bool);
sbsrefstate->lowerindexnull = (bool *) ptr;
/* ptr += nlower * sizeof(bool); */
/*
* Let the container-type-specific code have a chance. It must fill the
* "methods" struct with function pointers for us to possibly use in
* execution steps below; and it can optionally set up some data pointed
* to by the workspace field.
*/
memset(&methods, 0, sizeof(methods));
sbsroutines->exec_setup(sbsref, sbsrefstate, &methods);
/*
* Evaluate array input. It's safe to do so into resv/resnull, because we
* won't use that as target for any of the other subexpressions, and it'll
* be overwritten by the final EEOP_SBSREF_FETCH/ASSIGN step, which is
* pushed last.
*/
ExecInitExprRec(sbsref->refexpr, state, resv, resnull);
/*
* If refexpr yields NULL, and the operation should be strict, then result
* is NULL. We can implement this with just JUMP_IF_NULL, since we
* evaluated the array into the desired target location.
*/
if (!isAssignment && sbsroutines->fetch_strict)
{
scratch->opcode = EEOP_JUMP_IF_NULL;
scratch->d.jump.jumpdone = -1; /* adjust later */
ExprEvalPushStep(state, scratch);
adjust_jumps = lappend_int(adjust_jumps,
state->steps_len - 1);
}
/* Evaluate upper subscripts */
i = 0;
foreach(lc, sbsref->refupperindexpr)
{
Expr *e = (Expr *) lfirst(lc);
/* When slicing, individual subscript bounds can be omitted */
if (!e)
{
sbsrefstate->upperprovided[i] = false;
sbsrefstate->upperindexnull[i] = true;
}
else
{
sbsrefstate->upperprovided[i] = true;
/* Each subscript is evaluated into appropriate array entry */
ExecInitExprRec(e, state,
&sbsrefstate->upperindex[i],
&sbsrefstate->upperindexnull[i]);
}
i++;
}
/* Evaluate lower subscripts similarly */
i = 0;
foreach(lc, sbsref->reflowerindexpr)
{
Expr *e = (Expr *) lfirst(lc);
/* When slicing, individual subscript bounds can be omitted */
if (!e)
{
sbsrefstate->lowerprovided[i] = false;
sbsrefstate->lowerindexnull[i] = true;
}
else
{
sbsrefstate->lowerprovided[i] = true;
/* Each subscript is evaluated into appropriate array entry */
ExecInitExprRec(e, state,
&sbsrefstate->lowerindex[i],
&sbsrefstate->lowerindexnull[i]);
}
i++;
}
/* SBSREF_SUBSCRIPTS checks and converts all the subscripts at once */
if (methods.sbs_check_subscripts)
{
scratch->opcode = EEOP_SBSREF_SUBSCRIPTS;
scratch->d.sbsref_subscript.subscriptfunc = methods.sbs_check_subscripts;
scratch->d.sbsref_subscript.state = sbsrefstate;
scratch->d.sbsref_subscript.jumpdone = -1; /* adjust later */
ExprEvalPushStep(state, scratch);
adjust_jumps = lappend_int(adjust_jumps,
state->steps_len - 1);
}
if (isAssignment)
{
Datum *save_innermost_caseval;
bool *save_innermost_casenull;
/* Check for unimplemented methods */
if (!methods.sbs_assign)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("type %s does not support subscripted assignment",
format_type_be(sbsref->refcontainertype))));
/*
* We might have a nested-assignment situation, in which the
* refassgnexpr is itself a FieldStore or SubscriptingRef that needs
* to obtain and modify the previous value of the array element or
* slice being replaced. If so, we have to extract that value from
* the array and pass it down via the CaseTestExpr mechanism. It's
* safe to reuse the CASE mechanism because there cannot be a CASE
* between here and where the value would be needed, and an array
* assignment can't be within a CASE either. (So saving and restoring
* innermost_caseval is just paranoia, but let's do it anyway.)
*
* Since fetching the old element might be a nontrivial expense, do it
* only if the argument actually needs it.
*/
if (isAssignmentIndirectionExpr(sbsref->refassgnexpr))
{
if (!methods.sbs_fetch_old)
ereport(ERROR,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("type %s does not support subscripted assignment",
format_type_be(sbsref->refcontainertype))));
scratch->opcode = EEOP_SBSREF_OLD;
scratch->d.sbsref.subscriptfunc = methods.sbs_fetch_old;
scratch->d.sbsref.state = sbsrefstate;
ExprEvalPushStep(state, scratch);
}
/* SBSREF_OLD puts extracted value into prevvalue/prevnull */
save_innermost_caseval = state->innermost_caseval;
save_innermost_casenull = state->innermost_casenull;
state->innermost_caseval = &sbsrefstate->prevvalue;
state->innermost_casenull = &sbsrefstate->prevnull;
/* evaluate replacement value into replacevalue/replacenull */
ExecInitExprRec(sbsref->refassgnexpr, state,
&sbsrefstate->replacevalue, &sbsrefstate->replacenull);
state->innermost_caseval = save_innermost_caseval;
state->innermost_casenull = save_innermost_casenull;
/* and perform the assignment */
scratch->opcode = EEOP_SBSREF_ASSIGN;
scratch->d.sbsref.subscriptfunc = methods.sbs_assign;
scratch->d.sbsref.state = sbsrefstate;
ExprEvalPushStep(state, scratch);
}
else
{
/* array fetch is much simpler */
scratch->opcode = EEOP_SBSREF_FETCH;
scratch->d.sbsref.subscriptfunc = methods.sbs_fetch;
scratch->d.sbsref.state = sbsrefstate;
ExprEvalPushStep(state, scratch);
}
/* adjust jump targets */
foreach(lc, adjust_jumps)
{
ExprEvalStep *as = &state->steps[lfirst_int(lc)];
if (as->opcode == EEOP_SBSREF_SUBSCRIPTS)
{
Assert(as->d.sbsref_subscript.jumpdone == -1);
as->d.sbsref_subscript.jumpdone = state->steps_len;
}
else
{
Assert(as->opcode == EEOP_JUMP_IF_NULL);
Assert(as->d.jump.jumpdone == -1);
as->d.jump.jumpdone = state->steps_len;
}
}
}
/*
* Helper for preparing SubscriptingRef expressions for evaluation: is expr
* a nested FieldStore or SubscriptingRef that needs the old element value
* passed down?
*
* (We could use this in FieldStore too, but in that case passing the old
* value is so cheap there's no need.)
*
* Note: it might seem that this needs to recurse, but in most cases it does
* not; the CaseTestExpr, if any, will be directly the arg or refexpr of the
* top-level node. Nested-assignment situations give rise to expression
* trees in which each level of assignment has its own CaseTestExpr, and the
* recursive structure appears within the newvals or refassgnexpr field.
* There is an exception, though: if the array is an array-of-domain, we will
* have a CoerceToDomain or RelabelType as the refassgnexpr, and we need to
* be able to look through that.
*/
static bool
isAssignmentIndirectionExpr(Expr *expr)
{
if (expr == NULL)
return false; /* just paranoia */
if (IsA(expr, FieldStore))
{
FieldStore *fstore = (FieldStore *) expr;
if (fstore->arg && IsA(fstore->arg, CaseTestExpr))
return true;
}
else if (IsA(expr, SubscriptingRef))
{
SubscriptingRef *sbsRef = (SubscriptingRef *) expr;
if (sbsRef->refexpr && IsA(sbsRef->refexpr, CaseTestExpr))
return true;
}
else if (IsA(expr, CoerceToDomain))
{
CoerceToDomain *cd = (CoerceToDomain *) expr;
return isAssignmentIndirectionExpr(cd->arg);
}
else if (IsA(expr, RelabelType))
{
RelabelType *r = (RelabelType *) expr;
return isAssignmentIndirectionExpr(r->arg);
}
return false;
}
/*
* Prepare evaluation of a CoerceToDomain expression.
*/
static void
ExecInitCoerceToDomain(ExprEvalStep *scratch, CoerceToDomain *ctest,
ExprState *state, Datum *resv, bool *resnull)
{
DomainConstraintRef *constraint_ref;
Datum *domainval = NULL;
bool *domainnull = NULL;
ListCell *l;
scratch->d.domaincheck.resulttype = ctest->resulttype;
/* we'll allocate workspace only if needed */
scratch->d.domaincheck.checkvalue = NULL;
scratch->d.domaincheck.checknull = NULL;
/*
* Evaluate argument - it's fine to directly store it into resv/resnull,
* if there's constraint failures there'll be errors, otherwise it's what
* needs to be returned.
*/
ExecInitExprRec(ctest->arg, state, resv, resnull);
/*
* Note: if the argument is of varlena type, it could be a R/W expanded
* object. We want to return the R/W pointer as the final result, but we
* have to pass a R/O pointer as the value to be tested by any functions
* in check expressions. We don't bother to emit a MAKE_READONLY step
* unless there's actually at least one check expression, though. Until
* we've tested that, domainval/domainnull are NULL.
*/
/*
* Collect the constraints associated with the domain.
*
* Note: before PG v10 we'd recheck the set of constraints during each
* evaluation of the expression. Now we bake them into the ExprState
* during executor initialization. That means we don't need typcache.c to
* provide compiled exprs.
*/
constraint_ref = (DomainConstraintRef *)
palloc(sizeof(DomainConstraintRef));
InitDomainConstraintRef(ctest->resulttype,
constraint_ref,
CurrentMemoryContext,
false);
/*
* Compile code to check each domain constraint. NOTNULL constraints can
* just be applied on the resv/resnull value, but for CHECK constraints we
* need more pushups.
*/
foreach(l, constraint_ref->constraints)
{
DomainConstraintState *con = (DomainConstraintState *) lfirst(l);
Datum *save_innermost_domainval;
bool *save_innermost_domainnull;
scratch->d.domaincheck.constraintname = con->name;
switch (con->constrainttype)
{
case DOM_CONSTRAINT_NOTNULL:
scratch->opcode = EEOP_DOMAIN_NOTNULL;
ExprEvalPushStep(state, scratch);
break;
case DOM_CONSTRAINT_CHECK:
/* Allocate workspace for CHECK output if we didn't yet */
if (scratch->d.domaincheck.checkvalue == NULL)
{
scratch->d.domaincheck.checkvalue =
(Datum *) palloc(sizeof(Datum));
scratch->d.domaincheck.checknull =
(bool *) palloc(sizeof(bool));
}
/*
* If first time through, determine where CoerceToDomainValue
* nodes should read from.
*/
if (domainval == NULL)
{
/*
* Since value might be read multiple times, force to R/O
* - but only if it could be an expanded datum.
*/
if (get_typlen(ctest->resulttype) == -1)
{
ExprEvalStep scratch2 = {0};
/* Yes, so make output workspace for MAKE_READONLY */
domainval = (Datum *) palloc(sizeof(Datum));
domainnull = (bool *) palloc(sizeof(bool));
/* Emit MAKE_READONLY */
scratch2.opcode = EEOP_MAKE_READONLY;
scratch2.resvalue = domainval;
scratch2.resnull = domainnull;
scratch2.d.make_readonly.value = resv;
scratch2.d.make_readonly.isnull = resnull;
ExprEvalPushStep(state, &scratch2);
}
else
{
/* No, so it's fine to read from resv/resnull */
domainval = resv;
domainnull = resnull;
}
}
/*
* Set up value to be returned by CoerceToDomainValue nodes.
* We must save and restore innermost_domainval/null fields,
* in case this node is itself within a check expression for
* another domain.
*/
save_innermost_domainval = state->innermost_domainval;
save_innermost_domainnull = state->innermost_domainnull;
state->innermost_domainval = domainval;
state->innermost_domainnull = domainnull;
/* evaluate check expression value */
ExecInitExprRec(con->check_expr, state,
scratch->d.domaincheck.checkvalue,
scratch->d.domaincheck.checknull);
state->innermost_domainval = save_innermost_domainval;
state->innermost_domainnull = save_innermost_domainnull;
/* now test result */
scratch->opcode = EEOP_DOMAIN_CHECK;
ExprEvalPushStep(state, scratch);
break;
default:
elog(ERROR, "unrecognized constraint type: %d",
(int) con->constrainttype);
break;
}
}
}
/*
* Build transition/combine function invocations for all aggregate transition
* / combination function invocations in a grouping sets phase. This has to
* invoke all sort based transitions in a phase (if doSort is true), all hash
* based transitions (if doHash is true), or both (both true).
*
* The resulting expression will, for each set of transition values, first
* check for filters, evaluate aggregate input, check that that input is not
* NULL for a strict transition function, and then finally invoke the
* transition for each of the concurrently computed grouping sets.
*
* If nullcheck is true, the generated code will check for a NULL pointer to
* the array of AggStatePerGroup, and skip evaluation if so.
*/
ExprState *
ExecBuildAggTrans(AggState *aggstate, AggStatePerPhase phase,
bool doSort, bool doHash, bool nullcheck)
{
ExprState *state = makeNode(ExprState);
PlanState *parent = &aggstate->ss.ps;
ExprEvalStep scratch = {0};
bool isCombine = DO_AGGSPLIT_COMBINE(aggstate->aggsplit);
ExprSetupInfo deform = {0, 0, 0, NIL};
state->expr = (Expr *) aggstate;
state->parent = parent;
scratch.resvalue = &state->resvalue;
scratch.resnull = &state->resnull;
/*
* First figure out which slots, and how many columns from each, we're
* going to need.
*/
for (int transno = 0; transno < aggstate->numtrans; transno++)
{
AggStatePerTrans pertrans = &aggstate->pertrans[transno];
expr_setup_walker((Node *) pertrans->aggref->aggdirectargs,
&deform);
expr_setup_walker((Node *) pertrans->aggref->args,
&deform);
expr_setup_walker((Node *) pertrans->aggref->aggorder,
&deform);
expr_setup_walker((Node *) pertrans->aggref->aggdistinct,
&deform);
expr_setup_walker((Node *) pertrans->aggref->aggfilter,
&deform);
}
ExecPushExprSetupSteps(state, &deform);
/*
* Emit instructions for each transition value / grouping set combination.
*/
for (int transno = 0; transno < aggstate->numtrans; transno++)
{
AggStatePerTrans pertrans = &aggstate->pertrans[transno];
FunctionCallInfo trans_fcinfo = pertrans->transfn_fcinfo;
List *adjust_bailout = NIL;
NullableDatum *strictargs = NULL;
bool *strictnulls = NULL;
int argno;
ListCell *bail;
/*
* If filter present, emit. Do so before evaluating the input, to
* avoid potentially unneeded computations, or even worse, unintended
* side-effects. When combining, all the necessary filtering has
* already been done.
*/
if (pertrans->aggref->aggfilter && !isCombine)
{
/* evaluate filter expression */
ExecInitExprRec(pertrans->aggref->aggfilter, state,
&state->resvalue, &state->resnull);
/* and jump out if false */
scratch.opcode = EEOP_JUMP_IF_NOT_TRUE;
scratch.d.jump.jumpdone = -1; /* adjust later */
ExprEvalPushStep(state, &scratch);
adjust_bailout = lappend_int(adjust_bailout,
state->steps_len - 1);
}
/*
* Evaluate arguments to aggregate/combine function.
*/
argno = 0;
if (isCombine)
{
/*
* Combining two aggregate transition values. Instead of directly
* coming from a tuple the input is a, potentially deserialized,
* transition value.
*/
TargetEntry *source_tle;
Assert(pertrans->numSortCols == 0);
Assert(list_length(pertrans->aggref->args) == 1);
strictargs = trans_fcinfo->args + 1;
source_tle = (TargetEntry *) linitial(pertrans->aggref->args);
/*
* deserialfn_oid will be set if we must deserialize the input
* state before calling the combine function.
*/
if (!OidIsValid(pertrans->deserialfn_oid))
{
/*
* Start from 1, since the 0th arg will be the transition
* value
*/
ExecInitExprRec(source_tle->expr, state,
&trans_fcinfo->args[argno + 1].value,
&trans_fcinfo->args[argno + 1].isnull);
}
else
{
FunctionCallInfo ds_fcinfo = pertrans->deserialfn_fcinfo;
/* evaluate argument */
ExecInitExprRec(source_tle->expr, state,
&ds_fcinfo->args[0].value,
&ds_fcinfo->args[0].isnull);
/* Dummy second argument for type-safety reasons */
ds_fcinfo->args[1].value = PointerGetDatum(NULL);
ds_fcinfo->args[1].isnull = false;
/*
* Don't call a strict deserialization function with NULL
* input
*/
if (pertrans->deserialfn.fn_strict)
scratch.opcode = EEOP_AGG_STRICT_DESERIALIZE;
else
scratch.opcode = EEOP_AGG_DESERIALIZE;
scratch.d.agg_deserialize.fcinfo_data = ds_fcinfo;
scratch.d.agg_deserialize.jumpnull = -1; /* adjust later */
scratch.resvalue = &trans_fcinfo->args[argno + 1].value;
scratch.resnull = &trans_fcinfo->args[argno + 1].isnull;
ExprEvalPushStep(state, &scratch);
/* don't add an adjustment unless the function is strict */
if (pertrans->deserialfn.fn_strict)
adjust_bailout = lappend_int(adjust_bailout,
state->steps_len - 1);
/* restore normal settings of scratch fields */
scratch.resvalue = &state->resvalue;
scratch.resnull = &state->resnull;
}
argno++;
}
else if (pertrans->numSortCols == 0)
{
ListCell *arg;
/*
* Normal transition function without ORDER BY / DISTINCT.
*/
strictargs = trans_fcinfo->args + 1;
foreach(arg, pertrans->aggref->args)
{
TargetEntry *source_tle = (TargetEntry *) lfirst(arg);
/*
* Start from 1, since the 0th arg will be the transition
* value
*/
ExecInitExprRec(source_tle->expr, state,
&trans_fcinfo->args[argno + 1].value,
&trans_fcinfo->args[argno + 1].isnull);
argno++;
}
}
else if (pertrans->numInputs == 1)
{
/*
* DISTINCT and/or ORDER BY case, with a single column sorted on.
*/
TargetEntry *source_tle =
(TargetEntry *) linitial(pertrans->aggref->args);
Assert(list_length(pertrans->aggref->args) == 1);
ExecInitExprRec(source_tle->expr, state,
&state->resvalue,
&state->resnull);
strictnulls = &state->resnull;
argno++;
}
else
{
/*
* DISTINCT and/or ORDER BY case, with multiple columns sorted on.
*/
Datum *values = pertrans->sortslot->tts_values;
bool *nulls = pertrans->sortslot->tts_isnull;
ListCell *arg;
strictnulls = nulls;
foreach(arg, pertrans->aggref->args)
{
TargetEntry *source_tle = (TargetEntry *) lfirst(arg);
ExecInitExprRec(source_tle->expr, state,
&values[argno], &nulls[argno]);
argno++;
}
}
Assert(pertrans->numInputs == argno);
/*
* For a strict transfn, nothing happens when there's a NULL input; we
* just keep the prior transValue. This is true for both plain and
* sorted/distinct aggregates.
*/
if (trans_fcinfo->flinfo->fn_strict && pertrans->numTransInputs > 0)
{
if (strictnulls)
scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK_NULLS;
else
scratch.opcode = EEOP_AGG_STRICT_INPUT_CHECK_ARGS;
scratch.d.agg_strict_input_check.nulls = strictnulls;
scratch.d.agg_strict_input_check.args = strictargs;
scratch.d.agg_strict_input_check.jumpnull = -1; /* adjust later */
scratch.d.agg_strict_input_check.nargs = pertrans->numTransInputs;
ExprEvalPushStep(state, &scratch);
adjust_bailout = lappend_int(adjust_bailout,
state->steps_len - 1);
}
/*
* Call transition function (once for each concurrently evaluated
* grouping set). Do so for both sort and hash based computations, as
* applicable.
*/
if (doSort)
{
int processGroupingSets = Max(phase->numsets, 1);
int setoff = 0;
for (int setno = 0; setno < processGroupingSets; setno++)
{
ExecBuildAggTransCall(state, aggstate, &scratch, trans_fcinfo,
pertrans, transno, setno, setoff, false,
nullcheck);
setoff++;
}
}
if (doHash)
{
int numHashes = aggstate->num_hashes;
int setoff;
/* in MIXED mode, there'll be preceding transition values */
if (aggstate->aggstrategy != AGG_HASHED)
setoff = aggstate->maxsets;
else
setoff = 0;
for (int setno = 0; setno < numHashes; setno++)
{
ExecBuildAggTransCall(state, aggstate, &scratch, trans_fcinfo,
pertrans, transno, setno, setoff, true,
nullcheck);
setoff++;
}
}
/* adjust early bail out jump target(s) */
foreach(bail, adjust_bailout)
{
ExprEvalStep *as = &state->steps[lfirst_int(bail)];
if (as->opcode == EEOP_JUMP_IF_NOT_TRUE)
{
Assert(as->d.jump.jumpdone == -1);
as->d.jump.jumpdone = state->steps_len;
}
else if (as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_ARGS ||
as->opcode == EEOP_AGG_STRICT_INPUT_CHECK_NULLS)
{
Assert(as->d.agg_strict_input_check.jumpnull == -1);
as->d.agg_strict_input_check.jumpnull = state->steps_len;
}
else if (as->opcode == EEOP_AGG_STRICT_DESERIALIZE)
{
Assert(as->d.agg_deserialize.jumpnull == -1);
as->d.agg_deserialize.jumpnull = state->steps_len;
}
else
Assert(false);
}
}
scratch.resvalue = NULL;
scratch.resnull = NULL;
scratch.opcode = EEOP_DONE;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
return state;
}
/*
* Build transition/combine function invocation for a single transition
* value. This is separated from ExecBuildAggTrans() because there are
* multiple callsites (hash and sort in some grouping set cases).
*/
static void
ExecBuildAggTransCall(ExprState *state, AggState *aggstate,
ExprEvalStep *scratch,
FunctionCallInfo fcinfo, AggStatePerTrans pertrans,
int transno, int setno, int setoff, bool ishash,
bool nullcheck)
{
ExprContext *aggcontext;
int adjust_jumpnull = -1;
if (ishash)
aggcontext = aggstate->hashcontext;
else
aggcontext = aggstate->aggcontexts[setno];
/* add check for NULL pointer? */
if (nullcheck)
{
scratch->opcode = EEOP_AGG_PLAIN_PERGROUP_NULLCHECK;
scratch->d.agg_plain_pergroup_nullcheck.setoff = setoff;
/* adjust later */
scratch->d.agg_plain_pergroup_nullcheck.jumpnull = -1;
ExprEvalPushStep(state, scratch);
adjust_jumpnull = state->steps_len - 1;
}
/*
* Determine appropriate transition implementation.
*
* For non-ordered aggregates:
*
* If the initial value for the transition state doesn't exist in the
* pg_aggregate table then we will let the first non-NULL value returned
* from the outer procNode become the initial value. (This is useful for
* aggregates like max() and min().) The noTransValue flag signals that we
* need to do so. If true, generate a
* EEOP_AGG_INIT_STRICT_PLAIN_TRANS{,_BYVAL} step. This step also needs to
* do the work described next:
*
* If the function is strict, but does have an initial value, choose
* EEOP_AGG_STRICT_PLAIN_TRANS{,_BYVAL}, which skips the transition
* function if the transition value has become NULL (because a previous
* transition function returned NULL). This step also needs to do the work
* described next:
*
* Otherwise we call EEOP_AGG_PLAIN_TRANS{,_BYVAL}, which does not have to
* perform either of the above checks.
*
* Having steps with overlapping responsibilities is not nice, but
* aggregations are very performance sensitive, making this worthwhile.
*
* For ordered aggregates:
*
* Only need to choose between the faster path for a single ordered
* column, and the one between multiple columns. Checking strictness etc
* is done when finalizing the aggregate. See
* process_ordered_aggregate_{single, multi} and
* advance_transition_function.
*/
if (pertrans->numSortCols == 0)
{
if (pertrans->transtypeByVal)
{
if (fcinfo->flinfo->fn_strict &&
pertrans->initValueIsNull)
scratch->opcode = EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYVAL;
else if (fcinfo->flinfo->fn_strict)
scratch->opcode = EEOP_AGG_PLAIN_TRANS_STRICT_BYVAL;
else
scratch->opcode = EEOP_AGG_PLAIN_TRANS_BYVAL;
}
else
{
if (fcinfo->flinfo->fn_strict &&
pertrans->initValueIsNull)
scratch->opcode = EEOP_AGG_PLAIN_TRANS_INIT_STRICT_BYREF;
else if (fcinfo->flinfo->fn_strict)
scratch->opcode = EEOP_AGG_PLAIN_TRANS_STRICT_BYREF;
else
scratch->opcode = EEOP_AGG_PLAIN_TRANS_BYREF;
}
}
else if (pertrans->numInputs == 1)
scratch->opcode = EEOP_AGG_ORDERED_TRANS_DATUM;
else
scratch->opcode = EEOP_AGG_ORDERED_TRANS_TUPLE;
scratch->d.agg_trans.pertrans = pertrans;
scratch->d.agg_trans.setno = setno;
scratch->d.agg_trans.setoff = setoff;
scratch->d.agg_trans.transno = transno;
scratch->d.agg_trans.aggcontext = aggcontext;
ExprEvalPushStep(state, scratch);
/* fix up jumpnull */
if (adjust_jumpnull != -1)
{
ExprEvalStep *as = &state->steps[adjust_jumpnull];
Assert(as->opcode == EEOP_AGG_PLAIN_PERGROUP_NULLCHECK);
Assert(as->d.agg_plain_pergroup_nullcheck.jumpnull == -1);
as->d.agg_plain_pergroup_nullcheck.jumpnull = state->steps_len;
}
}
/*
* Build equality expression that can be evaluated using ExecQual(), returning
* true if the expression context's inner/outer tuple are NOT DISTINCT. I.e
* two nulls match, a null and a not-null don't match.
*
* desc: tuple descriptor of the to-be-compared tuples
* numCols: the number of attributes to be examined
* keyColIdx: array of attribute column numbers
* eqFunctions: array of function oids of the equality functions to use
* parent: parent executor node
*/
ExprState *
ExecBuildGroupingEqual(TupleDesc ldesc, TupleDesc rdesc,
const TupleTableSlotOps *lops, const TupleTableSlotOps *rops,
int numCols,
const AttrNumber *keyColIdx,
const Oid *eqfunctions,
const Oid *collations,
PlanState *parent)
{
ExprState *state = makeNode(ExprState);
ExprEvalStep scratch = {0};
int maxatt = -1;
List *adjust_jumps = NIL;
ListCell *lc;
/*
* When no columns are actually compared, the result's always true. See
* special case in ExecQual().
*/
if (numCols == 0)
return NULL;
state->expr = NULL;
state->flags = EEO_FLAG_IS_QUAL;
state->parent = parent;
scratch.resvalue = &state->resvalue;
scratch.resnull = &state->resnull;
/* compute max needed attribute */
for (int natt = 0; natt < numCols; natt++)
{
int attno = keyColIdx[natt];
if (attno > maxatt)
maxatt = attno;
}
Assert(maxatt >= 0);
/* push deform steps */
scratch.opcode = EEOP_INNER_FETCHSOME;
scratch.d.fetch.last_var = maxatt;
scratch.d.fetch.fixed = false;
scratch.d.fetch.known_desc = ldesc;
scratch.d.fetch.kind = lops;
if (ExecComputeSlotInfo(state, &scratch))
ExprEvalPushStep(state, &scratch);
scratch.opcode = EEOP_OUTER_FETCHSOME;
scratch.d.fetch.last_var = maxatt;
scratch.d.fetch.fixed = false;
scratch.d.fetch.known_desc = rdesc;
scratch.d.fetch.kind = rops;
if (ExecComputeSlotInfo(state, &scratch))
ExprEvalPushStep(state, &scratch);
/*
* Start comparing at the last field (least significant sort key). That's
* the most likely to be different if we are dealing with sorted input.
*/
for (int natt = numCols; --natt >= 0;)
{
int attno = keyColIdx[natt];
Form_pg_attribute latt = TupleDescAttr(ldesc, attno - 1);
Form_pg_attribute ratt = TupleDescAttr(rdesc, attno - 1);
Oid foid = eqfunctions[natt];
Oid collid = collations[natt];
FmgrInfo *finfo;
FunctionCallInfo fcinfo;
AclResult aclresult;
/* Check permission to call function */
aclresult = pg_proc_aclcheck(foid, GetUserId(), ACL_EXECUTE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(foid));
InvokeFunctionExecuteHook(foid);
/* Set up the primary fmgr lookup information */
finfo = palloc0(sizeof(FmgrInfo));
fcinfo = palloc0(SizeForFunctionCallInfo(2));
fmgr_info(foid, finfo);
fmgr_info_set_expr(NULL, finfo);
InitFunctionCallInfoData(*fcinfo, finfo, 2,
collid, NULL, NULL);
/* left arg */
scratch.opcode = EEOP_INNER_VAR;
scratch.d.var.attnum = attno - 1;
scratch.d.var.vartype = latt->atttypid;
scratch.resvalue = &fcinfo->args[0].value;
scratch.resnull = &fcinfo->args[0].isnull;
ExprEvalPushStep(state, &scratch);
/* right arg */
scratch.opcode = EEOP_OUTER_VAR;
scratch.d.var.attnum = attno - 1;
scratch.d.var.vartype = ratt->atttypid;
scratch.resvalue = &fcinfo->args[1].value;
scratch.resnull = &fcinfo->args[1].isnull;
ExprEvalPushStep(state, &scratch);
/* evaluate distinctness */
scratch.opcode = EEOP_NOT_DISTINCT;
scratch.d.func.finfo = finfo;
scratch.d.func.fcinfo_data = fcinfo;
scratch.d.func.fn_addr = finfo->fn_addr;
scratch.d.func.nargs = 2;
scratch.resvalue = &state->resvalue;
scratch.resnull = &state->resnull;
ExprEvalPushStep(state, &scratch);
/* then emit EEOP_QUAL to detect if result is false (or null) */
scratch.opcode = EEOP_QUAL;
scratch.d.qualexpr.jumpdone = -1;
scratch.resvalue = &state->resvalue;
scratch.resnull = &state->resnull;
ExprEvalPushStep(state, &scratch);
adjust_jumps = lappend_int(adjust_jumps,
state->steps_len - 1);
}
/* adjust jump targets */
foreach(lc, adjust_jumps)
{
ExprEvalStep *as = &state->steps[lfirst_int(lc)];
Assert(as->opcode == EEOP_QUAL);
Assert(as->d.qualexpr.jumpdone == -1);
as->d.qualexpr.jumpdone = state->steps_len;
}
scratch.resvalue = NULL;
scratch.resnull = NULL;
scratch.opcode = EEOP_DONE;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
return state;
}
/*
* Build equality expression that can be evaluated using ExecQual(), returning
* true if the expression context's inner/outer tuples are equal. Datums in
* the inner/outer slots are assumed to be in the same order and quantity as
* the 'eqfunctions' parameter. NULLs are treated as equal.
*
* desc: tuple descriptor of the to-be-compared tuples
* lops: the slot ops for the inner tuple slots
* rops: the slot ops for the outer tuple slots
* eqFunctions: array of function oids of the equality functions to use
* this must be the same length as the 'param_exprs' list.
* collations: collation Oids to use for equality comparison. Must be the
* same length as the 'param_exprs' list.
* parent: parent executor node
*/
ExprState *
ExecBuildParamSetEqual(TupleDesc desc,
const TupleTableSlotOps *lops,
const TupleTableSlotOps *rops,
const Oid *eqfunctions,
const Oid *collations,
const List *param_exprs,
PlanState *parent)
{
ExprState *state = makeNode(ExprState);
ExprEvalStep scratch = {0};
int maxatt = list_length(param_exprs);
List *adjust_jumps = NIL;
ListCell *lc;
state->expr = NULL;
state->flags = EEO_FLAG_IS_QUAL;
state->parent = parent;
scratch.resvalue = &state->resvalue;
scratch.resnull = &state->resnull;
/* push deform steps */
scratch.opcode = EEOP_INNER_FETCHSOME;
scratch.d.fetch.last_var = maxatt;
scratch.d.fetch.fixed = false;
scratch.d.fetch.known_desc = desc;
scratch.d.fetch.kind = lops;
if (ExecComputeSlotInfo(state, &scratch))
ExprEvalPushStep(state, &scratch);
scratch.opcode = EEOP_OUTER_FETCHSOME;
scratch.d.fetch.last_var = maxatt;
scratch.d.fetch.fixed = false;
scratch.d.fetch.known_desc = desc;
scratch.d.fetch.kind = rops;
if (ExecComputeSlotInfo(state, &scratch))
ExprEvalPushStep(state, &scratch);
for (int attno = 0; attno < maxatt; attno++)
{
Form_pg_attribute att = TupleDescAttr(desc, attno);
Oid foid = eqfunctions[attno];
Oid collid = collations[attno];
FmgrInfo *finfo;
FunctionCallInfo fcinfo;
AclResult aclresult;
/* Check permission to call function */
aclresult = pg_proc_aclcheck(foid, GetUserId(), ACL_EXECUTE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, OBJECT_FUNCTION, get_func_name(foid));
InvokeFunctionExecuteHook(foid);
/* Set up the primary fmgr lookup information */
finfo = palloc0(sizeof(FmgrInfo));
fcinfo = palloc0(SizeForFunctionCallInfo(2));
fmgr_info(foid, finfo);
fmgr_info_set_expr(NULL, finfo);
InitFunctionCallInfoData(*fcinfo, finfo, 2,
collid, NULL, NULL);
/* left arg */
scratch.opcode = EEOP_INNER_VAR;
scratch.d.var.attnum = attno;
scratch.d.var.vartype = att->atttypid;
scratch.resvalue = &fcinfo->args[0].value;
scratch.resnull = &fcinfo->args[0].isnull;
ExprEvalPushStep(state, &scratch);
/* right arg */
scratch.opcode = EEOP_OUTER_VAR;
scratch.d.var.attnum = attno;
scratch.d.var.vartype = att->atttypid;
scratch.resvalue = &fcinfo->args[1].value;
scratch.resnull = &fcinfo->args[1].isnull;
ExprEvalPushStep(state, &scratch);
/* evaluate distinctness */
scratch.opcode = EEOP_NOT_DISTINCT;
scratch.d.func.finfo = finfo;
scratch.d.func.fcinfo_data = fcinfo;
scratch.d.func.fn_addr = finfo->fn_addr;
scratch.d.func.nargs = 2;
scratch.resvalue = &state->resvalue;
scratch.resnull = &state->resnull;
ExprEvalPushStep(state, &scratch);
/* then emit EEOP_QUAL to detect if result is false (or null) */
scratch.opcode = EEOP_QUAL;
scratch.d.qualexpr.jumpdone = -1;
scratch.resvalue = &state->resvalue;
scratch.resnull = &state->resnull;
ExprEvalPushStep(state, &scratch);
adjust_jumps = lappend_int(adjust_jumps,
state->steps_len - 1);
}
/* adjust jump targets */
foreach(lc, adjust_jumps)
{
ExprEvalStep *as = &state->steps[lfirst_int(lc)];
Assert(as->opcode == EEOP_QUAL);
Assert(as->d.qualexpr.jumpdone == -1);
as->d.qualexpr.jumpdone = state->steps_len;
}
scratch.resvalue = NULL;
scratch.resnull = NULL;
scratch.opcode = EEOP_DONE;
ExprEvalPushStep(state, &scratch);
ExecReadyExpr(state);
return state;
}
|