~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item.cc

  • Committer: Brian Aker
  • Date: 2008-11-16 02:13:14 UTC
  • mfrom: (584.1.7 devel)
  • Revision ID: brian@tangent.org-20081116021314-31uh0w1l0601cwd5
Merger from Monty

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
 
28
28
const String my_null_string("NULL", 4, default_charset_info);
29
29
 
30
 
/****************************************************************************/
31
 
 
32
 
/* Hybrid_type_traits {_real} */
33
 
 
34
 
void Hybrid_type_traits::fix_length_and_dec(Item *item, Item *arg) const
35
 
{
36
 
  item->decimals= NOT_FIXED_DEC;
37
 
  item->max_length= item->float_length(arg->decimals);
38
 
}
39
 
 
40
 
static const Hybrid_type_traits real_traits_instance;
41
 
 
42
 
const Hybrid_type_traits *Hybrid_type_traits::instance()
43
 
{
44
 
  return &real_traits_instance;
45
 
}
46
 
 
47
 
int64_t Hybrid_type_traits::val_int(Hybrid_type *val,
48
 
                                    bool) const
49
 
{
50
 
  return (int64_t) rint(val->real);
51
 
}
52
 
 
53
 
my_decimal *
54
 
Hybrid_type_traits::val_decimal(Hybrid_type *val, my_decimal *) const
55
 
{
56
 
  double2my_decimal(E_DEC_FATAL_ERROR, val->real, val->dec_buf);
57
 
  return val->dec_buf;
58
 
}
59
 
 
60
 
 
61
 
String *
62
 
Hybrid_type_traits::val_str(Hybrid_type *val, String *to, uint8_t decimals) const
63
 
{
64
 
  to->set_real(val->real, decimals, &my_charset_bin);
65
 
  return to;
66
 
}
67
 
 
68
 
/* Hybrid_type_traits_decimal */
69
 
static const Hybrid_type_traits_decimal decimal_traits_instance;
70
 
 
71
 
const Hybrid_type_traits_decimal *Hybrid_type_traits_decimal::instance()
72
 
{
73
 
  return &decimal_traits_instance;
74
 
}
75
 
 
76
 
 
77
 
void
78
 
Hybrid_type_traits_decimal::fix_length_and_dec(Item *item, Item *arg) const
79
 
{
80
 
  item->decimals= arg->decimals;
81
 
  item->max_length= cmin(arg->max_length + DECIMAL_LONGLONG_DIGITS,
82
 
                        (unsigned int)DECIMAL_MAX_STR_LENGTH);
83
 
}
84
 
 
85
 
 
86
 
void Hybrid_type_traits_decimal::set_zero(Hybrid_type *val) const
87
 
{
88
 
  my_decimal_set_zero(&val->dec_buf[0]);
89
 
  val->used_dec_buf_no= 0;
90
 
}
91
 
 
92
 
 
93
 
void Hybrid_type_traits_decimal::add(Hybrid_type *val, Field *f) const
94
 
{
95
 
  my_decimal_add(E_DEC_FATAL_ERROR,
96
 
                 &val->dec_buf[val->used_dec_buf_no ^ 1],
97
 
                 &val->dec_buf[val->used_dec_buf_no],
98
 
                 f->val_decimal(&val->dec_buf[2]));
99
 
  val->used_dec_buf_no^= 1;
100
 
}
101
 
 
102
 
 
103
 
/**
104
 
  @todo
105
 
  what is '4' for scale?
106
 
*/
107
 
void Hybrid_type_traits_decimal::div(Hybrid_type *val, uint64_t u) const
108
 
{
109
 
  int2my_decimal(E_DEC_FATAL_ERROR, u, true, &val->dec_buf[2]);
110
 
  /* XXX: what is '4' for scale? */
111
 
  my_decimal_div(E_DEC_FATAL_ERROR,
112
 
                 &val->dec_buf[val->used_dec_buf_no ^ 1],
113
 
                 &val->dec_buf[val->used_dec_buf_no],
114
 
                 &val->dec_buf[2], 4);
115
 
  val->used_dec_buf_no^= 1;
116
 
}
117
 
 
118
 
 
119
 
