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
22
/* compare and test functions */
25
#include "drizzled/comp_creator.h"
26
#include "drizzled/item/row.h"
27
#include "drizzled/item/sum.h"
28
#include "drizzled/item/int.h"
29
#include "drizzled/item/float.h"
30
#include "drizzled/item/string.h"
31
#include "drizzled/item/decimal.h"
32
#include "drizzled/function/math/int.h"
33
#include "drizzled/function/numhybrid.h"
34
#include "drizzled/session.h"
35
#include "drizzled/common.h"
36
#include "drizzled/qsort_cmp.h"
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>
41
40
extern Item_result item_cmp_type(Item_result a,Item_result b);
42
42
class Item_bool_func2;
43
43
class Arg_comparator;
44
44
class Item_sum_hybrid;
47
48
typedef int (Arg_comparator::*arg_cmp_func)();
49
50
typedef int (*Item_field_cmpfunc)(Item_field *f1, Item_field *f2, void *arg);
51
uint64_t get_datetime_value(Session *session,
52
int64_t get_datetime_value(Session *session,
57
58
class Arg_comparator: public memory::SqlAlloc
68
69
bool is_nulls_eq; // TRUE <=> compare for the EQUAL_FUNC
69
70
enum enum_date_cmp_type { CMP_DATE_DFLT= 0, CMP_DATE_WITH_DATE,
70
71
CMP_DATE_WITH_STR, CMP_STR_WITH_DATE };
71
uint64_t (*get_value_func)(Session *session, Item ***item_arg, Item **cache_arg,
72
Item *warn_item, bool *is_null);
72
int64_t (*get_value_func)(Session *session, Item ***item_arg, Item **cache_arg,
73
Item *warn_item, bool *is_null);
74
75
DTCollation cmp_collation;
76
Arg_comparator(): session(0), a_cache(0), b_cache(0) {};
77
Arg_comparator(Item **a1, Item **a2): a(a1), b(a2), session(0),
78
a_cache(0), b_cache(0) {};
79
Arg_comparator(Item **a1, Item **a2);
80
81
int set_compare_func(Item_bool_func2 *owner, Item_result type);
81
82
inline int set_compare_func(Item_bool_func2 *owner_arg)
117
118
int compare_datetime(); // compare args[0] & args[1] as DATETIMEs
119
120
static enum enum_date_cmp_type can_compare_as_dates(Item *a, Item *b,
120
uint64_t *const_val_arg);
121
int64_t *const_val_arg);
122
123
void set_datetime_cmp_func(Item **a1, Item **b1);
123
124
static arg_cmp_func comparator_matrix [5][2];
125
126
friend class Item_func;
128
class Item_bool_func :public Item_int_func
131
Item_bool_func() :Item_int_func() {}
132
Item_bool_func(Item *a) :Item_int_func(a) {}
133
Item_bool_func(Item *a,Item *b) :Item_int_func(a,b) {}
134
Item_bool_func(Session *session, Item_bool_func *item) :Item_int_func(session, item) {}
135
bool is_bool_func() { return 1; }
136
void fix_length_and_dec() { decimals=0; max_length=1; }
137
uint32_t decimal_precision() const { return 1; }
142
131
Abstract Item class, to represent <code>X IS [NOT] (TRUE | FALSE)</code>
143
132
boolean predicates.
146
class Item_func_truth : public Item_bool_func
135
class Item_func_truth : public item::function::Boolean
149
138
virtual bool val_bool();
150
139
virtual int64_t val_int();
151
140
virtual void fix_length_and_dec();
152
virtual void print(String *str, enum_query_type query_type);
141
virtual void print(String *str);
155
144
Item_func_truth(Item *a, bool a_value, bool a_affirmative)
156
: Item_bool_func(a), value(a_value), affirmative(a_affirmative)
145
: item::function::Boolean(a), value(a_value), affirmative(a_affirmative)
159
148
~Item_func_truth()
256
245
bool result_for_null_param;
258
247
Item_in_optimizer(Item *a, Item_in_subselect *b):
259
Item_bool_func(a, reinterpret_cast<Item *>(b)), cache(0),
248
item::function::Boolean(a, reinterpret_cast<Item *>(b)), cache(0),
260
249
save_cache(0), result_for_null_param(UNKNOWN)
261
250
{ with_subselect= true; }
262
251
bool fix_fields(Session *, Item **);
361
350
virtual enum Functype rev_functype() const { return UNKNOWN_FUNC; }
362
351
bool have_rev_func() const { return rev_functype() != UNKNOWN_FUNC; }
364
virtual inline void print(String *str, enum_query_type query_type)
353
virtual inline void print(String *str)
366
Item_func::print_op(str, query_type);
355
Item_func::print_op(str);
369
358
bool is_null() { return test(args[0]->is_null() || args[1]->is_null()); }
391
class Item_func_not :public Item_bool_func
380
class Item_func_not :public item::function::Boolean
394
Item_func_not(Item *a) :Item_bool_func(a) {}
383
Item_func_not(Item *a) :item::function::Boolean(a) {}
395
384
int64_t val_int();
396
385
enum Functype functype() const { return NOT_FUNC; }
397
386
const char *func_name() const { return "not"; }
398
387
Item *neg_transformer(Session *session);
399
virtual void print(String *str, enum_query_type query_type);
388
virtual void print(String *str);
402
391
class Item_maxmin_subselect;
430
419
- To wrap condition that is pushed down into subquery
433
class Item_func_trig_cond: public Item_bool_func
422
class Item_func_trig_cond: public item::function::Boolean
437
Item_func_trig_cond(Item *a, bool *f) : Item_bool_func(a) { trig_var= f; }
426
Item_func_trig_cond(Item *a, bool *f) : item::function::Boolean(a) { trig_var= f; }
438
427
int64_t val_int() { return *trig_var ? args[0]->val_int() : 1; }
439
428
enum Functype functype() const { return TRIG_COND_FUNC; };
440
429
const char *func_name() const { return "trigcond"; };
463
452
int64_t val_int();
464
453
enum Functype functype() const { return NOT_ALL_FUNC; }
465
454
const char *func_name() const { return "<not>"; }
466
virtual void print(String *str, enum_query_type query_type);
455
virtual void print(String *str);
467
456
void set_sum_test(Item_sum_hybrid *item) { test_sum_item= item; };
468
457
void set_sub_test(Item_maxmin_subselect *item) { test_sub_item= item; };
469
458
bool empty_underlying_subquery();
625
614
const char *func_name() const { return "between"; }
626
615
bool fix_fields(Session *, Item **);
627
616
void fix_length_and_dec();
628
virtual void print(String *str, enum_query_type query_type);
617
virtual void print(String *str);
629
618
bool is_bool_func() { return 1; }
630
619
const CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
631
620
uint32_t decimal_precision() const { return 1; }
640
629
optimize_type select_optimize() const { return OPTIMIZE_NONE; }
641
630
const char *func_name() const { return "strcmp"; }
643
virtual inline void print(String *str, enum_query_type query_type)
632
virtual inline void print(String *str)
645
Item_func::print(str, query_type);
634
Item_func::print(str);
682
671
double real_op();
683
672
int64_t int_op();
684
673
String *str_op(String *);
685
my_decimal *decimal_op(my_decimal *);
674
type::Decimal *decimal_op(type::Decimal *);
686
675
void fix_length_and_dec();
687
676
void find_num_type() {}
688
677
enum Item_result result_type () const { return hybrid_type; }
701
690
double real_op();
702
691
int64_t int_op();
703
692
String *str_op(String *str);
704
my_decimal *decimal_op(my_decimal *);
693
type::Decimal *decimal_op(type::Decimal *);
705
694
enum_field_types field_type() const;
706
695
void fix_length_and_dec();
707
696
const char *func_name() const { return "ifnull"; }
725
714
double val_real();
726
715
int64_t val_int();
727
716
String *val_str(String *str);
728
my_decimal *val_decimal(my_decimal *);
717
type::Decimal *val_decimal(type::Decimal *);
729
718
enum Item_result result_type () const { return cached_result_type; }
730
719
enum_field_types field_type() const { return cached_field_type; }
731
720
bool fix_fields(Session *, Item **);
745
734
double val_real();
746
735
int64_t val_int();
747
736
String *val_str(String *str);
748
my_decimal *val_decimal(my_decimal *);
737
type::Decimal *val_decimal(type::Decimal *);
749
738
enum Item_result result_type () const { return cached_result_type; }
750
739
void fix_length_and_dec();
751
740
uint32_t decimal_precision() const { return args[0]->decimal_precision(); }
752
741
const char *func_name() const { return "nullif"; }
754
virtual inline void print(String *str, enum_query_type query_type)
743
virtual inline void print(String *str)
756
Item_func::print(str, query_type);
745
Item_func::print(str);
759
748
table_map not_null_tables() const { return 0; }
890
879
/* Cache for the left item. */
891
880
Item *lval_cache;
893
in_datetime(Item *warn_item_arg, uint32_t elements)
894
:in_int64_t(elements), session(current_session), warn_item(warn_item_arg),
882
in_datetime(Item *warn_item_arg, uint32_t elements);
896
884
void set(uint32_t pos,Item *item);
897
885
unsigned char *get_value(Item *item);
898
886
friend int cmp_int64_t(void *cmp_arg, packed_int64_t *a,packed_int64_t *b);
932
920
void value_to_item(uint32_t pos, Item *item)
934
my_decimal *dec= ((my_decimal *)base) + pos;
922
type::Decimal *dec= ((type::Decimal *)base) + pos;
935
923
Item_decimal *item_dec= (Item_decimal*)item;
936
924
item_dec->set_decimal_value(dec);
950
938
const CHARSET_INFO *cmp_charset;
951
cmp_item() { cmp_charset= &my_charset_bin; }
942
cmp_charset= &my_charset_bin;
952
945
virtual ~cmp_item() {}
953
946
virtual void store_value(Item *item)= 0;
954
947
virtual int cmp(Item *item)= 0;
1047
1041
/* Cache for the left item. */
1048
1042
Item *lval_cache;
1050
cmp_item_datetime(Item *warn_item_arg)
1051
:session(current_session), warn_item(warn_item_arg), lval_cache(0) {}
1044
cmp_item_datetime(Item *warn_item_arg);
1052
1046
void store_value(Item *item);
1053
1047
int cmp(Item *arg);
1054
1048
int compare(cmp_item *ci);
1157
1151
:Item_func(), first_expr_num(-1), else_expr_num(-1),
1158
1152
cached_result_type(INT_RESULT), left_result_type(INT_RESULT), case_item(0)
1160
ncases= list.elements;
1154
ncases= list.size();
1161
1155
if (first_expr_arg)
1163
first_expr_num= list.elements;
1157
first_expr_num= list.size();
1164
1158
list.push_back(first_expr_arg);
1166
1160
if (else_expr_arg)
1168
else_expr_num= list.elements;
1162
else_expr_num= list.size();
1169
1163
list.push_back(else_expr_arg);
1171
1165
set_arguments(list);
1174
1168
double val_real();
1175
1169
int64_t val_int();
1176
1170
String *val_str(String *);
1177
my_decimal *val_decimal(my_decimal *);
1171
type::Decimal *val_decimal(type::Decimal *);
1178
1172
bool fix_fields(Session *session, Item **ref);
1179
1173
void fix_length_and_dec();
1180
1174
uint32_t decimal_precision() const;
1182
1176
enum Item_result result_type () const { return cached_result_type; }
1183
1177
enum_field_types field_type() const { return cached_field_type; }
1184
1178
const char *func_name() const { return "case"; }
1185
virtual void print(String *str, enum_query_type query_type);
1179
virtual void print(String *str);
1186
1180
Item *find_item(String *str);
1187
1181
const CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
1188
1182
void cleanup();
1248
1242
optimize_type select_optimize() const
1249
1243
{ return OPTIMIZE_KEY; }
1250
virtual void print(String *str, enum_query_type query_type);
1244
virtual void print(String *str);
1251
1245
enum Functype functype() const { return IN_FUNC; }
1252
1246
const char *func_name() const { return " IN "; }
1253
1247
bool nulls_in_row();
1287
1281
/* Functions used by where clause */
1289
class Item_func_isnull :public Item_bool_func
1283
class Item_func_isnull :public item::function::Boolean
1292
1286
int64_t cached_value;
1294
Item_func_isnull(Item *a) :Item_bool_func(a) {}
1288
Item_func_isnull(Item *a) :item::function::Boolean(a) {}
1295
1289
int64_t val_int();
1296
1290
enum Functype functype() const { return ISNULL_FUNC; }
1297
1291
void fix_length_and_dec()
1356
class Item_func_isnotnull :public Item_bool_func
1350
class Item_func_isnotnull :public item::function::Boolean
1358
1352
bool abort_on_null;
1360
Item_func_isnotnull(Item *a) :Item_bool_func(a), abort_on_null(0) {}
1354
Item_func_isnotnull(Item *a) :item::function::Boolean(a), abort_on_null(0) {}
1361
1355
int64_t val_int();
1362
1356
enum Functype functype() const { return ISNOTNULL_FUNC; }
1363
1357
void fix_length_and_dec()
1369
1363
table_map not_null_tables() const
1370
1364
{ return abort_on_null ? not_null_tables_cache : 0; }
1371
1365
Item *neg_transformer(Session *session);
1372
virtual void print(String *str, enum_query_type query_type);
1366
virtual void print(String *str);
1373
1367
const CHARSET_INFO *compare_collation() { return args[0]->collation.collation; }
1374
1368
void top_level_item() { abort_on_null=1; }
1429
1423
using Item::split_sum_func;
1431
1425
/* Item_cond() is only used to create top level items */
1432
Item_cond(): Item_bool_func(), abort_on_null(1)
1426
Item_cond(): item::function::Boolean(), abort_on_null(1)
1433
1427
{ const_item_cache=0; }
1434
1428
Item_cond(Item *i1,Item *i2)
1435
:Item_bool_func(), abort_on_null(0)
1429
:item::function::Boolean(), abort_on_null(0)
1437
1431
list.push_back(i1);
1438
1432
list.push_back(i2);
1440
1434
Item_cond(Session *session, Item_cond *item);
1441
1435
Item_cond(List<Item> &nlist)
1442
:Item_bool_func(), list(nlist), abort_on_null(0) {}
1443
bool add(Item *item) { return list.push_back(item); }
1444
bool add_at_head(Item *item) { return list.push_front(item); }
1436
:item::function::Boolean(), list(nlist), abort_on_null(0) {}
1437
void add(Item *item) { list.push_back(item); }
1438
void add_at_head(Item *item) { list.push_front(item); }
1445
1439
void add_at_head(List<Item> *nlist) { list.prepand(nlist); }
1446
1440
bool fix_fields(Session *, Item **ref);
1447
1441
void fix_after_pullout(Select_Lex *new_parent, Item **ref);
1450
1444
List<Item>* argument_list() { return &list; }
1451
1445
table_map used_tables() const;
1452
1446
void update_used_tables();
1453
virtual void print(String *str, enum_query_type query_type);
1447
virtual void print(String *str);
1454
1448
void split_sum_func(Session *session, Item **ref_pointer_array, List<Item> &fields);
1455
1449
friend int setup_conds(Session *session, TableList *tables, TableList *leaves,
1543
1537
object represents f1=f2= ...=fn to the projection of known fields fi1=...=fik.
1546
class Item_equal: public Item_bool_func
1540
class Item_equal: public item::function::Boolean
1548
List<Item_field> fields; /* list of equal field items */
1549
Item *const_item; /* optional constant item equal to fields items */
1550
cmp_item *eval_item;
1554
: Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
1555
{ const_item_cache=0 ;}
1543
typedef List<Item_field> fields_t;
1553
fields_t::iterator begin()
1555
return fields.begin();
1556
1558
Item_equal(Item_field *f1, Item_field *f2);
1557
1559
Item_equal(Item *c, Item_field *f);
1558
1560
Item_equal(Item_equal *item_equal);
1561
1563
void add(Item_field *f);
1562
1564
uint32_t members();
1563
1565
bool contains(Field *field);
1564
Item_field* get_first() { return fields.head(); }
1566
Item_field* get_first() { return &fields.front(); }
1565
1567
void merge(Item_equal *item);
1566
1568
void update_const();
1567
1569
enum Functype functype() const { return MULT_EQUAL_FUNC; }
1569
1571
const char *func_name() const { return "multiple equal"; }
1570
1572
optimize_type select_optimize() const { return OPTIMIZE_EQUAL; }
1571
1573
void sort(Item_field_cmpfunc cmp, void *arg);
1572
friend class Item_equal_iterator;
1573
1574
void fix_length_and_dec();
1574
1575
bool fix_fields(Session *session, Item **ref);
1575
1576
void update_used_tables();
1576
1577
bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg);
1577
1578
Item *transform(Item_transformer transformer, unsigned char *arg);
1578
virtual void print(String *str, enum_query_type query_type);
1579
virtual void print(String *str);
1579
1580
const CHARSET_INFO *compare_collation()
1580
{ return fields.head()->collation.collation; }
1581
{ return fields.front().collation.collation; }
1583
fields_t fields; /* list of equal field items */
1584
Item *const_item; /* optional constant item equal to fields items */
1585
cmp_item *eval_item;
1583
1590
class COND_EQUAL: public memory::SqlAlloc
1598
class Item_equal_iterator : public List_iterator_fast<Item_field>
1601
inline Item_equal_iterator(Item_equal &item_equal)
1602
:List_iterator_fast<Item_field> (item_equal.fields)
1604
inline Item_field* operator++(int)
1606
Item_field *item= (*(List_iterator_fast<Item_field> *) this)++;
1609
inline void rewind(void)
1611
List_iterator_fast<Item_field>::rewind();
1604
typedef List<Item_field>::iterator Item_equal_iterator;
1615
1606
class Item_cond_and :public Item_cond