~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Brian Aker
  • Date: 2010-06-03 22:55:32 UTC
  • mto: (1578.6.9 explain-drizzle)
  • mto: This revision was merged to the branch mainline in revision 1591.
  • Revision ID: brian@gir-2.local-20100603225532-5yuc8um41i4owavf
Additional pass through to remove raw field access.

Show diffs side-by-side

added added

removed removed

Lines of Context:
41
41
#include "drizzled/field/decimal.h"
42
42
#include "drizzled/field/real.h"
43
43
#include "drizzled/field/double.h"
44
 
#include "drizzled/field/int32.h"
45
 
#include "drizzled/field/int64.h"
 
44
#include "drizzled/field/long.h"
 
45
#include "drizzled/field/int64_t.h"
46
46
#include "drizzled/field/num.h"
47
47
#include "drizzled/field/timestamp.h"
48
48
#include "drizzled/field/datetime.h"
92
92
  {
93
93
    case INT_RESULT:
94
94
      return val_int() != 0;
95
 
 
96
95
    case DECIMAL_RESULT:
97
96
    {
98
97
      my_decimal decimal_value;
101
100
        return !my_decimal_is_zero(val);
102
101
      return false;
103
102
    }
104
 
 
105
103
    case REAL_RESULT:
106
104
    case STRING_RESULT:
107
105
      return val_real() != 0.0;
108
 
 
109
106
    case ROW_RESULT:
 
107
    default:
110
108
      assert(0);
111
 
      abort();
 
109
      return false;
112
110
  }
113
 
 
114
 
  assert(0);
115
 
  abort();
116
111
}
117
112
 
118
113
String *Item::val_string_from_real(String *str)
835
830
                  0);
836
831
    }
837
832
    else
838
 
      prev_subselect_item->used_tables_cache|= found_field->getTable()->map;
 
833
      prev_subselect_item->used_tables_cache|= found_field->table->map;
839
834
    prev_subselect_item->const_item_cache= 0;
840
835
    mark_as_dependent(session, last_select, current_sel, resolved_item,
841
836
                      dependent);
856
851
    - the found item on success
857
852
    - NULL if find_item is not in group_list
858
853
*/
859
 
static Item** find_field_in_group_list(Item *find_item, Order *group_list)
 
