~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/min_max.cc

  • Committer: Jay Pipes
  • Date: 2008-07-17 17:54:00 UTC
  • mto: This revision was merged to the branch mainline in revision 182.
  • Revision ID: jay@mysql.com-20080717175400-xm2aazihjra8mdzq
Removal of DBUG from libdrizzle/ - Round 2

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