1420
1428
switch (res_type) {
1421
1429
case STRING_RESULT:
1423
char buff[MAX_FIELD_WIDTH];
1424
String tmp(buff,sizeof(buff),&my_charset_bin),*result;
1425
result=item->val_str(&tmp);
1426
if (item->null_value)
1427
new_item= new Item_null(name);
1430
uint32_t length= result->length();
1431
char *tmp_str= memory::sql_strmake(result->ptr(), length);
1432
new_item= new Item_string(name, tmp_str, length, result->charset());
1431
char buff[MAX_FIELD_WIDTH];
1432
String tmp(buff,sizeof(buff),&my_charset_bin),*result;
1433
result=item->val_str(&tmp);
1434
if (item->null_value)
1435
new_item= new Item_null(name);
1438
uint32_t length= result->length();
1439
char *tmp_str= memory::sql_strmake(result->ptr(), length);
1440
new_item= new Item_string(name, tmp_str, length, result->charset());
1436
1444
case INT_RESULT:
1438
int64_t result=item->val_int();
1439
uint32_t length=item->max_length;
1440
bool null_value=item->null_value;
1441
new_item= (null_value ? (Item*) new Item_null(name) :
1442
(Item*) new Item_int(name, result, length));
1446
int64_t result=item->val_int();
1447
uint32_t length=item->max_length;
1448
bool null_value=item->null_value;
1449
new_item= (null_value ? (Item*) new Item_null(name) :
1450
(Item*) new Item_int(name, result, length));
1445
1453
case ROW_RESULT:
1446
if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
1449
Substitute constants only in Item_rows. Don't affect other Items
1450
with ROW_RESULT (eg Item_singlerow_subselect).
1454
if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
1457
Substitute constants only in Item_rows. Don't affect other Items
1458
with ROW_RESULT (eg Item_singlerow_subselect).
1452
For such Items more optimal is to detect if it is constant and replace
1453
it with Item_row. This would optimize queries like this:
1454
SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
1456
Item_row *item_row= (Item_row*) item;
1457
Item_row *comp_item_row= (Item_row*) comp_item;
1461
If item and comp_item are both Item_rows and have same number of cols
1462
then process items in Item_row one by one.
1463
We can't ignore NULL values here as this item may be used with <=>, in
1464
which case NULL's are significant.
1466
assert(item->result_type() == comp_item->result_type());
1467
assert(item_row->cols() == comp_item_row->cols());
1468
col= item_row->cols();
1470
resolve_const_item(session, item_row->addr(col),
1471
comp_item_row->element_index(col));
1460
For such Items more optimal is to detect if it is constant and replace
1461
it with Item_row. This would optimize queries like this:
1462
SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
1464
Item_row *item_row= (Item_row*) item;
1465
Item_row *comp_item_row= (Item_row*) comp_item;
1469
If item and comp_item are both Item_rows and have same number of cols
1470
then process items in Item_row one by one.
1471
We can't ignore NULL values here as this item may be used with <=>, in
1472
which case NULL's are significant.
1474
assert(item->result_type() == comp_item->result_type());
1475
assert(item_row->cols() == comp_item_row->cols());
1476
col= item_row->cols();
1478
resolve_const_item(session, item_row->addr(col),
1479
comp_item_row->element_index(col));
1475
1483
case REAL_RESULT:
1476
{ // It must REAL_RESULT
1477
double result= item->val_real();
1478
uint32_t length=item->max_length,decimals=item->decimals;
1479
bool null_value=item->null_value;
1480
new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
1481
new Item_float(name, result, decimals, length));
1484
{ // It must REAL_RESULT
1485
double result= item->val_real();
1486
uint32_t length=item->max_length,decimals=item->decimals;
1487
bool null_value=item->null_value;
1488
new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
1489
new Item_float(name, result, decimals, length));
1484
1492
case DECIMAL_RESULT:
1486
my_decimal decimal_value;
1487
my_decimal *result= item->val_decimal(&decimal_value);
1488
uint32_t length= item->max_length, decimals= item->decimals;
1489
bool null_value= item->null_value;
1490
new_item= (null_value ?
1491
(Item*) new Item_null(name) :
1492
(Item*) new Item_decimal(name, result, length, decimals));
1494
my_decimal decimal_value;
1495
my_decimal *result= item->val_decimal(&decimal_value);
1496
uint32_t length= item->max_length, decimals= item->decimals;
1497
bool null_value= item->null_value;
1498
new_item= (null_value ?
1499
(Item*) new Item_null(name) :
1500
(Item*) new Item_decimal(name, result, length, decimals));
1499
1506
session->change_item_tree(ref, new_item);
1620
1633
new_field->set_derivation(item->collation.derivation);
1622
1636
case DECIMAL_RESULT:
1624
uint8_t dec= item->decimals;
1625
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
1626
uint32_t len= item->max_length;
1629
Trying to put too many digits overall in a DECIMAL(prec,dec)
1630
will always throw a warning. We must limit dec to
1631
DECIMAL_MAX_SCALE however to prevent an assert() later.
1636
signed int overflow;
1638
dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
1638
uint8_t dec= item->decimals;
1639
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
1640
uint32_t len= item->max_length;
1641
If the value still overflows the field with the corrected dec,
1642
we'll throw out decimals rather than integers. This is still
1643
bad and of course throws a truncation warning.
1644
+1: for decimal point
1643
Trying to put too many digits overall in a DECIMAL(prec,dec)
1644
will always throw a warning. We must limit dec to
1645
DECIMAL_MAX_SCALE however to prevent an assert() later.
1647
overflow= my_decimal_precision_to_length(intg + dec, dec,
1648
item->unsigned_flag) - len;
1651
dec= max(0, dec - overflow); // too long, discard fract
1653
len-= item->decimals - dec; // corrected value fits
1650
signed int overflow;
1652
dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
1655
If the value still overflows the field with the corrected dec,
1656
we'll throw out decimals rather than integers. This is still
1657
bad and of course throws a truncation warning.
1658
+1: for decimal point
1661
overflow= my_decimal_precision_to_length(intg + dec, dec,
1662
item->unsigned_flag) - len;
1665
dec= max(0, dec - overflow); // too long, discard fract
1667
len-= item->decimals - dec; // corrected value fits
1670
new_field= new Field_decimal(len,
1674
item->unsigned_flag);
1656
new_field= new Field_decimal(len,
1660
item->unsigned_flag);
1663
1678
case ROW_RESULT:
1665
1679
// This case should never be choosen
1670
1685
new_field->init(table);
1672
1687
if (copy_func && item->is_result_field())
1673
1688
*((*copy_func)++) = item; // Save for copy_funcs
1674
1690
if (modify_item)
1675
1691
item->set_result_field(new_field);
1676
1693
if (item->type() == Item::NULL_ITEM)
1677
1694
new_field->is_created_from_null_item= true;
1678
1696
return new_field;