854
static Item** find_field_in_group_list(Item *find_item, order_st *group_list)
860
855
{
861
856
  const char *db_name;
862
857
  const char *table_name;
863
858
  const char *field_name;
864
 
  Order *found_group= NULL;
 
859
  order_st *found_group= NULL;
865
860
  int found_match_degree= 0;
866
861
  Item_ident *cur_field;
867
862
  int cur_match_degree= 0;
887
882
 
888
883
  assert(field_name != 0);
889
884
 
890
 
  for (Order *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
 
885
  for (order_st *cur_group= group_list ; cur_group ; cur_group= cur_group->next)
891
886
  {
892
887
    if ((*(cur_group->item))->real_item()->type() == Item::FIELD_ITEM)
893
888
    {
949
944
{
950
945
  Item **group_by_ref= NULL;
951
946
  Item **select_ref= NULL;
952
 
  Order *group_list= (Order*) select->group_list.first;
 
947
  order_st *group_list= (order_st*) select->group_list.first;
953
948
  bool ambiguous_fields= false;
954
949
  uint32_t counter;
955
950
  enum_resolution_type resolution;
958
953
    Search for a column or derived column named as 'ref' in the SELECT
959
954
    clause of the current select.
960
955
  */
961
 
  if (!(select_ref= find_item_in_list(session,
962
 
                                      ref, *(select->get_item_list()),
 
956
  if (!(select_ref= find_item_in_list(ref, *(select->get_item_list()),
963
957
                                      &counter, REPORT_EXCEPT_NOT_FOUND,
964
958
                                      &resolution)))
965
959
    return NULL; /* Some error occurred. */
1049
1043
  case REAL_RESULT:    
1050
1044
    return DRIZZLE_TYPE_DOUBLE;
1051
1045
  case ROW_RESULT:
 
1046
  default:
1052
1047
    assert(0);
 
1048
    return DRIZZLE_TYPE_VARCHAR;
1053
1049
  }
1054
 
 
1055
 
  abort();
1056
1050
}
1057
1051
 
1058
1052
bool Item::is_datetime()
1063
1057
    case DRIZZLE_TYPE_DATETIME:
1064
1058
    case DRIZZLE_TYPE_TIMESTAMP:
1065
1059
      return true;
1066
 
    case DRIZZLE_TYPE_BLOB:
1067
 
    case DRIZZLE_TYPE_VARCHAR:
1068
 
    case DRIZZLE_TYPE_DOUBLE:
1069
 
    case DRIZZLE_TYPE_DECIMAL:
1070
 
    case DRIZZLE_TYPE_ENUM:
1071
 
    case DRIZZLE_TYPE_LONG:
1072
 
    case DRIZZLE_TYPE_LONGLONG:
1073
 
    case DRIZZLE_TYPE_NULL:
1074
 
    case DRIZZLE_TYPE_UUID:
1075
 
      return false;
 
1060
    default:
 
1061
      break;
1076
1062
  }
1077
 
 
1078
 
  assert(0);
1079
 
  abort();
 
1063
  return false;
1080
1064
}
1081
1065
 
1082
1066
String *Item::check_well_formed_result(String *str, bool send_error)
1139
1123
  Field *field;
1140
1124
  assert(collation.collation);
1141
1125
  if (max_length/collation.collation->mbmaxlen > CONVERT_IF_BIGGER_TO_BLOB)
1142
 
  {
1143
1126
    field= new Field_blob(max_length, maybe_null, name,
1144
1127
                          collation.collation);
1145
 
  }
1146
1128
  else
1147
 
  {
1148
 
    table->setVariableWidth();
1149
 
    field= new Field_varstring(max_length, maybe_null, name,
 
1129
    field= new Field_varstring(max_length, maybe_null, name, table->getMutableShare(),
1150
1130
                               collation.collation);
1151
 
  }
1152
1131
 
1153
1132
  if (field)
1154
1133
    field->init(table);
1161
1140
    The field functions defines a field to be not null if null_ptr is not 0
1162
1141
  */
1163
1142
  unsigned char *null_ptr= maybe_null ? (unsigned char*) "" : 0;
1164
 
  Field *field= NULL;
 
1143
  Field *field;
1165
1144
 
1166
1145
  switch (field_type()) {
1167
1146
  case DRIZZLE_TYPE_DECIMAL:
1176
1155
                                 unsigned_flag);
1177
1156
    break;
1178
1157
  case DRIZZLE_TYPE_LONG:
1179
 
    field= new field::Int32((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
 
1158
    field= new Field_long((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
1159
                          name, 0, unsigned_flag);
1180
1160
    break;
1181
1161
  case DRIZZLE_TYPE_LONGLONG:
1182
 
    field= new field::Int64((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE, name);
 
1162
    field= new Field_int64_t((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
 
1163
                              name, 0, unsigned_flag);
1183
1164
    break;
1184
1165
  case DRIZZLE_TYPE_DOUBLE:
1185
1166
    field= new Field_double((unsigned char*) 0, max_length, null_ptr, 0, Field::NONE,
1186
 
                            name, decimals, 0, unsigned_flag);
 
1167
                            name, decimals, 0, unsigned_flag);
1187
1168
    break;
1188
1169
  case DRIZZLE_TYPE_NULL:
1189
1170
    field= new Field_null((unsigned char*) 0, max_length, name, &my_charset_bin);
1197
1178
  case DRIZZLE_TYPE_DATETIME:
1198
1179
    field= new Field_datetime(maybe_null, name, &my_charset_bin);
1199
1180
    break;
1200
 
  case DRIZZLE_TYPE_UUID:
 
1181
  default:
 
1182
    /* This case should never be chosen */
 
1183
    assert(0);
 
1184
    /* Fall through to make_string_field() */
1201
1185
  case DRIZZLE_TYPE_ENUM:
1202
1186
  case DRIZZLE_TYPE_VARCHAR:
1203
1187
    return make_string_field(table);
1204
1188
  case DRIZZLE_TYPE_BLOB:
 
1189
    if (this->type() == Item::TYPE_HOLDER)
 
1190
      field= new Field_blob(max_length, maybe_null, name, collation.collation,
 
1191
                            1);
 
1192
    else
1205
1193
      field= new Field_blob(max_length, maybe_null, name, collation.collation);
1206
1194
    break;                                      // Blob handled outside of case
1207
1195
  }
1208
 
  assert(field);
1209
 
 
1210
1196
  if (field)
1211
1197
    field->init(table);
1212
1198
  return field;
1341
1327
  enum_field_types f_type;
1342
1328
 
1343
1329
  switch ((f_type=field_type())) {
1344
 
  case DRIZZLE_TYPE_DATE:
 
1330
  default:
1345
1331
  case DRIZZLE_TYPE_NULL:
1346
1332
  case DRIZZLE_TYPE_ENUM:
1347
1333
  case DRIZZLE_TYPE_BLOB:
1348
1334
  case DRIZZLE_TYPE_VARCHAR:
1349
 
  case DRIZZLE_TYPE_UUID:
1350
1335
  case DRIZZLE_TYPE_DECIMAL:
1351
 
    {
1352
 
      String *res;
1353
 
      if ((res=val_str(buffer)))
1354
 
        result= client->store(res->ptr(),res->length());
1355
 
      break;
1356
 
    }
 
1336
  {
 
1337
    String *res;
 
1338
    if ((res=val_str(buffer)))
 
1339
      result= client->store(res->ptr(),res->length());
 
1340
    break;
 
1341
  }
1357
1342
  case DRIZZLE_TYPE_LONG:
1358
 
    {
1359
 
      int64_t nr;
1360
 
      nr= val_int();
1361
 
      if (!null_value)
1362
 
        result= client->store((int32_t)nr);
1363
 
      break;
1364
 
    }
 
1343
  {
 
1344
    int64_t nr;
 
1345
    nr= val_int();
 
1346
    if (!null_value)
 
1347
      result= client->store((int32_t)nr);
 
1348
    break;
 
1349
  }
1365
1350
  case DRIZZLE_TYPE_LONGLONG:
 
1351
  {
 
1352
    int64_t nr;
 
1353
    nr= val_int();
 
1354
    if (!null_value)
1366
1355
    {
1367
 
      int64_t nr;
1368
 
      nr= val_int();
1369
 
      if (!null_value)
1370
 
      {
1371
 
        if (unsigned_flag)
1372
 
          result= client->store((uint64_t)nr);
1373
 
        else
1374
 
          result= client->store((int64_t)nr);
1375
 
      }
1376
 
      break;
 
1356
      if (unsigned_flag)
 
1357
        result= client->store((uint64_t)nr);
 
1358
      else
 
1359
        result= client->store((int64_t)nr);
1377
1360
    }
 
1361
    break;
 
1362
  }
1378
1363
  case DRIZZLE_TYPE_DOUBLE:
1379
 
    {
1380
 
      double nr= val_real();
1381
 
      if (!null_value)
1382
 
        result= client->store(nr, decimals, buffer);
1383
 
      break;
1384
 
    }
 
1364
  {
 
1365
    double nr= val_real();
 
1366
    if (!null_value)
 
1367
      result= client->store(nr, decimals, buffer);
 
1368
    break;
 
1369
  }
1385
1370
  case DRIZZLE_TYPE_DATETIME:
1386
1371
  case DRIZZLE_TYPE_TIMESTAMP:
1387
 
    {
1388
 
      DRIZZLE_TIME tm;
1389
 
      get_date(&tm, TIME_FUZZY_DATE);
1390
 
      if (!null_value)
1391
 
        result= client->store(&tm);
1392
 
      break;
1393
 
    }
 
1372
  {
 
1373
    DRIZZLE_TIME tm;
 
1374
    get_date(&tm, TIME_FUZZY_DATE);
 
1375
    if (!null_value)
 
1376
      result= client->store(&tm);
 
1377
    break;
 
1378
  }
1394
1379
  }
1395
1380
  if (null_value)
1396
1381
    result= client->store();
1397
 
 
1398
1382
  return result;
1399
1383
}
1400
1384
 
1402
1386
{
1403
1387
  if (a == STRING_RESULT && b == STRING_RESULT)
1404
1388
    return STRING_RESULT;
1405
 
 
1406
1389
  if (a == INT_RESULT && b == INT_RESULT)
1407
1390
    return INT_RESULT;
1408
1391
  else if (a == ROW_RESULT || b == ROW_RESULT)
1409
1392
    return ROW_RESULT;
1410
 
 
1411
1393
  if ((a == INT_RESULT || a == DECIMAL_RESULT) &&
1412
1394
      (b == INT_RESULT || b == DECIMAL_RESULT))
1413
1395
    return DECIMAL_RESULT;
1414
 
 
1415
1396
  return REAL_RESULT;
1416
1397
}
1417
1398
 
1427
1408
 
1428
1409
  switch (res_type) {
1429
1410
  case STRING_RESULT:
 
1411
  {
 
1412
    char buff[MAX_FIELD_WIDTH];
 
1413
    String tmp(buff,sizeof(buff),&my_charset_bin),*result;
 
1414
    result=item->val_str(&tmp);
 
1415
    if (item->null_value)
 
1416
      new_item= new Item_null(name);
 
1417
    else
1430
1418
    {
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);
1436
 
      else
1437
 
      {
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());
1441
 
      }
1442
 
      break;
 
1419
      uint32_t length= result->length();
 
1420
      char *tmp_str= memory::sql_strmake(result->ptr(), length);
 
1421
      new_item= new Item_string(name, tmp_str, length, result->charset());
1443
1422
    }
 
1423
    break;
 
1424
  }
1444
1425
  case INT_RESULT:
1445
 
    {
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));
1451
 
      break;
1452
 
    }
 
1426
  {
 
1427
    int64_t result=item->val_int();
 
1428
    uint32_t length=item->max_length;
 
1429
    bool null_value=item->null_value;
 
1430
    new_item= (null_value ? (Item*) new Item_null(name) :
 
1431
               (Item*) new Item_int(name, result, length));
 
1432
    break;
 
1433
  }
1453
1434
  case ROW_RESULT:
1454
 
    if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
1455
 
    {
1456
 
      /*
1457
 
        Substitute constants only in Item_rows. Don't affect other Items
1458
 
        with ROW_RESULT (eg Item_singlerow_subselect).
 
1435
  if (item->type() == Item::ROW_ITEM && comp_item->type() == Item::ROW_ITEM)
 
1436
  {
 
1437
    /*
 
1438
      Substitute constants only in Item_rows. Don't affect other Items
 
1439
      with ROW_RESULT (eg Item_singlerow_subselect).
1459
1440
 
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);
1463
 
      */
1464
 
      Item_row *item_row= (Item_row*) item;
1465
 
      Item_row *comp_item_row= (Item_row*) comp_item;
1466
 
      uint32_t col;
1467
 
      new_item= 0;
1468
 
      /*
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.
1473
 
      */
1474
 
      assert(item->result_type() == comp_item->result_type());
1475
 
      assert(item_row->cols() == comp_item_row->cols());
1476
 
      col= item_row->cols();
1477
 
      while (col-- > 0)
1478
 
        resolve_const_item(session, item_row->addr(col),
1479
 
                           comp_item_row->element_index(col));
1480
 
      break;
1481
 
    }
1482
 
    /* Fallthrough */
 
1441
      For such Items more optimal is to detect if it is constant and replace
 
1442
      it with Item_row. This would optimize queries like this:
 
1443
      SELECT * FROM t1 WHERE (a,b) = (SELECT a,b FROM t2 LIMIT 1);
 
1444
    */
 
1445
    Item_row *item_row= (Item_row*) item;
 
1446
    Item_row *comp_item_row= (Item_row*) comp_item;
 
1447
    uint32_t col;
 
1448
    new_item= 0;
 
1449
    /*
 
1450
      If item and comp_item are both Item_rows and have same number of cols
 
1451
      then process items in Item_row one by one.
 
1452
      We can't ignore NULL values here as this item may be used with <=>, in
 
1453
      which case NULL's are significant.
 
1454
    */
 
1455
    assert(item->result_type() == comp_item->result_type());
 
1456
    assert(item_row->cols() == comp_item_row->cols());
 
1457
    col= item_row->cols();
 
1458
    while (col-- > 0)
 
1459
      resolve_const_item(session, item_row->addr(col),
 
1460
                         comp_item_row->element_index(col));
 
1461
    break;
 
1462
  }
 
1463
  /* Fallthrough */
1483
1464
  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));
1490
 
      break;
1491
 
    }
 
