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
|
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
*
* Copyright (C) 2008 Sun Microsystems
*
* 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
*/
#include <drizzled/server_includes.h>
#include CMATH_H
#include <drizzled/sql_select.h>
#include <drizzled/error.h>
#include <drizzled/show.h>
#include <drizzled/item/cmpfunc.h>
#include <drizzled/item/cache_row.h>
#include <drizzled/item/type_holder.h>
#include <drizzled/item/sum.h>
#include <drizzled/functions/str/conv_charset.h>
#include <drizzled/virtual_column_info.h>
#include <drizzled/sql_base.h>
#include <drizzled/field/str.h>
#include <drizzled/field/longstr.h>
#include <drizzled/field/num.h>
#include <drizzled/field/blob.h>
#include <drizzled/field/enum.h>
#include <drizzled/field/null.h>
#include <drizzled/field/date.h>
#include <drizzled/field/fdecimal.h>
#include <drizzled/field/real.h>
#include <drizzled/field/double.h>
#include <drizzled/field/long.h>
#include <drizzled/field/int64_t.h>
#include <drizzled/field/num.h>
#include <drizzled/field/timetype.h>
#include <drizzled/field/timestamp.h>
#include <drizzled/field/datetime.h>
#include <drizzled/field/varstring.h>
#if defined(CMATH_NAMESPACE)
using namespace CMATH_NAMESPACE;
#endif
const String my_null_string("NULL", 4, default_charset_info);
/*****************************************************************************
** Item functions
*****************************************************************************/
/**
Init all special items.
*/
void item_init(void)
{
}
bool Item::is_expensive_processor(unsigned char *)
{
return 0;
}
void Item::fix_after_pullout(st_select_lex *, Item **)
{}
Field *Item::tmp_table_field(Table *)
{
return 0;
}
const char *Item::full_name(void) const
{
return name ? name : "???";
}
int64_t Item::val_int_endpoint(bool, bool *)
{
assert(0);
return 0;
}
/**
@todo
Make this functions class dependent
*/
bool Item::val_bool()
{
switch(result_type()) {
case INT_RESULT:
return val_int() != 0;
case DECIMAL_RESULT:
{
my_decimal decimal_value;
my_decimal *val= val_decimal(&decimal_value);
if (val)
return !my_decimal_is_zero(val);
return 0;
}
case REAL_RESULT:
case STRING_RESULT:
return val_real() != 0.0;
case ROW_RESULT:
default:
assert(0);
return 0; // Wrong (but safe)
}
}
String *Item::val_string_from_real(String *str)
{
double nr= val_real();
if (null_value)
return 0; /* purecov: inspected */
str->set_real(nr,decimals, &my_charset_bin);
return str;
}
String *Item::val_string_from_int(String *str)
{
int64_t nr= val_int();
if (null_value)
return 0;
str->set_int(nr, unsigned_flag, &my_charset_bin);
return str;
}
String *Item::val_string_from_decimal(String *str)
{
my_decimal dec_buf, *dec= val_decimal(&dec_buf);
if (null_value)
return 0;
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;
}
my_decimal *Item::val_decimal_from_real(my_decimal *decimal_value)
{
double nr= val_real();
if (null_value)
return 0;
double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
return (decimal_value);
}
my_decimal *Item::val_decimal_from_int(my_decimal *decimal_value)
{
int64_t nr= val_int();
if (null_value)
return 0;
int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
return decimal_value;
}
my_decimal *Item::val_decimal_from_string(my_decimal *decimal_value)
{
String *res;
char *end_ptr;
if (!(res= val_str(&str_value)))
return 0; // NULL or EOM
end_ptr= (char*) res->ptr()+ res->length();
if (str2my_decimal(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
res->ptr(), res->length(), res->charset(),
decimal_value) & E_DEC_BAD_NUM)
{
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
ER_TRUNCATED_WRONG_VALUE,
ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
str_value.c_ptr());
}
return decimal_value;
}
my_decimal *Item::val_decimal_from_date(my_decimal *decimal_value)
{
assert(fixed == 1);
DRIZZLE_TIME ltime;
if (get_date(<ime, TIME_FUZZY_DATE))
{
my_decimal_set_zero(decimal_value);
null_value= 1; // set NULL, stop processing
return 0;
}
return date2my_decimal(<ime, decimal_value);
}
my_decimal *Item::val_decimal_from_time(my_decimal *decimal_value)
{
assert(fixed == 1);
DRIZZLE_TIME ltime;
if (get_time(<ime))
{
my_decimal_set_zero(decimal_value);
return 0;
}
return date2my_decimal(<ime, decimal_value);
}
double Item::val_real_from_decimal()
{
/* Note that fix_fields may not be called for Item_avg_field items */
double result;
my_decimal value_buff, *dec_val= val_decimal(&value_buff);
if (null_value)
return 0.0;
my_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
return result;
}
int64_t Item::val_int_from_decimal()
{
/* Note that fix_fields may not be called for Item_avg_field items */
int64_t result;
my_decimal value, *dec_val= val_decimal(&value);
if (null_value)
return 0;
my_decimal2int(E_DEC_FATAL_ERROR, dec_val, unsigned_flag, &result);
return result;
}
int Item::save_time_in_field(Field *field)
{
DRIZZLE_TIME ltime;
if (get_time(<ime))
return set_field_to_null(field);
field->set_notnull();
return field->store_time(<ime, DRIZZLE_TIMESTAMP_TIME);
}
int Item::save_date_in_field(Field *field)
{
DRIZZLE_TIME ltime;
if (get_date(<ime, TIME_FUZZY_DATE))
return set_field_to_null(field);
field->set_notnull();
return field->store_time(<ime, DRIZZLE_TIMESTAMP_DATETIME);
}
/*
Store the string value in field directly
SYNOPSIS
Item::save_str_value_in_field()
field a pointer to field where to store
result the pointer to the string value to be stored
DESCRIPTION
The method is used by Item_*::save_in_field implementations
when we don't need to calculate the value to store
See Item_string::save_in_field() implementation for example
IMPLEMENTATION
Check if the Item is null and stores the NULL or the
result value in the field accordingly.
RETURN
Nonzero value if error
*/
int Item::save_str_value_in_field(Field *field, String *result)
{
if (null_value)
return set_field_to_null(field);
field->set_notnull();
return field->store(result->ptr(), result->length(),
collation.collation);
}
Item::Item():
is_expensive_cache(-1), name(0), orig_name(0), name_length(0),
fixed(0), is_autogenerated_name(true),
collation(&my_charset_bin, DERIVATION_COERCIBLE)
{
marker= 0;
maybe_null= false;
null_value= false;
with_sum_func= false;
unsigned_flag= false;
decimals= 0;
max_length= 0;
with_subselect= 0;
cmp_context= (Item_result)-1;
/* Put item in free list so that we can free all items at end */
Session *session= current_session;
next= session->free_list;
session->free_list= this;
/*
Item constructor can be called during execution other then SQL_COM
command => we should check session->lex->current_select on zero (session->lex
can be uninitialised)
*/
if (session->lex->current_select)
{
enum_parsing_place place=
session->lex->current_select->parsing_place;
if (place == SELECT_LIST ||
place == IN_HAVING)
session->lex->current_select->select_n_having_items++;
}
}
/**
Constructor used by Item_field, Item_ref & aggregate (sum)
functions.
Used for duplicating lists in processing queries with temporary
tables.
*/
Item::Item(Session *session, Item *item):
is_expensive_cache(-1),
str_value(item->str_value),
name(item->name),
orig_name(item->orig_name),
max_length(item->max_length),
marker(item->marker),
decimals(item->decimals),
maybe_null(item->maybe_null),
null_value(item->null_value),
unsigned_flag(item->unsigned_flag),
with_sum_func(item->with_sum_func),
fixed(item->fixed),
collation(item->collation),
cmp_context(item->cmp_context)
{
next= session->free_list; // Put in free list
session->free_list= this;
}
uint32_t Item::decimal_precision() const
{
Item_result restype= result_type();
if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
return cmin(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
(unsigned int)DECIMAL_MAX_PRECISION);
return cmin(max_length, (uint32_t)DECIMAL_MAX_PRECISION);
}
int Item::decimal_int_part() const
{
return my_decimal_int_part(decimal_precision(), decimals);
}
void Item::print(String *str, enum_query_type)
{
str->append(full_name());
}
void Item::print_item_w_name(String *str, enum_query_type query_type)
{
print(str, query_type);
if (name)
{
Session *session= current_session;
str->append(STRING_WITH_LEN(" AS "));
append_identifier(session, str, name, (uint) strlen(name));
}
}
void Item::split_sum_func(Session *, Item **, List<Item> &)
{}
void Item::cleanup()
{
fixed=0;
marker= 0;
if (orig_name)
name= orig_name;
return;
}
/**
cleanup() item if it is 'fixed'.
@param arg a dummy parameter, is not used here
*/
bool Item::cleanup_processor(unsigned char *)
{
if (fixed)
cleanup();
return false;
}
/**
rename item (used for views, cleanup() return original name).
@param new_name new name of item;
*/
void Item::rename(char *new_name)
{
/*
we can compare pointers to names here, because if name was not changed,
pointer will be same
*/
if (!orig_name && new_name != name)
orig_name= name;
name= new_name;
}
/**
Traverse item tree possibly transforming it (replacing items).
This function is designed to ease transformation of Item trees.
Re-execution note: every such transformation is registered for
rollback by Session::change_item_tree() and is rolled back at the end
of execution by Session::rollback_item_tree_changes().
Therefore:
- this function can not be used at prepared statement prepare
(in particular, in fix_fields!), as only permanent
transformation of Item trees are allowed at prepare.
- the transformer function shall allocate new Items in execution
memory root (session->mem_root) and not anywhere else: allocated
items will be gone in the end of execution.
If you don't need to transform an item tree, but only traverse
it, please use Item::walk() instead.
@param transformer functor that performs transformation of a subtree
@param arg opaque argument passed to the functor
@return
Returns pointer to the new subtree root. Session::change_item_tree()
should be called for it if transformation took place, i.e. if a
pointer to newly allocated item is returned.
*/
Item* Item::transform(Item_transformer transformer, unsigned char *arg)
{
return (this->*transformer)(arg);
}
bool Item::check_cols(uint32_t c)
{
if (c != 1)
{
my_error(ER_OPERAND_COLUMNS, MYF(0), c);
return 1;
}
return 0;
}
void Item::set_name(const char *str, uint32_t length, const CHARSET_INFO * const cs)
{
if (!length)
{
/* Empty string, used by AS or internal function like last_insert_id() */
name= (char*) str;
name_length= 0;
return;
}
if (cs->ctype)
{
uint32_t orig_len= length;
/*
This will probably need a better implementation in the future:
a function in CHARSET_INFO structure.
*/
while (length && !my_isgraph(cs,*str))
{ // Fix problem with yacc
length--;
str++;
}
if (orig_len != length && !is_autogenerated_name)
{
if (length == 0)
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
ER_NAME_BECOMES_EMPTY, ER(ER_NAME_BECOMES_EMPTY),
str + length - orig_len);
else
push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES),
str + length - orig_len);
}
}
if (!my_charset_same(cs, system_charset_info))
{
size_t res_length;
name= sql_strmake_with_convert(str, name_length= length, cs,
MAX_ALIAS_NAME, system_charset_info,
&res_length);
}
else
name= sql_strmake(str, (name_length= cmin(length,(unsigned int)MAX_ALIAS_NAME)));
}
/**
@details
This function is called when:
- Comparing items in the WHERE clause (when doing where optimization)
- When trying to find an order_st BY/GROUP BY item in the SELECT part
*/
bool Item::eq(const Item *item, bool) const
{
/*
Note, that this is never true if item is a Item_param:
for all basic constants we have special checks, and Item_param's
type() can be only among basic constant types.
*/
return type() == item->type() && name && item->name &&
!my_strcasecmp(system_charset_info,name,item->name);
}
Item *Item::safe_charset_converter(const CHARSET_INFO * const tocs)
{
Item_func_conv_charset *conv= new Item_func_conv_charset(this, tocs, 1);
return conv->safe ? conv : NULL;
}
/**
Get the value of the function as a DRIZZLE_TIME structure.
As a extra convenience the time structure is reset on error!
*/
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
{
if (result_type() == STRING_RESULT)
{
char buff[40];
String tmp(buff,sizeof(buff), &my_charset_bin),*res;
if (!(res=val_str(&tmp)) ||
str_to_datetime_with_warn(res->ptr(), res->length(),
ltime, fuzzydate) <= DRIZZLE_TIMESTAMP_ERROR)
goto err;
}
else
{
int64_t value= val_int();
int was_cut;
if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
{
char buff[22], *end;
end= int64_t10_to_str(value, buff, -10);
make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
NULL);
goto err;
}
}
return 0;
err:
memset(ltime, 0, sizeof(*ltime));
return 1;
}
/**
Get time of first argument.\
As a extra convenience the time structure is reset on error!
*/
bool Item::get_time(DRIZZLE_TIME *ltime)
{
char buff[40];
String tmp(buff,sizeof(buff),&my_charset_bin),*res;
if (!(res=val_str(&tmp)) ||
str_to_time_with_warn(res->ptr(), res->length(), ltime))
{
memset(ltime, 0, sizeof(*ltime));
return true;
}
return false;
}
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
{
return get_date(ltime,fuzzydate);
}
bool Item::is_null()
{
return false;
}
void Item::update_null_value ()
{
(void) val_int();
}
void Item::top_level_item(void)
{}
void Item::set_result_field(Field *)
{}
bool Item::is_result_field(void)
{
return 0;
}
bool Item::is_bool_func(void)
{
return 0;
}
void Item::save_in_result_field(bool)
{}
void Item::no_rows_in_result(void)
{}
Item *Item::copy_or_same(Session *)
{
return this;
}
Item *Item::copy_andor_structure(Session *)
{
return this;
}
Item *Item::real_item(void)
{
return this;
}
Item *Item::get_tmp_table_item(Session *session)
{
return copy_or_same(session);
}
const CHARSET_INFO *Item::default_charset()
{
return current_session->variables.collation_connection;
}
const CHARSET_INFO *Item::compare_collation()
{
return NULL;
}
bool Item::walk(Item_processor processor, bool, unsigned char *arg)
{
return (this->*processor)(arg);
}
Item* Item::compile(Item_analyzer analyzer, unsigned char **arg_p,
Item_transformer transformer, unsigned char *arg_t)
{
if ((this->*analyzer) (arg_p))
return ((this->*transformer) (arg_t));
return 0;
}
void Item::traverse_cond(Cond_traverser traverser, void *arg, traverse_order)
{
(*traverser)(this, arg);
}
bool Item::remove_dependence_processor(unsigned char *)
{
return 0;
}
bool Item::remove_fixed(unsigned char *)
{
fixed= 0;
return 0;
}
bool Item::collect_item_field_processor(unsigned char *)
{
return 0;
}
bool Item::find_item_in_field_list_processor(unsigned char *)
{
return 0;
}
bool Item::change_context_processor(unsigned char *)
{
return 0;
}
bool Item::reset_query_id_processor(unsigned char *)
{
return 0;
}
bool Item::register_field_in_read_map(unsigned char *)
{
return 0;
}
bool Item::register_field_in_bitmap(unsigned char *)
{
return 0;
}
bool Item::subst_argument_checker(unsigned char **arg)
{
if (*arg)
*arg= NULL;
return true;
}
bool Item::check_vcol_func_processor(unsigned char *)
{
return true;
}
Item *Item::equal_fields_propagator(unsigned char *)
{
return this;
}
bool Item::set_no_const_sub(unsigned char *)
{
return false;
}
Item *Item::replace_equal_field(unsigned char *)
{
return this;
}
uint32_t Item::cols()
{
return 1;
}
Item* Item::element_index(uint32_t)
{
return this;
}
Item** Item::addr(uint32_t)
{
return 0;
}
bool Item::null_inside()
{
return 0;
}
void Item::bring_value()
{}
Item *Item::neg_transformer(Session *)
{
return NULL;
}
Item *Item::update_value_transformer(unsigned char *)
{
return this;
}
void Item::delete_self()
{
cleanup();
delete this;
}
bool Item::result_as_int64_t()
{
return false;
}
bool Item::is_expensive()
{
if (is_expensive_cache < 0)
is_expensive_cache= walk(&Item::is_expensive_processor, 0,
(unsigned char*)0);
return test(is_expensive_cache);
}
int Item::save_in_field_no_warnings(Field *field, bool no_conversions)
{
int res;
Table *table= field->table;
Session *session= table->in_use;
enum_check_fields tmp= session->count_cuted_fields;
ulong sql_mode= session->variables.sql_mode;
session->variables.sql_mode&= ~(MODE_NO_ZERO_DATE);
session->count_cuted_fields= CHECK_FIELD_IGNORE;
res= save_in_field(field, no_conversions);
session->count_cuted_fields= tmp;
session->variables.sql_mode= sql_mode;
return res;
}
/*
need a special class to adjust printing : references to aggregate functions
must not be printed as refs because the aggregate functions that are added to
the front of select list are not printed as well.
*/
class Item_aggregate_ref : public Item_ref
{
public:
Item_aggregate_ref(Name_resolution_context *context_arg, Item **item,
const char *table_name_arg, const char *field_name_arg)
:Item_ref(context_arg, item, table_name_arg, field_name_arg) {}
virtual inline void print (String *str, enum_query_type query_type)
{
if (ref)
(*ref)->print(str, query_type);
else
Item_ident::print(str, query_type);
}
};
/**
Move SUM items out from item tree and replace with reference.
@param session Thread handler
@param ref_pointer_array Pointer to array of reference fields
@param fields All fields in select
@param ref Pointer to item
@param skip_registered <=> function be must skipped for registered
SUM items
@note
This is from split_sum_func() for items that should be split
All found SUM items are added FIRST in the fields list and
we replace the item with a reference.
session->fatal_error() may be called if we are out of memory
*/
void Item::split_sum_func(Session *session, Item **ref_pointer_array,
List<Item> &fields, Item **ref,
bool skip_registered)
{
/* An item of type Item_sum is registered <=> ref_by != 0 */
if (type() == SUM_FUNC_ITEM && skip_registered &&
((Item_sum *) this)->ref_by)
return;
if ((type() != SUM_FUNC_ITEM && with_sum_func) ||
(type() == FUNC_ITEM &&
(((Item_func *) this)->functype() == Item_func::ISNOTNULLTEST_FUNC ||
((Item_func *) this)->functype() == Item_func::TRIG_COND_FUNC)))
{
/* Will split complicated items and ignore simple ones */
split_sum_func(session, ref_pointer_array, fields);
}
else if ((type() == SUM_FUNC_ITEM || (used_tables() & ~PARAM_TABLE_BIT)) &&
type() != SUBSELECT_ITEM &&
type() != REF_ITEM)
{
/*
Replace item with a reference so that we can easily calculate
it (in case of sum functions) or copy it (in case of fields)
The test above is to ensure we don't do a reference for things
that are constants (PARAM_TABLE_BIT is in effect a constant)
or already referenced (for example an item in HAVING)
Exception is Item_direct_view_ref which we need to convert to
Item_ref to allow fields from view being stored in tmp table.
*/
Item_aggregate_ref *item_ref;
uint32_t el= fields.elements;
Item *real_itm= real_item();
ref_pointer_array[el]= real_itm;
if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
ref_pointer_array + el, 0, name)))
return; // fatal_error is set
if (type() == SUM_FUNC_ITEM)
item_ref->depended_from= ((Item_sum *) this)->depended_from();
fields.push_front(real_itm);
session->change_item_tree(ref, item_ref);
}
}
/****************************************************************************
Item_copy_string
****************************************************************************/
void Item_copy_string::copy()
{
String *res=item->val_str(&str_value);
if (res && res != &str_value)
str_value.copy(*res);
null_value=item->null_value;
}
/* ARGSUSED */
String *Item_copy_string::val_str(String *)
{
// Item_copy_string is used without fix_fields call
if (null_value)
return (String*) 0;
return &str_value;
}
my_decimal *Item_copy_string::val_decimal(my_decimal *decimal_value)
{
// Item_copy_string is used without fix_fields call
if (null_value)
return 0;
string2my_decimal(E_DEC_FATAL_ERROR, &str_value, decimal_value);
return (decimal_value);
}
/*
Functions to convert item to field (for send_fields)
*/
/* ARGSUSED */
bool Item::fix_fields(Session *, Item **)
{
// We do not check fields which are fixed during construction
assert(fixed == 0 || basic_const_item());
fixed= 1;
return false;
}
double Item_ref_null_helper::val_real()
{
assert(fixed == 1);
double tmp= (*ref)->val_result();
owner->was_null|= null_value= (*ref)->null_value;
return tmp;
}
int64_t Item_ref_null_helper::val_int()
{
assert(fixed == 1);
int64_t tmp= (*ref)->val_int_result();
owner->was_null|= null_value= (*ref)->null_value;
return tmp;
}
my_decimal *Item_ref_null_helper::val_decimal(my_decimal *decimal_value)
{
assert(fixed == 1);
my_decimal *val= (*ref)->val_decimal_result(decimal_value);
owner->was_null|= null_value= (*ref)->null_value;
return val;
}
bool Item_ref_null_helper::val_bool()
{
assert(fixed == 1);
bool val= (*ref)->val_bool_result();
owner->was_null|= null_value= (*ref)->null_value;
return val;
}
String* Item_ref_null_helper::val_str(String* s)
{
assert(fixed == 1);
String* tmp= (*ref)->str_result(s);
owner->was_null|= null_value= (*ref)->null_value;
return tmp;
}
bool Item_ref_null_helper::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzydate)
{
return (owner->was_null|= null_value= (*ref)->get_date(ltime, fuzzydate));
}
/**
Mark item and SELECT_LEXs as dependent if item was resolved in
outer SELECT.
@param session thread handler
@param last select from which current item depend
@param current current select
@param resolved_item item which was resolved in outer SELECT(for warning)
@param mark_item item which should be marked (can be differ in case of
substitution)
*/
void mark_as_dependent(Session *session, SELECT_LEX *last, SELECT_LEX *current,
Item_ident *resolved_item,
Item_ident *mark_item)
{
const char *db_name= (resolved_item->db_name ?
resolved_item->db_name : "");
const char *table_name= (resolved_item->table_name ?
resolved_item->table_name : "");
/* store pointer on SELECT_LEX from which item is dependent */
if (mark_item)
mark_item->depended_from= last;
current->mark_as_dependent(last);
if (session->lex->describe & DESCRIBE_EXTENDED)
{
char warn_buff[DRIZZLE_ERRMSG_SIZE];
sprintf(warn_buff, ER(ER_WARN_FIELD_RESOLVED),
db_name, (db_name[0] ? "." : ""),
table_name, (table_name [0] ? "." : ""),
resolved_item->field_name,
current->select_number, last->select_number);
push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
ER_WARN_FIELD_RESOLVED, warn_buff);
}
}
/**
Mark range of selects and resolved identifier (field/reference)
item as dependent.
@param session thread handler
@param last_select select where resolved_item was resolved
@param current_sel current select (select where resolved_item was placed)
@param found_field field which was found during resolving
@param found_item Item which was found during resolving (if resolved
identifier belongs to VIEW)
@param resolved_item Identifier which was resolved
@note
We have to mark all items between current_sel (including) and
last_select (excluding) as dependend (select before last_select should
be marked with actual table mask used by resolved item, all other with
OUTER_REF_TABLE_BIT) and also write dependence information to Item of
resolved identifier.
*/
void mark_select_range_as_dependent(Session *session,
SELECT_LEX *last_select,
SELECT_LEX *current_sel,
Field *found_field, Item *found_item,
Item_ident *resolved_item)
{
/*
Go from current SELECT to SELECT where field was resolved (it
have to be reachable from current SELECT, because it was already
done once when we resolved this field and cached result of
resolving)
*/
SELECT_LEX *previous_select= current_sel;
for (; previous_select->outer_select() != last_select;
previous_select= previous_select->outer_select())
{
Item_subselect *prev_subselect_item=
previous_select->master_unit()->item;
prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
prev_subselect_item->const_item_cache= 0;
}
{
Item_subselect *prev_subselect_item=
previous_select->master_unit()->item;
Item_ident *dependent= resolved_item;
if (found_field == view_ref_found)
{
Item::Type type= found_item->type();
prev_subselect_item->used_tables_cache|=
found_item->used_tables();
dependent= ((type == Item::REF_ITEM || type == Item::FIELD_ITEM) ?
(Item_ident*) found_item :
0);
}
else
prev_subselect_item->used_tables_cache|=
found_field->table->map;
prev_subselect_item->const_item_cache= 0;
mark_as_dependent(session, last_select, current_sel, resolved_item,
dependent);
}
}
/**
Search a GROUP BY clause for a field with a certain name.
Search the GROUP BY list for a column named as find_item. When searching
preference is given to columns that are qualified with the same table (and
database) name as the one being searched for.
@param find_item the item being searched for
@param group_list GROUP BY clause
@return
- the found item on success
- NULL if find_item is not in group_list
*/
static Item** find_field_in_group_list(Item *find_item, order_st *group_list)
{
const char *db_name;
const char *table_name;
const char *field_name;
order_st *found_group= NULL;
int found_match_degree= 0;
Item_ident *cur_field;
int cur_match_degree= 0;
char name_buff[NAME_LEN+1];
if (find_item->type() == Item::FIELD_ITEM ||
find_item->type() == Item::REF_ITEM)
{
db_name= ((Item_ident*) find_item)->db_name;
table_name= ((Item_ident*) find_item)->table_name;
field_name= ((Item_ident*) find_item)->field_name;
}
else
return NULL;
if (db_name && lower_case_table_names)
{
/* Convert database to lower case for comparison */
strncpy(name_buff, db_name, sizeof(name_buff)-1);
my_casedn_str(files_charset_info, name_buff);
db_name= name_buff;
}
assert(field_name != 0);
for (order_st *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
{
if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM)
{
cur_field= (Item_ident*) *cur_group->item;
cur_match_degree= 0;
assert(cur_field->field_name != 0);
if (!my_strcasecmp(system_charset_info,
cur_field->field_name, field_name))
++cur_match_degree;
else
continue;
if (cur_field->table_name && table_name)
{
/* If field_name is qualified by a table name. */
if (my_strcasecmp(table_alias_charset, cur_field->table_name, table_name))
/* Same field names, different tables. */
return NULL;
++cur_match_degree;
if (cur_field->db_name && db_name)
{
/* If field_name is also qualified by a database name. */
if (strcmp(cur_field->db_name, db_name))
/* Same field names, different databases. */
return NULL;
++cur_match_degree;
}
}
if (cur_match_degree > found_match_degree)
{
found_match_degree= cur_match_degree;
found_group= cur_group;
}
else if (found_group && (cur_match_degree == found_match_degree) &&
! (*(found_group->item))->eq(cur_field, 0))
{
/*
If the current resolve candidate matches equally well as the current
best match, they must reference the same column, otherwise the field
is ambiguous.
*/
my_error(ER_NON_UNIQ_ERROR, MYF(0),
find_item->full_name(), current_session->where);
return NULL;
}
}
}
if (found_group)
return found_group->item;
else
return NULL;
}
/**
Resolve a column reference in a sub-select.
Resolve a column reference (usually inside a HAVING clause) against the
SELECT and GROUP BY clauses of the query described by 'select'. The name
resolution algorithm searches both the SELECT and GROUP BY clauses, and in
case of a name conflict prefers GROUP BY column names over SELECT names. If
both clauses contain different fields with the same names, a warning is
issued that name of 'ref' is ambiguous. We extend ANSI SQL in that when no
GROUP BY column is found, then a HAVING name is resolved as a possibly
derived SELECT column. This extension is allowed only if the
MODE_ONLY_FULL_GROUP_BY sql mode isn't enabled.
@param session current thread
@param ref column reference being resolved
@param select the select that ref is resolved against
@note
The resolution procedure is:
- Search for a column or derived column named col_ref_i [in table T_j]
in the SELECT clause of Q.
- Search for a column named col_ref_i [in table T_j]
in the GROUP BY clause of Q.
- If found different columns with the same name in GROUP BY and SELECT
- issue a warning and return the GROUP BY column,
- otherwise
- if the MODE_ONLY_FULL_GROUP_BY mode is enabled return error
- else return the found SELECT column.
@return
- NULL - there was an error, and the error was already reported
- not_found_item - the item was not resolved, no error was reported
- resolved item - if the item was resolved
*/
Item**
resolve_ref_in_select_and_group(Session *session, Item_ident *ref, SELECT_LEX *select)
{
Item **group_by_ref= NULL;
Item **select_ref= NULL;
order_st *group_list= (order_st*) select->group_list.first;
bool ambiguous_fields= false;
uint32_t counter;
enum_resolution_type resolution;
/*
Search for a column or derived column named as 'ref' in the SELECT
clause of the current select.
*/
if (!(select_ref= find_item_in_list(ref, *(select->get_item_list()),
&counter, REPORT_EXCEPT_NOT_FOUND,
&resolution)))
return NULL; /* Some error occurred. */
if (resolution == RESOLVED_AGAINST_ALIAS)
ref->alias_name_used= true;
/* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
if (select->having_fix_field && !ref->with_sum_func && group_list)
{
group_by_ref= find_field_in_group_list(ref, group_list);
/* Check if the fields found in SELECT and GROUP BY are the same field. */
if (group_by_ref && (select_ref != not_found_item) &&
!((*group_by_ref)->eq(*select_ref, 0)))
{
ambiguous_fields= true;
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
ER(ER_NON_UNIQ_ERROR), ref->full_name(),
current_session->where);
}
}
if (select_ref != not_found_item || group_by_ref)
{
if (select_ref != not_found_item && !ambiguous_fields)
{
assert(*select_ref != 0);
if (!select->ref_pointer_array[counter])
{
my_error(ER_ILLEGAL_REFERENCE, MYF(0),
ref->name, "forward reference in item list");
return NULL;
}
assert((*select_ref)->fixed);
return (select->ref_pointer_array + counter);
}
if (group_by_ref)
return group_by_ref;
assert(false);
return NULL; /* So there is no compiler warning. */
}
return (Item**) not_found_item;
}
void Item::init_make_field(Send_field *tmp_field,
enum enum_field_types field_type_arg)
{
char *empty_name= (char*) "";
tmp_field->db_name= empty_name;
tmp_field->org_table_name= empty_name;
tmp_field->org_col_name= empty_name;
tmp_field->table_name= empty_name;
tmp_field->col_name= name;
tmp_field->charsetnr= collation.collation->number;
tmp_field->flags= (maybe_null ? 0 : NOT_NULL_FLAG) |
(my_binary_compare(collation.collation) ?
BINARY_FLAG : 0);
tmp_field->type= field_type_arg;
tmp_field->length=max_length;
tmp_field->decimals=decimals;
}
void Item::make_field(Send_field *tmp_field)
{
init_make_field(tmp_field, field_type());
}
enum_field_types Item::string_field_type() const
{
enum_field_types f_type= DRIZZLE_TYPE_VARCHAR;
if (max_length >= 65536)
f_type= DRIZZLE_TYPE_BLOB;
return f_type;
}
void Item_empty_string::make_field(Send_field *tmp_field)
{
init_make_field(tmp_field, string_field_type());
}
enum_field_types Item::field_type() const
{
switch (result_type()) {
case STRING_RESULT: return string_field_type();
case INT_RESULT: return DRIZZLE_TYPE_LONGLONG;
case DECIMAL_RESULT: return DRIZZLE_TYPE_NEWDECIMAL;
case REAL_RESULT: return DRIZZLE_TYPE_DOUBLE;
case ROW_RESULT:
default:
assert(0);
return DRIZZLE_TYPE_VARCHAR;
}
}
bool Item::is_datetime()
{
switch (field_type())
{
case DRIZZLE_TYPE_DATE:
case DRIZZLE_TYPE_DATETIME:
case DRIZZLE_TYPE_TIMESTAMP:
return true;
default:
break;
}
return false;
}
String *Item::check_well_formed_result(String *str, bool send_error)
{
/* Check whether we got a well-formed string */
const CHARSET_INFO * const cs= str->charset();
int well_formed_error;
uint32_t wlen= cs->cset->well_formed_len(cs,
str->ptr(), str->ptr() + str->length(),
str->length(), &well_formed_error);
if (wlen < str->length())
{
Session *session= current_session;
char hexbuf[7];
enum DRIZZLE_ERROR::enum_warning_level level;
uint32_t diff= str->length() - wlen;
set_if_smaller(diff, 3);
octet2hex(hexbuf, str->ptr() + wlen, diff);
if (send_error)
{
my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
cs->csname, hexbuf);
return 0;
}
{
level= DRIZZLE_ERROR::WARN_LEVEL_ERROR;
null_value= 1;
str= 0;
}
push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
}
return str;
}
/*
Compare two items using a given collation
SYNOPSIS
eq_by_collation()
item item to compare with
binary_cmp true <-> compare as binaries
cs collation to use when comparing strings
DESCRIPTION
This method works exactly as Item::eq if the collation cs coincides with
the collation of the compared objects. Otherwise, first the collations that
differ from cs are replaced for cs and then the items are compared by
Item::eq. After the comparison the original collations of items are
restored.
RETURN
1 compared items has been detected as equal
0 otherwise
*/
bool Item::eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO * const cs)
{
const CHARSET_INFO *save_cs= 0;
const CHARSET_INFO *save_item_cs= 0;
if (collation.collation != cs)
{
save_cs= collation.collation;
collation.collation= cs;
}
if (item->collation.collation != cs)
{
save_item_cs= item->collation.collation;
item->collation.collation= cs;
}
bool res= eq(item, binary_cmp);
if (save_cs)
collation.collation= save_cs;
if (save_item_cs)
item->collation.collation= save_item_cs;
return res;
}
/**
Create a field to hold a string value from an item.
If max_length > CONVERT_IF_BIGGER_TO_BLOB create a blob @n
If max_length > 0 create a varchar @n
If max_length == 0 create a CHAR(0)
@param table Table for which the field is created
*/
Field *Item::make_string_field(Table *table)
{
Field *field;
assert(collation.collation);
if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB)
field= new Field_blob(max_length, maybe_null, name,
collation.collation);
else
field= new Field_varstring(max_length, maybe_null, name, table->s,
collation.collation);
if (field)
field->init(table);
return field;
}
/**
Create a field based on field_type of argument.
For now, this is only used to create a field for
IFNULL(x,something) and time functions
@retval
NULL error
@retval
\# Created field
*/
Field *Item::tmp_table_field_from_field_type(Table *table, bool)
{
/*
The field functions defines a field to be not null if null_ptr is not 0
*/
unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
Field *field;
switch (field_type()) {
case DRIZZLE_TYPE_NEWDECIMAL:
field= new Field_new_decimal((unsigned char*) 0, max_length, null_ptr, 0,
Field::NONE, name, decimals, 0,
unsigned_flag);
break;
case DRIZZLE_TYPE_LONG:
field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
name, 0, unsigned_flag);
break;
case DRIZZLE_TYPE_LONGLONG:
field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
name, 0, unsigned_flag);
break;
case DRIZZLE_TYPE_DOUBLE:
field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
name, decimals, 0, unsigned_flag);
break;
case DRIZZLE_TYPE_NULL:
field= new Field_null((unsigned char*) 0, max_length, Field::NONE,
name, &my_charset_bin);
break;
case DRIZZLE_TYPE_DATE:
field= new Field_date(maybe_null, name, &my_charset_bin);
break;
case DRIZZLE_TYPE_TIME:
field= new Field_time(maybe_null, name, &my_charset_bin);
break;
case DRIZZLE_TYPE_TIMESTAMP:
field= new Field_timestamp(maybe_null, name, &my_charset_bin);
break;
case DRIZZLE_TYPE_DATETIME:
field= new Field_datetime(maybe_null, name, &my_charset_bin);
break;
default:
/* This case should never be chosen */
assert(0);
/* Fall through to make_string_field() */
case DRIZZLE_TYPE_ENUM:
case DRIZZLE_TYPE_VARCHAR:
return make_string_field(table);
case DRIZZLE_TYPE_BLOB:
if (this->type() == Item::TYPE_HOLDER)
field= new Field_blob(max_length, maybe_null, name, collation.collation,
1);
else
field= new Field_blob(max_length, maybe_null, name, collation.collation);
break; // Blob handled outside of case
}
if (field)
field->init(table);
return field;
}
/*
This implementation can lose str_value content, so if the
Item uses str_value to store something, it should
reimplement it's ::save_in_field() as Item_string, for example, does
*/
int Item::save_in_field(Field *field, bool no_conversions)
{
int error;
if (result_type() == STRING_RESULT)
{
String *result;
const CHARSET_INFO * const cs= collation.collation;
char buff[MAX_FIELD_WIDTH]; // Alloc buffer for small columns
str_value.set_quick(buff, sizeof(buff), cs);
result=val_str(&str_value);
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 &&
field->result_type() == STRING_RESULT)
{
double nr= val_real();
if (null_value)
return set_field_to_null_with_conversions(field, no_conversions);
field->set_notnull();
error= field->store(nr);
}
else if (result_type() == REAL_RESULT)
{
double nr= val_real();
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 *value= val_decimal(&decimal_value);
if (null_value)
return set_field_to_null_with_conversions(field, no_conversions);
field->set_notnull();
error=field->store_decimal(value);
}
else
{
int64_t nr=val_int();
if (null_value)
return set_field_to_null_with_conversions(field, no_conversions);
field->set_notnull();
error=field->store(nr, unsigned_flag);
}
return error;
}
Item *Item_int_with_ref::clone_item()
{
assert(ref->const_item());
/*
We need to evaluate the constant to make sure it works with
parameter markers.
*/
return (ref->unsigned_flag ?
new Item_uint(ref->name, ref->val_int(), ref->max_length) :
new Item_int(ref->name, ref->val_int(), ref->max_length));
}
inline uint32_t char_val(char X)
{
return (uint) (X >= '0' && X <= '9' ? X-'0' :
X >= 'A' && X <= 'Z' ? X-'A'+10 :
X-'a'+10);
}
Item_hex_string::Item_hex_string(const char *str, uint32_t str_length)
{
max_length=(str_length+1)/2;
char *ptr=(char*) sql_alloc(max_length+1);
if (!ptr)
return;
str_value.set(ptr,max_length,&my_charset_bin);
char *end=ptr+max_length;
if (max_length*2 != str_length)
*ptr++=char_val(*str++); // Not even, assume 0 prefix
while (ptr != end)
{
*ptr++= (char) (char_val(str[0])*16+char_val(str[1]));
str+=2;
}
*ptr=0; // Keep purify happy
collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
fixed= 1;
unsigned_flag= 1;
}
int64_t Item_hex_string::val_int()
{
// following assert is redundant, because fixed=1 assigned in constructor
assert(fixed == 1);
char *end=(char*) str_value.ptr()+str_value.length(),
*ptr=end-cmin(str_value.length(),(uint32_t)sizeof(int64_t));
uint64_t value=0;
for (; ptr != end ; ptr++)
value=(value << 8)+ (uint64_t) (unsigned char) *ptr;
return (int64_t) value;
}
my_decimal *Item_hex_string::val_decimal(my_decimal *decimal_value)
{
// following assert is redundant, because fixed=1 assigned in constructor
assert(fixed == 1);
uint64_t value= (uint64_t)val_int();
int2my_decimal(E_DEC_FATAL_ERROR, value, true, decimal_value);
return (decimal_value);
}
int Item_hex_string::save_in_field(Field *field, bool)
{
field->set_notnull();
if (field->result_type() == STRING_RESULT)
return field->store(str_value.ptr(), str_value.length(),
collation.collation);
uint64_t nr;
uint32_t length= str_value.length();
if (length > 8)
{
nr= field->flags & UNSIGNED_FLAG ? UINT64_MAX : INT64_MAX;
goto warn;
}
nr= (uint64_t) val_int();
if ((length == 8) && !(field->flags & UNSIGNED_FLAG) && (nr > INT64_MAX))
{
nr= INT64_MAX;
goto warn;
}
return field->store((int64_t) nr, true); // Assume hex numbers are unsigned
warn:
if (!field->store((int64_t) nr, true))
field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE,
1);
return 1;
}
void Item_hex_string::print(String *str, enum_query_type)
{
char *end= (char*) str_value.ptr() + str_value.length(),
*ptr= end - cmin(str_value.length(), (uint32_t)sizeof(int64_t));
str->append("0x");
for (; ptr != end ; ptr++)
{
str->append(_dig_vec_lower[((unsigned char) *ptr) >> 4]);
str->append(_dig_vec_lower[((unsigned char) *ptr) & 0x0F]);
}
}
bool Item_hex_string::eq(const Item *arg, bool binary_cmp) const
{
if (arg->basic_const_item() && arg->type() == type())
{
if (binary_cmp)
return !stringcmp(&str_value, &arg->str_value);
return !sortcmp(&str_value, &arg->str_value, collation.collation);
}
return false;
}
Item *Item_hex_string::safe_charset_converter(const CHARSET_INFO * const tocs)
{
Item_string *conv;
String tmp, *str= val_str(&tmp);
if (!(conv= new Item_string(str->ptr(), str->length(), tocs)))
return NULL;
conv->str_value.copy();
conv->str_value.mark_as_const();
return conv;
}
/*
bin item.
In string context this is a binary string.
In number context this is a int64_t value.
*/
Item_bin_string::Item_bin_string(const char *str, uint32_t str_length)
{
const char *end= str + str_length - 1;
unsigned char bits= 0;
uint32_t power= 1;
max_length= (str_length + 7) >> 3;
char *ptr= (char*) sql_alloc(max_length + 1);
if (!ptr)
return;
str_value.set(ptr, max_length, &my_charset_bin);
ptr+= max_length - 1;
ptr[1]= 0; // Set end null for string
for (; end >= str; end--)
{
if (power == 256)
{
power= 1;
*ptr--= bits;
bits= 0;
}
if (*end == '1')
bits|= power;
power<<= 1;
}
*ptr= (char) bits;
collation.set(&my_charset_bin, DERIVATION_COERCIBLE);
fixed= 1;
}
/**
This is only called from items that is not of type item_field.
*/
bool Item::send(Protocol *protocol, String *buffer)
{
bool result= false;
enum_field_types f_type;
switch ((f_type=field_type())) {
default:
case DRIZZLE_TYPE_NULL:
case DRIZZLE_TYPE_ENUM:
case DRIZZLE_TYPE_BLOB:
case DRIZZLE_TYPE_VARCHAR:
case DRIZZLE_TYPE_NEWDECIMAL:
{
String *res;
if ((res=val_str(buffer)))
result= protocol->store(res->ptr(),res->length(),res->charset());
break;
}
case DRIZZLE_TYPE_LONG:
{
int64_t nr;
nr= val_int();
if (!null_value)
result= protocol->store_long(nr);
break;
}
case DRIZZLE_TYPE_LONGLONG:
{
int64_t nr;
nr= val_int();
if (!null_value)
result= protocol->store_int64_t(nr, unsigned_flag);
break;
}
case DRIZZLE_TYPE_DOUBLE:
{
double nr= val_real();
if (!null_value)
result= protocol->store(nr, decimals, buffer);
break;
}
case DRIZZLE_TYPE_DATETIME:
case DRIZZLE_TYPE_TIMESTAMP:
{
DRIZZLE_TIME tm;
get_date(&tm, TIME_FUZZY_DATE);
if (!null_value)
{
if (f_type == DRIZZLE_TYPE_DATE)
return protocol->store_date(&tm);
else
result= protocol->store(&tm);
}
break;
}
case DRIZZLE_TYPE_TIME:
{
DRIZZLE_TIME tm;
get_time(&tm);
if (!null_value)
result= protocol->store_time(&tm);
break;
}
}
if (null_value)
result= protocol->store_null();
return result;
}
Item_ref::Item_ref(Name_resolution_context *context_arg,
Item **item, const char *table_name_arg,
const char *field_name_arg,
bool alias_name_used_arg)
:Item_ident(context_arg, NULL, table_name_arg, field_name_arg),
result_field(0), ref(item)
{
alias_name_used= alias_name_used_arg;
/*
This constructor used to create some internals references over fixed items
*/
if (ref && *ref && (*ref)->fixed)
set_properties();
}
/**
Resolve the name of a reference to a column reference.
The method resolves the column reference represented by 'this' as a column
present in one of: GROUP BY clause, SELECT clause, outer queries. It is
used typically for columns in the HAVING clause which are not under
aggregate functions.
POSTCONDITION @n
Item_ref::ref is 0 or points to a valid item.
@note
The name resolution algorithm used is (where [T_j] is an optional table
name that qualifies the column name):
@code
resolve_extended([T_j].col_ref_i)
{
Search for a column or derived column named col_ref_i [in table T_j]
in the SELECT and GROUP clauses of Q.
if such a column is NOT found AND // Lookup in outer queries.
there are outer queries
{
for each outer query Q_k beginning from the inner-most one
{
Search for a column or derived column named col_ref_i
[in table T_j] in the SELECT and GROUP clauses of Q_k.
if such a column is not found AND
- Q_k is not a group query AND
- Q_k is not inside an aggregate function
OR
- Q_(k-1) is not in a HAVING or SELECT clause of Q_k
{
search for a column or derived column named col_ref_i
[in table T_j] in the FROM clause of Q_k;
}
}
}
}
@endcode
@n
This procedure treats GROUP BY and SELECT clauses as one namespace for
column references in HAVING. Notice that compared to
Item_field::fix_fields, here we first search the SELECT and GROUP BY
clauses, and then we search the FROM clause.
@param[in] session current thread
@param[in,out] reference view column if this item was resolved to a
view column
@todo
Here we could first find the field anyway, and then test this
condition, so that we can give a better error message -
ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
ER_BAD_FIELD_ERROR which we produce now.
@retval
true if error
@retval
false on success
*/
bool Item_ref::fix_fields(Session *session, Item **reference)
{
enum_parsing_place place= NO_MATTER;
assert(fixed == 0);
SELECT_LEX *current_sel= session->lex->current_select;
if (!ref || ref == not_found_item)
{
if (!(ref= resolve_ref_in_select_and_group(session, this,
context->select_lex)))
goto error; /* Some error occurred (e.g. ambiguous names). */
if (ref == not_found_item) /* This reference was not resolved. */
{
Name_resolution_context *last_checked_context= context;
Name_resolution_context *outer_context= context->outer_context;
Field *from_field;
ref= 0;
if (!outer_context)
{
/* The current reference cannot be resolved in this query. */
my_error(ER_BAD_FIELD_ERROR,MYF(0),
this->full_name(), current_session->where);
goto error;
}
/*
If there is an outer context (select), and it is not a derived table
(which do not support the use of outer fields for now), try to
resolve this reference in the outer select(s).
We treat each subselect as a separate namespace, so that different
subselects may contain columns with the same names. The subselects are
searched starting from the innermost.
*/
from_field= (Field*) not_found_field;
do
{
SELECT_LEX *select= outer_context->select_lex;
Item_subselect *prev_subselect_item=
last_checked_context->select_lex->master_unit()->item;
last_checked_context= outer_context;
/* Search in the SELECT and GROUP lists of the outer select. */
if (outer_context->resolve_in_select_list)
{
if (!(ref= resolve_ref_in_select_and_group(session, this, select)))
goto error; /* Some error occurred (e.g. ambiguous names). */
if (ref != not_found_item)
{
assert(*ref && (*ref)->fixed);
prev_subselect_item->used_tables_cache|= (*ref)->used_tables();
prev_subselect_item->const_item_cache&= (*ref)->const_item();
break;
}
/*
Set ref to 0 to ensure that we get an error in case we replaced
this item with another item and still use this item in some
other place of the parse tree.
*/
ref= 0;
}
place= prev_subselect_item->parsing_place;
/*
Check table fields only if the subquery is used somewhere out of
HAVING or the outer SELECT does not use grouping (i.e. tables are
accessible).
TODO:
Here we could first find the field anyway, and then test this
condition, so that we can give a better error message -
ER_WRONG_FIELD_WITH_GROUP, instead of the less informative
ER_BAD_FIELD_ERROR which we produce now.
*/
if ((place != IN_HAVING ||
(!select->with_sum_func &&
select->group_list.elements == 0)))
{
/*
In case of view, find_field_in_tables() write pointer to view
field expression to 'reference', i.e. it substitute that
expression instead of this Item_ref
*/
from_field= find_field_in_tables(session, this,
outer_context->
first_name_resolution_table,
outer_context->
last_name_resolution_table,
reference,
IGNORE_EXCEPT_NON_UNIQUE,
true, true);
if (! from_field)
goto error;
if (from_field == view_ref_found)
{
Item::Type refer_type= (*reference)->type();
prev_subselect_item->used_tables_cache|=
(*reference)->used_tables();
prev_subselect_item->const_item_cache&=
(*reference)->const_item();
assert((*reference)->type() == REF_ITEM);
mark_as_dependent(session, last_checked_context->select_lex,
context->select_lex, this,
((refer_type == REF_ITEM ||
refer_type == FIELD_ITEM) ?
(Item_ident*) (*reference) :
0));
/*
view reference found, we substituted it instead of this
Item, so can quit
*/
return false;
}
if (from_field != not_found_field)
{
if (cached_table && cached_table->select_lex &&
outer_context->select_lex &&
cached_table->select_lex != outer_context->select_lex)
{
/*
Due to cache, find_field_in_tables() can return field which
doesn't belong to provided outer_context. In this case we have
to find proper field context in order to fix field correcly.
*/
do
{
outer_context= outer_context->outer_context;
select= outer_context->select_lex;
prev_subselect_item=
last_checked_context->select_lex->master_unit()->item;
last_checked_context= outer_context;
} while (outer_context && outer_context->select_lex &&
cached_table->select_lex != outer_context->select_lex);
}
prev_subselect_item->used_tables_cache|= from_field->table->map;
prev_subselect_item->const_item_cache= 0;
break;
}
}
assert(from_field == not_found_field);
/* Reference is not found => depend on outer (or just error). */
prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
prev_subselect_item->const_item_cache= 0;
outer_context= outer_context->outer_context;
} while (outer_context);
assert(from_field != 0 && from_field != view_ref_found);
if (from_field != not_found_field)
{
Item_field* fld;
if (!(fld= new Item_field(from_field)))
goto error;
session->change_item_tree(reference, fld);
mark_as_dependent(session, last_checked_context->select_lex,
session->lex->current_select, this, fld);
/*
A reference is resolved to a nest level that's outer or the same as
the nest level of the enclosing set function : adjust the value of
max_arg_level for the function if it's needed.
*/
if (session->lex->in_sum_func &&
session->lex->in_sum_func->nest_level >=
last_checked_context->select_lex->nest_level)
set_if_bigger(session->lex->in_sum_func->max_arg_level,
last_checked_context->select_lex->nest_level);
return false;
}
if (ref == 0)
{
/* The item was not a table field and not a reference */
my_error(ER_BAD_FIELD_ERROR, MYF(0),
this->full_name(), current_session->where);
goto error;
}
/* Should be checked in resolve_ref_in_select_and_group(). */
assert(*ref && (*ref)->fixed);
mark_as_dependent(session, last_checked_context->select_lex,
context->select_lex, this, this);
/*
A reference is resolved to a nest level that's outer or the same as
the nest level of the enclosing set function : adjust the value of
max_arg_level for the function if it's needed.
*/
if (session->lex->in_sum_func &&
session->lex->in_sum_func->nest_level >=
last_checked_context->select_lex->nest_level)
set_if_bigger(session->lex->in_sum_func->max_arg_level,
last_checked_context->select_lex->nest_level);
}
}
assert(*ref);
/*
Check if this is an incorrect reference in a group function or forward
reference. Do not issue an error if this is:
1. outer reference (will be fixed later by the fix_inner_refs function);
2. an unnamed reference inside an aggregate function.
*/
if (!((*ref)->type() == REF_ITEM &&
((Item_ref *)(*ref))->ref_type() == OUTER_REF) &&
(((*ref)->with_sum_func && name &&
!(current_sel->linkage != GLOBAL_OPTIONS_TYPE &&
current_sel->having_fix_field)) ||
!(*ref)->fixed))
{
my_error(ER_ILLEGAL_REFERENCE, MYF(0),
name, ((*ref)->with_sum_func?
"reference to group function":
"forward reference in item list"));
goto error;
}
set_properties();
if ((*ref)->check_cols(1))
goto error;
return false;
error:
context->process_error(session);
return true;
}
void Item_ref::set_properties()
{
max_length= (*ref)->max_length;
maybe_null= (*ref)->maybe_null;
decimals= (*ref)->decimals;
collation.set((*ref)->collation);
/*
We have to remember if we refer to a sum function, to ensure that
split_sum_func() doesn't try to change the reference.
*/
with_sum_func= (*ref)->with_sum_func;
unsigned_flag= (*ref)->unsigned_flag;
fixed= 1;
if (alias_name_used)
return;
if ((*ref)->type() == FIELD_ITEM)
alias_name_used= ((Item_ident *) (*ref))->alias_name_used;
else
alias_name_used= true; // it is not field, so it is was resolved by alias
}
void Item_ref::cleanup()
{
Item_ident::cleanup();
result_field= 0;
return;
}
void Item_ref::print(String *str, enum_query_type query_type)
{
if (ref)
{
if ((*ref)->type() != Item::CACHE_ITEM &&
!table_name && name && alias_name_used)
{
Session *session= current_session;
append_identifier(session, str, name, (uint) strlen(name));
}
else
(*ref)->print(str, query_type);
}
else
Item_ident::print(str, query_type);
}
bool Item_ref::send(Protocol *prot, String *tmp)
{
if (result_field)
return prot->store(result_field);
return (*ref)->send(prot, tmp);
}
double Item_ref::val_result()
{
if (result_field)
{
if ((null_value= result_field->is_null()))
return 0.0;
return result_field->val_real();
}
return val_real();
}
int64_t Item_ref::val_int_result()
{
if (result_field)
{
if ((null_value= result_field->is_null()))
return 0;
return result_field->val_int();
}
return val_int();
}
String *Item_ref::str_result(String* str)
{
if (result_field)
{
if ((null_value= result_field->is_null()))
return 0;
str->set_charset(str_value.charset());
return result_field->val_str(str, &str_value);
}
return val_str(str);
}
my_decimal *Item_ref::val_decimal_result(my_decimal *decimal_value)
{
if (result_field)
{
if ((null_value= result_field->is_null()))
return 0;
return result_field->val_decimal(decimal_value);
}
return val_decimal(decimal_value);
}
bool Item_ref::val_bool_result()
{
if (result_field)
{
if ((null_value= result_field->is_null()))
return 0;
switch (result_field->result_type()) {
case INT_RESULT:
return result_field->val_int() != 0;
case DECIMAL_RESULT:
{
my_decimal decimal_value;
my_decimal *val= result_field->val_decimal(&decimal_value);
if (val)
return !my_decimal_is_zero(val);
return 0;
}
case REAL_RESULT:
case STRING_RESULT:
return result_field->val_real() != 0.0;
case ROW_RESULT:
default:
assert(0);
}
}
return val_bool();
}
double Item_ref::val_real()
{
assert(fixed);
double tmp=(*ref)->val_result();
null_value=(*ref)->null_value;
return tmp;
}
int64_t Item_ref::val_int()
{
assert(fixed);
int64_t tmp=(*ref)->val_int_result();
null_value=(*ref)->null_value;
return tmp;
}
bool Item_ref::val_bool()
{
assert(fixed);
bool tmp= (*ref)->val_bool_result();
null_value= (*ref)->null_value;
return tmp;
}
String *Item_ref::val_str(String* tmp)
{
assert(fixed);
tmp=(*ref)->str_result(tmp);
null_value=(*ref)->null_value;
return tmp;
}
bool Item_ref::is_null()
{
assert(fixed);
return (*ref)->is_null();
}
bool Item_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
{
return (null_value=(*ref)->get_date_result(ltime,fuzzydate));
}
my_decimal *Item_ref::val_decimal(my_decimal *decimal_value)
{
my_decimal *val= (*ref)->val_decimal_result(decimal_value);
null_value= (*ref)->null_value;
return val;
}
int Item_ref::save_in_field(Field *to, bool no_conversions)
{
int res;
assert(!result_field);
res= (*ref)->save_in_field(to, no_conversions);
null_value= (*ref)->null_value;
return res;
}
void Item_ref::save_org_in_field(Field *field)
{
(*ref)->save_org_in_field(field);
}
void Item_ref::make_field(Send_field *field)
{
(*ref)->make_field(field);
/* Non-zero in case of a view */
if (name)
field->col_name= name;
if (table_name)
field->table_name= table_name;
if (db_name)
field->db_name= db_name;
}
Item *Item_ref::get_tmp_table_item(Session *session)
{
if (!result_field)
return (*ref)->get_tmp_table_item(session);
Item_field *item= new Item_field(result_field);
if (item)
{
item->table_name= table_name;
item->db_name= db_name;
}
return item;
}
void Item_ref_null_helper::print(String *str, enum_query_type query_type)
{
str->append(STRING_WITH_LEN("<ref_null_helper>("));
if (ref)
(*ref)->print(str, query_type);
else
str->append('?');
str->append(')');
}
double Item_direct_ref::val_real()
{
double tmp=(*ref)->val_real();
null_value=(*ref)->null_value;
return tmp;
}
int64_t Item_direct_ref::val_int()
{
int64_t tmp=(*ref)->val_int();
null_value=(*ref)->null_value;
return tmp;
}
String *Item_direct_ref::val_str(String* tmp)
{
tmp=(*ref)->val_str(tmp);
null_value=(*ref)->null_value;
return tmp;
}
my_decimal *Item_direct_ref::val_decimal(my_decimal *decimal_value)
{
my_decimal *tmp= (*ref)->val_decimal(decimal_value);
null_value=(*ref)->null_value;
return tmp;
}
bool Item_direct_ref::val_bool()
{
bool tmp= (*ref)->val_bool();
null_value=(*ref)->null_value;
return tmp;
}
bool Item_direct_ref::is_null()
{
return (*ref)->is_null();
}
bool Item_direct_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
{
return (null_value=(*ref)->get_date(ltime,fuzzydate));
}
/*
Prepare referenced outer field then call usual Item_direct_ref::fix_fields
SYNOPSIS
Item_outer_ref::fix_fields()
session thread handler
reference reference on reference where this item stored
RETURN
false OK
true Error
*/
bool Item_outer_ref::fix_fields(Session *session, Item **reference)
{
bool err;
/* outer_ref->check_cols() will be made in Item_direct_ref::fix_fields */
if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(session, reference)))
return true;
err= Item_direct_ref::fix_fields(session, reference);
if (!outer_ref)
outer_ref= *ref;
if ((*ref)->type() == Item::FIELD_ITEM)
table_name= ((Item_field*)outer_ref)->table_name;
return err;
}
void Item_outer_ref::fix_after_pullout(st_select_lex *new_parent, Item **ref)
{
if (depended_from == new_parent)
{
*ref= outer_ref;
outer_ref->fix_after_pullout(new_parent, ref);
}
}
void Item_ref::fix_after_pullout(st_select_lex *new_parent, Item **)
{
if (depended_from == new_parent)
{
(*ref)->fix_after_pullout(new_parent, ref);
depended_from= NULL;
}
}
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
{
return item->type() == DEFAULT_VALUE_ITEM &&
((Item_default_value *)item)->arg->eq(arg, binary_cmp);
}
bool Item_default_value::fix_fields(Session *session, Item **)
{
Item *real_arg;
Item_field *field_arg;
Field *def_field;
assert(fixed == 0);
if (!arg)
{
fixed= 1;
return false;
}
if (!arg->fixed && arg->fix_fields(session, &arg))
goto error;
real_arg= arg->real_item();
if (real_arg->type() != FIELD_ITEM)
{
my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->name);
goto error;
}
field_arg= (Item_field *)real_arg;
if (field_arg->field->flags & NO_DEFAULT_VALUE_FLAG)
{
my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), field_arg->field->field_name);
goto error;
}
if (!(def_field= (Field*) sql_alloc(field_arg->field->size_of())))
goto error;
memcpy(def_field, field_arg->field, field_arg->field->size_of());
def_field->move_field_offset((my_ptrdiff_t)
(def_field->table->s->default_values -
def_field->table->record[0]));
set_field(def_field);
return false;
error:
context->process_error(session);
return true;
}
void Item_default_value::print(String *str, enum_query_type query_type)
{
if (!arg)
{
str->append(STRING_WITH_LEN("default"));
return;
}
str->append(STRING_WITH_LEN("default("));
arg->print(str, query_type);
str->append(')');
}
int Item_default_value::save_in_field(Field *field_arg, bool no_conversions)
{
if (!arg)
{
if (field_arg->flags & NO_DEFAULT_VALUE_FLAG)
{
if (field_arg->reset())
{
my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
ER(ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
return -1;
}
{
push_warning_printf(field_arg->table->in_use,
DRIZZLE_ERROR::WARN_LEVEL_WARN,
ER_NO_DEFAULT_FOR_FIELD,
ER(ER_NO_DEFAULT_FOR_FIELD),
field_arg->field_name);
}
return 1;
}
field_arg->set_default();
return 0;
}
return Item_field::save_in_field(field_arg, no_conversions);
}
/**
This method like the walk method traverses the item tree, but at the
same time it can replace some nodes in the tree.
*/
Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
{
Item *new_item= arg->transform(transformer, args);
if (!new_item)
return 0;
/*
Session::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 (arg != new_item)
current_session->change_item_tree(&arg, new_item);
return (this->*transformer)(args);
}
bool Item_insert_value::eq(const Item *item, bool binary_cmp) const
{
return item->type() == INSERT_VALUE_ITEM &&
((Item_default_value *)item)->arg->eq(arg, binary_cmp);
}
bool Item_insert_value::fix_fields(Session *session, Item **)
{
assert(fixed == 0);
/* We should only check that arg is in first table */
if (!arg->fixed)
{
bool res;
TableList *orig_next_table= context->last_name_resolution_table;
context->last_name_resolution_table= context->first_name_resolution_table;
res= arg->fix_fields(session, &arg);
context->last_name_resolution_table= orig_next_table;
if (res)
return true;
}
if (arg->type() == REF_ITEM)
{
Item_ref *ref= (Item_ref *)arg;
if (ref->ref[0]->type() != FIELD_ITEM)
{
my_error(ER_BAD_FIELD_ERROR, MYF(0), "", "VALUES() function");
return true;
}
arg= ref->ref[0];
}
/*
According to our SQL grammar, VALUES() function can reference
only to a column.
*/
assert(arg->type() == FIELD_ITEM);
Item_field *field_arg= (Item_field *)arg;
if (field_arg->field->table->insert_values)
{
Field *def_field= (Field*) sql_alloc(field_arg->field->size_of());
if (!def_field)
return true;
memcpy(def_field, field_arg->field, field_arg->field->size_of());
def_field->move_field_offset((my_ptrdiff_t)
(def_field->table->insert_values -
def_field->table->record[0]));
set_field(def_field);
}
else
{
Field *tmp_field= field_arg->field;
/* charset doesn't matter here, it's to avoid sigsegv only */
tmp_field= new Field_null(0, 0, Field::NONE, field_arg->field->field_name,
&my_charset_bin);
if (tmp_field)
{
tmp_field->init(field_arg->field->table);
set_field(tmp_field);
}
}
return false;
}
void Item_insert_value::print(String *str, enum_query_type query_type)
{
str->append(STRING_WITH_LEN("values("));
arg->print(str, query_type);
str->append(')');
}
Item_result item_cmp_type(Item_result a,Item_result b)
{
if (a == STRING_RESULT && b == STRING_RESULT)
return STRING_RESULT;
if (a == INT_RESULT && b == INT_RESULT)
return INT_RESULT;
else if (a == ROW_RESULT || b == ROW_RESULT)
return ROW_RESULT;
if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
(b == INT_RESULT || b == DECIMAL_RESULT))
return DECIMAL_RESULT;
return REAL_RESULT;
}
void resolve_const_item(Session *session, Item **ref, Item *comp_item)
{
Item *item= *ref;
Item *new_item= NULL;
if (item->basic_const_item())
return; // Can't be better
Item_result res_type=item_cmp_type(comp_item->result_type(),
item->result_type());
char *name=item->name; // Alloced by sql_alloc
switch (res_type) {
case STRING_RESULT:
{
char buff[MAX_FIELD_WIDTH];
String tmp(buff,sizeof(buff),&my_charset_bin),*result;
result=item->val_str(&tmp);
if (item->null_value)
new_item= new Item_null(name);
else
{
uint32_t length= result->length();
char *tmp_str= sql_strmake(result->ptr(), length);
new_item= new Item_string(name, tmp_str, length, result->charset());
}
break;
}
case INT_RESULT:
{
int64_t result=item->val_int();
uint32_t length=item->max_length;
bool null_value=item->null_value;
new_item= (null_value ? (Item*) new Item_null(name) :
(Item*) new Item_int(name, result, length));
break;
}
case ROW_RESULT:
if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
{
/*
Substitute constants only in Item_rows. Don't affect other Items
with ROW_RESULT (eg Item_singlerow_subselect).
For such Items more optimal is to detect if it is constant and replace
it with Item_row. This would optimize queries like this:
SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
*/
Item_row *item_row= (Item_row*) item;
Item_row *comp_item_row= (Item_row*) comp_item;
uint32_t col;
new_item= 0;
/*
If item and comp_item are both Item_rows and have same number of cols
then process items in Item_row one by one.
We can't ignore NULL values here as this item may be used with <=>, in
which case NULL's are significant.
*/
assert(item->result_type() == comp_item->result_type());
assert(item_row->cols() == comp_item_row->cols());
col= item_row->cols();
while (col-- > 0)
resolve_const_item(session, item_row->addr(col),
comp_item_row->element_index(col));
break;
}
/* Fallthrough */
case REAL_RESULT:
{ // It must REAL_RESULT
double result= item->val_real();
uint32_t length=item->max_length,decimals=item->decimals;
bool null_value=item->null_value;
new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
new Item_float(name, result, decimals, length));
break;
}
case DECIMAL_RESULT:
{
my_decimal decimal_value;
my_decimal *result= item->val_decimal(&decimal_value);
uint32_t length= item->max_length, decimals= item->decimals;
bool null_value= item->null_value;
new_item= (null_value ?
(Item*) new Item_null(name) :
(Item*) new Item_decimal(name, result, length, decimals));
break;
}
default:
assert(0);
}
if (new_item)
session->change_item_tree(ref, new_item);
}
/**
Return true if the value stored in the field is equal to the const
item.
We need to use this on the range optimizer because in some cases
we can't store the value in the field without some precision/character loss.
*/
bool field_is_equal_to_item(Field *field,Item *item)
{
Item_result res_type=item_cmp_type(field->result_type(),
item->result_type());
if (res_type == STRING_RESULT)
{
char item_buff[MAX_FIELD_WIDTH];
char field_buff[MAX_FIELD_WIDTH];
String item_tmp(item_buff,sizeof(item_buff),&my_charset_bin),*item_result;
String field_tmp(field_buff,sizeof(field_buff),&my_charset_bin);
item_result=item->val_str(&item_tmp);
if (item->null_value)
return 1; // This must be true
field->val_str(&field_tmp);
return !stringcmp(&field_tmp,item_result);
}
if (res_type == INT_RESULT)
return 1; // Both where of type int
if (res_type == DECIMAL_RESULT)
{
my_decimal item_buf, *item_val,
field_buf, *field_val;
item_val= item->val_decimal(&item_buf);
if (item->null_value)
return 1; // This must be true
field_val= field->val_decimal(&field_buf);
return !my_decimal_cmp(item_val, field_val);
}
double result= item->val_real();
if (item->null_value)
return 1;
return result == field->val_real();
}
Item_cache* Item_cache::get_cache(const Item *item)
{
switch (item->result_type()) {
case INT_RESULT:
return new Item_cache_int();
case REAL_RESULT:
return new Item_cache_real();
case DECIMAL_RESULT:
return new Item_cache_decimal();
case STRING_RESULT:
return new Item_cache_str(item);
case ROW_RESULT:
return new Item_cache_row();
default:
// should never be in real life
assert(0);
return 0;
}
}
void Item_cache::print(String *str, enum_query_type query_type)
{
str->append(STRING_WITH_LEN("<cache>("));
if (example)
example->print(str, query_type);
else
Item::print(str, query_type);
str->append(')');
}
bool Item_cache::eq_def(Field *field)
{
return cached_field ? cached_field->eq_def (field) : false;
}
void Item_cache_int::store(Item *item)
{
value= item->val_int_result();
null_value= item->null_value;
unsigned_flag= item->unsigned_flag;
}
void Item_cache_int::store(Item *item, int64_t val_arg)
{
value= val_arg;
null_value= item->null_value;
unsigned_flag= item->unsigned_flag;
}
String *Item_cache_int::val_str(String *str)
{
assert(fixed == 1);
str->set(value, default_charset());
return str;
}
my_decimal *Item_cache_int::val_decimal(my_decimal *decimal_val)
{
assert(fixed == 1);
int2my_decimal(E_DEC_FATAL_ERROR, value, unsigned_flag, decimal_val);
return decimal_val;
}
void Item_cache_real::store(Item *item)
{
value= item->val_result();
null_value= item->null_value;
}
int64_t Item_cache_real::val_int()
{
assert(fixed == 1);
return (int64_t) rint(value);
}
String* Item_cache_real::val_str(String *str)
{
assert(fixed == 1);
str->set_real(value, decimals, default_charset());
return str;
}
my_decimal *Item_cache_real::val_decimal(my_decimal *decimal_val)
{
assert(fixed == 1);
double2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
return decimal_val;
}
void Item_cache_decimal::store(Item *item)
{
my_decimal *val= item->val_decimal_result(&decimal_value);
if (!(null_value= item->null_value) && val != &decimal_value)
my_decimal2decimal(val, &decimal_value);
}
double Item_cache_decimal::val_real()
{
assert(fixed);
double res;
my_decimal2double(E_DEC_FATAL_ERROR, &decimal_value, &res);
return res;
}
int64_t Item_cache_decimal::val_int()
{
assert(fixed);
int64_t res;
my_decimal2int(E_DEC_FATAL_ERROR, &decimal_value, unsigned_flag, &res);
return res;
}
String* Item_cache_decimal::val_str(String *str)
{
assert(fixed);
my_decimal_round(E_DEC_FATAL_ERROR, &decimal_value, decimals, false,
&decimal_value);
my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, str);
return str;
}
my_decimal *Item_cache_decimal::val_decimal(my_decimal *)
{
assert(fixed);
return &decimal_value;
}
Item_cache_str::Item_cache_str(const Item *item) :
Item_cache(), value(0),
is_varbinary(item->type() == FIELD_ITEM &&
((const Item_field *) item)->field->type() ==
DRIZZLE_TYPE_VARCHAR &&
!((const Item_field *) item)->field->has_charset())
{}
void Item_cache_str::store(Item *item)
{
value_buff.set(buffer, sizeof(buffer), item->collation.collation);
value= item->str_result(&value_buff);
if ((null_value= item->null_value))
value= 0;
else if (value != &value_buff)
{
/*
We copy string value to avoid changing value if 'item' is table field
in queries like following (where t1.c is varchar):
select a,
(select a,b,c from t1 where t1.a=t2.a) = ROW(a,2,'a'),
(select c from t1 where a=t2.a)
from t2;
*/
value_buff.copy(*value);
value= &value_buff;
}
}
double Item_cache_str::val_real()
{
assert(fixed == 1);
int err_not_used;
char *end_not_used;
if (value)
return my_strntod(value->charset(), (char*) value->ptr(),
value->length(), &end_not_used, &err_not_used);
return (double) 0;
}
int64_t Item_cache_str::val_int()
{
assert(fixed == 1);
int err;
if (value)
return my_strntoll(value->charset(), value->ptr(),
value->length(), 10, (char**) 0, &err);
else
return (int64_t)0;
}
my_decimal *Item_cache_str::val_decimal(my_decimal *decimal_val)
{
assert(fixed == 1);
if (value)
string2my_decimal(E_DEC_FATAL_ERROR, value, decimal_val);
else
decimal_val= 0;
return decimal_val;
}
int Item_cache_str::save_in_field(Field *field, bool no_conversions)
{
int res= Item_cache::save_in_field(field, no_conversions);
return res;
}
/**
Dummy error processor used by default by Name_resolution_context.
@note
do nothing
*/
void dummy_error_processor(Session *, void *)
{}
/**
Create field for temporary table using type of given item.
@param session Thread handler
@param item Item to create a field for
@param table Temporary table
@param copy_func If set and item is a function, store copy of
item in this array
@param modify_item 1 if item->result_field should point to new
item. This is relevent for how fill_record()
is going to work:
If modify_item is 1 then fill_record() will
update the record in the original table.
If modify_item is 0 then fill_record() will
update the temporary table
@param convert_blob_length If >0 create a varstring(convert_blob_length)
field instead of blob.
@retval
0 on error
@retval
new_created field
*/
static Field *create_tmp_field_from_item(Session *,
Item *item, Table *table,
Item ***copy_func, bool modify_item,
uint32_t convert_blob_length)
{
bool maybe_null= item->maybe_null;
Field *new_field;
switch (item->result_type()) {
case REAL_RESULT:
new_field= new Field_double(item->max_length, maybe_null,
item->name, item->decimals, true);
break;
case INT_RESULT:
/*
Select an integer type with the minimal fit precision.
MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
Field_long : make them Field_int64_t.
*/
if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
new_field=new Field_int64_t(item->max_length, maybe_null,
item->name, item->unsigned_flag);
else
new_field=new Field_long(item->max_length, maybe_null,
item->name, item->unsigned_flag);
break;
case STRING_RESULT:
assert(item->collation.collation);
enum enum_field_types type;
/*
DATE/TIME fields have STRING_RESULT result type.
To preserve type they needed to be handled separately.
*/
if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_DATE ||
type == DRIZZLE_TYPE_TIMESTAMP)
new_field= item->tmp_table_field_from_field_type(table, 1);
/*
Make sure that the blob fits into a Field_varstring which has
2-byte lenght.
*/
else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
convert_blob_length <= Field_varstring::MAX_SIZE &&
convert_blob_length)
new_field= new Field_varstring(convert_blob_length, maybe_null,
item->name, table->s,
item->collation.collation);
else
new_field= item->make_string_field(table);
new_field->set_derivation(item->collation.derivation);
break;
case DECIMAL_RESULT:
{
uint8_t dec= item->decimals;
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
uint32_t len= item->max_length;
/*
Trying to put too many digits overall in a DECIMAL(prec,dec)
will always throw a warning. We must limit dec to
DECIMAL_MAX_SCALE however to prevent an assert() later.
*/
if (dec > 0)
{
signed int overflow;
dec= cmin(dec, (uint8_t)DECIMAL_MAX_SCALE);
/*
If the value still overflows the field with the corrected dec,
we'll throw out decimals rather than integers. This is still
bad and of course throws a truncation warning.
+1: for decimal point
*/
overflow= my_decimal_precision_to_length(intg + dec, dec,
item->unsigned_flag) - len;
if (overflow > 0)
dec= cmax(0, dec - overflow); // too long, discard fract
else
len -= item->decimals - dec; // corrected value fits
}
new_field= new Field_new_decimal(len, maybe_null, item->name,
dec, item->unsigned_flag);
break;
}
case ROW_RESULT:
default:
// This case should never be choosen
assert(0);
new_field= 0;
break;
}
if (new_field)
new_field->init(table);
if (copy_func && item->is_result_field())
*((*copy_func)++) = item; // Save for copy_funcs
if (modify_item)
item->set_result_field(new_field);
if (item->type() == Item::NULL_ITEM)
new_field->is_created_from_null_item= true;
return new_field;
}
Field *create_tmp_field(Session *session, Table *table,Item *item,
Item::Type type, Item ***copy_func, Field **from_field,
Field **default_field, bool group, bool modify_item,
bool, bool make_copy_field,
uint32_t convert_blob_length)
{
Field *result;
Item::Type orig_type= type;
Item *orig_item= 0;
if (type != Item::FIELD_ITEM &&
item->real_item()->type() == Item::FIELD_ITEM)
{
orig_item= item;
item= item->real_item();
type= Item::FIELD_ITEM;
}
switch (type) {
case Item::SUM_FUNC_ITEM:
{
Item_sum *item_sum=(Item_sum*) item;
result= item_sum->create_tmp_field(group, table, convert_blob_length);
if (!result)
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
return result;
}
case Item::FIELD_ITEM:
case Item::DEFAULT_VALUE_ITEM:
{
Item_field *field= (Item_field*) item;
bool orig_modify= modify_item;
if (orig_type == Item::REF_ITEM)
modify_item= 0;
/*
If item have to be able to store NULLs but underlaid field can't do it,
create_tmp_field_from_field() can't be used for tmp field creation.
*/
if (field->maybe_null && !field->field->maybe_null())
{
result= create_tmp_field_from_item(session, item, table, NULL,
modify_item, convert_blob_length);
*from_field= field->field;
if (result && modify_item)
field->result_field= result;
}
else
result= create_tmp_field_from_field(session, (*from_field= field->field),
orig_item ? orig_item->name :
item->name,
table,
modify_item ? field :
NULL,
convert_blob_length);
if (orig_type == Item::REF_ITEM && orig_modify)
((Item_ref*)orig_item)->set_result_field(result);
if (field->field->eq_def(result))
*default_field= field->field;
return result;
}
/* Fall through */
case Item::FUNC_ITEM:
/* Fall through */
case Item::COND_ITEM:
case Item::FIELD_AVG_ITEM:
case Item::FIELD_STD_ITEM:
case Item::SUBSELECT_ITEM:
/* The following can only happen with 'CREATE TABLE ... SELECT' */
case Item::PROC_ITEM:
case Item::INT_ITEM:
case Item::REAL_ITEM:
case Item::DECIMAL_ITEM:
case Item::STRING_ITEM:
case Item::REF_ITEM:
case Item::NULL_ITEM:
case Item::VARBIN_ITEM:
if (make_copy_field)
{
assert(((Item_result_field*)item)->result_field);
*from_field= ((Item_result_field*)item)->result_field;
}
return create_tmp_field_from_item(session, item, table,
(make_copy_field ? 0 : copy_func),
modify_item, convert_blob_length);
case Item::TYPE_HOLDER:
result= ((Item_type_holder *)item)->make_field_by_type(table);
result->set_derivation(item->collation.derivation);
return result;
default: // Dosen't have to be stored
return 0;
}
}
/**
Wrapper of hide_view_error call for Name_resolution_context error
processor.
@note
hide view underlying tables details in error messages
*/
/*****************************************************************************
** Instantiate templates
*****************************************************************************/
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
template class List<Item>;
template class List_iterator<Item>;
template class List_iterator_fast<Item>;
template class List_iterator_fast<Item_field>;
template class List<List_item>;
#endif
|