~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Monty Taylor
  • Date: 2008-08-04 19:37:18 UTC
  • mto: (261.2.2 codestyle)
  • mto: This revision was merged to the branch mainline in revision 262.
  • Revision ID: monty@inaugust.com-20080804193718-f0rz13uli4429ozb
Changed gettext_noop() to N_()

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
 
20
 
 
21
 
#include <drizzled/server_includes.h>
22
 
#include CMATH_H
23
 
 
24
 
#include <drizzled/sql_select.h>
25
 
#include <drizzled/error.h>
26
 
#include <drizzled/show.h>
27
 
#include <drizzled/item/cmpfunc.h>
28
 
#include <drizzled/item/cache_row.h>
29
 
#include <drizzled/item/type_holder.h>
30
 
#include <drizzled/item/sum.h>
31
 
#include <drizzled/functions/str/conv_charset.h>
32
 
#include <drizzled/virtual_column_info.h>
33
 
#include <drizzled/sql_base.h>
34
 
 
35
 
 
36
 
#include <drizzled/field/str.h>
37
 
#include <drizzled/field/longstr.h>
38
 
#include <drizzled/field/num.h>
39
 
#include <drizzled/field/blob.h>
40
 
#include <drizzled/field/enum.h>
41
 
#include <drizzled/field/null.h>
42
 
#include <drizzled/field/date.h>
43
 
#include <drizzled/field/fdecimal.h>
44
 
#include <drizzled/field/real.h>
45
 
#include <drizzled/field/double.h>
46
 
#include <drizzled/field/long.h>
47
 
#include <drizzled/field/int64_t.h>
48
 
#include <drizzled/field/num.h>
49
 
#include <drizzled/field/timetype.h>
50
 
#include <drizzled/field/timestamp.h>
51
 
#include <drizzled/field/datetime.h>
52
 
#include <drizzled/field/fstring.h>
53
 
#include <drizzled/field/varstring.h>
54
 
 
55
 
 
56
 
#if defined(CMATH_NAMESPACE)
57
 
using namespace CMATH_NAMESPACE;
 
1
/* Copyright (C) 2000-2006 MySQL AB
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
 
 
17
#ifdef USE_PRAGMA_IMPLEMENTATION
 
18
#pragma implementation                          // gcc: Class implementation
58
19
#endif
 
20
#include "mysql_priv.h"
 
21
#include "sql_select.h"
 
22
#include <drizzled/drizzled_error_messages.h>
59
23
 
60
24
const String my_null_string("NULL", 4, default_charset_info);
61
25
 
62
 
 
63
 
const uint32_t NO_CACHED_FIELD_INDEX= UINT32_MAX;
64
 
 
 
26
/****************************************************************************/
 
27
 
 
28
/* Hybrid_type_traits {_real} */
 
29
 
 
30
void Hybrid_type_traits::fix_length_and_dec(Item *item, Item *arg) const
 
31
{
 
32
  item->decimals= NOT_FIXED_DEC;
 
33
  item->max_length= item->float_length(arg->decimals);
 
34
}
 
35
 
 
36
static const Hybrid_type_traits real_traits_instance;
 
37
 
 
38
const Hybrid_type_traits *Hybrid_type_traits::instance()
 
39
{
 
40
  return &real_traits_instance;
 
41
}
 
42
 
 
43
 
 
44
my_decimal *
 
45
Hybrid_type_traits::val_decimal(Hybrid_type *val,
 
46
                                my_decimal *to __attribute__((unused))) const
 
47
{
 
48
  double2my_decimal(E_DEC_FATAL_ERROR, val->real, val->dec_buf);
 
49
  return val->dec_buf;
 
50
}
 
51
 
 
52
 
 
53
String *
 
54
Hybrid_type_traits::val_str(Hybrid_type *val, String *to, uint8_t decimals) const
 
55
{
 
56
  to->set_real(val->real, decimals, &my_charset_bin);
 
57
  return to;
 
58
}
 
59
 
 
60
/* Hybrid_type_traits_decimal */
 
61
static const Hybrid_type_traits_decimal decimal_traits_instance;
 
62
 
 
63
const Hybrid_type_traits_decimal *Hybrid_type_traits_decimal::instance()
 
64
{
 
65
  return &decimal_traits_instance;
 
66
}
 
67
 
 
68
 
 
69
void
 
70
Hybrid_type_traits_decimal::fix_length_and_dec(Item *item, Item *arg) const
 
71
{
 
72
  item->decimals= arg->decimals;
 
73
  item->max_length= min(arg->max_length + DECIMAL_LONGLONG_DIGITS,
 
74
                        DECIMAL_MAX_STR_LENGTH);
 
75
}
 
76
 
 
77
 
 
78
void Hybrid_type_traits_decimal::set_zero(Hybrid_type *val) const
 
79
{
 
80
  my_decimal_set_zero(&val->dec_buf[0]);
 
81
  val->used_dec_buf_no= 0;
 
82
}
 
83
 
 
84
 
 
85
void Hybrid_type_traits_decimal::add(Hybrid_type *val, Field *f) const
 
86
{
 
87
  my_decimal_add(E_DEC_FATAL_ERROR,
 
88
                 &val->dec_buf[val->used_dec_buf_no ^ 1],
 
89
                 &val->dec_buf[val->used_dec_buf_no],
 
90
                 f->val_decimal(&val->dec_buf[2]));
 
91
  val->used_dec_buf_no^= 1;
 
92
}
 
93
 
 
94
 
 
95
/**
 
96
  @todo
 
97
  what is '4' for scale?
 
98
*/
 
99
void Hybrid_type_traits_decimal::div(Hybrid_type *val, uint64_t u) const
 
100
{
 
101
  int2my_decimal(E_DEC_FATAL_ERROR, u, true, &val->dec_buf[2]);
 
102
  /* XXX: what is '4' for scale? */
 
103
  my_decimal_div(E_DEC_FATAL_ERROR,
 
104
                 &val->dec_buf[val->used_dec_buf_no ^ 1],
 
105
                 &val->dec_buf[val->used_dec_buf_no],
 
106
                 &val->dec_buf[2], 4);
 
107
  val->used_dec_buf_no^= 1;
 
108
}
 
109
 
 
110
 
 
111
int64_t
 
112
Hybrid_type_traits_decimal::val_int(Hybrid_type *val, bool unsigned_flag) const
 
113
{
 
114
  int64_t result;
 
115
  my_decimal2int(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
 
116
                 unsigned_flag, &result);
 
117
  return result;
 
118
}
 
119
 
 
120
 
 
121
double
 
122
Hybrid_type_traits_decimal::val_real(Hybrid_type *val) const
 
123
{
 
124
  my_decimal2double(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
 
125
                    &val->real);
 
126
  return val->real;
 
127
}
 
128
 
 
129
 
 
130
String *
 
131
Hybrid_type_traits_decimal::val_str(Hybrid_type *val, String *to,
 
132
                                    uint8_t decimals) const
 
133
{
 
134
  my_decimal_round(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
 
135
                   decimals, false, &val->dec_buf[2]);
 
136
  my_decimal2string(E_DEC_FATAL_ERROR, &val->dec_buf[2], 0, 0, 0, to);
 
137
  return to;
 
138
}
 
139
 
 
140
/* Hybrid_type_traits_integer */
 
141
static const Hybrid_type_traits_integer integer_traits_instance;
 
142
 
 
143
const Hybrid_type_traits_integer *Hybrid_type_traits_integer::instance()
 
144
{
 
145
  return &integer_traits_instance;
 
146
}
 
147
 
 
148
void
 
149
Hybrid_type_traits_integer::fix_length_and_dec(Item *item,
 
150
                                               Item *arg __attribute__((unused))) const
 
151
{
 
152
  item->decimals= 0;
 
153
  item->max_length= MY_INT64_NUM_DECIMAL_DIGITS;
 
154
  item->unsigned_flag= 0;
 
155
}
65
156
 
66
157
/*****************************************************************************
67
158
** Item functions
76
167
}
77
168
 
78
169
 
79
 
bool Item::is_expensive_processor(unsigned char *)
80
 
{
81
 
  return 0;
82
 
}
83
 
 
84
 
void Item::fix_after_pullout(st_select_lex *, Item **)
85
 
{}
86
 
 
87
 
 
88
 
Field *Item::tmp_table_field(Table *)
89
 
{
90
 
  return 0;
91
 
}
92
 
 
93
 
 
94
 
const char *Item::full_name(void) const
95
 
{
96
 
  return name ? name : "???";
97
 
}
98
 
 
99
 
 
100
 
int64_t Item::val_int_endpoint(bool, bool *)
101
 
{
102
 
  assert(0);
103
 
  return 0;
104
 
}
105
 
 
106
 
 
107
170
/**
108
171
  @todo
109
172
    Make this functions class dependent
196
259
                     res->ptr(), res->length(), res->charset(),
197
260
                     decimal_value) & E_DEC_BAD_NUM)
198
261
  {
199
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
262
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
200
263
                        ER_TRUNCATED_WRONG_VALUE,
201
264
                        ER(ER_TRUNCATED_WRONG_VALUE), "DECIMAL",
202
265
                        str_value.c_ptr());
312
375
  collation(&my_charset_bin, DERIVATION_COERCIBLE)
313
376
{
314
377
  marker= 0;
315
 
  maybe_null= false;
316
 
  null_value= false;
317
 
  with_sum_func= false;
318
 
  unsigned_flag= false;
319
 
  decimals= 0; 
320
 
  max_length= 0;
 
378
  maybe_null=null_value=with_sum_func=unsigned_flag=0;
 
379
  decimals= 0; max_length= 0;
321
380
  with_subselect= 0;
322
381
  cmp_context= (Item_result)-1;
323
382
 
324
383
  /* Put item in free list so that we can free all items at end */
325
 
  Session *session= current_session;
326
 
  next= session->free_list;
327
 
  session->free_list= this;
 
384
  THD *thd= current_thd;
 
385
  next= thd->free_list;
 
386
  thd->free_list= this;
328
387
  /*
329
388
    Item constructor can be called during execution other then SQL_COM
330
 
    command => we should check session->lex->current_select on zero (session->lex
 
389
    command => we should check thd->lex->current_select on zero (thd->lex
331
390
    can be uninitialised)
332
391
  */
333
 
  if (session->lex->current_select)
 
392
  if (thd->lex->current_select)
334
393
  {
335
394
    enum_parsing_place place= 
336
 
      session->lex->current_select->parsing_place;
 
395
      thd->lex->current_select->parsing_place;
337
396
    if (place == SELECT_LIST ||
338
397
        place == IN_HAVING)
339
 
      session->lex->current_select->select_n_having_items++;
 
398
      thd->lex->current_select->select_n_having_items++;
340
399
  }
341
400
}
342
401
 
347
406
  Used for duplicating lists in processing queries with temporary
348
407
  tables.
349
408
*/
350
 
Item::Item(Session *session, Item *item):
 
409
Item::Item(THD *thd, Item *item):
351
410
  is_expensive_cache(-1),
352
411
  rsize(0),
353
412
  str_value(item->str_value),
364
423
  collation(item->collation),
365
424
  cmp_context(item->cmp_context)
366
425
{
367
 
  next= session->free_list;                             // Put in free list
368
 
  session->free_list= this;
 
426
  next= thd->free_list;                         // Put in free list
 
427
  thd->free_list= this;
369
428
}
370
429
 
371
430
 
372
 
uint32_t Item::decimal_precision() const
 
431
uint Item::decimal_precision() const
373
432
{
374
433
  Item_result restype= result_type();
375
434
 
376
435
  if ((restype == DECIMAL_RESULT) || (restype == INT_RESULT))
377
 
    return cmin(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
378
 
               (unsigned int)DECIMAL_MAX_PRECISION);
379
 
  return cmin(max_length, (uint32_t)DECIMAL_MAX_PRECISION);
380
 
}
381
 
 
382
 
 
383
 
int Item::decimal_int_part() const
384
 
{
385
 
  return my_decimal_int_part(decimal_precision(), decimals);
386
 
}
387
 
 
388
 
 
389
 
void Item::print(String *str, enum_query_type)
390
 
{
391
 
  str->append(full_name());
 
436
    return min(my_decimal_length_to_precision(max_length, decimals, unsigned_flag),
 
437
               DECIMAL_MAX_PRECISION);
 
438
  return min(max_length, DECIMAL_MAX_PRECISION);
392
439
}
393
440
 
394
441
 
398
445
 
399
446
  if (name)
400
447
  {
401
 
    Session *session= current_session;
 
448
    THD *thd= current_thd;
402
449
    str->append(STRING_WITH_LEN(" AS "));
403
 
    append_identifier(session, str, name, (uint) strlen(name));
 
450
    append_identifier(thd, str, name, (uint) strlen(name));
404
451
  }
405
452
}
406
453
 
407
454
 
408
 
void Item::split_sum_func(Session *, Item **, List<Item> &)
409
 
