1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
4
* Copyright (C) 2008 Sun Microsystems
6
6
* This program is free software; you can redistribute it and/or modify
7
7
* it under the terms of the GNU General Public License as published by
23
23
/* 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>
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/decimal.h"
31
#include "drizzled/function/math/int.h"
32
#include "drizzled/function/numhybrid.h"
33
#include "drizzled/session.h"
34
#include "drizzled/common.h"
35
#include "drizzled/qsort_cmp.h"
41
40
extern Item_result item_cmp_type(Item_result a,Item_result b);
43
41
class Item_bool_func2;
44
42
class Arg_comparator;
45
43
class Item_sum_hybrid;
49
46
typedef int (Arg_comparator::*arg_cmp_func)();
51
48
typedef int (*Item_field_cmpfunc)(Item_field *f1, Item_field *f2, void *arg);
53
int64_t get_datetime_value(Session *session,
50
uint64_t get_datetime_value(Session *session,
59
56
class Arg_comparator: public memory::SqlAlloc
70
67
bool is_nulls_eq; // TRUE <=> compare for the EQUAL_FUNC
71
68
enum enum_date_cmp_type { CMP_DATE_DFLT= 0, CMP_DATE_WITH_DATE,
72
69
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);
70
uint64_t (*get_value_func)(Session *session, Item ***item_arg, Item **cache_arg,
71
Item *warn_item, bool *is_null);
76
73
DTCollation cmp_collation;
80
Arg_comparator(Item **a1, Item **a2);
75
Arg_comparator(): session(0), a_cache(0), b_cache(0) {};
76
Arg_comparator(Item **a1, Item **a2): a(a1), b(a2), session(0),
77
a_cache(0), b_cache(0) {};
82
79
int set_compare_func(Item_bool_func2 *owner, Item_result type);
83
80
inline int set_compare_func(Item_bool_func2 *owner_arg)
119
116
int compare_datetime(); // compare args[0] & args[1] as DATETIMEs
121
118
static enum enum_date_cmp_type can_compare_as_dates(Item *a, Item *b,
122
int64_t *const_val_arg);
119
uint64_t *const_val_arg);
124
121
void set_datetime_cmp_func(Item **a1, Item **b1);
125
122
static arg_cmp_func comparator_matrix [5][2];
127
124
friend class Item_func;
127
class Item_bool_func :public Item_int_func
130
Item_bool_func() :Item_int_func() {}
131
Item_bool_func(Item *a) :Item_int_func(a) {}
132
Item_bool_func(Item *a,Item *b) :Item_int_func(a,b) {}
133
Item_bool_func(Session *session, Item_bool_func *item) :Item_int_func(session, item) {}
134
bool is_bool_func() { return 1; }
135
void fix_length_and_dec() { decimals=0; max_length=1; }
136
uint32_t decimal_precision() const { return 1; }
132
141
Abstract Item class, to represent <code>X IS [NOT] (TRUE | FALSE)</code>
133
142
boolean predicates.
136
class Item_func_truth : public item::function::Boolean
145
class Item_func_truth : public Item_bool_func
139
148
virtual bool val_bool();
145
154
Item_func_truth(Item *a, bool a_value, bool a_affirmative)
146
: item::function::Boolean(a), value(a_value), affirmative(a_affirmative)
155
: Item_bool_func(a), value(a_value), affirmative(a_affirmative)
149
158
~Item_func_truth()
246
255
bool result_for_null_param;
248
257
Item_in_optimizer(Item *a, Item_in_subselect *b):
249
item::function::Boolean(a, reinterpret_cast<Item *>(b)), cache(0),
258
Item_bool_func(a, reinterpret_cast<Item *>(b)), cache(0),
250
259
save_cache(0), result_for_null_param(UNKNOWN)
251
260
{ with_subselect= true; }
252
261
bool fix_fields(Session *, Item **);
381
class Item_func_not :public item::function::Boolean
390
class Item_func_not :public Item_bool_func
384
Item_func_not(Item *a) :item::function::Boolean(a) {}
393
Item_func_not(Item *a) :Item_bool_func(a) {}
385
394
int64_t val_int();
386
395
enum Functype functype() const { return NOT_FUNC; }
387
396
const char *func_name() const { return "not"; }
420
429
- To wrap condition that is pushed down into subquery
423
class Item_func_trig_cond: public item::function::Boolean
432
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; }
436
Item_func_trig_cond(Item *a, bool *f) : Item_bool_func(a) { trig_var= f; }
428
437
int64_t val_int() { return *trig_var ? args[0]->val_int() : 1; }
429
438
enum Functype functype() const { return TRIG_COND_FUNC; };
430
439
const char *func_name() const { return "trigcond"; };
672
681
double real_op();
673
682
int64_t int_op();
674
683
String *str_op(String *);
675
type::Decimal *decimal_op(type::Decimal *);
684
my_decimal *decimal_op(my_decimal *);
676
685
void fix_length_and_dec();
677
686
void find_num_type() {}
678
687
enum Item_result result_type () const { return hybrid_type; }
691
700
double real_op();
692
701
int64_t int_op();
693
702
String *str_op(String *str);
694
type::Decimal *decimal_op(type::Decimal *);
703
my_decimal *decimal_op(my_decimal *);
695
704
enum_field_types field_type() const;
696
705
void fix_length_and_dec();
697
706
const char *func_name() const { return "ifnull"; }
715
724
double val_real();
716
725
int64_t val_int();
717
726
String *val_str(String *str);
718
type::Decimal *val_decimal(type::Decimal *);
727
my_decimal *val_decimal(my_decimal *);
719
728
enum Item_result result_type () const { return cached_result_type; }
720
729
enum_field_types field_type() const { return cached_field_type; }
721
730
bool fix_fields(Session *, Item **);
735
744
double val_real();
736
745
int64_t val_int();
737
746
String *val_str(String *str);
738
type::Decimal *val_decimal(type::Decimal *);
747
my_decimal *val_decimal(my_decimal *);
739
748
enum Item_result result_type () const { return cached_result_type; }
740
749
void fix_length_and_dec();
741
750
uint32_t decimal_precision() const { return args[0]->decimal_precision(); }
880
889
/* Cache for the left item. */
881
890
Item *lval_cache;
883
in_datetime(Item *warn_item_arg, uint32_t elements);
892
in_datetime(Item *warn_item_arg, uint32_t elements)
893
:in_int64_t(elements), session(current_session), warn_item(warn_item_arg),
885
895
void set(uint32_t pos,Item *item);
886
896
unsigned char *get_value(Item *item);
887
897
friend int cmp_int64_t(void *cmp_arg, packed_int64_t *a,packed_int64_t *b);
921
931
void value_to_item(uint32_t pos, Item *item)
923
type::Decimal *dec= ((type::Decimal *)base) + pos;
933
my_decimal *dec= ((my_decimal *)base) + pos;
924
934
Item_decimal *item_dec= (Item_decimal*)item;
925
935
item_dec->set_decimal_value(dec);
939
949
const CHARSET_INFO *cmp_charset;
943
cmp_charset= &my_charset_bin;
950
cmp_item() { cmp_charset= &my_charset_bin; }
946
951
virtual ~cmp_item() {}
947
952
virtual void store_value(Item *item)= 0;
948
953
virtual int cmp(Item *item)= 0;
1042
1046
/* Cache for the left item. */
1043
1047
Item *lval_cache;
1045
cmp_item_datetime(Item *warn_item_arg);
1049
cmp_item_datetime(Item *warn_item_arg)
1050
:session(current_session), warn_item(warn_item_arg), lval_cache(0) {}
1047
1051
void store_value(Item *item);
1048
1052
int cmp(Item *arg);
1049
1053
int compare(cmp_item *ci);
1145
1149
Item_result cmp_type;
1146
1150
DTCollation cmp_collation;
1147
1151
enum_field_types cached_field_type;
1148
cmp_item *cmp_items[DECIMAL_RESULT+1]; /* For all result types */
1152
cmp_item *cmp_items[5]; /* For all result types */
1149
1153
cmp_item *case_item;
1151
1155
Item_func_case(List<Item> &list, Item *first_expr_arg, Item *else_expr_arg)
1169
1173
double val_real();
1170
1174
int64_t val_int();
1171
1175
String *val_str(String *);
1172
type::Decimal *val_decimal(type::Decimal *);
1176
my_decimal *val_decimal(my_decimal *);
1173
1177
bool fix_fields(Session *session, Item **ref);
1174
1178
void fix_length_and_dec();
1175
1179
uint32_t decimal_precision() const;
1282
1287
/* Functions used by where clause */
1284
class Item_func_isnull :public item::function::Boolean
1289
class Item_func_isnull :public Item_bool_func
1287
1292
int64_t cached_value;
1289
Item_func_isnull(Item *a) :item::function::Boolean(a) {}
1294
Item_func_isnull(Item *a) :Item_bool_func(a) {}
1290
1295
int64_t val_int();
1291
1296
enum Functype functype() const { return ISNULL_FUNC; }
1292
1297
void fix_length_and_dec()
1351
class Item_func_isnotnull :public item::function::Boolean
1356
class Item_func_isnotnull :public Item_bool_func
1353
1358
bool abort_on_null;
1355
Item_func_isnotnull(Item *a) :item::function::Boolean(a), abort_on_null(0) {}
1360
Item_func_isnotnull(Item *a) :Item_bool_func(a), abort_on_null(0) {}
1356
1361
int64_t val_int();
1357
1362
enum Functype functype() const { return ISNOTNULL_FUNC; }
1358
1363
void fix_length_and_dec()
1424
1429
using Item::split_sum_func;
1426
1431
/* Item_cond() is only used to create top level items */
1427
Item_cond(): item::function::Boolean(), abort_on_null(1)
1432
Item_cond(): Item_bool_func(), abort_on_null(1)
1428
1433
{ const_item_cache=0; }
1429
1434
Item_cond(Item *i1,Item *i2)
1430
:item::function::Boolean(), abort_on_null(0)
1435
:Item_bool_func(), abort_on_null(0)
1432
1437
list.push_back(i1);
1433
1438
list.push_back(i2);
1435
1440
Item_cond(Session *session, Item_cond *item);
1436
1441
Item_cond(List<Item> &nlist)
1437
:item::function::Boolean(), list(nlist), abort_on_null(0) {}
1442
:Item_bool_func(), list(nlist), abort_on_null(0) {}
1438
1443
bool add(Item *item) { return list.push_back(item); }
1439
1444
bool add_at_head(Item *item) { return list.push_front(item); }
1440
1445
void add_at_head(List<Item> *nlist) { list.prepand(nlist); }
1538
1543
object represents f1=f2= ...=fn to the projection of known fields fi1=...=fik.
1541
class Item_equal: public item::function::Boolean
1546
class Item_equal: public Item_bool_func
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;
1544
typedef List<Item_field> fields_t;
1554
fields_t::iterator begin()
1556
return fields.begin();
1554
: Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
1555
{ const_item_cache=0 ;}
1559
1556
Item_equal(Item_field *f1, Item_field *f2);
1560
1557
Item_equal(Item *c, Item_field *f);
1561
1558
Item_equal(Item_equal *item_equal);
1581
1578
virtual void print(String *str, enum_query_type query_type);
1582
1579
const CHARSET_INFO *compare_collation()
1583
1580
{ 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
1583
class COND_EQUAL: public memory::SqlAlloc
1607
class Item_equal_iterator : public List<Item_field>::iterator
1598
class Item_equal_iterator : public List_iterator_fast<Item_field>
1610
1601
inline Item_equal_iterator(Item_equal &item_equal)
1611
:List<Item_field>::iterator (item_equal.fields.begin() )
1602
:List_iterator_fast<Item_field> (item_equal.fields)
1613
1604
inline Item_field* operator++(int)
1615
Item_field *item= (*(List<Item_field>::iterator *) this)++;
1606
Item_field *item= (*(List_iterator_fast<Item_field> *) this)++;
1609
inline void rewind(void)
1611
List_iterator_fast<Item_field>::rewind();
1620
1615
class Item_cond_and :public Item_cond