1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
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
1
/* Copyright (C) 2000-2003 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
23
17
/* 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>
19
#ifdef USE_PRAGMA_INTERFACE
20
#pragma interface /* gcc class implementation */
41
23
extern Item_result item_cmp_type(Item_result a,Item_result b);
43
24
class Item_bool_func2;
44
25
class Arg_comparator;
45
class Item_sum_hybrid;
49
27
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
29
typedef int (*Item_field_cmpfunc)(Item_field *f1, Item_field *f2, void *arg);
31
class Arg_comparator: public Sql_alloc
64
36
Arg_comparator *comparators; // used only for compare_row()
66
38
/* Fields used in DATE/DATETIME comparison. */
68
40
enum_field_types a_type, b_type; // Types of a and b items
69
41
Item *a_cache, *b_cache; // Cached values of a and b items
70
42
bool is_nulls_eq; // TRUE <=> compare for the EQUAL_FUNC
71
43
enum enum_date_cmp_type { CMP_DATE_DFLT= 0, CMP_DATE_WITH_DATE,
72
44
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);
45
uint64_t (*get_value_func)(THD *thd, Item ***item_arg, Item **cache_arg,
46
Item *warn_item, bool *is_null);
76
48
DTCollation cmp_collation;
80
Arg_comparator(Item **a1, Item **a2);
50
Arg_comparator(): thd(0), a_cache(0), b_cache(0) {};
51
Arg_comparator(Item **a1, Item **a2): a(a1), b(a2), thd(0),
52
a_cache(0), b_cache(0) {};
82
54
int set_compare_func(Item_bool_func2 *owner, Item_result type);
83
55
inline int set_compare_func(Item_bool_func2 *owner_arg)
246
230
bool result_for_null_param;
248
232
Item_in_optimizer(Item *a, Item_in_subselect *b):
249
item::function::Boolean(a, reinterpret_cast<Item *>(b)), cache(0),
233
Item_bool_func(a, my_reinterpret_cast(Item *)(b)), cache(0),
250
234
save_cache(0), result_for_null_param(UNKNOWN)
251
235
{ with_subselect= true; }
252
bool fix_fields(Session *, Item **);
253
bool fix_left(Session *session, Item **ref);
236
bool fix_fields(THD *, Item **);
237
bool fix_left(THD *thd, Item **ref);
255
239
int64_t val_int();
257
241
const char *func_name() const { return "<in_optimizer>"; }
258
242
Item_cache **get_cache() { return &cache; }
259
243
void keep_top_level_cache();
260
Item *transform(Item_transformer transformer, unsigned char *arg);
244
Item *transform(Item_transformer transformer, uchar *arg);
250
Comp_creator() {} /* Remove gcc warning */
251
virtual ~Comp_creator() {} /* Remove gcc warning */
252
virtual Item_bool_func2* create(Item *a, Item *b) const = 0;
253
virtual const char* symbol(bool invert) const = 0;
254
virtual bool eqne_op() const = 0;
255
virtual bool l_op() const = 0;
263
258
class Eq_creator :public Comp_creator
373
362
allowed_arg_cols= 0; // Fetch this value from first argument
375
Item *neg_transformer(Session *session);
364
Item *neg_transformer(THD *thd);
376
365
virtual Item *negated_item();
377
bool subst_argument_checker(unsigned char **)
366
bool subst_argument_checker(uchar **arg __attribute__((unused)))
381
class Item_func_not :public item::function::Boolean
370
class Item_func_not :public Item_bool_func
384
Item_func_not(Item *a) :item::function::Boolean(a) {}
373
Item_func_not(Item *a) :Item_bool_func(a) {}
385
374
int64_t val_int();
386
375
enum Functype functype() const { return NOT_FUNC; }
387
376
const char *func_name() const { return "not"; }
388
Item *neg_transformer(Session *session);
377
Item *neg_transformer(THD *thd);
389
378
virtual void print(String *str, enum_query_type query_type);
409
398
the objects consisting of three elements: a predicate P, a pointer
410
399
to a variable g and a firing value s with following evaluation
411
400
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...
401
one item for the objects of the form P/g1/g2...
414
403
Objects of this class are built only for query execution after
415
404
the execution plan has been already selected. That's why this
416
class needs only val_int out of generic methods.
405
class needs only val_int out of generic methods.
418
407
Current uses of Item_func_trig_cond objects:
419
408
- To wrap selection conditions when executing outer joins
420
409
- To wrap condition that is pushed down into subquery
423
class Item_func_trig_cond: public item::function::Boolean
412
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; }
416
Item_func_trig_cond(Item *a, bool *f) : Item_bool_func(a) { trig_var= f; }
428
417
int64_t val_int() { return *trig_var ? args[0]->val_int() : 1; }
429
418
enum Functype functype() const { return TRIG_COND_FUNC; };
430
419
const char *func_name() const { return "trigcond"; };
757
742
/* A vector of values of some type */
759
class in_vector :public memory::SqlAlloc
744
class in_vector :public Sql_alloc
764
749
qsort2_cmp compare;
765
750
const CHARSET_INFO *collation;
769
in_vector(uint32_t elements,uint32_t element_length,qsort2_cmp cmp_func,
754
in_vector(uint elements,uint element_length,qsort2_cmp cmp_func,
770
755
const CHARSET_INFO * const cmp_coll)
771
:base((char*) memory::sql_calloc(elements*element_length)),
756
:base((char*) sql_calloc(elements*element_length)),
772
757
size(element_length), compare(cmp_func), collation(cmp_coll),
773
758
count(elements), used_count(elements) {}
774
759
virtual ~in_vector() {}
775
virtual void set(uint32_t pos,Item *item)=0;
776
virtual unsigned char *get_value(Item *item)=0;
760
virtual void set(uint pos,Item *item)=0;
761
virtual uchar *get_value(Item *item)=0;
764
my_qsort2(base,used_count,size,compare, (void *) collation);
778
766
int find(Item *item);
781
769
Create an instance of Item_{type} (e.g. Item_decimal) constant object
782
770
which type allows it to hold an element of this vector without any
810
799
char buff[STRING_BUFFER_USUAL_SIZE];
813
in_string(uint32_t elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs);
802
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);
804
void set(uint pos,Item *item);
805
uchar *get_value(Item *item);
817
806
Item* create_item()
819
808
return new Item_string(collation);
821
void value_to_item(uint32_t pos, Item *item)
810
void value_to_item(uint pos, Item *item)
823
812
String *str=((String*) base)+pos;
824
813
Item_string *to= (Item_string*)item;
825
814
to->str_value= *str;
834
823
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
824
elements of this vector. tmp is used in finding if a given value is in
838
struct packed_int64_t
827
struct packed_int64_t
841
830
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);
833
in_int64_t(uint elements);
834
void set(uint pos,Item *item);
835
uchar *get_value(Item *item);
848
837
Item* create_item()
851
We're created a signed INT, this may not be correct in
840
We're created a signed INT, this may not be correct in
852
841
general case (see BUG#19342).
854
843
return new Item_int((int64_t)0);
856
void value_to_item(uint32_t pos, Item *item)
845
void value_to_item(uint pos, Item *item)
858
847
((Item_int*) item)->value= ((packed_int64_t*) base)[pos].val;
859
848
((Item_int*) item)->unsigned_flag= (bool)
874
863
class in_datetime :public in_int64_t
878
867
/* An item used to issue warnings. */
880
869
/* Cache for the left item. */
881
870
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);
872
in_datetime(Item *warn_item_arg, uint elements)
873
:in_int64_t(elements), thd(current_thd), warn_item(warn_item_arg),
875
void set(uint pos,Item *item);
876
uchar *get_value(Item *item);
887
877
friend int cmp_int64_t(void *cmp_arg, packed_int64_t *a,packed_int64_t *b);
910
900
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);
904
in_decimal(uint elements);
905
void set(uint pos, Item *item);
906
uchar *get_value(Item *item);
917
907
Item *create_item()
919
909
return new Item_decimal(0, false);
921
void value_to_item(uint32_t pos, Item *item)
911
void value_to_item(uint pos, Item *item)
923
type::Decimal *dec= ((type::Decimal *)base) + pos;
913
my_decimal *dec= ((my_decimal *)base) + pos;
924
914
Item_decimal *item_dec= (Item_decimal*)item;
925
915
item_dec->set_decimal_value(dec);
1272
1258
cmp_item_row tmp;
1274
in_row(uint32_t elements, Item *);
1260
in_row(uint elements, Item *);
1276
void set(uint32_t pos,Item *item);
1277
unsigned char *get_value(Item *item);
1262
void set(uint pos,Item *item);
1263
uchar *get_value(Item *item);
1278
1264
friend void Item_func_in::fix_length_and_dec();
1279
1265
Item_result result_type() { return ROW_RESULT; }
1282
1268
/* Functions used by where clause */
1284
class Item_func_isnull :public item::function::Boolean
1270
class Item_func_isnull :public Item_bool_func
1287
1273
int64_t cached_value;
1289
Item_func_isnull(Item *a) :item::function::Boolean(a) {}
1275
Item_func_isnull(Item *a) :Item_bool_func(a) {}
1290
1276
int64_t val_int();
1291
1277
enum Functype functype() const { return ISNULL_FUNC; }
1292
1278
void fix_length_and_dec()
1388
1374
enum { alphabet_size = 256 };
1390
1376
Item *escape_item;
1392
1378
bool escape_used_in_parsing;
1399
1383
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),
1384
:Item_bool_func2(a,b), canDoTurboBM(false), pattern(0), pattern_len(0),
1401
1385
bmGs(0), bmBc(0), escape_item(escape_arg),
1402
escape_used_in_parsing(escape_used), escape(NULL) {}
1386
escape_used_in_parsing(escape_used) {}
1403
1387
int64_t val_int();
1404
1388
enum Functype functype() const { return LIKE_FUNC; }
1405
1389
optimize_type select_optimize() const;
1406
1390
cond_result eq_cmp_result() const { return COND_TRUE; }
1407
1391
const char *func_name() const { return "like"; }
1408
bool fix_fields(Session *session, Item **ref);
1392
bool fix_fields(THD *thd, Item **ref);
1409
1393
void cleanup();
1413
1397
typedef class Item COND;
1415
class Item_cond :public item::function::Boolean
1399
class Item_cond :public Item_bool_func
1418
1402
List<Item> list;
1420
1404
table_map and_tables_cache;
1424
using Item::split_sum_func;
1426
1407
/* Item_cond() is only used to create top level items */
1427
Item_cond(): item::function::Boolean(), abort_on_null(1)
1408
Item_cond(): Item_bool_func(), abort_on_null(1)
1428
1409
{ const_item_cache=0; }
1429
1410
Item_cond(Item *i1,Item *i2)
1430
:item::function::Boolean(), abort_on_null(0)
1411
:Item_bool_func(), abort_on_null(0)
1432
1413
list.push_back(i1);
1433
1414
list.push_back(i2);
1435
Item_cond(Session *session, Item_cond *item);
1416
Item_cond(THD *thd, Item_cond *item);
1436
1417
Item_cond(List<Item> &nlist)
1437
:item::function::Boolean(), list(nlist), abort_on_null(0) {}
1418
:Item_bool_func(), list(nlist), abort_on_null(0) {}
1438
1419
bool add(Item *item) { return list.push_back(item); }
1439
1420
bool add_at_head(Item *item) { return list.push_front(item); }
1440
1421
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);
1422
bool fix_fields(THD *, Item **ref);
1423
void fix_after_pullout(st_select_lex *new_parent, Item **ref);
1444
1425
enum Type type() const { return COND_ITEM; }
1445
1426
List<Item>* argument_list() { return &list; }
1446
1427
table_map used_tables() const;
1447
1428
void update_used_tables();
1448
1429
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,
1430
void split_sum_func(THD *thd, Item **ref_pointer_array, List<Item> &fields);
1431
friend int setup_conds(THD *thd, TableList *tables, TableList *leaves,
1452
1433
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);
1434
void copy_andor_arguments(THD *thd, Item_cond *item);
1435
bool walk(Item_processor processor, bool walk_subquery, uchar *arg);
1436
Item *transform(Item_transformer transformer, uchar *arg);
1456
1437
void traverse_cond(Cond_traverser, void *arg, traverse_order order);
1457
void neg_arguments(Session *session);
1438
void neg_arguments(THD *thd);
1458
1439
enum_field_types field_type() const { return DRIZZLE_TYPE_LONGLONG; }
1459
bool subst_argument_checker(unsigned char **)
1440
bool subst_argument_checker(uchar **arg __attribute__((unused)))
1460
1441
{ return true; }
1461
Item *compile(Item_analyzer analyzer, unsigned char **arg_p,
1462
Item_transformer transformer, unsigned char *arg_t);
1442
Item *compile(Item_analyzer analyzer, uchar **arg_p,
1443
Item_transformer transformer, uchar *arg_t);
1538
1519
object represents f1=f2= ...=fn to the projection of known fields fi1=...=fik.
1541
class Item_equal: public item::function::Boolean
1522
class Item_equal: public Item_bool_func
1524
List<Item_field> fields; /* list of equal field items */
1525
Item *const_item; /* optional constant item equal to fields items */
1526
cmp_item *eval_item;
1544
typedef List<Item_field> fields_t;
1554
fields_t::iterator begin()
1556
return fields.begin();
1530
: Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
1531
{ const_item_cache=0 ;}
1559
1532
Item_equal(Item_field *f1, Item_field *f2);
1560
1533
Item_equal(Item *c, Item_field *f);
1561
1534
Item_equal(Item_equal *item_equal);
1562
1535
inline Item* get_const() { return const_item; }
1563
1536
void add(Item *c);
1564
1537
void add(Item_field *f);
1566
1539
bool contains(Field *field);
1567
1540
Item_field* get_first() { return fields.head(); }
1568
1541
void merge(Item_equal *item);
1569
1542
void update_const();
1570
1543
enum Functype functype() const { return MULT_EQUAL_FUNC; }
1572
1545
const char *func_name() const { return "multiple equal"; }
1573
1546
optimize_type select_optimize() const { return OPTIMIZE_EQUAL; }
1574
1547
void sort(Item_field_cmpfunc cmp, void *arg);
1575
1548
friend class Item_equal_iterator;
1576
1549
void fix_length_and_dec();
1577
bool fix_fields(Session *session, Item **ref);
1550
bool fix_fields(THD *thd, Item **ref);
1578
1551
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);
1552
bool walk(Item_processor processor, bool walk_subquery, uchar *arg);
1553
Item *transform(Item_transformer transformer, uchar *arg);
1581
1554
virtual void print(String *str, enum_query_type query_type);
1582
const CHARSET_INFO *compare_collation()
1555
const CHARSET_INFO *compare_collation()
1583
1556
{ 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
1559
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 */
1562
uint max_members; /* max number of members the current level
1563
list and all lower level lists */
1597
1564
COND_EQUAL *upper_levels; /* multiple equalities of upper and levels */
1598
List<Item_equal> current_level; /* list of multiple equalities of
1565
List<Item_equal> current_level; /* list of multiple equalities of
1599
1566
the current and level */
1602
1569
upper_levels= 0;
1607
class Item_equal_iterator : public List<Item_field>::iterator
1574
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() )
1577
inline Item_equal_iterator(Item_equal &item_equal)
1578
:List_iterator_fast<Item_field> (item_equal.fields)
1613
1580
inline Item_field* operator++(int)
1615
Item_field *item= (*(List<Item_field>::iterator *) this)++;
1582
Item_field *item= (*(List_iterator_fast<Item_field> *) this)++;
1585
inline void rewind(void)
1587
List_iterator_fast<Item_field>::rewind();
1620
1591
class Item_cond_and :public Item_cond
1623
COND_EQUAL cond_equal; /* contains list of Item_equal objects for
1594
COND_EQUAL cond_equal; /* contains list of Item_equal objects for
1624
1595
the current and level and reference
1625
to multiple equalities of upper and levels */
1596
to multiple equalities of upper and levels */
1626
1597
Item_cond_and() :Item_cond() {}
1627
1598
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) {}
1599
Item_cond_and(THD *thd, Item_cond_and *item) :Item_cond(thd, item) {}
1629
1600
Item_cond_and(List<Item> &list_arg): Item_cond(list_arg) {}
1630
1601
enum Functype functype() const { return COND_AND_FUNC; }
1631
1602
int64_t val_int();
1632
1603
const char *func_name() const { return "and"; }
1633
1604
table_map not_null_tables() const
1634
1605
{ return abort_on_null ? not_null_tables_cache: and_tables_cache; }
1635
Item* copy_andor_structure(Session *session)
1606
Item* copy_andor_structure(THD *thd)
1637
1608
Item_cond_and *item;
1638
if ((item= new Item_cond_and(session, this)))
1639
item->copy_andor_arguments(session, this);
1609
if ((item= new Item_cond_and(thd, this)))
1610
item->copy_andor_arguments(thd, this);
1642
Item *neg_transformer(Session *session);
1613
Item *neg_transformer(THD *thd);
1645
1616
inline bool is_cond_and(Item *item)
1657
1628
Item_cond_or() :Item_cond() {}
1658
1629
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) {}
1630
Item_cond_or(THD *thd, Item_cond_or *item) :Item_cond(thd, item) {}
1660
1631
Item_cond_or(List<Item> &list_arg): Item_cond(list_arg) {}
1661
1632
enum Functype functype() const { return COND_OR_FUNC; }
1662
1633
int64_t val_int();
1663
1634
const char *func_name() const { return "or"; }
1664
1635
table_map not_null_tables() const { return and_tables_cache; }
1665
Item* copy_andor_structure(Session *session)
1636
Item* copy_andor_structure(THD *thd)
1667
1638
Item_cond_or *item;
1668
if ((item= new Item_cond_or(session, this)))
1669
item->copy_andor_arguments(session, this);
1639
if ((item= new Item_cond_or(thd, this)))
1640
item->copy_andor_arguments(thd, this);
1672
Item *neg_transformer(Session *session);
1643
Item *neg_transformer(THD *thd);
1675
1646
inline bool is_cond_or(Item *item)