{}
410
 
 
411
 
 
412
455
void Item::cleanup()
413
456
{
414
457
  fixed=0;
425
468
  @param arg   a dummy parameter, is not used here
426
469
*/
427
470
 
428
 
bool Item::cleanup_processor(unsigned char *)
 
471
bool Item::cleanup_processor(uchar *arg __attribute__((unused)))
429
472
{
430
473
  if (fixed)
431
474
    cleanup();
456
499
 
457
500
  This function is designed to ease transformation of Item trees.
458
501
  Re-execution note: every such transformation is registered for
459
 
  rollback by Session::change_item_tree() and is rolled back at the end
460
 
  of execution by Session::rollback_item_tree_changes().
 
502
  rollback by THD::change_item_tree() and is rolled back at the end
 
503
  of execution by THD::rollback_item_tree_changes().
461
504
 
462
505
  Therefore:
463
506
  - this function can not be used at prepared statement prepare
464
507
  (in particular, in fix_fields!), as only permanent
465
508
  transformation of Item trees are allowed at prepare.
466
509
  - the transformer function shall allocate new Items in execution
467
 
  memory root (session->mem_root) and not anywhere else: allocated
 
510
  memory root (thd->mem_root) and not anywhere else: allocated
468
511
  items will be gone in the end of execution.
469
512
 
470
513
  If you don't need to transform an item tree, but only traverse
475
518
  @param arg            opaque argument passed to the functor
476
519
 
477
520
  @return
478
 
    Returns pointer to the new subtree root.  Session::change_item_tree()
 
521
    Returns pointer to the new subtree root.  THD::change_item_tree()
479
522
    should be called for it if transformation took place, i.e. if a
480
523
    pointer to newly allocated item is returned.
481
524
*/
482
525
 
483
 
Item* Item::transform(Item_transformer transformer, unsigned char *arg)
 
526
Item* Item::transform(Item_transformer transformer, uchar *arg)
484
527
{
485
528
  return (this->*transformer)(arg);
486
529
}
504
547
  Constructor used by Item_field & Item_*_ref (see Item comment)
505
548
*/
506
549
 
507
 
Item_ident::Item_ident(Session *session, Item_ident *item)
508
 
  :Item(session, item),
 
550
Item_ident::Item_ident(THD *thd, Item_ident *item)
 
551
  :Item(thd, item),
509
552
   orig_db_name(item->orig_db_name),
510
553
   orig_table_name(item->orig_table_name), 
511
554
   orig_field_name(item->orig_field_name),
537
580
  return;
538
581
}
539
582
 
540
 
bool Item_ident::remove_dependence_processor(unsigned char * arg)
 
583
bool Item_ident::remove_dependence_processor(uchar * arg)
541
584
{
542
585
  if (depended_from == (st_select_lex *) arg)
543
586
    depended_from= 0;
563
606
    for the subsequent items.
564
607
*/
565
608
 
566
 
bool Item_field::collect_item_field_processor(unsigned char *arg)
 
609
bool Item_field::collect_item_field_processor(uchar *arg)
567
610
{
568
611
  List<Item_field> *item_list= (List<Item_field>*) arg;
569
612
  List_iterator<Item_field> item_list_it(*item_list);
594
637
    false otherwise
595
638
*/
596
639
 
597
 
bool Item_field::find_item_in_field_list_processor(unsigned char *arg)
 
640
bool Item_field::find_item_in_field_list_processor(uchar *arg)
598
641
{
599
642
  KEY_PART_INFO *first_non_group_part= *((KEY_PART_INFO **) arg);
600
643
  KEY_PART_INFO *last_part= *(((KEY_PART_INFO **) arg) + 1);
617
660
    column read set or to register used fields in a view
618
661
*/
619
662
 
620
 
bool Item_field::register_field_in_read_map(unsigned char *arg)
 
663
bool Item_field::register_field_in_read_map(uchar *arg)
621
664
{
622
 
  Table *table= (Table *) arg;
 
665
  TABLE *table= (TABLE *) arg;
623
666
  if (field->table == table || !table)
624
667
    bitmap_set_bit(field->table->read_set, field->field_index);
625
 
  if (field->vcol_info && field->vcol_info->expr_item)
626
 
    return field->vcol_info->expr_item->walk(&Item::register_field_in_read_map, 
627
 
                                             1, arg);
628
668
  return 0;
629
669
}
630
670
 
631
 
/*
632
 
  Mark field in bitmap supplied as *arg
633
 
 
634
 
*/
635
 
 
636
 
bool Item_field::register_field_in_bitmap(unsigned char *arg)
637
 
{
638
 
  MY_BITMAP *bitmap= (MY_BITMAP *) arg;
639
 
  assert(bitmap);
640
 
  bitmap_set_bit(bitmap, field->field_index);
641
 
  return false;
642
 
}
643
 
 
644
 
 
645
 
bool Item::check_cols(uint32_t c)
 
671
 
 
672
bool Item::check_cols(uint c)
646
673
{
647
674
  if (c != 1)
648
675
  {
653
680
}
654
681
 
655
682
 
656
 
void Item::set_name(const char *str, uint32_t length, const CHARSET_INFO * const cs)
 
683
void Item::set_name(const char *str, uint length, CHARSET_INFO *cs)
657
684
{
658
685
  if (!length)
659
686
  {
664
691
  }
665
692
  if (cs->ctype)
666
693
  {
667
 
    uint32_t orig_len= length;
 
694
    uint orig_len= length;
668
695
    /*
669
696
      This will probably need a better implementation in the future:
670
697
      a function in CHARSET_INFO structure.
677
704
    if (orig_len != length && !is_autogenerated_name)
678
705
    {
679
706
      if (length == 0)
680
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
707
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
681
708
                            ER_NAME_BECOMES_EMPTY, ER(ER_NAME_BECOMES_EMPTY),
682
709
                            str + length - orig_len);
683
710
      else
684
 
        push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
711
        push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
685
712
                            ER_REMOVED_SPACES, ER(ER_REMOVED_SPACES),
686
713
                            str + length - orig_len);
687
714
    }
694
721
                                   &res_length);
695
722
  }
696
723
  else
697
 
    name= sql_strmake(str, (name_length= cmin(length,(unsigned int)MAX_ALIAS_NAME)));
 
724
    name= sql_strmake(str, (name_length= min(length,MAX_ALIAS_NAME)));
698
725
}
699
726
 
700
727
 
702
729
  @details
703
730
  This function is called when:
704
731
  - Comparing items in the WHERE clause (when doing where optimization)
705
 
  - When trying to find an order_st BY/GROUP BY item in the SELECT part
 
732
  - When trying to find an ORDER BY/GROUP BY item in the SELECT part
706
733
*/
707
734
 
708
 
bool Item::eq(const Item *item, bool) const
 
735
bool Item::eq(const Item *item, bool binary_cmp __attribute__((unused))) const
709
736
{
710
737
  /*
711
738
    Note, that this is never true if item is a Item_param:
717
744
}
718
745
 
719
746
 
720
 
Item *Item::safe_charset_converter(const CHARSET_INFO * const tocs)
 
747
Item *Item::safe_charset_converter(CHARSET_INFO *tocs)
721
748
{
722
749
  Item_func_conv_charset *conv= new Item_func_conv_charset(this, tocs, 1);
723
750
  return conv->safe ? conv : NULL;
735
762
  the latter returns a non-fixed Item, so val_str() crashes afterwards.
736
763
  Override Item_num method, to return a fixed item.
737
764
*/
738
 
Item *Item_num::safe_charset_converter(const CHARSET_INFO * const)
 
765
Item *Item_num::safe_charset_converter(CHARSET_INFO *tocs __attribute__((unused)))
739
766
{
740
767
  Item_string *conv;
741
768
  char buf[64];
750
777
}
751
778
 
752
779
 
753
 
Item *Item_static_float_func::safe_charset_converter(const CHARSET_INFO * const)
 
780
Item *Item_static_float_func::safe_charset_converter(CHARSET_INFO *tocs __attribute__((unused)))
754
781
{
755
782
  Item_string *conv;
756
783
  char buf[64];
766
793
}
767
794
 
768
795
 
769
 
Item *Item_string::safe_charset_converter(const CHARSET_INFO * const tocs)
 
796
Item *Item_string::safe_charset_converter(CHARSET_INFO *tocs)
770
797
{
771
798
  Item_string *conv;
772
 
  uint32_t conv_errors;
 
799
  uint conv_errors;
773
800
  char *ptr;
774
801
  String tmp, cstr, *ostr= val_str(&tmp);
775
802
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
785
812
    */
786
813
    return NULL;
787
814
  }
788
 
  if (!(ptr= current_session->strmake(cstr.ptr(), cstr.length())))
 
815
  if (!(ptr= current_thd->strmake(cstr.ptr(), cstr.length())))
789
816
    return NULL;
790
817
  conv->str_value.set(ptr, cstr.length(), cstr.charset());
791
818
  /* Ensure that no one is going to change the result string */
794
821
}
795
822
 
796
823
 
797
 
Item *Item_param::safe_charset_converter(const CHARSET_INFO * const tocs)
 
824
Item *Item_param::safe_charset_converter(CHARSET_INFO *tocs)
798
825
{
799
826
  if (const_item())
800
827
  {
801
 
    uint32_t cnv_errors;
 
828
    uint cnv_errors;
802
829
    String *ostr= val_str(&cnvstr);
803
830
    cnvitem->str_value.copy(ostr->ptr(), ostr->length(),
804
831
                            ostr->charset(), tocs, &cnv_errors);
812
839
}
813
840
 
814
841
 
815
 
Item *Item_static_string_func::safe_charset_converter(const CHARSET_INFO * const tocs)
 
842
Item *Item_static_string_func::safe_charset_converter(CHARSET_INFO *tocs)
816
843
{
817
844
  Item_string *conv;
818
 
  uint32_t conv_errors;
 
845
  uint conv_errors;
819
846
  String tmp, cstr, *ostr= val_str(&tmp);
820
847
  cstr.copy(ostr->ptr(), ostr->length(), ostr->charset(), tocs, &conv_errors);
821
848
  if (conv_errors ||
857
884
  As a extra convenience the time structure is reset on error!
858
885
*/
859
886
 
860
 
bool Item::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
887
bool Item::get_date(DRIZZLE_TIME *ltime,uint fuzzydate)
861
888
{
862
889
  if (result_type() == STRING_RESULT)
863
890
  {
872
899
  {
873
900
    int64_t value= val_int();
874
901
    int was_cut;
875
 
    if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1L)
 
902
    if (number_to_datetime(value, ltime, fuzzydate, &was_cut) == -1LL)
876
903
    {
877
904
      char buff[22], *end;
878
905
      end= int64_t10_to_str(value, buff, -10);
879
 
      make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
906
      make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
880
907
                                   buff, (int) (end-buff), DRIZZLE_TIMESTAMP_NONE,
881
 
                                   NULL);
 
908
                                   NullS);
882
909
      goto err;
883
910
    }
884
911
  }
885
912
  return 0;
886
913
 
887
914
err:
888
 
  memset(ltime, 0, sizeof(*ltime));
 
915
  memset((char*) ltime, 0, sizeof(*ltime));
889
916
  return 1;
890
917
}
891
918
 
902
929
  if (!(res=val_str(&tmp)) ||
903
930
      str_to_time_with_warn(res->ptr(), res->length(), ltime))
904
931
  {
905
 
    memset(ltime, 0, sizeof(*ltime));
906
 
    return true;
 
932
    memset((char*) ltime, 0, sizeof(*ltime));
 
933
    return 1;
907
934
  }
908
 
  return false;
909
 
}
910
 
 
911
 
 
912
 
bool Item::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
913
 
{
914
 
  return get_date(ltime,fuzzydate);
915
 
}
916
 
 
917
 
 
918
 
bool Item::is_null()
919
 
{
920
 
  return false;
921
 
}
922
 
 
923
 
 
924
 
void Item::update_null_value ()
925
 
{
926
 
  (void) val_int();
927
 
}
928
 
 
929
 
 
930
 
void Item::top_level_item(void)
931
 
{}
932
 
 
933
 
 
934
 
void Item::set_result_field(Field *)
935
 
{}
936
 
 
937
 
 
938
 
bool Item::is_result_field(void)
939
 
{
940
 
  return 0;
941
 
}
942
 
 
943
 
 
944
 
bool Item::is_bool_func(void)
945
 
{
946
 
  return 0;
947
 
}
948
 
 
949
 
 
950
 
void Item::save_in_result_field(bool)
951
 
{}
952
 
 
953
 
 
954
 
void Item::no_rows_in_result(void)
955
 
{}
956
 
 
957
 
 
958
 
Item *Item::copy_or_same(Session *)
959
 
{
960
 
  return this;
961
 
}
962
 
 
963
 
 
964
 
Item *Item::copy_andor_structure(Session *)
965
 
{
966
 
  return this;
967
 
}
968
 
 
969
 
 
970
 
Item *Item::real_item(void)
971
 
{
972
 
  return this;
973
 
}
974
 
 
975
 
 
976
 
Item *Item::get_tmp_table_item(Session *session)
977
 
{
978
 
  return copy_or_same(session);
979
 
}
980
 
 
981
 
 
982
 
const CHARSET_INFO *Item::default_charset()
983
 
{
984
 
  return current_session->variables.collation_connection;
985
 
}
986
 
 
987
 
 
988
 
const CHARSET_INFO *Item::compare_collation()
989
 
{
990
 
  return NULL;
991
 
}
992
 
 
993
 
 
994
 
bool Item::walk(Item_processor processor, bool, unsigned char *arg)
995
 
{
996
 
  return (this->*processor)(arg);
997
 
}
998
 
 
999
 
 
1000
 
Item* Item::compile(Item_analyzer analyzer, unsigned char **arg_p,
1001
 
                    Item_transformer transformer, unsigned char *arg_t)
1002
 
{
1003
 
  if ((this->*analyzer) (arg_p))
1004
 
    return ((this->*transformer) (arg_t));
1005
 
  return 0;
1006
 
}
1007
 
 
1008
 
 
1009
 
void Item::traverse_cond(Cond_traverser traverser, void *arg, traverse_order)
1010
 
{
1011
 
  (*traverser)(this, arg);
1012
 
}
1013
 
 
1014
 
 
1015
 
bool Item::remove_dependence_processor(unsigned char *)
1016
 
{
1017
 
  return 0;
1018
 
}
1019
 
 
1020
 
 
1021
 
bool Item::remove_fixed(unsigned char *)
1022
 
{
1023
 
  fixed= 0;
1024
 
  return 0;
1025
 
}
1026
 
 
1027
 
 
1028
 
bool Item::collect_item_field_processor(unsigned char *)
1029
 
{
1030
 
  return 0;
1031
 
}
1032
 
 
1033
 
 
1034
 
bool Item::find_item_in_field_list_processor(unsigned char *)
1035
 
{
1036
 
  return 0;
1037
 
}
1038
 
 
1039
 
 
1040
 
bool Item::change_context_processor(unsigned char *)
1041
 
{
1042
 
  return 0;
1043
 
}
1044
 
 
1045
 
bool Item::reset_query_id_processor(unsigned char *)
1046
 
{
1047
 
  return 0;
1048
 
}
1049
 
 
1050
 
 
1051
 
bool Item::register_field_in_read_map(unsigned char *)
1052
 
{
1053
 
  return 0;
1054
 
}
1055
 
 
1056
 
 
1057
 
bool Item::register_field_in_bitmap(unsigned char *)
1058
 
{
1059
 
  return 0;
1060
 
}
1061
 
 
1062
 
 
1063
 
bool Item::subst_argument_checker(unsigned char **arg)
1064
 
{
1065
 
  if (*arg)
1066
 
    *arg= NULL;
1067
 
  return true;
1068
 
}
1069
 
 
1070
 
 
1071
 
bool Item::check_vcol_func_processor(unsigned char *)
1072
 
{
1073
 
  return true;
1074
 
}
1075
 
 
1076
 
 
1077
 
Item *Item::equal_fields_propagator(unsigned char *)
1078
 
{
1079
 
  return this;
1080
 
}
1081
 
 
1082
 
 
1083
 
bool Item::set_no_const_sub(unsigned char *)
1084
 
{
1085
 
  return false;
1086
 
}
1087
 
 
1088
 
 
1089
 
Item *Item::replace_equal_field(unsigned char *)
1090
 
{
1091
 
  return this;
1092
 
}
1093
 
 
1094
 
 
1095
 
Item *Item::this_item(void)
1096
 
{
1097
 
  return this;
1098
 
}
1099
 
 
1100
 
 
1101
 
const Item *Item::this_item(void) const
1102
 
{
1103
 
  return this;
1104
 
}
1105
 
 
1106
 
 
1107
 
Item **Item::this_item_addr(Session *, Item **addr_arg)
1108
 
{
1109
 
  return addr_arg;
1110
 
}
1111
 
 
1112
 
 
1113
 
uint32_t Item::cols()
1114
 
{
1115
 
  return 1;
1116
 
}
1117
 
 
1118
 
 
1119
 
Item* Item::element_index(uint32_t)
1120
 
{
1121
 
  return this;
1122
 
}
1123
 
 
1124
 
 
1125
 
Item** Item::addr(uint32_t)
1126
 
{
1127
 
  return 0;
1128
 
}
1129
 
 
1130
 
 
1131
 
bool Item::null_inside()
1132
 
{
1133
 
  return 0;
1134
 
}
1135
 
 
1136
 
 
1137
 
void Item::bring_value()
1138
 
{}
1139
 
 
1140
 
 
1141
 
Item_field *Item::filed_for_view_update()
1142
 
{
1143
 
  return 0;
1144
 
}
1145
 
 
1146
 
Item *Item::neg_transformer(Session *)
1147
 
{
1148
 
  return NULL;
1149
 
}
1150
 
 
1151
 
 
1152
 
Item *Item::update_value_transformer(unsigned char *)
1153
 
{
1154
 
  return this;
1155
 
}
1156
 
 
1157
 
 
1158
 
void Item::delete_self()
1159
 
{
1160
 
  cleanup();
1161
 
  delete this;
1162
 
}
1163
 
 
1164
 
bool Item::result_as_int64_t()
1165
 
{
1166
 
  return false;
1167
 
}
1168
 
 
1169
 
 
1170
 
bool Item::is_expensive()
1171
 
{
1172
 
  if (is_expensive_cache < 0)
1173
 
    is_expensive_cache= walk(&Item::is_expensive_processor, 0,
1174
 
                             (unsigned char*)0);
1175
 
  return test(is_expensive_cache);
1176
 
}
1177
 
 
 
935
  return 0;
 
936
}
 
937
 
 
938
CHARSET_INFO *Item::default_charset()
 
939
{
 
940
  return current_thd->variables.collation_connection;
 
941
}
 
942
 
 
943
 
 
944
/*
 
945
  Save value in field, but don't give any warnings
 
946
 
 
947
  NOTES
 
948
   This is used to temporary store and retrieve a value in a column,
 
949
   for example in opt_range to adjust the key value to fit the column.
 
950
*/
1178
951
 
1179
952
int Item::save_in_field_no_warnings(Field *field, bool no_conversions)
1180
953
{
1181
954
  int res;
1182
 
  Table *table= field->table;
1183
 
  Session *session= table->in_use;
1184
 
  enum_check_fields tmp= session->count_cuted_fields;
1185
 
  ulong sql_mode= session->variables.sql_mode;
1186
 
  session->variables.sql_mode&= ~(MODE_NO_ZERO_DATE);
1187
 
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
 
955
  TABLE *table= field->table;
 
956
  THD *thd= table->in_use;
 
957
  enum_check_fields tmp= thd->count_cuted_fields;
 
958
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
 
959
  ulong sql_mode= thd->variables.sql_mode;
 
960
  thd->variables.sql_mode&= ~(MODE_NO_ZERO_IN_DATE | MODE_NO_ZERO_DATE);
 
961
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
1188
962
  res= save_in_field(field, no_conversions);
1189
 
  session->count_cuted_fields= tmp;
1190
 
  session->variables.sql_mode= sql_mode;
 
963
  thd->count_cuted_fields= tmp;
 
964
  dbug_tmp_restore_column_map(table->write_set, old_map);
 
965
  thd->variables.sql_mode= sql_mode;
1191
966
  return res;
1192
967
}
1193
968
 
1217
992
/**
1218
993
  Move SUM items out from item tree and replace with reference.
1219
994
 
1220
 
  @param session                        Thread handler
 
995
  @param thd                    Thread handler
1221
996
  @param ref_pointer_array      Pointer to array of reference fields
1222
997
  @param fields         All fields in select
1223
998
  @param ref                    Pointer to item
1225
1000
                               SUM items
1226
1001
 
1227
1002
  @note
1228
 
    This is from split_sum_func() for items that should be split
 
1003
    This is from split_sum_func2() for items that should be split
1229
1004
 
1230
1005
    All found SUM items are added FIRST in the fields list and
1231
1006
    we replace the item with a reference.
1232
1007
 
1233
 
    session->fatal_error() may be called if we are out of memory
 
1008
    thd->fatal_error() may be called if we are out of memory
1234
1009
*/
1235
1010
 
1236
 
void Item::split_sum_func(Session *session, Item **ref_pointer_array,
1237
 
                          List<Item> &fields, Item **ref,
1238
 
                          bool skip_registered)
 
1011
void Item::split_sum_func2(THD *thd, Item **ref_pointer_array,
 
1012
                           List<Item> &fields, Item **ref, 
 
1013
                           bool skip_registered)
1239
1014
{
1240
 
  /* An item of type Item_sum  is registered <=> ref_by != 0 */
1241
 
  if (type() == SUM_FUNC_ITEM && skip_registered &&
 
1015
  /* An item of type Item_sum  is registered <=> ref_by != 0 */ 
 
1016
  if (type() == SUM_FUNC_ITEM && skip_registered && 
1242
1017
      ((Item_sum *) this)->ref_by)
1243
 
    return;
 
1018
    return;                                                 
1244
1019
  if ((type() != SUM_FUNC_ITEM && with_sum_func) ||
1245
1020
      (type() == FUNC_ITEM &&
1246
1021
       (((Item_func *) this)->functype() == Item_func::ISNOTNULLTEST_FUNC ||
1247
1022
        ((Item_func *) this)->functype() == Item_func::TRIG_COND_FUNC)))
1248
1023
  {
1249
1024
    /* Will split complicated items and ignore simple ones */
1250
 
    split_sum_func(session, ref_pointer_array, fields);
 
1025
    split_sum_func(thd, ref_pointer_array, fields);
1251
1026
  }
1252
1027
  else if ((type() == SUM_FUNC_ITEM || (used_tables() & ~PARAM_TABLE_BIT)) &&
1253
1028
           type() != SUBSELECT_ITEM &&
1254
 
           type() != REF_ITEM)
 
1029
           (type() != REF_ITEM ||
 
1030
           ((Item_ref*)this)->ref_type() == Item_ref::VIEW_REF))
1255
1031
  {
1256
1032
    /*
1257
1033
      Replace item with a reference so that we can easily calculate
1264
1040
      Item_ref to allow fields from view being stored in tmp table.
1265
1041
    */
1266
1042
    Item_aggregate_ref *item_ref;
1267
 
    uint32_t el= fields.elements;
 
1043
    uint el= fields.elements;
1268
1044
    Item *real_itm= real_item();
1269
1045
 
1270
1046
    ref_pointer_array[el]= real_itm;
1271
 
    if (!(item_ref= new Item_aggregate_ref(&session->lex->current_select->context,
 
1047
    if (!(item_ref= new Item_aggregate_ref(&thd->lex->current_select->context,
1272
1048
                                           ref_pointer_array + el, 0, name)))
1273
1049
      return;                                   // fatal_error is set
1274
1050
    if (type() == SUM_FUNC_ITEM)
1275
1051
      item_ref->depended_from= ((Item_sum *) this)->depended_from(); 
1276
1052
    fields.push_front(real_itm);
1277
 
    session->change_item_tree(ref, item_ref);
1278
 
  }
 
1053
    thd->change_item_tree(ref, item_ref);
 
1054
  }
 
1055
}
 
1056
 
 
1057
 
 
1058
static bool
 
1059
left_is_superset(DTCollation *left, DTCollation *right)
 
1060
{
 
1061
  /* Allow convert to Unicode */
 
1062
  if (left->collation->state & MY_CS_UNICODE &&
 
1063
      (left->derivation < right->derivation ||
 
1064
       (left->derivation == right->derivation &&
 
1065
        !(right->collation->state & MY_CS_UNICODE))))
 
1066
    return true;
 
1067
  /* Allow convert from ASCII */
 
1068
  if (right->repertoire == MY_REPERTOIRE_ASCII &&
 
1069
      (left->derivation < right->derivation ||
 
1070
       (left->derivation == right->derivation &&
 
1071
        !(left->repertoire == MY_REPERTOIRE_ASCII))))
 
1072
    return true;
 
1073
  /* Disallow conversion otherwise */
 
1074
  return false;
 
1075
}
 
1076
 
 
1077
/**
 
1078
  Aggregate two collations together taking
 
1079
  into account their coercibility (aka derivation):.
 
1080
 
 
1081
  0 == DERIVATION_EXPLICIT  - an explicitly written COLLATE clause @n
 
1082
  1 == DERIVATION_NONE      - a mix of two different collations @n
 
1083
  2 == DERIVATION_IMPLICIT  - a column @n
 
1084
  3 == DERIVATION_COERCIBLE - a string constant.
 
1085
 
 
1086
  The most important rules are:
 
1087
  -# If collations are the same:
 
1088
  chose this collation, and the strongest derivation.
 
1089
  -# If collations are different:
 
1090
  - Character sets may differ, but only if conversion without
 
1091
  data loss is possible. The caller provides flags whether
 
1092
  character set conversion attempts should be done. If no
 
1093
  flags are substituted, then the character sets must be the same.
 
1094
  Currently processed flags are:
 
1095
  MY_COLL_ALLOW_SUPERSET_CONV  - allow conversion to a superset
 
1096
  MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value
 
1097
  - two EXPLICIT collations produce an error, e.g. this is wrong:
 
1098
  CONCAT(expr1 collate latin1_swedish_ci, expr2 collate latin1_german_ci)
 
1099
  - the side with smaller derivation value wins,
 
1100
  i.e. a column is stronger than a string constant,
 
1101
  an explicit COLLATE clause is stronger than a column.
 
1102
  - if derivations are the same, we have DERIVATION_NONE,
 
1103
  we'll wait for an explicit COLLATE clause which possibly can
 
1104
  come from another argument later: for example, this is valid,
 
1105
  but we don't know yet when collecting the first two arguments:
 
1106
     @code
 
1107
       CONCAT(latin1_swedish_ci_column,
 
1108
              latin1_german1_ci_column,
 
1109
              expr COLLATE latin1_german2_ci)
 
1110
  @endcode
 
1111
*/
 
1112
 
 
1113
bool DTCollation::aggregate(DTCollation &dt, uint flags)
 
1114
{
 
1115
  if (!my_charset_same(collation, dt.collation))
 
1116
  {
 
1117
    /* 
 
1118
       We do allow to use binary strings (like BLOBS)
 
1119
       together with character strings.
 
1120
       Binaries have more precedence than a character
 
1121
       string of the same derivation.
 
1122
    */
 
1123
    if (collation == &my_charset_bin)
 
1124
    {
 
1125
      if (derivation <= dt.derivation)
 
1126
        ; // Do nothing
 
1127
      else
 
1128
      {
 
1129
        set(dt); 
 
1130
      }
 
1131
    }
 
1132
    else if (dt.collation == &my_charset_bin)
 
1133
    {
 
1134
      if (dt.derivation <= derivation)
 
1135
      {
 
1136
        set(dt);
 
1137
      }
 
1138
      else
 
1139
      {
 
1140
        // Do nothing
 
1141
      }
 
1142
    }
 
1143
    else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
 
1144
             left_is_superset(this, &dt))
 
1145
    {
 
1146
      // Do nothing
 
1147
    }
 
1148
    else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
 
1149
             left_is_superset(&dt, this))
 
1150
    {
 
1151
      set(dt);
 
1152
    }
 
1153
    else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
 
1154
             derivation < dt.derivation &&
 
1155
             dt.derivation >= DERIVATION_SYSCONST)
 
1156
    {
 
1157
      // Do nothing;
 
1158
    }
 
1159
    else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
 
1160
             dt.derivation < derivation &&
 
1161
             derivation >= DERIVATION_SYSCONST)
 
1162
    {
 
1163
      set(dt);
 
1164
    }
 
1165
    else
 
1166
    {
 
1167
      // Cannot apply conversion
 
1168
      set(0, DERIVATION_NONE, 0);
 
1169
      return 1;
 
1170
    }
 
1171
  }
 
1172
  else if (derivation < dt.derivation)
 
1173
  {
 
1174
    // Do nothing
 
1175
  }
 
1176
  else if (dt.derivation < derivation)
 
1177
  {
 
1178
    set(dt);
 
1179
  }
 
1180
  else
 
1181
  { 
 
1182
    if (collation == dt.collation)
 
1183
    {
 
1184
      // Do nothing
 
1185
    }
 
1186
    else 
 
1187
    {
 
1188
      if (derivation == DERIVATION_EXPLICIT)
 
1189
      {
 
1190
        set(0, DERIVATION_NONE, 0);
 
1191
        return 1;
 
1192
      }
 
1193
      if (collation->state & MY_CS_BINSORT)
 
1194
        return 0;
 
1195
      if (dt.collation->state & MY_CS_BINSORT)
 
1196
      {
 
1197
        set(dt);
 
1198
        return 0;
 
1199
      }
 
1200
      CHARSET_INFO *bin= get_charset_by_csname(collation->csname, 
 
1201
                                               MY_CS_BINSORT,MYF(0));
 
1202
      set(bin, DERIVATION_NONE);
 
1203
    }
 
1204
  }
 
1205
  repertoire|= dt.repertoire;
 
1206
  return 0;
 
1207
}
 
1208
 
 
1209
/******************************/
 
1210
static
 
1211
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, const char *fname)
 
1212
{
 
1213
  my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
 
1214
           c1.collation->name,c1.derivation_name(),
 
1215
           c2.collation->name,c2.derivation_name(),
 
1216
           fname);
 
1217
}
 
1218
 
 
1219
 
 
1220
static
 
1221
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, DTCollation &c3,
 
1222
                       const char *fname)
 
1223
{
 
1224
  my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0),
 
1225
           c1.collation->name,c1.derivation_name(),
 
1226
           c2.collation->name,c2.derivation_name(),
 
1227
           c3.collation->name,c3.derivation_name(),
 
1228
           fname);
 
