~drizzle-trunk/drizzle/development

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
20
#include <drizzled/server_includes.h>
21
#include CSTDINT_H
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
25
void Item_func_min_max::fix_length_and_dec()
26
{
27
  int max_int_part=0;
28
  bool datetime_found= false;
29
  decimals=0;
30
  max_length=0;
31
  maybe_null=0;
32
  cmp_type=args[0]->result_type();
33
34
  for (uint32_t i=0 ; i < arg_count ; i++)
35
  {
36
    set_if_bigger(max_length, args[i]->max_length);
37
    set_if_bigger(decimals, args[i]->decimals);
38
    set_if_bigger(max_int_part, args[i]->decimal_int_part());
39
    if (args[i]->maybe_null)
40
      maybe_null=1;
41
    cmp_type=item_cmp_type(cmp_type,args[i]->result_type());
42
    if (args[i]->result_type() != ROW_RESULT && args[i]->is_datetime())
43
    {
44
      datetime_found= true;
45
      if (!datetime_item || args[i]->field_type() == DRIZZLE_TYPE_DATETIME)
46
        datetime_item= args[i];
47
    }
48
  }
49
  if (cmp_type == STRING_RESULT)
50
  {
51
    agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
52
    if (datetime_found)
53
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
54
      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
55
      compare_as_dates= true;
56
    }
57
  }
58
  else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT))
59
    max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals,
60
                                            unsigned_flag);
61
  cached_field_type= agg_field_type(args, arg_count);
62
}
63
64
65
/*
66
  Compare item arguments in the DATETIME context.
67
68
  SYNOPSIS
69
    cmp_datetimes()
70
    value [out]   found least/greatest DATE/DATETIME value
71
72
  DESCRIPTION
73
    Compare item arguments as DATETIME values and return the index of the
74
    least/greatest argument in the arguments array.
75
    The correct integer DATE/DATETIME value of the found argument is
76
    stored to the value pointer, if latter is provided.
77
78
  RETURN
79
   0	If one of arguments is NULL
80
   #	index of the least/greatest argument
81
*/
82
83
uint32_t Item_func_min_max::cmp_datetimes(uint64_t *value)
84
{
85
  uint64_t min_max= 0;
86
  uint32_t min_max_idx= 0;
87
88
  for (uint32_t i=0; i < arg_count ; i++)
89
  {
90
    Item **arg= args + i;
779.3.10 by Monty Taylor
Turned on -Wshadow.
91
    bool is_null_unused;
92
    uint64_t res= get_datetime_value(session, &arg, 0, datetime_item,
93
                                     &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
94
    if ((null_value= args[i]->null_value))
95
      return 0;
96
    if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
97
    {
98
      min_max= res;
99
      min_max_idx= i;
100
    }
101
  }
102
  if (value)
103
  {
104
    *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
105
    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
106
      *value/= 1000000L;
107
  }
108
  return min_max_idx;
109
}
110
111
112
String *Item_func_min_max::val_str(String *str)
113
{
114
  assert(fixed == 1);
115
  if (compare_as_dates)
116
  {
117
    String *str_res;
118
    uint32_t min_max_idx= cmp_datetimes(NULL);
119
    if (null_value)
120
      return 0;
121
    str_res= args[min_max_idx]->val_str(str);
122
    str_res->set_charset(collation.collation);
123
    return str_res;
124
  }
125
  switch (cmp_type) {
126
  case INT_RESULT:
127
  {
128
    int64_t nr=val_int();
129
    if (null_value)
130
      return 0;
131
    str->set_int(nr, unsigned_flag, &my_charset_bin);
132
    return str;
133
  }
134
  case DECIMAL_RESULT:
135
  {
136
    my_decimal dec_buf, *dec_val= val_decimal(&dec_buf);
137
    if (null_value)
138
      return 0;
139
    my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str);
140
    return str;
141
  }
142
  case REAL_RESULT:
143
  {
144
    double nr= val_real();
145
    if (null_value)
146
      return 0; /* purecov: inspected */
147
    str->set_real(nr,decimals,&my_charset_bin);
148
    return str;
149
  }
150
  case STRING_RESULT:
151
  {
152
    String *res= NULL;
153
154
    for (uint32_t i=0; i < arg_count ; i++)
155
    {
156
      if (i == 0)
157
	res=args[i]->val_str(str);
158
      else
159
      {
160
	String *res2;
161
	res2= args[i]->val_str(res == str ? &tmp_value : str);
162
	if (res2)
163
	{
164
	  int cmp= sortcmp(res,res2,collation.collation);
165
	  if ((cmp_sign < 0 ? cmp : -cmp) < 0)
166
	    res=res2;
167
	}
168
      }
169
      if ((null_value= args[i]->null_value))
170
        return 0;
171
    }
172
    res->set_charset(collation.collation);
173
    return res;
174
  }
175
  case ROW_RESULT:
176
  default:
177
    // This case should never be chosen
178
    assert(0);
179
    return 0;
180
  }
181
  return 0;					// Keep compiler happy
182
}
183
184
185
double Item_func_min_max::val_real()
186
{
187
  assert(fixed == 1);
188
  double value=0.0;
189
  if (compare_as_dates)
190
  {
191
    uint64_t result= 0;
192
    (void)cmp_datetimes(&result);
193
    return (double)result;
194
  }
195
  for (uint32_t i=0; i < arg_count ; i++)
196
  {
197
    if (i == 0)
198
      value= args[i]->val_real();
199
    else
200
    {
201
      double tmp= args[i]->val_real();
202
      if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
203
	value=tmp;
204
    }
205
    if ((null_value= args[i]->null_value))
206
      break;
207
  }
208
  return value;
209
}
210
211
212
int64_t Item_func_min_max::val_int()
213
{
214
  assert(fixed == 1);
215
  int64_t value=0;
216
  if (compare_as_dates)
217
  {
218
    uint64_t result= 0;
219
    (void)cmp_datetimes(&result);
220
    return (int64_t)result;
221
  }
222
  for (uint32_t i=0; i < arg_count ; i++)
223
  {
224
    if (i == 0)
225
      value=args[i]->val_int();
226
    else
227
    {
228
      int64_t tmp=args[i]->val_int();
229
      if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
230
	value=tmp;
231
    }
232
    if ((null_value= args[i]->null_value))
233
      break;
234
  }
235
  return value;
236
}
237
238
239
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
240
{
241
  assert(fixed == 1);
242
  my_decimal tmp_buf, *tmp, *res= NULL;
243
244
  if (compare_as_dates)
245
  {
246
    uint64_t value= 0;
247
    (void)cmp_datetimes(&value);
248
    uint64_t2decimal(value, dec);
249
    return dec;
250
  }
251
  for (uint32_t i=0; i < arg_count ; i++)
252
  {
253
    if (i == 0)
254
      res= args[i]->val_decimal(dec);
255
    else
256
    {
257
      tmp= args[i]->val_decimal(&tmp_buf);      // Zero if NULL
258
      if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
259
      {
260
        if (tmp == &tmp_buf)
261
        {
262
          /* Move value out of tmp_buf as this will be reused on next loop */
263
          my_decimal2decimal(tmp, dec);
264
          res= dec;
265
        }
266
        else
267
          res= tmp;
268
      }
269
    }
270
    if ((null_value= args[i]->null_value))
271
    {
272
      res= 0;
273
      break;
274
    }
275
  }
276
  return res;
277
}
278