~drizzle-trunk/drizzle/development

390.1.2 by Monty Taylor
Fixed copyright headers in drizzled/
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 Sun Microsystems
5
 *
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.
9
 *
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.
14
 *
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
18
 */
1 by brian
clean slate
19
575.1.6 by Monty Taylor
Cleaned up some headers for PCH.
20
#ifndef DRIZZLED_ITEM_CMPFUNC_H
21
#define DRIZZLED_ITEM_CMPFUNC_H
1 by brian
clean slate
22
23
/* compare and test functions */
24
873.1.8 by Jay Pipes
Fixes Arg_comparator::can_compare_as_dates to never, ever allow bad
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"
642.1.26 by Lee
merge with latest from the trunk
34
1 by brian
clean slate
35
extern Item_result item_cmp_type(Item_result a,Item_result b);
36
class Item_bool_func2;
37
class Arg_comparator;
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
38
class Item_sum_hybrid;
39
class Item_row;
1 by brian
clean slate
40
41
typedef int (Arg_comparator::*arg_cmp_func)();
42
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
43
typedef int (*Item_field_cmpfunc)(Item_field *f1, Item_field *f2, void *arg);
1 by brian
clean slate
44
45
class Arg_comparator: public Sql_alloc
46
{
47
  Item **a, **b;
48
  arg_cmp_func func;
49
  Item_bool_func2 *owner;
50
  Arg_comparator *comparators;   // used only for compare_row()
51
  double precision;
52
  /* Fields used in DATE/DATETIME comparison. */
520.1.22 by Brian Aker
Second pass of thd cleanup
53
  Session *session;
1 by brian
clean slate
54
  enum_field_types a_type, b_type; // Types of a and b items
55
  Item *a_cache, *b_cache;         // Cached values of a and b items
56
  bool is_nulls_eq;                // TRUE <=> compare for the EQUAL_FUNC
57
  enum enum_date_cmp_type { CMP_DATE_DFLT= 0, CMP_DATE_WITH_DATE,
58
                            CMP_DATE_WITH_STR, CMP_STR_WITH_DATE };
520.1.22 by Brian Aker
Second pass of thd cleanup
59
  uint64_t (*get_value_func)(Session *session, Item ***item_arg, Item **cache_arg,
1 by brian
clean slate
60
                              Item *warn_item, bool *is_null);
61
public:
62
  DTCollation cmp_collation;
63
520.1.22 by Brian Aker
Second pass of thd cleanup
64
  Arg_comparator(): session(0), a_cache(0), b_cache(0) {};
65
  Arg_comparator(Item **a1, Item **a2): a(a1), b(a2), session(0),
1 by brian
clean slate
66
    a_cache(0), b_cache(0) {};
67
68
  int set_compare_func(Item_bool_func2 *owner, Item_result type);
69
  inline int set_compare_func(Item_bool_func2 *owner_arg)
70
  {
71
    return set_compare_func(owner_arg, item_cmp_type((*a)->result_type(),
72
                                                     (*b)->result_type()));
73
  }
74
  int set_cmp_func(Item_bool_func2 *owner_arg,
75
			  Item **a1, Item **a2,
76
			  Item_result type);
77
78
  inline int set_cmp_func(Item_bool_func2 *owner_arg,
79
			  Item **a1, Item **a2)
80
  {
81
    return set_cmp_func(owner_arg, a1, a2,
82
                        item_cmp_type((*a1)->result_type(),
83
                                      (*a2)->result_type()));
84
  }
85
  inline int compare() { return (this->*func)(); }
86
87
  int compare_string();		 // compare args[0] & args[1]
88
  int compare_binary_string();	 // compare args[0] & args[1]
89
  int compare_real();            // compare args[0] & args[1]
90
  int compare_decimal();         // compare args[0] & args[1]
91
  int compare_int_signed();      // compare args[0] & args[1]
92
  int compare_int_signed_unsigned();
93
  int compare_int_unsigned_signed();
94
  int compare_int_unsigned();
95
  int compare_row();             // compare args[0] & args[1]
96
  int compare_e_string();	 // compare args[0] & args[1]
97
  int compare_e_binary_string(); // compare args[0] & args[1]
98
  int compare_e_real();          // compare args[0] & args[1]
99
  int compare_e_decimal();       // compare args[0] & args[1]
100
  int compare_e_int();           // compare args[0] & args[1]
101
  int compare_e_int_diff_signedness();
102
  int compare_e_row();           // compare args[0] & args[1]
103
  int compare_real_fixed();
104
  int compare_e_real_fixed();
105
  int compare_datetime();        // compare args[0] & args[1] as DATETIMEs
106
107
  static enum enum_date_cmp_type can_compare_as_dates(Item *a, Item *b,
151 by Brian Aker
Ulonglong to uint64_t
108
                                                      uint64_t *const_val_arg);
1 by brian
clean slate
109
110
  void set_datetime_cmp_func(Item **a1, Item **b1);
111
  static arg_cmp_func comparator_matrix [5][2];
112
113
  friend class Item_func;
114
};
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) {}
520.1.22 by Brian Aker
Second pass of thd cleanup
122
  Item_bool_func(Session *session, Item_bool_func *item) :Item_int_func(session, item) {}
1 by brian
clean slate
123
  bool is_bool_func() { return 1; }
124
  void fix_length_and_dec() { decimals=0; max_length=1; }
482 by Brian Aker
Remove uint.
125
  uint32_t decimal_precision() const { return 1; }