1465
  {                                             // It must REAL_RESULT
 
1466
    double result= item->val_real();
 
1467
    uint32_t length=item->max_length,decimals=item->decimals;
 
1468
    bool null_value=item->null_value;
 
1469
    new_item= (null_value ? (Item*) new Item_null(name) : (Item*)
 
1470
               new Item_float(name, result, decimals, length));
 
1471
    break;
 
1472
  }
1492
1473
  case DECIMAL_RESULT:
1493
 
    {
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));
1501
 
      break;
1502
 
    }
1503
 
  }
1504
 
 
 
1474
  {
 
1475
    my_decimal decimal_value;
 
1476
    my_decimal *result= item->val_decimal(&decimal_value);
 
1477
    uint32_t length= item->max_length, decimals= item->decimals;
 
1478
    bool null_value= item->null_value;
 
1479
    new_item= (null_value ?
 
1480
               (Item*) new Item_null(name) :
 
1481
               (Item*) new Item_decimal(name, result, length, decimals));
 
1482
    break;
 
1483
  }
 
1484
  default:
 
1485
    assert(0);
 
1486
  }
1505
1487
  if (new_item)
1506
1488
    session->change_item_tree(ref, new_item);
1507
1489
}
1520
1502
    item_result=item->val_str(&item_tmp);
