~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/item_cmpfunc.cc

MergedĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
  This file defines all compare functions
22
22
*/
23
23
 
24
 
#include "config.h"
25
 
#include "drizzled/sql_select.h"
26
 
#include "drizzled/error.h"
27
 
#include "drizzled/temporal.h"
28
 
#include "drizzled/item/cmpfunc.h"
29
 
#include "drizzled/cached_item.h"
30
 
#include "drizzled/item/cache_int.h"
31
 
#include "drizzled/item/int_with_ref.h"
32
 
#include "drizzled/check_stack_overrun.h"
33
 
#include "drizzled/time_functions.h"
34
 
#include "drizzled/internal/my_sys.h"
35
 
#include <math.h>
36
 
#include <algorithm>
37
 
 
38
 
using namespace std;
39
 
 
40
 
namespace drizzled
41
 
{
42
 
 
43
 
extern const double log_10[309];
44
 
 
45
 
static Eq_creator eq_creator;
46
 
static Ne_creator ne_creator;
47
 
static Gt_creator gt_creator;
48
 
static Lt_creator lt_creator;
49
 
static Ge_creator ge_creator;
50
 
static Le_creator le_creator;
51
 
 
52
 
static bool convert_constant_item(Session *, Item_field *, Item **);
 
24
#ifdef USE_PRAGMA_IMPLEMENTATION
 
25
#pragma implementation                          // gcc: Class implementation
 
26
#endif
 
27
 
 
28
#include "mysql_priv.h"
 
29
#include <m_ctype.h>
 
30
#include "sql_select.h"
 
31
 
 
32
static bool convert_constant_item(THD *, Item_field *, Item **);
53
33
 
54
34
static Item_result item_store_type(Item_result a, Item *item,
55
 
                                   bool unsigned_flag)
 
35
                                   my_bool unsigned_flag)
56
36
{
57
37
  Item_result b= item->result_type();
58
38
 
67
47
    return INT_RESULT;
68
48
}
69
49
 
70
 
static void agg_result_type(Item_result *type, Item **items, uint32_t nitems)
 
