6469
Create field for temporary table using type of given item.
6471
@param session Thread handler
6472
@param item Item to create a field for
6473
@param table Temporary table
6474
@param copy_func If set and item is a function, store copy of
6476
@param modify_item 1 if item->result_field should point to new
6477
item. This is relevent for how fill_record()
6479
If modify_item is 1 then fill_record() will
6480
update the record in the original table.
6481
If modify_item is 0 then fill_record() will
6482
update the temporary table
6483
@param convert_blob_length If >0 create a varstring(convert_blob_length)
6484
field instead of blob.
6492
static Field *create_tmp_field_from_item(Session *,
6493
Item *item, Table *table,
6494
Item ***copy_func, bool modify_item,
6495
uint32_t convert_blob_length)
6497
bool maybe_null= item->maybe_null;
6500
switch (item->result_type()) {
6502
new_field= new Field_double(item->max_length, maybe_null,
6503
item->name, item->decimals, true);
6507
Select an integer type with the minimal fit precision.
6508
MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
6509
Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
6510
Field_long : make them Field_int64_t.
6512
if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
6513
new_field=new Field_int64_t(item->max_length, maybe_null,
6514
item->name, item->unsigned_flag);
6516
new_field=new Field_long(item->max_length, maybe_null,
6517
item->name, item->unsigned_flag);
6520
assert(item->collation.collation);
6522
enum enum_field_types type;
6524
DATE/TIME fields have STRING_RESULT result type.
6525
To preserve type they needed to be handled separately.
6527
if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
6528
type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_NEWDATE ||
6529
type == DRIZZLE_TYPE_TIMESTAMP)
6530
new_field= item->tmp_table_field_from_field_type(table, 1);
6532
Make sure that the blob fits into a Field_varstring which has
6535
else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
6536
convert_blob_length <= Field_varstring::MAX_SIZE &&
6537
convert_blob_length)
6538
new_field= new Field_varstring(convert_blob_length, maybe_null,
6539
item->name, table->s,
6540
item->collation.collation);
6542
new_field= item->make_string_field(table);
6543
new_field->set_derivation(item->collation.derivation);
6545
case DECIMAL_RESULT:
6547
uint8_t dec= item->decimals;
6548
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
6549
uint32_t len= item->max_length;
6552
Trying to put too many digits overall in a DECIMAL(prec,dec)
6553
will always throw a warning. We must limit dec to
6554
DECIMAL_MAX_SCALE however to prevent an assert() later.
6559
signed int overflow;
6561
dec= cmin(dec, (uint8_t)DECIMAL_MAX_SCALE);
6564
If the value still overflows the field with the corrected dec,
6565
we'll throw out decimals rather than integers. This is still
6566
bad and of course throws a truncation warning.
6567
+1: for decimal point
6570
overflow= my_decimal_precision_to_length(intg + dec, dec,
6571
item->unsigned_flag) - len;
6574
dec= cmax(0, dec - overflow); // too long, discard fract
6576
len -= item->decimals - dec; // corrected value fits
6579
new_field= new Field_new_decimal(len, maybe_null, item->name,
6580
dec, item->unsigned_flag);
6585
// This case should never be choosen
6591
new_field->init(table);
6593
if (copy_func && item->is_result_field())
6594
*((*copy_func)++) = item; // Save for copy_funcs
6596
item->set_result_field(new_field);
6597
if (item->type() == Item::NULL_ITEM)
6598
new_field->is_created_from_null_item= true;
6602
Field *create_tmp_field(Session *session, Table *table,Item *item,
6603
Item::Type type, Item ***copy_func, Field **from_field,
6604
Field **default_field, bool group, bool modify_item,
6605
bool, bool make_copy_field,
6606
uint32_t convert_blob_length)
6609
Item::Type orig_type= type;
6612
if (type != Item::FIELD_ITEM &&
6613
item->real_item()->type() == Item::FIELD_ITEM)
6616
item= item->real_item();
6617
type= Item::FIELD_ITEM;
6621
case Item::SUM_FUNC_ITEM:
6623
Item_sum *item_sum=(Item_sum*) item;
6624
result= item_sum->create_tmp_field(group, table, convert_blob_length);
6626
my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
6629
case Item::FIELD_ITEM:
6630
case Item::DEFAULT_VALUE_ITEM:
6632
Item_field *field= (Item_field*) item;
6633
bool orig_modify= modify_item;
6634
if (orig_type == Item::REF_ITEM)
6637
If item have to be able to store NULLs but underlaid field can't do it,
6638
create_tmp_field_from_field() can't be used for tmp field creation.
6640
if (field->maybe_null && !field->field->maybe_null())
6642
result= create_tmp_field_from_item(session, item, table, NULL,
6643
modify_item, convert_blob_length);
6644
*from_field= field->field;
6645
if (result && modify_item)
6646
field->result_field= result;
6649
result= create_tmp_field_from_field(session, (*from_field= field->field),
6650
orig_item ? orig_item->name :
6653
modify_item ? field :
6655
convert_blob_length);
6656
if (orig_type == Item::REF_ITEM && orig_modify)
6657
((Item_ref*)orig_item)->set_result_field(result);
6658
if (field->field->eq_def(result))
6659
*default_field= field->field;
6663
case Item::FUNC_ITEM:
6665
case Item::COND_ITEM:
6666
case Item::FIELD_AVG_ITEM:
6667
case Item::FIELD_STD_ITEM:
6668
case Item::SUBSELECT_ITEM:
6669
/* The following can only happen with 'CREATE TABLE ... SELECT' */
6670
case Item::PROC_ITEM:
6671
case Item::INT_ITEM:
6672
case Item::REAL_ITEM:
6673
case Item::DECIMAL_ITEM:
6674
case Item::STRING_ITEM:
6675
case Item::REF_ITEM:
6676
case Item::NULL_ITEM:
6677
case Item::VARBIN_ITEM:
6678
if (make_copy_field)
6680
assert(((Item_result_field*)item)->result_field);
6681
*from_field= ((Item_result_field*)item)->result_field;
6683
return create_tmp_field_from_item(session, item, table,
6684
(make_copy_field ? 0 : copy_func),
6685
modify_item, convert_blob_length);
6686
case Item::TYPE_HOLDER:
6687
result= ((Item_type_holder *)item)->make_field_by_type(table);
6688
result->set_derivation(item->collation.derivation);
6690
default: // Dosen't have to be stored
6467
6697
Wrapper of hide_view_error call for Name_resolution_context error