1521
1503
    if (item->null_value)
1522
1504
      return 1;                                 // This must be true
1523
 
    field->val_str_internal(&field_tmp);
1524
 
    return not stringcmp(&field_tmp,item_result);
 
1505
    field->val_str(&field_tmp);
 
1506
    return !stringcmp(&field_tmp,item_result);
1525
1507
  }
1526
 
 
1527
1508
  if (res_type == INT_RESULT)
1528
1509
    return 1;                                   // Both where of type int
1529
 
 
1530
1510
  if (res_type == DECIMAL_RESULT)
1531
1511
  {
1532
1512
    my_decimal item_buf, *item_val,
1537
1517
    field_val= field->val_decimal(&field_buf);
1538
1518
    return !my_decimal_cmp(item_val, field_val);
1539
1519
  }
1540
 
 
1541
1520
  double result= item->val_real();
1542
1521
  if (item->null_value)
1543
1522
    return 1;
1544
 
 
1545
1523
  return result == field->val_real();
1546
1524
}
1547
1525
 
1577
1555
                                         uint32_t convert_blob_length)
1578
1556
{
1579
1557
  bool maybe_null= item->maybe_null;
1580
 
  Field *new_field= NULL;
 
1558
  Field *new_field;
1581
1559
 
1582
1560
  switch (item->result_type()) {
1583
1561
  case REAL_RESULT:
1584
1562
    new_field= new Field_double(item->max_length, maybe_null,
1585
1563
                                item->name, item->decimals, true);
1586
1564
    break;
1587
 
 
1588
1565
  case INT_RESULT:
1589
1566
    /*
1590
1567
      Select an integer type with the minimal fit precision.
1591
1568
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
1592
1569
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into
1593
 
      Int32 -> make them field::Int64.
 
1570
      Field_long : make them Field_int64_t.
1594
1571
    */
1595
1572
    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);
 
1573
      new_field=new Field_int64_t(item->max_length, maybe_null,
 
1574
                                   item->name, item->unsigned_flag);
1598
1575
    else
1599
 
      new_field=new field::Int32(item->max_length, maybe_null,
1600
 
                                 item->name, item->unsigned_flag);
 
1576
      new_field=new Field_long(item->max_length, maybe_null,
 
1577
                               item->name, item->unsigned_flag);
1601
1578
    break;
1602
 
 
1603
1579
  case STRING_RESULT:
1604
1580
    assert(item->collation.collation);
1605
1581
 
1611
1587
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
1612
1588
        type == DRIZZLE_TYPE_DATE ||
1613
1589
        type == DRIZZLE_TYPE_TIMESTAMP)
