~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/functions/min_max.cc

  • Committer: Brian Aker
  • Date: 2008-10-29 13:46:43 UTC
  • Revision ID: brian@tangent.org-20081029134643-z6jcwjvyruhk2vlu
Updates for ignore file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
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
 
{
 
20
#include <drizzled/server_includes.h>
 
21
#include CSTDINT_H
 
22
#include <drizzled/functions/min_max.h>
28
23
 
29
24
void Item_func_min_max::fix_length_and_dec()
30
25
{
55
50
    agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
56
51
    if (datetime_found)
57
52
    {
58
 
      session= getSessionPtr();
 
53
      session= current_session;
59
54
      compare_as_dates= true;
60
55
    }
61
56
  }
62
57
  else if ((cmp_type == DECIMAL_RESULT) || (cmp_type == INT_RESULT))
63
 
    max_length= class_decimal_precision_to_length(max_int_part+decimals, decimals,
 
58
    max_length= my_decimal_precision_to_length(max_int_part+decimals, decimals,
64
59
                                            unsigned_flag);
65
60
  cached_field_type= agg_field_type(args, arg_count);
66
61
}
80
75
    stored to the value pointer, if latter is provided.
81
76
 
82
77
  RETURN
83
 
   0    If one of arguments is NULL or there was a execution error
 
78
   0    If one of arguments is NULL
84
79
   #    index of the least/greatest argument
85
80
*/
86
81
 
92
87
  for (uint32_t i=0; i < arg_count ; i++)
93
88
  {
94
89
    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
 
 
 
90
    bool is_null;
 
91
    uint64_t res= get_datetime_value(session, &arg, 0, datetime_item, &is_null);
106
92
    if ((null_value= args[i]->null_value))
107
93
      return 0;
108
94
    if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
114
100
  if (value)
115
101
  {
116
102
    *value= min_max;
117
 
    if (datetime_item->field_type() == DRIZZLE_TYPE_DATE)
 
103
    if (datetime_item->field_type() == DRIZZLE_TYPE_NEWDATE)
118
104
      *value/= 1000000L;
119
105
  }
120
106
  return min_max_idx;
131
117
    if (null_value)
132
118
      return 0;
133
119
    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
120
    str_res->set_charset(collation.collation);
141
121
    return str_res;
142
122
  }
143
123
  switch (cmp_type) {
144
124
  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
 
 
 
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
  }
153
132
  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
 
 
 
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
  }
162
140
  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
 
 
 
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
  }
171
148
  case STRING_RESULT:
 
149
  {
 
150
    String *res= NULL;
 
151
 
 
152
    for (uint32_t i=0; i < arg_count ; i++)
172
153
    {
173
 
      String *res= NULL;
174
 
 
175
 
      for (uint32_t i=0; i < arg_count ; i++)
 
154
      if (i == 0)
 
155
        res=args[i]->val_str(str);
 
156
      else
176
157
      {
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;
 
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
        }
192
166
      }
193
 
      res->set_charset(collation.collation);
194
 
      return res;
 
167
      if ((null_value= args[i]->null_value))
 
168
        return 0;
195
169
    }
196
 
 
 
170
    res->set_charset(collation.collation);
 
171
    return res;
 
172
  }
197
173
  case ROW_RESULT:
 
174
  default:
198
175
    // This case should never be chosen
199
176
    assert(0);
200
177
    return 0;
201
178
  }
202
 
 
203
179
  return 0;                                     // Keep compiler happy
204
180
}
205
181
 
258
234
}
259
235
 
260
236
 
261
 
type::Decimal *Item_func_min_max::val_decimal(type::Decimal *dec)
 
237
my_decimal *Item_func_min_max::val_decimal(my_decimal *dec)
262
238
{
263
239
  assert(fixed == 1);
264
 
  type::Decimal tmp_buf, *tmp, *res= NULL;
 
240
  my_decimal tmp_buf, *tmp, *res= NULL;
265
241
 
266
242
  if (compare_as_dates)
267
243
  {
277
253
    else
278
254
    {
279
255
      tmp= args[i]->val_decimal(&tmp_buf);      // Zero if NULL
280
 
      if (tmp && (class_decimal_cmp(tmp, res) * cmp_sign) < 0)
 
256
      if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0)
281
257
      {
282
258
        if (tmp == &tmp_buf)
283
259
        {
284
260
          /* Move value out of tmp_buf as this will be reused on next loop */
285
 
          class_decimal2decimal(tmp, dec);
 
261
          my_decimal2decimal(tmp, dec);
286
262
          res= dec;
287
263
        }
288
264
        else
298
274
  return res;
299
275
}
300
276
 
301
 
} /* namespace drizzled */