~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/function/min_max.cc

  • Committer: Monty Taylor
  • Date: 2008-12-24 01:49:53 UTC
  • mto: This revision was merged to the branch mainline in revision 751.
  • Revision ID: mordred@inaugust.com-20081224014953-rc9p7a162p74y889
Fixed connect.test.

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/function/min_max.h>
 
23
#include <drizzled/item/cmpfunc.h>
 
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
    {
 
54
      session= current_session;
 
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;
 
91
    bool is_null;
 
92
    uint64_t res= get_datetime_value(session, &arg, 0, datetime_item, &is_null);
 
93
    if ((null_value= args[i]->null_value))
 
94
      return 0;
 
95
    if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
 
96
    {
 
97
      min_max= res;
 
98
      min_max_idx= i;
 
99
    }
 
100
  }
 
101
  if (value)
 
102
  {
 
103
    *value= min_max;
 
104
    if (datetime_item->field_type() == DRIZZLE_TYPE_DATE)
 
105
      *value/= 1000000L;
 
106
  }
 
107
  return min_max_idx;
 
108
}
 
109
 
 
110
 
 
111
String *Item_func_min_max::val_str(String *str)
 
112
{
 
113
  assert(fixed == 1);
 
114
  if (compare_as_dates)
 
115
  {
 
116
    String *str_res;
 
117
    uint32_t min_max_idx= cmp_datetimes(NULL);
 
118
    if (null_value)
 
119
      return 0;
 
120
    str_res= args[min_max_idx]->val_str(str);
 
121
    str_res->set_charset(collation.collation);
 
122
    return str_res;
 
123
  }
 
124
  switch (cmp_type) {
 
125
  case INT_RESULT:
 
126
  {
 
127
    int64_t nr=val_int();
 
128
    if (null_value)
 
129
      return 0;
 
130
    str->set_int(nr, unsigned_flag, &my_charset_bin);
 
131
    return str;
 
132
  }
 
133
  case DECIMAL_RESULT:
 
134
  {
 
135
    my_decimal dec_buf, *dec_val= val_decimal(&dec_buf);
 
136
    if (null_value)
 
137
      return 0;
 
138
    my_decimal2string(E_DEC_FATAL_ERROR, dec_val, 0, 0, 0, str);
 
139
    return str;
 
140
  }
 
141
  case REAL_RESULT:
 
142
  {
 
143
    double nr= val_real();
 
144
    if (null_value)
 
145
      return 0; /* purecov: inspected */
 
146
    str->set_real(nr,decimals,&my_charset_bin);
 
147
    return str;
 
148
  }
 
149
  case STRING_RESULT:
 
150
  {
 
151
    String *res= NULL;
 
152
 
 
153
    for (uint32_t i=0; i < arg_count ; i++)
 
154
    {
 
155
      if (i == 0)
 
156
        res=args[i]->val_str(str);
 
157
      else
 
158
      {
 
159
        String *res2;
 
160
        res2= args[i]->val_str(res == str ? &tmp_value : str);
 
161
        if (res2)
 
162
        {
 
163
          int cmp= sortcmp(res,res2,collation.collation);
 
164
          if ((cmp_sign < 0 ? cmp : -cmp) < 0)
 
165
            res=res2;
 
166
        }
 
167
      }
 
168
      if ((null_value= args[i]->null_value))
 
169
        return 0;
 
170
    }
 
171
    res->set_charset(collation.collation);
 
172
    return res;
 
173
  }
 
174
  case ROW_RESULT:
 
175
  default:
 
176
    // This case should never be chosen
 
177
    assert(0);
 
178
    return 0;
 
179
  }
 
180
  return 0;                                     // Keep compiler happy
 
181
}
 
182
 
 
183
 
 
184
double Item_func_min_max::val_real()
 
185
{
 
186
  assert(fixed == 1);
 
187
  double value=0.0;
 
188
  if (compare_as_dates)
 
189
  {
 
190
    uint64_t result= 0;
 
191
    (void)cmp_datetimes(&result);
 
192
    return (double)result;
 
193
  }
 
194
  for (uint32_t i=0; i < arg_count ; i++)
 
195
  {
 
196
    if (i == 0)
 
197
      value= args[i]->val_real();
 
198
    else
 
199
    {
 
200
      double tmp= args[i]->val_real();
 
201
      if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
 
202
        value=tmp;
 
203
    }
 
204
    if ((null_value= args[i]->null_value))
 
205
      break;
 
206
  }
 
207
  return value;
 
208
}
 
209
 
 
210
 
 
211
int64_t Item_func_min_max::val_int()
 
212
{
 
213
  assert(fixed == 1);
 
214
  int64_t value=0;
 
215
  if (compare_as_dates)
 
216
  {
 
217
    uint64_t result= 0;
 
218
    (void)cmp_datetimes(&result);
 
219
    return (int64_t)result;
 
220
  }
 
221
  for (uint32_t i=0; i < arg_count ; i++)
 
222
  {
 
223
    if (i == 0)
 
224
      value=args[i]->val_int();
 
225
    else
 
226
    {
 
227
      int64_t tmp=args[i]->val_int();
 
228
      if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0)
 
229
        value=tmp;
 
230
    }
 
231
    if ((null_value= args[i]->null_value))
 
232
      break;
 
233
  }
 
234
  return value;
 
235
}
 
236
 
 
237
 
 
238
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
 
239
{
 
240
  assert(fixed == 1);
 
241
  my_decimal tmp_buf, *tmp, *res= NULL;
 
242
 
 
243
  if (compare_as_dates)
 
244
  {
 
245
    uint64_t value= 0;
 
246
    (void)cmp_datetimes(&value);
 
247
    uint64_t2decimal(value, dec);
 
248
    return dec;
 
249
  }
 
250
  for (uint32_t i=0; i < arg_count ; i++)
 
251
  {
 
252
    if (i == 0)
 
253
      res= args[i]->val_decimal(dec);
 
254
    else
 
255
    {
 
256
      tmp= args[i]->val_decimal(&tmp_buf);      // Zero if NULL
 
257
      if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
 
258
      {
 
259
        if (tmp == &tmp_buf)
 
260
        {
 
261
          /* Move value out of tmp_buf as this will be reused on next loop */
 
262
          my_decimal2decimal(tmp, dec);
 
263
          res= dec;
 
264
        }
 
265
        else
 
266
          res= tmp;
 
267
      }
 
268
    }
 
269
    if ((null_value= args[i]->null_value))
 
270
    {
 
271
      res= 0;
 
272
      break;
 
273
    }
 
274
  }
 
275
  return res;
 
276
}
 
277