1614
 
    {
1615
1590
      new_field= item->tmp_table_field_from_field_type(table, 1);
1616
 
      /*
1617
 
        Make sure that the blob fits into a Field_varstring which has
1618
 
        2-byte lenght.
1619
 
      */
1620
 
    }
 
1591
    /*
 
1592
      Make sure that the blob fits into a Field_varstring which has
 
1593
      2-byte lenght.
 
1594
    */
1621
1595
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
1622
1596
             convert_blob_length <= Field_varstring::MAX_SIZE &&
1623
1597
             convert_blob_length)
1624
 
    {
1625
 
      table->setVariableWidth();
1626
1598
      new_field= new Field_varstring(convert_blob_length, maybe_null,
1627
 
                                     item->name, item->collation.collation);
1628
 
    }
 
1599
                                     item->name, table->getMutableShare(),
 
1600
                                     item->collation.collation);
1629
1601
    else
1630
 
    {
1631
1602
      new_field= item->make_string_field(table);
1632
 
    }
1633
1603
    new_field->set_derivation(item->collation.derivation);
1634
1604
    break;
1635
 
 
1636
1605
  case DECIMAL_RESULT:
 
1606
  {
 
1607
    uint8_t dec= item->decimals;
 
1608
    uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
 
1609
    uint32_t len= item->max_length;
 
1610
 
 
1611
    /*
 
1612
      Trying to put too many digits overall in a DECIMAL(prec,dec)
 
1613
      will always throw a warning. We must limit dec to
 
1614
      DECIMAL_MAX_SCALE however to prevent an assert() later.
 
1615
    */
 
1616
 
 
1617
    if (dec > 0)
1637
1618
    {
1638
 
      uint8_t dec= item->decimals;
1639
 
      uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
1640
 
      uint32_t len= item->max_length;
 
1619
      signed int overflow;
 
1620
 
 
1621
      dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
1641
1622
 
1642
1623
      /*
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.
 
1624
        If the value still overflows the field with the corrected dec,
 
1625
        we'll throw out decimals rather than integers. This is still
 
1626
        bad and of course throws a truncation warning.
 
1627
        +1: for decimal point
1646
1628
      */
1647
1629
 
1648
 
      if (dec > 0)
1649
 
      {
1650
 
        signed int overflow;
1651
 
 
1652
 
        dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
1653
 
 
1654
 
        /*
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
1659
 
        */
1660
 
 
1661
 
        overflow= my_decimal_precision_to_length(intg + dec, dec,
1662
 
                                                 item->unsigned_flag) - len;
1663
 
 
1664
 
        if (overflow > 0)
1665
 
          dec= max(0, dec - overflow);            // too long, discard fract
1666
 
        else
1667
 
          len-= item->decimals - dec;             // corrected value fits
1668
 
      }
1669
 
 
1670
 
      new_field= new Field_decimal(len,
1671
 
                                   maybe_null,
1672
 
                                   item->name,
1673
 
                                   dec,
1674
 
                                   item->unsigned_flag);
1675
 
      break;
 
1630
      overflow= my_decimal_precision_to_length(intg + dec, dec,
 
1631
                                               item->unsigned_flag) - len;
 
1632
 
 
1633
      if (overflow > 0)
 
1634
        dec= max(0, dec - overflow);            // too long, discard fract
 
1635
      else
 
1636
        len-= item->decimals - dec;             // corrected value fits
1676
1637
    }
1677
1638
 
 
1639
    new_field= new Field_decimal(len,
 
1640
                                 maybe_null,
 
1641
                                 item->name,
 
1642
                                 dec,
 
1643
                                 item->unsigned_flag);
 
1644
    break;
 
1645
  }
