MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
item_timefunc.h
1 #ifndef ITEM_TIMEFUNC_INCLUDED
2 #define ITEM_TIMEFUNC_INCLUDED
3 
4 /* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
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 /* Function items used by mysql */
21 
22 #include <algorithm>
23 
24 class MY_LOCALE;
25 
26 bool get_interval_value(Item *args,interval_type int_type,
27  String *str_value, INTERVAL *interval);
28 
30 {
31 public:
33  longlong val_int();
34  const char *func_name() const { return "period_add"; }
35  void fix_length_and_dec()
36  {
37  fix_char_length(6); /* YYYYMM */
38  }
39 };
40 
41 
43 {
44 public:
46  longlong val_int();
47  const char *func_name() const { return "period_diff"; }
48  void fix_length_and_dec()
49  {
50  fix_char_length(6); /* YYYYMM */
51  }
52 };
53 
54 
56 {
57 public:
59  longlong val_int();
60  const char *func_name() const { return "to_days"; }
61  void fix_length_and_dec()
62  {
63  fix_char_length(6);
64  maybe_null=1;
65  }
66  enum_monotonicity_info get_monotonicity_info() const;
67  longlong val_int_endpoint(bool left_endp, bool *incl_endp);
68  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
69  bool check_valid_arguments_processor(uchar *int_arg)
70  {
71  return !has_date_args();
72  }
73 };
74 
75 
77 {
78 public:
80  longlong val_int();
81  const char *func_name() const { return "to_seconds"; }
82  void fix_length_and_dec()
83  {
84  fix_char_length(6);
85  maybe_null=1;
86  }
87  enum_monotonicity_info get_monotonicity_info() const;
88  longlong val_int_endpoint(bool left_endp, bool *incl_endp);
89  bool check_partition_func_processor(uchar *bool_arg) { return FALSE;}
90 
91  bool intro_version(uchar *int_arg)
92  {
93  using std::max;
94  int *input_version= (int*)int_arg;
95  /* This function was introduced in 5.5 */
96  int output_version= max(*input_version, 50500);
97  *input_version= output_version;
98  return 0;
99  }
100 
101  /* Only meaningful with date part and optional time part */
102  bool check_valid_arguments_processor(uchar *int_arg)
103  {
104  return !has_date_args();
105  }
106 };
107 
108 
110 {
111 public:
113  longlong val_int();
114  const char *func_name() const { return "dayofmonth"; }
115  void fix_length_and_dec()
116  {
117  fix_char_length(2); /* 1..31 */
118  maybe_null=1;
119  }
120  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
121  bool check_valid_arguments_processor(uchar *int_arg)
122  {
123  return !has_date_args();
124  }
125 };
126 
127 
132 {
133 public:
134  Item_func_month(Item *a) :Item_func(a) { collation.set_numeric(); }
135  longlong val_int();
136  double val_real()
137  { DBUG_ASSERT(fixed == 1); return (double) Item_func_month::val_int(); }
138  String *val_str(String *str)
139  {
140  longlong nr= val_int();
141  if (null_value)
142  return 0;
143  str->set(nr, collation.collation);
144  return str;
145  }
146  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
147  {
148  return get_date_from_int(ltime, fuzzydate);
149  }
150  bool get_time(MYSQL_TIME *ltime)
151  {
152  return get_time_from_int(ltime);
153  }
154  const char *func_name() const { return "month"; }
155  enum Item_result result_type () const { return INT_RESULT; }
156  void fix_length_and_dec()
157  {
158  fix_char_length(2);
159  maybe_null= 1;
160  }
161  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
162  bool check_valid_arguments_processor(uchar *int_arg)
163  {
164  return !has_date_args();
165  }
166 };
167 
168 
170 {
171  MY_LOCALE *locale;
172 public:
174  const char *func_name() const { return "monthname"; }
175  String *val_str(String *str);
176  void fix_length_and_dec();
177  bool check_partition_func_processor(uchar *int_arg) {return TRUE;}
178  bool check_valid_arguments_processor(uchar *int_arg)
179  {
180  return !has_date_args();
181  }
182 };
183 
184 
186 {
187 public:
189  longlong val_int();
190  const char *func_name() const { return "dayofyear"; }
191  void fix_length_and_dec()
192  {
193  fix_char_length(3);
194  maybe_null= 1;
195  }
196  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
197  bool check_valid_arguments_processor(uchar *int_arg)
198  {
199  return !has_date_args();
200  }
201 };
202 
203 
205 {
206 public:
208  longlong val_int();
209  const char *func_name() const { return "hour"; }
210  void fix_length_and_dec()
211  {
212  fix_char_length(2); /* 0..23 */
213  maybe_null=1;
214  }
215  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
216  bool check_valid_arguments_processor(uchar *int_arg)
217  {
218  return !has_time_args();
219  }
220 };
221 
222 
224 {
225 public:
227  longlong val_int();
228  const char *func_name() const { return "minute"; }
229  void fix_length_and_dec()
230  {
231  fix_char_length(2); /* 0..59 */
232  maybe_null=1;
233  }
234  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
235  bool check_valid_arguments_processor(uchar *int_arg)
236  {
237  return !has_time_args();
238  }
239 };
240 
241 
243 {
244 public:
246  longlong val_int();
247  const char *func_name() const { return "quarter"; }
248  void fix_length_and_dec()
249  {
250  fix_char_length(1); /* 1..4 */
251  maybe_null=1;
252  }
253  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
254  bool check_valid_arguments_processor(uchar *int_arg)
255  {
256  return !has_date_args();
257  }
258 };
259 
260 
262 {
263 public:
265  longlong val_int();
266  const char *func_name() const { return "second"; }
267  void fix_length_and_dec()
268  {
269  fix_char_length(2); /* 0..59 */
270  maybe_null=1;
271  }
272  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
273  bool check_valid_arguments_processor(uchar *int_arg)
274  {
275  return !has_time_args();
276  }
277 };
278 
279 
281 {
282 public:
283  Item_func_week(Item *a,Item *b) :Item_int_func(a,b) {}
284  longlong val_int();
285  const char *func_name() const { return "week"; }
286  void fix_length_and_dec()
287  {
288  fix_char_length(2); /* 0..54 */
289  maybe_null=1;
290  }
291 };
292 
294 {
295 public:
296  Item_func_yearweek(Item *a,Item *b) :Item_int_func(a,b) {}
297  longlong val_int();
298  const char *func_name() const { return "yearweek"; }
299  void fix_length_and_dec()
300  {
301  fix_char_length(6); /* YYYYWW */
302  maybe_null=1;
303  }
304  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
305  bool check_valid_arguments_processor(uchar *int_arg)
306  {
307  return !has_date_args();
308  }
309 };
310 
311 
313 {
314 public:
316  longlong val_int();
317  const char *func_name() const { return "year"; }
318  enum_monotonicity_info get_monotonicity_info() const;
319  longlong val_int_endpoint(bool left_endp, bool *incl_endp);
320  void fix_length_and_dec()
321  {
322  fix_char_length(4); /* 9999 */
323  maybe_null=1;
324  }
325  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
326  bool check_valid_arguments_processor(uchar *int_arg)
327  {
328  return !has_date_args();
329  }
330 };
331 
332 
337 {
338  bool odbc_type;
339 public:
340  Item_func_weekday(Item *a,bool type_arg)
341  :Item_func(a), odbc_type(type_arg) { collation.set_numeric(); }
342  longlong val_int();
343  double val_real() { DBUG_ASSERT(fixed == 1); return (double) val_int(); }
344  String *val_str(String *str)
345  {
346  DBUG_ASSERT(fixed == 1);
347  str->set(val_int(), &my_charset_bin);
348  return null_value ? 0 : str;
349  }
350  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
351  {
352  return get_date_from_int(ltime, fuzzydate);
353  }
354  bool get_time(MYSQL_TIME *ltime)
355  {
356  return get_time_from_int(ltime);
357  }
358  const char *func_name() const
359  {
360  return (odbc_type ? "dayofweek" : "weekday");
361  }
362  enum Item_result result_type () const { return INT_RESULT; }
363  void fix_length_and_dec()
364  {
365  fix_char_length(1);
366  maybe_null= 1;
367  }
368  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
369  bool check_valid_arguments_processor(uchar *int_arg)
370  {
371  return !has_date_args();
372  }
373 };
374 
381 {
382  MY_LOCALE *locale;
383  public:
385  const char *func_name() const { return "dayname"; }
386  String *val_str(String *str);
387  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
388  {
389  return get_date_from_string(ltime, fuzzydate);
390  }
391  bool get_time(MYSQL_TIME *ltime)
392  {
393  return get_time_from_string(ltime);
394  }
395  enum Item_result result_type () const { return STRING_RESULT; }
396  void fix_length_and_dec();
397  bool check_partition_func_processor(uchar *int_arg) {return TRUE;}
398 };
399 
400 
401 /*
402  Abstract class for functions returning "struct timeval".
403 */
405 {
406 public:
408  Item_timeval_func(Item *a) :Item_func(a) { }
415  virtual bool val_timeval(struct timeval *tm)= 0;
416  longlong val_int();
417  double val_real();
418  String *val_str(String *str);
419  my_decimal *val_decimal(my_decimal *decimal_value);
420  bool get_date(MYSQL_TIME *ltime, uint fuzzydate)
421  {
422  return get_date_from_numeric(ltime, fuzzydate);
423  }
424  bool get_time(MYSQL_TIME *ltime)
425  {
426  return get_time_from_numeric(ltime);
427  }
428  enum Item_result result_type() const
429  {
430  return decimals ? DECIMAL_RESULT : INT_RESULT;
431  }
432 };
433 
434 
436 {
437 public:
440  const char *func_name() const { return "unix_timestamp"; }
441  enum_monotonicity_info get_monotonicity_info() const;
442  longlong val_int_endpoint(bool left_endp, bool *incl_endp);
443  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
444  /*
445  UNIX_TIMESTAMP() depends on the current timezone
446  (and thus may not be used as a partitioning function)
447  when its argument is NOT of the TIMESTAMP type.
448  */
449  bool check_valid_arguments_processor(uchar *int_arg)
450  {
451  return !has_timestamp_args();
452  }
453  void fix_length_and_dec()
454  {
455  fix_length_and_dec_and_charset_datetime(11, arg_count == 0 ? 0 :
456  args[0]->datetime_precision());
457  }
458  bool val_timeval(struct timeval *tm);
459 };
460 
461 
463 {
464 public:
465  Item_func_time_to_sec(Item *item) :Item_int_func(item) {}
466  longlong val_int();
467  const char *func_name() const { return "time_to_sec"; }
468  void fix_length_and_dec()
469  {
470  maybe_null= TRUE;
471  fix_char_length(10);
472  }
473  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
474  bool check_valid_arguments_processor(uchar *int_arg)
475  {
476  return !has_time_args();
477  }
478 };
479 
480 
486 {
487 protected:
488  bool check_precision();
489 public:
492  Item_temporal_func(Item *a, Item *b) :Item_func(a, b) {}
493  Item_temporal_func(Item *a, Item *b, Item *c) :Item_func(a, b, c) {}
494  enum Item_result result_type () const
495  {
496  return STRING_RESULT;
497  }
498  CHARSET_INFO *charset_for_protocol() const
499  {
500  return &my_charset_bin;
501  }
502  Field *tmp_table_field(TABLE *table)
503  {
504  return tmp_table_field_from_field_type(table, 0);
505  }
507  {
508  DBUG_ASSERT(fixed);
509  return decimals;
510  }
512  {
513  DBUG_ASSERT(fixed);
514  return decimals;
515  }
516 };
517 
518 
524 {
525 protected:
526  sql_mode_t sql_mode; // sql_mode value is cached here in fix_length_and_dec()
527  enum_field_types cached_field_type; // TIME, DATE, DATETIME or STRING
528  String ascii_buf; // Conversion buffer
536  virtual bool val_datetime(MYSQL_TIME *ltime, uint fuzzy_date)= 0;
537 
538 public:
540  sql_mode(0)
541  { }
542  enum Item_result result_type () const { return STRING_RESULT; }
543  enum_field_types field_type() const { return cached_field_type; }
544  const CHARSET_INFO *charset_for_protocol() const
545  {
546  /*
547  Can return TIME, DATE, DATETIME or VARCHAR depending on arguments.
548  Send using "binary" when TIME, DATE or DATETIME,
549  or using collation.collation when VARCHAR
550  (which is fixed from @collation_connection in fix_length_and_dec).
551  */
552  DBUG_ASSERT(fixed == 1);
553  return cached_field_type == MYSQL_TYPE_STRING ?
554  collation.collation : &my_charset_bin;
555  }
556  Field *tmp_table_field(TABLE *table)
557  {
558  return tmp_table_field_from_field_type(table, 0);
559  }
560  longlong val_int() { return val_int_from_decimal(); }
561  double val_real() { return val_real_from_decimal(); }
562  my_decimal *val_decimal(my_decimal *decimal_value);
563  type_conversion_status save_in_field(Field *field, bool no_conversions);
567  String *val_str_ascii(String *str);
572  {
573  return val_str_from_val_str_ascii(str, &ascii_buf);
574  }
575  bool get_date(MYSQL_TIME *ltime, uint fuzzydate);
576  bool get_time(MYSQL_TIME *ltime);
577 };
578 
579 
580 /*
581  This can't be a Item_str_func, because the val_real() functions are special
582 */
583 
588 {
589 public:
591  { }
593  { }
595  { }
596  enum_field_types field_type() const { return MYSQL_TYPE_DATE; }
597  bool get_time(MYSQL_TIME *ltime)
598  {
599  return get_time_from_date(ltime);
600  }
601  String *val_str(String *str)
602  {
603  return val_string_from_date(str);
604  }
605  longlong val_int()
606  {
607  return val_int_from_date();
608  }
609  longlong val_date_temporal();
610  double val_real() { return (double) val_int(); }
611  const char *func_name() const { return "date"; }
612  void fix_length_and_dec()
613  {
614  fix_length_and_dec_and_charset_datetime(MAX_DATE_WIDTH, 0);
615  }
616  my_decimal *val_decimal(my_decimal *decimal_value)
617  {
618  DBUG_ASSERT(fixed == 1);
619  return val_decimal_from_date(decimal_value);
620  }
621  type_conversion_status save_in_field(Field *field, bool no_conversions)
622  {
623  return save_date_in_field(field);
624  }
625  // All date functions must implement get_date()
626  // to avoid use of generic Item::get_date()
627  // which converts to string and then parses the string as DATE.
628  virtual bool get_date(MYSQL_TIME *res, uint fuzzy_date)= 0;
629 };
630 
631 
636 {
637 public:
639  { }
641  { }
643  { }
645  { }
646  enum_field_types field_type() const { return MYSQL_TYPE_DATETIME; }
647  double val_real() { return val_real_from_decimal(); }
648  String *val_str(String *str)
649  {
650  return val_string_from_datetime(str);
651  }
652  longlong val_int()
653  {
654  return val_int_from_datetime();
655  }
656  longlong val_date_temporal();
657  my_decimal *val_decimal(my_decimal *decimal_value)
658  {
659  DBUG_ASSERT(fixed == 1);
660  return val_decimal_from_date(decimal_value);
661  }
662  type_conversion_status save_in_field(Field *field, bool no_conversions)
663  {
664  return save_date_in_field(field);
665  }
666  bool get_time(MYSQL_TIME *ltime)
667  {
668  return get_time_from_datetime(ltime);
669  }
670  // All datetime functions must implement get_date()
671  // to avoid use of generic Item::get_date()
672  // which converts to string and then parses the string as DATETIME.
673  virtual bool get_date(MYSQL_TIME *res, uint fuzzy_date)= 0;
674 };
675 
676 
681 {
682 public:
686  Item_time_func(Item *a, Item *b, Item *c) :Item_temporal_func(a, b ,c) {}
687  enum_field_types field_type() const { return MYSQL_TYPE_TIME; }
688  double val_real() { return val_real_from_decimal(); }
689  my_decimal *val_decimal(my_decimal *decimal_value)
690  {
691  DBUG_ASSERT(fixed == 1);
692  return val_decimal_from_time(decimal_value);
693  }
694  type_conversion_status save_in_field(Field *field, bool no_conversions)
695  {
696  return save_time_in_field(field);
697  }
698  longlong val_int()
699  {
700  return val_int_from_time();
701  }
702  longlong val_time_temporal();
703  bool get_date(MYSQL_TIME *res, uint fuzzy_date)
704  {
705  return get_date_from_time(res);
706  }
707  String *val_str(String *str)
708  {
709  return val_string_from_time(str);
710  }
711  // All time functions must implement get_time()
712  // to avoid use of generic Item::get_time()
713  // which converts to string and then parses the string as TIME.
714  virtual bool get_time(MYSQL_TIME *res)= 0;
715 };
716 
717 
727 {
728  MYSQL_TIME time;
729  longlong time_packed;
730  char string_buff[MAX_DATE_STRING_REP_LENGTH];
731  uint string_length;
732  uint8 dec;
733 
737  void cache_string();
741  void reset_string()
742  {
743  string_length= 0;
744  string_buff[0]= '\0';
745  }
749  void reset()
750  {
751  time.time_type= MYSQL_TIMESTAMP_NONE;
752  time_packed= 0;
753  reset_string();
754  dec= 0;
755  }
759  void get_TIME(MYSQL_TIME *ltime) const
760  {
761  DBUG_ASSERT(time.time_type != MYSQL_TIMESTAMP_NONE);
762  *ltime= time;
763  }
764 public:
765 
767  {
768  reset();
769  }
773  void set_date(MYSQL_TIME *ltime);
777  void set_time(MYSQL_TIME *ltime, uint8 dec_arg);
781  void set_datetime(MYSQL_TIME *ltime, uint8 dec_arg);
786  void set_date(struct timeval tv, Time_zone *tz);
791  void set_time(struct timeval tv, uint8 dec_arg, Time_zone *tz);
796  void set_datetime(struct timeval tv, uint8 dec_arg, Time_zone *tz);
800  bool eq(const MYSQL_TIME_cache &tm) const
801  {
802  return val_packed() == tm.val_packed();
803  }
804 
808  uint8 decimals() const
809  {
810  DBUG_ASSERT(time.time_type != MYSQL_TIMESTAMP_NONE);
811  return dec;
812  }
813 
817  longlong val_packed() const
818  {
819  DBUG_ASSERT(time.time_type != MYSQL_TIMESTAMP_NONE);
820  return time_packed;
821  }
826  bool get_date(MYSQL_TIME *ltime, uint fuzzyflags) const;
830  bool get_time(MYSQL_TIME *ltime) const
831  {
832  get_TIME(ltime);
833  return false;
834  }
839  {
840  DBUG_ASSERT(time.time_type != MYSQL_TIMESTAMP_NONE);
841  return &time;
842  }
846  String *val_str(String *str);
850  const char *cptr();
851 };
852 
853 
858 {
859  MYSQL_TIME_cache cached_time;
860 public:
866  {
867  cached_time.set_date(ltime);
868  fix_length_and_dec();
869  fixed= 1;
870  }
871  const char *func_name() const { return "date_literal"; }
872  void print(String *str, enum_query_type query_type);
873  longlong val_date_temporal()
874  {
875  DBUG_ASSERT(fixed);
876  return cached_time.val_packed();
877  }
878  bool get_date(MYSQL_TIME *ltime, uint fuzzy_date)
879  {
880  DBUG_ASSERT(fixed);
881  return cached_time.get_date(ltime, fuzzy_date);
882  }
883  String *val_str(String *str)
884  {
885  DBUG_ASSERT(fixed);
886  return cached_time.val_str(str);
887  }
888  void fix_length_and_dec()
889  {
890  fix_length_and_dec_and_charset_datetime(MAX_DATE_WIDTH, 0);
891  }
892  bool check_partition_func_processor(uchar *int_arg)
893  {
894  return FALSE;
895  }
896  bool basic_const_item() const { return true; }
897  bool const_item() const { return true; }
898  table_map used_tables() const { return (table_map) 0L; }
899  void cleanup()
900  {
901  // See Item_basic_const::cleanup()
902  if (orig_name.is_set())
903  item_name= orig_name;
904  }
905  bool eq(const Item *item, bool binary_cmp) const;
906 };
907 
908 
913 {
914  MYSQL_TIME_cache cached_time;
915 public:
921  Item_time_literal(MYSQL_TIME *ltime, uint dec_arg) :Item_time_func()
922  {
923  decimals= MY_MIN(dec_arg, DATETIME_MAX_DECIMALS);
924  cached_time.set_time(ltime, decimals);
925  fix_length_and_dec();
926  fixed= 1;
927  }
928  const char *func_name() const { return "time_literal"; }
929  void print(String *str, enum_query_type query_type);
930  longlong val_time_temporal()
931  {
932  DBUG_ASSERT(fixed);
933  return cached_time.val_packed();
934  }
935  bool get_time(MYSQL_TIME *ltime)
936  {
937  DBUG_ASSERT(fixed);
938  return cached_time.get_time(ltime);
939  }
940  String *val_str(String *str)
941  {
942  DBUG_ASSERT(fixed);
943  return cached_time.val_str(str);
944  }
945  void fix_length_and_dec()
946  {
947  fix_length_and_dec_and_charset_datetime(MAX_TIME_WIDTH, decimals);
948  }
949  bool check_partition_func_processor(uchar *int_arg)
950  {
951  return FALSE;
952  }
953  bool basic_const_item() const { return true; }
954  bool const_item() const { return true; }
955  table_map used_tables() const { return (table_map) 0L; }
956  void cleanup()
957  {
958  // See Item_basic_const::cleanup()
959  if (orig_name.is_set())
960  item_name= orig_name;
961  }
962  bool eq(const Item *item, bool binary_cmp) const;
963 };
964 
965 
970 {
971  MYSQL_TIME_cache cached_time;
972 public:
979  {
980  decimals= MY_MIN(dec_arg, DATETIME_MAX_DECIMALS);
981  cached_time.set_datetime(ltime, decimals);
982  fix_length_and_dec();
983  fixed= 1;
984  }
985  const char *func_name() const { return "datetime_literal"; }
986  void print(String *str, enum_query_type query_type);
987  longlong val_date_temporal()
988  {
989  DBUG_ASSERT(fixed);
990  return cached_time.val_packed();
991  }
992  bool get_date(MYSQL_TIME *ltime, uint fuzzy_date)
993  {
994  DBUG_ASSERT(fixed);
995  return cached_time.get_date(ltime, fuzzy_date);
996  }
997  String *val_str(String *str)
998  {
999  DBUG_ASSERT(fixed);
1000  return cached_time.val_str(str);
1001  }
1002  void fix_length_and_dec()
1003  {
1004  fix_length_and_dec_and_charset_datetime(MAX_DATETIME_WIDTH, decimals);
1005  }
1006  bool check_partition_func_processor(uchar *int_arg)
1007  {
1008  return FALSE;
1009  }
1010  bool basic_const_item() const { return true; }
1011  bool const_item() const { return true; }
1012  table_map used_tables() const { return (table_map) 0L; }
1013  void cleanup()
1014  {
1015  // See Item_basic_const::cleanup()
1016  if (orig_name.is_set())
1017  item_name= orig_name;
1018  }
1019  bool eq(const Item *item, bool binary_cmp) const;
1020 };
1021 
1022 
1023 /* Abstract CURTIME function. Children should define what time zone is used */
1024 
1026 {
1027  MYSQL_TIME_cache cached_time; // Initialized in fix_length_and_dec
1028 protected:
1029  // Abstract method that defines which time zone is used for conversion.
1030  virtual Time_zone *time_zone()= 0;
1031 public:
1036  Item_func_curtime(uint8 dec_arg) :Item_time_func() { decimals= dec_arg; }
1037  void fix_length_and_dec();
1039  {
1040  DBUG_ASSERT(fixed == 1);
1041  return cached_time.val_packed();
1042  }
1043  bool get_time(MYSQL_TIME *ltime)
1044  {
1045  DBUG_ASSERT(fixed == 1);
1046  return cached_time.get_time(ltime);
1047  }
1048  String *val_str(String *str)
1049  {
1050  DBUG_ASSERT(fixed == 1);
1051  return cached_time.val_str(&str_value);
1052  }
1053 };
1054 
1055 
1057 {
1058 protected:
1059  Time_zone *time_zone();
1060 public:
1061  Item_func_curtime_local(uint8 dec_arg) :Item_func_curtime(dec_arg) {}
1062  const char *func_name() const { return "curtime"; }
1063 };
1064 
1065 
1067 {
1068 protected:
1069  Time_zone *time_zone();
1070 public:
1071  Item_func_curtime_utc(uint8 dec_arg) :Item_func_curtime(dec_arg) {}
1072  const char *func_name() const { return "utc_time"; }
1073 };
1074 
1075 
1076 /* Abstract CURDATE function. See also Item_func_curtime. */
1077 
1079 {
1080  MYSQL_TIME_cache cached_time; // Initialized in fix_length_and_dec
1081 protected:
1082  virtual Time_zone *time_zone()= 0;
1083 public:
1085  void fix_length_and_dec();
1087  {
1088  DBUG_ASSERT(fixed == 1);
1089  return cached_time.val_packed();
1090  }
1091  bool get_date(MYSQL_TIME *res, uint fuzzy_date)
1092  {
1093  DBUG_ASSERT(fixed == 1);
1094  return cached_time.get_time(res);
1095  }
1096  String *val_str(String *str)
1097  {
1098  DBUG_ASSERT(fixed == 1);
1099  return cached_time.val_str(&str_value);
1100  }
1101 };
1102 
1103 
1105 {
1106 protected:
1107  Time_zone *time_zone();
1108 public:
1110  const char *func_name() const { return "curdate"; }
1111 };
1112 
1113 
1115 {
1116 protected:
1117  Time_zone *time_zone();
1118 public:
1120  const char *func_name() const { return "utc_date"; }
1121 };
1122 
1123 
1124 /* Abstract CURRENT_TIMESTAMP function. See also Item_func_curtime */
1125 
1127 {
1128  MYSQL_TIME_cache cached_time;
1129 protected:
1130  virtual Time_zone *time_zone()= 0;
1131 public:
1136  Item_func_now(uint8 dec_arg) :Item_datetime_func() { decimals= dec_arg; }
1137  void fix_length_and_dec();
1138  type_conversion_status save_in_field(Field *to, bool no_conversions);
1140  {
1141  DBUG_ASSERT(fixed == 1);
1142  return cached_time.val_packed();
1143  }
1144  bool get_date(MYSQL_TIME *res, uint fuzzy_date)
1145  {
1146  DBUG_ASSERT(fixed == 1);
1147  return cached_time.get_time(res);
1148  }
1149  String *val_str(String *str)
1150  {
1151  DBUG_ASSERT(fixed == 1);
1152  return cached_time.val_str(&str_value);
1153  }
1154 };
1155 
1156 
1158 {
1159 protected:
1160  Time_zone *time_zone();
1161 public:
1168  static void store_in(Field *field);
1169 
1170  Item_func_now_local(uint8 dec_arg) :Item_func_now(dec_arg) {}
1171  const char *func_name() const { return "now"; }
1172  virtual enum Functype functype() const { return NOW_FUNC; }
1173 };
1174 
1175 
1177 {
1178 protected:
1179  Time_zone *time_zone();
1180 public:
1181  Item_func_now_utc(uint8 dec_arg) :Item_func_now(dec_arg) {}
1182  const char *func_name() const { return "utc_timestamp"; }
1183 };
1184 
1185 
1186 /*
1187  This is like NOW(), but always uses the real current time, not the
1188  query_start(). This matches the Oracle behavior.
1189 */
1191 {
1192 public:
1193  Item_func_sysdate_local(uint8 dec_arg) :
1194  Item_datetime_func() { decimals= dec_arg; }
1195  bool const_item() const { return 0; }
1196  const char *func_name() const { return "sysdate"; }
1197  void fix_length_and_dec();
1198  bool get_date(MYSQL_TIME *res, uint fuzzy_date);
1204  table_map get_initial_pseudo_tables() const { return RAND_TABLE_BIT; }
1205 };
1206 
1207 
1209 {
1210 public:
1212  const char *func_name() const { return "from_days"; }
1213  bool get_date(MYSQL_TIME *res, uint fuzzy_date);
1214  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
1215  bool check_valid_arguments_processor(uchar *int_arg)
1216  {
1217  return has_date_args() || has_time_args();
1218  }
1219 };
1220 
1221 
1223 {
1224  int fixed_length;
1225  const bool is_time_format;
1226  String value;
1227 public:
1228  Item_func_date_format(Item *a,Item *b,bool is_time_format_arg)
1229  :Item_str_func(a,b),is_time_format(is_time_format_arg) {}
1230  String *val_str(String *str);
1231  const char *func_name() const
1232  { return is_time_format ? "time_format" : "date_format"; }
1233  void fix_length_and_dec();
1234  uint format_length(const String *format);
1235  bool eq(const Item *item, bool binary_cmp) const;
1236 };
1237 
1238 
1240 {
1241  THD *thd;
1242  public:
1244  const char *func_name() const { return "from_unixtime"; }
1245  void fix_length_and_dec();
1246  bool get_date(MYSQL_TIME *res, uint fuzzy_date);
1247 };
1248 
1249 
1250 /*
1251  We need Time_zone class declaration for storing pointers in
1252  Item_func_convert_tz.
1253 */
1254 class Time_zone;
1255 
1256 /*
1257  This class represents CONVERT_TZ() function.
1258  The important fact about this function that it is handled in special way.
1259  When such function is met in expression time_zone system tables are added
1260  to global list of tables to open, so later those already opened and locked
1261  tables can be used during this function calculation for loading time zone
1262  descriptions.
1263 */
1265 {
1266  /*
1267  If time zone parameters are constants we are caching objects that
1268  represent them (we use separate from_tz_cached/to_tz_cached members
1269  to indicate this fact, since NULL is legal value for from_tz/to_tz
1270  members.
1271  */
1272  bool from_tz_cached, to_tz_cached;
1273  Time_zone *from_tz, *to_tz;
1274  public:
1275  Item_func_convert_tz(Item *a, Item *b, Item *c):
1276  Item_datetime_func(a, b, c), from_tz_cached(0), to_tz_cached(0) {}
1277  const char *func_name() const { return "convert_tz"; }
1278  void fix_length_and_dec();
1279  bool get_date(MYSQL_TIME *res, uint fuzzy_date);
1280  void cleanup();
1281 };
1282 
1283 
1285 {
1286 public:
1287  Item_func_sec_to_time(Item *item) :Item_time_func(item) {}
1288  void fix_length_and_dec()
1289  {
1290  maybe_null=1;
1291  fix_length_and_dec_and_charset_datetime(MAX_TIME_WIDTH,
1292  MY_MIN(args[0]->decimals,
1293  DATETIME_MAX_DECIMALS));
1294  }
1295  const char *func_name() const { return "sec_to_time"; }
1296  bool get_time(MYSQL_TIME *ltime);
1297 };
1298 
1299 
1301 {
1302  String value;
1303  bool get_date_internal(MYSQL_TIME *res, uint fuzzy_date);
1304  bool get_time_internal(MYSQL_TIME *res);
1305 protected:
1306  bool val_datetime(MYSQL_TIME *ltime, uint fuzzy_date);
1307 
1308 public:
1309  const interval_type int_type; // keep it public
1310  const bool date_sub_interval; // keep it public
1311  Item_date_add_interval(Item *a,Item *b,interval_type type_arg,bool neg_arg)
1313  int_type(type_arg), date_sub_interval(neg_arg) {}
1314  const char *func_name() const { return "date_add_interval"; }
1315  void fix_length_and_dec();
1316  bool eq(const Item *item, bool binary_cmp) const;
1317  void print(String *str, enum_query_type query_type);
1318 };
1319 
1320 
1322 {
1323  bool date_value;
1324  public:
1325  const interval_type int_type; // keep it public
1326  Item_extract(interval_type type_arg, Item *a)
1327  :Item_int_func(a), int_type(type_arg) {}
1328  longlong val_int();
1329  enum Functype functype() const { return EXTRACT_FUNC; }
1330  const char *func_name() const { return "extract"; }
1331  void fix_length_and_dec();
1332  bool eq(const Item *item, bool binary_cmp) const;
1333  virtual void print(String *str, enum_query_type query_type);
1334  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
1335  bool check_valid_arguments_processor(uchar *int_arg)
1336  {
1337  switch (int_type) {
1338  case INTERVAL_YEAR:
1339  case INTERVAL_YEAR_MONTH:
1340  case INTERVAL_QUARTER:
1341  case INTERVAL_MONTH:
1342  /* case INTERVAL_WEEK: Not allowed as partitioning function, bug#57071 */
1343  case INTERVAL_DAY:
1344  return !has_date_args();
1345  case INTERVAL_DAY_HOUR:
1346  case INTERVAL_DAY_MINUTE:
1347  case INTERVAL_DAY_SECOND:
1348  case INTERVAL_DAY_MICROSECOND:
1349  return !has_datetime_args();
1350  case INTERVAL_HOUR:
1351  case INTERVAL_HOUR_MINUTE:
1352  case INTERVAL_HOUR_SECOND:
1353  case INTERVAL_MINUTE:
1354  case INTERVAL_MINUTE_SECOND:
1355  case INTERVAL_SECOND:
1356  case INTERVAL_MICROSECOND:
1357  case INTERVAL_HOUR_MICROSECOND:
1358  case INTERVAL_MINUTE_MICROSECOND:
1359  case INTERVAL_SECOND_MICROSECOND:
1360  return !has_time_args();
1361  default:
1362  /*
1363  INTERVAL_LAST is only an end marker,
1364  INTERVAL_WEEK depends on default_week_format which is a session
1365  variable and cannot be used for partitioning. See bug#57071.
1366  */
1367  break;
1368  }
1369  return true;
1370  }
1371 };
1372 
1373 
1375 {
1376 public:
1377  Item_date_typecast(Item *a) :Item_date_func(a) { maybe_null= 1; }
1378  void print(String *str, enum_query_type query_type);
1379  const char *func_name() const { return "cast_as_date"; }
1380  bool get_date(MYSQL_TIME *ltime, uint fuzzy_date);
1381  const char *cast_type() const { return "date"; }
1382 };
1383 
1384 
1386 {
1387  bool detect_precision_from_arg;
1388 public:
1390  {
1391  detect_precision_from_arg= true;
1392  }
1393  Item_time_typecast(Item *a, uint8 dec_arg): Item_time_func(a)
1394  {
1395  detect_precision_from_arg= false;
1396  decimals= dec_arg;
1397  }
1398  void print(String *str, enum_query_type query_type);
1399  const char *func_name() const { return "cast_as_time"; }
1400  bool get_time(MYSQL_TIME *ltime);
1401  const char *cast_type() const { return "time"; }
1402  void fix_length_and_dec()
1403  {
1404  maybe_null= 1;
1405  fix_length_and_dec_and_charset_datetime(MAX_TIME_WIDTH,
1406  detect_precision_from_arg ?
1407  args[0]->time_precision() :
1408  decimals);
1409  }
1410 };
1411 
1412 
1414 {
1415  bool detect_precision_from_arg;
1416 public:
1418  {
1419  detect_precision_from_arg= true;
1420  }
1421  Item_datetime_typecast(Item *a, uint8 dec_arg) :Item_datetime_func(a)
1422  {
1423  detect_precision_from_arg= false;
1424  decimals= dec_arg;
1425  }
1426  void print(String *str, enum_query_type query_type);
1427  const char *func_name() const { return "cast_as_datetime"; }
1428  const char *cast_type() const { return "datetime"; }
1429  void fix_length_and_dec()
1430  {
1431  maybe_null= 1;
1432  fix_length_and_dec_and_charset_datetime(MAX_DATETIME_WIDTH,
1433  detect_precision_from_arg ?
1434  args[0]->datetime_precision():
1435  decimals);
1436  }
1437  bool get_date(MYSQL_TIME *res, uint fuzzy_date);
1438 };
1439 
1440 
1442 {
1443 public:
1444  Item_func_makedate(Item *a, Item *b) :Item_date_func(a, b) { maybe_null= 1; }
1445  const char *func_name() const { return "makedate"; }
1446  bool get_date(MYSQL_TIME *ltime, uint fuzzy_date);
1447 };
1448 
1449 
1451 {
1452  const bool is_date;
1453  int sign;
1454  bool val_datetime(MYSQL_TIME *time, uint fuzzy_date);
1455 public:
1456  Item_func_add_time(Item *a, Item *b, bool type_arg, bool neg_arg)
1457  :Item_temporal_hybrid_func(a, b), is_date(type_arg)
1458  {
1459  sign= neg_arg ? -1 : 1;
1460  }
1461  void fix_length_and_dec();
1462  void print(String *str, enum_query_type query_type);
1463  const char *func_name() const { return "add_time"; }
1464 };
1465 
1466 
1468 {
1469 public:
1470  Item_func_timediff(Item *a, Item *b) :Item_time_func(a, b) {}
1471  const char *func_name() const { return "timediff"; }
1472  void fix_length_and_dec()
1473  {
1474  uint dec= MY_MAX(args[0]->time_precision(), args[1]->time_precision());
1475  fix_length_and_dec_and_charset_datetime(MAX_TIME_WIDTH, dec);
1476  maybe_null= 1;
1477  }
1478  bool get_time(MYSQL_TIME *ltime);
1479 };
1480 
1482 {
1483 public:
1484  Item_func_maketime(Item *a, Item *b, Item *c) :Item_time_func(a, b, c)
1485  {
1486  maybe_null= TRUE;
1487  }
1488  void fix_length_and_dec()
1489  {
1490  fix_length_and_dec_and_charset_datetime(MAX_TIME_WIDTH,
1491  MY_MIN(args[2]->decimals,
1492  DATETIME_MAX_DECIMALS));
1493  }
1494  const char *func_name() const { return "maketime"; }
1495  bool get_time(MYSQL_TIME *ltime);
1496 };
1497 
1499 {
1500 public:
1502  longlong val_int();
1503  const char *func_name() const { return "microsecond"; }
1504  void fix_length_and_dec()
1505  {
1506  maybe_null=1;
1507  }
1508  bool check_partition_func_processor(uchar *int_arg) {return FALSE;}
1509  bool check_valid_arguments_processor(uchar *int_arg)
1510  {
1511  return !has_time_args();
1512  }
1513 };
1514 
1515 
1517 {
1518  const interval_type int_type;
1519 public:
1520  Item_func_timestamp_diff(Item *a,Item *b,interval_type type_arg)
1521  :Item_int_func(a,b), int_type(type_arg) {}
1522  const char *func_name() const { return "timestampdiff"; }
1523  longlong val_int();
1524  void fix_length_and_dec()
1525  {
1526  maybe_null=1;
1527  }
1528  virtual void print(String *str, enum_query_type query_type);
1529 };
1530 
1531 
1532 enum date_time_format
1533 {
1534  USA_FORMAT, JIS_FORMAT, ISO_FORMAT, EUR_FORMAT, INTERNAL_FORMAT
1535 };
1536 
1538 {
1539 public:
1540  const timestamp_type type; // keep it public
1541  Item_func_get_format(timestamp_type type_arg, Item *a)
1542  :Item_str_ascii_func(a), type(type_arg)
1543  {}
1544  String *val_str_ascii(String *str);
1545  const char *func_name() const { return "get_format"; }
1546  void fix_length_and_dec()
1547  {
1548  maybe_null= 1;
1549  decimals=0;
1550  fix_length_and_charset(17, default_charset());
1551  }
1552  virtual void print(String *str, enum_query_type query_type);
1553 };
1554 
1555 
1557 {
1558  timestamp_type cached_timestamp_type;
1559  bool const_item;
1560  void fix_from_format(const char *format, uint length);
1561 protected:
1562  bool val_datetime(MYSQL_TIME *ltime, uint fuzzy_date);
1563 public:
1565  :Item_temporal_hybrid_func(a, b), const_item(false)
1566  {}
1567  const char *func_name() const { return "str_to_date"; }
1568  void fix_length_and_dec();
1569 };
1570 
1571 
1573 {
1574 public:
1575  Item_func_last_day(Item *a) :Item_date_func(a) { maybe_null= 1; }
1576  const char *func_name() const { return "last_day"; }
1577  bool get_date(MYSQL_TIME *res, uint fuzzy_date);
1578 };
1579 
1580 
1581 /* Function prototypes */
1582 
1583 bool make_date_time(DATE_TIME_FORMAT *format, MYSQL_TIME *l_time,
1584  timestamp_type type, String *str);
1585 
1586 #endif /* ITEM_TIMEFUNC_INCLUDED */