1229
}
 
1230
 
 
1231
 
 
1232
static
 
1233
void my_coll_agg_error(Item** args, uint count, const char *fname,
 
1234
                       int item_sep)
 
1235
{
 
1236
  if (count == 2)
 
1237
    my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
 
1238
  else if (count == 3)
 
1239
    my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
 
1240
                      args[2*item_sep]->collation, fname);
 
1241
  else
 
1242
    my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);
 
1243
}
 
1244
 
 
1245
 
 
1246
bool agg_item_collations(DTCollation &c, const char *fname,
 
1247
                         Item **av, uint count, uint flags, int item_sep)
 
1248
{
 
1249
  uint i;
 
1250
  Item **arg;
 
1251
  c.set(av[0]->collation);
 
1252
  for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
 
1253
  {
 
1254
    if (c.aggregate((*arg)->collation, flags))
 
1255
    {
 
1256
      my_coll_agg_error(av, count, fname, item_sep);
 
1257
      return true;
 
1258
    }
 
1259
  }
 
1260
  if ((flags & MY_COLL_DISALLOW_NONE) &&
 
1261
      c.derivation == DERIVATION_NONE)
 
1262
  {
 
1263
    my_coll_agg_error(av, count, fname, item_sep);
 
1264
    return true;
 
1265
  }
 
1266
  return false;
 
1267
}
 
1268
 
 
1269
 
 
1270
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
 
1271
                                        Item **av, uint count, uint flags)
 
1272
{
 
1273
  return (agg_item_collations(c, fname, av, count,
 
1274
                              flags | MY_COLL_DISALLOW_NONE, 1));
 
1275
}
 
1276
 
 
1277
 
 
1278
/**
 
1279
  Collect arguments' character sets together.
 
1280
 
 
1281
  We allow to apply automatic character set conversion in some cases.
 
1282
  The conditions when conversion is possible are:
 
1283
  - arguments A and B have different charsets
 
1284
  - A wins according to coercibility rules
 
1285
    (i.e. a column is stronger than a string constant,
 
1286
     an explicit COLLATE clause is stronger than a column)
 
1287
  - character set of A is either superset for character set of B,
 
1288
    or B is a string constant which can be converted into the
 
1289
    character set of A without data loss.
 
1290
    
 
1291
  If all of the above is true, then it's possible to convert
 
1292
  B into the character set of A, and then compare according
 
1293
  to the collation of A.
 
1294
  
 
1295
  For functions with more than two arguments:
 
1296
  @code
 
1297
    collect(A,B,C) ::= collect(collect(A,B),C)
 
1298
  @endcode
 
1299
  Since this function calls THD::change_item_tree() on the passed Item **
 
1300
  pointers, it is necessary to pass the original Item **'s, not copies.
 
1301
  Otherwise their values will not be properly restored (see BUG#20769).
 
1302
  If the items are not consecutive (eg. args[2] and args[5]), use the
 
1303
  item_sep argument, ie.
 
1304
  @code
 
1305
    agg_item_charsets(coll, fname, &args[2], 2, flags, 3)
 
1306
  @endcode
 
1307
*/
 
1308
 
 
1309
bool agg_item_charsets(DTCollation &coll, const char *fname,
 
1310
                       Item **args, uint nargs, uint flags, int item_sep)
 
1311
{
 
1312
  Item **arg, *safe_args[2];
 
1313
 
 
1314
  memset(safe_args, 0, sizeof(safe_args));
 
1315
 
 
1316
  if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
 
1317
    return true;
 
1318
 
 
1319
  /*
 
1320
    For better error reporting: save the first and the second argument.
 
1321
    We need this only if the the number of args is 3 or 2:
 
1322
    - for a longer argument list, "Illegal mix of collations"
 
1323
      doesn't display each argument's characteristics.
 
1324
    - if nargs is 1, then this error cannot happen.
 
1325
  */
 
1326
  if (nargs >=2 && nargs <= 3)
 
1327
  {
 
1328
    safe_args[0]= args[0];
 
1329
    safe_args[1]= args[item_sep];
 
1330
  }
 
1331
 
 
1332
  THD *thd= current_thd;
 
1333
  Query_arena *arena, backup;
 
1334
  bool res= false;
 
1335
  uint i;
 
1336
  /*
 
1337
    In case we're in statement prepare, create conversion item
 
1338
    in its memory: it will be reused on each execute.
 
1339
  */
 
1340
  arena= NULL;
 
1341
 
 
1342
  for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
 
1343
  {
 
1344
    Item* conv;
 
1345
    uint32_t dummy_offset;
 
1346
    if (!String::needs_conversion(0, (*arg)->collation.collation,
 
1347
                                  coll.collation,
 
1348
                                  &dummy_offset))
 
1349
      continue;
 
1350
 
 
1351
    if (!(conv= (*arg)->safe_charset_converter(coll.collation)) &&
 
1352
        ((*arg)->collation.repertoire == MY_REPERTOIRE_ASCII))
 
1353
      conv= new Item_func_conv_charset(*arg, coll.collation, 1);
 
1354
 
 
1355
    if (!conv)
 
1356
    {
 
1357
      if (nargs >=2 && nargs <= 3)
 
1358
      {
 
1359
        /* restore the original arguments for better error message */
 
1360
        args[0]= safe_args[0];
 
1361
        args[item_sep]= safe_args[1];
 
1362
      }
 
1363
      my_coll_agg_error(args, nargs, fname, item_sep);
 
1364
      res= true;
 
1365
      break; // we cannot return here, we need to restore "arena".
 
1366
    }
 
1367
    if ((*arg)->type() == Item::FIELD_ITEM)
 
1368
      ((Item_field *)(*arg))->no_const_subst= 1;
 
1369
    /*
 
1370
      If in statement prepare, then we create a converter for two
 
1371
      constant items, do it once and then reuse it.
 
1372
      If we're in execution of a prepared statement, arena is NULL,
 
1373
      and the conv was created in runtime memory. This can be
 
1374
      the case only if the argument is a parameter marker ('?'),
 
1375
      because for all true constants the charset converter has already
 
1376
      been created in prepare. In this case register the change for
 
1377
      rollback.
 
1378
    */
 
1379
    thd->change_item_tree(arg, conv);
 
1380
    /*
 
1381
      We do not check conv->fixed, because Item_func_conv_charset which can
 
1382
      be return by safe_charset_converter can't be fixed at creation
 
1383
    */
 
1384
    conv->fix_fields(thd, arg);
 
1385
  }
 
1386
  if (arena)
 
1387
    thd->restore_active_arena(arena, &backup);
 
1388
  return res;
1279
1389
}
1280
1390
 
1281
1391
 
1292
1402
  tmp_field->decimals= field->decimals();
1293
1403
}
1294
1404
 
1295
 
 
1296
 
double Item_ident_for_show::val_real()
1297
 
{
1298
 
  return field->val_real();
1299
 
}
1300
 
 
1301
 
 
1302
 
int64_t Item_ident_for_show::val_int()
1303
 
{
1304
 
  return field->val_int();
1305
 
}
1306
 
 
1307
 
 
1308
 
String *Item_ident_for_show::val_str(String *str)
1309
 
{
1310
 
  return field->val_str(str);
1311
 
}
1312
 
 
1313
 
 
1314
 
my_decimal *Item_ident_for_show::val_decimal(my_decimal *dec)
1315
 
{
1316
 
  return field->val_decimal(dec);
1317
 
}
1318
 
 
1319
 
 
1320
1405
/**********************************************/
1321
1406
 
1322
1407
Item_field::Item_field(Field *f)
1323
 
  :Item_ident(0, NULL, *f->table_name, f->field_name),
 
1408
  :Item_ident(0, NullS, *f->table_name, f->field_name),
1324
1409
   item_equal(0), no_const_subst(0),
1325
1410
   have_privileges(0), any_privileges(0)
