1 #ifndef ITEM_FUNC_INCLUDED 2 #define ITEM_FUNC_INCLUDED 3 /* Copyright (c) 2000, 2016, Oracle and/or its affiliates. 4 Copyright (c) 2009, 2020, MariaDB 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 Street, Fifth Floor, Boston, MA 02110-1335 USA */ 18 19 20 /* Function items used by mysql */ 21 22 #ifdef USE_PRAGMA_INTERFACE 23 #pragma interface /* gcc class implementation */ 24 #endif 25 26 #ifdef HAVE_IEEEFP_H 27 extern "C" /* Bug in BSDI include file */ 28 { 29 #include <ieeefp.h> 30 } 31 #endif 32 33 #include "sql_udf.h" // udf_handler 34 #include "my_decimal.h" // string2my_decimal 35 #include <cmath> 36 37 38 class Item_func :public Item_func_or_sum, 39 protected With_sum_func_cache 40 { 41 void sync_with_sum_func_and_with_field(List<Item> &list); 42 protected: check_arguments()43 virtual bool check_arguments() const 44 { 45 return check_argument_types_scalar(0, arg_count); 46 } 47 bool check_argument_types_like_args0() const; 48 bool check_argument_types_scalar(uint start, uint end) const; 49 bool check_argument_types_traditional_scalar(uint start, uint end) const; 50 bool check_argument_types_or_binary(const Type_handler *handler, 51 uint start, uint end) const; 52 bool check_argument_types_can_return_int(uint start, uint end) const; 53 bool check_argument_types_can_return_real(uint start, uint end) const; 54 bool check_argument_types_can_return_str(uint start, uint end) const; 55 bool check_argument_types_can_return_text(uint start, uint end) const; 56 bool check_argument_types_can_return_date(uint start, uint end) const; 57 bool check_argument_types_can_return_time(uint start, uint end) const; 58 void print_cast_temporal(String *str, enum_query_type query_type); 59 public: 60 61 table_map not_null_tables_cache; 62 63 enum Functype { UNKNOWN_FUNC,EQ_FUNC,EQUAL_FUNC,NE_FUNC,LT_FUNC,LE_FUNC, 64 GE_FUNC,GT_FUNC,FT_FUNC, 65 LIKE_FUNC,ISNULL_FUNC,ISNOTNULL_FUNC, 66 COND_AND_FUNC, COND_OR_FUNC, XOR_FUNC, 67 BETWEEN, IN_FUNC, MULT_EQUAL_FUNC, 68 INTERVAL_FUNC, ISNOTNULLTEST_FUNC, 69 SP_EQUALS_FUNC, SP_DISJOINT_FUNC,SP_INTERSECTS_FUNC, 70 SP_TOUCHES_FUNC,SP_CROSSES_FUNC,SP_WITHIN_FUNC, 71 SP_CONTAINS_FUNC,SP_OVERLAPS_FUNC, 72 SP_STARTPOINT,SP_ENDPOINT,SP_EXTERIORRING, 73 SP_POINTN,SP_GEOMETRYN,SP_INTERIORRINGN, SP_RELATE_FUNC, 74 NOT_FUNC, NOT_ALL_FUNC, TEMPTABLE_ROWID, 75 NOW_FUNC, NOW_UTC_FUNC, SYSDATE_FUNC, TRIG_COND_FUNC, 76 SUSERVAR_FUNC, GUSERVAR_FUNC, COLLATE_FUNC, 77 EXTRACT_FUNC, CHAR_TYPECAST_FUNC, FUNC_SP, UDF_FUNC, 78 NEG_FUNC, GSYSVAR_FUNC, IN_OPTIMIZER_FUNC, DYNCOL_FUNC, 79 JSON_EXTRACT_FUNC, JSON_VALID_FUNC, 80 CASE_SEARCHED_FUNC, // Used by ColumnStore/Spider 81 CASE_SIMPLE_FUNC // Used by ColumnStore/spider 82 }; functype_to_scalar_comparison_op(Functype type)83 static scalar_comparison_op functype_to_scalar_comparison_op(Functype type) 84 { 85 switch (type) { 86 case EQ_FUNC: return SCALAR_CMP_EQ; 87 case EQUAL_FUNC: return SCALAR_CMP_EQUAL; 88 case LT_FUNC: return SCALAR_CMP_LT; 89 case LE_FUNC: return SCALAR_CMP_LE; 90 case GE_FUNC: return SCALAR_CMP_GE; 91 case GT_FUNC: return SCALAR_CMP_GT; 92 default: break; 93 } 94 DBUG_ASSERT(0); 95 return SCALAR_CMP_EQ; 96 } type()97 enum Type type() const { return FUNC_ITEM; } functype()98 virtual enum Functype functype() const { return UNKNOWN_FUNC; } Item_func(THD * thd)99 Item_func(THD *thd): Item_func_or_sum(thd) 100 { 101 with_field= 0; 102 with_param= 0; 103 } Item_func(THD * thd,Item * a)104 Item_func(THD *thd, Item *a) 105 :Item_func_or_sum(thd, a), With_sum_func_cache(a) 106 { 107 with_param= a->with_param; 108 with_field= a->with_field; 109 } Item_func(THD * thd,Item * a,Item * b)110 Item_func(THD *thd, Item *a, Item *b) 111 :Item_func_or_sum(thd, a, b), With_sum_func_cache(a, b) 112 { 113 with_param= a->with_param || b->with_param; 114 with_field= a->with_field || b->with_field; 115 } Item_func(THD * thd,Item * a,Item * b,Item * c)116 Item_func(THD *thd, Item *a, Item *b, Item *c) 117 :Item_func_or_sum(thd, a, b, c), With_sum_func_cache(a, b, c) 118 { 119 with_field= a->with_field || b->with_field || c->with_field; 120 with_param= a->with_param || b->with_param || c->with_param; 121 } Item_func(THD * thd,Item * a,Item * b,Item * c,Item * d)122 Item_func(THD *thd, Item *a, Item *b, Item *c, Item *d) 123 :Item_func_or_sum(thd, a, b, c, d), With_sum_func_cache(a, b, c, d) 124 { 125 with_field= a->with_field || b->with_field || 126 c->with_field || d->with_field; 127 with_param= a->with_param || b->with_param || 128 c->with_param || d->with_param; 129 } Item_func(THD * thd,Item * a,Item * b,Item * c,Item * d,Item * e)130 Item_func(THD *thd, Item *a, Item *b, Item *c, Item *d, Item* e) 131 :Item_func_or_sum(thd, a, b, c, d, e), With_sum_func_cache(a, b, c, d, e) 132 { 133 with_field= a->with_field || b->with_field || 134 c->with_field || d->with_field || e->with_field; 135 with_param= a->with_param || b->with_param || 136 c->with_param || d->with_param || e->with_param; 137 } Item_func(THD * thd,List<Item> & list)138 Item_func(THD *thd, List<Item> &list): 139 Item_func_or_sum(thd, list) 140 { 141 set_arguments(thd, list); 142 } 143 // Constructor used for Item_cond_and/or (see Item comment) Item_func(THD * thd,Item_func * item)144 Item_func(THD *thd, Item_func *item) 145 :Item_func_or_sum(thd, item), With_sum_func_cache(item), 146 not_null_tables_cache(item->not_null_tables_cache) 147 { } 148 bool fix_fields(THD *, Item **ref); cleanup()149 void cleanup() 150 { 151 Item_func_or_sum::cleanup(); 152 used_tables_and_const_cache_init(); 153 } 154 void fix_after_pullout(st_select_lex *new_parent, Item **ref, bool merge); 155 void quick_fix_field(); 156 table_map not_null_tables() const; update_used_tables()157 void update_used_tables() 158 { 159 used_tables_and_const_cache_init(); 160 used_tables_and_const_cache_update_and_join(arg_count, args); 161 } 162 COND *build_equal_items(THD *thd, COND_EQUAL *inherited, 163 bool link_item_fields, 164 COND_EQUAL **cond_equal_ref); get_mm_tree(RANGE_OPT_PARAM * param,Item ** cond_ptr)165 SEL_TREE *get_mm_tree(RANGE_OPT_PARAM *param, Item **cond_ptr) 166 { 167 DBUG_ENTER("Item_func::get_mm_tree"); 168 DBUG_RETURN(const_item() ? get_mm_tree_for_const(param) : NULL); 169 } 170 bool eq(const Item *item, bool binary_cmp) const; key_item()171 virtual Item *key_item() const { return args[0]; } set_arguments(THD * thd,List<Item> & list)172 void set_arguments(THD *thd, List<Item> &list) 173 { 174 Item_args::set_arguments(thd, list); 175 sync_with_sum_func_and_with_field(list); 176 list.empty(); // Fields are used 177 } 178 void split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array, 179 List<Item> &fields, uint flags); 180 virtual void print(String *str, enum_query_type query_type); 181 void print_op(String *str, enum_query_type query_type); 182 void print_args(String *str, uint from, enum_query_type query_type); is_null()183 bool is_null() { 184 update_null_value(); 185 return null_value; 186 } 187 String *val_str_from_val_str_ascii(String *str, String *str2); 188 189 void signal_divide_by_null(); 190 friend class udf_handler; create_field_for_create_select(MEM_ROOT * root,TABLE * table)191 Field *create_field_for_create_select(MEM_ROOT *root, TABLE *table) 192 { return tmp_table_field_from_field_type(root, table); } 193 Item *get_tmp_table_item(THD *thd); 194 fix_char_length_ulonglong(ulonglong max_char_length_arg)195 void fix_char_length_ulonglong(ulonglong max_char_length_arg) 196 { 197 ulonglong max_result_length= max_char_length_arg * 198 collation.collation->mbmaxlen; 199 if (max_result_length >= MAX_BLOB_WIDTH) 200 { 201 max_length= MAX_BLOB_WIDTH; 202 maybe_null= 1; 203 } 204 else 205 max_length= (uint32) max_result_length; 206 } 207 Item *transform(THD *thd, Item_transformer transformer, uchar *arg); 208 Item* compile(THD *thd, Item_analyzer analyzer, uchar **arg_p, 209 Item_transformer transformer, uchar *arg_t); 210 void traverse_cond(Cond_traverser traverser, 211 void * arg, traverse_order order); 212 bool eval_not_null_tables(void *opt_arg); 213 bool find_not_null_fields(table_map allowed); 214 // bool is_expensive_processor(void *arg); 215 // virtual bool is_expensive() { return 0; } raise_numeric_overflow(const char * type_name)216 inline void raise_numeric_overflow(const char *type_name) 217 { 218 char buf[256]; 219 String str(buf, sizeof(buf), system_charset_info); 220 str.length(0); 221 print(&str, QT_NO_DATA_EXPANSION); 222 my_error(ER_DATA_OUT_OF_RANGE, MYF(0), type_name, str.c_ptr_safe()); 223 } raise_float_overflow()224 inline double raise_float_overflow() 225 { 226 raise_numeric_overflow("DOUBLE"); 227 return 0.0; 228 } raise_integer_overflow()229 inline longlong raise_integer_overflow() 230 { 231 raise_numeric_overflow(unsigned_flag ? "BIGINT UNSIGNED": "BIGINT"); 232 return 0; 233 } raise_decimal_overflow()234 inline int raise_decimal_overflow() 235 { 236 raise_numeric_overflow("DECIMAL"); 237 return E_DEC_OVERFLOW; 238 } 239 /** 240 Throw an error if the input double number is not finite, i.e. is either 241 +/-INF or NAN. 242 */ check_float_overflow(double value)243 inline double check_float_overflow(double value) 244 { 245 return std::isfinite(value) ? value : raise_float_overflow(); 246 } 247 /** 248 Throw an error if the input BIGINT value represented by the 249 (longlong value, bool unsigned flag) pair cannot be returned by the 250 function, i.e. is not compatible with this Item's unsigned_flag. 251 */ check_integer_overflow(longlong value,bool val_unsigned)252 inline longlong check_integer_overflow(longlong value, bool val_unsigned) 253 { 254 if ((unsigned_flag && !val_unsigned && value < 0) || 255 (!unsigned_flag && val_unsigned && 256 (ulonglong) value > (ulonglong) LONGLONG_MAX)) 257 return raise_integer_overflow(); 258 return value; 259 } 260 /** 261 Throw an error if the error code of a DECIMAL operation is E_DEC_OVERFLOW. 262 */ check_decimal_overflow(int error)263 inline int check_decimal_overflow(int error) 264 { 265 return (error == E_DEC_OVERFLOW) ? raise_decimal_overflow() : error; 266 } 267 has_timestamp_args()268 bool has_timestamp_args() 269 { 270 DBUG_ASSERT(fixed == TRUE); 271 for (uint i= 0; i < arg_count; i++) 272 { 273 if (args[i]->type() == Item::FIELD_ITEM && 274 args[i]->field_type() == MYSQL_TYPE_TIMESTAMP) 275 return TRUE; 276 } 277 return FALSE; 278 } 279 has_date_args()280 bool has_date_args() 281 { 282 DBUG_ASSERT(fixed == TRUE); 283 for (uint i= 0; i < arg_count; i++) 284 { 285 if (args[i]->type() == Item::FIELD_ITEM && 286 (args[i]->field_type() == MYSQL_TYPE_DATE || 287 args[i]->field_type() == MYSQL_TYPE_DATETIME)) 288 return TRUE; 289 } 290 return FALSE; 291 } 292 has_time_args()293 bool has_time_args() 294 { 295 DBUG_ASSERT(fixed == TRUE); 296 for (uint i= 0; i < arg_count; i++) 297 { 298 if (args[i]->type() == Item::FIELD_ITEM && 299 (args[i]->field_type() == MYSQL_TYPE_TIME || 300 args[i]->field_type() == MYSQL_TYPE_DATETIME)) 301 return TRUE; 302 } 303 return FALSE; 304 } 305 has_datetime_args()306 bool has_datetime_args() 307 { 308 DBUG_ASSERT(fixed == TRUE); 309 for (uint i= 0; i < arg_count; i++) 310 { 311 if (args[i]->type() == Item::FIELD_ITEM && 312 args[i]->field_type() == MYSQL_TYPE_DATETIME) 313 return TRUE; 314 } 315 return FALSE; 316 } 317 propagate_equal_fields(THD * thd,const Context & ctx,COND_EQUAL * cond)318 Item* propagate_equal_fields(THD *thd, const Context &ctx, COND_EQUAL *cond) 319 { 320 /* 321 By default only substitution for a field whose two different values 322 are never equal is allowed in the arguments of a function. 323 This is overruled for the direct arguments of comparison functions. 324 */ 325 Item_args::propagate_equal_fields(thd, Context_identity(), cond); 326 return this; 327 } 328 has_rand_bit()329 bool has_rand_bit() 330 { 331 return used_tables() & RAND_TABLE_BIT; 332 } 333 excl_dep_on_table(table_map tab_map)334 bool excl_dep_on_table(table_map tab_map) 335 { 336 if (used_tables() & OUTER_REF_TABLE_BIT) 337 return false; 338 return !(used_tables() & ~tab_map) || 339 Item_args::excl_dep_on_table(tab_map); 340 } 341 excl_dep_on_grouping_fields(st_select_lex * sel)342 bool excl_dep_on_grouping_fields(st_select_lex *sel) 343 { 344 if (has_rand_bit() || with_subquery()) 345 return false; 346 return Item_args::excl_dep_on_grouping_fields(sel); 347 } 348 excl_dep_on_in_subq_left_part(Item_in_subselect * subq_pred)349 bool excl_dep_on_in_subq_left_part(Item_in_subselect *subq_pred) 350 { 351 return Item_args::excl_dep_on_in_subq_left_part(subq_pred); 352 } 353 354 /* 355 We assume the result of any function that has a TIMESTAMP argument to be 356 timezone-dependent, since a TIMESTAMP value in both numeric and string 357 contexts is interpreted according to the current timezone. 358 The only exception is UNIX_TIMESTAMP() which returns the internal 359 representation of a TIMESTAMP argument verbatim, and thus does not depend on 360 the timezone. 361 */ check_valid_arguments_processor(void * bool_arg)362 virtual bool check_valid_arguments_processor(void *bool_arg) 363 { 364 return has_timestamp_args(); 365 } 366 find_function_processor(void * arg)367 virtual bool find_function_processor (void *arg) 368 { 369 return functype() == *(Functype *) arg; 370 } 371 no_rows_in_result()372 void no_rows_in_result() 373 { 374 for (uint i= 0; i < arg_count; i++) 375 { 376 args[i]->no_rows_in_result(); 377 } 378 } restore_to_before_no_rows_in_result()379 void restore_to_before_no_rows_in_result() 380 { 381 for (uint i= 0; i < arg_count; i++) 382 { 383 args[i]->no_rows_in_result(); 384 } 385 } 386 void convert_const_compared_to_int_field(THD *thd); 387 /** 388 Prepare arguments and setup a comparator. 389 Used in Item_func_xxx with two arguments and a comparator, 390 e.g. Item_bool_func2 and Item_func_nullif. 391 args[0] or args[1] can be modified: 392 - converted to character set and collation of the operation 393 - or replaced to an Item_int_with_ref 394 */ 395 bool setup_args_and_comparator(THD *thd, Arg_comparator *cmp); 396 with_sum_func()397 bool with_sum_func() const { return m_with_sum_func; } get_with_sum_func_cache()398 With_sum_func_cache* get_with_sum_func_cache() { return this; } get_item_func()399 Item_func *get_item_func() { return this; } is_simplified_cond_processor(void * arg)400 bool is_simplified_cond_processor(void *arg) 401 { return const_item() && !val_int(); } 402 }; 403 404 405 class Item_real_func :public Item_func 406 { 407 public: Item_real_func(THD * thd)408 Item_real_func(THD *thd): Item_func(thd) { collation= DTCollation_numeric(); } Item_real_func(THD * thd,Item * a)409 Item_real_func(THD *thd, Item *a): Item_func(thd, a) 410 { collation= DTCollation_numeric(); } Item_real_func(THD * thd,Item * a,Item * b)411 Item_real_func(THD *thd, Item *a, Item *b): Item_func(thd, a, b) 412 { collation= DTCollation_numeric(); } Item_real_func(THD * thd,List<Item> & list)413 Item_real_func(THD *thd, List<Item> &list): Item_func(thd, list) 414 { collation= DTCollation_numeric(); } 415 String *val_str(String*str); 416 my_decimal *val_decimal(my_decimal *decimal_value); val_int()417 longlong val_int() 418 { 419 DBUG_ASSERT(fixed == 1); 420 return Converter_double_to_longlong(val_real(), unsigned_flag).result(); 421 } get_date(THD * thd,MYSQL_TIME * ltime,date_mode_t fuzzydate)422 bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) 423 { return get_date_from_real(thd, ltime, fuzzydate); } type_handler()424 const Type_handler *type_handler() const { return &type_handler_double; } fix_length_and_dec()425 bool fix_length_and_dec() 426 { 427 decimals= NOT_FIXED_DEC; 428 max_length= float_length(decimals); 429 return FALSE; 430 } 431 }; 432 433 434 /** 435 Functions whose returned field type is determined at fix_fields() time. 436 */ 437 class Item_hybrid_func: public Item_func, 438 public Type_handler_hybrid_field_type 439 { 440 protected: 441 bool fix_attributes(Item **item, uint nitems); 442 public: Item_hybrid_func(THD * thd)443 Item_hybrid_func(THD *thd): Item_func(thd) { } Item_hybrid_func(THD * thd,Item * a)444 Item_hybrid_func(THD *thd, Item *a): Item_func(thd, a) { } Item_hybrid_func(THD * thd,Item * a,Item * b)445 Item_hybrid_func(THD *thd, Item *a, Item *b): Item_func(thd, a, b) { } Item_hybrid_func(THD * thd,Item * a,Item * b,Item * c)446 Item_hybrid_func(THD *thd, Item *a, Item *b, Item *c): 447 Item_func(thd, a, b, c) { } Item_hybrid_func(THD * thd,List<Item> & list)448 Item_hybrid_func(THD *thd, List<Item> &list): Item_func(thd, list) { } Item_hybrid_func(THD * thd,Item_hybrid_func * item)449 Item_hybrid_func(THD *thd, Item_hybrid_func *item) 450 :Item_func(thd, item), Type_handler_hybrid_field_type(item) { } type_handler()451 const Type_handler *type_handler() const 452 { return Type_handler_hybrid_field_type::type_handler(); } fix_length_and_dec_long_or_longlong(uint char_length,bool unsigned_arg)453 void fix_length_and_dec_long_or_longlong(uint char_length, bool unsigned_arg) 454 { 455 collation= DTCollation_numeric(); 456 unsigned_flag= unsigned_arg; 457 max_length= char_length; 458 set_handler(Type_handler::type_handler_long_or_longlong(char_length, 459 unsigned_arg)); 460 } fix_length_and_dec_ulong_or_ulonglong_by_nbits(uint nbits)461 void fix_length_and_dec_ulong_or_ulonglong_by_nbits(uint nbits) 462 { 463 uint digits= Type_handler_bit::Bit_decimal_notation_int_digits_by_nbits(nbits); 464 collation= DTCollation_numeric(); 465 unsigned_flag= true; 466 max_length= digits; 467 if (nbits > 32) 468 set_handler(&type_handler_ulonglong); 469 else 470 set_handler(&type_handler_ulong); 471 } 472 }; 473 474 475 class Item_handled_func: public Item_func 476 { 477 public: 478 class Handler 479 { 480 public: ~Handler()481 virtual ~Handler() { } 482 virtual String *val_str(Item_handled_func *, String *) const= 0; 483 virtual String *val_str_ascii(Item_handled_func *, String *) const= 0; 484 virtual double val_real(Item_handled_func *) const= 0; 485 virtual longlong val_int(Item_handled_func *) const= 0; 486 virtual my_decimal *val_decimal(Item_handled_func *, my_decimal *) const= 0; 487 virtual bool get_date(THD *thd, Item_handled_func *, MYSQL_TIME *, date_mode_t fuzzydate) const= 0; val_native(THD * thd,Item_handled_func *,Native * to)488 virtual bool val_native(THD *thd, Item_handled_func *, Native *to) const 489 { 490 DBUG_ASSERT(0); 491 to->length(0); 492 return true; 493 } 494 virtual const Type_handler * 495 return_type_handler(const Item_handled_func *item) const= 0; 496 virtual const Type_handler * type_handler_for_create_select(const Item_handled_func * item)497 type_handler_for_create_select(const Item_handled_func *item) const 498 { 499 return return_type_handler(item); 500 } 501 virtual bool fix_length_and_dec(Item_handled_func *) const= 0; 502 }; 503 504 class Handler_str: public Handler 505 { 506 public: val_str_ascii(Item_handled_func * item,String * str)507 String *val_str_ascii(Item_handled_func *item, String *str) const 508 { 509 return item->Item::val_str_ascii(str); 510 } val_real(Item_handled_func * item)511 double val_real(Item_handled_func *item) const 512 { 513 DBUG_ASSERT(item->is_fixed()); 514 StringBuffer<64> tmp; 515 String *res= item->val_str(&tmp); 516 return res ? item->double_from_string_with_check(res) : 0.0; 517 } val_int(Item_handled_func * item)518 longlong val_int(Item_handled_func *item) const 519 { 520 DBUG_ASSERT(item->is_fixed()); 521 StringBuffer<22> tmp; 522 String *res= item->val_str(&tmp); 523 return res ? item->longlong_from_string_with_check(res) : 0; 524 } val_decimal(Item_handled_func * item,my_decimal * to)525 my_decimal *val_decimal(Item_handled_func *item, my_decimal *to) const 526 { 527 return item->val_decimal_from_string(to); 528 } get_date(THD * thd,Item_handled_func * item,MYSQL_TIME * to,date_mode_t fuzzydate)529 bool get_date(THD *thd, Item_handled_func *item, MYSQL_TIME *to, 530 date_mode_t fuzzydate) const 531 { 532 return item->get_date_from_string(thd, to, fuzzydate); 533 } 534 }; 535 536 /** 537 Abstract class for functions returning TIME, DATE, DATETIME or string values, 538 whose data type depends on parameters and is set at fix_fields time. 539 */ 540 class Handler_temporal: public Handler 541 { 542 public: val_str(Item_handled_func * item,String * to)543 String *val_str(Item_handled_func *item, String *to) const 544 { 545 StringBuffer<MAX_FIELD_WIDTH> ascii_buf; 546 return item->val_str_from_val_str_ascii(to, &ascii_buf); 547 } 548 }; 549 550 /** 551 Abstract class for functions returning strings, 552 which are generated from get_date() results, 553 when get_date() can return different MYSQL_TIMESTAMP_XXX per row. 554 */ 555 class Handler_temporal_string: public Handler_temporal 556 { 557 public: return_type_handler(const Item_handled_func *)558 const Type_handler *return_type_handler(const Item_handled_func *) const 559 { 560 return &type_handler_string; 561 } 562 const Type_handler * type_handler_for_create_select(const Item_handled_func * item)563 type_handler_for_create_select(const Item_handled_func *item) const 564 { 565 return return_type_handler(item)->type_handler_for_tmp_table(item); 566 } val_real(Item_handled_func * item)567 double val_real(Item_handled_func *item) const 568 { 569 return Temporal_hybrid(item).to_double(); 570 } val_int(Item_handled_func * item)571 longlong val_int(Item_handled_func *item) const 572 { 573 return Temporal_hybrid(item).to_longlong(); 574 } val_decimal(Item_handled_func * item,my_decimal * to)575 my_decimal *val_decimal(Item_handled_func *item, my_decimal *to) const 576 { 577 return Temporal_hybrid(item).to_decimal(to); 578 } val_str_ascii(Item_handled_func * item,String * to)579 String *val_str_ascii(Item_handled_func *item, String *to) const 580 { 581 return Temporal_hybrid(item).to_string(to, item->decimals); 582 } 583 }; 584 585 586 class Handler_date: public Handler_temporal 587 { 588 public: return_type_handler(const Item_handled_func *)589 const Type_handler *return_type_handler(const Item_handled_func *) const 590 { 591 return &type_handler_newdate; 592 } fix_length_and_dec(Item_handled_func * item)593 bool fix_length_and_dec(Item_handled_func *item) const 594 { 595 item->fix_attributes_date(); 596 return false; 597 } val_real(Item_handled_func * item)598 double val_real(Item_handled_func *item) const 599 { 600 return Date(item).to_double(); 601 } val_int(Item_handled_func * item)602 longlong val_int(Item_handled_func *item) const 603 { 604 return Date(item).to_longlong(); 605 } val_decimal(Item_handled_func * item,my_decimal * to)606 my_decimal *val_decimal(Item_handled_func *item, my_decimal *to) const 607 { 608 return Date(item).to_decimal(to); 609 } val_str_ascii(Item_handled_func * item,String * to)610 String *val_str_ascii(Item_handled_func *item, String *to) const 611 { 612 return Date(item).to_string(to); 613 } 614 }; 615 616 617 class Handler_time: public Handler_temporal 618 { 619 public: return_type_handler(const Item_handled_func *)620 const Type_handler *return_type_handler(const Item_handled_func *) const 621 { 622 return &type_handler_time2; 623 } val_real(Item_handled_func * item)624 double val_real(Item_handled_func *item) const 625 { 626 return Time(item).to_double(); 627 } val_int(Item_handled_func * item)628 longlong val_int(Item_handled_func *item) const 629 { 630 return Time(item).to_longlong(); 631 } val_decimal(Item_handled_func * item,my_decimal * to)632 my_decimal *val_decimal(Item_handled_func *item, my_decimal *to) const 633 { 634 return Time(item).to_decimal(to); 635 } val_str_ascii(Item_handled_func * item,String * to)636 String *val_str_ascii(Item_handled_func *item, String *to) const 637 { 638 return Time(item).to_string(to, item->decimals); 639 } val_native(THD * thd,Item_handled_func * item,Native * to)640 bool val_native(THD *thd, Item_handled_func *item, Native *to) const 641 { 642 return Time(thd, item).to_native(to, item->decimals); 643 } 644 }; 645 646 647 class Handler_datetime: public Handler_temporal 648 { 649 public: return_type_handler(const Item_handled_func *)650 const Type_handler *return_type_handler(const Item_handled_func *) const 651 { 652 return &type_handler_datetime2; 653 } val_real(Item_handled_func * item)654 double val_real(Item_handled_func *item) const 655 { 656 return Datetime(item).to_double(); 657 } val_int(Item_handled_func * item)658 longlong val_int(Item_handled_func *item) const 659 { 660 return Datetime(item).to_longlong(); 661 } val_decimal(Item_handled_func * item,my_decimal * to)662 my_decimal *val_decimal(Item_handled_func *item, my_decimal *to) const 663 { 664 return Datetime(item).to_decimal(to); 665 } val_str_ascii(Item_handled_func * item,String * to)666 String *val_str_ascii(Item_handled_func *item, String *to) const 667 { 668 return Datetime(item).to_string(to, item->decimals); 669 } 670 }; 671 672 673 class Handler_int: public Handler 674 { 675 public: val_str(Item_handled_func * item,String * to)676 String *val_str(Item_handled_func *item, String *to) const 677 { 678 longlong nr= val_int(item); 679 if (item->null_value) 680 return 0; 681 to->set_int(nr, item->unsigned_flag, item->collation.collation); 682 return to; 683 } val_str_ascii(Item_handled_func * item,String * to)684 String *val_str_ascii(Item_handled_func *item, String *to) const 685 { 686 return item->Item::val_str_ascii(to); 687 } val_real(Item_handled_func * item)688 double val_real(Item_handled_func *item) const 689 { 690 return item->unsigned_flag ? (double) ((ulonglong) val_int(item)) : 691 (double) val_int(item); 692 } val_decimal(Item_handled_func * item,my_decimal * to)693 my_decimal *val_decimal(Item_handled_func *item, my_decimal *to) const 694 { 695 return item->val_decimal_from_int(to); 696 } get_date(THD * thd,Item_handled_func * item,MYSQL_TIME * to,date_mode_t fuzzydate)697 bool get_date(THD *thd, Item_handled_func *item, 698 MYSQL_TIME *to, date_mode_t fuzzydate) const 699 { 700 return item->get_date_from_int(thd, to, fuzzydate); 701 } val_int(Item_handled_func * item)702 longlong val_int(Item_handled_func *item) const 703 { 704 Longlong_null tmp= to_longlong_null(item); 705 item->null_value= tmp.is_null(); 706 return tmp.value(); 707 } 708 virtual Longlong_null to_longlong_null(Item_handled_func *item) const= 0; 709 }; 710 711 class Handler_slong: public Handler_int 712 { 713 public: return_type_handler(const Item_handled_func * item)714 const Type_handler *return_type_handler(const Item_handled_func *item) const 715 { 716 return &type_handler_slong; 717 } fix_length_and_dec(Item_handled_func * item)718 bool fix_length_and_dec(Item_handled_func *item) const 719 { 720 item->unsigned_flag= false; 721 item->collation= DTCollation_numeric(); 722 item->fix_char_length(11); 723 return false; 724 } 725 }; 726 727 class Handler_slong2: public Handler_slong 728 { 729 public: fix_length_and_dec(Item_handled_func * func)730 bool fix_length_and_dec(Item_handled_func *func) const 731 { 732 bool rc= Handler_slong::fix_length_and_dec(func); 733 func->max_length= 2; 734 return rc; 735 } 736 }; 737 738 class Handler_ulonglong: public Handler_int 739 { 740 public: return_type_handler(const Item_handled_func * item)741 const Type_handler *return_type_handler(const Item_handled_func *item) const 742 { 743 return &type_handler_ulonglong; 744 } fix_length_and_dec(Item_handled_func * item)745 bool fix_length_and_dec(Item_handled_func *item) const 746 { 747 item->unsigned_flag= true; 748 item->collation= DTCollation_numeric(); 749 item->fix_char_length(21); 750 return false; 751 } 752 }; 753 754 protected: 755 const Handler *m_func_handler; 756 public: Item_handled_func(THD * thd,Item * a)757 Item_handled_func(THD *thd, Item *a) 758 :Item_func(thd, a), m_func_handler(NULL) { } Item_handled_func(THD * thd,Item * a,Item * b)759 Item_handled_func(THD *thd, Item *a, Item *b) 760 :Item_func(thd, a, b), m_func_handler(NULL) { } set_func_handler(const Handler * handler)761 void set_func_handler(const Handler *handler) 762 { 763 m_func_handler= handler; 764 } type_handler()765 const Type_handler *type_handler() const 766 { 767 return m_func_handler->return_type_handler(this); 768 } create_field_for_create_select(MEM_ROOT * root,TABLE * table)769 Field *create_field_for_create_select(MEM_ROOT *root, TABLE *table) 770 { 771 DBUG_ASSERT(fixed); 772 const Type_handler *h= m_func_handler->type_handler_for_create_select(this); 773 return h->make_and_init_table_field(root, &name, 774 Record_addr(maybe_null), 775 *this, table); 776 } val_str(String * to)777 String *val_str(String *to) 778 { 779 return m_func_handler->val_str(this, to); 780 } val_str_ascii(String * to)781 String *val_str_ascii(String *to) 782 { 783 return m_func_handler->val_str_ascii(this, to); 784 } val_real()785 double val_real() 786 { 787 return m_func_handler->val_real(this); 788 } val_int()789 longlong val_int() 790 { 791 return m_func_handler->val_int(this); 792 } val_decimal(my_decimal * to)793 my_decimal *val_decimal(my_decimal *to) 794 { 795 return m_func_handler->val_decimal(this, to); 796 } get_date(THD * thd,MYSQL_TIME * to,date_mode_t fuzzydate)797 bool get_date(THD *thd, MYSQL_TIME *to, date_mode_t fuzzydate) 798 { 799 return m_func_handler->get_date(thd, this, to, fuzzydate); 800 } val_native(THD * thd,Native * to)801 bool val_native(THD *thd, Native *to) 802 { 803 return m_func_handler->val_native(thd, this, to); 804 } 805 }; 806 807 808 /** 809 Functions that at fix_fields() time determine the returned field type, 810 trying to preserve the exact data type of the arguments. 811 812 The descendants have to implement "native" value methods, 813 i.e. str_op(), date_op(), int_op(), real_op(), decimal_op(). 814 fix_fields() chooses which of the above value methods will be 815 used during execution time, according to the returned field type. 816 817 For example, if fix_fields() determines that the returned value type 818 is MYSQL_TYPE_LONG, then: 819 - int_op() is chosen as the execution time native method. 820 - val_int() returns the result of int_op() as is. 821 - all other methods, i.e. val_real(), val_decimal(), val_str(), get_date(), 822 call int_op() first, then convert the result to the requested data type. 823 */ 824 class Item_func_hybrid_field_type: public Item_hybrid_func 825 { 826 /* 827 Helper methods to make sure that the result of 828 decimal_op(), str_op() and date_op() is properly synched with null_value. 829 */ date_op_with_null_check(THD * thd,MYSQL_TIME * ltime)830 bool date_op_with_null_check(THD *thd, MYSQL_TIME *ltime) 831 { 832 bool rc= date_op(thd, ltime, date_mode_t(0)); 833 DBUG_ASSERT(!rc ^ null_value); 834 return rc; 835 } time_op_with_null_check(THD * thd,MYSQL_TIME * ltime)836 bool time_op_with_null_check(THD *thd, MYSQL_TIME *ltime) 837 { 838 bool rc= time_op(thd, ltime); 839 DBUG_ASSERT(!rc ^ null_value); 840 DBUG_ASSERT(rc || ltime->time_type == MYSQL_TIMESTAMP_TIME); 841 return rc; 842 } str_op_with_null_check(String * str)843 String *str_op_with_null_check(String *str) 844 { 845 String *res= str_op(str); 846 DBUG_ASSERT((res != NULL) ^ null_value); 847 return res; 848 } 849 850 public: 851 // Value methods that involve no conversion val_str_from_str_op(String * str)852 String *val_str_from_str_op(String *str) 853 { 854 return str_op_with_null_check(&str_value); 855 } val_int_from_int_op()856 longlong val_int_from_int_op() 857 { 858 return int_op(); 859 } val_real_from_real_op()860 double val_real_from_real_op() 861 { 862 return real_op(); 863 } 864 865 // Value methods that involve conversion 866 String *val_str_from_real_op(String *str); 867 String *val_str_from_int_op(String *str); 868 String *val_str_from_date_op(String *str); 869 String *val_str_from_time_op(String *str); 870 871 my_decimal *val_decimal_from_str_op(my_decimal *dec); 872 my_decimal *val_decimal_from_real_op(my_decimal *dec); 873 my_decimal *val_decimal_from_int_op(my_decimal *dec); 874 my_decimal *val_decimal_from_date_op(my_decimal *dec); 875 my_decimal *val_decimal_from_time_op(my_decimal *dec); 876 877 longlong val_int_from_str_op(); 878 longlong val_int_from_real_op(); 879 longlong val_int_from_date_op(); 880 longlong val_int_from_time_op(); 881 882 double val_real_from_str_op(); 883 double val_real_from_date_op(); 884 double val_real_from_time_op(); 885 double val_real_from_int_op(); 886 887 public: Item_func_hybrid_field_type(THD * thd)888 Item_func_hybrid_field_type(THD *thd): 889 Item_hybrid_func(thd) 890 { collation= DTCollation_numeric(); } Item_func_hybrid_field_type(THD * thd,Item * a)891 Item_func_hybrid_field_type(THD *thd, Item *a): 892 Item_hybrid_func(thd, a) 893 { collation= DTCollation_numeric(); } Item_func_hybrid_field_type(THD * thd,Item * a,Item * b)894 Item_func_hybrid_field_type(THD *thd, Item *a, Item *b): 895 Item_hybrid_func(thd, a, b) 896 { collation= DTCollation_numeric(); } Item_func_hybrid_field_type(THD * thd,Item * a,Item * b,Item * c)897 Item_func_hybrid_field_type(THD *thd, Item *a, Item *b, Item *c): 898 Item_hybrid_func(thd, a, b, c) 899 { collation= DTCollation_numeric(); } Item_func_hybrid_field_type(THD * thd,List<Item> & list)900 Item_func_hybrid_field_type(THD *thd, List<Item> &list): 901 Item_hybrid_func(thd, list) 902 { collation= DTCollation_numeric(); } 903 val_real()904 double val_real() 905 { 906 DBUG_ASSERT(fixed); 907 return Item_func_hybrid_field_type::type_handler()-> 908 Item_func_hybrid_field_type_val_real(this); 909 } val_int()910 longlong val_int() 911 { 912 DBUG_ASSERT(fixed); 913 return Item_func_hybrid_field_type::type_handler()-> 914 Item_func_hybrid_field_type_val_int(this); 915 } val_decimal(my_decimal * dec)916 my_decimal *val_decimal(my_decimal *dec) 917 { 918 DBUG_ASSERT(fixed); 919 return Item_func_hybrid_field_type::type_handler()-> 920 Item_func_hybrid_field_type_val_decimal(this, dec); 921 } val_str(String * str)922 String *val_str(String*str) 923 { 924 DBUG_ASSERT(fixed); 925 String *res= Item_func_hybrid_field_type::type_handler()-> 926 Item_func_hybrid_field_type_val_str(this, str); 927 DBUG_ASSERT(null_value == (res == NULL)); 928 return res; 929 } get_date(THD * thd,MYSQL_TIME * to,date_mode_t mode)930 bool get_date(THD *thd, MYSQL_TIME *to, date_mode_t mode) 931 { 932 DBUG_ASSERT(fixed); 933 return Item_func_hybrid_field_type::type_handler()-> 934 Item_func_hybrid_field_type_get_date_with_warn(thd, this, to, mode); 935 } 936 val_native(THD * thd,Native * to)937 bool val_native(THD *thd, Native *to) 938 { 939 DBUG_ASSERT(fixed); 940 return native_op(thd, to); 941 } 942 943 /** 944 @brief Performs the operation that this functions implements when the 945 result type is INT. 946 947 @return The result of the operation. 948 */ 949 virtual longlong int_op()= 0; to_longlong_null_op()950 Longlong_null to_longlong_null_op() 951 { 952 longlong nr= int_op(); 953 /* 954 C++ does not guarantee the order of parameter evaluation, 955 so to make sure "null_value" is passed to the constructor 956 after the int_op() call, int_op() is caled on a separate line. 957 */ 958 return Longlong_null(nr, null_value); 959 } to_longlong_hybrid_null_op()960 Longlong_hybrid_null to_longlong_hybrid_null_op() 961 { 962 return Longlong_hybrid_null(to_longlong_null_op(), unsigned_flag); 963 } 964 965 /** 966 @brief Performs the operation that this functions implements when the 967 result type is REAL. 968 969 @return The result of the operation. 970 */ 971 virtual double real_op()= 0; to_double_null_op()972 Double_null to_double_null_op() 973 { 974 // val_real() must be caleed on a separate line. See to_longlong_null() 975 double nr= real_op(); 976 return Double_null(nr, null_value); 977 } 978 979 /** 980 @brief Performs the operation that this functions implements when the 981 result type is DECIMAL. 982 983 @param A pointer where the DECIMAL value will be allocated. 984 @return 985 - 0 If the result is NULL 986 - The same pointer it was given, with the area initialized to the 987 result of the operation. 988 */ 989 virtual my_decimal *decimal_op(my_decimal *)= 0; 990 991 /** 992 @brief Performs the operation that this functions implements when the 993 result type is a string type. 994 995 @return The result of the operation. 996 */ 997 virtual String *str_op(String *)= 0; 998 999 /** 1000 @brief Performs the operation that this functions implements when 1001 field type is DATETIME or DATE. 1002 @return The result of the operation. 1003 */ 1004 virtual bool date_op(THD *thd, MYSQL_TIME *res, date_mode_t fuzzydate)= 0; 1005 1006 /** 1007 @brief Performs the operation that this functions implements when 1008 field type is TIME. 1009 @return The result of the operation. 1010 */ 1011 virtual bool time_op(THD *thd, MYSQL_TIME *res)= 0; 1012 1013 virtual bool native_op(THD *thd, Native *native)= 0; 1014 }; 1015 1016 1017 /* 1018 This class resembles SQL standard CASE-alike expressions: 1019 CASE and its abbreviations COALESCE, NULLIF, IFNULL, IF. 1020 1021 <case expression> ::= <case abbreviation> 1022 | <case specification> 1023 */ 1024 class Item_func_case_expression: public Item_func_hybrid_field_type 1025 { 1026 public: Item_func_case_expression(THD * thd)1027 Item_func_case_expression(THD *thd) 1028 :Item_func_hybrid_field_type(thd) 1029 { } Item_func_case_expression(THD * thd,Item * a)1030 Item_func_case_expression(THD *thd, Item *a) 1031 :Item_func_hybrid_field_type(thd, a) 1032 { } Item_func_case_expression(THD * thd,Item * a,Item * b)1033 Item_func_case_expression(THD *thd, Item *a, Item *b) 1034 :Item_func_hybrid_field_type(thd, a, b) 1035 { } Item_func_case_expression(THD * thd,Item * a,Item * b,Item * c)1036 Item_func_case_expression(THD *thd, Item *a, Item *b, Item *c) 1037 :Item_func_hybrid_field_type(thd, a, b, c) 1038 { } Item_func_case_expression(THD * thd,List<Item> & list)1039 Item_func_case_expression(THD *thd, List<Item> &list): 1040 Item_func_hybrid_field_type(thd, list) 1041 { } find_not_null_fields(table_map allowed)1042 bool find_not_null_fields(table_map allowed) { return false; } 1043 }; 1044 1045 1046 class Item_func_numhybrid: public Item_func_hybrid_field_type 1047 { 1048 protected: 1049 fix_decimals()1050 inline void fix_decimals() 1051 { 1052 DBUG_ASSERT(result_type() == DECIMAL_RESULT); 1053 if (decimals == NOT_FIXED_DEC) 1054 set_if_smaller(decimals, max_length - 1); 1055 } 1056 1057 public: Item_func_numhybrid(THD * thd)1058 Item_func_numhybrid(THD *thd): Item_func_hybrid_field_type(thd) 1059 { } Item_func_numhybrid(THD * thd,Item * a)1060 Item_func_numhybrid(THD *thd, Item *a): Item_func_hybrid_field_type(thd, a) 1061 { } Item_func_numhybrid(THD * thd,Item * a,Item * b)1062 Item_func_numhybrid(THD *thd, Item *a, Item *b): 1063 Item_func_hybrid_field_type(thd, a, b) 1064 { } Item_func_numhybrid(THD * thd,Item * a,Item * b,Item * c)1065 Item_func_numhybrid(THD *thd, Item *a, Item *b, Item *c): 1066 Item_func_hybrid_field_type(thd, a, b, c) 1067 { } Item_func_numhybrid(THD * thd,List<Item> & list)1068 Item_func_numhybrid(THD *thd, List<Item> &list): 1069 Item_func_hybrid_field_type(thd, list) 1070 { } str_op(String * str)1071 String *str_op(String *str) { DBUG_ASSERT(0); return 0; } date_op(THD * thd,MYSQL_TIME * ltime,date_mode_t fuzzydate)1072 bool date_op(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) 1073 { 1074 DBUG_ASSERT(0); 1075 return true; 1076 } time_op(THD * thd,MYSQL_TIME * ltime)1077 bool time_op(THD *thd, MYSQL_TIME *ltime) 1078 { 1079 DBUG_ASSERT(0); 1080 return true; 1081 } native_op(THD * thd,Native * to)1082 bool native_op(THD *thd, Native *to) 1083 { 1084 DBUG_ASSERT(0); 1085 return true; 1086 } 1087 }; 1088 1089 1090 /* function where type of result detected by first argument */ 1091 class Item_func_num1: public Item_func_numhybrid 1092 { 1093 public: Item_func_num1(THD * thd,Item * a)1094 Item_func_num1(THD *thd, Item *a): Item_func_numhybrid(thd, a) {} Item_func_num1(THD * thd,Item * a,Item * b)1095 Item_func_num1(THD *thd, Item *a, Item *b): Item_func_numhybrid(thd, a, b) {} check_partition_func_processor(void * int_arg)1096 bool check_partition_func_processor(void *int_arg) { return FALSE; } check_vcol_func_processor(void * arg)1097 bool check_vcol_func_processor(void *arg) { return FALSE; } 1098 }; 1099 1100 1101 /* Base class for operations like '+', '-', '*' */ 1102 class Item_num_op :public Item_func_numhybrid 1103 { 1104 protected: check_arguments()1105 bool check_arguments() const 1106 { 1107 return false; // Checked by aggregate_for_num_op() 1108 } 1109 public: Item_num_op(THD * thd,Item * a,Item * b)1110 Item_num_op(THD *thd, Item *a, Item *b): Item_func_numhybrid(thd, a, b) {} 1111 virtual void result_precision()= 0; 1112 print(String * str,enum_query_type query_type)1113 virtual inline void print(String *str, enum_query_type query_type) 1114 { 1115 print_op(str, query_type); 1116 } 1117 bool fix_type_handler(const Type_aggregator *aggregator); fix_length_and_dec_double()1118 void fix_length_and_dec_double() 1119 { 1120 aggregate_numeric_attributes_real(args, arg_count); 1121 max_length= float_length(decimals); 1122 } fix_length_and_dec_decimal()1123 void fix_length_and_dec_decimal() 1124 { 1125 unsigned_flag= args[0]->unsigned_flag & args[1]->unsigned_flag; 1126 result_precision(); 1127 fix_decimals(); 1128 } fix_length_and_dec_int()1129 void fix_length_and_dec_int() 1130 { 1131 unsigned_flag= args[0]->unsigned_flag | args[1]->unsigned_flag; 1132 result_precision(); 1133 decimals= 0; 1134 set_handler(type_handler_long_or_longlong()); 1135 } fix_length_and_dec_temporal(bool downcast_decimal_to_int)1136 void fix_length_and_dec_temporal(bool downcast_decimal_to_int) 1137 { 1138 set_handler(&type_handler_newdecimal); 1139 fix_length_and_dec_decimal(); 1140 if (decimals == 0 && downcast_decimal_to_int) 1141 set_handler(type_handler_long_or_longlong()); 1142 } need_parentheses_in_default()1143 bool need_parentheses_in_default() { return true; } 1144 }; 1145 1146 1147 class Item_int_func :public Item_func 1148 { 1149 public: 1150 /* 1151 QQ: shouldn't 20 characters be enough: 1152 Max unsigned = 18,446,744,073,709,551,615 = 20 digits, 20 characters 1153 Max signed = 9,223,372,036,854,775,807 = 19 digits, 19 characters 1154 Min signed = -9,223,372,036,854,775,808 = 19 digits, 20 characters 1155 */ Item_int_func(THD * thd)1156 Item_int_func(THD *thd): Item_func(thd) 1157 { collation= DTCollation_numeric(); fix_char_length(21); } Item_int_func(THD * thd,Item * a)1158 Item_int_func(THD *thd, Item *a): Item_func(thd, a) 1159 { collation= DTCollation_numeric(); fix_char_length(21); } Item_int_func(THD * thd,Item * a,Item * b)1160 Item_int_func(THD *thd, Item *a, Item *b): Item_func(thd, a, b) 1161 { collation= DTCollation_numeric(); fix_char_length(21); } Item_int_func(THD * thd,Item * a,Item * b,Item * c)1162 Item_int_func(THD *thd, Item *a, Item *b, Item *c): Item_func(thd, a, b, c) 1163 { collation= DTCollation_numeric(); fix_char_length(21); } Item_int_func(THD * thd,Item * a,Item * b,Item * c,Item * d)1164 Item_int_func(THD *thd, Item *a, Item *b, Item *c, Item *d): 1165 Item_func(thd, a, b, c, d) 1166 { collation= DTCollation_numeric(); fix_char_length(21); } Item_int_func(THD * thd,List<Item> & list)1167 Item_int_func(THD *thd, List<Item> &list): Item_func(thd, list) 1168 { collation= DTCollation_numeric(); fix_char_length(21); } Item_int_func(THD * thd,Item_int_func * item)1169 Item_int_func(THD *thd, Item_int_func *item) :Item_func(thd, item) 1170 { collation= DTCollation_numeric(); } 1171 double val_real(); 1172 String *val_str(String*str); val_decimal(my_decimal * decimal_value)1173 my_decimal *val_decimal(my_decimal *decimal_value) 1174 { 1175 return val_decimal_from_int(decimal_value); 1176 } get_date(THD * thd,MYSQL_TIME * ltime,date_mode_t fuzzydate)1177 bool get_date(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate) 1178 { return get_date_from_int(thd, ltime, fuzzydate); } 1179 const Type_handler *type_handler() const= 0; fix_length_and_dec()1180 bool fix_length_and_dec() { return FALSE; } 1181 }; 1182 1183 1184 class Item_long_func: public Item_int_func 1185 { 1186 public: Item_long_func(THD * thd)1187 Item_long_func(THD *thd): Item_int_func(thd) { } Item_long_func(THD * thd,Item * a)1188 Item_long_func(THD *thd, Item *a): Item_int_func(thd, a) {} Item_long_func(THD * thd,Item * a,Item * b)1189 Item_long_func(THD *thd, Item *a, Item *b): Item_int_func(thd, a, b) {} Item_long_func(THD * thd,Item * a,Item * b,Item * c)1190 Item_long_func(THD *thd, Item *a, Item *b, Item *c): Item_int_func(thd, a, b, c) {} Item_long_func(THD * thd,List<Item> & list)1191 Item_long_func(THD *thd, List<Item> &list): Item_int_func(thd, list) { } Item_long_func(THD * thd,Item_long_func * item)1192 Item_long_func(THD *thd, Item_long_func *item) :Item_int_func(thd, item) {} type_handler()1193 const Type_handler *type_handler() const 1194 { 1195 if (unsigned_flag) 1196 return &type_handler_ulong; 1197 return &type_handler_slong; 1198 } fix_length_and_dec()1199 bool fix_length_and_dec() { max_length= 11; return FALSE; } 1200 }; 1201 1202 1203 class Item_func_hash: public Item_int_func 1204 { 1205 public: Item_func_hash(THD * thd,List<Item> & item)1206 Item_func_hash(THD *thd, List<Item> &item): Item_int_func(thd, item) 1207 {} 1208 longlong val_int(); 1209 bool fix_length_and_dec(); type_handler()1210 const Type_handler *type_handler() const { return &type_handler_slong; } get_copy(THD * thd)1211 Item *get_copy(THD *thd) 1212 { return get_item_copy<Item_func_hash>(thd, this); } func_name()1213 const char *func_name() const { return "<hash>"; } 1214 }; 1215 1216 class Item_longlong_func: public Item_int_func 1217 { 1218 public: Item_longlong_func(THD * thd)1219 Item_longlong_func(THD *thd): Item_int_func(thd) { } Item_longlong_func(THD * thd,Item * a)1220 Item_longlong_func(THD *thd, Item *a): Item_int_func(thd, a) {} Item_longlong_func(THD * thd,Item * a,Item * b)1221 Item_longlong_func(THD *thd, Item *a, Item *b): Item_int_func(thd, a, b) {} Item_longlong_func(THD * thd,Item * a,Item * b,Item * c)1222 Item_longlong_func(THD *thd, Item *a, Item *b, Item *c): Item_int_func(thd, a, b, c) {} Item_longlong_func(THD * thd,Item * a,Item * b,Item * c,Item * d)1223 Item_longlong_func(THD *thd, Item *a, Item *b, Item *c, Item *d): 1224 Item_int_func(thd, a, b, c, d) {} Item_longlong_func(THD * thd,List<Item> & list)1225 Item_longlong_func(THD *thd, List<Item> &list): Item_int_func(thd, list) { } Item_longlong_func(THD * thd,Item_longlong_func * item)1226 Item_longlong_func(THD *thd, Item_longlong_func *item) :Item_int_func(thd, item) {} type_handler()1227 const Type_handler *type_handler() const 1228 { 1229 if (unsigned_flag) 1230 return &type_handler_ulonglong; 1231 return &type_handler_slonglong; 1232 } 1233 }; 1234 1235 1236 class Cursor_ref 1237 { 1238 protected: 1239 LEX_CSTRING m_cursor_name; 1240 uint m_cursor_offset; 1241 class sp_cursor *get_open_cursor_or_error(); Cursor_ref(const LEX_CSTRING * name,uint offset)1242 Cursor_ref(const LEX_CSTRING *name, uint offset) 1243 :m_cursor_name(*name), m_cursor_offset(offset) 1244 { } 1245 void print_func(String *str, const char *func_name); 1246 }; 1247 1248 1249 1250 class Item_func_cursor_rowcount: public Item_longlong_func, 1251 public Cursor_ref 1252 { 1253 public: Item_func_cursor_rowcount(THD * thd,const LEX_CSTRING * name,uint offset)1254 Item_func_cursor_rowcount(THD *thd, const LEX_CSTRING *name, uint offset) 1255 :Item_longlong_func(thd), Cursor_ref(name, offset) { maybe_null= true; } func_name()1256 const char *func_name() const { return "%ROWCOUNT"; } 1257 longlong val_int(); check_vcol_func_processor(void * arg)1258 bool check_vcol_func_processor(void *arg) 1259 { 1260 return mark_unsupported_function(func_name(), arg, VCOL_SESSION_FUNC); 1261 } print(String * str,enum_query_type query_type)1262 void print(String *str, enum_query_type query_type) 1263 { 1264 return Cursor_ref::print_func(str, func_name()); 1265 } get_copy(THD * thd)1266 Item *get_copy(THD *thd) 1267 { return get_item_copy<Item_func_cursor_rowcount>(thd, this); } 1268 }; 1269 1270 1271 1272 class Item_func_connection_id :public Item_long_func 1273 { 1274 longlong value; 1275 1276 public: Item_func_connection_id(THD * thd)1277 Item_func_connection_id(THD *thd): Item_long_func(thd) { unsigned_flag=1; } func_name()1278 const char *func_name() const { return "connection_id"; } 1279 bool fix_length_and_dec(); 1280 bool fix_fields(THD *thd, Item **ref); val_int()1281 longlong val_int() { DBUG_ASSERT(fixed == 1); return value; } check_vcol_func_processor(void * arg)1282 bool check_vcol_func_processor(void *arg) 1283 { 1284 return mark_unsupported_function(func_name(), "()", arg, VCOL_SESSION_FUNC); 1285 } get_copy(THD * thd)1286 Item *get_copy(THD *thd) 1287 { return get_item_copy<Item_func_connection_id>(thd, this); } 1288 }; 1289 1290 1291 class Item_func_signed :public Item_int_func 1292 { 1293 public: Item_func_signed(THD * thd,Item * a)1294 Item_func_signed(THD *thd, Item *a): Item_int_func(thd, a) 1295 { 1296 unsigned_flag= 0; 1297 } func_name()1298 const char *func_name() const { return "cast_as_signed"; } type_handler()1299 const Type_handler *type_handler() const 1300 { 1301 return Type_handler::type_handler_long_or_longlong(max_char_length(), 1302 false); 1303 } val_int()1304 longlong val_int() 1305 { 1306 longlong value= args[0]->val_int_signed_typecast(); 1307 null_value= args[0]->null_value; 1308 return value; 1309 } fix_length_and_dec_double()1310 void fix_length_and_dec_double() 1311 { 1312 fix_char_length(MAX_BIGINT_WIDTH); 1313 } fix_length_and_dec_generic()1314 void fix_length_and_dec_generic() 1315 { 1316 uint32 char_length= MY_MIN(args[0]->max_char_length(), 1317 MY_INT64_NUM_DECIMAL_DIGITS); 1318 /* 1319 args[0]->max_char_length() can return 0. 1320 Reserve max_length to fit at least one character for one digit, 1321 plus one character for the sign (if signed). 1322 */ 1323 set_if_bigger(char_length, 1U + (unsigned_flag ? 0 : 1)); 1324 fix_char_length(char_length); 1325 } fix_length_and_dec_string()1326 void fix_length_and_dec_string() 1327 { 1328 /* 1329 For strings, use decimal_int_part() instead of max_char_length(). 1330 This is important for Item_hex_hybrid: 1331 SELECT CAST(0x1FFFFFFFF AS SIGNED); 1332 Length is 5, decimal_int_part() is 13. 1333 */ 1334 uint32 char_length= MY_MIN(args[0]->decimal_int_part(), 1335 MY_INT64_NUM_DECIMAL_DIGITS); 1336 set_if_bigger(char_length, 1U + (unsigned_flag ? 0 : 1)); 1337 fix_char_length(char_length); 1338 } fix_length_and_dec()1339 bool fix_length_and_dec() 1340 { 1341 return args[0]->type_handler()->Item_func_signed_fix_length_and_dec(this); 1342 } 1343 virtual void print(String *str, enum_query_type query_type); decimal_precision()1344 uint decimal_precision() const { return args[0]->decimal_precision(); } need_parentheses_in_default()1345 bool need_parentheses_in_default() { return true; } get_copy(THD * thd)1346 Item *get_copy(THD *thd) 1347 { return get_item_copy<Item_func_signed>(thd, this); } 1348 }; 1349 1350 1351 class Item_func_unsigned :public Item_func_signed 1352 { 1353 public: Item_func_unsigned(THD * thd,Item * a)1354 Item_func_unsigned(THD *thd, Item *a): Item_func_signed(thd, a) 1355 { 1356 unsigned_flag= 1; 1357 } func_name()1358 const char *func_name() const { return "cast_as_unsigned"; } type_handler()1359 const Type_handler *type_handler() const 1360 { 1361 if (max_char_length() <= MY_INT32_NUM_DECIMAL_DIGITS - 1) 1362 return &type_handler_ulong; 1363 return &type_handler_ulonglong; 1364 } val_int()1365 longlong val_int() 1366 { 1367 longlong value= args[0]->val_int_unsigned_typecast(); 1368 null_value= args[0]->null_value; 1369 return value; 1370 } fix_length_and_dec()1371 bool fix_length_and_dec() 1372 { 1373 return args[0]->type_handler()->Item_func_unsigned_fix_length_and_dec(this); 1374 } decimal_precision()1375 uint decimal_precision() const { return max_length; } 1376 virtual void print(String *str, enum_query_type query_type); get_copy(THD * thd)1377 Item *get_copy(THD *thd) 1378 { return get_item_copy<Item_func_unsigned>(thd, this); } 1379 }; 1380 1381 1382 class Item_decimal_typecast :public Item_func 1383 { 1384 my_decimal decimal_value; 1385 public: Item_decimal_typecast(THD * thd,Item * a,uint len,uint dec)1386 Item_decimal_typecast(THD *thd, Item *a, uint len, uint dec) 1387 :Item_func(thd, a) 1388 { 1389 decimals= (uint8) dec; 1390 collation= DTCollation_numeric(); 1391 fix_char_length(my_decimal_precision_to_length_no_truncation(len, dec, 1392 unsigned_flag)); 1393 } val_str(String * str)1394 String *val_str(String *str) { return VDec(this).to_string(str); } val_real()1395 double val_real() { return VDec(this).to_double(); } val_int()1396 longlong val_int() { return VDec(this).to_longlong(unsigned_flag); } 1397 my_decimal *val_decimal(my_decimal*); get_date(THD * thd,MYSQL_TIME * to,date_mode_t mode)1398 bool get_date(THD *thd, MYSQL_TIME *to, date_mode_t mode) 1399 { 1400 return decimal_to_datetime_with_warn(thd, VDec(this).ptr(), to, mode, 1401 NULL, NULL); 1402 } type_handler()1403 const Type_handler *type_handler() const { return &type_handler_newdecimal; } fix_length_and_dec_generic()1404 void fix_length_and_dec_generic() {} fix_length_and_dec()1405 bool fix_length_and_dec() 1406 { 1407 return 1408 args[0]->type_handler()->Item_decimal_typecast_fix_length_and_dec(this); 1409 } func_name()1410 const char *func_name() const { return "decimal_typecast"; } 1411 virtual void print(String *str, enum_query_type query_type); need_parentheses_in_default()1412 bool need_parentheses_in_default() { return true; } get_copy(THD * thd)1413 Item *get_copy(THD *thd) 1414 { return get_item_copy<Item_decimal_typecast>(thd, this); } 1415 }; 1416 1417 1418 class Item_real_typecast: public Item_real_func 1419 { 1420 protected: 1421 double val_real_with_truncate(double max_value); 1422 public: Item_real_typecast(THD * thd,Item * a,uint len,uint dec)1423 Item_real_typecast(THD *thd, Item *a, uint len, uint dec) 1424 :Item_real_func(thd, a) 1425 { 1426 decimals= (uint8) dec; 1427 max_length= (uint32) len; 1428 } need_parentheses_in_default()1429 bool need_parentheses_in_default() { return true; } 1430 void print(String *str, enum_query_type query_type); fix_length_and_dec_generic()1431 void fix_length_and_dec_generic() { maybe_null= 1; } 1432 }; 1433 1434 1435 class Item_float_typecast :public Item_real_typecast 1436 { 1437 public: Item_float_typecast(THD * thd,Item * a)1438 Item_float_typecast(THD *thd, Item *a) 1439 :Item_real_typecast(thd, a, MAX_FLOAT_STR_LENGTH, NOT_FIXED_DEC) 1440 { } type_handler()1441 const Type_handler *type_handler() const { return &type_handler_float; } fix_length_and_dec()1442 bool fix_length_and_dec() 1443 { 1444 return 1445 args[0]->type_handler()->Item_float_typecast_fix_length_and_dec(this); 1446 } func_name()1447 const char *func_name() const { return "float_typecast"; } val_real()1448 double val_real() 1449 { 1450 return (double) (float) val_real_with_truncate(FLT_MAX); 1451 } val_str(String * str)1452 String *val_str(String*str) 1453 { 1454 Float nr(Item_float_typecast::val_real()); 1455 if (null_value) 1456 return 0; 1457 nr.to_string(str, decimals); 1458 return str; 1459 } get_copy(THD * thd)1460 Item *get_copy(THD *thd) 1461 { return get_item_copy<Item_float_typecast>(thd, this); } 1462 }; 1463 1464 1465 class Item_double_typecast :public Item_real_typecast 1466 { 1467 public: Item_double_typecast(THD * thd,Item * a,uint len,uint dec)1468 Item_double_typecast(THD *thd, Item *a, uint len, uint dec): 1469 Item_real_typecast(thd, a, len, dec) 1470 { } fix_length_and_dec()1471 bool fix_length_and_dec() 1472 { 1473 return 1474 args[0]->type_handler()->Item_double_typecast_fix_length_and_dec(this); 1475 } func_name()1476 const char *func_name() const { return "double_typecast"; } val_real()1477 double val_real() { return val_real_with_truncate(DBL_MAX); } get_copy(THD * thd)1478 Item *get_copy(THD *thd) 1479 { return get_item_copy<Item_double_typecast>(thd, this); } 1480 }; 1481 1482 1483 class Item_func_additive_op :public Item_num_op 1484 { 1485 public: Item_func_additive_op(THD * thd,Item * a,Item * b)1486 Item_func_additive_op(THD *thd, Item *a, Item *b): Item_num_op(thd, a, b) {} 1487 void result_precision(); check_partition_func_processor(void * int_arg)1488 bool check_partition_func_processor(void *int_arg) {return FALSE;} check_vcol_func_processor(void * arg)1489 bool check_vcol_func_processor(void *arg) { return FALSE;} 1490 }; 1491 1492 1493 class Item_func_plus :public Item_func_additive_op 1494 { 1495 public: Item_func_plus(THD * thd,Item * a,Item * b)1496 Item_func_plus(THD *thd, Item *a, Item *b): 1497 Item_func_additive_op(thd, a, b) {} func_name()1498 const char *func_name() const { return "+"; } precedence()1499 enum precedence precedence() const { return ADD_PRECEDENCE; } 1500 bool fix_length_and_dec(); 1501 longlong int_op(); 1502 double real_op(); 1503 my_decimal *decimal_op(my_decimal *); get_copy(THD * thd)1504 Item *get_copy(THD *thd) 1505 { return get_item_copy<Item_func_plus>(thd, this); } 1506 }; 1507 1508 class Item_func_minus :public Item_func_additive_op 1509 { 1510 bool m_depends_on_sql_mode_no_unsigned_subtraction; 1511 public: Item_func_minus(THD * thd,Item * a,Item * b)1512 Item_func_minus(THD *thd, Item *a, Item *b): 1513 Item_func_additive_op(thd, a, b), 1514 m_depends_on_sql_mode_no_unsigned_subtraction(false) 1515 { } func_name()1516 const char *func_name() const { return "-"; } precedence()1517 enum precedence precedence() const { return ADD_PRECEDENCE; } 1518 Sql_mode_dependency value_depends_on_sql_mode() const; 1519 longlong int_op(); 1520 double real_op(); 1521 my_decimal *decimal_op(my_decimal *); 1522 bool fix_length_and_dec(); 1523 void fix_unsigned_flag(); fix_length_and_dec_double()1524 void fix_length_and_dec_double() 1525 { 1526 Item_func_additive_op::fix_length_and_dec_double(); 1527 fix_unsigned_flag(); 1528 } fix_length_and_dec_decimal()1529 void fix_length_and_dec_decimal() 1530 { 1531 Item_func_additive_op::fix_length_and_dec_decimal(); 1532 fix_unsigned_flag(); 1533 } fix_length_and_dec_int()1534 void fix_length_and_dec_int() 1535 { 1536 Item_func_additive_op::fix_length_and_dec_int(); 1537 fix_unsigned_flag(); 1538 } get_copy(THD * thd)1539 Item *get_copy(THD *thd) 1540 { return get_item_copy<Item_func_minus>(thd, this); } 1541 }; 1542 1543 1544 class Item_func_mul :public Item_num_op 1545 { 1546 public: Item_func_mul(THD * thd,Item * a,Item * b)1547 Item_func_mul(THD *thd, Item *a, Item *b): 1548 Item_num_op(thd, a, b) {} func_name()1549 const char *func_name() const { return "*"; } precedence()1550 enum precedence precedence() const { return MUL_PRECEDENCE; } 1551 longlong int_op(); 1552 double real_op(); 1553 my_decimal *decimal_op(my_decimal *); 1554 void result_precision(); 1555 bool fix_length_and_dec(); check_partition_func_processor(void * int_arg)1556 bool check_partition_func_processor(void *int_arg) {return FALSE;} check_vcol_func_processor(void * arg)1557 bool check_vcol_func_processor(void *arg) { return FALSE;} get_copy(THD * thd)1558 Item *get_copy(THD *thd) 1559 { return get_item_copy<Item_func_mul>(thd, this); } 1560 }; 1561 1562 1563 class Item_func_div :public Item_num_op 1564 { 1565 public: 1566 uint prec_increment; Item_func_div(THD * thd,Item * a,Item * b)1567 Item_func_div(THD *thd, Item *a, Item *b): Item_num_op(thd, a, b) {} int_op()1568 longlong int_op() { DBUG_ASSERT(0); return 0; } 1569 double real_op(); 1570 my_decimal *decimal_op(my_decimal *); func_name()1571 const char *func_name() const { return "/"; } precedence()1572 enum precedence precedence() const { return MUL_PRECEDENCE; } 1573 bool fix_length_and_dec(); 1574 void fix_length_and_dec_double(); 1575 void fix_length_and_dec_int(); 1576 void result_precision(); get_copy(THD * thd)1577 Item *get_copy(THD *thd) 1578 { return get_item_copy<Item_func_div>(thd, this); } 1579 }; 1580 1581 1582 class Item_func_int_div :public Item_int_func 1583 { 1584 public: Item_func_int_div(THD * thd,Item * a,Item * b)1585 Item_func_int_div(THD *thd, Item *a, Item *b): Item_int_func(thd, a, b) 1586 {} 1587 longlong val_int(); func_name()1588 const char *func_name() const { return "DIV"; } precedence()1589 enum precedence precedence() const { return MUL_PRECEDENCE; } type_handler()1590 const Type_handler *type_handler() const 1591 { return type_handler_long_or_longlong(); } 1592 bool fix_length_and_dec(); print(String * str,enum_query_type query_type)1593 void print(String *str, enum_query_type query_type) 1594 { 1595 print_op(str, query_type); 1596 } 1597 check_partition_func_processor(void * int_arg)1598 bool check_partition_func_processor(void *int_arg) {return FALSE;} check_vcol_func_processor(void * arg)1599 bool check_vcol_func_processor(void *arg) { return FALSE;} need_parentheses_in_default()1600 bool need_parentheses_in_default() { return true; } get_copy(THD * thd)1601 Item *get_copy(THD *thd) 1602 { return get_item_copy<Item_func_int_div>(thd, this); } 1603 }; 1604 1605 1606 class Item_func_mod :public Item_num_op 1607 { 1608 public: Item_func_mod(THD * thd,Item * a,Item * b)1609 Item_func_mod(THD *thd, Item *a, Item *b): Item_num_op(thd, a, b) {} 1610 longlong int_op(); 1611 double real_op(); 1612 my_decimal *decimal_op(my_decimal *); func_name()1613 const char *func_name() const { return "MOD"; } precedence()1614 enum precedence precedence() const { return MUL_PRECEDENCE; } 1615 void result_precision(); 1616 bool fix_length_and_dec(); fix_length_and_dec_double()1617 void fix_length_and_dec_double() 1618 { 1619 Item_num_op::fix_length_and_dec_double(); 1620 unsigned_flag= args[0]->unsigned_flag; 1621 } fix_length_and_dec_decimal()1622 void fix_length_and_dec_decimal() 1623 { 1624 result_precision(); 1625 fix_decimals(); 1626 } fix_length_and_dec_int()1627 void fix_length_and_dec_int() 1628 { 1629 result_precision(); 1630 DBUG_ASSERT(decimals == 0); 1631 set_handler(type_handler_long_or_longlong()); 1632 } check_partition_func_processor(void * int_arg)1633 bool check_partition_func_processor(void *int_arg) {return FALSE;} check_vcol_func_processor(void * arg)1634 bool check_vcol_func_processor(void *arg) { return FALSE;} get_copy(THD * thd)1635 Item *get_copy(THD *thd) 1636 { return get_item_copy<Item_func_mod>(thd, this); } 1637 }; 1638 1639 1640 class Item_func_neg :public Item_func_num1 1641 { 1642 public: Item_func_neg(THD * thd,Item * a)1643 Item_func_neg(THD *thd, Item *a): Item_func_num1(thd, a) {} 1644 double real_op(); 1645 longlong int_op(); 1646 my_decimal *decimal_op(my_decimal *); func_name()1647 const char *func_name() const { return "-"; } functype()1648 enum Functype functype() const { return NEG_FUNC; } precedence()1649 enum precedence precedence() const { return NEG_PRECEDENCE; } print(String * str,enum_query_type query_type)1650 void print(String *str, enum_query_type query_type) 1651 { 1652 str->append(func_name()); 1653 args[0]->print_parenthesised(str, query_type, precedence()); 1654 } 1655 void fix_length_and_dec_int(); 1656 void fix_length_and_dec_double(); 1657 void fix_length_and_dec_decimal(); 1658 bool fix_length_and_dec(); decimal_precision()1659 uint decimal_precision() const { return args[0]->decimal_precision(); } need_parentheses_in_default()1660 bool need_parentheses_in_default() { return true; } get_copy(THD * thd)1661 Item *get_copy(THD *thd) 1662 { return get_item_copy<Item_func_neg>(thd, this); } 1663 }; 1664 1665 1666 class Item_func_abs :public Item_func_num1 1667 { 1668 public: Item_func_abs(THD * thd,Item * a)1669 Item_func_abs(THD *thd, Item *a): Item_func_num1(thd, a) {} 1670 double real_op(); 1671 longlong int_op(); 1672 my_decimal *decimal_op(my_decimal *); func_name()1673 const char *func_name() const { return "abs"; } 1674 void fix_length_and_dec_int(); 1675 void fix_length_and_dec_double(); 1676 void fix_length_and_dec_decimal(); 1677 bool fix_length_and_dec(); get_copy(THD * thd)1678 Item *get_copy(THD *thd) 1679 { return get_item_copy<Item_func_abs>(thd, this); } 1680 }; 1681 1682 // A class to handle logarithmic and trigonometric functions 1683 1684 class Item_dec_func :public Item_real_func 1685 { check_arguments()1686 bool check_arguments() const 1687 { return check_argument_types_can_return_real(0, arg_count); } 1688 public: Item_dec_func(THD * thd,Item * a)1689 Item_dec_func(THD *thd, Item *a): Item_real_func(thd, a) {} Item_dec_func(THD * thd,Item * a,Item * b)1690 Item_dec_func(THD *thd, Item *a, Item *b): Item_real_func(thd, a, b) {} fix_length_and_dec()1691 bool fix_length_and_dec() 1692 { 1693 decimals=NOT_FIXED_DEC; max_length=float_length(decimals); 1694 maybe_null=1; 1695 return FALSE; 1696 } 1697 }; 1698 1699 class Item_func_exp :public Item_dec_func 1700 { 1701 public: Item_func_exp(THD * thd,Item * a)1702 Item_func_exp(THD *thd, Item *a): Item_dec_func(thd, a) {} 1703 double val_real(); func_name()1704 const char *func_name() const { return "exp"; } get_copy(THD * thd)1705 Item *get_copy(THD *thd) 1706 { return get_item_copy<Item_func_exp>(thd, this); } 1707 }; 1708 1709 1710 class Item_func_ln :public Item_dec_func 1711 { 1712 public: Item_func_ln(THD * thd,Item * a)1713 Item_func_ln(THD *thd, Item *a): Item_dec_func(thd, a) {} 1714 double val_real(); func_name()1715 const char *func_name() const { return "ln"; } get_copy(THD * thd)1716 Item *get_copy(THD *thd) 1717 { return get_item_copy<Item_func_ln>(thd, this); } 1718 }; 1719 1720 1721 class Item_func_log :public Item_dec_func 1722 { 1723 public: Item_func_log(THD * thd,Item * a)1724 Item_func_log(THD *thd, Item *a): Item_dec_func(thd, a) {} Item_func_log(THD * thd,Item * a,Item * b)1725 Item_func_log(THD *thd, Item *a, Item *b): Item_dec_func(thd, a, b) {} 1726 double val_real(); func_name()1727 const char *func_name() const { return "log"; } get_copy(THD * thd)1728 Item *get_copy(THD *thd) 1729 { return get_item_copy<Item_func_log>(thd, this); } 1730 }; 1731 1732 1733 class Item_func_log2 :public Item_dec_func 1734 { 1735 public: Item_func_log2(THD * thd,Item * a)1736 Item_func_log2(THD *thd, Item *a): Item_dec_func(thd, a) {} 1737 double val_real(); func_name()1738 const char *func_name() const { return "log2"; } get_copy(THD * thd)1739 Item *get_copy(THD *thd) 1740 { return get_item_copy<Item_func_log2>(thd, this); } 1741 }; 1742 1743 1744 class Item_func_log10 :public Item_dec_func 1745 { 1746 public: Item_func_log10(THD * thd,Item * a)1747 Item_func_log10(THD *thd, Item *a): Item_dec_func(thd, a) {} 1748 double val_real(); func_name()1749 const char *func_name() const { return "log10"; } get_copy(THD * thd)1750 Item *get_copy(THD *thd) 1751 { return get_item_copy<Item_func_log10>(thd, this); } 1752 }; 1753 1754 1755 class Item_func_sqrt :public Item_dec_func 1756 { 1757 public: Item_func_sqrt(THD * thd,Item * a)1758 Item_func_sqrt(THD *thd, Item *a): Item_dec_func(thd, a) {} 1759 double val_real(); func_name()1760 const char *func_name() const { return "sqrt"; } get_copy(THD * thd)1761 Item *get_copy(THD *thd) 1762 { return get_item_copy<Item_func_sqrt>(thd, this); } 1763 }; 1764 1765 1766 class Item_func_pow :public Item_dec_func 1767 { 1768 public: Item_func_pow(THD * thd,Item * a,Item * b)1769 Item_func_pow(THD *thd, Item *a, Item *b): Item_dec_func(thd, a, b) {} 1770 double val_real(); func_name()1771 const char *func_name() const { return "pow"; } get_copy(THD * thd)1772 Item *get_copy(THD *thd) 1773 { return get_item_copy<Item_func_pow>(thd, this); } 1774 }; 1775 1776 1777 class Item_func_acos :public Item_dec_func 1778 { 1779 public: Item_func_acos(THD * thd,Item * a)1780 Item_func_acos(THD *thd, Item *a): Item_dec_func(thd, a) {} 1781 double val_real(); func_name()1782 const char *func_name() const { return "acos"; } get_copy(THD * thd)1783 Item *get_copy(THD *thd) 1784 { return get_item_copy<Item_func_acos>(thd, this); } 1785 }; 1786 1787 class Item_func_asin :public Item_dec_func 1788 { 1789 public: Item_func_asin(THD * thd,Item * a)1790 Item_func_asin(THD *thd, Item *a): Item_dec_func(thd, a) {} 1791 double val_real(); func_name()1792 const char *func_name() const { return "asin"; } get_copy(THD * thd)1793 Item *get_copy(THD *thd) 1794 { return get_item_copy<Item_func_asin>(thd, this); } 1795 }; 1796 1797 class Item_func_atan :public Item_dec_func 1798 { 1799 public: Item_func_atan(THD * thd,Item * a)1800 Item_func_atan(THD *thd, Item *a): Item_dec_func(thd, a) {} Item_func_atan(THD * thd,Item * a,Item * b)1801 Item_func_atan(THD *thd, Item *a, Item *b): Item_dec_func(thd, a, b) {} 1802 double val_real(); func_name()1803 const char *func_name() const { return "atan"; } get_copy(THD * thd)1804 Item *get_copy(THD *thd) 1805 { return get_item_copy<Item_func_atan>(thd, this); } 1806 }; 1807 1808 class Item_func_cos :public Item_dec_func 1809 { 1810 public: Item_func_cos(THD * thd,Item * a)1811 Item_func_cos(THD *thd, Item *a): Item_dec_func(thd, a) {} 1812 double val_real(); func_name()1813 const char *func_name() const { return "cos"; } get_copy(THD * thd)1814 Item *get_copy(THD *thd) 1815 { return get_item_copy<Item_func_cos>(thd, this); } 1816 }; 1817 1818 class Item_func_sin :public Item_dec_func 1819 { 1820 public: Item_func_sin(THD * thd,Item * a)1821 Item_func_sin(THD *thd, Item *a): Item_dec_func(thd, a) {} 1822 double val_real(); func_name()1823 const char *func_name() const { return "sin"; } get_copy(THD * thd)1824 Item *get_copy(THD *thd) 1825 { return get_item_copy<Item_func_sin>(thd, this); } 1826 }; 1827 1828 class Item_func_tan :public Item_dec_func 1829 { 1830 public: Item_func_tan(THD * thd,Item * a)1831 Item_func_tan(THD *thd, Item *a): Item_dec_func(thd, a) {} 1832 double val_real(); func_name()1833 const char *func_name() const { return "tan"; } get_copy(THD * thd)1834 Item *get_copy(THD *thd) 1835 { return get_item_copy<Item_func_tan>(thd, this); } 1836 }; 1837 1838 class Item_func_cot :public Item_dec_func 1839 { 1840 public: Item_func_cot(THD * thd,Item * a)1841 Item_func_cot(THD *thd, Item *a): Item_dec_func(thd, a) {} 1842 double val_real(); func_name()1843 const char *func_name() const { return "cot"; } get_copy(THD * thd)1844 Item *get_copy(THD *thd) 1845 { return get_item_copy<Item_func_cot>(thd, this); } 1846 }; 1847 1848 1849 class Item_func_int_val :public Item_func_hybrid_field_type 1850 { 1851 public: Item_func_int_val(THD * thd,Item * a)1852 Item_func_int_val(THD *thd, Item *a): Item_func_hybrid_field_type(thd, a) {} check_partition_func_processor(void * int_arg)1853 bool check_partition_func_processor(void *int_arg) { return FALSE; } check_vcol_func_processor(void * arg)1854 bool check_vcol_func_processor(void *arg) { return FALSE; } 1855 virtual decimal_round_mode round_mode() const= 0; 1856 void fix_length_and_dec_double(); 1857 void fix_length_and_dec_int_or_decimal(); fix_length_and_dec_time()1858 void fix_length_and_dec_time() 1859 { 1860 fix_attributes_time(0); 1861 set_handler(&type_handler_time2); 1862 } fix_length_and_dec_datetime()1863 void fix_length_and_dec_datetime() 1864 { 1865 fix_attributes_datetime(0); 1866 set_handler(&type_handler_datetime2); 1867 maybe_null= true; // E.g. CEILING(TIMESTAMP'0000-01-01 23:59:59.9') 1868 } 1869 bool fix_length_and_dec(); str_op(String * str)1870 String *str_op(String *str) { DBUG_ASSERT(0); return 0; } native_op(THD * thd,Native * to)1871 bool native_op(THD *thd, Native *to) 1872 { 1873 DBUG_ASSERT(0); 1874 return true; 1875 } 1876 }; 1877 1878 1879 class Item_func_ceiling :public Item_func_int_val 1880 { 1881 public: Item_func_ceiling(THD * thd,Item * a)1882 Item_func_ceiling(THD *thd, Item *a): Item_func_int_val(thd, a) {} func_name()1883 const char *func_name() const { return "ceiling"; } round_mode()1884 decimal_round_mode round_mode() const { return CEILING; } 1885 longlong int_op(); 1886 double real_op(); 1887 my_decimal *decimal_op(my_decimal *); 1888 bool date_op(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate); 1889 bool time_op(THD *thd, MYSQL_TIME *ltime); get_copy(THD * thd)1890 Item *get_copy(THD *thd) 1891 { return get_item_copy<Item_func_ceiling>(thd, this); } 1892 }; 1893 1894 1895 class Item_func_floor :public Item_func_int_val 1896 { 1897 public: Item_func_floor(THD * thd,Item * a)1898 Item_func_floor(THD *thd, Item *a): Item_func_int_val(thd, a) {} func_name()1899 const char *func_name() const { return "floor"; } round_mode()1900 decimal_round_mode round_mode() const { return FLOOR; } 1901 longlong int_op(); 1902 double real_op(); 1903 my_decimal *decimal_op(my_decimal *); 1904 bool date_op(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate); 1905 bool time_op(THD *thd, MYSQL_TIME *ltime); get_copy(THD * thd)1906 Item *get_copy(THD *thd) 1907 { return get_item_copy<Item_func_floor>(thd, this); } 1908 }; 1909 1910 /* This handles round and truncate */ 1911 1912 class Item_func_round :public Item_func_hybrid_field_type 1913 { 1914 bool truncate; 1915 void fix_length_and_dec_decimal(uint decimals_to_set); 1916 void fix_length_and_dec_double(uint decimals_to_set); 1917 bool test_if_length_can_increase(); 1918 public: Item_func_round(THD * thd,Item * a,Item * b,bool trunc_arg)1919 Item_func_round(THD *thd, Item *a, Item *b, bool trunc_arg) 1920 :Item_func_hybrid_field_type(thd, a, b), truncate(trunc_arg) {} func_name()1921 const char *func_name() const { return truncate ? "truncate" : "round"; } 1922 double real_op(); 1923 longlong int_op(); 1924 my_decimal *decimal_op(my_decimal *); 1925 bool date_op(THD *thd, MYSQL_TIME *ltime, date_mode_t fuzzydate); 1926 bool time_op(THD *thd, MYSQL_TIME *ltime); native_op(THD * thd,Native * to)1927 bool native_op(THD *thd, Native *to) 1928 { 1929 DBUG_ASSERT(0); 1930 return true; 1931 } str_op(String * str)1932 String *str_op(String *str) 1933 { 1934 DBUG_ASSERT(0); 1935 return NULL; 1936 } 1937 void fix_arg_decimal(); 1938 void fix_arg_int(const Type_handler *preferred, 1939 const Type_std_attributes *preferred_attributes, 1940 bool use_decimal_on_length_increase); 1941 void fix_arg_hex_hybrid(); 1942 void fix_arg_double(); 1943 void fix_arg_time(); 1944 void fix_arg_datetime(); 1945 void fix_arg_temporal(const Type_handler *h, uint int_part_length); fix_length_and_dec()1946 bool fix_length_and_dec() 1947 { 1948 /* 1949 We don't want to translate ENUM/SET to CHAR here. 1950 So let's real_type_handler(), not type_handler(). 1951 */ 1952 return args[0]->real_type_handler()->Item_func_round_fix_length_and_dec(this); 1953 } get_copy(THD * thd)1954 Item *get_copy(THD *thd) 1955 { return get_item_copy<Item_func_round>(thd, this); } 1956 }; 1957 1958 1959 class Item_func_rand :public Item_real_func 1960 { 1961 struct my_rnd_struct *rand; 1962 bool first_eval; // TRUE if val_real() is called 1st time check_arguments()1963 bool check_arguments() const 1964 { return check_argument_types_can_return_int(0, arg_count); } 1965 void seed_random (Item * val); 1966 public: Item_func_rand(THD * thd,Item * a)1967 Item_func_rand(THD *thd, Item *a): 1968 Item_real_func(thd, a), rand(0), first_eval(TRUE) {} Item_func_rand(THD * thd)1969 Item_func_rand(THD *thd): Item_real_func(thd) {} 1970 double val_real(); func_name()1971 const char *func_name() const { return "rand"; } const_item()1972 bool const_item() const { return 0; } 1973 void update_used_tables(); 1974 bool fix_fields(THD *thd, Item **ref); cleanup()1975 void cleanup() { first_eval= TRUE; Item_real_func::cleanup(); } check_vcol_func_processor(void * arg)1976 bool check_vcol_func_processor(void *arg) 1977 { 1978 return mark_unsupported_function(func_name(), "()", arg, 1979 VCOL_NON_DETERMINISTIC); 1980 } get_copy(THD * thd)1981 Item *get_copy(THD *thd) 1982 { return get_item_copy<Item_func_rand>(thd, this); } 1983 }; 1984 1985 1986 class Item_func_sign :public Item_long_func 1987 { check_arguments()1988 bool check_arguments() const 1989 { return args[0]->check_type_can_return_real(func_name()); } 1990 public: Item_func_sign(THD * thd,Item * a)1991 Item_func_sign(THD *thd, Item *a): Item_long_func(thd, a) {} func_name()1992 const char *func_name() const { return "sign"; } decimal_precision()1993 uint decimal_precision() const { return 1; } fix_length_and_dec()1994 bool fix_length_and_dec() { fix_char_length(2); return FALSE; } 1995 longlong val_int(); get_copy(THD * thd)1996 Item *get_copy(THD *thd) 1997 { return get_item_copy<Item_func_sign>(thd, this); } 1998 }; 1999 2000 2001 class Item_func_units :public Item_real_func 2002 { 2003 char *name; 2004 double mul,add; check_arguments()2005 bool check_arguments() const 2006 { return check_argument_types_can_return_real(0, arg_count); } 2007 public: Item_func_units(THD * thd,char * name_arg,Item * a,double mul_arg,double add_arg)2008 Item_func_units(THD *thd, char *name_arg, Item *a, double mul_arg, 2009 double add_arg): 2010 Item_real_func(thd, a), name(name_arg), mul(mul_arg), add(add_arg) {} 2011 double val_real(); func_name()2012 const char *func_name() const { return name; } fix_length_and_dec()2013 bool fix_length_and_dec() 2014 { 2015 decimals= NOT_FIXED_DEC; 2016 max_length= float_length(decimals); 2017 return FALSE; 2018 } get_copy(THD * thd)2019 Item *get_copy(THD *thd) 2020 { return get_item_copy<Item_func_units>(thd, this); } 2021 }; 2022 2023 2024 /** 2025 Item_func_min_max does not derive from Item_func_hybrid_field_type 2026 because the way how its methods val_xxx() and get_date() work depend 2027 not only by its arguments, but also on the context in which 2028 LEAST() and GREATEST() appear. 2029 For example, using Item_func_min_max in a CAST like this: 2030 CAST(LEAST('11','2') AS SIGNED) 2031 forces Item_func_min_max to compare the arguments as numbers rather 2032 than strings. 2033 Perhaps this should be changed eventually (see MDEV-5893). 2034 */ 2035 class Item_func_min_max :public Item_hybrid_func 2036 { 2037 String tmp_value; 2038 int cmp_sign; 2039 protected: check_arguments()2040 bool check_arguments() const 2041 { 2042 return false; // Checked by aggregate_for_min_max() 2043 } 2044 bool fix_attributes(Item **item, uint nitems); 2045 public: Item_func_min_max(THD * thd,List<Item> & list,int cmp_sign_arg)2046 Item_func_min_max(THD *thd, List<Item> &list, int cmp_sign_arg): 2047 Item_hybrid_func(thd, list), cmp_sign(cmp_sign_arg) 2048 {} 2049 String *val_str_native(String *str); 2050 double val_real_native(); 2051 longlong val_int_native(); 2052 my_decimal *val_decimal_native(my_decimal *); 2053 bool get_date_native(THD *thd, MYSQL_TIME *res, date_mode_t fuzzydate); 2054 bool get_time_native(THD *thd, MYSQL_TIME *res); 2055 val_real()2056 double val_real() 2057 { 2058 DBUG_ASSERT(fixed); 2059 return Item_func_min_max::type_handler()-> 2060 Item_func_min_max_val_real(this); 2061 } val_int()2062 longlong val_int() 2063 { 2064 DBUG_ASSERT(fixed); 2065 return Item_func_min_max::type_handler()-> 2066 Item_func_min_max_val_int(this); 2067 } val_str(String * str)2068 String *val_str(String *str) 2069 { 2070 DBUG_ASSERT(fixed); 2071 return Item_func_min_max::type_handler()-> 2072 Item_func_min_max_val_str(this, str); 2073 } val_decimal(my_decimal * dec)2074 my_decimal *val_decimal(my_decimal *dec) 2075 { 2076 DBUG_ASSERT(fixed); 2077 return Item_func_min_max::type_handler()-> 2078 Item_func_min_max_val_decimal(this, dec); 2079 } get_date(THD * thd,MYSQL_TIME * res,date_mode_t fuzzydate)2080 bool get_date(THD *thd, MYSQL_TIME *res, date_mode_t fuzzydate) 2081 { 2082 DBUG_ASSERT(fixed); 2083 return Item_func_min_max::type_handler()-> 2084 Item_func_min_max_get_date(thd, this, res, fuzzydate); 2085 } 2086 bool val_native(THD *thd, Native *to); aggregate_attributes_real(Item ** items,uint nitems)2087 void aggregate_attributes_real(Item **items, uint nitems) 2088 { 2089 /* 2090 Aggregating attributes for the double data type for LEAST/GREATEST 2091 is almost the same with aggregating for CASE-alike hybrid functions, 2092 (CASE..THEN, COALESCE, IF, etc). 2093 There is one notable difference though, when a numeric argument is mixed 2094 with a string argument: 2095 - CASE-alike functions return a string data type in such cases 2096 COALESCE(10,'x') -> VARCHAR(2) = '10' 2097 - LEAST/GREATEST returns double: 2098 GREATEST(10,'10e4') -> DOUBLE = 100000 2099 As the string argument can represent a number in the scientific notation, 2100 like in the example above, max_length of the result can be longer than 2101 max_length of the arguments. To handle this properly, max_length is 2102 additionally assigned to the result of float_length(decimals). 2103 */ 2104 Item_func::aggregate_attributes_real(items, nitems); 2105 max_length= float_length(decimals); 2106 } fix_length_and_dec()2107 bool fix_length_and_dec() 2108 { 2109 if (aggregate_for_min_max(func_name(), args, arg_count)) 2110 return true; 2111 fix_attributes(args, arg_count); 2112 return false; 2113 } 2114 }; 2115 2116 class Item_func_min :public Item_func_min_max 2117 { 2118 public: Item_func_min(THD * thd,List<Item> & list)2119 Item_func_min(THD *thd, List<Item> &list): Item_func_min_max(thd, list, 1) {} func_name()2120 const char *func_name() const { return "least"; } get_copy(THD * thd)2121 Item *get_copy(THD *thd) 2122 { return get_item_copy<Item_func_min>(thd, this); } 2123 }; 2124 2125 class Item_func_max :public Item_func_min_max 2126 { 2127 public: Item_func_max(THD * thd,List<Item> & list)2128 Item_func_max(THD *thd, List<Item> &list): Item_func_min_max(thd, list, -1) {} func_name()2129 const char *func_name() const { return "greatest"; } get_copy(THD * thd)2130 Item *get_copy(THD *thd) 2131 { return get_item_copy<Item_func_max>(thd, this); } 2132 }; 2133 2134 2135 /* 2136 Objects of this class are used for ROLLUP queries to wrap up 2137 each constant item referred to in GROUP BY list. 2138 */ 2139 2140 class Item_func_rollup_const :public Item_func 2141 { 2142 public: Item_func_rollup_const(THD * thd,Item * a)2143 Item_func_rollup_const(THD *thd, Item *a): Item_func(thd, a) 2144 { 2145 name= a->name; 2146 } val_real()2147 double val_real() { return val_real_from_item(args[0]); } val_int()2148 longlong val_int() { return val_int_from_item(args[0]); } val_str(String * str)2149 String *val_str(String *str) { return val_str_from_item(args[0], str); } val_native(THD * thd,Native * to)2150 bool val_native(THD *thd, Native *to) 2151 { return val_native_from_item(thd, args[0], to); } val_decimal(my_decimal * dec)2152 my_decimal *val_decimal(my_decimal *dec) 2153 { return val_decimal_from_item(args[0], dec); } get_date(THD * thd,MYSQL_TIME * ltime,date_mode_t fuzzydate)2154