~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item/cmpfunc.h

  • Committer: Monty Taylor
  • Date: 2009-03-02 23:14:32 UTC
  • mto: This revision was merged to the branch mainline in revision 910.
  • Revision ID: mordred@inaugust.com-20090302231432-i35xehp7uzo6hjjw
Updated build system to use new version numbering. Just remember to run ./config/autorun.sh before running make distcheck for release and all should be peachy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
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
22
22
 
23
23
/* compare and test functions */
24
24
 
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>
37
 
 
38
 
namespace drizzled
39
 
{
 
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"
40
34
 
41
35
extern Item_result item_cmp_type(Item_result a,Item_result b);
42
 
 
43
36
class Item_bool_func2;
44
37
class Arg_comparator;
45
38
class Item_sum_hybrid;
46
39
class Item_row;
47
 
class Session;
48
40
 
49
41
typedef int (Arg_comparator::*arg_cmp_func)();
50
42
 
51
43
typedef int (*Item_field_cmpfunc)(Item_field *f1, Item_field *f2, void *arg);
52
44
 
53
 
int64_t get_datetime_value(Session *session, 
54
 
                           Item ***item_arg, 
55
 
                           Item **cache_arg,
56
 
                           Item *warn_item, 
57
 
                           bool *is_null);
58
 
 
59
 
class Arg_comparator: public memory::SqlAlloc
 
45
class Arg_comparator: public Sql_alloc
60
46
{
61
47
  Item **a, **b;
62
48
  arg_cmp_func func;
70
56
  bool is_nulls_eq;                // TRUE <=> compare for the EQUAL_FUNC
71
57
  enum enum_date_cmp_type { CMP_DATE_DFLT= 0, CMP_DATE_WITH_DATE,
72
58
                            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);
 
59
  uint64_t (*get_value_func)(Session *session, Item ***item_arg, Item **cache_arg,
 
60
                              Item *warn_item, bool *is_null);
75
61
public:
76
62
  DTCollation cmp_collation;
77
63
 
78
 
  Arg_comparator();
79
 
 
80
 
  Arg_comparator(Item **a1, Item **a2);
 
64
  Arg_comparator(): session(0), a_cache(0), b_cache(0) {};
 
65
  Arg_comparator(Item **a1, Item **a2): a(a1), b(a2), session(0),
 
66
    a_cache(0), b_cache(0) {};
81
67
 
82
68
  int set_compare_func(Item_bool_func2 *owner, Item_result type);
83
69
  inline int set_compare_func(Item_bool_func2 *owner_arg)
119
105
  int compare_datetime();        // compare args[0] & args[1] as DATETIMEs
120
106
 
121
107
  static enum enum_date_cmp_type can_compare_as_dates(Item *a, Item *b,
122
 
                                                      int64_t *const_val_arg);
 
108
                                                      uint64_t *const_val_arg);
123
109
 
124
110
  void set_datetime_cmp_func(Item **a1, Item **b1);
125
111
  static arg_cmp_func comparator_matrix [5][2];
127
113
  friend class Item_func;
128
114
};
129
115
 
 
116
class Item_bool_func :public Item_int_func
 
117
{
 
118
public:
 
119
  Item_bool_func() :Item_int_func() {}
 
120
  Item_bool_func(Item *a) :Item_int_func(a) {}
 
121
  Item_bool_func(Item *a,Item *b) :Item_int_func(a,b) {}
 
122
  Item_bool_func(Session *session, Item_bool_func *item) :Item_int_func(session, item) {}
 
123
  bool is_bool_func() { return 1; }
 
124
  void fix_length_and_dec() { decimals=0; max_length=1; }
 
125
  uint32_t decimal_precision() const { return 1; }
 
126
};
 
127
 
130
128
 
131
129
/**
132
130
  Abstract Item class, to represent <code>X IS [NOT] (TRUE | FALSE)</code>
133
131
  boolean predicates.
134
132
*/
135
133
 
136
 
class Item_func_truth : public item::function::Boolean
 