1326
1411
{
1340
1425
  Item_field (this is important in prepared statements).
1341
1426
*/
1342
1427
 
1343
 
Item_field::Item_field(Session *, Name_resolution_context *context_arg,
 
1428
Item_field::Item_field(THD *thd __attribute__((unused)),
 
1429
                       Name_resolution_context *context_arg,
1344
1430
                       Field *f)
1345
1431
  :Item_ident(context_arg, f->table->s->db.str, *f->table_name, f->field_name),
1346
1432
   item_equal(0), no_const_subst(0),
1357
1443
   field(0), result_field(0), item_equal(0), no_const_subst(0),
1358
1444
   have_privileges(0), any_privileges(0)
1359
1445
{
1360
 
  SELECT_LEX *select= current_session->lex->current_select;
 
1446
  SELECT_LEX *select= current_thd->lex->current_select;
1361
1447
  collation.set(DERIVATION_IMPLICIT);
1362
1448
  if (select && select->parsing_place != IN_HAVING)
1363
1449
      select->select_n_where_fields++;
1367
1453
  Constructor need to process subselect with temporary tables (see Item)
1368
1454
*/
1369
1455
 
1370
 
Item_field::Item_field(Session *session, Item_field *item)
1371
 
  :Item_ident(session, item),
 
1456
Item_field::Item_field(THD *thd, Item_field *item)
 
1457
  :Item_ident(thd, item),
1372
1458
   field(item->field),
1373
1459
   result_field(item->result_field),
1374
1460
   item_equal(item->item_equal),
1419
1505
  {
1420
1506
    tmp=(char*) sql_alloc((uint) strlen(db_name)+(uint) strlen(table_name)+
1421
1507
                          (uint) strlen(field_name)+3);
1422
 
    strxmov(tmp,db_name,".",table_name,".",field_name,NULL);
 
1508
    strxmov(tmp,db_name,".",table_name,".",field_name,NullS);
1423
1509
  }
1424
1510
  else
1425
1511
  {
1427
1513
    {
1428
1514
      tmp= (char*) sql_alloc((uint) strlen(table_name) +
1429
1515
                             (uint) strlen(field_name) + 2);
1430
 
      strxmov(tmp, table_name, ".", field_name, NULL);
 
1516
      strxmov(tmp, table_name, ".", field_name, NullS);
1431
1517
    }
1432
1518
    else
1433
1519
      tmp= (char*) field_name;
1436
1522
}
1437
1523
 
1438
1524
void Item_ident::print(String *str,
1439
 
                       enum_query_type)
 
1525
                       enum_query_type query_type __attribute__((unused)))
1440
1526
{
1441
 
  Session *session= current_session;
 
1527
  THD *thd= current_thd;
1442
1528
  char d_name_buff[MAX_ALIAS_NAME], t_name_buff[MAX_ALIAS_NAME];
1443
1529
  const char *d_name= db_name, *t_name= table_name;
1444
1530
  if (lower_case_table_names== 1 ||
1446
1532
  {
1447
1533
    if (table_name && table_name[0])
1448
1534
    {
1449
 
      my_stpcpy(t_name_buff, table_name);
 
1535
      strmov(t_name_buff, table_name);
1450
1536
      my_casedn_str(files_charset_info, t_name_buff);
1451
1537
      t_name= t_name_buff;
1452
1538
    }
1453
1539
    if (db_name && db_name[0])
1454
1540
    {
1455
 
      my_stpcpy(d_name_buff, db_name);
 
1541
      strmov(d_name_buff, db_name);
1456
1542
      my_casedn_str(files_charset_info, d_name_buff);
1457
1543
      d_name= d_name_buff;
1458
1544
    }
1462
1548
  {
1463
1549
    const char *nm= (field_name && field_name[0]) ?
1464
1550
                      field_name : name ? name : "tmp_field";
1465
 
    append_identifier(session, str, nm, (uint) strlen(nm));
 
1551
    append_identifier(thd, str, nm, (uint) strlen(nm));
1466
1552
    return;
1467
1553
  }
1468
1554
  if (db_name && db_name[0] && !alias_name_used)
1469
1555
  {
1470
1556
    {
1471
 
      append_identifier(session, str, d_name, (uint)strlen(d_name));
 
1557
      append_identifier(thd, str, d_name, (uint)strlen(d_name));
1472
1558
      str->append('.');
1473
1559
    }
1474
 
    append_identifier(session, str, t_name, (uint)strlen(t_name));
 
1560
    append_identifier(thd, str, t_name, (uint)strlen(t_name));
1475
1561
    str->append('.');
1476
 
    append_identifier(session, str, field_name, (uint)strlen(field_name));
 
1562
    append_identifier(thd, str, field_name, (uint)strlen(field_name));
1477
1563
  }
1478
1564
  else
1479
1565
  {
1480
1566
    if (table_name[0])
1481
1567
    {
1482
 
      append_identifier(session, str, t_name, (uint) strlen(t_name));
 
1568
      append_identifier(thd, str, t_name, (uint) strlen(t_name));
1483
1569
      str->append('.');
1484
 
      append_identifier(session, str, field_name, (uint) strlen(field_name));
 
1570
      append_identifier(thd, str, field_name, (uint) strlen(field_name));
1485
1571
    }
1486
1572
    else
1487
 
      append_identifier(session, str, field_name, (uint) strlen(field_name));
 
1573
      append_identifier(thd, str, field_name, (uint) strlen(field_name));
1488
1574
  }
1489
1575
}
1490
1576
 
1533
1619
  return result_field->val_str(str,&str_value);
1534
1620
}
1535
1621
 
1536
 
bool Item_field::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
1622
bool Item_field::get_date(DRIZZLE_TIME *ltime,uint fuzzydate)
1537
1623
{
1538
1624
  if ((null_value=field->is_null()) || field->get_date(ltime,fuzzydate))
1539
1625
  {
1540
 
    memset(ltime, 0, sizeof(*ltime));
 
1626
    memset((char*) ltime, 0, sizeof(*ltime));
1541
1627
    return 1;
1542
1628
  }
1543
1629
  return 0;
1544
1630
}
1545
1631
 
1546
 
bool Item_field::get_date_result(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
1632
bool Item_field::get_date_result(DRIZZLE_TIME *ltime,uint fuzzydate)
1547
1633
{
1548
1634
  if ((null_value=result_field->is_null()) ||
1549
1635
      result_field->get_date(ltime,fuzzydate))
1550
1636
  {
1551
 
    memset(ltime, 0, sizeof(*ltime));
 
1637
    memset((char*) ltime, 0, sizeof(*ltime));
1552
1638
    return 1;
1553
1639
  }
1554
1640
  return 0;
1558
1644
{
1559
1645
  if ((null_value=field->is_null()) || field->get_time(ltime))
1560
1646
  {
1561
 
    memset(ltime, 0, sizeof(*ltime));
 
1647
    memset((char*) ltime, 0, sizeof(*ltime));
1562
1648
    return 1;
1563
1649
  }
1564
1650
  return 0;
1613
1699
}
1614
1700
 
1615
1701
 
1616
 
bool Item_field::eq(const Item *item, bool) const
 
1702
bool Item_field::eq(const Item *item,
 
1703
                    bool binary_cmp __attribute__((unused))) const
1617
1704
{
1618
1705
  Item *real_item= ((Item *) item)->real_item();
1619
1706
  if (real_item->type() != FIELD_ITEM)
1651
1738
}
1652
1739
 
1653
1740
 
1654
 
enum Item_result Item_field::result_type () const
1655
 
{
1656
 
  return field->result_type();
1657
 
}
1658
 
 
1659
 
 
1660
 
Item_result Item_field::cast_to_int_type() const
1661
 
{
1662
 
  return field->cast_to_int_type();
1663
 
}
1664
 
 
1665
 
 
1666
 
enum_field_types Item_field::field_type() const
1667
 
{
1668
 
  return field->type();
1669
 
}
1670
 
 
1671
 
 
1672
 
void Item_field::fix_after_pullout(st_select_lex *new_parent, Item **)
 
1741
void Item_field::fix_after_pullout(st_select_lex *new_parent,
 
1742
                                   Item **ref __attribute__((unused)))
1673
1743
{
1674
1744
  if (new_parent == depended_from)
1675
1745
    depended_from= NULL;
1682
1752
}
1683
1753
 
1684
1754
 
1685
 
bool Item_field::is_null()
1686
 
{
1687
 
  return field->is_null();
1688
 
}
1689
 
 
1690
 
 
1691
 
Item *Item_field::get_tmp_table_item(Session *session)
1692
 
{
1693
 
  Item_field *new_item= new Item_field(session, this);
 
1755
Item *Item_field::get_tmp_table_item(THD *thd)
 
1756
{
 
1757
  Item_field *new_item= new Item_field(thd, this);
1694
1758
  if (new_item)
1695
1759
    new_item->field= new_item->result_field;
1696
1760
  return new_item;
1697
1761
}
1698
1762
 
1699
 
int64_t Item_field::val_int_endpoint(bool, bool *)
 
1763
int64_t Item_field::val_int_endpoint(bool left_endp __attribute__((unused)),
 
1764
                                      bool *incl_endp __attribute__((unused)))
1700
1765
{
1701
1766
  int64_t res= val_int();
1702
1767
  return null_value? INT64_MIN : res;
1708
1773
  This is always 'signed'. Unsigned values are created with Item_uint()
1709
1774
*/
1710
1775
 
1711
 
Item_int::Item_int(const char *str_arg, uint32_t length)
 
1776
Item_int::Item_int(const char *str_arg, uint length)
1712
1777
{
1713
1778
  char *end_ptr= (char*) str_arg + length;
1714
1779
  int error;
1733
1798
  return str;
1734
1799
}
1735
1800
 
1736
 
void Item_int::print(String *str, enum_query_type)
 
1801
void Item_int::print(String *str,
 
1802
                     enum_query_type query_type __attribute__((unused)))
1737
1803
{
1738
1804
  // my_charset_bin is good enough for numbers
1739
1805
  str_value.set(value, &my_charset_bin);
1741
1807
}
1742
1808
 
1743
1809
 
1744
 
Item_uint::Item_uint(const char *str_arg, uint32_t length):
 
1810
Item_uint::Item_uint(const char *str_arg, uint length):
1745
1811
  Item_int(str_arg, length)
1746
1812
{
1747
1813
  unsigned_flag= 1;
1748
1814
}
1749
1815
 
1750
1816
 
1751
 
Item_uint::Item_uint(const char *str_arg, int64_t i, uint32_t length):
 
1817
Item_uint::Item_uint(const char *str_arg, int64_t i, uint length):
1752
1818
  Item_int(str_arg, i, length)
1753
1819
{
1754
1820
  unsigned_flag= 1;
1764
1830
}
1765
1831
 
1766
1832
 
1767
 
void Item_uint::print(String *str, enum_query_type)
 
1833
void Item_uint::print(String *str,
 
1834
                      enum_query_type query_type __attribute__((unused)))
1768
1835
{
1769
1836
  // latin1 is good enough for numbers
1770
1837
  str_value.set((uint64_t) value, default_charset());
1772
1839
}
1773
1840
 
1774
1841
 
1775
 
Item_decimal::Item_decimal(const char *str_arg, uint32_t length,
1776
 
                           const CHARSET_INFO * const charset)
 
1842
Item_decimal::Item_decimal(const char *str_arg, uint length,
 
1843
                           CHARSET_INFO *charset)
1777
1844
{
1778
1845
  str2my_decimal(E_DEC_FATAL_ERROR, str_arg, length, charset, &decimal_value);
1779
1846
  name= (char*) str_arg;
1793
1860
}
1794
1861
 
1795
1862
 
1796
 
Item_decimal::Item_decimal(double val, int, int)
 
1863
Item_decimal::Item_decimal(double val,
 
1864
                           int precision __attribute__((unused)),
 
1865
                           int scale __attribute__((unused)))
1797
1866
{
1798
1867
  double2my_decimal(E_DEC_FATAL_ERROR, val, &decimal_value);
1799
1868
  decimals= (uint8_t) decimal_value.frac;
1804
1873
 
1805
1874
 
1806
1875
Item_decimal::Item_decimal(const char *str, const my_decimal *val_arg,
1807
 
                           uint32_t decimal_par, uint32_t length)
 
1876
                           uint decimal_par, uint length)
1808
1877
{
1809
1878
  my_decimal2decimal(val_arg, &decimal_value);
1810
1879
  name= (char*) str;
1824
1893
}
1825
1894
 
1826
1895
 
1827
 
Item_decimal::Item_decimal(const unsigned char *bin, int precision, int scale)
 
1896
Item_decimal::Item_decimal(const uchar *bin, int precision, int scale)
1828
1897
{
1829
1898
  binary2my_decimal(E_DEC_FATAL_ERROR, bin,
1830
1899
                    &decimal_value, precision, scale);
1856
1925
  return result;
1857
1926
}
1858
1927
 
1859
 
void Item_decimal::print(String *str, enum_query_type)
 
1928
void Item_decimal::print(String *str,
 
1929
                         enum_query_type query_type __attribute__((unused)))
1860
1930
{
1861
1931
  my_decimal2string(E_DEC_FATAL_ERROR, &decimal_value, 0, 0, 0, &str_value);
1862
1932
  str->append(str_value);
1863
1933
}
1864
1934
 
1865
1935
 
1866
 
bool Item_decimal::eq(const Item *item, bool) const
 
1936
bool Item_decimal::eq(const Item *item,
 
1937
                      bool binary_cmp __attribute__((unused))) const
1867
1938
{
1868
1939
  if (type() == item->type() && item->basic_const_item())
1869
1940
  {
1900
1971
}
1901
1972
 
1902
1973
 
1903
 
int64_t Item_float::val_int()
1904
 
{
1905
 
  assert(fixed == 1);
1906
 
  if (value <= (double) INT64_MIN)
1907
 
  {
1908
 
     return INT64_MIN;
1909
 
  }
1910
 
  else if (value >= (double) (uint64_t) INT64_MAX)
1911
 
  {
1912
 
    return INT64_MAX;
1913
 
  }
1914
 
  return (int64_t) rint(value);
1915
 
}
1916
 
 
1917
1974
my_decimal *Item_float::val_decimal(my_decimal *decimal_value)
1918
1975
{
1919
1976
  // following assert is redundant, because fixed=1 assigned in constructor
1940
1997
  }
1941
1998
  else
1942
1999
  {
1943
 
    Session *session= current_session;
 
2000
    THD *thd= current_thd;
1944
2001
    LEX_STRING utf8_lex_str;
1945
2002
 
1946
 
    session->convert_string(&utf8_lex_str,
 
2003
    thd->convert_string(&utf8_lex_str,
1947
2004
                        system_charset_info,
1948
2005
                        str_value.c_ptr_safe(),
1949
2006
                        str_value.length(),
1966
2023
  int error;
1967
2024
  char *end, *org_end;
1968
2025
  double tmp;
1969
 
  const CHARSET_INFO * const cs= str_value.charset();
 
2026
  CHARSET_INFO *cs= str_value.charset();
1970
2027
 
1971
2028
  org_end= (char*) str_value.ptr() + str_value.length();
1972
2029
  tmp= my_strntod(cs, (char*) str_value.ptr(), str_value.length(), &end,
1977
2034
      We can use str_value.ptr() here as Item_string is gurantee to put an
1978
2035
      end \0 here.
1979
2036
    */
1980
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2037
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
1981
2038
                        ER_TRUNCATED_WRONG_VALUE,
1982
2039
                        ER(ER_TRUNCATED_WRONG_VALUE), "DOUBLE",
1983
2040
                        str_value.ptr());
1997
2054
  int64_t tmp;
1998
2055
  char *end= (char*) str_value.ptr()+ str_value.length();
1999
2056
  char *org_end= end;
2000
 
  const CHARSET_INFO * const cs= str_value.charset();
 
2057
  CHARSET_INFO *cs= str_value.charset();
2001
2058
 
2002
2059
  tmp= (*(cs->cset->strtoll10))(cs, str_value.ptr(), &end, &err);
2003
2060
  /*
2007
2064
  if (err > 0 ||
2008
2065
      (end != org_end && !check_if_only_end_space(cs, end, org_end)))
2009
2066
  {
2010
 
    push_warning_printf(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2067
    push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2011
2068
                        ER_TRUNCATED_WRONG_VALUE,
2012
2069
                        ER(ER_TRUNCATED_WRONG_VALUE), "INTEGER",
2013
2070
                        str_value.ptr());
2022
2079
}
2023
2080
 
2024
2081
 
2025
 
bool Item_null::eq(const Item *item, bool) const
 
2082
bool Item_null::eq(const Item *item,
 
2083
                   bool binary_cmp __attribute__((unused))) const
2026
2084
{ return item->type() == type(); }
2027
2085
 
2028
2086
 
2041
2099
  return 0;
2042
2100
}
2043
2101
/* ARGSUSED */
2044
 
String *Item_null::val_str(String *)
 
2102
String *Item_null::val_str(String *str __attribute__((unused)))
2045
2103
{
2046
2104
  // following assert is redundant, because fixed=1 assigned in constructor
2047
2105
  assert(fixed == 1);
2049
2107
  return 0;
2050
2108
}
2051
2109
 
2052
 
my_decimal *Item_null::val_decimal(my_decimal *)
 
2110
my_decimal *Item_null::val_decimal(my_decimal *decimal_value __attribute__((unused)))
2053
2111
{
2054
2112
  return 0;
2055
2113
}
2056
2114
 
2057
2115
 
2058
 
Item *Item_null::safe_charset_converter(const CHARSET_INFO * const tocs)
 
2116
Item *Item_null::safe_charset_converter(CHARSET_INFO *tocs)
2059
2117
{
2060
2118
  collation.set(tocs);
2061
2119
  return this;
2069
2127
*/
2070
2128
 
2071
2129
static void
2072
 
default_set_param_func(Item_param *param, unsigned char **, ulong)
 
2130
default_set_param_func(Item_param *param,
 
2131
                       uchar **pos __attribute__((unused)),
 
2132
                       ulong len __attribute__((unused)))
2073
2133
{
2074
2134
  param->set_null();
2075
2135
}
2076
2136
 
2077
2137
 
2078
 
Item_param::Item_param(uint32_t pos_in_query_arg) :
 
2138
Item_param::Item_param(uint pos_in_query_arg) :
2079
2139
  state(NO_VALUE),
2080
2140
  item_result_type(STRING_RESULT),
2081
2141
  /* Don't pretend to be a literal unless value for this item is set. */
2146
2206
    internal decimal value.
2147
2207
*/
2148
2208
 
2149
 
void Item_param::set_decimal(char *str, ulong length)
 
2209
void Item_param::set_decimal(const char *str, ulong length)
2150
2210
{
2151
2211
  char *end;
2152
2212
 
2153
 
  end= str+length;
2154
 
  str2my_decimal((uint)E_DEC_FATAL_ERROR, str, &decimal_value, &end);
 
2213
  end= (char*) str+length;
 
2214
  str2my_decimal(E_DEC_FATAL_ERROR, str, &decimal_value, &end);
2155
2215
  state= DECIMAL_VALUE;
2156
2216
  decimals= decimal_value.frac;
2157
2217
  max_length= my_decimal_precision_to_length(decimal_value.precision(),
2174
2234
    the fact that even wrong value sent over binary protocol fits into
2175
2235
    MAX_DATE_STRING_REP_LENGTH buffer.
2176
2236
*/
2177
 
void Item_param::set_time(DRIZZLE_TIME *tm,
2178
 
                          enum enum_drizzle_timestamp_type time_type,
 
2237
void Item_param::set_time(DRIZZLE_TIME *tm, timestamp_type time_type,
2179
2238
                          uint32_t max_length_arg)
2180
2239
2181
2240
  value.time= *tm;
2187
2246
      value.time.minute > 59 || value.time.second > 59)
2188
2247
  {
2189
2248
    char buff[MAX_DATE_STRING_REP_LENGTH];
2190
 
    uint32_t length= my_TIME_to_str(&value.time, buff);
2191
 
    make_truncated_value_warning(current_session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
2249
    uint length= my_TIME_to_str(&value.time, buff);
 
2250
    make_truncated_value_warning(current_thd, MYSQL_ERROR::WARN_LEVEL_WARN,
2192
2251
                                 buff, length, time_type, 0);
2193
2252
    set_zero_time(&value.time, DRIZZLE_TIMESTAMP_ERROR);
2194
2253
  }
2207
2266
    Assign string with no conversion: data is converted only after it's
2208
2267
    been written to the binary log.
2209
2268
  */
2210
 
  uint32_t dummy_errors;
 
2269
  uint dummy_errors;
2211
2270
  if (str_value.copy(str, length, &my_charset_bin, &my_charset_bin,
2212
2271
                     &dummy_errors))
2213
2272
    return(true);
2243
2302
/**
2244
2303
  Set parameter value from user variable value.
2245
2304
 
2246
 
  @param session   Current thread
 
2305
  @param thd   Current thread
2247
2306
  @param entry User variable structure (NULL means use NULL value)
2248
2307
 
2249
2308
  @retval
2252
2311
    1 Out of memory
2253
2312
*/
2254
2313
 
2255
 
bool Item_param::set_from_user_var(Session *session, const user_var_entry *entry)
 
2314
bool Item_param::set_from_user_var(THD *thd, const user_var_entry *entry)
2256
2315
{
2257
2316
  if (entry && entry->value)
2258
2317
  {
2260
2319
    unsigned_flag= entry->unsigned_flag;
2261
2320
    if (limit_clause_param)
2262
2321
    {
2263
 
      bool unused;
 
2322
      my_bool unused;
2264
2323
      set_int(entry->val_int(&unused), MY_INT64_NUM_DECIMAL_DIGITS);
2265
2324
      item_type= Item::INT_ITEM;
2266
2325
      return(!unsigned_flag && value.integer < 0 ? 1 : 0);
2276
2335
      break;
2277
2336
    case STRING_RESULT:
2278
2337
    {
2279
 
      const CHARSET_INFO * const fromcs= entry->collation.collation;
2280
 
      const CHARSET_INFO * const tocs= session->variables.collation_connection;
 
2338
      CHARSET_INFO *fromcs= entry->collation.collation;
 
2339
      CHARSET_INFO *tocs= thd->variables.collation_connection;
2281
2340
      uint32_t dummy_offset;
2282
2341
 
2283
2342
      value.cs_info.character_set_of_placeholder= 
2403
2462
}
2404
2463
 
2405
2464
 
2406
 
bool Item_param::get_date(DRIZZLE_TIME *res, uint32_t fuzzydate)
 
2465
bool Item_param::get_date(DRIZZLE_TIME *res, uint fuzzydate)
2407
2466
{
2408
2467
  if (state == TIME_VALUE)
2409
2468
  {
2610
2669
  connection.
2611
2670
*/
2612
2671
 
2613
 
bool Item_param::convert_str_value(Session *session)
 
2672
bool Item_param::convert_str_value(THD *thd)
2614
2673
{
2615
2674
  bool rc= false;
2616
2675
  if (state == STRING_VALUE || state == LONG_DATA_VALUE)
2624
2683
    if (value.cs_info.final_character_set_of_str_value !=
2625
2684
        value.cs_info.character_set_of_placeholder)
2626
2685
    {
2627
 
      rc= session->convert_string(&str_value,
 
2686
      rc= thd->convert_string(&str_value,
2628
2687
                              value.cs_info.character_set_of_placeholder,
2629
2688
                              value.cs_info.final_character_set_of_str_value);
2630
2689
    }
2715
2774
 
2716
2775
/* End of Item_param related */
2717
2776
 
2718
 
void Item_param::print(String *str, enum_query_type)
 
2777
void Item_param::print(String *str,
 
2778
                       enum_query_type query_type __attribute__((unused)))
2719
2779
{
2720
2780
  if (state == NO_VALUE)
2721
2781
  {
2745
2805
}
2746
2806
 
2747
2807
/* ARGSUSED */
2748
 
String *Item_copy_string::val_str(String *)
 
2808
String *Item_copy_string::val_str(String *str __attribute__((unused)))
2749
2809
{
2750
2810
  // Item_copy_string is used without fix_fields call
2751
2811
  if (null_value)
2769
2829
*/
2770
2830
 
2771
2831
/* ARGSUSED */
2772
 
bool Item::fix_fields(Session *, Item **)
 
2832
bool Item::fix_fields(THD *thd __attribute__((unused)),
 
2833
                      Item **ref __attribute__((unused)))
2773
2834
{
2774
2835
 
2775
2836
  // We do not check fields which are fixed during construction
2823
2884
}
2824
2885
 
2825
2886
 
2826
 
bool Item_ref_null_helper::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzydate)
 
2887
bool Item_ref_null_helper::get_date(DRIZZLE_TIME *ltime, uint fuzzydate)
2827
2888
{  
2828
2889
  return (owner->was_null|= null_value= (*ref)->get_date(ltime, fuzzydate));
2829
2890
}
2833
2894
  Mark item and SELECT_LEXs as dependent if item was resolved in
2834
2895
  outer SELECT.
2835
2896
 
2836
 
  @param session             thread handler
 
2897
  @param thd             thread handler
2837
2898
  @param last            select from which current item depend
2838
2899
  @param current         current select
2839
2900
  @param resolved_item   item which was resolved in outer SELECT(for warning)
2841
2902
                         substitution)
2842
2903
*/
2843
2904
 
2844
 
static void mark_as_dependent(Session *session, SELECT_LEX *last, SELECT_LEX *current,
 
2905
static void mark_as_dependent(THD *thd, SELECT_LEX *last, SELECT_LEX *current,
2845
2906
                              Item_ident *resolved_item,
2846
2907
                              Item_ident *mark_item)
2847
2908
{
2853
2914
  if (mark_item)
2854
2915
    mark_item->depended_from= last;
2855
2916
  current->mark_as_dependent(last);
2856
 
  if (session->lex->describe & DESCRIBE_EXTENDED)
 
2917
  if (thd->lex->describe & DESCRIBE_EXTENDED)
2857
2918
  {
2858
 
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
 
2919
    char warn_buff[MYSQL_ERRMSG_SIZE];
2859
2920
    sprintf(warn_buff, ER(ER_WARN_FIELD_RESOLVED),
2860
2921
            db_name, (db_name[0] ? "." : ""),
2861
2922
            table_name, (table_name [0] ? "." : ""),
2862
2923
            resolved_item->field_name,
2863
2924
            current->select_number, last->select_number);
2864
 
    push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
 
2925
    push_warning(thd, MYSQL_ERROR::WARN_LEVEL_NOTE,
2865
2926
                 ER_WARN_FIELD_RESOLVED, warn_buff);
2866
2927
  }
2867
2928
}
2871
2932
  Mark range of selects and resolved identifier (field/reference)
2872
2933
  item as dependent.
2873
2934
 
2874
 
  @param session             thread handler
 
2935
  @param thd             thread handler
2875
2936
  @param last_select     select where resolved_item was resolved
2876
2937
  @param current_sel     current select (select where resolved_item was placed)
2877
2938
  @param found_field     field which was found during resolving
2887
2948
    resolved identifier.
2888
2949
*/
2889
2950
 
2890
 
void mark_select_range_as_dependent(Session *session,
 
2951
void mark_select_range_as_dependent(THD *thd,
2891
2952
                                    SELECT_LEX *last_select,
2892
2953
                                    SELECT_LEX *current_sel,
2893
2954
                                    Field *found_field, Item *found_item,
2925
2986
      prev_subselect_item->used_tables_cache|=
2926
2987
        found_field->table->map;
2927
2988
    prev_subselect_item->const_item_cache= 0;
2928
 
    mark_as_dependent(session, last_select, current_sel, resolved_item,
 
2989
    mark_as_dependent(thd, last_select, current_sel, resolved_item,
2929
2990
                      dependent);
2930
2991
  }
2931
2992
}
2946
3007
    - NULL if find_item is not in group_list
2947
3008
*/
2948
3009
 
2949
 
static Item** find_field_in_group_list(Item *find_item, order_st *group_list)
 
3010
static Item** find_field_in_group_list(Item *find_item, ORDER *group_list)
2950
3011
{
2951
3012
  const char *db_name;
2952
3013
  const char *table_name;
2953
3014
  const char *field_name;
2954
 
  order_st      *found_group= NULL;
 
3015
  ORDER      *found_group= NULL;
2955
3016
  int         found_match_degree= 0;
2956
3017
  Item_ident *cur_field;
2957
3018
  int         cur_match_degree= 0;
2977
3038
 
2978
3039
  assert(field_name != 0);
2979
3040
 
2980
 
  for (order_st *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
 
3041
  for (ORDER *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
2981
3042
  {
2982
3043
    if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM)
2983
3044
    {
3024
3085
          is ambiguous.
3025
3086
        */
3026
3087
        my_error(ER_NON_UNIQ_ERROR, MYF(0),
3027
 
                 find_item->full_name(), current_session->where);
 
3088
                 find_item->full_name(), current_thd->where);
3028
3089
        return NULL;
3029
3090
      }
3030
3091
    }
3050
3111
  derived SELECT column. This extension is allowed only if the
3051
3112
  MODE_ONLY_FULL_GROUP_BY sql mode isn't enabled.
3052
3113
 
3053
 
  @param session     current thread
 
3114
  @param thd     current thread
3054
3115
  @param ref     column reference being resolved
3055
3116
  @param select  the select that ref is resolved against
3056
3117
 
3074
3135
*/
3075
3136
 
3076
3137
static Item**
3077
 
resolve_ref_in_select_and_group(Session *session, Item_ident *ref, SELECT_LEX *select)
 
3138
resolve_ref_in_select_and_group(THD *thd, Item_ident *ref, SELECT_LEX *select)
3078
3139
{
3079
3140
  Item **group_by_ref= NULL;
3080
3141
  Item **select_ref= NULL;
3081
 
  order_st *group_list= (order_st*) select->group_list.first;
 
3142
  ORDER *group_list= (ORDER*) select->group_list.first;
3082
3143
  bool ambiguous_fields= false;
3083
 
  uint32_t counter;
 
3144
  uint counter;
3084
3145
  enum_resolution_type resolution;
3085
3146
 
3086
3147
  /*
3104
3165
        !((*group_by_ref)->eq(*select_ref, 0)))
3105
3166
    {
3106
3167
      ambiguous_fields= true;
3107
 
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
 
3168
      push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
3108
3169
                          ER(ER_NON_UNIQ_ERROR), ref->full_name(),
3109
 
                          current_session->where);
 
3170
                          current_thd->where);
3110
3171
 
3111
3172
    }
3112
3173
  }
3147
3208
  current select as dependent. The found reference of field should be
3148
3209
  provided in 'from_field'.
3149
3210
 
3150
 
  @param[in] session             current thread
 
3211
  @param[in] thd             current thread
3151
3212
  @param[in,out] from_field  found field reference or (Field*)not_found_field
3152
3213
  @param[in,out] reference   view column if this item was resolved to a
3153
3214
    view column
3175
3236
*/
3176
3237
 
3177
3238
int
3178
 
Item_field::fix_outer_field(Session *session, Field **from_field, Item **reference)
 
3239
Item_field::fix_outer_field(THD *thd, Field **from_field, Item **reference)
3179
3240
{
3180
3241
  enum_parsing_place place= NO_MATTER;
3181
3242
  bool field_found= (*from_field != not_found_field);
3192
3253
  */
3193
3254
  Name_resolution_context *last_checked_context= context;
3194
3255
  Item **ref= (Item **) not_found_item;
3195
 
  SELECT_LEX *current_sel= (SELECT_LEX *) session->lex->current_select;
 
3256
  SELECT_LEX *current_sel= (SELECT_LEX *) thd->lex->current_select;
3196
3257
  Name_resolution_context *outer_context= 0;
3197
3258
  SELECT_LEX *select= 0;
3198
3259
  /* Currently derived tables cannot be correlated */
3222
3283
      the found view field into '*reference', in other words, it
3223
3284
      substitutes this Item_field with the found expression.
3224
3285
    */
3225
 
    if (field_found || (*from_field= find_field_in_tables(session, this,
 
3286
    if (field_found || (*from_field= find_field_in_tables(thd, this,
3226
3287
                                          outer_context->
3227
3288
                                            first_name_resolution_table,
3228
3289
                                          outer_context->
3256
3317
            ;
3257
3318
            if (!(rf= new Item_outer_ref(context, this)))
3258
3319
              return -1;
3259
 
            session->change_item_tree(reference, rf);
 
3320
            thd->change_item_tree(reference, rf);
3260
3321
            select->inner_refs_list.push_back(rf);
3261
 
            rf->in_sum_func= session->lex->in_sum_func;
 
3322
            rf->in_sum_func= thd->lex->in_sum_func;
3262
3323
          }
3263
3324
          /*
3264
3325
            A reference is resolved to a nest level that's outer or the same as
3265
3326
            the nest level of the enclosing set function : adjust the value of
3266
3327
            max_arg_level for the function if it's needed.
3267
3328
          */
3268
 
          if (session->lex->in_sum_func &&
3269
 
              session->lex->in_sum_func->nest_level >= select->nest_level)
 
3329
          if (thd->lex->in_sum_func &&
 
3330
              thd->lex->in_sum_func->nest_level >= select->nest_level)
3270
3331
          {
3271
3332
            Item::Type ref_type= (*reference)->type();
3272
 
            set_if_bigger(session->lex->in_sum_func->max_arg_level,
 
3333
            set_if_bigger(thd->lex->in_sum_func->max_arg_level,
3273
3334
                          select->nest_level);
3274
3335
            set_field(*from_field);
3275
3336
            fixed= 1;
3276
 
            mark_as_dependent(session, last_checked_context->select_lex,
 
3337
            mark_as_dependent(thd, last_checked_context->select_lex,
3277
3338
                              context->select_lex, this,
3278
3339
                              ((ref_type == REF_ITEM ||
3279
3340
                                ref_type == FIELD_ITEM) ?
3288
3349
            (*reference)->used_tables();
3289
3350
          prev_subselect_item->const_item_cache&=
3290
3351
            (*reference)->const_item();
3291
 
          mark_as_dependent(session, last_checked_context->select_lex,
 
3352
          mark_as_dependent(thd, last_checked_context->select_lex,
3292
3353
                            context->select_lex, this,
3293
3354
                            ((ref_type == REF_ITEM || ref_type == FIELD_ITEM) ?
3294
3355
                             (Item_ident*) (*reference) :
3308
3369
    /* Search in SELECT and GROUP lists of the outer select. */
3309
3370
    if (place != IN_WHERE && place != IN_ON)
3310
3371
    {
3311
 
      if (!(ref= resolve_ref_in_select_and_group(session, this, select)))
 
3372
      if (!(ref= resolve_ref_in_select_and_group(thd, this, select)))
3312
3373
        return -1; /* Some error occurred (e.g. ambiguous names). */
3313
3374
      if (ref != not_found_item)
3314
3375
      {
3336
3397
    if (upward_lookup)
3337
3398
    {
3338
3399
      // We can't say exactly what absent table or field
3339
 
      my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), session->where);
 
3400
      my_error(ER_BAD_FIELD_ERROR, MYF(0), full_name(), thd->where);
3340
3401
    }
3341
3402
    else
3342
3403
    {
3343
3404
      /* Call find_field_in_tables only to report the error */
3344
 
      find_field_in_tables(session, this,
 
3405
      find_field_in_tables(thd, this,
3345
3406
                           context->first_name_resolution_table,
3346
3407
                           context->last_name_resolution_table,
3347
3408
                           reference, REPORT_ALL_ERRORS,
3380
3441
    if (place != IN_HAVING && select->group_list.elements)
3381
3442
    {
3382
3443
      outer_context->select_lex->inner_refs_list.push_back((Item_outer_ref*)rf);
3383
 
      ((Item_outer_ref*)rf)->in_sum_func= session->lex->in_sum_func;
 
3444
      ((Item_outer_ref*)rf)->in_sum_func= thd->lex->in_sum_func;
3384
3445
    }
3385
 
    session->change_item_tree(reference, rf);
 
3446
    thd->change_item_tree(reference, rf);
3386
3447
    /*
3387
3448
      rf is Item_ref => never substitute other items (in this case)
3388
3449
      during fix_fields() => we can use rf after fix_fields()
3389
3450
    */
3390
3451
    assert(!rf->fixed);                // Assured by Item_ref()
3391
 
    if (rf->fix_fields(session, reference) || rf->check_cols(1))
 
3452
    if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3392
3453
      return -1;
3393
3454
 
3394
 
    mark_as_dependent(session, last_checked_context->select_lex,
 
3455
    mark_as_dependent(thd, last_checked_context->select_lex,
3395
3456
                      context->select_lex, this,
3396
3457
                      rf);
3397
3458
    return 0;
3398
3459
  }
3399
3460
  else
3400
3461
  {
3401
 
    mark_as_dependent(session, last_checked_context->select_lex,
 
3462
    mark_as_dependent(thd, last_checked_context->select_lex,
3402
3463
                      context->select_lex,
3403
3464
                      this, (Item_ident*)*reference);
3404
3465
    if (last_checked_context->select_lex->having_fix_field)
3409
3470
                       (char*) cached_table->alias, (char*) field_name);
3410
3471
      if (!rf)
3411
3472
        return -1;
3412
 
      session->change_item_tree(reference, rf);
 
3473
      thd->change_item_tree(reference, rf);
3413
3474
      /*
3414
3475
        rf is Item_ref => never substitute other items (in this case)
3415
3476
        during fix_fields() => we can use rf after fix_fields()
3416
3477
      */
3417
3478
      assert(!rf->fixed);                // Assured by Item_ref()
3418
 
      if (rf->fix_fields(session, reference) || rf->check_cols(1))
 
3479
      if (rf->fix_fields(thd, reference) || rf->check_cols(1))
3419
3480
        return -1;
3420
3481
      return 0;
3421
3482
    }
3459
3520
    Notice that compared to Item_ref::fix_fields, here we first search the FROM
3460
3521
    clause, and then we search the SELECT and GROUP BY clauses.
3461
3522
 
3462
 
  @param[in]     session        current thread
 
3523
  @param[in]     thd        current thread
3463
3524
  @param[in,out] reference  view column if this item was resolved to a
3464
3525
    view column
3465
3526
 
3469
3530
    false on success
3470
3531
*/
3471
3532
 
3472
 
bool Item_field::fix_fields(Session *session, Item **reference)
 
3533
bool Item_field::fix_fields(THD *thd, Item **reference)
3473
3534
{
3474
3535
  assert(fixed == 0);
3475
3536
  Field *from_field= (Field *)not_found_field;
3482
3543
      expression to 'reference', i.e. it substitute that expression instead
3483
3544
      of this Item_field
3484
3545
    */
3485
 
    if ((from_field= find_field_in_tables(session, this,
 
3546
    if ((from_field= find_field_in_tables(thd, this,
3486
3547
                                          context->first_name_resolution_table,
3487
3548
                                          context->last_name_resolution_table,
3488
3549
                                          reference,
3489
 
                                          session->lex->use_only_table_context ?
 
3550
                                          thd->lex->use_only_table_context ?
3490
3551
                                            REPORT_ALL_ERRORS : 
3491
3552
                                            IGNORE_EXCEPT_NON_UNIQUE,
3492
3553
                                          !any_privileges,
3495
3556
    {
3496
3557
      int ret;
3497
3558
      /* Look up in current select's item_list to find aliased fields */
3498
 
      if (session->lex->current_select->is_item_list_lookup)
 
3559
      if (thd->lex->current_select->is_item_list_lookup)
3499
3560
      {
3500
 
        uint32_t counter;
 
3561
        uint counter;
3501
3562
        enum_resolution_type resolution;
3502
 
        Item** res= find_item_in_list(this, session->lex->current_select->item_list,
 
3563
        Item** res= find_item_in_list(this, thd->lex->current_select->item_list,
3503
3564
                                      &counter, REPORT_EXCEPT_NOT_FOUND,
3504
3565
                                      &resolution);
3505
3566
        if (!res)
3523
3584
            {
3524
3585
              /* The column to which we link isn't valid. */
3525
3586
              my_error(ER_BAD_FIELD_ERROR, MYF(0), (*res)->name, 
3526
 
                       current_session->where);
 
3587
                       current_thd->where);
3527
3588
              return(1);
3528
3589
            }
3529
3590
 
3540
3601
            Item_ref *rf= new Item_ref(context, db_name,table_name,field_name);
3541
3602
            if (!rf)
3542
3603
              return 1;
3543
 
            session->change_item_tree(reference, rf);
 
3604
            thd->change_item_tree(reference, rf);
3544
3605
            /*
3545
3606
              Because Item_ref never substitutes itself with other items 
3546
3607
              in Item_ref::fix_fields(), we can safely use the original 
3547
3608
              pointer to it even after fix_fields()
3548
3609
             */
3549
 
            return rf->fix_fields(session, reference) ||  rf->check_cols(1);
 
3610
            return rf->fix_fields(thd, reference) ||  rf->check_cols(1);
3550
3611
          }
3551
3612
        }
3552
3613
      }
3553
 
      if ((ret= fix_outer_field(session, &from_field, reference)) < 0)
 
3614
      if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
3554
3615
        goto error;
3555
3616
      outer_fixed= true;
3556
3617
      if (!ret)
3564
3625
        cached_table->select_lex != context->select_lex)
3565
3626
    {
3566
3627
      int ret;
3567
 
      if ((ret= fix_outer_field(session, &from_field, reference)) < 0)
 
3628
      if ((ret= fix_outer_field(thd, &from_field, reference)) < 0)
3568
3629
        goto error;
3569
3630
      outer_fixed= 1;
3570
3631
      if (!ret)
3587
3648
      return false;
3588
3649
 
3589
3650
    set_field(from_field);
3590
 
    if (session->lex->in_sum_func &&
3591
 
        session->lex->in_sum_func->nest_level == 
3592
 
        session->lex->current_select->nest_level)
3593
 
      set_if_bigger(session->lex->in_sum_func->max_arg_level,
3594
 
                    session->lex->current_select->nest_level);
 
3651
    if (thd->lex->in_sum_func &&
 
3652
        thd->lex->in_sum_func->nest_level == 
 
3653
        thd->lex->current_select->nest_level)
 
3654
      set_if_bigger(thd->lex->in_sum_func->max_arg_level,
 
3655
                    thd->lex->current_select->nest_level);
3595
3656
  }
3596
 
  else if (session->mark_used_columns != MARK_COLUMNS_NONE)
 
3657
  else if (thd->mark_used_columns != MARK_COLUMNS_NONE)
3597
3658
  {
3598
 
    Table *table= field->table;
 
3659
    TABLE *table= field->table;
3599
3660
    MY_BITMAP *current_bitmap, *other_bitmap;
3600
 
    if (session->mark_used_columns == MARK_COLUMNS_READ)
 
3661
    if (thd->mark_used_columns == MARK_COLUMNS_READ)
3601
3662
    {
3602
3663
      current_bitmap= table->read_set;
3603
3664
      other_bitmap=   table->write_set;
3624
3685
  return false;
3625
3686
 
3626
3687
error:
3627
 
  context->process_error(session);
 
3688
  context->process_error(thd);
3628
3689
  return true;
3629
3690
}
3630
3691
 
3631
 
Item *Item_field::safe_charset_converter(const CHARSET_INFO * const tocs)
 
3692
Item *Item_field::safe_charset_converter(CHARSET_INFO *tocs)
3632
3693
{
3633
3694
  no_const_subst= 1;
3634
3695
  return Item::safe_charset_converter(tocs);
3648
3709
  return;
3649
3710
}
3650
3711
 
3651
 
 
3652
 
bool Item_field::result_as_int64_t()
3653
 
{
3654
 
  return field->can_be_compared_as_int64_t();
3655
 
}
3656
 
 
3657
 
 
3658
3712
/**
3659
3713
  Find a field among specified multiple equalities.
3660
3714
 
3724
3778
    false  otherwise
3725
3779
*/
3726
3780
 
3727
 
bool Item_field::subst_argument_checker(unsigned char **arg)
 
3781
bool Item_field::subst_argument_checker(uchar **arg)
3728
3782
{
3729
3783
  return (result_type() != STRING_RESULT) || (*arg);
3730
3784
}
3754
3808
    - pointer to the field item, otherwise.
3755
3809
*/
3756
3810
 
3757
 
Item *Item_field::equal_fields_propagator(unsigned char *arg)
 
3811
Item *Item_field::equal_fields_propagator(uchar *arg)
3758
3812
{
3759
3813
  if (no_const_subst)
3760
3814
    return this;
3787
3841
  See comments in Arg_comparator::set_compare_func() for details.
3788
3842
*/
3789
3843
 
3790
 
bool Item_field::set_no_const_sub(unsigned char *)
 
3844
bool Item_field::set_no_const_sub(uchar *arg __attribute__((unused)))
3791
3845
{
3792
3846
  if (field->charset() != &my_charset_bin)
3793
3847
    no_const_subst=1;
3820
3874
    - this - otherwise.
3821
3875
*/
3822
3876
 
3823
 
Item *Item_field::replace_equal_field(unsigned char *)
 
3877
Item *Item_field::replace_equal_field(uchar *arg __attribute__((unused)))
3824
3878
{
3825
3879
  if (item_equal)
3826
3880
  {
3840
3894
}
3841
3895
 
3842
3896
 
3843
 
uint32_t Item_field::max_disp_length()
3844
 
{
3845
 
  return field->max_display_length();
3846
 
}
3847
 
 
3848
 
 
3849
3897
void Item::init_make_field(Send_field *tmp_field,
3850
3898
                           enum enum_field_types field_type_arg)
3851
3899
{
3862
3910
  tmp_field->type=              field_type_arg;
3863
3911
  tmp_field->length=max_length;
3864
3912
  tmp_field->decimals=decimals;
 
3913
  if (unsigned_flag)
 
3914
    tmp_field->flags |= UNSIGNED_FLAG;
3865
3915
}
3866
3916
 
3867
3917
void Item::make_field(Send_field *tmp_field)
3904
3954
{
3905
3955
  switch (field_type())
3906
3956
  {
3907
 
    case DRIZZLE_TYPE_DATE:
 
3957
    case DRIZZLE_TYPE_NEWDATE:
3908
3958
    case DRIZZLE_TYPE_DATETIME:
3909
3959
    case DRIZZLE_TYPE_TIMESTAMP:
3910
3960
      return true;
3918
3968
String *Item::check_well_formed_result(String *str, bool send_error)
3919
3969
{
3920
3970
  /* Check whether we got a well-formed string */
3921
 
  const CHARSET_INFO * const cs= str->charset();
 
3971
  CHARSET_INFO *cs= str->charset();
3922
3972
  int well_formed_error;
3923
 
  uint32_t wlen= cs->cset->well_formed_len(cs,
 
3973
  uint wlen= cs->cset->well_formed_len(cs,
3924
3974
                                       str->ptr(), str->ptr() + str->length(),
3925
3975
                                       str->length(), &well_formed_error);
3926
3976
  if (wlen < str->length())
3927
3977
  {
3928
 
    Session *session= current_session;
 
3978
    THD *thd= current_thd;
3929
3979
    char hexbuf[7];
3930
 
    enum DRIZZLE_ERROR::enum_warning_level level;
3931
 
    uint32_t diff= str->length() - wlen;
 
3980
    enum MYSQL_ERROR::enum_warning_level level;
 
3981
    uint diff= str->length() - wlen;
3932
3982
    set_if_smaller(diff, 3);
3933
3983
    octet2hex(hexbuf, str->ptr() + wlen, diff);
3934
3984
    if (send_error)
3938
3988
      return 0;
3939
3989
    }
3940
3990
    {
3941
 
      level= DRIZZLE_ERROR::WARN_LEVEL_ERROR;
 
3991
      level= MYSQL_ERROR::WARN_LEVEL_ERROR;
3942
3992
      null_value= 1;
3943
3993
      str= 0;
3944
3994
    }
3945
 
    push_warning_printf(session, level, ER_INVALID_CHARACTER_STRING,
 
3995
    push_warning_printf(thd, level, ER_INVALID_CHARACTER_STRING,
3946
3996
                        ER(ER_INVALID_CHARACTER_STRING), cs->csname, hexbuf);
3947
3997
  }
3948
3998
  return str;
3969
4019
    0    otherwise
3970
4020
*/
3971
4021
 
3972
 
bool Item::eq_by_collation(Item *item, bool binary_cmp, const CHARSET_INFO * const cs)
 
4022
bool Item::eq_by_collation(Item *item, bool binary_cmp, CHARSET_INFO *cs)
3973
4023
{
3974
 
  const CHARSET_INFO *save_cs= 0;
3975
 
  const CHARSET_INFO *save_item_cs= 0;
 
4024
  CHARSET_INFO *save_cs= 0;
 
4025
  CHARSET_INFO *save_item_cs= 0;
3976
4026
  if (collation.collation != cs)
3977
4027
  {
3978
4028
    save_cs= collation.collation;
4002
4052
  @param table          Table for which the field is created
4003
4053
*/
4004
4054
 
4005
 
Field *Item::make_string_field(Table *table)
 
4055
Field *Item::make_string_field(TABLE *table)
4006
4056
{
4007
4057
  Field *field;
4008
4058
  assert(collation.collation);
4031
4081
    \#    Created field
4032
4082
*/
4033
4083
 
4034
 
Field *Item::tmp_table_field_from_field_type(Table *table, bool)
 
4084
Field *Item::tmp_table_field_from_field_type(TABLE *table, bool fixed_length __attribute__((unused)))
4035
4085
{
4036
4086
  /*
4037
4087
    The field functions defines a field to be not null if null_ptr is not 0
4038
4088
  */
4039
 
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
 
4089
  uchar *null_ptr= maybe_null ? (uchar*) "" : 0;
4040
4090
  Field *field;
4041
4091
 
4042
4092
  switch (field_type()) {
4043
4093
  case DRIZZLE_TYPE_NEWDECIMAL:
4044
 
    field= new Field_new_decimal((unsigned char*) 0, max_length, null_ptr, 0,
 
4094
    field= new Field_new_decimal((uchar*) 0, max_length, null_ptr, 0,
4045
4095
                                 Field::NONE, name, decimals, 0,
4046
4096
                                 unsigned_flag);
4047
4097
    break;
 
4098
  case DRIZZLE_TYPE_TINY:
 
4099
    field= new Field_tiny((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
 
4100
                          name, 0, unsigned_flag);
 
4101
    break;
 
4102
  case DRIZZLE_TYPE_SHORT:
 
4103
    field= new Field_short((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
 
4104
                           name, 0, unsigned_flag);
 
4105
    break;
4048
4106
  case DRIZZLE_TYPE_LONG:
4049
 
    field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
4107
    field= new Field_long((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4050
4108
                          name, 0, unsigned_flag);
4051
4109
    break;
4052
4110
  case DRIZZLE_TYPE_LONGLONG:
4053
 
    field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
4111
    field= new Field_int64_t((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4054
4112
                              name, 0, unsigned_flag);
4055
4113
    break;
4056
4114
  case DRIZZLE_TYPE_DOUBLE:
4057
 
    field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
4115
    field= new Field_double((uchar*) 0, max_length, null_ptr, 0, Field::NONE,
4058
4116
                            name, decimals, 0, unsigned_flag);
4059
4117
    break;
4060
4118
  case DRIZZLE_TYPE_NULL:
4061
 
    field= new Field_null((unsigned char*) 0, max_length, Field::NONE,
 
4119
    field= new Field_null((uchar*) 0, max_length, Field::NONE,
4062
4120
                          name, &my_charset_bin);
4063
4121
    break;
4064
 
  case DRIZZLE_TYPE_DATE:
4065
 
    field= new Field_date(maybe_null, name, &my_charset_bin);
 
4122
  case DRIZZLE_TYPE_NEWDATE:
 
4123
    field= new Field_newdate(maybe_null, name, &my_charset_bin);
4066
4124
    break;
4067
4125
  case DRIZZLE_TYPE_TIME:
4068
4126
    field= new Field_time(maybe_null, name, &my_charset_bin);
4078
4136
    assert(0);
4079
4137
    /* Fall through to make_string_field() */
4080
4138
  case DRIZZLE_TYPE_ENUM:
 
4139
  case DRIZZLE_TYPE_SET:
4081
4140
  case DRIZZLE_TYPE_VARCHAR:
4082
4141
    return make_string_field(table);
4083
4142
  case DRIZZLE_TYPE_BLOB:
4194
4253
  if (result_type() == STRING_RESULT)
4195
4254
  {
4196
4255
    String *result;
4197
 
    const CHARSET_INFO * const cs= collation.collation;
 
4256
    CHARSET_INFO *cs= collation.collation;
4198
4257
    char buff[MAX_FIELD_WIDTH];         // Alloc buffer for small columns
4199
4258
    str_value.set_quick(buff, sizeof(buff), cs);
4200
4259
    result=val_str(&str_value);
4248
4307
}
4249
4308
 
4250
4309
 
4251
 
int Item_string::save_in_field(Field *field, bool)
 
4310
int Item_string::save_in_field(Field *field,
 
4311
                               bool no_conversions __attribute__((unused)))
4252
4312
{
4253
4313
  String *result;
4254
4314
  result=val_str(&str_value);
4263
4323
}
4264
4324
 
4265
4325
 
4266
 
int Item_int::save_in_field(Field *field, bool)
 
4326
int Item_int::save_in_field(Field *field,
 
4327
                            bool no_conversions __attribute__((unused)))
4267
4328
{
4268
4329
  int64_t nr=val_int();
4269
4330
  if (null_value)
4273
4334
}
4274
4335
 
4275
4336
 
4276
 
int Item_decimal::save_in_field(Field *field, bool)
 
4337
int Item_decimal::save_in_field(Field *field,
 
4338
                                bool no_conversions __attribute__((unused)))
4277
4339
{
4278
4340
  field->set_notnull();
4279
4341
  return field->store_decimal(&decimal_value);
4280
4342
}
4281
4343
 
4282
4344
 
4283
 
bool Item_int::eq(const Item *arg, bool) const
 
4345
bool Item_int::eq(const Item *arg,
 
4346
                  bool binary_cmp __attribute__((unused))) const
4284
4347
{
4285
4348
  /* No need to check for null value as basic constant can't be NULL */
4286
4349
  if (arg->basic_const_item() && arg->type() == type())
4316
4379
}
4317
4380
 
4318
4381
 
4319
 
static uint32_t nr_of_decimals(const char *str, const char *end)
 
4382
static uint nr_of_decimals(const char *str, const char *end)
4320
4383
{
4321
4384
  const char *decimal_point;
4322
4385
 
4344
4407
  value is not a true double value (overflow)
4345
4408
*/
4346
4409
 
4347
 
Item_float::Item_float(const char *str_arg, uint32_t length)
 
4410
Item_float::Item_float(const char *str_arg, uint length)
4348
4411
{
4349
4412
  int error;
4350
4413
  char *end_not_used;
4366
4429
}
4367
4430
 
4368
4431
 
4369
 
int Item_float::save_in_field(Field *field, bool)
 
4432
int Item_float::save_in_field(Field *field,
 
4433
                              bool no_conversions __attribute__((unused)))
4370
4434
{
4371
4435
  double nr= val_real();
4372
4436
  if (null_value)
4376
4440
}
4377
4441
 
4378
4442
 
4379
 
void Item_float::print(String *str, enum_query_type)
 
4443
void Item_float::print(String *str,
 
4444
                       enum_query_type query_type __attribute__((unused)))
4380
4445
{
4381
4446
  if (presentation)
4382
4447
  {
4396
4461
  In number context this is a int64_t value.
4397
4462
*/
4398
4463
 
4399
 
bool Item_float::eq(const Item *arg, bool) const
 
4464
bool Item_float::eq(const Item *arg,
 
4465
                    bool binary_cmp __attribute__((unused))) const
4400
4466
{
4401
4467
  if (arg->basic_const_item() && arg->type() == type())
4402
4468
  {
4411
4477
}
4412
4478
 
4413
4479
 
4414
 
inline uint32_t char_val(char X)
 
4480
inline uint char_val(char X)
4415
4481
{
4416
4482
  return (uint) (X >= '0' && X <= '9' ? X-'0' :
4417
4483
                 X >= 'A' && X <= 'Z' ? X-'A'+10 :
4419
4485
}
4420
4486
 
4421
4487
 
4422
 
Item_hex_string::Item_hex_string(const char *str, uint32_t str_length)
 
4488
Item_hex_string::Item_hex_string(const char *str, uint str_length)
4423
4489
{
4424
4490
  max_length=(str_length+1)/2;
4425
4491
  char *ptr=(char*) sql_alloc(max_length+1);
4445
4511
  // following assert is redundant, because fixed=1 assigned in constructor
4446
4512
  assert(fixed == 1);
4447
4513
  char *end=(char*) str_value.ptr()+str_value.length(),
4448
 
       *ptr=end-cmin(str_value.length(),(uint32_t)sizeof(int64_t));
 
4514
       *ptr=end-min(str_value.length(),sizeof(int64_t));
4449
4515
 
4450
4516
  uint64_t value=0;
4451
4517
  for (; ptr != end ; ptr++)
4452
 
    value=(value << 8)+ (uint64_t) (unsigned char) *ptr;
 
4518
    value=(value << 8)+ (uint64_t) (uchar) *ptr;
4453
4519
  return (int64_t) value;
4454
4520
}
4455
4521
 
4464
4530
}
4465
4531
 
4466
4532
 
4467
 
int Item_hex_string::save_in_field(Field *field, bool)
 
4533
int Item_hex_string::save_in_field(Field *field,
 
4534
                                   bool no_conversions __attribute__((unused)))
4468
4535
{
4469
4536
  field->set_notnull();
4470
4537
  if (field->result_type() == STRING_RESULT)
4471
 
    return field->store(str_value.ptr(), str_value.length(),
 
4538
    return field->store(str_value.ptr(), str_value.length(), 
4472
4539
                        collation.collation);
4473
4540
 
4474
4541
  uint64_t nr;
4488
4555
 
4489
4556
warn:
4490
4557
  if (!field->store((int64_t) nr, true))
4491
 
    field->set_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE,
 
4558
    field->set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE,
4492
4559
                       1);
4493
4560
  return 1;
4494
4561
}
4495
4562
 
4496
4563
 
4497
 
void Item_hex_string::print(String *str, enum_query_type)
 
4564
void Item_hex_string::print(String *str,
 
4565
                            enum_query_type query_type __attribute__((unused)))
4498
4566
{
4499
4567
  char *end= (char*) str_value.ptr() + str_value.length(),
4500
 
       *ptr= end - cmin(str_value.length(), (uint32_t)sizeof(int64_t));
 
4568
       *ptr= end - min(str_value.length(), sizeof(int64_t));
4501
4569
  str->append("0x");
4502
4570
  for (; ptr != end ; ptr++)
4503
4571
  {
4504
 
    str->append(_dig_vec_lower[((unsigned char) *ptr) >> 4]);
4505
 
    str->append(_dig_vec_lower[((unsigned char) *ptr) & 0x0F]);
 
4572
    str->append(_dig_vec_lower[((uchar) *ptr) >> 4]);
 
4573
    str->append(_dig_vec_lower[((uchar) *ptr) & 0x0F]);
4506
4574
  }
4507
4575
}
4508
4576
 
4519
4587
}
4520
4588
 
4521
4589
 
4522
 
Item *Item_hex_string::safe_charset_converter(const CHARSET_INFO * const tocs)
 
4590
Item *Item_hex_string::safe_charset_converter(CHARSET_INFO *tocs)
4523
4591
{
4524
4592
  Item_string *conv;
4525
4593
  String tmp, *str= val_str(&tmp);
4538
4606
  In number context this is a int64_t value.
4539
4607
*/
4540
4608
  
4541
 
Item_bin_string::Item_bin_string(const char *str, uint32_t str_length)
 
4609
Item_bin_string::Item_bin_string(const char *str, uint str_length)
4542
4610
{
4543
4611
  const char *end= str + str_length - 1;
4544
 
  unsigned char bits= 0;
4545
 
  uint32_t power= 1;
 
4612
  uchar bits= 0;
 
4613
  uint power= 1;
4546
4614
 
4547
4615
  max_length= (str_length + 7) >> 3;
4548
4616
  char *ptr= (char*) sql_alloc(max_length + 1);
4574
4642
*/
4575
4643
 
4576
4644
bool Item_null::send(Protocol *protocol,
4577
 
                     String *)
 
4645
                     String *packet __attribute__((unused)))
4578
4646
{
4579
4647
  return protocol->store_null();
4580
4648
}
4592
4660
  default:
4593
4661
  case DRIZZLE_TYPE_NULL:
4594
4662
  case DRIZZLE_TYPE_ENUM:
 
4663
  case DRIZZLE_TYPE_SET:
4595
4664
  case DRIZZLE_TYPE_BLOB:
4596
4665
  case DRIZZLE_TYPE_VARCHAR:
4597
4666
  case DRIZZLE_TYPE_NEWDECIMAL:
4601
4670
      result= protocol->store(res->ptr(),res->length(),res->charset());
4602
4671
    break;
4603
4672
  }
 
4673
  case DRIZZLE_TYPE_TINY:
 
4674
  {
 
4675
    int64_t nr;
 
4676
    nr= val_int();
 
4677
    if (!null_value)
 
4678
      result= protocol->store_tiny(nr);
 
4679
    break;
 
4680
  }
 
4681
  case DRIZZLE_TYPE_SHORT:
 
4682
  {
 
4683
    int64_t nr;
 
4684
    nr= val_int();
 
4685
    if (!null_value)
 
4686
      result= protocol->store_short(nr);
 
4687
    break;
 
4688
  }
4604
4689
  case DRIZZLE_TYPE_LONG:
4605
4690
  {
4606
4691
    int64_t nr;
4631
4716
    get_date(&tm, TIME_FUZZY_DATE);
4632
4717
    if (!null_value)
4633
4718
    {
4634
 
      if (f_type == DRIZZLE_TYPE_DATE)
 
4719
      if (f_type == DRIZZLE_TYPE_NEWDATE)
4635
4720
        return protocol->store_date(&tm);
4636
4721
      else
4637
4722
        result= protocol->store(&tm);
4653
4738
}
4654
4739
 
4655
4740
 
4656
 
bool Item_field::send(Protocol *protocol, String *)
 
4741
bool Item_field::send(Protocol *protocol,
 
4742
                      String *buffer __attribute__((unused)))
4657
4743
{
4658
4744
  return protocol->store(result_field);
4659
4745
}
4665
4751
    need to set no_errors to prevent warnings about type conversion
4666
4752
    popping up.
4667
4753
  */
4668
 
  Session *session= field->table->in_use;
 
4754
  THD *thd= field->table->in_use;
4669
4755
  int no_errors;
4670
4756
 
4671
 
  no_errors= session->no_errors;
4672
 
  session->no_errors= 1;
 
4757
  no_errors= thd->no_errors;
 
4758
  thd->no_errors= 1;
4673
4759
  Item::update_null_value();
4674
 
  session->no_errors= no_errors;
 
4760
  thd->no_errors= no_errors;
4675
4761
}
4676
4762
 
4677
4763
 
4697
4783
    this field    otherwise
4698
4784
*/
4699
4785
 
4700
 
Item *Item_field::update_value_transformer(unsigned char *select_arg)
 
4786
Item *Item_field::update_value_transformer(uchar *select_arg)
4701
4787
{
4702
4788
  SELECT_LEX *select= (SELECT_LEX*)select_arg;
4703
4789
  assert(fixed);
4704
4790
 
4705
 
  if (field->table != select->context.table_list->table)
 
4791
  if (field->table != select->context.table_list->table &&
 
4792
      type() != Item::TRIGGER_FIELD_ITEM)
4706
4793
  {
4707
4794
    List<Item> *all_fields= &select->join->all_fields;
4708
4795
    Item **ref_pointer_array= select->ref_pointer_array;
4739
4826
                   Item **item, const char *table_name_arg,
4740
4827
                   const char *field_name_arg,
4741
4828
                   bool alias_name_used_arg)
4742
 
  :Item_ident(context_arg, NULL, table_name_arg, field_name_arg),
 
4829
  :Item_ident(context_arg, NullS, table_name_arg, field_name_arg),
4743
4830
   result_field(0), ref(item)
4744
4831
{
4745
4832
  alias_name_used= alias_name_used_arg;
4799
4886
    Item_field::fix_fields, here we first search the SELECT and GROUP BY
4800
4887
    clauses, and then we search the FROM clause.
4801
4888
 
4802
 
  @param[in]     session        current thread
 
4889
  @param[in]     thd        current thread
4803
4890
  @param[in,out] reference  view column if this item was resolved to a
4804
4891
    view column
4805
4892
 
4815
4902
    false on success
4816
4903
*/
4817
4904
 
4818
 
bool Item_ref::fix_fields(Session *session, Item **reference)
 
4905
bool Item_ref::fix_fields(THD *thd, Item **reference)
4819
4906
{
4820
4907
  enum_parsing_place place= NO_MATTER;
4821
4908
  assert(fixed == 0);
4822
 
  SELECT_LEX *current_sel= session->lex->current_select;
 
4909
  SELECT_LEX *current_sel= thd->lex->current_select;
4823
4910
 
4824
4911
  if (!ref || ref == not_found_item)
4825
4912
  {
4826
 
    if (!(ref= resolve_ref_in_select_and_group(session, this,
 
4913
    if (!(ref= resolve_ref_in_select_and_group(thd, this,
4827
4914
                                               context->select_lex)))
4828
4915
      goto error;             /* Some error occurred (e.g. ambiguous names). */
4829
4916
 
4838
4925
      {
4839
4926
        /* The current reference cannot be resolved in this query. */
4840
4927
        my_error(ER_BAD_FIELD_ERROR,MYF(0),
4841
 
                 this->full_name(), current_session->where);
 
4928
                 this->full_name(), current_thd->where);
4842
4929
        goto error;
4843
4930
      }
4844
4931
 
4863
4950
        /* Search in the SELECT and GROUP lists of the outer select. */
4864
4951
        if (outer_context->resolve_in_select_list)
4865
4952
        {
4866
 
          if (!(ref= resolve_ref_in_select_and_group(session, this, select)))
 
4953
          if (!(ref= resolve_ref_in_select_and_group(thd, this, select)))
4867
4954
            goto error; /* Some error occurred (e.g. ambiguous names). */
4868
4955
          if (ref != not_found_item)
4869
4956
          {
4900
4987
            field expression to 'reference', i.e. it substitute that
4901
4988
            expression instead of this Item_ref
4902
4989
          */
4903
 
          from_field= find_field_in_tables(session, this,
 
4990
          from_field= find_field_in_tables(thd, this,
4904
4991
                                           outer_context->
4905
4992
                                             first_name_resolution_table,
4906
4993
                                           outer_context->
4918
5005
            prev_subselect_item->const_item_cache&=
4919
5006
              (*reference)->const_item();
4920
5007
            assert((*reference)->type() == REF_ITEM);
4921
 
            mark_as_dependent(session, last_checked_context->select_lex,
 
5008
            mark_as_dependent(thd, last_checked_context->select_lex,
4922
5009
                              context->select_lex, this,
4923
5010
                              ((refer_type == REF_ITEM ||
4924
5011
                                refer_type == FIELD_ITEM) ?
4971
5058
        Item_field* fld;
4972
5059
        if (!(fld= new Item_field(from_field)))
4973
5060
          goto error;
4974
 
        session->change_item_tree(reference, fld);
4975
 
        mark_as_dependent(session, last_checked_context->select_lex,
4976
 
                          session->lex->current_select, this, fld);
 
5061
        thd->change_item_tree(reference, fld);
 
5062
        mark_as_dependent(thd, last_checked_context->select_lex,
 
5063
                          thd->lex->current_select, this, fld);
4977
5064
        /*
4978
5065
          A reference is resolved to a nest level that's outer or the same as
4979
5066
          the nest level of the enclosing set function : adjust the value of
4980
5067
          max_arg_level for the function if it's needed.
4981
5068
        */
4982
 
        if (session->lex->in_sum_func &&
4983
 
            session->lex->in_sum_func->nest_level >= 
 
5069
        if (thd->lex->in_sum_func &&
 
5070
            thd->lex->in_sum_func->nest_level >= 
4984
5071
            last_checked_context->select_lex->nest_level)
4985
 
          set_if_bigger(session->lex->in_sum_func->max_arg_level,
 
5072
          set_if_bigger(thd->lex->in_sum_func->max_arg_level,
4986
5073
                        last_checked_context->select_lex->nest_level);
4987
5074
        return false;
4988
5075
      }
4990
5077
      {
4991
5078
        /* The item was not a table field and not a reference */
4992
5079
        my_error(ER_BAD_FIELD_ERROR, MYF(0),
4993
 
                 this->full_name(), current_session->where);
 
5080
                 this->full_name(), current_thd->where);
4994
5081
        goto error;
4995
5082
      }
4996
5083
      /* Should be checked in resolve_ref_in_select_and_group(). */
4997
5084
      assert(*ref && (*ref)->fixed);
4998
 
      mark_as_dependent(session, last_checked_context->select_lex,
 
5085
      mark_as_dependent(thd, last_checked_context->select_lex,
4999
5086
                        context->select_lex, this, this);
5000
5087
      /*
5001
5088
        A reference is resolved to a nest level that's outer or the same as
5002
5089
        the nest level of the enclosing set function : adjust the value of
5003
5090
        max_arg_level for the function if it's needed.
5004
5091
      */
5005
 
      if (session->lex->in_sum_func &&
5006
 
          session->lex->in_sum_func->nest_level >= 
 
5092
      if (thd->lex->in_sum_func &&
 
5093
          thd->lex->in_sum_func->nest_level >= 
5007
5094
          last_checked_context->select_lex->nest_level)
5008
 
        set_if_bigger(session->lex->in_sum_func->max_arg_level,
 
5095
        set_if_bigger(thd->lex->in_sum_func->max_arg_level,
5009
5096
                      last_checked_context->select_lex->nest_level);
5010
5097
    }
5011
5098
  }
5038
5125
  return false;
5039
5126
 
5040
5127
error:
5041
 
  context->process_error(session);
 
5128
  context->process_error(thd);
5042
5129
  return true;
5043
5130
}
5044
5131
 
5080
5167
    if ((*ref)->type() != Item::CACHE_ITEM && ref_type() != VIEW_REF &&
5081
5168
        !table_name && name && alias_name_used)
5082
5169
    {
5083
 
      Session *session= current_session;
5084
 
      append_identifier(session, str, name, (uint) strlen(name));
 
5170
      THD *thd= current_thd;
 
5171
      append_identifier(thd, str, name, (uint) strlen(name));
5085
5172
    }
5086
5173
    else
5087
5174
      (*ref)->print(str, query_type);
5220
5307
}
5221
5308
 
5222
5309
 
5223
 
bool Item_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
5310
bool Item_ref::get_date(DRIZZLE_TIME *ltime,uint fuzzydate)
5224
5311
{
5225
5312
  return (null_value=(*ref)->get_date_result(ltime,fuzzydate));
5226
5313
}
5262
5349
}
5263
5350
 
5264
5351
 
5265
 
Item *Item_ref::get_tmp_table_item(Session *session)
 
5352
Item *Item_ref::get_tmp_table_item(THD *thd)
5266
5353
{
5267
5354
  if (!result_field)
5268
 
    return (*ref)->get_tmp_table_item(session);
 
5355
    return (*ref)->get_tmp_table_item(thd);
5269
5356
 
5270
5357
  Item_field *item= new Item_field(result_field);
5271
5358
  if (item)
5334
5421
}
5335
5422
 
5336
5423
 
5337
 
bool Item_direct_ref::get_date(DRIZZLE_TIME *ltime,uint32_t fuzzydate)
 
5424
bool Item_direct_ref::get_date(DRIZZLE_TIME *ltime,uint fuzzydate)
5338
5425
{
5339
5426
  return (null_value=(*ref)->get_date(ltime,fuzzydate));
5340
5427
}
5341
5428
 
 
5429
 
 
5430
/**
 
5431
  Prepare referenced field then call usual Item_direct_ref::fix_fields .
 
5432
 
 
5433
  @param thd         thread handler
 
5434
  @param reference   reference on reference where this item stored
 
5435
 
 
5436
  @retval
 
5437
    false   OK
 
5438
  @retval
 
5439
    true    Error
 
5440
*/
 
5441
 
 
5442
bool Item_direct_view_ref::fix_fields(THD *thd, Item **reference)
 
5443
{
 
5444
  /* view fild reference must be defined */
 
5445
  assert(*ref);
 
5446
  /* (*ref)->check_cols() will be made in Item_direct_ref::fix_fields */
 
5447
  if (!(*ref)->fixed &&
 
5448
      ((*ref)->fix_fields(thd, ref)))
 
5449
    return true;
 
5450
  return Item_direct_ref::fix_fields(thd, reference);
 
5451
}
 
5452
 
5342
5453
/*
5343
5454
  Prepare referenced outer field then call usual Item_direct_ref::fix_fields
5344
5455
 
5345
5456
  SYNOPSIS
5346
5457
    Item_outer_ref::fix_fields()
5347
 
    session         thread handler
 
5458
    thd         thread handler
5348
5459
    reference   reference on reference where this item stored
5349
5460
 
5350
5461
  RETURN
5352
5463
    true    Error
5353
5464
*/
5354
5465
 
5355
 
bool Item_outer_ref::fix_fields(Session *session, Item **reference)
 
5466
bool Item_outer_ref::fix_fields(THD *thd, Item **reference)
5356
5467
{
5357
5468
  bool err;
5358
5469
  /* outer_ref->check_cols() will be made in Item_direct_ref::fix_fields */
5359
 
  if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(session, reference)))
 
5470
  if ((*ref) && !(*ref)->fixed && ((*ref)->fix_fields(thd, reference)))
5360
5471
    return true;
5361
 
  err= Item_direct_ref::fix_fields(session, reference);
 
5472
  err= Item_direct_ref::fix_fields(thd, reference);
5362
5473
  if (!outer_ref)
5363
5474
    outer_ref= *ref;
5364
5475
  if ((*ref)->type() == Item::FIELD_ITEM)
5375
5486
  }
5376
5487
}
5377
5488
 
5378
 
void Item_ref::fix_after_pullout(st_select_lex *new_parent, Item **)
 
5489
void Item_ref::fix_after_pullout(st_select_lex *new_parent,
 
5490
                                 Item **refptr __attribute__((unused)))
5379
5491
{
5380
5492
  if (depended_from == new_parent)
5381
5493
  {
5384
5496
  }
5385
5497
}
5386
5498
 
 
5499
/**
 
5500
  Compare two view column references for equality.
 
5501
 
 
5502
  A view column reference is considered equal to another column
 
5503
  reference if the second one is a view column and if both column
 
5504
  references resolve to the same item. It is assumed that both
 
5505
  items are of the same type.
 
5506
 
 
5507
  @param item        item to compare with
 
5508
  @param binary_cmp  make binary comparison
 
5509
 
 
5510
  @retval
 
5511
    true    Referenced item is equal to given item
 
5512
  @retval
 
5513
    false   otherwise
 
5514
*/
 
5515
 
 
5516
bool Item_direct_view_ref::eq(const Item *item,
 
5517
                              bool binary_cmp __attribute__((unused))) const
 
5518
{
 
5519
  if (item->type() == REF_ITEM)
 
5520
  {
 
5521
    Item_ref *item_ref= (Item_ref*) item;
 
5522
    if (item_ref->ref_type() == VIEW_REF)
 
5523
    {
 
5524
      Item *item_ref_ref= *(item_ref->ref);
 
5525
      return ((*ref)->real_item() == item_ref_ref->real_item());
 
5526
    }
 
5527
  }
 
5528
  return false;
 
5529
}
 
5530
 
5387
5531
bool Item_default_value::eq(const Item *item, bool binary_cmp) const
5388
5532
{
5389
5533
  return item->type() == DEFAULT_VALUE_ITEM &&
5391
5535
}
5392
5536
 
5393
5537
 
5394
 
bool Item_default_value::fix_fields(Session *session, Item **)
 
5538
bool Item_default_value::fix_fields(THD *thd,
 
5539
                                    Item **items __attribute__((unused)))
5395
5540
{
5396
5541
  Item *real_arg;
5397
5542
  Item_field *field_arg;
5403
5548
    fixed= 1;
5404
5549
    return false;
5405
5550
  }
5406
 
  if (!arg->fixed && arg->fix_fields(session, &arg))
 
5551
  if (!arg->fixed && arg->fix_fields(thd, &arg))
5407
5552
    goto error;
5408
5553
 
5409
5554
 
5430
5575
  return false;
5431
5576
 
5432
5577
error:
5433
 
  context->process_error(session);
 
5578
  context->process_error(thd);
5434
5579
  return true;
5435
5580
}
5436
5581
 
5463
5608
 
5464
5609
      {
5465
5610
        push_warning_printf(field_arg->table->in_use,
5466
 
                            DRIZZLE_ERROR::WARN_LEVEL_WARN,
 
5611
                            MYSQL_ERROR::WARN_LEVEL_WARN,
5467
5612
                            ER_NO_DEFAULT_FOR_FIELD,
5468
5613
                            ER(ER_NO_DEFAULT_FOR_FIELD),
5469
5614
                            field_arg->field_name);
5482
5627
  same time it can replace some nodes in the tree.
5483
5628
*/ 
5484
5629
 
5485
 
Item *Item_default_value::transform(Item_transformer transformer, unsigned char *args)
 
5630
Item *Item_default_value::transform(Item_transformer transformer, uchar *args)
5486
5631
{
5487
5632
  Item *new_item= arg->transform(transformer, args);
5488
5633
  if (!new_item)
5489
5634
    return 0;
5490
5635
 
5491
5636
  /*
5492
 
    Session::change_item_tree() should be called only if the tree was
 
5637
    THD::change_item_tree() should be called only if the tree was
5493
5638
    really transformed, i.e. when a new item has been created.
5494
5639
    Otherwise we'll be allocating a lot of unnecessary memory for
5495
5640
    change records at each execution.
5496
5641
  */
5497
5642
  if (arg != new_item)
5498
 
    current_session->change_item_tree(&arg, new_item);
 
5643
    current_thd->change_item_tree(&arg, new_item);
5499
5644
  return (this->*transformer)(args);
5500
5645
}
5501
5646
 
5507
5652
}
5508
5653
 
5509
5654
 
5510
 
bool Item_insert_value::fix_fields(Session *session, Item **)
 
5655
bool Item_insert_value::fix_fields(THD *thd,
 
5656
                                   Item **items __attribute__((unused)))
5511
5657
{
5512
5658
  assert(fixed == 0);
5513
5659
  /* We should only check that arg is in first table */
5514
5660
  if (!arg->fixed)
5515
5661
  {
5516
5662
    bool res;
5517
 
    TableList *orig_next_table= context->last_name_resolution_table;
 
5663
    TABLE_LIST *orig_next_table= context->last_name_resolution_table;
5518
5664
    context->last_name_resolution_table= context->first_name_resolution_table;
5519
 
    res= arg->fix_fields(session, &arg);
 
5665
    res= arg->fix_fields(thd, &arg);
5520
5666
    context->last_name_resolution_table= orig_next_table;
5521
5667
    if (res)
5522
5668
      return true;
5589
5735
}
5590
5736
 
5591
5737
 
5592
 
void resolve_const_item(Session *session, Item **ref, Item *comp_item)
 
5738
void resolve_const_item(THD *thd, Item **ref, Item *comp_item)
5593
5739
{
5594
5740
  Item *item= *ref;
5595
5741
  Item *new_item= NULL;
5609
5755
      new_item= new Item_null(name);
5610
5756
    else
5611
5757
    {
5612
 
      uint32_t length= result->length();
 
5758
      uint length= result->length();
5613
5759
      char *tmp_str= sql_strmake(result->ptr(), length);
5614
5760
      new_item= new Item_string(name, tmp_str, length, result->charset());
5615
5761
    }
5618
5764
  case INT_RESULT:
5619
5765
  {
5620
5766
    int64_t result=item->val_int();
5621
 
    uint32_t length=item->max_length;
 
5767
    uint length=item->max_length;
5622
5768
    bool null_value=item->null_value;
5623
5769
    new_item= (null_value ? (Item*) new Item_null(name) :
5624
5770
               (Item*) new Item_int(name, result, length));
5637
5783
    */
5638
5784
    Item_row *item_row= (Item_row*) item;
5639
5785
    Item_row *comp_item_row= (Item_row*) comp_item;
5640
 
    uint32_t col;
 
5786
    uint col;
5641
5787
    new_item= 0;
5642
5788
    /*
5643
5789
      If item and comp_item are both Item_rows and have same number of cols
5649
5795
    assert(item_row->cols() == comp_item_row->cols());
5650
5796
    col= item_row->cols();
5651
5797
    while (col-- > 0)
5652
 
      resolve_const_item(session, item_row->addr(col),
 
5798
      resolve_const_item(thd, item_row->addr(col),
5653
5799
                         comp_item_row->element_index(col));
5654
5800
    break;
5655
5801
  }
5657
5803
  case REAL_RESULT:
5658
5804
  {                                             // It must REAL_RESULT
5659
5805
    double result= item->val_real();
5660
 
    uint32_t length=item->max_length,decimals=item->decimals;
 
5806
    uint length=item->max_length,decimals=item->decimals;
5661
5807
    bool null_value=item->null_value;
5662
5808
    new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
5663
5809
               new Item_float(name, result, decimals, length));
5667
5813
  {
5668
5814
    my_decimal decimal_value;
5669
5815
    my_decimal *result= item->val_decimal(&decimal_value);
5670
 
    uint32_t length= item->max_length, decimals= item->decimals;
 
5816
    uint length= item->max_length, decimals= item->decimals;
5671
5817
    bool null_value= item->null_value;
5672
5818
    new_item= (null_value ?
5673
5819
               (Item*) new Item_null(name) :
5678
5824
    assert(0);
5679
5825
  }
5680
5826
  if (new_item)
5681
 
    session->change_item_tree(ref, new_item);
 
5827
    thd->change_item_tree(ref, new_item);
5682
5828
}
5683
5829
 
5684
5830
/**
5756
5902
}
5757
5903
 
5758
5904
 
5759
 
bool Item_cache::eq_def(Field *field)
5760
 
{
5761
 
  return cached_field ? cached_field->eq_def (field) : false;
5762
 
}
5763
 
 
5764
 
 
5765
5905
void Item_cache_int::store(Item *item)
5766
5906
{
5767
5907
  value= item->val_int_result();
5856
5996
  return str;
5857
5997
}
5858
5998
 
5859
 
my_decimal *Item_cache_decimal::val_decimal(my_decimal *)
 
5999
my_decimal *Item_cache_decimal::val_decimal(my_decimal *val __attribute__((unused)))
5860
6000
{
5861
6001
  assert(fixed);
5862
6002
  return &decimal_value;
5863
6003
}
5864
6004
 
5865
6005
 
5866
 
Item_cache_str::Item_cache_str(const Item *item) :
5867
 
  Item_cache(), value(0),
5868
 
  is_varbinary(item->type() == FIELD_ITEM &&
5869
 
               ((const Item_field *) item)->field->type() ==
5870
 
               DRIZZLE_TYPE_VARCHAR &&
5871
 
               !((const Item_field *) item)->field->has_charset())
5872
 
{}
5873
 
 
5874
6006
void Item_cache_str::store(Item *item)
5875
6007
{
5876
6008
  value_buff.set(buffer, sizeof(buffer), item->collation.collation);
5934
6066
}
5935
6067
 
5936
6068
 
 
6069
bool Item_cache_row::allocate(uint num)
 
6070
{
 
6071
  item_count= num;
 
6072
  THD *thd= current_thd;
 
6073
  return (!(values= 
 
6074
            (Item_cache **) thd->calloc(sizeof(Item_cache *)*item_count)));
 
6075
}
 
6076
 
 
6077
 
 
6078
bool Item_cache_row::setup(Item * item)
 
6079
{
 
6080
  example= item;
 
6081
  if (!values && allocate(item->cols()))
 
6082
    return 1;
 
6083
  for (uint i= 0; i < item_count; i++)
 
6084
  {
 
6085
    Item *el= item->element_index(i);
 
6086
    Item_cache *tmp;
 
6087
    if (!(tmp= values[i]= Item_cache::get_cache(el)))
 
6088
      return 1;
 
6089
    tmp->setup(el);
 
6090
  }
 
6091
  return 0;
 
6092
}
 
6093
 
 
6094
 
 
6095
void Item_cache_row::store(Item * item)
 
6096
{
 
6097
  null_value= 0;
 
6098
  item->bring_value();
 
6099
  for (uint i= 0; i < item_count; i++)
 
6100
  {
 
6101
    values[i]->store(item->element_index(i));
 
6102
    null_value|= values[i]->null_value;
 
6103
  }
 
6104
}
 
6105
 
 
6106
 
 
6107
void Item_cache_row::illegal_method_call(const char *method __attribute__((unused)))
 
6108
{
 
6109
  assert(0);
 
6110
  my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
 
6111
  return;
 
6112
}
 
6113
 
 
6114
 
 
6115
bool Item_cache_row::check_cols(uint c)
 
6116
{
 
6117
  if (c != item_count)
 
6118
  {
 
6119
    my_error(ER_OPERAND_COLUMNS, MYF(0), c);
 
6120
    return 1;
 
6121
  }
 
6122
  return 0;
 
6123
}
 
6124
 
 
6125
 
 
6126
bool Item_cache_row::null_inside()
 
6127
{
 
6128
  for (uint i= 0; i < item_count; i++)
 
6129
  {
 
6130
    if (values[i]->cols() > 1)
 
6131
    {
 
6132
      if (values[i]->null_inside())
 
6133
        return 1;
 
6134
    }
 
6135
    else
 
6136
    {
 
6137
      values[i]->update_null_value();
 
6138
      if (values[i]->null_value)
 
6139
        return 1;
 
6140
    }
 
6141
  }
 
6142
  return 0;
 
6143
}
 
6144
 
 
6145
 
 
6146
void Item_cache_row::bring_value()
 
6147
{
 
6148
  for (uint i= 0; i < item_count; i++)
 
6149
    values[i]->bring_value();
 
6150
  return;
 
6151
}
 
6152
 
 
6153
 
 
6154
Item_type_holder::Item_type_holder(THD *thd, Item *item)
 
6155
  :Item(thd, item), enum_set_typelib(0), fld_type(get_real_type(item))
 
6156
{
 
6157
  assert(item->fixed);
 
6158
  maybe_null= item->maybe_null;
 
6159
  collation.set(item->collation);
 
6160
  get_full_info(item);
 
6161
  /* fix variable decimals which always is NOT_FIXED_DEC */
 
6162
  if (Field::result_merge_type(fld_type) == INT_RESULT)
 
6163
    decimals= 0;
 
6164
  prev_decimal_int_part= item->decimal_int_part();
 
6165
}
 
6166
 
 
6167
 
 
6168
/**
 
6169
  Return expression type of Item_type_holder.
 
6170
 
 
6171
  @return
 
6172
    Item_result (type of internal MySQL expression result)
 
6173
*/
 
6174
 
 
6175
Item_result Item_type_holder::result_type() const
 
6176
{
 
6177
  return Field::result_merge_type(fld_type);
 
6178
}
 
6179
 
 
6180
 
 
6181
/**
 
6182
  Find real field type of item.
 
6183
 
 
6184
  @return
 
6185
    type of field which should be created to store item value
 
6186
*/
 
6187
 
 
6188
enum_field_types Item_type_holder::get_real_type(Item *item)
 
6189
{
 
6190
  switch(item->type())
 
6191
  {
 
6192
  case FIELD_ITEM:
 
6193
  {
 
6194
    /*
 
6195
      Item_fields::field_type ask Field_type() but sometimes field return
 
6196
      a different type, like for enum/set, so we need to ask real type.
 
6197
    */
 
6198
    Field *field= ((Item_field *) item)->field;
 
6199
    enum_field_types type= field->real_type();
 
6200
    if (field->is_created_from_null_item)
 
6201
      return DRIZZLE_TYPE_NULL;
 
6202
    return type;
 
6203
  }
 
6204
  case SUM_FUNC_ITEM:
 
6205
  {
 
6206
    /*
 
6207
      Argument of aggregate function sometimes should be asked about field
 
6208
      type
 
6209
    */
 
6210
    Item_sum *item_sum= (Item_sum *) item;
 
6211
    if (item_sum->keep_field_type())
 
6212
      return get_real_type(item_sum->args[0]);
 
6213
    break;
 
6214
  }
 
6215
  case FUNC_ITEM:
 
6216
    if (((Item_func *) item)->functype() == Item_func::GUSERVAR_FUNC)
 
6217
    {
 
6218
      /*
 
6219
        There are work around of problem with changing variable type on the
 
6220
        fly and variable always report "string" as field type to get
 
6221
        acceptable information for client in send_field, so we make field
 
6222
        type from expression type.
 
6223
      */
 
6224
      switch (item->result_type()) {
 
6225
      case STRING_RESULT:
 
6226
        return DRIZZLE_TYPE_VARCHAR;
 
6227
      case INT_RESULT:
 
6228
        return DRIZZLE_TYPE_LONGLONG;
 
6229
      case REAL_RESULT:
 
6230
        return DRIZZLE_TYPE_DOUBLE;
 
6231
      case DECIMAL_RESULT:
 
6232
        return DRIZZLE_TYPE_NEWDECIMAL;
 
6233
      case ROW_RESULT:
 
6234
      default:
 
6235
        assert(0);
 
6236
        return DRIZZLE_TYPE_VARCHAR;
 
6237
      }
 
6238
    }
 
6239
    break;
 
6240
  default:
 
6241
    break;
 
6242
  }
 
6243
  return item->field_type();
 
6244
}
 
6245
 
 
6246
/**
 
6247
  Find field type which can carry current Item_type_holder type and
 
6248
  type of given Item.
 
6249
 
 
6250
  @param thd     thread handler
 
6251
  @param item    given item to join its parameters with this item ones
 
6252
 
 
6253
  @retval
 
6254
    true   error - types are incompatible
 
6255
  @retval
 
6256
    false  OK
 
6257
*/
 
6258
 
 
6259
bool Item_type_holder::join_types(THD *thd __attribute__((unused)),
 
6260
                                  Item *item)
 
6261
{
 
6262
  uint max_length_orig= max_length;
 
6263
  uint decimals_orig= decimals;
 
6264
  fld_type= Field::field_type_merge(fld_type, get_real_type(item));
 
6265
  {
 
6266
    int item_decimals= item->decimals;
 
6267
    /* fix variable decimals which always is NOT_FIXED_DEC */
 
6268
    if (Field::result_merge_type(fld_type) == INT_RESULT)
 
6269
      item_decimals= 0;
 
6270
    decimals= max(decimals, item_decimals);
 
6271
  }
 
6272
  if (Field::result_merge_type(fld_type) == DECIMAL_RESULT)
 
6273
  {
 
6274
    decimals= min(max(decimals, item->decimals), DECIMAL_MAX_SCALE);
 
6275
    int precision= min(max(prev_decimal_int_part, item->decimal_int_part())
 
6276
                       + decimals, DECIMAL_MAX_PRECISION);
 
6277
    unsigned_flag&= item->unsigned_flag;
 
6278
    max_length= my_decimal_precision_to_length(precision, decimals,
 
6279
                                               unsigned_flag);
 
6280
  }
 
6281
 
 
6282
  switch (Field::result_merge_type(fld_type))
 
6283
  {
 
6284
  case STRING_RESULT:
 
6285
  {
 
6286
    const char *old_cs, *old_derivation;
 
6287
    uint32_t old_max_chars= max_length / collation.collation->mbmaxlen;
 
6288
    old_cs= collation.collation->name;
 
6289
    old_derivation= collation.derivation_name();
 
6290
    if (collation.aggregate(item->collation, MY_COLL_ALLOW_CONV))
 
6291
    {
 
6292
      my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0),
 
6293
               old_cs, old_derivation,
 
6294
               item->collation.collation->name,
 
6295
               item->collation.derivation_name(),
 
6296
               "UNION");
 
6297
      return(true);
 
6298
    }
 
6299
    /*
 
6300
      To figure out max_length, we have to take into account possible
 
6301
      expansion of the size of the values because of character set
 
6302
      conversions.
 
6303
     */
 
6304
    if (collation.collation != &my_charset_bin)
 
6305
    {
 
6306
      max_length= max(old_max_chars * collation.collation->mbmaxlen,
 
6307
                      display_length(item) /
 
6308
                      item->collation.collation->mbmaxlen *
 
6309
                      collation.collation->mbmaxlen);
 
6310
    }
 
6311
    else
 
6312
      set_if_bigger(max_length, display_length(item));
 
6313
    break;
 
6314
  }
 
6315
  case REAL_RESULT:
 
6316
  {
 
6317
    if (decimals != NOT_FIXED_DEC)
 
6318
    {
 
6319
      int delta1= max_length_orig - decimals_orig;
 
6320
      int delta2= item->max_length - item->decimals;
 
6321
      max_length= max(delta1, delta2) + decimals;
 
6322
      if (fld_type == DRIZZLE_TYPE_DOUBLE && max_length > DBL_DIG + 2) 
 
6323
      {
 
6324
        max_length= DBL_DIG + 7;
 
6325
        decimals= NOT_FIXED_DEC;
 
6326
      }
 
6327
    }
 
6328
    else
 
6329
      max_length= DBL_DIG+7;
 
6330
    break;
 
6331
  }
 
6332
  default:
 
6333
    max_length= max(max_length, display_length(item));
 
6334
  };
 
6335
  maybe_null|= item->maybe_null;
 
6336
  get_full_info(item);
 
6337
 
 
6338
  /* Remember decimal integer part to be used in DECIMAL_RESULT handleng */
 
6339
  prev_decimal_int_part= decimal_int_part();
 
6340
  return(false);
 
6341
}
 
6342
 
 
6343
/**
 
6344
  Calculate lenth for merging result for given Item type.
 
6345
 
 
6346
  @param item  Item for length detection
 
6347
 
 
6348
  @return
 
6349
    length
 
6350
*/
 
6351
 
 
6352
uint32_t Item_type_holder::display_length(Item *item)
 
6353
{
 
6354
  if (item->type() == Item::FIELD_ITEM)
 
6355
    return ((Item_field *)item)->max_disp_length();
 
6356
 
 
6357
  switch (item->field_type())
 
6358
  {
 
6359
  case DRIZZLE_TYPE_TIMESTAMP:
 
6360
  case DRIZZLE_TYPE_TIME:
 
6361
  case DRIZZLE_TYPE_DATETIME:
 
6362
  case DRIZZLE_TYPE_NEWDATE:
 
6363
  case DRIZZLE_TYPE_VARCHAR:
 
6364
  case DRIZZLE_TYPE_NEWDECIMAL:
 
6365
  case DRIZZLE_TYPE_ENUM:
 
6366
  case DRIZZLE_TYPE_SET:
 
6367
  case DRIZZLE_TYPE_BLOB:
 
6368
  case DRIZZLE_TYPE_TINY:
 
6369
    return 4;
 
6370
  case DRIZZLE_TYPE_SHORT:
 
6371
    return 6;
 
6372
  case DRIZZLE_TYPE_LONG:
 
6373
    return MY_INT32_NUM_DECIMAL_DIGITS;
 
6374
  case DRIZZLE_TYPE_DOUBLE:
 
6375
    return 53;
 
6376
  case DRIZZLE_TYPE_NULL:
 
6377
    return 0;
 
6378
  case DRIZZLE_TYPE_LONGLONG:
 
6379
    return 20;
 
6380
  default:
 
6381
    assert(0); // we should never go there
 
6382
    return 0;
 
6383
  }
 
6384
}
 
6385
 
 
6386
 
 
6387
/**
 
6388
  Make temporary table field according collected information about type
 
6389
  of UNION result.
 
6390
 
 
6391
  @param table  temporary table for which we create fields
 
6392
 
 
6393
  @return
 
6394
    created field
 
6395
*/
 
6396
 
 
6397
Field *Item_type_holder::make_field_by_type(TABLE *table)
 
6398
{
 
6399
  /*
 
6400
    The field functions defines a field to be not null if null_ptr is not 0
 
6401
  */
 
6402
  uchar *null_ptr= maybe_null ? (uchar*) "" : 0;
 
6403
  Field *field;
 
6404
 
 
6405
  switch (fld_type) {
 
6406
  case DRIZZLE_TYPE_ENUM:
 
6407
    assert(enum_set_typelib);
 
6408
    field= new Field_enum((uchar *) 0, max_length, null_ptr, 0,
 
6409
                          Field::NONE, name,
 
6410
                          get_enum_pack_length(enum_set_typelib->count),
 
6411
                          enum_set_typelib, collation.collation);
 
6412
    if (field)
 
6413
      field->init(table);
 
6414
    return field;
 
6415
  case DRIZZLE_TYPE_SET:
 
6416
    assert(enum_set_typelib);
 
6417
    field= new Field_set((uchar *) 0, max_length, null_ptr, 0,
 
6418
                         Field::NONE, name,
 
6419
                         get_set_pack_length(enum_set_typelib->count),
 
6420
                         enum_set_typelib, collation.collation);
 
6421
    if (field)
 
6422
      field->init(table);
 
6423
    return field;
 
6424
  case DRIZZLE_TYPE_NULL:
 
6425
    return make_string_field(table);
 
6426
  default:
 
6427
    break;
 
6428
  }
 
6429
  return tmp_table_field_from_field_type(table, 0);
 
6430
}
 
6431
 
 
6432
 
 
6433
/**
 
6434
  Get full information from Item about enum/set fields to be able to create
 
6435
  them later.
 
6436
 
 
6437
  @param item    Item for information collection
 
6438
*/
 
6439
void Item_type_holder::get_full_info(Item *item)
 
6440
{
 
6441
  if (fld_type == DRIZZLE_TYPE_ENUM ||
 
6442
      fld_type == DRIZZLE_TYPE_SET)
 
6443
  {
 
6444
    if (item->type() == Item::SUM_FUNC_ITEM &&
 
6445
        (((Item_sum*)item)->sum_func() == Item_sum::MAX_FUNC ||
 
6446
         ((Item_sum*)item)->sum_func() == Item_sum::MIN_FUNC))
 
6447
      item = ((Item_sum*)item)->args[0];
 
6448
    /*
 
6449
      We can have enum/set type after merging only if we have one enum|set
 
6450
      field (or MIN|MAX(enum|set field)) and number of NULL fields
 
6451
    */
 
6452
    assert((enum_set_typelib &&
 
6453
                 get_real_type(item) == DRIZZLE_TYPE_NULL) ||
 
6454
                (!enum_set_typelib &&
 
6455
                 item->type() == Item::FIELD_ITEM &&
 
6456
                 (get_real_type(item) == DRIZZLE_TYPE_ENUM ||
 
6457
                  get_real_type(item) == DRIZZLE_TYPE_SET) &&
 
6458
                 ((Field_enum*)((Item_field *) item)->field)->typelib));
 
6459
    if (!enum_set_typelib)
 
6460
    {
 
6461
      enum_set_typelib= ((Field_enum*)((Item_field *) item)->field)->typelib;
 
6462
    }
 
6463
  }
 
6464
}
 
6465
 
 
6466
 
 
6467
double Item_type_holder::val_real()
 
6468
{
 
6469
  assert(0); // should never be called
 
6470
  return 0.0;
 
6471
}
 
6472
 
 
6473
 
 
6474
int64_t Item_type_holder::val_int()
 
6475
{
 
6476
  assert(0); // should never be called
 
6477
  return 0;
 
6478
}
 
6479
 
 
6480
my_decimal *Item_type_holder::val_decimal(my_decimal *)
 
6481
{
 
6482
  assert(0); // should never be called
 
6483
  return 0;
 
6484
}
 
6485
 
 
6486
String *Item_type_holder::val_str(String*)
 
6487
{
 
6488
  assert(0); // should never be called
 
6489
  return 0;
 
6490
}
 
6491
 
 
6492
void Item_result_field::cleanup()
 
6493
{
 
6494
  Item::cleanup();
 
6495
  result_field= 0;
 
6496
  return;
 
6497
}
 
6498
 
5937
6499
/**
5938
6500
  Dummy error processor used by default by Name_resolution_context.
5939
6501
 
5941
6503
    do nothing
5942
6504
*/
5943
6505
 
5944
 
void dummy_error_processor(Session *, void *)
 
6506
void dummy_error_processor(THD *thd __attribute__((unused)),
 
6507
                           void *data __attribute__((unused)))
5945
6508
{}
5946
6509
 
5947
6510
/**
5948
 
  Create field for temporary table using type of given item.
5949
 
 
5950
 
  @param session                   Thread handler
5951
 
  @param item                  Item to create a field for
5952
 
  @param table                 Temporary table
5953
 
  @param copy_func             If set and item is a function, store copy of
5954
 
                               item in this array
5955
 
  @param modify_item           1 if item->result_field should point to new
5956
 
                               item. This is relevent for how fill_record()
5957
 
                               is going to work:
5958
 
                               If modify_item is 1 then fill_record() will
5959
 
                               update the record in the original table.
5960
 
                               If modify_item is 0 then fill_record() will
5961
 
                               update the temporary table
5962
 
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
5963
 
                               field instead of blob.
5964
 
 
5965
 
  @retval
5966
 
    0  on error
5967
 
  @retval
5968
 
    new_created field
5969
 
*/
5970
 
 
5971
 
static Field *create_tmp_field_from_item(Session *,
5972
 
                                         Item *item, Table *table,
5973
 
                                         Item ***copy_func, bool modify_item,
5974
 
                                         uint32_t convert_blob_length)
5975
 
{
5976
 
  bool maybe_null= item->maybe_null;
5977
 
  Field *new_field;
5978
 
 
5979
 
  switch (item->result_type()) {
5980
 
  case REAL_RESULT:
5981
 
    new_field= new Field_double(item->max_length, maybe_null,
5982
 
                                item->name, item->decimals, true);
5983
 
    break;
5984
 
  case INT_RESULT:
5985
 
    /* 
5986
 
      Select an integer type with the minimal fit precision.
5987
 
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
5988
 
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into 
5989
 
      Field_long : make them Field_int64_t.  
5990
 
    */
5991
 
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
5992
 
      new_field=new Field_int64_t(item->max_length, maybe_null,
5993
 
                                   item->name, item->unsigned_flag);
5994
 
    else
5995
 
      new_field=new Field_long(item->max_length, maybe_null,
5996
 
                               item->name, item->unsigned_flag);
5997
 
    break;
5998
 
  case STRING_RESULT:
5999
 
    assert(item->collation.collation);
6000
 
  
6001
 
    enum enum_field_types type;
6002
 
    /*
6003
 
      DATE/TIME fields have STRING_RESULT result type. 
6004
 
      To preserve type they needed to be handled separately.
6005
 
    */
6006
 
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
6007
 
        type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_DATE ||
6008
 
        type == DRIZZLE_TYPE_TIMESTAMP)
6009
 
      new_field= item->tmp_table_field_from_field_type(table, 1);
6010
 
    /* 
6011
 
      Make sure that the blob fits into a Field_varstring which has 
6012
 
      2-byte lenght. 
6013
 
    */
6014
 
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
6015
 
             convert_blob_length <= Field_varstring::MAX_SIZE && 
6016
 
             convert_blob_length)
6017
 
      new_field= new Field_varstring(convert_blob_length, maybe_null,
6018
 
                                     item->name, table->s,
6019
 
                                     item->collation.collation);
6020
 
    else
6021
 
      new_field= item->make_string_field(table);
6022
 
    new_field->set_derivation(item->collation.derivation);
6023
 
    break;
6024
 
  case DECIMAL_RESULT:
6025
 
  {
6026
 
    uint8_t dec= item->decimals;
6027
 
    uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
6028
 
    uint32_t len= item->max_length;
6029
 
 
6030
 
    /*
6031
 
      Trying to put too many digits overall in a DECIMAL(prec,dec)
6032
 
      will always throw a warning. We must limit dec to
6033
 
      DECIMAL_MAX_SCALE however to prevent an assert() later.
6034
 
    */
6035
 
 
6036
 
    if (dec > 0)
6037
 
    {
6038
 
      signed int overflow;
6039
 
 
6040
 
      dec= cmin(dec, (uint8_t)DECIMAL_MAX_SCALE);
6041
 
 
6042
 
      /*
6043
 
        If the value still overflows the field with the corrected dec,
6044
 
        we'll throw out decimals rather than integers. This is still
6045
 
        bad and of course throws a truncation warning.
6046
 
        +1: for decimal point
6047
 
      */
6048
 
 
6049
 
      overflow= my_decimal_precision_to_length(intg + dec, dec,
6050
 
                                               item->unsigned_flag) - len;
6051
 
 
6052
 
      if (overflow > 0)
6053
 
        dec= cmax(0, dec - overflow);            // too long, discard fract
6054
 
      else
6055
 
        len -= item->decimals - dec;            // corrected value fits
6056
 
    }
6057
 
 
6058
 
    new_field= new Field_new_decimal(len, maybe_null, item->name,
6059
 
                                     dec, item->unsigned_flag);
6060
 
    break;
6061
 
  }
6062
 
  case ROW_RESULT:
6063
 
  default:
6064
 
    // This case should never be choosen
6065
 
    assert(0);
6066
 
    new_field= 0;
6067
 
    break;
6068
 
  }
6069
 
  if (new_field)
6070
 
    new_field->init(table);
6071
 
    
6072
 
  if (copy_func && item->is_result_field())
6073
 
    *((*copy_func)++) = item;                   // Save for copy_funcs
6074
 
  if (modify_item)
6075
 
    item->set_result_field(new_field);
6076
 
  if (item->type() == Item::NULL_ITEM)
6077
 
    new_field->is_created_from_null_item= true;
6078
 
  return new_field;
6079
 
}
6080
 
 
6081
 
Field *create_tmp_field(Session *session, Table *table,Item *item,
6082
 
                        Item::Type type, Item ***copy_func, Field **from_field,
6083
 
                        Field **default_field, bool group, bool modify_item,
6084
 
                        bool, bool make_copy_field,
6085
 
                        uint32_t convert_blob_length)
6086
 
{
6087
 
  Field *result;
6088
 
  Item::Type orig_type= type;
6089
 
  Item *orig_item= 0;
6090
 
 
6091
 
  if (type != Item::FIELD_ITEM &&
6092
 
      item->real_item()->type() == Item::FIELD_ITEM)
6093
 
  {
6094
 
    orig_item= item;
6095
 
    item= item->real_item();
6096
 
    type= Item::FIELD_ITEM;
6097
 
  }
6098
 
 
6099
 
  switch (type) {
6100
 
  case Item::SUM_FUNC_ITEM:
6101
 
  {
6102
 
    Item_sum *item_sum=(Item_sum*) item;
6103
 
    result= item_sum->create_tmp_field(group, table, convert_blob_length);
6104
 
    if (!result)
6105
 
      my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
6106
 
    return result;
6107
 
  }
6108
 
  case Item::FIELD_ITEM:
6109
 
  case Item::DEFAULT_VALUE_ITEM:
6110
 
  {
6111
 
    Item_field *field= (Item_field*) item;
6112
 
    bool orig_modify= modify_item;
6113
 
    if (orig_type == Item::REF_ITEM)
6114
 
      modify_item= 0;
6115
 
    /*
6116
 
      If item have to be able to store NULLs but underlaid field can't do it,
6117
 
      create_tmp_field_from_field() can't be used for tmp field creation.
6118
 
    */
6119
 
    if (field->maybe_null && !field->field->maybe_null())
6120
 
    {
6121
 
      result= create_tmp_field_from_item(session, item, table, NULL,
6122
 
                                         modify_item, convert_blob_length);
6123
 
      *from_field= field->field;
6124
 
      if (result && modify_item)
6125
 
        field->result_field= result;
6126
 
    } 
6127
 
    else
6128
 
      result= create_tmp_field_from_field(session, (*from_field= field->field),
6129
 
                                          orig_item ? orig_item->name :
6130
 
                                          item->name,
6131
 
                                          table,
6132
 
                                          modify_item ? field :
6133
 
                                          NULL,
6134
 
                                          convert_blob_length);
6135
 
    if (orig_type == Item::REF_ITEM && orig_modify)
6136
 
      ((Item_ref*)orig_item)->set_result_field(result);
6137
 
    if (field->field->eq_def(result))
6138
 
      *default_field= field->field;
6139
 
    return result;
6140
 
  }
6141
 
  /* Fall through */
6142
 
  case Item::FUNC_ITEM:
6143
 
    /* Fall through */
6144
 
  case Item::COND_ITEM:
6145
 
  case Item::FIELD_AVG_ITEM:
6146
 
  case Item::FIELD_STD_ITEM:
6147
 
  case Item::SUBSELECT_ITEM:
6148
 
    /* The following can only happen with 'CREATE TABLE ... SELECT' */
6149
 
  case Item::PROC_ITEM:
6150
 
  case Item::INT_ITEM:
6151
 
  case Item::REAL_ITEM:
6152
 
  case Item::DECIMAL_ITEM:
6153
 
  case Item::STRING_ITEM:
6154
 
  case Item::REF_ITEM:
6155
 
  case Item::NULL_ITEM:
6156
 
  case Item::VARBIN_ITEM:
6157
 
    if (make_copy_field)
6158
 
    {
6159
 
      assert(((Item_result_field*)item)->result_field);
6160
 
      *from_field= ((Item_result_field*)item)->result_field;
6161
 
    }
6162
 
    return create_tmp_field_from_item(session, item, table,
6163
 
                                      (make_copy_field ? 0 : copy_func),
6164
 
                                       modify_item, convert_blob_length);
6165
 
  case Item::TYPE_HOLDER:  
6166
 
    result= ((Item_type_holder *)item)->make_field_by_type(table);
6167
 
    result->set_derivation(item->collation.derivation);
6168
 
    return result;
6169
 
  default:                                      // Dosen't have to be stored
6170
 
    return 0;
6171
 
  }
6172
 
}
6173
 
 
6174
 
 
6175
 
/**
6176
6511
  Wrapper of hide_view_error call for Name_resolution_context error
6177
6512
  processor.
6178
6513