1
/* Copyright (C) 2000-2006 MySQL AB
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.
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.
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 */
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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.
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.
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
17
21
#include <drizzled/server_includes.h>
5584
bool Item_cache_row::allocate(uint32_t num)
5587
Session *session= current_session;
5589
(Item_cache **) session->calloc(sizeof(Item_cache *)*item_count)));
5593
bool Item_cache_row::setup(Item * item)
5596
if (!values && allocate(item->cols()))
5598
for (uint32_t i= 0; i < item_count; i++)
5600
Item *el= item->element_index(i);
5602
if (!(tmp= values[i]= Item_cache::get_cache(el)))
5610
void Item_cache_row::store(Item * item)
5613
item->bring_value();
5614
for (uint32_t i= 0; i < item_count; i++)
5616
values[i]->store(item->element_index(i));
5617
null_value|= values[i]->null_value;
5622
void Item_cache_row::illegal_method_call(const char *)
5625
my_error(ER_OPERAND_COLUMNS, MYF(0), 1);
5630
bool Item_cache_row::check_cols(uint32_t c)
5632
if (c != item_count)
5634
my_error(ER_OPERAND_COLUMNS, MYF(0), c);
5641
bool Item_cache_row::null_inside()
5643
for (uint32_t i= 0; i < item_count; i++)
5645
if (values[i]->cols() > 1)
5647
if (values[i]->null_inside())
5652
values[i]->update_null_value();
5653
if (values[i]->null_value)
5661
void Item_cache_row::bring_value()
5663
for (uint32_t i= 0; i < item_count; i++)
5664
values[i]->bring_value();
5669
Item_type_holder::Item_type_holder(Session *session, Item *item)
5670
:Item(session, item), enum_set_typelib(0), fld_type(get_real_type(item))
5672
assert(item->fixed);
5673
maybe_null= item->maybe_null;
5674
collation.set(item->collation);
5675
get_full_info(item);
5676
/* fix variable decimals which always is NOT_FIXED_DEC */
5677
if (Field::result_merge_type(fld_type) == INT_RESULT)
5679
prev_decimal_int_part= item->decimal_int_part();
5684
Return expression type of Item_type_holder.
5687
Item_result (type of internal MySQL expression result)
5690
Item_result Item_type_holder::result_type() const
5692
return Field::result_merge_type(fld_type);
5697
Find real field type of item.
5700
type of field which should be created to store item value
5703
enum_field_types Item_type_holder::get_real_type(Item *item)
5705
switch(item->type())
5710
Item_fields::field_type ask Field_type() but sometimes field return
5711
a different type, like for enum/set, so we need to ask real type.
5713
Field *field= ((Item_field *) item)->field;
5714
enum_field_types type= field->real_type();
5715
if (field->is_created_from_null_item)
5716
return DRIZZLE_TYPE_NULL;
5722
Argument of aggregate function sometimes should be asked about field
5725
Item_sum *item_sum= (Item_sum *) item;
5726
if (item_sum->keep_field_type())
5727
return get_real_type(item_sum->args[0]);
5731
if (((Item_func *) item)->functype() == Item_func::GUSERVAR_FUNC)
5734
There are work around of problem with changing variable type on the
5735
fly and variable always report "string" as field type to get
5736
acceptable information for client in send_field, so we make field
5737
type from expression type.
5739
switch (item->result_type()) {
5741
return DRIZZLE_TYPE_VARCHAR;
5743
return DRIZZLE_TYPE_LONGLONG;
5745
return DRIZZLE_TYPE_DOUBLE;
5746
case DECIMAL_RESULT:
5747
return DRIZZLE_TYPE_NEWDECIMAL;
5751
return DRIZZLE_TYPE_VARCHAR;
5758
return item->field_type();
5762
Find field type which can carry current Item_type_holder type and
5765
@param session thread handler
5766
@param item given item to join its parameters with this item ones
5769
true error - types are incompatible
5774
bool Item_type_holder::join_types(Session *, Item *item)
5776
uint32_t max_length_orig= max_length;
5777
uint32_t decimals_orig= decimals;
5778
fld_type= Field::field_type_merge(fld_type, get_real_type(item));
5780
int item_decimals= item->decimals;
5781
/* fix variable decimals which always is NOT_FIXED_DEC */
5782
if (Field::result_merge_type(fld_type) == INT_RESULT)
5784
decimals= cmax((int)decimals, item_decimals);
5786
if (Field::result_merge_type(fld_type) == DECIMAL_RESULT)
5788
decimals= cmin((int)cmax(decimals, item->decimals), DECIMAL_MAX_SCALE);
5789
int precision= cmin(cmax(prev_decimal_int_part, item->decimal_int_part())
5790
+ decimals, DECIMAL_MAX_PRECISION);
5791
unsigned_flag&= item->unsigned_flag;
5792
max_length= my_decimal_precision_to_length(precision, decimals,
5796
switch (Field::result_merge_type(fld_type))
5800
const char *old_cs, *old_derivation;
5801
uint32_t old_max_chars= max_length / collation.collation->mbmaxlen;
5802
old_cs= collation.collation->name;
5803
old_derivation= collation.derivation_name();
5804
if (collation.aggregate(item->collation, MY_COLL_ALLOW_CONV))
5806
my_error(ER_CANT_AGGREGATE_2COLLATIONS, MYF(0),
5807
old_cs, old_derivation,
5808
item->collation.collation->name,
5809
item->collation.derivation_name(),
5814
To figure out max_length, we have to take into account possible
5815
expansion of the size of the values because of character set
5818
if (collation.collation != &my_charset_bin)
5820
max_length= cmax(old_max_chars * collation.collation->mbmaxlen,
5821
display_length(item) /
5822
item->collation.collation->mbmaxlen *
5823
collation.collation->mbmaxlen);
5826
set_if_bigger(max_length, display_length(item));
5831
if (decimals != NOT_FIXED_DEC)
5833
int delta1= max_length_orig - decimals_orig;
5834
int delta2= item->max_length - item->decimals;
5835
max_length= cmax(delta1, delta2) + decimals;
5836
if (fld_type == DRIZZLE_TYPE_DOUBLE && max_length > DBL_DIG + 2)
5838
max_length= DBL_DIG + 7;
5839
decimals= NOT_FIXED_DEC;
5843
max_length= DBL_DIG+7;
5847
max_length= cmax(max_length, display_length(item));
5849
maybe_null|= item->maybe_null;
5850
get_full_info(item);
5852
/* Remember decimal integer part to be used in DECIMAL_RESULT handleng */
5853
prev_decimal_int_part= decimal_int_part();
5858
Calculate lenth for merging result for given Item type.
5860
@param item Item for length detection
5866
uint32_t Item_type_holder::display_length(Item *item)
5868
if (item->type() == Item::FIELD_ITEM)
5869
return ((Item_field *)item)->max_disp_length();
5871
switch (item->field_type())
5873
case DRIZZLE_TYPE_TIMESTAMP:
5874
case DRIZZLE_TYPE_TIME:
5875
case DRIZZLE_TYPE_DATETIME:
5876
case DRIZZLE_TYPE_NEWDATE:
5877
case DRIZZLE_TYPE_VARCHAR:
5878
case DRIZZLE_TYPE_NEWDECIMAL:
5879
case DRIZZLE_TYPE_ENUM:
5880
case DRIZZLE_TYPE_BLOB:
5882
case DRIZZLE_TYPE_LONG:
5883
return MY_INT32_NUM_DECIMAL_DIGITS;
5884
case DRIZZLE_TYPE_DOUBLE:
5886
case DRIZZLE_TYPE_NULL:
5888
case DRIZZLE_TYPE_LONGLONG:
5891
assert(0); // we should never go there
5898
Make temporary table field according collected information about type
5901
@param table temporary table for which we create fields
5907
Field *Item_type_holder::make_field_by_type(Table *table)
5910
The field functions defines a field to be not null if null_ptr is not 0
5912
unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
5916
case DRIZZLE_TYPE_ENUM:
5917
assert(enum_set_typelib);
5918
field= new Field_enum((unsigned char *) 0, max_length, null_ptr, 0,
5920
get_enum_pack_length(enum_set_typelib->count),
5921
enum_set_typelib, collation.collation);
5925
case DRIZZLE_TYPE_NULL:
5926
return make_string_field(table);
5930
return tmp_table_field_from_field_type(table, 0);
5935
Get full information from Item about enum/set fields to be able to create
5938
@param item Item for information collection
5940
void Item_type_holder::get_full_info(Item *item)
5942
if (fld_type == DRIZZLE_TYPE_ENUM)
5944
if (item->type() == Item::SUM_FUNC_ITEM &&
5945
(((Item_sum*)item)->sum_func() == Item_sum::MAX_FUNC ||
5946
((Item_sum*)item)->sum_func() == Item_sum::MIN_FUNC))
5947
item = ((Item_sum*)item)->args[0];
5949
We can have enum/set type after merging only if we have one enum|set
5950
field (or MIN|MAX(enum|set field)) and number of NULL fields
5952
assert((enum_set_typelib &&
5953
get_real_type(item) == DRIZZLE_TYPE_NULL) ||
5954
(!enum_set_typelib &&
5955
item->type() == Item::FIELD_ITEM &&
5956
(get_real_type(item) == DRIZZLE_TYPE_ENUM) &&
5957
((Field_enum*)((Item_field *) item)->field)->typelib));
5958
if (!enum_set_typelib)
5960
enum_set_typelib= ((Field_enum*)((Item_field *) item)->field)->typelib;
5966
double Item_type_holder::val_real()
5968
assert(0); // should never be called
5973
int64_t Item_type_holder::val_int()
5975
assert(0); // should never be called
5979
my_decimal *Item_type_holder::val_decimal(my_decimal *)
5981
assert(0); // should never be called
5985
String *Item_type_holder::val_str(String*)
5987
assert(0); // should never be called
5991
void Item_result_field::cleanup()
5999
5591
Dummy error processor used by default by Name_resolution_context.