492.3.13
by Lee
code clean move Item_func_floor, Item_func_length, Item_func_min_max, Item_func_rand, Item_func_round to functions directory |
1 |
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
|
2 |
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
|
|
3 |
*
|
|
4 |
* Copyright (C) 2008 Sun Microsystems
|
|
5 |
*
|
|
6 |
* This program is free software; you can redistribute it and/or modify
|
|
7 |
* it under the terms of the GNU General Public License as published by
|
|
8 |
* the Free Software Foundation; version 2 of the License.
|
|
9 |
*
|
|
10 |
* This program is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
13 |
* GNU General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License
|
|
16 |
* along with this program; if not, write to the Free Software
|
|
17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
18 |
*/
|
|
19 |
||
1241.9.36
by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h. |
20 |
#include "config.h" |
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
21 |
|
670.1.20
by Monty Taylor
Renamed functions to function... everything else is singular. |
22 |
#include <drizzled/function/min_max.h> |
584.4.7
by Monty Taylor
Removed a big bank of includes from item.h. |
23 |
#include <drizzled/item/cmpfunc.h> |
492.3.13
by Lee
code clean move Item_func_floor, Item_func_length, Item_func_min_max, Item_func_rand, Item_func_round to functions directory |
24 |
|
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
25 |
namespace drizzled |
26 |
{
|
|
27 |
||
492.3.13
by Lee
code clean move Item_func_floor, Item_func_length, Item_func_min_max, Item_func_rand, Item_func_round to functions directory |
28 |
void Item_func_min_max::fix_length_and_dec() |
29 |
{
|
|
30 |
int max_int_part=0; |
|
31 |
bool datetime_found= false; |
|
32 |
decimals=0; |
|
33 |
max_length=0; |
|
34 |
maybe_null=0; |
|
35 |
cmp_type=args[0]->result_type(); |
|
36 |
||
37 |
for (uint32_t i=0 ; i < arg_count ; i++) |
|
38 |
{
|
|
39 |
set_if_bigger(max_length, args[i]->max_length); |
|
40 |
set_if_bigger(decimals, args[i]->decimals); |
|
41 |
set_if_bigger(max_int_part, args[i]->decimal_int_part()); |
|
42 |
if (args[i]->maybe_null) |
|
43 |
maybe_null=1; |
|
44 |
cmp_type=item_cmp_type(cmp_type,args[i]->result_type()); |
|
45 |
if (args[i]->result_type() != ROW_RESULT && args[i]->is_datetime()) |
|
46 |
{
|
|
47 |
datetime_found= true; |
|
48 |
if (!datetime_item || args[i]->field_type() == DRIZZLE_TYPE_DATETIME) |
|
49 |
datetime_item= args[i]; |
|
50 |
}
|
|
51 |
}
|
|
52 |
if (cmp_type == STRING_RESULT) |
|
53 |
{
|
|
54 |
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1); |
|
55 |
if (datetime_found) |
|
56 |
{
|
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
57 |
session= current_session; |
492.3.13
by Lee
code clean move Item_func_floor, Item_func_length, Item_func_min_max, Item_func_rand, Item_func_round to functions directory |
58 |
compare_as_dates= true; |
59 |
}
|
|
60 |
}
|
|
61 |
else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT)) |
|
62 |
max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals, |
|
63 |
unsigned_flag); |
|
64 |
cached_field_type= agg_field_type(args, arg_count); |
|
65 |
}
|
|
66 |
||
67 |
||
68 |
/*
|
|
69 |
Compare item arguments in the DATETIME context.
|
|
70 |
||
71 |
SYNOPSIS
|
|
72 |
cmp_datetimes()
|
|
73 |
value [out] found least/greatest DATE/DATETIME value
|
|
74 |
||
75 |
DESCRIPTION
|
|
76 |
Compare item arguments as DATETIME values and return the index of the
|
|
77 |
least/greatest argument in the arguments array.
|
|
78 |
The correct integer DATE/DATETIME value of the found argument is
|
|
79 |
stored to the value pointer, if latter is provided.
|
|
80 |
||
81 |
RETURN
|
|
82 |
0 If one of arguments is NULL
|
|
83 |
# index of the least/greatest argument
|
|
84 |
*/
|
|
85 |
||
86 |
uint32_t Item_func_min_max::cmp_datetimes(uint64_t *value) |
|
87 |
{
|
|
88 |
uint64_t min_max= 0; |
|
89 |
uint32_t min_max_idx= 0; |
|
90 |
||
91 |
for (uint32_t i=0; i < arg_count ; i++) |
|
92 |
{
|
|
93 |
Item **arg= args + i; |
|
779.3.10
by Monty Taylor
Turned on -Wshadow. |
94 |
bool is_null_unused; |
95 |
uint64_t res= get_datetime_value(session, &arg, 0, datetime_item, |
|
96 |
&is_null_unused); |
|
492.3.13
by Lee
code clean move Item_func_floor, Item_func_length, Item_func_min_max, Item_func_rand, Item_func_round to functions directory |
97 |
if ((null_value= args[i]->null_value)) |
98 |
return 0; |
|
99 |
if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0) |
|
100 |
{
|
|
101 |
min_max= res; |
|
102 |
min_max_idx= i; |
|
103 |
}
|
|
104 |
}
|
|
105 |
if (value) |
|
106 |
{
|
|
107 |
*value= min_max; |
|
575.5.1
by David Axmark
Changed NEWDATE to DATE. One failing test but I think its somewhere else in the code |
108 |
if (datetime_item->field_type() == DRIZZLE_TYPE_DATE) |
492.3.13
by Lee
code clean move Item_func_floor, Item_func_length, Item_func_min_max, Item_func_rand, Item_func_round to functions directory |
109 |
*value/= 1000000L; |
110 |
}
|
|
111 |
return min_max_idx; |
|
112 |
}
|
|
113 |
||
114 |
||
115 |
String *Item_func_min_max::val_str(String *str) |
|
116 |
{
|
|
117 |
assert(fixed == 1); |
|
118 |
if (compare_as_dates) |
|
119 |
{
|
|
120 |
String *str_res; |
|
121 |
uint32_t min_max_idx= cmp_datetimes(NULL); |
|
122 |
if (null_value) |
|
123 |
return 0; |
|
124 |
str_res= args[min_max_idx]->val_str(str); |
|
125 |
str_res->set_charset(collation.collation); |
|
126 |
return str_res; |
|
127 |
}
|
|
128 |
switch (cmp_type) { |
|
129 |
case INT_RESULT: |
|
130 |
{
|
|
131 |
int64_t nr=val_int(); |
|
132 |
if (null_value) |
|
133 |
return 0; |
|
134 |
str->set_int(nr, unsigned_flag, &my_charset_bin); |
|
135 |
return str; |
|
136 |
}
|
|
137 |
case DECIMAL_RESULT: |
|
138 |
{
|
|
139 |
my_decimal dec_buf, *dec_val= val_decimal(&dec_buf); |
|
140 |
if (null_value) |
|
141 |
return 0; |
|
142 |
my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str); |
|
143 |
return str; |
|
144 |
}
|
|
145 |
case REAL_RESULT: |
|
146 |
{
|
|
147 |
double nr= val_real(); |
|
148 |
if (null_value) |
|
971.6.11
by Eric Day
Removed purecov messages. |
149 |
return 0; |
492.3.13
by Lee
code clean move Item_func_floor, Item_func_length, Item_func_min_max, Item_func_rand, Item_func_round to functions directory |
150 |
str->set_real(nr,decimals,&my_charset_bin); |
151 |
return str; |
|
152 |
}
|
|
153 |
case STRING_RESULT: |
|
154 |
{
|
|
155 |
String *res= NULL; |
|
156 |
||
157 |
for (uint32_t i=0; i < arg_count ; i++) |
|
158 |
{
|
|
159 |
if (i == 0) |
|
160 |
res=args[i]->val_str(str); |
|
161 |
else
|
|
162 |
{
|
|
163 |
String *res2; |
|
164 |
res2= args[i]->val_str(res == str ? &tmp_value : str); |
|
165 |
if (res2) |
|
166 |
{
|
|
167 |
int cmp= sortcmp(res,res2,collation.collation); |
|
168 |
if ((cmp_sign < 0 ? cmp : -cmp) < 0) |
|
169 |
res=res2; |
|
170 |
}
|
|
171 |
}
|
|
172 |
if ((null_value= args[i]->null_value)) |
|
173 |
return 0; |
|
174 |
}
|
|
175 |
res->set_charset(collation.collation); |
|
176 |
return res; |
|
177 |
}
|
|
178 |
case ROW_RESULT: |
|
179 |
default: |
|
180 |
// This case should never be chosen
|
|
181 |
assert(0); |
|
182 |
return 0; |
|
183 |
}
|
|
184 |
return 0; // Keep compiler happy |
|
185 |
}
|
|
186 |
||
187 |
||
188 |
double Item_func_min_max::val_real() |
|
189 |
{
|
|
190 |
assert(fixed == 1); |
|
191 |
double value=0.0; |
|
192 |
if (compare_as_dates) |
|
193 |
{
|
|
194 |
uint64_t result= 0; |
|
195 |
(void)cmp_datetimes(&result); |
|
196 |
return (double)result; |
|
197 |
}
|
|
198 |
for (uint32_t i=0; i < arg_count ; i++) |
|
199 |
{
|
|
200 |
if (i == 0) |
|
201 |
value= args[i]->val_real(); |
|
202 |
else
|
|
203 |
{
|
|
204 |
double tmp= args[i]->val_real(); |
|
205 |
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0) |
|
206 |
value=tmp; |
|
207 |
}
|
|
208 |
if ((null_value= args[i]->null_value)) |
|
209 |
break; |
|
210 |
}
|
|
211 |
return value; |
|
212 |
}
|
|
213 |
||
214 |
||
215 |
int64_t Item_func_min_max::val_int() |
|
216 |
{
|
|
217 |
assert(fixed == 1); |
|
218 |
int64_t value=0; |
|
219 |
if (compare_as_dates) |
|
220 |
{
|
|
221 |
uint64_t result= 0; |
|
222 |
(void)cmp_datetimes(&result); |
|
223 |
return (int64_t)result; |
|
224 |
}
|
|
225 |
for (uint32_t i=0; i < arg_count ; i++) |
|
226 |
{
|
|
227 |
if (i == 0) |
|
228 |
value=args[i]->val_int(); |
|
229 |
else
|
|
230 |
{
|
|
231 |
int64_t tmp=args[i]->val_int(); |
|
232 |
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0) |
|
233 |
value=tmp; |
|
234 |
}
|
|
235 |
if ((null_value= args[i]->null_value)) |
|
236 |
break; |
|
237 |
}
|
|
238 |
return value; |
|
239 |
}
|
|
240 |
||
241 |
||
242 |
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec) |
|
243 |
{
|
|
244 |
assert(fixed == 1); |
|
245 |
my_decimal tmp_buf, *tmp, *res= NULL; |
|
246 |
||
247 |
if (compare_as_dates) |
|
248 |
{
|
|
249 |
uint64_t value= 0; |
|
250 |
(void)cmp_datetimes(&value); |
|
251 |
uint64_t2decimal(value, dec); |
|
252 |
return dec; |
|
253 |
}
|
|
254 |
for (uint32_t i=0; i < arg_count ; i++) |
|
255 |
{
|
|
256 |
if (i == 0) |
|
257 |
res= args[i]->val_decimal(dec); |
|
258 |
else
|
|
259 |
{
|
|
260 |
tmp= args[i]->val_decimal(&tmp_buf); // Zero if NULL |
|
261 |
if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0) |
|
262 |
{
|
|
263 |
if (tmp == &tmp_buf) |
|
264 |
{
|
|
265 |
/* Move value out of tmp_buf as this will be reused on next loop */
|
|
266 |
my_decimal2decimal(tmp, dec); |
|
267 |
res= dec; |
|
268 |
}
|
|
269 |
else
|
|
270 |
res= tmp; |
|
271 |
}
|
|
272 |
}
|
|
273 |
if ((null_value= args[i]->null_value)) |
|
274 |
{
|
|
275 |
res= 0; |
|
276 |
break; |
|
277 |
}
|
|
278 |
}
|
|
279 |
return res; |
|
280 |
}
|
|
281 |
||
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
282 |
} /* namespace drizzled */ |