~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/functions/min_max.cc

code clean up to moving functions into drizzled/functions directory - Item_num_op and Item_numhybrid

Show diffs side-by-side

added added

removed removed

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