drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
cmpfunc.h
1/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3 *
4 * Copyright (C) 2008 Sun Microsystems, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#pragma once
21
22/* compare and test functions */
23
24#include <drizzled/common.h>
25#include <drizzled/comp_creator.h>
26#include <drizzled/function/math/int.h>
27#include <drizzled/function/numhybrid.h>
28#include <drizzled/item/decimal.h>
29#include <drizzled/item/float.h>
30#include <drizzled/item/function/boolean.h>
31#include <drizzled/item/int.h>
32#include <drizzled/item/row.h>
33#include <drizzled/item/string.h>
34#include <drizzled/item/sum.h>
35#include <drizzled/qsort_cmp.h>
36#include <climits>
37
38#include "drizzled/compare_double.h"
39
40namespace drizzled {
41
42extern Item_result item_cmp_type(Item_result a,Item_result b);
43
44typedef int (Arg_comparator::*arg_cmp_func)();
45
46typedef int (*Item_field_cmpfunc)(Item_field *f1, Item_field *f2, void *arg);
47
48int64_t get_datetime_value(Session *session,
49 Item ***item_arg,
50 Item **cache_arg,
51 Item *warn_item,
52 bool *is_null);
53
54class Arg_comparator: public memory::SqlAlloc
55{
56 Item **a, **b;
57 arg_cmp_func func;
58 Item_bool_func2 *owner;
59 Arg_comparator *comparators; // used only for compare_row()
60 double precision;
61 /* Fields used in DATE/DATETIME comparison. */
62 Session *session;
63 enum_field_types a_type, b_type; // Types of a and b items
64 Item *a_cache, *b_cache; // Cached values of a and b items
65 bool is_nulls_eq; // TRUE <=> compare for the EQUAL_FUNC
66 enum enum_date_cmp_type { CMP_DATE_DFLT= 0, CMP_DATE_WITH_DATE,
67 CMP_DATE_WITH_STR, CMP_STR_WITH_DATE };
68 int64_t (*get_value_func)(Session *session, Item ***item_arg, Item **cache_arg,
69 Item *warn_item, bool *is_null);
70public:
71 DTCollation cmp_collation;
72
73 Arg_comparator();
74
75 Arg_comparator(Item **a1, Item **a2);
76
77 int set_compare_func(Item_bool_func2 *owner, Item_result type);
78 inline int set_compare_func(Item_bool_func2 *owner_arg)
79 {
80 return set_compare_func(owner_arg, item_cmp_type((*a)->result_type(),
81 (*b)->result_type()));
82 }
83 int set_cmp_func(Item_bool_func2 *owner_arg,
84 Item **a1, Item **a2,
85 Item_result type);
86
87 inline int set_cmp_func(Item_bool_func2 *owner_arg,
88 Item **a1, Item **a2)
89 {
90 return set_cmp_func(owner_arg, a1, a2,
91 item_cmp_type((*a1)->result_type(),
92 (*a2)->result_type()));
93 }
94 inline int compare() { return (this->*func)(); }
95
96 int compare_string(); // compare args[0] & args[1]
97 int compare_binary_string(); // compare args[0] & args[1]
98 int compare_real(); // compare args[0] & args[1]
99 int compare_decimal(); // compare args[0] & args[1]
100 int compare_int_signed(); // compare args[0] & args[1]
104 int compare_row(); // compare args[0] & args[1]
105 int compare_e_string(); // compare args[0] & args[1]
106 int compare_e_binary_string(); // compare args[0] & args[1]
107 int compare_e_real(); // compare args[0] & args[1]
108 int compare_e_decimal(); // compare args[0] & args[1]
109 int compare_e_int(); // compare args[0] & args[1]
111 int compare_e_row(); // compare args[0] & args[1]
112 int compare_real_fixed();
113 int compare_e_real_fixed();
114 int compare_datetime(); // compare args[0] & args[1] as DATETIMEs
115
116 static enum enum_date_cmp_type can_compare_as_dates(Item *a, Item *b,
117 int64_t *const_val_arg);
118
119 void set_datetime_cmp_func(Item **a1, Item **b1);
120 static arg_cmp_func comparator_matrix [5][2];
121
122 friend class Item_func;
123};
124
125
130
131class Item_func_truth : public item::function::Boolean
132{
133public:
134 virtual bool val_bool();
135 virtual int64_t val_int();
136 virtual void fix_length_and_dec();
137 virtual void print(String *str);
138
139protected:
140 Item_func_truth(Item *a, bool a_value, bool a_affirmative)
141 : item::function::Boolean(a), value(a_value), affirmative(a_affirmative)
142 {}
143
144private:
149 const bool value;
153 const bool affirmative;
154};
155
156
160
161class Item_func_istrue : public Item_func_truth
162{
163public:
164 Item_func_istrue(Item *a) : Item_func_truth(a, true, true) {}
165 virtual const char* func_name() const { return "istrue"; }
166};
167
168
172
173class Item_func_isnottrue : public Item_func_truth
174{
175public:
176 Item_func_isnottrue(Item *a) : Item_func_truth(a, true, false) {}
177 virtual const char* func_name() const { return "isnottrue"; }
178};
179
180
184
185class Item_func_isfalse : public Item_func_truth
186{
187public:
188 Item_func_isfalse(Item *a) : Item_func_truth(a, false, true) {}
189 virtual const char* func_name() const { return "isfalse"; }
190};
191
192
196
197class Item_func_isnotfalse : public Item_func_truth
198{
199public:
200 Item_func_isnotfalse(Item *a) : Item_func_truth(a, false, false) {}
201 virtual const char* func_name() const { return "isnotfalse"; }
202};
203
204
205#define UNKNOWN ((bool)-1)
206
207
208/*
209 Item_in_optimizer(left_expr, Item_in_subselect(...))
210
211 Item_in_optimizer is used to wrap an instance of Item_in_subselect. This
212 class does the following:
213 - Evaluate the left expression and store it in Item_cache_* object (to
214 avoid re-evaluating it many times during subquery execution)
215 - Shortcut the evaluation of "NULL IN (...)" to NULL in the cases where we
216 don't care if the result is NULL or FALSE.
217
218 NOTE
219 It is not quite clear why the above listed functionality should be
220 placed into a separate class called 'Item_in_optimizer'.
221*/
222
223class Item_in_optimizer: public item::function::Boolean
224{
225protected:
226 Item_cache *cache;
227 bool save_cache;
228 /*
229 Stores the value of "NULL IN (SELECT ...)" for uncorrelated subqueries:
230 UNKNOWN - "NULL in (SELECT ...)" has not yet been evaluated
231 FALSE - result is FALSE
232 TRUE - result is NULL
233 */
234 bool result_for_null_param;
235public:
236 Item_in_optimizer(Item *a, Item_in_subselect *b):
237 item::function::Boolean(a, reinterpret_cast<Item *>(b)), cache(0),
238 save_cache(0), result_for_null_param(UNKNOWN)
239 { with_subselect= true; }
240 bool fix_fields(Session *, Item **);
241 bool fix_left(Session *session, Item **ref);
242 bool is_null();
243 int64_t val_int();
244 void cleanup();
245 const char *func_name() const { return "<in_optimizer>"; }
246 Item_cache **get_cache() { return &cache; }
247 void keep_top_level_cache();
248 Item *transform(Item_transformer transformer, unsigned char *arg);
249};
250
252{
253public:
254 virtual Item_bool_func2* create(Item *a, Item *b) const;
255 virtual const char* symbol(bool invert) const { return invert? "<>" : "="; }
256 virtual bool eqne_op() const { return 1; }
257 virtual bool l_op() const { return 0; }
258 static const Eq_creator *instance();
259};
260
262{
263public:
264 virtual Item_bool_func2* create(Item *a, Item *b) const;
265 virtual const char* symbol(bool invert) const { return invert? "=" : "<>"; }
266 virtual bool eqne_op() const { return 1; }
267 virtual bool l_op() const { return 0; }
268 static const Ne_creator *instance();
269};
270
272{
273public:
274 virtual Item_bool_func2* create(Item *a, Item *b) const;
275 virtual const char* symbol(bool invert) const { return invert? "<=" : ">"; }
276 virtual bool eqne_op() const { return 0; }
277 virtual bool l_op() const { return 0; }
278 static const Gt_creator *instance();
279};
280
282{
283public:
284 virtual Item_bool_func2* create(Item *a, Item *b) const;
285 virtual const char* symbol(bool invert) const { return invert? ">=" : "<"; }
286 virtual bool eqne_op() const { return 0; }
287 virtual bool l_op() const { return 1; }
288 static const Lt_creator *instance();
289};
290
292{
293public:
294 virtual Item_bool_func2* create(Item *a, Item *b) const;
295 virtual const char* symbol(bool invert) const { return invert? "<" : ">="; }
296 virtual bool eqne_op() const { return 0; }
297 virtual bool l_op() const { return 0; }
298 static const Ge_creator *instance();
299};
300
302{
303public:
304 virtual Item_bool_func2* create(Item *a, Item *b) const;
305 virtual const char* symbol(bool invert) const { return invert? ">" : "<="; }
306 virtual bool eqne_op() const { return 0; }
307 virtual bool l_op() const { return 1; }
308 static const Le_creator *instance();
309};
310
311class Item_bool_func2 :public Item_int_func
312{ /* Bool with 2 string args */
313protected:
314 Arg_comparator cmp;
315 String tmp_value1,tmp_value2;
316 bool abort_on_null;
317
318public:
319 Item_bool_func2(Item *a,Item *b)
320 :Item_int_func(a,b), cmp(tmp_arg, tmp_arg+1), abort_on_null(false) {}
321 void fix_length_and_dec();
322 void set_cmp_func()
323 {
324 cmp.set_cmp_func(this, tmp_arg, tmp_arg+1);
325 }
326 optimize_type select_optimize() const { return OPTIMIZE_OP; }
327 virtual enum Functype rev_functype() const { return UNKNOWN_FUNC; }
328 bool have_rev_func() const { return rev_functype() != UNKNOWN_FUNC; }
329
330 virtual inline void print(String *str)
331 {
332 Item_func::print_op(str);
333 }
334
335 bool is_null() { return test(args[0]->is_null() || args[1]->is_null()); }
336 bool is_bool_func() { return 1; }
337 const charset_info_st *compare_collation() { return cmp.cmp_collation.collation; }
338 uint32_t decimal_precision() const { return 1; }
339 void top_level_item() { abort_on_null= true; }
340
341 friend class Arg_comparator;
342};
343
344class Item_bool_rowready_func2 :public Item_bool_func2
345{
346public:
347 Item_bool_rowready_func2(Item *a, Item *b) :Item_bool_func2(a, b)
348 {
349 allowed_arg_cols= 0; // Fetch this value from first argument
350 }
351 Item *neg_transformer(Session *session);
352 virtual Item *negated_item();
353 bool subst_argument_checker(unsigned char **)
354 { return true; }
355};
356
357class Item_func_not :public item::function::Boolean
358{
359public:
360 Item_func_not(Item *a) :item::function::Boolean(a) {}
361 int64_t val_int();
362 enum Functype functype() const { return NOT_FUNC; }
363 const char *func_name() const { return "not"; }
364 Item *neg_transformer(Session *session);
365 virtual void print(String *str);
366};
367
368/*
369 trigcond<param>(arg) ::= param? arg : TRUE
370
371 The class Item_func_trig_cond is used for guarded predicates
372 which are employed only for internal purposes.
373 A guarded predicate is an object consisting of an a regular or
374 a guarded predicate P and a pointer to a boolean guard variable g.
375 A guarded predicate P/g is evaluated to true if the value of the
376 guard g is false, otherwise it is evaluated to the same value that
377 the predicate P: val(P/g)= g ? val(P):true.
378 Guarded predicates allow us to include predicates into a conjunction
379 conditionally. Currently they are utilized for pushed down predicates
380 in queries with outer join operations.
381
382 In the future, probably, it makes sense to extend this class to
383 the objects consisting of three elements: a predicate P, a pointer
384 to a variable g and a firing value s with following evaluation
385 rule: val(P/g,s)= g==s? val(P) : true. It will allow us to build only
386 one item for the objects of the form P/g1/g2...
387
388 Objects of this class are built only for query execution after
389 the execution plan has been already selected. That's why this
390 class needs only val_int out of generic methods.
391
392 Current uses of Item_func_trig_cond objects:
393 - To wrap selection conditions when executing outer joins
394 - To wrap condition that is pushed down into subquery
395*/
396
397class Item_func_trig_cond: public item::function::Boolean
398{
399 bool *trig_var;
400public:
401 Item_func_trig_cond(Item *a, bool *f) : item::function::Boolean(a) { trig_var= f; }
402 int64_t val_int() { return *trig_var ? args[0]->val_int() : 1; }
403 enum Functype functype() const { return TRIG_COND_FUNC; };
404 const char *func_name() const { return "trigcond"; };
405 bool const_item() const { return false; }
406 bool *get_trig_var() { return trig_var; }
407 /* The following is needed for ICP: */
408 table_map used_tables() const { return args[0]->used_tables(); }
409};
410
411class Item_func_not_all :public Item_func_not
412{
413 /* allow to check presence of values in max/min optimization */
414 Item_sum_hybrid *test_sum_item;
415 Item_maxmin_subselect *test_sub_item;
416
417 bool abort_on_null;
418public:
419 bool show;
420
421 Item_func_not_all(Item *a)
422 :Item_func_not(a), test_sum_item(0), test_sub_item(0), abort_on_null(0),
423 show(0)
424 {}
425 virtual void top_level_item() { abort_on_null= 1; }
426 bool top_level() { return abort_on_null; }
427 int64_t val_int();
428 enum Functype functype() const { return NOT_ALL_FUNC; }
429 const char *func_name() const { return "<not>"; }
430 virtual void print(String *str);
431 void set_sum_test(Item_sum_hybrid *item) { test_sum_item= item; };
432 void set_sub_test(Item_maxmin_subselect *item) { test_sub_item= item; };
433 bool empty_underlying_subquery();
434 Item *neg_transformer(Session *session);
435};
436
437
438class Item_func_nop_all :public Item_func_not_all
439{
440public:
441
442 Item_func_nop_all(Item *a) :Item_func_not_all(a) {}
443 int64_t val_int();
444 const char *func_name() const { return "<nop>"; }
445 Item *neg_transformer(Session *session);
446};
447
448
449class Item_func_eq :public Item_bool_rowready_func2
450{
451public:
452 Item_func_eq(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {}
453 int64_t val_int();
454 enum Functype functype() const { return EQ_FUNC; }
455 enum Functype rev_functype() const { return EQ_FUNC; }
456 cond_result eq_cmp_result() const { return COND_TRUE; }
457 const char *func_name() const { return "="; }
459};
460
461class Item_func_equal :public Item_bool_rowready_func2
462{
463public:
464 Item_func_equal(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {};
465 int64_t val_int();
466 void fix_length_and_dec();
467 table_map not_null_tables() const { return 0; }
468 enum Functype functype() const { return EQUAL_FUNC; }
469 enum Functype rev_functype() const { return EQUAL_FUNC; }
470 cond_result eq_cmp_result() const { return COND_TRUE; }
471 const char *func_name() const { return "<=>"; }
472 Item *neg_transformer(Session *) { return 0; }
473};
474
475
476class Item_func_ge :public Item_bool_rowready_func2
477{
478public:
479 Item_func_ge(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {};
480 int64_t val_int();
481 enum Functype functype() const { return GE_FUNC; }
482 enum Functype rev_functype() const { return LE_FUNC; }
483 cond_result eq_cmp_result() const { return COND_TRUE; }
484 const char *func_name() const { return ">="; }
486};
487
488
489class Item_func_gt :public Item_bool_rowready_func2
490{
491public:
492 Item_func_gt(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {};
493 int64_t val_int();
494 enum Functype functype() const { return GT_FUNC; }
495 enum Functype rev_functype() const { return LT_FUNC; }
496 cond_result eq_cmp_result() const { return COND_FALSE; }
497 const char *func_name() const { return ">"; }
499};
500
501
502class Item_func_le :public Item_bool_rowready_func2
503{
504public:
505 Item_func_le(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {};
506 int64_t val_int();
507 enum Functype functype() const { return LE_FUNC; }
508 enum Functype rev_functype() const { return GE_FUNC; }
509 cond_result eq_cmp_result() const { return COND_TRUE; }
510 const char *func_name() const { return "<="; }
512};
513
514
515class Item_func_lt :public Item_bool_rowready_func2
516{
517public:
518 Item_func_lt(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {}
519 int64_t val_int();
520 enum Functype functype() const { return LT_FUNC; }
521 enum Functype rev_functype() const { return GT_FUNC; }
522 cond_result eq_cmp_result() const { return COND_FALSE; }
523 const char *func_name() const { return "<"; }
525};
526
527
528class Item_func_ne :public Item_bool_rowready_func2
529{
530public:
531 Item_func_ne(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {}
532 int64_t val_int();
533 enum Functype functype() const { return NE_FUNC; }
534 cond_result eq_cmp_result() const { return COND_FALSE; }
535 optimize_type select_optimize() const { return OPTIMIZE_KEY; }
536 const char *func_name() const { return "<>"; }
538};
539
540
541/*
542 The class Item_func_opt_neg is defined to factor out the functionality
543 common for the classes Item_func_between and Item_func_in. The objects
544 of these classes can express predicates or there negations.
545 The alternative approach would be to create pairs Item_func_between,
546 Item_func_notbetween and Item_func_in, Item_func_notin.
547
548*/
549
550class Item_func_opt_neg :public Item_int_func
551{
552public:
553 bool negated; /* <=> the item represents NOT <func> */
554 bool pred_level; /* <=> [NOT] <func> is used on a predicate level */
555public:
556 Item_func_opt_neg(Item *a, Item *b, Item *c)
557 :Item_int_func(a, b, c), negated(0), pred_level(0) {}
558 Item_func_opt_neg(List<Item> &list)
559 :Item_int_func(list), negated(0), pred_level(0) {}
560public:
561 inline void negate() { negated= !negated; }
562 inline void top_level_item() { pred_level= 1; }
563 Item *neg_transformer(Session *)
564 {
565 negated= !negated;
566 return this;
567 }
568 bool eq(const Item *item, bool binary_cmp) const;
569 bool subst_argument_checker(unsigned char **)
570 { return true; }
571};
572
573
574class Item_func_between :public Item_func_opt_neg
575{
576 DTCollation cmp_collation;
577public:
578 Item_result cmp_type;
579 String value0,value1,value2;
580 /* TRUE <=> arguments will be compared as dates. */
581 bool compare_as_dates;
582 /* Comparators used for DATE/DATETIME comparison. */
583 Arg_comparator ge_cmp, le_cmp;
584 Item_func_between(Item *a, Item *b, Item *c)
585 :Item_func_opt_neg(a, b, c), compare_as_dates(false) {}
586 int64_t val_int();
587 optimize_type select_optimize() const { return OPTIMIZE_KEY; }
588 enum Functype functype() const { return BETWEEN; }
589 const char *func_name() const { return "between"; }
590 bool fix_fields(Session *, Item **);
591 void fix_length_and_dec();
592 virtual void print(String *str);
593 bool is_bool_func() { return 1; }
594 const charset_info_st *compare_collation() { return cmp_collation.collation; }
595 uint32_t decimal_precision() const { return 1; }
596};
597
598
599class Item_func_strcmp :public Item_bool_func2
600{
601public:
602 Item_func_strcmp(Item *a,Item *b) :Item_bool_func2(a,b) {}
603 int64_t val_int();
604 optimize_type select_optimize() const { return OPTIMIZE_NONE; }
605 const char *func_name() const { return "strcmp"; }
606
607 virtual inline void print(String *str)
608 {
609 Item_func::print(str);
610 }
611};
612
613
615{
616 Item_result type;
617 double dbl;
618 type::Decimal dec;
619};
620
621class Item_func_interval :public Item_int_func
622{
623 Item_row *row;
624 bool use_decimal_comparison;
625 interval_range *intervals;
626public:
627 Item_func_interval(Item_row *a)
628 :Item_int_func(a),row(a),intervals(0)
629 {
630 allowed_arg_cols= 0; // Fetch this value from first argument
631 }
632 int64_t val_int();
633 void fix_length_and_dec();
634 const char *func_name() const { return "interval"; }
635 uint32_t decimal_precision() const { return 2; }
636};
637
638
639class Item_func_coalesce :public Item_func_numhybrid
640{
641protected:
642 enum_field_types cached_field_type;
643 Item_func_coalesce(Item *a, Item *b) :Item_func_numhybrid(a, b) {}
644public:
645 Item_func_coalesce(List<Item> &list) :Item_func_numhybrid(list) {}
646 double real_op();
647 int64_t int_op();
648 String *str_op(String *);
650 void fix_length_and_dec();
651 void find_num_type() {}
652 enum Item_result result_type () const { return hybrid_type; }
653 const char *func_name() const { return "coalesce"; }
654 table_map not_null_tables() const { return 0; }
655 enum_field_types field_type() const { return cached_field_type; }
656};
657
658
659class Item_func_ifnull :public Item_func_coalesce
660{
661protected:
662 bool field_type_defined;
663public:
664 Item_func_ifnull(Item *a, Item *b) :Item_func_coalesce(a,b) {}
665 double real_op();
666 int64_t int_op();
667 String *str_op(String *str);
669 enum_field_types field_type() const;
670 void fix_length_and_dec();
671 const char *func_name() const { return "ifnull"; }
672 Field *tmp_table_field()
673 {
674 return Item_func::tmp_table_field();
675 }
676 Field *tmp_table_field(Table *table);
677 uint32_t decimal_precision() const;
678};
679
680
681class Item_func_if :public Item_func
682{
683 Item_result cached_result_type;
684 enum_field_types cached_field_type;
685
686public:
687 Item_func_if(Item *a, Item *b, Item *c) :
688 Item_func(a,b,c),
689 cached_result_type(INT_RESULT)
690 {}
691
692 double val_real();
693 int64_t val_int();
694 String *val_str(String *str);
696 Item_result result_type () const { return cached_result_type; }
697 enum_field_types field_type() const { return cached_field_type; }
698 bool fix_fields(Session *, Item **);
699 void fix_length_and_dec();
700 uint32_t decimal_precision() const;
701 const char *func_name() const { return "if"; }
702};
703
704
705class Item_func_nullif :public Item_bool_func2
706{
707 enum Item_result cached_result_type;
708public:
709 Item_func_nullif(Item *a,Item *b)
710 :Item_bool_func2(a,b), cached_result_type(INT_RESULT)
711 {}
712 double val_real();
713 int64_t val_int();
714 String *val_str(String *str);
716 enum Item_result result_type () const { return cached_result_type; }
717 void fix_length_and_dec();
718 uint32_t decimal_precision() const { return args[0]->decimal_precision(); }
719 const char *func_name() const { return "nullif"; }
720
721 virtual inline void print(String *str)
722 {
723 Item_func::print(str);
724 }
725
726 table_map not_null_tables() const { return 0; }
727 bool is_null();
728};
729
730
731/* Functions to handle the optimized IN */
732
733
734/* A vector of values of some type */
735
736class in_vector :public memory::SqlAlloc
737{
738public:
739 char *base;
740 uint32_t size;
741 qsort2_cmp compare;
742 const charset_info_st *collation;
743 uint32_t count;
744 uint32_t used_count;
745 in_vector() {}
746 in_vector(uint32_t elements,uint32_t element_length,qsort2_cmp cmp_func,
747 const charset_info_st * const cmp_coll)
748 :base((char*) memory::sql_calloc(elements*element_length)),
749 size(element_length), compare(cmp_func), collation(cmp_coll),
750 count(elements), used_count(elements) {}
751 virtual ~in_vector() {}
752 virtual void set(uint32_t pos,Item *item)=0;
753 virtual unsigned char *get_value(Item *item)=0;
754 void sort();
755 int find(Item *item);
756
757 /*
758 Create an instance of Item_{type} (e.g. Item_decimal) constant object
759 which type allows it to hold an element of this vector without any
760 conversions.
761 The purpose of this function is to be able to get elements of this
762 vector in form of Item_xxx constants without creating Item_xxx object
763 for every array element you get (i.e. this implements "FlyWeight" pattern)
764 */
765 virtual Item* create_item() { return NULL; }
766
767 /*
768 Store the value at position #pos into provided item object
769 SYNOPSIS
770 value_to_item()
771 pos Index of value to store
772 item Constant item to store value into. The item must be of the same
773 type that create_item() returns.
774 */
775 virtual void value_to_item(uint32_t, Item *) { }
776
777 /* Compare values number pos1 and pos2 for equality */
778 bool compare_elems(uint32_t pos1, uint32_t pos2)
779 {
780 return test(compare(collation, base + pos1*size, base + pos2*size));
781 }
782 virtual Item_result result_type()= 0;
783};
784
785class in_string :public in_vector
786{
787 char buff[STRING_BUFFER_USUAL_SIZE];
788 String tmp;
789public:
790 in_string(uint32_t elements,qsort2_cmp cmp_func, const charset_info_st * const cs);
791 ~in_string();
792 void set(uint32_t pos,Item *item);
793 unsigned char *get_value(Item *item);
794 Item* create_item()
795 {
796 return new Item_string(collation);
797 }
798 void value_to_item(uint32_t pos, Item *item)
799 {
800 String *str=((String*) base)+pos;
801 Item_string *to= (Item_string*)item;
802 to->str_value= *str;
803 }
804 Item_result result_type() { return STRING_RESULT; }
805};
806
807class in_int64_t :public in_vector
808{
809protected:
810 /*
811 Here we declare a temporary variable (tmp) of the same type as the
812 elements of this vector. tmp is used in finding if a given value is in
813 the list.
814 */
816 {
817 int64_t val;
818 int64_t unsigned_flag; // Use int64_t, not bool, to preserve alignment
819 } tmp;
820public:
821 in_int64_t(uint32_t elements);
822 void set(uint32_t pos,Item *item);
823 unsigned char *get_value(Item *item);
824
825 Item* create_item()
826 {
827 /*
828 We're created a signed INT, this may not be correct in
829 general case (see BUG#19342).
830 */
831 return new Item_int((int64_t)0);
832 }
833 void value_to_item(uint32_t pos, Item *item)
834 {
835 ((Item_int*) item)->value= ((packed_int64_t*) base)[pos].val;
836 ((Item_int*) item)->unsigned_flag= (bool)
837 ((packed_int64_t*) base)[pos].unsigned_flag;
838 }
839 Item_result result_type() { return INT_RESULT; }
840
841 friend int cmp_int64_t(void *cmp_arg, packed_int64_t *a,packed_int64_t *b);
842};
843
844
845/*
846 Class to represent a vector of constant DATE/DATETIME values.
847 Values are obtained with help of the get_datetime_value() function.
848 If the left item is a constant one then its value is cached in the
849 lval_cache variable.
850*/
851class in_datetime :public in_int64_t
852{
853public:
854 Session *session;
855 /* An item used to issue warnings. */
856 Item *warn_item;
857 /* Cache for the left item. */
858 Item *lval_cache;
859
860 in_datetime(Item *warn_item_arg, uint32_t elements);
861
862 void set(uint32_t pos,Item *item);
863 unsigned char *get_value(Item *item);
864 friend int cmp_int64_t(void *cmp_arg, packed_int64_t *a,packed_int64_t *b);
865};
866
867
868class in_double :public in_vector
869{
870 double tmp;
871public:
872 in_double(uint32_t elements);
873 void set(uint32_t pos,Item *item);
874 unsigned char *get_value(Item *item);
875 Item *create_item()
876 {
877 return new Item_float(0.0, 0);
878 }
879 void value_to_item(uint32_t pos, Item *item)
880 {
881 ((Item_float*)item)->value= ((double*) base)[pos];
882 }
883 Item_result result_type() { return REAL_RESULT; }
884};
885
886
887class in_decimal :public in_vector
888{
889 type::Decimal val;
890public:
891 in_decimal(uint32_t elements);
892 void set(uint32_t pos, Item *item);
893 unsigned char *get_value(Item *item);
894 Item *create_item()
895 {
896 return new Item_decimal(0, false);
897 }
898 void value_to_item(uint32_t pos, Item *item)
899 {
900 type::Decimal *dec= ((type::Decimal *)base) + pos;
901 Item_decimal *item_dec= (Item_decimal*)item;
902 item_dec->set_decimal_value(dec);
903 }
904 Item_result result_type() { return DECIMAL_RESULT; }
905
906};
907
908
909/*
910** Classes for easy comparing of non const items
911*/
912
913class cmp_item :public memory::SqlAlloc
914{
915public:
916 const charset_info_st *cmp_charset;
917
918 cmp_item()
919 {
920 cmp_charset= &my_charset_bin;
921 }
922
923 virtual ~cmp_item() {}
924 virtual void store_value(Item *item)= 0;
925 virtual int cmp(Item *item)= 0;
926 // for optimized IN with row
927 virtual int compare(cmp_item *item)= 0;
928 static cmp_item* get_comparator(Item_result type, const charset_info_st * const cs);
929 virtual cmp_item *make_same()= 0;
930 virtual void store_value_by_template(cmp_item *, Item *item)
931 {
932 store_value(item);
933 }
934};
935
936class cmp_item_string :public cmp_item
937{
938protected:
939 String *value_res;
940public:
941 cmp_item_string () {}
942 cmp_item_string (const charset_info_st * const cs) { cmp_charset= cs; }
943 void set_charset(const charset_info_st * const cs) { cmp_charset= cs; }
944 friend class cmp_item_sort_string;
945 friend class cmp_item_sort_string_in_static;
946};
947
948class cmp_item_sort_string :public cmp_item_string
949{
950protected:
951 char value_buff[STRING_BUFFER_USUAL_SIZE];
952 String value;
953public:
954 cmp_item_sort_string():
955 cmp_item_string() {}
956 cmp_item_sort_string(const charset_info_st * const cs):
957 cmp_item_string(cs),
958 value(value_buff, sizeof(value_buff), cs) {}
959 void store_value(Item *item)
960 {
961 value_res= item->val_str(&value);
962 }
963 int cmp(Item *arg)
964 {
965 char buff[STRING_BUFFER_USUAL_SIZE];
966 String tmp(buff, sizeof(buff), cmp_charset), *res;
967 res= arg->val_str(&tmp);
968 return (value_res ? (res ? sortcmp(value_res, res, cmp_charset) : 1) :
969 (res ? -1 : 0));
970 }
971 int compare(cmp_item *ci)
972 {
973 cmp_item_string *l_cmp= (cmp_item_string *) ci;
974 return sortcmp(value_res, l_cmp->value_res, cmp_charset);
975 }
976 cmp_item *make_same();
977 void set_charset(const charset_info_st * const cs)
978 {
979 cmp_charset= cs;
980 value.set_quick(value_buff, sizeof(value_buff), cs);
981 }
982};
983
984class cmp_item_int :public cmp_item
985{
986 int64_t value;
987public:
988 void store_value(Item *item)
989 {
990 value= item->val_int();
991 }
992 int cmp(Item *arg)
993 {
994 return value != arg->val_int();
995 }
996 int compare(cmp_item *ci)
997 {
998 cmp_item_int *l_cmp= (cmp_item_int *)ci;
999 return (value < l_cmp->value) ? -1 : ((value == l_cmp->value) ? 0 : 1);
1000 }
1001 cmp_item *make_same();
1002};
1003
1004/*
1005 Compare items in the DATETIME context.
1006 Values are obtained with help of the get_datetime_value() function.
1007 If the left item is a constant one then its value is cached in the
1008 lval_cache variable.
1009*/
1010class cmp_item_datetime :public cmp_item
1011{
1012 int64_t value;
1013
1014public:
1015 Session *session;
1016 /* Item used for issuing warnings. */
1017 Item *warn_item;
1018 /* Cache for the left item. */
1019 Item *lval_cache;
1020
1021 cmp_item_datetime(Item *warn_item_arg);
1022
1023 void store_value(Item *item);
1024 int cmp(Item *arg);
1025 int compare(cmp_item *ci);
1026 cmp_item *make_same();
1027};
1028
1029class cmp_item_real :public cmp_item
1030{
1031 double value;
1032public:
1033 void store_value(Item *item)
1034 {
1035 value= item->val_real();
1036 }
1037 int cmp(Item *arg)
1038 {
1039 return compare_ne_double(value, arg->val_real());
1040 }
1041 int compare(cmp_item *ci)
1042 {
1043 cmp_item_real *l_cmp= (cmp_item_real *) ci;
1044 return (value < l_cmp->value)? -1 : (compare_double(value, l_cmp->value) ? 0 : 1);
1045 }
1046 cmp_item *make_same();
1047};
1048
1049
1050class cmp_item_decimal :public cmp_item
1051{
1052 type::Decimal value;
1053public:
1054 void store_value(Item *item);
1055 int cmp(Item *arg);
1056 int compare(cmp_item *c);
1057 cmp_item *make_same();
1058};
1059
1060
1061/*
1062 cmp_item for optimized IN with row (right part string, which never
1063 be changed)
1064*/
1065
1066class cmp_item_sort_string_in_static :public cmp_item_string
1067{
1068 protected:
1069 String value;
1070public:
1071 cmp_item_sort_string_in_static(const charset_info_st * const cs):
1072 cmp_item_string(cs) {}
1073 void store_value(Item *item)
1074 {
1075 value_res= item->val_str(&value);
1076 }
1077 int cmp(Item *)
1078 {
1079 // Should never be called
1080 assert(0);
1081 return 1;
1082 }
1083 int compare(cmp_item *ci)
1084 {
1085 cmp_item_string *l_cmp= (cmp_item_string *) ci;
1086 return sortcmp(value_res, l_cmp->value_res, cmp_charset);
1087 }
1088 cmp_item *make_same()
1089 {
1090 return new cmp_item_sort_string_in_static(cmp_charset);
1091 }
1092};
1093
1094
1095/*
1096 The class Item_func_case is the CASE ... WHEN ... THEN ... END function
1097 implementation.
1098
1099 When there is no expression between CASE and the first WHEN
1100 (the CASE expression) then this function simple checks all WHEN expressions
1101 one after another. When some WHEN expression evaluated to TRUE then the
1102 value of the corresponding THEN expression is returned.
1103
1104 When the CASE expression is specified then it is compared to each WHEN
1105 expression individually. When an equal WHEN expression is found
1106 corresponding THEN expression is returned.
1107 In order to do correct comparisons several comparators are used. One for
1108 each result type. Different result types that are used in particular
1109 CASE ... END expression are collected in the fix_length_and_dec() member
1110 function and only comparators for there result types are used.
1111*/
1112
1113class Item_func_case :public Item_func
1114{
1115 int first_expr_num, else_expr_num;
1116 enum Item_result cached_result_type, left_result_type;
1117 String tmp_value;
1118 uint32_t ncases;
1119 Item_result cmp_type;
1120 DTCollation cmp_collation;
1121 enum_field_types cached_field_type;
1122 cmp_item *cmp_items[DECIMAL_RESULT+1]; /* For all result types */
1123 cmp_item *case_item;
1124public:
1125 Item_func_case(List<Item> &list, Item *first_expr_arg, Item *else_expr_arg)
1126 :Item_func(), first_expr_num(-1), else_expr_num(-1),
1127 cached_result_type(INT_RESULT), left_result_type(INT_RESULT), case_item(0)
1128 {
1129 ncases= list.size();
1130 if (first_expr_arg)
1131 {
1132 first_expr_num= list.size();
1133 list.push_back(first_expr_arg);
1134 }
1135 if (else_expr_arg)
1136 {
1137 else_expr_num= list.size();
1138 list.push_back(else_expr_arg);
1139 }
1140 set_arguments(list);
1141 memset(&cmp_items, 0, sizeof(cmp_items));
1142 }
1143 double val_real();
1144 int64_t val_int();
1145 String *val_str(String *);
1147 bool fix_fields(Session *session, Item **ref);
1148 void fix_length_and_dec();
1149 uint32_t decimal_precision() const;
1150 table_map not_null_tables() const { return 0; }
1151 enum Item_result result_type () const { return cached_result_type; }
1152 enum_field_types field_type() const { return cached_field_type; }
1153 const char *func_name() const { return "case"; }
1154 virtual void print(String *str);
1155 Item *find_item(String *str);
1156 const charset_info_st *compare_collation() { return cmp_collation.collation; }
1157 void cleanup();
1158 void agg_str_lengths(Item *arg);
1159 void agg_num_lengths(Item *arg);
1160};
1161
1162/*
1163 The Item_func_in class implements the in_expr IN(values_list) function.
1164
1165 The current implementation distinguishes 2 cases:
1166 1) all items in the value_list are constants and have the same
1167 result type. This case is handled by in_vector class.
1168 2) items in the value_list have different result types or there is some
1169 non-constant items.
1170 In this case Item_func_in employs several cmp_item objects to performs
1171 comparisons of in_expr and an item from the values_list. One cmp_item
1172 object for each result type. Different result types are collected in the
1173 fix_length_and_dec() member function by means of collect_cmp_types()
1174 function.
1175*/
1176class Item_func_in :public Item_func_opt_neg
1177{
1178public:
1179 /*
1180 an array of values when the right hand arguments of IN
1181 are all SQL constant and there are no nulls
1182 */
1183 in_vector *array;
1184 bool have_null;
1185 /*
1186 true when all arguments of the IN clause are of compatible types
1187 and can be used safely as comparisons for key conditions
1188 */
1189 bool arg_types_compatible;
1190 Item_result left_result_type;
1191 cmp_item *cmp_items[6]; /* One cmp_item for each result type */
1192 DTCollation cmp_collation;
1193
1194 Item_func_in(List<Item> &list)
1195 :Item_func_opt_neg(list), array(0), have_null(0),
1196 arg_types_compatible(false)
1197 {
1198 memset(&cmp_items, 0, sizeof(cmp_items));
1199 allowed_arg_cols= 0; // Fetch this value from first argument
1200 }
1201 int64_t val_int();
1202 bool fix_fields(Session *, Item **);
1203 void fix_length_and_dec();
1204 uint32_t decimal_precision() const { return 1; }
1205 void cleanup()
1206 {
1207 Item_int_func::cleanup();
1208 delete array;
1209 array= 0;
1210 for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
1211 {
1212 delete cmp_items[i];
1213 cmp_items[i]= 0;
1214 }
1215 return;
1216 }
1217 optimize_type select_optimize() const
1218 { return OPTIMIZE_KEY; }
1219 virtual void print(String *str);
1220 enum Functype functype() const { return IN_FUNC; }
1221 const char *func_name() const { return " IN "; }
1222 bool nulls_in_row();
1223 bool is_bool_func() { return 1; }
1224 const charset_info_st *compare_collation() { return cmp_collation.collation; }
1225};
1226
1227class cmp_item_row :public cmp_item
1228{
1229 cmp_item **comparators;
1230 uint32_t n;
1231public:
1232 cmp_item_row(): comparators(0), n(0) {}
1233 ~cmp_item_row();
1234 void store_value(Item *item);
1235 inline void alloc_comparators();
1236 int cmp(Item *arg);
1237 int compare(cmp_item *arg);
1238 cmp_item *make_same();
1239 void store_value_by_template(cmp_item *tmpl, Item *);
1240 friend void Item_func_in::fix_length_and_dec();
1241};
1242
1243
1244class in_row :public in_vector
1245{
1246 cmp_item_row tmp;
1247public:
1248 in_row(uint32_t elements, Item *);
1249 ~in_row();
1250 void set(uint32_t pos,Item *item);
1251 unsigned char *get_value(Item *item);
1252 friend void Item_func_in::fix_length_and_dec();
1253 Item_result result_type() { return ROW_RESULT; }
1254};
1255
1256/* Functions used by where clause */
1257
1258class Item_func_isnull :public item::function::Boolean
1259{
1260protected:
1261 int64_t cached_value;
1262public:
1263 Item_func_isnull(Item *a) :item::function::Boolean(a) {}
1264 int64_t val_int();
1265 enum Functype functype() const { return ISNULL_FUNC; }
1266 void fix_length_and_dec()
1267 {
1268 decimals=0; max_length=1; maybe_null=0;
1269 update_used_tables();
1270 }
1271 const char *func_name() const { return "isnull"; }
1272 /* Optimize case of not_null_column IS NULL */
1273 virtual void update_used_tables()
1274 {
1275 if (!args[0]->maybe_null)
1276 {
1277 used_tables_cache= 0; /* is always false */
1278 const_item_cache= 1;
1279 cached_value= (int64_t) 0;
1280 }
1281 else
1282 {
1283 args[0]->update_used_tables();
1284 if ((const_item_cache= !(used_tables_cache= args[0]->used_tables())) &&
1286 {
1287 /* Remember if the value is always NULL or never NULL */
1288 cached_value= (int64_t) args[0]->is_null();
1289 }
1290 }
1291 }
1292 table_map not_null_tables() const { return 0; }
1293 optimize_type select_optimize() const { return OPTIMIZE_NULL; }
1294 Item *neg_transformer(Session *session);
1295 const charset_info_st *compare_collation() { return args[0]->collation.collation; }
1296};
1297
1298/* Functions used by HAVING for rewriting IN subquery */
1299
1300/*
1301 This is like IS NOT NULL but it also remembers if it ever has
1302 encountered a NULL.
1303*/
1304class Item_is_not_null_test :public Item_func_isnull
1305{
1306 Item_in_subselect* owner;
1307public:
1308 Item_is_not_null_test(Item_in_subselect* ow, Item *a)
1309 :Item_func_isnull(a), owner(ow)
1310 {}
1311 enum Functype functype() const { return ISNOTNULLTEST_FUNC; }
1312 int64_t val_int();
1313 const char *func_name() const { return "<is_not_null_test>"; }
1314 void update_used_tables();
1315 /*
1316 we add RAND_TABLE_BIT to prevent moving this item from HAVING to WHERE
1317 */
1318 table_map used_tables() const
1319 { return used_tables_cache | RAND_TABLE_BIT; }
1320};
1321
1322
1323class Item_func_isnotnull :public item::function::Boolean
1324{
1325 bool abort_on_null;
1326public:
1327 Item_func_isnotnull(Item *a) :item::function::Boolean(a), abort_on_null(0) {}
1328 int64_t val_int();
1329 enum Functype functype() const { return ISNOTNULL_FUNC; }
1330 void fix_length_and_dec()
1331 {
1332 decimals=0; max_length=1; maybe_null=0;
1333 }
1334 const char *func_name() const { return "isnotnull"; }
1335 optimize_type select_optimize() const { return OPTIMIZE_NULL; }
1336 table_map not_null_tables() const
1337 { return abort_on_null ? not_null_tables_cache : 0; }
1338 Item *neg_transformer(Session *session);
1339 virtual void print(String *str);
1340 const charset_info_st *compare_collation() { return args[0]->collation.collation; }
1341 void top_level_item() { abort_on_null=1; }
1342};
1343
1344
1345class Item_func_like :public Item_bool_func2
1346{
1347 // Turbo Boyer-Moore data
1348 bool canDoTurboBM; // pattern is '%abcd%' case
1349 const char* pattern;
1350 int pattern_len;
1351
1352 // TurboBM buffers, *this is owner
1353 int* bmGs; // good suffix shift table, size is pattern_len + 1
1354 int* bmBc; // bad character shift table, size is alphabet_size
1355
1356 void turboBM_compute_suffixes(int* suff);
1357 void turboBM_compute_good_suffix_shifts(int* suff);
1359 bool turboBM_matches(const char* text, int text_len) const;
1360 enum { alphabet_size = 256 };
1361
1362 Item *escape_item;
1363
1364 bool escape_used_in_parsing;
1365
1366
1367public:
1368
1369 char *escape;
1370
1371 Item_func_like(Item *a,Item *b, Item *escape_arg, bool escape_used)
1372 :Item_bool_func2(a,b), canDoTurboBM(false), pattern(0), pattern_len(0),
1373 bmGs(0), bmBc(0), escape_item(escape_arg),
1374 escape_used_in_parsing(escape_used), escape(NULL) {}
1375 int64_t val_int();
1376 enum Functype functype() const { return LIKE_FUNC; }
1377 optimize_type select_optimize() const;
1378 cond_result eq_cmp_result() const { return COND_TRUE; }
1379 const char *func_name() const { return "like"; }
1380 bool fix_fields(Session *session, Item **ref);
1381 void cleanup();
1382};
1383
1384
1385class Item_cond :public item::function::Boolean
1386{
1387protected:
1388 List<Item> list;
1389 bool abort_on_null;
1390 table_map and_tables_cache;
1391
1392public:
1393
1394 using Item::split_sum_func;
1395
1396 /* Item_cond() is only used to create top level items */
1397 Item_cond(): item::function::Boolean(), abort_on_null(1)
1398 { const_item_cache=0; }
1399 Item_cond(Item *i1,Item *i2)
1400 :item::function::Boolean(), abort_on_null(0)
1401 {
1402 list.push_back(i1);
1403 list.push_back(i2);
1404 }
1405 Item_cond(Session *session, Item_cond *item);
1406 Item_cond(List<Item> &nlist)
1407 :item::function::Boolean(), list(nlist), abort_on_null(0) {}
1408 void add(Item *item) { list.push_back(item); }
1409 void add_at_head(Item *item) { list.push_front(item); }
1410 void add_at_head(List<Item> *nlist) { list.prepand(nlist); }
1411 bool fix_fields(Session *, Item **ref);
1412 void fix_after_pullout(Select_Lex *new_parent, Item **ref);
1413
1414 enum Type type() const { return COND_ITEM; }
1415 List<Item>* argument_list() { return &list; }
1416 table_map used_tables() const;
1417 void update_used_tables();
1418 virtual void print(String *str);
1419 void split_sum_func(Session *session, Item **ref_pointer_array, List<Item> &fields);
1420 friend int setup_conds(Session *session, TableList *tables, TableList *leaves,
1421 COND **conds);
1422 void top_level_item() { abort_on_null=1; }
1423 void copy_andor_arguments(Session *session, Item_cond *item);
1424 bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg);
1425 Item *transform(Item_transformer transformer, unsigned char *arg);
1426 void traverse_cond(Cond_traverser, void *arg, traverse_order order);
1427 void neg_arguments(Session *session);
1428 enum_field_types field_type() const { return DRIZZLE_TYPE_LONGLONG; }
1429 bool subst_argument_checker(unsigned char **)
1430 { return true; }
1431 Item *compile(Item_analyzer analyzer, unsigned char **arg_p,
1432 Item_transformer transformer, unsigned char *arg_t);
1433};
1434
1435
1436/*
1437 The class Item_equal is used to represent conjunctions of equality
1438 predicates of the form field1 = field2, and field=const in where
1439 conditions and on expressions.
1440
1441 All equality predicates of the form field1=field2 contained in a
1442 conjunction are substituted for a sequence of items of this class.
1443 An item of this class Item_equal(f1,f2,...fk) represents a
1444 multiple equality f1=f2=...=fk.
1445
1446 If a conjunction contains predicates f1=f2 and f2=f3, a new item of
1447 this class is created Item_equal(f1,f2,f3) representing the multiple
1448 equality f1=f2=f3 that substitutes the above equality predicates in
1449 the conjunction.
1450 A conjunction of the predicates f2=f1 and f3=f1 and f3=f2 will be
1451 substituted for the item representing the same multiple equality
1452 f1=f2=f3.
1453 An item Item_equal(f1,f2) can appear instead of a conjunction of
1454 f2=f1 and f1=f2, or instead of just the predicate f1=f2.
1455
1456 An item of the class Item_equal inherits equalities from outer
1457 conjunctive levels.
1458
1459 Suppose we have a where condition of the following form:
1460 WHERE f1=f2 AND f3=f4 AND f3=f5 AND ... AND (...OR (f1=f3 AND ...)).
1461 In this case:
1462 f1=f2 will be substituted for Item_equal(f1,f2);
1463 f3=f4 and f3=f5 will be substituted for Item_equal(f3,f4,f5);
1464 f1=f3 will be substituted for Item_equal(f1,f2,f3,f4,f5);
1465
1466 An object of the class Item_equal can contain an optional constant
1467 item c. Then it represents a multiple equality of the form
1468 c=f1=...=fk.
1469
1470 Objects of the class Item_equal are used for the following:
1471
1472 1. An object Item_equal(t1.f1,...,tk.fk) allows us to consider any
1473 pair of tables ti and tj as joined by an equi-condition.
1474 Thus it provide us with additional access paths from table to table.
1475
1476 2. An object Item_equal(t1.f1,...,tk.fk) is applied to deduce new
1477 SARGable predicates:
1478 f1=...=fk AND P(fi) => f1=...=fk AND P(fi) AND P(fj).
1479 It also can give us additional index scans and can allow us to
1480 improve selectivity estimates.
1481
1482 3. An object Item_equal(t1.f1,...,tk.fk) is used to optimize the
1483 selected execution plan for the query: if table ti is accessed
1484 before the table tj then in any predicate P in the where condition
1485 the occurrence of tj.fj is substituted for ti.fi. This can allow
1486 an evaluation of the predicate at an earlier step.
1487
1488 When feature 1 is supported they say that join transitive closure
1489 is employed.
1490 When feature 2 is supported they say that search argument transitive
1491 closure is employed.
1492 Both features are usually supported by preprocessing original query and
1493 adding additional predicates.
1494 We do not just add predicates, we rather dynamically replace some
1495 predicates that can not be used to access tables in the investigated
1496 plan for those, obtained by substitution of some fields for equal fields,
1497 that can be used.
1498
1499 Prepared Statements/Stored Procedures note: instances of class
1500 Item_equal are created only at the time a PS/SP is executed and
1501 are deleted in the end of execution. All changes made to these
1502 objects need not be registered in the list of changes of the parse
1503 tree and do not harm PS/SP re-execution.
1504
1505 Item equal objects are employed only at the optimize phase. Usually they are
1506 not supposed to be evaluated. Yet in some cases we call the method val_int()
1507 for them. We have to take care of restricting the predicate such an
1508 object represents f1=f2= ...=fn to the projection of known fields fi1=...=fik.
1509*/
1510
1511class Item_equal: public item::function::Boolean
1512{
1513public:
1514 typedef List<Item_field> fields_t;
1515
1516 Item_equal() :
1517 const_item(0),
1518 eval_item(0),
1519 cond_false(0)
1520 {
1521 const_item_cache=0;
1522 }
1523
1524 fields_t::iterator begin()
1525 {
1526 return fields.begin();
1527 }
1528
1529 Item_equal(Item_field *f1, Item_field *f2);
1530 Item_equal(Item *c, Item_field *f);
1531 Item_equal(Item_equal *item_equal);
1532 inline Item* get_const() { return const_item; }
1533 void add(Item *c);
1534 void add(Item_field *f);
1535 uint32_t members();
1536 bool contains(Field *field);
1537 Item_field* get_first() { return &fields.front(); }
1538 void merge(Item_equal *item);
1539 void update_const();
1540 enum Functype functype() const { return MULT_EQUAL_FUNC; }
1541 int64_t val_int();
1542 const char *func_name() const { return "multiple equal"; }
1543 optimize_type select_optimize() const { return OPTIMIZE_EQUAL; }
1544 void sort(Item_field_cmpfunc cmp, void *arg);
1545 void fix_length_and_dec();
1546 bool fix_fields(Session *session, Item **ref);
1547 void update_used_tables();
1548 bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg);
1549 Item *transform(Item_transformer transformer, unsigned char *arg);
1550 virtual void print(String *str);
1551 const charset_info_st *compare_collation()
1552 { return fields.front().collation.collation; }
1553private:
1554 fields_t fields; /* list of equal field items */
1555 Item *const_item; /* optional constant item equal to fields items */
1556 cmp_item *eval_item;
1557 bool cond_false;
1558
1559};
1560
1561class COND_EQUAL: public memory::SqlAlloc
1562{
1563public:
1564 uint32_t max_members; /* max number of members the current level
1565 list and all lower level lists */
1566 COND_EQUAL *upper_levels; /* multiple equalities of upper and levels */
1567 List<Item_equal> current_level; /* list of multiple equalities of
1568 the current and level */
1569 COND_EQUAL() : max_members(0), upper_levels(NULL) {}
1570};
1571
1572typedef List<Item_field>::iterator Item_equal_iterator;
1573
1574class Item_cond_and :public Item_cond
1575{
1576public:
1577 COND_EQUAL cond_equal; /* contains list of Item_equal objects for
1578 the current and level and reference
1579 to multiple equalities of upper and levels */
1580 Item_cond_and() :Item_cond() {}
1581 Item_cond_and(Item *i1,Item *i2) :Item_cond(i1,i2) {}
1582 Item_cond_and(Session *session, Item_cond_and *item) :Item_cond(session, item) {}
1583 Item_cond_and(List<Item> &list_arg): Item_cond(list_arg) {}
1584 enum Functype functype() const { return COND_AND_FUNC; }
1585 int64_t val_int();
1586 const char *func_name() const { return "and"; }
1587 table_map not_null_tables() const
1588 { return abort_on_null ? not_null_tables_cache: and_tables_cache; }
1589 Item* copy_andor_structure(Session *session)
1590 {
1591 Item_cond_and *item;
1592 item= new Item_cond_and(session, this);
1593 item->copy_andor_arguments(session, this);
1594 return item;
1595 }
1596 Item *neg_transformer(Session *session);
1597};
1598
1599inline bool is_cond_and(Item *item)
1600{
1601 if (item->type() != Item::COND_ITEM)
1602 return false;
1603
1604 Item_cond *cond_item= (Item_cond*) item;
1605 return (cond_item->functype() == Item_func::COND_AND_FUNC);
1606}
1607
1608class Item_cond_or :public Item_cond
1609{
1610public:
1611 Item_cond_or() :Item_cond() {}
1612 Item_cond_or(Item *i1,Item *i2) :Item_cond(i1,i2) {}
1613 Item_cond_or(Session *session, Item_cond_or *item) :Item_cond(session, item) {}
1614 Item_cond_or(List<Item> &list_arg): Item_cond(list_arg) {}
1615 enum Functype functype() const { return COND_OR_FUNC; }
1616 int64_t val_int();
1617 const char *func_name() const { return "or"; }
1618 table_map not_null_tables() const { return and_tables_cache; }
1619 Item* copy_andor_structure(Session *session)
1620 {
1621 Item_cond_or *item;
1622 item= new Item_cond_or(session, this);
1623 item->copy_andor_arguments(session, this);
1624 return item;
1625 }
1626 Item *neg_transformer(Session *session);
1627};
1628
1629inline bool is_cond_or(Item *item)
1630{
1631 if (item->type() != Item::COND_ITEM)
1632 return false;
1633
1634 Item_cond *cond_item= (Item_cond*) item;
1635 return (cond_item->functype() == Item_func::COND_OR_FUNC);
1636}
1637
1638/*
1639 XOR is Item_cond, not an Item_int_func because we could like to
1640 optimize (a XOR b) later on. It's low prio, though
1641*/
1642
1643class Item_cond_xor :public Item_cond
1644{
1645public:
1646 Item_cond_xor() :Item_cond() {}
1647 Item_cond_xor(Item *i1,Item *i2) :Item_cond(i1,i2) {}
1648 enum Functype functype() const { return COND_XOR_FUNC; }
1649 /* TODO: remove the next line when implementing XOR optimization */
1650 enum Type type() const { return FUNC_ITEM; }
1651 int64_t val_int();
1652 const char *func_name() const { return "xor"; }
1654};
1655
1656enum_field_types agg_field_type(Item **items, uint32_t nitems);
1657
1658
1659/* Some useful inline functions */
1660
1661inline Item *and_conds(Item *a, Item *b)
1662{
1663 if (!b) return a;
1664 if (!a) return b;
1665 return new Item_cond_and(a, b);
1666}
1667
1668Item *and_expressions(Item *a, Item *b, Item **org_item);
1669
1670} /* namespace drizzled */
1671
virtual void print(String *str)
Definition cmpfunc.h:330
table_map not_null_tables() const
Definition cmpfunc.h:1587
table_map not_null_tables() const
Definition cmpfunc.h:1618
void split_sum_func(Session *session, Item **ref_pointer_array, List< Item > &fields)
Definition cmpfunc.cc:4144
table_map used_tables() const
Definition cmpfunc.cc:4154
Item * compile(Item_analyzer analyzer, unsigned char **arg_p, Item_transformer transformer, unsigned char *arg_t)
Definition cmpfunc.cc:4081
virtual void print(String *str)
Definition cmpfunc.cc:4176
Item * transform(Item_transformer transformer, unsigned char *arg)
Definition cmpfunc.cc:4042
void fix_after_pullout(Select_Lex *new_parent, Item **ref)
Definition cmpfunc.cc:3981
void merge(Item_equal *item)
Definition cmpfunc.cc:4995
void sort(Item_field_cmpfunc cmp, void *arg)
Definition cmpfunc.cc:5029
virtual void print(String *str)
Definition cmpfunc.cc:5175
Item * transform(Item_transformer transformer, unsigned char *arg)
Definition cmpfunc.cc:5161
bool contains(Field *field)
Definition cmpfunc.cc:4971
bool fix_fields(Session *, Item **)
Definition cmpfunc.cc:1998
virtual void print(String *str)
Definition cmpfunc.cc:2189
virtual void print(String *str)
Definition cmpfunc.cc:2854
type::Decimal * val_decimal(type::Decimal *)
Definition cmpfunc.cc:2699
Item * find_item(String *str)
Definition cmpfunc.cc:2606
table_map not_null_tables() const
Definition cmpfunc.h:1150
String * val_str(String *)
Definition cmpfunc.cc:2644
table_map not_null_tables() const
Definition cmpfunc.h:654
type::Decimal * decimal_op(type::Decimal *)
Performs the operation that this functions implements when the result type is DECIMAL.
Definition cmpfunc.cc:2938
double real_op()
Performs the operation that this functions implements when the result type is REAL.
Definition cmpfunc.cc:2923
String * str_op(String *)
Definition cmpfunc.cc:2895
int64_t int_op()
Performs the operation that this functions implements when the result type is INT.
Definition cmpfunc.cc:2909
table_map not_null_tables() const
Definition cmpfunc.h:467
String * val_str(String *str)
Definition cmpfunc.cc:2463
type::Decimal * val_decimal(type::Decimal *)
Definition cmpfunc.cc:2478
bool fix_fields(Session *, Item **)
Definition cmpfunc.cc:2359
String * str_op(String *str)
Definition cmpfunc.cc:2314
int64_t int_op()
Performs the operation that this functions implements when the result type is INT.
Definition cmpfunc.cc:2281
type::Decimal * decimal_op(type::Decimal *)
Performs the operation that this functions implements when the result type is DECIMAL.
Definition cmpfunc.cc:2297
double real_op()
Performs the operation that this functions implements when the result type is REAL.
Definition cmpfunc.cc:2265
bool fix_fields(Session *, Item **)
Definition cmpfunc.cc:3554
virtual void print(String *str)
Definition cmpfunc.cc:3812
Item * neg_transformer(Session *session)
Definition cmpfunc.cc:4809
virtual void print(String *str)
Definition cmpfunc.cc:4362
table_map not_null_tables() const
Definition cmpfunc.h:1336
table_map not_null_tables() const
Definition cmpfunc.h:1292
Item * neg_transformer(Session *session)
Definition cmpfunc.cc:4799
optimize_type select_optimize() const
Definition cmpfunc.cc:4399
bool turboBM_matches(const char *text, int text_len) const
Definition cmpfunc.cc:4641
void turboBM_compute_bad_character_shifts()
Definition cmpfunc.cc:4610
void turboBM_compute_good_suffix_shifts(int *suff)
Definition cmpfunc.cc:4566
void turboBM_compute_suffixes(int *suff)
Definition cmpfunc.cc:4512
virtual void top_level_item()
Definition cmpfunc.h:425
virtual void print(String *str)
Definition cmpfunc.cc:388
Item * neg_transformer(Session *session)
Definition cmpfunc.cc:4845
virtual void print(String *str)
Definition cmpfunc.cc:353
Item * neg_transformer(Session *session)
Definition cmpfunc.cc:4783
type::Decimal * val_decimal(type::Decimal *)
Definition cmpfunc.cc:2563
String * val_str(String *str)
Definition cmpfunc.cc:2547
virtual void print(String *str)
Definition cmpfunc.h:721
table_map not_null_tables() const
Definition cmpfunc.h:726
bool eq(const Item *item, bool binary_cmp) const
Definition cmpfunc.cc:1792
virtual void print(String *str)
Definition cmpfunc.h:607
table_map used_tables() const
Definition cmpfunc.h:408
virtual bool val_bool()
Definition cmpfunc.cc:1473
virtual void print(String *str)
Definition cmpfunc.cc:1458
virtual int64_t val_int()
Definition cmpfunc.cc:1496
table_map used_tables() const
Definition func.cc:430
virtual bool const_item() const
virtual void print(String *str)
Definition func.cc:442
Item * transform(Item_transformer transformer, unsigned char *arg)
Definition cmpfunc.cc:1680
table_map used_tables() const
Definition cmpfunc.h:1318
virtual int64_t val_int()=0
virtual double val_real()=0
bool maybe_null
Definition item.h:123
bool with_subselect
Definition item.h:144
virtual String * val_str(String *str)=0
String str_value
Definition item.h:109
Convert from decimal to binary representation.
Definition common_fwd.h:215
TODO: Rename this file - func.h is stupid.
enum_field_types agg_field_type(Item **items, uint32_t nitems)
Aggregates field types from the array of items.
Definition cmpfunc.cc:202
Item * and_expressions(Item *a, Item *b, Item **org_item)
Definition cmpfunc.cc:4285