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