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
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
|
/* Copyright (c) 2000, 2015, Oracle and/or its affiliates.
Copyright (c) 2009, 2015, MariaDB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/**
@file
@brief
This file defines all numerical functions
*/
#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation // gcc: Class implementation
#endif
#include "sql_plugin.h"
#include "sql_priv.h"
/*
It is necessary to include set_var.h instead of item.h because there
are dependencies on include order for set_var.h and item.h. This
will be resolved later.
*/
#include "sql_class.h" // set_var.h: THD
#include "set_var.h"
#include "slave.h" // for wait_for_master_pos
#include "sql_show.h" // append_identifier
#include "strfunc.h" // find_type
#include "sql_parse.h" // is_update_query
#include "sql_acl.h" // EXECUTE_ACL
#include "mysqld.h" // LOCK_short_uuid_generator
#include "rpl_mi.h"
#include "sql_time.h"
#include <m_ctype.h>
#include <hash.h>
#include <time.h>
#include <ft_global.h>
#include <my_bit.h>
#include "sp_head.h"
#include "sp_rcontext.h"
#include "sp.h"
#include "set_var.h"
#include "debug_sync.h"
#ifdef NO_EMBEDDED_ACCESS_CHECKS
#define sp_restore_security_context(A,B) while (0) {}
#endif
bool check_reserved_words(LEX_STRING *name)
{
if (!my_strcasecmp(system_charset_info, name->str, "GLOBAL") ||
!my_strcasecmp(system_charset_info, name->str, "LOCAL") ||
!my_strcasecmp(system_charset_info, name->str, "SESSION"))
return TRUE;
return FALSE;
}
/**
@return
TRUE if item is a constant
*/
bool
eval_const_cond(COND *cond)
{
return ((Item_func*) cond)->val_int() ? TRUE : FALSE;
}
/**
Test if the sum of arguments overflows the ulonglong range.
*/
static inline bool test_if_sum_overflows_ull(ulonglong arg1, ulonglong arg2)
{
return ULONGLONG_MAX - arg1 < arg2;
}
void Item_args::set_arguments(THD *thd, List<Item> &list)
{
arg_count= list.elements;
if (arg_count <= 2)
{
args= tmp_arg;
}
else if (!(args= (Item**) thd->alloc(sizeof(Item*) * arg_count)))
{
arg_count= 0;
return;
}
uint i= 0;
List_iterator_fast<Item> li(list);
Item *item;
while ((item= li++))
args[i++]= item;
}
Item_args::Item_args(THD *thd, const Item_args *other)
:arg_count(other->arg_count)
{
if (arg_count <= 2)
{
args= tmp_arg;
}
else if (!(args= (Item**) thd->alloc(sizeof(Item*) * arg_count)))
{
arg_count= 0;
return;
}
memcpy(args, other->args, sizeof(Item*) * arg_count);
}
void Item_func::sync_with_sum_func_and_with_field(List<Item> &list)
{
List_iterator_fast<Item> li(list);
Item *item;
while ((item= li++))
{
with_sum_func|= item->with_sum_func;
with_field|= item->with_field;
}
}
/*
Resolve references to table column for a function and its argument
SYNOPSIS:
fix_fields()
thd Thread object
ref Pointer to where this object is used. This reference
is used if we want to replace this object with another
one (for example in the summary functions).
DESCRIPTION
Call fix_fields() for all arguments to the function. The main intention
is to allow all Item_field() objects to setup pointers to the table fields.
Sets as a side effect the following class variables:
maybe_null Set if any argument may return NULL
with_sum_func Set if any of the arguments contains a sum function
with_field Set if any of the arguments contains or is a field
used_tables_cache Set to union of the tables used by arguments
str_value.charset If this is a string function, set this to the
character set for the first argument.
If any argument is binary, this is set to binary
If for any item any of the defaults are wrong, then this can
be fixed in the fix_length_and_dec() function that is called
after this one or by writing a specialized fix_fields() for the
item.
RETURN VALUES
FALSE ok
TRUE Got error. Stored with my_error().
*/
bool
Item_func::fix_fields(THD *thd, Item **ref)
{
DBUG_ASSERT(fixed == 0);
Item **arg,**arg_end;
uchar buff[STACK_BUFF_ALLOC]; // Max argument in function
/*
The Used_tables_and_const_cache of "this" was initialized by
the constructor, or by Item_func::cleanup().
*/
DBUG_ASSERT(used_tables_cache == 0);
DBUG_ASSERT(const_item_cache == true);
not_null_tables_cache= 0;
/*
Use stack limit of STACK_MIN_SIZE * 2 since
on some platforms a recursive call to fix_fields
requires more than STACK_MIN_SIZE bytes (e.g. for
MIPS, it takes about 22kB to make one recursive
call to Item_func::fix_fields())
*/
if (check_stack_overrun(thd, STACK_MIN_SIZE * 2, buff))
return TRUE; // Fatal error if flag is set!
if (arg_count)
{ // Print purify happy
for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
{
Item *item;
/*
We can't yet set item to *arg as fix_fields may change *arg
We shouldn't call fix_fields() twice, so check 'fixed' field first
*/
if ((!(*arg)->fixed && (*arg)->fix_fields(thd, arg)))
return TRUE; /* purecov: inspected */
item= *arg;
if (allowed_arg_cols)
{
if (item->check_cols(allowed_arg_cols))
return 1;
}
else
{
/* we have to fetch allowed_arg_cols from first argument */
DBUG_ASSERT(arg == args); // it is first argument
allowed_arg_cols= item->cols();
DBUG_ASSERT(allowed_arg_cols); // Can't be 0 any more
}
if (item->maybe_null)
maybe_null=1;
with_sum_func= with_sum_func || item->with_sum_func;
with_field= with_field || item->with_field;
used_tables_and_const_cache_join(item);
with_subselect|= item->has_subquery();
}
}
fix_length_and_dec();
if (thd->is_error()) // An error inside fix_length_and_dec occurred
return TRUE;
fixed= 1;
return FALSE;
}
void
Item_func::quick_fix_field()
{
Item **arg,**arg_end;
if (arg_count)
{
for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
{
if (!(*arg)->fixed)
(*arg)->quick_fix_field();
}
}
fixed= 1;
}
bool
Item_func::eval_not_null_tables(uchar *opt_arg)
{
Item **arg,**arg_end;
not_null_tables_cache= 0;
if (arg_count)
{
for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
{
not_null_tables_cache|= (*arg)->not_null_tables();
}
}
return FALSE;
}
void Item_func::fix_after_pullout(st_select_lex *new_parent, Item **ref)
{
Item **arg,**arg_end;
used_tables_and_const_cache_init();
not_null_tables_cache= 0;
if (arg_count)
{
for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++)
{
(*arg)->fix_after_pullout(new_parent, arg);
Item *item= *arg;
used_tables_and_const_cache_join(item);
not_null_tables_cache|= item->not_null_tables();
}
}
}
void Item_func::traverse_cond(Cond_traverser traverser,
void *argument, traverse_order order)
{
if (arg_count)
{
Item **arg,**arg_end;
switch (order) {
case(PREFIX):
(*traverser)(this, argument);
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
{
(*arg)->traverse_cond(traverser, argument, order);
}
break;
case (POSTFIX):
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
{
(*arg)->traverse_cond(traverser, argument, order);
}
(*traverser)(this, argument);
}
}
else
(*traverser)(this, argument);
}
bool Item_args::transform_args(THD *thd, Item_transformer transformer, uchar *arg)
{
for (uint i= 0; i < arg_count; i++)
{
Item *new_item= args[i]->transform(thd, transformer, arg);
if (!new_item)
return true;
/*
THD::change_item_tree() should be called only if the tree was
really transformed, i.e. when a new item has been created.
Otherwise we'll be allocating a lot of unnecessary memory for
change records at each execution.
*/
if (args[i] != new_item)
thd->change_item_tree(&args[i], new_item);
}
return false;
}
/**
Transform an Item_func object with a transformer callback function.
The function recursively applies the transform method to each
argument of the Item_func node.
If the call of the method for an argument item returns a new item
the old item is substituted for a new one.
After this the transformer is applied to the root node
of the Item_func object.
@param transformer the transformer callback function to be applied to
the nodes of the tree of the object
@param argument parameter to be passed to the transformer
@return
Item returned as the result of transformation of the root node
*/
Item *Item_func::transform(THD *thd, Item_transformer transformer, uchar *argument)
{
DBUG_ASSERT(!thd->stmt_arena->is_stmt_prepare());
if (transform_args(thd, transformer, argument))
return 0;
return (this->*transformer)(thd, argument);
}
/**
Compile Item_func object with a processor and a transformer
callback functions.
First the function applies the analyzer to the root node of
the Item_func object. Then if the analizer succeeeds (returns TRUE)
the function recursively applies the compile method to each argument
of the Item_func node.
If the call of the method for an argument item returns a new item
the old item is substituted for a new one.
After this the transformer is applied to the root node
of the Item_func object.
The compile function is not called if the analyzer returns NULL
in the parameter arg_p.
@param analyzer the analyzer callback function to be applied to the
nodes of the tree of the object
@param[in,out] arg_p parameter to be passed to the processor
@param transformer the transformer callback function to be applied to the
nodes of the tree of the object
@param arg_t parameter to be passed to the transformer
@return
Item returned as the result of transformation of the root node
*/
Item *Item_func::compile(THD *thd, Item_analyzer analyzer, uchar **arg_p,
Item_transformer transformer, uchar *arg_t)
{
if (!(this->*analyzer)(arg_p))
return 0;
if (*arg_p && arg_count)
{
Item **arg,**arg_end;
for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++)
{
/*
The same parameter value of arg_p must be passed
to analyze any argument of the condition formula.
*/
uchar *arg_v= *arg_p;
Item *new_item= (*arg)->compile(thd, analyzer, &arg_v, transformer,
arg_t);
if (new_item && *arg != new_item)
thd->change_item_tree(arg, new_item);
}
}
return (this->*transformer)(thd, arg_t);
}
void Item_args::propagate_equal_fields(THD *thd,
const Item::Context &ctx,
COND_EQUAL *cond)
{
uint i;
for (i= 0; i < arg_count; i++)
args[i]->propagate_equal_fields_and_change_item_tree(thd, ctx, cond,
&args[i]);
}
/**
See comments in Item_cond::split_sum_func()
*/
void Item_func::split_sum_func(THD *thd, Item **ref_pointer_array,
List<Item> &fields, uint flags)
{
Item **arg, **arg_end;
for (arg= args, arg_end= args+arg_count; arg != arg_end ; arg++)
(*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg,
flags | SPLIT_SUM_SKIP_REGISTERED);
}
table_map Item_func::not_null_tables() const
{
return not_null_tables_cache;
}
void Item_func::print(String *str, enum_query_type query_type)
{
str->append(func_name());
str->append('(');
print_args(str, 0, query_type);
str->append(')');
}
void Item_func::print_args(String *str, uint from, enum_query_type query_type)
{
for (uint i=from ; i < arg_count ; i++)
{
if (i != from)
str->append(',');
args[i]->print(str, query_type);
}
}
void Item_func::print_op(String *str, enum_query_type query_type)
{
str->append('(');
for (uint i=0 ; i < arg_count-1 ; i++)
{
args[i]->print(str, query_type);
str->append(' ');
str->append(func_name());
str->append(' ');
}
args[arg_count-1]->print(str, query_type);
str->append(')');
}
bool Item_func::eq(const Item *item, bool binary_cmp) const
{
/* Assume we don't have rtti */
if (this == item)
return 1;
/*
Ensure that we are comparing two functions and that the function
is deterministic.
*/
if (item->type() != FUNC_ITEM || (used_tables() & RAND_TABLE_BIT))
return 0;
Item_func *item_func=(Item_func*) item;
Item_func::Functype func_type;
if ((func_type= functype()) != item_func->functype() ||
arg_count != item_func->arg_count ||
(func_type != Item_func::FUNC_SP &&
func_name() != item_func->func_name()) ||
(func_type == Item_func::FUNC_SP &&
my_strcasecmp(system_charset_info, func_name(), item_func->func_name())))
return 0;
for (uint i=0; i < arg_count ; i++)
if (!args[i]->eq(item_func->args[i], binary_cmp))
return 0;
return 1;
}
Field *Item_func::tmp_table_field(TABLE *table)
{
Field *field= NULL;
MEM_ROOT *mem_root= table->in_use->mem_root;
switch (result_type()) {
case INT_RESULT:
if (max_char_length() > MY_INT32_NUM_DECIMAL_DIGITS)
field= new (mem_root)
Field_longlong(max_char_length(), maybe_null, name,
unsigned_flag);
else
field= new (mem_root)
Field_long(max_char_length(), maybe_null, name,
unsigned_flag);
break;
case REAL_RESULT:
field= new (mem_root)
Field_double(max_char_length(), maybe_null, name, decimals);
break;
case STRING_RESULT:
return make_string_field(table);
case DECIMAL_RESULT:
field= Field_new_decimal::create_from_item(mem_root, this);
break;
case ROW_RESULT:
case TIME_RESULT:
// This case should never be chosen
DBUG_ASSERT(0);
field= 0;
break;
}
if (field)
field->init(table);
return field;
}
/*
bool Item_func::is_expensive_processor(uchar *arg)
{
return is_expensive();
}
*/
my_decimal *Item_func::val_decimal(my_decimal *decimal_value)
{
DBUG_ASSERT(fixed);
longlong nr= val_int();
if (null_value)
return 0; /* purecov: inspected */
int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
return decimal_value;
}
String *Item_real_func::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
double nr= val_real();
if (null_value)
return 0; /* purecov: inspected */
str->set_real(nr, decimals, collation.collation);
return str;
}
my_decimal *Item_real_func::val_decimal(my_decimal *decimal_value)
{
DBUG_ASSERT(fixed);
double nr= val_real();
if (null_value)
return 0; /* purecov: inspected */
double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
return decimal_value;
}
void Item_udf_func::fix_num_length_and_dec()
{
uint fl_length= 0;
decimals=0;
for (uint i=0 ; i < arg_count ; i++)
{
set_if_bigger(decimals,args[i]->decimals);
set_if_bigger(fl_length, args[i]->max_length);
}
max_length=float_length(decimals);
if (fl_length > max_length)
{
decimals= NOT_FIXED_DEC;
max_length= float_length(NOT_FIXED_DEC);
}
}
/**
Count max_length and decimals for temporal functions.
@param item Argument array
@param nitems Number of arguments in the array.
@retval False on success, true on error.
*/
void Item_func::count_datetime_length(enum_field_types field_type_arg,
Item **item, uint nitems)
{
unsigned_flag= 0;
decimals= 0;
if (field_type_arg != MYSQL_TYPE_DATE)
{
for (uint i= 0; i < nitems; i++)
set_if_bigger(decimals, item[i]->decimals);
}
set_if_smaller(decimals, TIME_SECOND_PART_DIGITS);
uint len= decimals ? (decimals + 1) : 0;
len+= mysql_temporal_int_part_length(field_type_arg);
fix_char_length(len);
}
/**
Set max_length/decimals of function if function is fixed point and
result length/precision depends on argument ones.
*/
void Item_func::count_decimal_length(Item **item, uint nitems)
{
int max_int_part= 0;
decimals= 0;
unsigned_flag= 1;
for (uint i=0 ; i < nitems ; i++)
{
set_if_bigger(decimals, item[i]->decimals);
set_if_bigger(max_int_part, item[i]->decimal_int_part());
set_if_smaller(unsigned_flag, item[i]->unsigned_flag);
}
int precision= MY_MIN(max_int_part + decimals, DECIMAL_MAX_PRECISION);
fix_char_length(my_decimal_precision_to_length_no_truncation(precision,
decimals,
unsigned_flag));
}
/**
Set max_length of if it is maximum length of its arguments.
*/
void Item_func::count_only_length(Item **item, uint nitems)
{
uint32 char_length= 0;
unsigned_flag= 0;
for (uint i= 0; i < nitems ; i++)
{
set_if_bigger(char_length, item[i]->max_char_length());
set_if_bigger(unsigned_flag, item[i]->unsigned_flag);
}
fix_char_length(char_length);
}
/**
Set max_length/decimals of function if function is floating point and
result length/precision depends on argument ones.
*/
void Item_func::count_real_length(Item **items, uint nitems)
{
uint32 length= 0;
decimals= 0;
max_length= 0;
unsigned_flag= false;
for (uint i=0 ; i < nitems ; i++)
{
if (decimals != NOT_FIXED_DEC)
{
set_if_bigger(decimals, items[i]->decimals);
set_if_bigger(length, (items[i]->max_length - items[i]->decimals));
}
set_if_bigger(max_length, items[i]->max_length);
}
if (decimals != NOT_FIXED_DEC)
{
max_length= length;
length+= decimals;
if (length < max_length) // If previous operation gave overflow
max_length= UINT_MAX32;
else
max_length= length;
}
}
/**
Calculate max_length and decimals for STRING_RESULT functions.
@param field_type Field type.
@param items Argument array.
@param nitems Number of arguments.
@retval False on success, true on error.
*/
bool Item_func::count_string_result_length(enum_field_types field_type_arg,
Item **items, uint nitems)
{
if (agg_arg_charsets_for_string_result(collation, items, nitems, 1))
return true;
if (is_temporal_type(field_type_arg))
count_datetime_length(field_type_arg, items, nitems);
else
{
count_only_length(items, nitems);
decimals= max_length ? NOT_FIXED_DEC : 0;
}
return false;
}
void Item_func::signal_divide_by_null()
{
THD *thd= current_thd;
if (thd->variables.sql_mode & MODE_ERROR_FOR_DIVISION_BY_ZERO)
push_warning(thd, Sql_condition::WARN_LEVEL_WARN, ER_DIVISION_BY_ZERO,
ER_THD(thd, ER_DIVISION_BY_ZERO));
null_value= 1;
}
Item *Item_func::get_tmp_table_item(THD *thd)
{
if (!with_sum_func && !const_item())
return new (thd->mem_root) Item_temptable_field(thd, result_field);
return copy_or_same(thd);
}
double Item_int_func::val_real()
{
DBUG_ASSERT(fixed == 1);
return unsigned_flag ? (double) ((ulonglong) val_int()) : (double) val_int();
}
String *Item_int_func::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
longlong nr=val_int();
if (null_value)
return 0;
str->set_int(nr, unsigned_flag, collation.collation);
return str;
}
void Item_func_connection_id::fix_length_and_dec()
{
Item_int_func::fix_length_and_dec();
max_length= 10;
}
bool Item_func_connection_id::fix_fields(THD *thd, Item **ref)
{
if (Item_int_func::fix_fields(thd, ref))
return TRUE;
thd->thread_specific_used= TRUE;
value= thd->variables.pseudo_thread_id;
return FALSE;
}
/**
Check arguments here to determine result's type for a numeric
function of two arguments.
*/
void Item_num_op::fix_length_and_dec(void)
{
DBUG_ENTER("Item_num_op::fix_length_and_dec");
DBUG_PRINT("info", ("name %s", func_name()));
DBUG_ASSERT(arg_count == 2);
Item_result r0= args[0]->cast_to_int_type();
Item_result r1= args[1]->cast_to_int_type();
if (r0 == REAL_RESULT || r1 == REAL_RESULT ||
r0 == STRING_RESULT || r1 ==STRING_RESULT)
{
count_real_length(args, arg_count);
max_length= float_length(decimals);
set_handler_by_result_type(REAL_RESULT);
}
else if (r0 == DECIMAL_RESULT || r1 == DECIMAL_RESULT ||
r0 == TIME_RESULT || r1 == TIME_RESULT)
{
set_handler_by_result_type(DECIMAL_RESULT);
result_precision();
fix_decimals();
if ((r0 == TIME_RESULT || r1 == TIME_RESULT) && decimals == 0)
set_handler_by_result_type(INT_RESULT);
}
else
{
DBUG_ASSERT(r0 == INT_RESULT && r1 == INT_RESULT);
set_handler_by_result_type(INT_RESULT);
result_precision();
decimals= 0;
}
DBUG_PRINT("info", ("Type: %s",
(result_type() == REAL_RESULT ? "REAL_RESULT" :
result_type() == DECIMAL_RESULT ? "DECIMAL_RESULT" :
result_type() == INT_RESULT ? "INT_RESULT" :
"--ILLEGAL!!!--")));
DBUG_VOID_RETURN;
}
/**
Set result type for a numeric function of one argument
(can be also used by a numeric function of many arguments, if the result
type depends only on the first argument)
*/
void Item_func_num1::fix_length_and_dec()
{
DBUG_ENTER("Item_func_num1::fix_length_and_dec");
DBUG_PRINT("info", ("name %s", func_name()));
// Note, cast_to_int_type() can return TIME_RESULT
switch (args[0]->cast_to_int_type()) {
case INT_RESULT:
set_handler_by_result_type(INT_RESULT);
max_length= args[0]->max_length;
unsigned_flag= args[0]->unsigned_flag;
break;
case STRING_RESULT:
case REAL_RESULT:
set_handler_by_result_type(REAL_RESULT);
decimals= args[0]->decimals; // Preserve NOT_FIXED_DEC
max_length= float_length(decimals);
break;
case TIME_RESULT:
case DECIMAL_RESULT:
set_handler_by_result_type(DECIMAL_RESULT);
decimals= args[0]->decimal_scale(); // Do not preserve NOT_FIXED_DEC
max_length= args[0]->max_length;
break;
case ROW_RESULT:
DBUG_ASSERT(0);
}
DBUG_PRINT("info", ("Type: %s",
(result_type() == REAL_RESULT ? "REAL_RESULT" :
result_type() == DECIMAL_RESULT ? "DECIMAL_RESULT" :
result_type() == INT_RESULT ? "INT_RESULT" :
"--ILLEGAL!!!--")));
DBUG_VOID_RETURN;
}
String *Item_func_hybrid_field_type::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
switch (Item_func_hybrid_field_type::cmp_type()) {
case DECIMAL_RESULT:
{
my_decimal decimal_value, *val;
if (!(val= decimal_op_with_null_check(&decimal_value)))
return 0; // null is set
my_decimal_round(E_DEC_FATAL_ERROR, val, decimals, FALSE, val);
str->set_charset(collation.collation);
my_decimal2string(E_DEC_FATAL_ERROR, val, 0, 0, 0, str);
break;
}
case INT_RESULT:
{
longlong nr= int_op();
if (null_value)
return 0; /* purecov: inspected */
str->set_int(nr, unsigned_flag, collation.collation);
break;
}
case REAL_RESULT:
{
double nr= real_op();
if (null_value)
return 0; /* purecov: inspected */
str->set_real(nr, decimals, collation.collation);
break;
}
case TIME_RESULT:
{
MYSQL_TIME ltime;
if (date_op_with_null_check(<ime) ||
(null_value= str->alloc(MAX_DATE_STRING_REP_LENGTH)))
return (String *) 0;
ltime.time_type= mysql_type_to_time_type(field_type());
str->length(my_TIME_to_str(<ime, const_cast<char*>(str->ptr()), decimals));
str->set_charset(&my_charset_bin);
DBUG_ASSERT(!null_value);
return str;
}
case STRING_RESULT:
return str_op_with_null_check(&str_value);
case ROW_RESULT:
DBUG_ASSERT(0);
}
DBUG_ASSERT(!null_value || (str == NULL));
return str;
}
double Item_func_hybrid_field_type::val_real()
{
DBUG_ASSERT(fixed == 1);
switch (Item_func_hybrid_field_type::cmp_type()) {
case DECIMAL_RESULT:
{
my_decimal decimal_value, *val;
double result;
if (!(val= decimal_op_with_null_check(&decimal_value)))
return 0.0; // null is set
my_decimal2double(E_DEC_FATAL_ERROR, val, &result);
return result;
}
case INT_RESULT:
{
longlong result= int_op();
return unsigned_flag ? (double) ((ulonglong) result) : (double) result;
}
case REAL_RESULT:
return real_op();
case TIME_RESULT:
{
MYSQL_TIME ltime;
if (date_op_with_null_check(<ime))
return 0;
ltime.time_type= mysql_type_to_time_type(field_type());
return TIME_to_double(<ime);
}
case STRING_RESULT:
{
String *res= str_op_with_null_check(&str_value);
return res ? double_from_string_with_check(res) : 0.0;
}
case ROW_RESULT:
DBUG_ASSERT(0);
}
return 0.0;
}
longlong Item_func_hybrid_field_type::val_int()
{
DBUG_ASSERT(fixed == 1);
switch (Item_func_hybrid_field_type::cmp_type()) {
case DECIMAL_RESULT:
{
my_decimal decimal_value, *val;
if (!(val= decimal_op_with_null_check(&decimal_value)))
return 0; // null is set
longlong result;
my_decimal2int(E_DEC_FATAL_ERROR, val, unsigned_flag, &result);
return result;
}
case INT_RESULT:
return int_op();
case REAL_RESULT:
return (longlong) rint(real_op());
case TIME_RESULT:
{
MYSQL_TIME ltime;
if (date_op_with_null_check(<ime))
return 0;
ltime.time_type= mysql_type_to_time_type(field_type());
return TIME_to_ulonglong(<ime);
}
case STRING_RESULT:
{
String *res= str_op_with_null_check(&str_value);
return res ? longlong_from_string_with_check(res) : 0;
}
case ROW_RESULT:
DBUG_ASSERT(0);
}
return 0;
}
my_decimal *Item_func_hybrid_field_type::val_decimal(my_decimal *decimal_value)
{
my_decimal *val= decimal_value;
DBUG_ASSERT(fixed == 1);
switch (Item_func_hybrid_field_type::cmp_type()) {
case DECIMAL_RESULT:
val= decimal_op_with_null_check(decimal_value);
break;
case INT_RESULT:
{
longlong result= int_op();
if (null_value)
return NULL;
int2my_decimal(E_DEC_FATAL_ERROR, result, unsigned_flag, decimal_value);
break;
}
case REAL_RESULT:
{
double result= (double)real_op();
if (null_value)
return NULL;
double2my_decimal(E_DEC_FATAL_ERROR, result, decimal_value);
break;
}
case TIME_RESULT:
{
MYSQL_TIME ltime;
if (date_op_with_null_check(<ime))
{
my_decimal_set_zero(decimal_value);
return 0;
}
ltime.time_type= mysql_type_to_time_type(field_type());
return date2my_decimal(<ime, decimal_value);
}
case STRING_RESULT:
{
String *res= str_op_with_null_check(&str_value);
return res ? decimal_from_string_with_check(decimal_value, res) : 0;
}
case ROW_RESULT:
DBUG_ASSERT(0);
}
return val;
}
bool Item_func_hybrid_field_type::get_date(MYSQL_TIME *ltime,
ulonglong fuzzydate)
{
DBUG_ASSERT(fixed == 1);
switch (Item_func_hybrid_field_type::cmp_type()) {
case DECIMAL_RESULT:
{
my_decimal value, *res;
if (!(res= decimal_op_with_null_check(&value)) ||
decimal_to_datetime_with_warn(res, ltime, fuzzydate,
field_name_or_null()))
goto err;
break;
}
case INT_RESULT:
{
longlong value= int_op();
bool neg= !unsigned_flag && value < 0;
if (null_value || int_to_datetime_with_warn(neg, neg ? -value : value,
ltime, fuzzydate,
field_name_or_null()))
goto err;
break;
}
case REAL_RESULT:
{
double value= real_op();
if (null_value || double_to_datetime_with_warn(value, ltime, fuzzydate,
field_name_or_null()))
goto err;
break;
}
case TIME_RESULT:
return date_op(ltime,
fuzzydate |
(field_type() == MYSQL_TYPE_TIME ? TIME_TIME_ONLY : 0));
case STRING_RESULT:
{
char buff[40];
String tmp(buff,sizeof(buff), &my_charset_bin),*res;
if (!(res= str_op_with_null_check(&tmp)) ||
str_to_datetime_with_warn(res->charset(), res->ptr(), res->length(),
ltime, fuzzydate))
goto err;
break;
}
case ROW_RESULT:
DBUG_ASSERT(0);
}
return (null_value= 0);
err:
bzero(ltime, sizeof(*ltime));
return null_value|= !(fuzzydate & TIME_FUZZY_DATES);
}
void Item_func_signed::print(String *str, enum_query_type query_type)
{
str->append(STRING_WITH_LEN("cast("));
args[0]->print(str, query_type);
str->append(STRING_WITH_LEN(" as signed)"));
}
longlong Item_func_signed::val_int_from_str(int *error)
{
char buff[MAX_FIELD_WIDTH];
String tmp(buff,sizeof(buff), &my_charset_bin), *res;
/*
For a string result, we must first get the string and then convert it
to a longlong
*/
if (!(res= args[0]->val_str(&tmp)))
{
null_value= 1;
*error= 0;
return 0;
}
null_value= 0;
Converter_strtoll10_with_warn cnv(NULL, Warn_filter_all(),
res->charset(), res->ptr(), res->length());
*error= cnv.error();
return cnv.result();
}
longlong Item_func_signed::val_int()
{
longlong value;
int error;
if (args[0]->cast_to_int_type() != STRING_RESULT)
{
value= args[0]->val_int();
null_value= args[0]->null_value;
return value;
}
else if (args[0]->dynamic_result())
{
/* We come here when argument has an unknown type */
args[0]->unsigned_flag= 0; // Mark that we want to have a signed value
value= args[0]->val_int();
null_value= args[0]->null_value;
if (!null_value && args[0]->unsigned_flag && value < 0)
goto err; // Warn about overflow
return value;
}
value= val_int_from_str(&error);
if (value < 0 && error == 0)
goto err;
return value;
err:
push_warning(current_thd, Sql_condition::WARN_LEVEL_NOTE, ER_UNKNOWN_ERROR,
"Cast to signed converted positive out-of-range integer to "
"it's negative complement");
return value;
}
void Item_func_unsigned::print(String *str, enum_query_type query_type)
{
str->append(STRING_WITH_LEN("cast("));
args[0]->print(str, query_type);
str->append(STRING_WITH_LEN(" as unsigned)"));
}
longlong Item_func_unsigned::val_int()
{
longlong value;
int error;
if (args[0]->cast_to_int_type() == DECIMAL_RESULT)
{
my_decimal tmp, *dec= args[0]->val_decimal(&tmp);
if (!(null_value= args[0]->null_value))
my_decimal2int(E_DEC_FATAL_ERROR, dec, 1, &value);
else
value= 0;
return value;
}
else if (args[0]->dynamic_result())
{
/* We come here when argument has an unknown type */
args[0]->unsigned_flag= 1; // Mark that we want to have an unsigned value
value= args[0]->val_int();
null_value= args[0]->null_value;
if (!null_value && args[0]->unsigned_flag == 0 && value < 0)
goto err; // Warn about overflow
return value;
}
else if (args[0]->cast_to_int_type() != STRING_RESULT)
{
value= args[0]->val_int();
null_value= args[0]->null_value;
if (!null_value && args[0]->unsigned_flag == 0 && value < 0)
goto err; // Warn about overflow
return value;
}
value= val_int_from_str(&error);
if (error < 0)
goto err;
return value;
err:
push_warning(current_thd, Sql_condition::WARN_LEVEL_NOTE, ER_UNKNOWN_ERROR,
"Cast to unsigned converted negative integer to it's "
"positive complement");
return value;
}
String *Item_decimal_typecast::val_str(String *str)
{
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
if (null_value)
return NULL;
my_decimal2string(E_DEC_FATAL_ERROR, tmp, 0, 0, 0, str);
return str;
}
double Item_decimal_typecast::val_real()
{
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
double res;
if (null_value)
return 0.0;
my_decimal2double(E_DEC_FATAL_ERROR, tmp, &res);
return res;
}
longlong Item_decimal_typecast::val_int()
{
my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf);
longlong res;
if (null_value)
return 0;
my_decimal2int(E_DEC_FATAL_ERROR, tmp, unsigned_flag, &res);
return res;
}
my_decimal *Item_decimal_typecast::val_decimal(my_decimal *dec)
{
my_decimal tmp_buf, *tmp= args[0]->val_decimal(&tmp_buf);
bool sign;
uint precision;
if ((null_value= args[0]->null_value))
return NULL;
my_decimal_round(E_DEC_FATAL_ERROR, tmp, decimals, FALSE, dec);
sign= dec->sign();
if (unsigned_flag)
{
if (sign)
{
my_decimal_set_zero(dec);
goto err;
}
}
precision= my_decimal_length_to_precision(max_length,
decimals, unsigned_flag);
if (precision - decimals < (uint) my_decimal_intg(dec))
{
max_my_decimal(dec, precision, decimals);
dec->sign(sign);
goto err;
}
return dec;
err:
THD *thd= current_thd;
push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
ER_WARN_DATA_OUT_OF_RANGE,
ER_THD(thd, ER_WARN_DATA_OUT_OF_RANGE),
name, 1L);
return dec;
}
void Item_decimal_typecast::print(String *str, enum_query_type query_type)
{
char len_buf[20*3 + 1];
char *end;
uint precision= my_decimal_length_to_precision(max_length, decimals,
unsigned_flag);
str->append(STRING_WITH_LEN("cast("));
args[0]->print(str, query_type);
str->append(STRING_WITH_LEN(" as decimal("));
end=int10_to_str(precision, len_buf,10);
str->append(len_buf, (uint32) (end - len_buf));
str->append(',');
end=int10_to_str(decimals, len_buf,10);
str->append(len_buf, (uint32) (end - len_buf));
str->append(')');
str->append(')');
}
double Item_double_typecast::val_real()
{
int error;
double tmp= args[0]->val_real();
if ((null_value= args[0]->null_value))
return 0.0;
if ((error= truncate_double(&tmp, max_length, decimals, 0, DBL_MAX)))
{
THD *thd= current_thd;
push_warning_printf(thd,
Sql_condition::WARN_LEVEL_WARN,
ER_WARN_DATA_OUT_OF_RANGE,
ER_THD(thd, ER_WARN_DATA_OUT_OF_RANGE),
name, 1);
if (error < 0)
{
null_value= 1; // Illegal value
tmp= 0.0;
}
}
return tmp;
}
void Item_double_typecast::print(String *str, enum_query_type query_type)
{
char len_buf[20*3 + 1];
char *end;
str->append(STRING_WITH_LEN("cast("));
args[0]->print(str, query_type);
str->append(STRING_WITH_LEN(" as double"));
if (decimals != NOT_FIXED_DEC)
{
str->append('(');
end= int10_to_str(max_length, len_buf,10);
str->append(len_buf, (uint32) (end - len_buf));
str->append(',');
end= int10_to_str(decimals, len_buf,10);
str->append(len_buf, (uint32) (end - len_buf));
str->append(')');
}
str->append(')');
}
double Item_func_plus::real_op()
{
double value= args[0]->val_real() + args[1]->val_real();
if ((null_value=args[0]->null_value || args[1]->null_value))
return 0.0;
return check_float_overflow(value);
}
longlong Item_func_plus::int_op()
{
longlong val0= args[0]->val_int();
longlong val1= args[1]->val_int();
longlong res= val0 + val1;
bool res_unsigned= FALSE;
if ((null_value= args[0]->null_value || args[1]->null_value))
return 0;
/*
First check whether the result can be represented as a
(bool unsigned_flag, longlong value) pair, then check if it is compatible
with this Item's unsigned_flag by calling check_integer_overflow().
*/
if (args[0]->unsigned_flag)
{
if (args[1]->unsigned_flag || val1 >= 0)
{
if (test_if_sum_overflows_ull((ulonglong) val0, (ulonglong) val1))
goto err;
res_unsigned= TRUE;
}
else
{
/* val1 is negative */
if ((ulonglong) val0 > (ulonglong) LONGLONG_MAX)
res_unsigned= TRUE;
}
}
else
{
if (args[1]->unsigned_flag)
{
if (val0 >= 0)
{
if (test_if_sum_overflows_ull((ulonglong) val0, (ulonglong) val1))
goto err;
res_unsigned= TRUE;
}
else
{
if ((ulonglong) val1 > (ulonglong) LONGLONG_MAX)
res_unsigned= TRUE;
}
}
else
{
if (val0 >=0 && val1 >= 0)
res_unsigned= TRUE;
else if (val0 < 0 && val1 < 0 && res >= 0)
goto err;
}
}
return check_integer_overflow(res, res_unsigned);
err:
return raise_integer_overflow();
}
/**
Calculate plus of two decimals.
@param decimal_value Buffer that can be used to store result
@retval
0 Value was NULL; In this case null_value is set
@retval
\# Value of operation as a decimal
*/
my_decimal *Item_func_plus::decimal_op(my_decimal *decimal_value)
{
my_decimal value1, *val1;
my_decimal value2, *val2;
val1= args[0]->val_decimal(&value1);
if ((null_value= args[0]->null_value))
return 0;
val2= args[1]->val_decimal(&value2);
if (!(null_value= (args[1]->null_value ||
check_decimal_overflow(my_decimal_add(E_DEC_FATAL_ERROR &
~E_DEC_OVERFLOW,
decimal_value,
val1, val2)) > 3)))
return decimal_value;
return 0;
}
/**
Set precision of results for additive operations (+ and -)
*/
void Item_func_additive_op::result_precision()
{
decimals= MY_MAX(args[0]->decimal_scale(), args[1]->decimal_scale());
int arg1_int= args[0]->decimal_precision() - args[0]->decimal_scale();
int arg2_int= args[1]->decimal_precision() - args[1]->decimal_scale();
int precision= MY_MAX(arg1_int, arg2_int) + 1 + decimals;
DBUG_ASSERT(arg1_int >= 0);
DBUG_ASSERT(arg2_int >= 0);
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
if (result_type() == INT_RESULT)
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
else
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
max_length= my_decimal_precision_to_length_no_truncation(precision, decimals,
unsigned_flag);
}
/**
The following function is here to allow the user to force
subtraction of UNSIGNED BIGINT to return negative values.
*/
void Item_func_minus::fix_length_and_dec()
{
Item_num_op::fix_length_and_dec();
if (unsigned_flag &&
(current_thd->variables.sql_mode & MODE_NO_UNSIGNED_SUBTRACTION))
unsigned_flag=0;
}
double Item_func_minus::real_op()
{
double value= args[0]->val_real() - args[1]->val_real();
if ((null_value=args[0]->null_value || args[1]->null_value))
return 0.0;
return check_float_overflow(value);
}
longlong Item_func_minus::int_op()
{
longlong val0= args[0]->val_int();
longlong val1= args[1]->val_int();
longlong res= val0 - val1;
bool res_unsigned= FALSE;
if ((null_value= args[0]->null_value || args[1]->null_value))
return 0;
/*
First check whether the result can be represented as a
(bool unsigned_flag, longlong value) pair, then check if it is compatible
with this Item's unsigned_flag by calling check_integer_overflow().
*/
if (args[0]->unsigned_flag)
{
if (args[1]->unsigned_flag)
{
if ((ulonglong) val0 < (ulonglong) val1)
{
if (res >= 0)
goto err;
}
else
res_unsigned= TRUE;
}
else
{
if (val1 >= 0)
{
if ((ulonglong) val0 > (ulonglong) val1)
res_unsigned= TRUE;
}
else
{
if (test_if_sum_overflows_ull((ulonglong) val0, (ulonglong) -val1))
goto err;
res_unsigned= TRUE;
}
}
}
else
{
if (args[1]->unsigned_flag)
{
if ((ulonglong) (val0 - LONGLONG_MIN) < (ulonglong) val1)
goto err;
}
else
{
if (val0 > 0 && val1 < 0)
res_unsigned= TRUE;
else if (val0 < 0 && val1 > 0 && res >= 0)
goto err;
}
}
return check_integer_overflow(res, res_unsigned);
err:
return raise_integer_overflow();
}
/**
See Item_func_plus::decimal_op for comments.
*/
my_decimal *Item_func_minus::decimal_op(my_decimal *decimal_value)
{
my_decimal value1, *val1;
my_decimal value2, *val2=
val1= args[0]->val_decimal(&value1);
if ((null_value= args[0]->null_value))
return 0;
val2= args[1]->val_decimal(&value2);
if (!(null_value= (args[1]->null_value ||
(check_decimal_overflow(my_decimal_sub(E_DEC_FATAL_ERROR &
~E_DEC_OVERFLOW,
decimal_value, val1,
val2)) > 3))))
return decimal_value;
return 0;
}
double Item_func_mul::real_op()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real() * args[1]->val_real();
if ((null_value=args[0]->null_value || args[1]->null_value))
return 0.0;
return check_float_overflow(value);
}
longlong Item_func_mul::int_op()
{
DBUG_ASSERT(fixed == 1);
longlong a= args[0]->val_int();
longlong b= args[1]->val_int();
longlong res;
ulonglong res0, res1;
ulong a0, a1, b0, b1;
bool res_unsigned= FALSE;
bool a_negative= FALSE, b_negative= FALSE;
if ((null_value= args[0]->null_value || args[1]->null_value))
return 0;
/*
First check whether the result can be represented as a
(bool unsigned_flag, longlong value) pair, then check if it is compatible
with this Item's unsigned_flag by calling check_integer_overflow().
Let a = a1 * 2^32 + a0 and b = b1 * 2^32 + b0. Then
a * b = (a1 * 2^32 + a0) * (b1 * 2^32 + b0) = a1 * b1 * 2^64 +
+ (a1 * b0 + a0 * b1) * 2^32 + a0 * b0;
We can determine if the above sum overflows the ulonglong range by
sequentially checking the following conditions:
1. If both a1 and b1 are non-zero.
2. Otherwise, if (a1 * b0 + a0 * b1) is greater than ULONG_MAX.
3. Otherwise, if (a1 * b0 + a0 * b1) * 2^32 + a0 * b0 is greater than
ULONGLONG_MAX.
Since we also have to take the unsigned_flag for a and b into account,
it is easier to first work with absolute values and set the
correct sign later.
*/
if (!args[0]->unsigned_flag && a < 0)
{
a_negative= TRUE;
a= -a;
}
if (!args[1]->unsigned_flag && b < 0)
{
b_negative= TRUE;
b= -b;
}
a0= 0xFFFFFFFFUL & a;
a1= ((ulonglong) a) >> 32;
b0= 0xFFFFFFFFUL & b;
b1= ((ulonglong) b) >> 32;
if (a1 && b1)
goto err;
res1= (ulonglong) a1 * b0 + (ulonglong) a0 * b1;
if (res1 > 0xFFFFFFFFUL)
goto err;
res1= res1 << 32;
res0= (ulonglong) a0 * b0;
if (test_if_sum_overflows_ull(res1, res0))
goto err;
res= res1 + res0;
if (a_negative != b_negative)
{
if ((ulonglong) res > (ulonglong) LONGLONG_MIN + 1)
goto err;
res= -res;
}
else
res_unsigned= TRUE;
return check_integer_overflow(res, res_unsigned);
err:
return raise_integer_overflow();
}
/** See Item_func_plus::decimal_op for comments. */
my_decimal *Item_func_mul::decimal_op(my_decimal *decimal_value)
{
my_decimal value1, *val1;
my_decimal value2, *val2;
val1= args[0]->val_decimal(&value1);
if ((null_value= args[0]->null_value))
return 0;
val2= args[1]->val_decimal(&value2);
if (!(null_value= (args[1]->null_value ||
(check_decimal_overflow(my_decimal_mul(E_DEC_FATAL_ERROR &
~E_DEC_OVERFLOW,
decimal_value, val1,
val2)) > 3))))
return decimal_value;
return 0;
}
void Item_func_mul::result_precision()
{
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
if (result_type() == INT_RESULT)
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
else
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
decimals= MY_MIN(args[0]->decimal_scale() + args[1]->decimal_scale(),
DECIMAL_MAX_SCALE);
uint est_prec = args[0]->decimal_precision() + args[1]->decimal_precision();
uint precision= MY_MIN(est_prec, DECIMAL_MAX_PRECISION);
max_length= my_decimal_precision_to_length_no_truncation(precision, decimals,
unsigned_flag);
}
double Item_func_div::real_op()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
double val2= args[1]->val_real();
if ((null_value= args[0]->null_value || args[1]->null_value))
return 0.0;
if (val2 == 0.0)
{
signal_divide_by_null();
return 0.0;
}
return check_float_overflow(value/val2);
}
my_decimal *Item_func_div::decimal_op(my_decimal *decimal_value)
{
my_decimal value1, *val1;
my_decimal value2, *val2;
int err;
val1= args[0]->val_decimal(&value1);
if ((null_value= args[0]->null_value))
return 0;
val2= args[1]->val_decimal(&value2);
if ((null_value= args[1]->null_value))
return 0;
if ((err= check_decimal_overflow(my_decimal_div(E_DEC_FATAL_ERROR &
~E_DEC_OVERFLOW &
~E_DEC_DIV_ZERO,
decimal_value,
val1, val2,
prec_increment))) > 3)
{
if (err == E_DEC_DIV_ZERO)
signal_divide_by_null();
null_value= 1;
return 0;
}
return decimal_value;
}
void Item_func_div::result_precision()
{
/*
We need to add args[1]->divisor_precision_increment(),
to properly handle the cases like this:
SELECT 5.05 / 0.014; -> 360.714286
i.e. when the divisor has a zero integer part
and non-zero digits appear only after the decimal point.
Precision in this example is calculated as
args[0]->decimal_precision() + // 3
args[1]->divisor_precision_increment() + // 3
prec_increment // 4
which gives 10 decimals digits.
*/
uint precision=MY_MIN(args[0]->decimal_precision() +
args[1]->divisor_precision_increment() + prec_increment,
DECIMAL_MAX_PRECISION);
/* Integer operations keep unsigned_flag if one of arguments is unsigned */
if (result_type() == INT_RESULT)
unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag;
else
unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag;
decimals= MY_MIN(args[0]->decimal_scale() + prec_increment, DECIMAL_MAX_SCALE);
max_length= my_decimal_precision_to_length_no_truncation(precision, decimals,
unsigned_flag);
}
void Item_func_div::fix_length_and_dec()
{
DBUG_ENTER("Item_func_div::fix_length_and_dec");
prec_increment= current_thd->variables.div_precincrement;
Item_num_op::fix_length_and_dec();
switch (Item_func_div::result_type()) {
case REAL_RESULT:
{
decimals=MY_MAX(args[0]->decimals,args[1]->decimals)+prec_increment;
set_if_smaller(decimals, NOT_FIXED_DEC);
uint tmp=float_length(decimals);
if (decimals == NOT_FIXED_DEC)
max_length= tmp;
else
{
max_length=args[0]->max_length - args[0]->decimals + decimals;
set_if_smaller(max_length,tmp);
}
break;
}
case INT_RESULT:
set_handler_by_result_type(DECIMAL_RESULT);
DBUG_PRINT("info", ("Type changed: DECIMAL_RESULT"));
result_precision();
break;
case DECIMAL_RESULT:
result_precision();
fix_decimals();
break;
case STRING_RESULT:
case ROW_RESULT:
case TIME_RESULT:
DBUG_ASSERT(0);
}
maybe_null= 1; // devision by zero
DBUG_VOID_RETURN;
}
/* Integer division */
longlong Item_func_int_div::val_int()
{
DBUG_ASSERT(fixed == 1);
/*
Perform division using DECIMAL math if either of the operands has a
non-integer type
*/
if (args[0]->result_type() != INT_RESULT ||
args[1]->result_type() != INT_RESULT)
{
my_decimal tmp;
my_decimal *val0p= args[0]->val_decimal(&tmp);
if ((null_value= args[0]->null_value))
return 0;
my_decimal val0= *val0p;
my_decimal *val1p= args[1]->val_decimal(&tmp);
if ((null_value= args[1]->null_value))
return 0;
my_decimal val1= *val1p;
int err;
if ((err= my_decimal_div(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, &tmp,
&val0, &val1, 0)) > 3)
{
if (err == E_DEC_DIV_ZERO)
signal_divide_by_null();
return 0;
}
my_decimal truncated;
const bool do_truncate= true;
if (my_decimal_round(E_DEC_FATAL_ERROR, &tmp, 0, do_truncate, &truncated))
DBUG_ASSERT(false);
longlong res;
if (my_decimal2int(E_DEC_FATAL_ERROR, &truncated, unsigned_flag, &res) &
E_DEC_OVERFLOW)
raise_integer_overflow();
return res;
}
longlong val0=args[0]->val_int();
longlong val1=args[1]->val_int();
bool val0_negative, val1_negative, res_negative;
ulonglong uval0, uval1, res;
if ((null_value= (args[0]->null_value || args[1]->null_value)))
return 0;
if (val1 == 0)
{
signal_divide_by_null();
return 0;
}
val0_negative= !args[0]->unsigned_flag && val0 < 0;
val1_negative= !args[1]->unsigned_flag && val1 < 0;
res_negative= val0_negative != val1_negative;
uval0= (ulonglong) (val0_negative ? -val0 : val0);
uval1= (ulonglong) (val1_negative ? -val1 : val1);
res= uval0 / uval1;
if (res_negative)
{
if (res > (ulonglong) LONGLONG_MAX)
return raise_integer_overflow();
res= (ulonglong) (-(longlong) res);
}
return check_integer_overflow(res, !res_negative);
}
void Item_func_int_div::fix_length_and_dec()
{
Item_result argtype= args[0]->result_type();
/* use precision ony for the data type it is applicable for and valid */
uint32 char_length= args[0]->max_char_length() -
(argtype == DECIMAL_RESULT || argtype == INT_RESULT ?
args[0]->decimals : 0);
fix_char_length(char_length > MY_INT64_NUM_DECIMAL_DIGITS ?
MY_INT64_NUM_DECIMAL_DIGITS : char_length);
maybe_null=1;
unsigned_flag=args[0]->unsigned_flag | args[1]->unsigned_flag;
}
longlong Item_func_mod::int_op()
{
DBUG_ASSERT(fixed == 1);
longlong val0= args[0]->val_int();
longlong val1= args[1]->val_int();
bool val0_negative, val1_negative;
ulonglong uval0, uval1;
ulonglong res;
if ((null_value= args[0]->null_value || args[1]->null_value))
return 0; /* purecov: inspected */
if (val1 == 0)
{
signal_divide_by_null();
return 0;
}
/*
'%' is calculated by integer division internally. Since dividing
LONGLONG_MIN by -1 generates SIGFPE, we calculate using unsigned values and
then adjust the sign appropriately.
*/
val0_negative= !args[0]->unsigned_flag && val0 < 0;
val1_negative= !args[1]->unsigned_flag && val1 < 0;
uval0= (ulonglong) (val0_negative ? -val0 : val0);
uval1= (ulonglong) (val1_negative ? -val1 : val1);
res= uval0 % uval1;
return check_integer_overflow(val0_negative ? -(longlong) res : res,
!val0_negative);
}
double Item_func_mod::real_op()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
double val2= args[1]->val_real();
if ((null_value= args[0]->null_value || args[1]->null_value))
return 0.0; /* purecov: inspected */
if (val2 == 0.0)
{
signal_divide_by_null();
return 0.0;
}
return fmod(value,val2);
}
my_decimal *Item_func_mod::decimal_op(my_decimal *decimal_value)
{
my_decimal value1, *val1;
my_decimal value2, *val2;
val1= args[0]->val_decimal(&value1);
if ((null_value= args[0]->null_value))
return 0;
val2= args[1]->val_decimal(&value2);
if ((null_value= args[1]->null_value))
return 0;
switch (my_decimal_mod(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value,
val1, val2)) {
case E_DEC_TRUNCATED:
case E_DEC_OK:
return decimal_value;
case E_DEC_DIV_ZERO:
signal_divide_by_null();
default:
null_value= 1;
return 0;
}
}
void Item_func_mod::result_precision()
{
decimals= MY_MAX(args[0]->decimal_scale(), args[1]->decimal_scale());
max_length= MY_MAX(args[0]->max_length, args[1]->max_length);
}
void Item_func_mod::fix_length_and_dec()
{
Item_num_op::fix_length_and_dec();
maybe_null= 1;
unsigned_flag= args[0]->unsigned_flag;
}
double Item_func_neg::real_op()
{
double value= args[0]->val_real();
null_value= args[0]->null_value;
return -value;
}
longlong Item_func_neg::int_op()
{
longlong value= args[0]->val_int();
if ((null_value= args[0]->null_value))
return 0;
if (args[0]->unsigned_flag &&
(ulonglong) value > (ulonglong) LONGLONG_MAX + 1)
return raise_integer_overflow();
if (value == LONGLONG_MIN)
{
if (args[0]->unsigned_flag != unsigned_flag)
/* negation of LONGLONG_MIN is LONGLONG_MIN. */
return LONGLONG_MIN;
else
return raise_integer_overflow();
}
return check_integer_overflow(-value, !args[0]->unsigned_flag && value < 0);
}
my_decimal *Item_func_neg::decimal_op(my_decimal *decimal_value)
{
my_decimal val, *value= args[0]->val_decimal(&val);
if (!(null_value= args[0]->null_value))
{
my_decimal2decimal(value, decimal_value);
my_decimal_neg(decimal_value);
return decimal_value;
}
return 0;
}
void Item_func_neg::fix_length_and_dec()
{
DBUG_ENTER("Item_func_neg::fix_length_and_dec");
Item_func_num1::fix_length_and_dec();
/* 1 add because sign can appear */
max_length= args[0]->max_length + 1;
/*
If this is in integer context keep the context as integer if possible
(This is how multiplication and other integer functions works)
Use val() to get value as arg_type doesn't mean that item is
Item_int or Item_float due to existence of Item_param.
*/
if (Item_func_neg::result_type() == INT_RESULT && args[0]->const_item())
{
longlong val= args[0]->val_int();
if ((ulonglong) val >= (ulonglong) LONGLONG_MIN &&
((ulonglong) val != (ulonglong) LONGLONG_MIN ||
args[0]->type() != INT_ITEM))
{
/*
Ensure that result is converted to DECIMAL, as longlong can't hold
the negated number
*/
set_handler_by_result_type(DECIMAL_RESULT);
DBUG_PRINT("info", ("Type changed: DECIMAL_RESULT"));
}
}
unsigned_flag= 0;
DBUG_VOID_RETURN;
}
double Item_func_abs::real_op()
{
double value= args[0]->val_real();
null_value= args[0]->null_value;
return fabs(value);
}
longlong Item_func_abs::int_op()
{
longlong value= args[0]->val_int();
if ((null_value= args[0]->null_value))
return 0;
if (unsigned_flag)
return value;
/* -LONGLONG_MIN = LONGLONG_MAX + 1 => outside of signed longlong range */
if (value == LONGLONG_MIN)
return raise_integer_overflow();
return (value >= 0) ? value : -value;
}
my_decimal *Item_func_abs::decimal_op(my_decimal *decimal_value)
{
my_decimal val, *value= args[0]->val_decimal(&val);
if (!(null_value= args[0]->null_value))
{
my_decimal2decimal(value, decimal_value);
if (decimal_value->sign())
my_decimal_neg(decimal_value);
return decimal_value;
}
return 0;
}
void Item_func_abs::fix_length_and_dec()
{
Item_func_num1::fix_length_and_dec();
unsigned_flag= args[0]->unsigned_flag;
}
/** Gateway to natural LOG function. */
double Item_func_ln::val_real()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
if ((null_value= args[0]->null_value))
return 0.0;
if (value <= 0.0)
{
signal_divide_by_null();
return 0.0;
}
return log(value);
}
/**
Extended but so slower LOG function.
We have to check if all values are > zero and first one is not one
as these are the cases then result is not a number.
*/
double Item_func_log::val_real()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
if ((null_value= args[0]->null_value))
return 0.0;
if (value <= 0.0)
{
signal_divide_by_null();
return 0.0;
}
if (arg_count == 2)
{
double value2= args[1]->val_real();
if ((null_value= args[1]->null_value))
return 0.0;
if (value2 <= 0.0 || value == 1.0)
{
signal_divide_by_null();
return 0.0;
}
return log(value2) / log(value);
}
return log(value);
}
double Item_func_log2::val_real()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
if ((null_value=args[0]->null_value))
return 0.0;
if (value <= 0.0)
{
signal_divide_by_null();
return 0.0;
}
return log(value) / M_LN2;
}
double Item_func_log10::val_real()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
if ((null_value= args[0]->null_value))
return 0.0;
if (value <= 0.0)
{
signal_divide_by_null();
return 0.0;
}
return log10(value);
}
double Item_func_exp::val_real()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
if ((null_value=args[0]->null_value))
return 0.0; /* purecov: inspected */
return check_float_overflow(exp(value));
}
double Item_func_sqrt::val_real()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
if ((null_value=(args[0]->null_value || value < 0)))
return 0.0; /* purecov: inspected */
return sqrt(value);
}
double Item_func_pow::val_real()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
double val2= args[1]->val_real();
if ((null_value=(args[0]->null_value || args[1]->null_value)))
return 0.0; /* purecov: inspected */
return check_float_overflow(pow(value,val2));
}
// Trigonometric functions
double Item_func_acos::val_real()
{
DBUG_ASSERT(fixed == 1);
/* One can use this to defer SELECT processing. */
DEBUG_SYNC(current_thd, "before_acos_function");
// the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
volatile double value= args[0]->val_real();
if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
return 0.0;
return acos(value);
}
double Item_func_asin::val_real()
{
DBUG_ASSERT(fixed == 1);
// the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug)
volatile double value= args[0]->val_real();
if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0))))
return 0.0;
return asin(value);
}
double Item_func_atan::val_real()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
if ((null_value=args[0]->null_value))
return 0.0;
if (arg_count == 2)
{
double val2= args[1]->val_real();
if ((null_value=args[1]->null_value))
return 0.0;
return check_float_overflow(atan2(value,val2));
}
return atan(value);
}
double Item_func_cos::val_real()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
if ((null_value=args[0]->null_value))
return 0.0;
return cos(value);
}
double Item_func_sin::val_real()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
if ((null_value=args[0]->null_value))
return 0.0;
return sin(value);
}
double Item_func_tan::val_real()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
if ((null_value=args[0]->null_value))
return 0.0;
return check_float_overflow(tan(value));
}
double Item_func_cot::val_real()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
if ((null_value=args[0]->null_value))
return 0.0;
return check_float_overflow(1.0 / tan(value));
}
// Shift-functions, same as << and >> in C/C++
longlong Item_func_shift_left::val_int()
{
DBUG_ASSERT(fixed == 1);
uint shift;
ulonglong res= ((ulonglong) args[0]->val_int() <<
(shift=(uint) args[1]->val_int()));
if (args[0]->null_value || args[1]->null_value)
{
null_value=1;
return 0;
}
null_value=0;
return (shift < sizeof(longlong)*8 ? (longlong) res : 0);
}
longlong Item_func_shift_right::val_int()
{
DBUG_ASSERT(fixed == 1);
uint shift;
ulonglong res= (ulonglong) args[0]->val_int() >>
(shift=(uint) args[1]->val_int());
if (args[0]->null_value || args[1]->null_value)
{
null_value=1;
return 0;
}
null_value=0;
return (shift < sizeof(longlong)*8 ? (longlong) res : 0);
}
longlong Item_func_bit_neg::val_int()
{
DBUG_ASSERT(fixed == 1);
ulonglong res= (ulonglong) args[0]->val_int();
if ((null_value=args[0]->null_value))
return 0;
return ~res;
}
// Conversion functions
void Item_func_integer::fix_length_and_dec()
{
max_length=args[0]->max_length - args[0]->decimals+1;
uint tmp=float_length(decimals);
set_if_smaller(max_length,tmp);
decimals=0;
}
void Item_func_int_val::fix_length_and_dec()
{
DBUG_ENTER("Item_func_int_val::fix_length_and_dec");
DBUG_PRINT("info", ("name %s", func_name()));
ulonglong tmp_max_length= (ulonglong ) args[0]->max_length -
(args[0]->decimals ? args[0]->decimals + 1 : 0) + 2;
max_length= tmp_max_length > (ulonglong) 4294967295U ?
(uint32) 4294967295U : (uint32) tmp_max_length;
uint tmp= float_length(decimals);
set_if_smaller(max_length,tmp);
decimals= 0;
// Note, cast_to_int_type() can return TIME_RESULT
switch (args[0]->cast_to_int_type())
{
case STRING_RESULT:
case REAL_RESULT:
set_handler_by_result_type(REAL_RESULT);
max_length= float_length(decimals);
break;
case INT_RESULT:
case TIME_RESULT:
case DECIMAL_RESULT:
/*
-2 because in most high position can't be used any digit for longlong
and one position for increasing value during operation
*/
if ((args[0]->max_length - args[0]->decimals) >=
(DECIMAL_LONGLONG_DIGITS - 2))
{
set_handler_by_result_type(DECIMAL_RESULT);
}
else
{
unsigned_flag= args[0]->unsigned_flag;
set_handler_by_result_type(INT_RESULT);
}
break;
case ROW_RESULT:
DBUG_ASSERT(0);
}
DBUG_PRINT("info", ("Type: %s",
(result_type() == REAL_RESULT ? "REAL_RESULT" :
result_type() == DECIMAL_RESULT ? "DECIMAL_RESULT" :
result_type() == INT_RESULT ? "INT_RESULT" :
"--ILLEGAL!!!--")));
DBUG_VOID_RETURN;
}
longlong Item_func_ceiling::int_op()
{
longlong result;
switch (args[0]->result_type()) {
case INT_RESULT:
result= args[0]->val_int();
null_value= args[0]->null_value;
break;
case DECIMAL_RESULT:
{
my_decimal dec_buf, *dec;
if ((dec= Item_func_ceiling::decimal_op(&dec_buf)))
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
else
result= 0;
break;
}
default:
result= (longlong)Item_func_ceiling::real_op();
};
return result;
}
double Item_func_ceiling::real_op()
{
/*
the volatile's for BUG #3051 to calm optimizer down (because of gcc's
bug)
*/
volatile double value= args[0]->val_real();
null_value= args[0]->null_value;
return ceil(value);
}
my_decimal *Item_func_ceiling::decimal_op(my_decimal *decimal_value)
{
my_decimal val, *value= args[0]->val_decimal(&val);
if (!(null_value= (args[0]->null_value ||
my_decimal_ceiling(E_DEC_FATAL_ERROR, value,
decimal_value) > 1)))
return decimal_value;
return 0;
}
longlong Item_func_floor::int_op()
{
longlong result;
switch (args[0]->result_type()) {
case INT_RESULT:
result= args[0]->val_int();
null_value= args[0]->null_value;
break;
case DECIMAL_RESULT:
{
my_decimal dec_buf, *dec;
if ((dec= Item_func_floor::decimal_op(&dec_buf)))
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
else
result= 0;
break;
}
default:
result= (longlong)Item_func_floor::real_op();
};
return result;
}
double Item_func_floor::real_op()
{
/*
the volatile's for BUG #3051 to calm optimizer down (because of gcc's
bug)
*/
volatile double value= args[0]->val_real();
null_value= args[0]->null_value;
return floor(value);
}
my_decimal *Item_func_floor::decimal_op(my_decimal *decimal_value)
{
my_decimal val, *value= args[0]->val_decimal(&val);
if (!(null_value= (args[0]->null_value ||
my_decimal_floor(E_DEC_FATAL_ERROR, value,
decimal_value) > 1)))
return decimal_value;
return 0;
}
void Item_func_round::fix_length_and_dec()
{
int decimals_to_set;
longlong val1;
bool val1_unsigned;
unsigned_flag= args[0]->unsigned_flag;
if (!args[1]->const_item())
{
decimals= args[0]->decimals;
max_length= float_length(decimals);
if (args[0]->result_type() == DECIMAL_RESULT)
{
max_length++;
set_handler_by_result_type(DECIMAL_RESULT);
}
else
set_handler_by_result_type(REAL_RESULT);
return;
}
val1= args[1]->val_int();
if ((null_value= args[1]->null_value))
return;
val1_unsigned= args[1]->unsigned_flag;
if (val1 < 0)
decimals_to_set= val1_unsigned ? INT_MAX : 0;
else
decimals_to_set= (val1 > INT_MAX) ? INT_MAX : (int) val1;
if (args[0]->decimals == NOT_FIXED_DEC)
{
decimals= MY_MIN(decimals_to_set, NOT_FIXED_DEC);
max_length= float_length(decimals);
set_handler_by_result_type(REAL_RESULT);
return;
}
switch (args[0]->result_type()) {
case REAL_RESULT:
case STRING_RESULT:
set_handler_by_result_type(REAL_RESULT);
decimals= MY_MIN(decimals_to_set, NOT_FIXED_DEC);
max_length= float_length(decimals);
break;
case INT_RESULT:
if ((!decimals_to_set && truncate) || (args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS))
{
int length_can_increase= MY_TEST(!truncate && (val1 < 0) &&
!val1_unsigned);
max_length= args[0]->max_length + length_can_increase;
/* Here we can keep INT_RESULT */
set_handler_by_result_type(INT_RESULT);
decimals= 0;
break;
}
/* fall through */
case DECIMAL_RESULT:
{
set_handler_by_result_type(DECIMAL_RESULT);
decimals_to_set= MY_MIN(DECIMAL_MAX_SCALE, decimals_to_set);
int decimals_delta= args[0]->decimals - decimals_to_set;
int precision= args[0]->decimal_precision();
int length_increase= ((decimals_delta <= 0) || truncate) ? 0:1;
precision-= decimals_delta - length_increase;
decimals= MY_MIN(decimals_to_set, DECIMAL_MAX_SCALE);
max_length= my_decimal_precision_to_length_no_truncation(precision,
decimals,
unsigned_flag);
break;
}
case ROW_RESULT:
case TIME_RESULT:
DBUG_ASSERT(0); /* This result type isn't handled */
}
}
double my_double_round(double value, longlong dec, bool dec_unsigned,
bool truncate)
{
double tmp;
bool dec_negative= (dec < 0) && !dec_unsigned;
ulonglong abs_dec= dec_negative ? -dec : dec;
/*
tmp2 is here to avoid return the value with 80 bit precision
This will fix that the test round(0.1,1) = round(0.1,1) is true
Tagging with volatile is no guarantee, it may still be optimized away...
*/
volatile double tmp2;
tmp=(abs_dec < array_elements(log_10) ?
log_10[abs_dec] : pow(10.0,(double) abs_dec));
// Pre-compute these, to avoid optimizing away e.g. 'floor(v/tmp) * tmp'.
volatile double value_div_tmp= value / tmp;
volatile double value_mul_tmp= value * tmp;
if (!dec_negative && my_isinf(tmp)) // "dec" is too large positive number
return value;
if (dec_negative && my_isinf(tmp))
tmp2= 0.0;
else if (!dec_negative && my_isinf(value_mul_tmp))
tmp2= value;
else if (truncate)
{
if (value >= 0.0)
tmp2= dec < 0 ? floor(value_div_tmp) * tmp : floor(value_mul_tmp) / tmp;
else
tmp2= dec < 0 ? ceil(value_div_tmp) * tmp : ceil(value_mul_tmp) / tmp;
}
else
tmp2=dec < 0 ? rint(value_div_tmp) * tmp : rint(value_mul_tmp) / tmp;
return tmp2;
}
double Item_func_round::real_op()
{
double value= args[0]->val_real();
if (!(null_value= args[0]->null_value))
{
longlong dec= args[1]->val_int();
if (!(null_value= args[1]->null_value))
return my_double_round(value, dec, args[1]->unsigned_flag, truncate);
}
return 0.0;
}
/*
Rounds a given value to a power of 10 specified as the 'to' argument,
avoiding overflows when the value is close to the ulonglong range boundary.
*/
static inline ulonglong my_unsigned_round(ulonglong value, ulonglong to)
{
ulonglong tmp= value / to * to;
return (value - tmp < (to >> 1)) ? tmp : tmp + to;
}
longlong Item_func_round::int_op()
{
longlong value= args[0]->val_int();
longlong dec= args[1]->val_int();
decimals= 0;
ulonglong abs_dec;
if ((null_value= args[0]->null_value || args[1]->null_value))
return 0;
if ((dec >= 0) || args[1]->unsigned_flag)
return value; // integer have not digits after point
abs_dec= -dec;
longlong tmp;
if(abs_dec >= array_elements(log_10_int))
return 0;
tmp= log_10_int[abs_dec];
if (truncate)
value= (unsigned_flag) ?
((ulonglong) value / tmp) * tmp : (value / tmp) * tmp;
else
value= (unsigned_flag || value >= 0) ?
my_unsigned_round((ulonglong) value, tmp) :
-(longlong) my_unsigned_round((ulonglong) -value, tmp);
return value;
}
my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value)
{
my_decimal val, *value= args[0]->val_decimal(&val);
longlong dec= args[1]->val_int();
if (dec >= 0 || args[1]->unsigned_flag)
dec= MY_MIN((ulonglong) dec, decimals);
else if (dec < INT_MIN)
dec= INT_MIN;
if (!(null_value= (args[0]->null_value || args[1]->null_value ||
my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec,
truncate, decimal_value) > 1)))
return decimal_value;
return 0;
}
void Item_func_rand::seed_random(Item *arg)
{
/*
TODO: do not do reinit 'rand' for every execute of PS/SP if
args[0] is a constant.
*/
uint32 tmp;
#ifdef WITH_WSREP
THD *thd= current_thd;
if (WSREP(thd))
{
if (thd->wsrep_exec_mode==REPL_RECV)
tmp= thd->wsrep_rand;
else
tmp= thd->wsrep_rand= (uint32) arg->val_int();
}
else
#endif /* WITH_WSREP */
tmp= (uint32) arg->val_int();
my_rnd_init(rand, (uint32) (tmp*0x10001L+55555555L),
(uint32) (tmp*0x10000001L));
}
bool Item_func_rand::fix_fields(THD *thd,Item **ref)
{
if (Item_real_func::fix_fields(thd, ref))
return TRUE;
used_tables_cache|= RAND_TABLE_BIT;
if (arg_count)
{ // Only use argument once in query
/*
Allocate rand structure once: we must use thd->stmt_arena
to create rand in proper mem_root if it's a prepared statement or
stored procedure.
No need to send a Rand log event if seed was given eg: RAND(seed),
as it will be replicated in the query as such.
*/
if (!rand && !(rand= (struct my_rnd_struct*)
thd->stmt_arena->alloc(sizeof(*rand))))
return TRUE;
}
else
{
/*
Save the seed only the first time RAND() is used in the query
Once events are forwarded rather than recreated,
the following can be skipped if inside the slave thread
*/
if (!thd->rand_used)
{
thd->rand_used= 1;
thd->rand_saved_seed1= thd->rand.seed1;
thd->rand_saved_seed2= thd->rand.seed2;
}
rand= &thd->rand;
}
return FALSE;
}
void Item_func_rand::update_used_tables()
{
Item_real_func::update_used_tables();
used_tables_cache|= RAND_TABLE_BIT;
}
double Item_func_rand::val_real()
{
DBUG_ASSERT(fixed == 1);
if (arg_count)
{
if (!args[0]->const_item())
seed_random(args[0]);
else if (first_eval)
{
/*
Constantness of args[0] may be set during JOIN::optimize(), if arg[0]
is a field item of "constant" table. Thus, we have to evaluate
seed_random() for constant arg there but not at the fix_fields method.
*/
first_eval= FALSE;
seed_random(args[0]);
}
}
return my_rnd(rand);
}
longlong Item_func_sign::val_int()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
null_value=args[0]->null_value;
return value < 0.0 ? -1 : (value > 0 ? 1 : 0);
}
double Item_func_units::val_real()
{
DBUG_ASSERT(fixed == 1);
double value= args[0]->val_real();
if ((null_value=args[0]->null_value))
return 0;
return check_float_overflow(value * mul + add);
}
void Item_func_min_max::fix_length_and_dec()
{
uint unsigned_count= 0;
int max_int_part=0;
decimals=0;
max_length=0;
maybe_null=0;
thd= current_thd;
Item_result tmp_cmp_type= args[0]->cmp_type();
uint string_type_count= 0;
uint temporal_type_count= 0;
enum_field_types temporal_field_type= MYSQL_TYPE_DATETIME;
for (uint i=0 ; i < arg_count ; i++)
{
set_if_bigger(max_length, args[i]->max_length);
set_if_bigger(decimals, args[i]->decimals);
set_if_bigger(max_int_part, args[i]->decimal_int_part());
unsigned_count+= args[i]->unsigned_flag;
if (args[i]->maybe_null)
maybe_null= 1;
tmp_cmp_type= item_cmp_type(tmp_cmp_type, args[i]->cmp_type());
string_type_count+= args[i]->cmp_type() == STRING_RESULT;
if (args[i]->cmp_type() == TIME_RESULT)
{
if (!temporal_type_count)
temporal_field_type= args[i]->field_type();
else
temporal_field_type= Field::field_type_merge(temporal_field_type,
args[i]->field_type());
temporal_type_count++;
}
}
unsigned_flag= unsigned_count == arg_count; // if all args are unsigned
switch (tmp_cmp_type) {
case TIME_RESULT:
// At least one temporal argument was found.
collation.set_numeric();
set_handler_by_field_type(temporal_field_type);
if (is_temporal_type_with_time(temporal_field_type))
set_if_smaller(decimals, TIME_SECOND_PART_DIGITS);
else
decimals= 0;
break;
case STRING_RESULT:
/*
All arguments are of string-alike types:
CHAR, VARCHAR, TEXT, BINARY, VARBINARY, BLOB, SET, ENUM
No numeric and no temporal types were found.
*/
agg_arg_charsets_for_string_result_with_comparison(collation,
args, arg_count);
set_handler_by_field_type(agg_field_type(args, arg_count, false));
break;
case INT_RESULT:
/*
All arguments have INT-alike types:
TINY, SHORT, LONG, LONGLONG, INT24, YEAR, BIT.
*/
collation.set_numeric();
fix_char_length(my_decimal_precision_to_length_no_truncation(max_int_part +
decimals,
decimals,
unsigned_flag));
if (unsigned_count != 0 && unsigned_count != arg_count)
{
/*
If all args are of INT-alike type, but have different unsigned_flag,
then change type to DECIMAL.
*/
set_handler_by_field_type(MYSQL_TYPE_NEWDECIMAL);
}
else
{
/*
There are only INT-alike arguments with equal unsigned_flag.
Aggregate types to get the best covering type.
Treat BIT as LONGLONG when aggregating to non-BIT types.
Possible final type: TINY, SHORT, LONG, LONGLONG, INT24, YEAR, BIT.
*/
set_handler_by_field_type(agg_field_type(args, arg_count, true));
}
break;
case DECIMAL_RESULT:
// All arguments are of DECIMAL type
collation.set_numeric();
fix_char_length(my_decimal_precision_to_length_no_truncation(max_int_part +
decimals,
decimals,
unsigned_flag));
set_handler_by_field_type(MYSQL_TYPE_NEWDECIMAL);
break;
case ROW_RESULT:
DBUG_ASSERT(0);
// Pass through
case REAL_RESULT:
collation.set_numeric();
fix_char_length(float_length(decimals));
/*
Set type to DOUBLE, as Item_func::tmp_table_field() does not
distinguish between DOUBLE and FLOAT and always creates Field_double.
Perhaps we should eventually change this to use agg_field_type() here,
and fix Item_func::tmp_table_field() to create Field_float when possible.
*/
set_handler_by_field_type(MYSQL_TYPE_DOUBLE);
break;
}
}
/*
Compare item arguments in the DATETIME context.
DESCRIPTION
Compare item arguments as DATETIME values and return the index of the
least/greatest argument in the arguments array.
The correct DATE/DATETIME value of the found argument is
stored to the value pointer, if latter is provided.
RETURN
1 If one of arguments is NULL or there was a execution error
0 Otherwise
*/
bool Item_func_min_max::get_date(MYSQL_TIME *ltime, ulonglong fuzzy_date)
{
longlong UNINIT_VAR(min_max);
DBUG_ASSERT(fixed == 1);
/*
just like ::val_int() method of a string item can be called,
for example, SELECT CONCAT("10", "12") + 1,
::get_date() can be called for non-temporal values,
for example, SELECT MONTH(GREATEST("2011-11-21", "2010-10-09"))
*/
if (Item_func_min_max::cmp_type() != TIME_RESULT)
return Item_func::get_date(ltime, fuzzy_date);
for (uint i=0; i < arg_count ; i++)
{
longlong res= args[i]->val_temporal_packed(Item_func_min_max::field_type());
/* Check if we need to stop (because of error or KILL) and stop the loop */
if (thd->is_error() || args[i]->null_value)
{
return (null_value= 1);
}
if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
min_max= res;
}
unpack_time(min_max, ltime);
if (Item_func_min_max::field_type() == MYSQL_TYPE_DATE)
{
ltime->time_type= MYSQL_TIMESTAMP_DATE;
ltime->hour= ltime->minute= ltime->second= ltime->second_part= 0;
}
else if (Item_func_min_max::field_type() == MYSQL_TYPE_TIME)
{
ltime->time_type= MYSQL_TIMESTAMP_TIME;
ltime->hour+= (ltime->month * 32 + ltime->day) * 24;
ltime->year= ltime->month= ltime->day= 0;
if (adjust_time_range_with_warn(ltime,
std::min<uint>(decimals, TIME_SECOND_PART_DIGITS)))
return (null_value= true);
}
if (!(fuzzy_date & TIME_TIME_ONLY) &&
((null_value= check_date_with_warn(ltime, fuzzy_date,
MYSQL_TIMESTAMP_ERROR))))
return true;
return (null_value= 0);
}
String *Item_func_min_max::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
if (Item_func_min_max::cmp_type() == TIME_RESULT)
return val_string_from_date(str);
switch (Item_func_min_max::result_type()) {
case INT_RESULT:
return val_string_from_int(str);
case DECIMAL_RESULT:
return val_string_from_decimal(str);
case REAL_RESULT:
return val_string_from_real(str);
case STRING_RESULT:
{
String *UNINIT_VAR(res);
for (uint i=0; i < arg_count ; i++)
{
if (i == 0)
res=args[i]->val_str(str);
else
{
String *res2;
res2= args[i]->val_str(res == str ? &tmp_value : str);
if (res2)
{
int cmp= sortcmp(res,res2,collation.collation);
if ((cmp_sign < 0 ? cmp : -cmp) < 0)
res=res2;
}
}
if ((null_value= args[i]->null_value))
return 0;
}
res->set_charset(collation.collation);
return res;
}
case ROW_RESULT:
case TIME_RESULT:
DBUG_ASSERT(0); // This case should never be chosen
return 0;
}
return 0; // Keep compiler happy
}
double Item_func_min_max::val_real()
{
DBUG_ASSERT(fixed == 1);
double value=0.0;
if (Item_func_min_max::cmp_type() == TIME_RESULT)
{
MYSQL_TIME ltime;
if (get_date(<ime, 0))
return 0;
return TIME_to_double(<ime);
}
for (uint i=0; i < arg_count ; i++)
{
if (i == 0)
value= args[i]->val_real();
else
{
double tmp= args[i]->val_real();
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
value=tmp;
}
if ((null_value= args[i]->null_value))
break;
}
return value;
}
longlong Item_func_min_max::val_int()
{
DBUG_ASSERT(fixed == 1);
longlong value=0;
if (Item_func_min_max::cmp_type() == TIME_RESULT)
{
MYSQL_TIME ltime;
if (get_date(<ime, 0))
return 0;
return TIME_to_ulonglong(<ime);
}
for (uint i=0; i < arg_count ; i++)
{
if (i == 0)
value=args[i]->val_int();
else
{
longlong tmp=args[i]->val_int();
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
value=tmp;
}
if ((null_value= args[i]->null_value))
break;
}
return value;
}
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
{
DBUG_ASSERT(fixed == 1);
my_decimal tmp_buf, *tmp, *UNINIT_VAR(res);
if (Item_func_min_max::cmp_type() == TIME_RESULT)
{
MYSQL_TIME ltime;
if (get_date(<ime, 0))
return 0;
return date2my_decimal(<ime, dec);
}
for (uint i=0; i < arg_count ; i++)
{
if (i == 0)
res= args[i]->val_decimal(dec);
else
{
tmp= args[i]->val_decimal(&tmp_buf); // Zero if NULL
if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
{
if (tmp == &tmp_buf)
{
/* Move value out of tmp_buf as this will be reused on next loop */
my_decimal2decimal(tmp, dec);
res= dec;
}
else
res= tmp;
}
}
if ((null_value= args[i]->null_value))
{
res= 0;
break;
}
}
return res;
}
longlong Item_func_length::val_int()
{
DBUG_ASSERT(fixed == 1);
String *res=args[0]->val_str(&value);
if (!res)
{
null_value=1;
return 0; /* purecov: inspected */
}
null_value=0;
return (longlong) res->length();
}
longlong Item_func_char_length::val_int()
{
DBUG_ASSERT(fixed == 1);
String *res=args[0]->val_str(&value);
if (!res)
{
null_value=1;
return 0; /* purecov: inspected */
}
null_value=0;
return (longlong) res->numchars();
}
longlong Item_func_coercibility::val_int()
{
DBUG_ASSERT(fixed == 1);
null_value= 0;
return (longlong) args[0]->collation.derivation;
}
void Item_func_locate::fix_length_and_dec()
{
max_length= MY_INT32_NUM_DECIMAL_DIGITS;
agg_arg_charsets_for_comparison(cmp_collation, args, 2);
}
longlong Item_func_locate::val_int()
{
DBUG_ASSERT(fixed == 1);
String *a=args[0]->val_str(&value1);
String *b=args[1]->val_str(&value2);
if (!a || !b)
{
null_value=1;
return 0; /* purecov: inspected */
}
null_value=0;
/* must be longlong to avoid truncation */
longlong start= 0;
longlong start0= 0;
my_match_t match;
if (arg_count == 3)
{
start0= start= args[2]->val_int() - 1;
if ((start < 0) || (start > a->length()))
return 0;
/* start is now sufficiently valid to pass to charpos function */
start= a->charpos((int) start);
if (start + b->length() > a->length())
return 0;
}
if (!b->length()) // Found empty string at start
return start + 1;
if (!cmp_collation.collation->coll->instr(cmp_collation.collation,
a->ptr()+start,
(uint) (a->length()-start),
b->ptr(), b->length(),
&match, 1))
return 0;
return (longlong) match.mb_len + start0 + 1;
}
void Item_func_locate::print(String *str, enum_query_type query_type)
{
str->append(STRING_WITH_LEN("locate("));
args[1]->print(str, query_type);
str->append(',');
args[0]->print(str, query_type);
if (arg_count == 3)
{
str->append(',');
args[2]->print(str, query_type);
}
str->append(')');
}
longlong Item_func_field::val_int()
{
DBUG_ASSERT(fixed == 1);
if (cmp_type == STRING_RESULT)
{
String *field;
if (!(field= args[0]->val_str(&value)))
return 0;
for (uint i=1 ; i < arg_count ; i++)
{
String *tmp_value=args[i]->val_str(&tmp);
if (tmp_value && !sortcmp(field,tmp_value,cmp_collation.collation))
return (longlong) (i);
}
}
else if (cmp_type == INT_RESULT)
{
longlong val= args[0]->val_int();
if (args[0]->null_value)
return 0;
for (uint i=1; i < arg_count ; i++)
{
if (val == args[i]->val_int() && !args[i]->null_value)
return (longlong) (i);
}
}
else if (cmp_type == DECIMAL_RESULT)
{
my_decimal dec_arg_buf, *dec_arg,
dec_buf, *dec= args[0]->val_decimal(&dec_buf);
if (args[0]->null_value)
return 0;
for (uint i=1; i < arg_count; i++)
{
dec_arg= args[i]->val_decimal(&dec_arg_buf);
if (!args[i]->null_value && !my_decimal_cmp(dec_arg, dec))
return (longlong) (i);
}
}
else
{
double val= args[0]->val_real();
if (args[0]->null_value)
return 0;
for (uint i=1; i < arg_count ; i++)
{
if (val == args[i]->val_real() && !args[i]->null_value)
return (longlong) (i);
}
}
return 0;
}
void Item_func_field::fix_length_and_dec()
{
maybe_null=0; max_length=3;
cmp_type= args[0]->result_type();
for (uint i=1; i < arg_count ; i++)
cmp_type= item_cmp_type(cmp_type, args[i]->result_type());
if (cmp_type == STRING_RESULT)
agg_arg_charsets_for_comparison(cmp_collation, args, arg_count);
}
longlong Item_func_ascii::val_int()
{
DBUG_ASSERT(fixed == 1);
String *res=args[0]->val_str(&value);
if (!res)
{
null_value=1;
return 0;
}
null_value=0;
return (longlong) (res->length() ? (uchar) (*res)[0] : (uchar) 0);
}
longlong Item_func_ord::val_int()
{
DBUG_ASSERT(fixed == 1);
String *res=args[0]->val_str(&value);
if (!res)
{
null_value=1;
return 0;
}
null_value=0;
if (!res->length()) return 0;
#ifdef USE_MB
if (use_mb(res->charset()))
{
register const char *str=res->ptr();
register uint32 n=0, l=my_ismbchar(res->charset(),str,str+res->length());
if (!l)
return (longlong)((uchar) *str);
while (l--)
n=(n<<8)|(uint32)((uchar) *str++);
return (longlong) n;
}
#endif
return (longlong) ((uchar) (*res)[0]);
}
/* Search after a string in a string of strings separated by ',' */
/* Returns number of found type >= 1 or 0 if not found */
/* This optimizes searching in enums to bit testing! */
void Item_func_find_in_set::fix_length_and_dec()
{
decimals=0;
max_length=3; // 1-999
if (args[0]->const_item() && args[1]->type() == FIELD_ITEM)
{
Field *field= ((Item_field*) args[1])->field;
if (field->real_type() == MYSQL_TYPE_SET)
{
String *find=args[0]->val_str(&value);
if (find)
{
// find is not NULL pointer so args[0] is not a null-value
DBUG_ASSERT(!args[0]->null_value);
enum_value= find_type(((Field_enum*) field)->typelib,find->ptr(),
find->length(), 0);
enum_bit=0;
if (enum_value)
enum_bit=1LL << (enum_value-1);
}
}
}
agg_arg_charsets_for_comparison(cmp_collation, args, 2);
}
static const char separator=',';
longlong Item_func_find_in_set::val_int()
{
DBUG_ASSERT(fixed == 1);
if (enum_value)
{
// enum_value is set iff args[0]->const_item() in fix_length_and_dec().
DBUG_ASSERT(args[0]->const_item());
ulonglong tmp= (ulonglong) args[1]->val_int();
null_value= args[1]->null_value;
/*
No need to check args[0]->null_value since enum_value is set iff
args[0] is a non-null const item. Note: no DBUG_ASSERT on
args[0]->null_value here because args[0] may have been replaced
by an Item_cache on which val_int() has not been called. See
BUG#11766317
*/
if (!null_value)
{
if (tmp & enum_bit)
return enum_value;
}
return 0L;
}
String *find=args[0]->val_str(&value);
String *buffer=args[1]->val_str(&value2);
if (!find || !buffer)
{
null_value=1;
return 0; /* purecov: inspected */
}
null_value=0;
if ((int) (buffer->length() - find->length()) >= 0)
{
my_wc_t wc= 0;
CHARSET_INFO *cs= cmp_collation.collation;
const char *str_begin= buffer->ptr();
const char *str_end= buffer->ptr();
const char *real_end= str_end+buffer->length();
const uchar *find_str= (const uchar *) find->ptr();
uint find_str_len= find->length();
int position= 0;
while (1)
{
int symbol_len;
if ((symbol_len= cs->cset->mb_wc(cs, &wc, (uchar*) str_end,
(uchar*) real_end)) > 0)
{
const char *substr_end= str_end + symbol_len;
bool is_last_item= (substr_end == real_end);
bool is_separator= (wc == (my_wc_t) separator);
if (is_separator || is_last_item)
{
position++;
if (is_last_item && !is_separator)
str_end= substr_end;
if (!my_strnncoll(cs, (const uchar *) str_begin,
(uint) (str_end - str_begin),
find_str, find_str_len))
return (longlong) position;
else
str_begin= substr_end;
}
str_end= substr_end;
}
else if (str_end - str_begin == 0 &&
find_str_len == 0 &&
wc == (my_wc_t) separator)
return (longlong) ++position;
else
return 0;
}
}
return 0;
}
longlong Item_func_bit_count::val_int()
{
DBUG_ASSERT(fixed == 1);
ulonglong value= (ulonglong) args[0]->val_int();
if ((null_value= args[0]->null_value))
return 0; /* purecov: inspected */
return (longlong) my_count_bits(value);
}
/****************************************************************************
** Functions to handle dynamic loadable functions
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
** Rewritten by monty.
****************************************************************************/
#ifdef HAVE_DLOPEN
void udf_handler::cleanup()
{
if (!not_original)
{
if (initialized)
{
if (u_d->func_deinit != NULL)
{
Udf_func_deinit deinit= u_d->func_deinit;
(*deinit)(&initid);
}
free_udf(u_d);
initialized= FALSE;
}
if (buffers) // Because of bug in ecc
delete [] buffers;
buffers= 0;
}
}
bool
udf_handler::fix_fields(THD *thd, Item_func_or_sum *func,
uint arg_count, Item **arguments)
{
uchar buff[STACK_BUFF_ALLOC]; // Max argument in function
DBUG_ENTER("Item_udf_func::fix_fields");
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
DBUG_RETURN(TRUE); // Fatal error flag is set!
udf_func *tmp_udf=find_udf(u_d->name.str,(uint) u_d->name.length,1);
if (!tmp_udf)
{
my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str);
DBUG_RETURN(TRUE);
}
u_d=tmp_udf;
args=arguments;
/* Fix all arguments */
func->maybe_null=0;
func->used_tables_and_const_cache_init();
if ((f_args.arg_count=arg_count))
{
if (!(f_args.arg_type= (Item_result*)
thd->alloc(f_args.arg_count*sizeof(Item_result))))
{
free_udf(u_d);
DBUG_RETURN(TRUE);
}
uint i;
Item **arg,**arg_end;
for (i=0, arg=arguments, arg_end=arguments+arg_count;
arg != arg_end ;
arg++,i++)
{
if (!(*arg)->fixed &&
(*arg)->fix_fields(thd, arg))
DBUG_RETURN(1);
// we can't assign 'item' before, because fix_fields() can change arg
Item *item= *arg;
if (item->check_cols(1))
DBUG_RETURN(TRUE);
/*
TODO: We should think about this. It is not always
right way just to set an UDF result to return my_charset_bin
if one argument has binary sorting order.
The result collation should be calculated according to arguments
derivations in some cases and should not in other cases.
Moreover, some arguments can represent a numeric input
which doesn't effect the result character set and collation.
There is no a general rule for UDF. Everything depends on
the particular user defined function.
*/
if (item->collation.collation->state & MY_CS_BINSORT)
func->collation.set(&my_charset_bin);
if (item->maybe_null)
func->maybe_null=1;
func->with_sum_func= func->with_sum_func || item->with_sum_func;
func->with_field= func->with_field || item->with_field;
func->with_subselect|= item->with_subselect;
func->used_tables_and_const_cache_join(item);
f_args.arg_type[i]=item->result_type();
}
//TODO: why all following memory is not allocated with 1 call of sql_alloc?
if (!(buffers=new String[arg_count]) ||
!(f_args.args= (char**) thd->alloc(arg_count * sizeof(char *))) ||
!(f_args.lengths= (ulong*) thd->alloc(arg_count * sizeof(long))) ||
!(f_args.maybe_null= (char*) thd->alloc(arg_count * sizeof(char))) ||
!(num_buffer= (char*) thd->alloc(arg_count *
ALIGN_SIZE(sizeof(double)))) ||
!(f_args.attributes= (char**) thd->alloc(arg_count *
sizeof(char *))) ||
!(f_args.attribute_lengths= (ulong*) thd->alloc(arg_count *
sizeof(long))))
{
free_udf(u_d);
DBUG_RETURN(TRUE);
}
}
func->fix_length_and_dec();
initid.max_length=func->max_length;
initid.maybe_null=func->maybe_null;
initid.const_item=func->const_item_cache;
initid.decimals=func->decimals;
initid.ptr=0;
if (u_d->func_init)
{
char init_msg_buff[MYSQL_ERRMSG_SIZE];
char *to=num_buffer;
for (uint i=0; i < arg_count; i++)
{
/*
For a constant argument i, args->args[i] points to the argument value.
For non-constant, args->args[i] is NULL.
*/
f_args.args[i]= NULL; /* Non-const unless updated below. */
f_args.lengths[i]= arguments[i]->max_length;
f_args.maybe_null[i]= (char) arguments[i]->maybe_null;
f_args.attributes[i]= arguments[i]->name;
f_args.attribute_lengths[i]= arguments[i]->name_length;
if (arguments[i]->const_item())
{
switch (arguments[i]->result_type()) {
case STRING_RESULT:
case DECIMAL_RESULT:
{
String *res= arguments[i]->val_str(&buffers[i]);
if (arguments[i]->null_value)
continue;
f_args.args[i]= (char*) res->c_ptr_safe();
f_args.lengths[i]= res->length();
break;
}
case INT_RESULT:
*((longlong*) to)= arguments[i]->val_int();
if (arguments[i]->null_value)
continue;
f_args.args[i]= to;
to+= ALIGN_SIZE(sizeof(longlong));
break;
case REAL_RESULT:
*((double*) to)= arguments[i]->val_real();
if (arguments[i]->null_value)
continue;
f_args.args[i]= to;
to+= ALIGN_SIZE(sizeof(double));
break;
case ROW_RESULT:
case TIME_RESULT:
DBUG_ASSERT(0); // This case should never be chosen
break;
}
}
}
Udf_func_init init= u_d->func_init;
if ((error=(uchar) init(&initid, &f_args, init_msg_buff)))
{
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
u_d->name.str, init_msg_buff);
free_udf(u_d);
DBUG_RETURN(TRUE);
}
func->max_length=MY_MIN(initid.max_length,MAX_BLOB_WIDTH);
func->maybe_null=initid.maybe_null;
/*
The above call for init() can reset initid.const_item to "false",
e.g. when the UDF function wants to be non-deterministic.
See sequence_init() in udf_example.cc.
*/
func->const_item_cache= initid.const_item;
func->decimals=MY_MIN(initid.decimals,NOT_FIXED_DEC);
}
initialized=1;
if (error)
{
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
u_d->name.str, ER_THD(thd, ER_UNKNOWN_ERROR));
DBUG_RETURN(TRUE);
}
DBUG_RETURN(FALSE);
}
bool udf_handler::get_arguments()
{
if (error)
return 1; // Got an error earlier
char *to= num_buffer;
uint str_count=0;
for (uint i=0; i < f_args.arg_count; i++)
{
f_args.args[i]=0;
switch (f_args.arg_type[i]) {
case STRING_RESULT:
case DECIMAL_RESULT:
{
String *res=args[i]->val_str(&buffers[str_count++]);
if (!(args[i]->null_value))
{
f_args.args[i]= (char*) res->ptr();
f_args.lengths[i]= res->length();
}
else
{
f_args.lengths[i]= 0;
}
break;
}
case INT_RESULT:
*((longlong*) to) = args[i]->val_int();
if (!args[i]->null_value)
{
f_args.args[i]=to;
to+= ALIGN_SIZE(sizeof(longlong));
}
break;
case REAL_RESULT:
*((double*) to)= args[i]->val_real();
if (!args[i]->null_value)
{
f_args.args[i]=to;
to+= ALIGN_SIZE(sizeof(double));
}
break;
case ROW_RESULT:
case TIME_RESULT:
DBUG_ASSERT(0); // This case should never be chosen
break;
}
}
return 0;
}
/**
@return
(String*)NULL in case of NULL values
*/
String *udf_handler::val_str(String *str,String *save_str)
{
uchar is_null_tmp=0;
ulong res_length;
DBUG_ENTER("udf_handler::val_str");
if (get_arguments())
DBUG_RETURN(0);
char * (*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
u_d->func;
if ((res_length=str->alloced_length()) < MAX_FIELD_WIDTH)
{ // This happens VERY seldom
if (str->alloc(MAX_FIELD_WIDTH))
{
error=1;
DBUG_RETURN(0);
}
}
char *res=func(&initid, &f_args, (char*) str->ptr(), &res_length,
&is_null_tmp, &error);
DBUG_PRINT("info", ("udf func returned, res_length: %lu", res_length));
if (is_null_tmp || !res || error) // The !res is for safety
{
DBUG_PRINT("info", ("Null or error"));
DBUG_RETURN(0);
}
if (res == str->ptr())
{
str->length(res_length);
DBUG_PRINT("exit", ("str: %*.s", (int) str->length(), str->ptr()));
DBUG_RETURN(str);
}
save_str->set(res, res_length, str->charset());
DBUG_PRINT("exit", ("save_str: %s", save_str->ptr()));
DBUG_RETURN(save_str);
}
/*
For the moment, UDF functions are returning DECIMAL values as strings
*/
my_decimal *udf_handler::val_decimal(my_bool *null_value, my_decimal *dec_buf)
{
char buf[DECIMAL_MAX_STR_LENGTH+1], *end;
ulong res_length= DECIMAL_MAX_STR_LENGTH;
if (get_arguments())
{
*null_value=1;
return 0;
}
char *(*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
u_d->func;
char *res= func(&initid, &f_args, buf, &res_length, &is_null, &error);
if (is_null || error)
{
*null_value= 1;
return 0;
}
end= res+ res_length;
str2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf, &end);
return dec_buf;
}
void Item_udf_func::cleanup()
{
udf.cleanup();
Item_func::cleanup();
}
void Item_udf_func::print(String *str, enum_query_type query_type)
{
str->append(func_name());
str->append('(');
for (uint i=0 ; i < arg_count ; i++)
{
if (i != 0)
str->append(',');
args[i]->print_item_w_name(str, query_type);
}
str->append(')');
}
double Item_func_udf_float::val_real()
{
double res;
my_bool tmp_null_value;
DBUG_ASSERT(fixed == 1);
DBUG_ENTER("Item_func_udf_float::val");
DBUG_PRINT("info",("result_type: %d arg_count: %d",
args[0]->result_type(), arg_count));
res= udf.val(&tmp_null_value);
null_value= tmp_null_value;
DBUG_RETURN(res);
}
String *Item_func_udf_float::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
double nr= val_real();
if (null_value)
return 0; /* purecov: inspected */
str->set_real(nr,decimals,&my_charset_bin);
return str;
}
longlong Item_func_udf_int::val_int()
{
longlong res;
my_bool tmp_null_value;
DBUG_ASSERT(fixed == 1);
DBUG_ENTER("Item_func_udf_int::val_int");
res= udf.val_int(&tmp_null_value);
null_value= tmp_null_value;
DBUG_RETURN(res);
}
String *Item_func_udf_int::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
longlong nr=val_int();
if (null_value)
return 0;
str->set_int(nr, unsigned_flag, &my_charset_bin);
return str;
}
longlong Item_func_udf_decimal::val_int()
{
my_bool tmp_null_value;
longlong result;
my_decimal dec_buf, *dec= udf.val_decimal(&tmp_null_value, &dec_buf);
null_value= tmp_null_value;
if (null_value)
return 0;
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
return result;
}
double Item_func_udf_decimal::val_real()
{
my_bool tmp_null_value;
double result;
my_decimal dec_buf, *dec= udf.val_decimal(&tmp_null_value, &dec_buf);
null_value= tmp_null_value;
if (null_value)
return 0.0;
my_decimal2double(E_DEC_FATAL_ERROR, dec, &result);
return result;
}
my_decimal *Item_func_udf_decimal::val_decimal(my_decimal *dec_buf)
{
my_decimal *res;
my_bool tmp_null_value;
DBUG_ASSERT(fixed == 1);
DBUG_ENTER("Item_func_udf_decimal::val_decimal");
DBUG_PRINT("info",("result_type: %d arg_count: %d",
args[0]->result_type(), arg_count));
res= udf.val_decimal(&tmp_null_value, dec_buf);
null_value= tmp_null_value;
DBUG_RETURN(res);
}
String *Item_func_udf_decimal::val_str(String *str)
{
my_bool tmp_null_value;
my_decimal dec_buf, *dec= udf.val_decimal(&tmp_null_value, &dec_buf);
null_value= tmp_null_value;
if (null_value)
return 0;
if (str->length() < DECIMAL_MAX_STR_LENGTH)
str->length(DECIMAL_MAX_STR_LENGTH);
my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, FALSE, &dec_buf);
my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, '0', str);
return str;
}
/* Default max_length is max argument length */
void Item_func_udf_str::fix_length_and_dec()
{
DBUG_ENTER("Item_func_udf_str::fix_length_and_dec");
max_length=0;
for (uint i = 0; i < arg_count; i++)
set_if_bigger(max_length,args[i]->max_length);
DBUG_VOID_RETURN;
}
String *Item_func_udf_str::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
String *res=udf.val_str(str,&str_value);
null_value = !res;
return res;
}
/**
@note
This has to come last in the udf_handler methods, or C for AIX
version 6.0.0.0 fails to compile with debugging enabled. (Yes, really.)
*/
udf_handler::~udf_handler()
{
/* Everything should be properly cleaned up by this moment. */
DBUG_ASSERT(not_original || !(initialized || buffers));
}
#else
bool udf_handler::get_arguments() { return 0; }
#endif /* HAVE_DLOPEN */
longlong Item_master_pos_wait::val_int()
{
DBUG_ASSERT(fixed == 1);
THD* thd = current_thd;
String *log_name = args[0]->val_str(&value);
int event_count= 0;
null_value=0;
if (thd->slave_thread || !log_name || !log_name->length())
{
null_value = 1;
return 0;
}
#ifdef HAVE_REPLICATION
longlong pos = (ulong)args[1]->val_int();
longlong timeout = (arg_count>=3) ? args[2]->val_int() : 0 ;
String connection_name_buff;
LEX_STRING connection_name;
Master_info *mi;
if (arg_count >= 4)
{
String *con;
if (!(con= args[3]->val_str(&connection_name_buff)))
goto err;
connection_name.str= (char*) con->ptr();
connection_name.length= con->length();
if (check_master_connection_name(&connection_name))
{
my_error(ER_WRONG_ARGUMENTS, MYF(ME_JUST_WARNING),
"MASTER_CONNECTION_NAME");
goto err;
}
}
else
connection_name= thd->variables.default_master_connection;
mysql_mutex_lock(&LOCK_active_mi);
mi= master_info_index->get_master_info(&connection_name,
Sql_condition::WARN_LEVEL_WARN);
mysql_mutex_unlock(&LOCK_active_mi);
if (!mi)
goto err;
if ((event_count = mi->rli.wait_for_pos(thd, log_name, pos, timeout)) == -2)
{
null_value = 1;
event_count=0;
}
#endif
return event_count;
#ifdef HAVE_REPLICATION
err:
{
null_value = 1;
return 0;
}
#endif
}
longlong Item_master_gtid_wait::val_int()
{
DBUG_ASSERT(fixed == 1);
longlong result= 0;
if (args[0]->null_value)
{
null_value= 1;
return 0;
}
null_value=0;
#ifdef HAVE_REPLICATION
THD* thd= current_thd;
longlong timeout_us;
String *gtid_pos = args[0]->val_str(&value);
if (arg_count==2 && !args[1]->null_value)
timeout_us= (longlong)(1e6*args[1]->val_real());
else
timeout_us= (longlong)-1;
result= rpl_global_gtid_waiting.wait_for_pos(thd, gtid_pos, timeout_us);
#endif
return result;
}
/**
Enables a session to wait on a condition until a timeout or a network
disconnect occurs.
@remark The connection is polled every m_interrupt_interval nanoseconds.
*/
class Interruptible_wait
{
THD *m_thd;
struct timespec m_abs_timeout;
static const ulonglong m_interrupt_interval;
public:
Interruptible_wait(THD *thd)
: m_thd(thd) {}
~Interruptible_wait() {}
public:
/**
Set the absolute timeout.
@param timeout The amount of time in nanoseconds to wait
*/
void set_timeout(ulonglong timeout)
{
/*
Calculate the absolute system time at the start so it can
be controlled in slices. It relies on the fact that once
the absolute time passes, the timed wait call will fail
automatically with a timeout error.
*/
set_timespec_nsec(m_abs_timeout, timeout);
}
/** The timed wait. */
int wait(mysql_cond_t *, mysql_mutex_t *);
};
/** Time to wait before polling the connection status. */
const ulonglong Interruptible_wait::m_interrupt_interval= 5 * 1000000000ULL;
/**
Wait for a given condition to be signaled.
@param cond The condition variable to wait on.
@param mutex The associated mutex.
@remark The absolute timeout is preserved across calls.
@retval return value from mysql_cond_timedwait
*/
int Interruptible_wait::wait(mysql_cond_t *cond, mysql_mutex_t *mutex)
{
int error;
struct timespec timeout;
while (1)
{
/* Wait for a fixed interval. */
set_timespec_nsec(timeout, m_interrupt_interval);
/* But only if not past the absolute timeout. */
if (cmp_timespec(timeout, m_abs_timeout) > 0)
timeout= m_abs_timeout;
error= mysql_cond_timedwait(cond, mutex, &timeout);
if (error == ETIMEDOUT || error == ETIME)
{
/* Return error if timed out or connection is broken. */
if (!cmp_timespec(timeout, m_abs_timeout) || !m_thd->is_connected())
break;
}
/* Otherwise, propagate status to the caller. */
else
break;
}
return error;
}
/**
For locks with EXPLICIT duration, MDL returns a new ticket
every time a lock is granted. This allows to implement recursive
locks without extra allocation or additional data structures, such
as below. However, if there are too many tickets in the same
MDL_context, MDL_context::find_ticket() is getting too slow,
since it's using a linear search.
This is why a separate structure is allocated for a user
level lock, and before requesting a new lock from MDL,
GET_LOCK() checks thd->ull_hash if such lock is already granted,
and if so, simply increments a reference counter.
*/
class User_level_lock
{
public:
MDL_ticket *lock;
int refs;
};
/** Extract a hash key from User_level_lock. */
uchar *ull_get_key(const uchar *ptr, size_t *length,
my_bool not_used __attribute__((unused)))
{
User_level_lock *ull = (User_level_lock*) ptr;
MDL_key *key = ull->lock->get_key();
*length= key->length();
return (uchar*) key->ptr();
}
/**
Release all user level locks for this THD.
*/
void mysql_ull_cleanup(THD *thd)
{
User_level_lock *ull;
DBUG_ENTER("mysql_ull_cleanup");
for (uint i= 0; i < thd->ull_hash.records; i++)
{
ull = (User_level_lock*) my_hash_element(&thd->ull_hash, i);
thd->mdl_context.release_lock(ull->lock);
my_free(ull);
}
my_hash_free(&thd->ull_hash);
DBUG_VOID_RETURN;
}
/**
Set explicit duration for metadata locks corresponding to
user level locks to protect them from being released at the end
of transaction.
*/
void mysql_ull_set_explicit_lock_duration(THD *thd)
{
User_level_lock *ull;
DBUG_ENTER("mysql_ull_set_explicit_lock_duration");
for (uint i= 0; i < thd->ull_hash.records; i++)
{
ull= (User_level_lock*) my_hash_element(&thd->ull_hash, i);
thd->mdl_context.set_lock_duration(ull->lock, MDL_EXPLICIT);
}
DBUG_VOID_RETURN;
}
/**
When MDL detects a lock wait timeout, it pushes
an error into the statement diagnostics area.
For GET_LOCK(), lock wait timeout is not an error,
but a special return value (0).
Similarly, killing get_lock wait is not an error either,
but a return value NULL.
Capture and suppress lock wait timeouts and kills.
*/
class Lock_wait_timeout_handler: public Internal_error_handler
{
public:
Lock_wait_timeout_handler() :m_lock_wait_timeout(false) {}
bool m_lock_wait_timeout;
bool handle_condition(THD * /* thd */, uint sql_errno,
const char * /* sqlstate */,
Sql_condition::enum_warning_level /* level */,
const char *message,
Sql_condition ** /* cond_hdl */);
};
bool
Lock_wait_timeout_handler::
handle_condition(THD *thd, uint sql_errno,
const char * /* sqlstate */,
Sql_condition::enum_warning_level /* level */,
const char *message,
Sql_condition ** /* cond_hdl */)
{
if (sql_errno == ER_LOCK_WAIT_TIMEOUT)
{
m_lock_wait_timeout= true;
return true; /* condition handled */
}
if (thd->is_killed())
return true;
return false;
}
static int ull_name_ok(String *name)
{
if (!name || !name->length())
return 0;
if (name->length() > NAME_LEN)
{
my_error(ER_TOO_LONG_IDENT, MYF(0), name->c_ptr_safe());
return 0;
}
return 1;
}
/**
Get a user level lock.
@retval
1 : Got lock
@retval
0 : Timeout
@retval
NULL : Error
*/
longlong Item_func_get_lock::val_int()
{
DBUG_ASSERT(fixed == 1);
String *res= args[0]->val_str(&value);
double timeout= args[1]->val_real();
THD *thd= current_thd;
User_level_lock *ull;
DBUG_ENTER("Item_func_get_lock::val_int");
null_value= 1;
/*
In slave thread no need to get locks, everything is serialized. Anyway
there is no way to make GET_LOCK() work on slave like it did on master
(i.e. make it return exactly the same value) because we don't have the
same other concurrent threads environment. No matter what we return here,
it's not guaranteed to be same as on master.
*/
if (thd->slave_thread)
{
null_value= 0;
DBUG_RETURN(1);
}
if (args[1]->null_value ||
(!args[1]->unsigned_flag && ((longlong) timeout < 0)))
{
char buf[22];
if (args[1]->null_value)
strmov(buf, "NULL");
else
llstr(((longlong) timeout), buf);
push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
"timeout", buf, "get_lock");
null_value= 1;
DBUG_RETURN(0);
}
if (!ull_name_ok(res))
DBUG_RETURN(0);
DBUG_PRINT("enter", ("lock: %.*s", res->length(), res->ptr()));
/* HASH entries are of type User_level_lock. */
if (! my_hash_inited(&thd->ull_hash) &&
my_hash_init(&thd->ull_hash, &my_charset_bin,
16 /* small hash */, 0, 0, ull_get_key, NULL, 0))
{
DBUG_RETURN(0);
}
MDL_request ull_request;
ull_request.init(MDL_key::USER_LOCK, res->c_ptr_safe(), "",
MDL_SHARED_NO_WRITE, MDL_EXPLICIT);
MDL_key *ull_key = &ull_request.key;
if ((ull= (User_level_lock*)
my_hash_search(&thd->ull_hash, ull_key->ptr(), ull_key->length())))
{
/* Recursive lock */
ull->refs++;
null_value = 0;
DBUG_PRINT("info", ("recursive lock, ref-count: %d", (int) ull->refs));
DBUG_RETURN(1);
}
Lock_wait_timeout_handler lock_wait_timeout_handler;
thd->push_internal_handler(&lock_wait_timeout_handler);
bool error= thd->mdl_context.acquire_lock(&ull_request, timeout);
(void) thd->pop_internal_handler();
if (error)
{
if (lock_wait_timeout_handler.m_lock_wait_timeout)
null_value= 0;
DBUG_RETURN(0);
}
ull= (User_level_lock*) my_malloc(sizeof(User_level_lock),
MYF(MY_WME|MY_THREAD_SPECIFIC));
if (ull == NULL)
{
thd->mdl_context.release_lock(ull_request.ticket);
DBUG_RETURN(0);
}
ull->lock= ull_request.ticket;
ull->refs= 1;
if (my_hash_insert(&thd->ull_hash, (uchar*) ull))
{
thd->mdl_context.release_lock(ull->lock);
my_free(ull);
DBUG_RETURN(0);
}
null_value= 0;
DBUG_RETURN(1);
}
/**
Release a user level lock.
@return
- 1 if lock released
- 0 if lock wasn't held
- (SQL) NULL if no such lock
*/
longlong Item_func_release_lock::val_int()
{
DBUG_ASSERT(fixed == 1);
String *res= args[0]->val_str(&value);
THD *thd= current_thd;
DBUG_ENTER("Item_func_release_lock::val_int");
null_value= 1;
if (!ull_name_ok(res))
DBUG_RETURN(0);
DBUG_PRINT("enter", ("lock: %.*s", res->length(), res->ptr()));
MDL_key ull_key;
ull_key.mdl_key_init(MDL_key::USER_LOCK, res->c_ptr_safe(), "");
User_level_lock *ull;
if (!(ull=
(User_level_lock*) my_hash_search(&thd->ull_hash,
ull_key.ptr(), ull_key.length())))
{
null_value= thd->mdl_context.get_lock_owner(&ull_key) == 0;
DBUG_RETURN(0);
}
DBUG_PRINT("info", ("ref count: %d", (int) ull->refs));
null_value= 0;
if (--ull->refs == 0)
{
my_hash_delete(&thd->ull_hash, (uchar*) ull);
thd->mdl_context.release_lock(ull->lock);
my_free(ull);
}
DBUG_RETURN(1);
}
/**
Check a user level lock.
Sets null_value=TRUE on error.
@retval
1 Available
@retval
0 Already taken, or error
*/
longlong Item_func_is_free_lock::val_int()
{
DBUG_ASSERT(fixed == 1);
String *res= args[0]->val_str(&value);
THD *thd= current_thd;
null_value= 1;
if (!ull_name_ok(res))
return 0;
MDL_key ull_key;
ull_key.mdl_key_init(MDL_key::USER_LOCK, res->c_ptr_safe(), "");
null_value= 0;
return thd->mdl_context.get_lock_owner(&ull_key) == 0;
}
longlong Item_func_is_used_lock::val_int()
{
DBUG_ASSERT(fixed == 1);
String *res= args[0]->val_str(&value);
THD *thd= current_thd;
null_value= 1;
if (!ull_name_ok(res))
return 0;
MDL_key ull_key;
ull_key.mdl_key_init(MDL_key::USER_LOCK, res->c_ptr_safe(), "");
ulong thread_id = thd->mdl_context.get_lock_owner(&ull_key);
if (thread_id == 0)
return 0;
null_value= 0;
return thread_id;
}
longlong Item_func_last_insert_id::val_int()
{
THD *thd= current_thd;
DBUG_ASSERT(fixed == 1);
if (arg_count)
{
longlong value= args[0]->val_int();
null_value= args[0]->null_value;
/*
LAST_INSERT_ID(X) must affect the client's mysql_insert_id() as
documented in the manual. We don't want to touch
first_successful_insert_id_in_cur_stmt because it would make
LAST_INSERT_ID(X) take precedence over an generated auto_increment
value for this row.
*/
thd->arg_of_last_insert_id_function= TRUE;
thd->first_successful_insert_id_in_prev_stmt= value;
return value;
}
return
static_cast<longlong>(thd->read_first_successful_insert_id_in_prev_stmt());
}
bool Item_func_last_insert_id::fix_fields(THD *thd, Item **ref)
{
thd->lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
return Item_int_func::fix_fields(thd, ref);
}
/* This function is just used to test speed of different functions */
longlong Item_func_benchmark::val_int()
{
DBUG_ASSERT(fixed == 1);
char buff[MAX_FIELD_WIDTH];
String tmp(buff,sizeof(buff), &my_charset_bin);
my_decimal tmp_decimal;
THD *thd= current_thd;
ulonglong loop_count;
loop_count= (ulonglong) args[0]->val_int();
if (args[0]->null_value ||
(!args[0]->unsigned_flag && (((longlong) loop_count) < 0)))
{
if (!args[0]->null_value)
{
char buff[22];
llstr(((longlong) loop_count), buff);
push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
ER_WRONG_VALUE_FOR_TYPE,
ER_THD(thd, ER_WRONG_VALUE_FOR_TYPE),
"count", buff, "benchmark");
}
null_value= 1;
return 0;
}
null_value=0;
for (ulonglong loop=0 ; loop < loop_count && !thd->killed; loop++)
{
switch (args[1]->result_type()) {
case REAL_RESULT:
(void) args[1]->val_real();
break;
case INT_RESULT:
(void) args[1]->val_int();
break;
case STRING_RESULT:
(void) args[1]->val_str(&tmp);
break;
case DECIMAL_RESULT:
(void) args[1]->val_decimal(&tmp_decimal);
break;
case ROW_RESULT:
case TIME_RESULT:
DBUG_ASSERT(0); // This case should never be chosen
return 0;
}
}
return 0;
}
void Item_func_benchmark::print(String *str, enum_query_type query_type)
{
str->append(STRING_WITH_LEN("benchmark("));
args[0]->print(str, query_type);
str->append(',');
args[1]->print(str, query_type);
str->append(')');
}
mysql_mutex_t LOCK_item_func_sleep;
#ifdef HAVE_PSI_INTERFACE
static PSI_mutex_key key_LOCK_item_func_sleep;
static PSI_mutex_info item_func_sleep_mutexes[]=
{
{ &key_LOCK_item_func_sleep, "LOCK_user_locks", PSI_FLAG_GLOBAL}
};
static void init_item_func_sleep_psi_keys(void)
{
const char* category= "sql";
int count;
if (PSI_server == NULL)
return;
count= array_elements(item_func_sleep_mutexes);
PSI_server->register_mutex(category, item_func_sleep_mutexes, count);
}
#endif
static bool item_func_sleep_inited= 0;
void item_func_sleep_init(void)
{
#ifdef HAVE_PSI_INTERFACE
init_item_func_sleep_psi_keys();
#endif
mysql_mutex_init(key_LOCK_item_func_sleep, &LOCK_item_func_sleep, MY_MUTEX_INIT_SLOW);
item_func_sleep_inited= 1;
}
void item_func_sleep_free(void)
{
if (item_func_sleep_inited)
{
item_func_sleep_inited= 0;
mysql_mutex_destroy(&LOCK_item_func_sleep);
}
}
/** This function is just used to create tests with time gaps. */
longlong Item_func_sleep::val_int()
{
THD *thd= current_thd;
Interruptible_wait timed_cond(thd);
mysql_cond_t cond;
double timeout;
int error;
DBUG_ASSERT(fixed == 1);
timeout= args[0]->val_real();
/*
On 64-bit OSX mysql_cond_timedwait() waits forever
if passed abstime time has already been exceeded by
the system time.
When given a very short timeout (< 10 mcs) just return
immediately.
We assume that the lines between this test and the call
to mysql_cond_timedwait() will be executed in less than 0.00001 sec.
*/
if (timeout < 0.00001)
return 0;
timed_cond.set_timeout((ulonglong) (timeout * 1000000000.0));
mysql_cond_init(key_item_func_sleep_cond, &cond, NULL);
mysql_mutex_lock(&LOCK_item_func_sleep);
THD_STAGE_INFO(thd, stage_user_sleep);
thd->mysys_var->current_mutex= &LOCK_item_func_sleep;
thd->mysys_var->current_cond= &cond;
error= 0;
thd_wait_begin(thd, THD_WAIT_SLEEP);
while (!thd->killed)
{
error= timed_cond.wait(&cond, &LOCK_item_func_sleep);
if (error == ETIMEDOUT || error == ETIME)
break;
error= 0;
}
thd_wait_end(thd);
mysql_mutex_unlock(&LOCK_item_func_sleep);
mysql_mutex_lock(&thd->mysys_var->mutex);
thd->mysys_var->current_mutex= 0;
thd->mysys_var->current_cond= 0;
mysql_mutex_unlock(&thd->mysys_var->mutex);
mysql_cond_destroy(&cond);
DBUG_EXECUTE_IF("sleep_inject_query_done_debug_sync", {
debug_sync_set_action
(thd, STRING_WITH_LEN("dispatch_command_end SIGNAL query_done"));
};);
return MY_TEST(!error); // Return 1 killed
}
#define extra_size sizeof(double)
user_var_entry *get_variable(HASH *hash, LEX_STRING &name,
bool create_if_not_exists)
{
user_var_entry *entry;
if (!(entry = (user_var_entry*) my_hash_search(hash, (uchar*) name.str,
name.length)) &&
create_if_not_exists)
{
uint size=ALIGN_SIZE(sizeof(user_var_entry))+name.length+1+extra_size;
if (!my_hash_inited(hash))
return 0;
if (!(entry = (user_var_entry*) my_malloc(size,
MYF(MY_WME | ME_FATALERROR |
MY_THREAD_SPECIFIC))))
return 0;
entry->name.str=(char*) entry+ ALIGN_SIZE(sizeof(user_var_entry))+
extra_size;
entry->name.length=name.length;
entry->value=0;
entry->length=0;
entry->update_query_id=0;
entry->set_charset(NULL);
entry->unsigned_flag= 0;
/*
If we are here, we were called from a SET or a query which sets a
variable. Imagine it is this:
INSERT INTO t SELECT @a:=10, @a:=@a+1.
Then when we have a Item_func_get_user_var (because of the @a+1) so we
think we have to write the value of @a to the binlog. But before that,
we have a Item_func_set_user_var to create @a (@a:=10), in this we mark
the variable as "already logged" (line below) so that it won't be logged
by Item_func_get_user_var (because that's not necessary).
*/
entry->used_query_id=current_thd->query_id;
entry->type=STRING_RESULT;
memcpy(entry->name.str, name.str, name.length+1);
if (my_hash_insert(hash,(uchar*) entry))
{
my_free(entry);
return 0;
}
}
return entry;
}
void Item_func_set_user_var::cleanup()
{
Item_func::cleanup();
m_var_entry= NULL;
}
bool Item_func_set_user_var::set_entry(THD *thd, bool create_if_not_exists)
{
if (m_var_entry && thd->thread_id == entry_thread_id)
goto end; // update entry->update_query_id for PS
if (!(m_var_entry= get_variable(&thd->user_vars, name, create_if_not_exists)))
{
entry_thread_id= 0;
return TRUE;
}
entry_thread_id= thd->thread_id;
/*
Remember the last query which updated it, this way a query can later know
if this variable is a constant item in the query (it is if update_query_id
is different from query_id).
*/
end:
m_var_entry->update_query_id= thd->query_id;
return FALSE;
}
/*
When a user variable is updated (in a SET command or a query like
SELECT @a:= ).
*/
bool Item_func_set_user_var::fix_fields(THD *thd, Item **ref)
{
DBUG_ASSERT(fixed == 0);
/* fix_fields will call Item_func_set_user_var::fix_length_and_dec */
if (Item_func::fix_fields(thd, ref) || set_entry(thd, TRUE))
return TRUE;
/*
As it is wrong and confusing to associate any
character set with NULL, @a should be latin2
after this query sequence:
SET @a=_latin2'string';
SET @a=NULL;
I.e. the second query should not change the charset
to the current default value, but should keep the
original value assigned during the first query.
In order to do it, we don't copy charset
from the argument if the argument is NULL
and the variable has previously been initialized.
*/
null_item= (args[0]->type() == NULL_ITEM);
if (!m_var_entry->charset() || !null_item)
m_var_entry->set_charset(args[0]->collation.derivation == DERIVATION_NUMERIC ?
default_charset() : args[0]->collation.collation);
collation.set(m_var_entry->charset(), DERIVATION_IMPLICIT);
set_handler_by_result_type(args[0]->result_type(),
max_length, collation.collation);
if (thd->lex->current_select)
{
/*
When this function is used in a derived table/view force the derived
table to be materialized to preserve possible side-effect of setting a
user variable.
*/
SELECT_LEX_UNIT *unit= thd->lex->current_select->master_unit();
TABLE_LIST *derived;
for (derived= unit->derived;
derived;
derived= derived->select_lex->master_unit()->derived)
derived->set_materialized_derived();
}
return FALSE;
}
void
Item_func_set_user_var::fix_length_and_dec()
{
maybe_null=args[0]->maybe_null;
decimals=args[0]->decimals;
collation.set(DERIVATION_IMPLICIT);
if (args[0]->collation.derivation == DERIVATION_NUMERIC)
fix_length_and_charset(args[0]->max_char_length(), default_charset());
else
{
fix_length_and_charset(args[0]->max_char_length(),
args[0]->collation.collation);
}
unsigned_flag= args[0]->unsigned_flag;
}
/*
Mark field in read_map
NOTES
This is used by filesort to register used fields in a a temporary
column read set or to register used fields in a view
*/
bool Item_func_set_user_var::register_field_in_read_map(uchar *arg)
{
if (result_field)
{
TABLE *table= (TABLE *) arg;
if (result_field->table == table || !table)
bitmap_set_bit(result_field->table->read_set, result_field->field_index);
if (result_field->vcol_info)
return result_field->vcol_info->
expr_item->walk(&Item::register_field_in_read_map, 1, arg);
}
return 0;
}
/*
Mark field in bitmap supplied as *arg
*/
bool Item_func_set_user_var::register_field_in_bitmap(uchar *arg)
{
MY_BITMAP *bitmap = (MY_BITMAP *) arg;
DBUG_ASSERT(bitmap);
if (result_field)
{
if (!bitmap)
return 1;
bitmap_set_bit(bitmap, result_field->field_index);
}
return 0;
}
/**
Set value to user variable.
@param entry pointer to structure representing variable
@param set_null should we set NULL value ?
@param ptr pointer to buffer with new value
@param length length of new value
@param type type of new value
@param cs charset info for new value
@param dv derivation for new value
@param unsigned_arg indiates if a value of type INT_RESULT is unsigned
@note Sets error and fatal error if allocation fails.
@retval
false success
@retval
true failure
*/
static bool
update_hash(user_var_entry *entry, bool set_null, void *ptr, uint length,
Item_result type, CHARSET_INFO *cs,
bool unsigned_arg)
{
if (set_null)
{
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
if (entry->value && entry->value != pos)
my_free(entry->value);
entry->value= 0;
entry->length= 0;
}
else
{
if (type == STRING_RESULT)
length++; // Store strings with end \0
if (length <= extra_size)
{
/* Save value in value struct */
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
if (entry->value != pos)
{
if (entry->value)
my_free(entry->value);
entry->value=pos;
}
}
else
{
/* Allocate variable */
if (entry->length != length)
{
char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry));
if (entry->value == pos)
entry->value=0;
entry->value= (char*) my_realloc(entry->value, length,
MYF(MY_ALLOW_ZERO_PTR | MY_WME |
ME_FATALERROR |
MY_THREAD_SPECIFIC));
if (!entry->value)
return 1;
}
}
if (type == STRING_RESULT)
{
length--; // Fix length change above
entry->value[length]= 0; // Store end \0
}
memmove(entry->value, ptr, length);
if (type == DECIMAL_RESULT)
((my_decimal*)entry->value)->fix_buffer_pointer();
entry->length= length;
entry->set_charset(cs);
entry->unsigned_flag= unsigned_arg;
}
entry->type=type;
return 0;
}
bool
Item_func_set_user_var::update_hash(void *ptr, uint length,
Item_result res_type,
CHARSET_INFO *cs,
bool unsigned_arg)
{
/*
If we set a variable explicitely to NULL then keep the old
result type of the variable
*/
if ((null_value= args[0]->null_value) && null_item)
res_type= m_var_entry->type; // Don't change type of item
if (::update_hash(m_var_entry, (null_value= args[0]->null_value),
ptr, length, res_type, cs, unsigned_arg))
{
null_value= 1;
return 1;
}
return 0;
}
/** Get the value of a variable as a double. */
double user_var_entry::val_real(bool *null_value)
{
if ((*null_value= (value == 0)))
return 0.0;
switch (type) {
case REAL_RESULT:
return *(double*) value;
case INT_RESULT:
return (double) *(longlong*) value;
case DECIMAL_RESULT:
{
double result;
my_decimal2double(E_DEC_FATAL_ERROR, (my_decimal *)value, &result);
return result;
}
case STRING_RESULT:
return my_atof(value); // This is null terminated
case ROW_RESULT:
case TIME_RESULT:
DBUG_ASSERT(0); // Impossible
break;
}
return 0.0; // Impossible
}
/** Get the value of a variable as an integer. */
longlong user_var_entry::val_int(bool *null_value) const
{
if ((*null_value= (value == 0)))
return 0;
switch (type) {
case REAL_RESULT:
return (longlong) *(double*) value;
case INT_RESULT:
return *(longlong*) value;
case DECIMAL_RESULT:
{
longlong result;
my_decimal2int(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, &result);
return result;
}
case STRING_RESULT:
{
int error;
return my_strtoll10(value, (char**) 0, &error);// String is null terminated
}
case ROW_RESULT:
case TIME_RESULT:
DBUG_ASSERT(0); // Impossible
break;
}
return 0; // Impossible
}
/** Get the value of a variable as a string. */
String *user_var_entry::val_str(bool *null_value, String *str,
uint decimals)
{
if ((*null_value= (value == 0)))
return (String*) 0;
switch (type) {
case REAL_RESULT:
str->set_real(*(double*) value, decimals, charset());
break;
case INT_RESULT:
if (!unsigned_flag)
str->set(*(longlong*) value, charset());
else
str->set(*(ulonglong*) value, charset());
break;
case DECIMAL_RESULT:
str_set_decimal((my_decimal *) value, str, charset());
break;
case STRING_RESULT:
if (str->copy(value, length, charset()))
str= 0; // EOM error
break;
case ROW_RESULT:
case TIME_RESULT:
DBUG_ASSERT(0); // Impossible
break;
}
return(str);
}
/** Get the value of a variable as a decimal. */
my_decimal *user_var_entry::val_decimal(bool *null_value, my_decimal *val)
{
if ((*null_value= (value == 0)))
return 0;
switch (type) {
case REAL_RESULT:
double2my_decimal(E_DEC_FATAL_ERROR, *(double*) value, val);
break;
case INT_RESULT:
int2my_decimal(E_DEC_FATAL_ERROR, *(longlong*) value, 0, val);
break;
case DECIMAL_RESULT:
my_decimal2decimal((my_decimal *) value, val);
break;
case STRING_RESULT:
str2my_decimal(E_DEC_FATAL_ERROR, value, length, charset(), val);
break;
case ROW_RESULT:
case TIME_RESULT:
DBUG_ASSERT(0); // Impossible
break;
}
return(val);
}
/**
This functions is invoked on SET \@variable or
\@variable:= expression.
Evaluate (and check expression), store results.
@note
For now it always return OK. All problem with value evaluating
will be caught by thd->is_error() check in sql_set_variables().
@retval
FALSE OK.
*/
bool
Item_func_set_user_var::check(bool use_result_field)
{
DBUG_ENTER("Item_func_set_user_var::check");
if (use_result_field && !result_field)
use_result_field= FALSE;
switch (Item_func_set_user_var::result_type()) {
case REAL_RESULT:
{
save_result.vreal= use_result_field ? result_field->val_real() :
args[0]->val_real();
break;
}
case INT_RESULT:
{
save_result.vint= use_result_field ? result_field->val_int() :
args[0]->val_int();
unsigned_flag= (use_result_field ?
((Field_num*)result_field)->unsigned_flag:
args[0]->unsigned_flag);
break;
}
case STRING_RESULT:
{
save_result.vstr= use_result_field ? result_field->val_str(&value) :
args[0]->val_str(&value);
break;
}
case DECIMAL_RESULT:
{
save_result.vdec= use_result_field ?
result_field->val_decimal(&decimal_buff) :
args[0]->val_decimal(&decimal_buff);
break;
}
case ROW_RESULT:
case TIME_RESULT:
DBUG_ASSERT(0); // This case should never be chosen
break;
}
DBUG_RETURN(FALSE);
}
/**
@brief Evaluate and store item's result.
This function is invoked on "SELECT ... INTO @var ...".
@param item An item to get value from.
*/
void Item_func_set_user_var::save_item_result(Item *item)
{
DBUG_ENTER("Item_func_set_user_var::save_item_result");
switch (args[0]->result_type()) {
case REAL_RESULT:
save_result.vreal= item->val_result();
break;
case INT_RESULT:
save_result.vint= item->val_int_result();
unsigned_flag= item->unsigned_flag;
break;
case STRING_RESULT:
save_result.vstr= item->str_result(&value);
break;
case DECIMAL_RESULT:
save_result.vdec= item->val_decimal_result(&decimal_buff);
break;
case ROW_RESULT:
case TIME_RESULT:
DBUG_ASSERT(0); // This case should never be chosen
break;
}
DBUG_VOID_RETURN;
}
/**
This functions is invoked on
SET \@variable or \@variable:= expression.
@note
We have to store the expression as such in the variable, independent of
the value method used by the user
@retval
0 OK
@retval
1 EOM Error
*/
bool
Item_func_set_user_var::update()
{
bool res= 0;
DBUG_ENTER("Item_func_set_user_var::update");
switch (Item_func_set_user_var::result_type()) {
case REAL_RESULT:
{
res= update_hash((void*) &save_result.vreal,sizeof(save_result.vreal),
REAL_RESULT, default_charset(), 0);
break;
}
case INT_RESULT:
{
res= update_hash((void*) &save_result.vint, sizeof(save_result.vint),
INT_RESULT, default_charset(), unsigned_flag);
break;
}
case STRING_RESULT:
{
if (!save_result.vstr) // Null value
res= update_hash((void*) 0, 0, STRING_RESULT, &my_charset_bin, 0);
else
res= update_hash((void*) save_result.vstr->ptr(),
save_result.vstr->length(), STRING_RESULT,
save_result.vstr->charset(), 0);
break;
}
case DECIMAL_RESULT:
{
if (!save_result.vdec) // Null value
res= update_hash((void*) 0, 0, DECIMAL_RESULT, &my_charset_bin, 0);
else
res= update_hash((void*) save_result.vdec,
sizeof(my_decimal), DECIMAL_RESULT,
default_charset(), 0);
break;
}
case ROW_RESULT:
case TIME_RESULT:
DBUG_ASSERT(0); // This case should never be chosen
break;
}
DBUG_RETURN(res);
}
double Item_func_set_user_var::val_real()
{
DBUG_ASSERT(fixed == 1);
check(0);
update(); // Store expression
return m_var_entry->val_real(&null_value);
}
longlong Item_func_set_user_var::val_int()
{
DBUG_ASSERT(fixed == 1);
check(0);
update(); // Store expression
return m_var_entry->val_int(&null_value);
}
String *Item_func_set_user_var::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
check(0);
update(); // Store expression
return m_var_entry->val_str(&null_value, str, decimals);
}
my_decimal *Item_func_set_user_var::val_decimal(my_decimal *val)
{
DBUG_ASSERT(fixed == 1);
check(0);
update(); // Store expression
return m_var_entry->val_decimal(&null_value, val);
}
double Item_func_set_user_var::val_result()
{
DBUG_ASSERT(fixed == 1);
check(TRUE);
update(); // Store expression
return m_var_entry->val_real(&null_value);
}
longlong Item_func_set_user_var::val_int_result()
{
DBUG_ASSERT(fixed == 1);
check(TRUE);
update(); // Store expression
return m_var_entry->val_int(&null_value);
}
bool Item_func_set_user_var::val_bool_result()
{
DBUG_ASSERT(fixed == 1);
check(TRUE);
update(); // Store expression
return m_var_entry->val_int(&null_value) != 0;
}
String *Item_func_set_user_var::str_result(String *str)
{
DBUG_ASSERT(fixed == 1);
check(TRUE);
update(); // Store expression
return m_var_entry->val_str(&null_value, str, decimals);
}
my_decimal *Item_func_set_user_var::val_decimal_result(my_decimal *val)
{
DBUG_ASSERT(fixed == 1);
check(TRUE);
update(); // Store expression
return m_var_entry->val_decimal(&null_value, val);
}
bool Item_func_set_user_var::is_null_result()
{
DBUG_ASSERT(fixed == 1);
check(TRUE);
update(); // Store expression
return is_null();
}
void Item_func_set_user_var::print(String *str, enum_query_type query_type)
{
str->append(STRING_WITH_LEN("(@"));
str->append(name.str, name.length);
str->append(STRING_WITH_LEN(":="));
args[0]->print(str, query_type);
str->append(')');
}
void Item_func_set_user_var::print_as_stmt(String *str,
enum_query_type query_type)
{
str->append(STRING_WITH_LEN("set @"));
str->append(name.str, name.length);
str->append(STRING_WITH_LEN(":="));
args[0]->print(str, query_type);
str->append(')');
}
bool Item_func_set_user_var::send(Protocol *protocol, String *str_arg)
{
if (result_field)
{
check(1);
update();
return protocol->store(result_field);
}
return Item::send(protocol, str_arg);
}
void Item_func_set_user_var::make_field(Send_field *tmp_field)
{
if (result_field)
{
result_field->make_field(tmp_field);
DBUG_ASSERT(tmp_field->table_name != 0);
if (Item::name)
tmp_field->col_name=Item::name; // Use user supplied name
}
else
Item::make_field(tmp_field);
}
/*
Save the value of a user variable into a field
SYNOPSIS
save_in_field()
field target field to save the value to
no_conversion flag indicating whether conversions are allowed
DESCRIPTION
Save the function value into a field and update the user variable
accordingly. If a result field is defined and the target field doesn't
coincide with it then the value from the result field will be used as
the new value of the user variable.
The reason to have this method rather than simply using the result
field in the val_xxx() methods is that the value from the result field
not always can be used when the result field is defined.
Let's consider the following cases:
1) when filling a tmp table the result field is defined but the value of it
is undefined because it has to be produced yet. Thus we can't use it.
2) on execution of an INSERT ... SELECT statement the save_in_field()
function will be called to fill the data in the new record. If the SELECT
part uses a tmp table then the result field is defined and should be
used in order to get the correct result.
The difference between the SET_USER_VAR function and regular functions
like CONCAT is that the Item_func objects for the regular functions are
replaced by Item_field objects after the values of these functions have
been stored in a tmp table. Yet an object of the Item_field class cannot
be used to update a user variable.
Due to this we have to handle the result field in a special way here and
in the Item_func_set_user_var::send() function.
RETURN VALUES
FALSE Ok
TRUE Error
*/
int Item_func_set_user_var::save_in_field(Field *field, bool no_conversions,
bool can_use_result_field)
{
bool use_result_field= (!can_use_result_field ? 0 :
(result_field && result_field != field));
int error;
/* Update the value of the user variable */
check(use_result_field);
update();
if (result_type() == STRING_RESULT ||
(result_type() == REAL_RESULT &&
field->result_type() == STRING_RESULT))
{
String *result;
CHARSET_INFO *cs= collation.collation;
char buff[MAX_FIELD_WIDTH]; // Alloc buffer for small columns
str_value.set_quick(buff, sizeof(buff), cs);
result= m_var_entry->val_str(&null_value, &str_value, decimals);
if (null_value)
{
str_value.set_quick(0, 0, cs);
return set_field_to_null_with_conversions(field, no_conversions);
}
/* NOTE: If null_value == FALSE, "result" must be not NULL. */
field->set_notnull();
error=field->store(result->ptr(),result->length(),cs);
str_value.set_quick(0, 0, cs);
}
else if (result_type() == REAL_RESULT)
{
double nr= m_var_entry->val_real(&null_value);
if (null_value)
return set_field_to_null(field);
field->set_notnull();
error=field->store(nr);
}
else if (result_type() == DECIMAL_RESULT)
{
my_decimal decimal_value;
my_decimal *val= m_var_entry->val_decimal(&null_value, &decimal_value);
if (null_value)
return set_field_to_null(field);
field->set_notnull();
error=field->store_decimal(val);
}
else
{
longlong nr= m_var_entry->val_int(&null_value);
if (null_value)
return set_field_to_null_with_conversions(field, no_conversions);
field->set_notnull();
error=field->store(nr, unsigned_flag);
}
return error;
}
String *
Item_func_get_user_var::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
DBUG_ENTER("Item_func_get_user_var::val_str");
if (!m_var_entry)
DBUG_RETURN((String*) 0); // No such variable
DBUG_RETURN(m_var_entry->val_str(&null_value, str, decimals));
}
double Item_func_get_user_var::val_real()
{
DBUG_ASSERT(fixed == 1);
if (!m_var_entry)
return 0.0; // No such variable
return (m_var_entry->val_real(&null_value));
}
my_decimal *Item_func_get_user_var::val_decimal(my_decimal *dec)
{
DBUG_ASSERT(fixed == 1);
if (!m_var_entry)
return 0;
return m_var_entry->val_decimal(&null_value, dec);
}
longlong Item_func_get_user_var::val_int()
{
DBUG_ASSERT(fixed == 1);
if (!m_var_entry)
return 0; // No such variable
return (m_var_entry->val_int(&null_value));
}
/**
Get variable by name and, if necessary, put the record of variable
use into the binary log.
When a user variable is invoked from an update query (INSERT, UPDATE etc),
stores this variable and its value in thd->user_var_events, so that it can be
written to the binlog (will be written just before the query is written, see
log.cc).
@param thd Current thread
@param name Variable name
@param[out] out_entry variable structure or NULL. The pointer is set
regardless of whether function succeeded or not.
@retval
0 OK
@retval
1 Failed to put appropriate record into binary log
*/
static int
get_var_with_binlog(THD *thd, enum_sql_command sql_command,
LEX_STRING &name, user_var_entry **out_entry)
{
BINLOG_USER_VAR_EVENT *user_var_event;
user_var_entry *var_entry;
var_entry= get_variable(&thd->user_vars, name, 0);
/*
Any reference to user-defined variable which is done from stored
function or trigger affects their execution and the execution of the
calling statement. We must log all such variables even if they are
not involved in table-updating statements.
*/
if (!(opt_bin_log &&
(is_update_query(sql_command) || thd->in_sub_stmt)))
{
*out_entry= var_entry;
return 0;
}
if (!var_entry)
{
/*
If the variable does not exist, it's NULL, but we want to create it so
that it gets into the binlog (if it didn't, the slave could be
influenced by a variable of the same name previously set by another
thread).
We create it like if it had been explicitly set with SET before.
The 'new' mimics what sql_yacc.yy does when 'SET @a=10;'.
sql_set_variables() is what is called from 'case SQLCOM_SET_OPTION'
in dispatch_command()). Instead of building a one-element list to pass to
sql_set_variables(), we could instead manually call check() and update();
this would save memory and time; but calling sql_set_variables() makes
one unique place to maintain (sql_set_variables()).
Manipulation with lex is necessary since free_underlaid_joins
is going to release memory belonging to the main query.
*/
List<set_var_base> tmp_var_list;
LEX *sav_lex= thd->lex, lex_tmp;
thd->lex= &lex_tmp;
lex_start(thd);
tmp_var_list.push_back(new (thd->mem_root)
set_var_user(new (thd->mem_root)
Item_func_set_user_var(thd, name,
new (thd->mem_root) Item_null(thd))),
thd->mem_root);
/* Create the variable */
if (sql_set_variables(thd, &tmp_var_list, false))
{
thd->lex= sav_lex;
goto err;
}
thd->lex= sav_lex;
if (!(var_entry= get_variable(&thd->user_vars, name, 0)))
goto err;
}
else if (var_entry->used_query_id == thd->query_id ||
mysql_bin_log.is_query_in_union(thd, var_entry->used_query_id))
{
/*
If this variable was already stored in user_var_events by this query
(because it's used in more than one place in the query), don't store
it.
*/
*out_entry= var_entry;
return 0;
}
uint size;
/*
First we need to store value of var_entry, when the next situation
appears:
> set @a:=1;
> insert into t1 values (@a), (@a:=@a+1), (@a:=@a+1);
We have to write to binlog value @a= 1.
We allocate the user_var_event on user_var_events_alloc pool, not on
the this-statement-execution pool because in SPs user_var_event objects
may need to be valid after current [SP] statement execution pool is
destroyed.
*/
size= ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT)) + var_entry->length;
if (!(user_var_event= (BINLOG_USER_VAR_EVENT *)
alloc_root(thd->user_var_events_alloc, size)))
goto err;
user_var_event->value= (char*) user_var_event +
ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT));
user_var_event->user_var_event= var_entry;
user_var_event->type= var_entry->type;
user_var_event->charset_number= var_entry->charset()->number;
user_var_event->unsigned_flag= var_entry->unsigned_flag;
if (!var_entry->value)
{
/* NULL value*/
user_var_event->length= 0;
user_var_event->value= 0;
}
else
{
user_var_event->length= var_entry->length;
memcpy(user_var_event->value, var_entry->value,
var_entry->length);
}
/* Mark that this variable has been used by this query */
var_entry->used_query_id= thd->query_id;
if (insert_dynamic(&thd->user_var_events, (uchar*) &user_var_event))
goto err;
*out_entry= var_entry;
return 0;
err:
*out_entry= var_entry;
return 1;
}
void Item_func_get_user_var::fix_length_and_dec()
{
THD *thd=current_thd;
int error;
maybe_null=1;
decimals=NOT_FIXED_DEC;
max_length=MAX_BLOB_WIDTH;
error= get_var_with_binlog(thd, thd->lex->sql_command, name, &m_var_entry);
/*
If the variable didn't exist it has been created as a STRING-type.
'm_var_entry' is NULL only if there occurred an error during the call to
get_var_with_binlog.
*/
if (!error && m_var_entry)
{
unsigned_flag= m_var_entry->unsigned_flag;
max_length= m_var_entry->length;
collation.set(m_var_entry->charset(), DERIVATION_IMPLICIT);
set_handler_by_result_type(m_var_entry->type);
switch (Item_func_get_user_var::result_type()) {
case REAL_RESULT:
fix_char_length(DBL_DIG + 8);
break;
case INT_RESULT:
fix_char_length(MAX_BIGINT_WIDTH);
decimals=0;
break;
case STRING_RESULT:
max_length= MAX_BLOB_WIDTH - 1;
set_handler_by_field_type(MYSQL_TYPE_MEDIUM_BLOB);
break;
case DECIMAL_RESULT:
fix_char_length(DECIMAL_MAX_STR_LENGTH);
decimals= DECIMAL_MAX_SCALE;
break;
case ROW_RESULT: // Keep compiler happy
case TIME_RESULT:
DBUG_ASSERT(0); // This case should never be chosen
break;
}
}
else
{
collation.set(&my_charset_bin, DERIVATION_IMPLICIT);
null_value= 1;
set_handler_by_field_type(MYSQL_TYPE_LONG_BLOB);
max_length= MAX_BLOB_WIDTH;
}
}
bool Item_func_get_user_var::const_item() const
{
return (!m_var_entry ||
current_thd->query_id != m_var_entry->update_query_id);
}
void Item_func_get_user_var::print(String *str, enum_query_type query_type)
{
str->append(STRING_WITH_LEN("(@"));
append_identifier(current_thd, str, name.str, name.length);
str->append(')');
}
bool Item_func_get_user_var::eq(const Item *item, bool binary_cmp) const
{
/* Assume we don't have rtti */
if (this == item)
return 1; // Same item is same.
/* Check if other type is also a get_user_var() object */
if (item->type() != FUNC_ITEM ||
((Item_func*) item)->functype() != functype())
return 0;
Item_func_get_user_var *other=(Item_func_get_user_var*) item;
return (name.length == other->name.length &&
!memcmp(name.str, other->name.str, name.length));
}
bool Item_func_get_user_var::set_value(THD *thd,
sp_rcontext * /*ctx*/, Item **it)
{
Item_func_set_user_var *suv= new (thd->mem_root) Item_func_set_user_var(thd, get_name(), *it);
/*
Item_func_set_user_var is not fixed after construction, call
fix_fields().
*/
return (!suv || suv->fix_fields(thd, it) || suv->check(0) || suv->update());
}
bool Item_user_var_as_out_param::fix_fields(THD *thd, Item **ref)
{
DBUG_ASSERT(fixed == 0);
DBUG_ASSERT(thd->lex->exchange);
if (Item::fix_fields(thd, ref) ||
!(entry= get_variable(&thd->user_vars, name, 1)))
return TRUE;
entry->type= STRING_RESULT;
/*
Let us set the same collation which is used for loading
of fields in LOAD DATA INFILE.
(Since Item_user_var_as_out_param is used only there).
*/
entry->set_charset(thd->lex->exchange->cs ?
thd->lex->exchange->cs :
thd->variables.collation_database);
entry->update_query_id= thd->query_id;
return FALSE;
}
void Item_user_var_as_out_param::set_null_value(CHARSET_INFO* cs)
{
::update_hash(entry, TRUE, 0, 0, STRING_RESULT, cs, 0 /* unsigned_arg */);
}
void Item_user_var_as_out_param::set_value(const char *str, uint length,
CHARSET_INFO* cs)
{
::update_hash(entry, FALSE, (void*)str, length, STRING_RESULT, cs,
0 /* unsigned_arg */);
}
double Item_user_var_as_out_param::val_real()
{
DBUG_ASSERT(0);
return 0.0;
}
longlong Item_user_var_as_out_param::val_int()
{
DBUG_ASSERT(0);
return 0;
}
String* Item_user_var_as_out_param::val_str(String *str)
{
DBUG_ASSERT(0);
return 0;
}
my_decimal* Item_user_var_as_out_param::val_decimal(my_decimal *decimal_buffer)
{
DBUG_ASSERT(0);
return 0;
}
void Item_user_var_as_out_param::print_for_load(THD *thd, String *str)
{
str->append('@');
append_identifier(thd, str, name.str, name.length);
}
Item_func_get_system_var::
Item_func_get_system_var(THD *thd, sys_var *var_arg, enum_var_type var_type_arg,
LEX_STRING *component_arg, const char *name_arg,
size_t name_len_arg):
Item_func(thd), var(var_arg), var_type(var_type_arg),
orig_var_type(var_type_arg), component(*component_arg), cache_present(0)
{
/* set_name() will allocate the name */
set_name(name_arg, (uint) name_len_arg, system_charset_info);
}
bool Item_func_get_system_var::is_written_to_binlog()
{
return var->is_written_to_binlog(var_type);
}
void Item_func_get_system_var::update_null_value()
{
THD *thd= current_thd;
int save_no_errors= thd->no_errors;
thd->no_errors= TRUE;
Item::update_null_value();
thd->no_errors= save_no_errors;
}
void Item_func_get_system_var::fix_length_and_dec()
{
char *cptr;
maybe_null= TRUE;
max_length= 0;
if (var->check_type(var_type))
{
if (var_type != OPT_DEFAULT)
{
my_error(ER_INCORRECT_GLOBAL_LOCAL_VAR, MYF(0),
var->name.str, var_type == OPT_GLOBAL ? "SESSION" : "GLOBAL");
return;
}
/* As there was no local variable, return the global value */
var_type= OPT_GLOBAL;
}
switch (var->show_type())
{
case SHOW_HA_ROWS:
case SHOW_UINT:
case SHOW_ULONG:
case SHOW_ULONGLONG:
unsigned_flag= TRUE;
/* fall through */
case SHOW_SINT:
case SHOW_SLONG:
case SHOW_SLONGLONG:
collation.set_numeric();
fix_char_length(MY_INT64_NUM_DECIMAL_DIGITS);
decimals=0;
break;
case SHOW_CHAR:
case SHOW_CHAR_PTR:
mysql_mutex_lock(&LOCK_global_system_variables);
cptr= var->show_type() == SHOW_CHAR ?
(char*) var->value_ptr(current_thd, var_type, &component) :
*(char**) var->value_ptr(current_thd, var_type, &component);
if (cptr)
max_length= system_charset_info->cset->numchars(system_charset_info,
cptr,
cptr + strlen(cptr));
mysql_mutex_unlock(&LOCK_global_system_variables);
collation.set(system_charset_info, DERIVATION_SYSCONST);
max_length*= system_charset_info->mbmaxlen;
decimals=NOT_FIXED_DEC;
break;
case SHOW_LEX_STRING:
{
mysql_mutex_lock(&LOCK_global_system_variables);
LEX_STRING *ls= ((LEX_STRING*)var->value_ptr(current_thd, var_type, &component));
max_length= system_charset_info->cset->numchars(system_charset_info,
ls->str,
ls->str + ls->length);
mysql_mutex_unlock(&LOCK_global_system_variables);
collation.set(system_charset_info, DERIVATION_SYSCONST);
max_length*= system_charset_info->mbmaxlen;
decimals=NOT_FIXED_DEC;
}
break;
case SHOW_BOOL:
case SHOW_MY_BOOL:
collation.set_numeric();
fix_char_length(1);
decimals=0;
break;
case SHOW_DOUBLE:
decimals= 6;
collation.set_numeric();
fix_char_length(DBL_DIG + 6);
break;
default:
my_error(ER_VAR_CANT_BE_READ, MYF(0), var->name.str);
break;
}
}
void Item_func_get_system_var::print(String *str, enum_query_type query_type)
{
str->append(name, name_length);
}
enum Item_result Item_func_get_system_var::result_type() const
{
switch (var->show_type())
{
case SHOW_BOOL:
case SHOW_MY_BOOL:
case SHOW_SINT:
case SHOW_SLONG:
case SHOW_SLONGLONG:
case SHOW_UINT:
case SHOW_ULONG:
case SHOW_ULONGLONG:
case SHOW_HA_ROWS:
return INT_RESULT;
case SHOW_CHAR:
case SHOW_CHAR_PTR:
case SHOW_LEX_STRING:
return STRING_RESULT;
case SHOW_DOUBLE:
return REAL_RESULT;
default:
my_error(ER_VAR_CANT_BE_READ, MYF(0), var->name.str);
return STRING_RESULT; // keep the compiler happy
}
}
enum_field_types Item_func_get_system_var::field_type() const
{
switch (var->show_type())
{
case SHOW_BOOL:
case SHOW_MY_BOOL:
case SHOW_SINT:
case SHOW_SLONG:
case SHOW_SLONGLONG:
case SHOW_UINT:
case SHOW_ULONG:
case SHOW_ULONGLONG:
case SHOW_HA_ROWS:
return MYSQL_TYPE_LONGLONG;
case SHOW_CHAR:
case SHOW_CHAR_PTR:
case SHOW_LEX_STRING:
return MYSQL_TYPE_VARCHAR;
case SHOW_DOUBLE:
return MYSQL_TYPE_DOUBLE;
default:
my_error(ER_VAR_CANT_BE_READ, MYF(0), var->name.str);
return MYSQL_TYPE_VARCHAR; // keep the compiler happy
}
}
longlong Item_func_get_system_var::val_int()
{
THD *thd= current_thd;
DBUG_EXECUTE_IF("simulate_non_gtid_aware_master",
{
if (0 == strcmp("gtid_domain_id", var->name.str))
{
my_error(ER_VAR_CANT_BE_READ, MYF(0), var->name.str);
return 0;
}
});
if (cache_present && thd->query_id == used_query_id)
{
if (cache_present & GET_SYS_VAR_CACHE_LONG)
{
null_value= cached_null_value;
return cached_llval;
}
else if (cache_present & GET_SYS_VAR_CACHE_DOUBLE)
{
null_value= cached_null_value;
cached_llval= (longlong) cached_dval;
cache_present|= GET_SYS_VAR_CACHE_LONG;
return cached_llval;
}
else if (cache_present & GET_SYS_VAR_CACHE_STRING)
{
null_value= cached_null_value;
if (!null_value)
cached_llval= longlong_from_string_with_check(&cached_strval);
else
cached_llval= 0;
cache_present|= GET_SYS_VAR_CACHE_LONG;
return cached_llval;
}
}
cached_llval= var->val_int(&null_value, thd, var_type, &component);
cache_present |= GET_SYS_VAR_CACHE_LONG;
used_query_id= thd->query_id;
cached_null_value= null_value;
return cached_llval;
}
String* Item_func_get_system_var::val_str(String* str)
{
THD *thd= current_thd;
if (cache_present && thd->query_id == used_query_id)
{
if (cache_present & GET_SYS_VAR_CACHE_STRING)
{
null_value= cached_null_value;
return null_value ? NULL : &cached_strval;
}
else if (cache_present & GET_SYS_VAR_CACHE_LONG)
{
null_value= cached_null_value;
if (!null_value)
cached_strval.set (cached_llval, collation.collation);
cache_present|= GET_SYS_VAR_CACHE_STRING;
return null_value ? NULL : &cached_strval;
}
else if (cache_present & GET_SYS_VAR_CACHE_DOUBLE)
{
null_value= cached_null_value;
if (!null_value)
cached_strval.set_real (cached_dval, decimals, collation.collation);
cache_present|= GET_SYS_VAR_CACHE_STRING;
return null_value ? NULL : &cached_strval;
}
}
str= var->val_str(&cached_strval, thd, var_type, &component);
cache_present|= GET_SYS_VAR_CACHE_STRING;
used_query_id= thd->query_id;
cached_null_value= null_value= !str;
return str;
}
double Item_func_get_system_var::val_real()
{
THD *thd= current_thd;
if (cache_present && thd->query_id == used_query_id)
{
if (cache_present & GET_SYS_VAR_CACHE_DOUBLE)
{
null_value= cached_null_value;
return cached_dval;
}
else if (cache_present & GET_SYS_VAR_CACHE_LONG)
{
null_value= cached_null_value;
cached_dval= (double)cached_llval;
cache_present|= GET_SYS_VAR_CACHE_DOUBLE;
return cached_dval;
}
else if (cache_present & GET_SYS_VAR_CACHE_STRING)
{
null_value= cached_null_value;
if (!null_value)
cached_dval= double_from_string_with_check(&cached_strval);
else
cached_dval= 0;
cache_present|= GET_SYS_VAR_CACHE_DOUBLE;
return cached_dval;
}
}
cached_dval= var->val_real(&null_value, thd, var_type, &component);
cache_present |= GET_SYS_VAR_CACHE_DOUBLE;
used_query_id= thd->query_id;
cached_null_value= null_value;
return cached_dval;
}
bool Item_func_get_system_var::eq(const Item *item, bool binary_cmp) const
{
/* Assume we don't have rtti */
if (this == item)
return 1; // Same item is same.
/* Check if other type is also a get_user_var() object */
if (item->type() != FUNC_ITEM ||
((Item_func*) item)->functype() != functype())
return 0;
Item_func_get_system_var *other=(Item_func_get_system_var*) item;
return (var == other->var && var_type == other->var_type);
}
void Item_func_get_system_var::cleanup()
{
Item_func::cleanup();
cache_present= 0;
var_type= orig_var_type;
cached_strval.free();
}
void Item_func_match::init_search(THD *thd, bool no_order)
{
DBUG_ENTER("Item_func_match::init_search");
if (!table->file->get_table()) // the handler isn't opened yet
DBUG_VOID_RETURN;
/* Check if init_search() has been called before */
if (ft_handler)
{
if (join_key)
table->file->ft_handler= ft_handler;
DBUG_VOID_RETURN;
}
if (key == NO_SUCH_KEY)
{
List<Item> fields;
fields.push_back(new (thd->mem_root)
Item_string(thd, " ", 1, cmp_collation.collation),
thd->mem_root);
for (uint i= 1; i < arg_count; i++)
fields.push_back(args[i]);
concat_ws= new (thd->mem_root) Item_func_concat_ws(thd, fields);
/*
Above function used only to get value and do not need fix_fields for it:
Item_string - basic constant
fields - fix_fields() was already called for this arguments
Item_func_concat_ws - do not need fix_fields() to produce value
*/
concat_ws->quick_fix_field();
}
if (master)
{
join_key= master->join_key= join_key | master->join_key;
master->init_search(thd, no_order);
ft_handler= master->ft_handler;
join_key= master->join_key;
DBUG_VOID_RETURN;
}
String *ft_tmp= 0;
// MATCH ... AGAINST (NULL) is meaningless, but possible
if (!(ft_tmp=key_item()->val_str(&value)))
{
ft_tmp= &value;
value.set("", 0, cmp_collation.collation);
}
if (ft_tmp->charset() != cmp_collation.collation)
{
uint dummy_errors;
search_value.copy(ft_tmp->ptr(), ft_tmp->length(), ft_tmp->charset(),
cmp_collation.collation, &dummy_errors);
ft_tmp= &search_value;
}
if (join_key && !no_order)
flags|=FT_SORTED;
if (key != NO_SUCH_KEY)
THD_STAGE_INFO(table->in_use, stage_fulltext_initialization);
ft_handler= table->file->ft_init_ext(flags, key, ft_tmp);
if (join_key)
table->file->ft_handler=ft_handler;
DBUG_VOID_RETURN;
}
bool Item_func_match::fix_fields(THD *thd, Item **ref)
{
DBUG_ASSERT(fixed == 0);
Item *UNINIT_VAR(item); // Safe as arg_count is > 1
status_var_increment(thd->status_var.feature_fulltext);
maybe_null=1;
join_key=0;
/*
const_item is assumed in quite a bit of places, so it would be difficult
to remove; If it would ever to be removed, this should include
modifications to find_best and auto_close as complement to auto_init code
above.
*/
if (Item_func::fix_fields(thd, ref) ||
!args[0]->const_during_execution())
{
my_error(ER_WRONG_ARGUMENTS,MYF(0),"AGAINST");
return TRUE;
}
bool allows_multi_table_search= true;
const_item_cache=0;
table= 0;
for (uint i=1 ; i < arg_count ; i++)
{
item= args[i]= args[i]->real_item();
/*
When running in PS mode, some Item_field's can already be replaced
to Item_func_conv_charset during PREPARE time. This is possible
in case of "MATCH (f1,..,fN) AGAINST (... IN BOOLEAN MODE)"
when running without any fulltext indexes and when fields f1..fN
have different character sets.
So we check for FIELD_ITEM only during prepare time and in non-PS mode,
and do not check in PS execute time.
*/
if (!thd->stmt_arena->is_stmt_execute() &&
item->type() != Item::FIELD_ITEM)
{
my_error(ER_WRONG_ARGUMENTS, MYF(0), "MATCH");
return TRUE;
}
/*
During the prepare-time execution of fix_fields() of a PS query some
Item_fields's could have been already replaced to Item_func_conv_charset
(by the call for agg_arg_charsets_for_comparison below()).
But agg_arg_charsets_for_comparison() is written in a way that
at least *one* of the Item_field's is not replaced.
This makes sure that "table" gets initialized during PS execution time.
*/
if (item->type() == Item::FIELD_ITEM)
table= ((Item_field *)item)->field->table;
allows_multi_table_search &= allows_search_on_non_indexed_columns(table);
}
/*
Check that all columns come from the same table.
We've already checked that columns in MATCH are fields so
PARAM_TABLE_BIT can only appear from AGAINST argument.
*/
if ((used_tables_cache & ~PARAM_TABLE_BIT) != item->used_tables())
key=NO_SUCH_KEY;
if (key == NO_SUCH_KEY && !allows_multi_table_search)
{
my_error(ER_WRONG_ARGUMENTS,MYF(0),"MATCH");
return TRUE;
}
if (!(table->file->ha_table_flags() & HA_CAN_FULLTEXT))
{
my_error(ER_TABLE_CANT_HANDLE_FT, MYF(0), table->file->table_type());
return 1;
}
table->fulltext_searched=1;
return agg_arg_charsets_for_comparison(cmp_collation, args+1, arg_count-1);
}
bool Item_func_match::fix_index()
{
Item_field *item;
uint ft_to_key[MAX_KEY], ft_cnt[MAX_KEY], fts=0, keynr;
uint max_cnt=0, mkeys=0, i;
/*
We will skip execution if the item is not fixed
with fix_field
*/
if (!fixed)
return false;
if (key == NO_SUCH_KEY)
return 0;
if (!table)
goto err;
for (keynr=0 ; keynr < table->s->keys ; keynr++)
{
if ((table->key_info[keynr].flags & HA_FULLTEXT) &&
(flags & FT_BOOL ? table->keys_in_use_for_query.is_set(keynr) :
table->s->keys_in_use.is_set(keynr)))
{
ft_to_key[fts]=keynr;
ft_cnt[fts]=0;
fts++;
}
}
if (!fts)
goto err;
for (i=1; i < arg_count; i++)
{
item=(Item_field*)args[i];
for (keynr=0 ; keynr < fts ; keynr++)
{
KEY *ft_key=&table->key_info[ft_to_key[keynr]];
uint key_parts=ft_key->user_defined_key_parts;
for (uint part=0 ; part < key_parts ; part++)
{
if (item->field->eq(ft_key->key_part[part].field))
ft_cnt[keynr]++;
}
}
}
for (keynr=0 ; keynr < fts ; keynr++)
{
if (ft_cnt[keynr] > max_cnt)
{
mkeys=0;
max_cnt=ft_cnt[mkeys]=ft_cnt[keynr];
ft_to_key[mkeys]=ft_to_key[keynr];
continue;
}
if (max_cnt && ft_cnt[keynr] == max_cnt)
{
mkeys++;
ft_cnt[mkeys]=ft_cnt[keynr];
ft_to_key[mkeys]=ft_to_key[keynr];
continue;
}
}
for (keynr=0 ; keynr <= mkeys ; keynr++)
{
// partial keys doesn't work
if (max_cnt < arg_count-1 ||
max_cnt < table->key_info[ft_to_key[keynr]].user_defined_key_parts)
continue;
key=ft_to_key[keynr];
return 0;
}
err:
if (allows_search_on_non_indexed_columns(table))
{
key=NO_SUCH_KEY;
return 0;
}
my_message(ER_FT_MATCHING_KEY_NOT_FOUND,
ER(ER_FT_MATCHING_KEY_NOT_FOUND), MYF(0));
return 1;
}
bool Item_func_match::eq(const Item *item, bool binary_cmp) const
{
if (item->type() != FUNC_ITEM ||
((Item_func*)item)->functype() != FT_FUNC ||
flags != ((Item_func_match*)item)->flags)
return 0;
Item_func_match *ifm=(Item_func_match*) item;
if (key == ifm->key && table == ifm->table &&
key_item()->eq(ifm->key_item(), binary_cmp))
return 1;
return 0;
}
double Item_func_match::val_real()
{
DBUG_ASSERT(fixed == 1);
DBUG_ENTER("Item_func_match::val");
if (ft_handler == NULL)
DBUG_RETURN(-1.0);
if (key != NO_SUCH_KEY && table->null_row) /* NULL row from an outer join */
DBUG_RETURN(0.0);
if (join_key)
{
if (table->file->ft_handler)
DBUG_RETURN(ft_handler->please->get_relevance(ft_handler));
join_key=0;
}
if (key == NO_SUCH_KEY)
{
String *a= concat_ws->val_str(&value);
if ((null_value= (a == 0)) || !a->length())
DBUG_RETURN(0);
DBUG_RETURN(ft_handler->please->find_relevance(ft_handler,
(uchar *)a->ptr(), a->length()));
}
DBUG_RETURN(ft_handler->please->find_relevance(ft_handler,
table->record[0], 0));
}
void Item_func_match::print(String *str, enum_query_type query_type)
{
str->append(STRING_WITH_LEN("(match "));
print_args(str, 1, query_type);
str->append(STRING_WITH_LEN(" against ("));
args[0]->print(str, query_type);
if (flags & FT_BOOL)
str->append(STRING_WITH_LEN(" in boolean mode"));
else if (flags & FT_EXPAND)
str->append(STRING_WITH_LEN(" with query expansion"));
str->append(STRING_WITH_LEN("))"));
}
longlong Item_func_bit_xor::val_int()
{
DBUG_ASSERT(fixed == 1);
ulonglong arg1= (ulonglong) args[0]->val_int();
ulonglong arg2= (ulonglong) args[1]->val_int();
if ((null_value= (args[0]->null_value || args[1]->null_value)))
return 0;
return (longlong) (arg1 ^ arg2);
}
/***************************************************************************
System variables
****************************************************************************/
/**
Return value of an system variable base[.name] as a constant item.
@param thd Thread handler
@param var_type global / session
@param name Name of base or system variable
@param component Component.
@note
If component.str = 0 then the variable name is in 'name'
@return
- 0 : error
- # : constant item
*/
Item *get_system_var(THD *thd, enum_var_type var_type, LEX_STRING name,
LEX_STRING component)
{
sys_var *var;
LEX_STRING *base_name, *component_name;
if (component.str)
{
base_name= &component;
component_name= &name;
}
else
{
base_name= &name;
component_name= &component; // Empty string
}
if (!(var= find_sys_var(thd, base_name->str, base_name->length)))
return 0;
if (component.str)
{
if (!var->is_struct())
{
my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), base_name->str);
return 0;
}
}
thd->lex->uncacheable(UNCACHEABLE_SIDEEFFECT);
set_if_smaller(component_name->length, MAX_SYS_VAR_LENGTH);
return new (thd->mem_root) Item_func_get_system_var(thd, var, var_type, component_name,
NULL, 0);
}
longlong Item_func_row_count::val_int()
{
DBUG_ASSERT(fixed == 1);
THD *thd= current_thd;
return thd->get_row_count_func();
}
Item_func_sp::Item_func_sp(THD *thd, Name_resolution_context *context_arg,
sp_name *name):
Item_func(thd), context(context_arg), m_name(name), m_sp(NULL), sp_result_field(NULL)
{
maybe_null= 1;
m_name->init_qname(thd);
dummy_table= (TABLE*) thd->calloc(sizeof(TABLE)+ sizeof(TABLE_SHARE));
dummy_table->s= (TABLE_SHARE*) (dummy_table+1);
}
Item_func_sp::Item_func_sp(THD *thd, Name_resolution_context *context_arg,
sp_name *name_arg, List<Item> &list):
Item_func(thd, list), context(context_arg), m_name(name_arg), m_sp(NULL),
sp_result_field(NULL)
{
maybe_null= 1;
m_name->init_qname(thd);
dummy_table= (TABLE*) thd->calloc(sizeof(TABLE)+ sizeof(TABLE_SHARE));
dummy_table->s= (TABLE_SHARE*) (dummy_table+1);
}
void
Item_func_sp::cleanup()
{
if (sp_result_field)
{
delete sp_result_field;
sp_result_field= NULL;
}
m_sp= NULL;
dummy_table->alias.free();
Item_func::cleanup();
}
const char *
Item_func_sp::func_name() const
{
THD *thd= current_thd;
/* Calculate length to avoid reallocation of string for sure */
uint len= (((m_name->m_explicit_name ? m_name->m_db.length : 0) +
m_name->m_name.length)*2 + //characters*quoting
2 + // ` and `
(m_name->m_explicit_name ?
3 : 0) + // '`', '`' and '.' for the db
1 + // end of string
ALIGN_SIZE(1)); // to avoid String reallocation
String qname((char *)alloc_root(thd->mem_root, len), len,
system_charset_info);
qname.length(0);
if (m_name->m_explicit_name)
{
append_identifier(thd, &qname, m_name->m_db.str, m_name->m_db.length);
qname.append('.');
}
append_identifier(thd, &qname, m_name->m_name.str, m_name->m_name.length);
return qname.c_ptr_safe();
}
void my_missing_function_error(const LEX_STRING &token, const char *func_name)
{
if (token.length && is_lex_native_function (&token))
my_error(ER_FUNC_INEXISTENT_NAME_COLLISION, MYF(0), func_name);
else
my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "FUNCTION", func_name);
}
/**
@brief Initialize the result field by creating a temporary dummy table
and assign it to a newly created field object. Meta data used to
create the field is fetched from the sp_head belonging to the stored
proceedure found in the stored procedure functon cache.
@note This function should be called from fix_fields to init the result
field. It is some what related to Item_field.
@see Item_field
@param thd A pointer to the session and thread context.
@return Function return error status.
@retval TRUE is returned on an error
@retval FALSE is returned on success.
*/
bool
Item_func_sp::init_result_field(THD *thd)
{
LEX_STRING empty_name= { C_STRING_WITH_LEN("") };
TABLE_SHARE *share;
DBUG_ENTER("Item_func_sp::init_result_field");
DBUG_ASSERT(m_sp == NULL);
DBUG_ASSERT(sp_result_field == NULL);
if (!(m_sp= sp_find_routine(thd, TYPE_ENUM_FUNCTION, m_name,
&thd->sp_func_cache, TRUE)))
{
my_missing_function_error (m_name->m_name, m_name->m_qname.str);
context->process_error(thd);
DBUG_RETURN(TRUE);
}
/*
A Field need to be attached to a Table.
Below we "create" a dummy table by initializing
the needed pointers.
*/
share= dummy_table->s;
dummy_table->alias.set("", 0, table_alias_charset);
dummy_table->maybe_null = maybe_null;
dummy_table->in_use= thd;
dummy_table->copy_blobs= TRUE;
share->table_cache_key = empty_name;
share->table_name = empty_name;
if (!(sp_result_field= m_sp->create_result_field(max_length, name,
dummy_table)))
{
DBUG_RETURN(TRUE);
}
if (sp_result_field->pack_length() > sizeof(result_buf))
{
void *tmp;
if (!(tmp= thd->alloc(sp_result_field->pack_length())))
DBUG_RETURN(TRUE);
sp_result_field->move_field((uchar*) tmp);
}
else
sp_result_field->move_field(result_buf);
sp_result_field->null_ptr= (uchar *) &null_value;
sp_result_field->null_bit= 1;
DBUG_RETURN(FALSE);
}
/**
@note
Deterministic stored procedures are considered inexpensive.
Consequently such procedures may be evaluated during optimization,
if they are constant (checked by the optimizer).
*/
bool Item_func_sp::is_expensive()
{
return !(m_sp->m_chistics->detistic);
}
/**
@brief Initialize local members with values from the Field interface.
@note called from Item::fix_fields.
*/
void Item_func_sp::fix_length_and_dec()
{
DBUG_ENTER("Item_func_sp::fix_length_and_dec");
DBUG_ASSERT(sp_result_field);
Type_std_attributes::set(sp_result_field);
maybe_null= 1;
DBUG_VOID_RETURN;
}
/**
@brief Execute function & store value in field.
@return Function returns error status.
@retval FALSE on success.
@retval TRUE if an error occurred.
*/
bool
Item_func_sp::execute()
{
THD *thd= current_thd;
/* Execute function and store the return value in the field. */
if (execute_impl(thd))
{
null_value= 1;
context->process_error(thd);
if (thd->killed)
thd->send_kill_message();
return TRUE;
}
/* Check that the field (the value) is not NULL. */
null_value= sp_result_field->is_null();
return null_value;
}
/**
@brief Execute function and store the return value in the field.
@note This function was intended to be the concrete implementation of
the interface function execute. This was never realized.
@return The error state.
@retval FALSE on success
@retval TRUE if an error occurred.
*/
bool
Item_func_sp::execute_impl(THD *thd)
{
bool err_status= TRUE;
Sub_statement_state statement_state;
Security_context *save_security_ctx= thd->security_ctx;
enum enum_sp_data_access access=
(m_sp->m_chistics->daccess == SP_DEFAULT_ACCESS) ?
SP_DEFAULT_ACCESS_MAPPING : m_sp->m_chistics->daccess;
DBUG_ENTER("Item_func_sp::execute_impl");
if (context->security_ctx)
{
/* Set view definer security context */
thd->security_ctx= context->security_ctx;
}
if (sp_check_access(thd))
goto error;
/*
Throw an error if a non-deterministic function is called while
statement-based replication (SBR) is active.
*/
if (!m_sp->m_chistics->detistic && !trust_function_creators &&
(access == SP_CONTAINS_SQL || access == SP_MODIFIES_SQL_DATA) &&
(mysql_bin_log.is_open() &&
thd->variables.binlog_format == BINLOG_FORMAT_STMT))
{
my_error(ER_BINLOG_UNSAFE_ROUTINE, MYF(0));
goto error;
}
/*
Disable the binlogging if this is not a SELECT statement. If this is a
SELECT, leave binlogging on, so execute_function() code writes the
function call into binlog.
*/
thd->reset_sub_statement_state(&statement_state, SUB_STMT_FUNCTION);
err_status= m_sp->execute_function(thd, args, arg_count, sp_result_field);
thd->restore_sub_statement_state(&statement_state);
error:
thd->security_ctx= save_security_ctx;
DBUG_RETURN(err_status);
}
void
Item_func_sp::make_field(Send_field *tmp_field)
{
DBUG_ENTER("Item_func_sp::make_field");
DBUG_ASSERT(sp_result_field);
sp_result_field->make_field(tmp_field);
if (name)
tmp_field->col_name= name;
DBUG_VOID_RETURN;
}
enum enum_field_types
Item_func_sp::field_type() const
{
DBUG_ENTER("Item_func_sp::field_type");
DBUG_ASSERT(sp_result_field);
DBUG_RETURN(sp_result_field->type());
}
Item_result
Item_func_sp::result_type() const
{
DBUG_ENTER("Item_func_sp::result_type");
DBUG_PRINT("info", ("m_sp = %p", (void *) m_sp));
DBUG_ASSERT(sp_result_field);
DBUG_RETURN(sp_result_field->result_type());
}
longlong Item_func_found_rows::val_int()
{
DBUG_ASSERT(fixed == 1);
return current_thd->found_rows();
}
Field *
Item_func_sp::tmp_table_field(TABLE *t_arg)
{
DBUG_ENTER("Item_func_sp::tmp_table_field");
DBUG_ASSERT(sp_result_field);
DBUG_RETURN(sp_result_field);
}
/**
@brief Checks if requested access to function can be granted to user.
If function isn't found yet, it searches function first.
If function can't be found or user don't have requested access
error is raised.
@param thd thread handler
@return Indication if the access was granted or not.
@retval FALSE Access is granted.
@retval TRUE Requested access can't be granted or function doesn't exists.
*/
bool
Item_func_sp::sp_check_access(THD *thd)
{
DBUG_ENTER("Item_func_sp::sp_check_access");
DBUG_ASSERT(m_sp);
if (check_routine_access(thd, EXECUTE_ACL,
m_sp->m_db.str, m_sp->m_name.str, 0, FALSE))
DBUG_RETURN(TRUE);
DBUG_RETURN(FALSE);
}
bool
Item_func_sp::fix_fields(THD *thd, Item **ref)
{
bool res;
DBUG_ENTER("Item_func_sp::fix_fields");
DBUG_ASSERT(fixed == 0);
/*
Checking privileges to execute the function while creating view and
executing the function of select.
*/
if (!(thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW) ||
(thd->lex->sql_command == SQLCOM_CREATE_VIEW))
{
Security_context *save_security_ctx= thd->security_ctx;
if (context->security_ctx)
thd->security_ctx= context->security_ctx;
res= check_routine_access(thd, EXECUTE_ACL, m_name->m_db.str,
m_name->m_name.str, 0, FALSE);
thd->security_ctx= save_security_ctx;
if (res)
{
context->process_error(thd);
DBUG_RETURN(res);
}
}
/*
We must call init_result_field before Item_func::fix_fields()
to make m_sp and result_field members available to fix_length_and_dec(),
which is called from Item_func::fix_fields().
*/
res= init_result_field(thd);
if (res)
DBUG_RETURN(res);
res= Item_func::fix_fields(thd, ref);
if (res)
DBUG_RETURN(res);
if (thd->lex->is_view_context_analysis())
{
/*
Here we check privileges of the stored routine only during view
creation, in order to validate the view. A runtime check is
perfomed in Item_func_sp::execute(), and this method is not
called during context analysis. Notice, that during view
creation we do not infer into stored routine bodies and do not
check privileges of its statements, which would probably be a
good idea especially if the view has SQL SECURITY DEFINER and
the used stored procedure has SQL SECURITY DEFINER.
*/
res= sp_check_access(thd);
#ifndef NO_EMBEDDED_ACCESS_CHECKS
/*
Try to set and restore the security context to see whether it's valid
*/
Security_context *save_secutiry_ctx;
res= set_routine_security_ctx(thd, m_sp, false, &save_secutiry_ctx);
if (!res)
m_sp->m_security_ctx.restore_security_context(thd, save_secutiry_ctx);
#endif /* ! NO_EMBEDDED_ACCESS_CHECKS */
}
if (!m_sp->m_chistics->detistic)
{
used_tables_cache |= RAND_TABLE_BIT;
const_item_cache= FALSE;
}
DBUG_RETURN(res);
}
void Item_func_sp::update_used_tables()
{
Item_func::update_used_tables();
if (!m_sp->m_chistics->detistic)
{
used_tables_cache |= RAND_TABLE_BIT;
const_item_cache= FALSE;
}
}
/*
uuid_short handling.
The short uuid is defined as a longlong that contains the following bytes:
Bytes Comment
1 Server_id & 255
4 Startup time of server in seconds
3 Incrementor
This means that an uuid is guaranteed to be unique
even in a replication environment if the following holds:
- The last byte of the server id is unique
- If you between two shutdown of the server don't get more than
an average of 2^24 = 16M calls to uuid_short() per second.
*/
ulonglong uuid_value;
void uuid_short_init()
{
uuid_value= ((((ulonglong) global_system_variables.server_id) << 56) +
(((ulonglong) server_start_time) << 24));
}
longlong Item_func_uuid_short::val_int()
{
ulonglong val;
mysql_mutex_lock(&LOCK_short_uuid_generator);
val= uuid_value++;
mysql_mutex_unlock(&LOCK_short_uuid_generator);
return (longlong) val;
}
/**
Last_value - return last argument.
*/
void Item_func_last_value::evaluate_sideeffects()
{
DBUG_ASSERT(fixed == 1 && arg_count > 0);
for (uint i= 0; i < arg_count-1 ; i++)
args[i]->val_int();
}
String *Item_func_last_value::val_str(String *str)
{
String *tmp;
evaluate_sideeffects();
tmp= last_value->val_str(str);
null_value= last_value->null_value;
return tmp;
}
longlong Item_func_last_value::val_int()
{
longlong tmp;
evaluate_sideeffects();
tmp= last_value->val_int();
null_value= last_value->null_value;
return tmp;
}
double Item_func_last_value::val_real()
{
double tmp;
evaluate_sideeffects();
tmp= last_value->val_real();
null_value= last_value->null_value;
return tmp;
}
my_decimal *Item_func_last_value::val_decimal(my_decimal *decimal_value)
{
my_decimal *tmp;
evaluate_sideeffects();
tmp= last_value->val_decimal(decimal_value);
null_value= last_value->null_value;
return tmp;
}
void Item_func_last_value::fix_length_and_dec()
{
last_value= args[arg_count -1];
Type_std_attributes::set(last_value);
maybe_null= last_value->maybe_null;
}
|