1 by brian
clean slate
126
};
127
128
129
/**
130
  Abstract Item class, to represent <code>X IS [NOT] (TRUE | FALSE)</code>
131
  boolean predicates.
132
*/
133
134
class Item_func_truth : public Item_bool_func
135
{
136
public:
137
  virtual bool val_bool();
152 by Brian Aker
longlong replacement
138
  virtual int64_t val_int();
1 by brian
clean slate
139
  virtual void fix_length_and_dec();
140
  virtual void print(String *str, enum_query_type query_type);
141
142
protected:
143
  Item_func_truth(Item *a, bool a_value, bool a_affirmative)
144
  : Item_bool_func(a), value(a_value), affirmative(a_affirmative)
145
  {}
146
147
  ~Item_func_truth()
148
  {}
149
private:
150
  /**
151
    True for <code>X IS [NOT] TRUE</code>,
152
    false for <code>X IS [NOT] FALSE</code> predicates.
153
  */
154
  const bool value;
155
  /**
156
    True for <code>X IS Y</code>, false for <code>X IS NOT Y</code> predicates.
157
  */
158
  const bool affirmative;
159
};
160
161
162
/**
163
  This Item represents a <code>X IS TRUE</code> boolean predicate.
164
*/
165
166
class Item_func_istrue : public Item_func_truth
167
{
168
public:
169
  Item_func_istrue(Item *a) : Item_func_truth(a, true, true) {}
170
  ~Item_func_istrue() {}
171
  virtual const char* func_name() const { return "istrue"; }
172
};
173
174
175
/**
176
  This Item represents a <code>X IS NOT TRUE</code> boolean predicate.
177
*/
178
179
class Item_func_isnottrue : public Item_func_truth
180
{
181
public:
182
  Item_func_isnottrue(Item *a) : Item_func_truth(a, true, false) {}
183
  ~Item_func_isnottrue() {}
184
  virtual const char* func_name() const { return "isnottrue"; }
185
};
186
187
188
/**
189
  This Item represents a <code>X IS FALSE</code> boolean predicate.
190
*/
191
192
class Item_func_isfalse : public Item_func_truth
193
{
194
public:
195
  Item_func_isfalse(Item *a) : Item_func_truth(a, false, true) {}
196
  ~Item_func_isfalse() {}
197
  virtual const char* func_name() const { return "isfalse"; }
198
};
199
200
201
/**
202
  This Item represents a <code>X IS NOT FALSE</code> boolean predicate.
203
*/
204
205
class Item_func_isnotfalse : public Item_func_truth
206
{
207
public:
208
  Item_func_isnotfalse(Item *a) : Item_func_truth(a, false, false) {}
209
  ~Item_func_isnotfalse() {}
210
  virtual const char* func_name() const { return "isnotfalse"; }
211
};
212
213
214
class Item_cache;
584.2.8 by Stewart Smith
(mostly) revert previous patch that tried to get rid of the UNKNOWN define in item_cmpfunc.h
215
#define UNKNOWN ((bool)-1)
216
1 by brian
clean slate
217
218
/*
219
  Item_in_optimizer(left_expr, Item_in_subselect(...))
220
221
  Item_in_optimizer is used to wrap an instance of Item_in_subselect. This
222
  class does the following:
223
   - Evaluate the left expression and store it in Item_cache_* object (to
224
     avoid re-evaluating it many times during subquery execution)
225
   - Shortcut the evaluation of "NULL IN (...)" to NULL in the cases where we
226
     don't care if the result is NULL or FALSE.
227
228
  NOTE
229
    It is not quite clear why the above listed functionality should be
230
    placed into a separate class called 'Item_in_optimizer'.
231
*/
232
233
class Item_in_optimizer: public Item_bool_func
234
{
235
protected:
236
  Item_cache *cache;
237
  bool save_cache;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
238
  /*
1 by brian
clean slate
239
    Stores the value of "NULL IN (SELECT ...)" for uncorrelated subqueries:
240
      UNKNOWN - "NULL in (SELECT ...)" has not yet been evaluated
241
      FALSE   - result is FALSE
242
      TRUE    - result is NULL
243
  */
275 by Brian Aker
Full removal of my_bool from central server.
244
  bool result_for_null_param;
1 by brian
clean slate
245
public:
246
  Item_in_optimizer(Item *a, Item_in_subselect *b):
451 by Monty Taylor
Removed my_reinterpret_cast. It's not GNU specific.
247
    Item_bool_func(a, reinterpret_cast<Item *>(b)), cache(0),
584.2.8 by Stewart Smith
(mostly) revert previous patch that tried to get rid of the UNKNOWN define in item_cmpfunc.h
248
    save_cache(0), result_for_null_param(UNKNOWN)
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
249
  { with_subselect= true; }
520.1.21 by Brian Aker
THD -> Session rename
250
  bool fix_fields(Session *, Item **);
520.1.22 by Brian Aker
Second pass of thd cleanup
251
  bool fix_left(Session *session, Item **ref);
1 by brian
clean slate
252
  bool is_null();
152 by Brian Aker
longlong replacement
253
  int64_t val_int();
1 by brian
clean slate
254
  void cleanup();
255
  const char *func_name() const { return "<in_optimizer>"; }
256
  Item_cache **get_cache() { return &cache; }
257
  void keep_top_level_cache();
481 by Brian Aker
Remove all of uchar.
258
  Item *transform(Item_transformer transformer, unsigned char *arg);
1 by brian
clean slate
259
};
260
261
class Eq_creator :public Comp_creator
262
{
263
public:
264
  Eq_creator() {}                             /* Remove gcc warning */
265
  virtual ~Eq_creator() {}                    /* Remove gcc warning */
266
  virtual Item_bool_func2* create(Item *a, Item *b) const;
267
  virtual const char* symbol(bool invert) const { return invert? "<>" : "="; }
268
  virtual bool eqne_op() const { return 1; }
269
  virtual bool l_op() const { return 0; }
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
270
  static const Eq_creator *instance();
1 by brian
clean slate
271
};
272
273
class Ne_creator :public Comp_creator
274
{
275
public:
276
  Ne_creator() {}                             /* Remove gcc warning */
277
  virtual ~Ne_creator() {}                    /* Remove gcc warning */
278
  virtual Item_bool_func2* create(Item *a, Item *b) const;
279
  virtual const char* symbol(bool invert) const { return invert? "=" : "<>"; }
280
  virtual bool eqne_op() const { return 1; }
281
  virtual bool l_op() const { return 0; }
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
282
  static const Ne_creator *instance();
1 by brian
clean slate
283
};
284
285
class Gt_creator :public Comp_creator
286
{
287
public:
288
  Gt_creator() {}                             /* Remove gcc warning */
289
  virtual ~Gt_creator() {}                    /* Remove gcc warning */
290
  virtual Item_bool_func2* create(Item *a, Item *b) const;
291
  virtual const char* symbol(bool invert) const { return invert? "<=" : ">"; }
292
  virtual bool eqne_op() const { return 0; }
293
  virtual bool l_op() const { return 0; }
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
294
  static const Gt_creator *instance();
1 by brian
clean slate
295
};
296
297
class Lt_creator :public Comp_creator
298
{
299
public:
300
  Lt_creator() {}                             /* Remove gcc warning */
301
  virtual ~Lt_creator() {}                    /* Remove gcc warning */
302
  virtual Item_bool_func2* create(Item *a, Item *b) const;
303
  virtual const char* symbol(bool invert) const { return invert? ">=" : "<"; }
304
  virtual bool eqne_op() const { return 0; }
305
  virtual bool l_op() const { return 1; }
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
306
  static const Lt_creator *instance();
1 by brian
clean slate
307
};
308
309
class Ge_creator :public Comp_creator
310
{
311
public:
312
  Ge_creator() {}                             /* Remove gcc warning */
313
  virtual ~Ge_creator() {}                    /* Remove gcc warning */
314
  virtual Item_bool_func2* create(Item *a, Item *b) const;
315
  virtual const char* symbol(bool invert) const { return invert? "<" : ">="; }
316
  virtual bool eqne_op() const { return 0; }
317
  virtual bool l_op() const { return 0; }
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
318
  static const Ge_creator *instance();
1 by brian
clean slate
319
};
320
321
class Le_creator :public Comp_creator
322
{
323
public:
324
  Le_creator() {}                             /* Remove gcc warning */
325
  virtual ~Le_creator() {}                    /* Remove gcc warning */
326
  virtual Item_bool_func2* create(Item *a, Item *b) const;
327
  virtual const char* symbol(bool invert) const { return invert? ">" : "<="; }
328
  virtual bool eqne_op() const { return 0; }
329
  virtual bool l_op() const { return 1; }
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
330
  static const Le_creator *instance();
1 by brian
clean slate
331
};
332
333
class Item_bool_func2 :public Item_int_func
334
{						/* Bool with 2 string args */
335
protected:
336
  Arg_comparator cmp;
337
  String tmp_value1,tmp_value2;
338
  bool abort_on_null;
339
340
public:
341
  Item_bool_func2(Item *a,Item *b)
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
342
    :Item_int_func(a,b), cmp(tmp_arg, tmp_arg+1), abort_on_null(false) {}
1 by brian
clean slate
343
  void fix_length_and_dec();
344
  void set_cmp_func()
345
  {
346
    cmp.set_cmp_func(this, tmp_arg, tmp_arg+1);
347
  }
348
  optimize_type select_optimize() const { return OPTIMIZE_OP; }
349
  virtual enum Functype rev_functype() const { return UNKNOWN_FUNC; }
350
  bool have_rev_func() const { return rev_functype() != UNKNOWN_FUNC; }
351
352
  virtual inline void print(String *str, enum_query_type query_type)
353
  {
354
    Item_func::print_op(str, query_type);
355
  }
356
357
  bool is_null() { return test(args[0]->is_null() || args[1]->is_null()); }
358
  bool is_bool_func() { return 1; }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
359
  const CHARSET_INFO *compare_collation() { return cmp.cmp_collation.collation; }
482 by Brian Aker
Remove uint.
360
  uint32_t decimal_precision() const { return 1; }
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
361
  void top_level_item() { abort_on_null= true; }
1 by brian
clean slate
362
363
  friend class  Arg_comparator;
364
};
365
366
class Item_bool_rowready_func2 :public Item_bool_func2
367
{
368
public:
369
  Item_bool_rowready_func2(Item *a, Item *b) :Item_bool_func2(a, b)
370
  {
371
    allowed_arg_cols= 0;  // Fetch this value from first argument
372
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
373
  Item *neg_transformer(Session *session);
1 by brian
clean slate
374
  virtual Item *negated_item();
646 by Brian Aker
Next pass through attribute.
375
  bool subst_argument_checker(unsigned char **)
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
376
  { return true; }
1 by brian
clean slate
377
};
378
379
class Item_func_not :public Item_bool_func
380
{
381
public:
382
  Item_func_not(Item *a) :Item_bool_func(a) {}
152 by Brian Aker
longlong replacement
383
  int64_t val_int();
1 by brian
clean slate
384
  enum Functype functype() const { return NOT_FUNC; }
385
  const char *func_name() const { return "not"; }
520.1.22 by Brian Aker
Second pass of thd cleanup
386
  Item *neg_transformer(Session *session);
1 by brian
clean slate
387
  virtual void print(String *str, enum_query_type query_type);
388
};
389
390
class Item_maxmin_subselect;
391
392
/*
393
  trigcond<param>(arg) ::= param? arg : TRUE
394
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
395
  The class Item_func_trig_cond is used for guarded predicates
1 by brian
clean slate
396
  which are employed only for internal purposes.
397
  A guarded predicate is an object consisting of an a regular or
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
398
  a guarded predicate P and a pointer to a boolean guard variable g.
1 by brian
clean slate
399
  A guarded predicate P/g is evaluated to true if the value of the
400
  guard g is false, otherwise it is evaluated to the same value that
401
  the predicate P: val(P/g)= g ? val(P):true.
402
  Guarded predicates allow us to include predicates into a conjunction
403
  conditionally. Currently they are utilized for pushed down predicates
404
  in queries with outer join operations.
405
406
  In the future, probably, it makes sense to extend this class to
407
  the objects consisting of three elements: a predicate P, a pointer
408
  to a variable g and a firing value s with following evaluation
409
  rule: val(P/g,s)= g==s? val(P) : true. It will allow us to build only
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
410
  one item for the objects of the form P/g1/g2...
1 by brian
clean slate
411
412
  Objects of this class are built only for query execution after
413
  the execution plan has been already selected. That's why this
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
414
  class needs only val_int out of generic methods.
415
1 by brian
clean slate
416
  Current uses of Item_func_trig_cond objects:
417
   - To wrap selection conditions when executing outer joins
418
   - To wrap condition that is pushed down into subquery
419
*/
420
421
class Item_func_trig_cond: public Item_bool_func
422
{
423
  bool *trig_var;
424
public:
425
  Item_func_trig_cond(Item *a, bool *f) : Item_bool_func(a) { trig_var= f; }
152 by Brian Aker
longlong replacement
426
  int64_t val_int() { return *trig_var ? args[0]->val_int() : 1; }
1 by brian
clean slate
427
  enum Functype functype() const { return TRIG_COND_FUNC; };
428
  const char *func_name() const { return "trigcond"; };
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
429
  bool const_item() const { return false; }
1 by brian
clean slate
430
  bool *get_trig_var() { return trig_var; }
431
  /* The following is needed for ICP: */
432
  table_map used_tables() const { return args[0]->used_tables(); }
433
};
434
435
class Item_func_not_all :public Item_func_not
436
{
437
  /* allow to check presence of values in max/min optimization */
438
  Item_sum_hybrid *test_sum_item;
439
  Item_maxmin_subselect *test_sub_item;
440
441
  bool abort_on_null;
442
public:
443
  bool show;
444
445
  Item_func_not_all(Item *a)
446
    :Item_func_not(a), test_sum_item(0), test_sub_item(0), abort_on_null(0),
447
     show(0)
448
    {}
449
  virtual void top_level_item() { abort_on_null= 1; }
450
  bool top_level() { return abort_on_null; }
152 by Brian Aker
longlong replacement
451
  int64_t val_int();
1 by brian
clean slate
452
  enum Functype functype() const { return NOT_ALL_FUNC; }
453
  const char *func_name() const { return "<not>"; }
454
  virtual void print(String *str, enum_query_type query_type);
455
  void set_sum_test(Item_sum_hybrid *item) { test_sum_item= item; };
456
  void set_sub_test(Item_maxmin_subselect *item) { test_sub_item= item; };
457
  bool empty_underlying_subquery();
520.1.22 by Brian Aker
Second pass of thd cleanup
458
  Item *neg_transformer(Session *session);
1 by brian
clean slate
459
};
460
461
462
class Item_func_nop_all :public Item_func_not_all
463
{
464
public:
465
466
  Item_func_nop_all(Item *a) :Item_func_not_all(a) {}
152 by Brian Aker
longlong replacement
467
  int64_t val_int();
1 by brian
clean slate
468
  const char *func_name() const { return "<nop>"; }
520.1.22 by Brian Aker
Second pass of thd cleanup
469
  Item *neg_transformer(Session *session);
1 by brian
clean slate
470
};
471
472
473
class Item_func_eq :public Item_bool_rowready_func2
474
{
475
public:
476
  Item_func_eq(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {}
152 by Brian Aker
longlong replacement
477
  int64_t val_int();
1 by brian
clean slate
478
  enum Functype functype() const { return EQ_FUNC; }
479
  enum Functype rev_functype() const { return EQ_FUNC; }
480
  cond_result eq_cmp_result() const { return COND_TRUE; }
481
  const char *func_name() const { return "="; }
482
  Item *negated_item();
483
};
484
485
class Item_func_equal :public Item_bool_rowready_func2
486
{
487
public:
488
  Item_func_equal(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {};
152 by Brian Aker
longlong replacement
489
  int64_t val_int();
1 by brian
clean slate
490
  void fix_length_and_dec();
491
  table_map not_null_tables() const { return 0; }
492
  enum Functype functype() const { return EQUAL_FUNC; }
493
  enum Functype rev_functype() const { return EQUAL_FUNC; }
494
  cond_result eq_cmp_result() const { return COND_TRUE; }
495
  const char *func_name() const { return "<=>"; }
646 by Brian Aker
Next pass through attribute.
496
  Item *neg_transformer(Session *) { return 0; }
1 by brian
clean slate
497
};
498
499
500
class Item_func_ge :public Item_bool_rowready_func2
501
{
502
public:
503
  Item_func_ge(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {};
152 by Brian Aker
longlong replacement
504
  int64_t val_int();
1 by brian
clean slate
505
  enum Functype functype() const { return GE_FUNC; }
506
  enum Functype rev_functype() const { return LE_FUNC; }
507
  cond_result eq_cmp_result() const { return COND_TRUE; }
508
  const char *func_name() const { return ">="; }
509
  Item *negated_item();
510
};
511
512
513
class Item_func_gt :public Item_bool_rowready_func2
514
{
515
public:
516
  Item_func_gt(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {};
152 by Brian Aker
longlong replacement
517
  int64_t val_int();
1 by brian
clean slate
518
  enum Functype functype() const { return GT_FUNC; }
519
  enum Functype rev_functype() const { return LT_FUNC; }
520
  cond_result eq_cmp_result() const { return COND_FALSE; }
521
  const char *func_name() const { return ">"; }
522
  Item *negated_item();
523
};
524
525
526
class Item_func_le :public Item_bool_rowready_func2
527
{
528
public:
529
  Item_func_le(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {};
152 by Brian Aker
longlong replacement
530
  int64_t val_int();
1 by brian
clean slate
531
  enum Functype functype() const { return LE_FUNC; }
532
  enum Functype rev_functype() const { return GE_FUNC; }
533
  cond_result eq_cmp_result() const { return COND_TRUE; }
534
  const char *func_name() const { return "<="; }
535
  Item *negated_item();
536
};
537
538
539
class Item_func_lt :public Item_bool_rowready_func2
540
{
541
public:
542
  Item_func_lt(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {}
152 by Brian Aker
longlong replacement
543
  int64_t val_int();
1 by brian
clean slate
544
  enum Functype functype() const { return LT_FUNC; }
545
  enum Functype rev_functype() const { return GT_FUNC; }
546
  cond_result eq_cmp_result() const { return COND_FALSE; }
547
  const char *func_name() const { return "<"; }
548
  Item *negated_item();
549
};
550
551
552
class Item_func_ne :public Item_bool_rowready_func2
553
{
554
public:
555
  Item_func_ne(Item *a,Item *b) :Item_bool_rowready_func2(a,b) {}
152 by Brian Aker
longlong replacement
556
  int64_t val_int();
1 by brian
clean slate
557
  enum Functype functype() const { return NE_FUNC; }
558
  cond_result eq_cmp_result() const { return COND_FALSE; }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
559
  optimize_type select_optimize() const { return OPTIMIZE_KEY; }
1 by brian
clean slate
560
  const char *func_name() const { return "<>"; }
561
  Item *negated_item();
562
};
563
564
565
/*
566
  The class Item_func_opt_neg is defined to factor out the functionality
567
  common for the classes Item_func_between and Item_func_in. The objects
568
  of these classes can express predicates or there negations.
569
  The alternative approach would be to create pairs Item_func_between,
570
  Item_func_notbetween and Item_func_in, Item_func_notin.
571
572
*/
573
574
class Item_func_opt_neg :public Item_int_func
575
{
576
public:
577
  bool negated;     /* <=> the item represents NOT <func> */
578
  bool pred_level;  /* <=> [NOT] <func> is used on a predicate level */
579
public:
580
  Item_func_opt_neg(Item *a, Item *b, Item *c)
581
    :Item_int_func(a, b, c), negated(0), pred_level(0) {}
582
  Item_func_opt_neg(List<Item> &list)
583
    :Item_int_func(list), negated(0), pred_level(0) {}
584
public:
585
  inline void negate() { negated= !negated; }
586
  inline void top_level_item() { pred_level= 1; }
646 by Brian Aker
Next pass through attribute.
587
  Item *neg_transformer(Session *)
1 by brian
clean slate
588
  {
589
    negated= !negated;
590
    return this;
591
  }
592
  bool eq(const Item *item, bool binary_cmp) const;
646 by Brian Aker
Next pass through attribute.
593
  bool subst_argument_checker(unsigned char **)
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
594
  { return true; }
1 by brian
clean slate
595
};
596
597
598
class Item_func_between :public Item_func_opt_neg
599
{
600
  DTCollation cmp_collation;
601
public:
602
  Item_result cmp_type;
603
  String value0,value1,value2;
604
  /* TRUE <=> arguments will be compared as dates. */
605
  bool compare_as_dates;
606
  /* Comparators used for DATE/DATETIME comparison. */
607
  Arg_comparator ge_cmp, le_cmp;
608
  Item_func_between(Item *a, Item *b, Item *c)
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
609
    :Item_func_opt_neg(a, b, c), compare_as_dates(false) {}
152 by Brian Aker
longlong replacement
610
  int64_t val_int();
1 by brian
clean slate
611
  optimize_type select_optimize() const { return OPTIMIZE_KEY; }
612
  enum Functype functype() const   { return BETWEEN; }
613
  const char *func_name() const { return "between"; }
520.1.21 by Brian Aker
THD -> Session rename
614
  bool fix_fields(Session *, Item **);
1 by brian
clean slate
615
  void fix_length_and_dec();
616
  virtual void print(String *str, enum_query_type query_type);
617
  bool is_bool_func() { return 1; }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
618
  const CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
482 by Brian Aker
Remove uint.
619
  uint32_t decimal_precision() const { return 1; }
1 by brian
clean slate
620
};
621
622
623
class Item_func_strcmp :public Item_bool_func2
624
{
625
public:
626
  Item_func_strcmp(Item *a,Item *b) :Item_bool_func2(a,b) {}
152 by Brian Aker
longlong replacement
627
  int64_t val_int();
1 by brian
clean slate
628
  optimize_type select_optimize() const { return OPTIMIZE_NONE; }
629
  const char *func_name() const { return "strcmp"; }
630
631
  virtual inline void print(String *str, enum_query_type query_type)
632
  {
633
    Item_func::print(str, query_type);
634
  }
635
};
636
637
638
struct interval_range
639
{
640
  Item_result type;
641
  double dbl;
642
  my_decimal dec;
643
};
644
645
class Item_func_interval :public Item_int_func
646
{
647
  Item_row *row;
275 by Brian Aker
Full removal of my_bool from central server.
648
  bool use_decimal_comparison;
1 by brian
clean slate
649
  interval_range *intervals;
650
public:
651
  Item_func_interval(Item_row *a)
652
    :Item_int_func(a),row(a),intervals(0)
653
  {
654
    allowed_arg_cols= 0;    // Fetch this value from first argument
655
  }
152 by Brian Aker
longlong replacement
656
  int64_t val_int();
1 by brian
clean slate
657
  void fix_length_and_dec();
658
  const char *func_name() const { return "interval"; }
482 by Brian Aker
Remove uint.
659
  uint32_t decimal_precision() const { return 2; }
1 by brian
clean slate
660
};
661
662
663
class Item_func_coalesce :public Item_func_numhybrid
664
{
665
protected:
666
  enum_field_types cached_field_type;
667
  Item_func_coalesce(Item *a, Item *b) :Item_func_numhybrid(a, b) {}
668
public:
669
  Item_func_coalesce(List<Item> &list) :Item_func_numhybrid(list) {}
670
  double real_op();
152 by Brian Aker
longlong replacement
671
  int64_t int_op();
1 by brian
clean slate
672
  String *str_op(String *);
673
  my_decimal *decimal_op(my_decimal *);
674
  void fix_length_and_dec();
675
  void find_num_type() {}
676
  enum Item_result result_type () const { return hybrid_type; }
677
  const char *func_name() const { return "coalesce"; }
678
  table_map not_null_tables() const { return 0; }
679
  enum_field_types field_type() const { return cached_field_type; }
680
};
681
682
683
class Item_func_ifnull :public Item_func_coalesce
684
{
685
protected:
686
  bool field_type_defined;
687
public:
688
  Item_func_ifnull(Item *a, Item *b) :Item_func_coalesce(a,b) {}
689
  double real_op();
152 by Brian Aker
longlong replacement
690
  int64_t int_op();
1 by brian
clean slate
691
  String *str_op(String *str);
692
  my_decimal *decimal_op(my_decimal *);
693
  enum_field_types field_type() const;
694
  void fix_length_and_dec();
695
  const char *func_name() const { return "ifnull"; }
779.3.16 by Monty Taylor
Some Sun warning fixes.
696
  Field *tmp_table_field()
697
  {
698
    return Item_func::tmp_table_field();
699
  }
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
700
  Field *tmp_table_field(Table *table);
482 by Brian Aker
Remove uint.
701
  uint32_t decimal_precision() const;
1 by brian
clean slate
702
};
703
704
705
class Item_func_if :public Item_func
706
{
707
  enum Item_result cached_result_type;
708
  enum_field_types cached_field_type;
709
public:
710
  Item_func_if(Item *a,Item *b,Item *c)
711
    :Item_func(a,b,c), cached_result_type(INT_RESULT)
712
  {}
713
  double val_real();
152 by Brian Aker
longlong replacement
714
  int64_t val_int();
1 by brian
clean slate
715
  String *val_str(String *str);
716
  my_decimal *val_decimal(my_decimal *);
717
  enum Item_result result_type () const { return cached_result_type; }
718
  enum_field_types field_type() const { return cached_field_type; }
520.1.21 by Brian Aker
THD -> Session rename
719
  bool fix_fields(Session *, Item **);
1 by brian
clean slate
720
  void fix_length_and_dec();
482 by Brian Aker
Remove uint.
721
  uint32_t decimal_precision() const;
1 by brian
clean slate
722
  const char *func_name() const { return "if"; }
723
};
724
725
726
class Item_func_nullif :public Item_bool_func2
727
{
728
  enum Item_result cached_result_type;
729
public:
730
  Item_func_nullif(Item *a,Item *b)
731
    :Item_bool_func2(a,b), cached_result_type(INT_RESULT)
732
  {}
733
  double val_real();
152 by Brian Aker
longlong replacement
734
  int64_t val_int();
1 by brian
clean slate
735
  String *val_str(String *str);
736
  my_decimal *val_decimal(my_decimal *);
737
  enum Item_result result_type () const { return cached_result_type; }
738
  void fix_length_and_dec();
482 by Brian Aker
Remove uint.
739
  uint32_t decimal_precision() const { return args[0]->decimal_precision(); }
1 by brian
clean slate
740
  const char *func_name() const { return "nullif"; }
741
742
  virtual inline void print(String *str, enum_query_type query_type)
743
  {
744
    Item_func::print(str, query_type);
745
  }
746
747
  table_map not_null_tables() const { return 0; }
748
  bool is_null();
749
};
750
751
752
/* Functions to handle the optimized IN */
753
754
755
/* A vector of values of some type  */
756
757
class in_vector :public Sql_alloc
758
{
759
public:
760
  char *base;
482 by Brian Aker
Remove uint.
761
  uint32_t size;
1 by brian
clean slate
762
  qsort2_cmp compare;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
763
  const CHARSET_INFO *collation;
482 by Brian Aker
Remove uint.
764
  uint32_t count;
765
  uint32_t used_count;
1 by brian
clean slate
766
  in_vector() {}
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
767
  in_vector(uint32_t elements,uint32_t element_length,qsort2_cmp cmp_func,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
768
  	    const CHARSET_INFO * const cmp_coll)
1 by brian
clean slate
769
    :base((char*) sql_calloc(elements*element_length)),
770
     size(element_length), compare(cmp_func), collation(cmp_coll),
771
     count(elements), used_count(elements) {}
772
  virtual ~in_vector() {}
482 by Brian Aker
Remove uint.
773
  virtual void set(uint32_t pos,Item *item)=0;
481 by Brian Aker
Remove all of uchar.
774
  virtual unsigned char *get_value(Item *item)=0;
1 by brian
clean slate
775
  void sort()
776
  {
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
777
    my_qsort2(base,used_count,size,compare, (void *) collation);
1 by brian
clean slate
778
  }
779
  int find(Item *item);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
780
781
  /*
1 by brian
clean slate
782
    Create an instance of Item_{type} (e.g. Item_decimal) constant object
783
    which type allows it to hold an element of this vector without any
784
    conversions.
785
    The purpose of this function is to be able to get elements of this
786
    vector in form of Item_xxx constants without creating Item_xxx object
787
    for every array element you get (i.e. this implements "FlyWeight" pattern)
788
  */
789
  virtual Item* create_item() { return NULL; }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
790
1 by brian
clean slate
791
  /*
792
    Store the value at position #pos into provided item object
793
    SYNOPSIS
794
      value_to_item()
795
        pos   Index of value to store
796
        item  Constant item to store value into. The item must be of the same
797
              type that create_item() returns.
798
  */
646 by Brian Aker
Next pass through attribute.
799
  virtual void value_to_item(uint32_t, Item *) { }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
800
1 by brian
clean slate
801
  /* Compare values number pos1 and pos2 for equality */
482 by Brian Aker
Remove uint.
802
  bool compare_elems(uint32_t pos1, uint32_t pos2)
1 by brian
clean slate
803
  {
804
    return test(compare(collation, base + pos1*size, base + pos2*size));
805
  }
806
  virtual Item_result result_type()= 0;
807
};
808
809
class in_string :public in_vector
810
{
811
  char buff[STRING_BUFFER_USUAL_SIZE];
812
  String tmp;
813
public:
482 by Brian Aker
Remove uint.
814
  in_string(uint32_t elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs);
1 by brian
clean slate
815
  ~in_string();
482 by Brian Aker
Remove uint.
816
  void set(uint32_t pos,Item *item);
481 by Brian Aker
Remove all of uchar.
817
  unsigned char *get_value(Item *item);
1 by brian
clean slate
818
  Item* create_item()
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
819
  {
1 by brian
clean slate
820
    return new Item_string(collation);
821
  }
482 by Brian Aker
Remove uint.
822
  void value_to_item(uint32_t pos, Item *item)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
823
  {
1 by brian
clean slate
824
    String *str=((String*) base)+pos;
825
    Item_string *to= (Item_string*)item;
826
    to->str_value= *str;
827
  }
828
  Item_result result_type() { return STRING_RESULT; }
829
};
830
152 by Brian Aker
longlong replacement
831
class in_int64_t :public in_vector
1 by brian
clean slate
832
{
833
protected:
834
  /*
835
    Here we declare a temporary variable (tmp) of the same type as the
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
836
    elements of this vector. tmp is used in finding if a given value is in
837
    the list.
1 by brian
clean slate
838
  */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
839
  struct packed_int64_t
1 by brian
clean slate
840
  {
152 by Brian Aker
longlong replacement
841
    int64_t val;
842
    int64_t unsigned_flag;  // Use int64_t, not bool, to preserve alignment
1 by brian
clean slate
843
  } tmp;
844
public:
482 by Brian Aker
Remove uint.
845
  in_int64_t(uint32_t elements);
846
  void set(uint32_t pos,Item *item);
481 by Brian Aker
Remove all of uchar.
847
  unsigned char *get_value(Item *item);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
848
1 by brian
clean slate
849
  Item* create_item()
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
850
  {
851
    /*
852
      We're created a signed INT, this may not be correct in
1 by brian
clean slate
853
      general case (see BUG#19342).
854
    */
152 by Brian Aker
longlong replacement
855
    return new Item_int((int64_t)0);
1 by brian
clean slate
856
  }
482 by Brian Aker
Remove uint.
857
  void value_to_item(uint32_t pos, Item *item)
1 by brian
clean slate
858
  {
152 by Brian Aker
longlong replacement
859
    ((Item_int*) item)->value= ((packed_int64_t*) base)[pos].val;
275 by Brian Aker
Full removal of my_bool from central server.
860
    ((Item_int*) item)->unsigned_flag= (bool)
152 by Brian Aker
longlong replacement
861
      ((packed_int64_t*) base)[pos].unsigned_flag;
1 by brian
clean slate
862
  }
863
  Item_result result_type() { return INT_RESULT; }
864
152 by Brian Aker
longlong replacement
865
  friend int cmp_int64_t(void *cmp_arg, packed_int64_t *a,packed_int64_t *b);
1 by brian
clean slate
866
};
867
868
869
/*
870
  Class to represent a vector of constant DATE/DATETIME values.
871
  Values are obtained with help of the get_datetime_value() function.
872
  If the left item is a constant one then its value is cached in the
873
  lval_cache variable.
874
*/
152 by Brian Aker
longlong replacement
875
class in_datetime :public in_int64_t
1 by brian
clean slate
876
{
877
public:
520.1.22 by Brian Aker
Second pass of thd cleanup
878
  Session *session;
1 by brian
clean slate
879
  /* An item used to issue warnings. */
880
  Item *warn_item;
881
  /* Cache for the left item. */
882
  Item *lval_cache;
883
482 by Brian Aker
Remove uint.
884
  in_datetime(Item *warn_item_arg, uint32_t elements)
520.1.22 by Brian Aker
Second pass of thd cleanup
885
    :in_int64_t(elements), session(current_session), warn_item(warn_item_arg),
1 by brian
clean slate
886
     lval_cache(0) {};
482 by Brian Aker
Remove uint.
887
  void set(uint32_t pos,Item *item);
481 by Brian Aker
Remove all of uchar.
888
  unsigned char *get_value(Item *item);
152 by Brian Aker
longlong replacement
889
  friend int cmp_int64_t(void *cmp_arg, packed_int64_t *a,packed_int64_t *b);
1 by brian
clean slate
890
};
891
892
893
class in_double :public in_vector
894
{
895
  double tmp;
896
public:
482 by Brian Aker
Remove uint.
897
  in_double(uint32_t elements);
898
  void set(uint32_t pos,Item *item);
481 by Brian Aker
Remove all of uchar.
899
  unsigned char *get_value(Item *item);
1 by brian
clean slate
900
  Item *create_item()
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
901
  {
1 by brian
clean slate
902
    return new Item_float(0.0, 0);
903
  }
482 by Brian Aker
Remove uint.
904
  void value_to_item(uint32_t pos, Item *item)
1 by brian
clean slate
905
  {
906
    ((Item_float*)item)->value= ((double*) base)[pos];
907
  }
908
  Item_result result_type() { return REAL_RESULT; }
909
};
910
911
912
class in_decimal :public in_vector
913
{
914
  my_decimal val;
915
public:
482 by Brian Aker
Remove uint.
916
  in_decimal(uint32_t elements);
917
  void set(uint32_t pos, Item *item);
481 by Brian Aker
Remove all of uchar.
918
  unsigned char *get_value(Item *item);
1 by brian
clean slate
919
  Item *create_item()
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
920
  {
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
921
    return new Item_decimal(0, false);
1 by brian
clean slate
922
  }
482 by Brian Aker
Remove uint.
923
  void value_to_item(uint32_t pos, Item *item)
1 by brian
clean slate
924
  {
925
    my_decimal *dec= ((my_decimal *)base) + pos;
926
    Item_decimal *item_dec= (Item_decimal*)item;
927
    item_dec->set_decimal_value(dec);
928
  }
929
  Item_result result_type() { return DECIMAL_RESULT; }
930
931
};
932
933
934
/*
935
** Classes for easy comparing of non const items
936
*/
937
938
class cmp_item :public Sql_alloc
939
{
940
public:
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
941
  const CHARSET_INFO *cmp_charset;
1 by brian
clean slate
942
  cmp_item() { cmp_charset= &my_charset_bin; }
943
  virtual ~cmp_item() {}
944
  virtual void store_value(Item *item)= 0;
945
  virtual int cmp(Item *item)= 0;
946
  // for optimized IN with row
947
  virtual int compare(cmp_item *item)= 0;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
948
  static cmp_item* get_comparator(Item_result type, const CHARSET_INFO * const cs);
1 by brian
clean slate
949
  virtual cmp_item *make_same()= 0;
646 by Brian Aker
Next pass through attribute.
950
  virtual void store_value_by_template(cmp_item *, Item *item)
1 by brian
clean slate
951
  {
952
    store_value(item);
953
  }
954
};
955
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
956
class cmp_item_string :public cmp_item
1 by brian
clean slate
957
{
958
protected:
959
  String *value_res;
960
public:
961
  cmp_item_string () {}
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
962
  cmp_item_string (const CHARSET_INFO * const cs) { cmp_charset= cs; }
963
  void set_charset(const CHARSET_INFO * const cs) { cmp_charset= cs; }
1 by brian
clean slate
964
  friend class cmp_item_sort_string;
965
  friend class cmp_item_sort_string_in_static;
966
};
967
968
class cmp_item_sort_string :public cmp_item_string
969
{
970
protected:
971
  char value_buff[STRING_BUFFER_USUAL_SIZE];
972
  String value;
973
public:
974
  cmp_item_sort_string():
975
    cmp_item_string() {}
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
976
  cmp_item_sort_string(const CHARSET_INFO * const cs):
1 by brian
clean slate
977
    cmp_item_string(cs),
978
    value(value_buff, sizeof(value_buff), cs) {}
979
  void store_value(Item *item)
980
  {
981
    value_res= item->val_str(&value);
982
  }
983
  int cmp(Item *arg)
984
  {
985
    char buff[STRING_BUFFER_USUAL_SIZE];
986
    String tmp(buff, sizeof(buff), cmp_charset), *res;
987
    res= arg->val_str(&tmp);
988
    return (value_res ? (res ? sortcmp(value_res, res, cmp_charset) : 1) :
989
            (res ? -1 : 0));
990
  }
991
  int compare(cmp_item *ci)
992
  {
993
    cmp_item_string *l_cmp= (cmp_item_string *) ci;
994
    return sortcmp(value_res, l_cmp->value_res, cmp_charset);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
995
  }
1 by brian
clean slate
996
  cmp_item *make_same();
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
997
  void set_charset(const CHARSET_INFO * const cs)
1 by brian
clean slate
998
  {
999
    cmp_charset= cs;
1000
    value.set_quick(value_buff, sizeof(value_buff), cs);
1001
  }
1002
};
1003
1004
class cmp_item_int :public cmp_item
1005
{
152 by Brian Aker
longlong replacement
1006
  int64_t value;
1 by brian
clean slate
1007
public:
1008
  cmp_item_int() {}                           /* Remove gcc warning */
1009
  void store_value(Item *item)
1010
  {
1011
    value= item->val_int();
1012
  }
1013
  int cmp(Item *arg)
1014
  {
1015
    return value != arg->val_int();
1016
  }
1017
  int compare(cmp_item *ci)
1018
  {
1019
    cmp_item_int *l_cmp= (cmp_item_int *)ci;
1020
    return (value < l_cmp->value) ? -1 : ((value == l_cmp->value) ? 0 : 1);
1021
  }
1022
  cmp_item *make_same();
1023
};
1024
1025
/*
1026
  Compare items in the DATETIME context.
1027
  Values are obtained with help of the get_datetime_value() function.
1028
  If the left item is a constant one then its value is cached in the
1029
  lval_cache variable.
1030
*/
1031
class cmp_item_datetime :public cmp_item
1032
{
151 by Brian Aker
Ulonglong to uint64_t
1033
  uint64_t value;
1 by brian
clean slate
1034
public:
520.1.22 by Brian Aker
Second pass of thd cleanup
1035
  Session *session;
1 by brian
clean slate
1036
  /* Item used for issuing warnings. */
1037
  Item *warn_item;
1038
  /* Cache for the left item. */
1039
  Item *lval_cache;
1040
1041
  cmp_item_datetime(Item *warn_item_arg)
520.1.22 by Brian Aker
Second pass of thd cleanup
1042
    :session(current_session), warn_item(warn_item_arg), lval_cache(0) {}
1 by brian
clean slate
1043
  void store_value(Item *item);
1044
  int cmp(Item *arg);
1045
  int compare(cmp_item *ci);
1046
  cmp_item *make_same();
1047
};
1048
1049
class cmp_item_real :public cmp_item
1050
{
1051
  double value;
1052
public:
1053
  cmp_item_real() {}                          /* Remove gcc warning */
1054
  void store_value(Item *item)
1055
  {
1056
    value= item->val_real();
1057
  }
1058
  int cmp(Item *arg)
1059
  {
1060
    return value != arg->val_real();
1061
  }
1062
  int compare(cmp_item *ci)
1063
  {
1064
    cmp_item_real *l_cmp= (cmp_item_real *) ci;
1065
    return (value < l_cmp->value)? -1 : ((value == l_cmp->value) ? 0 : 1);
1066
  }
1067
  cmp_item *make_same();
1068
};
1069
1070
1071
class cmp_item_decimal :public cmp_item
1072
{
1073
  my_decimal value;
1074
public:
1075
  cmp_item_decimal() {}                       /* Remove gcc warning */
1076
  void store_value(Item *item);
1077
  int cmp(Item *arg);
1078
  int compare(cmp_item *c);
1079
  cmp_item *make_same();
1080
};
1081
1082
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1083
/*
1 by brian
clean slate
1084
   cmp_item for optimized IN with row (right part string, which never
1085
   be changed)
1086
*/
1087
1088
class cmp_item_sort_string_in_static :public cmp_item_string
1089
{
1090
 protected:
1091
  String value;
1092
public:
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1093
  cmp_item_sort_string_in_static(const CHARSET_INFO * const cs):
1 by brian
clean slate
1094
    cmp_item_string(cs) {}
1095
  void store_value(Item *item)
1096
  {
1097
    value_res= item->val_str(&value);
1098
  }
646 by Brian Aker
Next pass through attribute.
1099
  int cmp(Item *)
1 by brian
clean slate
1100
  {
1101
    // Should never be called
51.1.17 by Jay Pipes
Removed/replaced DBUG symbols
1102
    assert(0);
1 by brian
clean slate
1103
    return 1;
1104
  }
1105
  int compare(cmp_item *ci)
1106
  {
1107
    cmp_item_string *l_cmp= (cmp_item_string *) ci;
1108
    return sortcmp(value_res, l_cmp->value_res, cmp_charset);
1109
  }
1110
  cmp_item *make_same()
1111
  {
1112
    return new cmp_item_sort_string_in_static(cmp_charset);
1113
  }
1114
};
1115
1116
1117
/*
1118
  The class Item_func_case is the CASE ... WHEN ... THEN ... END function
1119
  implementation.
1120
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1121
  When there is no expression between CASE and the first WHEN
1 by brian
clean slate
1122
  (the CASE expression) then this function simple checks all WHEN expressions
1123
  one after another. When some WHEN expression evaluated to TRUE then the
1124
  value of the corresponding THEN expression is returned.
1125
1126
  When the CASE expression is specified then it is compared to each WHEN
1127
  expression individually. When an equal WHEN expression is found
1128
  corresponding THEN expression is returned.
1129
  In order to do correct comparisons several comparators are used. One for
1130
  each result type. Different result types that are used in particular
1131
  CASE ... END expression are collected in the fix_length_and_dec() member
1132
  function and only comparators for there result types are used.
1133
*/
1134
1135
class Item_func_case :public Item_func
1136
{
1137
  int first_expr_num, else_expr_num;
1138
  enum Item_result cached_result_type, left_result_type;
1139
  String tmp_value;
482 by Brian Aker
Remove uint.
1140
  uint32_t ncases;
1 by brian
clean slate
1141
  Item_result cmp_type;
1142
  DTCollation cmp_collation;
1143
  enum_field_types cached_field_type;
1144
  cmp_item *cmp_items[5]; /* For all result types */
1145
  cmp_item *case_item;
1146
public:
1147
  Item_func_case(List<Item> &list, Item *first_expr_arg, Item *else_expr_arg)
1148
    :Item_func(), first_expr_num(-1), else_expr_num(-1),
1149
    cached_result_type(INT_RESULT), left_result_type(INT_RESULT), case_item(0)
1150
  {
1151
    ncases= list.elements;
1152
    if (first_expr_arg)
1153
    {
1154
      first_expr_num= list.elements;
1155
      list.push_back(first_expr_arg);
1156
    }
1157
    if (else_expr_arg)
1158
    {
1159
      else_expr_num= list.elements;
1160
      list.push_back(else_expr_arg);
1161
    }
1162
    set_arguments(list);
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
1163
    memset(&cmp_items, 0, sizeof(cmp_items));
1 by brian
clean slate
1164
  }
1165
  double val_real();
152 by Brian Aker
longlong replacement
1166
  int64_t val_int();
1 by brian
clean slate
1167
  String *val_str(String *);
1168
  my_decimal *val_decimal(my_decimal *);
520.1.22 by Brian Aker
Second pass of thd cleanup
1169
  bool fix_fields(Session *session, Item **ref);
1 by brian
clean slate
1170
  void fix_length_and_dec();
482 by Brian Aker
Remove uint.
1171
  uint32_t decimal_precision() const;
1 by brian
clean slate
1172
  table_map not_null_tables() const { return 0; }
1173
  enum Item_result result_type () const { return cached_result_type; }
1174
  enum_field_types field_type() const { return cached_field_type; }
1175
  const char *func_name() const { return "case"; }
1176
  virtual void print(String *str, enum_query_type query_type);
1177
  Item *find_item(String *str);
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1178
  const CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
1 by brian
clean slate
1179
  void cleanup();
1180
  void agg_str_lengths(Item *arg);
1181
  void agg_num_lengths(Item *arg);
1182
};
1183
1184
/*
1185
  The Item_func_in class implements the in_expr IN(values_list) function.
1186
1187
  The current implementation distinguishes 2 cases:
1188
  1) all items in the value_list are constants and have the same
1189
    result type. This case is handled by in_vector class.
1190
  2) items in the value_list have different result types or there is some
1191
    non-constant items.
1192
    In this case Item_func_in employs several cmp_item objects to performs
1193
    comparisons of in_expr and an item from the values_list. One cmp_item
1194
    object for each result type. Different result types are collected in the
1195
    fix_length_and_dec() member function by means of collect_cmp_types()
1196
    function.
1197
*/
1198
class Item_func_in :public Item_func_opt_neg
1199
{
1200
public:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1201
  /*
1 by brian
clean slate
1202
    an array of values when the right hand arguments of IN
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1203
    are all SQL constant and there are no nulls
1 by brian
clean slate
1204
  */
1205
  in_vector *array;
1206
  bool have_null;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1207
  /*
1 by brian
clean slate
1208
    true when all arguments of the IN clause are of compatible types
1209
    and can be used safely as comparisons for key conditions
1210
  */
1211
  bool arg_types_compatible;
1212
  Item_result left_result_type;
1213
  cmp_item *cmp_items[6]; /* One cmp_item for each result type */
1214
  DTCollation cmp_collation;
1215
1216
  Item_func_in(List<Item> &list)
1217
    :Item_func_opt_neg(list), array(0), have_null(0),
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1218
    arg_types_compatible(false)
1 by brian
clean slate
1219
  {
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
1220
    memset(&cmp_items, 0, sizeof(cmp_items));
1 by brian
clean slate
1221
    allowed_arg_cols= 0;  // Fetch this value from first argument
1222
  }
152 by Brian Aker
longlong replacement
1223
  int64_t val_int();
520.1.21 by Brian Aker
THD -> Session rename
1224
  bool fix_fields(Session *, Item **);
1 by brian
clean slate
1225
  void fix_length_and_dec();
482 by Brian Aker
Remove uint.
1226
  uint32_t decimal_precision() const { return 1; }
1 by brian
clean slate
1227
  void cleanup()
1228
  {
482 by Brian Aker
Remove uint.
1229
    uint32_t i;
1 by brian
clean slate
1230
    Item_int_func::cleanup();
1231
    delete array;
1232
    array= 0;
895 by Brian Aker
Completion (?) of uint conversion.
1233
    for (i= 0; i <= (uint32_t)DECIMAL_RESULT + 1; i++)
1 by brian
clean slate
1234
    {
1235
      delete cmp_items[i];
1236
      cmp_items[i]= 0;
1237
    }
51.1.17 by Jay Pipes
Removed/replaced DBUG symbols
1238
    return;
1 by brian
clean slate
1239
  }
1240
  optimize_type select_optimize() const
1241
    { return OPTIMIZE_KEY; }
1242
  virtual void print(String *str, enum_query_type query_type);
1243
  enum Functype functype() const { return IN_FUNC; }
1244
  const char *func_name() const { return " IN "; }
1245
  bool nulls_in_row();
1246
  bool is_bool_func() { return 1; }
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1247
  const CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
1 by brian
clean slate
1248
};
1249
1250
class cmp_item_row :public cmp_item
1251
{
1252
  cmp_item **comparators;
482 by Brian Aker
Remove uint.
1253
  uint32_t n;
1 by brian
clean slate
1254
public:
1255
  cmp_item_row(): comparators(0), n(0) {}
1256
  ~cmp_item_row();
1257
  void store_value(Item *item);
1258
  inline void alloc_comparators();
1259
  int cmp(Item *arg);
1260
  int compare(cmp_item *arg);
1261
  cmp_item *make_same();
1262
  void store_value_by_template(cmp_item *tmpl, Item *);
1263
  friend void Item_func_in::fix_length_and_dec();
1264
};
1265
1266
1267
class in_row :public in_vector
1268
{
1269
  cmp_item_row tmp;
1270
public:
482 by Brian Aker
Remove uint.
1271
  in_row(uint32_t elements, Item *);
1 by brian
clean slate
1272
  ~in_row();
482 by Brian Aker
Remove uint.
1273
  void set(uint32_t pos,Item *item);
481 by Brian Aker
Remove all of uchar.
1274
  unsigned char *get_value(Item *item);
1 by brian
clean slate
1275
  friend void Item_func_in::fix_length_and_dec();
1276
  Item_result result_type() { return ROW_RESULT; }
1277
};
1278
1279
/* Functions used by where clause */
1280
1281
class Item_func_isnull :public Item_bool_func
1282
{
1283
protected:
152 by Brian Aker
longlong replacement
1284
  int64_t cached_value;
1 by brian
clean slate
1285
public:
1286
  Item_func_isnull(Item *a) :Item_bool_func(a) {}
152 by Brian Aker
longlong replacement
1287
  int64_t val_int();
1 by brian
clean slate
1288
  enum Functype functype() const { return ISNULL_FUNC; }
1289
  void fix_length_and_dec()
1290
  {
1291
    decimals=0; max_length=1; maybe_null=0;
1292
    update_used_tables();
1293
  }
1294
  const char *func_name() const { return "isnull"; }
1295
  /* Optimize case of not_null_column IS NULL */
1296
  virtual void update_used_tables()
1297
  {
1298
    if (!args[0]->maybe_null)
1299
    {
1300
      used_tables_cache= 0;			/* is always false */
1301
      const_item_cache= 1;
152 by Brian Aker
longlong replacement
1302
      cached_value= (int64_t) 0;
1 by brian
clean slate
1303
    }
1304
    else
1305
    {
1306
      args[0]->update_used_tables();
1307
      if ((const_item_cache= !(used_tables_cache= args[0]->used_tables())) &&
1308
          !with_subselect)
1309
      {
1310
	/* Remember if the value is always NULL or never NULL */
152 by Brian Aker
longlong replacement
1311
	cached_value= (int64_t) args[0]->is_null();
1 by brian
clean slate
1312
      }
1313
    }
1314
  }
1315
  table_map not_null_tables() const { return 0; }
1316
  optimize_type select_optimize() const { return OPTIMIZE_NULL; }
520.1.22 by Brian Aker
Second pass of thd cleanup
1317
  Item *neg_transformer(Session *session);
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1318
  const CHARSET_INFO *compare_collation() { return args[0]->collation.collation; }
1 by brian
clean slate
1319
};
1320
1321
/* Functions used by HAVING for rewriting IN subquery */
1322
1323
class Item_in_subselect;
1324
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1325
/*
1 by brian
clean slate
1326
  This is like IS NOT NULL but it also remembers if it ever has
1327
  encountered a NULL.
1328
*/
1329
class Item_is_not_null_test :public Item_func_isnull
1330
{
1331
  Item_in_subselect* owner;
1332
public:
1333
  Item_is_not_null_test(Item_in_subselect* ow, Item *a)
1334
    :Item_func_isnull(a), owner(ow)
1335
  {}
1336
  enum Functype functype() const { return ISNOTNULLTEST_FUNC; }
152 by Brian Aker
longlong replacement
1337
  int64_t val_int();
1 by brian
clean slate
1338
  const char *func_name() const { return "<is_not_null_test>"; }
1339
  void update_used_tables();
1340
  /*
1341
    we add RAND_TABLE_BIT to prevent moving this item from HAVING to WHERE
1342
  */
1343
  table_map used_tables() const
1344
    { return used_tables_cache | RAND_TABLE_BIT; }
1345
};
1346
1347
1348
class Item_func_isnotnull :public Item_bool_func
1349
{
1350
  bool abort_on_null;
1351
public:
1352
  Item_func_isnotnull(Item *a) :Item_bool_func(a), abort_on_null(0) {}
152 by Brian Aker
longlong replacement
1353
  int64_t val_int();
1 by brian
clean slate
1354
  enum Functype functype() const { return ISNOTNULL_FUNC; }
1355
  void fix_length_and_dec()
1356
  {
1357
    decimals=0; max_length=1; maybe_null=0;
1358
  }
1359
  const char *func_name() const { return "isnotnull"; }
1360
  optimize_type select_optimize() const { return OPTIMIZE_NULL; }
1361
  table_map not_null_tables() const
1362
  { return abort_on_null ? not_null_tables_cache : 0; }
520.1.22 by Brian Aker
Second pass of thd cleanup
1363
  Item *neg_transformer(Session *session);
1 by brian
clean slate
1364
  virtual void print(String *str, enum_query_type query_type);
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1365
  const CHARSET_INFO *compare_collation() { return args[0]->collation.collation; }
1 by brian
clean slate
1366
  void top_level_item() { abort_on_null=1; }
1367
};
1368
1369
1370
class Item_func_like :public Item_bool_func2
1371
{
1372
  // Turbo Boyer-Moore data
1373
  bool        canDoTurboBM;	// pattern is '%abcd%' case
1374
  const char* pattern;
1375
  int         pattern_len;
1376
1377
  // TurboBM buffers, *this is owner
1378
  int* bmGs; //   good suffix shift table, size is pattern_len + 1
1379
  int* bmBc; // bad character shift table, size is alphabet_size
1380
1381
  void turboBM_compute_suffixes(int* suff);
1382
  void turboBM_compute_good_suffix_shifts(int* suff);
1383
  void turboBM_compute_bad_character_shifts();
1384
  bool turboBM_matches(const char* text, int text_len) const;
1385
  enum { alphabet_size = 256 };
1386
1387
  Item *escape_item;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1388
1 by brian
clean slate
1389
  bool escape_used_in_parsing;
1390
1054.2.11 by Monty Taylor
Removed copy_and_convert.
1391
1 by brian
clean slate
1392
public:
1054.2.11 by Monty Taylor
Removed copy_and_convert.
1393
1394
  char *escape;
1 by brian
clean slate
1395
1396
  Item_func_like(Item *a,Item *b, Item *escape_arg, bool escape_used)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1397
    :Item_bool_func2(a,b), canDoTurboBM(false), pattern(0), pattern_len(0),
1 by brian
clean slate
1398
     bmGs(0), bmBc(0), escape_item(escape_arg),
1054.2.11 by Monty Taylor
Removed copy_and_convert.
1399
     escape_used_in_parsing(escape_used), escape(NULL) {}
152 by Brian Aker
longlong replacement
1400
  int64_t val_int();
1 by brian
clean slate
1401
  enum Functype functype() const { return LIKE_FUNC; }
1402
  optimize_type select_optimize() const;
1403
  cond_result eq_cmp_result() const { return COND_TRUE; }
1404
  const char *func_name() const { return "like"; }
520.1.22 by Brian Aker
Second pass of thd cleanup
1405
  bool fix_fields(Session *session, Item **ref);
1 by brian
clean slate
1406
  void cleanup();
1407
};
1408
1409
1410
typedef class Item COND;
1411
1412
class Item_cond :public Item_bool_func
1413
{
1414
protected:
1415
  List<Item> list;
1416
  bool abort_on_null;
1417
  table_map and_tables_cache;
1418
1419
public:
779.3.18 by Monty Taylor
Cleaned up warnings up through innodb.
1420
1421
  using Item::split_sum_func;
1422
1 by brian
clean slate
1423
  /* Item_cond() is only used to create top level items */
1424
  Item_cond(): Item_bool_func(), abort_on_null(1)
1425
  { const_item_cache=0; }
1426
  Item_cond(Item *i1,Item *i2)
1427
    :Item_bool_func(), abort_on_null(0)
1428
  {
1429
    list.push_back(i1);
1430
    list.push_back(i2);
1431
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1432
  Item_cond(Session *session, Item_cond *item);
1 by brian
clean slate
1433
  Item_cond(List<Item> &nlist)
1434
    :Item_bool_func(), list(nlist), abort_on_null(0) {}
1435
  bool add(Item *item) { return list.push_back(item); }
1436
  bool add_at_head(Item *item) { return list.push_front(item); }
1437
  void add_at_head(List<Item> *nlist) { list.prepand(nlist); }
520.1.21 by Brian Aker
THD -> Session rename
1438
  bool fix_fields(Session *, Item **ref);
846 by Brian Aker
Removing on typedeffed class.
1439
  void fix_after_pullout(Select_Lex *new_parent, Item **ref);
1 by brian
clean slate
1440
1441
  enum Type type() const { return COND_ITEM; }
1442
  List<Item>* argument_list() { return &list; }
1443
  table_map used_tables() const;
1444
  void update_used_tables();
1445
  virtual void print(String *str, enum_query_type query_type);
520.1.22 by Brian Aker
Second pass of thd cleanup
1446
  void split_sum_func(Session *session, Item **ref_pointer_array, List<Item> &fields);
1447
  friend int setup_conds(Session *session, TableList *tables, TableList *leaves,
1 by brian
clean slate
1448
                         COND **conds);
1449
  void top_level_item() { abort_on_null=1; }
520.1.22 by Brian Aker
Second pass of thd cleanup
1450
  void copy_andor_arguments(Session *session, Item_cond *item);
481 by Brian Aker
Remove all of uchar.
1451
  bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg);
1452
  Item *transform(Item_transformer transformer, unsigned char *arg);
1 by brian
clean slate
1453
  void traverse_cond(Cond_traverser, void *arg, traverse_order order);
520.1.22 by Brian Aker
Second pass of thd cleanup
1454
  void neg_arguments(Session *session);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1455
  enum_field_types field_type() const { return DRIZZLE_TYPE_LONGLONG; }
646 by Brian Aker
Next pass through attribute.
1456
  bool subst_argument_checker(unsigned char **)
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1457
  { return true; }
481 by Brian Aker
Remove all of uchar.
1458
  Item *compile(Item_analyzer analyzer, unsigned char **arg_p,
1459
                Item_transformer transformer, unsigned char *arg_t);
1 by brian
clean slate
1460
};
1461
1462
1463
/*
1464
  The class Item_equal is used to represent conjunctions of equality
1465
  predicates of the form field1 = field2, and field=const in where
1466
  conditions and on expressions.
1467
1468
  All equality predicates of the form field1=field2 contained in a
1469
  conjunction are substituted for a sequence of items of this class.
1470
  An item of this class Item_equal(f1,f2,...fk) represents a
1471
  multiple equality f1=f2=...=fk.
1472
1473
  If a conjunction contains predicates f1=f2 and f2=f3, a new item of
1474
  this class is created Item_equal(f1,f2,f3) representing the multiple
1475
  equality f1=f2=f3 that substitutes the above equality predicates in
1476
  the conjunction.
1477
  A conjunction of the predicates f2=f1 and f3=f1 and f3=f2 will be
1478
  substituted for the item representing the same multiple equality
1479
  f1=f2=f3.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1480
  An item Item_equal(f1,f2) can appear instead of a conjunction of
1 by brian
clean slate
1481
  f2=f1 and f1=f2, or instead of just the predicate f1=f2.
1482
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1483
  An item of the class Item_equal inherits equalities from outer
1 by brian
clean slate
1484
  conjunctive levels.
1485
1486
  Suppose we have a where condition of the following form:
1487
  WHERE f1=f2 AND f3=f4 AND f3=f5 AND ... AND (...OR (f1=f3 AND ...)).
1488
  In this case:
1489
    f1=f2 will be substituted for Item_equal(f1,f2);
1490
    f3=f4 and f3=f5  will be substituted for Item_equal(f3,f4,f5);
1491
    f1=f3 will be substituted for Item_equal(f1,f2,f3,f4,f5);
1492
1493
  An object of the class Item_equal can contain an optional constant
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1494
  item c. Then it represents a multiple equality of the form
1 by brian
clean slate
1495
  c=f1=...=fk.
1496
1497
  Objects of the class Item_equal are used for the following:
1498
1499
  1. An object Item_equal(t1.f1,...,tk.fk) allows us to consider any
1500
  pair of tables ti and tj as joined by an equi-condition.
1501
  Thus it provide us with additional access paths from table to table.
1502
1503
  2. An object Item_equal(t1.f1,...,tk.fk) is applied to deduce new
1504
  SARGable predicates:
1505
    f1=...=fk AND P(fi) => f1=...=fk AND P(fi) AND P(fj).
1506
  It also can give us additional index scans and can allow us to
1507
  improve selectivity estimates.
1508
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1509
  3. An object Item_equal(t1.f1,...,tk.fk) is used to optimize the
1510
  selected execution plan for the query: if table ti is accessed
1 by brian
clean slate
1511
  before the table tj then in any predicate P in the where condition
1512
  the occurrence of tj.fj is substituted for ti.fi. This can allow
1513
  an evaluation of the predicate at an earlier step.
1514
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1515
  When feature 1 is supported they say that join transitive closure
1 by brian
clean slate
1516
  is employed.
1517
  When feature 2 is supported they say that search argument transitive
1518
  closure is employed.
1519
  Both features are usually supported by preprocessing original query and
1520
  adding additional predicates.
1521
  We do not just add predicates, we rather dynamically replace some
1522
  predicates that can not be used to access tables in the investigated
1523
  plan for those, obtained by substitution of some fields for equal fields,
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1524
  that can be used.
1 by brian
clean slate
1525
1526
  Prepared Statements/Stored Procedures note: instances of class
1527
  Item_equal are created only at the time a PS/SP is executed and
1528
  are deleted in the end of execution. All changes made to these
1529
  objects need not be registered in the list of changes of the parse
1530
  tree and do not harm PS/SP re-execution.
1531
1532
  Item equal objects are employed only at the optimize phase. Usually they are
1533
  not supposed to be evaluated.  Yet in some cases we call the method val_int()
1534
  for them. We have to take care of restricting the predicate such an
1535
  object represents f1=f2= ...=fn to the projection of known fields fi1=...=fik.
1536
*/
1537
1538
class Item_equal: public Item_bool_func
1539
{
1540
  List<Item_field> fields; /* list of equal field items                    */
1541
  Item *const_item;        /* optional constant item equal to fields items */
1542
  cmp_item *eval_item;
1543
  bool cond_false;
1544
public:
1545
  inline Item_equal()
1546
    : Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
1547
  { const_item_cache=0 ;}
1548
  Item_equal(Item_field *f1, Item_field *f2);
1549
  Item_equal(Item *c, Item_field *f);
1550
  Item_equal(Item_equal *item_equal);
1551
  inline Item* get_const() { return const_item; }
1552
  void add(Item *c);
1553
  void add(Item_field *f);
482 by Brian Aker
Remove uint.
1554
  uint32_t members();
1 by brian
clean slate
1555
  bool contains(Field *field);
1556
  Item_field* get_first() { return fields.head(); }
1557
  void merge(Item_equal *item);
1558
  void update_const();
1559
  enum Functype functype() const { return MULT_EQUAL_FUNC; }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1560
  int64_t val_int();
1 by brian
clean slate
1561
  const char *func_name() const { return "multiple equal"; }
1562
  optimize_type select_optimize() const { return OPTIMIZE_EQUAL; }
1563
  void sort(Item_field_cmpfunc cmp, void *arg);
1564
  friend class Item_equal_iterator;
1565
  void fix_length_and_dec();
520.1.22 by Brian Aker
Second pass of thd cleanup
1566
  bool fix_fields(Session *session, Item **ref);
1 by brian
clean slate
1567
  void update_used_tables();
481 by Brian Aker
Remove all of uchar.
1568
  bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg);
1569
  Item *transform(Item_transformer transformer, unsigned char *arg);
1 by brian
clean slate
1570
  virtual void print(String *str, enum_query_type query_type);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1571
  const CHARSET_INFO *compare_collation()
1 by brian
clean slate
1572
  { return fields.head()->collation.collation; }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1573
};
1 by brian
clean slate
1574
1575
class COND_EQUAL: public Sql_alloc
1576
{
1577
public:
482 by Brian Aker
Remove uint.
1578
  uint32_t max_members;               /* max number of members the current level
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1579
                                     list and all lower level lists */
1 by brian
clean slate
1580
  COND_EQUAL *upper_levels;       /* multiple equalities of upper and levels */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1581
  List<Item_equal> current_level; /* list of multiple equalities of
1 by brian
clean slate
1582
                                     the current and level           */
1583
  COND_EQUAL()
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1584
  {
1 by brian
clean slate
1585
    upper_levels= 0;
1586
  }
1587
};
1588
1589
1590
class Item_equal_iterator : public List_iterator_fast<Item_field>
1591
{
1592
public:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1593
  inline Item_equal_iterator(Item_equal &item_equal)
1 by brian
clean slate
1594
    :List_iterator_fast<Item_field> (item_equal.fields)
1595
  {}
1596
  inline Item_field* operator++(int)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1597
  {
1 by brian
clean slate
1598
    Item_field *item= (*(List_iterator_fast<Item_field> *) this)++;
1599
    return  item;
1600
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1601
  inline void rewind(void)
1602
  {
1 by brian
clean slate
1603
    List_iterator_fast<Item_field>::rewind();
1604
  }
1605
};
1606
1607
class Item_cond_and :public Item_cond
1608
{
1609
public:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1610
  COND_EQUAL cond_equal;  /* contains list of Item_equal objects for
1 by brian
clean slate
1611
                             the current and level and reference
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1612
                             to multiple equalities of upper and levels */
1 by brian
clean slate
1613
  Item_cond_and() :Item_cond() {}
1614
  Item_cond_and(Item *i1,Item *i2) :Item_cond(i1,i2) {}
520.1.22 by Brian Aker
Second pass of thd cleanup
1615
  Item_cond_and(Session *session, Item_cond_and *item) :Item_cond(session, item) {}
1 by brian
clean slate
1616
  Item_cond_and(List<Item> &list_arg): Item_cond(list_arg) {}
1617
  enum Functype functype() const { return COND_AND_FUNC; }
152 by Brian Aker
longlong replacement
1618
  int64_t val_int();
1 by brian
clean slate
1619
  const char *func_name() const { return "and"; }
1620
  table_map not_null_tables() const
1621
  { return abort_on_null ? not_null_tables_cache: and_tables_cache; }
520.1.22 by Brian Aker
Second pass of thd cleanup
1622
  Item* copy_andor_structure(Session *session)
1 by brian
clean slate
1623
  {
1624
    Item_cond_and *item;
520.1.22 by Brian Aker
Second pass of thd cleanup
1625
    if ((item= new Item_cond_and(session, this)))
1626
       item->copy_andor_arguments(session, this);
1 by brian
clean slate
1627
    return item;
1628
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1629
  Item *neg_transformer(Session *session);
1 by brian
clean slate
1630
};
1631
1632
inline bool is_cond_and(Item *item)
1633
{
1634
  if (item->type() != Item::COND_ITEM)
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1635
    return false;
1 by brian
clean slate
1636
1637
  Item_cond *cond_item= (Item_cond*) item;
1638
  return (cond_item->functype() == Item_func::COND_AND_FUNC);
1639
}
1640
1641
class Item_cond_or :public Item_cond
1642
{
1643
public:
1644
  Item_cond_or() :Item_cond() {}
1645
  Item_cond_or(Item *i1,Item *i2) :Item_cond(i1,i2) {}
520.1.22 by Brian Aker
Second pass of thd cleanup
1646
  Item_cond_or(Session *session, Item_cond_or *item) :Item_cond(session, item) {}
1 by brian
clean slate
1647
  Item_cond_or(List<Item> &list_arg): Item_cond(list_arg) {}
1648
  enum Functype functype() const { return COND_OR_FUNC; }
152 by Brian Aker
longlong replacement
1649
  int64_t val_int();
1 by brian
clean slate
1650
  const char *func_name() const { return "or"; }
1651
  table_map not_null_tables() const { return and_tables_cache; }
520.1.22 by Brian Aker
Second pass of thd cleanup
1652
  Item* copy_andor_structure(Session *session)
1 by brian
clean slate
1653
  {
1654
    Item_cond_or *item;
520.1.22 by Brian Aker
Second pass of thd cleanup
1655
    if ((item= new Item_cond_or(session, this)))
1656
      item->copy_andor_arguments(session, this);
1 by brian
clean slate
1657
    return item;
1658
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1659
  Item *neg_transformer(Session *session);
1 by brian
clean slate
1660
};
1661
1662
inline bool is_cond_or(Item *item)
1663
{
1664
  if (item->type() != Item::COND_ITEM)
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1665
    return false;
1 by brian
clean slate
1666
1667
  Item_cond *cond_item= (Item_cond*) item;
1668
  return (cond_item->functype() == Item_func::COND_OR_FUNC);
1669
}
1670
1671
/*
1672
  XOR is Item_cond, not an Item_int_func because we could like to
1673
  optimize (a XOR b) later on. It's low prio, though
1674
*/
1675
1676
class Item_cond_xor :public Item_cond
1677
{
1678
public:
1679
  Item_cond_xor() :Item_cond() {}
1680
  Item_cond_xor(Item *i1,Item *i2) :Item_cond(i1,i2) {}
1681
  enum Functype functype() const { return COND_XOR_FUNC; }
1682
  /* TODO: remove the next line when implementing XOR optimization */
1683
  enum Type type() const { return FUNC_ITEM; }
152 by Brian Aker
longlong replacement
1684
  int64_t val_int();
1 by brian
clean slate
1685
  const char *func_name() const { return "xor"; }
1686
  void top_level_item() {}
1687
};
1688
1689
1690
/* Some useful inline functions */
1691
1692
inline Item *and_conds(Item *a, Item *b)
1693
{
1694
  if (!b) return a;
1695
  if (!a) return b;
1696
  return new Item_cond_and(a, b);
1697
}
1698
1699
Item *and_expressions(Item *a, Item *b, Item **org_item);
575.1.6 by Monty Taylor
Cleaned up some headers for PCH.
1700
1701
#endif /* DRIZZLED_ITEM_CMPFUNC_H */