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)
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, my_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();
257
245
const char *func_name() const { return "<in_optimizer>"; }
258
246
Item_cache **get_cache() { return &cache; }
259
247
void keep_top_level_cache();
260
Item *transform(Item_transformer transformer, unsigned char *arg);
248
Item *transform(Item_transformer transformer, uchar *arg);
254
Comp_creator() {} /* Remove gcc warning */
255
virtual ~Comp_creator() {} /* Remove gcc warning */
256
virtual Item_bool_func2* create(Item *a, Item *b) const = 0;
257
virtual const char* symbol(bool invert) const = 0;
258
virtual bool eqne_op() const = 0;
259
virtual bool l_op() const = 0;
263
262
class Eq_creator :public Comp_creator
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(uchar **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);
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"; };
757
746
/* A vector of values of some type */
759
class in_vector :public memory::SqlAlloc
748
class in_vector :public Sql_alloc
764
753
qsort2_cmp compare;
765
754
const CHARSET_INFO *collation;
769
in_vector(uint32_t elements,uint32_t element_length,qsort2_cmp cmp_func,
758
in_vector(uint elements,uint 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
virtual void set(uint32_t pos,Item *item)=0;
776
virtual unsigned char *get_value(Item *item)=0;
764
virtual void set(uint pos,Item *item)=0;
765
virtual uchar *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
810
803
char buff[STRING_BUFFER_USUAL_SIZE];
813
in_string(uint32_t elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs);
806
in_string(uint elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs);
815
void set(uint32_t pos,Item *item);
816
unsigned char *get_value(Item *item);
808
void set(uint pos,Item *item);
809
uchar *get_value(Item *item);
817
810
Item* create_item()
819
812
return new Item_string(collation);
821
void value_to_item(uint32_t pos, Item *item)
814
void value_to_item(uint pos, Item *item)
823
816
String *str=((String*) base)+pos;
824
817
Item_string *to= (Item_string*)item;
825
818
to->str_value= *str;
834
827
Here we declare a temporary variable (tmp) of the same type as the
835
elements of this vector. tmp is used in finding if a given value is in
828
elements of this vector. tmp is used in finding if a given value is in
838
struct packed_int64_t
831
struct packed_int64_t
841
834
int64_t unsigned_flag; // Use int64_t, not bool, to preserve alignment
844
in_int64_t(uint32_t elements);
845
void set(uint32_t pos,Item *item);
846
unsigned char *get_value(Item *item);
837
in_int64_t(uint elements);
838
void set(uint pos,Item *item);
839
uchar *get_value(Item *item);
848
841
Item* create_item()
851
We're created a signed INT, this may not be correct in
844
We're created a signed INT, this may not be correct in
852
845
general case (see BUG#19342).
854
847
return new Item_int((int64_t)0);
856
void value_to_item(uint32_t pos, Item *item)
849
void value_to_item(uint pos, Item *item)
858
851
((Item_int*) item)->value= ((packed_int64_t*) base)[pos].val;
859
852
((Item_int*) item)->unsigned_flag= (bool)
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);
885
void set(uint32_t pos,Item *item);
886
unsigned char *get_value(Item *item);
876
in_datetime(Item *warn_item_arg, uint elements)
877
:in_int64_t(elements), thd(current_thd), warn_item(warn_item_arg),
879
void set(uint pos,Item *item);
880
uchar *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
in_decimal(uint32_t elements);
915
void set(uint32_t pos, Item *item);
916
unsigned char *get_value(Item *item);
908
in_decimal(uint elements);
909
void set(uint pos, Item *item);
910
uchar *get_value(Item *item);
917
911
Item *create_item()
919
913
return new Item_decimal(0, false);
921
void value_to_item(uint32_t pos, Item *item)
915
void value_to_item(uint 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);
1272
1262
cmp_item_row tmp;
1274
in_row(uint32_t elements, Item *);
1264
in_row(uint elements, Item *);
1276
void set(uint32_t pos,Item *item);
1277
unsigned char *get_value(Item *item);
1266
void set(uint pos,Item *item);
1267
uchar *get_value(Item *item);
1278
1268
friend void Item_func_in::fix_length_and_dec();
1279
1269
Item_result result_type() { return ROW_RESULT; }
1282
1272
/* Functions used by where clause */
1284
class Item_func_isnull :public item::function::Boolean
1274
class Item_func_isnull :public Item_bool_func
1287
1277
int64_t cached_value;
1289
Item_func_isnull(Item *a) :item::function::Boolean(a) {}
1279
Item_func_isnull(Item *a) :Item_bool_func(a) {}
1290
1280
int64_t val_int();
1291
1281
enum Functype functype() const { return ISNULL_FUNC; }
1292
1282
void fix_length_and_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);
1454
bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg);
1455
Item *transform(Item_transformer transformer, unsigned char *arg);
1438
void copy_andor_arguments(THD *thd, Item_cond *item);
1439
bool walk(Item_processor processor, bool walk_subquery, uchar *arg);
1440
Item *transform(Item_transformer transformer, uchar *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(uchar **arg __attribute__((unused)))
1460
1445
{ return true; }
1461
Item *compile(Item_analyzer analyzer, unsigned char **arg_p,
1462
Item_transformer transformer, unsigned char *arg_t);
1446
Item *compile(Item_analyzer analyzer, uchar **arg_p,
1447
Item_transformer transformer, uchar *arg_t);
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);
1562
1539
inline Item* get_const() { return const_item; }
1563
1540
void add(Item *c);
1564
1541
void add(Item_field *f);
1566
1543
bool contains(Field *field);
1567
1544
Item_field* get_first() { return fields.head(); }
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
bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg);
1580
Item *transform(Item_transformer transformer, unsigned char *arg);
1556
bool walk(Item_processor processor, bool walk_subquery, uchar *arg);
1557
Item *transform(Item_transformer transformer, uchar *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
uint32_t max_members; /* max number of members the current level
1596
list and all lower level lists */
1566
uint max_members; /* max number of members the current level
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)