~ubuntu-branches/ubuntu/maverick/mysql-5.1/maverick-proposed

« back to all changes in this revision

Viewing changes to sql/item_func.cc

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-02-22 14:16:05 UTC
  • mto: This revision was merged to the branch mainline in revision 20.
  • Revision ID: package-import@ubuntu.com-20120222141605-nxlu9yzc6attylc2
Tags: upstream-5.1.61
ImportĀ upstreamĀ versionĀ 5.1.61

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright 2000-2008 MySQL AB, 2008 Sun Microsystems, Inc.
 
1
/* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
2
2
 
3
3
   This program is free software; you can redistribute it and/or modify
4
4
   it under the terms of the GNU General Public License as published by
11
11
 
12
12
   You should have received a copy of the GNU General Public License
13
13
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
 
 
 
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
16
15
 
17
16
/**
18
17
  @file
157
156
  used_tables_cache= not_null_tables_cache= 0;
158
157
  const_item_cache=1;
159
158
 
160
 
  if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
 
159
  /*
 
160
    Use stack limit of STACK_MIN_SIZE * 2 since
 
161
    on some platforms a recursive call to fix_fields
 
162
    requires more than STACK_MIN_SIZE bytes (e.g. for
 
163
    MIPS, it takes about 22kB to make one recursive
 
164
    call to Item_func::fix_fields())
 
165
  */
 
166
  if (check_stack_overrun(thd, STACK_MIN_SIZE * 2, buff))
161
167
    return TRUE;                                // Fatal error if flag is set!
162
168
  if (arg_count)
163
169
  {                                             // Print purify happy
475
481
my_decimal *Item_func::val_decimal(my_decimal *decimal_value)
476
482
{
477
483
  DBUG_ASSERT(fixed);
478
 
  int2my_decimal(E_DEC_FATAL_ERROR, val_int(), unsigned_flag, decimal_value);
 
484
  longlong nr= val_int();
 
485
  if (null_value)
 
486
    return 0; /* purecov: inspected */
 
487
  int2my_decimal(E_DEC_FATAL_ERROR, nr, unsigned_flag, decimal_value);
479
488
  return decimal_value;
480
489
}
481
490
 
833
842
      return 0;
834
843
 
835
844
    char *end= (char*) res->ptr() + res->length();
836
 
    CHARSET_INFO *cs= str_value.charset();
 
845
    CHARSET_INFO *cs= res->charset();
837
846
    return (*(cs->cset->strtoll10))(cs, res->ptr(), &end, &err_not_used);
838
847
  }
839
848
  default:
1056
1065
  push_warning_printf(current_thd, MYSQL_ERROR::WARN_LEVEL_ERROR,
1057
1066
                      ER_WARN_DATA_OUT_OF_RANGE,
1058
1067
                      ER(ER_WARN_DATA_OUT_OF_RANGE),
1059
 
                      name, 1);
 
1068
                      name, 1L);
1060
1069
  return dec;
1061
1070
}
1062
1071
 
1322
1331
  {
1323
1332
    decimals=max(args[0]->decimals,args[1]->decimals)+prec_increment;
1324
1333
    set_if_smaller(decimals, NOT_FIXED_DEC);
1325
 
    max_length=args[0]->max_length - args[0]->decimals + decimals;
1326
1334
    uint tmp=float_length(decimals);
1327
 
    set_if_smaller(max_length,tmp);
 
1335
    if (decimals == NOT_FIXED_DEC)
 
1336
      max_length= tmp;
 
1337
    else
 
1338
    {
 
1339
      max_length=args[0]->max_length - args[0]->decimals + decimals;
 
1340
      set_if_smaller(max_length,tmp);
 
1341
    }
1328
1342
    break;
1329
1343
  }
1330
1344
  case INT_RESULT:
1356
1370
    signal_divide_by_null();
1357
1371
    return 0;
1358
1372
  }
1359
 
  return (unsigned_flag ?
1360
 
          (ulonglong) value / (ulonglong) val2 :
1361
 
          value / val2);
 
1373
 
 
1374
  if (unsigned_flag)
 
1375
    return  ((ulonglong) value / (ulonglong) val2);
 
1376
  else if (value == LONGLONG_MIN && val2 == -1)
 
1377
    return LONGLONG_MIN;
 
1378
  else
 
1379
    return value / val2;
1362
1380
}
1363
1381
 
1364
1382
 
1392
1410
  if (args[0]->unsigned_flag)
1393
1411
    result= args[1]->unsigned_flag ? 
1394
1412
      ((ulonglong) value) % ((ulonglong) val2) : ((ulonglong) value) % val2;
1395
 
  else
1396
 
    result= args[1]->unsigned_flag ?