1678
1646
  case ROW_RESULT:
 
1647
  default:
1679
1648
    // This case should never be choosen
1680
1649
    assert(0);
1681
 
    abort();
 
1650
    new_field= 0;
 
1651
    break;
1682
1652
  }
1683
 
 
1684
1653
  if (new_field)
1685
1654
    new_field->init(table);
1686
1655
 
1687
1656
  if (copy_func && item->is_result_field())
1688
1657
    *((*copy_func)++) = item;                   // Save for copy_funcs
1689
 
 
1690
1658
  if (modify_item)
1691
1659
    item->set_result_field(new_field);
1692
 
 
1693
1660
  if (item->type() == Item::NULL_ITEM)
1694
1661
    new_field->is_created_from_null_item= true;
1695
 
 
1696
1662
  return new_field;
1697
1663
}
1698
1664
 
1749
1715
        field->result_field= result;
1750
1716
    }
1751
1717
    else
1752
 
    {
1753
1718
      result= create_tmp_field_from_field(session, (*from_field= field->field),
1754
1719
                                          orig_item ? orig_item->name :
1755
1720
                                          item->name,
1757
1722
                                          modify_item ? field :
1758
1723
                                          NULL,
1759
1724
                                          convert_blob_length);
1760
 
    }
1761
1725
    if (orig_type == Item::REF_ITEM && orig_modify)
1762
1726
      ((Item_ref*)orig_item)->set_result_field(result);
1763
1727
    if (field->field->eq_def(result))
1797
1761
  }
1798
1762
}
1799
1763
 
1800
 
std::ostream& operator<<(std::ostream& output, const Item &item)
1801
 
{
1802
 
  output << "Item:(";
1803
 
  output <<  item.name;
1804
 
  output << ", ";
1805
 
  output << drizzled::display::type(item.type());
1806
 
  output << ")";
1807
 
 
1808
 
  return output;  // for multiple << operators.
1809
 
}
1810
 
 
1811
1764
} /* namespace drizzled */