~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
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
25
#include <drizzled/comp_creator.h>
26
#include <drizzled/item/row.h>
27
#include <drizzled/item/sum.h>
642.1.21 by Lee
header file clean up
28
#include <drizzled/item/int.h>
670.1.22 by Monty Taylor
Merged from Lee.
29
#include <drizzled/item/float.h>
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
30
#include <drizzled/function/int.h>
31
#include <drizzled/function/numhybrid.h>
670.2.1 by Monty Taylor
Moved pthread keys
32
#include <drizzled/session.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
33
1 by brian
clean slate
34
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"; }
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
696
  Field *tmp_table_field(Table *table);
482 by Brian Aker
Remove uint.
697
  uint32_t decimal_precision() const;
1 by brian
clean slate
698
};
699
700
701
class Item_func_if :public Item_func
702
{
703
  enum Item_result cached_result_type;
704
  enum_field_types cached_field_type;
705
public:
706
  Item_func_if(Item *a,Item *b,Item *c)
707
    :Item_func(a,b,c), cached_result_type(INT_RESULT)
708
  {}
709
  double val_real();
152 by Brian Aker
longlong replacement
710
  int64_t val_int();
1 by brian
clean slate
711
  String *val_str(String *str);
712
  my_decimal *val_decimal(my_decimal *);
713
  enum Item_result result_type () const { return cached_result_type; }
714
  enum_field_types field_type() const { return cached_field_type; }
520.1.21 by Brian Aker
THD -> Session rename
715
  bool fix_fields(Session *, Item **);
1 by brian
clean slate
716
  void fix_length_and_dec();
482 by Brian Aker
Remove uint.
717
  uint32_t decimal_precision() const;
1 by brian
clean slate
718
  const char *func_name() const { return "if"; }
719
};
720
721
722
class Item_func_nullif :public Item_bool_func2
723
{
724
  enum Item_result cached_result_type;
725
public:
726
  Item_func_nullif(Item *a,Item *b)
727
    :Item_bool_func2(a,b), cached_result_type(INT_RESULT)
728
  {}
729
  double val_real();
152 by Brian Aker
longlong replacement
730
  int64_t val_int();
1 by brian
clean slate
731
  String *val_str(String *str);
732
  my_decimal *val_decimal(my_decimal *);
733
  enum Item_result result_type () const { return cached_result_type; }
734
  void fix_length_and_dec();
482 by Brian Aker
Remove uint.
735
  uint32_t decimal_precision() const { return args[0]->decimal_precision(); }
1 by brian
clean slate
736
  const char *func_name() const { return "nullif"; }
737
738
  virtual inline void print(String *str, enum_query_type query_type)
739
  {
740
    Item_func::print(str, query_type);
741
  }
742
743
  table_map not_null_tables() const { return 0; }
744
  bool is_null();
745
};
746
747
748
/* Functions to handle the optimized IN */
749
750
751
/* A vector of values of some type  */
752
753
class in_vector :public Sql_alloc
754
{
755
public:
756
  char *base;
482 by Brian Aker
Remove uint.
757
  uint32_t size;
1 by brian
clean slate
758
  qsort2_cmp compare;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
759
  const CHARSET_INFO *collation;
482 by Brian Aker
Remove uint.
760
  uint32_t count;
761
  uint32_t used_count;
1 by brian
clean slate
762
  in_vector() {}
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
763
  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.
764
  	    const CHARSET_INFO * const cmp_coll)
1 by brian
clean slate
765
    :base((char*) sql_calloc(elements*element_length)),
766
     size(element_length), compare(cmp_func), collation(cmp_coll),
767
     count(elements), used_count(elements) {}
768
  virtual ~in_vector() {}
482 by Brian Aker
Remove uint.
769
  virtual void set(uint32_t pos,Item *item)=0;
481 by Brian Aker
Remove all of uchar.
770
  virtual unsigned char *get_value(Item *item)=0;
1 by brian
clean slate
771
  void sort()
772
  {
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
773
    my_qsort2(base,used_count,size,compare, (void *) collation);
1 by brian
clean slate
774
  }
775
  int find(Item *item);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
776
777
  /*
1 by brian
clean slate
778
    Create an instance of Item_{type} (e.g. Item_decimal) constant object
779
    which type allows it to hold an element of this vector without any
780
    conversions.
781
    The purpose of this function is to be able to get elements of this
782
    vector in form of Item_xxx constants without creating Item_xxx object
783
    for every array element you get (i.e. this implements "FlyWeight" pattern)
784
  */
785
  virtual Item* create_item() { return NULL; }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
786
1 by brian
clean slate
787
  /*
788
    Store the value at position #pos into provided item object
789
    SYNOPSIS
790
      value_to_item()
791
        pos   Index of value to store
792
        item  Constant item to store value into. The item must be of the same
793
              type that create_item() returns.
794
  */
646 by Brian Aker
Next pass through attribute.
795
  virtual void value_to_item(uint32_t, Item *) { }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
796
1 by brian
clean slate
797
  /* Compare values number pos1 and pos2 for equality */
482 by Brian Aker
Remove uint.
798
  bool compare_elems(uint32_t pos1, uint32_t pos2)
1 by brian
clean slate
799
  {
800
    return test(compare(collation, base + pos1*size, base + pos2*size));
801
  }
802
  virtual Item_result result_type()= 0;