int64_t
120
 
Hybrid_type_traits_decimal::val_int(Hybrid_type *val, bool unsigned_flag) const
121
 
{
122
 
  int64_t result;
123
 
  my_decimal2int(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
124
 
                 unsigned_flag, &result);
125
 
  return result;
126
 
}
127
 
 
128
 
 
129
 
double
130
 
Hybrid_type_traits_decimal::val_real(Hybrid_type *val) const
131
 
{
132
 
  my_decimal2double(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
133
 
                    &val->real);
134
 
  return val->real;
135
 
}
136
 
 
137
 
 
138
 
String *
139
 
Hybrid_type_traits_decimal::val_str(Hybrid_type *val, String *to,
140
 
                                    uint8_t decimals) const
141
 
{
142
 
  my_decimal_round(E_DEC_FATAL_ERROR, &val->dec_buf[val->used_dec_buf_no],
143
 
                   decimals, false, &val->dec_buf[2]);
144
 
  my_decimal2string(E_DEC_FATAL_ERROR, &val->dec_buf[2], 0, 0, 0, to);
145
 
  return to;
146
 
}
147
 
 
148
 
/* Hybrid_type_traits_integer */
149
 
static const Hybrid_type_traits_integer integer_traits_instance;
150
 
 
151
 
const Hybrid_type_traits_integer *Hybrid_type_traits_integer::instance()
152
 
{
153
 
  return &integer_traits_instance;
154
 
}
155
 
 
156
 
void
157
 
Hybrid_type_traits_integer::fix_length_and_dec(Item *item, Item *) const
158
 
{
159
 
  item->decimals= 0;
160
 
  item->max_length= MY_INT64_NUM_DECIMAL_DIGITS;
161
 
  item->unsigned_flag= 0;
162
 
}
163
30
 
164
31
/*****************************************************************************
165
32
** Item functions
1080
947
}
1081
948
 
1082
949
 
1083
 
static bool
1084
 
left_is_superset(DTCollation *left, DTCollation *right)
1085
 
{
1086
 
  /* Allow convert to Unicode */
1087
 
  if (left->collation->state & MY_CS_UNICODE &&
1088
 
      (left->derivation < right->derivation ||
1089
 
       (left->derivation == right->derivation &&
1090
 
        !(right->collation->state & MY_CS_UNICODE))))
1091
 
    return true;
1092
 
  /* Allow convert from ASCII */
1093
 
  if (right->repertoire == MY_REPERTOIRE_ASCII &&
1094
 
      (left->derivation < right->derivation ||
1095
 
       (left->derivation == right->derivation &&
1096
 
        !(left->repertoire == MY_REPERTOIRE_ASCII))))
1097
 
    return true;
1098
 
  /* Disallow conversion otherwise */
1099
 
  return false;
1100
 
}
1101
 
 
1102
 
