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