1397
 
      value % ((ulonglong) val2) : value % val2;
 
1413
  else result= args[1]->unsigned_flag ?
 
1414
         value % ((ulonglong) val2) :
 
1415
         (val2 == -1) ? 0 : value % val2;
1398
1416
 
1399
1417
  return result;
1400
1418
}
1787
1805
 
1788
1806
void Item_func_int_val::fix_num_length_and_dec()
1789
1807
{
1790
 
  max_length= args[0]->max_length - (args[0]->decimals ?
1791
 
                                     args[0]->decimals + 1 :
1792
 
                                     0) + 2;
 
1808
  ulonglong tmp_max_length= (ulonglong ) args[0]->max_length - 
 
1809
    (args[0]->decimals ? args[0]->decimals + 1 : 0) + 2;
 
1810
  max_length= tmp_max_length > (ulonglong) max_field_size ?
 
1811
    max_field_size : (uint32) tmp_max_length;
1793
1812
  uint tmp= float_length(decimals);
1794
1813
  set_if_smaller(max_length,tmp);
1795
1814
  decimals= 0;
1953
1972
  }
1954
1973
 
1955
1974
  val1= args[1]->val_int();
 
1975
  if ((null_value= args[1]->is_null()))
 
1976
    return;
 
1977
 
1956
1978
  val1_unsigned= args[1]->unsigned_flag;
1957
1979
  if (val1 < 0)
1958
1980
    decimals_to_set= val1_unsigned ? INT_MAX : 0;
2102
2124
  if (!(null_value= (args[0]->null_value || args[1]->null_value ||
2103
2125
                     my_decimal_round(E_DEC_FATAL_ERROR, value, (int) dec,
2104
2126
                                      truncate, decimal_value) > 1))) 
2105
 
  {
2106
 
    decimal_value->frac= decimals;
2107
2127
    return decimal_value;
2108
 
  }
2109
2128
  return 0;
2110
2129
}
2111
2130
 
2243
2262
    max_length= my_decimal_precision_to_length_no_truncation(max_int_part +
2244
2263
                                                             decimals, decimals,
2245
2264
                                                             unsigned_flag);
 
2265
  else if (cmp_type == REAL_RESULT)
 
2266
    max_length= float_length(decimals);
2246
2267
  cached_field_type= agg_field_type(args, arg_count);
2247
2268
}
2248
2269
 
2261
2282
    stored to the value pointer, if latter is provided.
2262
2283
 
2263
2284
  RETURN
2264
 
   0    If one of arguments is NULL
 
2285
   0    If one of arguments is NULL or there was a execution error
2265
2286
   #    index of the least/greatest argument
2266
2287
*/
2267
2288
 
2275
2296
    Item **arg= args + i;
2276
2297
    bool is_null;
2277
2298
    longlong res= get_datetime_value(thd, &arg, 0, datetime_item, &is_null);
 
2299
 
 
2300
    /* Check if we need to stop (because of error or KILL)  and stop the loop */
 
2301
    if (thd->is_error())
 
2302
    {
 
2303
      null_value= 1;
 
2304
      return 0;
 
2305
    }
 
2306
 
2278
2307
    if ((null_value= args[i]->null_value))
2279
2308
      return 0;
2280
2309
    if (i == 0 || (res < min_max ? cmp_sign : -cmp_sign) > 0)
2303
2332
    if (null_value)
2304
2333
      return 0;
2305
2334
    str_res= args[min_max_idx]->val_str(str);
 
2335
    if (args[min_max_idx]->null_value)
 
2336
    {
 
2337
      // check if the call to val_str() above returns a NULL value
 
2338
      null_value= 1;
 
2339
      return NULL;
 
2340
    }
2306
2341
    str_res->set_charset(collation.collation);
2307
2342
    return str_res;
2308
2343
  }
2818
2853
 
2819
2854
  if (!tmp_udf)
2820
2855
  {
2821
 
    my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str, errno);
 
2856
    my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str);
2822
2857
    DBUG_RETURN(TRUE);
2823
2858
  }
2824
2859
  u_d=tmp_udf;
3808
3843
  maybe_null=args[0]->maybe_null;
3809
3844
  max_length=args[0]->max_length;
3810
3845
  decimals=args[0]->decimals;
 
3846
  unsigned_flag= args[0]->unsigned_flag;
3811
3847
  collation.set(args[0]->collation.collation, DERIVATION_IMPLICIT);
3812
3848
}
3813
3849
 
3897
3933
      length--;                                 // Fix length change above
3898
3934
      entry->value[length]= 0;                  // Store end \0
3899
3935
    }
3900
 
    memcpy(entry->value,ptr,length);
 
3936
    memmove(entry->value, ptr, length);
