Drizzled Public API Documentation

item.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-2009 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 #include <drizzled/sql_select.h>
22 #include <drizzled/error.h>
23 #include <drizzled/show.h>
24 #include <drizzled/item/cmpfunc.h>
25 #include <drizzled/item/cache_row.h>
26 #include <drizzled/item/type_holder.h>
27 #include <drizzled/item/sum.h>
28 #include <drizzled/item/copy_string.h>
29 #include <drizzled/function/str/conv_charset.h>
30 #include <drizzled/sql_base.h>
31 #include <drizzled/util/convert.h>
32 #include <drizzled/plugin/client.h>
33 #include <drizzled/time_functions.h>
34 #include <drizzled/field/str.h>
35 #include <drizzled/field/num.h>
36 #include <drizzled/field/blob.h>
37 #include <drizzled/field/date.h>
38 #include <drizzled/field/datetime.h>
39 #include <drizzled/field/decimal.h>
40 #include <drizzled/field/double.h>
41 #include <drizzled/field/enum.h>
42 #include <drizzled/field/epoch.h>
43 #include <drizzled/field/int32.h>
44 #include <drizzled/field/int64.h>
45 #include <drizzled/field/microtime.h>
46 #include <drizzled/field/null.h>
47 #include <drizzled/field/real.h>
48 #include <drizzled/field/size.h>
49 #include <drizzled/field/time.h>
50 #include <drizzled/field/varstring.h>
51 #include <drizzled/current_session.h>
52 #include <drizzled/session.h>
53 #include <drizzled/internal/m_string.h>
54 #include <drizzled/item/ref.h>
55 #include <drizzled/item/subselect.h>
56 #include <drizzled/sql_lex.h>
57 #include <drizzled/system_variables.h>
58 
59 #include <cstdio>
60 #include <math.h>
61 #include <algorithm>
62 #include <float.h>
63 
64 using namespace std;
65 
66 namespace drizzled {
67 
68 const String my_null_string("NULL", 4, default_charset_info);
69 
70 bool Item::is_expensive_processor(unsigned char *)
71 {
72  return false;
73 }
74 
75 void Item::fix_after_pullout(Select_Lex *, Item **)
76 {}
77 
78 Field *Item::tmp_table_field(Table *)
79 {
80  return NULL;
81 }
82 
83 const char *Item::full_name(void) const
84 {
85  return name ? name : "???";
86 }
87 
88 int64_t Item::val_int_endpoint(bool, bool *)
89 {
90  assert(0);
91  return 0;
92 }
93 
95 bool Item::val_bool()
96 {
97  switch(result_type())
98  {
99  case INT_RESULT:
100  return val_int() != 0;
101 
102  case DECIMAL_RESULT:
103  {
104  type::Decimal decimal_value;
105  type::Decimal *val= val_decimal(&decimal_value);
106  if (val)
107  return not val->isZero();
108  return false;
109  }
110 
111  case REAL_RESULT:
112  case STRING_RESULT:
113  return val_real() != 0.0;
114 
115  case ROW_RESULT:
116  assert(0);
117  abort();
118  }
119 
120  assert(0);
121  abort();
122 }
123 
124 String *Item::val_string_from_real(String *str)
125 {
126  double nr= val_real();
127  if (null_value)
128  return NULL;
129 
130  str->set_real(nr, decimals, &my_charset_bin);
131  return str;
132 }
133 
134 String *Item::val_string_from_int(String *str)
135 {
136  int64_t nr= val_int();
137  if (null_value)
138  return NULL;
139 
140  str->set_int(nr, unsigned_flag, &my_charset_bin);
141  return str;
142 }
143 
144 String *Item::val_string_from_decimal(String *str)
145 {
146  type::Decimal dec_buf, *dec= val_decimal(&dec_buf);
147  if (null_value)
148  return NULL;
149 
150  class_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
151  class_decimal2string(&dec_buf, 0, str);
152  return str;
153 }
154 
155 type::Decimal *Item::val_decimal_from_real(type::Decimal *decimal_value)
156 {
157  double nr= val_real();
158  if (null_value)
159  return NULL;
160 
161  double2_class_decimal(E_DEC_FATAL_ERROR, nr, decimal_value);
162  return (decimal_value);
163 }
164 
165 type::Decimal *Item::val_decimal_from_int(type::Decimal *decimal_value)
166 {
167  int64_t nr= val_int();
168  if (null_value)
169  return NULL;
170 
171  int2_class_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
172  return decimal_value;
173 }
174 
175 type::Decimal *Item::val_decimal_from_string(type::Decimal *decimal_value)
176 {
177  String *res;
178  if (!(res= val_str(&str_value)))
179  return NULL;
180 
181  if (decimal_value->store(E_DEC_FATAL_ERROR & ~E_DEC_BAD_NUM,
182  res->ptr(),
183  res->length(),
184  res->charset()) & E_DEC_BAD_NUM)
185  {
186  push_warning_printf(&getSession(),
187  DRIZZLE_ERROR::WARN_LEVEL_WARN,
188  ER_TRUNCATED_WRONG_VALUE,
189  ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
190  str_value.c_ptr());
191  }
192  return decimal_value;
193 }
194 
195 type::Decimal *Item::val_decimal_from_date(type::Decimal *decimal_value)
196 {
197  assert(fixed);
198  type::Time ltime;
199  if (get_date(ltime, TIME_FUZZY_DATE))
200  {
201  decimal_value->set_zero();
202  null_value= 1; // set NULL, stop processing
203  return NULL;
204  }
205  return date2_class_decimal(&ltime, decimal_value);
206 }
207 
208 type::Decimal *Item::val_decimal_from_time(type::Decimal *decimal_value)
209 {
210  assert(fixed);
211  type::Time ltime;
212  if (get_time(ltime))
213  {
214  decimal_value->set_zero();
215  return NULL;
216  }
217  return date2_class_decimal(&ltime, decimal_value);
218 }
219 
220 double Item::val_real_from_decimal()
221 {
222  /* Note that fix_fields may not be called for Item_avg_field items */
223  double result;
224  type::Decimal value_buff, *dec_val= val_decimal(&value_buff);
225  if (null_value)
226  return 0.0;
227  class_decimal2double(E_DEC_FATAL_ERROR, dec_val, &result);
228  return result;
229 }
230 
231 int64_t Item::val_int_from_decimal()
232 {
233  /* Note that fix_fields may not be called for Item_avg_field items */
234  int64_t result;
235  type::Decimal value, *dec_val= val_decimal(&value);
236 
237  if (null_value)
238  return 0;
239  dec_val->val_int32(E_DEC_FATAL_ERROR, unsigned_flag, &result);
240 
241  return result;
242 }
243 
244 bool Item::save_time_in_field(Field *field)
245 {
246  type::Time ltime;
247 
248  if (get_time(ltime))
249  return set_field_to_null(field);
250 
251  field->set_notnull();
252 
253  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_TIME);
254 }
255 
256 bool Item::save_date_in_field(Field *field)
257 {
258  type::Time ltime;
259 
260  if (get_date(ltime, TIME_FUZZY_DATE))
261  return set_field_to_null(field);
262 
263  field->set_notnull();
264 
265  return field->store_time(ltime, type::DRIZZLE_TIMESTAMP_DATETIME);
266 }
267 
272 int Item::save_str_value_in_field(Field *field, String *result)
273 {
274  if (null_value)
275  return set_field_to_null(field);
276 
277  field->set_notnull();
278 
279  return field->store(result->ptr(), result->length(), collation.collation);
280 }
281 
282 Item::Item():
283  is_expensive_cache(-1),
284  name(0),
285  name_length(0),
286  max_length(0),
287  marker(0),
288  decimals(0),
289  fixed(false),
290  maybe_null(false),
291  null_value(false),
292  unsigned_flag(false),
293  with_sum_func(false),
294  is_autogenerated_name(true),
295  with_subselect(false),
296  collation(&my_charset_bin, DERIVATION_COERCIBLE),
297  _session(*current_session)
298 {
299  cmp_context= (Item_result)-1;
300 
301  /* Put item in free list so that we can free all items at end */
302  next= getSession().free_list;
303  getSession().free_list= this;
304 
305  /*
306  Item constructor can be called during execution other then SQL_COM
307  command => we should check session->lex().current_select on zero (session->lex
308  can be uninitialised)
309  */
310  if (getSession().lex().current_select)
311  {
312  enum_parsing_place place= getSession().lex().current_select->parsing_place;
313  if (place == SELECT_LIST || place == IN_HAVING)
314  getSession().lex().current_select->select_n_having_items++;
315  }
316 }
317 
318 Item::Item(Session *session, Item *item):
319  is_expensive_cache(-1),
320  str_value(item->str_value),
321  name(item->name),
322  name_length(item->name_length),
323  max_length(item->max_length),
324  marker(item->marker),
325  decimals(item->decimals),
326  fixed(item->fixed),
327  maybe_null(item->maybe_null),
328  null_value(item->null_value),
329  unsigned_flag(item->unsigned_flag),
330  with_sum_func(item->with_sum_func),
331  is_autogenerated_name(item->is_autogenerated_name),
332  with_subselect(item->with_subselect),
333  collation(item->collation),
334  cmp_context(item->cmp_context),
335  _session(*session)
336 {
337  /* Put this item in the session's free list */
338  next= getSession().free_list;
339  getSession().free_list= this;
340 }
341 
342 uint32_t Item::float_length(uint32_t decimals_par) const
343 {
344  return decimals != NOT_FIXED_DEC ? (DBL_DIG+2+decimals_par) : DBL_DIG+8;
345 }
346 
347 uint32_t Item::decimal_precision() const
348 {
349  Item_result restype= result_type();
350 
351  if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
352  return min(class_decimal_length_to_precision(max_length, decimals, unsigned_flag),
353  (uint32_t) DECIMAL_MAX_PRECISION);
354  return min(max_length, (uint32_t) DECIMAL_MAX_PRECISION);
355 }
356 
357 int Item::decimal_int_part() const
358 {
359  return class_decimal_int_part(decimal_precision(), decimals);
360 }
361 
362 void Item::print(String *str)
363 {
364  str->append(full_name(), strlen(full_name()));
365 }
366 
367 void Item::print_item_w_name(String *str)
368 {
369  print(str);
370 
371  if (name)
372  {
373  str->append(STRING_WITH_LEN(" AS "));
374  str->append_identifier(str_ref(name));
375  }
376 }
377 
378 void Item::split_sum_func(Session *, Item **, List<Item> &)
379 {}
380 
381 void Item::cleanup()
382 {
383  fixed= false;
384  marker= 0;
385 }
386 
387 Item* Item::transform(Item_transformer transformer, unsigned char *arg)
388 {
389  return (this->*transformer)(arg);
390 }
391 
392 bool Item::check_cols(uint32_t c)
393 {
394  if (c != 1)
395  {
396  my_error(ER_OPERAND_COLUMNS, MYF(0), c);
397  return true;
398  }
399  return false;
400 }
401 
402 void Item::set_name(const char *str, uint32_t length, const charset_info_st* cs)
403 {
404  if (not length)
405  {
406  if (0) // str && *str)
407  std::cerr << "non-empty empty name: " << str << std::endl;
408  /* Empty string, used by AS or internal function like last_insert_id() */
409  name= str; // should be NULL (or "");
410  name_length= 0;
411  return;
412  }
413  if (cs->ctype)
414  {
415  uint32_t orig_len= length;
416  while (length && not cs->isgraph(*str))
417  {
418  /* Fix problem with yacc */
419  length--;
420  str++;
421  }
422  if (orig_len != length && not is_autogenerated_name)
423  {
424  if (length == 0)
425  push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NAME_BECOMES_EMPTY, ER(ER_NAME_BECOMES_EMPTY), str + length - orig_len);
426  else
427  push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES), str + length - orig_len);
428  }
429  }
430  name= memory::sql_strdup(str_ref(str, length));
431 }
432 
433 bool Item::eq(const Item *item, bool) const
434 {
435  /*
436  Note, that this is never true if item is a Item_param:
437  for all basic constants we have special checks, and Item_param's
438  type() can be only among basic constant types.
439  */
440  return type() == item->type() &&
441  name &&
442  item->name &&
443  not system_charset_info->strcasecmp(name, item->name);
444 }
445 
446 Item *Item::safe_charset_converter(const charset_info_st* tocs)
447 {
448  Item_func_conv_charset *conv= new Item_func_conv_charset(this, tocs, 1);
449  return conv->safe ? conv : NULL; // memory leak? (conv if not conv->safe)
450 }
451 
452 bool Item::get_date(type::Time &ltime,uint32_t fuzzydate)
453 {
454  do
455  {
456  if (is_null())
457  {
458  break;
459  }
460  else if (result_type() == STRING_RESULT)
461  {
462  char buff[type::Time::MAX_STRING_LENGTH];
463  String tmp(buff,sizeof(buff), &my_charset_bin);
464  String *res= val_str(&tmp);
465  if (not res || str_to_datetime_with_warn(getSession(), *res, ltime, fuzzydate) <= type::DRIZZLE_TIMESTAMP_ERROR)
466  {
467  break;
468  }
469  }
470  else
471  {
472  int64_t value= val_int();
473  type::datetime_t date_value;
474 
475  ltime.convert(date_value, value, fuzzydate);
476 
477  if (not type::is_valid(date_value))
478  {
479  char buff[DECIMAL_LONGLONG_DIGITS];
480  char* end= internal::int64_t10_to_str(value, buff, -10);
481  make_truncated_value_warning(getSession(), DRIZZLE_ERROR::WARN_LEVEL_WARN, str_ref(buff, (int) (end-buff)), type::DRIZZLE_TIMESTAMP_NONE, NULL);
482  break;
483  }
484  }
485 
486  return false;
487  } while (0);
488 
489  ltime.reset();
490 
491  return true;
492 }
493 
495 {
496  char buff[type::Time::MAX_STRING_LENGTH];
497  String tmp(buff,sizeof(buff),&my_charset_bin);
498  String *res= val_str(&tmp);
499  if (not res || str_to_time_with_warn(getSession(), *res, ltime))
500  {
501  ltime.reset();
502  return true;
503  }
504 
505  return false;
506 }
507 
508 bool Item::get_date_result(type::Time &ltime,uint32_t fuzzydate)
509 {
510  return get_date(ltime, fuzzydate);
511 }
512 
514 {
515  return false;
516 }
517 
519 {
520  (void) val_int();
521 }
522 
524 {}
525 
527 {}
528 
529 bool Item::is_result_field(void)
530 {
531  return false;
532 }
533 
534 bool Item::is_bool_func(void)
535 {
536  return false;
537 }
538 
539 void Item::save_in_result_field(bool)
540 {}
541 
543 {}
544 
545 Item *Item::copy_or_same(Session *)
546 {
547  return this;
548 }
549 
550 Item *Item::copy_andor_structure(Session *)
551 {
552  return this;
553 }
554 
555 Item *Item::real_item(void)
556 {
557  return this;
558 }
559 
560 const Item *Item::real_item(void) const
561 {
562  return this;
563 }
564 
565 Item *Item::get_tmp_table_item(Session *session)
566 {
567  return copy_or_same(session);
568 }
569 
570 const charset_info_st *Item::default_charset()
571 {
572  return current_session->variables.getCollation();
573 }
574 
575 const charset_info_st *Item::compare_collation()
576 {
577  return NULL;
578 }
579 
580 bool Item::walk(Item_processor processor, bool, unsigned char *arg)
581 {
582  return (this->*processor)(arg);
583 }
584 
585 Item* Item::compile(Item_analyzer analyzer, unsigned char **arg_p, Item_transformer transformer, unsigned char *arg_t)
586 {
587  return (this->*analyzer)(arg_p)
588  ? (this->*transformer)(arg_t)
589  : NULL;
590 }
591 
592 void Item::traverse_cond(Cond_traverser traverser, void *arg, traverse_order)
593 {
594  (*traverser)(this, arg);
595 }
596 
597 bool Item::remove_dependence_processor(unsigned char *)
598 {
599  return false;
600 }
601 
602 bool Item::collect_item_field_processor(unsigned char *)
603 {
604  return false;
605 }
606 
607 bool Item::find_item_in_field_list_processor(unsigned char *)
608 {
609  return false;
610 }
611 
612 bool Item::change_context_processor(unsigned char *)
613 {
614  return false;
615 }
616 
617 bool Item::register_field_in_read_map(unsigned char *)
618 {
619  return false;
620 }
621 
622 bool Item::subst_argument_checker(unsigned char **arg)
623 {
624  if (*arg)
625  *arg= NULL;
626  return true;
627 }
628 
629 Item *Item::equal_fields_propagator(unsigned char *)
630 {
631  return this;
632 }
633 
634 bool Item::set_no_const_sub(unsigned char *)
635 {
636  return false;
637 }
638 
639 Item *Item::replace_equal_field(unsigned char *)
640 {
641  return this;
642 }
643 
644 uint32_t Item::cols()
645 {
646  return 1;
647 }
648 
649 Item* Item::element_index(uint32_t)
650 {
651  return this;
652 }
653 
654 Item** Item::addr(uint32_t)
655 {
656  return NULL;
657 }
658 
659 bool Item::null_inside()
660 {
661  return false;
662 }
663 
664 void Item::bring_value()
665 {}
666 
667 Item *Item::neg_transformer(Session *)
668 {
669  return NULL;
670 }
671 
672 Item *Item::update_value_transformer(unsigned char *)
673 {
674  return this;
675 }
676 
677 void Item::delete_self()
678 {
679  cleanup();
680  delete this;
681 }
682 
684 {
685  return false;
686 }
687 
689 {
690  if (is_expensive_cache < 0)
691  is_expensive_cache= walk(&Item::is_expensive_processor, 0, NULL);
692  return test(is_expensive_cache);
693 }
694 
695 /*
696  need a special class to adjust printing : references to aggregate functions
697  must not be printed as refs because the aggregate functions that are added to
698  the front of select list are not printed as well.
699 */
701 {
702 public:
703  Item_aggregate_ref(Name_resolution_context *context_arg, Item **item,
704  const char *table_name_arg, const char *field_name_arg)
705  :Item_ref(context_arg, item, table_name_arg, field_name_arg) {}
706 
707  virtual inline void print (String *str)
708  {
709  if (ref)
710  (*ref)->print(str);
711  else
712  Item_ident::print(str);
713  }
714 };
715 
716 void Item::split_sum_func(Session *session, Item **ref_pointer_array,
717  List<Item> &fields, Item **ref,
718  bool skip_registered)
719 {
720  /* An item of type Item_sum is registered <=> ref_by != 0 */
721  if (type() == SUM_FUNC_ITEM &&
722  skip_registered &&
723  ((Item_sum *) this)->ref_by)
724  return;
725 
726  if ((type() != SUM_FUNC_ITEM && with_sum_func) ||
727  (type() == FUNC_ITEM &&
728  (((Item_func *) this)->functype() == Item_func::ISNOTNULLTEST_FUNC ||
729  ((Item_func *) this)->functype() == Item_func::TRIG_COND_FUNC)))
730  {
731  /* Will split complicated items and ignore simple ones */
732  split_sum_func(session, ref_pointer_array, fields);
733  }
734  else if ((type() == SUM_FUNC_ITEM || (used_tables() & ~PARAM_TABLE_BIT)) &&
735  type() != SUBSELECT_ITEM &&
736  type() != REF_ITEM)
737  {
738  /*
739  Replace item with a reference so that we can easily calculate
740  it (in case of sum functions) or copy it (in case of fields)
741 
742  The test above is to ensure we don't do a reference for things
743  that are constants (PARAM_TABLE_BIT is in effect a constant)
744  or already referenced (for example an item in HAVING)
745  Exception is Item_direct_view_ref which we need to convert to
746  Item_ref to allow fields from view being stored in tmp table.
747  */
748  Item_aggregate_ref *item_ref;
749  uint32_t el= fields.size();
750  Item *real_itm= real_item();
751 
752  ref_pointer_array[el]= real_itm;
753  item_ref= new Item_aggregate_ref(&session->lex().current_select->context, ref_pointer_array + el, 0, name);
754  if (type() == SUM_FUNC_ITEM)
755  item_ref->depended_from= ((Item_sum *) this)->depended_from();
756  fields.push_front(real_itm);
757  *ref= item_ref;
758  }
759 }
760 
761 /*
762  Functions to convert item to field (for send_fields)
763 */
764 bool Item::fix_fields(Session *, Item **)
765 {
766  /* We do not check fields which are fixed during construction */
767  assert(! fixed || basic_const_item());
768  fixed= true;
769  return false;
770 }
771 
772 void mark_as_dependent(Session *session, Select_Lex *last, Select_Lex *current,
773  Item_ident *resolved_item,
774  Item_ident *mark_item)
775 {
776  const char *db_name= (resolved_item->db_name ?
777  resolved_item->db_name : "");
778  const char *table_name= (resolved_item->table_name ?
779  resolved_item->table_name : "");
780  /* store pointer on Select_Lex from which item is dependent */
781  if (mark_item)
782  mark_item->depended_from= last;
783  current->mark_as_dependent(last);
784  if (session->lex().describe & DESCRIBE_EXTENDED)
785  {
786  char warn_buff[DRIZZLE_ERRMSG_SIZE];
787  snprintf(warn_buff, sizeof(warn_buff), ER(ER_WARN_FIELD_RESOLVED),
788  db_name, (db_name[0] ? "." : ""),
789  table_name, (table_name [0] ? "." : ""),
790  resolved_item->field_name,
791  current->select_number, last->select_number);
792  push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
793  ER_WARN_FIELD_RESOLVED, warn_buff);
794  }
795 }
796 
798  Select_Lex *last_select,
799  Select_Lex *current_sel,
800  Field *found_field, Item *found_item,
801  Item_ident *resolved_item)
802 {
803  /*
804  Go from current SELECT to SELECT where field was resolved (it
805  have to be reachable from current SELECT, because it was already
806  done once when we resolved this field and cached result of
807  resolving)
808  */
809  Select_Lex *previous_select= current_sel;
810  for (; previous_select->outer_select() != last_select;
811  previous_select= previous_select->outer_select())
812  {
813  Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
814  prev_subselect_item->used_tables_cache|= OUTER_REF_TABLE_BIT;
815  prev_subselect_item->const_item_cache= false;
816  }
817  {
818  Item_subselect *prev_subselect_item= previous_select->master_unit()->item;
819  Item_ident *dependent= resolved_item;
820  if (found_field == view_ref_found)
821  {
822  Item::Type type= found_item->type();
823  prev_subselect_item->used_tables_cache|= found_item->used_tables();
824  dependent= ((type == Item::REF_ITEM || type == Item::FIELD_ITEM) ?
825  (Item_ident*) found_item :
826  0);
827  }
828  else
829  prev_subselect_item->used_tables_cache|= found_field->getTable()->map;
830  prev_subselect_item->const_item_cache= false;
831  mark_as_dependent(session, last_select, current_sel, resolved_item,
832  dependent);
833  }
834 }
835 
850 static Item** find_field_in_group_list(Session *session, Item *find_item, Order *group_list)
851 {
852  const char *db_name;
853  const char *table_name;
854  const char *field_name;
855  Order *found_group= NULL;
856  int found_match_degree= 0;
857  Item_ident *cur_field;
858  int cur_match_degree= 0;
859  char name_buff[NAME_LEN+1];
860 
861  if (find_item->type() == Item::FIELD_ITEM ||
862  find_item->type() == Item::REF_ITEM)
863  {
864  db_name= ((Item_ident*) find_item)->db_name;
865  table_name= ((Item_ident*) find_item)->table_name;
866  field_name= ((Item_ident*) find_item)->field_name;
867  }
868  else
869  return NULL;
870 
871  if (db_name)
872  {
873  /* Convert database to lower case for comparison */
874  strncpy(name_buff, db_name, sizeof(name_buff)-1);
875  files_charset_info->casedn_str(name_buff);
876  db_name= name_buff;
877  }
878 
879  assert(field_name != 0);
880 
881  for (Order *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
882  {
883  if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM)
884  {
885  cur_field= (Item_ident*) *cur_group->item;
886  cur_match_degree= 0;
887 
888  assert(cur_field->field_name != 0);
889 
890  if (system_charset_info->strcasecmp(cur_field->field_name, field_name))
891  continue;
892  ++cur_match_degree;
893 
894  if (cur_field->table_name && table_name)
895  {
896  /* If field_name is qualified by a table name. */
897  if (table_alias_charset->strcasecmp(cur_field->table_name, table_name))
898  /* Same field names, different tables. */
899  return NULL;
900 
901  ++cur_match_degree;
902  if (cur_field->db_name && db_name)
903  {
904  /* If field_name is also qualified by a database name. */
905  if (system_charset_info->strcasecmp(cur_field->db_name, db_name))
906  {
907  /* Same field names, different databases. */
908  return NULL;
909  }
910  ++cur_match_degree;
911  }
912  }
913 
914  if (cur_match_degree > found_match_degree)
915  {
916  found_match_degree= cur_match_degree;
917  found_group= cur_group;
918  }
919  else if (found_group &&
920  (cur_match_degree == found_match_degree) &&
921  ! (*(found_group->item))->eq(cur_field, 0))
922  {
923  /*
924  If the current resolve candidate matches equally well as the current
925  best match, they must reference the same column, otherwise the field
926  is ambiguous.
927  */
928  my_error(ER_NON_UNIQ_ERROR, MYF(0), find_item->full_name(), session->where());
929  return NULL;
930  }
931  }
932  }
933 
934  if (found_group)
935  return found_group->item;
936 
937  return NULL;
938 }
939 
940 Item** resolve_ref_in_select_and_group(Session *session, Item_ident *ref, Select_Lex *select)
941 {
942  Item **group_by_ref= NULL;
943  Item **select_ref= NULL;
944  Order *group_list= (Order*) select->group_list.first;
945  bool ambiguous_fields= false;
946  uint32_t counter;
947  enum_resolution_type resolution;
948 
949  /*
950  Search for a column or derived column named as 'ref' in the SELECT
951  clause of the current select.
952  */
953  if (!(select_ref= find_item_in_list(session,
954  ref, *(select->get_item_list()),
955  &counter, REPORT_EXCEPT_NOT_FOUND,
956  &resolution)))
957  return NULL; /* Some error occurred. */
958  if (resolution == RESOLVED_AGAINST_ALIAS)
959  ref->alias_name_used= true;
960 
961  /* If this is a non-aggregated field inside HAVING, search in GROUP BY. */
962  if (select->having_fix_field && !ref->with_sum_func && group_list)
963  {
964  group_by_ref= find_field_in_group_list(session, ref, group_list);
965 
966  /* Check if the fields found in SELECT and GROUP BY are the same field. */
967  if (group_by_ref && (select_ref != not_found_item) &&
968  !((*group_by_ref)->eq(*select_ref, 0)))
969  {
970  ambiguous_fields= true;
971  push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
972  ER(ER_NON_UNIQ_ERROR), ref->full_name(),
973  session->where());
974 
975  }
976  }
977 
978  if (select_ref != not_found_item || group_by_ref)
979  {
980  if (select_ref != not_found_item && !ambiguous_fields)
981  {
982  assert(*select_ref != 0);
983  if (!select->ref_pointer_array[counter])
984  {
985  my_error(ER_ILLEGAL_REFERENCE, MYF(0),
986  ref->name, "forward reference in item list");
987  return NULL;
988  }
989  assert((*select_ref)->fixed);
990  return (select->ref_pointer_array + counter);
991  }
992  if (group_by_ref)
993  return group_by_ref;
994  assert(false);
995  return NULL; /* So there is no compiler warning. */
996  }
997 
998  return (Item**) not_found_item;
999 }
1000 
1001 void Item::init_make_field(SendField *tmp_field, enum_field_types field_type_arg)
1002 {
1003  tmp_field->db_name= "";
1004  tmp_field->org_table_name= "";
1005  tmp_field->org_col_name= "";
1006  tmp_field->table_name= "";
1007  tmp_field->col_name= name;
1008  tmp_field->charsetnr= collation.collation->number;
1009  tmp_field->flags= (maybe_null ? 0 : NOT_NULL_FLAG) | (collation.collation->binary_compare() ? BINARY_FLAG : 0);
1010  tmp_field->type= field_type_arg;
1011  tmp_field->length= max_length;
1012  tmp_field->decimals= decimals;
1013 }
1014 
1015 void Item::make_field(SendField *tmp_field)
1016 {
1017  init_make_field(tmp_field, field_type());
1018 }
1019 
1020 enum_field_types Item::string_field_type() const
1021 {
1022  enum_field_types f_type= DRIZZLE_TYPE_VARCHAR;
1023  if (max_length >= 65536)
1024  f_type= DRIZZLE_TYPE_BLOB;
1025  return f_type;
1026 }
1027 
1028 enum_field_types Item::field_type() const
1029 {
1030  switch (result_type()) {
1031  case STRING_RESULT:
1032  return string_field_type();
1033  case INT_RESULT:
1034  return DRIZZLE_TYPE_LONGLONG;
1035  case DECIMAL_RESULT:
1036  return DRIZZLE_TYPE_DECIMAL;
1037  case REAL_RESULT:
1038  return DRIZZLE_TYPE_DOUBLE;
1039  case ROW_RESULT:
1040  assert(0);
1041  }
1042 
1043  abort();
1044 }
1045 
1046 bool Item::is_datetime()
1047 {
1048  return field::isDateTime(field_type());
1049 }
1050 
1051 String *Item::check_well_formed_result(String *str, bool send_error)
1052 {
1053  /* Check whether we got a well-formed string */
1054  const charset_info_st * const cs= str->charset();
1055  int well_formed_error;
1056  uint32_t wlen= cs->cset->well_formed_len(*cs, *str, str->length(), &well_formed_error);
1057  if (wlen < str->length())
1058  {
1059  char hexbuf[7];
1060  enum DRIZZLE_ERROR::enum_warning_level level;
1061  uint32_t diff= str->length() - wlen;
1062  set_if_smaller(diff, 3U);
1063  (void) drizzled_string_to_hex(hexbuf, str->ptr() + wlen, diff);
1064  if (send_error)
1065  {
1066  my_error(ER_INVALID_CHARACTER_STRING, MYF(0),
1067  cs->csname, hexbuf);
1068  return NULL;
1069  }
1070  {
1071  level= DRIZZLE_ERROR::WARN_LEVEL_ERROR;
1072  null_value= 1;
1073  str= 0;
1074  }
1075  push_warning_printf(&getSession(), level, ER_INVALID_CHARACTER_STRING,
1076  ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
1077  }
1078  return str;
1079 }
1080 
1081 bool Item::eq_by_collation(Item *item, bool binary_cmp, const charset_info_st * const cs)
1082 {
1083  const charset_info_st *save_cs= 0;
1084  const charset_info_st *save_item_cs= 0;
1085  if (collation.collation != cs)
1086  {
1087  save_cs= collation.collation;
1088  collation.collation= cs;
1089  }
1090  if (item->collation.collation != cs)
1091  {
1092  save_item_cs= item->collation.collation;
1093  item->collation.collation= cs;
1094  }
1095  bool res= eq(item, binary_cmp);
1096  if (save_cs)
1097  collation.collation= save_cs;
1098  if (save_item_cs)
1099  item->collation.collation= save_item_cs;
1100  return res;
1101 }
1102 
1104 {
1105  Field *field;
1106  assert(collation.collation);
1107  if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB)
1108  {
1109  field= new Field_blob(max_length, maybe_null, name,
1110  collation.collation);
1111  }
1112  else
1113  {
1114  table->setVariableWidth();
1115  field= new Field_varstring(max_length, maybe_null, name,
1116  collation.collation);
1117  }
1118 
1119  if (field)
1120  field->init(table);
1121  return field;
1122 }
1123 
1125 {
1126  /*
1127  The field functions defines a field to be not null if null_ptr is not 0
1128  */
1129  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
1130  Field *field= NULL;
1131 
1132  switch (field_type()) {
1133  case DRIZZLE_TYPE_DECIMAL:
1134  field= new Field_decimal((unsigned char*) 0,
1135  max_length,
1136  null_ptr,
1137  0,
1138  Field::NONE,
1139  name,
1140  decimals);
1141  break;
1142  case DRIZZLE_TYPE_LONG:
1143  field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
1144  break;
1145  case DRIZZLE_TYPE_LONGLONG:
1146  field= new field::Int64((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
1147  break;
1148  case DRIZZLE_TYPE_DOUBLE:
1149  field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1150  name, decimals, 0, unsigned_flag);
1151  break;
1152  case DRIZZLE_TYPE_NULL:
1153  field= new Field_null((unsigned char*) 0, max_length, name);
1154  break;
1155  case DRIZZLE_TYPE_DATE:
1156  field= new Field_date(maybe_null, name);
1157  break;
1158 
1159  case DRIZZLE_TYPE_MICROTIME:
1160  field= new field::Microtime(maybe_null, name);
1161  break;
1162 
1163  case DRIZZLE_TYPE_TIMESTAMP:
1164  field= new field::Epoch(maybe_null, name);
1165  break;
1166  case DRIZZLE_TYPE_DATETIME:
1167  field= new Field_datetime(maybe_null, name);
1168  break;
1169  case DRIZZLE_TYPE_TIME:
1170  field= new field::Time(maybe_null, name);
1171  break;
1172  case DRIZZLE_TYPE_BOOLEAN:
1173  case DRIZZLE_TYPE_UUID:
1174  case DRIZZLE_TYPE_IPV6:
1175  case DRIZZLE_TYPE_ENUM:
1176  case DRIZZLE_TYPE_VARCHAR:
1177  return make_string_field(table);
1178  case DRIZZLE_TYPE_BLOB:
1179  field= new Field_blob(max_length, maybe_null, name, collation.collation);
1180  break; // Blob handled outside of case
1181  }
1182  assert(field);
1183 
1184  if (field)
1185  field->init(table);
1186  return field;
1187 }
1188 
1189 /*
1190  This implementation can lose str_value content, so if the
1191  Item uses str_value to store something, it should
1192  reimplement it's ::save_in_field() as Item_string, for example, does
1193 */
1194 int Item::save_in_field(Field *field, bool no_conversions)
1195 {
1196  int error;
1197  if (result_type() == STRING_RESULT)
1198  {
1199  String *result;
1200  const charset_info_st * const cs= collation.collation;
1201  char buff[MAX_FIELD_WIDTH]; // Alloc buffer for small columns
1202  str_value.set_quick(buff, sizeof(buff), cs);
1203  result=val_str(&str_value);
1204  if (null_value)
1205  {
1206  str_value.set_quick(0, 0, cs);
1207  return set_field_to_null_with_conversions(field, no_conversions);
1208  }
1209 
1210  /* NOTE: If null_value == false, "result" must be not NULL. */
1211 
1212  field->set_notnull();
1213  error=field->store(result->ptr(),result->length(),cs);
1214  str_value.set_quick(0, 0, cs);
1215  }
1216  else if (result_type() == REAL_RESULT &&
1217  field->result_type() == STRING_RESULT)
1218  {
1219  double nr= val_real();
1220  if (null_value)
1221  return set_field_to_null_with_conversions(field, no_conversions);
1222  field->set_notnull();
1223  error= field->store(nr);
1224  }
1225  else if (result_type() == REAL_RESULT)
1226  {
1227  double nr= val_real();
1228  if (null_value)
1229  return set_field_to_null(field);
1230  field->set_notnull();
1231  error=field->store(nr);
1232  }
1233  else if (result_type() == DECIMAL_RESULT)
1234  {
1235  type::Decimal decimal_value;
1236  type::Decimal *value= val_decimal(&decimal_value);
1237  if (null_value)
1238  return set_field_to_null_with_conversions(field, no_conversions);
1239  field->set_notnull();
1240  error=field->store_decimal(value);
1241  }
1242  else
1243  {
1244  int64_t nr=val_int();
1245  if (null_value)
1246  return set_field_to_null_with_conversions(field, no_conversions);
1247  field->set_notnull();
1248  error=field->store(nr, unsigned_flag);
1249  }
1250  return error;
1251 }
1252 
1262 bool Item::cache_const_expr_analyzer(unsigned char **arg)
1263 {
1264  bool *cache_flag= (bool*)*arg;
1265  if (!*cache_flag)
1266  {
1267  Item *item= real_item();
1268  /*
1269  Cache constant items unless it's a basic constant, constant field or
1270  a subselect (they use their own cache).
1271  */
1272  if (const_item() &&
1273  !(item->basic_const_item() || item->type() == Item::FIELD_ITEM ||
1274  item->type() == SUBSELECT_ITEM ||
1275  /*
1276  Do not cache GET_USER_VAR() function as its const_item() may
1277  return TRUE for the current thread but it still may change
1278  during the execution.
1279  */
1280  (item->type() == Item::FUNC_ITEM &&
1281  ((Item_func*)item)->functype() == Item_func::GUSERVAR_FUNC)))
1282  *cache_flag= true;
1283  return true;
1284  }
1285  return false;
1286 }
1287 
1298 {
1299  if (*(bool*)arg)
1300  {
1301  *((bool*)arg)= false;
1302  Item_cache *cache= Item_cache::get_cache(this);
1303  if (!cache)
1304  return NULL;
1305  cache->setup(this);
1306  cache->store(this);
1307  return cache;
1308  }
1309  return this;
1310 }
1311 
1312 void Item::send(plugin::Client *client, String *buffer)
1313 {
1314  switch (field_type())
1315  {
1316  case DRIZZLE_TYPE_DATE:
1317  case DRIZZLE_TYPE_NULL:
1318  case DRIZZLE_TYPE_ENUM:
1319  case DRIZZLE_TYPE_BLOB:
1320  case DRIZZLE_TYPE_VARCHAR:
1321  case DRIZZLE_TYPE_BOOLEAN:
1322  case DRIZZLE_TYPE_UUID:
1323  case DRIZZLE_TYPE_IPV6:
1324  case DRIZZLE_TYPE_DECIMAL:
1325  {
1326  if (String* res=val_str(buffer))
1327  client->store(res->ptr(), res->length());
1328  break;
1329  }
1330  case DRIZZLE_TYPE_LONG:
1331  {
1332  int64_t nr= val_int();
1333  if (!null_value)
1334  client->store((int32_t)nr);
1335  break;
1336  }
1337  case DRIZZLE_TYPE_LONGLONG:
1338  {
1339  int64_t nr= val_int();
1340  if (!null_value)
1341  {
1342  if (unsigned_flag)
1343  client->store((uint64_t)nr);
1344  else
1345  client->store((int64_t)nr);
1346  }
1347  break;
1348  }
1349  case DRIZZLE_TYPE_DOUBLE:
1350  {
1351  double nr= val_real();
1352  if (!null_value)
1353  client->store(nr, decimals, buffer);
1354  break;
1355  }
1356  case DRIZZLE_TYPE_TIME:
1357  {
1358  type::Time tm;
1359  get_time(tm);
1360  if (not null_value)
1361  client->store(&tm);
1362  break;
1363  }
1364  case DRIZZLE_TYPE_DATETIME:
1365  case DRIZZLE_TYPE_MICROTIME:
1366  case DRIZZLE_TYPE_TIMESTAMP:
1367  {
1368  type::Time tm;
1369  get_date(tm, TIME_FUZZY_DATE);
1370  if (!null_value)
1371  client->store(&tm);
1372  break;
1373  }
1374  }
1375  if (null_value)
1376  client->store();
1377 }
1378 
1379 uint32_t Item::max_char_length() const
1380 {
1381  return max_length / collation.collation->mbmaxlen;
1382 }
1383 
1384 void Item::fix_char_length(uint32_t max_char_length_arg)
1385 {
1386  max_length= char_to_byte_length_safe(max_char_length_arg, collation.collation->mbmaxlen);
1387 }
1388 
1389 Item_result item_cmp_type(Item_result a,Item_result b)
1390 {
1391  if (a == STRING_RESULT && b == STRING_RESULT)
1392  return STRING_RESULT;
1393 
1394  if (a == INT_RESULT && b == INT_RESULT)
1395  return INT_RESULT;
1396  else if (a == ROW_RESULT || b == ROW_RESULT)
1397  return ROW_RESULT;
1398 
1399  if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
1400  (b == INT_RESULT || b == DECIMAL_RESULT))
1401  return DECIMAL_RESULT;
1402 
1403  return REAL_RESULT;
1404 }
1405 
1406 void resolve_const_item(Session *session, Item **ref, Item *comp_item)
1407 {
1408  Item *item= *ref;
1409  Item *new_item= NULL;
1410  if (item->basic_const_item())
1411  return; /* Can't be better */
1412  Item_result res_type=item_cmp_type(comp_item->result_type(), item->result_type());
1413  const char *name=item->name; /* Alloced by memory::sql_alloc */
1414 
1415  switch (res_type)
1416  {
1417  case STRING_RESULT:
1418  {
1419  char buff[MAX_FIELD_WIDTH];
1420  String tmp(buff,sizeof(buff),&my_charset_bin),*result;
1421  result=item->val_str(&tmp);
1422  if (item->null_value)
1423  new_item= new Item_null(name);
1424  else
1425  new_item= new Item_string(name, memory::sql_strdup(*result), result->length(), result->charset());
1426  break;
1427  }
1428  case INT_RESULT:
1429  {
1430  new_item= item->null_value ? (Item*)new Item_null(name) : (Item*)new Item_int(name, item->val_int(), item->max_length);
1431  break;
1432  }
1433  case ROW_RESULT:
1434  if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
1435  {
1436  /*
1437  Substitute constants only in Item_rows. Don't affect other Items
1438  with ROW_RESULT (eg Item_singlerow_subselect).
1439 
1440  For such Items more optimal is to detect if it is constant and replace
1441  it with Item_row. This would optimize queries like this:
1442  SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
1443  */
1444  Item_row *item_row= (Item_row*) item;
1445  Item_row *comp_item_row= (Item_row*) comp_item;
1446  new_item= 0;
1447  /*
1448  If item and comp_item are both Item_rows and have same number of cols
1449  then process items in Item_row one by one.
1450  We can't ignore NULL values here as this item may be used with <=>, in
1451  which case NULL's are significant.
1452  */
1453  assert(item->result_type() == comp_item->result_type());
1454  assert(item_row->cols() == comp_item_row->cols());
1455  for (uint32_t col= item_row->cols(); col--; )
1456  resolve_const_item(session, item_row->addr(col), comp_item_row->element_index(col));
1457  break;
1458  }
1459  /* Fallthrough */
1460  case REAL_RESULT:
1461  { // It must REAL_RESULT
1462  double result= item->val_real();
1463  uint32_t length=item->max_length,decimals=item->decimals;
1464  bool null_value=item->null_value;
1465  new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
1466  new Item_float(name, result, decimals, length));
1467  break;
1468  }
1469  case DECIMAL_RESULT:
1470  {
1471  type::Decimal decimal_value;
1472  type::Decimal *result= item->val_decimal(&decimal_value);
1473  uint32_t length= item->max_length, decimals= item->decimals;
1474  bool null_value= item->null_value;
1475  new_item= (null_value ?
1476  (Item*) new Item_null(name) :
1477  (Item*) new Item_decimal(name, result, length, decimals));
1478  break;
1479  }
1480  }
1481 
1482  if (new_item)
1483  *ref= new_item;
1484 }
1485 
1487 {
1488 
1489  Item_result res_type=item_cmp_type(field->result_type(),
1490  item->result_type());
1491  if (res_type == STRING_RESULT)
1492  {
1493  char item_buff[MAX_FIELD_WIDTH];
1494  char field_buff[MAX_FIELD_WIDTH];
1495  String item_tmp(item_buff,sizeof(item_buff),&my_charset_bin),*item_result;
1496  String field_tmp(field_buff,sizeof(field_buff),&my_charset_bin);
1497  item_result=item->val_str(&item_tmp);
1498  if (item->null_value)
1499  return 1; // This must be true
1500  field->val_str_internal(&field_tmp);
1501  return not stringcmp(&field_tmp,item_result);
1502  }
1503 
1504  if (res_type == INT_RESULT)
1505  return 1; // Both where of type int
1506 
1507  if (res_type == DECIMAL_RESULT)
1508  {
1509  type::Decimal item_buf, *item_val,
1510  field_buf, *field_val;
1511  item_val= item->val_decimal(&item_buf);
1512  if (item->null_value)
1513  return 1; // This must be true
1514  field_val= field->val_decimal(&field_buf);
1515  return !class_decimal_cmp(item_val, field_val);
1516  }
1517 
1518  double result= item->val_real();
1519  if (item->null_value)
1520  return 1;
1521 
1522  return result == field->val_real();
1523 }
1524 
1549  Item *item, Table *table,
1550  Item ***copy_func, bool modify_item,
1551  uint32_t convert_blob_length)
1552 {
1553  bool maybe_null= item->maybe_null;
1554  Field *new_field= NULL;
1555 
1556  switch (item->result_type()) {
1557  case REAL_RESULT:
1558  new_field= new Field_double(item->max_length, maybe_null,
1559  item->name, item->decimals, true);
1560  break;
1561 
1562  case INT_RESULT:
1563  /*
1564  Select an integer type with the minimal fit precision.
1565  MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
1566  Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
1567  Int32 -> make them field::Int64.
1568  */
1569  if (item->unsigned_flag)
1570  {
1571  new_field= new field::Size(item->max_length, maybe_null, item->name, item->unsigned_flag);
1572  }
1573  else if (item->max_length >= MY_INT32_NUM_DECIMAL_DIGITS - 1)
1574  {
1575  new_field= new field::Int64(item->max_length, maybe_null, item->name, item->unsigned_flag);
1576  }
1577  else
1578  {
1579  new_field= new field::Int32(item->max_length, maybe_null, item->name, item->unsigned_flag);
1580  }
1581 
1582  break;
1583 
1584  case STRING_RESULT:
1585  assert(item->collation.collation);
1586 
1587  /*
1588  DATE/TIME fields have STRING_RESULT result type.
1589  To preserve type they needed to be handled separately.
1590  */
1591  if (field::isDateTime(item->field_type()))
1592  {
1593  new_field= item->tmp_table_field_from_field_type(table, 1);
1594  /*
1595  Make sure that the blob fits into a Field_varstring which has
1596  2-byte lenght.
1597  */
1598  }
1599  else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
1600  convert_blob_length <= Field_varstring::MAX_SIZE &&
1601  convert_blob_length)
1602  {
1603  table->setVariableWidth();
1604  new_field= new Field_varstring(convert_blob_length, maybe_null,
1605  item->name, item->collation.collation);
1606  }
1607  else
1608  {
1609  new_field= item->make_string_field(table);
1610  }
1611  new_field->set_derivation(item->collation.derivation);
1612  break;
1613 
1614  case DECIMAL_RESULT:
1615  {
1616  uint8_t dec= item->decimals;
1617  uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
1618  uint32_t len= item->max_length;
1619 
1620  /*
1621  Trying to put too many digits overall in a DECIMAL(prec,dec)
1622  will always throw a warning. We must limit dec to
1623  DECIMAL_MAX_SCALE however to prevent an assert() later.
1624  */
1625 
1626  if (dec > 0)
1627  {
1628  signed int overflow;
1629 
1630  dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
1631 
1632  /*
1633  If the value still overflows the field with the corrected dec,
1634  we'll throw out decimals rather than integers. This is still
1635  bad and of course throws a truncation warning.
1636  +1: for decimal point
1637  */
1638 
1639  overflow= class_decimal_precision_to_length(intg + dec, dec,
1640  item->unsigned_flag) - len;
1641 
1642  if (overflow > 0)
1643  dec= max(0, dec - overflow); // too long, discard fract
1644  else
1645  len-= item->decimals - dec; // corrected value fits
1646  }
1647 
1648  new_field= new Field_decimal(len,
1649  maybe_null,
1650  item->name,
1651  dec,
1652  item->unsigned_flag);
1653  break;
1654  }
1655 
1656  case ROW_RESULT:
1657  // This case should never be choosen
1658  assert(0);
1659  abort();
1660  }
1661 
1662  if (new_field)
1663  new_field->init(table);
1664 
1665  if (copy_func && item->is_result_field())
1666  *((*copy_func)++) = item; // Save for copy_funcs
1667 
1668  if (modify_item)
1669  item->set_result_field(new_field);
1670 
1671  if (item->type() == Item::NULL_ITEM)
1672  new_field->is_created_from_null_item= true;
1673 
1674  return new_field;
1675 }
1676 
1678  Table *table,
1679  Item *item,
1680  Item::Type type,
1681  Item ***copy_func,
1682  Field **from_field,
1683  Field **default_field,
1684  bool group,
1685  bool modify_item,
1686  bool make_copy_field,
1687  uint32_t convert_blob_length)
1688 {
1689  Field *result;
1690  Item::Type orig_type= type;
1691  Item *orig_item= 0;
1692 
1693  if (type != Item::FIELD_ITEM &&
1694  item->real_item()->type() == Item::FIELD_ITEM)
1695  {
1696  orig_item= item;
1697  item= item->real_item();
1698  type= Item::FIELD_ITEM;
1699  }
1700 
1701  switch (type) {
1702  case Item::SUM_FUNC_ITEM:
1703  {
1704  Item_sum *item_sum=(Item_sum*) item;
1705  result= item_sum->create_tmp_field(group, table, convert_blob_length);
1706  if (!result)
1707  my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
1708  return result;
1709  }
1710  case Item::FIELD_ITEM:
1711  case Item::DEFAULT_VALUE_ITEM:
1712  {
1713  Item_field *field= (Item_field*) item;
1714  bool orig_modify= modify_item;
1715  if (orig_type == Item::REF_ITEM)
1716  modify_item= 0;
1717  /*
1718  If item have to be able to store NULLs but underlaid field can't do it,
1719  create_tmp_field_from_field() can't be used for tmp field creation.
1720  */
1721  if (field->maybe_null && !field->field->maybe_null())
1722  {
1723  result= create_tmp_field_from_item(session, item, table, NULL,
1724  modify_item, convert_blob_length);
1725  *from_field= field->field;
1726  if (result && modify_item)
1727  field->result_field= result;
1728  }
1729  else
1730  {
1731  result= create_tmp_field_from_field(session, (*from_field= field->field),
1732  orig_item ? orig_item->name :
1733  item->name,
1734  table,
1735  modify_item ? field :
1736  NULL,
1737  convert_blob_length);
1738  }
1739  if (orig_type == Item::REF_ITEM && orig_modify)
1740  ((Item_ref*)orig_item)->set_result_field(result);
1741  if (field->field->eq_def(result))
1742  *default_field= field->field;
1743  return result;
1744  }
1745  /* Fall through */
1746  case Item::FUNC_ITEM:
1747  /* Fall through */
1748  case Item::COND_ITEM:
1749  case Item::FIELD_AVG_ITEM:
1750  case Item::FIELD_STD_ITEM:
1751  case Item::SUBSELECT_ITEM:
1752  /* The following can only happen with 'CREATE TABLE ... SELECT' */
1753  case Item::PROC_ITEM:
1754  case Item::INT_ITEM:
1755  case Item::REAL_ITEM:
1756  case Item::DECIMAL_ITEM:
1757  case Item::STRING_ITEM:
1758  case Item::REF_ITEM:
1759  case Item::NULL_ITEM:
1760  case Item::VARBIN_ITEM:
1761  if (make_copy_field)
1762  {
1763  assert(((Item_result_field*)item)->result_field);
1764  *from_field= ((Item_result_field*)item)->result_field;
1765  }
1766  return create_tmp_field_from_item(session, item, table,
1767  (make_copy_field ? 0 : copy_func),
1768  modify_item, convert_blob_length);
1769  case Item::TYPE_HOLDER:
1770  result= ((Item_type_holder *)item)->make_field_by_type(table);
1771  result->set_derivation(item->collation.derivation);
1772  return result;
1773  default: // Dosen't have to be stored
1774  return NULL;
1775  }
1776 }
1777 
1778 std::ostream& operator<<(std::ostream& output, const Item &item)
1779 {
1780  switch (item.type())
1781  {
1782  case drizzled::Item::SUBSELECT_ITEM :
1783  case drizzled::Item::FIELD_ITEM :
1784  case drizzled::Item::SUM_FUNC_ITEM :
1785  case drizzled::Item::STRING_ITEM :
1786  case drizzled::Item::INT_ITEM :
1787  case drizzled::Item::REAL_ITEM :
1788  case drizzled::Item::NULL_ITEM :
1789  case drizzled::Item::VARBIN_ITEM :
1790  case drizzled::Item::COPY_STR_ITEM :
1791  case drizzled::Item::FIELD_AVG_ITEM :
1792  case drizzled::Item::DEFAULT_VALUE_ITEM :
1793  case drizzled::Item::PROC_ITEM :
1794  case drizzled::Item::COND_ITEM :
1795  case drizzled::Item::REF_ITEM :
1796  case drizzled::Item::FIELD_STD_ITEM :
1797  case drizzled::Item::FIELD_VARIANCE_ITEM :
1798  case drizzled::Item::INSERT_VALUE_ITEM :
1799  case drizzled::Item::ROW_ITEM:
1800  case drizzled::Item::CACHE_ITEM :
1801  case drizzled::Item::TYPE_HOLDER :
1802  case drizzled::Item::PARAM_ITEM :
1803  case drizzled::Item::DECIMAL_ITEM :
1804  case drizzled::Item::FUNC_ITEM :
1805  case drizzled::Item::BOOLEAN_ITEM :
1806  {
1807  output << "Item:(";
1808  output << item.full_name();
1809  output << ", ";
1810  output << drizzled::display::type(item.type());
1811  output << ")";
1812  }
1813  break;
1814  }
1815 
1816  return output; // for multiple << operators.
1817 }
1818 
1819 } /* namespace drizzled */