134
class Item_func_truth : public Item_bool_func
137
135
{
138
136
public:
139
137
  virtual bool val_bool();
143
141
 
144
142
protected:
145
143
  Item_func_truth(Item *a, bool a_value, bool a_affirmative)
146
 
  : item::function::Boolean(a), value(a_value), affirmative(a_affirmative)
 
144
  : Item_bool_func(a), value(a_value), affirmative(a_affirmative)
147
145
  {}
148
146
 
149
147
  ~Item_func_truth()
232
230
    placed into a separate class called 'Item_in_optimizer'.
233
231
*/
234
232
 
235
 
class Item_in_optimizer: public item::function::Boolean
 
233
class Item_in_optimizer: public Item_bool_func
236
234
{
237
235
protected:
238
236
  Item_cache *cache;
246
244
  bool result_for_null_param;
247
245
public:
248
246
  Item_in_optimizer(Item *a, Item_in_subselect *b):
249
 
    item::function::Boolean(a, reinterpret_cast<Item *>(b)), cache(0),
 
247
    Item_bool_func(a, reinterpret_cast<Item *>(b)), cache(0),
250
248
    save_cache(0), result_for_null_param(UNKNOWN)
251
249
  { with_subselect= true; }
252
250
  bool fix_fields(Session *, Item **);
378
376
  { return true; }
379
377
};
380
378
 
381
 
class Item_func_not :public item::function::Boolean
 
379
class Item_func_not :public Item_bool_func
382
380
{
383
381
public:
384
 
  Item_func_not(Item *a) :item::function::Boolean(a) {}
 
382
  Item_func_not(Item *a) :Item_bool_func(a) {}
385
383
  int64_t val_int();
386
384
  enum Functype functype() const { return NOT_FUNC; }
387
385
  const char *func_name() const { return "not"; }
420
418
   - To wrap condition that is pushed down into subquery
421
419
*/
422
420
 
423
 
class Item_func_trig_cond: public item::function::Boolean
 
421
class Item_func_trig_cond: public Item_bool_func
424
422
{
425
423
  bool *trig_var;
426
424
public:
427
 
  Item_func_trig_cond(Item *a, bool *f) : item::function::Boolean(a) { trig_var= f; }
 
425
  Item_func_trig_cond(Item *a, bool *f) : Item_bool_func(a) { trig_var= f; }
428
426
  int64_t val_int() { return *trig_var ? args[0]->val_int() : 1; }
429
427
  enum Functype functype() const { return TRIG_COND_FUNC; };
430
428
  const char *func_name() const { return "trigcond"; };
641
639
{
642
640
  Item_result type;
643
641
  double dbl;
644
 
  type::Decimal dec;
 
642
  my_decimal dec;
645
643
};
646
644
 
647
645
class Item_func_interval :public Item_int_func
672
670
  double real_op();
673
671
  int64_t int_op();
674
672
  String *str_op(String *);
675
 
  type::Decimal *decimal_op(type::Decimal *);
 
673
  my_decimal *decimal_op(my_decimal *);
676
674
  void fix_length_and_dec();
677
675
  void find_num_type() {}
678
676
  enum Item_result result_type () const { return hybrid_type; }
691
689
  double real_op();
692
690
  int64_t int_op();
693
691
  String *str_op(String *str);
694
 
  type::Decimal *decimal_op(type::Decimal *);
 
692
  my_decimal *decimal_op(my_decimal *);
695
693
  enum_field_types field_type() const;
696
694
  void fix_length_and_dec();
697
695
  const char *func_name() const { return "ifnull"; }
715
713
  double val_real();
716
714
  int64_t val_int();
717
715
  String *val_str(String *str);
718
 
  type::Decimal *val_decimal(type::Decimal *);
 
716
  my_decimal *val_decimal(my_decimal *);
719
717
  enum Item_result result_type () const { return cached_result_type; }
720
718
  enum_field_types field_type() const { return cached_field_type; }
721
719
  bool fix_fields(Session *, Item **);
735
733
  double val_real();
736
734
  int64_t val_int();
737
735
  String *val_str(String *str);
738
 
  type::Decimal *val_decimal(type::Decimal *);
 
736
  my_decimal *val_decimal(my_decimal *);
739
737
  enum Item_result result_type () const { return cached_result_type; }
740
738
  void fix_length_and_dec();
741
739
  uint32_t decimal_precision() const { return args[0]->decimal_precision(); }
756
754
 
757
755
/* A vector of values of some type  */
758
756
 
759
 
class in_vector :public memory::SqlAlloc
 
757
class in_vector :public Sql_alloc
760
758
{
761
759
public:
762
760
  char *base;
768
766
  in_vector() {}
769
767
  in_vector(uint32_t elements,uint32_t element_length,qsort2_cmp cmp_func,
770
768
            const CHARSET_INFO * const cmp_coll)
771
 
    :base((char*) memory::sql_calloc(elements*element_length)),
 
769
    :base((char*) sql_calloc(elements*element_length)),
772
770
     size(element_length), compare(cmp_func), collation(cmp_coll),
773
771
     count(elements), used_count(elements) {}
774
772
  virtual ~in_vector() {}
775
773
  virtual void set(uint32_t pos,Item *item)=0;
776
774
  virtual unsigned char *get_value(Item *item)=0;
777
 
  void sort();
 
775
  void sort()
 
776
  {
 
777
    my_qsort2(base,used_count,size,compare, (void *) collation);
 
778
  }
778
779
  int find(Item *item);
779
780
 
780
781
  /*
880
881
  /* Cache for the left item. */
881
882
  Item *lval_cache;
882
883
 
883
 
  in_datetime(Item *warn_item_arg, uint32_t elements);
884
 
 
 
884
  in_datetime(Item *warn_item_arg, uint32_t elements)
 
885
    :in_int64_t(elements), session(current_session), warn_item(warn_item_arg),
 
886
     lval_cache(0) {};
885
887
  void set(uint32_t pos,Item *item);
886
888
  unsigned char *get_value(Item *item);
887
889
  friend int cmp_int64_t(void *cmp_arg, packed_int64_t *a,packed_int64_t *b);
909
911
 
910
912
class in_decimal :public in_vector
911
913
{
912
 
  type::Decimal val;
 
914
  my_decimal val;
913
915
public:
914
916
  in_decimal(uint32_t elements);
915
917
  void set(uint32_t pos, Item *item);
920
922
  }
921
923
  void value_to_item(uint32_t pos, Item *item)
922
924
  {
923
 
    type::Decimal *dec= ((type::Decimal *)base) + pos;
 
925
    my_decimal *dec= ((my_decimal *)base) + pos;
924
926
    Item_decimal *item_dec= (Item_decimal*)item;
925
927
    item_dec->set_decimal_value(dec);
926
928
  }
933
935
** Classes for easy comparing of non const items
934
936
*/
935
937
 
936
 
class cmp_item :public memory::SqlAlloc
 
938
class cmp_item :public Sql_alloc
937
939
{
938
940
public:
939
941
  const CHARSET_INFO *cmp_charset;
940
 
 
941
 
  cmp_item()
942
 
  {
943
 
    cmp_charset= &my_charset_bin;
944
 
  }
945
 
 
 
942
  cmp_item() { cmp_charset= &my_charset_bin; }
946
943
  virtual ~cmp_item() {}
947
944
  virtual void store_value(Item *item)= 0;
948
945
  virtual int cmp(Item *item)= 0;
1033
1030
*/
1034
1031
class cmp_item_datetime :public cmp_item
1035
1032
{
1036
 
  int64_t value;
1037
 
 
 
1033
  uint64_t value;
1038
1034
public:
1039
1035
  Session *session;
1040
1036
  /* Item used for issuing warnings. */
1042
1038
  /* Cache for the left item. */
1043
1039
  Item *lval_cache;
1044
1040
 
1045
 
  cmp_item_datetime(Item *warn_item_arg);
1046
 
 
 
1041
  cmp_item_datetime(Item *warn_item_arg)
 
1042
    :session(current_session), warn_item(warn_item_arg), lval_cache(0) {}
1047
1043
  void store_value(Item *item);
1048
1044
  int cmp(Item *arg);
1049
1045
  int compare(cmp_item *ci);
1074
1070
 
1075
1071
class cmp_item_decimal :public cmp_item
1076
1072
{
1077
 
  type::Decimal value;
 
1073
  my_decimal value;
1078
1074
public:
1079
1075
  cmp_item_decimal() {}                       /* Remove gcc warning */
1080
1076
  void store_value(Item *item);
1145
1141
  Item_result cmp_type;
1146
1142
  DTCollation cmp_collation;
1147
1143
  enum_field_types cached_field_type;
1148
 
  cmp_item *cmp_items[DECIMAL_RESULT+1]; /* For all result types */
 
1144
  cmp_item *cmp_items[5]; /* For all result types */
1149
1145
  cmp_item *case_item;
1150
1146
public:
1151
1147
  Item_func_case(List<Item> &list, Item *first_expr_arg, Item *else_expr_arg)
1169
1165
  double val_real();
1170
1166
  int64_t val_int();
1171
1167
  String *val_str(String *);
1172
 
  type::Decimal *val_decimal(type::Decimal *);
 
1168
  my_decimal *val_decimal(my_decimal *);
1173
1169
  bool fix_fields(Session *session, Item **ref);
1174
1170
  void fix_length_and_dec();
1175
1171
  uint32_t decimal_precision() const;
1230
1226
  uint32_t decimal_precision() const { return 1; }
1231
1227
  void cleanup()
1232
1228
  {
 
1229
    uint32_t i;
1233
1230
    Item_int_func::cleanup();
1234
1231
    delete array;
1235
1232
    array= 0;
1236
 
    for (int i= STRING_RESULT; i <= DECIMAL_RESULT; i++)
 
1233
    for (i= 0; i <= (uint32_t)DECIMAL_RESULT + 1; i++)
1237
1234
    {
1238
1235
      delete cmp_items[i];
1239
1236
      cmp_items[i]= 0;
1281
1278
 
1282
1279
/* Functions used by where clause */
1283
1280
 
1284
 
class Item_func_isnull :public item::function::Boolean
 
1281
class Item_func_isnull :public Item_bool_func
1285
1282
{
1286
1283
protected:
1287
1284
  int64_t cached_value;
1288
1285
public:
1289
 
  Item_func_isnull(Item *a) :item::function::Boolean(a) {}
 
1286
  Item_func_isnull(Item *a) :Item_bool_func(a) {}
1290
1287
  int64_t val_int();
1291
1288
  enum Functype functype() const { return ISNULL_FUNC; }
1292
1289
  void fix_length_and_dec()
1348
1345
};
1349
1346
 
1350
1347
 
1351
 
class Item_func_isnotnull :public item::function::Boolean
 
1348
class Item_func_isnotnull :public Item_bool_func
1352
1349
{
1353
1350
  bool abort_on_null;
1354
1351
public:
1355
 
  Item_func_isnotnull(Item *a) :item::function::Boolean(a), abort_on_null(0) {}
 
1352
  Item_func_isnotnull(Item *a) :Item_bool_func(a), abort_on_null(0) {}
1356
1353
  int64_t val_int();
1357
1354
  enum Functype functype() const { return ISNOTNULL_FUNC; }
1358
1355
  void fix_length_and_dec()
1391
1388
 
1392
1389
  bool escape_used_in_parsing;
1393
1390
 
1394
 
 
1395
1391
public:
1396
 
 
1397
 
  char *escape;
 
1392
  int escape;
1398
1393
 
1399
1394
  Item_func_like(Item *a,Item *b, Item *escape_arg, bool escape_used)
1400
1395
    :Item_bool_func2(a,b), canDoTurboBM(false), pattern(0), pattern_len(0),
1401
1396
     bmGs(0), bmBc(0), escape_item(escape_arg),
1402
 
     escape_used_in_parsing(escape_used), escape(NULL) {}
 
1397
     escape_used_in_parsing(escape_used) {}
1403
1398
  int64_t val_int();
1404
1399
  enum Functype functype() const { return LIKE_FUNC; }
1405
1400
  optimize_type select_optimize() const;
1412
1407
 
1413
1408
typedef class Item COND;
1414
1409
 
1415
 
class Item_cond :public item::function::Boolean
 
1410
class Item_cond :public Item_bool_func
1416
1411
{
1417
1412
protected:
1418
1413
  List<Item> list;
1424
1419
  using Item::split_sum_func;
1425
1420
 
1426
1421
  /* Item_cond() is only used to create top level items */
1427
 
  Item_cond(): item::function::Boolean(), abort_on_null(1)
 
1422
  Item_cond(): Item_bool_func(), abort_on_null(1)
1428
1423
  { const_item_cache=0; }
1429
1424
  Item_cond(Item *i1,Item *i2)
1430
 
    :item::function::Boolean(), abort_on_null(0)
 
1425
    :Item_bool_func(), abort_on_null(0)
1431
1426
  {
1432
1427
    list.push_back(i1);
1433
1428
    list.push_back(i2);
1434
1429
  }
1435
1430
  Item_cond(Session *session, Item_cond *item);
1436
1431
  Item_cond(List<Item> &nlist)
1437
 
    :item::function::Boolean(), list(nlist), abort_on_null(0) {}
 
1432
    :Item_bool_func(), list(nlist), abort_on_null(0) {}
1438
1433
  bool add(Item *item) { return list.push_back(item); }
1439
1434
  bool add_at_head(Item *item) { return list.push_front(item); }
1440
1435
  void add_at_head(List<Item> *nlist) { list.prepand(nlist); }
1538
1533
  object represents f1=f2= ...=fn to the projection of known fields fi1=...=fik.
1539
1534
*/
1540
1535
 
1541
 
class Item_equal: public item::function::Boolean
 
1536
class Item_equal: public Item_bool_func
1542
1537
{
 
1538
  List<Item_field> fields; /* list of equal field items                    */
 
1539
  Item *const_item;        /* optional constant item equal to fields items */
 
1540
  cmp_item *eval_item;
 
1541
  bool cond_false;
1543
1542
public:
1544
 
  typedef List<Item_field> fields_t;
1545
 
 
1546
 
  Item_equal() :
1547
 
    const_item(0),
1548
 
    eval_item(0),
1549
 
    cond_false(0)
1550
 
  {
1551
 
    const_item_cache=0;
1552
 
  }
1553
 
 
1554
 
  fields_t::iterator begin()
1555
 
  {
1556
 
    return fields.begin();
1557
 
  }
1558
 
 
 
1543
  inline Item_equal()
 
1544
    : Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
 
1545
  { const_item_cache=0 ;}
1559
1546
  Item_equal(Item_field *f1, Item_field *f2);
1560
1547
  Item_equal(Item *c, Item_field *f);
1561
1548
  Item_equal(Item_equal *item_equal);
1581
1568
  virtual void print(String *str, enum_query_type query_type);
1582
1569
  const CHARSET_INFO *compare_collation()
1583
1570
  { return fields.head()->collation.collation; }
1584
 
private:
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;
1588
 
  bool cond_false;
1589
 
 
1590
1571
};
1591
1572
 
1592
 
class COND_EQUAL: public memory::SqlAlloc
 
1573
class COND_EQUAL: public Sql_alloc
1593
1574
{
1594
1575
public:
1595
1576
  uint32_t max_members;               /* max number of members the current level
1604
1585
};
1605
1586
 
1606
1587
 
1607
 
class Item_equal_iterator : public List<Item_field>::iterator
 
1588
class Item_equal_iterator : public List_iterator_fast<Item_field>
1608
1589
{
1609
1590
public:
1610
1591
  inline Item_equal_iterator(Item_equal &item_equal)
1611
 
    :List<Item_field>::iterator (item_equal.fields.begin() )
 
1592
    :List_iterator_fast<Item_field> (item_equal.fields)
1612
1593
  {}
1613
1594
  inline Item_field* operator++(int)
1614
1595
  {
1615
 
    Item_field *item= (*(List<Item_field>::iterator *) this)++;
 
1596
    Item_field *item= (*(List_iterator_fast<Item_field> *) this)++;
1616
1597
    return  item;
1617
1598
  }
 
1599
  inline void rewind(void)
 
1600
  {
 
1601
    List_iterator_fast<Item_field>::rewind();
 
1602
  }
1618
1603
};
1619
1604
 
1620
1605
class Item_cond_and :public Item_cond
1699
1684
  void top_level_item() {}
1700
1685
};
1701
1686
 
1702
 
enum_field_types agg_field_type(Item **items, uint32_t nitems);
1703
 
 
1704
1687
 
1705
1688
/* Some useful inline functions */
1706
1689
 
1713
1696
 
1714
1697
Item *and_expressions(Item *a, Item *b, Item **org_item);
1715
1698
 
1716
 
} /* namespace drizzled */
1717
 
 
1718
1699
#endif /* DRIZZLED_ITEM_CMPFUNC_H */