3901
3937
    if (type == DECIMAL_RESULT)
3902
3938
      ((my_decimal*)entry->value)->fix_buffer_pointer();
3903
3939
    entry->length= length;
4037
4073
    int2my_decimal(E_DEC_FATAL_ERROR, *(longlong*) value, 0, val);
4038
4074
    break;
4039
4075
  case DECIMAL_RESULT:
4040
 
    val= (my_decimal *)value;
 
4076
    my_decimal2decimal((my_decimal *) value, val);
4041
4077
    break;
4042
4078
  case STRING_RESULT:
4043
4079
    str2my_decimal(E_DEC_FATAL_ERROR, value, length, collation.collation, val);
4261
4297
  return entry->val_int(&null_value);
4262
4298
}
4263
4299
 
 
4300
bool Item_func_set_user_var::val_bool_result()
 
4301
{
 
4302
  DBUG_ASSERT(fixed == 1);
 
4303
  check(TRUE);
 
4304
  update();                                     // Store expression
 
4305
  return entry->val_int(&null_value) != 0;
 
4306
}
 
4307
 
4264
4308
String *Item_func_set_user_var::str_result(String *str)
4265
4309
{
4266
4310
  DBUG_ASSERT(fixed == 1);
4638
4682
      decimals=0;
4639
4683
      break;
4640
4684
    case STRING_RESULT:
4641
 
      max_length= MAX_BLOB_WIDTH;
 
4685
      max_length= MAX_BLOB_WIDTH - 1;
4642
4686
      break;
4643
4687
    case DECIMAL_RESULT:
4644
4688
      max_length= DECIMAL_MAX_STR_LENGTH;
4715
4759
bool Item_user_var_as_out_param::fix_fields(THD *thd, Item **ref)
4716
4760
{
4717
4761
  DBUG_ASSERT(fixed == 0);
 
4762
  DBUG_ASSERT(thd->lex->exchange);
4718
4763
  if (Item::fix_fields(thd, ref) ||
4719
4764
      !(entry= get_variable(&thd->user_vars, name, 1)))
4720
4765
    return TRUE;
4724
4769
    of fields in LOAD DATA INFILE.
4725
4770
    (Since Item_user_var_as_out_param is used only there).
4726
4771
  */
4727
 
  entry->collation.set(thd->variables.collation_database);
 
4772
  entry->collation.set(thd->lex->exchange->cs ? 
 
4773
                       thd->lex->exchange->cs :
 
4774
                       thd->variables.collation_database);
4728
4775
  entry->update_query_id= thd->query_id;
4729
4776
  return FALSE;
4730
4777
}
4838
4885
      decimals=0;
4839
4886
      break;
4840
4887
    case SHOW_LONGLONG:
4841
 
      unsigned_flag= FALSE;
 
4888
      unsigned_flag= TRUE;
4842
4889
      max_length= MY_INT64_NUM_DECIMAL_DIGITS;
4843
4890
      decimals=0;
4844
4891
      break;
4979
5026
  {
4980
5027
    case SHOW_INT:      get_sys_var_safe (uint);
4981
5028
    case SHOW_LONG:     get_sys_var_safe (ulong);
4982
 
    case SHOW_LONGLONG: get_sys_var_safe (longlong);
 
5029
    case SHOW_LONGLONG: get_sys_var_safe (ulonglong);
4983
5030
    case SHOW_HA_ROWS:  get_sys_var_safe (ha_rows);
4984
5031
    case SHOW_BOOL:     get_sys_var_safe (bool);
4985
5032
    case SHOW_MY_BOOL:  get_sys_var_safe (my_bool);
5270
5317
 
5271
5318
  /* Check if init_search() has been called before */
5272
5319
  if (ft_handler)
 
5320
  {
 
5321
    /*
 
5322
      We should reset ft_handler as it is cleaned up
 
5323
      on destruction of FT_SELECT object
 
5324
      (necessary in case of re-execution of subquery).
 
5325
      TODO: FT_SELECT should not clean up ft_handler.
 
5326
    */
 
5327
    if (join_key)
 
5328
      table->file->ft_handler= ft_handler;
5273
5329
    DBUG_VOID_RETURN;
 
5330
  }
5274
5331
 
5275
5332
  if (key == NO_SUCH_KEY)
5276
5333
  {
6018
6075
  if (res)
6019
6076
    DBUG_RETURN(res);
6020
6077
 
6021
 
  if (thd->lex->view_prepare_mode)
 
6078
  if (thd->lex->context_analysis_only & CONTEXT_ANALYSIS_ONLY_VIEW)
6022
6079
  {
6023
6080
    /*
6024
6081
      Here we check privileges of the stored routine only during view