/**
1103
 
  Aggregate two collations together taking
1104
 
  into account their coercibility (aka derivation):.
1105
 
 
1106
 
  0 == DERIVATION_EXPLICIT  - an explicitly written COLLATE clause @n
1107
 
  1 == DERIVATION_NONE      - a mix of two different collations @n
1108
 
  2 == DERIVATION_IMPLICIT  - a column @n
1109
 
  3 == DERIVATION_COERCIBLE - a string constant.
1110
 
 
1111
 
  The most important rules are:
1112
 
  -# If collations are the same:
1113
 
  chose this collation, and the strongest derivation.
1114
 
  -# If collations are different:
1115
 
  - Character sets may differ, but only if conversion without
1116
 
  data loss is possible. The caller provides flags whether
1117
 
  character set conversion attempts should be done. If no
1118
 
  flags are substituted, then the character sets must be the same.
1119
 
  Currently processed flags are:
1120
 
  MY_COLL_ALLOW_SUPERSET_CONV  - allow conversion to a superset
1121
 
  MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value
1122
 
  - two EXPLICIT collations produce an error, e.g. this is wrong:
1123
 
  CONCAT(expr1 collate latin1_swedish_ci, expr2 collate latin1_german_ci)
1124
 
  - the side with smaller derivation value wins,
1125
 
  i.e. a column is stronger than a string constant,
1126
 
  an explicit COLLATE clause is stronger than a column.
1127
 
  - if derivations are the same, we have DERIVATION_NONE,
1128
 
  we'll wait for an explicit COLLATE clause which possibly can
1129
 
  come from another argument later: for example, this is valid,
1130
 
  but we don't know yet when collecting the first two arguments:
1131
 
     @code
1132
 
       CONCAT(latin1_swedish_ci_column,
1133
 
              latin1_german1_ci_column,
1134
 
              expr COLLATE latin1_german2_ci)
1135
 
  @endcode
1136
 
*/
1137
 
 
1138
 
bool DTCollation::aggregate(DTCollation &dt, uint32_t flags)
1139
 
{
1140
 
  if (!my_charset_same(collation, dt.collation))
1141
 
  {
1142
 
    /* 
1143
 
       We do allow to use binary strings (like BLOBS)
1144
 
       together with character strings.
1145
 
       Binaries have more precedence than a character
1146
 
       string of the same derivation.
1147
 
    */
1148
 
    if (collation == &my_charset_bin)
1149
 
    {
1150
 
      if (derivation <= dt.derivation)
1151
 
        ; // Do nothing
1152
 
      else
1153
 
      {
1154
 
        set(dt); 
1155
 
      }
1156
 
    }
1157
 
    else if (dt.collation == &my_charset_bin)
1158
 
    {
1159
 
      if (dt.derivation <= derivation)
1160
 
      {
1161
 
        set(dt);
1162
 
      }
1163
 
      else
1164
 
      {
1165
 
        // Do nothing
1166
 
      }
1167
 
    }
1168
 
    else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1169
 
             left_is_superset(this, &dt))
1170
 
    {
1171
 
      // Do nothing
1172
 
    }
1173
 
    else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1174
 
             left_is_superset(&dt, this))
1175
 
    {
1176
 
      set(dt);
1177
 
    }
1178
 
    else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
1179
 
             derivation < dt.derivation &&
1180
 
             dt.derivation >= DERIVATION_SYSCONST)
1181
 
    {
1182
 
      // Do nothing;
1183
 
    }
1184
 
    else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
1185
 
             dt.derivation < derivation &&
1186
 
             derivation >= DERIVATION_SYSCONST)
1187
 
    {
1188
 
      set(dt);
1189
 
    }
1190
 
    else
1191
 
    {
1192
 
      // Cannot apply conversion
1193
 
      set(0, DERIVATION_NONE, 0);
1194
 
      return 1;
1195
 
    }
1196
 
  }
1197
 
  else if (derivation < dt.derivation)
1198
 
  {
1199
 
    // Do nothing
1200
 
  }
1201
 
  else if (dt.derivation < derivation)
1202
 
  {
1203
 
    set(dt);
1204
 
  }
1205
 
  else
1206
 
  { 
1207
 
    if (collation == dt.collation)
1208
 
    {
1209
 
      // Do nothing
1210
 
    }
1211
 
    else 
1212
 
    {
1213
 
      if (derivation == DERIVATION_EXPLICIT)
1214
 
      {
1215
 
        set(0, DERIVATION_NONE, 0);
1216
 
        return 1;
1217
 
      }
1218
 
      if (collation->state & MY_CS_BINSORT)
1219
 
        return 0;
1220
 
      if (dt.collation->state & MY_CS_BINSORT)
1221
 
      {
1222
 
        set(dt);
1223
 
        return 0;
1224
 
      }
1225
 
      const CHARSET_INFO * const bin= get_charset_by_csname(collation->csname, 
1226
 
                                                            MY_CS_BINSORT,MYF(0));
1227
 
      set(bin, DERIVATION_NONE);
1228
 
    }
1229
 
  }