803
};
804
805
class in_string :public in_vector
806
{
807
  char buff[STRING_BUFFER_USUAL_SIZE];
808
  String tmp;
809
public:
482 by Brian Aker
Remove uint.
810
  in_string(uint32_t elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs);
1 by brian
clean slate
811
  ~in_string();
482 by Brian Aker
Remove uint.
812
  void set(uint32_t pos,Item *item);
481 by Brian Aker
Remove all of uchar.
813
  unsigned char *get_value(Item *item);
1 by brian
clean slate
814
  Item* create_item()
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
815
  {
1 by brian
clean slate
816
    return new Item_string(collation);
817
  }
482 by Brian Aker
Remove uint.
818
  void value_to_item(uint32_t pos, Item *item)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
819
  {
1 by brian
clean slate
820
    String *str=((String*) base)+pos;
821
    Item_string *to= (Item_string*)item;
822
    to->str_value= *str;
823
  }
824
  Item_result result_type() { return STRING_RESULT; }
825
};
826
152 by Brian Aker
longlong replacement
827
class in_int64_t :public in_vector
1 by brian
clean slate
828
{
829
protected:
830
  /*
831
    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:
832
    elements of this vector. tmp is used in finding if a given value is in
833
    the list.
1 by brian
clean slate
834
  */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
835
  struct packed_int64_t
1 by brian
clean slate
836
  {
152 by Brian Aker
longlong replacement
837
    int64_t val;
838
    int64_t unsigned_flag;  // Use int64_t, not bool, to preserve alignment
1 by brian
clean slate
839
  } tmp;
840
public:
482 by Brian Aker
Remove uint.
841
  in_int64_t(uint32_t elements);
842
  void set(uint32_t pos,Item *item);
481 by Brian Aker
Remove all of uchar.
843
  unsigned char *get_value(Item *item);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
844
1 by brian
clean slate
845
  Item* create_item()
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
846
  {
847
    /*
848
      We're created a signed INT, this may not be correct in
1 by brian
clean slate
849
      general case (see BUG#19342).
850
    */
152 by Brian Aker
longlong replacement
851
    return new Item_int((int64_t)0);
1 by brian
clean slate
852
  }
482 by Brian Aker
Remove uint.
853
  void value_to_item(uint32_t pos, Item *item)
1 by brian
clean slate
854
  {
152 by Brian Aker
longlong replacement
855
    ((Item_int*) item)->value= ((packed_int64_t*) base)[pos].val;
275 by Brian Aker
Full removal of my_bool from central server.
856
    ((Item_int*) item)->unsigned_flag= (bool)
152 by Brian Aker
longlong replacement
857
      ((packed_int64_t*) base)[pos].unsigned_flag;
1 by brian
clean slate
858
  }
859
  Item_result result_type() { return INT_RESULT; }
860
152 by Brian Aker
longlong replacement
861
  friend int cmp_int64_t(void *cmp_arg, packed_int64_t *a,packed_int64_t *b);
1 by brian
clean slate
862
};
863
864
865
/*
866
  Class to represent a vector of constant DATE/DATETIME values.
867
  Values are obtained with help of the get_datetime_value() function.
868
  If the left item is a constant one then its value is cached in the
869
  lval_cache variable.
870
*/
152 by Brian Aker
longlong replacement
871
class in_datetime :public in_int64_t
1 by brian
clean slate
872
{
873
public:
520.1.22 by Brian Aker
Second pass of thd cleanup
874
  Session *session;
1 by brian
clean slate
875
  /* An item used to issue warnings. */
876
  Item *warn_item;
877
  /* Cache for the left item. */
878
  Item *lval_cache;
879
482 by Brian Aker
Remove uint.
880
  in_datetime(Item *warn_item_arg, uint32_t elements)
520.1.22 by Brian Aker
Second pass of thd cleanup
881
    :in_int64_t(elements), session(current_session), warn_item(warn_item_arg),
1 by brian
clean slate
882
     lval_cache(0) {};
482 by Brian Aker
Remove uint.
883
  void set(uint32_t pos,Item *item);
481 by Brian Aker
Remove all of uchar.
884
  unsigned char *get_value(Item *item);
152 by Brian Aker
longlong replacement
885
  friend int cmp_int64_t(void *cmp_arg, packed_int64_t *a,packed_int64_t *b);
1 by brian
clean slate
886
};
887
888
889
class in_double :public in_vector
890
{
891
  double tmp;
892
public:
482 by Brian Aker
Remove uint.
893
  in_double(uint32_t elements);
894
  void set(uint32_t pos,Item *item);
481 by Brian Aker
Remove all of uchar.
895
  unsigned char *get_value(Item *item);
1 by brian
clean slate
896
  Item *create_item()
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
897
  {
1 by brian
clean slate
898
    return new Item_float(0.0, 0);
899
  }
482 by Brian Aker
Remove uint.
900
  void value_to_item(uint32_t pos, Item *item)
1 by brian
clean slate
901
  {
902
    ((Item_float*)item)->value= ((double*) base)[pos];
903
  }
904
  Item_result result_type() { return REAL_RESULT; }
905
};
906
907
908
class in_decimal :public in_vector
909
{
910
  my_decimal val;
911
public:
482 by Brian Aker
Remove uint.
912
  in_decimal(uint32_t elements);
913
  void set(uint32_t pos, Item *item);
481 by Brian Aker
Remove all of uchar.
914
  unsigned char *get_value(Item *item);
1 by brian
clean slate
915
  Item *create_item()
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
916
  {
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
917
    return new Item_decimal(0, false);
1 by brian
clean slate
918
  }
482 by Brian Aker
Remove uint.
919
  void value_to_item(uint32_t pos, Item *item)
1 by brian
clean slate
920
  {
921
    my_decimal *dec= ((my_decimal *)base) + pos;
922
    Item_decimal *item_dec= (Item_decimal*)item;
923
    item_dec->set_decimal_value(dec);
924
  }
925
  Item_result result_type() { return DECIMAL_RESULT; }
926
927
};
928
929
930
/*
931
** Classes for easy comparing of non const items
932
*/
933
934
class cmp_item :public Sql_alloc
935
{
936
public:
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
937
  const CHARSET_INFO *cmp_charset;
1 by brian
clean slate
938
  cmp_item() { cmp_charset= &my_charset_bin; }
939
  virtual ~cmp_item() {}
940
  virtual void store_value(Item *item)= 0;
941
  virtual int cmp(Item *item)= 0;
942
  // for optimized IN with row
943
  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.
944
  static cmp_item* get_comparator(Item_result type, const CHARSET_INFO * const cs);
1 by brian
clean slate
945
  virtual cmp_item *make_same()= 0;
646 by Brian Aker
Next pass through attribute.
946
  virtual void store_value_by_template(cmp_item *, Item *item)
1 by brian
clean slate
947
  {
948
    store_value(item);
949
  }
950
};
951
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
952
class cmp_item_string :public cmp_item
1 by brian
clean slate
953
{
954
protected:
955
  String *value_res;
956
public:
957
  cmp_item_string () {}
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
958
  cmp_item_string (const CHARSET_INFO * const cs) { cmp_charset= cs; }
959
  void set_charset(const CHARSET_INFO * const cs) { cmp_charset= cs; }
1 by brian
clean slate
960
  friend class cmp_item_sort_string;
961
  friend class cmp_item_sort_string_in_static;
962
};
963
964
class cmp_item_sort_string :public cmp_item_string
965
{
966
protected:
967
  char value_buff[STRING_BUFFER_USUAL_SIZE];
968
  String value;
969
public:
970
  cmp_item_sort_string():
971
    cmp_item_string() {}
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
972
  cmp_item_sort_string(const CHARSET_INFO * const cs):
1 by brian
clean slate
973
    cmp_item_string(cs),
974
    value(value_buff, sizeof(value_buff), cs) {}
975
  void store_value(Item *item)
976
  {
977
    value_res= item->val_str(&value);
978
  }
979
  int cmp(Item *arg)
980
  {
981
    char buff[STRING_BUFFER_USUAL_SIZE];
982
    String tmp(buff, sizeof(buff), cmp_charset), *res;
983
    res= arg->val_str(&tmp);
984
    return (value_res ? (res ? sortcmp(value_res, res, cmp_charset) : 1) :
985
            (res ? -1 : 0));
986
  }
987
  int compare(cmp_item *ci)
988
  {
989
    cmp_item_string *l_cmp= (cmp_item_string *) ci;
990
    return sortcmp(value_res, l_cmp->value_res, cmp_charset);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
991
  }
1 by brian
clean slate
992
  cmp_item *make_same();
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
993
  void set_charset(const CHARSET_INFO * const cs)
1 by brian
clean slate
994
  {
995
    cmp_charset= cs;
996
    value.set_quick(value_buff, sizeof(value_buff), cs);
997
  }
998
};
999
1000
class cmp_item_int :public cmp_item
1001
{
152 by Brian Aker
longlong replacement
1002
  int64_t value;
1 by brian
clean slate
1003
public:
1004
  cmp_item_int() {}                           /* Remove gcc warning */
1005
  void store_value(Item *item)
1006
  {
1007
    value= item->val_int();
1008
  }
1009
  int cmp(Item *arg)
1010
  {
1011
    return value != arg->val_int();
1012
  }
1013
  int compare(cmp_item *ci)
1014
  {
1015
    cmp_item_int *l_cmp= (cmp_item_int *)ci;
1016
    return (value < l_cmp->value) ? -1 : ((value == l_cmp->value) ? 0 : 1);
1017
  }
1018
  cmp_item *make_same();
1019
};
1020
1021
/*
1022
  Compare items in the DATETIME context.
1023
  Values are obtained with help of the get_datetime_value() function.
1024
  If the left item is a constant one then its value is cached in the
1025
  lval_cache variable.
1026
*/
1027
class cmp_item_datetime :public cmp_item
1028
{
151 by Brian Aker
Ulonglong to uint64_t
1029
  uint64_t value;
1 by brian
clean slate
1030
public:
520.1.22 by Brian Aker
Second pass of thd cleanup
1031
  Session *session;
1 by brian
clean slate
1032
  /* Item used for issuing warnings. */
1033
  Item *warn_item;
1034
  /* Cache for the left item. */
1035
  Item *lval_cache;
1036
1037
  cmp_item_datetime(Item *warn_item_arg)
520.1.22 by Brian Aker
Second pass of thd cleanup
1038
    :session(current_session), warn_item(warn_item_arg), lval_cache(0) {}
1 by brian
clean slate
1039
  void store_value(Item *item);
1040
  int cmp(Item *arg);
1041
  int compare(cmp_item *ci);
1042
  cmp_item *make_same();
1043
};
1044
1045
class cmp_item_real :public cmp_item
1046
{
1047
  double value;
1048
public:
1049
  cmp_item_real() {}                          /* Remove gcc warning */
1050
  void store_value(Item *item)
1051
  {
1052
    value= item->val_real();
1053
  }
1054
  int cmp(Item *arg)
1055
  {
1056
    return value != arg->val_real();
1057
  }
1058
  int compare(cmp_item *ci)
1059
  {
1060
    cmp_item_real *l_cmp= (cmp_item_real *) ci;
1061
    return (value < l_cmp->value)? -1 : ((value == l_cmp->value) ? 0 : 1);
1062
  }
1063
  cmp_item *make_same();
1064
};
1065
1066
1067
class cmp_item_decimal :public cmp_item
1068
{
1069
  my_decimal value;
1070
public:
1071
  cmp_item_decimal() {}                       /* Remove gcc warning */
1072
  void store_value(Item *item);
1073
  int cmp(Item *arg);
1074
  int compare(cmp_item *c);
1075
  cmp_item *make_same();
1076
};
1077
1078
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1079
/*
1 by brian
clean slate
1080
   cmp_item for optimized IN with row (right part string, which never
1081
   be changed)
1082
*/
1083
1084
class cmp_item_sort_string_in_static :public cmp_item_string
1085
{
1086
 protected:
1087
  String value;
1088
public:
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1089
  cmp_item_sort_string_in_static(const CHARSET_INFO * const cs):
1 by brian
clean slate
1090
    cmp_item_string(cs) {}
1091
  void store_value(Item *item)
1092
  {
1093
    value_res= item->val_str(&value);
1094
  }
646 by Brian Aker
Next pass through attribute.
1095
  int cmp(Item *)
1 by brian
clean slate
1096
  {
1097
    // Should never be called
51.1.17 by Jay Pipes
Removed/replaced DBUG symbols
1098
    assert(0);
1 by brian
clean slate
1099
    return 1;
1100
  }
1101
  int compare(cmp_item *ci)
1102
  {
1103
    cmp_item_string *l_cmp= (cmp_item_string *) ci;
1104
    return sortcmp(value_res, l_cmp->value_res, cmp_charset);
1105
  }
1106
  cmp_item *make_same()
1107
  {
1108
    return new cmp_item_sort_string_in_static(cmp_charset);
1109
  }
1110
};
1111
1112
1113
/*
1114
  The class Item_func_case is the CASE ... WHEN ... THEN ... END function
1115
  implementation.
1116
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1117
  When there is no expression between CASE and the first WHEN
1 by brian
clean slate
1118
  (the CASE expression) then this function simple checks all WHEN expressions
1119
  one after another. When some WHEN expression evaluated to TRUE then the
1120
  value of the corresponding THEN expression is returned.
1121
1122
  When the CASE expression is specified then it is compared to each WHEN
1123
  expression individually. When an equal WHEN expression is found
1124
  corresponding THEN expression is returned.
1125
  In order to do correct comparisons several comparators are used. One for
1126
  each result type. Different result types that are used in particular
1127
  CASE ... END expression are collected in the fix_length_and_dec() member
1128
  function and only comparators for there result types are used.
1129
*/
1130
1131
class Item_func_case :public Item_func
1132
{
1133
  int first_expr_num, else_expr_num;
1134
  enum Item_result cached_result_type, left_result_type;
1135
  String tmp_value;
482 by Brian Aker
Remove uint.
1136
  uint32_t ncases;
1 by brian
clean slate
1137
  Item_result cmp_type;
1138
  DTCollation cmp_collation;
1139
  enum_field_types cached_field_type;
1140
  cmp_item *cmp_items[5]; /* For all result types */
1141
  cmp_item *case_item;
1142
public:
1143
  Item_func_case(List<Item> &list, Item *first_expr_arg, Item *else_expr_arg)
1144
    :Item_func(), first_expr_num(-1), else_expr_num(-1),
1145
    cached_result_type(INT_RESULT), left_result_type(INT_RESULT), case_item(0)
1146
  {
1147
    ncases= list.elements;
1148
    if (first_expr_arg)
1149
    {
1150
      first_expr_num= list.elements;
1151
      list.push_back(first_expr_arg);
1152
    }
1153
    if (else_expr_arg)
1154
    {
1155
      else_expr_num= list.elements;
1156
      list.push_back(else_expr_arg);
1157
    }
1158
    set_arguments(list);
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
1159
    memset(&cmp_items, 0, sizeof(cmp_items));
1 by brian
clean slate
1160
  }
1161
  double val_real();
152 by Brian Aker
longlong replacement
1162
  int64_t val_int();
1 by brian
clean slate
1163
  String *val_str(String *);
1164
  my_decimal *val_decimal(my_decimal *);
520.1.22 by Brian Aker
Second pass of thd cleanup
1165
  bool fix_fields(Session *session, Item **ref);
1 by brian
clean slate
1166
  void fix_length_and_dec();
482 by Brian Aker
Remove uint.
1167
  uint32_t decimal_precision() const;
1 by brian
clean slate
1168
  table_map not_null_tables() const { return 0; }
1169
  enum Item_result result_type () const { return cached_result_type; }
1170
  enum_field_types field_type() const { return cached_field_type; }
1171
  const char *func_name() const { return "case"; }
1172
  virtual void print(String *str, enum_query_type query_type);
1173
  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.
1174
  const CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
1 by brian
clean slate
1175
  void cleanup();
1176
  void agg_str_lengths(Item *arg);
1177
  void agg_num_lengths(Item *arg);
1178
};
1179
1180
/*
1181
  The Item_func_in class implements the in_expr IN(values_list) function.
1182
1183
  The current implementation distinguishes 2 cases:
1184
  1) all items in the value_list are constants and have the same
1185
    result type. This case is handled by in_vector class.
1186
  2) items in the value_list have different result types or there is some
1187
    non-constant items.
1188
    In this case Item_func_in employs several cmp_item objects to performs
1189
    comparisons of in_expr and an item from the values_list. One cmp_item
1190
    object for each result type. Different result types are collected in the
1191
    fix_length_and_dec() member function by means of collect_cmp_types()
1192
    function.
1193
*/
1194
class Item_func_in :public Item_func_opt_neg
1195
{
1196
public:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1197
  /*
1 by brian
clean slate
1198
    an array of values when the right hand arguments of IN
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1199
    are all SQL constant and there are no nulls
1 by brian
clean slate
1200
  */
1201
  in_vector *array;
1202
  bool have_null;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1203
  /*
1 by brian
clean slate
1204
    true when all arguments of the IN clause are of compatible types
1205
    and can be used safely as comparisons for key conditions
1206
  */
1207
  bool arg_types_compatible;
1208
  Item_result left_result_type;
1209
  cmp_item *cmp_items[6]; /* One cmp_item for each result type */
1210
  DTCollation cmp_collation;
1211
1212
  Item_func_in(List<Item> &list)
1213
    :Item_func_opt_neg(list), array(0), have_null(0),
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1214
    arg_types_compatible(false)
1 by brian
clean slate
1215
  {
212.6.1 by Mats Kindahl
Replacing all bzero() calls with memset() calls and removing the bzero.c file.
1216
    memset(&cmp_items, 0, sizeof(cmp_items));
1 by brian
clean slate
1217
    allowed_arg_cols= 0;  // Fetch this value from first argument
1218
  }
152 by Brian Aker
longlong replacement
1219
  int64_t val_int();
520.1.21 by Brian Aker
THD -> Session rename
1220
  bool fix_fields(Session *, Item **);
1 by brian
clean slate
1221
  void fix_length_and_dec();
482 by Brian Aker
Remove uint.
1222
  uint32_t decimal_precision() const { return 1; }
1 by brian
clean slate
1223
  void cleanup()
1224
  {
482 by Brian Aker
Remove uint.
1225
    uint32_t i;
1 by brian
clean slate
1226
    Item_int_func::cleanup();
1227
    delete array;
1228
    array= 0;
1229
    for (i= 0; i <= (uint)DECIMAL_RESULT + 1; i++)
1230
    {
1231
      delete cmp_items[i];
1232
      cmp_items[i]= 0;
1233
    }
51.1.17 by Jay Pipes
Removed/replaced DBUG symbols
1234
    return;
1 by brian
clean slate
1235
  }
1236
  optimize_type select_optimize() const
1237
    { return OPTIMIZE_KEY; }
1238
  virtual void print(String *str, enum_query_type query_type);
1239
  enum Functype functype() const { return IN_FUNC; }
1240
  const char *func_name() const { return " IN "; }
1241
  bool nulls_in_row();
1242
  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.
1243
  const CHARSET_INFO *compare_collation() { return cmp_collation.collation; }
1 by brian
clean slate
1244
};
1245
1246
class cmp_item_row :public cmp_item
1247
{
1248
  cmp_item **comparators;
482 by Brian Aker
Remove uint.
1249
  uint32_t n;
1 by brian
clean slate
1250
public:
1251
  cmp_item_row(): comparators(0), n(0) {}
1252
  ~cmp_item_row();
1253
  void store_value(Item *item);
1254
  inline void alloc_comparators();
1255
  int cmp(Item *arg);
1256
  int compare(cmp_item *arg);
1257
  cmp_item *make_same();
1258
  void store_value_by_template(cmp_item *tmpl, Item *);
1259
  friend void Item_func_in::fix_length_and_dec();
1260
};
1261
1262
1263
class in_row :public in_vector
1264
{
1265
  cmp_item_row tmp;
1266
public:
482 by Brian Aker
Remove uint.
1267
  in_row(uint32_t elements, Item *);
1 by brian
clean slate
1268
  ~in_row();
482 by Brian Aker
Remove uint.
1269
  void set(uint32_t pos,Item *item);
481 by Brian Aker
Remove all of uchar.
1270
  unsigned char *get_value(Item *item);
1 by brian
clean slate
1271
  friend void Item_func_in::fix_length_and_dec();
1272
  Item_result result_type() { return ROW_RESULT; }
1273
};
1274
1275
/* Functions used by where clause */
1276
1277
class Item_func_isnull :public Item_bool_func
1278
{
1279
protected:
152 by Brian Aker
longlong replacement
1280
  int64_t cached_value;
1 by brian
clean slate
1281
public:
1282
  Item_func_isnull(Item *a) :Item_bool_func(a) {}
152 by Brian Aker
longlong replacement
1283
  int64_t val_int();
1 by brian
clean slate
1284
  enum Functype functype() const { return ISNULL_FUNC; }
1285
  void fix_length_and_dec()
1286
  {
1287
    decimals=0; max_length=1; maybe_null=0;
1288
    update_used_tables();
1289
  }
1290
  const char *func_name() const { return "isnull"; }
1291
  /* Optimize case of not_null_column IS NULL */
1292
  virtual void update_used_tables()
1293
  {
1294
    if (!args[0]->maybe_null)
1295
    {
1296
      used_tables_cache= 0;			/* is always false */
1297
      const_item_cache= 1;
152 by Brian Aker
longlong replacement
1298
      cached_value= (int64_t) 0;
1 by brian
clean slate
1299
    }
1300
    else
1301
    {
1302
      args[0]->update_used_tables();
1303
      if ((const_item_cache= !(used_tables_cache= args[0]->used_tables())) &&
1304
          !with_subselect)
1305
      {
1306
	/* Remember if the value is always NULL or never NULL */
152 by Brian Aker
longlong replacement
1307
	cached_value= (int64_t) args[0]->is_null();
1 by brian
clean slate
1308
      }
1309
    }
1310
  }
1311
  table_map not_null_tables() const { return 0; }
1312
  optimize_type select_optimize() const { return OPTIMIZE_NULL; }
520.1.22 by Brian Aker
Second pass of thd cleanup
1313
  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.
1314
  const CHARSET_INFO *compare_collation() { return args[0]->collation.collation; }
1 by brian
clean slate
1315
};
1316
1317
/* Functions used by HAVING for rewriting IN subquery */
1318
1319
class Item_in_subselect;
1320
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1321
/*
1 by brian
clean slate
1322
  This is like IS NOT NULL but it also remembers if it ever has
1323
  encountered a NULL.
1324
*/
1325
class Item_is_not_null_test :public Item_func_isnull
1326
{
1327
  Item_in_subselect* owner;
1328
public:
1329
  Item_is_not_null_test(Item_in_subselect* ow, Item *a)
1330
    :Item_func_isnull(a), owner(ow)
1331
  {}
1332
  enum Functype functype() const { return ISNOTNULLTEST_FUNC; }
152 by Brian Aker
longlong replacement
1333
  int64_t val_int();
1 by brian
clean slate
1334
  const char *func_name() const { return "<is_not_null_test>"; }
1335
  void update_used_tables();
1336
  /*
1337
    we add RAND_TABLE_BIT to prevent moving this item from HAVING to WHERE
1338
  */
1339
  table_map used_tables() const
1340
    { return used_tables_cache | RAND_TABLE_BIT; }
1341
};
1342
1343
1344
class Item_func_isnotnull :public Item_bool_func
1345
{
1346
  bool abort_on_null;
1347
public:
1348
  Item_func_isnotnull(Item *a) :Item_bool_func(a), abort_on_null(0) {}
152 by Brian Aker
longlong replacement
1349
  int64_t val_int();
1 by brian
clean slate
1350
  enum Functype functype() const { return ISNOTNULL_FUNC; }
1351
  void fix_length_and_dec()
1352
  {
1353
    decimals=0; max_length=1; maybe_null=0;
1354
  }
1355
  const char *func_name() const { return "isnotnull"; }
1356
  optimize_type select_optimize() const { return OPTIMIZE_NULL; }
1357
  table_map not_null_tables() const
1358
  { return abort_on_null ? not_null_tables_cache : 0; }
520.1.22 by Brian Aker
Second pass of thd cleanup
1359
  Item *neg_transformer(Session *session);
1 by brian
clean slate
1360
  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.
1361
  const CHARSET_INFO *compare_collation() { return args[0]->collation.collation; }
1 by brian
clean slate
1362
  void top_level_item() { abort_on_null=1; }
1363
};
1364
1365
1366
class Item_func_like :public Item_bool_func2
1367
{
1368
  // Turbo Boyer-Moore data
1369
  bool        canDoTurboBM;	// pattern is '%abcd%' case
1370
  const char* pattern;
1371
  int         pattern_len;
1372
1373
  // TurboBM buffers, *this is owner
1374
  int* bmGs; //   good suffix shift table, size is pattern_len + 1
1375
  int* bmBc; // bad character shift table, size is alphabet_size
1376
1377
  void turboBM_compute_suffixes(int* suff);
1378
  void turboBM_compute_good_suffix_shifts(int* suff);
1379
  void turboBM_compute_bad_character_shifts();
1380
  bool turboBM_matches(const char* text, int text_len) const;
1381
  enum { alphabet_size = 256 };
1382
1383
  Item *escape_item;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1384
1 by brian
clean slate
1385
  bool escape_used_in_parsing;
1386
1387
public:
1388
  int escape;
1389
1390
  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:
1391
    :Item_bool_func2(a,b), canDoTurboBM(false), pattern(0), pattern_len(0),
1 by brian
clean slate
1392
     bmGs(0), bmBc(0), escape_item(escape_arg),
1393
     escape_used_in_parsing(escape_used) {}
152 by Brian Aker
longlong replacement
1394
  int64_t val_int();
1 by brian
clean slate
1395
  enum Functype functype() const { return LIKE_FUNC; }
1396
  optimize_type select_optimize() const;
1397
  cond_result eq_cmp_result() const { return COND_TRUE; }
1398
  const char *func_name() const { return "like"; }
520.1.22 by Brian Aker
Second pass of thd cleanup
1399
  bool fix_fields(Session *session, Item **ref);
1 by brian
clean slate
1400
  void cleanup();
1401
};
1402
1403
1404
typedef class Item COND;
1405
1406
class Item_cond :public Item_bool_func
1407
{
1408
protected:
1409
  List<Item> list;
1410
  bool abort_on_null;
1411
  table_map and_tables_cache;
1412
1413
public:
1414
  /* Item_cond() is only used to create top level items */
1415
  Item_cond(): Item_bool_func(), abort_on_null(1)
1416
  { const_item_cache=0; }
1417
  Item_cond(Item *i1,Item *i2)
1418
    :Item_bool_func(), abort_on_null(0)
1419
  {
1420
    list.push_back(i1);
1421
    list.push_back(i2);
1422
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1423
  Item_cond(Session *session, Item_cond *item);
1 by brian
clean slate
1424
  Item_cond(List<Item> &nlist)
1425
    :Item_bool_func(), list(nlist), abort_on_null(0) {}
1426
  bool add(Item *item) { return list.push_back(item); }
1427
  bool add_at_head(Item *item) { return list.push_front(item); }
1428
  void add_at_head(List<Item> *nlist) { list.prepand(nlist); }
520.1.21 by Brian Aker
THD -> Session rename
1429
  bool fix_fields(Session *, Item **ref);
1 by brian
clean slate
1430
  void fix_after_pullout(st_select_lex *new_parent, Item **ref);
1431
1432
  enum Type type() const { return COND_ITEM; }
1433
  List<Item>* argument_list() { return &list; }
1434
  table_map used_tables() const;
1435
  void update_used_tables();
1436
  virtual void print(String *str, enum_query_type query_type);
520.1.22 by Brian Aker
Second pass of thd cleanup
1437
  void split_sum_func(Session *session, Item **ref_pointer_array, List<Item> &fields);
1438
  friend int setup_conds(Session *session, TableList *tables, TableList *leaves,
1 by brian
clean slate
1439
                         COND **conds);
1440
  void top_level_item() { abort_on_null=1; }
520.1.22 by Brian Aker
Second pass of thd cleanup
1441
  void copy_andor_arguments(Session *session, Item_cond *item);
481 by Brian Aker
Remove all of uchar.
1442
  bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg);
1443
  Item *transform(Item_transformer transformer, unsigned char *arg);
1 by brian
clean slate
1444
  void traverse_cond(Cond_traverser, void *arg, traverse_order order);
520.1.22 by Brian Aker
Second pass of thd cleanup
1445
  void neg_arguments(Session *session);
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1446
  enum_field_types field_type() const { return DRIZZLE_TYPE_LONGLONG; }
646 by Brian Aker
Next pass through attribute.
1447
  bool subst_argument_checker(unsigned char **)
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1448
  { return true; }
481 by Brian Aker
Remove all of uchar.
1449
  Item *compile(Item_analyzer analyzer, unsigned char **arg_p,
1450
                Item_transformer transformer, unsigned char *arg_t);
1 by brian
clean slate
1451
};
1452
1453
1454
/*
1455
  The class Item_equal is used to represent conjunctions of equality
1456
  predicates of the form field1 = field2, and field=const in where
1457
  conditions and on expressions.
1458
1459
  All equality predicates of the form field1=field2 contained in a
1460
  conjunction are substituted for a sequence of items of this class.
1461
  An item of this class Item_equal(f1,f2,...fk) represents a
1462
  multiple equality f1=f2=...=fk.
1463
1464
  If a conjunction contains predicates f1=f2 and f2=f3, a new item of
1465
  this class is created Item_equal(f1,f2,f3) representing the multiple
1466
  equality f1=f2=f3 that substitutes the above equality predicates in
1467
  the conjunction.
1468
  A conjunction of the predicates f2=f1 and f3=f1 and f3=f2 will be
1469
  substituted for the item representing the same multiple equality
1470
  f1=f2=f3.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1471
  An item Item_equal(f1,f2) can appear instead of a conjunction of
1 by brian
clean slate
1472
  f2=f1 and f1=f2, or instead of just the predicate f1=f2.
1473
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1474
  An item of the class Item_equal inherits equalities from outer
1 by brian
clean slate
1475
  conjunctive levels.
1476
1477
  Suppose we have a where condition of the following form:
1478
  WHERE f1=f2 AND f3=f4 AND f3=f5 AND ... AND (...OR (f1=f3 AND ...)).
1479
  In this case:
1480
    f1=f2 will be substituted for Item_equal(f1,f2);
1481
    f3=f4 and f3=f5  will be substituted for Item_equal(f3,f4,f5);
1482
    f1=f3 will be substituted for Item_equal(f1,f2,f3,f4,f5);
1483
1484
  An object of the class Item_equal can contain an optional constant
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1485
  item c. Then it represents a multiple equality of the form
1 by brian
clean slate
1486
  c=f1=...=fk.
1487
1488
  Objects of the class Item_equal are used for the following:
1489
1490
  1. An object Item_equal(t1.f1,...,tk.fk) allows us to consider any
1491
  pair of tables ti and tj as joined by an equi-condition.
1492
  Thus it provide us with additional access paths from table to table.
1493
1494
  2. An object Item_equal(t1.f1,...,tk.fk) is applied to deduce new
1495
  SARGable predicates:
1496
    f1=...=fk AND P(fi) => f1=...=fk AND P(fi) AND P(fj).
1497
  It also can give us additional index scans and can allow us to
1498
  improve selectivity estimates.
1499
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1500
  3. An object Item_equal(t1.f1,...,tk.fk) is used to optimize the
1501
  selected execution plan for the query: if table ti is accessed
1 by brian
clean slate
1502
  before the table tj then in any predicate P in the where condition
1503
  the occurrence of tj.fj is substituted for ti.fi. This can allow
1504
  an evaluation of the predicate at an earlier step.
1505
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1506
  When feature 1 is supported they say that join transitive closure
1 by brian
clean slate
1507
  is employed.
1508
  When feature 2 is supported they say that search argument transitive
1509
  closure is employed.
1510
  Both features are usually supported by preprocessing original query and
1511
  adding additional predicates.
1512
  We do not just add predicates, we rather dynamically replace some
1513
  predicates that can not be used to access tables in the investigated
1514
  plan for those, obtained by substitution of some fields for equal fields,
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1515
  that can be used.
1 by brian
clean slate
1516
1517
  Prepared Statements/Stored Procedures note: instances of class
1518
  Item_equal are created only at the time a PS/SP is executed and
1519
  are deleted in the end of execution. All changes made to these
1520
  objects need not be registered in the list of changes of the parse
1521
  tree and do not harm PS/SP re-execution.
1522
1523
  Item equal objects are employed only at the optimize phase. Usually they are
1524
  not supposed to be evaluated.  Yet in some cases we call the method val_int()
1525
  for them. We have to take care of restricting the predicate such an
1526
  object represents f1=f2= ...=fn to the projection of known fields fi1=...=fik.
1527
*/
1528
1529
class Item_equal: public Item_bool_func
1530
{
1531
  List<Item_field> fields; /* list of equal field items                    */
1532
  Item *const_item;        /* optional constant item equal to fields items */
1533
  cmp_item *eval_item;
1534
  bool cond_false;
1535
public:
1536
  inline Item_equal()
1537
    : Item_bool_func(), const_item(0), eval_item(0), cond_false(0)
1538
  { const_item_cache=0 ;}
1539
  Item_equal(Item_field *f1, Item_field *f2);
1540
  Item_equal(Item *c, Item_field *f);
1541
  Item_equal(Item_equal *item_equal);
1542
  inline Item* get_const() { return const_item; }
1543
  void add(Item *c);
1544
  void add(Item_field *f);
482 by Brian Aker
Remove uint.
1545
  uint32_t members();
1 by brian
clean slate
1546
  bool contains(Field *field);
1547
  Item_field* get_first() { return fields.head(); }
1548
  void merge(Item_equal *item);
1549
  void update_const();
1550
  enum Functype functype() const { return MULT_EQUAL_FUNC; }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1551
  int64_t val_int();
1 by brian
clean slate
1552
  const char *func_name() const { return "multiple equal"; }
1553
  optimize_type select_optimize() const { return OPTIMIZE_EQUAL; }
1554
  void sort(Item_field_cmpfunc cmp, void *arg);
1555
  friend class Item_equal_iterator;
1556
  void fix_length_and_dec();
520.1.22 by Brian Aker
Second pass of thd cleanup
1557
  bool fix_fields(Session *session, Item **ref);
1 by brian
clean slate
1558
  void update_used_tables();
481 by Brian Aker
Remove all of uchar.
1559
  bool walk(Item_processor processor, bool walk_subquery, unsigned char *arg);
1560
  Item *transform(Item_transformer transformer, unsigned char *arg);
1 by brian
clean slate
1561
  virtual void print(String *str, enum_query_type query_type);
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1562
  const CHARSET_INFO *compare_collation()
1 by brian
clean slate
1563
  { return fields.head()->collation.collation; }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1564
};
1 by brian
clean slate
1565
1566
class COND_EQUAL: public Sql_alloc
1567
{
1568
public:
482 by Brian Aker
Remove uint.
1569
  uint32_t max_members;               /* max number of members the current level
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1570
                                     list and all lower level lists */
1 by brian
clean slate
1571
  COND_EQUAL *upper_levels;       /* multiple equalities of upper and levels */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1572
  List<Item_equal> current_level; /* list of multiple equalities of
1 by brian
clean slate
1573
                                     the current and level           */
1574
  COND_EQUAL()
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1575
  {
1 by brian
clean slate
1576
    upper_levels= 0;
1577
  }
1578
};
1579
1580
1581
class Item_equal_iterator : public List_iterator_fast<Item_field>
1582
{
1583
public:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1584
  inline Item_equal_iterator(Item_equal &item_equal)
1 by brian
clean slate
1585
    :List_iterator_fast<Item_field> (item_equal.fields)
1586
  {}
1587
  inline Item_field* operator++(int)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1588
  {
1 by brian
clean slate
1589
    Item_field *item= (*(List_iterator_fast<Item_field> *) this)++;
1590
    return  item;
1591
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1592
  inline void rewind(void)
1593
  {
1 by brian
clean slate
1594
    List_iterator_fast<Item_field>::rewind();
1595
  }
1596
};
1597
1598
class Item_cond_and :public Item_cond
1599
{
1600
public:
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1601
  COND_EQUAL cond_equal;  /* contains list of Item_equal objects for
1 by brian
clean slate
1602
                             the current and level and reference
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1603
                             to multiple equalities of upper and levels */
1 by brian
clean slate
1604
  Item_cond_and() :Item_cond() {}
1605
  Item_cond_and(Item *i1,Item *i2) :Item_cond(i1,i2) {}
520.1.22 by Brian Aker
Second pass of thd cleanup
1606
  Item_cond_and(Session *session, Item_cond_and *item) :Item_cond(session, item) {}
1 by brian
clean slate
1607
  Item_cond_and(List<Item> &list_arg): Item_cond(list_arg) {}
1608
  enum Functype functype() const { return COND_AND_FUNC; }
152 by Brian Aker
longlong replacement
1609
  int64_t val_int();
1 by brian
clean slate
1610
  const char *func_name() const { return "and"; }
1611
  table_map not_null_tables() const
1612
  { return abort_on_null ? not_null_tables_cache: and_tables_cache; }
520.1.22 by Brian Aker
Second pass of thd cleanup
1613
  Item* copy_andor_structure(Session *session)
1 by brian
clean slate
1614
  {
1615
    Item_cond_and *item;
520.1.22 by Brian Aker
Second pass of thd cleanup
1616
    if ((item= new Item_cond_and(session, this)))
1617
       item->copy_andor_arguments(session, this);
1 by brian
clean slate
1618
    return item;
1619
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1620
  Item *neg_transformer(Session *session);
1 by brian
clean slate
1621
};
1622
1623
inline bool is_cond_and(Item *item)
1624
{
1625
  if (item->type() != Item::COND_ITEM)
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1626
    return false;
1 by brian
clean slate
1627
1628
  Item_cond *cond_item= (Item_cond*) item;
1629
  return (cond_item->functype() == Item_func::COND_AND_FUNC);
1630
}
1631
1632
class Item_cond_or :public Item_cond
1633
{
1634
public:
1635
  Item_cond_or() :Item_cond() {}
1636
  Item_cond_or(Item *i1,Item *i2) :Item_cond(i1,i2) {}
520.1.22 by Brian Aker
Second pass of thd cleanup
1637
  Item_cond_or(Session *session, Item_cond_or *item) :Item_cond(session, item) {}
1 by brian
clean slate
1638
  Item_cond_or(List<Item> &list_arg): Item_cond(list_arg) {}
1639
  enum Functype functype() const { return COND_OR_FUNC; }
152 by Brian Aker
longlong replacement
1640
  int64_t val_int();
1 by brian
clean slate
1641
  const char *func_name() const { return "or"; }
1642
  table_map not_null_tables() const { return and_tables_cache; }
520.1.22 by Brian Aker
Second pass of thd cleanup
1643
  Item* copy_andor_structure(Session *session)
1 by brian
clean slate
1644
  {
1645
    Item_cond_or *item;
520.1.22 by Brian Aker
Second pass of thd cleanup
1646
    if ((item= new Item_cond_or(session, this)))
1647
      item->copy_andor_arguments(session, this);
1 by brian
clean slate
1648
    return item;
1649
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
1650
  Item *neg_transformer(Session *session);
1 by brian
clean slate
1651
};
1652
1653
inline bool is_cond_or(Item *item)
1654
{
1655
  if (item->type() != Item::COND_ITEM)
51.1.77 by Jay Pipes
Standardized TRUE/FALSE, removed/replaced DBUG symbols
1656
    return false;
1 by brian
clean slate
1657
1658
  Item_cond *cond_item= (Item_cond*) item;
1659
  return (cond_item->functype() == Item_func::COND_OR_FUNC);
1660
}
1661
1662
/*
1663
  XOR is Item_cond, not an Item_int_func because we could like to
1664
  optimize (a XOR b) later on. It's low prio, though
1665
*/
1666
1667
class Item_cond_xor :public Item_cond
1668
{
1669
public:
1670
  Item_cond_xor() :Item_cond() {}
1671
  Item_cond_xor(Item *i1,Item *i2) :Item_cond(i1,i2) {}
1672
  enum Functype functype() const { return COND_XOR_FUNC; }
1673
  /* TODO: remove the next line when implementing XOR optimization */
1674
  enum Type type() const { return FUNC_ITEM; }
152 by Brian Aker
longlong replacement
1675
  int64_t val_int();
1 by brian
clean slate
1676
  const char *func_name() const { return "xor"; }
1677
  void top_level_item() {}
1678
};
1679
1680
1681
/* Some useful inline functions */
1682
1683
inline Item *and_conds(Item *a, Item *b)
1684
{
1685
  if (!b) return a;
1686
  if (!a) return b;
1687
  return new Item_cond_and(a, b);
1688
}
1689
1690
Item *and_expressions(Item *a, Item *b, Item **org_item);
575.1.6 by Monty Taylor
Cleaned up some headers for PCH.
1691
1692
#endif /* DRIZZLED_ITEM_CMPFUNC_H */