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
|
/* -*- 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 CSTDINT_H
#include <drizzled/function/min_max.h>
#include <drizzled/item/cmpfunc.h>
void Item_func_min_max::fix_length_and_dec()
{
int max_int_part=0;
bool datetime_found= false;
decimals=0;
max_length=0;
maybe_null=0;
cmp_type=args[0]->result_type();
for (uint32_t i=0 ; i < arg_count ; i++)
{
set_if_bigger(max_length, args[i]->max_length);
set_if_bigger(decimals, args[i]->decimals);
set_if_bigger(max_int_part, args[i]->decimal_int_part());
if (args[i]->maybe_null)
maybe_null=1;
cmp_type=item_cmp_type(cmp_type,args[i]->result_type());
if (args[i]->result_type() != ROW_RESULT && args[i]->is_datetime())
{
datetime_found= true;
if (!datetime_item || args[i]->field_type() == DRIZZLE_TYPE_DATETIME)
datetime_item= args[i];
}
}
if (cmp_type == STRING_RESULT)
{
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
if (datetime_found)
{
session= current_session;
compare_as_dates= true;
}
}
else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT))
max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals,
unsigned_flag);
cached_field_type= agg_field_type(args, arg_count);
}
/*
Compare item arguments in the DATETIME context.
SYNOPSIS
cmp_datetimes()
value [out] found least/greatest DATE/DATETIME value
DESCRIPTION
Compare item arguments as DATETIME values and return the index of the
least/greatest argument in the arguments array.
The correct integer DATE/DATETIME value of the found argument is
stored to the value pointer, if latter is provided.
RETURN
0 If one of arguments is NULL
# index of the least/greatest argument
*/
uint32_t Item_func_min_max::cmp_datetimes(uint64_t *value)
{
uint64_t min_max= 0;
uint32_t min_max_idx= 0;
for (uint32_t i=0; i < arg_count ; i++)
{
Item **arg= args + i;
bool is_null_unused;
uint64_t res= get_datetime_value(session, &arg, 0, datetime_item,
&is_null_unused);
if ((null_value= args[i]->null_value))
return 0;
if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
{
min_max= res;
min_max_idx= i;
}
}
if (value)
{
*value= min_max;
if (datetime_item->field_type() == DRIZZLE_TYPE_DATE)
*value/= 1000000L;
}
return min_max_idx;
}
String *Item_func_min_max::val_str(String *str)
{
assert(fixed == 1);
if (compare_as_dates)
{
String *str_res;
uint32_t min_max_idx= cmp_datetimes(NULL);
if (null_value)
return 0;
str_res= args[min_max_idx]->val_str(str);
str_res->set_charset(collation.collation);
return str_res;
}
switch (cmp_type) {
case INT_RESULT:
{
int64_t nr=val_int();
if (null_value)
return 0;
str->set_int(nr, unsigned_flag, &my_charset_bin);
return str;
}
case DECIMAL_RESULT:
{
my_decimal dec_buf, *dec_val= val_decimal(&dec_buf);
if (null_value)
return 0;
my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str);
return str;
}
case REAL_RESULT:
{
double nr= val_real();
if (null_value)
return 0; /* purecov: inspected */
str->set_real(nr,decimals,&my_charset_bin);
return str;
}
case STRING_RESULT:
{
String *res= NULL;
for (uint32_t i=0; i < arg_count ; i++)
{
if (i == 0)
res=args[i]->val_str(str);
else
{
String *res2;
res2= args[i]->val_str(res == str ? &tmp_value : str);
if (res2)
{
int cmp= sortcmp(res,res2,collation.collation);
if ((cmp_sign < 0 ? cmp : -cmp) < 0)
res=res2;
}
}
if ((null_value= args[i]->null_value))
return 0;
}
res->set_charset(collation.collation);
return res;
}
case ROW_RESULT:
default:
// This case should never be chosen
assert(0);
return 0;
}
return 0; // Keep compiler happy
}
double Item_func_min_max::val_real()
{
assert(fixed == 1);
double value=0.0;
if (compare_as_dates)
{
uint64_t result= 0;
(void)cmp_datetimes(&result);
return (double)result;
}
for (uint32_t i=0; i < arg_count ; i++)
{
if (i == 0)
value= args[i]->val_real();
else
{
double tmp= args[i]->val_real();
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
value=tmp;
}
if ((null_value= args[i]->null_value))
break;
}
return value;
}
int64_t Item_func_min_max::val_int()
{
assert(fixed == 1);
int64_t value=0;
if (compare_as_dates)
{
uint64_t result= 0;
(void)cmp_datetimes(&result);
return (int64_t)result;
}
for (uint32_t i=0; i < arg_count ; i++)
{
if (i == 0)
value=args[i]->val_int();
else
{
int64_t tmp=args[i]->val_int();
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
value=tmp;
}
if ((null_value= args[i]->null_value))
break;
}
return value;
}
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
{
assert(fixed == 1);
my_decimal tmp_buf, *tmp, *res= NULL;
if (compare_as_dates)
{
uint64_t value= 0;
(void)cmp_datetimes(&value);
uint64_t2decimal(value, dec);
return dec;
}
for (uint32_t i=0; i < arg_count ; i++)
{
if (i == 0)
res= args[i]->val_decimal(dec);
else
{
tmp= args[i]->val_decimal(&tmp_buf); // Zero if NULL
if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
{
if (tmp == &tmp_buf)
{
/* Move value out of tmp_buf as this will be reused on next loop */
my_decimal2decimal(tmp, dec);
res= dec;
}
else
res= tmp;
}
}
if ((null_value= args[i]->null_value))
{
res= 0;
break;
}
}
return res;
}
|