17
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20
#ifndef DRIZZLED_ITEM_CMPFUNC_H
21
#define DRIZZLED_ITEM_CMPFUNC_H
23
21
/* compare and test functions */
25
#include <drizzled/common.h>
26
#include <drizzled/comp_creator.h>
27
#include <drizzled/function/math/int.h>
28
#include <drizzled/function/numhybrid.h>
29
#include <drizzled/item/decimal.h>
30
#include <drizzled/item/float.h>
31
#include <drizzled/item/function/boolean.h>
32
#include <drizzled/item/int.h>
33
#include <drizzled/item/row.h>
34
#include <drizzled/item/string.h>
35
#include <drizzled/item/sum.h>
36
#include <drizzled/qsort_cmp.h>
23
#ifdef USE_PRAGMA_INTERFACE
24
#pragma interface /* gcc class implementation */
41
27
extern Item_result item_cmp_type(Item_result a,Item_result b);
43
28
class Item_bool_func2;
44
29
class Arg_comparator;
45
class Item_sum_hybrid;
49
31
typedef int (Arg_comparator::*arg_cmp_func)();
51
typedef int (*Item_field_cmpfunc)(Item_field *f1, Item_field *f2, void *arg);
53
int64_t get_datetime_value(Session *session,
59
class Arg_comparator: public memory::SqlAlloc
33
typedef int (*Item_field_cmpfunc)(Item_field *f1, Item_field *f2, void *arg);
35
class Arg_comparator: public Sql_alloc
64
40
Arg_comparator *comparators; // used only for compare_row()
66
42
/* Fields used in DATE/DATETIME comparison. */
68
44
enum_field_types a_type, b_type; // Types of a and b items
69
45
Item *a_cache, *b_cache; // Cached values of a and b items
70
46
bool is_nulls_eq; // TRUE <=> compare for the EQUAL_FUNC
71
47
enum enum_date_cmp_type { CMP_DATE_DFLT= 0, CMP_DATE_WITH_DATE,
72
48
CMP_DATE_WITH_STR, CMP_STR_WITH_DATE };
73
int64_t (*get_value_func)(Session *session, Item ***item_arg, Item **cache_arg,
74
Item *warn_item, bool *is_null);
49
uint64_t (*get_value_func)(THD *thd, Item ***item_arg, Item **cache_arg,
50
Item *warn_item, bool *is_null);
76
52
DTCollation cmp_collation;
80
Arg_comparator(Item **a1, Item **a2);
54
Arg_comparator(): thd(0), a_cache(0), b_cache(0) {};
55
Arg_comparator(Item **a1, Item **a2): a(a1), b(a2), thd(0),
56
a_cache(0), b_cache(0) {};
82
58
int set_compare_func(Item_bool_func2 *owner, Item_result type);
83
59
inline int set_compare_func(Item_bool_func2 *owner_arg)
127
103
friend class Item_func;
106
class Item_bool_func :public Item_int_func
109
Item_bool_func() :Item_int_func() {}
110
Item_bool_func(Item *a) :Item_int_func(a) {}
111
Item_bool_func(Item *a,Item *b) :Item_int_func(a,b) {}
112
Item_bool_func(THD *thd, Item_bool_func *item) :Item_int_func(thd, item) {}
113
bool is_bool_func() { return 1; }
114
void fix_length_and_dec() { decimals=0; max_length=1; }
115
uint32_t decimal_precision() const { return 1; }
132
120
Abstract Item class, to represent <code>X IS [NOT] (TRUE | FALSE)</code>
133
121
boolean predicates.
136
class Item_func_truth : public item::function::Boolean
124
class Item_func_truth : public Item_bool_func
139
127
virtual bool val_bool();
246
234
bool result_for_null_param;
248
236
Item_in_optimizer(Item *a, Item_in_subselect *b):
249
item::function::Boolean(a, reinterpret_cast<Item *>(b)), cache(0),
237
Item_bool_func(a, reinterpret_cast<Item *>(b)), cache(0),
250
238
save_cache(0), result_for_null_param(UNKNOWN)
251
239
{ with_subselect= true; }
252
bool fix_fields(Session *, Item **);
253
bool fix_left(Session *session, Item **ref);
240
bool fix_fields(THD *, Item **);
241
bool fix_left(THD *thd, Item **ref);
255
243
int64_t val_int();
373
366
allowed_arg_cols= 0; // Fetch this value from first argument
375
Item *neg_transformer(Session *session);
368
Item *neg_transformer(THD *thd);
376
369
virtual Item *negated_item();
377
bool subst_argument_checker(unsigned char **)
370
bool subst_argument_checker(unsigned char **arg __attribute__((unused)))
381
class Item_func_not :public item::function::Boolean
374
class Item_func_not :public Item_bool_func
384
Item_func_not(Item *a) :item::function::Boolean(a) {}
377
Item_func_not(Item *a) :Item_bool_func(a) {}
385
378
int64_t val_int();
386
379
enum Functype functype() const { return NOT_FUNC; }
387
380
const char *func_name() const { return "not"; }
388
Item *neg_transformer(Session *session);
381
Item *neg_transformer(THD *thd);
389
382
virtual void print(String *str, enum_query_type query_type);
395
388
trigcond<param>(arg) ::= param? arg : TRUE
397
The class Item_func_trig_cond is used for guarded predicates
390
The class Item_func_trig_cond is used for guarded predicates
398
391
which are employed only for internal purposes.
399
392
A guarded predicate is an object consisting of an a regular or
400
a guarded predicate P and a pointer to a boolean guard variable g.
393
a guarded predicate P and a pointer to a boolean guard variable g.
401
394
A guarded predicate P/g is evaluated to true if the value of the
402
395
guard g is false, otherwise it is evaluated to the same value that
403
396
the predicate P: val(P/g)= g ? val(P):true.
409
402
the objects consisting of three elements: a predicate P, a pointer
410
403
to a variable g and a firing value s with following evaluation
411
404
rule: val(P/g,s)= g==s? val(P) : true. It will allow us to build only
412
one item for the objects of the form P/g1/g2...
405
one item for the objects of the form P/g1/g2...
414
407
Objects of this class are built only for query execution after
415
408
the execution plan has been already selected. That's why this
416
class needs only val_int out of generic methods.
409
class needs only val_int out of generic methods.
418
411
Current uses of Item_func_trig_cond objects:
419
412
- To wrap selection conditions when executing outer joins
420
413
- To wrap condition that is pushed down into subquery
423
class Item_func_trig_cond: public item::function::Boolean
416
class Item_func_trig_cond: public Item_bool_func
427
Item_func_trig_cond(Item *a, bool *f) : item::function::Boolean(a) { trig_var= f; }
420
Item_func_trig_cond(Item *a, bool *f) : Item_bool_func(a) { trig_var= f; }
428
421
int64_t val_int() { return *trig_var ? args[0]->val_int() : 1; }
429
422
enum Functype functype() const { return TRIG_COND_FUNC; };
430
423
const char *func_name() const { return "trigcond"; };
715
704
double val_real();
716
705
int64_t val_int();
717
706
String *val_str(String *str);
718
type::Decimal *val_decimal(type::Decimal *);
707
my_decimal *val_decimal(my_decimal *);
719
708
enum Item_result result_type () const { return cached_result_type; }
720
709
enum_field_types field_type() const { return cached_field_type; }
721
bool fix_fields(Session *, Item **);
710
bool fix_fields(THD *, Item **);
722
711
void fix_length_and_dec();
723
712
uint32_t decimal_precision() const;
724
713
const char *func_name() const { return "if"; }
767
756
uint32_t used_count;
769
in_vector(uint32_t elements,uint32_t element_length,qsort2_cmp cmp_func,
758
in_vector(uint32_t elements,uint32_t element_length,qsort2_cmp cmp_func,
770
759
const CHARSET_INFO * const cmp_coll)
771
:base((char*) memory::sql_calloc(elements*element_length)),
760
:base((char*) sql_calloc(elements*element_length)),
772
761
size(element_length), compare(cmp_func), collation(cmp_coll),
773
762
count(elements), used_count(elements) {}
774
763
virtual ~in_vector() {}
775
764
virtual void set(uint32_t pos,Item *item)=0;
776
765
virtual unsigned char *get_value(Item *item)=0;
768
my_qsort2(base,used_count,size,compare, (void *) collation);
778
770
int find(Item *item);
781
773
Create an instance of Item_{type} (e.g. Item_decimal) constant object
782
774
which type allows it to hold an element of this vector without any
874
867
class in_datetime :public in_int64_t
878
871
/* An item used to issue warnings. */
880
873
/* Cache for the left item. */
881
874
Item *lval_cache;
883
in_datetime(Item *warn_item_arg, uint32_t elements);
876
in_datetime(Item *warn_item_arg, uint32_t elements)
877
:in_int64_t(elements), thd(current_thd), warn_item(warn_item_arg),
885
879
void set(uint32_t pos,Item *item);
886
880
unsigned char *get_value(Item *item);
887
881
friend int cmp_int64_t(void *cmp_arg, packed_int64_t *a,packed_int64_t *b);
910
904
class in_decimal :public in_vector
914
908
in_decimal(uint32_t elements);
915
909
void set(uint32_t pos, Item *item);
916
910
unsigned char *get_value(Item *item);
917
911
Item *create_item()
919
913
return new Item_decimal(0, false);
921
915
void value_to_item(uint32_t pos, Item *item)
923
type::Decimal *dec= ((type::Decimal *)base) + pos;
917
my_decimal *dec= ((my_decimal *)base) + pos;
924
918
Item_decimal *item_dec= (Item_decimal*)item;
925
919
item_dec->set_decimal_value(dec);
1388
1378
enum { alphabet_size = 256 };
1390
1380
Item *escape_item;
1392
1382
bool escape_used_in_parsing;
1399
1387
Item_func_like(Item *a,Item *b, Item *escape_arg, bool escape_used)
1400
:Item_bool_func2(a,b), canDoTurboBM(false), pattern(0), pattern_len(0),
1388
:Item_bool_func2(a,b), canDoTurboBM(false), pattern(0), pattern_len(0),
1401
1389
bmGs(0), bmBc(0), escape_item(escape_arg),
1402
escape_used_in_parsing(escape_used), escape(NULL) {}
1390
escape_used_in_parsing(escape_used) {}
1403
1391
int64_t val_int();
1404
1392
enum Functype functype() const { return LIKE_FUNC; }
1405
1393
optimize_type select_optimize() const;
1406
1394
cond_result eq_cmp_result() const { return COND_TRUE; }
1407
1395
const char *func_name() const { return "like"; }
1408
bool fix_fields(Session *session, Item **ref);
1396
bool fix_fields(THD *thd, Item **ref);
1409
1397
void cleanup();
1413
1401
typedef class Item COND;
1415
class Item_cond :public item::function::Boolean
1403
class Item_cond :public Item_bool_func
1418
1406
List<Item> list;
1420
1408
table_map and_tables_cache;
1424
using Item::split_sum_func;
1426
1411
/* Item_cond() is only used to create top level items */
1427
Item_cond(): item::function::Boolean(), abort_on_null(1)
1412
Item_cond(): Item_bool_func(), abort_on_null(1)
1428
1413
{ const_item_cache=0; }
1429
1414
Item_cond(Item *i1,Item *i2)
1430
:item::function::Boolean(), abort_on_null(0)
1415
:Item_bool_func(), abort_on_null(0)
1432
1417
list.push_back(i1);
1433
1418
list.push_back(i2);
1435
Item_cond(Session *session, Item_cond *item);
1420
Item_cond(THD *thd, Item_cond *item);
1436
1421
Item_cond(List<Item> &nlist)
1437
:item::function::Boolean(), list(nlist), abort_on_null(0) {}
1422
:Item_bool_func(), list(nlist), abort_on_null(0) {}
1438
1423
bool add(Item *item) { return list.push_back(item); }
1439
1424
bool add_at_head(Item *item) { return list.push_front(item); }
1440
1425
void add_at_head(List<Item> *nlist) { list.prepand(nlist); }
1441
bool fix_fields(Session *, Item **ref);
1442
void fix_after_pullout(Select_Lex *new_parent, Item **ref);
1426
bool fix_fields(THD *, Item **ref);
1427
void fix_after_pullout(st_select_lex *new_parent, Item **ref);
1444
1429
enum Type type() const { return COND_ITEM; }
1445
1430
List<Item>* argument_list() { return &list; }
1446
1431
table_map used_tables() const;
1447
1432
void update_used_tables();
1448
1433
virtual void print(String *str, enum_query_type query_type);
1449
void split_sum_func(Session *session, Item **ref_pointer_array, List<Item> &fields);
1450
friend int setup_conds(Session *session, TableList *tables, TableList *leaves,
1434
void split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields);
1435
friend int setup_conds(THD *thd, TableList *tables, TableList *leaves,
1452
1437
void top_level_item() { abort_on_null=1; }
1453
void copy_andor_arguments(Session *session, Item_cond *item);
1438
void copy_andor_arguments(THD *thd, Item_cond *item);
1454
1439
bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg);
1455
1440
Item *transform(Item_transformer transformer, unsigned char *arg);
1456
1441
void traverse_cond(Cond_traverser, void *arg, traverse_order order);
1457
void neg_arguments(Session *session);
1442
void neg_arguments(THD *thd);
1458
1443
enum_field_types field_type() const { return DRIZZLE_TYPE_LONGLONG; }
1459
bool subst_argument_checker(unsigned char **)
1444
bool subst_argument_checker(unsigned char **arg __attribute__((unused)))
1460
1445
{ return true; }
1461
1446
Item *compile(Item_analyzer analyzer, unsigned char **arg_p,
1462
1447
Item_transformer transformer, unsigned char *arg_t);
1509
1494
It also can give us additional index scans and can allow us to
1510
1495
improve selectivity estimates.
1512
3. An object Item_equal(t1.f1,...,tk.fk) is used to optimize the
1513
selected execution plan for the query: if table ti is accessed
1497
3. An object Item_equal(t1.f1,...,tk.fk) is used to optimize the
1498
selected execution plan for the query: if table ti is accessed
1514
1499
before the table tj then in any predicate P in the where condition
1515
1500
the occurrence of tj.fj is substituted for ti.fi. This can allow
1516
1501
an evaluation of the predicate at an earlier step.
1518
When feature 1 is supported they say that join transitive closure
1503
When feature 1 is supported they say that join transitive closure
1520
1505
When feature 2 is supported they say that search argument transitive
1521
1506
closure is employed.
1538
1523
object represents f1=f2= ...=fn to the projection of known fields fi1=...=fik.
1541
class Item_equal: public item::function::Boolean
1526
class Item_equal: public Item_bool_func
1528
List<Item_field> fields; /* list of equal field items */
1529
Item *const_item; /* optional constant item equal to fields items */
1530
cmp_item *eval_item;
1544
typedef List<Item_field> fields_t;
1554
fields_t::iterator begin()
1556
return fields.begin();
1534
: Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
1535
{ const_item_cache=0 ;}
1559
1536
Item_equal(Item_field *f1, Item_field *f2);
1560
1537
Item_equal(Item *c, Item_field *f);
1561
1538
Item_equal(Item_equal *item_equal);
1568
1545
void merge(Item_equal *item);
1569
1546
void update_const();
1570
1547
enum Functype functype() const { return MULT_EQUAL_FUNC; }
1572
1549
const char *func_name() const { return "multiple equal"; }
1573
1550
optimize_type select_optimize() const { return OPTIMIZE_EQUAL; }
1574
1551
void sort(Item_field_cmpfunc cmp, void *arg);
1575
1552
friend class Item_equal_iterator;
1576
1553
void fix_length_and_dec();
1577
bool fix_fields(Session *session, Item **ref);
1554
bool fix_fields(THD *thd, Item **ref);
1578
1555
void update_used_tables();
1579
1556
bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg);
1580
1557
Item *transform(Item_transformer transformer, unsigned char *arg);
1581
1558
virtual void print(String *str, enum_query_type query_type);
1582
const CHARSET_INFO *compare_collation()
1559
const CHARSET_INFO *compare_collation()
1583
1560
{ return fields.head()->collation.collation; }
1585
fields_t fields; /* list of equal field items */
1586
Item *const_item; /* optional constant item equal to fields items */
1587
cmp_item *eval_item;
1592
class COND_EQUAL: public memory::SqlAlloc
1563
class COND_EQUAL: public Sql_alloc
1595
1566
uint32_t max_members; /* max number of members the current level
1596
list and all lower level lists */
1567
list and all lower level lists */
1597
1568
COND_EQUAL *upper_levels; /* multiple equalities of upper and levels */
1598
List<Item_equal> current_level; /* list of multiple equalities of
1569
List<Item_equal> current_level; /* list of multiple equalities of
1599
1570
the current and level */
1602
1573
upper_levels= 0;
1607
class Item_equal_iterator : public List<Item_field>::iterator
1578
class Item_equal_iterator : public List_iterator_fast<Item_field>
1610
inline Item_equal_iterator(Item_equal &item_equal)
1611
:List<Item_field>::iterator (item_equal.fields.begin() )
1581
inline Item_equal_iterator(Item_equal &item_equal)
1582
:List_iterator_fast<Item_field> (item_equal.fields)
1613
1584
inline Item_field* operator++(int)
1615
Item_field *item= (*(List<Item_field>::iterator *) this)++;
1586
Item_field *item= (*(List_iterator_fast<Item_field> *) this)++;
1589
inline void rewind(void)
1591
List_iterator_fast<Item_field>::rewind();
1620
1595
class Item_cond_and :public Item_cond
1623
COND_EQUAL cond_equal; /* contains list of Item_equal objects for
1598
COND_EQUAL cond_equal; /* contains list of Item_equal objects for
1624
1599
the current and level and reference
1625
to multiple equalities of upper and levels */
1600
to multiple equalities of upper and levels */
1626
1601
Item_cond_and() :Item_cond() {}
1627
1602
Item_cond_and(Item *i1,Item *i2) :Item_cond(i1,i2) {}
1628
Item_cond_and(Session *session, Item_cond_and *item) :Item_cond(session, item) {}
1603
Item_cond_and(THD *thd, Item_cond_and *item) :Item_cond(thd, item) {}
1629
1604
Item_cond_and(List<Item> &list_arg): Item_cond(list_arg) {}
1630
1605
enum Functype functype() const { return COND_AND_FUNC; }
1631
1606
int64_t val_int();
1632
1607
const char *func_name() const { return "and"; }
1633
1608
table_map not_null_tables() const
1634
1609
{ return abort_on_null ? not_null_tables_cache: and_tables_cache; }
1635
Item* copy_andor_structure(Session *session)
1610
Item* copy_andor_structure(THD *thd)
1637
1612
Item_cond_and *item;
1638
if ((item= new Item_cond_and(session, this)))
1639
item->copy_andor_arguments(session, this);
1613
if ((item= new Item_cond_and(thd, this)))
1614
item->copy_andor_arguments(thd, this);
1642
Item *neg_transformer(Session *session);
1617
Item *neg_transformer(THD *thd);
1645
1620
inline bool is_cond_and(Item *item)
1657
1632
Item_cond_or() :Item_cond() {}
1658
1633
Item_cond_or(Item *i1,Item *i2) :Item_cond(i1,i2) {}
1659
Item_cond_or(Session *session, Item_cond_or *item) :Item_cond(session, item) {}
1634
Item_cond_or(THD *thd, Item_cond_or *item) :Item_cond(thd, item) {}
1660
1635
Item_cond_or(List<Item> &list_arg): Item_cond(list_arg) {}
1661
1636
enum Functype functype() const { return COND_OR_FUNC; }
1662
1637
int64_t val_int();
1663
1638
const char *func_name() const { return "or"; }
1664
1639
table_map not_null_tables() const { return and_tables_cache; }
1665
Item* copy_andor_structure(Session *session)
1640
Item* copy_andor_structure(THD *thd)
1667
1642
Item_cond_or *item;
1668
if ((item= new Item_cond_or(session, this)))
1669
item->copy_andor_arguments(session, this);
1643
if ((item= new Item_cond_or(thd, this)))
1644
item->copy_andor_arguments(thd, this);
1672
Item *neg_transformer(Session *session);
1647
Item *neg_transformer(THD *thd);
1675
1650
inline bool is_cond_or(Item *item)