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
22
#include <drizzled/function/min_max.h>
23
#include <drizzled/item/cmpfunc.h>
28
void Item_func_min_max::fix_length_and_dec()
31
bool datetime_found= false;
35
cmp_type=args[0]->result_type();
37
for (uint32_t i=0 ; i < arg_count ; i++)
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)
44
cmp_type=item_cmp_type(cmp_type,args[i]->result_type());
45
if (args[i]->result_type() != ROW_RESULT && args[i]->is_datetime())
48
if (!datetime_item || args[i]->field_type() == DRIZZLE_TYPE_DATETIME)
49
datetime_item= args[i];
52
if (cmp_type == STRING_RESULT)
54
agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
57
session= getSessionPtr();
58
compare_as_dates= true;
61
else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT))
62
max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals,
64
cached_field_type= agg_field_type(args, arg_count);
69
Compare item arguments in the DATETIME context.
73
value [out] found least/greatest DATE/DATETIME value
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.
82
0 If one of arguments is NULL or there was a execution error
83
# index of the least/greatest argument
86
uint32_t Item_func_min_max::cmp_datetimes(uint64_t *value)
89
uint32_t min_max_idx= 0;
91
for (uint32_t i=0; i < arg_count ; i++)
95
uint64_t res= get_datetime_value(session, &arg, 0, datetime_item,
98
/* Check if we need to stop (because of error or KILL) and stop the loop */
99
if (session->is_error())
105
if ((null_value= args[i]->null_value))
107
if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
116
if (datetime_item->field_type() == DRIZZLE_TYPE_DATE)
123
String *Item_func_min_max::val_str(String *str)
126
if (compare_as_dates)
129
uint32_t min_max_idx= cmp_datetimes(NULL);
132
str_res= args[min_max_idx]->val_str(str);
133
if (args[min_max_idx]->null_value)
135
// check if the call to val_str() above returns a NULL value
139
str_res->set_charset(collation.collation);
145
int64_t nr=val_int();
148
str->set_int(nr, unsigned_flag, &my_charset_bin);
154
my_decimal dec_buf, *dec_val= val_decimal(&dec_buf);
157
my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str);
163
double nr= val_real();
166
str->set_real(nr,decimals,&my_charset_bin);
174
for (uint32_t i=0; i < arg_count ; i++)
177
res=args[i]->val_str(str);
181
res2= args[i]->val_str(res == str ? &tmp_value : str);
184
int cmp= sortcmp(res,res2,collation.collation);
185
if ((cmp_sign < 0 ? cmp : -cmp) < 0)
189
if ((null_value= args[i]->null_value))
192
res->set_charset(collation.collation);
197
// This case should never be chosen
202
return 0; // Keep compiler happy
206
double Item_func_min_max::val_real()
210
if (compare_as_dates)
213
(void)cmp_datetimes(&result);
214
return (double)result;
216
for (uint32_t i=0; i < arg_count ; i++)
219
value= args[i]->val_real();
222
double tmp= args[i]->val_real();
223
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
226
if ((null_value= args[i]->null_value))
233
int64_t Item_func_min_max::val_int()
237
if (compare_as_dates)
240
(void)cmp_datetimes(&result);
241
return (int64_t)result;
243
for (uint32_t i=0; i < arg_count ; i++)
246
value=args[i]->val_int();
249
int64_t tmp=args[i]->val_int();
250
if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
253
if ((null_value= args[i]->null_value))
260
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
263
my_decimal tmp_buf, *tmp, *res= NULL;
265
if (compare_as_dates)
268
(void)cmp_datetimes(&value);
269
uint64_t2decimal(value, dec);
272
for (uint32_t i=0; i < arg_count ; i++)
275
res= args[i]->val_decimal(dec);
278
tmp= args[i]->val_decimal(&tmp_buf); // Zero if NULL
279
if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
283
/* Move value out of tmp_buf as this will be reused on next loop */
284
my_decimal2decimal(tmp, dec);
291
if ((null_value= args[i]->null_value))
300
} /* namespace drizzled */