2711
2741
return (int64_t) my_count_bits(value);
2745
/****************************************************************************
2746
** Functions to handle dynamic loadable functions
2747
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2748
** Rewritten by monty.
2749
****************************************************************************/
2751
void udf_handler::cleanup()
2757
if (u_d->func_deinit != NULL)
2759
Udf_func_deinit deinit= u_d->func_deinit;
2765
if (buffers) // Because of bug in ecc
2773
udf_handler::fix_fields(THD *thd, Item_result_field *func,
2774
uint arg_count, Item **arguments)
2776
uchar buff[STACK_BUFF_ALLOC]; // Max argument in function
2778
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2779
return(true); // Fatal error flag is set!
2781
udf_func *tmp_udf=find_udf(u_d->name.str,(uint) u_d->name.length);
2785
my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str, errno);
2791
/* Fix all arguments */
2793
used_tables_cache=0;
2796
if ((f_args.arg_count=arg_count))
2798
if (!(f_args.arg_type= (Item_result*)
2799
sql_alloc(f_args.arg_count*sizeof(Item_result))))
2805
Item **arg,**arg_end;
2806
for (i=0, arg=arguments, arg_end=arguments+arg_count;
2810
if (!(*arg)->fixed &&
2811
(*arg)->fix_fields(thd, arg))
2813
// we can't assign 'item' before, because fix_fields() can change arg
2815
if (item->check_cols(1))
2818
TODO: We should think about this. It is not always
2819
right way just to set an UDF result to return my_charset_bin
2820
if one argument has binary sorting order.
2821
The result collation should be calculated according to arguments
2822
derivations in some cases and should not in other cases.
2823
Moreover, some arguments can represent a numeric input
2824
which doesn't effect the result character set and collation.
2825
There is no a general rule for UDF. Everything depends on
2826
the particular user defined function.
2828
if (item->collation.collation->state & MY_CS_BINSORT)
2829
func->collation.set(&my_charset_bin);
2830
if (item->maybe_null)
2832
func->with_sum_func= func->with_sum_func || item->with_sum_func;
2833
used_tables_cache|=item->used_tables();
2834
const_item_cache&=item->const_item();
2835
f_args.arg_type[i]=item->result_type();
2837
//TODO: why all following memory is not allocated with 1 call of sql_alloc?
2838
if (!(buffers=new String[arg_count]) ||
2839
!(f_args.args= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2840
!(f_args.lengths= (ulong*) sql_alloc(arg_count * sizeof(long))) ||
2841
!(f_args.maybe_null= (char*) sql_alloc(arg_count * sizeof(char))) ||
2842
!(num_buffer= (char*) sql_alloc(arg_count *
2843
ALIGN_SIZE(sizeof(double)))) ||
2844
!(f_args.attributes= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2845
!(f_args.attribute_lengths= (ulong*) sql_alloc(arg_count *
2851
func->fix_length_and_dec();
2852
initid.max_length=func->max_length;
2853
initid.maybe_null=func->maybe_null;
2854
initid.const_item=const_item_cache;
2855
initid.decimals=func->decimals;
2860
char init_msg_buff[MYSQL_ERRMSG_SIZE];
2861
char *to=num_buffer;
2862
for (uint i=0; i < arg_count; i++)
2865
For a constant argument i, args->args[i] points to the argument value.
2866
For non-constant, args->args[i] is NULL.
2868
f_args.args[i]= NULL; /* Non-const unless updated below. */
2870
f_args.lengths[i]= arguments[i]->max_length;
2871
f_args.maybe_null[i]= (char) arguments[i]->maybe_null;
2872
f_args.attributes[i]= arguments[i]->name;
2873
f_args.attribute_lengths[i]= arguments[i]->name_length;
2875
if (arguments[i]->const_item())
2877
switch (arguments[i]->result_type())
2880
case DECIMAL_RESULT:
2882
String *res= arguments[i]->val_str(&buffers[i]);
2883
if (arguments[i]->null_value)
2885
f_args.args[i]= (char*) res->c_ptr();
2886
f_args.lengths[i]= res->length();
2890
*((int64_t*) to)= arguments[i]->val_int();
2891
if (arguments[i]->null_value)
2894
to+= ALIGN_SIZE(sizeof(int64_t));
2897
*((double*) to)= arguments[i]->val_real();
2898
if (arguments[i]->null_value)
2901
to+= ALIGN_SIZE(sizeof(double));
2905
// This case should never be chosen
2911
Udf_func_init init= u_d->func_init;
2912
if ((error=(uchar) init(&initid, &f_args, init_msg_buff)))
2914
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2915
u_d->name.str, init_msg_buff);
2918
func->max_length=min(initid.max_length,MAX_BLOB_WIDTH);
2919
func->maybe_null=initid.maybe_null;
2920
const_item_cache=initid.const_item;
2922
Keep used_tables_cache in sync with const_item_cache.
2923
See the comment in Item_udf_func::update_used tables.
2925
if (!const_item_cache && !used_tables_cache)
2926
used_tables_cache= RAND_TABLE_BIT;
2927
func->decimals=min(initid.decimals,NOT_FIXED_DEC);
2932
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2933
u_d->name.str, ER(ER_UNKNOWN_ERROR));
2940
bool udf_handler::get_arguments()
2943
return 1; // Got an error earlier
2944
char *to= num_buffer;
2946
for (uint i=0; i < f_args.arg_count; i++)
2949
switch (f_args.arg_type[i]) {
2951
case DECIMAL_RESULT:
2953
String *res=args[i]->val_str(&buffers[str_count++]);
2954
if (!(args[i]->null_value))
2956
f_args.args[i]= (char*) res->ptr();
2957
f_args.lengths[i]= res->length();
2962
*((int64_t*) to) = args[i]->val_int();
2963
if (!args[i]->null_value)
2966
to+= ALIGN_SIZE(sizeof(int64_t));
2970
*((double*) to)= args[i]->val_real();
2971
if (!args[i]->null_value)
2974
to+= ALIGN_SIZE(sizeof(double));
2979
// This case should never be chosen
2989
(String*)NULL in case of NULL values
2991
String *udf_handler::val_str(String *str,String *save_str)
2993
uchar is_null_tmp=0;
2996
if (get_arguments())
2998
char * (*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
2999
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
3002
if ((res_length=str->alloced_length()) < MAX_FIELD_WIDTH)
3003
{ // This happens VERY seldom
3004
if (str->alloc(MAX_FIELD_WIDTH))
3010
char *res=func(&initid, &f_args, (char*) str->ptr(), &res_length,
3011
&is_null_tmp, &error);
3012
if (is_null_tmp || !res || error) // The !res is for safety
3016
if (res == str->ptr())
3018
str->length(res_length);
3021
save_str->set(res, res_length, str->charset());
3027
For the moment, UDF functions are returning DECIMAL values as strings
3030
my_decimal *udf_handler::val_decimal(my_bool *null_value, my_decimal *dec_buf)
3032
char buf[DECIMAL_MAX_STR_LENGTH+1], *end;
3033
ulong res_length= DECIMAL_MAX_STR_LENGTH;
3035
if (get_arguments())
3040
char *(*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
3041
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
3044
char *res= func(&initid, &f_args, buf, &res_length, &is_null, &error);
3045
if (is_null || error)
3050
end= res+ res_length;
3051
str2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf, &end);
3056
void Item_udf_func::cleanup()
3059
Item_func::cleanup();
3063
void Item_udf_func::print(String *str, enum_query_type query_type)
3065
str->append(func_name());
3067
for (uint i=0 ; i < arg_count ; i++)
3071
args[i]->print_item_w_name(str, query_type);
3077
double Item_func_udf_float::val_real()
3080
return(udf.val(&null_value));
3084
String *Item_func_udf_float::val_str(String *str)
3087
double nr= val_real();
3089
return 0; /* purecov: inspected */
3090
str->set_real(nr,decimals,&my_charset_bin);
3095
int64_t Item_func_udf_int::val_int()
3098
return(udf.val_int(&null_value));
3102
String *Item_func_udf_int::val_str(String *str)
3105
int64_t nr=val_int();
3108
str->set_int(nr, unsigned_flag, &my_charset_bin);
3113
int64_t Item_func_udf_decimal::val_int()
3115
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3119
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
3124
double Item_func_udf_decimal::val_real()
3126
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3130
my_decimal2double(E_DEC_FATAL_ERROR, dec, &result);
3135
my_decimal *Item_func_udf_decimal::val_decimal(my_decimal *dec_buf)
3138
return(udf.val_decimal(&null_value, dec_buf));
3142
String *Item_func_udf_decimal::val_str(String *str)
3144
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3147
if (str->length() < DECIMAL_MAX_STR_LENGTH)
3148
str->length(DECIMAL_MAX_STR_LENGTH);
3149
my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
3150
my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, '0', str);
3155
void Item_func_udf_decimal::fix_length_and_dec()
3157
fix_num_length_and_dec();
3161
/* Default max_length is max argument length */
3163
void Item_func_udf_str::fix_length_and_dec()
3166
for (uint i = 0; i < arg_count; i++)
3167
set_if_bigger(max_length,args[i]->max_length);
3171
String *Item_func_udf_str::val_str(String *str)
3174
String *res=udf.val_str(str,&str_value);
3182
This has to come last in the udf_handler methods, or C for AIX
3183
version 6.0.0.0 fails to compile with debugging enabled. (Yes, really.)
3186
udf_handler::~udf_handler()
3188
/* Everything should be properly cleaned up by this moment. */
3189
assert(not_original || !(initialized || buffers));
2715
3193
** User level locks