1176
1156
unsigned_flag);
1178
1158
case DRIZZLE_TYPE_LONG:
1179
field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
1159
field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1160
name, 0, unsigned_flag);
1181
1162
case DRIZZLE_TYPE_LONGLONG:
1182
field= new field::Int64((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
1163
field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1164
name, 0, unsigned_flag);
1184
1166
case DRIZZLE_TYPE_DOUBLE:
1185
1167
field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1186
name, decimals, 0, unsigned_flag);
1168
name, decimals, 0, unsigned_flag);
1188
1170
case DRIZZLE_TYPE_NULL:
1189
1171
field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
1341
1328
enum_field_types f_type;
1343
1330
switch ((f_type=field_type())) {
1344
case DRIZZLE_TYPE_DATE:
1345
1332
case DRIZZLE_TYPE_NULL:
1346
1333
case DRIZZLE_TYPE_ENUM:
1347
1334
case DRIZZLE_TYPE_BLOB:
1348
1335
case DRIZZLE_TYPE_VARCHAR:
1349
case DRIZZLE_TYPE_UUID:
1350
1336
case DRIZZLE_TYPE_DECIMAL:
1353
if ((res=val_str(buffer)))
1354
result= client->store(res->ptr(),res->length());
1339
if ((res=val_str(buffer)))
1340
result= client->store(res->ptr(),res->length());
1357
1343
case DRIZZLE_TYPE_LONG:
1362
result= client->store((int32_t)nr);
1348
result= client->store((int32_t)nr);
1365
1351
case DRIZZLE_TYPE_LONGLONG:
1372
result= client->store((uint64_t)nr);
1374
result= client->store((int64_t)nr);
1358
result= client->store((uint64_t)nr);
1360
result= client->store((int64_t)nr);
1378
1364
case DRIZZLE_TYPE_DOUBLE:
1380
double nr= val_real();
1382
result= client->store(nr, decimals, buffer);
1366
double nr= val_real();
1368
result= client->store(nr, decimals, buffer);
1385
1371
case DRIZZLE_TYPE_DATETIME:
1386
1372
case DRIZZLE_TYPE_TIMESTAMP:
1389
get_date(&tm, TIME_FUZZY_DATE);
1391
result= client->store(&tm);
1375
get_date(&tm, TIME_FUZZY_DATE);
1377
result= client->store(&tm);
1395
1381
if (null_value)
1396
1382
result= client->store();
1428
1410
switch (res_type) {
1429
1411
case STRING_RESULT:
1413
char buff[MAX_FIELD_WIDTH];
1414
String tmp(buff,sizeof(buff),&my_charset_bin),*result;
1415
result=item->val_str(&tmp);
1416
if (item->null_value)
1417
new_item= new Item_null(name);
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());
1420
uint32_t length= result->length();
1421
char *tmp_str= memory::sql_strmake(result->ptr(), length);
1422
new_item= new Item_string(name, tmp_str, length, result->charset());
1444
1426
case INT_RESULT:
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));
1428
int64_t result=item->val_int();
1429
uint32_t length=item->max_length;
1430
bool null_value=item->null_value;
1431
new_item= (null_value ? (Item*) new Item_null(name) :
1432
(Item*) new Item_int(name, result, length));
1453
1435
case ROW_RESULT:
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).
1436
if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
1439
Substitute constants only in Item_rows. Don't affect other Items
1440
with ROW_RESULT (eg Item_singlerow_subselect).
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));
1442
For such Items more optimal is to detect if it is constant and replace
1443
it with Item_row. This would optimize queries like this:
1444
SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
1446
Item_row *item_row= (Item_row*) item;
1447
Item_row *comp_item_row= (Item_row*) comp_item;
1451
If item and comp_item are both Item_rows and have same number of cols
1452
then process items in Item_row one by one.
1453
We can't ignore NULL values here as this item may be used with <=>, in
1454
which case NULL's are significant.
1456
assert(item->result_type() == comp_item->result_type());
1457
assert(item_row->cols() == comp_item_row->cols());
1458
col= item_row->cols();
1460
resolve_const_item(session, item_row->addr(col),
1461
comp_item_row->element_index(col));
1483
1465
case REAL_RESULT:
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));
1466
{ // It must REAL_RESULT
1467
double result= item->val_real();
1468
uint32_t length=item->max_length,decimals=item->decimals;
1469
bool null_value=item->null_value;
1470
new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
1471
new Item_float(name, result, decimals, length));
1492
1474
case DECIMAL_RESULT:
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));
1476
my_decimal decimal_value;
1477
my_decimal *result= item->val_decimal(&decimal_value);
1478
uint32_t length= item->max_length, decimals= item->decimals;
1479
bool null_value= item->null_value;
1480
new_item= (null_value ?
1481
(Item*) new Item_null(name) :
1482
(Item*) new Item_decimal(name, result, length, decimals));
1506
1489
session->change_item_tree(ref, new_item);
1577
1556
uint32_t convert_blob_length)
1579
1558
bool maybe_null= item->maybe_null;
1580
Field *new_field= NULL;
1582
1561
switch (item->result_type()) {
1583
1562
case REAL_RESULT:
1584
1563
new_field= new Field_double(item->max_length, maybe_null,
1585
1564
item->name, item->decimals, true);
1588
1566
case INT_RESULT:
1590
1568
Select an integer type with the minimal fit precision.
1591
1569
MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
1592
1570
Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
1593
Int32 -> make them field::Int64.
1571
Field_long : make them Field_int64_t.
1595
1573
if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
1596
new_field=new field::Int64(item->max_length, maybe_null,
1597
item->name, item->unsigned_flag);
1574
new_field=new Field_int64_t(item->max_length, maybe_null,
1575
item->name, item->unsigned_flag);
1599
new_field=new field::Int32(item->max_length, maybe_null,
1600
item->name, item->unsigned_flag);
1577
new_field=new Field_long(item->max_length, maybe_null,
1578
item->name, item->unsigned_flag);
1603
1580
case STRING_RESULT:
1604
1581
assert(item->collation.collation);
1611
1588
if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
1612
1589
type == DRIZZLE_TYPE_DATE ||
1613
1590
type == DRIZZLE_TYPE_TIMESTAMP)
1615
1591
new_field= item->tmp_table_field_from_field_type(table, 1);
1617
Make sure that the blob fits into a Field_varstring which has
1593
Make sure that the blob fits into a Field_varstring which has
1621
1596
else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
1622
1597
convert_blob_length <= Field_varstring::MAX_SIZE &&
1623
1598
convert_blob_length)
1625
table->setVariableWidth();
1626
1599
new_field= new Field_varstring(convert_blob_length, maybe_null,
1627
item->name, item->collation.collation);
1600
item->name, table->getMutableShare(),
1601
item->collation.collation);
1631
1603
new_field= item->make_string_field(table);
1633
1604
new_field->set_derivation(item->collation.derivation);
1636
1606
case DECIMAL_RESULT:
1608
uint8_t dec= item->decimals;
1609
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
1610
uint32_t len= item->max_length;
1613
Trying to put too many digits overall in a DECIMAL(prec,dec)
1614
will always throw a warning. We must limit dec to
1615
DECIMAL_MAX_SCALE however to prevent an assert() later.
1638
uint8_t dec= item->decimals;
1639
uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
1640
uint32_t len= item->max_length;
1620
signed int overflow;
1622
dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
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.
1625
If the value still overflows the field with the corrected dec,
1626
we'll throw out decimals rather than integers. This is still
1627
bad and of course throws a truncation warning.
1628
+1: for decimal point
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);
1631
overflow= my_decimal_precision_to_length(intg + dec, dec,
1632
item->unsigned_flag) - len;
1635
dec= max(0, dec - overflow); // too long, discard fract
1637
len-= item->decimals - dec; // corrected value fits
1640
new_field= new Field_decimal(len,
1644
item->unsigned_flag);
1678
1647
case ROW_RESULT:
1679
1649
// This case should never be choosen
1685
1655
new_field->init(table);
1687
1657
if (copy_func && item->is_result_field())
1688
1658
*((*copy_func)++) = item; // Save for copy_funcs
1690
1659
if (modify_item)
1691
1660
item->set_result_field(new_field);
1693
1661
if (item->type() == Item::NULL_ITEM)
1694
1662
new_field->is_created_from_null_item= true;
1696
1663
return new_field;