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/common.h"
35
#include "drizzled/qsort_cmp.h"
36
#include "drizzled/item/function/boolean.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
39
extern Item_result item_cmp_type(Item_result a,Item_result b);
43
class Item_bool_func2;
45
class Item_sum_hybrid;
49
41
typedef int (Arg_comparator::*arg_cmp_func)();
51
43
typedef int (*Item_field_cmpfunc)(Item_field *f1, Item_field *f2, void *arg);
76
68
DTCollation cmp_collation;
79
session(current_session),
84
Arg_comparator(Item **a1, Item **a2):
87
session(current_session),
72
Arg_comparator(Item **a1, Item **a2);
92
74
int set_compare_func(Item_bool_func2 *owner, Item_result type);
93
75
inline int set_compare_func(Item_bool_func2 *owner_arg)
149
131
virtual bool val_bool();
150
132
virtual int64_t val_int();
151
133
virtual void fix_length_and_dec();
152
virtual void print(String *str, enum_query_type query_type);
134
virtual void print(String *str);
155
137
Item_func_truth(Item *a, bool a_value, bool a_affirmative)
156
138
: item::function::Boolean(a), value(a_value), affirmative(a_affirmative)
163
143
True for <code>X IS [NOT] TRUE</code>,
220
197
Item_func_isnotfalse(Item *a) : Item_func_truth(a, false, false) {}
221
~Item_func_isnotfalse() {}
222
198
virtual const char* func_name() const { return "isnotfalse"; }
227
202
#define UNKNOWN ((bool)-1)
273
248
class Eq_creator :public Comp_creator
276
Eq_creator() {} /* Remove gcc warning */
277
virtual ~Eq_creator() {} /* Remove gcc warning */
278
251
virtual Item_bool_func2* create(Item *a, Item *b) const;
279
252
virtual const char* symbol(bool invert) const { return invert? "<>" : "="; }
280
253
virtual bool eqne_op() const { return 1; }
285
258
class Ne_creator :public Comp_creator
288
Ne_creator() {} /* Remove gcc warning */
289
virtual ~Ne_creator() {} /* Remove gcc warning */
290
261
virtual Item_bool_func2* create(Item *a, Item *b) const;
291
262
virtual const char* symbol(bool invert) const { return invert? "=" : "<>"; }
292
263
virtual bool eqne_op() const { return 1; }
297
268
class Gt_creator :public Comp_creator
300
Gt_creator() {} /* Remove gcc warning */
301
virtual ~Gt_creator() {} /* Remove gcc warning */
302
271
virtual Item_bool_func2* create(Item *a, Item *b) const;
303
272
virtual const char* symbol(bool invert) const { return invert? "<=" : ">"; }
304
273
virtual bool eqne_op() const { return 0; }
309
278
class Lt_creator :public Comp_creator
312
Lt_creator() {} /* Remove gcc warning */
313
virtual ~Lt_creator() {} /* Remove gcc warning */
314
281
virtual Item_bool_func2* create(Item *a, Item *b) const;
315
282
virtual const char* symbol(bool invert) const { return invert? ">=" : "<"; }
316
283
virtual bool eqne_op() const { return 0; }
321
288
class Ge_creator :public Comp_creator
324
Ge_creator() {} /* Remove gcc warning */
325
virtual ~Ge_creator() {} /* Remove gcc warning */
326
291
virtual Item_bool_func2* create(Item *a, Item *b) const;
327
292
virtual const char* symbol(bool invert) const { return invert? "<" : ">="; }
328
293
virtual bool eqne_op() const { return 0; }
333
298
class Le_creator :public Comp_creator
336
Le_creator() {} /* Remove gcc warning */
337
virtual ~Le_creator() {} /* Remove gcc warning */
338
301
virtual Item_bool_func2* create(Item *a, Item *b) const;
339
302
virtual const char* symbol(bool invert) const { return invert? ">" : "<="; }
340
303
virtual bool eqne_op() const { return 0; }
361
324
virtual enum Functype rev_functype() const { return UNKNOWN_FUNC; }
362
325
bool have_rev_func() const { return rev_functype() != UNKNOWN_FUNC; }
364
virtual inline void print(String *str, enum_query_type query_type)
327
virtual inline void print(String *str)
366
Item_func::print_op(str, query_type);
329
Item_func::print_op(str);
369
332
bool is_null() { return test(args[0]->is_null() || args[1]->is_null()); }
370
333
bool is_bool_func() { return 1; }
371
const CHARSET_INFO *compare_collation() { return cmp.cmp_collation.collation; }
334
const charset_info_st *compare_collation() { return cmp.cmp_collation.collation; }
372
335
uint32_t decimal_precision() const { return 1; }
373
336
void top_level_item() { abort_on_null= true; }
396
359
enum Functype functype() const { return NOT_FUNC; }
397
360
const char *func_name() const { return "not"; }
398
361
Item *neg_transformer(Session *session);
399
virtual void print(String *str, enum_query_type query_type);
362
virtual void print(String *str);
402
class Item_maxmin_subselect;
405
366
trigcond<param>(arg) ::= param? arg : TRUE
463
424
int64_t val_int();
464
425
enum Functype functype() const { return NOT_ALL_FUNC; }
465
426
const char *func_name() const { return "<not>"; }
466
virtual void print(String *str, enum_query_type query_type);
427
virtual void print(String *str);
467
428
void set_sum_test(Item_sum_hybrid *item) { test_sum_item= item; };
468
429
void set_sub_test(Item_maxmin_subselect *item) { test_sub_item= item; };
469
430
bool empty_underlying_subquery();
625
586
const char *func_name() const { return "between"; }
626
587
bool fix_fields(Session *, Item **);
627
588
void fix_length_and_dec();
628
virtual void print(String *str, enum_query_type query_type);
589
virtual void print(String *str);
629
590
bool is_bool_func() { return 1; }
630
const CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
591
const charset_info_st *compare_collation() { return cmp_collation.collation; }
631
592
uint32_t decimal_precision() const { return 1; }
640
601
optimize_type select_optimize() const { return OPTIMIZE_NONE; }
641
602
const char *func_name() const { return "strcmp"; }
643
virtual inline void print(String *str, enum_query_type query_type)
604
virtual inline void print(String *str)
645
Item_func::print(str, query_type);
606
Item_func::print(str);
717
678
class Item_func_if :public Item_func
719
enum Item_result cached_result_type;
680
Item_result cached_result_type;
720
681
enum_field_types cached_field_type;
722
Item_func_if(Item *a,Item *b,Item *c)
723
:Item_func(a,b,c), cached_result_type(INT_RESULT)
684
Item_func_if(Item *a, Item *b, Item *c) :
686
cached_result_type(INT_RESULT)
725
689
double val_real();
726
690
int64_t val_int();
727
691
String *val_str(String *str);
728
692
type::Decimal *val_decimal(type::Decimal *);
729
enum Item_result result_type () const { return cached_result_type; }
693
Item_result result_type () const { return cached_result_type; }
730
694
enum_field_types field_type() const { return cached_field_type; }
731
695
bool fix_fields(Session *, Item **);
732
696
void fix_length_and_dec();
751
715
uint32_t decimal_precision() const { return args[0]->decimal_precision(); }
752
716
const char *func_name() const { return "nullif"; }
754
virtual inline void print(String *str, enum_query_type query_type)
718
virtual inline void print(String *str)
756
Item_func::print(str, query_type);
720
Item_func::print(str);
759
723
table_map not_null_tables() const { return 0; }
774
738
qsort2_cmp compare;
775
const CHARSET_INFO *collation;
739
const charset_info_st *collation;
777
741
uint32_t used_count;
779
743
in_vector(uint32_t elements,uint32_t element_length,qsort2_cmp cmp_func,
780
const CHARSET_INFO * const cmp_coll)
744
const charset_info_st * const cmp_coll)
781
745
:base((char*) memory::sql_calloc(elements*element_length)),
782
746
size(element_length), compare(cmp_func), collation(cmp_coll),
783
747
count(elements), used_count(elements) {}
820
784
char buff[STRING_BUFFER_USUAL_SIZE];
823
in_string(uint32_t elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs);
787
in_string(uint32_t elements,qsort2_cmp cmp_func, const charset_info_st * const cs);
825
789
void set(uint32_t pos,Item *item);
826
790
unsigned char *get_value(Item *item);
890
854
/* Cache for the left item. */
891
855
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),
857
in_datetime(Item *warn_item_arg, uint32_t elements);
896
859
void set(uint32_t pos,Item *item);
897
860
unsigned char *get_value(Item *item);
898
861
friend int cmp_int64_t(void *cmp_arg, packed_int64_t *a,packed_int64_t *b);
959
922
virtual int cmp(Item *item)= 0;
960
923
// for optimized IN with row
961
924
virtual int compare(cmp_item *item)= 0;
962
static cmp_item* get_comparator(Item_result type, const CHARSET_INFO * const cs);
925
static cmp_item* get_comparator(Item_result type, const charset_info_st * const cs);
963
926
virtual cmp_item *make_same()= 0;
964
927
virtual void store_value_by_template(cmp_item *, Item *item)
973
936
String *value_res;
975
938
cmp_item_string () {}
976
cmp_item_string (const CHARSET_INFO * const cs) { cmp_charset= cs; }
977
void set_charset(const CHARSET_INFO * const cs) { cmp_charset= cs; }
939
cmp_item_string (const charset_info_st * const cs) { cmp_charset= cs; }
940
void set_charset(const charset_info_st * const cs) { cmp_charset= cs; }
978
941
friend class cmp_item_sort_string;
979
942
friend class cmp_item_sort_string_in_static;
988
951
cmp_item_sort_string():
989
952
cmp_item_string() {}
990
cmp_item_sort_string(const CHARSET_INFO * const cs):
953
cmp_item_sort_string(const charset_info_st * const cs):
991
954
cmp_item_string(cs),
992
955
value(value_buff, sizeof(value_buff), cs) {}
993
956
void store_value(Item *item)
1008
971
return sortcmp(value_res, l_cmp->value_res, cmp_charset);
1010
973
cmp_item *make_same();
1011
void set_charset(const CHARSET_INFO * const cs)
974
void set_charset(const charset_info_st * const cs)
1013
976
cmp_charset= cs;
1014
977
value.set_quick(value_buff, sizeof(value_buff), cs);
1053
1015
/* Cache for the left item. */
1054
1016
Item *lval_cache;
1056
cmp_item_datetime(Item *warn_item_arg)
1057
:session(current_session), warn_item(warn_item_arg), lval_cache(0) {}
1018
cmp_item_datetime(Item *warn_item_arg);
1058
1020
void store_value(Item *item);
1059
1021
int cmp(Item *arg);
1060
1022
int compare(cmp_item *ci);
1163
1123
:Item_func(), first_expr_num(-1), else_expr_num(-1),
1164
1124
cached_result_type(INT_RESULT), left_result_type(INT_RESULT), case_item(0)
1166
ncases= list.elements;
1126
ncases= list.size();
1167
1127
if (first_expr_arg)
1169
first_expr_num= list.elements;
1129
first_expr_num= list.size();
1170
1130
list.push_back(first_expr_arg);
1172
1132
if (else_expr_arg)
1174
else_expr_num= list.elements;
1134
else_expr_num= list.size();
1175
1135
list.push_back(else_expr_arg);
1177
1137
set_arguments(list);
1188
1148
enum Item_result result_type () const { return cached_result_type; }
1189
1149
enum_field_types field_type() const { return cached_field_type; }
1190
1150
const char *func_name() const { return "case"; }
1191
virtual void print(String *str, enum_query_type query_type);
1151
virtual void print(String *str);
1192
1152
Item *find_item(String *str);
1193
const CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
1153
const charset_info_st *compare_collation() { return cmp_collation.collation; }
1194
1154
void cleanup();
1195
1155
void agg_str_lengths(Item *arg);
1196
1156
void agg_num_lengths(Item *arg);
1254
1214
optimize_type select_optimize() const
1255
1215
{ return OPTIMIZE_KEY; }
1256
virtual void print(String *str, enum_query_type query_type);
1216
virtual void print(String *str);
1257
1217
enum Functype functype() const { return IN_FUNC; }
1258
1218
const char *func_name() const { return " IN "; }
1259
1219
bool nulls_in_row();
1260
1220
bool is_bool_func() { return 1; }
1261
const CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
1221
const charset_info_st *compare_collation() { return cmp_collation.collation; }
1264
1224
class cmp_item_row :public cmp_item
1329
1289
table_map not_null_tables() const { return 0; }
1330
1290
optimize_type select_optimize() const { return OPTIMIZE_NULL; }
1331
1291
Item *neg_transformer(Session *session);
1332
const CHARSET_INFO *compare_collation() { return args[0]->collation.collation; }
1292
const charset_info_st *compare_collation() { return args[0]->collation.collation; }
1335
1295
/* Functions used by HAVING for rewriting IN subquery */
1337
class Item_in_subselect;
1340
1298
This is like IS NOT NULL but it also remembers if it ever has
1341
1299
encountered a NULL.
1375
1333
table_map not_null_tables() const
1376
1334
{ return abort_on_null ? not_null_tables_cache : 0; }
1377
1335
Item *neg_transformer(Session *session);
1378
virtual void print(String *str, enum_query_type query_type);
1379
const CHARSET_INFO *compare_collation() { return args[0]->collation.collation; }
1336
virtual void print(String *str);
1337
const charset_info_st *compare_collation() { return args[0]->collation.collation; }
1380
1338
void top_level_item() { abort_on_null=1; }
1446
1402
Item_cond(Session *session, Item_cond *item);
1447
1403
Item_cond(List<Item> &nlist)
1448
1404
:item::function::Boolean(), list(nlist), abort_on_null(0) {}
1449
bool add(Item *item) { return list.push_back(item); }
1450
bool add_at_head(Item *item) { return list.push_front(item); }
1405
void add(Item *item) { list.push_back(item); }
1406
void add_at_head(Item *item) { list.push_front(item); }
1451
1407
void add_at_head(List<Item> *nlist) { list.prepand(nlist); }
1452
1408
bool fix_fields(Session *, Item **ref);
1453
1409
void fix_after_pullout(Select_Lex *new_parent, Item **ref);
1456
1412
List<Item>* argument_list() { return &list; }
1457
1413
table_map used_tables() const;
1458
1414
void update_used_tables();
1459
virtual void print(String *str, enum_query_type query_type);
1415
virtual void print(String *str);
1460
1416
void split_sum_func(Session *session, Item **ref_pointer_array, List<Item> &fields);
1461
1417
friend int setup_conds(Session *session, TableList *tables, TableList *leaves,
1552
1508
class Item_equal: public item::function::Boolean
1554
List<Item_field> fields; /* list of equal field items */
1555
Item *const_item; /* optional constant item equal to fields items */
1556
cmp_item *eval_item;
1511
typedef List<Item_field> fields_t;
1560
inline Item_equal() :
1561
item::function::Boolean(),
1566
1518
const_item_cache=0;
1521
fields_t::iterator begin()
1523
return fields.begin();
1569
1526
Item_equal(Item_field *f1, Item_field *f2);
1570
1527
Item_equal(Item *c, Item_field *f);
1571
1528
Item_equal(Item_equal *item_equal);
1574
1531
void add(Item_field *f);
1575
1532
uint32_t members();
1576
1533
bool contains(Field *field);
1577
Item_field* get_first() { return fields.head(); }
1534
Item_field* get_first() { return &fields.front(); }
1578
1535
void merge(Item_equal *item);
1579
1536
void update_const();
1580
1537
enum Functype functype() const { return MULT_EQUAL_FUNC; }
1582
1539
const char *func_name() const { return "multiple equal"; }
1583
1540
optimize_type select_optimize() const { return OPTIMIZE_EQUAL; }
1584
1541
void sort(Item_field_cmpfunc cmp, void *arg);
1585
friend class Item_equal_iterator;
1586
1542
void fix_length_and_dec();
1587
1543
bool fix_fields(Session *session, Item **ref);
1588
1544
void update_used_tables();
1589
1545
bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg);
1590
1546
Item *transform(Item_transformer transformer, unsigned char *arg);
1591
virtual void print(String *str, enum_query_type query_type);
1592
const CHARSET_INFO *compare_collation()
1593
{ return fields.head()->collation.collation; }
1547
virtual void print(String *str);
1548
const charset_info_st *compare_collation()
1549
{ return fields.front().collation.collation; }
1551
fields_t fields; /* list of equal field items */
1552
Item *const_item; /* optional constant item equal to fields items */
1553
cmp_item *eval_item;
1596
1558
class COND_EQUAL: public memory::SqlAlloc
1611
class Item_equal_iterator : public List_iterator_fast<Item_field>
1614
inline Item_equal_iterator(Item_equal &item_equal)
1615
:List_iterator_fast<Item_field> (item_equal.fields)
1617
inline Item_field* operator++(int)
1619
Item_field *item= (*(List_iterator_fast<Item_field> *) this)++;
1622
inline void rewind(void)
1624
List_iterator_fast<Item_field>::rewind();
1572
typedef List<Item_field>::iterator Item_equal_iterator;
1628
1574
class Item_cond_and :public Item_cond
1643
1589
Item* copy_andor_structure(Session *session)
1645
1591
Item_cond_and *item;
1646
if ((item= new Item_cond_and(session, this)))
1647
item->copy_andor_arguments(session, this);
1592
item= new Item_cond_and(session, this);
1593
item->copy_andor_arguments(session, this);
1650
1596
Item *neg_transformer(Session *session);
1673
1619
Item* copy_andor_structure(Session *session)
1675
1621
Item_cond_or *item;
1676
if ((item= new Item_cond_or(session, this)))
1677
item->copy_andor_arguments(session, this);
1622
item= new Item_cond_or(session, this);
1623
item->copy_andor_arguments(session, this);
1680
1626
Item *neg_transformer(Session *session);