Drizzled Public API Documentation

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