1230
 
  repertoire|= dt.repertoire;
1231
 
  return 0;
1232
 
}
1233
 
 
1234
 
/******************************/
1235
 
static
1236
 
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, const char *fname)
1237
 
{
1238
 
  my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
1239
 
           c1.collation->name,c1.derivation_name(),
1240
 
           c2.collation->name,c2.derivation_name(),
1241
 
           fname);
1242
 
}
1243
 
 
1244
 
 
1245
 
static
1246
 
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, DTCollation &c3,
1247
 
                       const char *fname)
1248
 
{
1249
 
  my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0),
1250
 
           c1.collation->name,c1.derivation_name(),
1251
 
           c2.collation->name,c2.derivation_name(),
1252
 
           c3.collation->name,c3.derivation_name(),
1253
 
           fname);
1254
 
}
1255
 
 
1256
 
 
1257
 
static
1258
 
void my_coll_agg_error(Item** args, uint32_t count, const char *fname,
1259
 
                       int item_sep)
1260
 
{
1261
 
  if (count == 2)
1262
 
    my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
1263
 
  else if (count == 3)
1264
 
    my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
1265
 
                      args[2*item_sep]->collation, fname);
1266
 
  else
1267
 
    my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);
1268
 
}
1269
 
 
1270
 
 
1271
 
bool agg_item_collations(DTCollation &c, const char *fname,
1272
 
                         Item **av, uint32_t count, uint32_t flags, int item_sep)
1273
 
{
1274
 
  uint32_t i;
1275
 
  Item **arg;
1276
 
  c.set(av[0]->collation);
1277
 
  for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
1278
 
  {
1279
 
    if (c.aggregate((*arg)->collation, flags))
1280
 
    {
1281
 
      my_coll_agg_error(av, count, fname, item_sep);
1282
 
      return true;
1283
 
    }
1284
 
  }
1285
 
  if ((flags & MY_COLL_DISALLOW_NONE) &&
1286
 
      c.derivation == DERIVATION_NONE)
1287
 
  {
1288
 
    my_coll_agg_error(av, count, fname, item_sep);
1289
 
    return true;
1290
 
  }
1291
 
  return false;
1292
 
}
1293
 
 
1294
 
 
1295
 
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
1296
 
                                        Item **av, uint32_t count, uint32_t flags)
1297
 
{
1298
 
  return (agg_item_collations(c, fname, av, count,
1299
 
                              flags | MY_COLL_DISALLOW_NONE, 1));
1300
 
}
1301
 
 
1302
 
 
1303
 
/**
1304
 
  Collect arguments' character sets together.
1305
 
 
1306
 
  We allow to apply automatic character set conversion in some cases.
1307
 
  The conditions when conversion is possible are:
1308
 
  - arguments A and B have different charsets
1309
 
  - A wins according to coercibility rules
1310
 
    (i.e. a column is stronger than a string constant,
1311
 
     an explicit COLLATE clause is stronger than a column)
1312
 
  - character set of A is either superset for character set of B,
1313
 
    or B is a string constant which can be converted into the
1314
 
    character set of A without data loss.
1315
 
    
1316
 
  If all of the above is true, then it's possible to convert
1317
 
  B into the character set of A, and then compare according
1318
 
  to the collation of A.
1319
 
  
1320
 
  For functions with more than two arguments:
1321
 
  @code
1322
 
    collect(A,B,C) ::= collect(collect(A,B),C)
1323
 
  @endcode
1324
 
  Since this function calls Session::change_item_tree() on the passed Item **
1325
 
  pointers, it is necessary to pass the original Item **'s, not copies.
1326
 
  Otherwise their values will not be properly restored (see BUG#20769).
1327
 
  If the items are not consecutive (eg. args[2] and args[5]), use the
1328
 
  item_sep argument, ie.
1329
 
  @code
1330
 
    agg_item_charsets(coll, fname, &args[2], 2, flags, 3)
1331
 
  @endcode
1332
 
*/
1333
 
 
1334
 
