1 | /* Copyright (c) 2000, 2015, Oracle and/or its affiliates. |
2 | Copyright (c) 2009, 2017, MariaDB |
3 | |
4 | This program is free software; you can redistribute it and/or modify |
5 | it under the terms of the GNU General Public License as published by |
6 | the Free Software Foundation; version 2 of the License. |
7 | |
8 | This program is distributed in the hope that it will be useful, |
9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | GNU General Public License for more details. |
12 | |
13 | You should have received a copy of the GNU General Public License |
14 | along with this program; if not, write to the Free Software |
15 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ |
16 | |
17 | /** |
18 | @file |
19 | |
20 | @brief |
21 | This file defines all numerical functions |
22 | */ |
23 | |
24 | #ifdef USE_PRAGMA_IMPLEMENTATION |
25 | #pragma implementation // gcc: Class implementation |
26 | #endif |
27 | |
28 | #include "sql_plugin.h" |
29 | #include "sql_priv.h" |
30 | /* |
31 | It is necessary to include set_var.h instead of item.h because there |
32 | are dependencies on include order for set_var.h and item.h. This |
33 | will be resolved later. |
34 | */ |
35 | #include "sql_class.h" // set_var.h: THD |
36 | #include "set_var.h" |
37 | #include "slave.h" // for wait_for_master_pos |
38 | #include "sql_show.h" // append_identifier |
39 | #include "strfunc.h" // find_type |
40 | #include "sql_parse.h" // is_update_query |
41 | #include "sql_acl.h" // EXECUTE_ACL |
42 | #include "mysqld.h" // LOCK_short_uuid_generator |
43 | #include "rpl_mi.h" |
44 | #include "sql_time.h" |
45 | #include <m_ctype.h> |
46 | #include <hash.h> |
47 | #include <time.h> |
48 | #include <ft_global.h> |
49 | #include <my_bit.h> |
50 | |
51 | #include "sp_head.h" |
52 | #include "sp_rcontext.h" |
53 | #include "sp.h" |
54 | #include "set_var.h" |
55 | #include "debug_sync.h" |
56 | #include "sql_base.h" |
57 | #include "sql_cte.h" |
58 | |
59 | #ifdef NO_EMBEDDED_ACCESS_CHECKS |
60 | #define sp_restore_security_context(A,B) while (0) {} |
61 | #endif |
62 | |
63 | bool check_reserved_words(const LEX_CSTRING *name) |
64 | { |
65 | if (lex_string_eq(name, STRING_WITH_LEN("GLOBAL" )) || |
66 | lex_string_eq(name, STRING_WITH_LEN("LOCAL" )) || |
67 | lex_string_eq(name, STRING_WITH_LEN("SESSION" ))) |
68 | return TRUE; |
69 | return FALSE; |
70 | } |
71 | |
72 | |
73 | /** |
74 | @return |
75 | TRUE if item is a constant |
76 | */ |
77 | |
78 | bool |
79 | eval_const_cond(COND *cond) |
80 | { |
81 | return ((Item_func*) cond)->val_int() ? TRUE : FALSE; |
82 | } |
83 | |
84 | |
85 | /** |
86 | Test if the sum of arguments overflows the ulonglong range. |
87 | */ |
88 | static inline bool test_if_sum_overflows_ull(ulonglong arg1, ulonglong arg2) |
89 | { |
90 | return ULONGLONG_MAX - arg1 < arg2; |
91 | } |
92 | |
93 | |
94 | /** |
95 | Allocate memory for arguments using tmp_args or thd->alloc(). |
96 | @retval false - success |
97 | @retval true - error (arg_count is set to 0 for conveniece) |
98 | */ |
99 | bool Item_args::alloc_arguments(THD *thd, uint count) |
100 | { |
101 | if (count <= 2) |
102 | { |
103 | args= tmp_arg; |
104 | return false; |
105 | } |
106 | if ((args= (Item**) thd->alloc(sizeof(Item*) * count)) == NULL) |
107 | { |
108 | arg_count= 0; |
109 | return true; |
110 | } |
111 | return false; |
112 | } |
113 | |
114 | |
115 | void Item_args::set_arguments(THD *thd, List<Item> &list) |
116 | { |
117 | if (alloc_arguments(thd, list.elements)) |
118 | return; |
119 | List_iterator_fast<Item> li(list); |
120 | Item *item; |
121 | for (arg_count= 0; (item= li++); ) |
122 | args[arg_count++]= item; |
123 | } |
124 | |
125 | |
126 | Item_args::Item_args(THD *thd, const Item_args *other) |
127 | :arg_count(other->arg_count) |
128 | { |
129 | if (arg_count <= 2) |
130 | { |
131 | args= tmp_arg; |
132 | } |
133 | else if (!(args= (Item**) thd->alloc(sizeof(Item*) * arg_count))) |
134 | { |
135 | arg_count= 0; |
136 | return; |
137 | } |
138 | memcpy(args, other->args, sizeof(Item*) * arg_count); |
139 | } |
140 | |
141 | |
142 | void Item_func::sync_with_sum_func_and_with_field(List<Item> &list) |
143 | { |
144 | List_iterator_fast<Item> li(list); |
145 | Item *item; |
146 | while ((item= li++)) |
147 | { |
148 | with_sum_func|= item->with_sum_func; |
149 | with_window_func|= item->with_window_func; |
150 | with_field|= item->with_field; |
151 | with_param|= item->with_param; |
152 | } |
153 | } |
154 | |
155 | |
156 | bool Item_func::check_argument_types_like_args0() const |
157 | { |
158 | if (arg_count < 2) |
159 | return false; |
160 | uint cols= args[0]->cols(); |
161 | bool is_scalar= args[0]->type_handler()->is_scalar_type(); |
162 | for (uint i= 1; i < arg_count; i++) |
163 | { |
164 | if (is_scalar != args[i]->type_handler()->is_scalar_type()) |
165 | { |
166 | my_error(ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION, MYF(0), |
167 | args[0]->type_handler()->name().ptr(), |
168 | args[i]->type_handler()->name().ptr(), func_name()); |
169 | return true; |
170 | } |
171 | if (args[i]->check_cols(cols)) |
172 | return true; |
173 | } |
174 | return false; |
175 | } |
176 | |
177 | |
178 | bool Item_func::check_argument_types_or_binary(const Type_handler *handler, |
179 | uint start, uint end) const |
180 | { |
181 | for (uint i= start; i < end ; i++) |
182 | { |
183 | DBUG_ASSERT(i < arg_count); |
184 | if (args[i]->check_type_or_binary(func_name(), handler)) |
185 | return true; |
186 | } |
187 | return false; |
188 | } |
189 | |
190 | |
191 | bool Item_func::check_argument_types_traditional_scalar(uint start, |
192 | uint end) const |
193 | { |
194 | for (uint i= start; i < end ; i++) |
195 | { |
196 | DBUG_ASSERT(i < arg_count); |
197 | if (args[i]->check_type_traditional_scalar(func_name())) |
198 | return true; |
199 | } |
200 | return false; |
201 | } |
202 | |
203 | |
204 | bool Item_func::check_argument_types_can_return_int(uint start, |
205 | uint end) const |
206 | { |
207 | for (uint i= start; i < end ; i++) |
208 | { |
209 | DBUG_ASSERT(i < arg_count); |
210 | if (args[i]->check_type_can_return_int(func_name())) |
211 | return true; |
212 | } |
213 | return false; |
214 | } |
215 | |
216 | |
217 | bool Item_func::check_argument_types_can_return_real(uint start, |
218 | uint end) const |
219 | { |
220 | for (uint i= start; i < end ; i++) |
221 | { |
222 | DBUG_ASSERT(i < arg_count); |
223 | if (args[i]->check_type_can_return_real(func_name())) |
224 | return true; |
225 | } |
226 | return false; |
227 | } |
228 | |
229 | |
230 | bool Item_func::check_argument_types_can_return_text(uint start, |
231 | uint end) const |
232 | { |
233 | for (uint i= start; i < end ; i++) |
234 | { |
235 | DBUG_ASSERT(i < arg_count); |
236 | if (args[i]->check_type_can_return_text(func_name())) |
237 | return true; |
238 | } |
239 | return false; |
240 | } |
241 | |
242 | |
243 | bool Item_func::check_argument_types_can_return_str(uint start, |
244 | uint end) const |
245 | { |
246 | for (uint i= start; i < end ; i++) |
247 | { |
248 | DBUG_ASSERT(i < arg_count); |
249 | if (args[i]->check_type_can_return_str(func_name())) |
250 | return true; |
251 | } |
252 | return false; |
253 | } |
254 | |
255 | |
256 | bool Item_func::check_argument_types_can_return_date(uint start, |
257 | uint end) const |
258 | { |
259 | for (uint i= start; i < end ; i++) |
260 | { |
261 | DBUG_ASSERT(i < arg_count); |
262 | if (args[i]->check_type_can_return_date(func_name())) |
263 | return true; |
264 | } |
265 | return false; |
266 | } |
267 | |
268 | |
269 | bool Item_func::check_argument_types_can_return_time(uint start, |
270 | uint end) const |
271 | { |
272 | for (uint i= start; i < end ; i++) |
273 | { |
274 | DBUG_ASSERT(i < arg_count); |
275 | if (args[i]->check_type_can_return_time(func_name())) |
276 | return true; |
277 | } |
278 | return false; |
279 | } |
280 | |
281 | |
282 | bool Item_func::check_argument_types_scalar(uint start, uint end) const |
283 | { |
284 | for (uint i= start; i < end; i++) |
285 | { |
286 | DBUG_ASSERT(i < arg_count); |
287 | if (args[i]->check_type_scalar(func_name())) |
288 | return true; |
289 | } |
290 | return false; |
291 | } |
292 | |
293 | |
294 | /* |
295 | Resolve references to table column for a function and its argument |
296 | |
297 | SYNOPSIS: |
298 | fix_fields() |
299 | thd Thread object |
300 | ref Pointer to where this object is used. This reference |
301 | is used if we want to replace this object with another |
302 | one (for example in the summary functions). |
303 | |
304 | DESCRIPTION |
305 | Call fix_fields() for all arguments to the function. The main intention |
306 | is to allow all Item_field() objects to setup pointers to the table fields. |
307 | |
308 | Sets as a side effect the following class variables: |
309 | maybe_null Set if any argument may return NULL |
310 | with_sum_func Set if any of the arguments contains a sum function |
311 | with_window_func Set if any of the arguments contain a window function |
312 | with_field Set if any of the arguments contains or is a field |
313 | used_tables_cache Set to union of the tables used by arguments |
314 | |
315 | str_value.charset If this is a string function, set this to the |
316 | character set for the first argument. |
317 | If any argument is binary, this is set to binary |
318 | |
319 | If for any item any of the defaults are wrong, then this can |
320 | be fixed in the fix_length_and_dec() function that is called |
321 | after this one or by writing a specialized fix_fields() for the |
322 | item. |
323 | |
324 | RETURN VALUES |
325 | FALSE ok |
326 | TRUE Got error. Stored with my_error(). |
327 | */ |
328 | |
329 | bool |
330 | Item_func::fix_fields(THD *thd, Item **ref) |
331 | { |
332 | DBUG_ASSERT(fixed == 0); |
333 | Item **arg,**arg_end; |
334 | uchar buff[STACK_BUFF_ALLOC]; // Max argument in function |
335 | |
336 | /* |
337 | The Used_tables_and_const_cache of "this" was initialized by |
338 | the constructor, or by Item_func::cleanup(). |
339 | */ |
340 | DBUG_ASSERT(used_tables_cache == 0); |
341 | DBUG_ASSERT(const_item_cache == true); |
342 | |
343 | not_null_tables_cache= 0; |
344 | |
345 | /* |
346 | Use stack limit of STACK_MIN_SIZE * 2 since |
347 | on some platforms a recursive call to fix_fields |
348 | requires more than STACK_MIN_SIZE bytes (e.g. for |
349 | MIPS, it takes about 22kB to make one recursive |
350 | call to Item_func::fix_fields()) |
351 | */ |
352 | if (check_stack_overrun(thd, STACK_MIN_SIZE * 2, buff)) |
353 | return TRUE; // Fatal error if flag is set! |
354 | if (arg_count) |
355 | { // Print purify happy |
356 | for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++) |
357 | { |
358 | Item *item; |
359 | /* |
360 | We can't yet set item to *arg as fix_fields may change *arg |
361 | We shouldn't call fix_fields() twice, so check 'fixed' field first |
362 | */ |
363 | if ((!(*arg)->fixed && (*arg)->fix_fields(thd, arg))) |
364 | return TRUE; /* purecov: inspected */ |
365 | item= *arg; |
366 | |
367 | if (item->maybe_null) |
368 | maybe_null=1; |
369 | |
370 | with_sum_func= with_sum_func || item->with_sum_func; |
371 | with_param= with_param || item->with_param; |
372 | with_window_func= with_window_func || item->with_window_func; |
373 | with_field= with_field || item->with_field; |
374 | used_tables_and_const_cache_join(item); |
375 | m_with_subquery|= item->with_subquery(); |
376 | } |
377 | } |
378 | if (check_arguments()) |
379 | return true; |
380 | fix_length_and_dec(); |
381 | if (unlikely(thd->is_error())) // An error inside fix_length_and_dec occurred |
382 | return TRUE; |
383 | fixed= 1; |
384 | return FALSE; |
385 | } |
386 | |
387 | void |
388 | Item_func::quick_fix_field() |
389 | { |
390 | Item **arg,**arg_end; |
391 | if (arg_count) |
392 | { |
393 | for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++) |
394 | { |
395 | if (!(*arg)->fixed) |
396 | (*arg)->quick_fix_field(); |
397 | } |
398 | } |
399 | fixed= 1; |
400 | } |
401 | |
402 | |
403 | bool |
404 | Item_func::eval_not_null_tables(void *opt_arg) |
405 | { |
406 | Item **arg,**arg_end; |
407 | not_null_tables_cache= 0; |
408 | if (arg_count) |
409 | { |
410 | for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++) |
411 | { |
412 | not_null_tables_cache|= (*arg)->not_null_tables(); |
413 | } |
414 | } |
415 | return FALSE; |
416 | } |
417 | |
418 | |
419 | void Item_func::fix_after_pullout(st_select_lex *new_parent, Item **ref, |
420 | bool merge) |
421 | { |
422 | Item **arg,**arg_end; |
423 | |
424 | used_tables_and_const_cache_init(); |
425 | not_null_tables_cache= 0; |
426 | |
427 | if (arg_count) |
428 | { |
429 | for (arg=args, arg_end=args+arg_count; arg != arg_end ; arg++) |
430 | { |
431 | (*arg)->fix_after_pullout(new_parent, arg, merge); |
432 | Item *item= *arg; |
433 | |
434 | used_tables_and_const_cache_join(item); |
435 | not_null_tables_cache|= item->not_null_tables(); |
436 | } |
437 | } |
438 | } |
439 | |
440 | |
441 | void Item_func::traverse_cond(Cond_traverser traverser, |
442 | void *argument, traverse_order order) |
443 | { |
444 | if (arg_count) |
445 | { |
446 | Item **arg,**arg_end; |
447 | |
448 | switch (order) { |
449 | case(PREFIX): |
450 | (*traverser)(this, argument); |
451 | for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++) |
452 | { |
453 | (*arg)->traverse_cond(traverser, argument, order); |
454 | } |
455 | break; |
456 | case (POSTFIX): |
457 | for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++) |
458 | { |
459 | (*arg)->traverse_cond(traverser, argument, order); |
460 | } |
461 | (*traverser)(this, argument); |
462 | } |
463 | } |
464 | else |
465 | (*traverser)(this, argument); |
466 | } |
467 | |
468 | |
469 | bool Item_args::transform_args(THD *thd, Item_transformer transformer, uchar *arg) |
470 | { |
471 | for (uint i= 0; i < arg_count; i++) |
472 | { |
473 | Item *new_item= args[i]->transform(thd, transformer, arg); |
474 | if (!new_item) |
475 | return true; |
476 | /* |
477 | THD::change_item_tree() should be called only if the tree was |
478 | really transformed, i.e. when a new item has been created. |
479 | Otherwise we'll be allocating a lot of unnecessary memory for |
480 | change records at each execution. |
481 | */ |
482 | if (args[i] != new_item) |
483 | thd->change_item_tree(&args[i], new_item); |
484 | } |
485 | return false; |
486 | } |
487 | |
488 | |
489 | /** |
490 | Transform an Item_func object with a transformer callback function. |
491 | |
492 | The function recursively applies the transform method to each |
493 | argument of the Item_func node. |
494 | If the call of the method for an argument item returns a new item |
495 | the old item is substituted for a new one. |
496 | After this the transformer is applied to the root node |
497 | of the Item_func object. |
498 | @param transformer the transformer callback function to be applied to |
499 | the nodes of the tree of the object |
500 | @param argument parameter to be passed to the transformer |
501 | |
502 | @return |
503 | Item returned as the result of transformation of the root node |
504 | */ |
505 | |
506 | Item *Item_func::transform(THD *thd, Item_transformer transformer, uchar *argument) |
507 | { |
508 | DBUG_ASSERT(!thd->stmt_arena->is_stmt_prepare()); |
509 | if (transform_args(thd, transformer, argument)) |
510 | return 0; |
511 | return (this->*transformer)(thd, argument); |
512 | } |
513 | |
514 | |
515 | /** |
516 | Compile Item_func object with a processor and a transformer |
517 | callback functions. |
518 | |
519 | First the function applies the analyzer to the root node of |
520 | the Item_func object. Then if the analizer succeeeds (returns TRUE) |
521 | the function recursively applies the compile method to each argument |
522 | of the Item_func node. |
523 | If the call of the method for an argument item returns a new item |
524 | the old item is substituted for a new one. |
525 | After this the transformer is applied to the root node |
526 | of the Item_func object. |
527 | The compile function is not called if the analyzer returns NULL |
528 | in the parameter arg_p. |
529 | |
530 | @param analyzer the analyzer callback function to be applied to the |
531 | nodes of the tree of the object |
532 | @param[in,out] arg_p parameter to be passed to the processor |
533 | @param transformer the transformer callback function to be applied to the |
534 | nodes of the tree of the object |
535 | @param arg_t parameter to be passed to the transformer |
536 | |
537 | @return |
538 | Item returned as the result of transformation of the root node |
539 | */ |
540 | |
541 | Item *Item_func::compile(THD *thd, Item_analyzer analyzer, uchar **arg_p, |
542 | Item_transformer transformer, uchar *arg_t) |
543 | { |
544 | if (!(this->*analyzer)(arg_p)) |
545 | return 0; |
546 | if (*arg_p && arg_count) |
547 | { |
548 | Item **arg,**arg_end; |
549 | for (arg= args, arg_end= args+arg_count; arg != arg_end; arg++) |
550 | { |
551 | /* |
552 | The same parameter value of arg_p must be passed |
553 | to analyze any argument of the condition formula. |
554 | */ |
555 | uchar *arg_v= *arg_p; |
556 | Item *new_item= (*arg)->compile(thd, analyzer, &arg_v, transformer, |
557 | arg_t); |
558 | if (new_item && *arg != new_item) |
559 | thd->change_item_tree(arg, new_item); |
560 | } |
561 | } |
562 | return (this->*transformer)(thd, arg_t); |
563 | } |
564 | |
565 | |
566 | void Item_args::propagate_equal_fields(THD *thd, |
567 | const Item::Context &ctx, |
568 | COND_EQUAL *cond) |
569 | { |
570 | uint i; |
571 | for (i= 0; i < arg_count; i++) |
572 | args[i]->propagate_equal_fields_and_change_item_tree(thd, ctx, cond, |
573 | &args[i]); |
574 | } |
575 | |
576 | |
577 | /** |
578 | See comments in Item_cond::split_sum_func() |
579 | */ |
580 | |
581 | void Item_func::split_sum_func(THD *thd, Ref_ptr_array ref_pointer_array, |
582 | List<Item> &fields, uint flags) |
583 | { |
584 | Item **arg, **arg_end; |
585 | for (arg= args, arg_end= args+arg_count; arg != arg_end ; arg++) |
586 | (*arg)->split_sum_func2(thd, ref_pointer_array, fields, arg, |
587 | flags | SPLIT_SUM_SKIP_REGISTERED); |
588 | } |
589 | |
590 | |
591 | table_map Item_func::not_null_tables() const |
592 | { |
593 | return not_null_tables_cache; |
594 | } |
595 | |
596 | |
597 | void Item_func::print(String *str, enum_query_type query_type) |
598 | { |
599 | str->append(func_name()); |
600 | str->append('('); |
601 | print_args(str, 0, query_type); |
602 | str->append(')'); |
603 | } |
604 | |
605 | |
606 | void Item_func::print_args(String *str, uint from, enum_query_type query_type) |
607 | { |
608 | for (uint i=from ; i < arg_count ; i++) |
609 | { |
610 | if (i != from) |
611 | str->append(','); |
612 | args[i]->print(str, query_type); |
613 | } |
614 | } |
615 | |
616 | |
617 | void Item_func::print_op(String *str, enum_query_type query_type) |
618 | { |
619 | for (uint i=0 ; i < arg_count-1 ; i++) |
620 | { |
621 | args[i]->print_parenthesised(str, query_type, precedence()); |
622 | str->append(' '); |
623 | str->append(func_name()); |
624 | str->append(' '); |
625 | } |
626 | args[arg_count-1]->print_parenthesised(str, query_type, |
627 | (enum precedence)(precedence() + 1)); |
628 | } |
629 | |
630 | |
631 | bool Item_func::eq(const Item *item, bool binary_cmp) const |
632 | { |
633 | /* Assume we don't have rtti */ |
634 | if (this == item) |
635 | return 1; |
636 | /* |
637 | Ensure that we are comparing two functions and that the function |
638 | is deterministic. |
639 | */ |
640 | if (item->type() != FUNC_ITEM || (used_tables() & RAND_TABLE_BIT)) |
641 | return 0; |
642 | Item_func *item_func=(Item_func*) item; |
643 | Item_func::Functype func_type; |
644 | if ((func_type= functype()) != item_func->functype() || |
645 | arg_count != item_func->arg_count || |
646 | (func_type != Item_func::FUNC_SP && |
647 | func_name() != item_func->func_name()) || |
648 | (func_type == Item_func::FUNC_SP && |
649 | my_strcasecmp(system_charset_info, func_name(), item_func->func_name()))) |
650 | return 0; |
651 | for (uint i=0; i < arg_count ; i++) |
652 | if (!args[i]->eq(item_func->args[i], binary_cmp)) |
653 | return 0; |
654 | return 1; |
655 | } |
656 | |
657 | |
658 | /* |
659 | bool Item_func::is_expensive_processor(uchar *arg) |
660 | { |
661 | return is_expensive(); |
662 | } |
663 | */ |
664 | |
665 | my_decimal *Item_func::val_decimal(my_decimal *decimal_value) |
666 | { |
667 | DBUG_ASSERT(fixed); |
668 | longlong nr= val_int(); |
669 | if (null_value) |
670 | return 0; /* purecov: inspected */ |
671 | int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value); |
672 | return decimal_value; |
673 | } |
674 | |
675 | |
676 | bool Item_hybrid_func::fix_attributes(Item **items, uint nitems) |
677 | { |
678 | bool rc= Item_hybrid_func::type_handler()-> |
679 | Item_hybrid_func_fix_attributes(current_thd, |
680 | func_name(), this, this, |
681 | items, nitems); |
682 | DBUG_ASSERT(!rc || current_thd->is_error()); |
683 | return rc; |
684 | } |
685 | |
686 | |
687 | String *Item_real_func::val_str(String *str) |
688 | { |
689 | DBUG_ASSERT(fixed == 1); |
690 | double nr= val_real(); |
691 | if (null_value) |
692 | return 0; /* purecov: inspected */ |
693 | str->set_real(nr, decimals, collation.collation); |
694 | return str; |
695 | } |
696 | |
697 | |
698 | my_decimal *Item_real_func::val_decimal(my_decimal *decimal_value) |
699 | { |
700 | DBUG_ASSERT(fixed); |
701 | double nr= val_real(); |
702 | if (null_value) |
703 | return 0; /* purecov: inspected */ |
704 | double2my_decimal(E_DEC_FATAL_ERROR, nr, decimal_value); |
705 | return decimal_value; |
706 | } |
707 | |
708 | |
709 | #ifdef HAVE_DLOPEN |
710 | void Item_udf_func::fix_num_length_and_dec() |
711 | { |
712 | uint fl_length= 0; |
713 | decimals=0; |
714 | for (uint i=0 ; i < arg_count ; i++) |
715 | { |
716 | set_if_bigger(decimals,args[i]->decimals); |
717 | set_if_bigger(fl_length, args[i]->max_length); |
718 | } |
719 | max_length=float_length(decimals); |
720 | if (fl_length > max_length) |
721 | { |
722 | decimals= NOT_FIXED_DEC; |
723 | max_length= float_length(NOT_FIXED_DEC); |
724 | } |
725 | } |
726 | #endif |
727 | |
728 | |
729 | void Item_func::signal_divide_by_null() |
730 | { |
731 | THD *thd= current_thd; |
732 | if (thd->variables.sql_mode & MODE_ERROR_FOR_DIVISION_BY_ZERO) |
733 | push_warning(thd, Sql_condition::WARN_LEVEL_WARN, ER_DIVISION_BY_ZERO, |
734 | ER_THD(thd, ER_DIVISION_BY_ZERO)); |
735 | null_value= 1; |
736 | } |
737 | |
738 | |
739 | Item *Item_func::get_tmp_table_item(THD *thd) |
740 | { |
741 | if (!with_sum_func && !const_item()) |
742 | return new (thd->mem_root) Item_temptable_field(thd, result_field); |
743 | return copy_or_same(thd); |
744 | } |
745 | |
746 | double Item_int_func::val_real() |
747 | { |
748 | DBUG_ASSERT(fixed == 1); |
749 | |
750 | return unsigned_flag ? (double) ((ulonglong) val_int()) : (double) val_int(); |
751 | } |
752 | |
753 | |
754 | String *Item_int_func::val_str(String *str) |
755 | { |
756 | DBUG_ASSERT(fixed == 1); |
757 | longlong nr=val_int(); |
758 | if (null_value) |
759 | return 0; |
760 | str->set_int(nr, unsigned_flag, collation.collation); |
761 | return str; |
762 | } |
763 | |
764 | |
765 | void Item_func_connection_id::fix_length_and_dec() |
766 | { |
767 | Item_long_func::fix_length_and_dec(); |
768 | max_length= 10; |
769 | } |
770 | |
771 | |
772 | bool Item_func_connection_id::fix_fields(THD *thd, Item **ref) |
773 | { |
774 | if (Item_int_func::fix_fields(thd, ref)) |
775 | return TRUE; |
776 | thd->thread_specific_used= TRUE; |
777 | value= thd->variables.pseudo_thread_id; |
778 | return FALSE; |
779 | } |
780 | |
781 | |
782 | bool Item_num_op::fix_type_handler(const Type_aggregator *aggregator) |
783 | { |
784 | DBUG_ASSERT(arg_count == 2); |
785 | const Type_handler *h0= args[0]->cast_to_int_type_handler(); |
786 | const Type_handler *h1= args[1]->cast_to_int_type_handler(); |
787 | if (!aggregate_for_num_op(aggregator, h0, h1)) |
788 | return false; |
789 | my_error(ER_ILLEGAL_PARAMETER_DATA_TYPES2_FOR_OPERATION, MYF(0), |
790 | h0->name().ptr(), h1->name().ptr(), func_name()); |
791 | return true; |
792 | } |
793 | |
794 | |
795 | void Item_func_plus::fix_length_and_dec(void) |
796 | { |
797 | DBUG_ENTER("Item_func_plus::fix_length_and_dec" ); |
798 | DBUG_PRINT("info" , ("name %s" , func_name())); |
799 | const Type_aggregator *aggregator= &type_handler_data->m_type_aggregator_for_plus; |
800 | DBUG_EXECUTE_IF("num_op" , aggregator= &type_handler_data->m_type_aggregator_for_result;); |
801 | DBUG_ASSERT(aggregator->is_commutative()); |
802 | if (!fix_type_handler(aggregator)) |
803 | { |
804 | Item_func_plus::type_handler()->Item_func_plus_fix_length_and_dec(this); |
805 | DBUG_PRINT("info" , ("Type: %s" , type_handler()->name().ptr())); |
806 | } |
807 | DBUG_VOID_RETURN; |
808 | } |
809 | |
810 | |
811 | String *Item_func_hybrid_field_type::val_str_from_decimal_op(String *str) |
812 | { |
813 | my_decimal decimal_value, *val; |
814 | if (!(val= decimal_op_with_null_check(&decimal_value))) |
815 | return 0; // null is set |
816 | DBUG_ASSERT(!null_value); |
817 | my_decimal_round(E_DEC_FATAL_ERROR, val, decimals, FALSE, val); |
818 | str->set_charset(collation.collation); |
819 | my_decimal2string(E_DEC_FATAL_ERROR, val, 0, 0, 0, str); |
820 | return str; |
821 | } |
822 | |
823 | double Item_func_hybrid_field_type::val_real_from_decimal_op() |
824 | { |
825 | my_decimal decimal_value, *val; |
826 | if (!(val= decimal_op_with_null_check(&decimal_value))) |
827 | return 0.0; // null is set |
828 | double result; |
829 | my_decimal2double(E_DEC_FATAL_ERROR, val, &result); |
830 | return result; |
831 | } |
832 | |
833 | longlong Item_func_hybrid_field_type::val_int_from_decimal_op() |
834 | { |
835 | my_decimal decimal_value, *val; |
836 | if (!(val= decimal_op_with_null_check(&decimal_value))) |
837 | return 0; // null is set |
838 | longlong result; |
839 | my_decimal2int(E_DEC_FATAL_ERROR, val, unsigned_flag, &result); |
840 | return result; |
841 | } |
842 | |
843 | bool Item_func_hybrid_field_type::get_date_from_decimal_op(MYSQL_TIME *ltime, |
844 | ulonglong fuzzydate) |
845 | { |
846 | my_decimal value, *res; |
847 | if (!(res= decimal_op_with_null_check(&value)) || |
848 | decimal_to_datetime_with_warn(res, ltime, fuzzydate, |
849 | field_name_or_null())) |
850 | return make_zero_mysql_time(ltime, fuzzydate); |
851 | return (null_value= 0); |
852 | } |
853 | |
854 | |
855 | String *Item_func_hybrid_field_type::val_str_from_int_op(String *str) |
856 | { |
857 | longlong nr= int_op(); |
858 | if (null_value) |
859 | return 0; /* purecov: inspected */ |
860 | str->set_int(nr, unsigned_flag, collation.collation); |
861 | return str; |
862 | } |
863 | |
864 | double Item_func_hybrid_field_type::val_real_from_int_op() |
865 | { |
866 | longlong result= int_op(); |
867 | return unsigned_flag ? (double) ((ulonglong) result) : (double) result; |
868 | } |
869 | |
870 | my_decimal * |
871 | Item_func_hybrid_field_type::val_decimal_from_int_op(my_decimal *dec) |
872 | { |
873 | longlong result= int_op(); |
874 | if (null_value) |
875 | return NULL; |
876 | int2my_decimal(E_DEC_FATAL_ERROR, result, unsigned_flag, dec); |
877 | return dec; |
878 | } |
879 | |
880 | bool Item_func_hybrid_field_type::get_date_from_int_op(MYSQL_TIME *ltime, |
881 | ulonglong fuzzydate) |
882 | { |
883 | longlong value= int_op(); |
884 | bool neg= !unsigned_flag && value < 0; |
885 | if (null_value || int_to_datetime_with_warn(neg, neg ? -value : value, |
886 | ltime, fuzzydate, |
887 | field_name_or_null())) |
888 | return make_zero_mysql_time(ltime, fuzzydate); |
889 | return (null_value= 0); |
890 | } |
891 | |
892 | |
893 | String *Item_func_hybrid_field_type::val_str_from_real_op(String *str) |
894 | { |
895 | double nr= real_op(); |
896 | if (null_value) |
897 | return 0; /* purecov: inspected */ |
898 | str->set_real(nr, decimals, collation.collation); |
899 | return str; |
900 | } |
901 | |
902 | longlong Item_func_hybrid_field_type::val_int_from_real_op() |
903 | { |
904 | return (longlong) rint(real_op()); |
905 | } |
906 | |
907 | my_decimal * |
908 | Item_func_hybrid_field_type::val_decimal_from_real_op(my_decimal *dec) |
909 | { |
910 | double result= (double) real_op(); |
911 | if (null_value) |
912 | return NULL; |
913 | double2my_decimal(E_DEC_FATAL_ERROR, result, dec); |
914 | return dec; |
915 | } |
916 | |
917 | bool Item_func_hybrid_field_type::get_date_from_real_op(MYSQL_TIME *ltime, |
918 | ulonglong fuzzydate) |
919 | { |
920 | double value= real_op(); |
921 | if (null_value || double_to_datetime_with_warn(value, ltime, fuzzydate, |
922 | field_name_or_null())) |
923 | return make_zero_mysql_time(ltime, fuzzydate); |
924 | return (null_value= 0); |
925 | } |
926 | |
927 | |
928 | String *Item_func_hybrid_field_type::val_str_from_date_op(String *str) |
929 | { |
930 | MYSQL_TIME ltime; |
931 | if (date_op_with_null_check(<ime) || |
932 | (null_value= str->alloc(MAX_DATE_STRING_REP_LENGTH))) |
933 | return (String *) 0; |
934 | str->length(my_TIME_to_str(<ime, const_cast<char*>(str->ptr()), decimals)); |
935 | str->set_charset(&my_charset_bin); |
936 | DBUG_ASSERT(!null_value); |
937 | return str; |
938 | } |
939 | |
940 | double Item_func_hybrid_field_type::val_real_from_date_op() |
941 | { |
942 | MYSQL_TIME ltime; |
943 | if (date_op_with_null_check(<ime)) |
944 | return 0; |
945 | return TIME_to_double(<ime); |
946 | } |
947 | |
948 | longlong Item_func_hybrid_field_type::val_int_from_date_op() |
949 | { |
950 | MYSQL_TIME ltime; |
951 | if (date_op_with_null_check(<ime)) |
952 | return 0; |
953 | return TIME_to_ulonglong(<ime); |
954 | } |
955 | |
956 | my_decimal * |
957 | Item_func_hybrid_field_type::val_decimal_from_date_op(my_decimal *dec) |
958 | { |
959 | MYSQL_TIME ltime; |
960 | if (date_op_with_null_check(<ime)) |
961 | { |
962 | my_decimal_set_zero(dec); |
963 | return 0; |
964 | } |
965 | return date2my_decimal(<ime, dec); |
966 | } |
967 | |
968 | |
969 | String *Item_func_hybrid_field_type::val_str_from_time_op(String *str) |
970 | { |
971 | MYSQL_TIME ltime; |
972 | if (time_op_with_null_check(<ime) || |
973 | (null_value= my_TIME_to_str(<ime, str, decimals))) |
974 | return NULL; |
975 | return str; |
976 | } |
977 | |
978 | double Item_func_hybrid_field_type::val_real_from_time_op() |
979 | { |
980 | MYSQL_TIME ltime; |
981 | return time_op_with_null_check(<ime) ? 0 : TIME_to_double(<ime); |
982 | } |
983 | |
984 | longlong Item_func_hybrid_field_type::val_int_from_time_op() |
985 | { |
986 | MYSQL_TIME ltime; |
987 | return time_op_with_null_check(<ime) ? 0 : TIME_to_ulonglong(<ime); |
988 | } |
989 | |
990 | my_decimal * |
991 | Item_func_hybrid_field_type::val_decimal_from_time_op(my_decimal *dec) |
992 | { |
993 | MYSQL_TIME ltime; |
994 | if (time_op_with_null_check(<ime)) |
995 | { |
996 | my_decimal_set_zero(dec); |
997 | return 0; |
998 | } |
999 | return date2my_decimal(<ime, dec); |
1000 | } |
1001 | |
1002 | |
1003 | double Item_func_hybrid_field_type::val_real_from_str_op() |
1004 | { |
1005 | String *res= str_op_with_null_check(&str_value); |
1006 | return res ? double_from_string_with_check(res) : 0.0; |
1007 | } |
1008 | |
1009 | longlong Item_func_hybrid_field_type::val_int_from_str_op() |
1010 | { |
1011 | String *res= str_op_with_null_check(&str_value); |
1012 | return res ? longlong_from_string_with_check(res) : 0; |
1013 | } |
1014 | |
1015 | my_decimal * |
1016 | Item_func_hybrid_field_type::val_decimal_from_str_op(my_decimal *decimal_value) |
1017 | { |
1018 | String *res= str_op_with_null_check(&str_value); |
1019 | return res ? decimal_from_string_with_check(decimal_value, res) : 0; |
1020 | } |
1021 | |
1022 | bool Item_func_hybrid_field_type::get_date_from_str_op(MYSQL_TIME *ltime, |
1023 | ulonglong fuzzydate) |
1024 | { |
1025 | StringBuffer<40> tmp; |
1026 | String *res; |
1027 | if (!(res= str_op_with_null_check(&tmp)) || |
1028 | str_to_datetime_with_warn(res->charset(), res->ptr(), res->length(), |
1029 | ltime, fuzzydate)) |
1030 | return make_zero_mysql_time(ltime, fuzzydate); |
1031 | return (null_value= 0); |
1032 | } |
1033 | |
1034 | |
1035 | void Item_func_signed::print(String *str, enum_query_type query_type) |
1036 | { |
1037 | str->append(STRING_WITH_LEN("cast(" )); |
1038 | args[0]->print(str, query_type); |
1039 | str->append(STRING_WITH_LEN(" as signed)" )); |
1040 | |
1041 | } |
1042 | |
1043 | |
1044 | void Item_func_unsigned::print(String *str, enum_query_type query_type) |
1045 | { |
1046 | str->append(STRING_WITH_LEN("cast(" )); |
1047 | args[0]->print(str, query_type); |
1048 | str->append(STRING_WITH_LEN(" as unsigned)" )); |
1049 | |
1050 | } |
1051 | |
1052 | |
1053 | String *Item_decimal_typecast::val_str(String *str) |
1054 | { |
1055 | my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf); |
1056 | if (null_value) |
1057 | return NULL; |
1058 | my_decimal2string(E_DEC_FATAL_ERROR, tmp, 0, 0, 0, str); |
1059 | return str; |
1060 | } |
1061 | |
1062 | |
1063 | double Item_decimal_typecast::val_real() |
1064 | { |
1065 | my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf); |
1066 | double res; |
1067 | if (null_value) |
1068 | return 0.0; |
1069 | my_decimal2double(E_DEC_FATAL_ERROR, tmp, &res); |
1070 | return res; |
1071 | } |
1072 | |
1073 | |
1074 | longlong Item_decimal_typecast::val_int() |
1075 | { |
1076 | my_decimal tmp_buf, *tmp= val_decimal(&tmp_buf); |
1077 | longlong res; |
1078 | if (null_value) |
1079 | return 0; |
1080 | my_decimal2int(E_DEC_FATAL_ERROR, tmp, unsigned_flag, &res); |
1081 | return res; |
1082 | } |
1083 | |
1084 | |
1085 | my_decimal *Item_decimal_typecast::val_decimal(my_decimal *dec) |
1086 | { |
1087 | my_decimal tmp_buf, *tmp= args[0]->val_decimal(&tmp_buf); |
1088 | bool sign; |
1089 | uint precision; |
1090 | |
1091 | if ((null_value= args[0]->null_value)) |
1092 | return NULL; |
1093 | my_decimal_round(E_DEC_FATAL_ERROR, tmp, decimals, FALSE, dec); |
1094 | sign= dec->sign(); |
1095 | if (unsigned_flag) |
1096 | { |
1097 | if (sign) |
1098 | { |
1099 | my_decimal_set_zero(dec); |
1100 | goto err; |
1101 | } |
1102 | } |
1103 | precision= my_decimal_length_to_precision(max_length, |
1104 | decimals, unsigned_flag); |
1105 | if (precision - decimals < (uint) my_decimal_intg(dec)) |
1106 | { |
1107 | max_my_decimal(dec, precision, decimals); |
1108 | dec->sign(sign); |
1109 | goto err; |
1110 | } |
1111 | return dec; |
1112 | |
1113 | err: |
1114 | THD *thd= current_thd; |
1115 | push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, |
1116 | ER_WARN_DATA_OUT_OF_RANGE, |
1117 | ER_THD(thd, ER_WARN_DATA_OUT_OF_RANGE), |
1118 | name.str, 1L); |
1119 | return dec; |
1120 | } |
1121 | |
1122 | |
1123 | void Item_decimal_typecast::print(String *str, enum_query_type query_type) |
1124 | { |
1125 | char len_buf[20*3 + 1]; |
1126 | char *end; |
1127 | |
1128 | uint precision= my_decimal_length_to_precision(max_length, decimals, |
1129 | unsigned_flag); |
1130 | str->append(STRING_WITH_LEN("cast(" )); |
1131 | args[0]->print(str, query_type); |
1132 | str->append(STRING_WITH_LEN(" as decimal(" )); |
1133 | |
1134 | end=int10_to_str(precision, len_buf,10); |
1135 | str->append(len_buf, (uint32) (end - len_buf)); |
1136 | |
1137 | str->append(','); |
1138 | |
1139 | end=int10_to_str(decimals, len_buf,10); |
1140 | str->append(len_buf, (uint32) (end - len_buf)); |
1141 | |
1142 | str->append(')'); |
1143 | str->append(')'); |
1144 | } |
1145 | |
1146 | |
1147 | double Item_double_typecast::val_real() |
1148 | { |
1149 | int error; |
1150 | double tmp= args[0]->val_real(); |
1151 | if ((null_value= args[0]->null_value)) |
1152 | return 0.0; |
1153 | |
1154 | if (unlikely((error= truncate_double(&tmp, max_length, decimals, 0, |
1155 | DBL_MAX)))) |
1156 | { |
1157 | THD *thd= current_thd; |
1158 | push_warning_printf(thd, |
1159 | Sql_condition::WARN_LEVEL_WARN, |
1160 | ER_WARN_DATA_OUT_OF_RANGE, |
1161 | ER_THD(thd, ER_WARN_DATA_OUT_OF_RANGE), |
1162 | name.str, (ulong) 1); |
1163 | if (error < 0) |
1164 | { |
1165 | null_value= 1; // Illegal value |
1166 | tmp= 0.0; |
1167 | } |
1168 | } |
1169 | return tmp; |
1170 | } |
1171 | |
1172 | |
1173 | void Item_double_typecast::print(String *str, enum_query_type query_type) |
1174 | { |
1175 | char len_buf[20*3 + 1]; |
1176 | char *end; |
1177 | |
1178 | str->append(STRING_WITH_LEN("cast(" )); |
1179 | args[0]->print(str, query_type); |
1180 | str->append(STRING_WITH_LEN(" as double" )); |
1181 | if (decimals != NOT_FIXED_DEC) |
1182 | { |
1183 | str->append('('); |
1184 | end= int10_to_str(max_length, len_buf,10); |
1185 | str->append(len_buf, (uint32) (end - len_buf)); |
1186 | str->append(','); |
1187 | end= int10_to_str(decimals, len_buf,10); |
1188 | str->append(len_buf, (uint32) (end - len_buf)); |
1189 | str->append(')'); |
1190 | } |
1191 | str->append(')'); |
1192 | } |
1193 | |
1194 | double Item_func_plus::real_op() |
1195 | { |
1196 | double value= args[0]->val_real() + args[1]->val_real(); |
1197 | if ((null_value=args[0]->null_value || args[1]->null_value)) |
1198 | return 0.0; |
1199 | return check_float_overflow(value); |
1200 | } |
1201 | |
1202 | |
1203 | longlong Item_func_plus::int_op() |
1204 | { |
1205 | longlong val0= args[0]->val_int(); |
1206 | longlong val1= args[1]->val_int(); |
1207 | longlong res= val0 + val1; |
1208 | bool res_unsigned= FALSE; |
1209 | |
1210 | if ((null_value= args[0]->null_value || args[1]->null_value)) |
1211 | return 0; |
1212 | |
1213 | /* |
1214 | First check whether the result can be represented as a |
1215 | (bool unsigned_flag, longlong value) pair, then check if it is compatible |
1216 | with this Item's unsigned_flag by calling check_integer_overflow(). |
1217 | */ |
1218 | if (args[0]->unsigned_flag) |
1219 | { |
1220 | if (args[1]->unsigned_flag || val1 >= 0) |
1221 | { |
1222 | if (test_if_sum_overflows_ull((ulonglong) val0, (ulonglong) val1)) |
1223 | goto err; |
1224 | res_unsigned= TRUE; |
1225 | } |
1226 | else |
1227 | { |
1228 | /* val1 is negative */ |
1229 | if ((ulonglong) val0 > (ulonglong) LONGLONG_MAX) |
1230 | res_unsigned= TRUE; |
1231 | } |
1232 | } |
1233 | else |
1234 | { |
1235 | if (args[1]->unsigned_flag) |
1236 | { |
1237 | if (val0 >= 0) |
1238 | { |
1239 | if (test_if_sum_overflows_ull((ulonglong) val0, (ulonglong) val1)) |
1240 | goto err; |
1241 | res_unsigned= TRUE; |
1242 | } |
1243 | else |
1244 | { |
1245 | if ((ulonglong) val1 > (ulonglong) LONGLONG_MAX) |
1246 | res_unsigned= TRUE; |
1247 | } |
1248 | } |
1249 | else |
1250 | { |
1251 | if (val0 >=0 && val1 >= 0) |
1252 | res_unsigned= TRUE; |
1253 | else if (val0 < 0 && val1 < 0 && res >= 0) |
1254 | goto err; |
1255 | } |
1256 | } |
1257 | return check_integer_overflow(res, res_unsigned); |
1258 | |
1259 | err: |
1260 | return raise_integer_overflow(); |
1261 | } |
1262 | |
1263 | |
1264 | /** |
1265 | Calculate plus of two decimals. |
1266 | |
1267 | @param decimal_value Buffer that can be used to store result |
1268 | |
1269 | @retval |
1270 | 0 Value was NULL; In this case null_value is set |
1271 | @retval |
1272 | \# Value of operation as a decimal |
1273 | */ |
1274 | |
1275 | my_decimal *Item_func_plus::decimal_op(my_decimal *decimal_value) |
1276 | { |
1277 | my_decimal value1, *val1; |
1278 | my_decimal value2, *val2; |
1279 | val1= args[0]->val_decimal(&value1); |
1280 | if ((null_value= args[0]->null_value)) |
1281 | return 0; |
1282 | val2= args[1]->val_decimal(&value2); |
1283 | if (!(null_value= (args[1]->null_value || |
1284 | check_decimal_overflow(my_decimal_add(E_DEC_FATAL_ERROR & |
1285 | ~E_DEC_OVERFLOW, |
1286 | decimal_value, |
1287 | val1, val2)) > 3))) |
1288 | return decimal_value; |
1289 | return 0; |
1290 | } |
1291 | |
1292 | /** |
1293 | Set precision of results for additive operations (+ and -) |
1294 | */ |
1295 | void Item_func_additive_op::result_precision() |
1296 | { |
1297 | decimals= MY_MAX(args[0]->decimal_scale(), args[1]->decimal_scale()); |
1298 | int arg1_int= args[0]->decimal_precision() - args[0]->decimal_scale(); |
1299 | int arg2_int= args[1]->decimal_precision() - args[1]->decimal_scale(); |
1300 | int precision= MY_MAX(arg1_int, arg2_int) + 1 + decimals; |
1301 | |
1302 | DBUG_ASSERT(arg1_int >= 0); |
1303 | DBUG_ASSERT(arg2_int >= 0); |
1304 | |
1305 | max_length= my_decimal_precision_to_length_no_truncation(precision, decimals, |
1306 | unsigned_flag); |
1307 | } |
1308 | |
1309 | |
1310 | /** |
1311 | The following function is here to allow the user to force |
1312 | subtraction of UNSIGNED BIGINT to return negative values. |
1313 | */ |
1314 | void Item_func_minus::fix_unsigned_flag() |
1315 | { |
1316 | if (unsigned_flag && |
1317 | (current_thd->variables.sql_mode & MODE_NO_UNSIGNED_SUBTRACTION)) |
1318 | unsigned_flag=0; |
1319 | } |
1320 | |
1321 | |
1322 | void Item_func_minus::fix_length_and_dec() |
1323 | { |
1324 | DBUG_ENTER("Item_func_minus::fix_length_and_dec" ); |
1325 | DBUG_PRINT("info" , ("name %s" , func_name())); |
1326 | const Type_aggregator *aggregator= &type_handler_data->m_type_aggregator_for_minus; |
1327 | DBUG_EXECUTE_IF("num_op" , aggregator= &type_handler_data->m_type_aggregator_non_commutative_test;); |
1328 | DBUG_ASSERT(!aggregator->is_commutative()); |
1329 | if (!fix_type_handler(aggregator)) |
1330 | { |
1331 | Item_func_minus::type_handler()->Item_func_minus_fix_length_and_dec(this); |
1332 | DBUG_PRINT("info" , ("Type: %s" , type_handler()->name().ptr())); |
1333 | } |
1334 | DBUG_VOID_RETURN; |
1335 | } |
1336 | |
1337 | |
1338 | double Item_func_minus::real_op() |
1339 | { |
1340 | double value= args[0]->val_real() - args[1]->val_real(); |
1341 | if ((null_value=args[0]->null_value || args[1]->null_value)) |
1342 | return 0.0; |
1343 | return check_float_overflow(value); |
1344 | } |
1345 | |
1346 | |
1347 | longlong Item_func_minus::int_op() |
1348 | { |
1349 | longlong val0= args[0]->val_int(); |
1350 | longlong val1= args[1]->val_int(); |
1351 | longlong res= val0 - val1; |
1352 | bool res_unsigned= FALSE; |
1353 | |
1354 | if ((null_value= args[0]->null_value || args[1]->null_value)) |
1355 | return 0; |
1356 | |
1357 | /* |
1358 | First check whether the result can be represented as a |
1359 | (bool unsigned_flag, longlong value) pair, then check if it is compatible |
1360 | with this Item's unsigned_flag by calling check_integer_overflow(). |
1361 | */ |
1362 | if (args[0]->unsigned_flag) |
1363 | { |
1364 | if (args[1]->unsigned_flag) |
1365 | { |
1366 | if ((ulonglong) val0 < (ulonglong) val1) |
1367 | { |
1368 | if (res >= 0) |
1369 | goto err; |
1370 | } |
1371 | else |
1372 | res_unsigned= TRUE; |
1373 | } |
1374 | else |
1375 | { |
1376 | if (val1 >= 0) |
1377 | { |
1378 | if ((ulonglong) val0 > (ulonglong) val1) |
1379 | res_unsigned= TRUE; |
1380 | } |
1381 | else |
1382 | { |
1383 | if (test_if_sum_overflows_ull((ulonglong) val0, (ulonglong) -val1)) |
1384 | goto err; |
1385 | res_unsigned= TRUE; |
1386 | } |
1387 | } |
1388 | } |
1389 | else |
1390 | { |
1391 | if (args[1]->unsigned_flag) |
1392 | { |
1393 | if ((ulonglong) (val0 - LONGLONG_MIN) < (ulonglong) val1) |
1394 | goto err; |
1395 | } |
1396 | else |
1397 | { |
1398 | if (val0 > 0 && val1 < 0) |
1399 | res_unsigned= TRUE; |
1400 | else if (val0 < 0 && val1 > 0 && res >= 0) |
1401 | goto err; |
1402 | } |
1403 | } |
1404 | return check_integer_overflow(res, res_unsigned); |
1405 | |
1406 | err: |
1407 | return raise_integer_overflow(); |
1408 | } |
1409 | |
1410 | |
1411 | /** |
1412 | See Item_func_plus::decimal_op for comments. |
1413 | */ |
1414 | |
1415 | my_decimal *Item_func_minus::decimal_op(my_decimal *decimal_value) |
1416 | { |
1417 | my_decimal value1, *val1; |
1418 | my_decimal value2, *val2= |
1419 | |
1420 | val1= args[0]->val_decimal(&value1); |
1421 | if ((null_value= args[0]->null_value)) |
1422 | return 0; |
1423 | val2= args[1]->val_decimal(&value2); |
1424 | if (!(null_value= (args[1]->null_value || |
1425 | (check_decimal_overflow(my_decimal_sub(E_DEC_FATAL_ERROR & |
1426 | ~E_DEC_OVERFLOW, |
1427 | decimal_value, val1, |
1428 | val2)) > 3)))) |
1429 | return decimal_value; |
1430 | return 0; |
1431 | } |
1432 | |
1433 | |
1434 | double Item_func_mul::real_op() |
1435 | { |
1436 | DBUG_ASSERT(fixed == 1); |
1437 | double value= args[0]->val_real() * args[1]->val_real(); |
1438 | if ((null_value=args[0]->null_value || args[1]->null_value)) |
1439 | return 0.0; |
1440 | return check_float_overflow(value); |
1441 | } |
1442 | |
1443 | |
1444 | longlong Item_func_mul::int_op() |
1445 | { |
1446 | DBUG_ASSERT(fixed == 1); |
1447 | longlong a= args[0]->val_int(); |
1448 | longlong b= args[1]->val_int(); |
1449 | longlong res; |
1450 | ulonglong res0, res1; |
1451 | ulong a0, a1, b0, b1; |
1452 | bool res_unsigned= FALSE; |
1453 | bool a_negative= FALSE, b_negative= FALSE; |
1454 | |
1455 | if ((null_value= args[0]->null_value || args[1]->null_value)) |
1456 | return 0; |
1457 | |
1458 | /* |
1459 | First check whether the result can be represented as a |
1460 | (bool unsigned_flag, longlong value) pair, then check if it is compatible |
1461 | with this Item's unsigned_flag by calling check_integer_overflow(). |
1462 | |
1463 | Let a = a1 * 2^32 + a0 and b = b1 * 2^32 + b0. Then |
1464 | a * b = (a1 * 2^32 + a0) * (b1 * 2^32 + b0) = a1 * b1 * 2^64 + |
1465 | + (a1 * b0 + a0 * b1) * 2^32 + a0 * b0; |
1466 | We can determine if the above sum overflows the ulonglong range by |
1467 | sequentially checking the following conditions: |
1468 | 1. If both a1 and b1 are non-zero. |
1469 | 2. Otherwise, if (a1 * b0 + a0 * b1) is greater than ULONG_MAX. |
1470 | 3. Otherwise, if (a1 * b0 + a0 * b1) * 2^32 + a0 * b0 is greater than |
1471 | ULONGLONG_MAX. |
1472 | |
1473 | Since we also have to take the unsigned_flag for a and b into account, |
1474 | it is easier to first work with absolute values and set the |
1475 | correct sign later. |
1476 | */ |
1477 | if (!args[0]->unsigned_flag && a < 0) |
1478 | { |
1479 | a_negative= TRUE; |
1480 | a= -a; |
1481 | } |
1482 | if (!args[1]->unsigned_flag && b < 0) |
1483 | { |
1484 | b_negative= TRUE; |
1485 | b= -b; |
1486 | } |
1487 | |
1488 | a0= 0xFFFFFFFFUL & a; |
1489 | a1= ((ulonglong) a) >> 32; |
1490 | b0= 0xFFFFFFFFUL & b; |
1491 | b1= ((ulonglong) b) >> 32; |
1492 | |
1493 | if (a1 && b1) |
1494 | goto err; |
1495 | |
1496 | res1= (ulonglong) a1 * b0 + (ulonglong) a0 * b1; |
1497 | if (res1 > 0xFFFFFFFFUL) |
1498 | goto err; |
1499 | |
1500 | res1= res1 << 32; |
1501 | res0= (ulonglong) a0 * b0; |
1502 | |
1503 | if (test_if_sum_overflows_ull(res1, res0)) |
1504 | goto err; |
1505 | res= res1 + res0; |
1506 | |
1507 | if (a_negative != b_negative) |
1508 | { |
1509 | if ((ulonglong) res > (ulonglong) LONGLONG_MIN + 1) |
1510 | goto err; |
1511 | res= -res; |
1512 | } |
1513 | else |
1514 | res_unsigned= TRUE; |
1515 | |
1516 | return check_integer_overflow(res, res_unsigned); |
1517 | |
1518 | err: |
1519 | return raise_integer_overflow(); |
1520 | } |
1521 | |
1522 | |
1523 | /** See Item_func_plus::decimal_op for comments. */ |
1524 | |
1525 | my_decimal *Item_func_mul::decimal_op(my_decimal *decimal_value) |
1526 | { |
1527 | my_decimal value1, *val1; |
1528 | my_decimal value2, *val2; |
1529 | val1= args[0]->val_decimal(&value1); |
1530 | if ((null_value= args[0]->null_value)) |
1531 | return 0; |
1532 | val2= args[1]->val_decimal(&value2); |
1533 | if (!(null_value= (args[1]->null_value || |
1534 | (check_decimal_overflow(my_decimal_mul(E_DEC_FATAL_ERROR & |
1535 | ~E_DEC_OVERFLOW, |
1536 | decimal_value, val1, |
1537 | val2)) > 3)))) |
1538 | return decimal_value; |
1539 | return 0; |
1540 | } |
1541 | |
1542 | |
1543 | void Item_func_mul::result_precision() |
1544 | { |
1545 | decimals= MY_MIN(args[0]->decimal_scale() + args[1]->decimal_scale(), |
1546 | DECIMAL_MAX_SCALE); |
1547 | uint est_prec = args[0]->decimal_precision() + args[1]->decimal_precision(); |
1548 | uint precision= MY_MIN(est_prec, DECIMAL_MAX_PRECISION); |
1549 | max_length= my_decimal_precision_to_length_no_truncation(precision, decimals, |
1550 | unsigned_flag); |
1551 | } |
1552 | |
1553 | |
1554 | void Item_func_mul::fix_length_and_dec(void) |
1555 | { |
1556 | DBUG_ENTER("Item_func_mul::fix_length_and_dec" ); |
1557 | DBUG_PRINT("info" , ("name %s" , func_name())); |
1558 | const Type_aggregator *aggregator= &type_handler_data->m_type_aggregator_for_mul; |
1559 | DBUG_EXECUTE_IF("num_op" , aggregator= &type_handler_data->m_type_aggregator_for_result;); |
1560 | DBUG_ASSERT(aggregator->is_commutative()); |
1561 | if (!fix_type_handler(aggregator)) |
1562 | { |
1563 | Item_func_mul::type_handler()->Item_func_mul_fix_length_and_dec(this); |
1564 | DBUG_PRINT("info" , ("Type: %s" , type_handler()->name().ptr())); |
1565 | } |
1566 | DBUG_VOID_RETURN; |
1567 | } |
1568 | |
1569 | |
1570 | double Item_func_div::real_op() |
1571 | { |
1572 | DBUG_ASSERT(fixed == 1); |
1573 | double value= args[0]->val_real(); |
1574 | double val2= args[1]->val_real(); |
1575 | if ((null_value= args[0]->null_value || args[1]->null_value)) |
1576 | return 0.0; |
1577 | if (val2 == 0.0) |
1578 | { |
1579 | signal_divide_by_null(); |
1580 | return 0.0; |
1581 | } |
1582 | return check_float_overflow(value/val2); |
1583 | } |
1584 | |
1585 | |
1586 | my_decimal *Item_func_div::decimal_op(my_decimal *decimal_value) |
1587 | { |
1588 | my_decimal value1, *val1; |
1589 | my_decimal value2, *val2; |
1590 | int err; |
1591 | |
1592 | val1= args[0]->val_decimal(&value1); |
1593 | if ((null_value= args[0]->null_value)) |
1594 | return 0; |
1595 | val2= args[1]->val_decimal(&value2); |
1596 | if ((null_value= args[1]->null_value)) |
1597 | return 0; |
1598 | if ((err= check_decimal_overflow(my_decimal_div(E_DEC_FATAL_ERROR & |
1599 | ~E_DEC_OVERFLOW & |
1600 | ~E_DEC_DIV_ZERO, |
1601 | decimal_value, |
1602 | val1, val2, |
1603 | prec_increment))) > 3) |
1604 | { |
1605 | if (err == E_DEC_DIV_ZERO) |
1606 | signal_divide_by_null(); |
1607 | null_value= 1; |
1608 | return 0; |
1609 | } |
1610 | return decimal_value; |
1611 | } |
1612 | |
1613 | |
1614 | void Item_func_div::result_precision() |
1615 | { |
1616 | /* |
1617 | We need to add args[1]->divisor_precision_increment(), |
1618 | to properly handle the cases like this: |
1619 | SELECT 5.05 / 0.014; -> 360.714286 |
1620 | i.e. when the divisor has a zero integer part |
1621 | and non-zero digits appear only after the decimal point. |
1622 | Precision in this example is calculated as |
1623 | args[0]->decimal_precision() + // 3 |
1624 | args[1]->divisor_precision_increment() + // 3 |
1625 | prec_increment // 4 |
1626 | which gives 10 decimals digits. |
1627 | */ |
1628 | uint precision=MY_MIN(args[0]->decimal_precision() + |
1629 | args[1]->divisor_precision_increment() + prec_increment, |
1630 | DECIMAL_MAX_PRECISION); |
1631 | decimals= MY_MIN(args[0]->decimal_scale() + prec_increment, DECIMAL_MAX_SCALE); |
1632 | max_length= my_decimal_precision_to_length_no_truncation(precision, decimals, |
1633 | unsigned_flag); |
1634 | } |
1635 | |
1636 | |
1637 | void Item_func_div::fix_length_and_dec_double(void) |
1638 | { |
1639 | Item_num_op::fix_length_and_dec_double(); |
1640 | decimals= MY_MAX(args[0]->decimals, args[1]->decimals) + prec_increment; |
1641 | set_if_smaller(decimals, NOT_FIXED_DEC); |
1642 | uint tmp= float_length(decimals); |
1643 | if (decimals == NOT_FIXED_DEC) |
1644 | max_length= tmp; |
1645 | else |
1646 | { |
1647 | max_length=args[0]->max_length - args[0]->decimals + decimals; |
1648 | set_if_smaller(max_length, tmp); |
1649 | } |
1650 | } |
1651 | |
1652 | |
1653 | void Item_func_div::fix_length_and_dec_int(void) |
1654 | { |
1655 | set_handler(&type_handler_newdecimal); |
1656 | DBUG_PRINT("info" , ("Type changed: %s" , type_handler()->name().ptr())); |
1657 | Item_num_op::fix_length_and_dec_decimal(); |
1658 | } |
1659 | |
1660 | |
1661 | void Item_func_div::fix_length_and_dec(void) |
1662 | { |
1663 | DBUG_ENTER("Item_func_div::fix_length_and_dec" ); |
1664 | DBUG_PRINT("info" , ("name %s" , func_name())); |
1665 | prec_increment= current_thd->variables.div_precincrement; |
1666 | maybe_null= 1; // devision by zero |
1667 | |
1668 | const Type_aggregator *aggregator= &type_handler_data->m_type_aggregator_for_div; |
1669 | DBUG_EXECUTE_IF("num_op" , aggregator= &type_handler_data->m_type_aggregator_non_commutative_test;); |
1670 | DBUG_ASSERT(!aggregator->is_commutative()); |
1671 | if (!fix_type_handler(aggregator)) |
1672 | { |
1673 | Item_func_div::type_handler()->Item_func_div_fix_length_and_dec(this); |
1674 | DBUG_PRINT("info" , ("Type: %s" , type_handler()->name().ptr())); |
1675 | } |
1676 | DBUG_VOID_RETURN; |
1677 | } |
1678 | |
1679 | |
1680 | /* Integer division */ |
1681 | longlong Item_func_int_div::val_int() |
1682 | { |
1683 | DBUG_ASSERT(fixed == 1); |
1684 | |
1685 | /* |
1686 | Perform division using DECIMAL math if either of the operands has a |
1687 | non-integer type |
1688 | */ |
1689 | if (args[0]->result_type() != INT_RESULT || |
1690 | args[1]->result_type() != INT_RESULT) |
1691 | { |
1692 | my_decimal tmp; |
1693 | my_decimal *val0p= args[0]->val_decimal(&tmp); |
1694 | if ((null_value= args[0]->null_value)) |
1695 | return 0; |
1696 | my_decimal val0= *val0p; |
1697 | |
1698 | my_decimal *val1p= args[1]->val_decimal(&tmp); |
1699 | if ((null_value= args[1]->null_value)) |
1700 | return 0; |
1701 | my_decimal val1= *val1p; |
1702 | |
1703 | int err; |
1704 | if ((err= my_decimal_div(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, &tmp, |
1705 | &val0, &val1, 0)) > 3) |
1706 | { |
1707 | if (err == E_DEC_DIV_ZERO) |
1708 | signal_divide_by_null(); |
1709 | return 0; |
1710 | } |
1711 | |
1712 | my_decimal truncated; |
1713 | const bool do_truncate= true; |
1714 | if (my_decimal_round(E_DEC_FATAL_ERROR, &tmp, 0, do_truncate, &truncated)) |
1715 | DBUG_ASSERT(false); |
1716 | |
1717 | longlong res; |
1718 | if (my_decimal2int(E_DEC_FATAL_ERROR, &truncated, unsigned_flag, &res) & |
1719 | E_DEC_OVERFLOW) |
1720 | raise_integer_overflow(); |
1721 | return res; |
1722 | } |
1723 | |
1724 | longlong val0=args[0]->val_int(); |
1725 | longlong val1=args[1]->val_int(); |
1726 | bool val0_negative, val1_negative, res_negative; |
1727 | ulonglong uval0, uval1, res; |
1728 | if ((null_value= (args[0]->null_value || args[1]->null_value))) |
1729 | return 0; |
1730 | if (val1 == 0) |
1731 | { |
1732 | signal_divide_by_null(); |
1733 | return 0; |
1734 | } |
1735 | |
1736 | val0_negative= !args[0]->unsigned_flag && val0 < 0; |
1737 | val1_negative= !args[1]->unsigned_flag && val1 < 0; |
1738 | res_negative= val0_negative != val1_negative; |
1739 | uval0= (ulonglong) (val0_negative ? -val0 : val0); |
1740 | uval1= (ulonglong) (val1_negative ? -val1 : val1); |
1741 | res= uval0 / uval1; |
1742 | if (res_negative) |
1743 | { |
1744 | if (res > (ulonglong) LONGLONG_MAX) |
1745 | return raise_integer_overflow(); |
1746 | res= (ulonglong) (-(longlong) res); |
1747 | } |
1748 | return check_integer_overflow(res, !res_negative); |
1749 | } |
1750 | |
1751 | |
1752 | void Item_func_int_div::fix_length_and_dec() |
1753 | { |
1754 | Item_result argtype= args[0]->result_type(); |
1755 | /* use precision ony for the data type it is applicable for and valid */ |
1756 | uint32 char_length= args[0]->max_char_length() - |
1757 | (argtype == DECIMAL_RESULT || argtype == INT_RESULT ? |
1758 | args[0]->decimals : 0); |
1759 | fix_char_length(char_length > MY_INT64_NUM_DECIMAL_DIGITS ? |
1760 | MY_INT64_NUM_DECIMAL_DIGITS : char_length); |
1761 | maybe_null=1; |
1762 | unsigned_flag=args[0]->unsigned_flag | args[1]->unsigned_flag; |
1763 | } |
1764 | |
1765 | |
1766 | longlong Item_func_mod::int_op() |
1767 | { |
1768 | DBUG_ASSERT(fixed == 1); |
1769 | longlong val0= args[0]->val_int(); |
1770 | longlong val1= args[1]->val_int(); |
1771 | bool val0_negative, val1_negative; |
1772 | ulonglong uval0, uval1; |
1773 | ulonglong res; |
1774 | |
1775 | if ((null_value= args[0]->null_value || args[1]->null_value)) |
1776 | return 0; /* purecov: inspected */ |
1777 | if (val1 == 0) |
1778 | { |
1779 | signal_divide_by_null(); |
1780 | return 0; |
1781 | } |
1782 | |
1783 | /* |
1784 | '%' is calculated by integer division internally. Since dividing |
1785 | LONGLONG_MIN by -1 generates SIGFPE, we calculate using unsigned values and |
1786 | then adjust the sign appropriately. |
1787 | */ |
1788 | val0_negative= !args[0]->unsigned_flag && val0 < 0; |
1789 | val1_negative= !args[1]->unsigned_flag && val1 < 0; |
1790 | uval0= (ulonglong) (val0_negative ? -val0 : val0); |
1791 | uval1= (ulonglong) (val1_negative ? -val1 : val1); |
1792 | res= uval0 % uval1; |
1793 | return check_integer_overflow(val0_negative ? -(longlong) res : res, |
1794 | !val0_negative); |
1795 | } |
1796 | |
1797 | double Item_func_mod::real_op() |
1798 | { |
1799 | DBUG_ASSERT(fixed == 1); |
1800 | double value= args[0]->val_real(); |
1801 | double val2= args[1]->val_real(); |
1802 | if ((null_value= args[0]->null_value || args[1]->null_value)) |
1803 | return 0.0; /* purecov: inspected */ |
1804 | if (val2 == 0.0) |
1805 | { |
1806 | signal_divide_by_null(); |
1807 | return 0.0; |
1808 | } |
1809 | return fmod(value,val2); |
1810 | } |
1811 | |
1812 | |
1813 | my_decimal *Item_func_mod::decimal_op(my_decimal *decimal_value) |
1814 | { |
1815 | my_decimal value1, *val1; |
1816 | my_decimal value2, *val2; |
1817 | |
1818 | val1= args[0]->val_decimal(&value1); |
1819 | if ((null_value= args[0]->null_value)) |
1820 | return 0; |
1821 | val2= args[1]->val_decimal(&value2); |
1822 | if ((null_value= args[1]->null_value)) |
1823 | return 0; |
1824 | switch (my_decimal_mod(E_DEC_FATAL_ERROR & ~E_DEC_DIV_ZERO, decimal_value, |
1825 | val1, val2)) { |
1826 | case E_DEC_TRUNCATED: |
1827 | case E_DEC_OK: |
1828 | return decimal_value; |
1829 | case E_DEC_DIV_ZERO: |
1830 | signal_divide_by_null(); |
1831 | /* fall through */ |
1832 | default: |
1833 | null_value= 1; |
1834 | return 0; |
1835 | } |
1836 | } |
1837 | |
1838 | |
1839 | void Item_func_mod::result_precision() |
1840 | { |
1841 | decimals= MY_MAX(args[0]->decimal_scale(), args[1]->decimal_scale()); |
1842 | max_length= MY_MAX(args[0]->max_length, args[1]->max_length); |
1843 | } |
1844 | |
1845 | |
1846 | void Item_func_mod::fix_length_and_dec() |
1847 | { |
1848 | DBUG_ENTER("Item_func_mod::fix_length_and_dec" ); |
1849 | DBUG_PRINT("info" , ("name %s" , func_name())); |
1850 | maybe_null= true; // division by zero |
1851 | const Type_aggregator *aggregator= &type_handler_data->m_type_aggregator_for_mod; |
1852 | DBUG_EXECUTE_IF("num_op" , aggregator= &type_handler_data->m_type_aggregator_non_commutative_test;); |
1853 | DBUG_ASSERT(!aggregator->is_commutative()); |
1854 | if (!fix_type_handler(aggregator)) |
1855 | { |
1856 | Item_func_mod::type_handler()->Item_func_mod_fix_length_and_dec(this); |
1857 | DBUG_PRINT("info" , ("Type: %s" , type_handler()->name().ptr())); |
1858 | } |
1859 | DBUG_VOID_RETURN; |
1860 | } |
1861 | |
1862 | |
1863 | double Item_func_neg::real_op() |
1864 | { |
1865 | double value= args[0]->val_real(); |
1866 | null_value= args[0]->null_value; |
1867 | return -value; |
1868 | } |
1869 | |
1870 | |
1871 | longlong Item_func_neg::int_op() |
1872 | { |
1873 | longlong value= args[0]->val_int(); |
1874 | if ((null_value= args[0]->null_value)) |
1875 | return 0; |
1876 | if (args[0]->unsigned_flag && |
1877 | (ulonglong) value > (ulonglong) LONGLONG_MAX + 1) |
1878 | return raise_integer_overflow(); |
1879 | |
1880 | if (value == LONGLONG_MIN) |
1881 | { |
1882 | if (args[0]->unsigned_flag != unsigned_flag) |
1883 | /* negation of LONGLONG_MIN is LONGLONG_MIN. */ |
1884 | return LONGLONG_MIN; |
1885 | else |
1886 | return raise_integer_overflow(); |
1887 | } |
1888 | |
1889 | return check_integer_overflow(-value, !args[0]->unsigned_flag && value < 0); |
1890 | } |
1891 | |
1892 | |
1893 | my_decimal *Item_func_neg::decimal_op(my_decimal *decimal_value) |
1894 | { |
1895 | my_decimal val, *value= args[0]->val_decimal(&val); |
1896 | if (!(null_value= args[0]->null_value)) |
1897 | { |
1898 | my_decimal2decimal(value, decimal_value); |
1899 | my_decimal_neg(decimal_value); |
1900 | return decimal_value; |
1901 | } |
1902 | return 0; |
1903 | } |
1904 | |
1905 | |
1906 | void Item_func_neg::fix_length_and_dec_int() |
1907 | { |
1908 | max_length= args[0]->max_length + 1; |
1909 | set_handler(type_handler_long_or_longlong()); |
1910 | |
1911 | /* |
1912 | If this is in integer context keep the context as integer if possible |
1913 | (This is how multiplication and other integer functions works) |
1914 | Use val() to get value as arg_type doesn't mean that item is |
1915 | Item_int or Item_float due to existence of Item_param. |
1916 | */ |
1917 | if (args[0]->const_item()) |
1918 | { |
1919 | longlong val= args[0]->val_int(); |
1920 | if ((ulonglong) val >= (ulonglong) LONGLONG_MIN && |
1921 | ((ulonglong) val != (ulonglong) LONGLONG_MIN || |
1922 | args[0]->type() != INT_ITEM)) |
1923 | { |
1924 | /* |
1925 | Ensure that result is converted to DECIMAL, as longlong can't hold |
1926 | the negated number |
1927 | */ |
1928 | set_handler_by_result_type(DECIMAL_RESULT); |
1929 | DBUG_PRINT("info" , ("Type changed: DECIMAL_RESULT" )); |
1930 | } |
1931 | } |
1932 | unsigned_flag= false; |
1933 | } |
1934 | |
1935 | |
1936 | void Item_func_neg::fix_length_and_dec_double() |
1937 | { |
1938 | set_handler(&type_handler_double); |
1939 | decimals= args[0]->decimals; // Preserve NOT_FIXED_DEC |
1940 | max_length= args[0]->max_length + 1; |
1941 | // Limit length with something reasonable |
1942 | uint32 mlen= type_handler()->max_display_length(this); |
1943 | set_if_smaller(max_length, mlen); |
1944 | unsigned_flag= false; |
1945 | } |
1946 | |
1947 | |
1948 | void Item_func_neg::fix_length_and_dec_decimal() |
1949 | { |
1950 | set_handler(&type_handler_newdecimal); |
1951 | decimals= args[0]->decimal_scale(); // Do not preserve NOT_FIXED_DEC |
1952 | max_length= args[0]->max_length + 1; |
1953 | unsigned_flag= false; |
1954 | } |
1955 | |
1956 | |
1957 | void Item_func_neg::fix_length_and_dec() |
1958 | { |
1959 | DBUG_ENTER("Item_func_neg::fix_length_and_dec" ); |
1960 | DBUG_PRINT("info" , ("name %s" , func_name())); |
1961 | args[0]->cast_to_int_type_handler()->Item_func_neg_fix_length_and_dec(this); |
1962 | DBUG_PRINT("info" , ("Type: %s" , type_handler()->name().ptr())); |
1963 | DBUG_VOID_RETURN; |
1964 | } |
1965 | |
1966 | |
1967 | double Item_func_abs::real_op() |
1968 | { |
1969 | double value= args[0]->val_real(); |
1970 | null_value= args[0]->null_value; |
1971 | return fabs(value); |
1972 | } |
1973 | |
1974 | |
1975 | longlong Item_func_abs::int_op() |
1976 | { |
1977 | longlong value= args[0]->val_int(); |
1978 | if ((null_value= args[0]->null_value)) |
1979 | return 0; |
1980 | if (unsigned_flag) |
1981 | return value; |
1982 | /* -LONGLONG_MIN = LONGLONG_MAX + 1 => outside of signed longlong range */ |
1983 | if (value == LONGLONG_MIN) |
1984 | return raise_integer_overflow(); |
1985 | return (value >= 0) ? value : -value; |
1986 | } |
1987 | |
1988 | |
1989 | my_decimal *Item_func_abs::decimal_op(my_decimal *decimal_value) |
1990 | { |
1991 | my_decimal val, *value= args[0]->val_decimal(&val); |
1992 | if (!(null_value= args[0]->null_value)) |
1993 | { |
1994 | my_decimal2decimal(value, decimal_value); |
1995 | if (decimal_value->sign()) |
1996 | my_decimal_neg(decimal_value); |
1997 | return decimal_value; |
1998 | } |
1999 | return 0; |
2000 | } |
2001 | |
2002 | |
2003 | void Item_func_abs::fix_length_and_dec_int() |
2004 | { |
2005 | max_length= args[0]->max_length; |
2006 | unsigned_flag= args[0]->unsigned_flag; |
2007 | set_handler(type_handler_long_or_longlong()); |
2008 | } |
2009 | |
2010 | |
2011 | void Item_func_abs::fix_length_and_dec_double() |
2012 | { |
2013 | set_handler(&type_handler_double); |
2014 | decimals= args[0]->decimals; // Preserve NOT_FIXED_DEC |
2015 | max_length= float_length(decimals); |
2016 | unsigned_flag= args[0]->unsigned_flag; |
2017 | } |
2018 | |
2019 | |
2020 | void Item_func_abs::fix_length_and_dec_decimal() |
2021 | { |
2022 | set_handler(&type_handler_newdecimal); |
2023 | decimals= args[0]->decimal_scale(); // Do not preserve NOT_FIXED_DEC |
2024 | max_length= args[0]->max_length; |
2025 | unsigned_flag= args[0]->unsigned_flag; |
2026 | } |
2027 | |
2028 | |
2029 | void Item_func_abs::fix_length_and_dec() |
2030 | { |
2031 | DBUG_ENTER("Item_func_abs::fix_length_and_dec" ); |
2032 | DBUG_PRINT("info" , ("name %s" , func_name())); |
2033 | args[0]->cast_to_int_type_handler()->Item_func_abs_fix_length_and_dec(this); |
2034 | DBUG_PRINT("info" , ("Type: %s" , type_handler()->name().ptr())); |
2035 | DBUG_VOID_RETURN; |
2036 | } |
2037 | |
2038 | |
2039 | /** Gateway to natural LOG function. */ |
2040 | double Item_func_ln::val_real() |
2041 | { |
2042 | DBUG_ASSERT(fixed == 1); |
2043 | double value= args[0]->val_real(); |
2044 | if ((null_value= args[0]->null_value)) |
2045 | return 0.0; |
2046 | if (value <= 0.0) |
2047 | { |
2048 | signal_divide_by_null(); |
2049 | return 0.0; |
2050 | } |
2051 | return log(value); |
2052 | } |
2053 | |
2054 | /** |
2055 | Extended but so slower LOG function. |
2056 | |
2057 | We have to check if all values are > zero and first one is not one |
2058 | as these are the cases then result is not a number. |
2059 | */ |
2060 | double Item_func_log::val_real() |
2061 | { |
2062 | DBUG_ASSERT(fixed == 1); |
2063 | double value= args[0]->val_real(); |
2064 | if ((null_value= args[0]->null_value)) |
2065 | return 0.0; |
2066 | if (value <= 0.0) |
2067 | { |
2068 | signal_divide_by_null(); |
2069 | return 0.0; |
2070 | } |
2071 | if (arg_count == 2) |
2072 | { |
2073 | double value2= args[1]->val_real(); |
2074 | if ((null_value= args[1]->null_value)) |
2075 | return 0.0; |
2076 | if (value2 <= 0.0 || value == 1.0) |
2077 | { |
2078 | signal_divide_by_null(); |
2079 | return 0.0; |
2080 | } |
2081 | return log(value2) / log(value); |
2082 | } |
2083 | return log(value); |
2084 | } |
2085 | |
2086 | double Item_func_log2::val_real() |
2087 | { |
2088 | DBUG_ASSERT(fixed == 1); |
2089 | double value= args[0]->val_real(); |
2090 | |
2091 | if ((null_value=args[0]->null_value)) |
2092 | return 0.0; |
2093 | if (value <= 0.0) |
2094 | { |
2095 | signal_divide_by_null(); |
2096 | return 0.0; |
2097 | } |
2098 | return log(value) / M_LN2; |
2099 | } |
2100 | |
2101 | double Item_func_log10::val_real() |
2102 | { |
2103 | DBUG_ASSERT(fixed == 1); |
2104 | double value= args[0]->val_real(); |
2105 | if ((null_value= args[0]->null_value)) |
2106 | return 0.0; |
2107 | if (value <= 0.0) |
2108 | { |
2109 | signal_divide_by_null(); |
2110 | return 0.0; |
2111 | } |
2112 | return log10(value); |
2113 | } |
2114 | |
2115 | double Item_func_exp::val_real() |
2116 | { |
2117 | DBUG_ASSERT(fixed == 1); |
2118 | double value= args[0]->val_real(); |
2119 | if ((null_value=args[0]->null_value)) |
2120 | return 0.0; /* purecov: inspected */ |
2121 | return check_float_overflow(exp(value)); |
2122 | } |
2123 | |
2124 | double Item_func_sqrt::val_real() |
2125 | { |
2126 | DBUG_ASSERT(fixed == 1); |
2127 | double value= args[0]->val_real(); |
2128 | if ((null_value=(args[0]->null_value || value < 0))) |
2129 | return 0.0; /* purecov: inspected */ |
2130 | return sqrt(value); |
2131 | } |
2132 | |
2133 | double Item_func_pow::val_real() |
2134 | { |
2135 | DBUG_ASSERT(fixed == 1); |
2136 | double value= args[0]->val_real(); |
2137 | double val2= args[1]->val_real(); |
2138 | if ((null_value=(args[0]->null_value || args[1]->null_value))) |
2139 | return 0.0; /* purecov: inspected */ |
2140 | return check_float_overflow(pow(value,val2)); |
2141 | } |
2142 | |
2143 | // Trigonometric functions |
2144 | |
2145 | double Item_func_acos::val_real() |
2146 | { |
2147 | DBUG_ASSERT(fixed == 1); |
2148 | /* One can use this to defer SELECT processing. */ |
2149 | DEBUG_SYNC(current_thd, "before_acos_function" ); |
2150 | // the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug) |
2151 | volatile double value= args[0]->val_real(); |
2152 | if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0)))) |
2153 | return 0.0; |
2154 | return acos(value); |
2155 | } |
2156 | |
2157 | double Item_func_asin::val_real() |
2158 | { |
2159 | DBUG_ASSERT(fixed == 1); |
2160 | // the volatile's for BUG #2338 to calm optimizer down (because of gcc's bug) |
2161 | volatile double value= args[0]->val_real(); |
2162 | if ((null_value=(args[0]->null_value || (value < -1.0 || value > 1.0)))) |
2163 | return 0.0; |
2164 | return asin(value); |
2165 | } |
2166 | |
2167 | double Item_func_atan::val_real() |
2168 | { |
2169 | DBUG_ASSERT(fixed == 1); |
2170 | double value= args[0]->val_real(); |
2171 | if ((null_value=args[0]->null_value)) |
2172 | return 0.0; |
2173 | if (arg_count == 2) |
2174 | { |
2175 | double val2= args[1]->val_real(); |
2176 | if ((null_value=args[1]->null_value)) |
2177 | return 0.0; |
2178 | return check_float_overflow(atan2(value,val2)); |
2179 | } |
2180 | return atan(value); |
2181 | } |
2182 | |
2183 | double Item_func_cos::val_real() |
2184 | { |
2185 | DBUG_ASSERT(fixed == 1); |
2186 | double value= args[0]->val_real(); |
2187 | if ((null_value=args[0]->null_value)) |
2188 | return 0.0; |
2189 | return cos(value); |
2190 | } |
2191 | |
2192 | double Item_func_sin::val_real() |
2193 | { |
2194 | DBUG_ASSERT(fixed == 1); |
2195 | double value= args[0]->val_real(); |
2196 | if ((null_value=args[0]->null_value)) |
2197 | return 0.0; |
2198 | return sin(value); |
2199 | } |
2200 | |
2201 | double Item_func_tan::val_real() |
2202 | { |
2203 | DBUG_ASSERT(fixed == 1); |
2204 | double value= args[0]->val_real(); |
2205 | if ((null_value=args[0]->null_value)) |
2206 | return 0.0; |
2207 | return check_float_overflow(tan(value)); |
2208 | } |
2209 | |
2210 | |
2211 | double Item_func_cot::val_real() |
2212 | { |
2213 | DBUG_ASSERT(fixed == 1); |
2214 | double value= args[0]->val_real(); |
2215 | if ((null_value=args[0]->null_value)) |
2216 | return 0.0; |
2217 | return check_float_overflow(1.0 / tan(value)); |
2218 | } |
2219 | |
2220 | |
2221 | // Shift-functions, same as << and >> in C/C++ |
2222 | |
2223 | |
2224 | longlong Item_func_shift_left::val_int() |
2225 | { |
2226 | DBUG_ASSERT(fixed == 1); |
2227 | uint shift; |
2228 | ulonglong res= ((ulonglong) args[0]->val_int() << |
2229 | (shift=(uint) args[1]->val_int())); |
2230 | if (args[0]->null_value || args[1]->null_value) |
2231 | { |
2232 | null_value=1; |
2233 | return 0; |
2234 | } |
2235 | null_value=0; |
2236 | return (shift < sizeof(longlong)*8 ? (longlong) res : 0); |
2237 | } |
2238 | |
2239 | longlong Item_func_shift_right::val_int() |
2240 | { |
2241 | DBUG_ASSERT(fixed == 1); |
2242 | uint shift; |
2243 | ulonglong res= (ulonglong) args[0]->val_int() >> |
2244 | (shift=(uint) args[1]->val_int()); |
2245 | if (args[0]->null_value || args[1]->null_value) |
2246 | { |
2247 | null_value=1; |
2248 | return 0; |
2249 | } |
2250 | null_value=0; |
2251 | return (shift < sizeof(longlong)*8 ? (longlong) res : 0); |
2252 | } |
2253 | |
2254 | |
2255 | longlong Item_func_bit_neg::val_int() |
2256 | { |
2257 | DBUG_ASSERT(fixed == 1); |
2258 | ulonglong res= (ulonglong) args[0]->val_int(); |
2259 | if ((null_value=args[0]->null_value)) |
2260 | return 0; |
2261 | return ~res; |
2262 | } |
2263 | |
2264 | |
2265 | // Conversion functions |
2266 | |
2267 | void Item_func_int_val::fix_length_and_dec_int_or_decimal() |
2268 | { |
2269 | ulonglong tmp_max_length= (ulonglong ) args[0]->max_length - |
2270 | (args[0]->decimals ? args[0]->decimals + 1 : 0) + 2; |
2271 | max_length= tmp_max_length > (ulonglong) UINT_MAX32 ? |
2272 | (uint32) UINT_MAX32 : (uint32) tmp_max_length; |
2273 | uint tmp= float_length(decimals); |
2274 | set_if_smaller(max_length,tmp); |
2275 | decimals= 0; |
2276 | |
2277 | /* |
2278 | -2 because in most high position can't be used any digit for longlong |
2279 | and one position for increasing value during operation |
2280 | */ |
2281 | if (args[0]->max_length - args[0]->decimals >= DECIMAL_LONGLONG_DIGITS - 2) |
2282 | { |
2283 | set_handler(&type_handler_newdecimal); |
2284 | } |
2285 | else |
2286 | { |
2287 | unsigned_flag= args[0]->unsigned_flag; |
2288 | set_handler(type_handler_long_or_longlong()); |
2289 | } |
2290 | } |
2291 | |
2292 | |
2293 | void Item_func_int_val::fix_length_and_dec_double() |
2294 | { |
2295 | set_handler(&type_handler_double); |
2296 | max_length= float_length(0); |
2297 | decimals= 0; |
2298 | } |
2299 | |
2300 | |
2301 | void Item_func_int_val::fix_length_and_dec() |
2302 | { |
2303 | DBUG_ENTER("Item_func_int_val::fix_length_and_dec" ); |
2304 | DBUG_PRINT("info" , ("name %s" , func_name())); |
2305 | args[0]->cast_to_int_type_handler()-> |
2306 | Item_func_int_val_fix_length_and_dec(this); |
2307 | DBUG_PRINT("info" , ("Type: %s" , type_handler()->name().ptr())); |
2308 | DBUG_VOID_RETURN; |
2309 | } |
2310 | |
2311 | |
2312 | longlong Item_func_ceiling::int_op() |
2313 | { |
2314 | longlong result; |
2315 | switch (args[0]->result_type()) { |
2316 | case INT_RESULT: |
2317 | result= args[0]->val_int(); |
2318 | null_value= args[0]->null_value; |
2319 | break; |
2320 | case DECIMAL_RESULT: |
2321 | { |
2322 | my_decimal dec_buf, *dec; |
2323 | if ((dec= Item_func_ceiling::decimal_op(&dec_buf))) |
2324 | my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result); |
2325 | else |
2326 | result= 0; |
2327 | break; |
2328 | } |
2329 | default: |
2330 | result= (longlong)Item_func_ceiling::real_op(); |
2331 | }; |
2332 | return result; |
2333 | } |
2334 | |
2335 | |
2336 | double Item_func_ceiling::real_op() |
2337 | { |
2338 | /* |
2339 | the volatile's for BUG #3051 to calm optimizer down (because of gcc's |
2340 | bug) |
2341 | */ |
2342 | volatile double value= args[0]->val_real(); |
2343 | null_value= args[0]->null_value; |
2344 | return ceil(value); |
2345 | } |
2346 | |
2347 | |
2348 | my_decimal *Item_func_ceiling::decimal_op(my_decimal *decimal_value) |
2349 | { |
2350 | my_decimal val, *value= args[0]->val_decimal(&val); |
2351 | if (!(null_value= (args[0]->null_value || |
2352 | my_decimal_ceiling(E_DEC_FATAL_ERROR, value, |
2353 | decimal_value) > 1))) |
2354 | return decimal_value; |
2355 | return 0; |
2356 | } |
2357 | |
2358 | |
2359 | longlong Item_func_floor::int_op() |
2360 | { |
2361 | longlong result; |
2362 | switch (args[0]->result_type()) { |
2363 | case INT_RESULT: |
2364 | result= args[0]->val_int(); |
2365 | null_value= args[0]->null_value; |
2366 | break; |
2367 | case DECIMAL_RESULT: |
2368 | { |
2369 | my_decimal dec_buf, *dec; |
2370 | if ((dec= Item_func_floor::decimal_op(&dec_buf))) |
2371 | my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result); |
2372 | else |
2373 | result= 0; |
2374 | break; |
2375 | } |
2376 | default: |
2377 | result= (longlong)Item_func_floor::real_op(); |
2378 | }; |
2379 | return result; |
2380 | } |
2381 | |
2382 | |
2383 | double Item_func_floor::real_op() |
2384 | { |
2385 | /* |
2386 | the volatile's for BUG #3051 to calm optimizer down (because of gcc's |
2387 | bug) |
2388 | */ |
2389 | volatile double value= args[0]->val_real(); |
2390 | null_value= args[0]->null_value; |
2391 | return floor(value); |
2392 | } |
2393 | |
2394 | |
2395 | my_decimal *Item_func_floor::decimal_op(my_decimal *decimal_value) |
2396 | { |
2397 | my_decimal val, *value= args[0]->val_decimal(&val); |
2398 | if (!(null_value= (args[0]->null_value || |
2399 | my_decimal_floor(E_DEC_FATAL_ERROR, value, |
2400 | decimal_value) > 1))) |
2401 | return decimal_value; |
2402 | return 0; |
2403 | } |
2404 | |
2405 | |
2406 | void Item_func_round::fix_length_and_dec_decimal(uint decimals_to_set) |
2407 | { |
2408 | int decimals_delta= args[0]->decimals - decimals_to_set; |
2409 | int length_increase= (decimals_delta <= 0 || truncate) ? 0 : 1; |
2410 | int precision= args[0]->decimal_precision() + length_increase - |
2411 | decimals_delta; |
2412 | DBUG_ASSERT(decimals_to_set <= DECIMAL_MAX_SCALE); |
2413 | set_handler(&type_handler_newdecimal); |
2414 | unsigned_flag= args[0]->unsigned_flag; |
2415 | decimals= decimals_to_set; |
2416 | max_length= my_decimal_precision_to_length_no_truncation(precision, |
2417 | decimals, |
2418 | unsigned_flag); |
2419 | } |
2420 | |
2421 | void Item_func_round::fix_length_and_dec_double(uint decimals_to_set) |
2422 | { |
2423 | set_handler(&type_handler_double); |
2424 | unsigned_flag= args[0]->unsigned_flag; |
2425 | decimals= decimals_to_set; |
2426 | max_length= float_length(decimals_to_set); |
2427 | } |
2428 | |
2429 | |
2430 | void Item_func_round::fix_arg_decimal() |
2431 | { |
2432 | if (args[1]->const_item()) |
2433 | { |
2434 | uint dec= (uint) args[1]->val_uint_from_val_int(DECIMAL_MAX_SCALE); |
2435 | if (args[1]->null_value) |
2436 | fix_length_and_dec_double(NOT_FIXED_DEC); |
2437 | else |
2438 | fix_length_and_dec_decimal(dec); |
2439 | } |
2440 | else |
2441 | { |
2442 | set_handler(&type_handler_newdecimal); |
2443 | unsigned_flag= args[0]->unsigned_flag; |
2444 | decimals= args[0]->decimals; |
2445 | max_length= float_length(args[0]->decimals) + 1; |
2446 | } |
2447 | } |
2448 | |
2449 | |
2450 | void Item_func_round::fix_arg_double() |
2451 | { |
2452 | if (args[1]->const_item()) |
2453 | { |
2454 | uint dec= (uint) args[1]->val_uint_from_val_int(NOT_FIXED_DEC); |
2455 | fix_length_and_dec_double(args[1]->null_value ? NOT_FIXED_DEC : dec); |
2456 | } |
2457 | else |
2458 | fix_length_and_dec_double(args[0]->decimals); |
2459 | } |
2460 | |
2461 | |
2462 | void Item_func_round::fix_arg_int() |
2463 | { |
2464 | if (args[1]->const_item()) |
2465 | { |
2466 | longlong val1= args[1]->val_int(); |
2467 | bool val1_is_negative= val1 < 0 && !args[1]->unsigned_flag; |
2468 | uint decimals_to_set= val1_is_negative ? |
2469 | 0 : (uint) MY_MIN(val1, DECIMAL_MAX_SCALE); |
2470 | if (args[1]->null_value) |
2471 | fix_length_and_dec_double(NOT_FIXED_DEC); |
2472 | else if ((!decimals_to_set && truncate) || |
2473 | args[0]->decimal_precision() < DECIMAL_LONGLONG_DIGITS) |
2474 | { |
2475 | // Length can increase in some cases: ROUND(9,-1) -> 10 |
2476 | int length_can_increase= MY_TEST(!truncate && val1_is_negative); |
2477 | max_length= args[0]->max_length + length_can_increase; |
2478 | // Here we can keep INT_RESULT |
2479 | unsigned_flag= args[0]->unsigned_flag; |
2480 | decimals= 0; |
2481 | set_handler(type_handler_long_or_longlong()); |
2482 | } |
2483 | else |
2484 | fix_length_and_dec_decimal(decimals_to_set); |
2485 | } |
2486 | else |
2487 | fix_length_and_dec_double(args[0]->decimals); |
2488 | |
2489 | } |
2490 | |
2491 | |
2492 | double my_double_round(double value, longlong dec, bool dec_unsigned, |
2493 | bool truncate) |
2494 | { |
2495 | double tmp; |
2496 | bool dec_negative= (dec < 0) && !dec_unsigned; |
2497 | ulonglong abs_dec= dec_negative ? -dec : dec; |
2498 | /* |
2499 | tmp2 is here to avoid return the value with 80 bit precision |
2500 | This will fix that the test round(0.1,1) = round(0.1,1) is true |
2501 | Tagging with volatile is no guarantee, it may still be optimized away... |
2502 | */ |
2503 | volatile double tmp2; |
2504 | |
2505 | tmp=(abs_dec < array_elements(log_10) ? |
2506 | log_10[abs_dec] : pow(10.0,(double) abs_dec)); |
2507 | |
2508 | // Pre-compute these, to avoid optimizing away e.g. 'floor(v/tmp) * tmp'. |
2509 | volatile double value_div_tmp= value / tmp; |
2510 | volatile double value_mul_tmp= value * tmp; |
2511 | |
2512 | if (!dec_negative && my_isinf(tmp)) // "dec" is too large positive number |
2513 | return value; |
2514 | |
2515 | if (dec_negative && my_isinf(tmp)) |
2516 | tmp2= 0.0; |
2517 | else if (!dec_negative && my_isinf(value_mul_tmp)) |
2518 | tmp2= value; |
2519 | else if (truncate) |
2520 | { |
2521 | if (value >= 0.0) |
2522 | tmp2= dec < 0 ? floor(value_div_tmp) * tmp : floor(value_mul_tmp) / tmp; |
2523 | else |
2524 | tmp2= dec < 0 ? ceil(value_div_tmp) * tmp : ceil(value_mul_tmp) / tmp; |
2525 | } |
2526 | else |
2527 | tmp2=dec < 0 ? rint(value_div_tmp) * tmp : rint(value_mul_tmp) / tmp; |
2528 | |
2529 | return tmp2; |
2530 | } |
2531 | |
2532 | |
2533 | double Item_func_round::real_op() |
2534 | { |
2535 | double value= args[0]->val_real(); |
2536 | |
2537 | if (!(null_value= args[0]->null_value)) |
2538 | { |
2539 | longlong dec= args[1]->val_int(); |
2540 | if (!(null_value= args[1]->null_value)) |
2541 | return my_double_round(value, dec, args[1]->unsigned_flag, truncate); |
2542 | } |
2543 | return 0.0; |
2544 | } |
2545 | |
2546 | /* |
2547 | Rounds a given value to a power of 10 specified as the 'to' argument, |
2548 | avoiding overflows when the value is close to the ulonglong range boundary. |
2549 | */ |
2550 | |
2551 | static inline ulonglong my_unsigned_round(ulonglong value, ulonglong to) |
2552 | { |
2553 | ulonglong tmp= value / to * to; |
2554 | return (value - tmp < (to >> 1)) ? tmp : tmp + to; |
2555 | } |
2556 | |
2557 | |
2558 | longlong Item_func_round::int_op() |
2559 | { |
2560 | longlong value= args[0]->val_int(); |
2561 | longlong dec= args[1]->val_int(); |
2562 | decimals= 0; |
2563 | ulonglong abs_dec; |
2564 | if ((null_value= args[0]->null_value || args[1]->null_value)) |
2565 | return 0; |
2566 | if ((dec >= 0) || args[1]->unsigned_flag) |
2567 | return value; // integer have not digits after point |
2568 | |
2569 | abs_dec= -dec; |
2570 | longlong tmp; |
2571 | |
2572 | if(abs_dec >= array_elements(log_10_int)) |
2573 | return 0; |
2574 | |
2575 | tmp= log_10_int[abs_dec]; |
2576 | |
2577 | if (truncate) |
2578 | value= (unsigned_flag) ? |
2579 | ((ulonglong) value / tmp) * tmp : (value / tmp) * tmp; |
2580 | else |
2581 | value= (unsigned_flag || value >= 0) ? |
2582 | my_unsigned_round((ulonglong) value, tmp) : |
2583 | -(longlong) my_unsigned_round((ulonglong) -value, tmp); |
2584 | return value; |
2585 | } |
2586 | |
2587 | |
2588 | my_decimal *Item_func_round::decimal_op(my_decimal *decimal_value) |
2589 | { |
2590 | my_decimal val, *value= args[0]->val_decimal(&val); |
2591 | longlong dec= args[1]->val_int(); |
2592 | if (dec >= 0 || args[1]->unsigned_flag) |
2593 | dec= MY_MIN((ulonglong) dec, decimals); |
2594 | else if (dec < INT_MIN) |
2595 | dec= INT_MIN; |
2596 | |
2597 | if (!(null_value= (args[0]->null_value || args[1]->null_value || |
2598 | my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec, |
2599 | truncate, decimal_value) > 1))) |
2600 | return decimal_value; |
2601 | return 0; |
2602 | } |
2603 | |
2604 | |
2605 | void Item_func_rand::seed_random(Item *arg) |
2606 | { |
2607 | /* |
2608 | TODO: do not do reinit 'rand' for every execute of PS/SP if |
2609 | args[0] is a constant. |
2610 | */ |
2611 | uint32 tmp; |
2612 | #ifdef WITH_WSREP |
2613 | THD *thd= current_thd; |
2614 | if (WSREP(thd)) |
2615 | { |
2616 | if (thd->wsrep_exec_mode==REPL_RECV) |
2617 | tmp= thd->wsrep_rand; |
2618 | else |
2619 | tmp= thd->wsrep_rand= (uint32) arg->val_int(); |
2620 | } |
2621 | else |
2622 | #endif /* WITH_WSREP */ |
2623 | tmp= (uint32) arg->val_int(); |
2624 | |
2625 | my_rnd_init(rand, (uint32) (tmp*0x10001L+55555555L), |
2626 | (uint32) (tmp*0x10000001L)); |
2627 | } |
2628 | |
2629 | |
2630 | bool Item_func_rand::fix_fields(THD *thd,Item **ref) |
2631 | { |
2632 | if (Item_real_func::fix_fields(thd, ref)) |
2633 | return TRUE; |
2634 | used_tables_cache|= RAND_TABLE_BIT; |
2635 | if (arg_count) |
2636 | { // Only use argument once in query |
2637 | /* |
2638 | Allocate rand structure once: we must use thd->stmt_arena |
2639 | to create rand in proper mem_root if it's a prepared statement or |
2640 | stored procedure. |
2641 | |
2642 | No need to send a Rand log event if seed was given eg: RAND(seed), |
2643 | as it will be replicated in the query as such. |
2644 | */ |
2645 | if (!rand && !(rand= (struct my_rnd_struct*) |
2646 | thd->stmt_arena->alloc(sizeof(*rand)))) |
2647 | return TRUE; |
2648 | } |
2649 | else |
2650 | { |
2651 | /* |
2652 | Save the seed only the first time RAND() is used in the query |
2653 | Once events are forwarded rather than recreated, |
2654 | the following can be skipped if inside the slave thread |
2655 | */ |
2656 | if (!thd->rand_used) |
2657 | { |
2658 | thd->rand_used= 1; |
2659 | thd->rand_saved_seed1= thd->rand.seed1; |
2660 | thd->rand_saved_seed2= thd->rand.seed2; |
2661 | } |
2662 | rand= &thd->rand; |
2663 | } |
2664 | return FALSE; |
2665 | } |
2666 | |
2667 | void Item_func_rand::update_used_tables() |
2668 | { |
2669 | Item_real_func::update_used_tables(); |
2670 | used_tables_cache|= RAND_TABLE_BIT; |
2671 | } |
2672 | |
2673 | |
2674 | double Item_func_rand::val_real() |
2675 | { |
2676 | DBUG_ASSERT(fixed == 1); |
2677 | if (arg_count) |
2678 | { |
2679 | if (!args[0]->const_item()) |
2680 | seed_random(args[0]); |
2681 | else if (first_eval) |
2682 | { |
2683 | /* |
2684 | Constantness of args[0] may be set during JOIN::optimize(), if arg[0] |
2685 | is a field item of "constant" table. Thus, we have to evaluate |
2686 | seed_random() for constant arg there but not at the fix_fields method. |
2687 | */ |
2688 | first_eval= FALSE; |
2689 | seed_random(args[0]); |
2690 | } |
2691 | } |
2692 | return my_rnd(rand); |
2693 | } |
2694 | |
2695 | longlong Item_func_sign::val_int() |
2696 | { |
2697 | DBUG_ASSERT(fixed == 1); |
2698 | double value= args[0]->val_real(); |
2699 | null_value=args[0]->null_value; |
2700 | return value < 0.0 ? -1 : (value > 0 ? 1 : 0); |
2701 | } |
2702 | |
2703 | |
2704 | double Item_func_units::val_real() |
2705 | { |
2706 | DBUG_ASSERT(fixed == 1); |
2707 | double value= args[0]->val_real(); |
2708 | if ((null_value=args[0]->null_value)) |
2709 | return 0; |
2710 | return check_float_overflow(value * mul + add); |
2711 | } |
2712 | |
2713 | |
2714 | bool Item_func_min_max::fix_attributes(Item **items, uint nitems) |
2715 | { |
2716 | bool rc= Item_func_min_max::type_handler()-> |
2717 | Item_func_min_max_fix_attributes(current_thd, this, items, nitems); |
2718 | DBUG_ASSERT(!rc || current_thd->is_error()); |
2719 | return rc; |
2720 | } |
2721 | |
2722 | |
2723 | /* |
2724 | Compare item arguments using DATETIME/DATE/TIME representation. |
2725 | |
2726 | DESCRIPTION |
2727 | Compare item arguments as DATETIME values and return the index of the |
2728 | least/greatest argument in the arguments array. |
2729 | The correct DATE/DATETIME value of the found argument is |
2730 | stored to the value pointer, if latter is provided. |
2731 | |
2732 | RETURN |
2733 | 1 If one of arguments is NULL or there was a execution error |
2734 | 0 Otherwise |
2735 | */ |
2736 | |
2737 | bool Item_func_min_max::get_date_native(MYSQL_TIME *ltime, ulonglong fuzzy_date) |
2738 | { |
2739 | longlong UNINIT_VAR(min_max); |
2740 | DBUG_ASSERT(fixed == 1); |
2741 | |
2742 | for (uint i=0; i < arg_count ; i++) |
2743 | { |
2744 | longlong res= args[i]->val_datetime_packed(); |
2745 | |
2746 | /* Check if we need to stop (because of error or KILL) and stop the loop */ |
2747 | if (unlikely(args[i]->null_value)) |
2748 | return (null_value= 1); |
2749 | |
2750 | if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0) |
2751 | min_max= res; |
2752 | } |
2753 | unpack_time(min_max, ltime, mysql_timestamp_type()); |
2754 | |
2755 | if (!(fuzzy_date & TIME_TIME_ONLY) && |
2756 | unlikely((null_value= check_date_with_warn(ltime, fuzzy_date, |
2757 | MYSQL_TIMESTAMP_ERROR)))) |
2758 | return true; |
2759 | |
2760 | return (null_value= 0); |
2761 | } |
2762 | |
2763 | |
2764 | bool Item_func_min_max::get_time_native(MYSQL_TIME *ltime) |
2765 | { |
2766 | DBUG_ASSERT(fixed == 1); |
2767 | |
2768 | Time value(args[0]); |
2769 | if (!value.is_valid_time()) |
2770 | return (null_value= true); |
2771 | |
2772 | for (uint i= 1; i < arg_count ; i++) |
2773 | { |
2774 | Time tmp(args[i]); |
2775 | if (!tmp.is_valid_time()) |
2776 | return (null_value= true); |
2777 | |
2778 | int cmp= value.cmp(&tmp); |
2779 | if ((cmp_sign < 0 ? cmp : -cmp) < 0) |
2780 | value= tmp; |
2781 | } |
2782 | value.copy_to_mysql_time(ltime); |
2783 | return (null_value= 0); |
2784 | } |
2785 | |
2786 | |
2787 | String *Item_func_min_max::val_str_native(String *str) |
2788 | { |
2789 | String *UNINIT_VAR(res); |
2790 | for (uint i=0; i < arg_count ; i++) |
2791 | { |
2792 | if (i == 0) |
2793 | res=args[i]->val_str(str); |
2794 | else |
2795 | { |
2796 | String *res2; |
2797 | res2= args[i]->val_str(res == str ? &tmp_value : str); |
2798 | if (res2) |
2799 | { |
2800 | int cmp= sortcmp(res,res2,collation.collation); |
2801 | if ((cmp_sign < 0 ? cmp : -cmp) < 0) |
2802 | res=res2; |
2803 | } |
2804 | } |
2805 | if ((null_value= args[i]->null_value)) |
2806 | return 0; |
2807 | } |
2808 | res->set_charset(collation.collation); |
2809 | return res; |
2810 | } |
2811 | |
2812 | |
2813 | double Item_func_min_max::val_real_native() |
2814 | { |
2815 | double value=0.0; |
2816 | for (uint i=0; i < arg_count ; i++) |
2817 | { |
2818 | if (i == 0) |
2819 | value= args[i]->val_real(); |
2820 | else |
2821 | { |
2822 | double tmp= args[i]->val_real(); |
2823 | if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0) |
2824 | value=tmp; |
2825 | } |
2826 | if ((null_value= args[i]->null_value)) |
2827 | break; |
2828 | } |
2829 | return value; |
2830 | } |
2831 | |
2832 | |
2833 | longlong Item_func_min_max::val_int_native() |
2834 | { |
2835 | DBUG_ASSERT(fixed == 1); |
2836 | longlong value=0; |
2837 | for (uint i=0; i < arg_count ; i++) |
2838 | { |
2839 | if (i == 0) |
2840 | value=args[i]->val_int(); |
2841 | else |
2842 | { |
2843 | longlong tmp=args[i]->val_int(); |
2844 | if (!args[i]->null_value && (tmp < value ? cmp_sign : -cmp_sign) > 0) |
2845 | value=tmp; |
2846 | } |
2847 | if ((null_value= args[i]->null_value)) |
2848 | break; |
2849 | } |
2850 | return value; |
2851 | } |
2852 | |
2853 | |
2854 | my_decimal *Item_func_min_max::val_decimal_native(my_decimal *dec) |
2855 | { |
2856 | DBUG_ASSERT(fixed == 1); |
2857 | my_decimal tmp_buf, *tmp, *UNINIT_VAR(res); |
2858 | |
2859 | for (uint i=0; i < arg_count ; i++) |
2860 | { |
2861 | if (i == 0) |
2862 | res= args[i]->val_decimal(dec); |
2863 | else |
2864 | { |
2865 | tmp= args[i]->val_decimal(&tmp_buf); // Zero if NULL |
2866 | if (tmp && (my_decimal_cmp(tmp, res) * cmp_sign) < 0) |
2867 | { |
2868 | if (tmp == &tmp_buf) |
2869 | { |
2870 | /* Move value out of tmp_buf as this will be reused on next loop */ |
2871 | my_decimal2decimal(tmp, dec); |
2872 | res= dec; |
2873 | } |
2874 | else |
2875 | res= tmp; |
2876 | } |
2877 | } |
2878 | if ((null_value= args[i]->null_value)) |
2879 | { |
2880 | res= 0; |
2881 | break; |
2882 | } |
2883 | } |
2884 | return res; |
2885 | } |
2886 | |
2887 | |
2888 | longlong Item_func_bit_length::val_int() |
2889 | { |
2890 | DBUG_ASSERT(fixed == 1); |
2891 | String *res= args[0]->val_str(&value); |
2892 | return (null_value= !res) ? 0 : (longlong) res->length() * 8; |
2893 | } |
2894 | |
2895 | |
2896 | longlong Item_func_octet_length::val_int() |
2897 | { |
2898 | DBUG_ASSERT(fixed == 1); |
2899 | String *res=args[0]->val_str(&value); |
2900 | if (!res) |
2901 | { |
2902 | null_value=1; |
2903 | return 0; /* purecov: inspected */ |
2904 | } |
2905 | null_value=0; |
2906 | return (longlong) res->length(); |
2907 | } |
2908 | |
2909 | |
2910 | longlong Item_func_char_length::val_int() |
2911 | { |
2912 | DBUG_ASSERT(fixed == 1); |
2913 | String *res=args[0]->val_str(&value); |
2914 | if (!res) |
2915 | { |
2916 | null_value=1; |
2917 | return 0; /* purecov: inspected */ |
2918 | } |
2919 | null_value=0; |
2920 | return (longlong) res->numchars(); |
2921 | } |
2922 | |
2923 | |
2924 | longlong Item_func_coercibility::val_int() |
2925 | { |
2926 | DBUG_ASSERT(fixed == 1); |
2927 | null_value= 0; |
2928 | return (longlong) args[0]->collation.derivation; |
2929 | } |
2930 | |
2931 | |
2932 | longlong Item_func_locate::val_int() |
2933 | { |
2934 | DBUG_ASSERT(fixed == 1); |
2935 | String *a=args[0]->val_str(&value1); |
2936 | String *b=args[1]->val_str(&value2); |
2937 | if (!a || !b) |
2938 | { |
2939 | null_value=1; |
2940 | return 0; /* purecov: inspected */ |
2941 | } |
2942 | null_value=0; |
2943 | /* must be longlong to avoid truncation */ |
2944 | longlong start= 0; |
2945 | longlong start0= 0; |
2946 | my_match_t match; |
2947 | |
2948 | if (arg_count == 3) |
2949 | { |
2950 | start0= start= args[2]->val_int() - 1; |
2951 | |
2952 | if ((start < 0) || (start > a->length())) |
2953 | return 0; |
2954 | |
2955 | /* start is now sufficiently valid to pass to charpos function */ |
2956 | start= a->charpos((int) start); |
2957 | |
2958 | if (start + b->length() > a->length()) |
2959 | return 0; |
2960 | } |
2961 | |
2962 | if (!b->length()) // Found empty string at start |
2963 | return start + 1; |
2964 | |
2965 | if (!cmp_collation.collation->coll->instr(cmp_collation.collation, |
2966 | a->ptr()+start, |
2967 | (uint) (a->length()-start), |
2968 | b->ptr(), b->length(), |
2969 | &match, 1)) |
2970 | return 0; |
2971 | return (longlong) match.mb_len + start0 + 1; |
2972 | } |
2973 | |
2974 | |
2975 | void Item_func_locate::print(String *str, enum_query_type query_type) |
2976 | { |
2977 | str->append(STRING_WITH_LEN("locate(" )); |
2978 | args[1]->print(str, query_type); |
2979 | str->append(','); |
2980 | args[0]->print(str, query_type); |
2981 | if (arg_count == 3) |
2982 | { |
2983 | str->append(','); |
2984 | args[2]->print(str, query_type); |
2985 | } |
2986 | str->append(')'); |
2987 | } |
2988 | |
2989 | |
2990 | longlong Item_func_field::val_int() |
2991 | { |
2992 | DBUG_ASSERT(fixed == 1); |
2993 | |
2994 | if (cmp_type == STRING_RESULT) |
2995 | { |
2996 | String *field; |
2997 | if (!(field= args[0]->val_str(&value))) |
2998 | return 0; |
2999 | for (uint i=1 ; i < arg_count ; i++) |
3000 | { |
3001 | String *tmp_value=args[i]->val_str(&tmp); |
3002 | if (tmp_value && !sortcmp(field,tmp_value,cmp_collation.collation)) |
3003 | return (longlong) (i); |
3004 | } |
3005 | } |
3006 | else if (cmp_type == INT_RESULT) |
3007 | { |
3008 | longlong val= args[0]->val_int(); |
3009 | if (args[0]->null_value) |
3010 | return 0; |
3011 | for (uint i=1; i < arg_count ; i++) |
3012 | { |
3013 | if (val == args[i]->val_int() && !args[i]->null_value) |
3014 | return (longlong) (i); |
3015 | } |
3016 | } |
3017 | else if (cmp_type == DECIMAL_RESULT) |
3018 | { |
3019 | my_decimal dec_arg_buf, *dec_arg, |
3020 | dec_buf, *dec= args[0]->val_decimal(&dec_buf); |
3021 | if (args[0]->null_value) |
3022 | return 0; |
3023 | for (uint i=1; i < arg_count; i++) |
3024 | { |
3025 | dec_arg= args[i]->val_decimal(&dec_arg_buf); |
3026 | if (!args[i]->null_value && !my_decimal_cmp(dec_arg, dec)) |
3027 | return (longlong) (i); |
3028 | } |
3029 | } |
3030 | else |
3031 | { |
3032 | double val= args[0]->val_real(); |
3033 | if (args[0]->null_value) |
3034 | return 0; |
3035 | for (uint i=1; i < arg_count ; i++) |
3036 | { |
3037 | if (val == args[i]->val_real() && !args[i]->null_value) |
3038 | return (longlong) (i); |
3039 | } |
3040 | } |
3041 | return 0; |
3042 | } |
3043 | |
3044 | |
3045 | void Item_func_field::fix_length_and_dec() |
3046 | { |
3047 | maybe_null=0; max_length=3; |
3048 | cmp_type= args[0]->result_type(); |
3049 | for (uint i=1; i < arg_count ; i++) |
3050 | cmp_type= item_cmp_type(cmp_type, args[i]->result_type()); |
3051 | if (cmp_type == STRING_RESULT) |
3052 | agg_arg_charsets_for_comparison(cmp_collation, args, arg_count); |
3053 | } |
3054 | |
3055 | |
3056 | longlong Item_func_ascii::val_int() |
3057 | { |
3058 | DBUG_ASSERT(fixed == 1); |
3059 | String *res=args[0]->val_str(&value); |
3060 | if (!res) |
3061 | { |
3062 | null_value=1; |
3063 | return 0; |
3064 | } |
3065 | null_value=0; |
3066 | return (longlong) (res->length() ? (uchar) (*res)[0] : (uchar) 0); |
3067 | } |
3068 | |
3069 | longlong Item_func_ord::val_int() |
3070 | { |
3071 | DBUG_ASSERT(fixed == 1); |
3072 | String *res=args[0]->val_str(&value); |
3073 | if (!res) |
3074 | { |
3075 | null_value=1; |
3076 | return 0; |
3077 | } |
3078 | null_value=0; |
3079 | if (!res->length()) return 0; |
3080 | #ifdef USE_MB |
3081 | if (use_mb(res->charset())) |
3082 | { |
3083 | const char *str=res->ptr(); |
3084 | uint32 n=0, l=my_ismbchar(res->charset(),str,str+res->length()); |
3085 | if (!l) |
3086 | return (longlong)((uchar) *str); |
3087 | while (l--) |
3088 | n=(n<<8)|(uint32)((uchar) *str++); |
3089 | return (longlong) n; |
3090 | } |
3091 | #endif |
3092 | return (longlong) ((uchar) (*res)[0]); |
3093 | } |
3094 | |
3095 | /* Search after a string in a string of strings separated by ',' */ |
3096 | /* Returns number of found type >= 1 or 0 if not found */ |
3097 | /* This optimizes searching in enums to bit testing! */ |
3098 | |
3099 | void Item_func_find_in_set::fix_length_and_dec() |
3100 | { |
3101 | decimals=0; |
3102 | max_length=3; // 1-999 |
3103 | if (args[0]->const_item() && args[1]->type() == FIELD_ITEM) |
3104 | { |
3105 | Field *field= ((Item_field*) args[1])->field; |
3106 | if (field->real_type() == MYSQL_TYPE_SET) |
3107 | { |
3108 | String *find=args[0]->val_str(&value); |
3109 | if (find) |
3110 | { |
3111 | // find is not NULL pointer so args[0] is not a null-value |
3112 | DBUG_ASSERT(!args[0]->null_value); |
3113 | enum_value= find_type(((Field_enum*) field)->typelib,find->ptr(), |
3114 | find->length(), 0); |
3115 | enum_bit=0; |
3116 | if (enum_value) |
3117 | enum_bit=1LL << (enum_value-1); |
3118 | } |
3119 | } |
3120 | } |
3121 | agg_arg_charsets_for_comparison(cmp_collation, args, 2); |
3122 | } |
3123 | |
3124 | static const char separator=','; |
3125 | |
3126 | longlong Item_func_find_in_set::val_int() |
3127 | { |
3128 | DBUG_ASSERT(fixed == 1); |
3129 | if (enum_value) |
3130 | { |
3131 | // enum_value is set iff args[0]->const_item() in fix_length_and_dec(). |
3132 | DBUG_ASSERT(args[0]->const_item()); |
3133 | |
3134 | ulonglong tmp= (ulonglong) args[1]->val_int(); |
3135 | null_value= args[1]->null_value; |
3136 | /* |
3137 | No need to check args[0]->null_value since enum_value is set iff |
3138 | args[0] is a non-null const item. Note: no DBUG_ASSERT on |
3139 | args[0]->null_value here because args[0] may have been replaced |
3140 | by an Item_cache on which val_int() has not been called. See |
3141 | BUG#11766317 |
3142 | */ |
3143 | if (!null_value) |
3144 | { |
3145 | if (tmp & enum_bit) |
3146 | return enum_value; |
3147 | } |
3148 | return 0L; |
3149 | } |
3150 | |
3151 | String *find=args[0]->val_str(&value); |
3152 | String *buffer=args[1]->val_str(&value2); |
3153 | if (!find || !buffer) |
3154 | { |
3155 | null_value=1; |
3156 | return 0; /* purecov: inspected */ |
3157 | } |
3158 | null_value=0; |
3159 | |
3160 | if ((int) (buffer->length() - find->length()) >= 0) |
3161 | { |
3162 | my_wc_t wc= 0; |
3163 | CHARSET_INFO *cs= cmp_collation.collation; |
3164 | const char *str_begin= buffer->ptr(); |
3165 | const char *str_end= buffer->ptr(); |
3166 | const char *real_end= str_end+buffer->length(); |
3167 | const uchar *find_str= (const uchar *) find->ptr(); |
3168 | uint find_str_len= find->length(); |
3169 | int position= 0; |
3170 | while (1) |
3171 | { |
3172 | int symbol_len; |
3173 | if ((symbol_len= cs->cset->mb_wc(cs, &wc, (uchar*) str_end, |
3174 | (uchar*) real_end)) > 0) |
3175 | { |
3176 | const char *substr_end= str_end + symbol_len; |
3177 | bool is_last_item= (substr_end == real_end); |
3178 | bool is_separator= (wc == (my_wc_t) separator); |
3179 | if (is_separator || is_last_item) |
3180 | { |
3181 | position++; |
3182 | if (is_last_item && !is_separator) |
3183 | str_end= substr_end; |
3184 | if (!my_strnncoll(cs, (const uchar *) str_begin, |
3185 | (uint) (str_end - str_begin), |
3186 | find_str, find_str_len)) |
3187 | return (longlong) position; |
3188 | else |
3189 | str_begin= substr_end; |
3190 | } |
3191 | str_end= substr_end; |
3192 | } |
3193 | else if (str_end - str_begin == 0 && |
3194 | find_str_len == 0 && |
3195 | wc == (my_wc_t) separator) |
3196 | return (longlong) ++position; |
3197 | else |
3198 | return 0; |
3199 | } |
3200 | } |
3201 | return 0; |
3202 | } |
3203 | |
3204 | longlong Item_func_bit_count::val_int() |
3205 | { |
3206 | DBUG_ASSERT(fixed == 1); |
3207 | ulonglong value= (ulonglong) args[0]->val_int(); |
3208 | if ((null_value= args[0]->null_value)) |
3209 | return 0; /* purecov: inspected */ |
3210 | return (longlong) my_count_bits(value); |
3211 | } |
3212 | |
3213 | |
3214 | /**************************************************************************** |
3215 | ** Functions to handle dynamic loadable functions |
3216 | ** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su> |
3217 | ** Rewritten by monty. |
3218 | ****************************************************************************/ |
3219 | |
3220 | #ifdef HAVE_DLOPEN |
3221 | |
3222 | void udf_handler::cleanup() |
3223 | { |
3224 | if (!not_original) |
3225 | { |
3226 | if (initialized) |
3227 | { |
3228 | if (u_d->func_deinit != NULL) |
3229 | { |
3230 | Udf_func_deinit deinit= u_d->func_deinit; |
3231 | (*deinit)(&initid); |
3232 | } |
3233 | free_udf(u_d); |
3234 | initialized= FALSE; |
3235 | } |
3236 | if (buffers) // Because of bug in ecc |
3237 | delete [] buffers; |
3238 | buffers= 0; |
3239 | } |
3240 | } |
3241 | |
3242 | |
3243 | bool |
3244 | udf_handler::fix_fields(THD *thd, Item_func_or_sum *func, |
3245 | uint arg_count, Item **arguments) |
3246 | { |
3247 | uchar buff[STACK_BUFF_ALLOC]; // Max argument in function |
3248 | DBUG_ENTER("Item_udf_func::fix_fields" ); |
3249 | |
3250 | if (check_stack_overrun(thd, STACK_MIN_SIZE, buff)) |
3251 | DBUG_RETURN(TRUE); // Fatal error flag is set! |
3252 | |
3253 | udf_func *tmp_udf=find_udf(u_d->name.str,u_d->name.length,1); |
3254 | |
3255 | if (!tmp_udf) |
3256 | { |
3257 | my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str); |
3258 | DBUG_RETURN(TRUE); |
3259 | } |
3260 | u_d=tmp_udf; |
3261 | args=arguments; |
3262 | |
3263 | /* Fix all arguments */ |
3264 | func->maybe_null=0; |
3265 | func->used_tables_and_const_cache_init(); |
3266 | |
3267 | if ((f_args.arg_count=arg_count)) |
3268 | { |
3269 | if (!(f_args.arg_type= (Item_result*) |
3270 | thd->alloc(f_args.arg_count*sizeof(Item_result)))) |
3271 | |
3272 | { |
3273 | free_udf(u_d); |
3274 | DBUG_RETURN(TRUE); |
3275 | } |
3276 | uint i; |
3277 | Item **arg,**arg_end; |
3278 | for (i=0, arg=arguments, arg_end=arguments+arg_count; |
3279 | arg != arg_end ; |
3280 | arg++,i++) |
3281 | { |
3282 | if (!(*arg)->fixed && |
3283 | (*arg)->fix_fields(thd, arg)) |
3284 | DBUG_RETURN(1); |
3285 | // we can't assign 'item' before, because fix_fields() can change arg |
3286 | Item *item= *arg; |
3287 | if (item->check_cols(1)) |
3288 | DBUG_RETURN(TRUE); |
3289 | /* |
3290 | TODO: We should think about this. It is not always |
3291 | right way just to set an UDF result to return my_charset_bin |
3292 | if one argument has binary sorting order. |
3293 | The result collation should be calculated according to arguments |
3294 | derivations in some cases and should not in other cases. |
3295 | Moreover, some arguments can represent a numeric input |
3296 | which doesn't effect the result character set and collation. |
3297 | There is no a general rule for UDF. Everything depends on |
3298 | the particular user defined function. |
3299 | */ |
3300 | if (item->collation.collation->state & MY_CS_BINSORT) |
3301 | func->collation.set(&my_charset_bin); |
3302 | if (item->maybe_null) |
3303 | func->maybe_null=1; |
3304 | func->with_sum_func= func->with_sum_func || item->with_sum_func; |
3305 | func->with_field= func->with_field || item->with_field; |
3306 | func->with_param= func->with_param || item->with_param; |
3307 | func->With_subquery_cache::join(item); |
3308 | func->used_tables_and_const_cache_join(item); |
3309 | f_args.arg_type[i]=item->result_type(); |
3310 | } |
3311 | if (!(buffers=new (thd->mem_root) String[arg_count]) || |
3312 | !multi_alloc_root(thd->mem_root, |
3313 | &f_args.args, arg_count * sizeof(char *), |
3314 | &f_args.lengths, arg_count * sizeof(long), |
3315 | &f_args.maybe_null, arg_count * sizeof(char), |
3316 | &num_buffer, arg_count * sizeof(double), |
3317 | &f_args.attributes, arg_count * sizeof(char *), |
3318 | &f_args.attribute_lengths, arg_count * sizeof(long), |
3319 | NullS)) |
3320 | { |
3321 | free_udf(u_d); |
3322 | DBUG_RETURN(TRUE); |
3323 | } |
3324 | } |
3325 | func->fix_length_and_dec(); |
3326 | initid.max_length=func->max_length; |
3327 | initid.maybe_null=func->maybe_null; |
3328 | initid.const_item=func->const_item_cache; |
3329 | initid.decimals=func->decimals; |
3330 | initid.ptr=0; |
3331 | for (uint i1= 0 ; i1 < arg_count ; i1++) |
3332 | buffers[i1].set_thread_specific(); |
3333 | |
3334 | if (u_d->func_init) |
3335 | { |
3336 | char init_msg_buff[MYSQL_ERRMSG_SIZE]; |
3337 | char *to=num_buffer; |
3338 | for (uint i=0; i < arg_count; i++) |
3339 | { |
3340 | /* |
3341 | For a constant argument i, args->args[i] points to the argument value. |
3342 | For non-constant, args->args[i] is NULL. |
3343 | */ |
3344 | f_args.args[i]= NULL; /* Non-const unless updated below. */ |
3345 | |
3346 | f_args.lengths[i]= arguments[i]->max_length; |
3347 | f_args.maybe_null[i]= (char) arguments[i]->maybe_null; |
3348 | f_args.attributes[i]= arguments[i]->name.str; |
3349 | f_args.attribute_lengths[i]= (ulong)arguments[i]->name.length; |
3350 | |
3351 | if (arguments[i]->const_item()) |
3352 | { |
3353 | switch (arguments[i]->result_type()) { |
3354 | case STRING_RESULT: |
3355 | case DECIMAL_RESULT: |
3356 | { |
3357 | String *res= arguments[i]->val_str(&buffers[i]); |
3358 | if (arguments[i]->null_value) |
3359 | continue; |
3360 | f_args.args[i]= (char*) res->c_ptr_safe(); |
3361 | f_args.lengths[i]= res->length(); |
3362 | break; |
3363 | } |
3364 | case INT_RESULT: |
3365 | *((longlong*) to)= arguments[i]->val_int(); |
3366 | if (arguments[i]->null_value) |
3367 | continue; |
3368 | f_args.args[i]= to; |
3369 | to+= ALIGN_SIZE(sizeof(longlong)); |
3370 | break; |
3371 | case REAL_RESULT: |
3372 | *((double*) to)= arguments[i]->val_real(); |
3373 | if (arguments[i]->null_value) |
3374 | continue; |
3375 | f_args.args[i]= to; |
3376 | to+= ALIGN_SIZE(sizeof(double)); |
3377 | break; |
3378 | case ROW_RESULT: |
3379 | case TIME_RESULT: |
3380 | DBUG_ASSERT(0); // This case should never be chosen |
3381 | break; |
3382 | } |
3383 | } |
3384 | } |
3385 | Udf_func_init init= u_d->func_init; |
3386 | if (unlikely((error=(uchar) init(&initid, &f_args, init_msg_buff)))) |
3387 | { |
3388 | my_error(ER_CANT_INITIALIZE_UDF, MYF(0), |
3389 | u_d->name.str, init_msg_buff); |
3390 | free_udf(u_d); |
3391 | DBUG_RETURN(TRUE); |
3392 | } |
3393 | func->max_length=MY_MIN(initid.max_length,MAX_BLOB_WIDTH); |
3394 | func->maybe_null=initid.maybe_null; |
3395 | /* |
3396 | The above call for init() can reset initid.const_item to "false", |
3397 | e.g. when the UDF function wants to be non-deterministic. |
3398 | See sequence_init() in udf_example.cc. |
3399 | */ |
3400 | func->const_item_cache= initid.const_item; |
3401 | func->decimals=MY_MIN(initid.decimals,NOT_FIXED_DEC); |
3402 | } |
3403 | initialized=1; |
3404 | if (unlikely(error)) |
3405 | { |
3406 | my_error(ER_CANT_INITIALIZE_UDF, MYF(0), |
3407 | u_d->name.str, ER_THD(thd, ER_UNKNOWN_ERROR)); |
3408 | DBUG_RETURN(TRUE); |
3409 | } |
3410 | DBUG_RETURN(FALSE); |
3411 | } |
3412 | |
3413 | |
3414 | bool udf_handler::get_arguments() |
3415 | { |
3416 | if (unlikely(error)) |
3417 | return 1; // Got an error earlier |
3418 | char *to= num_buffer; |
3419 | uint str_count=0; |
3420 | for (uint i=0; i < f_args.arg_count; i++) |
3421 | { |
3422 | f_args.args[i]=0; |
3423 | switch (f_args.arg_type[i]) { |
3424 | case STRING_RESULT: |
3425 | case DECIMAL_RESULT: |
3426 | { |
3427 | String *res=args[i]->val_str(&buffers[str_count++]); |
3428 | if (!(args[i]->null_value)) |
3429 | { |
3430 | f_args.args[i]= (char*) res->ptr(); |
3431 | f_args.lengths[i]= res->length(); |
3432 | } |
3433 | else |
3434 | { |
3435 | f_args.lengths[i]= 0; |
3436 | } |
3437 | break; |
3438 | } |
3439 | case INT_RESULT: |
3440 | *((longlong*) to) = args[i]->val_int(); |
3441 | if (!args[i]->null_value) |
3442 | { |
3443 | f_args.args[i]=to; |
3444 | to+= ALIGN_SIZE(sizeof(longlong)); |
3445 | } |
3446 | break; |
3447 | case REAL_RESULT: |
3448 | *((double*) to)= args[i]->val_real(); |
3449 | if (!args[i]->null_value) |
3450 | { |
3451 | f_args.args[i]=to; |
3452 | to+= ALIGN_SIZE(sizeof(double)); |
3453 | } |
3454 | break; |
3455 | case ROW_RESULT: |
3456 | case TIME_RESULT: |
3457 | DBUG_ASSERT(0); // This case should never be chosen |
3458 | break; |
3459 | } |
3460 | } |
3461 | return 0; |
3462 | } |
3463 | |
3464 | /** |
3465 | @return |
3466 | (String*)NULL in case of NULL values |
3467 | */ |
3468 | String *udf_handler::val_str(String *str,String *save_str) |
3469 | { |
3470 | uchar is_null_tmp=0; |
3471 | ulong res_length; |
3472 | DBUG_ENTER("udf_handler::val_str" ); |
3473 | |
3474 | if (get_arguments()) |
3475 | DBUG_RETURN(0); |
3476 | char * (*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)= |
3477 | (char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)) |
3478 | u_d->func; |
3479 | |
3480 | if ((res_length=str->alloced_length()) < MAX_FIELD_WIDTH) |
3481 | { // This happens VERY seldom |
3482 | if (str->alloc(MAX_FIELD_WIDTH)) |
3483 | { |
3484 | error=1; |
3485 | DBUG_RETURN(0); |
3486 | } |
3487 | } |
3488 | char *res=func(&initid, &f_args, (char*) str->ptr(), &res_length, |
3489 | &is_null_tmp, &error); |
3490 | DBUG_PRINT("info" , ("udf func returned, res_length: %lu" , res_length)); |
3491 | if (is_null_tmp || !res || unlikely(error)) // The !res is for safety |
3492 | { |
3493 | DBUG_PRINT("info" , ("Null or error" )); |
3494 | DBUG_RETURN(0); |
3495 | } |
3496 | if (res == str->ptr()) |
3497 | { |
3498 | str->length(res_length); |
3499 | DBUG_PRINT("exit" , ("str: %*.s" , (int) str->length(), str->ptr())); |
3500 | DBUG_RETURN(str); |
3501 | } |
3502 | save_str->set(res, res_length, str->charset()); |
3503 | DBUG_PRINT("exit" , ("save_str: %s" , save_str->ptr())); |
3504 | DBUG_RETURN(save_str); |
3505 | } |
3506 | |
3507 | |
3508 | /* |
3509 | For the moment, UDF functions are returning DECIMAL values as strings |
3510 | */ |
3511 | |
3512 | my_decimal *udf_handler::val_decimal(my_bool *null_value, my_decimal *dec_buf) |
3513 | { |
3514 | char buf[DECIMAL_MAX_STR_LENGTH+1], *end; |
3515 | ulong res_length= DECIMAL_MAX_STR_LENGTH; |
3516 | |
3517 | if (get_arguments()) |
3518 | { |
3519 | *null_value=1; |
3520 | return 0; |
3521 | } |
3522 | char *(*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)= |
3523 | (char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)) |
3524 | u_d->func; |
3525 | |
3526 | char *res= func(&initid, &f_args, buf, &res_length, &is_null, &error); |
3527 | if (is_null || unlikely(error)) |
3528 | { |
3529 | *null_value= 1; |
3530 | return 0; |
3531 | } |
3532 | end= res+ res_length; |
3533 | str2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf, &end); |
3534 | return dec_buf; |
3535 | } |
3536 | |
3537 | |
3538 | void Item_udf_func::cleanup() |
3539 | { |
3540 | udf.cleanup(); |
3541 | Item_func::cleanup(); |
3542 | } |
3543 | |
3544 | |
3545 | void Item_udf_func::print(String *str, enum_query_type query_type) |
3546 | { |
3547 | str->append(func_name()); |
3548 | str->append('('); |
3549 | for (uint i=0 ; i < arg_count ; i++) |
3550 | { |
3551 | if (i != 0) |
3552 | str->append(','); |
3553 | args[i]->print_item_w_name(str, query_type); |
3554 | } |
3555 | str->append(')'); |
3556 | } |
3557 | |
3558 | |
3559 | double Item_func_udf_float::val_real() |
3560 | { |
3561 | double res; |
3562 | my_bool tmp_null_value; |
3563 | DBUG_ASSERT(fixed == 1); |
3564 | DBUG_ENTER("Item_func_udf_float::val" ); |
3565 | DBUG_PRINT("info" ,("result_type: %d arg_count: %d" , |
3566 | args[0]->result_type(), arg_count)); |
3567 | res= udf.val(&tmp_null_value); |
3568 | null_value= tmp_null_value; |
3569 | DBUG_RETURN(res); |
3570 | } |
3571 | |
3572 | |
3573 | String *Item_func_udf_float::val_str(String *str) |
3574 | { |
3575 | DBUG_ASSERT(fixed == 1); |
3576 | double nr= val_real(); |
3577 | if (null_value) |
3578 | return 0; /* purecov: inspected */ |
3579 | str->set_real(nr,decimals,&my_charset_bin); |
3580 | return str; |
3581 | } |
3582 | |
3583 | |
3584 | longlong Item_func_udf_int::val_int() |
3585 | { |
3586 | longlong res; |
3587 | my_bool tmp_null_value; |
3588 | DBUG_ASSERT(fixed == 1); |
3589 | DBUG_ENTER("Item_func_udf_int::val_int" ); |
3590 | res= udf.val_int(&tmp_null_value); |
3591 | null_value= tmp_null_value; |
3592 | DBUG_RETURN(res); |
3593 | } |
3594 | |
3595 | |
3596 | String *Item_func_udf_int::val_str(String *str) |
3597 | { |
3598 | DBUG_ASSERT(fixed == 1); |
3599 | longlong nr=val_int(); |
3600 | if (null_value) |
3601 | return 0; |
3602 | str->set_int(nr, unsigned_flag, &my_charset_bin); |
3603 | return str; |
3604 | } |
3605 | |
3606 | |
3607 | longlong Item_func_udf_decimal::val_int() |
3608 | { |
3609 | my_bool tmp_null_value; |
3610 | longlong result; |
3611 | my_decimal dec_buf, *dec= udf.val_decimal(&tmp_null_value, &dec_buf); |
3612 | null_value= tmp_null_value; |
3613 | if (null_value) |
3614 | return 0; |
3615 | my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result); |
3616 | return result; |
3617 | } |
3618 | |
3619 | |
3620 | double Item_func_udf_decimal::val_real() |
3621 | { |
3622 | my_bool tmp_null_value; |
3623 | double result; |
3624 | my_decimal dec_buf, *dec= udf.val_decimal(&tmp_null_value, &dec_buf); |
3625 | null_value= tmp_null_value; |
3626 | if (null_value) |
3627 | return 0.0; |
3628 | my_decimal2double(E_DEC_FATAL_ERROR, dec, &result); |
3629 | return result; |
3630 | } |
3631 | |
3632 | |
3633 | my_decimal *Item_func_udf_decimal::val_decimal(my_decimal *dec_buf) |
3634 | { |
3635 | my_decimal *res; |
3636 | my_bool tmp_null_value; |
3637 | DBUG_ASSERT(fixed == 1); |
3638 | DBUG_ENTER("Item_func_udf_decimal::val_decimal" ); |
3639 | DBUG_PRINT("info" ,("result_type: %d arg_count: %d" , |
3640 | args[0]->result_type(), arg_count)); |
3641 | |
3642 | res= udf.val_decimal(&tmp_null_value, dec_buf); |
3643 | null_value= tmp_null_value; |
3644 | DBUG_RETURN(res); |
3645 | } |
3646 | |
3647 | |
3648 | String *Item_func_udf_decimal::val_str(String *str) |
3649 | { |
3650 | my_bool tmp_null_value; |
3651 | my_decimal dec_buf, *dec= udf.val_decimal(&tmp_null_value, &dec_buf); |
3652 | null_value= tmp_null_value; |
3653 | if (null_value) |
3654 | return 0; |
3655 | if (str->length() < DECIMAL_MAX_STR_LENGTH) |
3656 | str->length(DECIMAL_MAX_STR_LENGTH); |
3657 | my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, FALSE, &dec_buf); |
3658 | my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, '0', str); |
3659 | return str; |
3660 | } |
3661 | |
3662 | |
3663 | /* Default max_length is max argument length */ |
3664 | |
3665 | void Item_func_udf_str::fix_length_and_dec() |
3666 | { |
3667 | DBUG_ENTER("Item_func_udf_str::fix_length_and_dec" ); |
3668 | max_length=0; |
3669 | for (uint i = 0; i < arg_count; i++) |
3670 | set_if_bigger(max_length,args[i]->max_length); |
3671 | DBUG_VOID_RETURN; |
3672 | } |
3673 | |
3674 | String *Item_func_udf_str::val_str(String *str) |
3675 | { |
3676 | DBUG_ASSERT(fixed == 1); |
3677 | String *res=udf.val_str(str,&str_value); |
3678 | null_value = !res; |
3679 | return res; |
3680 | } |
3681 | |
3682 | |
3683 | /** |
3684 | @note |
3685 | This has to come last in the udf_handler methods, or C for AIX |
3686 | version 6.0.0.0 fails to compile with debugging enabled. (Yes, really.) |
3687 | */ |
3688 | |
3689 | udf_handler::~udf_handler() |
3690 | { |
3691 | /* Everything should be properly cleaned up by this moment. */ |
3692 | DBUG_ASSERT(not_original || !(initialized || buffers)); |
3693 | } |
3694 | |
3695 | #else |
3696 | bool udf_handler::get_arguments() { return 0; } |
3697 | #endif /* HAVE_DLOPEN */ |
3698 | |
3699 | |
3700 | longlong Item_master_pos_wait::val_int() |
3701 | { |
3702 | DBUG_ASSERT(fixed == 1); |
3703 | THD* thd = current_thd; |
3704 | String *log_name = args[0]->val_str(&value); |
3705 | int event_count= 0; |
3706 | |
3707 | null_value=0; |
3708 | if (thd->slave_thread || !log_name || !log_name->length()) |
3709 | { |
3710 | null_value = 1; |
3711 | return 0; |
3712 | } |
3713 | #ifdef HAVE_REPLICATION |
3714 | longlong pos = (ulong)args[1]->val_int(); |
3715 | longlong timeout = (arg_count>=3) ? args[2]->val_int() : 0 ; |
3716 | String connection_name_buff; |
3717 | LEX_CSTRING connection_name; |
3718 | Master_info *mi= NULL; |
3719 | if (arg_count >= 4) |
3720 | { |
3721 | String *con; |
3722 | if (!(con= args[3]->val_str(&connection_name_buff))) |
3723 | goto err; |
3724 | |
3725 | connection_name.str= con->ptr(); |
3726 | connection_name.length= con->length(); |
3727 | if (check_master_connection_name(&connection_name)) |
3728 | { |
3729 | my_error(ER_WRONG_ARGUMENTS, MYF(ME_JUST_WARNING), |
3730 | "MASTER_CONNECTION_NAME" ); |
3731 | goto err; |
3732 | } |
3733 | } |
3734 | else |
3735 | connection_name= thd->variables.default_master_connection; |
3736 | |
3737 | if (!(mi= get_master_info(&connection_name, Sql_condition::WARN_LEVEL_WARN))) |
3738 | goto err; |
3739 | |
3740 | if ((event_count = mi->rli.wait_for_pos(thd, log_name, pos, timeout)) == -2) |
3741 | { |
3742 | null_value = 1; |
3743 | event_count=0; |
3744 | } |
3745 | mi->release(); |
3746 | #endif |
3747 | return event_count; |
3748 | |
3749 | #ifdef HAVE_REPLICATION |
3750 | err: |
3751 | { |
3752 | null_value = 1; |
3753 | return 0; |
3754 | } |
3755 | #endif |
3756 | } |
3757 | |
3758 | |
3759 | longlong Item_master_gtid_wait::val_int() |
3760 | { |
3761 | DBUG_ASSERT(fixed == 1); |
3762 | longlong result= 0; |
3763 | String *gtid_pos __attribute__((unused)) = args[0]->val_str(&value); |
3764 | |
3765 | if (args[0]->null_value) |
3766 | { |
3767 | null_value= 1; |
3768 | return 0; |
3769 | } |
3770 | |
3771 | null_value=0; |
3772 | #ifdef HAVE_REPLICATION |
3773 | THD* thd= current_thd; |
3774 | longlong timeout_us; |
3775 | |
3776 | if (arg_count==2 && !args[1]->null_value) |
3777 | timeout_us= (longlong)(1e6*args[1]->val_real()); |
3778 | else |
3779 | timeout_us= (longlong)-1; |
3780 | |
3781 | result= rpl_global_gtid_waiting.wait_for_pos(thd, gtid_pos, timeout_us); |
3782 | #else |
3783 | null_value= 0; |
3784 | #endif /* REPLICATION */ |
3785 | return result; |
3786 | } |
3787 | |
3788 | |
3789 | /** |
3790 | Enables a session to wait on a condition until a timeout or a network |
3791 | disconnect occurs. |
3792 | |
3793 | @remark The connection is polled every m_interrupt_interval nanoseconds. |
3794 | */ |
3795 | |
3796 | class Interruptible_wait |
3797 | { |
3798 | THD *m_thd; |
3799 | struct timespec m_abs_timeout; |
3800 | static const ulonglong m_interrupt_interval; |
3801 | |
3802 | public: |
3803 | Interruptible_wait(THD *thd) |
3804 | : m_thd(thd) {} |
3805 | |
3806 | ~Interruptible_wait() {} |
3807 | |
3808 | public: |
3809 | /** |
3810 | Set the absolute timeout. |
3811 | |
3812 | @param timeout The amount of time in nanoseconds to wait |
3813 | */ |
3814 | void set_timeout(ulonglong timeout) |
3815 | { |
3816 | /* |
3817 | Calculate the absolute system time at the start so it can |
3818 | be controlled in slices. It relies on the fact that once |
3819 | the absolute time passes, the timed wait call will fail |
3820 | automatically with a timeout error. |
3821 | */ |
3822 | set_timespec_nsec(m_abs_timeout, timeout); |
3823 | } |
3824 | |
3825 | /** The timed wait. */ |
3826 | int wait(mysql_cond_t *, mysql_mutex_t *); |
3827 | }; |
3828 | |
3829 | |
3830 | /** Time to wait before polling the connection status. */ |
3831 | const ulonglong Interruptible_wait::m_interrupt_interval= 5 * 1000000000ULL; |
3832 | |
3833 | |
3834 | /** |
3835 | Wait for a given condition to be signaled. |
3836 | |
3837 | @param cond The condition variable to wait on. |
3838 | @param mutex The associated mutex. |
3839 | |
3840 | @remark The absolute timeout is preserved across calls. |
3841 | |
3842 | @retval return value from mysql_cond_timedwait |
3843 | */ |
3844 | |
3845 | int Interruptible_wait::wait(mysql_cond_t *cond, mysql_mutex_t *mutex) |
3846 | { |
3847 | int error; |
3848 | struct timespec timeout; |
3849 | |
3850 | while (1) |
3851 | { |
3852 | /* Wait for a fixed interval. */ |
3853 | set_timespec_nsec(timeout, m_interrupt_interval); |
3854 | |
3855 | /* But only if not past the absolute timeout. */ |
3856 | if (cmp_timespec(timeout, m_abs_timeout) > 0) |
3857 | timeout= m_abs_timeout; |
3858 | |
3859 | error= mysql_cond_timedwait(cond, mutex, &timeout); |
3860 | if (error == ETIMEDOUT || error == ETIME) |
3861 | { |
3862 | /* Return error if timed out or connection is broken. */ |
3863 | if (!cmp_timespec(timeout, m_abs_timeout) || !m_thd->is_connected()) |
3864 | break; |
3865 | } |
3866 | /* Otherwise, propagate status to the caller. */ |
3867 | else |
3868 | break; |
3869 | } |
3870 | |
3871 | return error; |
3872 | } |
3873 | |
3874 | |
3875 | /** |
3876 | For locks with EXPLICIT duration, MDL returns a new ticket |
3877 | every time a lock is granted. This allows to implement recursive |
3878 | locks without extra allocation or additional data structures, such |
3879 | as below. However, if there are too many tickets in the same |
3880 | MDL_context, MDL_context::find_ticket() is getting too slow, |
3881 | since it's using a linear search. |
3882 | This is why a separate structure is allocated for a user |
3883 | level lock, and before requesting a new lock from MDL, |
3884 | GET_LOCK() checks thd->ull_hash if such lock is already granted, |
3885 | and if so, simply increments a reference counter. |
3886 | */ |
3887 | |
3888 | class User_level_lock |
3889 | { |
3890 | public: |
3891 | MDL_ticket *lock; |
3892 | int refs; |
3893 | }; |
3894 | |
3895 | |
3896 | /** Extract a hash key from User_level_lock. */ |
3897 | |
3898 | uchar *ull_get_key(const uchar *ptr, size_t *length, |
3899 | my_bool not_used __attribute__((unused))) |
3900 | { |
3901 | User_level_lock *ull = (User_level_lock*) ptr; |
3902 | MDL_key *key = ull->lock->get_key(); |
3903 | *length= key->length(); |
3904 | return (uchar*) key->ptr(); |
3905 | } |
3906 | |
3907 | |
3908 | /** |
3909 | Release all user level locks for this THD. |
3910 | */ |
3911 | |
3912 | void mysql_ull_cleanup(THD *thd) |
3913 | { |
3914 | User_level_lock *ull; |
3915 | DBUG_ENTER("mysql_ull_cleanup" ); |
3916 | |
3917 | for (uint i= 0; i < thd->ull_hash.records; i++) |
3918 | { |
3919 | ull = (User_level_lock*) my_hash_element(&thd->ull_hash, i); |
3920 | thd->mdl_context.release_lock(ull->lock); |
3921 | my_free(ull); |
3922 | } |
3923 | |
3924 | my_hash_free(&thd->ull_hash); |
3925 | |
3926 | DBUG_VOID_RETURN; |
3927 | } |
3928 | |
3929 | |
3930 | /** |
3931 | Set explicit duration for metadata locks corresponding to |
3932 | user level locks to protect them from being released at the end |
3933 | of transaction. |
3934 | */ |
3935 | |
3936 | void mysql_ull_set_explicit_lock_duration(THD *thd) |
3937 | { |
3938 | User_level_lock *ull; |
3939 | DBUG_ENTER("mysql_ull_set_explicit_lock_duration" ); |
3940 | |
3941 | for (uint i= 0; i < thd->ull_hash.records; i++) |
3942 | { |
3943 | ull= (User_level_lock*) my_hash_element(&thd->ull_hash, i); |
3944 | thd->mdl_context.set_lock_duration(ull->lock, MDL_EXPLICIT); |
3945 | } |
3946 | DBUG_VOID_RETURN; |
3947 | } |
3948 | |
3949 | |
3950 | /** |
3951 | When MDL detects a lock wait timeout, it pushes |
3952 | an error into the statement diagnostics area. |
3953 | For GET_LOCK(), lock wait timeout is not an error, |
3954 | but a special return value (0). |
3955 | Similarly, killing get_lock wait is not an error either, |
3956 | but a return value NULL. |
3957 | Capture and suppress lock wait timeouts and kills. |
3958 | */ |
3959 | |
3960 | class Lock_wait_timeout_handler: public Internal_error_handler |
3961 | { |
3962 | public: |
3963 | Lock_wait_timeout_handler() :m_lock_wait_timeout(false) {} |
3964 | |
3965 | bool m_lock_wait_timeout; |
3966 | |
3967 | bool handle_condition(THD * /* thd */, uint sql_errno, |
3968 | const char * /* sqlstate */, |
3969 | Sql_condition::enum_warning_level* /* level */, |
3970 | const char *message, |
3971 | Sql_condition ** /* cond_hdl */); |
3972 | }; |
3973 | |
3974 | bool |
3975 | Lock_wait_timeout_handler:: |
3976 | handle_condition(THD *thd, uint sql_errno, |
3977 | const char * /* sqlstate */, |
3978 | Sql_condition::enum_warning_level* /* level */, |
3979 | const char *message, |
3980 | Sql_condition ** /* cond_hdl */) |
3981 | { |
3982 | if (sql_errno == ER_LOCK_WAIT_TIMEOUT) |
3983 | { |
3984 | m_lock_wait_timeout= true; |
3985 | return true; /* condition handled */ |
3986 | } |
3987 | if (thd->is_killed()) |
3988 | return true; |
3989 | |
3990 | return false; |
3991 | } |
3992 | |
3993 | |
3994 | static int ull_name_ok(String *name) |
3995 | { |
3996 | if (!name || !name->length()) |
3997 | return 0; |
3998 | |
3999 | if (name->length() > NAME_LEN) |
4000 | { |
4001 | my_error(ER_TOO_LONG_IDENT, MYF(0), name->c_ptr_safe()); |
4002 | return 0; |
4003 | } |
4004 | return 1; |
4005 | } |
4006 | |
4007 | |
4008 | /** |
4009 | Get a user level lock. |
4010 | |
4011 | @retval |
4012 | 1 : Got lock |
4013 | @retval |
4014 | 0 : Timeout |
4015 | @retval |
4016 | NULL : Error |
4017 | */ |
4018 | |
4019 | longlong Item_func_get_lock::val_int() |
4020 | { |
4021 | DBUG_ASSERT(fixed == 1); |
4022 | String *res= args[0]->val_str(&value); |
4023 | double timeout= args[1]->val_real(); |
4024 | THD *thd= current_thd; |
4025 | User_level_lock *ull; |
4026 | DBUG_ENTER("Item_func_get_lock::val_int" ); |
4027 | |
4028 | null_value= 1; |
4029 | /* |
4030 | In slave thread no need to get locks, everything is serialized. Anyway |
4031 | there is no way to make GET_LOCK() work on slave like it did on master |
4032 | (i.e. make it return exactly the same value) because we don't have the |
4033 | same other concurrent threads environment. No matter what we return here, |
4034 | it's not guaranteed to be same as on master. |
4035 | */ |
4036 | if (thd->slave_thread) |
4037 | { |
4038 | null_value= 0; |
4039 | DBUG_RETURN(1); |
4040 | } |
4041 | |
4042 | if (args[1]->null_value || |
4043 | (!args[1]->unsigned_flag && ((longlong) timeout < 0))) |
4044 | { |
4045 | char buf[22]; |
4046 | if (args[1]->null_value) |
4047 | strmov(buf, "NULL" ); |
4048 | else |
4049 | llstr(((longlong) timeout), buf); |
4050 | push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, |
4051 | ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE), |
4052 | "timeout" , buf, "get_lock" ); |
4053 | null_value= 1; |
4054 | DBUG_RETURN(0); |
4055 | } |
4056 | |
4057 | if (!ull_name_ok(res)) |
4058 | DBUG_RETURN(0); |
4059 | DBUG_PRINT("enter" , ("lock: %.*s" , res->length(), res->ptr())); |
4060 | /* HASH entries are of type User_level_lock. */ |
4061 | if (! my_hash_inited(&thd->ull_hash) && |
4062 | my_hash_init(&thd->ull_hash, &my_charset_bin, |
4063 | 16 /* small hash */, 0, 0, ull_get_key, NULL, 0)) |
4064 | { |
4065 | DBUG_RETURN(0); |
4066 | } |
4067 | |
4068 | MDL_request ull_request; |
4069 | ull_request.init(MDL_key::USER_LOCK, res->c_ptr_safe(), "" , |
4070 | MDL_SHARED_NO_WRITE, MDL_EXPLICIT); |
4071 | MDL_key *ull_key = &ull_request.key; |
4072 | |
4073 | |
4074 | if ((ull= (User_level_lock*) |
4075 | my_hash_search(&thd->ull_hash, ull_key->ptr(), ull_key->length()))) |
4076 | { |
4077 | /* Recursive lock */ |
4078 | ull->refs++; |
4079 | null_value = 0; |
4080 | DBUG_PRINT("info" , ("recursive lock, ref-count: %d" , (int) ull->refs)); |
4081 | DBUG_RETURN(1); |
4082 | } |
4083 | |
4084 | Lock_wait_timeout_handler lock_wait_timeout_handler; |
4085 | thd->push_internal_handler(&lock_wait_timeout_handler); |
4086 | bool error= thd->mdl_context.acquire_lock(&ull_request, timeout); |
4087 | (void) thd->pop_internal_handler(); |
4088 | if (unlikely(error)) |
4089 | { |
4090 | if (lock_wait_timeout_handler.m_lock_wait_timeout) |
4091 | null_value= 0; |
4092 | DBUG_RETURN(0); |
4093 | } |
4094 | |
4095 | ull= (User_level_lock*) my_malloc(sizeof(User_level_lock), |
4096 | MYF(MY_WME|MY_THREAD_SPECIFIC)); |
4097 | if (ull == NULL) |
4098 | { |
4099 | thd->mdl_context.release_lock(ull_request.ticket); |
4100 | DBUG_RETURN(0); |
4101 | } |
4102 | |
4103 | ull->lock= ull_request.ticket; |
4104 | ull->refs= 1; |
4105 | |
4106 | if (my_hash_insert(&thd->ull_hash, (uchar*) ull)) |
4107 | { |
4108 | thd->mdl_context.release_lock(ull->lock); |
4109 | my_free(ull); |
4110 | DBUG_RETURN(0); |
4111 | } |
4112 | null_value= 0; |
4113 | |
4114 | DBUG_RETURN(1); |
4115 | } |
4116 | |
4117 | |
4118 | /** |
4119 | Release a user level lock. |
4120 | @return |
4121 | - 1 if lock released |
4122 | - 0 if lock wasn't held |
4123 | - (SQL) NULL if no such lock |
4124 | */ |
4125 | |
4126 | longlong Item_func_release_lock::val_int() |
4127 | { |
4128 | DBUG_ASSERT(fixed == 1); |
4129 | String *res= args[0]->val_str(&value); |
4130 | THD *thd= current_thd; |
4131 | DBUG_ENTER("Item_func_release_lock::val_int" ); |
4132 | null_value= 1; |
4133 | |
4134 | if (!ull_name_ok(res)) |
4135 | DBUG_RETURN(0); |
4136 | |
4137 | DBUG_PRINT("enter" , ("lock: %.*s" , res->length(), res->ptr())); |
4138 | |
4139 | MDL_key ull_key; |
4140 | ull_key.mdl_key_init(MDL_key::USER_LOCK, res->c_ptr_safe(), "" ); |
4141 | |
4142 | User_level_lock *ull; |
4143 | |
4144 | if (!my_hash_inited(&thd->ull_hash) || |
4145 | !(ull= |
4146 | (User_level_lock*) my_hash_search(&thd->ull_hash, |
4147 | ull_key.ptr(), ull_key.length()))) |
4148 | { |
4149 | null_value= thd->mdl_context.get_lock_owner(&ull_key) == 0; |
4150 | DBUG_RETURN(0); |
4151 | } |
4152 | DBUG_PRINT("info" , ("ref count: %d" , (int) ull->refs)); |
4153 | null_value= 0; |
4154 | if (--ull->refs == 0) |
4155 | { |
4156 | my_hash_delete(&thd->ull_hash, (uchar*) ull); |
4157 | thd->mdl_context.release_lock(ull->lock); |
4158 | my_free(ull); |
4159 | } |
4160 | DBUG_RETURN(1); |
4161 | } |
4162 | |
4163 | |
4164 | /** |
4165 | Check a user level lock. |
4166 | |
4167 | Sets null_value=TRUE on error. |
4168 | |
4169 | @retval |
4170 | 1 Available |
4171 | @retval |
4172 | 0 Already taken, or error |
4173 | */ |
4174 | |
4175 | longlong Item_func_is_free_lock::val_int() |
4176 | { |
4177 | DBUG_ASSERT(fixed == 1); |
4178 | String *res= args[0]->val_str(&value); |
4179 | THD *thd= current_thd; |
4180 | null_value= 1; |
4181 | |
4182 | if (!ull_name_ok(res)) |
4183 | return 0; |
4184 | |
4185 | MDL_key ull_key; |
4186 | ull_key.mdl_key_init(MDL_key::USER_LOCK, res->c_ptr_safe(), "" ); |
4187 | |
4188 | null_value= 0; |
4189 | return thd->mdl_context.get_lock_owner(&ull_key) == 0; |
4190 | } |
4191 | |
4192 | |
4193 | longlong Item_func_is_used_lock::val_int() |
4194 | { |
4195 | DBUG_ASSERT(fixed == 1); |
4196 | String *res= args[0]->val_str(&value); |
4197 | THD *thd= current_thd; |
4198 | null_value= 1; |
4199 | |
4200 | if (!ull_name_ok(res)) |
4201 | return 0; |
4202 | |
4203 | MDL_key ull_key; |
4204 | ull_key.mdl_key_init(MDL_key::USER_LOCK, res->c_ptr_safe(), "" ); |
4205 | ulong thread_id = thd->mdl_context.get_lock_owner(&ull_key); |
4206 | if (thread_id == 0) |
4207 | return 0; |
4208 | |
4209 | null_value= 0; |
4210 | return thread_id; |
4211 | } |
4212 | |
4213 | |
4214 | longlong Item_func_last_insert_id::val_int() |
4215 | { |
4216 | THD *thd= current_thd; |
4217 | DBUG_ASSERT(fixed == 1); |
4218 | if (arg_count) |
4219 | { |
4220 | longlong value= args[0]->val_int(); |
4221 | null_value= args[0]->null_value; |
4222 | /* |
4223 | LAST_INSERT_ID(X) must affect the client's mysql_insert_id() as |
4224 | documented in the manual. We don't want to touch |
4225 | first_successful_insert_id_in_cur_stmt because it would make |
4226 | LAST_INSERT_ID(X) take precedence over an generated auto_increment |
4227 | value for this row. |
4228 | */ |
4229 | thd->arg_of_last_insert_id_function= TRUE; |
4230 | thd->first_successful_insert_id_in_prev_stmt= value; |
4231 | return value; |
4232 | } |
4233 | return |
4234 | static_cast<longlong>(thd->read_first_successful_insert_id_in_prev_stmt()); |
4235 | } |
4236 | |
4237 | |
4238 | bool Item_func_last_insert_id::fix_fields(THD *thd, Item **ref) |
4239 | { |
4240 | thd->lex->uncacheable(UNCACHEABLE_SIDEEFFECT); |
4241 | return Item_int_func::fix_fields(thd, ref); |
4242 | } |
4243 | |
4244 | |
4245 | /* This function is just used to test speed of different functions */ |
4246 | |
4247 | longlong Item_func_benchmark::val_int() |
4248 | { |
4249 | DBUG_ASSERT(fixed == 1); |
4250 | char buff[MAX_FIELD_WIDTH]; |
4251 | String tmp(buff,sizeof(buff), &my_charset_bin); |
4252 | my_decimal tmp_decimal; |
4253 | THD *thd= current_thd; |
4254 | ulonglong loop_count; |
4255 | |
4256 | loop_count= (ulonglong) args[0]->val_int(); |
4257 | |
4258 | if (args[0]->null_value || |
4259 | (!args[0]->unsigned_flag && (((longlong) loop_count) < 0))) |
4260 | { |
4261 | if (!args[0]->null_value) |
4262 | { |
4263 | char buff[22]; |
4264 | llstr(((longlong) loop_count), buff); |
4265 | push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, |
4266 | ER_WRONG_VALUE_FOR_TYPE, |
4267 | ER_THD(thd, ER_WRONG_VALUE_FOR_TYPE), |
4268 | "count" , buff, "benchmark" ); |
4269 | } |
4270 | |
4271 | null_value= 1; |
4272 | return 0; |
4273 | } |
4274 | |
4275 | null_value=0; |
4276 | for (ulonglong loop=0 ; loop < loop_count && !thd->killed; loop++) |
4277 | { |
4278 | switch (args[1]->result_type()) { |
4279 | case REAL_RESULT: |
4280 | (void) args[1]->val_real(); |
4281 | break; |
4282 | case INT_RESULT: |
4283 | (void) args[1]->val_int(); |
4284 | break; |
4285 | case STRING_RESULT: |
4286 | (void) args[1]->val_str(&tmp); |
4287 | break; |
4288 | case DECIMAL_RESULT: |
4289 | (void) args[1]->val_decimal(&tmp_decimal); |
4290 | break; |
4291 | case ROW_RESULT: |
4292 | case TIME_RESULT: |
4293 | DBUG_ASSERT(0); // This case should never be chosen |
4294 | return 0; |
4295 | } |
4296 | } |
4297 | return 0; |
4298 | } |
4299 | |
4300 | |
4301 | void Item_func_benchmark::print(String *str, enum_query_type query_type) |
4302 | { |
4303 | str->append(STRING_WITH_LEN("benchmark(" )); |
4304 | args[0]->print(str, query_type); |
4305 | str->append(','); |
4306 | args[1]->print(str, query_type); |
4307 | str->append(')'); |
4308 | } |
4309 | |
4310 | |
4311 | mysql_mutex_t LOCK_item_func_sleep; |
4312 | |
4313 | #ifdef HAVE_PSI_INTERFACE |
4314 | static PSI_mutex_key key_LOCK_item_func_sleep; |
4315 | |
4316 | static PSI_mutex_info item_func_sleep_mutexes[]= |
4317 | { |
4318 | { &key_LOCK_item_func_sleep, "LOCK_user_locks" , PSI_FLAG_GLOBAL} |
4319 | }; |
4320 | |
4321 | |
4322 | static void init_item_func_sleep_psi_keys(void) |
4323 | { |
4324 | const char* category= "sql" ; |
4325 | int count; |
4326 | |
4327 | if (PSI_server == NULL) |
4328 | return; |
4329 | |
4330 | count= array_elements(item_func_sleep_mutexes); |
4331 | PSI_server->register_mutex(category, item_func_sleep_mutexes, count); |
4332 | } |
4333 | #endif |
4334 | |
4335 | static bool item_func_sleep_inited= 0; |
4336 | |
4337 | |
4338 | void item_func_sleep_init(void) |
4339 | { |
4340 | #ifdef HAVE_PSI_INTERFACE |
4341 | init_item_func_sleep_psi_keys(); |
4342 | #endif |
4343 | |
4344 | mysql_mutex_init(key_LOCK_item_func_sleep, &LOCK_item_func_sleep, MY_MUTEX_INIT_SLOW); |
4345 | item_func_sleep_inited= 1; |
4346 | } |
4347 | |
4348 | |
4349 | void item_func_sleep_free(void) |
4350 | { |
4351 | if (item_func_sleep_inited) |
4352 | { |
4353 | item_func_sleep_inited= 0; |
4354 | mysql_mutex_destroy(&LOCK_item_func_sleep); |
4355 | } |
4356 | } |
4357 | |
4358 | |
4359 | /** This function is just used to create tests with time gaps. */ |
4360 | |
4361 | longlong Item_func_sleep::val_int() |
4362 | { |
4363 | THD *thd= current_thd; |
4364 | Interruptible_wait timed_cond(thd); |
4365 | mysql_cond_t cond; |
4366 | double timeout; |
4367 | int error; |
4368 | |
4369 | DBUG_ASSERT(fixed == 1); |
4370 | |
4371 | timeout= args[0]->val_real(); |
4372 | /* |
4373 | On 64-bit OSX mysql_cond_timedwait() waits forever |
4374 | if passed abstime time has already been exceeded by |
4375 | the system time. |
4376 | When given a very short timeout (< 10 mcs) just return |
4377 | immediately. |
4378 | We assume that the lines between this test and the call |
4379 | to mysql_cond_timedwait() will be executed in less than 0.00001 sec. |
4380 | */ |
4381 | if (timeout < 0.00001) |
4382 | return 0; |
4383 | |
4384 | timed_cond.set_timeout((ulonglong) (timeout * 1000000000.0)); |
4385 | |
4386 | mysql_cond_init(key_item_func_sleep_cond, &cond, NULL); |
4387 | mysql_mutex_lock(&LOCK_item_func_sleep); |
4388 | |
4389 | THD_STAGE_INFO(thd, stage_user_sleep); |
4390 | thd->mysys_var->current_mutex= &LOCK_item_func_sleep; |
4391 | thd->mysys_var->current_cond= &cond; |
4392 | |
4393 | error= 0; |
4394 | thd_wait_begin(thd, THD_WAIT_SLEEP); |
4395 | while (!thd->killed) |
4396 | { |
4397 | error= timed_cond.wait(&cond, &LOCK_item_func_sleep); |
4398 | if (error == ETIMEDOUT || error == ETIME) |
4399 | break; |
4400 | error= 0; |
4401 | } |
4402 | thd_wait_end(thd); |
4403 | mysql_mutex_unlock(&LOCK_item_func_sleep); |
4404 | mysql_mutex_lock(&thd->mysys_var->mutex); |
4405 | thd->mysys_var->current_mutex= 0; |
4406 | thd->mysys_var->current_cond= 0; |
4407 | mysql_mutex_unlock(&thd->mysys_var->mutex); |
4408 | |
4409 | mysql_cond_destroy(&cond); |
4410 | |
4411 | DBUG_EXECUTE_IF("sleep_inject_query_done_debug_sync" , { |
4412 | debug_sync_set_action |
4413 | (thd, STRING_WITH_LEN("dispatch_command_end SIGNAL query_done" )); |
4414 | };); |
4415 | |
4416 | return MY_TEST(!error); // Return 1 killed |
4417 | } |
4418 | |
4419 | |
4420 | bool Item_func_user_var::check_vcol_func_processor(void *arg) |
4421 | { |
4422 | return mark_unsupported_function("@" , name.str, arg, VCOL_NON_DETERMINISTIC); |
4423 | } |
4424 | |
4425 | #define sizeof(double) |
4426 | |
4427 | user_var_entry *get_variable(HASH *hash, LEX_CSTRING *name, |
4428 | bool create_if_not_exists) |
4429 | { |
4430 | user_var_entry *entry; |
4431 | |
4432 | if (!(entry = (user_var_entry*) my_hash_search(hash, (uchar*) name->str, |
4433 | name->length)) && |
4434 | create_if_not_exists) |
4435 | { |
4436 | size_t size=ALIGN_SIZE(sizeof(user_var_entry))+name->length+1+extra_size; |
4437 | if (!my_hash_inited(hash)) |
4438 | return 0; |
4439 | if (!(entry = (user_var_entry*) my_malloc(size, |
4440 | MYF(MY_WME | ME_FATALERROR | |
4441 | MY_THREAD_SPECIFIC)))) |
4442 | return 0; |
4443 | entry->name.str=(char*) entry+ ALIGN_SIZE(sizeof(user_var_entry))+ |
4444 | extra_size; |
4445 | entry->name.length=name->length; |
4446 | entry->value=0; |
4447 | entry->length=0; |
4448 | entry->update_query_id=0; |
4449 | entry->set_charset(NULL); |
4450 | entry->unsigned_flag= 0; |
4451 | /* |
4452 | If we are here, we were called from a SET or a query which sets a |
4453 | variable. Imagine it is this: |
4454 | INSERT INTO t SELECT @a:=10, @a:=@a+1. |
4455 | Then when we have a Item_func_get_user_var (because of the @a+1) so we |
4456 | think we have to write the value of @a to the binlog. But before that, |
4457 | we have a Item_func_set_user_var to create @a (@a:=10), in this we mark |
4458 | the variable as "already logged" (line below) so that it won't be logged |
4459 | by Item_func_get_user_var (because that's not necessary). |
4460 | */ |
4461 | entry->used_query_id=current_thd->query_id; |
4462 | entry->type=STRING_RESULT; |
4463 | memcpy((char*) entry->name.str, name->str, name->length+1); |
4464 | if (my_hash_insert(hash,(uchar*) entry)) |
4465 | { |
4466 | my_free(entry); |
4467 | return 0; |
4468 | } |
4469 | } |
4470 | return entry; |
4471 | } |
4472 | |
4473 | |
4474 | void Item_func_set_user_var::cleanup() |
4475 | { |
4476 | Item_func::cleanup(); |
4477 | m_var_entry= NULL; |
4478 | } |
4479 | |
4480 | |
4481 | bool Item_func_set_user_var::set_entry(THD *thd, bool create_if_not_exists) |
4482 | { |
4483 | if (m_var_entry && thd->thread_id == entry_thread_id) |
4484 | goto end; // update entry->update_query_id for PS |
4485 | if (!(m_var_entry= get_variable(&thd->user_vars, &name, create_if_not_exists))) |
4486 | { |
4487 | entry_thread_id= 0; |
4488 | return TRUE; |
4489 | } |
4490 | entry_thread_id= thd->thread_id; |
4491 | /* |
4492 | Remember the last query which updated it, this way a query can later know |
4493 | if this variable is a constant item in the query (it is if update_query_id |
4494 | is different from query_id). |
4495 | */ |
4496 | end: |
4497 | m_var_entry->update_query_id= thd->query_id; |
4498 | return FALSE; |
4499 | } |
4500 | |
4501 | |
4502 | /* |
4503 | When a user variable is updated (in a SET command or a query like |
4504 | SELECT @a:= ). |
4505 | */ |
4506 | |
4507 | bool Item_func_set_user_var::fix_fields(THD *thd, Item **ref) |
4508 | { |
4509 | DBUG_ASSERT(fixed == 0); |
4510 | /* fix_fields will call Item_func_set_user_var::fix_length_and_dec */ |
4511 | if (Item_func::fix_fields(thd, ref) || set_entry(thd, TRUE)) |
4512 | return TRUE; |
4513 | /* |
4514 | As it is wrong and confusing to associate any |
4515 | character set with NULL, @a should be latin2 |
4516 | after this query sequence: |
4517 | |
4518 | SET @a=_latin2'string'; |
4519 | SET @a=NULL; |
4520 | |
4521 | I.e. the second query should not change the charset |
4522 | to the current default value, but should keep the |
4523 | original value assigned during the first query. |
4524 | In order to do it, we don't copy charset |
4525 | from the argument if the argument is NULL |
4526 | and the variable has previously been initialized. |
4527 | */ |
4528 | null_item= (args[0]->type() == NULL_ITEM); |
4529 | if (!m_var_entry->charset() || !null_item) |
4530 | m_var_entry->set_charset(args[0]->collation.derivation == DERIVATION_NUMERIC ? |
4531 | default_charset() : args[0]->collation.collation); |
4532 | collation.set(m_var_entry->charset(), DERIVATION_IMPLICIT); |
4533 | switch (args[0]->result_type()) { |
4534 | case STRING_RESULT: |
4535 | case TIME_RESULT: |
4536 | set_handler(type_handler_long_blob. |
4537 | type_handler_adjusted_to_max_octet_length(max_length, |
4538 | collation.collation)); |
4539 | break; |
4540 | case REAL_RESULT: |
4541 | set_handler(&type_handler_double); |
4542 | break; |
4543 | case INT_RESULT: |
4544 | set_handler(Type_handler::type_handler_long_or_longlong(max_char_length())); |
4545 | break; |
4546 | case DECIMAL_RESULT: |
4547 | set_handler(&type_handler_newdecimal); |
4548 | break; |
4549 | case ROW_RESULT: |
4550 | DBUG_ASSERT(0); |
4551 | set_handler(&type_handler_row); |
4552 | break; |
4553 | } |
4554 | if (thd->lex->current_select) |
4555 | { |
4556 | /* |
4557 | When this function is used in a derived table/view force the derived |
4558 | table to be materialized to preserve possible side-effect of setting a |
4559 | user variable. |
4560 | */ |
4561 | SELECT_LEX_UNIT *unit= thd->lex->current_select->master_unit(); |
4562 | TABLE_LIST *derived; |
4563 | for (derived= unit->derived; |
4564 | derived; |
4565 | derived= unit->derived) |
4566 | { |
4567 | derived->set_materialized_derived(); |
4568 | derived->prohibit_cond_pushdown= true; |
4569 | if (unit->with_element && unit->with_element->is_recursive) |
4570 | break; |
4571 | unit= derived->select_lex->master_unit(); |
4572 | } |
4573 | } |
4574 | |
4575 | return FALSE; |
4576 | } |
4577 | |
4578 | |
4579 | void |
4580 | Item_func_set_user_var::fix_length_and_dec() |
4581 | { |
4582 | maybe_null=args[0]->maybe_null; |
4583 | decimals=args[0]->decimals; |
4584 | collation.set(DERIVATION_IMPLICIT); |
4585 | if (args[0]->collation.derivation == DERIVATION_NUMERIC) |
4586 | fix_length_and_charset(args[0]->max_char_length(), default_charset()); |
4587 | else |
4588 | { |
4589 | fix_length_and_charset(args[0]->max_char_length(), |
4590 | args[0]->collation.collation); |
4591 | } |
4592 | unsigned_flag= args[0]->unsigned_flag; |
4593 | } |
4594 | |
4595 | |
4596 | /* |
4597 | Mark field in read_map |
4598 | |
4599 | NOTES |
4600 | This is used by filesort to register used fields in a a temporary |
4601 | column read set or to register used fields in a view |
4602 | */ |
4603 | |
4604 | bool Item_func_set_user_var::register_field_in_read_map(void *arg) |
4605 | { |
4606 | if (result_field) |
4607 | { |
4608 | TABLE *table= (TABLE *) arg; |
4609 | if (result_field->table == table || !table) |
4610 | bitmap_set_bit(result_field->table->read_set, result_field->field_index); |
4611 | if (result_field->vcol_info) |
4612 | return result_field->vcol_info-> |
4613 | expr->walk(&Item::register_field_in_read_map, 1, arg); |
4614 | } |
4615 | return 0; |
4616 | } |
4617 | |
4618 | /* |
4619 | Mark field in bitmap supplied as *arg |
4620 | |
4621 | */ |
4622 | |
4623 | bool Item_func_set_user_var::register_field_in_bitmap(void *arg) |
4624 | { |
4625 | MY_BITMAP *bitmap = (MY_BITMAP *) arg; |
4626 | DBUG_ASSERT(bitmap); |
4627 | if (result_field) |
4628 | { |
4629 | if (!bitmap) |
4630 | return 1; |
4631 | bitmap_set_bit(bitmap, result_field->field_index); |
4632 | } |
4633 | return 0; |
4634 | } |
4635 | |
4636 | /** |
4637 | Set value to user variable. |
4638 | |
4639 | @param entry pointer to structure representing variable |
4640 | @param set_null should we set NULL value ? |
4641 | @param ptr pointer to buffer with new value |
4642 | @param length length of new value |
4643 | @param type type of new value |
4644 | @param cs charset info for new value |
4645 | @param dv derivation for new value |
4646 | @param unsigned_arg indiates if a value of type INT_RESULT is unsigned |
4647 | |
4648 | @note Sets error and fatal error if allocation fails. |
4649 | |
4650 | @retval |
4651 | false success |
4652 | @retval |
4653 | true failure |
4654 | */ |
4655 | |
4656 | static bool |
4657 | update_hash(user_var_entry *entry, bool set_null, void *ptr, size_t length, |
4658 | Item_result type, CHARSET_INFO *cs, |
4659 | bool unsigned_arg) |
4660 | { |
4661 | if (set_null) |
4662 | { |
4663 | char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry)); |
4664 | if (entry->value && entry->value != pos) |
4665 | my_free(entry->value); |
4666 | entry->value= 0; |
4667 | entry->length= 0; |
4668 | } |
4669 | else |
4670 | { |
4671 | if (type == STRING_RESULT) |
4672 | length++; // Store strings with end \0 |
4673 | if (length <= extra_size) |
4674 | { |
4675 | /* Save value in value struct */ |
4676 | char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry)); |
4677 | if (entry->value != pos) |
4678 | { |
4679 | if (entry->value) |
4680 | my_free(entry->value); |
4681 | entry->value=pos; |
4682 | } |
4683 | } |
4684 | else |
4685 | { |
4686 | /* Allocate variable */ |
4687 | if (entry->length != length) |
4688 | { |
4689 | char *pos= (char*) entry+ ALIGN_SIZE(sizeof(user_var_entry)); |
4690 | if (entry->value == pos) |
4691 | entry->value=0; |
4692 | entry->value= (char*) my_realloc(entry->value, length, |
4693 | MYF(MY_ALLOW_ZERO_PTR | MY_WME | |
4694 | ME_FATALERROR | |
4695 | MY_THREAD_SPECIFIC)); |
4696 | if (!entry->value) |
4697 | return 1; |
4698 | } |
4699 | } |
4700 | if (type == STRING_RESULT) |
4701 | { |
4702 | length--; // Fix length change above |
4703 | entry->value[length]= 0; // Store end \0 |
4704 | } |
4705 | memmove(entry->value, ptr, length); |
4706 | if (type == DECIMAL_RESULT) |
4707 | ((my_decimal*)entry->value)->fix_buffer_pointer(); |
4708 | entry->length= length; |
4709 | entry->set_charset(cs); |
4710 | entry->unsigned_flag= unsigned_arg; |
4711 | } |
4712 | entry->type=type; |
4713 | return 0; |
4714 | } |
4715 | |
4716 | |
4717 | bool |
4718 | Item_func_set_user_var::update_hash(void *ptr, size_t length, |
4719 | Item_result res_type, |
4720 | CHARSET_INFO *cs, |
4721 | bool unsigned_arg) |
4722 | { |
4723 | /* |
4724 | If we set a variable explicitly to NULL then keep the old |
4725 | result type of the variable |
4726 | */ |
4727 | if (args[0]->type() == Item::FIELD_ITEM) |
4728 | { |
4729 | /* args[0]->null_value may be outdated */ |
4730 | null_value= ((Item_field*)args[0])->field->is_null(); |
4731 | } |
4732 | else |
4733 | null_value= args[0]->null_value; |
4734 | if (null_value && null_item) |
4735 | res_type= m_var_entry->type; // Don't change type of item |
4736 | if (::update_hash(m_var_entry, null_value, |
4737 | ptr, length, res_type, cs, unsigned_arg)) |
4738 | { |
4739 | null_value= 1; |
4740 | return 1; |
4741 | } |
4742 | return 0; |
4743 | } |
4744 | |
4745 | |
4746 | /** Get the value of a variable as a double. */ |
4747 | |
4748 | double user_var_entry::val_real(bool *null_value) |
4749 | { |
4750 | if ((*null_value= (value == 0))) |
4751 | return 0.0; |
4752 | |
4753 | switch (type) { |
4754 | case REAL_RESULT: |
4755 | return *(double*) value; |
4756 | case INT_RESULT: |
4757 | return (double) *(longlong*) value; |
4758 | case DECIMAL_RESULT: |
4759 | { |
4760 | double result; |
4761 | my_decimal2double(E_DEC_FATAL_ERROR, (my_decimal *)value, &result); |
4762 | return result; |
4763 | } |
4764 | case STRING_RESULT: |
4765 | return my_atof(value); // This is null terminated |
4766 | case ROW_RESULT: |
4767 | case TIME_RESULT: |
4768 | DBUG_ASSERT(0); // Impossible |
4769 | break; |
4770 | } |
4771 | return 0.0; // Impossible |
4772 | } |
4773 | |
4774 | |
4775 | /** Get the value of a variable as an integer. */ |
4776 | |
4777 | longlong user_var_entry::val_int(bool *null_value) const |
4778 | { |
4779 | if ((*null_value= (value == 0))) |
4780 | return 0; |
4781 | |
4782 | switch (type) { |
4783 | case REAL_RESULT: |
4784 | return (longlong) *(double*) value; |
4785 | case INT_RESULT: |
4786 | return *(longlong*) value; |
4787 | case DECIMAL_RESULT: |
4788 | { |
4789 | longlong result; |
4790 | my_decimal2int(E_DEC_FATAL_ERROR, (my_decimal *)value, 0, &result); |
4791 | return result; |
4792 | } |
4793 | case STRING_RESULT: |
4794 | { |
4795 | int error; |
4796 | return my_strtoll10(value, (char**) 0, &error);// String is null terminated |
4797 | } |
4798 | case ROW_RESULT: |
4799 | case TIME_RESULT: |
4800 | DBUG_ASSERT(0); // Impossible |
4801 | break; |
4802 | } |
4803 | return 0; // Impossible |
4804 | } |
4805 | |
4806 | |
4807 | /** Get the value of a variable as a string. */ |
4808 | |
4809 | String *user_var_entry::val_str(bool *null_value, String *str, |
4810 | uint decimals) |
4811 | { |
4812 | if ((*null_value= (value == 0))) |
4813 | return (String*) 0; |
4814 | |
4815 | switch (type) { |
4816 | case REAL_RESULT: |
4817 | str->set_real(*(double*) value, decimals, charset()); |
4818 | break; |
4819 | case INT_RESULT: |
4820 | if (!unsigned_flag) |
4821 | str->set(*(longlong*) value, charset()); |
4822 | else |
4823 | str->set(*(ulonglong*) value, charset()); |
4824 | break; |
4825 | case DECIMAL_RESULT: |
4826 | str_set_decimal((my_decimal *) value, str, charset()); |
4827 | break; |
4828 | case STRING_RESULT: |
4829 | if (str->copy(value, length, charset())) |
4830 | str= 0; // EOM error |
4831 | break; |
4832 | case ROW_RESULT: |
4833 | case TIME_RESULT: |
4834 | DBUG_ASSERT(0); // Impossible |
4835 | break; |
4836 | } |
4837 | return(str); |
4838 | } |
4839 | |
4840 | /** Get the value of a variable as a decimal. */ |
4841 | |
4842 | my_decimal *user_var_entry::val_decimal(bool *null_value, my_decimal *val) |
4843 | { |
4844 | if ((*null_value= (value == 0))) |
4845 | return 0; |
4846 | |
4847 | switch (type) { |
4848 | case REAL_RESULT: |
4849 | double2my_decimal(E_DEC_FATAL_ERROR, *(double*) value, val); |
4850 | break; |
4851 | case INT_RESULT: |
4852 | int2my_decimal(E_DEC_FATAL_ERROR, *(longlong*) value, 0, val); |
4853 | break; |
4854 | case DECIMAL_RESULT: |
4855 | my_decimal2decimal((my_decimal *) value, val); |
4856 | break; |
4857 | case STRING_RESULT: |
4858 | str2my_decimal(E_DEC_FATAL_ERROR, value, length, charset(), val); |
4859 | break; |
4860 | case ROW_RESULT: |
4861 | case TIME_RESULT: |
4862 | DBUG_ASSERT(0); // Impossible |
4863 | break; |
4864 | } |
4865 | return(val); |
4866 | } |
4867 | |
4868 | /** |
4869 | This functions is invoked on SET \@variable or |
4870 | \@variable:= expression. |
4871 | |
4872 | Evaluate (and check expression), store results. |
4873 | |
4874 | @note |
4875 | For now it always return OK. All problem with value evaluating |
4876 | will be caught by thd->is_error() check in sql_set_variables(). |
4877 | |
4878 | @retval |
4879 | FALSE OK. |
4880 | */ |
4881 | |
4882 | bool |
4883 | Item_func_set_user_var::check(bool use_result_field) |
4884 | { |
4885 | DBUG_ENTER("Item_func_set_user_var::check" ); |
4886 | if (use_result_field && !result_field) |
4887 | use_result_field= FALSE; |
4888 | |
4889 | switch (result_type()) { |
4890 | case REAL_RESULT: |
4891 | { |
4892 | save_result.vreal= use_result_field ? result_field->val_real() : |
4893 | args[0]->val_real(); |
4894 | break; |
4895 | } |
4896 | case INT_RESULT: |
4897 | { |
4898 | save_result.vint= use_result_field ? result_field->val_int() : |
4899 | args[0]->val_int(); |
4900 | unsigned_flag= (use_result_field ? |
4901 | ((Field_num*)result_field)->unsigned_flag: |
4902 | args[0]->unsigned_flag); |
4903 | break; |
4904 | } |
4905 | case STRING_RESULT: |
4906 | { |
4907 | save_result.vstr= use_result_field ? result_field->val_str(&value) : |
4908 | args[0]->val_str(&value); |
4909 | break; |
4910 | } |
4911 | case DECIMAL_RESULT: |
4912 | { |
4913 | save_result.vdec= use_result_field ? |
4914 | result_field->val_decimal(&decimal_buff) : |
4915 | args[0]->val_decimal(&decimal_buff); |
4916 | break; |
4917 | } |
4918 | case ROW_RESULT: |
4919 | case TIME_RESULT: |
4920 | DBUG_ASSERT(0); // This case should never be chosen |
4921 | break; |
4922 | } |
4923 | DBUG_RETURN(FALSE); |
4924 | } |
4925 | |
4926 | |
4927 | /** |
4928 | @brief Evaluate and store item's result. |
4929 | This function is invoked on "SELECT ... INTO @var ...". |
4930 | |
4931 | @param item An item to get value from. |
4932 | */ |
4933 | |
4934 | void Item_func_set_user_var::save_item_result(Item *item) |
4935 | { |
4936 | DBUG_ENTER("Item_func_set_user_var::save_item_result" ); |
4937 | |
4938 | switch (args[0]->result_type()) { |
4939 | case REAL_RESULT: |
4940 | save_result.vreal= item->val_result(); |
4941 | break; |
4942 | case INT_RESULT: |
4943 | save_result.vint= item->val_int_result(); |
4944 | unsigned_flag= item->unsigned_flag; |
4945 | break; |
4946 | case STRING_RESULT: |
4947 | save_result.vstr= item->str_result(&value); |
4948 | break; |
4949 | case DECIMAL_RESULT: |
4950 | save_result.vdec= item->val_decimal_result(&decimal_buff); |
4951 | break; |
4952 | case ROW_RESULT: |
4953 | case TIME_RESULT: |
4954 | DBUG_ASSERT(0); // This case should never be chosen |
4955 | break; |
4956 | } |
4957 | DBUG_VOID_RETURN; |
4958 | } |
4959 | |
4960 | |
4961 | /** |
4962 | This functions is invoked on |
4963 | SET \@variable or \@variable:= expression. |
4964 | |
4965 | @note |
4966 | We have to store the expression as such in the variable, independent of |
4967 | the value method used by the user |
4968 | |
4969 | @retval |
4970 | 0 OK |
4971 | @retval |
4972 | 1 EOM Error |
4973 | |
4974 | */ |
4975 | |
4976 | bool |
4977 | Item_func_set_user_var::update() |
4978 | { |
4979 | bool res= 0; |
4980 | DBUG_ENTER("Item_func_set_user_var::update" ); |
4981 | |
4982 | switch (result_type()) { |
4983 | case REAL_RESULT: |
4984 | { |
4985 | res= update_hash((void*) &save_result.vreal,sizeof(save_result.vreal), |
4986 | REAL_RESULT, default_charset(), 0); |
4987 | break; |
4988 | } |
4989 | case INT_RESULT: |
4990 | { |
4991 | res= update_hash((void*) &save_result.vint, sizeof(save_result.vint), |
4992 | INT_RESULT, default_charset(), unsigned_flag); |
4993 | break; |
4994 | } |
4995 | case STRING_RESULT: |
4996 | { |
4997 | if (!save_result.vstr) // Null value |
4998 | res= update_hash((void*) 0, 0, STRING_RESULT, &my_charset_bin, 0); |
4999 | else |
5000 | res= update_hash((void*) save_result.vstr->ptr(), |
5001 | save_result.vstr->length(), STRING_RESULT, |
5002 | save_result.vstr->charset(), 0); |
5003 | break; |
5004 | } |
5005 | case DECIMAL_RESULT: |
5006 | { |
5007 | if (!save_result.vdec) // Null value |
5008 | res= update_hash((void*) 0, 0, DECIMAL_RESULT, &my_charset_bin, 0); |
5009 | else |
5010 | res= update_hash((void*) save_result.vdec, |
5011 | sizeof(my_decimal), DECIMAL_RESULT, |
5012 | default_charset(), 0); |
5013 | break; |
5014 | } |
5015 | case ROW_RESULT: |
5016 | case TIME_RESULT: |
5017 | DBUG_ASSERT(0); // This case should never be chosen |
5018 | break; |
5019 | } |
5020 | DBUG_RETURN(res); |
5021 | } |
5022 | |
5023 | |
5024 | double Item_func_set_user_var::val_real() |
5025 | { |
5026 | DBUG_ASSERT(fixed == 1); |
5027 | check(0); |
5028 | update(); // Store expression |
5029 | return m_var_entry->val_real(&null_value); |
5030 | } |
5031 | |
5032 | longlong Item_func_set_user_var::val_int() |
5033 | { |
5034 | DBUG_ASSERT(fixed == 1); |
5035 | check(0); |
5036 | update(); // Store expression |
5037 | return m_var_entry->val_int(&null_value); |
5038 | } |
5039 | |
5040 | String *Item_func_set_user_var::val_str(String *str) |
5041 | { |
5042 | DBUG_ASSERT(fixed == 1); |
5043 | check(0); |
5044 | update(); // Store expression |
5045 | return m_var_entry->val_str(&null_value, str, decimals); |
5046 | } |
5047 | |
5048 | |
5049 | my_decimal *Item_func_set_user_var::val_decimal(my_decimal *val) |
5050 | { |
5051 | DBUG_ASSERT(fixed == 1); |
5052 | check(0); |
5053 | update(); // Store expression |
5054 | return m_var_entry->val_decimal(&null_value, val); |
5055 | } |
5056 | |
5057 | |
5058 | double Item_func_set_user_var::val_result() |
5059 | { |
5060 | DBUG_ASSERT(fixed == 1); |
5061 | check(TRUE); |
5062 | update(); // Store expression |
5063 | return m_var_entry->val_real(&null_value); |
5064 | } |
5065 | |
5066 | longlong Item_func_set_user_var::val_int_result() |
5067 | { |
5068 | DBUG_ASSERT(fixed == 1); |
5069 | check(TRUE); |
5070 | update(); // Store expression |
5071 | return m_var_entry->val_int(&null_value); |
5072 | } |
5073 | |
5074 | bool Item_func_set_user_var::val_bool_result() |
5075 | { |
5076 | DBUG_ASSERT(fixed == 1); |
5077 | check(TRUE); |
5078 | update(); // Store expression |
5079 | return m_var_entry->val_int(&null_value) != 0; |
5080 | } |
5081 | |
5082 | String *Item_func_set_user_var::str_result(String *str) |
5083 | { |
5084 | DBUG_ASSERT(fixed == 1); |
5085 | check(TRUE); |
5086 | update(); // Store expression |
5087 | return m_var_entry->val_str(&null_value, str, decimals); |
5088 | } |
5089 | |
5090 | |
5091 | my_decimal *Item_func_set_user_var::val_decimal_result(my_decimal *val) |
5092 | { |
5093 | DBUG_ASSERT(fixed == 1); |
5094 | check(TRUE); |
5095 | update(); // Store expression |
5096 | return m_var_entry->val_decimal(&null_value, val); |
5097 | } |
5098 | |
5099 | |
5100 | bool Item_func_set_user_var::is_null_result() |
5101 | { |
5102 | DBUG_ASSERT(fixed == 1); |
5103 | check(TRUE); |
5104 | update(); // Store expression |
5105 | return is_null(); |
5106 | } |
5107 | |
5108 | |
5109 | void Item_func_set_user_var::print(String *str, enum_query_type query_type) |
5110 | { |
5111 | str->append(STRING_WITH_LEN("@" )); |
5112 | str->append(&name); |
5113 | str->append(STRING_WITH_LEN(":=" )); |
5114 | args[0]->print_parenthesised(str, query_type, precedence()); |
5115 | } |
5116 | |
5117 | |
5118 | void Item_func_set_user_var::print_as_stmt(String *str, |
5119 | enum_query_type query_type) |
5120 | { |
5121 | str->append(STRING_WITH_LEN("set @" )); |
5122 | str->append(&name); |
5123 | str->append(STRING_WITH_LEN(":=" )); |
5124 | args[0]->print_parenthesised(str, query_type, precedence()); |
5125 | } |
5126 | |
5127 | bool Item_func_set_user_var::send(Protocol *protocol, st_value *buffer) |
5128 | { |
5129 | if (result_field) |
5130 | { |
5131 | check(1); |
5132 | update(); |
5133 | return protocol->store(result_field); |
5134 | } |
5135 | return Item::send(protocol, buffer); |
5136 | } |
5137 | |
5138 | void Item_func_set_user_var::make_send_field(THD *thd, Send_field *tmp_field) |
5139 | { |
5140 | if (result_field) |
5141 | { |
5142 | result_field->make_send_field(tmp_field); |
5143 | DBUG_ASSERT(tmp_field->table_name != 0); |
5144 | if (Item::name.str) |
5145 | tmp_field->col_name= Item::name; // Use user supplied name |
5146 | } |
5147 | else |
5148 | Item::make_send_field(thd, tmp_field); |
5149 | } |
5150 | |
5151 | |
5152 | /* |
5153 | Save the value of a user variable into a field |
5154 | |
5155 | SYNOPSIS |
5156 | save_in_field() |
5157 | field target field to save the value to |
5158 | no_conversion flag indicating whether conversions are allowed |
5159 | |
5160 | DESCRIPTION |
5161 | Save the function value into a field and update the user variable |
5162 | accordingly. If a result field is defined and the target field doesn't |
5163 | coincide with it then the value from the result field will be used as |
5164 | the new value of the user variable. |
5165 | |
5166 | The reason to have this method rather than simply using the result |
5167 | field in the val_xxx() methods is that the value from the result field |
5168 | not always can be used when the result field is defined. |
5169 | Let's consider the following cases: |
5170 | 1) when filling a tmp table the result field is defined but the value of it |
5171 | is undefined because it has to be produced yet. Thus we can't use it. |
5172 | 2) on execution of an INSERT ... SELECT statement the save_in_field() |
5173 | function will be called to fill the data in the new record. If the SELECT |
5174 | part uses a tmp table then the result field is defined and should be |
5175 | used in order to get the correct result. |
5176 | |
5177 | The difference between the SET_USER_VAR function and regular functions |
5178 | like CONCAT is that the Item_func objects for the regular functions are |
5179 | replaced by Item_field objects after the values of these functions have |
5180 | been stored in a tmp table. Yet an object of the Item_field class cannot |
5181 | be used to update a user variable. |
5182 | Due to this we have to handle the result field in a special way here and |
5183 | in the Item_func_set_user_var::send() function. |
5184 | |
5185 | RETURN VALUES |
5186 | FALSE Ok |
5187 | TRUE Error |
5188 | */ |
5189 | |
5190 | int Item_func_set_user_var::save_in_field(Field *field, bool no_conversions, |
5191 | bool can_use_result_field) |
5192 | { |
5193 | bool use_result_field= (!can_use_result_field ? 0 : |
5194 | (result_field && result_field != field)); |
5195 | int error; |
5196 | |
5197 | /* Update the value of the user variable */ |
5198 | check(use_result_field); |
5199 | update(); |
5200 | |
5201 | if (result_type() == STRING_RESULT || |
5202 | (result_type() == REAL_RESULT && |
5203 | field->result_type() == STRING_RESULT)) |
5204 | { |
5205 | String *result; |
5206 | CHARSET_INFO *cs= collation.collation; |
5207 | char buff[MAX_FIELD_WIDTH]; // Alloc buffer for small columns |
5208 | str_value.set_quick(buff, sizeof(buff), cs); |
5209 | result= m_var_entry->val_str(&null_value, &str_value, decimals); |
5210 | |
5211 | if (null_value) |
5212 | { |
5213 | str_value.set_quick(0, 0, cs); |
5214 | return set_field_to_null_with_conversions(field, no_conversions); |
5215 | } |
5216 | |
5217 | /* NOTE: If null_value == FALSE, "result" must be not NULL. */ |
5218 | |
5219 | field->set_notnull(); |
5220 | error=field->store(result->ptr(),result->length(),cs); |
5221 | str_value.set_quick(0, 0, cs); |
5222 | } |
5223 | else if (result_type() == REAL_RESULT) |
5224 | { |
5225 | double nr= m_var_entry->val_real(&null_value); |
5226 | if (null_value) |
5227 | return set_field_to_null(field); |
5228 | field->set_notnull(); |
5229 | error=field->store(nr); |
5230 | } |
5231 | else if (result_type() == DECIMAL_RESULT) |
5232 | { |
5233 | my_decimal decimal_value; |
5234 | my_decimal *val= m_var_entry->val_decimal(&null_value, &decimal_value); |
5235 | if (null_value) |
5236 | return set_field_to_null(field); |
5237 | field->set_notnull(); |
5238 | error=field->store_decimal(val); |
5239 | } |
5240 | else |
5241 | { |
5242 | longlong nr= m_var_entry->val_int(&null_value); |
5243 | if (null_value) |
5244 | return set_field_to_null_with_conversions(field, no_conversions); |
5245 | field->set_notnull(); |
5246 | error=field->store(nr, unsigned_flag); |
5247 | } |
5248 | return error; |
5249 | } |
5250 | |
5251 | |
5252 | String * |
5253 | Item_func_get_user_var::val_str(String *str) |
5254 | { |
5255 | DBUG_ASSERT(fixed == 1); |
5256 | DBUG_ENTER("Item_func_get_user_var::val_str" ); |
5257 | if (!m_var_entry) |
5258 | DBUG_RETURN((String*) 0); // No such variable |
5259 | DBUG_RETURN(m_var_entry->val_str(&null_value, str, decimals)); |
5260 | } |
5261 | |
5262 | |
5263 | double Item_func_get_user_var::val_real() |
5264 | { |
5265 | DBUG_ASSERT(fixed == 1); |
5266 | if (!m_var_entry) |
5267 | return 0.0; // No such variable |
5268 | return (m_var_entry->val_real(&null_value)); |
5269 | } |
5270 | |
5271 | |
5272 | my_decimal *Item_func_get_user_var::val_decimal(my_decimal *dec) |
5273 | { |
5274 | DBUG_ASSERT(fixed == 1); |
5275 | if (!m_var_entry) |
5276 | return 0; |
5277 | return m_var_entry->val_decimal(&null_value, dec); |
5278 | } |
5279 | |
5280 | |
5281 | longlong Item_func_get_user_var::val_int() |
5282 | { |
5283 | DBUG_ASSERT(fixed == 1); |
5284 | if (!m_var_entry) |
5285 | return 0; // No such variable |
5286 | return (m_var_entry->val_int(&null_value)); |
5287 | } |
5288 | |
5289 | |
5290 | /** |
5291 | Get variable by name and, if necessary, put the record of variable |
5292 | use into the binary log. |
5293 | |
5294 | When a user variable is invoked from an update query (INSERT, UPDATE etc), |
5295 | stores this variable and its value in thd->user_var_events, so that it can be |
5296 | written to the binlog (will be written just before the query is written, see |
5297 | log.cc). |
5298 | |
5299 | @param thd Current thread |
5300 | @param name Variable name |
5301 | @param[out] out_entry variable structure or NULL. The pointer is set |
5302 | regardless of whether function succeeded or not. |
5303 | |
5304 | @retval |
5305 | 0 OK |
5306 | @retval |
5307 | 1 Failed to put appropriate record into binary log |
5308 | |
5309 | */ |
5310 | |
5311 | static int |
5312 | get_var_with_binlog(THD *thd, enum_sql_command sql_command, |
5313 | LEX_CSTRING *name, user_var_entry **out_entry) |
5314 | { |
5315 | BINLOG_USER_VAR_EVENT *user_var_event; |
5316 | user_var_entry *var_entry; |
5317 | var_entry= get_variable(&thd->user_vars, name, 0); |
5318 | |
5319 | /* |
5320 | Any reference to user-defined variable which is done from stored |
5321 | function or trigger affects their execution and the execution of the |
5322 | calling statement. We must log all such variables even if they are |
5323 | not involved in table-updating statements. |
5324 | */ |
5325 | if (!(opt_bin_log && |
5326 | (is_update_query(sql_command) || thd->in_sub_stmt))) |
5327 | { |
5328 | *out_entry= var_entry; |
5329 | return 0; |
5330 | } |
5331 | |
5332 | if (!var_entry) |
5333 | { |
5334 | /* |
5335 | If the variable does not exist, it's NULL, but we want to create it so |
5336 | that it gets into the binlog (if it didn't, the slave could be |
5337 | influenced by a variable of the same name previously set by another |
5338 | thread). |
5339 | We create it like if it had been explicitly set with SET before. |
5340 | The 'new' mimics what sql_yacc.yy does when 'SET @a=10;'. |
5341 | sql_set_variables() is what is called from 'case SQLCOM_SET_OPTION' |
5342 | in dispatch_command()). Instead of building a one-element list to pass to |
5343 | sql_set_variables(), we could instead manually call check() and update(); |
5344 | this would save memory and time; but calling sql_set_variables() makes |
5345 | one unique place to maintain (sql_set_variables()). |
5346 | |
5347 | Manipulation with lex is necessary since free_underlaid_joins |
5348 | is going to release memory belonging to the main query. |
5349 | */ |
5350 | |
5351 | List<set_var_base> tmp_var_list; |
5352 | LEX *sav_lex= thd->lex, lex_tmp; |
5353 | thd->lex= &lex_tmp; |
5354 | lex_start(thd); |
5355 | tmp_var_list.push_back(new (thd->mem_root) |
5356 | set_var_user(new (thd->mem_root) |
5357 | Item_func_set_user_var(thd, name, |
5358 | new (thd->mem_root) Item_null(thd))), |
5359 | thd->mem_root); |
5360 | /* Create the variable if the above allocations succeeded */ |
5361 | if (unlikely(thd->is_fatal_error) || |
5362 | unlikely(sql_set_variables(thd, &tmp_var_list, false))) |
5363 | { |
5364 | thd->lex= sav_lex; |
5365 | goto err; |
5366 | } |
5367 | thd->lex= sav_lex; |
5368 | if (unlikely(!(var_entry= get_variable(&thd->user_vars, name, 0)))) |
5369 | goto err; |
5370 | } |
5371 | else if (var_entry->used_query_id == thd->query_id || |
5372 | mysql_bin_log.is_query_in_union(thd, var_entry->used_query_id)) |
5373 | { |
5374 | /* |
5375 | If this variable was already stored in user_var_events by this query |
5376 | (because it's used in more than one place in the query), don't store |
5377 | it. |
5378 | */ |
5379 | *out_entry= var_entry; |
5380 | return 0; |
5381 | } |
5382 | |
5383 | size_t size; |
5384 | /* |
5385 | First we need to store value of var_entry, when the next situation |
5386 | appears: |
5387 | > set @a:=1; |
5388 | > insert into t1 values (@a), (@a:=@a+1), (@a:=@a+1); |
5389 | We have to write to binlog value @a= 1. |
5390 | |
5391 | We allocate the user_var_event on user_var_events_alloc pool, not on |
5392 | the this-statement-execution pool because in SPs user_var_event objects |
5393 | may need to be valid after current [SP] statement execution pool is |
5394 | destroyed. |
5395 | */ |
5396 | size= ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT)) + var_entry->length; |
5397 | if (unlikely(!(user_var_event= (BINLOG_USER_VAR_EVENT *) |
5398 | alloc_root(thd->user_var_events_alloc, size)))) |
5399 | goto err; |
5400 | |
5401 | user_var_event->value= (char*) user_var_event + |
5402 | ALIGN_SIZE(sizeof(BINLOG_USER_VAR_EVENT)); |
5403 | user_var_event->user_var_event= var_entry; |
5404 | user_var_event->type= var_entry->type; |
5405 | user_var_event->charset_number= var_entry->charset()->number; |
5406 | user_var_event->unsigned_flag= var_entry->unsigned_flag; |
5407 | if (!var_entry->value) |
5408 | { |
5409 | /* NULL value*/ |
5410 | user_var_event->length= 0; |
5411 | user_var_event->value= 0; |
5412 | } |
5413 | else |
5414 | { |
5415 | user_var_event->length= var_entry->length; |
5416 | memcpy(user_var_event->value, var_entry->value, |
5417 | var_entry->length); |
5418 | } |
5419 | /* Mark that this variable has been used by this query */ |
5420 | var_entry->used_query_id= thd->query_id; |
5421 | if (insert_dynamic(&thd->user_var_events, (uchar*) &user_var_event)) |
5422 | goto err; |
5423 | |
5424 | *out_entry= var_entry; |
5425 | return 0; |
5426 | |
5427 | err: |
5428 | *out_entry= var_entry; |
5429 | return 1; |
5430 | } |
5431 | |
5432 | void Item_func_get_user_var::fix_length_and_dec() |
5433 | { |
5434 | THD *thd=current_thd; |
5435 | int error; |
5436 | maybe_null=1; |
5437 | decimals=NOT_FIXED_DEC; |
5438 | max_length=MAX_BLOB_WIDTH; |
5439 | |
5440 | error= get_var_with_binlog(thd, thd->lex->sql_command, &name, &m_var_entry); |
5441 | |
5442 | /* |
5443 | If the variable didn't exist it has been created as a STRING-type. |
5444 | 'm_var_entry' is NULL only if there occurred an error during the call to |
5445 | get_var_with_binlog. |
5446 | */ |
5447 | if (likely(!error && m_var_entry)) |
5448 | { |
5449 | unsigned_flag= m_var_entry->unsigned_flag; |
5450 | max_length= (uint32)m_var_entry->length; |
5451 | collation.set(m_var_entry->charset(), DERIVATION_IMPLICIT); |
5452 | set_handler_by_result_type(m_var_entry->type); |
5453 | switch (result_type()) { |
5454 | case REAL_RESULT: |
5455 | fix_char_length(DBL_DIG + 8); |
5456 | break; |
5457 | case INT_RESULT: |
5458 | fix_char_length(MAX_BIGINT_WIDTH); |
5459 | decimals=0; |
5460 | break; |
5461 | case STRING_RESULT: |
5462 | max_length= MAX_BLOB_WIDTH - 1; |
5463 | break; |
5464 | case DECIMAL_RESULT: |
5465 | fix_char_length(DECIMAL_MAX_STR_LENGTH); |
5466 | decimals= DECIMAL_MAX_SCALE; |
5467 | break; |
5468 | case ROW_RESULT: // Keep compiler happy |
5469 | case TIME_RESULT: |
5470 | DBUG_ASSERT(0); // This case should never be chosen |
5471 | break; |
5472 | } |
5473 | } |
5474 | else |
5475 | { |
5476 | collation.set(&my_charset_bin, DERIVATION_IMPLICIT); |
5477 | null_value= 1; |
5478 | set_handler(&type_handler_long_blob); |
5479 | max_length= MAX_BLOB_WIDTH; |
5480 | } |
5481 | } |
5482 | |
5483 | |
5484 | bool Item_func_get_user_var::const_item() const |
5485 | { |
5486 | return (!m_var_entry || |
5487 | current_thd->query_id != m_var_entry->update_query_id); |
5488 | } |
5489 | |
5490 | |
5491 | void Item_func_get_user_var::print(String *str, enum_query_type query_type) |
5492 | { |
5493 | str->append(STRING_WITH_LEN("@" )); |
5494 | append_identifier(current_thd, str, &name); |
5495 | } |
5496 | |
5497 | |
5498 | bool Item_func_get_user_var::eq(const Item *item, bool binary_cmp) const |
5499 | { |
5500 | /* Assume we don't have rtti */ |
5501 | if (this == item) |
5502 | return 1; // Same item is same. |
5503 | /* Check if other type is also a get_user_var() object */ |
5504 | if (item->type() != FUNC_ITEM || |
5505 | ((Item_func*) item)->functype() != functype()) |
5506 | return 0; |
5507 | Item_func_get_user_var *other=(Item_func_get_user_var*) item; |
5508 | return (name.length == other->name.length && |
5509 | !memcmp(name.str, other->name.str, name.length)); |
5510 | } |
5511 | |
5512 | |
5513 | bool Item_func_get_user_var::set_value(THD *thd, |
5514 | sp_rcontext * /*ctx*/, Item **it) |
5515 | { |
5516 | LEX_CSTRING tmp_name= get_name(); |
5517 | Item_func_set_user_var *suv= new (thd->mem_root) Item_func_set_user_var(thd, &tmp_name, *it); |
5518 | /* |
5519 | Item_func_set_user_var is not fixed after construction, call |
5520 | fix_fields(). |
5521 | */ |
5522 | return (!suv || suv->fix_fields(thd, it) || suv->check(0) || suv->update()); |
5523 | } |
5524 | |
5525 | |
5526 | bool Item_user_var_as_out_param::fix_fields(THD *thd, Item **ref) |
5527 | { |
5528 | DBUG_ASSERT(fixed == 0); |
5529 | DBUG_ASSERT(thd->lex->exchange); |
5530 | if (Item::fix_fields(thd, ref) || |
5531 | !(entry= get_variable(&thd->user_vars, &org_name, 1))) |
5532 | return TRUE; |
5533 | entry->type= STRING_RESULT; |
5534 | /* |
5535 | Let us set the same collation which is used for loading |
5536 | of fields in LOAD DATA INFILE. |
5537 | (Since Item_user_var_as_out_param is used only there). |
5538 | */ |
5539 | entry->set_charset(thd->lex->exchange->cs ? |
5540 | thd->lex->exchange->cs : |
5541 | thd->variables.collation_database); |
5542 | entry->update_query_id= thd->query_id; |
5543 | return FALSE; |
5544 | } |
5545 | |
5546 | |
5547 | void Item_user_var_as_out_param::set_null_value(CHARSET_INFO* cs) |
5548 | { |
5549 | ::update_hash(entry, TRUE, 0, 0, STRING_RESULT, cs, 0 /* unsigned_arg */); |
5550 | } |
5551 | |
5552 | |
5553 | void Item_user_var_as_out_param::set_value(const char *str, uint length, |
5554 | CHARSET_INFO* cs) |
5555 | { |
5556 | ::update_hash(entry, FALSE, (void*)str, length, STRING_RESULT, cs, |
5557 | 0 /* unsigned_arg */); |
5558 | } |
5559 | |
5560 | |
5561 | double Item_user_var_as_out_param::val_real() |
5562 | { |
5563 | DBUG_ASSERT(0); |
5564 | return 0.0; |
5565 | } |
5566 | |
5567 | |
5568 | longlong Item_user_var_as_out_param::val_int() |
5569 | { |
5570 | DBUG_ASSERT(0); |
5571 | return 0; |
5572 | } |
5573 | |
5574 | |
5575 | String* Item_user_var_as_out_param::val_str(String *str) |
5576 | { |
5577 | DBUG_ASSERT(0); |
5578 | return 0; |
5579 | } |
5580 | |
5581 | |
5582 | my_decimal* Item_user_var_as_out_param::val_decimal(my_decimal *decimal_buffer) |
5583 | { |
5584 | DBUG_ASSERT(0); |
5585 | return 0; |
5586 | } |
5587 | |
5588 | |
5589 | bool Item_user_var_as_out_param::get_date(MYSQL_TIME *ltime, ulonglong fuzzy) |
5590 | { |
5591 | DBUG_ASSERT(0); |
5592 | return true; |
5593 | } |
5594 | |
5595 | |
5596 | void Item_user_var_as_out_param::load_data_print_for_log_event(THD *thd, |
5597 | String *str) |
5598 | const |
5599 | { |
5600 | str->append('@'); |
5601 | append_identifier(thd, str, &org_name); |
5602 | } |
5603 | |
5604 | |
5605 | Item_func_get_system_var:: |
5606 | Item_func_get_system_var(THD *thd, sys_var *var_arg, enum_var_type var_type_arg, |
5607 | LEX_CSTRING *component_arg, const char *name_arg, |
5608 | size_t name_len_arg): |
5609 | Item_func(thd), var(var_arg), var_type(var_type_arg), |
5610 | orig_var_type(var_type_arg), component(*component_arg), cache_present(0) |
5611 | { |
5612 | /* set_name() will allocate the name */ |
5613 | set_name(thd, name_arg, (uint) name_len_arg, system_charset_info); |
5614 | } |
5615 | |
5616 | |
5617 | bool Item_func_get_system_var::is_written_to_binlog() |
5618 | { |
5619 | return var->is_written_to_binlog(var_type); |
5620 | } |
5621 | |
5622 | |
5623 | void Item_func_get_system_var::update_null_value() |
5624 | { |
5625 | THD *thd= current_thd; |
5626 | int save_no_errors= thd->no_errors; |
5627 | thd->no_errors= TRUE; |
5628 | Item::update_null_value(); |
5629 | thd->no_errors= save_no_errors; |
5630 | } |
5631 | |
5632 | |
5633 | void Item_func_get_system_var::fix_length_and_dec() |
5634 | { |
5635 | char *cptr; |
5636 | maybe_null= TRUE; |
5637 | max_length= 0; |
5638 | |
5639 | if (var->check_type(var_type)) |
5640 | { |
5641 | if (var_type != OPT_DEFAULT) |
5642 | { |
5643 | my_error(ER_INCORRECT_GLOBAL_LOCAL_VAR, MYF(0), |
5644 | var->name.str, var_type == OPT_GLOBAL ? "SESSION" : "GLOBAL" ); |
5645 | return; |
5646 | } |
5647 | /* As there was no local variable, return the global value */ |
5648 | var_type= OPT_GLOBAL; |
5649 | } |
5650 | |
5651 | switch (var->show_type()) |
5652 | { |
5653 | case SHOW_HA_ROWS: |
5654 | case SHOW_UINT: |
5655 | case SHOW_ULONG: |
5656 | case SHOW_ULONGLONG: |
5657 | unsigned_flag= TRUE; |
5658 | /* fall through */ |
5659 | case SHOW_SINT: |
5660 | case SHOW_SLONG: |
5661 | case SHOW_SLONGLONG: |
5662 | collation.set_numeric(); |
5663 | fix_char_length(MY_INT64_NUM_DECIMAL_DIGITS); |
5664 | decimals=0; |
5665 | break; |
5666 | case SHOW_CHAR: |
5667 | case SHOW_CHAR_PTR: |
5668 | mysql_mutex_lock(&LOCK_global_system_variables); |
5669 | cptr= var->show_type() == SHOW_CHAR ? |
5670 | (char*) var->value_ptr(current_thd, var_type, &component) : |
5671 | *(char**) var->value_ptr(current_thd, var_type, &component); |
5672 | if (cptr) |
5673 | max_length= (uint32)system_charset_info->cset->numchars(system_charset_info, |
5674 | cptr, |
5675 | cptr + strlen(cptr)); |
5676 | mysql_mutex_unlock(&LOCK_global_system_variables); |
5677 | collation.set(system_charset_info, DERIVATION_SYSCONST); |
5678 | max_length*= system_charset_info->mbmaxlen; |
5679 | decimals=NOT_FIXED_DEC; |
5680 | break; |
5681 | case SHOW_LEX_STRING: |
5682 | { |
5683 | mysql_mutex_lock(&LOCK_global_system_variables); |
5684 | LEX_STRING *ls= ((LEX_STRING*)var->value_ptr(current_thd, var_type, &component)); |
5685 | max_length= (uint32)system_charset_info->cset->numchars(system_charset_info, |
5686 | ls->str, |
5687 | ls->str + ls->length); |
5688 | mysql_mutex_unlock(&LOCK_global_system_variables); |
5689 | collation.set(system_charset_info, DERIVATION_SYSCONST); |
5690 | max_length*= system_charset_info->mbmaxlen; |
5691 | decimals=NOT_FIXED_DEC; |
5692 | } |
5693 | break; |
5694 | case SHOW_BOOL: |
5695 | case SHOW_MY_BOOL: |
5696 | collation.set_numeric(); |
5697 | fix_char_length(1); |
5698 | decimals=0; |
5699 | break; |
5700 | case SHOW_DOUBLE: |
5701 | decimals= 6; |
5702 | collation.set_numeric(); |
5703 | fix_char_length(DBL_DIG + 6); |
5704 | break; |
5705 | default: |
5706 | my_error(ER_VAR_CANT_BE_READ, MYF(0), var->name.str); |
5707 | break; |
5708 | } |
5709 | } |
5710 | |
5711 | |
5712 | void Item_func_get_system_var::print(String *str, enum_query_type query_type) |
5713 | { |
5714 | if (name.length) |
5715 | str->append(&name); |
5716 | else |
5717 | { |
5718 | str->append(STRING_WITH_LEN("@@" )); |
5719 | if (component.length) |
5720 | { |
5721 | str->append(&component); |
5722 | str->append('.'); |
5723 | } |
5724 | else if (var_type == SHOW_OPT_GLOBAL && var->scope() != sys_var::GLOBAL) |
5725 | { |
5726 | str->append(STRING_WITH_LEN("global." )); |
5727 | } |
5728 | str->append(&var->name); |
5729 | } |
5730 | } |
5731 | |
5732 | bool Item_func_get_system_var::check_vcol_func_processor(void *arg) |
5733 | { |
5734 | return mark_unsupported_function("@@" , var->name.str, arg, VCOL_SESSION_FUNC); |
5735 | } |
5736 | |
5737 | |
5738 | const Type_handler *Item_func_get_system_var::type_handler() const |
5739 | { |
5740 | switch (var->show_type()) |
5741 | { |
5742 | case SHOW_BOOL: |
5743 | case SHOW_MY_BOOL: |
5744 | case SHOW_SINT: |
5745 | case SHOW_SLONG: |
5746 | case SHOW_SLONGLONG: |
5747 | case SHOW_UINT: |
5748 | case SHOW_ULONG: |
5749 | case SHOW_ULONGLONG: |
5750 | case SHOW_HA_ROWS: |
5751 | return &type_handler_longlong; |
5752 | case SHOW_CHAR: |
5753 | case SHOW_CHAR_PTR: |
5754 | case SHOW_LEX_STRING: |
5755 | return &type_handler_varchar; |
5756 | case SHOW_DOUBLE: |
5757 | return &type_handler_double; |
5758 | default: |
5759 | my_error(ER_VAR_CANT_BE_READ, MYF(0), var->name.str); |
5760 | return &type_handler_varchar; // keep the compiler happy |
5761 | } |
5762 | } |
5763 | |
5764 | |
5765 | longlong Item_func_get_system_var::val_int() |
5766 | { |
5767 | THD *thd= current_thd; |
5768 | |
5769 | DBUG_EXECUTE_IF("simulate_non_gtid_aware_master" , |
5770 | { |
5771 | if (0 == strcmp("gtid_domain_id" , var->name.str)) |
5772 | { |
5773 | my_error(ER_VAR_CANT_BE_READ, MYF(0), var->name.str); |
5774 | return 0; |
5775 | } |
5776 | }); |
5777 | if (cache_present && thd->query_id == used_query_id) |
5778 | { |
5779 | if (cache_present & GET_SYS_VAR_CACHE_LONG) |
5780 | { |
5781 | null_value= cached_null_value; |
5782 | return cached_llval; |
5783 | } |
5784 | else if (cache_present & GET_SYS_VAR_CACHE_DOUBLE) |
5785 | { |
5786 | null_value= cached_null_value; |
5787 | cached_llval= (longlong) cached_dval; |
5788 | cache_present|= GET_SYS_VAR_CACHE_LONG; |
5789 | return cached_llval; |
5790 | } |
5791 | else if (cache_present & GET_SYS_VAR_CACHE_STRING) |
5792 | { |
5793 | null_value= cached_null_value; |
5794 | if (!null_value) |
5795 | cached_llval= longlong_from_string_with_check(&cached_strval); |
5796 | else |
5797 | cached_llval= 0; |
5798 | cache_present|= GET_SYS_VAR_CACHE_LONG; |
5799 | return cached_llval; |
5800 | } |
5801 | } |
5802 | |
5803 | cached_llval= var->val_int(&null_value, thd, var_type, &component); |
5804 | cache_present |= GET_SYS_VAR_CACHE_LONG; |
5805 | used_query_id= thd->query_id; |
5806 | cached_null_value= null_value; |
5807 | return cached_llval; |
5808 | } |
5809 | |
5810 | |
5811 | String* Item_func_get_system_var::val_str(String* str) |
5812 | { |
5813 | THD *thd= current_thd; |
5814 | |
5815 | if (cache_present && thd->query_id == used_query_id) |
5816 | { |
5817 | if (cache_present & GET_SYS_VAR_CACHE_STRING) |
5818 | { |
5819 | null_value= cached_null_value; |
5820 | return null_value ? NULL : &cached_strval; |
5821 | } |
5822 | else if (cache_present & GET_SYS_VAR_CACHE_LONG) |
5823 | { |
5824 | null_value= cached_null_value; |
5825 | if (!null_value) |
5826 | cached_strval.set (cached_llval, collation.collation); |
5827 | cache_present|= GET_SYS_VAR_CACHE_STRING; |
5828 | return null_value ? NULL : &cached_strval; |
5829 | } |
5830 | else if (cache_present & GET_SYS_VAR_CACHE_DOUBLE) |
5831 | { |
5832 | null_value= cached_null_value; |
5833 | if (!null_value) |
5834 | cached_strval.set_real (cached_dval, decimals, collation.collation); |
5835 | cache_present|= GET_SYS_VAR_CACHE_STRING; |
5836 | return null_value ? NULL : &cached_strval; |
5837 | } |
5838 | } |
5839 | |
5840 | str= var->val_str(&cached_strval, thd, var_type, &component); |
5841 | cache_present|= GET_SYS_VAR_CACHE_STRING; |
5842 | used_query_id= thd->query_id; |
5843 | cached_null_value= null_value= !str; |
5844 | return str; |
5845 | } |
5846 | |
5847 | |
5848 | double Item_func_get_system_var::val_real() |
5849 | { |
5850 | THD *thd= current_thd; |
5851 | |
5852 | if (cache_present && thd->query_id == used_query_id) |
5853 | { |
5854 | if (cache_present & GET_SYS_VAR_CACHE_DOUBLE) |
5855 | { |
5856 | null_value= cached_null_value; |
5857 | return cached_dval; |
5858 | } |
5859 | else if (cache_present & GET_SYS_VAR_CACHE_LONG) |
5860 | { |
5861 | null_value= cached_null_value; |
5862 | cached_dval= (double)cached_llval; |
5863 | cache_present|= GET_SYS_VAR_CACHE_DOUBLE; |
5864 | return cached_dval; |
5865 | } |
5866 | else if (cache_present & GET_SYS_VAR_CACHE_STRING) |
5867 | { |
5868 | null_value= cached_null_value; |
5869 | if (!null_value) |
5870 | cached_dval= double_from_string_with_check(&cached_strval); |
5871 | else |
5872 | cached_dval= 0; |
5873 | cache_present|= GET_SYS_VAR_CACHE_DOUBLE; |
5874 | return cached_dval; |
5875 | } |
5876 | } |
5877 | |
5878 | cached_dval= var->val_real(&null_value, thd, var_type, &component); |
5879 | cache_present |= GET_SYS_VAR_CACHE_DOUBLE; |
5880 | used_query_id= thd->query_id; |
5881 | cached_null_value= null_value; |
5882 | return cached_dval; |
5883 | } |
5884 | |
5885 | |
5886 | bool Item_func_get_system_var::eq(const Item *item, bool binary_cmp) const |
5887 | { |
5888 | /* Assume we don't have rtti */ |
5889 | if (this == item) |
5890 | return 1; // Same item is same. |
5891 | /* Check if other type is also a get_user_var() object */ |
5892 | if (item->type() != FUNC_ITEM || |
5893 | ((Item_func*) item)->functype() != functype()) |
5894 | return 0; |
5895 | Item_func_get_system_var *other=(Item_func_get_system_var*) item; |
5896 | return (var == other->var && var_type == other->var_type); |
5897 | } |
5898 | |
5899 | |
5900 | void Item_func_get_system_var::cleanup() |
5901 | { |
5902 | Item_func::cleanup(); |
5903 | cache_present= 0; |
5904 | var_type= orig_var_type; |
5905 | cached_strval.free(); |
5906 | } |
5907 | |
5908 | /** |
5909 | @retval |
5910 | 0 ok |
5911 | 1 OOM error |
5912 | */ |
5913 | |
5914 | bool Item_func_match::init_search(THD *thd, bool no_order) |
5915 | { |
5916 | DBUG_ENTER("Item_func_match::init_search" ); |
5917 | |
5918 | if (!table->file->get_table()) // the handler isn't opened yet |
5919 | DBUG_RETURN(0); |
5920 | |
5921 | /* Check if init_search() has been called before */ |
5922 | if (ft_handler) |
5923 | { |
5924 | if (join_key) |
5925 | table->file->ft_handler= ft_handler; |
5926 | DBUG_RETURN(0); |
5927 | } |
5928 | |
5929 | if (key == NO_SUCH_KEY) |
5930 | { |
5931 | List<Item> fields; |
5932 | fields.push_back(new (thd->mem_root) |
5933 | Item_string(thd, " " , 1, cmp_collation.collation), |
5934 | thd->mem_root); |
5935 | for (uint i= 1; i < arg_count; i++) |
5936 | fields.push_back(args[i]); |
5937 | concat_ws= new (thd->mem_root) Item_func_concat_ws(thd, fields); |
5938 | if (unlikely(thd->is_fatal_error)) |
5939 | DBUG_RETURN(1); // OOM in new or push_back |
5940 | /* |
5941 | Above function used only to get value and do not need fix_fields for it: |
5942 | Item_string - basic constant |
5943 | fields - fix_fields() was already called for this arguments |
5944 | Item_func_concat_ws - do not need fix_fields() to produce value |
5945 | */ |
5946 | concat_ws->quick_fix_field(); |
5947 | } |
5948 | |
5949 | if (master) |
5950 | { |
5951 | join_key= master->join_key= join_key | master->join_key; |
5952 | if (master->init_search(thd, no_order)) |
5953 | DBUG_RETURN(1); |
5954 | ft_handler= master->ft_handler; |
5955 | join_key= master->join_key; |
5956 | DBUG_RETURN(0); |
5957 | } |
5958 | |
5959 | String *ft_tmp= 0; |
5960 | |
5961 | // MATCH ... AGAINST (NULL) is meaningless, but possible |
5962 | if (!(ft_tmp=key_item()->val_str(&value))) |
5963 | { |
5964 | ft_tmp= &value; |
5965 | value.set("" , 0, cmp_collation.collation); |
5966 | } |
5967 | |
5968 | if (ft_tmp->charset() != cmp_collation.collation) |
5969 | { |
5970 | uint dummy_errors; |
5971 | if (search_value.copy(ft_tmp->ptr(), ft_tmp->length(), ft_tmp->charset(), |
5972 | cmp_collation.collation, &dummy_errors)) |
5973 | DBUG_RETURN(1); |
5974 | ft_tmp= &search_value; |
5975 | } |
5976 | |
5977 | if (join_key && !no_order) |
5978 | flags|=FT_SORTED; |
5979 | |
5980 | if (key != NO_SUCH_KEY) |
5981 | THD_STAGE_INFO(table->in_use, stage_fulltext_initialization); |
5982 | |
5983 | ft_handler= table->file->ft_init_ext(flags, key, ft_tmp); |
5984 | |
5985 | if (join_key) |
5986 | table->file->ft_handler=ft_handler; |
5987 | |
5988 | DBUG_RETURN(0); |
5989 | } |
5990 | |
5991 | |
5992 | bool Item_func_match::fix_fields(THD *thd, Item **ref) |
5993 | { |
5994 | DBUG_ASSERT(fixed == 0); |
5995 | Item *UNINIT_VAR(item); // Safe as arg_count is > 1 |
5996 | |
5997 | status_var_increment(thd->status_var.feature_fulltext); |
5998 | |
5999 | maybe_null=1; |
6000 | join_key=0; |
6001 | |
6002 | /* |
6003 | const_item is assumed in quite a bit of places, so it would be difficult |
6004 | to remove; If it would ever to be removed, this should include |
6005 | modifications to find_best and auto_close as complement to auto_init code |
6006 | above. |
6007 | */ |
6008 | if (Item_func::fix_fields(thd, ref) || |
6009 | !args[0]->const_during_execution()) |
6010 | { |
6011 | my_error(ER_WRONG_ARGUMENTS,MYF(0),"AGAINST" ); |
6012 | return TRUE; |
6013 | } |
6014 | |
6015 | bool allows_multi_table_search= true; |
6016 | const_item_cache=0; |
6017 | table= 0; |
6018 | for (uint i=1 ; i < arg_count ; i++) |
6019 | { |
6020 | item= args[i]= args[i]->real_item(); |
6021 | /* |
6022 | When running in PS mode, some Item_field's can already be replaced |
6023 | to Item_func_conv_charset during PREPARE time. This is possible |
6024 | in case of "MATCH (f1,..,fN) AGAINST (... IN BOOLEAN MODE)" |
6025 | when running without any fulltext indexes and when fields f1..fN |
6026 | have different character sets. |
6027 | So we check for FIELD_ITEM only during prepare time and in non-PS mode, |
6028 | and do not check in PS execute time. |
6029 | */ |
6030 | if (!thd->stmt_arena->is_stmt_execute() && |
6031 | item->type() != Item::FIELD_ITEM) |
6032 | { |
6033 | my_error(ER_WRONG_ARGUMENTS, MYF(0), "MATCH" ); |
6034 | return TRUE; |
6035 | } |
6036 | /* |
6037 | During the prepare-time execution of fix_fields() of a PS query some |
6038 | Item_fields's could have been already replaced to Item_func_conv_charset |
6039 | (by the call for agg_arg_charsets_for_comparison below()). |
6040 | But agg_arg_charsets_for_comparison() is written in a way that |
6041 | at least *one* of the Item_field's is not replaced. |
6042 | This makes sure that "table" gets initialized during PS execution time. |
6043 | */ |
6044 | if (item->type() == Item::FIELD_ITEM) |
6045 | table= ((Item_field *)item)->field->table; |
6046 | |
6047 | allows_multi_table_search &= allows_search_on_non_indexed_columns(table); |
6048 | } |
6049 | |
6050 | /* |
6051 | Check that all columns come from the same table. |
6052 | We've already checked that columns in MATCH are fields so |
6053 | PARAM_TABLE_BIT can only appear from AGAINST argument. |
6054 | */ |
6055 | if ((used_tables_cache & ~PARAM_TABLE_BIT) != item->used_tables()) |
6056 | key=NO_SUCH_KEY; |
6057 | |
6058 | if (key == NO_SUCH_KEY && !allows_multi_table_search) |
6059 | { |
6060 | my_error(ER_WRONG_ARGUMENTS,MYF(0),"MATCH" ); |
6061 | return TRUE; |
6062 | } |
6063 | if (!(table->file->ha_table_flags() & HA_CAN_FULLTEXT)) |
6064 | { |
6065 | my_error(ER_TABLE_CANT_HANDLE_FT, MYF(0), table->file->table_type()); |
6066 | return 1; |
6067 | } |
6068 | table->fulltext_searched=1; |
6069 | return agg_arg_charsets_for_comparison(cmp_collation, args+1, arg_count-1); |
6070 | } |
6071 | |
6072 | bool Item_func_match::fix_index() |
6073 | { |
6074 | Item_field *item; |
6075 | uint ft_to_key[MAX_KEY], ft_cnt[MAX_KEY], fts=0, keynr; |
6076 | uint max_cnt=0, mkeys=0, i; |
6077 | |
6078 | /* |
6079 | We will skip execution if the item is not fixed |
6080 | with fix_field |
6081 | */ |
6082 | if (!fixed) |
6083 | return false; |
6084 | |
6085 | if (key == NO_SUCH_KEY) |
6086 | return 0; |
6087 | |
6088 | if (!table) |
6089 | goto err; |
6090 | |
6091 | for (keynr=0 ; keynr < table->s->keys ; keynr++) |
6092 | { |
6093 | if ((table->key_info[keynr].flags & HA_FULLTEXT) && |
6094 | (flags & FT_BOOL ? table->keys_in_use_for_query.is_set(keynr) : |
6095 | table->s->keys_in_use.is_set(keynr))) |
6096 | |
6097 | { |
6098 | ft_to_key[fts]=keynr; |
6099 | ft_cnt[fts]=0; |
6100 | fts++; |
6101 | } |
6102 | } |
6103 | |
6104 | if (!fts) |
6105 | goto err; |
6106 | |
6107 | for (i=1; i < arg_count; i++) |
6108 | { |
6109 | if (args[i]->type() != FIELD_ITEM) |
6110 | goto err; |
6111 | item=(Item_field*)args[i]; |
6112 | for (keynr=0 ; keynr < fts ; keynr++) |
6113 | { |
6114 | KEY *ft_key=&table->key_info[ft_to_key[keynr]]; |
6115 | uint key_parts=ft_key->user_defined_key_parts; |
6116 | |
6117 | for (uint part=0 ; part < key_parts ; part++) |
6118 | { |
6119 | if (item->field->eq(ft_key->key_part[part].field)) |
6120 | ft_cnt[keynr]++; |
6121 | } |
6122 | } |
6123 | } |
6124 | |
6125 | for (keynr=0 ; keynr < fts ; keynr++) |
6126 | { |
6127 | if (ft_cnt[keynr] > max_cnt) |
6128 | { |
6129 | mkeys=0; |
6130 | max_cnt=ft_cnt[mkeys]=ft_cnt[keynr]; |
6131 | ft_to_key[mkeys]=ft_to_key[keynr]; |
6132 | continue; |
6133 | } |
6134 | if (max_cnt && ft_cnt[keynr] == max_cnt) |
6135 | { |
6136 | mkeys++; |
6137 | ft_cnt[mkeys]=ft_cnt[keynr]; |
6138 | ft_to_key[mkeys]=ft_to_key[keynr]; |
6139 | continue; |
6140 | } |
6141 | } |
6142 | |
6143 | for (keynr=0 ; keynr <= mkeys ; keynr++) |
6144 | { |
6145 | // partial keys doesn't work |
6146 | if (max_cnt < arg_count-1 || |
6147 | max_cnt < table->key_info[ft_to_key[keynr]].user_defined_key_parts) |
6148 | continue; |
6149 | |
6150 | key=ft_to_key[keynr]; |
6151 | |
6152 | return 0; |
6153 | } |
6154 | |
6155 | err: |
6156 | if (allows_search_on_non_indexed_columns(table)) |
6157 | { |
6158 | key=NO_SUCH_KEY; |
6159 | return 0; |
6160 | } |
6161 | my_message(ER_FT_MATCHING_KEY_NOT_FOUND, |
6162 | ER(ER_FT_MATCHING_KEY_NOT_FOUND), MYF(0)); |
6163 | return 1; |
6164 | } |
6165 | |
6166 | |
6167 | bool Item_func_match::eq(const Item *item, bool binary_cmp) const |
6168 | { |
6169 | if (item->type() != FUNC_ITEM || |
6170 | ((Item_func*)item)->functype() != FT_FUNC || |
6171 | flags != ((Item_func_match*)item)->flags) |
6172 | return 0; |
6173 | |
6174 | Item_func_match *ifm=(Item_func_match*) item; |
6175 | |
6176 | if (key == ifm->key && table == ifm->table && |
6177 | key_item()->eq(ifm->key_item(), binary_cmp)) |
6178 | return 1; |
6179 | |
6180 | return 0; |
6181 | } |
6182 | |
6183 | |
6184 | double Item_func_match::val_real() |
6185 | { |
6186 | DBUG_ASSERT(fixed == 1); |
6187 | DBUG_ENTER("Item_func_match::val" ); |
6188 | if (ft_handler == NULL) |
6189 | DBUG_RETURN(-1.0); |
6190 | |
6191 | if (key != NO_SUCH_KEY && table->null_row) /* NULL row from an outer join */ |
6192 | DBUG_RETURN(0.0); |
6193 | |
6194 | if (join_key) |
6195 | { |
6196 | if (table->file->ft_handler) |
6197 | DBUG_RETURN(ft_handler->please->get_relevance(ft_handler)); |
6198 | join_key=0; |
6199 | } |
6200 | |
6201 | if (key == NO_SUCH_KEY) |
6202 | { |
6203 | String *a= concat_ws->val_str(&value); |
6204 | if ((null_value= (a == 0)) || !a->length()) |
6205 | DBUG_RETURN(0); |
6206 | DBUG_RETURN(ft_handler->please->find_relevance(ft_handler, |
6207 | (uchar *)a->ptr(), a->length())); |
6208 | } |
6209 | DBUG_RETURN(ft_handler->please->find_relevance(ft_handler, |
6210 | table->record[0], 0)); |
6211 | } |
6212 | |
6213 | void Item_func_match::print(String *str, enum_query_type query_type) |
6214 | { |
6215 | str->append(STRING_WITH_LEN("(match " )); |
6216 | print_args(str, 1, query_type); |
6217 | str->append(STRING_WITH_LEN(" against (" )); |
6218 | args[0]->print(str, query_type); |
6219 | if (flags & FT_BOOL) |
6220 | str->append(STRING_WITH_LEN(" in boolean mode" )); |
6221 | else if (flags & FT_EXPAND) |
6222 | str->append(STRING_WITH_LEN(" with query expansion" )); |
6223 | str->append(STRING_WITH_LEN("))" )); |
6224 | } |
6225 | |
6226 | longlong Item_func_bit_xor::val_int() |
6227 | { |
6228 | DBUG_ASSERT(fixed == 1); |
6229 | ulonglong arg1= (ulonglong) args[0]->val_int(); |
6230 | ulonglong arg2= (ulonglong) args[1]->val_int(); |
6231 | if ((null_value= (args[0]->null_value || args[1]->null_value))) |
6232 | return 0; |
6233 | return (longlong) (arg1 ^ arg2); |
6234 | } |
6235 | |
6236 | |
6237 | /*************************************************************************** |
6238 | System variables |
6239 | ****************************************************************************/ |
6240 | |
6241 | /** |
6242 | Return value of an system variable base[.name] as a constant item. |
6243 | |
6244 | @param thd Thread handler |
6245 | @param var_type global / session |
6246 | @param name Name of base or system variable |
6247 | @param component Component. |
6248 | |
6249 | @note |
6250 | If component.str = 0 then the variable name is in 'name' |
6251 | |
6252 | @return |
6253 | - 0 : error |
6254 | - # : constant item |
6255 | */ |
6256 | |
6257 | |
6258 | Item *get_system_var(THD *thd, enum_var_type var_type, |
6259 | const LEX_CSTRING *name, |
6260 | const LEX_CSTRING *component) |
6261 | { |
6262 | sys_var *var; |
6263 | LEX_CSTRING base_name, component_name; |
6264 | |
6265 | if (component->str) |
6266 | { |
6267 | base_name= *component; |
6268 | component_name= *name; |
6269 | } |
6270 | else |
6271 | { |
6272 | base_name= *name; |
6273 | component_name= *component; // Empty string |
6274 | } |
6275 | |
6276 | if (!(var= find_sys_var(thd, base_name.str, base_name.length))) |
6277 | return 0; |
6278 | if (component->str) |
6279 | { |
6280 | if (!var->is_struct()) |
6281 | { |
6282 | my_error(ER_VARIABLE_IS_NOT_STRUCT, MYF(0), base_name.str); |
6283 | return 0; |
6284 | } |
6285 | } |
6286 | thd->lex->uncacheable(UNCACHEABLE_SIDEEFFECT); |
6287 | |
6288 | set_if_smaller(component_name.length, MAX_SYS_VAR_LENGTH); |
6289 | |
6290 | return new (thd->mem_root) Item_func_get_system_var(thd, var, var_type, |
6291 | &component_name, |
6292 | NULL, 0); |
6293 | } |
6294 | |
6295 | |
6296 | longlong Item_func_row_count::val_int() |
6297 | { |
6298 | DBUG_ASSERT(fixed == 1); |
6299 | THD *thd= current_thd; |
6300 | |
6301 | return thd->get_row_count_func(); |
6302 | } |
6303 | |
6304 | |
6305 | |
6306 | |
6307 | Item_func_sp::Item_func_sp(THD *thd, Name_resolution_context *context_arg, |
6308 | sp_name *name, const Sp_handler *sph): |
6309 | Item_func(thd), Item_sp(thd, context_arg, name), m_handler(sph) |
6310 | { |
6311 | maybe_null= 1; |
6312 | } |
6313 | |
6314 | |
6315 | Item_func_sp::Item_func_sp(THD *thd, Name_resolution_context *context_arg, |
6316 | sp_name *name_arg, const Sp_handler *sph, |
6317 | List<Item> &list): |
6318 | Item_func(thd, list), Item_sp(thd, context_arg, name_arg), m_handler(sph) |
6319 | { |
6320 | maybe_null= 1; |
6321 | } |
6322 | |
6323 | |
6324 | void |
6325 | Item_func_sp::cleanup() |
6326 | { |
6327 | Item_sp::cleanup(); |
6328 | Item_func::cleanup(); |
6329 | } |
6330 | |
6331 | const char * |
6332 | Item_func_sp::func_name() const |
6333 | { |
6334 | THD *thd= current_thd; |
6335 | return Item_sp::func_name(thd); |
6336 | } |
6337 | |
6338 | |
6339 | void my_missing_function_error(const LEX_CSTRING &token, const char *func_name) |
6340 | { |
6341 | if (token.length && is_lex_native_function (&token)) |
6342 | my_error(ER_FUNC_INEXISTENT_NAME_COLLISION, MYF(0), func_name); |
6343 | else |
6344 | my_error(ER_SP_DOES_NOT_EXIST, MYF(0), "FUNCTION" , func_name); |
6345 | } |
6346 | |
6347 | |
6348 | /** |
6349 | @note |
6350 | Deterministic stored procedures are considered inexpensive. |
6351 | Consequently such procedures may be evaluated during optimization, |
6352 | if they are constant (checked by the optimizer). |
6353 | */ |
6354 | |
6355 | bool Item_func_sp::is_expensive() |
6356 | { |
6357 | return !m_sp->detistic() || |
6358 | current_thd->locked_tables_mode < LTM_LOCK_TABLES; |
6359 | } |
6360 | |
6361 | |
6362 | /** |
6363 | @brief Initialize local members with values from the Field interface. |
6364 | |
6365 | @note called from Item::fix_fields. |
6366 | */ |
6367 | |
6368 | void Item_func_sp::fix_length_and_dec() |
6369 | { |
6370 | DBUG_ENTER("Item_func_sp::fix_length_and_dec" ); |
6371 | |
6372 | DBUG_ASSERT(sp_result_field); |
6373 | Type_std_attributes::set(sp_result_field->type_std_attributes()); |
6374 | // There is a bug in the line below. See MDEV-11292 for details. |
6375 | collation.derivation= DERIVATION_COERCIBLE; |
6376 | maybe_null= 1; |
6377 | |
6378 | DBUG_VOID_RETURN; |
6379 | } |
6380 | |
6381 | |
6382 | bool |
6383 | Item_func_sp::execute() |
6384 | { |
6385 | /* Execute function and store the return value in the field. */ |
6386 | return Item_sp::execute(current_thd, &null_value, args, arg_count); |
6387 | } |
6388 | |
6389 | |
6390 | void |
6391 | Item_func_sp::make_send_field(THD *thd, Send_field *tmp_field) |
6392 | { |
6393 | DBUG_ENTER("Item_func_sp::make_send_field" ); |
6394 | DBUG_ASSERT(sp_result_field); |
6395 | sp_result_field->make_send_field(tmp_field); |
6396 | if (name.str) |
6397 | { |
6398 | DBUG_ASSERT(name.length == strlen(name.str)); |
6399 | tmp_field->col_name= name; |
6400 | } |
6401 | DBUG_VOID_RETURN; |
6402 | } |
6403 | |
6404 | |
6405 | const Type_handler *Item_func_sp::type_handler() const |
6406 | { |
6407 | DBUG_ENTER("Item_func_sp::type_handler" ); |
6408 | DBUG_PRINT("info" , ("m_sp = %p" , (void *) m_sp)); |
6409 | DBUG_ASSERT(sp_result_field); |
6410 | // This converts ENUM/SET to STRING |
6411 | const Type_handler *handler= sp_result_field->type_handler(); |
6412 | DBUG_RETURN(handler->type_handler_for_item_field()); |
6413 | } |
6414 | |
6415 | |
6416 | longlong Item_func_found_rows::val_int() |
6417 | { |
6418 | DBUG_ASSERT(fixed == 1); |
6419 | return current_thd->found_rows(); |
6420 | } |
6421 | |
6422 | |
6423 | longlong Item_func_oracle_sql_rowcount::val_int() |
6424 | { |
6425 | DBUG_ASSERT(fixed == 1); |
6426 | THD *thd= current_thd; |
6427 | /* |
6428 | In case when a query like this: |
6429 | INSERT a INTO @va FROM t1; |
6430 | returns multiple rows, SQL%ROWCOUNT should report 1 rather than -1. |
6431 | */ |
6432 | longlong rows= thd->get_row_count_func(); |
6433 | return rows != -1 ? rows : // ROW_COUNT() |
6434 | thd->found_rows(); // FOUND_ROWS() |
6435 | } |
6436 | |
6437 | |
6438 | longlong Item_func_sqlcode::val_int() |
6439 | { |
6440 | DBUG_ASSERT(fixed); |
6441 | DBUG_ASSERT(!null_value); |
6442 | Diagnostics_area::Sql_condition_iterator it= |
6443 | current_thd->get_stmt_da()->sql_conditions(); |
6444 | const Sql_condition *err; |
6445 | if ((err= it++)) |
6446 | return err->get_sql_errno(); |
6447 | return 0; |
6448 | } |
6449 | |
6450 | |
6451 | bool |
6452 | Item_func_sp::fix_fields(THD *thd, Item **ref) |
6453 | { |
6454 | bool res; |
6455 | DBUG_ENTER("Item_func_sp::fix_fields" ); |
6456 | DBUG_ASSERT(fixed == 0); |
6457 | sp_head *sp= m_handler->sp_find_routine(thd, m_name, true); |
6458 | |
6459 | /* |
6460 | Checking privileges to execute the function while creating view and |
6461 | executing the function of select. |
6462 | */ |
6463 | if (!(thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW) || |
6464 | (thd->lex->sql_command == SQLCOM_CREATE_VIEW)) |
6465 | { |
6466 | Security_context *save_security_ctx= thd->security_ctx; |
6467 | if (context->security_ctx) |
6468 | thd->security_ctx= context->security_ctx; |
6469 | |
6470 | /* |
6471 | If the routine is not found, let's still check EXECUTE_ACL to decide |
6472 | whether to return "Access denied" or "Routine does not exist". |
6473 | */ |
6474 | res= sp ? sp->check_execute_access(thd) : |
6475 | check_routine_access(thd, EXECUTE_ACL, &m_name->m_db, |
6476 | &m_name->m_name, |
6477 | &sp_handler_function, false); |
6478 | thd->security_ctx= save_security_ctx; |
6479 | |
6480 | if (res) |
6481 | { |
6482 | context->process_error(thd); |
6483 | DBUG_RETURN(res); |
6484 | } |
6485 | } |
6486 | |
6487 | |
6488 | /* Custom aggregates are transformed into an Item_sum_sp. We can not do this |
6489 | earlier as we have no way of knowing what kind of Item we should create |
6490 | when parsing the query. |
6491 | |
6492 | TODO(cvicentiu): See if this limitation can be lifted. |
6493 | */ |
6494 | |
6495 | DBUG_ASSERT(m_sp == NULL); |
6496 | if (!(m_sp= sp)) |
6497 | { |
6498 | my_missing_function_error(m_name->m_name, ErrConvDQName(m_name).ptr()); |
6499 | context->process_error(thd); |
6500 | DBUG_RETURN(TRUE); |
6501 | } |
6502 | |
6503 | /* |
6504 | We must call init_result_field before Item_func::fix_fields() |
6505 | to make m_sp and result_field members available to fix_length_and_dec(), |
6506 | which is called from Item_func::fix_fields(). |
6507 | */ |
6508 | res= init_result_field(thd, max_length, maybe_null, &null_value, &name); |
6509 | |
6510 | if (res) |
6511 | DBUG_RETURN(TRUE); |
6512 | |
6513 | if (m_sp->agg_type() == GROUP_AGGREGATE) |
6514 | { |
6515 | List<Item> list; |
6516 | list.empty(); |
6517 | for (uint i=0; i < arg_count; i++) |
6518 | list.push_back(*(args+i)); |
6519 | |
6520 | Item_sum_sp *item_sp; |
6521 | Query_arena *arena, backup; |
6522 | arena= thd->activate_stmt_arena_if_needed(&backup); |
6523 | |
6524 | if (arg_count) |
6525 | item_sp= new (thd->mem_root) Item_sum_sp(thd, context, m_name, sp, list); |
6526 | else |
6527 | item_sp= new (thd->mem_root) Item_sum_sp(thd, context, m_name, sp); |
6528 | |
6529 | if (arena) |
6530 | thd->restore_active_arena(arena, &backup); |
6531 | if (!item_sp) |
6532 | DBUG_RETURN(TRUE); |
6533 | *ref= item_sp; |
6534 | item_sp->name= name; |
6535 | bool err= item_sp->fix_fields(thd, ref); |
6536 | if (err) |
6537 | DBUG_RETURN(TRUE); |
6538 | |
6539 | list.empty(); |
6540 | DBUG_RETURN(FALSE); |
6541 | } |
6542 | |
6543 | res= Item_func::fix_fields(thd, ref); |
6544 | |
6545 | if (res) |
6546 | DBUG_RETURN(TRUE); |
6547 | |
6548 | if (thd->lex->is_view_context_analysis()) |
6549 | { |
6550 | /* |
6551 | Here we check privileges of the stored routine only during view |
6552 | creation, in order to validate the view. A runtime check is |
6553 | perfomed in Item_func_sp::execute(), and this method is not |
6554 | called during context analysis. Notice, that during view |
6555 | creation we do not infer into stored routine bodies and do not |
6556 | check privileges of its statements, which would probably be a |
6557 | good idea especially if the view has SQL SECURITY DEFINER and |
6558 | the used stored procedure has SQL SECURITY DEFINER. |
6559 | */ |
6560 | res= sp_check_access(thd); |
6561 | #ifndef NO_EMBEDDED_ACCESS_CHECKS |
6562 | /* |
6563 | Try to set and restore the security context to see whether it's valid |
6564 | */ |
6565 | Security_context *save_secutiry_ctx; |
6566 | res= set_routine_security_ctx(thd, m_sp, &save_secutiry_ctx); |
6567 | if (!res) |
6568 | m_sp->m_security_ctx.restore_security_context(thd, save_secutiry_ctx); |
6569 | |
6570 | #endif /* ! NO_EMBEDDED_ACCESS_CHECKS */ |
6571 | } |
6572 | |
6573 | if (!m_sp->detistic()) |
6574 | { |
6575 | used_tables_cache |= RAND_TABLE_BIT; |
6576 | const_item_cache= FALSE; |
6577 | } |
6578 | |
6579 | DBUG_RETURN(res); |
6580 | } |
6581 | |
6582 | |
6583 | void Item_func_sp::update_used_tables() |
6584 | { |
6585 | Item_func::update_used_tables(); |
6586 | |
6587 | if (!m_sp->detistic()) |
6588 | { |
6589 | used_tables_cache |= RAND_TABLE_BIT; |
6590 | const_item_cache= FALSE; |
6591 | } |
6592 | } |
6593 | |
6594 | bool Item_func_sp::check_vcol_func_processor(void *arg) |
6595 | { |
6596 | return mark_unsupported_function(func_name(), "()" , arg, VCOL_IMPOSSIBLE); |
6597 | } |
6598 | |
6599 | /* |
6600 | uuid_short handling. |
6601 | |
6602 | The short uuid is defined as a longlong that contains the following bytes: |
6603 | |
6604 | Bytes Comment |
6605 | 1 Server_id & 255 |
6606 | 4 Startup time of server in seconds |
6607 | 3 Incrementor |
6608 | |
6609 | This means that an uuid is guaranteed to be unique |
6610 | even in a replication environment if the following holds: |
6611 | |
6612 | - The last byte of the server id is unique |
6613 | - If you between two shutdown of the server don't get more than |
6614 | an average of 2^24 = 16M calls to uuid_short() per second. |
6615 | */ |
6616 | |
6617 | ulonglong uuid_value; |
6618 | |
6619 | void uuid_short_init() |
6620 | { |
6621 | uuid_value= ((((ulonglong) global_system_variables.server_id) << 56) + |
6622 | (((ulonglong) server_start_time) << 24)); |
6623 | } |
6624 | |
6625 | |
6626 | longlong Item_func_uuid_short::val_int() |
6627 | { |
6628 | ulonglong val; |
6629 | mysql_mutex_lock(&LOCK_short_uuid_generator); |
6630 | val= uuid_value++; |
6631 | mysql_mutex_unlock(&LOCK_short_uuid_generator); |
6632 | return (longlong) val; |
6633 | } |
6634 | |
6635 | |
6636 | /** |
6637 | Last_value - return last argument. |
6638 | */ |
6639 | |
6640 | void Item_func_last_value::evaluate_sideeffects() |
6641 | { |
6642 | DBUG_ASSERT(fixed == 1 && arg_count > 0); |
6643 | for (uint i= 0; i < arg_count-1 ; i++) |
6644 | args[i]->val_int(); |
6645 | } |
6646 | |
6647 | String *Item_func_last_value::val_str(String *str) |
6648 | { |
6649 | String *tmp; |
6650 | evaluate_sideeffects(); |
6651 | tmp= last_value->val_str(str); |
6652 | null_value= last_value->null_value; |
6653 | return tmp; |
6654 | } |
6655 | |
6656 | longlong Item_func_last_value::val_int() |
6657 | { |
6658 | longlong tmp; |
6659 | evaluate_sideeffects(); |
6660 | tmp= last_value->val_int(); |
6661 | null_value= last_value->null_value; |
6662 | return tmp; |
6663 | } |
6664 | |
6665 | double Item_func_last_value::val_real() |
6666 | { |
6667 | double tmp; |
6668 | evaluate_sideeffects(); |
6669 | tmp= last_value->val_real(); |
6670 | null_value= last_value->null_value; |
6671 | return tmp; |
6672 | } |
6673 | |
6674 | my_decimal *Item_func_last_value::val_decimal(my_decimal *decimal_value) |
6675 | { |
6676 | my_decimal *tmp; |
6677 | evaluate_sideeffects(); |
6678 | tmp= last_value->val_decimal(decimal_value); |
6679 | null_value= last_value->null_value; |
6680 | return tmp; |
6681 | } |
6682 | |
6683 | |
6684 | bool Item_func_last_value::get_date(MYSQL_TIME *ltime, ulonglong fuzzydate) |
6685 | { |
6686 | evaluate_sideeffects(); |
6687 | bool tmp= last_value->get_date(ltime, fuzzydate); |
6688 | null_value= last_value->null_value; |
6689 | return tmp; |
6690 | } |
6691 | |
6692 | |
6693 | void Item_func_last_value::fix_length_and_dec() |
6694 | { |
6695 | last_value= args[arg_count -1]; |
6696 | Type_std_attributes::set(last_value); |
6697 | maybe_null= last_value->maybe_null; |
6698 | } |
6699 | |
6700 | |
6701 | void Cursor_ref::print_func(String *str, const char *func_name) |
6702 | { |
6703 | append_identifier(current_thd, str, &m_cursor_name); |
6704 | str->append(func_name); |
6705 | } |
6706 | |
6707 | |
6708 | sp_cursor *Cursor_ref::get_open_cursor_or_error() |
6709 | { |
6710 | THD *thd= current_thd; |
6711 | sp_cursor *c= thd->spcont->get_cursor(m_cursor_offset); |
6712 | DBUG_ASSERT(c); |
6713 | if (!c/*safety*/ || !c->is_open()) |
6714 | { |
6715 | my_message(ER_SP_CURSOR_NOT_OPEN, ER_THD(thd, ER_SP_CURSOR_NOT_OPEN), |
6716 | MYF(0)); |
6717 | return NULL; |
6718 | } |
6719 | return c; |
6720 | } |
6721 | |
6722 | |
6723 | longlong Item_func_cursor_isopen::val_int() |
6724 | { |
6725 | sp_cursor *c= current_thd->spcont->get_cursor(m_cursor_offset); |
6726 | DBUG_ASSERT(c != NULL); |
6727 | return c ? c->is_open() : 0; |
6728 | } |
6729 | |
6730 | |
6731 | longlong Item_func_cursor_found::val_int() |
6732 | { |
6733 | sp_cursor *c= get_open_cursor_or_error(); |
6734 | return !(null_value= (!c || c->fetch_count() == 0)) && c->found(); |
6735 | } |
6736 | |
6737 | |
6738 | longlong Item_func_cursor_notfound::val_int() |
6739 | { |
6740 | sp_cursor *c= get_open_cursor_or_error(); |
6741 | return !(null_value= (!c || c->fetch_count() == 0)) && !c->found(); |
6742 | } |
6743 | |
6744 | |
6745 | longlong Item_func_cursor_rowcount::val_int() |
6746 | { |
6747 | sp_cursor *c= get_open_cursor_or_error(); |
6748 | return !(null_value= !c) ? c->row_count() : 0; |
6749 | } |
6750 | |
6751 | /***************************************************************************** |
6752 | SEQUENCE functions |
6753 | *****************************************************************************/ |
6754 | |
6755 | longlong Item_func_nextval::val_int() |
6756 | { |
6757 | longlong value; |
6758 | int error; |
6759 | const char *key; |
6760 | uint length= get_table_def_key(table_list, &key); |
6761 | THD *thd; |
6762 | SEQUENCE_LAST_VALUE *entry; |
6763 | char buff[80]; |
6764 | String key_buff(buff,sizeof(buff), &my_charset_bin); |
6765 | DBUG_ENTER("Item_func_nextval::val_int" ); |
6766 | update_table(); |
6767 | DBUG_ASSERT(table && table->s->sequence); |
6768 | thd= table->in_use; |
6769 | |
6770 | if (thd->count_cuted_fields == CHECK_FIELD_EXPRESSION) |
6771 | { |
6772 | /* Alter table checking if function works */ |
6773 | null_value= 0; |
6774 | DBUG_RETURN(0); |
6775 | } |
6776 | |
6777 | if (table->s->tmp_table != NO_TMP_TABLE) |
6778 | { |
6779 | /* |
6780 | Temporary tables has an extra \0 at end to distinguish it from |
6781 | normal tables |
6782 | */ |
6783 | key_buff.copy(key, length, &my_charset_bin); |
6784 | key_buff.append((char) 0); |
6785 | key= key_buff.ptr(); |
6786 | length++; |
6787 | } |
6788 | |
6789 | if (!(entry= ((SEQUENCE_LAST_VALUE*) |
6790 | my_hash_search(&thd->sequences, (uchar*) key, length)))) |
6791 | { |
6792 | if (!(key= (char*) my_memdup(key, length, MYF(MY_WME))) || |
6793 | !(entry= new SEQUENCE_LAST_VALUE((uchar*) key, length))) |
6794 | { |
6795 | /* EOM, error given */ |
6796 | my_free((char*) key); |
6797 | delete entry; |
6798 | null_value= 1; |
6799 | DBUG_RETURN(0); |
6800 | } |
6801 | if (my_hash_insert(&thd->sequences, (uchar*) entry)) |
6802 | { |
6803 | /* EOM, error given */ |
6804 | delete entry; |
6805 | null_value= 1; |
6806 | DBUG_RETURN(0); |
6807 | } |
6808 | } |
6809 | entry->null_value= null_value= 0; |
6810 | value= table->s->sequence->next_value(table, 0, &error); |
6811 | entry->value= value; |
6812 | entry->set_version(table); |
6813 | |
6814 | if (unlikely(error)) // Warning already printed |
6815 | entry->null_value= null_value= 1; // For not strict mode |
6816 | DBUG_RETURN(value); |
6817 | } |
6818 | |
6819 | |
6820 | /* Print for nextval and lastval */ |
6821 | |
6822 | void Item_func_nextval::print(String *str, enum_query_type query_type) |
6823 | { |
6824 | char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME]; |
6825 | LEX_CSTRING d_name= table_list->db; |
6826 | LEX_CSTRING t_name= table_list->table_name; |
6827 | bool use_db_name= d_name.str && d_name.str[0]; |
6828 | THD *thd= current_thd; // Don't trust 'table' |
6829 | |
6830 | str->append(func_name()); |
6831 | str->append('('); |
6832 | |
6833 | /* |
6834 | for next_val we assume that table_list has been updated to contain |
6835 | the current db. |
6836 | */ |
6837 | |
6838 | if (lower_case_table_names > 0) |
6839 | { |
6840 | strmake(t_name_buff, t_name.str, MAX_ALIAS_NAME-1); |
6841 | t_name.length= my_casedn_str(files_charset_info, t_name_buff); |
6842 | t_name.str= t_name_buff; |
6843 | if (use_db_name) |
6844 | { |
6845 | strmake(d_name_buff, d_name.str, MAX_ALIAS_NAME-1); |
6846 | d_name.length= my_casedn_str(files_charset_info, d_name_buff); |
6847 | d_name.str= d_name_buff; |
6848 | } |
6849 | } |
6850 | |
6851 | if (use_db_name) |
6852 | { |
6853 | append_identifier(thd, str, &d_name); |
6854 | str->append('.'); |
6855 | } |
6856 | append_identifier(thd, str, &t_name); |
6857 | str->append(')'); |
6858 | } |
6859 | |
6860 | |
6861 | /* Return last used value for sequence or NULL if sequence hasn't been used */ |
6862 | |
6863 | longlong Item_func_lastval::val_int() |
6864 | { |
6865 | const char *key; |
6866 | SEQUENCE_LAST_VALUE *entry; |
6867 | uint length= get_table_def_key(table_list, &key); |
6868 | THD *thd; |
6869 | char buff[80]; |
6870 | String key_buff(buff,sizeof(buff), &my_charset_bin); |
6871 | DBUG_ENTER("Item_func_lastval::val_int" ); |
6872 | update_table(); |
6873 | thd= table->in_use; |
6874 | |
6875 | if (table->s->tmp_table != NO_TMP_TABLE) |
6876 | { |
6877 | /* |
6878 | Temporary tables has an extra \0 at end to distinguish it from |
6879 | normal tables |
6880 | */ |
6881 | key_buff.copy(key, length, &my_charset_bin); |
6882 | key_buff.append((char) 0); |
6883 | key= key_buff.ptr(); |
6884 | length++; |
6885 | } |
6886 | |
6887 | if (!(entry= ((SEQUENCE_LAST_VALUE*) |
6888 | my_hash_search(&thd->sequences, (uchar*) key, length)))) |
6889 | { |
6890 | /* Sequence not used */ |
6891 | null_value= 1; |
6892 | DBUG_RETURN(0); |
6893 | } |
6894 | if (entry->check_version(table)) |
6895 | { |
6896 | /* Table droped and re-created, remove current version */ |
6897 | my_hash_delete(&thd->sequences, (uchar*) entry); |
6898 | null_value= 1; |
6899 | DBUG_RETURN(0); |
6900 | } |
6901 | |
6902 | null_value= entry->null_value; |
6903 | DBUG_RETURN(entry->value); |
6904 | } |
6905 | |
6906 | |
6907 | /* |
6908 | Sets next value to be returned from sequences |
6909 | |
6910 | SELECT setval('foo', 42, 0); Next nextval will return 43 |
6911 | SELECT setval('foo', 42, 0, true); Same as above |
6912 | SELECT setval('foo', 42, 0, false); Next nextval will return 42 |
6913 | */ |
6914 | |
6915 | longlong Item_func_setval::val_int() |
6916 | { |
6917 | longlong value; |
6918 | int error; |
6919 | THD *thd; |
6920 | DBUG_ENTER("Item_func_setval::val_int" ); |
6921 | |
6922 | update_table(); |
6923 | DBUG_ASSERT(table && table->s->sequence); |
6924 | thd= table->in_use; |
6925 | |
6926 | if (unlikely(thd->count_cuted_fields == CHECK_FIELD_EXPRESSION)) |
6927 | { |
6928 | /* Alter table checking if function works */ |
6929 | null_value= 0; |
6930 | DBUG_RETURN(0); |
6931 | } |
6932 | |
6933 | value= nextval; |
6934 | error= table->s->sequence->set_value(table, nextval, round, is_used); |
6935 | if (unlikely(error)) |
6936 | { |
6937 | null_value= 1; |
6938 | value= 0; |
6939 | } |
6940 | DBUG_RETURN(value); |
6941 | } |
6942 | |
6943 | |
6944 | /* Print for setval */ |
6945 | |
6946 | void Item_func_setval::print(String *str, enum_query_type query_type) |
6947 | { |
6948 | char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME]; |
6949 | LEX_CSTRING d_name= table_list->db; |
6950 | LEX_CSTRING t_name= table_list->table_name; |
6951 | bool use_db_name= d_name.str && d_name.str[0]; |
6952 | THD *thd= current_thd; // Don't trust 'table' |
6953 | |
6954 | str->append(func_name()); |
6955 | str->append('('); |
6956 | |
6957 | /* |
6958 | for next_val we assume that table_list has been updated to contain |
6959 | the current db. |
6960 | */ |
6961 | |
6962 | if (lower_case_table_names > 0) |
6963 | { |
6964 | strmake(t_name_buff, t_name.str, MAX_ALIAS_NAME-1); |
6965 | t_name.length= my_casedn_str(files_charset_info, t_name_buff); |
6966 | t_name.str= t_name_buff; |
6967 | if (use_db_name) |
6968 | { |
6969 | strmake(d_name_buff, d_name.str, MAX_ALIAS_NAME-1); |
6970 | d_name.length= my_casedn_str(files_charset_info, d_name_buff); |
6971 | d_name.str= d_name_buff; |
6972 | } |
6973 | } |
6974 | |
6975 | if (use_db_name) |
6976 | { |
6977 | append_identifier(thd, str, &d_name); |
6978 | str->append('.'); |
6979 | } |
6980 | append_identifier(thd, str, &t_name); |
6981 | str->append(','); |
6982 | str->append_longlong(nextval); |
6983 | str->append(','); |
6984 | str->append_longlong(is_used); |
6985 | str->append(','); |
6986 | str->append_ulonglong(round); |
6987 | str->append(')'); |
6988 | } |
6989 | |