50
static void agg_result_type(Item_result *type, Item **items, uint nitems)
71
51
{
72
52
  Item **item, **item_end;
73
 
  bool unsigned_flag= 0;
 
53
  my_bool unsigned_flag= 0;
74
54
 
75
55
  *type= STRING_RESULT;
76
56
  /* Skip beginning NULL items */
104
84
  DESCRIPTION
105
85
    The function checks that two expressions have compatible row signatures
106
86
    i.e. that the number of columns they return are the same and that if they
107
 
    are both row expressions then each component from the first expression has
 
87
    are both row expressions then each component from the first expression has 
108
88
    a row signature compatible with the signature of the corresponding component
109
89
    of the second expression.
110
90
 
115
95
 
116
96
static int cmp_row_type(Item* item1, Item* item2)
117
97
{
118
 
  uint32_t n= item1->cols();
 
98
  uint n= item1->cols();
119
99
  if (item2->check_cols(n))
120
100
    return 1;
121
 
  for (uint32_t i=0; i<n; i++)
 
101
  for (uint i=0; i<n; i++)
122
102
  {
123
103
    if (item2->element_index(i)->check_cols(item1->element_index(i)->cols()) ||
124
104
        (item1->element_index(i)->result_type() == ROW_RESULT &&
152
132
    0  otherwise
153
133
*/
154
134
 
155
 
static int agg_cmp_type(Item_result *type, Item **items, uint32_t nitems)
 
135
static int agg_cmp_type(Item_result *type, Item **items, uint nitems)
156
136
{
157
 
  uint32_t i;
 
137
  uint i;
158
138
  type[0]= items[0]->result_type();
159
139
  for (i= 1 ; i < nitems ; i++)
160
140
  {
165
145
      of the first row expression has a compatible row signature with
166
146
      the signature of the corresponding component of the second row
167
147
      expression.
168
 
    */
 
148
    */ 
169
149
    if (type[0] == ROW_RESULT && cmp_row_type(items[0], items[i]))
170
150
      return 1;     // error found: invalid usage of rows
171
151
  }
191
171
  @return aggregated field type.
192
172
*/
193
173
 
194
 
enum_field_types agg_field_type(Item **items, uint32_t nitems)
 
174
enum_field_types agg_field_type(Item **items, uint nitems)
195
175
{
196
 
  uint32_t i;
 
176
  uint i;
197
177
  if (!nitems || items[0]->result_type() == ROW_RESULT )
198
178
    return (enum_field_types)-1;
199
179
  enum_field_types res= items[0]->field_type();
209
189
    collect_cmp_types()
210
190
      items             Array of items to collect types from
211
191
      nitems            Number of items in the array
212
 
      skip_nulls        Don't collect types of NULL items if TRUE
213
192
 
214
193
  DESCRIPTION
215
194
    This function collects different result types for comparison of the first
220
199
    Bitmap of collected types - otherwise
221
200
*/
222
201
 
223
 
static uint32_t collect_cmp_types(Item **items, uint32_t nitems, bool skip_nulls= false)
 
202
static uint collect_cmp_types(Item **items, uint nitems)
224
203
{
225
 
  uint32_t i;
226
 
  uint32_t found_types;
 
204
  uint i;
 
205
  uint found_types;
227
206
  Item_result left_result= items[0]->result_type();
228
 
  assert(nitems > 1);
 
207
  DBUG_ASSERT(nitems > 1);
229
208
  found_types= 0;
230
209
  for (i= 1; i < nitems ; i++)
231
210
  {
232
 
    if (skip_nulls && items[i]->type() == Item::NULL_ITEM)
233
 
      continue; // Skip NULL constant items
234
 
    if ((left_result == ROW_RESULT ||
 
211
    if ((left_result == ROW_RESULT || 
235
212
         items[i]->result_type() == ROW_RESULT) &&
236
213
        cmp_row_type(items[0], items[i]))
237
214
      return 0;
238
 
    found_types|= 1<< (uint32_t)item_cmp_type(left_result,
 
215
    found_types|= 1<< (uint)item_cmp_type(left_result,
239
216
                                           items[i]->result_type());
240
217
  }
241
 
  /*
242
 
   Even if all right-hand items are NULLs and we are skipping them all, we need
243
 
   at least one type bit in the found_type bitmask.
244
 
  */
245
 
  if (skip_nulls && !found_types)
246
 
    found_types= 1 << (uint)left_result;
247
218
  return found_types;
248
219
}
249
220
 
 
221
static void my_coll_agg_error(DTCollation &c1, DTCollation &c2,
 
222
                              const char *fname)
 
223
{
 
224
  my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0),
 
225
           c1.collation->name,c1.derivation_name(),
 
226
           c2.collation->name,c2.derivation_name(),
 
227
           fname);
 
228
}
 
229
 
250
230
 
251
231
Item_bool_func2* Eq_creator::create(Item *a, Item *b) const
252
232
{
254
234
}
255
235
 
256
236
 
257
 
const Eq_creator* Eq_creator::instance()
258
 
{
259
 
  return &eq_creator;
260
 
}
261
 
 
262
 
 
263
237
Item_bool_func2* Ne_creator::create(Item *a, Item *b) const
264
238
{
265
239
  return new Item_func_ne(a, b);
266
240
}
267
241
 
268
242
 
269
 
const Ne_creator* Ne_creator::instance()
270
 
{
271
 
  return &ne_creator;
272
 
}
273
 
 
274
 
 
275
243
Item_bool_func2* Gt_creator::create(Item *a, Item *b) const
276
244
{
277
245
  return new Item_func_gt(a, b);
278
246
}
279
247
 
280
248
 
281
 
const Gt_creator* Gt_creator::instance()
282
 
{
283
 
  return &gt_creator;
284
 
}
285
 
 
286
 
 
287
249
Item_bool_func2* Lt_creator::create(Item *a, Item *b) const
288
250
{
289
251
  return new Item_func_lt(a, b);
290
252
}
291
253
 
292
254
 
293
 
const Lt_creator* Lt_creator::instance()
294
 
{
295
 
  return &lt_creator;
296
 
}
297
 
 
298
 
 
299
255
Item_bool_func2* Ge_creator::create(Item *a, Item *b) const
300
256
{
301
257
  return new Item_func_ge(a, b);
302
258
}
303
259
 
304
260
 
305
 
const Ge_creator* Ge_creator::instance()
306
 
{
307
 
  return &ge_creator;
308
 
}
309
 
 
310
 
 
311
261
Item_bool_func2* Le_creator::create(Item *a, Item *b) const
312
262
{
313
263
  return new Item_func_le(a, b);
314
264
}
315
265
 
316
 
const Le_creator* Le_creator::instance()
317
 
{
318
 
  return &le_creator;
319
 
}
320
 
 
321
 
 
322
266
/*
323
267
  Test functions
324
268
  Most of these  returns 0LL if false and 1LL if true and
325
269
  NULL if some arg is NULL.
326
270
*/
327
271
 
328
 
int64_t Item_func_not::val_int()
 
272
longlong Item_func_not::val_int()
329
273
{
330
 
  assert(fixed == 1);
 
274
  DBUG_ASSERT(fixed == 1);
331
275
  bool value= args[0]->val_bool();
332
276
  null_value=args[0]->null_value;
333
277
  return ((!null_value && value == 0) ? 1 : 0);
354
298
*/
355
299
 
356
300
 
357
 
int64_t Item_func_not_all::val_int()
 
301
longlong Item_func_not_all::val_int()
358
302
{
359
 
  assert(fixed == 1);
 
303
  DBUG_ASSERT(fixed == 1);
360
304
  bool value= args[0]->val_bool();
361
305
 
362
306
  /*
395
339
    returns some rows it return same value as argument (true/false).
396
340
*/
397
341
 
398
 
int64_t Item_func_nop_all::val_int()
 
342
longlong Item_func_nop_all::val_int()
399
343
{
400
 
  assert(fixed == 1);
401
 
  int64_t value= args[0]->val_int();
 
344
  DBUG_ASSERT(fixed == 1);
 
345
  longlong value= args[0]->val_int();
402
346
 
403
347
  /*
404
348
    return false if there was records in underlying select in max/min
422
366
    also when comparing bigint to strings (in which case strings
423
367
    are converted to bigints).
424
368
 
425
 
  @param  session             thread handle
 
369
  @param  thd             thread handle
426
370
  @param  field_item      item will be converted using the type of this field
427
371
  @param[in,out] item     reference to the item to convert
428
372
 
439
383
    1  Item was replaced with an integer version of the item
440
384
*/
441
385
 
442
 
static bool convert_constant_item(Session *session, Item_field *field_item,
 
386
static bool convert_constant_item(THD *thd, Item_field *field_item,
443
387
                                  Item **item)
444
388
{
445
389
  Field *field= field_item->field;
446
390
  int result= 0;
447
391
 
448
 
  field->setWriteSet();
449
 
 
450
392
  if (!(*item)->with_subselect && (*item)->const_item())
451
393
  {
452
 
    ulong orig_sql_mode= session->variables.sql_mode;
453
 
    enum_check_fields orig_count_cuted_fields= session->count_cuted_fields;
454
 
    uint64_t orig_field_val= 0; /* original field value if valid */
 
394
    TABLE *table= field->table;
 
395
    ulong orig_sql_mode= thd->variables.sql_mode;
 
396
    enum_check_fields orig_count_cuted_fields= thd->count_cuted_fields;
 
397
    my_bitmap_map *old_write_map;
 
398
    my_bitmap_map *old_read_map;
 
399
    ulonglong orig_field_val= 0; /* original field value if valid */
455
400
 
 
401
    if (table)
 
402
    {
 
403
      old_write_map= dbug_tmp_use_all_columns(table, table->write_set);
 
404
      old_read_map= dbug_tmp_use_all_columns(table, table->read_set);
 
405
    }
456
406
    /* For comparison purposes allow invalid dates like 2000-01-32 */
457
 
    session->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) |
 
407
    thd->variables.sql_mode= (orig_sql_mode & ~MODE_NO_ZERO_DATE) | 
458
408
                             MODE_INVALID_DATES;
459
 
    session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
409
    thd->count_cuted_fields= CHECK_FIELD_IGNORE;
460
410
 
461
411
    /*
462
412
      Store the value of the field if it references an outer field because
469
419
      Item *tmp= new Item_int_with_ref(field->val_int(), *item,
470
420
                                       test(field->flags & UNSIGNED_FLAG));
471
421
      if (tmp)
472
 
        session->change_item_tree(item, tmp);
 
422
        thd->change_item_tree(item, tmp);
473
423
      result= 1;                                        // Item was replaced
474
424
    }
475
425
    /* Restore the original field value. */
477
427
    {
478
428
      result= field->store(orig_field_val, true);
479
429
      /* orig_field_val must be a valid value that can be restored back. */
480
 
      assert(!result);
481
 
    }
482
 
    session->variables.sql_mode= orig_sql_mode;
483
 
    session->count_cuted_fields= orig_count_cuted_fields;
 
430
      DBUG_ASSERT(!result);
 
431
    }
 
432
    thd->variables.sql_mode= orig_sql_mode;
 
433
    thd->count_cuted_fields= orig_count_cuted_fields;
 
434
    if (table)
 
435
    {
 
436
      dbug_tmp_restore_column_map(table->write_set, old_write_map);
 
437
      dbug_tmp_restore_column_map(table->read_set, old_read_map);
 
438
    }
484
439
  }
485
440
  return result;
486
441
}
489
444
void Item_bool_func2::fix_length_and_dec()
490
445
{
491
446
  max_length= 1;                                     // Function returns 0 or 1
492
 
  Session *session;
 
447
  THD *thd;
493
448
 
494
449
  /*
495
450
    As some compare functions are generated after sql_yacc,
498
453
  if (!args[0] || !args[1])
499
454
    return;
500
455
 
501
 
  /*
 
456
  /* 
502
457
    We allow to convert to Unicode character sets in some cases.
503
458
    The conditions when conversion is possible are:
504
459
    - arguments A and B have different charsets
505
460
    - A wins according to coercibility rules
506
461
    - character set of A is superset for character set of B
507
 
 
 
462
   
508
463
    If all of the above is true, then it's possible to convert
509
464
    B into the character set of A, and then compare according
510
465
    to the collation of A.
511
466
  */
512
467
 
513
 
 
 
468
  
514
469
  DTCollation coll;
515
470
  if (args[0]->result_type() == STRING_RESULT &&
516
471
      args[1]->result_type() == STRING_RESULT &&
517
472
      agg_arg_charsets(coll, args, 2, MY_COLL_CMP_CONV, 1))
518
473
    return;
519
 
 
 
474
    
520
475
  args[0]->cmp_context= args[1]->cmp_context=
521
476
    item_cmp_type(args[0]->result_type(), args[1]->result_type());
522
477
  // Make a special case of compare with fields to get nicer DATE comparisons
527
482
    return;
528
483
  }
529
484
 
530
 
  session= current_session;
531
 
  Item_field *field_item= NULL;
532
 
 
533
 
  if (args[0]->real_item()->type() == FIELD_ITEM)
 
485
  thd= current_thd;
 
486
  if (!thd->is_context_analysis_only())
534
487
  {
535
 
    field_item= static_cast<Item_field*>(args[0]->real_item());
536
 
    if (field_item->field->can_be_compared_as_int64_t() &&
537
 
        !(field_item->is_datetime() && args[1]->result_type() == STRING_RESULT))
 
488
    if (args[0]->real_item()->type() == FIELD_ITEM)
538
489
    {
539
 
      if (convert_constant_item(session, field_item, &args[1]))
 
490
      Item_field *field_item= (Item_field*) (args[0]->real_item());
 
491
      if (field_item->field->can_be_compared_as_longlong() &&
 
492
          !(field_item->is_datetime() &&
 
493
            args[1]->result_type() == STRING_RESULT))
540
494
      {
541
 
        cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
542
 
                         INT_RESULT);           // Works for all types.
543
 
        args[0]->cmp_context= args[1]->cmp_context= INT_RESULT;
544
 
        return;
 
495
        if (convert_constant_item(thd, field_item, &args[1]))
 
496
        {
 
497
          cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
 
498
                           INT_RESULT);         // Works for all types.
 
499
          args[0]->cmp_context= args[1]->cmp_context= INT_RESULT;
 
500
          return;
 
501
        }
545
502
      }
546
503
    }
547
 
 
548
504
    if (args[1]->real_item()->type() == FIELD_ITEM)
549
505
    {
550
 
      field_item= static_cast<Item_field*>(args[1]->real_item());
551
 
      if (field_item->field->can_be_compared_as_int64_t() &&
 
506
      Item_field *field_item= (Item_field*) (args[1]->real_item());
 
507
      if (field_item->field->can_be_compared_as_longlong() &&
552
508
          !(field_item->is_datetime() &&
553
509
            args[0]->result_type() == STRING_RESULT))
554
510
      {
555
 
        if (convert_constant_item(session, field_item, &args[0]))
 
511
        if (convert_constant_item(thd, field_item, &args[0]))
556
512
        {
557
513
          cmp.set_cmp_func(this, tmp_arg, tmp_arg+1,
558
514
                           INT_RESULT); // Works for all types.
574
530
  switch (type) {
575
531
  case ROW_RESULT:
576
532
  {
577
 
    uint32_t n= (*a)->cols();
 
533
    uint n= (*a)->cols();
578
534
    if (n != (*b)->cols())
579
535
    {
580
536
      my_error(ER_OPERAND_COLUMNS, MYF(0), n);
583
539
    }
584
540
    if (!(comparators= new Arg_comparator[n]))
585
541
      return 1;
586
 
    for (uint32_t i=0; i < n; i++)
 
542
    for (uint i=0; i < n; i++)
587
543
    {
588
544
      if ((*a)->element_index(i)->cols() != (*b)->element_index(i)->cols())
589
545
      {
600
556
      We must set cmp_charset here as we may be called from for an automatic
601
557
      generated item, like in natural join
602
558
    */
603
 
    if (cmp_collation.set((*a)->collation, (*b)->collation) ||
 
559
    if (cmp_collation.set((*a)->collation, (*b)->collation) || 
604
560
        cmp_collation.derivation == DERIVATION_NONE)
605
561
    {
606
562
      my_coll_agg_error((*a)->collation, (*b)->collation, owner->func_name());
625
581
        which would be transformed to:
626
582
        WHERE col= 'j'
627
583
      */
628
 
      (*a)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
629
 
      (*b)->walk(&Item::set_no_const_sub, false, (unsigned char*) 0);
 
584
      (*a)->walk(&Item::set_no_const_sub, false, (uchar*) 0);
 
585
      (*b)->walk(&Item::set_no_const_sub, false, (uchar*) 0);
630
586
    }
631
587
    break;
632
588
  }
663
619
    break;
664
620
  }
665
621
  default:
666
 
    assert(0);
 
622
    DBUG_ASSERT(0);
667
623
  }
668
624
  return 0;
669
625
}
672
628
/**
673
629
  @brief Convert date provided in a string to the int representation.
674
630
 
675
 
  @param[in]   session        thread handle
 
631
  @param[in]   thd        thread handle
676
632
  @param[in]   str        a string to convert
677
633
  @param[in]   warn_type  type of the timestamp for issuing the warning
678
634
  @param[in]   warn_name  field name for issuing the warning
691
647
    converted value. 0 on error and on zero-dates -- check 'failure'
692
648
*/
693
649
 
694
 
static uint64_t
695
 
get_date_from_str(Session *session, String *str, enum enum_drizzle_timestamp_type warn_type,
 
650
static ulonglong
 
651
get_date_from_str(THD *thd, String *str, timestamp_type warn_type,
696
652
                  char *warn_name, bool *error_arg)
697
653
{
698
 
  uint64_t value= 0;
 
654
  ulonglong value= 0;
699
655
  int error;
700
 
  DRIZZLE_TIME l_time;
701
 
  enum enum_drizzle_timestamp_type ret;
 
656
  MYSQL_TIME l_time;
 
657
  enum_mysql_timestamp_type ret;
702
658
 
703
659
  ret= str_to_datetime(str->ptr(), str->length(), &l_time,
704
660
                       (TIME_FUZZY_DATE | MODE_INVALID_DATES |
705
 
                        (session->variables.sql_mode & MODE_NO_ZERO_DATE)),
 
661
                        (thd->variables.sql_mode &
 
662
                         (MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE))),
706
663
                       &error);
707
664
 
708
 
  if (ret == DRIZZLE_TIMESTAMP_DATETIME || ret == DRIZZLE_TIMESTAMP_DATE)
 
665
  if (ret == MYSQL_TIMESTAMP_DATETIME || ret == MYSQL_TIMESTAMP_DATE)
709
666
  {
710
667
    /*
711
668
      Do not return yet, we may still want to throw a "trailing garbage"
712
669
      warning.
713
670
    */
714
671
    *error_arg= false;
715
 
    value= TIME_to_uint64_t_datetime(&l_time);
 
672
    value= TIME_to_ulonglong_datetime(&l_time);
716
673
  }
717
674
  else
718
675
  {
721
678
  }
722
679
 
723
680
  if (error > 0)
724
 
  {
725
 
    make_truncated_value_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
681
    make_truncated_value_warning(thd, MYSQL_ERROR::WARN_LEVEL_WARN,
726
682
                                 str->ptr(), str->length(),
727
683
                                 warn_type, warn_name);
728
 
  }
729
684
 
730
685
  return value;
731
686
}
748
703
         int result and the other item (b or a) is an item with string result.
749
704
         If the second item is a constant one then it's checked to be
750
705
         convertible to the DATE/DATETIME type. If the constant can't be
751
 
         converted to a DATE/DATETIME then an error is issued back to the Session.
 
706
         converted to a DATE/DATETIME then the compare_datetime() comparator
 
707
         isn't used and the warning about wrong DATE/DATETIME value is issued.
752
708
      In all other cases (date-[int|real|decimal]/[int|real|decimal]-date)
753
709
      the comparison is handled by other comparators.
754
 
 
755
710
    If the datetime comparator can be used and one the operands of the
756
711
    comparison is a string constant that was successfully converted to a
757
712
    DATE/DATETIME type then the result of the conversion is returned in the
764
719
*/
765
720
 
766
721
enum Arg_comparator::enum_date_cmp_type
767
 
Arg_comparator::can_compare_as_dates(Item *a, Item *b, uint64_t *const_value)
 
722
Arg_comparator::can_compare_as_dates(Item *a, Item *b, ulonglong *const_value)
768
723
{
769
724
  enum enum_date_cmp_type cmp_type= CMP_DATE_DFLT;
770
725
  Item *str_arg= 0, *date_arg= 0;
800
755
        (str_arg->type() != Item::FUNC_ITEM ||
801
756
        ((Item_func*)str_arg)->functype() != Item_func::GUSERVAR_FUNC))
802
757
    {
803
 
      /*
804
 
       * OK, we are here if we've got a date field (or something which can be 
805
 
       * compared as a date field) on one side of the equation, and a constant
806
 
       * string on the other side.  In this case, we must verify that the constant
807
 
       * string expression can indeed be evaluated as a datetime.  If it cannot, 
808
 
       * we throw an error here and stop processsing.  Bad data should ALWAYS 
809
 
       * produce an error, and no implicit conversion or truncation should take place.
810
 
       *
811
 
       * If the conversion to a DateTime temporal is successful, then we convert
812
 
       * the Temporal instance to a uint64_t for the comparison operator, which
813
 
       * compares date(times) using int64_t semantics.
814
 
       *
815
 
       * @TODO
816
 
       *
817
 
       * Does a uint64_t conversion really have to happen here?  Fields return int64_t
818
 
       * from val_int(), not uint64_t...
819
 
       */
820
 
      uint64_t value;
821
 
      String *str_val;
822
 
      String tmp;
823
 
      /* DateTime used to pick up as many string conversion possibilities as possible. */
824
 
      DateTime temporal;
 
758
      THD *thd= current_thd;
 
759
      ulonglong value;
 
760
      bool error;
 
761
      String tmp, *str_val= 0;
 
762
      timestamp_type t_type= (date_arg->field_type() == MYSQL_TYPE_DATE ?
 
763
                              MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME);
825
764
 
826
765
      str_val= str_arg->val_str(&tmp);
827
 
      if (! str_val)
828
 
      {
829
 
        /* 
830
 
         * If we are here, it is most likely due to the comparison item
831
 
         * being a NULL.  Although this is incorrect (SQL demands that the term IS NULL
832
 
         * be used, not = NULL since no item can be equal to NULL).
833
 
         *
834
 
         * So, return gracefully.
835
 
         */
836
 
        return CMP_DATE_DFLT;
837
 
      }
838
 
      if (! temporal.from_string(str_val->c_ptr(), str_val->length()))
839
 
      {
840
 
        /* Chuck an error. Bad datetime input. */
841
 
        my_error(ER_INVALID_DATETIME_VALUE, MYF(ME_FATALERROR), str_val->c_ptr());
842
 
        return CMP_DATE_DFLT; /* :( What else can I return... */
843
 
      }
844
 
 
845
 
      /* String conversion was good.  Convert to an integer for comparison purposes. */
846
 
      int64_t int_value;
847
 
      temporal.to_int64_t(&int_value);
848
 
      value= (uint64_t) int_value;
849
 
 
 
766
      if (str_arg->null_value)
 
767
        return CMP_DATE_DFLT;
 
768
      value= get_date_from_str(thd, str_val, t_type, date_arg->name, &error);
 
769
      if (error)
 
770
        return CMP_DATE_DFLT;
850
771
      if (const_value)
851
772
        *const_value= value;
852
773
    }
855
776
}
856
777
 
857
778
 
 
779
/*
 
780
  Retrieves correct TIME value from the given item.
 
781
 
 
782
  SYNOPSIS
 
783
    get_time_value()
 
784
    thd                 thread handle
 
785
    item_arg   [in/out] item to retrieve TIME value from
 
786
    cache_arg  [in/out] pointer to place to store the cache item to
 
787
    warn_item  [in]     unused
 
788
    is_null    [out]    true <=> the item_arg is null
 
789
 
 
790
  DESCRIPTION
 
791
    Retrieves the correct TIME value from given item for comparison by the
 
792
    compare_datetime() function.
 
793
    If item's result can be compared as longlong then its int value is used
 
794
    and a value returned by get_time function is used otherwise.
 
795
    If an item is a constant one then its value is cached and it isn't
 
796
    get parsed again. An Item_cache_int object is used for for cached values.
 
797
    It seamlessly substitutes the original item.  The cache item is marked as
 
798
    non-constant to prevent re-caching it again.
 
799
 
 
800
  RETURN
 
801
    obtained value
 
802
*/
 
803
 
 
804
ulonglong
 
805
get_time_value(THD *thd __attribute__((__unused__)),
 
806
               Item ***item_arg, Item **cache_arg,
 
807
               Item *warn_item __attribute__((__unused__)),
 
808
               bool *is_null)
 
809
{
 
810
  ulonglong value;
 
811
  Item *item= **item_arg;
 
812
  MYSQL_TIME ltime;
 
813
 
 
814
  if (item->result_as_longlong())
 
815
  {
 
816
    value= item->val_int();
 
817
    *is_null= item->null_value;
 
818
  }
 
819
  else
 
820
  {
 
821
    *is_null= item->get_time(&ltime);
 
822
    value= !*is_null ? TIME_to_ulonglong_datetime(&ltime) : 0;
 
823
  }
 
824
  /*
 
825
    Do not cache GET_USER_VAR() function as its const_item() may return true
 
826
    for the current thread but it still may change during the execution.
 
827
  */
 
828
  if (item->const_item() && cache_arg && (item->type() != Item::FUNC_ITEM ||
 
829
      ((Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
 
830
  {
 
831
    Item_cache_int *cache= new Item_cache_int();
 
832
    /* Mark the cache as non-const to prevent re-caching. */
 
833
    cache->set_used_tables(1);
 
834
    cache->store(item, value);
 
835
    *cache_arg= cache;
 
836
    *item_arg= cache_arg;
 
837
  }
 
838
  return value;
 
839
}
 
840
 
 
841
 
858
842
int Arg_comparator::set_cmp_func(Item_bool_func2 *owner_arg,
859
843
                                        Item **a1, Item **a2,
860
844
                                        Item_result type)
861
845
{
862
846
  enum enum_date_cmp_type cmp_type;
863
 
  uint64_t const_value= (uint64_t)-1;
 
847
  ulonglong const_value= (ulonglong)-1;
864
848
  a= a1;
865
849
  b= a2;
866
850
 
867
851
  if ((cmp_type= can_compare_as_dates(*a, *b, &const_value)))
868
852
  {
869
 
    session= current_session;
 
853
    thd= current_thd;
870
854
    owner= owner_arg;
871
855
    a_type= (*a)->field_type();
872
856
    b_type= (*b)->field_type();
873
857
    a_cache= 0;
874
858
    b_cache= 0;
875
859
 
876
 
    if (const_value != (uint64_t)-1)
 
860
    if (const_value != (ulonglong)-1)
877
861
    {
878
862
      Item_cache_int *cache= new Item_cache_int();
879
863
      /* Mark the cache as non-const to prevent re-caching. */
896
880
    get_value_func= &get_datetime_value;
897
881
    return 0;
898
882
  }
 
883
  else if (type == STRING_RESULT && (*a)->field_type() == MYSQL_TYPE_TIME &&
 
884
           (*b)->field_type() == MYSQL_TYPE_TIME)
 
885
  {
 
886
    /* Compare TIME values as integers. */
 
887
    thd= current_thd;
 
888
    owner= owner_arg;
 
889
    a_cache= 0;
 
890
    b_cache= 0;
 
891
    is_nulls_eq= test(owner && owner->functype() == Item_func::EQUAL_FUNC);
 
892
    func= &Arg_comparator::compare_datetime;
 
893
    get_value_func= &get_time_value;
 
894
    return 0;
 
895
  }
899
896
 
900
897
  return set_compare_func(owner_arg, type);
901
898
}
903
900
 
904
901
void Arg_comparator::set_datetime_cmp_func(Item **a1, Item **b1)
905
902
{
906
 
  session= current_session;
 
903
  thd= current_thd;
907
904
  /* A caller will handle null values by itself. */
908
905
  owner= NULL;
909
906
  a= a1;
923
920
 
924
921
  SYNOPSIS
925
922
    get_datetime_value()
926
 
    session                 thread handle
 
923
    thd                 thread handle
927
924
    item_arg   [in/out] item to retrieve DATETIME value from
928
925
    cache_arg  [in/out] pointer to place to store the caching item to
929
926
    warn_item  [in]     item for issuing the conversion warning
932
929
  DESCRIPTION
933
930
    Retrieves the correct DATETIME value from given item for comparison by the
934
931
    compare_datetime() function.
935
 
    If item's result can be compared as int64_t then its int value is used
 
932
    If item's result can be compared as longlong then its int value is used
936
933
    and its string value is used otherwise. Strings are always parsed and
937
934
    converted to int values by the get_date_from_str() function.
938
935
    This allows us to compare correctly string dates with missed insignificant
947
944
    obtained value
948
945
*/
949
946
 
950
 
uint64_t
951
 
get_datetime_value(Session *session, Item ***item_arg, Item **cache_arg,
 
947
ulonglong
 
948
get_datetime_value(THD *thd, Item ***item_arg, Item **cache_arg,
952
949
                   Item *warn_item, bool *is_null)
953
950
{
954
 
  uint64_t value= 0;
 
951
  ulonglong value= 0;
955
952
  String buf, *str= 0;
956
953
  Item *item= **item_arg;
957
954
 
958
 
  if (item->result_as_int64_t())
 
955
  if (item->result_as_longlong())
959
956
  {
960
957
    value= item->val_int();
961
958
    *is_null= item->null_value;
962
959
    enum_field_types f_type= item->field_type();
963
960
    /*
964
 
      Item_date_add_interval may return DRIZZLE_TYPE_STRING as the result
 
961
      Item_date_add_interval may return MYSQL_TYPE_STRING as the result
965
962
      field type. To detect that the DATE value has been returned we
966
963
      compare it with 100000000L - any DATE value should be less than it.
967
964
      Don't shift cached DATETIME values up for the second time.
968
965
    */
969
 
    if (f_type == DRIZZLE_TYPE_DATE ||
970
 
        (f_type != DRIZZLE_TYPE_DATETIME && value < 100000000L))
 
966
    if (f_type == MYSQL_TYPE_DATE ||
 
967
        (f_type != MYSQL_TYPE_DATETIME && value < 100000000L))
971
968
      value*= 1000000L;
972
969
  }
973
970
  else
976
973
    *is_null= item->null_value;
977
974
  }
978
975
  if (*is_null)
979
 
    return ~(uint64_t) 0;
 
976
    return ~(ulonglong) 0;
980
977
  /*
981
978
    Convert strings to the integer DATE/DATETIME representation.
982
979
    Even if both dates provided in strings we can't compare them directly as
987
984
  {
988
985
    bool error;
989
986
    enum_field_types f_type= warn_item->field_type();
990
 
    enum enum_drizzle_timestamp_type t_type= f_type ==
991
 
      DRIZZLE_TYPE_DATE ? DRIZZLE_TIMESTAMP_DATE : DRIZZLE_TIMESTAMP_DATETIME;
992
 
    value= get_date_from_str(session, str, t_type, warn_item->name, &error);
 
987
    timestamp_type t_type= f_type ==
 
988
      MYSQL_TYPE_DATE ? MYSQL_TIMESTAMP_DATE : MYSQL_TIMESTAMP_DATETIME;
 
989
    value= get_date_from_str(thd, str, t_type, warn_item->name, &error);
993
990
    /*
994
991
      If str did not contain a valid date according to the current
995
992
      SQL_MODE, get_date_from_str() has already thrown a warning,
1004
1001
  if (item->const_item() && cache_arg && (item->type() != Item::FUNC_ITEM ||
1005
1002
      ((Item_func*)item)->functype() != Item_func::GUSERVAR_FUNC))
1006
1003
  {
1007
 
    Item_cache_int *cache= new Item_cache_int(DRIZZLE_TYPE_DATETIME);
 
1004
    Item_cache_int *cache= new Item_cache_int(MYSQL_TYPE_DATETIME);
1008
1005
    /* Mark the cache as non-const to prevent re-caching. */
1009
1006
    cache->set_used_tables(1);
1010
1007
    cache->store(item, value);
1038
1035
int Arg_comparator::compare_datetime()
1039
1036
{
1040
1037
  bool is_null= false;
1041
 
  uint64_t a_value, b_value;
 
1038
  ulonglong a_value, b_value;
1042
1039
 
1043
1040
  /* Get DATE/DATETIME/TIME value of the 'a' item. */
1044
 
  a_value= (*get_value_func)(session, &a, &a_cache, *b, &is_null);
 
1041
  a_value= (*get_value_func)(thd, &a, &a_cache, *b, &is_null);
1045
1042
  if (!is_nulls_eq && is_null)
1046
1043
  {
1047
1044
    if (owner)
1050
1047
  }
1051
1048
 
1052
1049
  /* Get DATE/DATETIME/TIME value of the 'b' item. */
1053
 
  b_value= (*get_value_func)(session, &b, &b_cache, *a, &is_null);
 
1050
  b_value= (*get_value_func)(thd, &b, &b_cache, *a, &is_null);
1054
1051
  if (is_null)
1055
1052
  {
1056
1053
    if (owner)
1064
1061
  /* Compare values. */
1065
1062
  if (is_nulls_eq)
1066
1063
    return (a_value == b_value);
1067
 
  return (a_value < b_value) ? -1 : ((a_value > b_value) ? 1 : 0);
 
1064
  return a_value < b_value ? -1 : (a_value > b_value ? 1 : 0);
1068
1065
}
1069
1066
 
1070
1067
 
1103
1100
    if ((res2= (*b)->val_str(&owner->tmp_value2)))
1104
1101
    {
1105
1102
      owner->null_value= 0;
1106
 
      uint32_t res1_length= res1->length();
1107
 
      uint32_t res2_length= res2->length();
 
1103
      uint res1_length= res1->length();
 
1104
      uint res2_length= res2->length();
1108
1105
      int cmp= memcmp(res1->ptr(), res2->ptr(), min(res1_length,res2_length));
1109
1106
      return cmp ? cmp : (int) (res1_length - res2_length);
1110
1107
    }
1242
1239
 
1243
1240
int Arg_comparator::compare_int_signed()
1244
1241
{
1245
 
  int64_t val1= (*a)->val_int();
 
1242
  longlong val1= (*a)->val_int();
1246
1243
  if (!(*a)->null_value)
1247
1244
  {
1248
 
    int64_t val2= (*b)->val_int();
 
1245
    longlong val2= (*b)->val_int();
1249
1246
    if (!(*b)->null_value)
1250
1247
    {
1251
1248
      owner->null_value= 0;
1265
1262
 
1266
1263
int Arg_comparator::compare_int_unsigned()
1267
1264
{
1268
 
  uint64_t val1= (*a)->val_int();
 
1265
  ulonglong val1= (*a)->val_int();
1269
1266
  if (!(*a)->null_value)
1270
1267
  {
1271
 
    uint64_t val2= (*b)->val_int();
 
1268
    ulonglong val2= (*b)->val_int();
1272
1269
    if (!(*b)->null_value)
1273
1270
    {
1274
1271
      owner->null_value= 0;
1288
1285
 
1289
1286
int Arg_comparator::compare_int_signed_unsigned()
1290
1287
{
1291
 
  int64_t sval1= (*a)->val_int();
 
1288
  longlong sval1= (*a)->val_int();
1292
1289
  if (!(*a)->null_value)
1293
1290
  {
1294
 
    uint64_t uval2= (uint64_t)(*b)->val_int();
 
1291
    ulonglong uval2= (ulonglong)(*b)->val_int();
1295
1292
    if (!(*b)->null_value)
1296
1293
    {
1297
1294
      owner->null_value= 0;
1298
 
      if (sval1 < 0 || (uint64_t)sval1 < uval2)
 
1295
      if (sval1 < 0 || (ulonglong)sval1 < uval2)
1299
1296
        return -1;
1300
 
      if ((uint64_t)sval1 == uval2)
 
1297
      if ((ulonglong)sval1 == uval2)
1301
1298
        return 0;
1302
1299
      return 1;
1303
1300
    }
1313
1310
 
1314
1311
int Arg_comparator::compare_int_unsigned_signed()
1315
1312
{
1316
 
  uint64_t uval1= (uint64_t)(*a)->val_int();
 
1313
  ulonglong uval1= (ulonglong)(*a)->val_int();
1317
1314
  if (!(*a)->null_value)
1318
1315
  {
1319
 
    int64_t sval2= (*b)->val_int();
 
1316
    longlong sval2= (*b)->val_int();
1320
1317
    if (!(*b)->null_value)
1321
1318
    {
1322
1319
      owner->null_value= 0;
1323
1320
      if (sval2 < 0)
1324
1321
        return 1;
1325
 
      if (uval1 < (uint64_t)sval2)
 
1322
      if (uval1 < (ulonglong)sval2)
1326
1323
        return -1;
1327
 
      if (uval1 == (uint64_t)sval2)
 
1324
      if (uval1 == (ulonglong)sval2)
1328
1325
        return 0;
1329
1326
      return 1;
1330
1327
    }
1336
1333
 
1337
1334
int Arg_comparator::compare_e_int()
1338
1335
{
1339
 
  int64_t val1= (*a)->val_int();
1340
 
  int64_t val2= (*b)->val_int();
 
1336
  longlong val1= (*a)->val_int();
 
1337
  longlong val2= (*b)->val_int();
1341
1338
  if ((*a)->null_value || (*b)->null_value)
1342
1339
    return test((*a)->null_value && (*b)->null_value);
1343
1340
  return test(val1 == val2);
1348
1345
*/
1349
1346
int Arg_comparator::compare_e_int_diff_signedness()
1350
1347
{
1351
 
  int64_t val1= (*a)->val_int();
1352
 
  int64_t val2= (*b)->val_int();
 
1348
  longlong val1= (*a)->val_int();
 
1349
  longlong val2= (*b)->val_int();
1353
1350
  if ((*a)->null_value || (*b)->null_value)
1354
1351
    return test((*a)->null_value && (*b)->null_value);
1355
1352
  return (val1 >= 0) && test(val1 == val2);
1361
1358
  bool was_null= 0;
1362
1359
  (*a)->bring_value();
1363
1360
  (*b)->bring_value();
1364
 
  uint32_t n= (*a)->cols();
1365
 
  for (uint32_t i= 0; i<n; i++)
 
1361
  uint n= (*a)->cols();
 
1362
  for (uint i= 0; i<n; i++)
1366
1363
  {
1367
1364
    res= comparators[i].compare();
1368
1365
    if (owner->null_value)
1404
1401
{
1405
1402
  (*a)->bring_value();
1406
1403
  (*b)->bring_value();
1407
 
  uint32_t n= (*a)->cols();
1408
 
  for (uint32_t i= 0; i<n; i++)
 
1404
  uint n= (*a)->cols();
 
1405
  for (uint i= 0; i<n; i++)
1409
1406
  {
1410
1407
    if (!comparators[i].compare())
1411
1408
      return 0;
1461
1458
}
1462
1459
 
1463
1460
 
1464
 
int64_t Item_func_truth::val_int()
 
1461
longlong Item_func_truth::val_int()
1465
1462
{
1466
1463
  return (val_bool() ? 1 : 0);
1467
1464
}
1468
1465
 
1469
1466
 
1470
 
bool Item_in_optimizer::fix_left(Session *session, Item **)
 
1467
bool Item_in_optimizer::fix_left(THD *thd, Item **ref __attribute__((__unused__)))
1471
1468
{
1472
 
  if ((!args[0]->fixed && args[0]->fix_fields(session, args)) ||
 
1469
  if ((!args[0]->fixed && args[0]->fix_fields(thd, args)) ||
1473
1470
      (!cache && !(cache= Item_cache::get_cache(args[0]))))
1474
1471
    return 1;
1475
1472
 
1483
1480
  }
1484
1481
  else
1485
1482
  {
1486
 
    uint32_t n= cache->cols();
1487
 
    for (uint32_t i= 0; i < n; i++)
 
1483
    uint n= cache->cols();
 
1484
    for (uint i= 0; i < n; i++)
1488
1485
    {
1489
1486
      if (args[0]->element_index(i)->used_tables())
1490
1487
        ((Item_cache *)cache->element_index(i))->set_used_tables(OUTER_REF_TABLE_BIT);
1501
1498
}
1502
1499
 
1503
1500
 
1504
 
bool Item_in_optimizer::fix_fields(Session *session, Item **ref)
 
1501
bool Item_in_optimizer::fix_fields(THD *thd, Item **ref)
1505
1502
{
1506
 
  assert(fixed == 0);
1507
 
  if (fix_left(session, ref))
 
1503
  DBUG_ASSERT(fixed == 0);
 
1504
  if (fix_left(thd, ref))
1508
1505
    return true;
1509
1506
  if (args[0]->maybe_null)
1510
1507
    maybe_null=1;
1511
1508
 
1512
 
  if (!args[1]->fixed && args[1]->fix_fields(session, args+1))
 
1509
  if (!args[1]->fixed && args[1]->fix_fields(thd, args+1))
1513
1510
    return true;
1514
1511
  Item_in_subselect * sub= (Item_in_subselect *)args[1];
1515
1512
  if (args[0]->cols() != sub->engine->cols())
1528
1525
}
1529
1526
 
1530
1527
 
1531
 
int64_t Item_in_optimizer::val_int()
 
1528
longlong Item_in_optimizer::val_int()
1532
1529
{
1533
1530
  bool tmp;
1534
 
  assert(fixed == 1);
 
1531
  DBUG_ASSERT(fixed == 1);
1535
1532
  cache->store(args[0]);
1536
 
 
 
1533
  
1537
1534
  if (cache->null_value)
1538
1535
  {
1539
1536
    if (((Item_in_subselect*)args[1])->is_top_level_item())
1561
1558
          We disable the predicates we've pushed down into subselect, run the
1562
1559
          subselect and see if it has produced any rows.
1563
1560
        */
1564
 
        Item_in_subselect *item_subs=(Item_in_subselect*)args[1];
 
1561
        Item_in_subselect *item_subs=(Item_in_subselect*)args[1]; 
1565
1562
        if (cache->cols() == 1)
1566
1563
        {
1567
1564
          item_subs->set_cond_guard_var(0, false);
1571
1568
        }
1572
1569
        else
1573
1570
        {
1574
 
          uint32_t i;
1575
 
          uint32_t ncols= cache->cols();
 
1571
          uint i;
 
1572
          uint ncols= cache->cols();
1576
1573
          /*
1577
1574
            Turn off the predicates that are based on column compares for
1578
1575
            which the left part is currently NULL
1582
1579
            if (cache->element_index(i)->null_value)
1583
1580
              item_subs->set_cond_guard_var(i, false);
1584
1581
          }
1585
 
 
 
1582
          
1586
1583
          (void) args[1]->val_bool_result();
1587
1584
          result_for_null_param= null_value= !item_subs->engine->no_rows();
1588
 
 
 
1585
          
1589
1586
          /* Turn all predicates back on */
1590
1587
          for (i= 0; i < ncols; i++)
1591
1588
            item_subs->set_cond_guard_var(i, true);
1609
1606
 
1610
1607
void Item_in_optimizer::cleanup()
1611
1608
{
 
1609
  DBUG_ENTER("Item_in_optimizer::cleanup");
1612
1610
  Item_bool_func::cleanup();
1613
1611
  if (!save_cache)
1614
1612
    cache= 0;
1615
 
  return;
 
1613
  DBUG_VOID_RETURN;
1616
1614
}
1617
1615
 
1618
1616
 
1645
1643
    @retval NULL if an error occurred
1646
1644
*/
1647
1645
 
1648
 
Item *Item_in_optimizer::transform(Item_transformer transformer, unsigned char *argument)
 
1646
Item *Item_in_optimizer::transform(Item_transformer transformer, uchar *argument)
1649
1647
{
1650
1648
  Item *new_item;
1651
1649
 
1652
 
  assert(arg_count == 2);
 
1650
  DBUG_ASSERT(arg_count == 2);
1653
1651
 
1654
1652
  /* Transform the left IN operand. */
1655
1653
  new_item= (*args)->transform(transformer, argument);
1656
1654
  if (!new_item)
1657
1655
    return 0;
1658
1656
  /*
1659
 
    Session::change_item_tree() should be called only if the tree was
 
1657
    THD::change_item_tree() should be called only if the tree was
1660
1658
    really transformed, i.e. when a new item has been created.
1661
1659
    Otherwise we'll be allocating a lot of unnecessary memory for
1662
1660
    change records at each execution.
1663
1661
  */
1664
1662
  if ((*args) != new_item)
1665
 
    current_session->change_item_tree(args, new_item);
 
1663
    current_thd->change_item_tree(args, new_item);
1666
1664
 
1667
1665
  /*
1668
1666
    Transform the right IN operand which should be an Item_in_subselect or a
1671
1669
    transformation, we only make both operands the same.
1672
1670
    TODO: is it the way it should be?
1673
1671
  */
1674
 
  assert((args[1])->type() == Item::SUBSELECT_ITEM &&
 
1672
  DBUG_ASSERT((args[1])->type() == Item::SUBSELECT_ITEM &&
1675
1673
              (((Item_subselect*)(args[1]))->substype() ==
1676
1674
               Item_subselect::IN_SUBS ||
1677
1675
               ((Item_subselect*)(args[1]))->substype() ==
1687
1685
 
1688
1686
 
1689
1687
 
1690
 
int64_t Item_func_eq::val_int()
 
1688
longlong Item_func_eq::val_int()
1691
1689
{
1692
 
  assert(fixed == 1);
 
1690
  DBUG_ASSERT(fixed == 1);
1693
1691
  int value= cmp.compare();
1694
1692
  return value == 0 ? 1 : 0;
1695
1693
}
1703
1701
  maybe_null=null_value=0;
1704
1702
}
1705
1703
 
1706
 
int64_t Item_func_equal::val_int()
 
1704
longlong Item_func_equal::val_int()
1707
1705
{
1708
 
  assert(fixed == 1);
 
1706
  DBUG_ASSERT(fixed == 1);
1709
1707
  return cmp.compare();
1710
1708
}
1711
1709
 
1712
 
int64_t Item_func_ne::val_int()
 
1710
longlong Item_func_ne::val_int()
1713
1711
{
1714
 
  assert(fixed == 1);
 
1712
  DBUG_ASSERT(fixed == 1);
1715
1713
  int value= cmp.compare();
1716
1714
  return value != 0 && !null_value ? 1 : 0;
1717
1715
}
1718
1716
 
1719
1717
 
1720
 
int64_t Item_func_ge::val_int()
 
1718
longlong Item_func_ge::val_int()
1721
1719
{
1722
 
  assert(fixed == 1);
 
1720
  DBUG_ASSERT(fixed == 1);
1723
1721
  int value= cmp.compare();
1724
1722
  return value >= 0 ? 1 : 0;
1725
1723
}
1726
1724
 
1727
1725
 
1728
 
int64_t Item_func_gt::val_int()
 
1726
longlong Item_func_gt::val_int()
1729
1727
{
1730
 
  assert(fixed == 1);
 
1728
  DBUG_ASSERT(fixed == 1);
1731
1729
  int value= cmp.compare();
1732
1730
  return value > 0 ? 1 : 0;
1733
1731
}
1734
1732
 
1735
 
int64_t Item_func_le::val_int()
 
1733
longlong Item_func_le::val_int()
1736
1734
{
1737
 
  assert(fixed == 1);
 
1735
  DBUG_ASSERT(fixed == 1);
1738
1736
  int value= cmp.compare();
1739
1737
  return value <= 0 && !null_value ? 1 : 0;
1740
1738
}
1741
1739
 
1742
1740
 
1743
 
int64_t Item_func_lt::val_int()
 
1741
longlong Item_func_lt::val_int()
1744
1742
{
1745
 
  assert(fixed == 1);
 
1743
  DBUG_ASSERT(fixed == 1);
1746
1744
  int value= cmp.compare();
1747
1745
  return value < 0 && !null_value ? 1 : 0;
1748
1746
}
1749
1747
 
1750
1748
 
1751
 
int64_t Item_func_strcmp::val_int()
 
1749
longlong Item_func_strcmp::val_int()
1752
1750
{
1753
 
  assert(fixed == 1);
 
1751
  DBUG_ASSERT(fixed == 1);
1754
1752
  String *a=args[0]->val_str(&tmp_value1);
1755
1753
  String *b=args[1]->val_str(&tmp_value2);
1756
1754
  if (!a || !b)
1760
1758
  }
1761
1759
  int value= sortcmp(a,b,cmp.cmp_collation.collation);
1762
1760
  null_value=0;
1763
 
  return !value ? 0 : (value < 0 ? (int64_t) -1 : (int64_t) 1);
 
1761
  return !value ? 0 : (value < 0 ? (longlong) -1 : (longlong) 1);
1764
1762
}
1765
1763
 
1766
1764
 
1777
1775
    return 0;
1778
1776
  if (negated != ((Item_func_opt_neg *) item_func)->negated)
1779
1777
    return 0;
1780
 
  for (uint32_t i=0; i < arg_count ; i++)
 
1778
  for (uint i=0; i < arg_count ; i++)
1781
1779
    if (!args[i]->eq(item_func->arguments()[i], binary_cmp))
1782
1780
      return 0;
1783
1781
  return 1;
1786
1784
 
1787
1785
void Item_func_interval::fix_length_and_dec()
1788
1786
{
1789
 
  uint32_t rows= row->cols();
1790
 
 
 
1787
  uint rows= row->cols();
 
1788
  
1791
1789
  use_decimal_comparison= ((row->element_index(0)->result_type() ==
1792
1790
                            DECIMAL_RESULT) ||
1793
1791
                           (row->element_index(0)->result_type() ==
1796
1794
  {
1797
1795
    bool not_null_consts= true;
1798
1796
 
1799
 
    for (uint32_t i= 1; not_null_consts && i < rows; i++)
 
1797
    for (uint i= 1; not_null_consts && i < rows; i++)
1800
1798
    {
1801
1799
      Item *el= row->element_index(i);
1802
1800
      not_null_consts&= el->const_item() & !el->is_null();
1804
1802
 
1805
1803
    if (not_null_consts &&
1806
1804
        (intervals=
1807
 
          (interval_range*) memory::sql_alloc(sizeof(interval_range) * (rows - 1))))
 
1805
          (interval_range*) sql_alloc(sizeof(interval_range) * (rows - 1))))
1808
1806
    {
1809
1807
      if (use_decimal_comparison)
1810
1808
      {
1811
 
        for (uint32_t i= 1; i < rows; i++)
 
1809
        for (uint i= 1; i < rows; i++)
1812
1810
        {
1813
1811
          Item *el= row->element_index(i);
1814
1812
          interval_range *range= intervals + (i-1);
1833
1831
      }
1834
1832
      else
1835
1833
      {
1836
 
        for (uint32_t i= 1; i < rows; i++)
 
1834
        for (uint i= 1; i < rows; i++)
1837
1835
        {
1838
1836
          intervals[i-1].dbl= row->element_index(i)->val_real();
1839
1837
        }
1863
1861
    - arg_count if higher than biggest argument
1864
1862
*/
1865
1863
 
1866
 
int64_t Item_func_interval::val_int()
 
1864
longlong Item_func_interval::val_int()
1867
1865
{
1868
 
  assert(fixed == 1);
 
1866
  DBUG_ASSERT(fixed == 1);
1869
1867
  double value;
1870
1868
  my_decimal dec_buf, *dec= NULL;
1871
 
  uint32_t i;
 
1869
  uint i;
1872
1870
 
1873
1871
  if (use_decimal_comparison)
1874
1872
  {
1886
1884
 
1887
1885
  if (intervals)
1888
1886
  {                                     // Use binary search to find interval
1889
 
    uint32_t start,end;
 
1887
    uint start,end;
1890
1888
    start= 0;
1891
1889
    end=   row->cols()-2;
1892
1890
    while (start != end)
1893
1891
    {
1894
 
      uint32_t mid= (start + end + 1) / 2;
 
1892
      uint mid= (start + end + 1) / 2;
1895
1893
      interval_range *range= intervals + mid;
1896
 
      bool cmp_result;
 
1894
      my_bool cmp_result;
1897
1895
      /*
1898
1896
        The values in the range intervall may have different types,
1899
1897
        Only do a decimal comparision of the first argument is a decimal
1928
1926
      if (my_decimal_cmp(e_dec, dec) > 0)
1929
1927
        return i - 1;
1930
1928
    }
1931
 
    else
 
1929
    else 
1932
1930
    {
1933
1931
      double val= el->val_real();
1934
1932
      /* Skip NULL ranges. */
1950
1948
    The function saves in ref the pointer to the item or to a newly created
1951
1949
    item that is considered as a replacement for the original one.
1952
1950
 
1953
 
  @param session     reference to the global context of the query thread
 
1951
  @param thd     reference to the global context of the query thread
1954
1952
  @param ref     pointer to Item* variable where pointer to resulting "fixed"
1955
1953
                 item is to be assigned
1956
1954
 
1970
1968
    1   got error
1971
1969
*/
1972
1970
 
1973
 
bool Item_func_between::fix_fields(Session *session, Item **ref)
 
1971
bool Item_func_between::fix_fields(THD *thd, Item **ref)
1974
1972
{
1975
 
  if (Item_func_opt_neg::fix_fields(session, ref))
 
1973
  if (Item_func_opt_neg::fix_fields(thd, ref))
1976
1974
    return 1;
1977
1975
 
1978
 
  session->lex->current_select->between_count++;
 
1976
  thd->lex->current_select->between_count++;
1979
1977
 
1980
1978
  /* not_null_tables_cache == union(T1(e),T1(e1),T1(e2)) */
1981
1979
  if (pred_level && !negated)
1995
1993
  max_length= 1;
1996
1994
  int i;
1997
1995
  bool datetime_found= false;
 
1996
  int time_items_found= 0;
1998
1997
  compare_as_dates= true;
1999
 
  Session *session= current_session;
 
1998
  THD *thd= current_thd;
2000
1999
 
2001
2000
  /*
2002
2001
    As some compare functions are generated after sql_yacc,
2024
2023
        datetime_found= true;
2025
2024
        continue;
2026
2025
      }
 
2026
      if (args[i]->field_type() == MYSQL_TYPE_TIME &&
 
2027
          args[i]->result_as_longlong())
 
2028
        time_items_found++;
2027
2029
    }
2028
2030
  }
2029
2031
  if (!datetime_found)
2034
2036
    ge_cmp.set_datetime_cmp_func(args, args + 1);
2035
2037
    le_cmp.set_datetime_cmp_func(args, args + 2);
2036
2038
  }
 
2039
  else if (time_items_found == 3)
 
2040
  {
 
2041
    /* Compare TIME items as integers. */
 
2042
    cmp_type= INT_RESULT;
 
2043
  }
2037
2044
  else if (args[0]->real_item()->type() == FIELD_ITEM &&
2038
 
           session->lex->sql_command != SQLCOM_SHOW_CREATE)
 
2045
           thd->lex->sql_command != SQLCOM_SHOW_CREATE)
2039
2046
  {
2040
2047
    Item_field *field_item= (Item_field*) (args[0]->real_item());
2041
 
    if (field_item->field->can_be_compared_as_int64_t())
 
2048
    if (field_item->field->can_be_compared_as_longlong())
2042
2049
    {
2043
2050
      /*
2044
2051
        The following can't be recoded with || as convert_constant_item
2045
2052
        changes the argument
2046
2053
      */
2047
 
      if (convert_constant_item(session, field_item, &args[1]))
 
2054
      if (convert_constant_item(thd, field_item, &args[1]))
2048
2055
        cmp_type=INT_RESULT;                    // Works for all types.
2049
 
      if (convert_constant_item(session, field_item, &args[2]))
 
2056
      if (convert_constant_item(thd, field_item, &args[2]))
2050
2057
        cmp_type=INT_RESULT;                    // Works for all types.
2051
2058
    }
2052
2059
  }
2053
2060
}
2054
2061
 
2055
2062
 
2056
 
int64_t Item_func_between::val_int()
 
2063
longlong Item_func_between::val_int()
2057
2064
{                                               // ANSI BETWEEN
2058
 
  assert(fixed == 1);
 
2065
  DBUG_ASSERT(fixed == 1);
2059
2066
  if (compare_as_dates)
2060
2067
  {
2061
2068
    int ge_res, le_res;
2066
2073
    le_res= le_cmp.compare();
2067
2074
 
2068
2075
    if (!args[1]->null_value && !args[2]->null_value)
2069
 
      return (int64_t) ((ge_res >= 0 && le_res <=0) != negated);
 
2076
      return (longlong) ((ge_res >= 0 && le_res <=0) != negated);
2070
2077
    else if (args[1]->null_value)
2071
2078
    {
2072
2079
      null_value= le_res > 0;                   // not null if false range.
2085
2092
    a=args[1]->val_str(&value1);
2086
2093
    b=args[2]->val_str(&value2);
2087
2094
    if (!args[1]->null_value && !args[2]->null_value)
2088
 
      return (int64_t) ((sortcmp(value,a,cmp_collation.collation) >= 0 &&
 
2095
      return (longlong) ((sortcmp(value,a,cmp_collation.collation) >= 0 &&
2089
2096
                          sortcmp(value,b,cmp_collation.collation) <= 0) !=
2090
2097
                         negated);
2091
2098
    if (args[1]->null_value && args[2]->null_value)
2103
2110
  }
2104
2111
  else if (cmp_type == INT_RESULT)
2105
2112
  {
2106
 
    int64_t value=args[0]->val_int(), a, b;
 
2113
    longlong value=args[0]->val_int(), a, b;
2107
2114
    if ((null_value=args[0]->null_value))
2108
 
      return 0;
 
2115
      return 0;                                 /* purecov: inspected */
2109
2116
    a=args[1]->val_int();
2110
2117
    b=args[2]->val_int();
2111
2118
    if (!args[1]->null_value && !args[2]->null_value)
2112
 
      return (int64_t) ((value >= a && value <= b) != negated);
 
2119
      return (longlong) ((value >= a && value <= b) != negated);
2113
2120
    if (args[1]->null_value && args[2]->null_value)
2114
2121
      null_value=1;
2115
2122
    else if (args[1]->null_value)
2126
2133
    my_decimal dec_buf, *dec= args[0]->val_decimal(&dec_buf),
2127
2134
               a_buf, *a_dec, b_buf, *b_dec;
2128
2135
    if ((null_value=args[0]->null_value))
2129
 
      return 0;
 
2136
      return 0;                                 /* purecov: inspected */
2130
2137
    a_dec= args[1]->val_decimal(&a_buf);
2131
2138
    b_dec= args[2]->val_decimal(&b_buf);
2132
2139
    if (!args[1]->null_value && !args[2]->null_value)
2133
 
      return (int64_t) ((my_decimal_cmp(dec, a_dec) >= 0 &&
 
2140
      return (longlong) ((my_decimal_cmp(dec, a_dec) >= 0 &&
2134
2141
                          my_decimal_cmp(dec, b_dec) <= 0) != negated);
2135
2142
    if (args[1]->null_value && args[2]->null_value)
2136
2143
      null_value=1;
2143
2150
  {
2144
2151
    double value= args[0]->val_real(),a,b;
2145
2152
    if ((null_value=args[0]->null_value))
2146
 
      return 0;
 
2153
      return 0;                                 /* purecov: inspected */
2147
2154
    a= args[1]->val_real();
2148
2155
    b= args[2]->val_real();
2149
2156
    if (!args[1]->null_value && !args[2]->null_value)
2150
 
      return (int64_t) ((value >= a && value <= b) != negated);
 
2157
      return (longlong) ((value >= a && value <= b) != negated);
2151
2158
    if (args[1]->null_value && args[2]->null_value)
2152
2159
      null_value=1;
2153
2160
    else if (args[1]->null_value)
2159
2166
      null_value= value >= a;
2160
2167
    }
2161
2168
  }
2162
 
  return (int64_t) (!null_value && negated);
 
2169
  return (longlong) (!null_value && negated);
2163
2170
}
2164
2171
 
2165
2172
 
2180
2187
Item_func_ifnull::fix_length_and_dec()
2181
2188
{
2182
2189
  agg_result_type(&hybrid_type, args, 2);
2183
 
  maybe_null= args[1]->maybe_null;
 
2190
  maybe_null=args[1]->maybe_null;
2184
2191
  decimals= max(args[0]->decimals, args[1]->decimals);
2185
2192
  unsigned_flag= args[0]->unsigned_flag && args[1]->unsigned_flag;
2186
2193
 
2187
 
  if (hybrid_type == DECIMAL_RESULT || hybrid_type == INT_RESULT)
 
2194
  if (hybrid_type == DECIMAL_RESULT || hybrid_type == INT_RESULT) 
2188
2195
  {
2189
2196
    int len0= args[0]->max_length - args[0]->decimals
2190
2197
      - (args[0]->unsigned_flag ? 0 : 1);
2197
2204
  else
2198
2205
    max_length= max(args[0]->max_length, args[1]->max_length);
2199
2206
 
2200
 
  switch (hybrid_type)
2201
 
  {
 
2207
  switch (hybrid_type) {
2202
2208
  case STRING_RESULT:
2203
2209
    agg_arg_charsets(collation, args, arg_count, MY_COLL_CMP_CONV, 1);
2204
2210
    break;
2210
2216
    break;
2211
2217
  case ROW_RESULT:
2212
2218
  default:
2213
 
    assert(0);
 
2219
    DBUG_ASSERT(0);
2214
2220
  }
2215
2221
  cached_field_type= agg_field_type(args, 2);
2216
2222
}
2217
2223
 
2218
2224
 
2219
 
uint32_t Item_func_ifnull::decimal_precision() const
 
2225
uint Item_func_ifnull::decimal_precision() const
2220
2226
{
2221
 
  int max_int_part= max(args[0]->decimal_int_part(),args[1]->decimal_int_part());
 
2227
  int max_int_part=max(args[0]->decimal_int_part(),args[1]->decimal_int_part());
2222
2228
  return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2223
2229
}
2224
2230
 
2225
2231
 
2226
 
enum_field_types Item_func_ifnull::field_type() const
 
2232
enum_field_types Item_func_ifnull::field_type() const 
2227
2233
{
2228
2234
  return cached_field_type;
2229
2235
}
2230
2236
 
2231
 
Field *Item_func_ifnull::tmp_table_field(Table *table)
 
2237
Field *Item_func_ifnull::tmp_table_field(TABLE *table)
2232
2238
{
2233
2239
  return tmp_table_field_from_field_type(table, 0);
2234
2240
}
2236
2242
double
2237
2243
Item_func_ifnull::real_op()
2238
2244
{
2239
 
  assert(fixed == 1);
 
2245
  DBUG_ASSERT(fixed == 1);
2240
2246
  double value= args[0]->val_real();
2241
2247
  if (!args[0]->null_value)
2242
2248
  {
2249
2255
  return value;
2250
2256
}
2251
2257
 
2252
 
int64_t
 
2258
longlong
2253
2259
Item_func_ifnull::int_op()
2254
2260
{
2255
 
  assert(fixed == 1);
2256
 
  int64_t value=args[0]->val_int();
 
2261
  DBUG_ASSERT(fixed == 1);
 
2262
  longlong value=args[0]->val_int();
2257
2263
  if (!args[0]->null_value)
2258
2264
  {
2259
2265
    null_value=0;
2268
2274
 
2269
2275
my_decimal *Item_func_ifnull::decimal_op(my_decimal *decimal_value)
2270
2276
{
2271
 
  assert(fixed == 1);
 
2277
  DBUG_ASSERT(fixed == 1);
2272
2278
  my_decimal *value= args[0]->val_decimal(decimal_value);
2273
2279
  if (!args[0]->null_value)
2274
2280
  {
2285
2291
String *
2286
2292
Item_func_ifnull::str_op(String *str)
2287
2293
{
2288
 
  assert(fixed == 1);
 
2294
  DBUG_ASSERT(fixed == 1);
2289
2295
  String *res  =args[0]->val_str(str);
2290
2296
  if (!args[0]->null_value)
2291
2297
  {
2309
2315
    The function saves in ref the pointer to the item or to a newly created
2310
2316
    item that is considered as a replacement for the original one.
2311
2317
 
2312
 
  @param session     reference to the global context of the query thread
 
2318
  @param thd     reference to the global context of the query thread
2313
2319
  @param ref     pointer to Item* variable where pointer to resulting "fixed"
2314
2320
                 item is to be assigned
2315
2321
 
2328
2334
*/
2329
2335
 
2330
2336
bool
2331
 
Item_func_if::fix_fields(Session *session, Item **ref)
 
2337
Item_func_if::fix_fields(THD *thd, Item **ref)
2332
2338
{
2333
 
  assert(fixed == 0);
 
2339
  DBUG_ASSERT(fixed == 0);
2334
2340
  args[0]->top_level_item();
2335
2341
 
2336
 
  if (Item_func::fix_fields(session, ref))
 
2342
  if (Item_func::fix_fields(thd, ref))
2337
2343
    return 1;
2338
2344
 
2339
2345
  not_null_tables_cache= (args[1]->not_null_tables() &
2346
2352
void
2347
2353
Item_func_if::fix_length_and_dec()
2348
2354
{
2349
 
  maybe_null= args[1]->maybe_null || args[2]->maybe_null;
 
2355
  maybe_null=args[1]->maybe_null || args[2]->maybe_null;
2350
2356
  decimals= max(args[1]->decimals, args[2]->decimals);
2351
 
  unsigned_flag= args[1]->unsigned_flag && args[2]->unsigned_flag;
 
2357
  unsigned_flag=args[1]->unsigned_flag && args[2]->unsigned_flag;
2352
2358
 
2353
 
  enum Item_result arg1_type= args[1]->result_type();
2354
 
  enum Item_result arg2_type= args[2]->result_type();
2355
 
  bool null1= args[1]->const_item() && args[1]->null_value;
2356
 
  bool null2= args[2]->const_item() && args[2]->null_value;
 
2359
  enum Item_result arg1_type=args[1]->result_type();
 
2360
  enum Item_result arg2_type=args[2]->result_type();
 
2361
  bool null1=args[1]->const_item() && args[1]->null_value;
 
2362
  bool null2=args[2]->const_item() && args[2]->null_value;
2357
2363
 
2358
2364
  if (null1)
2359
2365
  {
2391
2397
    int len2= args[2]->max_length - args[2]->decimals
2392
2398
      - (args[2]->unsigned_flag ? 0 : 1);
2393
2399
 
2394
 
    max_length= max(len1, len2) + decimals + (unsigned_flag ? 0 : 1);
 
2400
    max_length=max(len1, len2) + decimals + (unsigned_flag ? 0 : 1);
2395
2401
  }
2396
2402
  else
2397
2403
    max_length= max(args[1]->max_length, args[2]->max_length);
2398
2404
}
2399
2405
 
2400
2406
 
2401
 
uint32_t Item_func_if::decimal_precision() const
 
2407
uint Item_func_if::decimal_precision() const
2402
2408
{
2403
 
  int precision= (max(args[1]->decimal_int_part(),args[2]->decimal_int_part())+
2404
 
                  decimals);
 
2409
  int precision=(max(args[1]->decimal_int_part(),args[2]->decimal_int_part())+
 
2410
                 decimals);
2405
2411
  return min(precision, DECIMAL_MAX_PRECISION);
2406
2412
}
2407
2413
 
2409
2415
double
2410
2416
Item_func_if::val_real()
2411
2417
{
2412
 
  assert(fixed == 1);
 
2418
  DBUG_ASSERT(fixed == 1);
2413
2419
  Item *arg= args[0]->val_bool() ? args[1] : args[2];
2414
2420
  double value= arg->val_real();
2415
2421
  null_value=arg->null_value;
2416
2422
  return value;
2417
2423
}
2418
2424
 
2419
 
int64_t
 
2425
longlong
2420
2426
Item_func_if::val_int()
2421
2427
{
2422
 
  assert(fixed == 1);
 
2428
  DBUG_ASSERT(fixed == 1);
2423
2429
  Item *arg= args[0]->val_bool() ? args[1] : args[2];
2424
 
  int64_t value=arg->val_int();
 
2430
  longlong value=arg->val_int();
2425
2431
  null_value=arg->null_value;
2426
2432
  return value;
2427
2433
}
2429
2435
String *
2430
2436
Item_func_if::val_str(String *str)
2431
2437
{
2432
 
  assert(fixed == 1);
 
2438
  DBUG_ASSERT(fixed == 1);
2433
2439
  Item *arg= args[0]->val_bool() ? args[1] : args[2];
2434
2440
  String *res=arg->val_str(str);
2435
2441
  if (res)
2442
2448
my_decimal *
2443
2449
Item_func_if::val_decimal(my_decimal *decimal_value)
2444
2450
{
2445
 
  assert(fixed == 1);
 
2451
  DBUG_ASSERT(fixed == 1);
2446
2452
  Item *arg= args[0]->val_bool() ? args[1] : args[2];
2447
2453
  my_decimal *value= arg->val_decimal(decimal_value);
2448
2454
  null_value= arg->null_value;
2481
2487
double
2482
2488
Item_func_nullif::val_real()
2483
2489
{
2484
 
  assert(fixed == 1);
 
2490
  DBUG_ASSERT(fixed == 1);
2485
2491
  double value;
2486
2492
  if (!cmp.compare())
2487
2493
  {
2493
2499
  return value;
2494
2500
}
2495
2501
 
2496
 
int64_t
 
2502
longlong
2497
2503
Item_func_nullif::val_int()
2498
2504
{
2499
 
  assert(fixed == 1);
2500
 
  int64_t value;
 
2505
  DBUG_ASSERT(fixed == 1);
 
2506
  longlong value;
2501
2507
  if (!cmp.compare())
2502
2508
  {
2503
2509
    null_value=1;
2511
2517
String *
2512
2518
Item_func_nullif::val_str(String *str)
2513
2519
{
2514
 
  assert(fixed == 1);
 
2520
  DBUG_ASSERT(fixed == 1);
2515
2521
  String *res;
2516
2522
  if (!cmp.compare())
2517
2523
  {
2527
2533
my_decimal *
2528
2534
Item_func_nullif::val_decimal(my_decimal * decimal_value)
2529
2535
{
2530
 
  assert(fixed == 1);
 
2536
  DBUG_ASSERT(fixed == 1);
2531
2537
  my_decimal *res;
2532
2538
  if (!cmp.compare())
2533
2539
  {
2543
2549
bool
2544
2550
Item_func_nullif::is_null()
2545
2551
{
2546
 
  return (null_value= (!cmp.compare() ? 1 : args[0]->null_value));
 
2552
  return (null_value= (!cmp.compare() ? 1 : args[0]->null_value)); 
2547
2553
}
2548
2554
 
2549
2555
 
2568
2574
           failed
2569
2575
*/
2570
2576
 
2571
 
Item *Item_func_case::find_item(String *)
 
2577
Item *Item_func_case::find_item(String *str __attribute__((__unused__)))
2572
2578
{
2573
 
  uint32_t value_added_map= 0;
 
2579
  uint value_added_map= 0;
2574
2580
 
2575
2581
  if (first_expr_num == -1)
2576
2582
  {
2577
 
    for (uint32_t i=0 ; i < ncases ; i+=2)
 
2583
    for (uint i=0 ; i < ncases ; i+=2)
2578
2584
    {
2579
2585
      // No expression between CASE and the first WHEN
2580
2586
      if (args[i]->val_bool())
2585
2591
  else
2586
2592
  {
2587
2593
    /* Compare every WHEN argument with it and return the first match */
2588
 
    for (uint32_t i=0 ; i < ncases ; i+=2)
 
2594
    for (uint i=0 ; i < ncases ; i+=2)
2589
2595
    {
2590
2596
      cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
2591
 
      assert(cmp_type != ROW_RESULT);
2592
 
      assert(cmp_items[(uint32_t)cmp_type]);
2593
 
      if (!(value_added_map & (1<<(uint32_t)cmp_type)))
 
2597
      DBUG_ASSERT(cmp_type != ROW_RESULT);
 
2598
      DBUG_ASSERT(cmp_items[(uint)cmp_type]);
 
2599
      if (!(value_added_map & (1<<(uint)cmp_type)))
2594
2600
      {
2595
 
        cmp_items[(uint32_t)cmp_type]->store_value(args[first_expr_num]);
 
2601
        cmp_items[(uint)cmp_type]->store_value(args[first_expr_num]);
2596
2602
        if ((null_value=args[first_expr_num]->null_value))
2597
2603
          return else_expr_num != -1 ? args[else_expr_num] : 0;
2598
 
        value_added_map|= 1<<(uint32_t)cmp_type;
 
2604
        value_added_map|= 1<<(uint)cmp_type;
2599
2605
      }
2600
 
      if (!cmp_items[(uint32_t)cmp_type]->cmp(args[i]) && !args[i]->null_value)
 
2606
      if (!cmp_items[(uint)cmp_type]->cmp(args[i]) && !args[i]->null_value)
2601
2607
        return args[i + 1];
2602
2608
    }
2603
2609
  }
2608
2614
 
2609
2615
String *Item_func_case::val_str(String *str)
2610
2616
{
2611
 
  assert(fixed == 1);
 
2617
  DBUG_ASSERT(fixed == 1);
2612
2618
  String *res;
2613
2619
  Item *item=find_item(str);
2614
2620
 
2624
2630
}
2625
2631
 
2626
2632
 
2627
 
int64_t Item_func_case::val_int()
 
2633
longlong Item_func_case::val_int()
2628
2634
{
2629
 
  assert(fixed == 1);
 
2635
  DBUG_ASSERT(fixed == 1);
2630
2636
  char buff[MAX_FIELD_WIDTH];
2631
2637
  String dummy_str(buff,sizeof(buff),default_charset());
2632
2638
  Item *item=find_item(&dummy_str);
2633
 
  int64_t res;
 
2639
  longlong res;
2634
2640
 
2635
2641
  if (!item)
2636
2642
  {
2644
2650
 
2645
2651
double Item_func_case::val_real()
2646
2652
{
2647
 
  assert(fixed == 1);
 
2653
  DBUG_ASSERT(fixed == 1);
2648
2654
  char buff[MAX_FIELD_WIDTH];
2649
2655
  String dummy_str(buff,sizeof(buff),default_charset());
2650
2656
  Item *item=find_item(&dummy_str);
2663
2669
 
2664
2670
my_decimal *Item_func_case::val_decimal(my_decimal *decimal_value)
2665
2671
{
2666
 
  assert(fixed == 1);
 
2672
  DBUG_ASSERT(fixed == 1);
2667
2673
  char buff[MAX_FIELD_WIDTH];
2668
2674
  String dummy_str(buff, sizeof(buff), default_charset());
2669
2675
  Item *item= find_item(&dummy_str);
2681
2687
}
2682
2688
 
2683
2689
 
2684
 
bool Item_func_case::fix_fields(Session *session, Item **ref)
 
2690
bool Item_func_case::fix_fields(THD *thd, Item **ref)
2685
2691
{
2686
2692
  /*
2687
2693
    buff should match stack usage from
2688
2694
    Item_func_case::val_int() -> Item_func_case::find_item()
2689
2695
  */
2690
 
  unsigned char buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2
2691
 
                     +sizeof(double)*2+sizeof(int64_t)*2];
2692
 
  bool res= Item_func::fix_fields(session, ref);
 
2696
  uchar buff[MAX_FIELD_WIDTH*2+sizeof(String)*2+sizeof(String*)*2+sizeof(double)*2+sizeof(longlong)*2];
 
2697
  bool res= Item_func::fix_fields(thd, ref);
2693
2698
  /*
2694
2699
    Call check_stack_overrun after fix_fields to be sure that stack variable
2695
2700
    is not optimized away
2696
2701
  */
2697
 
  if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
 
2702
  if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2698
2703
    return true;                                // Fatal error flag is set!
2699
2704
  return res;
2700
2705
}
2710
2715
 
2711
2716
void Item_func_case::agg_num_lengths(Item *arg)
2712
2717
{
2713
 
  uint32_t len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
 
2718
  uint len= my_decimal_length_to_precision(arg->max_length, arg->decimals,
2714
2719
                                           arg->unsigned_flag) - arg->decimals;
2715
 
  set_if_bigger(max_length, len);
 
2720
  set_if_bigger(max_length, len); 
2716
2721
  set_if_bigger(decimals, arg->decimals);
2717
 
  unsigned_flag= unsigned_flag && arg->unsigned_flag;
 
2722
  unsigned_flag= unsigned_flag && arg->unsigned_flag; 
2718
2723
}
2719
2724
 
2720
2725
 
2721
2726
void Item_func_case::fix_length_and_dec()
2722
2727
{
2723
2728
  Item **agg;
2724
 
  uint32_t nagg;
2725
 
  uint32_t found_types= 0;
2726
 
  if (!(agg= (Item**) memory::sql_alloc(sizeof(Item*)*(ncases+1))))
 
2729
  uint nagg;
 
2730
  uint found_types= 0;
 
2731
  if (!(agg= (Item**) sql_alloc(sizeof(Item*)*(ncases+1))))
2727
2732
    return;
2728
 
 
 
2733
  
2729
2734
  /*
2730
2735
    Aggregate all THEN and ELSE expression types
2731
2736
    and collations when string result
2732
2737
  */
2733
 
 
 
2738
  
2734
2739
  for (nagg= 0 ; nagg < ncases/2 ; nagg++)
2735
2740
    agg[nagg]= args[nagg*2+1];
2736
 
 
 
2741
  
2737
2742
  if (else_expr_num != -1)
2738
2743
    agg[nagg++]= args[else_expr_num];
2739
 
 
 
2744
  
2740
2745
  agg_result_type(&cached_result_type, agg, nagg);
2741
2746
  if ((cached_result_type == STRING_RESULT) &&
2742
2747
      agg_arg_charsets(collation, agg, nagg, MY_COLL_ALLOW_CONV, 1))
2743
2748
    return;
2744
 
 
 
2749
  
2745
2750
  cached_field_type= agg_field_type(agg, nagg);
2746
2751
  /*
2747
2752
    Aggregate first expression and all THEN expression types
2749
2754
  */
2750
2755
  if (first_expr_num != -1)
2751
2756
  {
2752
 
    uint32_t i;
 
2757
    uint i;
2753
2758
    agg[0]= args[first_expr_num];
2754
2759
    left_result_type= agg[0]->result_type();
2755
2760
 
2759
2764
    if (!(found_types= collect_cmp_types(agg, nagg)))
2760
2765
      return;
2761
2766
 
2762
 
    for (i= 0; i <= (uint32_t)DECIMAL_RESULT; i++)
 
2767
    for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
2763
2768
    {
2764
2769
      if (found_types & (1 << i) && !cmp_items[i])
2765
2770
      {
2766
 
        assert((Item_result)i != ROW_RESULT);
 
2771
        DBUG_ASSERT((Item_result)i != ROW_RESULT);
2767
2772
        if ((Item_result)i == STRING_RESULT &&
2768
2773
            agg_arg_charsets(cmp_collation, agg, nagg, MY_COLL_CMP_CONV, 1))
2769
2774
          return;
2777
2782
 
2778
2783
  if (else_expr_num == -1 || args[else_expr_num]->maybe_null)
2779
2784
    maybe_null=1;
2780
 
 
 
2785
  
2781
2786
  max_length=0;
2782
2787
  decimals=0;
2783
2788
  unsigned_flag= true;
2784
2789
  if (cached_result_type == STRING_RESULT)
2785
2790
  {
2786
 
    for (uint32_t i= 0; i < ncases; i+= 2)
 
2791
    for (uint i= 0; i < ncases; i+= 2)
2787
2792
      agg_str_lengths(args[i + 1]);
2788
2793
    if (else_expr_num != -1)
2789
2794
      agg_str_lengths(args[else_expr_num]);
2790
2795
  }
2791
2796
  else
2792
2797
  {
2793
 
    for (uint32_t i= 0; i < ncases; i+= 2)
 
2798
    for (uint i= 0; i < ncases; i+= 2)
2794
2799
      agg_num_lengths(args[i + 1]);
2795
 
    if (else_expr_num != -1)
 
2800
    if (else_expr_num != -1) 
2796
2801
      agg_num_lengths(args[else_expr_num]);
2797
2802
    max_length= my_decimal_precision_to_length(max_length + decimals, decimals,
2798
2803
                                               unsigned_flag);
2800
2805
}
2801
2806
 
2802
2807
 
2803
 
uint32_t Item_func_case::decimal_precision() const
 
2808
uint Item_func_case::decimal_precision() const
2804
2809
{
2805
2810
  int max_int_part=0;
2806
 
  for (uint32_t i=0 ; i < ncases ; i+=2)
 
2811
  for (uint i=0 ; i < ncases ; i+=2)
2807
2812
    set_if_bigger(max_int_part, args[i+1]->decimal_int_part());
2808
2813
 
2809
 
  if (else_expr_num != -1)
 
2814
  if (else_expr_num != -1) 
2810
2815
    set_if_bigger(max_int_part, args[else_expr_num]->decimal_int_part());
2811
2816
  return min(max_int_part + decimals, DECIMAL_MAX_PRECISION);
2812
2817
}
2825
2830
    args[first_expr_num]->print(str, query_type);
2826
2831
    str->append(' ');
2827
2832
  }
2828
 
  for (uint32_t i=0 ; i < ncases ; i+=2)
 
2833
  for (uint i=0 ; i < ncases ; i+=2)
2829
2834
  {
2830
2835
    str->append(STRING_WITH_LEN("when "));
2831
2836
    args[i]->print(str, query_type);
2845
2850
 
2846
2851
void Item_func_case::cleanup()
2847
2852
{
2848
 
  uint32_t i;
 
2853
  uint i;
 
2854
  DBUG_ENTER("Item_func_case::cleanup");
2849
2855
  Item_func::cleanup();
2850
 
  for (i= 0; i <= (uint32_t)DECIMAL_RESULT; i++)
 
2856
  for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
2851
2857
  {
2852
2858
    delete cmp_items[i];
2853
2859
    cmp_items[i]= 0;
2854
2860
  }
2855
 
  return;
 
2861
  DBUG_VOID_RETURN;
2856
2862
}
2857
2863
 
2858
2864
 
2862
2868
 
2863
2869
String *Item_func_coalesce::str_op(String *str)
2864
2870
{
2865
 
  assert(fixed == 1);
 
2871
  DBUG_ASSERT(fixed == 1);
2866
2872
  null_value=0;
2867
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
2873
  for (uint i=0 ; i < arg_count ; i++)
2868
2874
  {
2869
2875
    String *res;
2870
2876
    if ((res=args[i]->val_str(str)))
2874
2880
  return 0;
2875
2881
}
2876
2882
 
2877
 
int64_t Item_func_coalesce::int_op()
 
2883
longlong Item_func_coalesce::int_op()
2878
2884
{
2879
 
  assert(fixed == 1);
 
2885
  DBUG_ASSERT(fixed == 1);
2880
2886
  null_value=0;
2881
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
2887
  for (uint i=0 ; i < arg_count ; i++)
2882
2888
  {
2883
 
    int64_t res=args[i]->val_int();
 
2889
    longlong res=args[i]->val_int();
2884
2890
    if (!args[i]->null_value)
2885
2891
      return res;
2886
2892
  }
2890
2896
 
2891
2897
double Item_func_coalesce::real_op()
2892
2898
{
2893
 
  assert(fixed == 1);
 
2899
  DBUG_ASSERT(fixed == 1);
2894
2900
  null_value=0;
2895
 
  for (uint32_t i=0 ; i < arg_count ; i++)
 
2901
  for (uint i=0 ; i < arg_count ; i++)
2896
2902
  {
2897
2903
    double res= args[i]->val_real();
2898
2904
    if (!args[i]->null_value)
2905
2911
 
2906
2912
my_decimal *Item_func_coalesce::decimal_op(my_decimal *decimal_value)
2907
2913
{
2908
 
  assert(fixed == 1);
 
2914
  DBUG_ASSERT(fixed == 1);
2909
2915
  null_value= 0;
2910
 
  for (uint32_t i= 0; i < arg_count; i++)
 
2916
  for (uint i= 0; i < arg_count; i++)
2911
2917
  {
2912
2918
    my_decimal *res= args[i]->val_decimal(decimal_value);
2913
2919
    if (!args[i]->null_value)
2940
2946
    break;
2941
2947
  case ROW_RESULT:
2942
2948
  default:
2943
 
    assert(0);
 
2949
    DBUG_ASSERT(0);
2944
2950
  }
2945
2951
}
2946
2952
 
2949
2955
****************************************************************************/
2950
2956
 
2951
2957
/*
2952
 
  Determine which of the signed int64_t arguments is bigger
 
2958
  Determine which of the signed longlong arguments is bigger
2953
2959
 
2954
2960
  SYNOPSIS
2955
2961
    cmp_longs()
2957
2963
      b_val     right argument
2958
2964
 
2959
2965
  DESCRIPTION
2960
 
    This function will compare two signed int64_t arguments
 
2966
    This function will compare two signed longlong arguments
2961
2967
    and will return -1, 0, or 1 if left argument is smaller than,
2962
2968
    equal to or greater than the right argument.
2963
2969
 
2966
2972
    0           left argument is equal to the right argument.
2967
2973
    1           left argument is greater than the right argument.
2968
2974
*/
2969
 
static inline int cmp_longs (int64_t a_val, int64_t b_val)
 
2975
static inline int cmp_longs (longlong a_val, longlong b_val)
2970
2976
{
2971
2977
  return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
2972
2978
}
2973
2979
 
2974
2980
 
2975
2981
/*
2976
 
  Determine which of the unsigned int64_t arguments is bigger
 
2982
  Determine which of the unsigned longlong arguments is bigger
2977
2983
 
2978
2984
  SYNOPSIS
2979
2985
    cmp_ulongs()
2981
2987
      b_val     right argument
2982
2988
 
2983
2989
  DESCRIPTION
2984
 
    This function will compare two unsigned int64_t arguments
 
2990
    This function will compare two unsigned longlong arguments
2985
2991
    and will return -1, 0, or 1 if left argument is smaller than,
2986
2992
    equal to or greater than the right argument.
2987
2993
 
2990
2996
    0           left argument is equal to the right argument.
2991
2997
    1           left argument is greater than the right argument.
2992
2998
*/
2993
 
static inline int cmp_ulongs (uint64_t a_val, uint64_t b_val)
 
2999
static inline int cmp_ulongs (ulonglong a_val, ulonglong b_val)
2994
3000
{
2995
3001
  return a_val < b_val ? -1 : a_val == b_val ? 0 : 1;
2996
3002
}
2997
3003
 
2998
3004
 
2999
3005
/*
3000
 
  Compare two integers in IN value list format (packed_int64_t)
 
3006
  Compare two integers in IN value list format (packed_longlong) 
3001
3007
 
3002
3008
  SYNOPSIS
3003
 
    cmp_int64_t()
 
3009
    cmp_longlong()
3004
3010
      cmp_arg   an argument passed to the calling function (my_qsort2)
3005
3011
      a         left argument
3006
3012
      b         right argument
3010
3016
    format and will return -1, 0, or 1 if left argument is smaller than,
3011
3017
    equal to or greater than the right argument.
3012
3018
    It's used in sorting the IN values list and finding an element in it.
3013
 
    Depending on the signedness of the arguments cmp_int64_t() will
 
3019
    Depending on the signedness of the arguments cmp_longlong() will
3014
3020
    compare them as either signed (using cmp_longs()) or unsigned (using
3015
3021
    cmp_ulongs()).
3016
3022
 
3019
3025
    0           left argument is equal to the right argument.
3020
3026
    1           left argument is greater than the right argument.
3021
3027
*/
3022
 
int cmp_int64_t(void *, in_int64_t::packed_int64_t *a,
3023
 
                in_int64_t::packed_int64_t *b)
 
3028
int cmp_longlong(void *cmp_arg __attribute__((__unused__)),
 
3029
                 in_longlong::packed_longlong *a,
 
3030
                 in_longlong::packed_longlong *b)
3024
3031
{
3025
3032
  if (a->unsigned_flag != b->unsigned_flag)
3026
 
  {
3027
 
    /*
3028
 
      One of the args is unsigned and is too big to fit into the
 
3033
  { 
 
3034
    /* 
 
3035
      One of the args is unsigned and is too big to fit into the 
3029
3036
      positive signed range. Report no match.
3030
 
    */
3031
 
    if ((a->unsigned_flag && ((uint64_t) a->val) > (uint64_t) INT64_MAX) ||
3032
 
        (b->unsigned_flag && ((uint64_t) b->val) > (uint64_t) INT64_MAX))
 
3037
    */  
 
3038
    if ((a->unsigned_flag && ((ulonglong) a->val) > (ulonglong) LONGLONG_MAX) ||
 
3039
        (b->unsigned_flag && ((ulonglong) b->val) > (ulonglong) LONGLONG_MAX))
3033
3040
      return a->unsigned_flag ? 1 : -1;
3034
3041
    /*
3035
 
      Although the signedness differs both args can fit into the signed
 
3042
      Although the signedness differs both args can fit into the signed 
3036
3043
      positive range. Make them signed and compare as usual.
3037
 
    */
 
3044
    */  
3038
3045
    return cmp_longs (a->val, b->val);
3039
3046
  }
3040
3047
  if (a->unsigned_flag)
3041
 
    return cmp_ulongs ((uint64_t) a->val, (uint64_t) b->val);
 
3048
    return cmp_ulongs ((ulonglong) a->val, (ulonglong) b->val);
3042
3049
  else
3043
3050
    return cmp_longs (a->val, b->val);
3044
3051
}
3045
3052
 
3046
 
static int cmp_double(void *, double *a, double *b)
 
3053
static int cmp_double(void *cmp_arg __attribute__((__unused__)), double *a,double *b)
3047
3054
{
3048
3055
  return *a < *b ? -1 : *a == *b ? 0 : 1;
3049
3056
}
3050
3057
 
3051
 
static int cmp_row(void *, cmp_item_row *a, cmp_item_row *b)
 
3058
static int cmp_row(void *cmp_arg __attribute__((__unused__)), cmp_item_row *a, cmp_item_row *b)
3052
3059
{
3053
3060
  return a->compare(b);
3054
3061
}
3055
3062
 
3056
3063
 
3057
 
static int cmp_decimal(void *, my_decimal *a, my_decimal *b)
 
3064
static int cmp_decimal(void *cmp_arg __attribute__((__unused__)), my_decimal *a, my_decimal *b)
3058
3065
{
3059
3066
  /*
3060
3067
    We need call of fixing buffer pointer, because fast sort just copy
3066
3073
}
3067
3074
 
3068
3075
 
3069
 
void in_vector::sort()
3070
 
{
3071
 
  internal::my_qsort2(base,used_count,size,compare, (void *) collation);
3072
 
}
3073
 
 
3074
 
 
3075
3076
int in_vector::find(Item *item)
3076
3077
{
3077
 
  unsigned char *result=get_value(item);
 
3078
  uchar *result=get_value(item);
3078
3079
  if (!result || !used_count)
3079
3080
    return 0;                           // Null value
3080
3081
 
3081
 
  uint32_t start,end;
 
3082
  uint start,end;
3082
3083
  start=0; end=used_count-1;
3083
3084
  while (start != end)
3084
3085
  {
3085
 
    uint32_t mid=(start+end+1)/2;
 
3086
    uint mid=(start+end+1)/2;
3086
3087
    int res;
3087
3088
    if ((res=(*compare)(collation, base+mid*size, result)) == 0)
3088
3089
      return 1;
3094
3095
  return (int) ((*compare)(collation, base+start*size, result) == 0);
3095
3096
}
3096
3097
 
3097
 
in_string::in_string(uint32_t elements,qsort2_cmp cmp_func, const CHARSET_INFO * const cs)
 
3098
in_string::in_string(uint elements,qsort2_cmp cmp_func, CHARSET_INFO *cs)
3098
3099
  :in_vector(elements, sizeof(String), cmp_func, cs),
3099
3100
   tmp(buff, sizeof(buff), &my_charset_bin)
3100
3101
{}
3103
3104
{
3104
3105
  if (base)
3105
3106
  {
3106
 
    // base was allocated with help of memory::sql_alloc => following is OK
3107
 
    for (uint32_t i=0 ; i < count ; i++)
 
3107
    // base was allocated with help of sql_alloc => following is OK
 
3108
    for (uint i=0 ; i < count ; i++)
3108
3109
      ((String*) base)[i].free();
3109
3110
  }
3110
3111
}
3111
3112
 
3112
 
void in_string::set(uint32_t pos,Item *item)
 
3113
void in_string::set(uint pos,Item *item)
3113
3114
{
3114
3115
  String *str=((String*) base)+pos;
3115
3116
  String *res=item->val_str(str);
3124
3125
  }
3125
3126
  if (!str->charset())
3126
3127
  {
3127
 
    const CHARSET_INFO *cs;
 
3128
    CHARSET_INFO *cs;
3128
3129
    if (!(cs= item->collation.collation))
3129
3130
      cs= &my_charset_bin;              // Should never happen for STR items
3130
3131
    str->set_charset(cs);
3132
3133
}
3133
3134
 
3134
3135
 
3135
 
unsigned char *in_string::get_value(Item *item)
 
3136
uchar *in_string::get_value(Item *item)
3136
3137
{
3137
 
  return (unsigned char*) item->val_str(&tmp);
 
3138
  return (uchar*) item->val_str(&tmp);
3138
3139
}
3139
3140
 
3140
 
in_row::in_row(uint32_t elements, Item *)
 
3141
in_row::in_row(uint elements, Item * item __attribute__((__unused__)))
3141
3142
{
3142
3143
  base= (char*) new cmp_item_row[count= elements];
3143
3144
  size= sizeof(cmp_item_row);
3156
3157
    delete [] (cmp_item_row*) base;
3157
3158
}
3158
3159
 
3159
 
unsigned char *in_row::get_value(Item *item)
 
3160
uchar *in_row::get_value(Item *item)
3160
3161
{
3161
3162
  tmp.store_value(item);
3162
3163
  if (item->is_null())
3163
3164
    return 0;
3164
 
  return (unsigned char *)&tmp;
 
3165
  return (uchar *)&tmp;
3165
3166
}
3166
3167
 
3167
 
void in_row::set(uint32_t pos, Item *item)
 
3168
void in_row::set(uint pos, Item *item)
3168
3169
{
 
3170
  DBUG_ENTER("in_row::set");
 
3171
  DBUG_PRINT("enter", ("pos: %u  item: 0x%lx", pos, (ulong) item));
3169
3172
  ((cmp_item_row*) base)[pos].store_value_by_template(&tmp, item);
3170
 
  return;
 
3173
  DBUG_VOID_RETURN;
3171
3174
}
3172
3175
 
3173
 
in_int64_t::in_int64_t(uint32_t elements)
3174
 
  :in_vector(elements,sizeof(packed_int64_t),(qsort2_cmp) cmp_int64_t, 0)
 
3176
in_longlong::in_longlong(uint elements)
 
3177
  :in_vector(elements,sizeof(packed_longlong),(qsort2_cmp) cmp_longlong, 0)
3175
3178
{}
3176
3179
 
3177
 
void in_int64_t::set(uint32_t pos,Item *item)
 
3180
void in_longlong::set(uint pos,Item *item)
3178
3181
{
3179
 
  struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
3180
 
 
 
3182
  struct packed_longlong *buff= &((packed_longlong*) base)[pos];
 
3183
  
3181
3184
  buff->val= item->val_int();
3182
3185
  buff->unsigned_flag= item->unsigned_flag;
3183
3186
}
3184
3187
 
3185
 
unsigned char *in_int64_t::get_value(Item *item)
 
3188
uchar *in_longlong::get_value(Item *item)
3186
3189
{
3187
3190
  tmp.val= item->val_int();
3188
3191
  if (item->null_value)
3189
3192
    return 0;
3190
3193
  tmp.unsigned_flag= item->unsigned_flag;
3191
 
  return (unsigned char*) &tmp;
 
3194
  return (uchar*) &tmp;
3192
3195
}
3193
3196
 
3194
 
void in_datetime::set(uint32_t pos,Item *item)
 
3197
void in_datetime::set(uint pos,Item *item)
3195
3198
{
3196
3199
  Item **tmp_item= &item;
3197
3200
  bool is_null;
3198
 
  struct packed_int64_t *buff= &((packed_int64_t*) base)[pos];
 
3201
  struct packed_longlong *buff= &((packed_longlong*) base)[pos];
3199
3202
 
3200
 
  buff->val= get_datetime_value(session, &tmp_item, 0, warn_item, &is_null);
 
3203
  buff->val= get_datetime_value(thd, &tmp_item, 0, warn_item, &is_null);
3201
3204
  buff->unsigned_flag= 1L;
3202
3205
}
3203
3206
 
3204
 
unsigned char *in_datetime::get_value(Item *item)
 
3207
uchar *in_datetime::get_value(Item *item)
3205
3208
{
3206
3209
  bool is_null;
3207
3210
  Item **tmp_item= lval_cache ? &lval_cache : &item;
3208
 
  tmp.val= get_datetime_value(session, &tmp_item, &lval_cache, warn_item, &is_null);
 
3211
  tmp.val= get_datetime_value(thd, &tmp_item, &lval_cache, warn_item, &is_null);
3209
3212
  if (item->null_value)
3210
3213
    return 0;
3211
3214
  tmp.unsigned_flag= 1L;
3212
 
  return (unsigned char*) &tmp;
 
3215
  return (uchar*) &tmp;
3213
3216
}
3214
3217
 
3215
 
in_double::in_double(uint32_t elements)
 
3218
in_double::in_double(uint elements)
3216
3219
  :in_vector(elements,sizeof(double),(qsort2_cmp) cmp_double, 0)
3217
3220
{}
3218
3221
 
3219
 
void in_double::set(uint32_t pos,Item *item)
 
3222
void in_double::set(uint pos,Item *item)
3220
3223
{
3221
3224
  ((double*) base)[pos]= item->val_real();
3222
3225
}
3223
3226
 
3224
 
unsigned char *in_double::get_value(Item *item)
 
3227
uchar *in_double::get_value(Item *item)
3225
3228
{
3226
3229
  tmp= item->val_real();
3227
3230
  if (item->null_value)
3228
 
    return 0;
3229
 
  return (unsigned char*) &tmp;
 
3231
    return 0;                                   /* purecov: inspected */
 
3232
  return (uchar*) &tmp;
3230
3233
}
3231
3234
 
3232
3235
 
3233
 
in_decimal::in_decimal(uint32_t elements)
 
3236
in_decimal::in_decimal(uint elements)
3234
3237
  :in_vector(elements, sizeof(my_decimal),(qsort2_cmp) cmp_decimal, 0)
3235
3238
{}
3236
3239
 
3237
3240
 
3238
 
void in_decimal::set(uint32_t pos, Item *item)
 
3241
void in_decimal::set(uint pos, Item *item)
3239
3242
{
3240
3243
  /* as far as 'item' is constant, we can store reference on my_decimal */
3241
3244
  my_decimal *dec= ((my_decimal *)base) + pos;
3242
3245
  dec->len= DECIMAL_BUFF_LENGTH;
3243
3246
  dec->fix_buffer_pointer();
3244
3247
  my_decimal *res= item->val_decimal(dec);
3245
 
  /* if item->val_decimal() is evaluated to NULL then res == 0 */
 
3248
  /* if item->val_decimal() is evaluated to NULL then res == 0 */ 
3246
3249
  if (!item->null_value && res != dec)
3247
3250
    my_decimal2decimal(res, dec);
3248
3251
}
3249
3252
 
3250
3253
 
3251
 
unsigned char *in_decimal::get_value(Item *item)
 
3254
uchar *in_decimal::get_value(Item *item)
3252
3255
{
3253
3256
  my_decimal *result= item->val_decimal(&val);
3254
3257
  if (item->null_value)
3255
3258
    return 0;
3256
 
  return (unsigned char *)result;
 
3259
  return (uchar *)result;
3257
3260
}
3258
3261
 
3259
3262
 
3260
3263
cmp_item* cmp_item::get_comparator(Item_result type,
3261
 
                                   const CHARSET_INFO * const cs)
 
3264
                                   CHARSET_INFO *cs)
3262
3265
{
3263
3266
  switch (type) {
3264
3267
  case STRING_RESULT:
3272
3275
  case DECIMAL_RESULT:
3273
3276
    return new cmp_item_decimal;
3274
3277
  default:
3275
 
    assert(0);
 
3278
    DBUG_ASSERT(0);
3276
3279
    break;
3277
3280
  }
3278
3281
  return 0; // to satisfy compiler :)
3302
3305
 
3303
3306
cmp_item_row::~cmp_item_row()
3304
3307
{
 
3308
  DBUG_ENTER("~cmp_item_row");
 
3309
  DBUG_PRINT("enter",("this: 0x%lx", (long) this));
3305
3310
  if (comparators)
3306
3311
  {
3307
 
    for (uint32_t i= 0; i < n; i++)
 
3312
    for (uint i= 0; i < n; i++)
3308
3313
    {
3309
3314
      if (comparators[i])
3310
3315
        delete comparators[i];
3311
3316
    }
3312
3317
  }
3313
 
  return;
 
3318
  DBUG_VOID_RETURN;
3314
3319
}
3315
3320
 
3316
3321
 
3317
3322
void cmp_item_row::alloc_comparators()
3318
3323
{
3319
3324
  if (!comparators)
3320
 
    comparators= (cmp_item **) current_session->calloc(sizeof(cmp_item *)*n);
 
3325
    comparators= (cmp_item **) current_thd->calloc(sizeof(cmp_item *)*n);
3321
3326
}
3322
3327
 
3323
3328
 
3324
3329
void cmp_item_row::store_value(Item *item)
3325
3330
{
 
3331
  DBUG_ENTER("cmp_item_row::store_value");
3326
3332
  n= item->cols();
3327
3333
  alloc_comparators();
3328
3334
  if (comparators)
3329
3335
  {
3330
3336
    item->bring_value();
3331
3337
    item->null_value= 0;
3332
 
    for (uint32_t i=0; i < n; i++)
 
3338
    for (uint i=0; i < n; i++)
3333
3339
    {
3334
3340
      if (!comparators[i])
3335
3341
        if (!(comparators[i]=
3340
3346
      item->null_value|= item->element_index(i)->null_value;
3341
3347
    }
3342
3348
  }
3343
 
  return;
 
3349
  DBUG_VOID_RETURN;
3344
3350
}
3345
3351
 
3346
3352
 
3353
3359
    return;
3354
3360
  }
3355
3361
  n= tmpl->n;
3356
 
  if ((comparators= (cmp_item **) memory::sql_alloc(sizeof(cmp_item *)*n)))
 
3362
  if ((comparators= (cmp_item **) sql_alloc(sizeof(cmp_item *)*n)))
3357
3363
  {
3358
3364
    item->bring_value();
3359
3365
    item->null_value= 0;
3360
 
    for (uint32_t i=0; i < n; i++)
 
3366
    for (uint i=0; i < n; i++)
3361
3367
    {
3362
3368
      if (!(comparators[i]= tmpl->comparators[i]->make_same()))
3363
3369
        break;                                  // new failed
3379
3385
  }
3380
3386
  bool was_null= 0;
3381
3387
  arg->bring_value();
3382
 
  for (uint32_t i=0; i < n; i++)
 
3388
  for (uint i=0; i < n; i++)
3383
3389
  {
3384
3390
    if (comparators[i]->cmp(arg->element_index(i)))
3385
3391
    {
3395
3401
int cmp_item_row::compare(cmp_item *c)
3396
3402
{
3397
3403
  cmp_item_row *l_cmp= (cmp_item_row *) c;
3398
 
  for (uint32_t i=0; i < n; i++)
 
3404
  for (uint i=0; i < n; i++)
3399
3405
  {
3400
3406
    int res;
3401
3407
    if ((res= comparators[i]->compare(l_cmp->comparators[i])))
3440
3446
{
3441
3447
  bool is_null;
3442
3448
  Item **tmp_item= lval_cache ? &lval_cache : &item;
3443
 
  value= get_datetime_value(session, &tmp_item, &lval_cache, warn_item, &is_null);
 
3449
  value= get_datetime_value(thd, &tmp_item, &lval_cache, warn_item, &is_null);
3444
3450
}
3445
3451
 
3446
3452
 
3449
3455
  bool is_null;
3450
3456
  Item **tmp_item= &arg;
3451
3457
  return value !=
3452
 
    get_datetime_value(session, &tmp_item, 0, warn_item, &is_null);
 
3458
    get_datetime_value(thd, &tmp_item, 0, warn_item, &is_null);
3453
3459
}
3454
3460
 
3455
3461
 
3486
3492
    The function saves in ref the pointer to the item or to a newly created
3487
3493
    item that is considered as a replacement for the original one.
3488
3494
 
3489
 
  @param session     reference to the global context of the query thread
 
3495
  @param thd     reference to the global context of the query thread
3490
3496
  @param ref     pointer to Item* variable where pointer to resulting "fixed"
3491
3497
                 item is to be assigned
3492
3498
 
3507
3513
*/
3508
3514
 
3509
3515
bool
3510
 
Item_func_in::fix_fields(Session *session, Item **ref)
 
3516
Item_func_in::fix_fields(THD *thd, Item **ref)
3511
3517
{
3512
3518
  Item **arg, **arg_end;
3513
3519
 
3514
 
  if (Item_func_opt_neg::fix_fields(session, ref))
 
3520
  if (Item_func_opt_neg::fix_fields(thd, ref))
3515
3521
    return 1;
3516
3522
 
3517
3523
  /* not_null_tables_cache == union(T1(e),union(T1(ei))) */
3527
3533
}
3528
3534
 
3529
3535
 
3530
 
static int srtcmp_in(const CHARSET_INFO * const cs, const String *x,const String *y)
 
3536
static int srtcmp_in(CHARSET_INFO *cs, const String *x,const String *y)
3531
3537
{
3532
3538
  return cs->coll->strnncollsp(cs,
3533
 
                               (unsigned char *) x->ptr(),x->length(),
3534
 
                               (unsigned char *) y->ptr(),y->length(), 0);
 
3539
                               (uchar *) x->ptr(),x->length(),
 
3540
                               (uchar *) y->ptr(),y->length(), 0);
3535
3541
}
3536
3542
 
3537
3543
 
3539
3545
{
3540
3546
  Item **arg, **arg_end;
3541
3547
  bool const_itm= 1;
3542
 
  Session *session= current_session;
 
3548
  THD *thd= current_thd;
3543
3549
  bool datetime_found= false;
3544
3550
  /* true <=> arguments values will be compared as DATETIMEs. */
3545
3551
  bool compare_as_datetime= false;
3546
3552
  Item *date_arg= 0;
3547
 
  uint32_t found_types= 0;
3548
 
  uint32_t type_cnt= 0, i;
 
3553
  uint found_types= 0;
 
3554
  uint type_cnt= 0, i;
3549
3555
  Item_result cmp_type= STRING_RESULT;
3550
3556
  left_result_type= args[0]->result_type();
3551
 
  if (!(found_types= collect_cmp_types(args, arg_count, true)))
 
3557
  if (!(found_types= collect_cmp_types(args, arg_count)))
3552
3558
    return;
3553
 
 
 
3559
  
3554
3560
  for (arg= args + 1, arg_end= args + arg_count; arg != arg_end ; arg++)
3555
3561
  {
3556
3562
    if (!arg[0]->const_item())
3559
3565
      break;
3560
3566
    }
3561
3567
  }
3562
 
  for (i= 0; i <= (uint32_t)DECIMAL_RESULT; i++)
 
3568
  for (i= 0; i <= (uint)DECIMAL_RESULT; i++)
3563
3569
  {
3564
3570
    if (found_types & 1 << i)
3565
3571
    {
3570
3576
 
3571
3577
  if (type_cnt == 1)
3572
3578
  {
3573
 
    if (cmp_type == STRING_RESULT &&
 
3579
    if (cmp_type == STRING_RESULT && 
3574
3580
        agg_arg_charsets(cmp_collation, args, arg_count, MY_COLL_CMP_CONV, 1))
3575
3581
      return;
3576
3582
    arg_types_compatible= true;
3601
3607
    /* All DATE/DATETIME fields/functions has the STRING result type. */
3602
3608
    if (cmp_type == STRING_RESULT || cmp_type == ROW_RESULT)
3603
3609
    {
3604
 
      uint32_t col, num_cols= args[0]->cols();
 
3610
      uint col, cols= args[0]->cols();
3605
3611
 
3606
 
      for (col= 0; col < num_cols; col++)
 
3612
      for (col= 0; col < cols; col++)
3607
3613
      {
3608
3614
        bool skip_column= false;
3609
3615
        /*
3628
3634
            */
3629
3635
            if (!date_arg)
3630
3636
              date_arg= itm;
3631
 
            else if (itm->field_type() == DRIZZLE_TYPE_DATETIME)
 
3637
            else if (itm->field_type() == MYSQL_TYPE_DATETIME)
3632
3638
            {
3633
3639
              date_arg= itm;
3634
3640
              /* All arguments are already checked to have the STRING result. */
3672
3678
      /*
3673
3679
        IN must compare INT columns and constants as int values (the same
3674
3680
        way as equality does).
3675
 
        So we must check here if the column on the left and all the constant
3676
 
        values on the right can be compared as integers and adjust the
 
3681
        So we must check here if the column on the left and all the constant 
 
3682
        values on the right can be compared as integers and adjust the 
3677
3683
        comparison type accordingly.
3678
 
      */
 
3684
      */  
3679
3685
      if (args[0]->real_item()->type() == FIELD_ITEM &&
3680
 
          session->lex->sql_command != SQLCOM_SHOW_CREATE &&
 
3686
          thd->lex->sql_command != SQLCOM_SHOW_CREATE &&
3681
3687
          cmp_type != INT_RESULT)
3682
3688
      {
3683
3689
        Item_field *field_item= (Item_field*) (args[0]->real_item());
3684
 
        if (field_item->field->can_be_compared_as_int64_t())
 
3690
        if (field_item->field->can_be_compared_as_longlong())
3685
3691
        {
3686
3692
          bool all_converted= true;
3687
3693
          for (arg=args+1, arg_end=args+arg_count; arg != arg_end ; arg++)
3688
3694
          {
3689
 
            if (!convert_constant_item (session, field_item, &arg[0]))
 
3695
            if (!convert_constant_item (thd, field_item, &arg[0]))
3690
3696
              all_converted= false;
3691
3697
          }
3692
3698
          if (all_converted)
3695
3701
      }
3696
3702
      switch (cmp_type) {
3697
3703
      case STRING_RESULT:
3698
 
        array=new in_string(arg_count-1,(qsort2_cmp) srtcmp_in,
 
3704
        array=new in_string(arg_count-1,(qsort2_cmp) srtcmp_in, 
3699
3705
                            cmp_collation.collation);
3700
3706
        break;
3701
3707
      case INT_RESULT:
3702
 
        array= new in_int64_t(arg_count-1);
 
3708
        array= new in_longlong(arg_count-1);
3703
3709
        break;
3704
3710
      case REAL_RESULT:
3705
3711
        array= new in_double(arg_count-1);
3716
3722
        array= new in_decimal(arg_count - 1);
3717
3723
        break;
3718
3724
      default:
3719
 
        assert(0);
 
3725
        DBUG_ASSERT(0);
3720
3726
        return;
3721
3727
      }
3722
3728
    }
3723
 
    if (array && !(session->is_fatal_error))            // If not EOM
 
3729
    if (array && !(thd->is_fatal_error))                // If not EOM
3724
3730
    {
3725
 
      uint32_t j=0;
3726
 
      for (uint32_t arg_num=1 ; arg_num < arg_count ; arg_num++)
 
3731
      uint j=0;
 
3732
      for (uint i=1 ; i < arg_count ; i++)
3727
3733
      {
3728
 
        if (!args[arg_num]->null_value)                 // Skip NULL values
3729
 
        {
3730
 
          array->set(j,args[arg_num]);
 
3734
        array->set(j,args[i]);
 
3735
        if (!args[i]->null_value)                       // Skip NULL values
3731
3736
          j++;
3732
 
        }
3733
3737
        else
3734
3738
          have_null= 1;
3735
3739
      }
3743
3747
      cmp_items[STRING_RESULT]= new cmp_item_datetime(date_arg);
3744
3748
    else
3745
3749
    {
3746
 
      for (i= 0; i <= (uint32_t) DECIMAL_RESULT; i++)
 
3750
      for (i= 0; i <= (uint) DECIMAL_RESULT; i++)
3747
3751
      {
3748
3752
        if (found_types & (1 << i) && !cmp_items[i])
3749
3753
        {
3800
3804
    Value of the function
3801
3805
*/
3802
3806
 
3803
 
int64_t Item_func_in::val_int()
 
3807
longlong Item_func_in::val_int()
3804
3808
{
3805
3809
  cmp_item *in_item;
3806
 
  assert(fixed == 1);
3807
 
  uint32_t value_added_map= 0;
 
3810
  DBUG_ASSERT(fixed == 1);
 
3811
  uint value_added_map= 0;
3808
3812
  if (array)
3809
3813
  {
3810
3814
    int tmp=array->find(args[0]);
3811
3815
    null_value=args[0]->null_value || (!tmp && have_null);
3812
 
    return (int64_t) (!null_value && tmp != negated);
 
3816
    return (longlong) (!null_value && tmp != negated);
3813
3817
  }
3814
3818
 
3815
 
  for (uint32_t i= 1 ; i < arg_count ; i++)
 
3819
  for (uint i= 1 ; i < arg_count ; i++)
3816
3820
  {
3817
3821
    Item_result cmp_type= item_cmp_type(left_result_type, args[i]->result_type());
3818
 
    in_item= cmp_items[(uint32_t)cmp_type];
3819
 
    assert(in_item);
3820
 
    if (!(value_added_map & (1 << (uint32_t)cmp_type)))
 
3822
    in_item= cmp_items[(uint)cmp_type];
 
3823
    DBUG_ASSERT(in_item);
 
3824
    if (!(value_added_map & (1 << (uint)cmp_type)))
3821
3825
    {
3822
3826
      in_item->store_value(args[0]);
3823
3827
      if ((null_value=args[0]->null_value))
3824
3828
        return 0;
3825
3829
      have_null= 0;
3826
 
      value_added_map|= 1 << (uint32_t)cmp_type;
 
3830
      value_added_map|= 1 << (uint)cmp_type;
3827
3831
    }
3828
3832
    if (!in_item->cmp(args[i]) && !args[i]->null_value)
3829
 
      return (int64_t) (!negated);
 
3833
      return (longlong) (!negated);
3830
3834
    have_null|= args[i]->null_value;
3831
3835
  }
3832
3836
 
3833
3837
  null_value= have_null;
3834
 
  return (int64_t) (!null_value && negated);
3835
 
}
3836
 
 
3837
 
 
3838
 
Item_cond::Item_cond(Session *session, Item_cond *item)
3839
 
  :Item_bool_func(session, item),
 
3838
  return (longlong) (!null_value && negated);
 
3839
}
 
3840
 
 
3841
 
 
3842
longlong Item_func_bit_or::val_int()
 
3843
{
 
3844
  DBUG_ASSERT(fixed == 1);
 
3845
  ulonglong arg1= (ulonglong) args[0]->val_int();
 
3846
  if (args[0]->null_value)
 
3847
  {
 
3848
    null_value=1; /* purecov: inspected */
 
3849
    return 0; /* purecov: inspected */
 
3850
  }
 
3851
  ulonglong arg2= (ulonglong) args[1]->val_int();
 
3852
  if (args[1]->null_value)
 
3853
  {
 
3854
    null_value=1;
 
3855
    return 0;
 
3856
  }
 
3857
  null_value=0;
 
3858
  return (longlong) (arg1 | arg2);
 
3859
}
 
3860
 
 
3861
 
 
3862
longlong Item_func_bit_and::val_int()
 
3863
{
 
3864
  DBUG_ASSERT(fixed == 1);
 
3865
  ulonglong arg1= (ulonglong) args[0]->val_int();
 
3866
  if (args[0]->null_value)
 
3867
  {
 
3868
    null_value=1; /* purecov: inspected */
 
3869
    return 0; /* purecov: inspected */
 
3870
  }
 
3871
  ulonglong arg2= (ulonglong) args[1]->val_int();
 
3872
  if (args[1]->null_value)
 
3873
  {
 
3874
    null_value=1; /* purecov: inspected */
 
3875
    return 0; /* purecov: inspected */
 
3876
  }
 
3877
  null_value=0;
 
3878
  return (longlong) (arg1 & arg2);
 
3879
}
 
3880
 
 
3881
Item_cond::Item_cond(THD *thd, Item_cond *item)
 
3882
  :Item_bool_func(thd, item),
3840
3883
   abort_on_null(item->abort_on_null),
3841
3884
   and_tables_cache(item->and_tables_cache)
3842
3885
{
3846
3889
}
3847
3890
 
3848
3891
 
3849
 
void Item_cond::copy_andor_arguments(Session *session, Item_cond *item)
 
3892
void Item_cond::copy_andor_arguments(THD *thd, Item_cond *item)
3850
3893
{
3851
3894
  List_iterator_fast<Item> li(item->list);
3852
3895
  while (Item *it= li++)
3853
 
    list.push_back(it->copy_andor_structure(session));
 
3896
    list.push_back(it->copy_andor_structure(thd));
3854
3897
}
3855
3898
 
3856
3899
 
3857
3900
bool
3858
 
Item_cond::fix_fields(Session *session, Item **)
 
3901
Item_cond::fix_fields(THD *thd, Item **ref __attribute__((__unused__)))
3859
3902
{
3860
 
  assert(fixed == 0);
 
3903
  DBUG_ASSERT(fixed == 0);
3861
3904
  List_iterator<Item> li(list);
3862
3905
  Item *item;
3863
 
  void *orig_session_marker= session->session_marker;
3864
 
  unsigned char buff[sizeof(char*)];                    // Max local vars in function
 
3906
  void *orig_thd_marker= thd->thd_marker;
 
3907
  uchar buff[sizeof(char*)];                    // Max local vars in function
3865
3908
  not_null_tables_cache= used_tables_cache= 0;
3866
3909
  const_item_cache= 1;
3867
3910
 
3868
3911
  if (functype() == COND_OR_FUNC)
3869
 
    session->session_marker= 0;
 
3912
    thd->thd_marker= 0;
3870
3913
  /*
3871
3914
    and_table_cache is the value that Item_cond_or() returns for
3872
3915
    not_null_tables()
3873
3916
  */
3874
3917
  and_tables_cache= ~(table_map) 0;
3875
3918
 
3876
 
  if (check_stack_overrun(session, STACK_MIN_SIZE, buff))
 
3919
  if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
3877
3920
    return true;                                // Fatal error flag is set!
3878
3921
  /*
3879
3922
    The following optimization reduces the depth of an AND-OR tree.
3906
3949
 
3907
3950
    // item can be substituted in fix_fields
3908
3951
    if ((!item->fixed &&
3909
 
         item->fix_fields(session, li.ref())) ||
 
3952
         item->fix_fields(thd, li.ref())) ||
3910
3953
        (item= *li.ref())->check_cols(1))
3911
 
      return true;
 
3954
      return true; /* purecov: inspected */
3912
3955
    used_tables_cache|=     item->used_tables();
3913
3956
    if (item->const_item())
3914
3957
      and_tables_cache= (table_map) 0;
3918
3961
      not_null_tables_cache|= tmp_table_map;
3919
3962
      and_tables_cache&= tmp_table_map;
3920
3963
      const_item_cache= false;
3921
 
    }
 
3964
    }  
3922
3965
    with_sum_func=          with_sum_func || item->with_sum_func;
3923
3966
    with_subselect|=        item->with_subselect;
3924
3967
    if (item->maybe_null)
3925
3968
      maybe_null=1;
3926
3969
  }
3927
 
  session->lex->current_select->cond_count+= list.elements;
3928
 
  session->session_marker= orig_session_marker;
 
3970
  thd->lex->current_select->cond_count+= list.elements;
 
3971
  thd->thd_marker= orig_thd_marker;
3929
3972
  fix_length_and_dec();
3930
3973
  fixed= 1;
3931
3974
  return false;
3932
3975
}
3933
3976
 
3934
3977
 
3935
 
void Item_cond::fix_after_pullout(Select_Lex *new_parent, Item **)
 
3978
void Item_cond::fix_after_pullout(st_select_lex *new_parent, Item **ref __attribute__((__unused__)))
3936
3979
{
3937
3980
  List_iterator<Item> li(list);
3938
3981
  Item *item;
3959
4002
      not_null_tables_cache|= tmp_table_map;
3960
4003
      and_tables_cache&= tmp_table_map;
3961
4004
      const_item_cache= false;
3962
 
    }
 
4005
    }  
3963
4006
  }
3964
4007
}
3965
4008
 
3966
4009
 
3967
 
bool Item_cond::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
 
4010
bool Item_cond::walk(Item_processor processor, bool walk_subquery, uchar *arg)
3968
4011
{
3969
4012
  List_iterator_fast<Item> li(list);
3970
4013
  Item *item;
3977
4020
 
3978
4021
/**
3979
4022
  Transform an Item_cond object with a transformer callback function.
3980
 
 
 
4023
  
3981
4024
    The function recursively applies the transform method to each
3982
4025
     member item of the condition list.
3983
4026
    If the call of the method for a member item returns a new item
3984
4027
    the old item is substituted for a new one.
3985
4028
    After this the transformer is applied to the root node
3986
 
    of the Item_cond object.
3987
 
 
 
4029
    of the Item_cond object. 
 
4030
     
3988
4031
  @param transformer   the transformer callback function to be applied to
3989
4032
                       the nodes of the tree of the object
3990
4033
  @param arg           parameter to be passed to the transformer
3991
4034
 
3992
4035
  @return
3993
 
    Item returned as the result of transformation of the root node
 
4036
    Item returned as the result of transformation of the root node 
3994
4037
*/
3995
4038
 
3996
 
Item *Item_cond::transform(Item_transformer transformer, unsigned char *arg)
 
4039
Item *Item_cond::transform(Item_transformer transformer, uchar *arg)
3997
4040
{
3998
4041
  List_iterator<Item> li(list);
3999
4042
  Item *item;
4004
4047
      return 0;
4005
4048
 
4006
4049
    /*
4007
 
      Session::change_item_tree() should be called only if the tree was
 
4050
      THD::change_item_tree() should be called only if the tree was
4008
4051
      really transformed, i.e. when a new item has been created.
4009
4052
      Otherwise we'll be allocating a lot of unnecessary memory for
4010
4053
      change records at each execution.
4011
4054
    */
4012
4055
    if (new_item != item)
4013
 
      current_session->change_item_tree(li.ref(), new_item);
 
4056
      current_thd->change_item_tree(li.ref(), new_item);
4014
4057
  }
4015
4058
  return Item_func::transform(transformer, arg);
4016
4059
}
4019
4062
/**
4020
4063
  Compile Item_cond object with a processor and a transformer
4021
4064
  callback functions.
4022
 
 
 
4065
  
4023
4066
    First the function applies the analyzer to the root node of
4024
4067
    the Item_func object. Then if the analyzer succeeeds (returns true)
4025
4068
    the function recursively applies the compile method to member
4027
4070
    If the call of the method for a member item returns a new item
4028
4071
    the old item is substituted for a new one.
4029
4072
    After this the transformer is applied to the root node
4030
 
    of the Item_cond object.
4031
 
 
 
4073
    of the Item_cond object. 
 
4074
     
4032
4075
  @param analyzer      the analyzer callback function to be applied to the
4033
4076
                       nodes of the tree of the object
4034
4077
  @param[in,out] arg_p parameter to be passed to the analyzer
4037
4080
  @param arg_t         parameter to be passed to the transformer
4038
4081
 
4039
4082
  @return
4040
 
    Item returned as the result of transformation of the root node
 
4083
    Item returned as the result of transformation of the root node 
4041
4084
*/
4042
4085
 
4043
 
Item *Item_cond::compile(Item_analyzer analyzer, unsigned char **arg_p,
4044
 
                         Item_transformer transformer, unsigned char *arg_t)
 
4086
Item *Item_cond::compile(Item_analyzer analyzer, uchar **arg_p,
 
4087
                         Item_transformer transformer, uchar *arg_t)
4045
4088
{
4046
4089
  if (!(this->*analyzer)(arg_p))
4047
4090
    return 0;
4048
 
 
 
4091
  
4049
4092
  List_iterator<Item> li(list);
4050
4093
  Item *item;
4051
4094
  while ((item= li++))
4052
4095
  {
4053
 
    /*
 
4096
    /* 
4054
4097
      The same parameter value of arg_p must be passed
4055
4098
      to analyze any argument of the condition formula.
4056
 
    */
4057
 
    unsigned char *arg_v= *arg_p;
 
4099
    */   
 
4100
    uchar *arg_v= *arg_p;
4058
4101
    Item *new_item= item->compile(analyzer, &arg_v, transformer, arg_t);
4059
4102
    if (new_item && new_item != item)
4060
4103
      li.replace(new_item);
4068
4111
  List_iterator<Item> li(list);
4069
4112
  Item *item;
4070
4113
 
4071
 
  switch (order) {
4072
 
  case (T_PREFIX):
 
4114
  switch(order) {
 
4115
  case(PREFIX):
4073
4116
    (*traverser)(this, arg);
4074
4117
    while ((item= li++))
4075
4118
    {
4077
4120
    }
4078
4121
    (*traverser)(NULL, arg);
4079
4122
    break;
4080
 
  case (T_POSTFIX):
 
4123
  case(POSTFIX):
4081
4124
    while ((item= li++))
4082
4125
    {
4083
4126
      item->traverse_cond(traverser, arg, order);
4094
4137
  (Calculation done by update_sum_func() and copy_sum_funcs() in
4095
4138
  sql_select.cc)
4096
4139
 
4097
 
  @param session                        Thread handler
 
4140
  @param thd                    Thread handler
4098
4141
  @param ref_pointer_array      Pointer to array of reference fields
4099
4142
  @param fields         All fields in select
4100
4143
 
4103
4146
    that have or refer (HAVING) to a SUM expression.
4104
4147
*/
4105
4148
 
4106
 
void Item_cond::split_sum_func(Session *session, Item **ref_pointer_array,
 
4149
void Item_cond::split_sum_func(THD *thd, Item **ref_pointer_array,
4107
4150
                               List<Item> &fields)
4108
4151
{
4109
4152
  List_iterator<Item> li(list);
4110
4153
  Item *item;
4111
4154
  while ((item= li++))
4112
 
    item->split_sum_func(session, ref_pointer_array,
4113
 
                         fields, li.ref(), true);
 
4155
    item->split_sum_func2(thd, ref_pointer_array, fields, li.ref(), true);
4114
4156
}
4115
4157
 
4116
4158
 
4155
4197
}
4156
4198
 
4157
4199
 
4158
 
void Item_cond::neg_arguments(Session *session)
 
4200
void Item_cond::neg_arguments(THD *thd)
4159
4201
{
4160
4202
  List_iterator<Item> li(list);
4161
4203
  Item *item;
4162
4204
  while ((item= li++))          /* Apply not transformation to the arguments */
4163
4205
  {
4164
 
    Item *new_item= item->neg_transformer(session);
 
4206
    Item *new_item= item->neg_transformer(thd);
4165
4207
    if (!new_item)
4166
4208
    {
4167
4209
      if (!(new_item= new Item_func_not(item)))
4168
4210
        return;                                 // Fatal OEM error
4169
4211
    }
4170
 
    li.replace(new_item);
 
4212
    VOID(li.replace(new_item));
4171
4213
  }
4172
4214
}
4173
4215
 
4192
4234
*/
4193
4235
 
4194
4236
 
4195
 
int64_t Item_cond_and::val_int()
 
4237
longlong Item_cond_and::val_int()
4196
4238
{
4197
 
  assert(fixed == 1);
 
4239
  DBUG_ASSERT(fixed == 1);
4198
4240
  List_iterator_fast<Item> li(list);
4199
4241
  Item *item;
4200
4242
  null_value= 0;
4210
4252
}
4211
4253
 
4212
4254
 
4213
 
int64_t Item_cond_or::val_int()
 
4255
longlong Item_cond_or::val_int()
4214
4256
{
4215
 
  assert(fixed == 1);
 
4257
  DBUG_ASSERT(fixed == 1);
4216
4258
  List_iterator_fast<Item> li(list);
4217
4259
  Item *item;
4218
4260
  null_value=0;
4271
4313
}
4272
4314
 
4273
4315
 
4274
 
int64_t Item_func_isnull::val_int()
 
4316
longlong Item_func_isnull::val_int()
4275
4317
{
4276
 
  assert(fixed == 1);
 
4318
  DBUG_ASSERT(fixed == 1);
4277
4319
  /*
4278
4320
    Handle optimization if the argument can't be null
4279
4321
    This has to be here because of the test in update_used_tables().
4283
4325
  return args[0]->is_null() ? 1: 0;
4284
4326
}
4285
4327
 
4286
 
int64_t Item_is_not_null_test::val_int()
 
4328
longlong Item_is_not_null_test::val_int()
4287
4329
{
4288
 
  assert(fixed == 1);
 
4330
  DBUG_ASSERT(fixed == 1);
 
4331
  DBUG_ENTER("Item_is_not_null_test::val_int");
4289
4332
  if (!used_tables_cache && !with_subselect)
4290
4333
  {
4291
4334
    owner->was_null|= (!cached_value);
4292
 
    return(cached_value);
 
4335
    DBUG_PRINT("info", ("cached: %ld", (long) cached_value));
 
4336
    DBUG_RETURN(cached_value);
4293
4337
  }
4294
4338
  if (args[0]->is_null())
4295
4339
  {
 
4340
    DBUG_PRINT("info", ("null"));
4296
4341
    owner->was_null|= 1;
4297
 
    return(0);
 
4342
    DBUG_RETURN(0);
4298
4343
  }
4299
4344
  else
4300
 
    return(1);
 
4345
    DBUG_RETURN(1);
4301
4346
}
4302
4347
 
4303
4348
/**
4308
4353
  if (!args[0]->maybe_null)
4309
4354
  {
4310
4355
    used_tables_cache= 0;                       /* is always true */
4311
 
    cached_value= (int64_t) 1;
 
4356
    cached_value= (longlong) 1;
4312
4357
  }
4313
4358
  else
4314
4359
  {
4316
4361
    if (!(used_tables_cache=args[0]->used_tables()) && !with_subselect)
4317
4362
    {
4318
4363
      /* Remember if the value is always NULL or never NULL */
4319
 
      cached_value= (int64_t) !args[0]->is_null();
 
4364
      cached_value= (longlong) !args[0]->is_null();
4320
4365
    }
4321
4366
  }
4322
4367
}
4323
4368
 
4324
4369
 
4325
 
int64_t Item_func_isnotnull::val_int()
 
4370
longlong Item_func_isnotnull::val_int()
4326
4371
{
4327
 
  assert(fixed == 1);
 
4372
  DBUG_ASSERT(fixed == 1);
4328
4373
  return args[0]->is_null() ? 0 : 1;
4329
4374
}
4330
4375
 
4337
4382
}
4338
4383
 
4339
4384
 
4340
 
int64_t Item_func_like::val_int()
 
4385
longlong Item_func_like::val_int()
4341
4386
{
4342
 
  assert(fixed == 1);
 
4387
  DBUG_ASSERT(fixed == 1);
4343
4388
  String* res = args[0]->val_str(&tmp_value1);
4344
4389
  if (args[0]->null_value)
4345
4390
  {
4356
4401
  if (canDoTurboBM)
4357
4402
    return turboBM_matches(res->ptr(), res->length()) ? 1 : 0;
4358
4403
  return my_wildcmp(cmp.cmp_collation.collation,
4359
 
                    res->ptr(),res->ptr()+res->length(),
 
4404
                    res->ptr(),res->ptr()+res->length(),
4360
4405
                    res2->ptr(),res2->ptr()+res2->length(),
4361
 
                    make_escape_code(cmp.cmp_collation.collation, escape),
4362
 
                    internal::wild_one,internal::wild_many) ? 0 : 1;
 
4406
                    escape,wild_one,wild_many) ? 0 : 1;
4363
4407
}
4364
4408
 
4365
4409
 
4376
4420
    if (!res2)
4377
4421
      return OPTIMIZE_NONE;
4378
4422
 
4379
 
    if (*res2->ptr() != internal::wild_many)
 
4423
    if (*res2->ptr() != wild_many)
4380
4424
    {
4381
 
      if (args[0]->result_type() != STRING_RESULT || *res2->ptr() != internal::wild_one)
 
4425
      if (args[0]->result_type() != STRING_RESULT || *res2->ptr() != wild_one)
4382
4426
        return OPTIMIZE_OP;
4383
4427
    }
4384
4428
  }
4386
4430
}
4387
4431
 
4388
4432
 
4389
 
bool Item_func_like::fix_fields(Session *session, Item **ref)
 
4433
bool Item_func_like::fix_fields(THD *thd, Item **ref)
4390
4434
{
4391
 
  assert(fixed == 0);
4392
 
  if (Item_bool_func2::fix_fields(session, ref) ||
4393
 
      escape_item->fix_fields(session, &escape_item))
 
4435
  DBUG_ASSERT(fixed == 0);
 
4436
  if (Item_bool_func2::fix_fields(thd, ref) ||
 
4437
      escape_item->fix_fields(thd, &escape_item))
4394
4438
    return true;
4395
4439
 
4396
4440
  if (!escape_item->const_during_execution())
4398
4442
    my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
4399
4443
    return true;
4400
4444
  }
4401
 
 
 
4445
  
4402
4446
  if (escape_item->const_item())
4403
4447
  {
4404
 
    
4405
4448
    /* If we are on execution stage */
4406
4449
    String *escape_str= escape_item->val_str(&tmp_value1);
4407
4450
    if (escape_str)
4408
4451
    {
4409
 
      escape= (char *)memory::sql_alloc(escape_str->length());
4410
 
      strcpy(escape, escape_str->ptr()); 
 
4452
      if (escape_used_in_parsing && (
 
4453
             (((thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES) &&
 
4454
                escape_str->numchars() != 1) ||
 
4455
               escape_str->numchars() > 1)))
 
4456
      {
 
4457
        my_error(ER_WRONG_ARGUMENTS,MYF(0),"ESCAPE");
 
4458
        return true;
 
4459
      }
 
4460
 
 
4461
      if (use_mb(cmp.cmp_collation.collation))
 
4462
      {
 
4463
        CHARSET_INFO *cs= escape_str->charset();
 
4464
        my_wc_t wc;
 
4465
        int rc= cs->cset->mb_wc(cs, &wc,
 
4466
                                (const uchar*) escape_str->ptr(),
 
4467
                                (const uchar*) escape_str->ptr() +
 
4468
                                               escape_str->length());
 
4469
        escape= (int) (rc > 0 ? wc : '\\');
 
4470
      }
 
4471
      else
 
4472
      {
 
4473
        /*
 
4474
          In the case of 8bit character set, we pass native
 
4475
          code instead of Unicode code as "escape" argument.
 
4476
          Convert to "cs" if charset of escape differs.
 
4477
        */
 
4478
        CHARSET_INFO *cs= cmp.cmp_collation.collation;
 
4479
        uint32 unused;
 
4480
        if (escape_str->needs_conversion(escape_str->length(),
 
4481
                                         escape_str->charset(), cs, &unused))
 
4482
        {
 
4483
          char ch;
 
4484
          uint errors;
 
4485
          uint32 cnvlen= copy_and_convert(&ch, 1, cs, escape_str->ptr(),
 
4486
                                          escape_str->length(),
 
4487
                                          escape_str->charset(), &errors);
 
4488
          escape= cnvlen ? ch : '\\';
 
4489
        }
 
4490
        else
 
4491
          escape= *(escape_str->ptr());
 
4492
      }
4411
4493
    }
4412
4494
    else
4413
 
    {
4414
 
      escape= (char *)memory::sql_alloc(1);
4415
 
      strcpy(escape, "\\");
4416
 
    } 
4417
 
   
 
4495
      escape= '\\';
 
4496
 
4418
4497
    /*
4419
4498
      We could also do boyer-more for non-const items, but as we would have to
4420
4499
      recompute the tables for each row it's not worth it.
4421
4500
    */
4422
 
    if (args[1]->const_item() && !use_strnxfrm(collation.collation))
 
4501
    if (args[1]->const_item() && !use_strnxfrm(collation.collation) &&
 
4502
       !(specialflag & SPECIAL_NO_NEW_FUNC))
4423
4503
    {
4424
4504
      String* res2 = args[1]->val_str(&tmp_value2);
4425
4505
      if (!res2)
4426
4506
        return false;                           // Null argument
4427
 
 
 
4507
      
4428
4508
      const size_t len   = res2->length();
4429
4509
      const char*  first = res2->ptr();
4430
4510
      const char*  last  = first + len - 1;
4432
4512
        len must be > 2 ('%pattern%')
4433
4513
        heuristic: only do TurboBM for pattern_len > 2
4434
4514
      */
4435
 
 
 
4515
      
4436
4516
      if (len > MIN_TURBOBM_PATTERN_LEN + 2 &&
4437
 
          *first == internal::wild_many &&
4438
 
          *last  == internal::wild_many)
 
4517
          *first == wild_many &&
 
4518
          *last  == wild_many)
4439
4519
      {
4440
4520
        const char* tmp = first + 1;
4441
 
        for (; *tmp != internal::wild_many && *tmp != internal::wild_one; tmp++)
4442
 
        {
4443
 
          if (escape == tmp)
4444
 
            break;
4445
 
        }
4446
 
  
 
4521
        for (; *tmp != wild_many && *tmp != wild_one && *tmp != escape; tmp++) ;
4447
4522
        canDoTurboBM = (tmp == last) && !use_mb(args[0]->collation.collation);
4448
4523
      }
4449
4524
      if (canDoTurboBM)
4450
4525
      {
4451
4526
        pattern     = first + 1;
4452
4527
        pattern_len = (int) len - 2;
4453
 
        int *suff = (int*) session->alloc((int) (sizeof(int)*
 
4528
        DBUG_PRINT("info", ("Initializing pattern: '%s'", first));
 
4529
        int *suff = (int*) thd->alloc((int) (sizeof(int)*
4454
4530
                                      ((pattern_len + 1)*2+
4455
4531
                                      alphabet_size)));
4456
4532
        bmGs      = suff + pattern_len + 1;
4457
4533
        bmBc      = bmGs + pattern_len + 1;
4458
4534
        turboBM_compute_good_suffix_shifts(suff);
4459
4535
        turboBM_compute_bad_character_shifts();
 
4536
        DBUG_PRINT("info",("done"));
4460
4537
      }
4461
4538
    }
4462
4539
  }
4470
4547
}
4471
4548
 
4472
4549
#ifdef LIKE_CMP_TOUPPER
4473
 
#define likeconv(cs,A) (unsigned char) (cs)->toupper(A)
 
4550
#define likeconv(cs,A) (uchar) (cs)->toupper(A)
4474
4551
#else
4475
 
#define likeconv(cs,A) (unsigned char) (cs)->sort_order[(unsigned char) (A)]
 
4552
#define likeconv(cs,A) (uchar) (cs)->sort_order[(uchar) (A)]
4476
4553
#endif
4477
4554
 
4478
4555
 
4486
4563
  int            f = 0;
4487
4564
  int            g = plm1;
4488
4565
  int *const splm1 = suff + plm1;
4489
 
  const CHARSET_INFO * const cs= cmp.cmp_collation.collation;
 
4566
  CHARSET_INFO  *cs= cmp.cmp_collation.collation;
4490
4567
 
4491
4568
  *splm1 = pattern_len;
4492
4569
 
4493
4570
  if (!cs->sort_order)
4494
4571
  {
4495
 
    for (int i = pattern_len - 2; i >= 0; i--)
 
4572
    int i;
 
4573
    for (i = pattern_len - 2; i >= 0; i--)
4496
4574
    {
4497
4575
      int tmp = *(splm1 + i - f);
4498
4576
      if (g < i && tmp < i - g)
4499
 
        suff[i] = tmp;
 
4577
        suff[i] = tmp;
4500
4578
      else
4501
4579
      {
4502
 
        if (i < g)
4503
 
          g = i;
4504
 
        f = i;
4505
 
        while (g >= 0 && pattern[g] == pattern[g + plm1 - f])
4506
 
          g--;
4507
 
        suff[i] = f - g;
 
4580
        if (i < g)
 
4581
          g = i; // g = min(i, g)
 
4582
        f = i;
 
4583
        while (g >= 0 && pattern[g] == pattern[g + plm1 - f])
 
4584
          g--;
 
4585
        suff[i] = f - g;
4508
4586
      }
4509
4587
    }
4510
4588
  }
4511
4589
  else
4512
4590
  {
4513
 
    for (int i = pattern_len - 2; 0 <= i; --i)
 
4591
    int i;
 
4592
    for (i = pattern_len - 2; 0 <= i; --i)
4514
4593
    {
4515
4594
      int tmp = *(splm1 + i - f);
4516
4595
      if (g < i && tmp < i - g)
4517
 
        suff[i] = tmp;
 
4596
        suff[i] = tmp;
4518
4597
      else
4519
4598
      {
4520
 
        if (i < g)
4521
 
          g = i;
4522
 
        f = i;
4523
 
        while (g >= 0 &&
4524
 
               likeconv(cs, pattern[g]) == likeconv(cs, pattern[g + plm1 - f]))
4525
 
          g--;
4526
 
        suff[i] = f - g;
 
4599
        if (i < g)
 
4600
          g = i; // g = min(i, g)
 
4601
        f = i;
 
4602
        while (g >= 0 &&
 
4603
               likeconv(cs, pattern[g]) == likeconv(cs, pattern[g + plm1 - f]))
 
4604
          g--;
 
4605
        suff[i] = f - g;
4527
4606
      }
4528
4607
    }
4529
4608
  }
4584
4663
  int *end = bmBc + alphabet_size;
4585
4664
  int j;
4586
4665
  const int plm1 = pattern_len - 1;
4587
 
  const CHARSET_INFO *const cs= cmp.cmp_collation.collation;
 
4666
  CHARSET_INFO  *cs= cmp.cmp_collation.collation;
4588
4667
 
4589
4668
  for (i = bmBc; i < end; i++)
4590
4669
    *i = pattern_len;
4592
4671
  if (!cs->sort_order)
4593
4672
  {
4594
4673
    for (j = 0; j < plm1; j++)
4595
 
      bmBc[(uint32_t) (unsigned char) pattern[j]] = plm1 - j;
 
4674
      bmBc[(uint) (uchar) pattern[j]] = plm1 - j;
4596
4675
  }
4597
4676
  else
4598
4677
  {
4599
4678
    for (j = 0; j < plm1; j++)
4600
 
      bmBc[(uint32_t) likeconv(cs,pattern[j])] = plm1 - j;
 
4679
      bmBc[(uint) likeconv(cs,pattern[j])] = plm1 - j;
4601
4680
  }
4602
4681
}
4603
4682
 
4616
4695
  int shift = pattern_len;
4617
4696
  int j     = 0;
4618
4697
  int u     = 0;
4619
 
  const CHARSET_INFO * const cs= cmp.cmp_collation.collation;
 
4698
  CHARSET_INFO  *cs= cmp.cmp_collation.collation;
4620
4699
 
4621
4700
  const int plm1=  pattern_len - 1;
4622
4701
  const int tlmpl= text_len - pattern_len;
4638
4717
 
4639
4718
      register const int v = plm1 - i;
4640
4719
      turboShift = u - v;
4641
 
      bcShift    = bmBc[(uint32_t) (unsigned char) text[i + j]] - plm1 + i;
4642
 
      shift      = (turboShift > bcShift) ? turboShift : bcShift;
4643
 
      shift      = (shift > bmGs[i]) ? shift : bmGs[i];
 
4720
      bcShift    = bmBc[(uint) (uchar) text[i + j]] - plm1 + i;
 
4721
      shift      = max(turboShift, bcShift);
 
4722
      shift      = max(shift, bmGs[i]);
4644
4723
      if (shift == bmGs[i])
4645
 
        u = (pattern_len - shift < v) ? pattern_len - shift : v;
 
4724
        u = min(pattern_len - shift, v);
4646
4725
      else
4647
4726
      {
4648
 
        if (turboShift < bcShift)
4649
 
          shift= max(shift, u + 1);
4650
 
        u = 0;
 
4727
        if (turboShift < bcShift)
 
4728
          shift = max(shift, u + 1);
 
4729
        u = 0;
4651
4730
      }
4652
4731
      j+= shift;
4653
4732
    }
4660
4739
      register int i = plm1;
4661
4740
      while (i >= 0 && likeconv(cs,pattern[i]) == likeconv(cs,text[i + j]))
4662
4741
      {
4663
 
        i--;
4664
 
        if (i == plm1 - shift)
4665
 
          i-= u;
 
4742
        i--;
 
4743
        if (i == plm1 - shift)
 
4744
          i-= u;
4666
4745
      }
4667
 
 
4668
4746
      if (i < 0)
4669
 
        return 1;
 
4747
        return 1;
4670
4748
 
4671
 
      register const int v= plm1 - i;
4672
 
      turboShift= u - v;
4673
 
      bcShift= bmBc[(uint32_t) likeconv(cs, text[i + j])] - plm1 + i;
4674
 
      shift= (turboShift > bcShift) ? turboShift : bcShift;
4675
 
      shift= max(shift, bmGs[i]);
4676
 
      
 
4749
      register const int v = plm1 - i;
 
4750
      turboShift = u - v;
 
4751
      bcShift    = bmBc[(uint) likeconv(cs, text[i + j])] - plm1 + i;
 
4752
      shift      = max(turboShift, bcShift);
 
4753
      shift      = max(shift, bmGs[i]);
4677
4754
      if (shift == bmGs[i])
4678
 
        u= (pattern_len - shift < v) ? pattern_len - shift : v;
 
4755
        u = min(pattern_len - shift, v);
4679
4756
      else
4680
4757
      {
4681
 
        if (turboShift < bcShift)
4682
 
          shift= max(shift, u + 1);
4683
 
        u = 0;
 
4758
        if (turboShift < bcShift)
 
4759
          shift = max(shift, u + 1);
 
4760
        u = 0;
4684
4761
      }
4685
 
 
4686
4762
      j+= shift;
4687
4763
    }
4688
4764
    return 0;
4706
4782
    very fast to use.
4707
4783
*/
4708
4784
 
4709
 
int64_t Item_cond_xor::val_int()
 
4785
longlong Item_cond_xor::val_int()
4710
4786
{
4711
 
  assert(fixed == 1);
 
4787
  DBUG_ASSERT(fixed == 1);
4712
4788
  List_iterator<Item> li(list);
4713
4789
  Item *item;
4714
 
  int result=0;
 
4790
  int result=0; 
4715
4791
  null_value=0;
4716
4792
  while ((item=li++))
4717
4793
  {
4722
4798
      return 0;
4723
4799
    }
4724
4800
  }
4725
 
  return (int64_t) result;
 
4801
  return (longlong) result;
4726
4802
}
4727
4803
 
4728
4804
/**
4744
4820
       IS NOT NULL(a)     -> IS NULL(a)
4745
4821
    @endverbatim
4746
4822
 
4747
 
  @param session                thread handler
 
4823
  @param thd            thread handler
4748
4824
 
4749
4825
  @return
4750
4826
    New item or
4751
4827
    NULL if we cannot apply NOT transformation (see Item::neg_transformer()).
4752
4828
*/
4753
4829
 
4754
 
Item *Item_func_not::neg_transformer(Session *) /* NOT(x)  ->  x */
 
4830
Item *Item_func_not::neg_transformer(THD *thd __attribute__((__unused__)))      /* NOT(x)  ->  x */
4755
4831
{
4756
4832
  return args[0];
4757
4833
}
4758
4834
 
4759
4835
 
4760
 
Item *Item_bool_rowready_func2::neg_transformer(Session *)
 
4836
Item *Item_bool_rowready_func2::neg_transformer(THD *thd __attribute__((__unused__)))
4761
4837
{
4762
4838
  Item *item= negated_item();
4763
4839
  return item;
4767
4843
/**
4768
4844
  a IS NULL  ->  a IS NOT NULL.
4769
4845
*/
4770
 
Item *Item_func_isnull::neg_transformer(Session *)
 
4846
Item *Item_func_isnull::neg_transformer(THD *thd __attribute__((__unused__)))
4771
4847
{
4772
4848
  Item *item= new Item_func_isnotnull(args[0]);
4773
4849
  return item;
4777
4853
/**
4778
4854
  a IS NOT NULL  ->  a IS NULL.
4779
4855
*/
4780
 
Item *Item_func_isnotnull::neg_transformer(Session *)
 
4856
Item *Item_func_isnotnull::neg_transformer(THD *thd __attribute__((__unused__)))
4781
4857
{
4782
4858
  Item *item= new Item_func_isnull(args[0]);
4783
4859
  return item;
4784
4860
}
4785
4861
 
4786
4862
 
4787
 
Item *Item_cond_and::neg_transformer(Session *session)  /* NOT(a AND b AND ...)  -> */
 
4863
Item *Item_cond_and::neg_transformer(THD *thd)  /* NOT(a AND b AND ...)  -> */
4788
4864
                                        /* NOT a OR NOT b OR ... */
4789
4865
{
4790
 
  neg_arguments(session);
 
4866
  neg_arguments(thd);
4791
4867
  Item *item= new Item_cond_or(list);
4792
4868
  return item;
4793
4869
}
4794
4870
 
4795
4871
 
4796
 
Item *Item_cond_or::neg_transformer(Session *session)   /* NOT(a OR b OR ...)  -> */
 
4872
Item *Item_cond_or::neg_transformer(THD *thd)   /* NOT(a OR b OR ...)  -> */
4797
4873
                                        /* NOT a AND NOT b AND ... */
4798
4874
{
4799
 
  neg_arguments(session);
 
4875
  neg_arguments(thd);
4800
4876
  Item *item= new Item_cond_and(list);
4801
4877
  return item;
4802
4878
}
4803
4879
 
4804
4880
 
4805
 
Item *Item_func_nop_all::neg_transformer(Session *)
 
4881
Item *Item_func_nop_all::neg_transformer(THD *thd __attribute__((__unused__)))
4806
4882
{
4807
4883
  /* "NOT (e $cmp$ ANY (SELECT ...)) -> e $rev_cmp$" ALL (SELECT ...) */
4808
4884
  Item_func_not_all *new_item= new Item_func_not_all(args[0]);
4813
4889
  return new_item;
4814
4890
}
4815
4891
 
4816
 
Item *Item_func_not_all::neg_transformer(Session *)
 
4892
Item *Item_func_not_all::neg_transformer(THD *thd __attribute__((__unused__)))
4817
4893
{
4818
4894
  /* "NOT (e $cmp$ ALL (SELECT ...)) -> e $rev_cmp$" ANY (SELECT ...) */
4819
4895
  Item_func_nop_all *new_item= new Item_func_nop_all(args[0]);
4864
4940
*/
4865
4941
Item *Item_bool_rowready_func2::negated_item()
4866
4942
{
4867
 
  assert(0);
 
4943
  DBUG_ASSERT(0);
4868
4944
  return 0;
4869
4945
}
4870
4946
 
4920
4996
  fields.push_back(f);
4921
4997
}
4922
4998
 
4923
 
uint32_t Item_equal::members()
 
4999
uint Item_equal::members()
4924
5000
{
4925
5001
  return fields.elements;
4926
5002
}
4936
5012
  @retval
4937
5013
    1       if nultiple equality contains a reference to field
4938
5014
  @retval
4939
 
    0       otherwise
 
5015
    0       otherwise    
4940
5016
*/
4941
5017
 
4942
5018
bool Item_equal::contains(Field *field)
4954
5030
 
4955
5031
/**
4956
5032
  Join members of another Item_equal object.
4957
 
 
 
5033
  
4958
5034
    The function actually merges two multiple equalities.
4959
5035
    After this operation the Item_equal object additionally contains
4960
5036
    the field items of another item of the type Item_equal.
4961
5037
    If the optional constant items are not equal the cond_false flag is
4962
 
    set to 1.
 
5038
    set to 1.  
4963
5039
  @param item    multiple equality whose members are to be joined
4964
5040
*/
4965
5041
 
4969
5045
  Item *c= item->const_item;
4970
5046
  if (c)
4971
5047
  {
4972
 
    /*
4973
 
      The flag cond_false will be set to 1 after this, if
4974
 
      the multiple equality already contains a constant and its
 
5048
    /* 
 
5049
      The flag cond_false will be set to 1 after this, if 
 
5050
      the multiple equality already contains a constant and its 
4975
5051
      value is  not equal to the value of c.
4976
5052
    */
4977
5053
    add(c);
4978
5054
  }
4979
5055
  cond_false|= item->cond_false;
4980
 
}
 
5056
4981
5057
 
4982
5058
 
4983
5059
/**
5053
5129
  }
5054
5130
}
5055
5131
 
5056
 
bool Item_equal::fix_fields(Session *, Item **)
 
5132
bool Item_equal::fix_fields(THD *thd __attribute__((__unused__)), Item **ref __attribute__((__unused__)))
5057
5133
{
5058
5134
  List_iterator_fast<Item_field> li(fields);
5059
5135
  Item *item;
5088
5164
  }
5089
5165
}
5090
5166
 
5091
 
int64_t Item_equal::val_int()
 
5167
longlong Item_equal::val_int()
5092
5168
{
5093
5169
  Item_field *item_field;
5094
5170
  if (cond_false)
5101
5177
  while ((item_field= it++))
5102
5178
  {
5103
5179
    /* Skip fields of non-const tables. They haven't been read yet */
5104
 
    if (item_field->field->getTable()->const_table)
 
5180
    if (item_field->field->table->const_table)
5105
5181
    {
5106
5182
      if ((null_value= item_field->null_value) || eval_item->cmp(item_field))
5107
5183
        return 0;
5117
5193
                                      item->collation.collation);
5118
5194
}
5119
5195
 
5120
 
bool Item_equal::walk(Item_processor processor, bool walk_subquery, unsigned char *arg)
 
5196
bool Item_equal::walk(Item_processor processor, bool walk_subquery, uchar *arg)
5121
5197
{
5122
5198
  List_iterator_fast<Item_field> it(fields);
5123
5199
  Item *item;
5129
5205
  return Item_func::walk(processor, walk_subquery, arg);
5130
5206
}
5131
5207
 
5132
 
Item *Item_equal::transform(Item_transformer transformer, unsigned char *arg)
 
5208
Item *Item_equal::transform(Item_transformer transformer, uchar *arg)
5133
5209
{
5134
5210
  List_iterator<Item_field> it(fields);
5135
5211
  Item *item;
5140
5216
      return 0;
5141
5217
 
5142
5218
    /*
5143
 
      Session::change_item_tree() should be called only if the tree was
 
5219
      THD::change_item_tree() should be called only if the tree was
5144
5220
      really transformed, i.e. when a new item has been created.
5145
5221
      Otherwise we'll be allocating a lot of unnecessary memory for
5146
5222
      change records at each execution.
5147
5223
    */
5148
5224
    if (new_item != item)
5149
 
      current_session->change_item_tree((Item **) it.ref(), new_item);
 
5225
      current_thd->change_item_tree((Item **) it.ref(), new_item);
5150
5226
  }
5151
5227
  return Item_func::transform(transformer, arg);
5152
5228
}
5173
5249
  str->append(')');
5174
5250
}
5175
5251
 
5176
 
} /* namespace drizzled */