bool agg_item_charsets(DTCollation &coll, const char *fname,
1335
 
                       Item **args, uint32_t nargs, uint32_t flags, int item_sep)
1336
 
{
1337
 
  Item **arg, *safe_args[2];
1338
 
 
1339
 
  memset(safe_args, 0, sizeof(safe_args));
1340
 
 
1341
 
  if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
1342
 
    return true;
1343
 
 
1344
 
  /*
1345
 
    For better error reporting: save the first and the second argument.
1346
 
    We need this only if the the number of args is 3 or 2:
1347
 
    - for a longer argument list, "Illegal mix of collations"
1348
 
      doesn't display each argument's characteristics.
1349
 
    - if nargs is 1, then this error cannot happen.
1350
 
  */
1351
 
  if (nargs >=2 && nargs <= 3)
1352
 
  {
1353
 
    safe_args[0]= args[0];
1354
 
    safe_args[1]= args[item_sep];
1355
 
  }
1356
 
 
1357
 
  Session *session= current_session;
1358
 
  bool res= false;
1359
 
  uint32_t i;
1360
 
 
1361
 
  for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
1362
 
  {
1363
 
    Item* conv;
1364
 
    uint32_t dummy_offset;
1365
 
    if (!String::needs_conversion(0, (*arg)->collation.collation,
1366
 
                                  coll.collation,
1367
 
                                  &dummy_offset))
1368
 
      continue;
1369
 
 
1370
 
    if (!(conv= (*arg)->safe_charset_converter(coll.collation)) &&
1371
 
        ((*arg)->collation.repertoire == MY_REPERTOIRE_ASCII))
1372
 
      conv= new Item_func_conv_charset(*arg, coll.collation, 1);
1373
 
 
1374
 
    if (!conv)
1375
 
    {
1376
 
      if (nargs >=2 && nargs <= 3)
1377
 
      {
1378
 
        /* restore the original arguments for better error message */
1379
 
        args[0]= safe_args[0];
1380
 
        args[item_sep]= safe_args[1];
1381
 
      }
1382
 
      my_coll_agg_error(args, nargs, fname, item_sep);
1383
 
      res= true;
1384
 
      break; // we cannot return here, we need to restore "arena".
1385
 
    }
1386
 
    if ((*arg)->type() == Item::FIELD_ITEM)
1387
 
      ((Item_field *)(*arg))->no_const_subst= 1;
1388
 
    /*
1389
 
      If in statement prepare, then we create a converter for two
1390
 
      constant items, do it once and then reuse it.
1391
 
      If we're in execution of a prepared statement, arena is NULL,
1392
 
      and the conv was created in runtime memory. This can be
1393
 
      the case only if the argument is a parameter marker ('?'),
1394
 
      because for all true constants the charset converter has already
1395
 
      been created in prepare. In this case register the change for
1396
 
      rollback.
1397
 
    */
1398
 
    session->change_item_tree(arg, conv);
1399
 
    /*
1400
 
      We do not check conv->fixed, because Item_func_conv_charset which can
1401
 
      be return by safe_charset_converter can't be fixed at creation
1402
 
    */
1403
 
    conv->fix_fields(session, arg);
1404
 
  }
1405
 
 
1406
 
  return res;
1407
 
}
1408
 
 
1409
 
 
1410
950
void Item_ident_for_show::make_field(Send_field *tmp_field)
1411
951
{
1412
952
  tmp_field->table_name= tmp_field->org_table_name= table_name;