1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
20
#include <drizzled/server_includes.h>
22
#include <drizzled/functions/min_max.h>
24
void Item_func_min_max::fix_length_and_dec()
27
bool datetime_found= false;
31
cmp_type=args[0]->result_type();
33
for (uint32_t i=0 ; i < arg_count ; i++)
35
set_if_bigger(max_length, args[i]->max_length);
36
set_if_bigger(decimals, args[i]->decimals);
37
set_if_bigger(max_int_part, args[i]->decimal_int_part());
38
if (args[i]->maybe_null)
40
cmp_type=item_cmp_type(cmp_type,args[i]->result_type());
41
if (args[i]->result_type() != ROW_RESULT && args[i]->is_datetime())
44
if (!datetime_item || args[i]->field_type() == DRIZZLE_TYPE_DATETIME)
45
datetime_item= args[i];
48
if (cmp_type == STRING_RESULT)
50
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
54
compare_as_dates= true;
57
else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT))
58
max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals,
60
cached_field_type= agg_field_type(args, arg_count);
65
Compare item arguments in the DATETIME context.
69
value [out] found least/greatest DATE/DATETIME value
72
Compare item arguments as DATETIME values and return the index of the
73
least/greatest argument in the arguments array.
74
The correct integer DATE/DATETIME value of the found argument is
75
stored to the value pointer, if latter is provided.
78
0 If one of arguments is NULL
79
# index of the least/greatest argument
82
uint32_t Item_func_min_max::cmp_datetimes(uint64_t *value)
85
uint32_t min_max_idx= 0;
87
for (uint32_t i=0; i < arg_count ; i++)
91
uint64_t res= get_datetime_value(thd, &arg, 0, datetime_item, &is_null);
92
if ((null_value= args[i]->null_value))
94
if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
103
if (datetime_item->field_type() == DRIZZLE_TYPE_NEWDATE)
110
String *Item_func_min_max::val_str(String *str)
113
if (compare_as_dates)
116
uint32_t min_max_idx= cmp_datetimes(NULL);
119
str_res= args[min_max_idx]->val_str(str);
120
str_res->set_charset(collation.collation);
126
int64_t nr=val_int();
129
str->set_int(nr, unsigned_flag, &my_charset_bin);
134
my_decimal dec_buf, *dec_val= val_decimal(&dec_buf);
137
my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str);
142
double nr= val_real();
144
return 0; /* purecov: inspected */
145
str->set_real(nr,decimals,&my_charset_bin);
152
for (uint32_t i=0; i < arg_count ; i++)
155
res=args[i]->val_str(str);
159
res2= args[i]->val_str(res == str ? &tmp_value : str);
162
int cmp= sortcmp(res,res2,collation.collation);
163
if ((cmp_sign < 0 ? cmp : -cmp) < 0)
167
if ((null_value= args[i]->null_value))
170
res->set_charset(collation.collation);
175
// This case should never be chosen
179
return 0; // Keep compiler happy
183
double Item_func_min_max::val_real()
187
if (compare_as_dates)
190
(void)cmp_datetimes(&result);
191
return (double)result;
193
for (uint32_t i=0; i < arg_count ; i++)
196
value= args[i]->val_real();
199
double tmp= args[i]->val_real();
200
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
203
if ((null_value= args[i]->null_value))
210
int64_t Item_func_min_max::val_int()
214
if (compare_as_dates)
217
(void)cmp_datetimes(&result);
218
return (int64_t)result;
220
for (uint32_t i=0; i < arg_count ; i++)
223
value=args[i]->val_int();
226
int64_t tmp=args[i]->val_int();
227
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
230
if ((null_value= args[i]->null_value))
237
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
240
my_decimal tmp_buf, *tmp, *res= NULL;
242
if (compare_as_dates)
245
(void)cmp_datetimes(&value);
246
uint64_t2decimal(value, dec);
249
for (uint32_t i=0; i < arg_count ; i++)
252
res= args[i]->val_decimal(dec);
255
tmp= args[i]->val_decimal(&tmp_buf); // Zero if NULL
256
if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
260
/* Move value out of tmp_buf as this will be reused on next loop */
261
my_decimal2decimal(tmp, dec);
268
if ((null_value= args[i]->null_value))