2715
2715
return (int64_t) my_count_bits(value);
2719
/****************************************************************************
2720
** Functions to handle dynamic loadable functions
2721
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2722
** Rewritten by monty.
2723
****************************************************************************/
2725
void udf_handler::cleanup()
2731
if (u_d->func_deinit != NULL)
2733
Udf_func_deinit deinit= u_d->func_deinit;
2739
if (buffers) // Because of bug in ecc
2747
udf_handler::fix_fields(THD *thd, Item_result_field *func,
2748
uint arg_count, Item **arguments)
2750
uchar buff[STACK_BUFF_ALLOC]; // Max argument in function
2752
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2753
return(true); // Fatal error flag is set!
2755
udf_func *tmp_udf=find_udf(u_d->name.str,(uint) u_d->name.length);
2759
my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str, errno);
2765
/* Fix all arguments */
2767
used_tables_cache=0;
2770
if ((f_args.arg_count=arg_count))
2772
if (!(f_args.arg_type= (Item_result*)
2773
sql_alloc(f_args.arg_count*sizeof(Item_result))))
2779
Item **arg,**arg_end;
2780
for (i=0, arg=arguments, arg_end=arguments+arg_count;
2784
if (!(*arg)->fixed &&
2785
(*arg)->fix_fields(thd, arg))
2787
// we can't assign 'item' before, because fix_fields() can change arg
2789
if (item->check_cols(1))
2792
TODO: We should think about this. It is not always
2793
right way just to set an UDF result to return my_charset_bin
2794
if one argument has binary sorting order.
2795
The result collation should be calculated according to arguments
2796
derivations in some cases and should not in other cases.
2797
Moreover, some arguments can represent a numeric input
2798
which doesn't effect the result character set and collation.
2799
There is no a general rule for UDF. Everything depends on
2800
the particular user defined function.
2802
if (item->collation.collation->state & MY_CS_BINSORT)
2803
func->collation.set(&my_charset_bin);
2804
if (item->maybe_null)
2806
func->with_sum_func= func->with_sum_func || item->with_sum_func;
2807
used_tables_cache|=item->used_tables();
2808
const_item_cache&=item->const_item();
2809
f_args.arg_type[i]=item->result_type();
2811
//TODO: why all following memory is not allocated with 1 call of sql_alloc?
2812
if (!(buffers=new String[arg_count]) ||
2813
!(f_args.args= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2814
!(f_args.lengths= (ulong*) sql_alloc(arg_count * sizeof(long))) ||
2815
!(f_args.maybe_null= (char*) sql_alloc(arg_count * sizeof(char))) ||
2816
!(num_buffer= (char*) sql_alloc(arg_count *
2817
ALIGN_SIZE(sizeof(double)))) ||
2818
!(f_args.attributes= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2819
!(f_args.attribute_lengths= (ulong*) sql_alloc(arg_count *
2825
func->fix_length_and_dec();
2826
initid.max_length=func->max_length;
2827
initid.maybe_null=func->maybe_null;
2828
initid.const_item=const_item_cache;
2829
initid.decimals=func->decimals;
2834
char init_msg_buff[DRIZZLE_ERRMSG_SIZE];
2835
char *to=num_buffer;
2836
for (uint i=0; i < arg_count; i++)
2839
For a constant argument i, args->args[i] points to the argument value.
2840
For non-constant, args->args[i] is NULL.
2842
f_args.args[i]= NULL; /* Non-const unless updated below. */
2844
f_args.lengths[i]= arguments[i]->max_length;
2845
f_args.maybe_null[i]= (char) arguments[i]->maybe_null;
2846
f_args.attributes[i]= arguments[i]->name;
2847
f_args.attribute_lengths[i]= arguments[i]->name_length;
2849
if (arguments[i]->const_item())
2851
switch (arguments[i]->result_type())
2854
case DECIMAL_RESULT:
2856
String *res= arguments[i]->val_str(&buffers[i]);
2857
if (arguments[i]->null_value)
2859
f_args.args[i]= (char*) res->c_ptr();
2860
f_args.lengths[i]= res->length();
2864
*((int64_t*) to)= arguments[i]->val_int();
2865
if (arguments[i]->null_value)
2868
to+= ALIGN_SIZE(sizeof(int64_t));
2871
*((double*) to)= arguments[i]->val_real();
2872
if (arguments[i]->null_value)
2875
to+= ALIGN_SIZE(sizeof(double));
2879
// This case should never be chosen
2885
Udf_func_init init= u_d->func_init;
2886
if ((error=(uchar) init(&initid, &f_args, init_msg_buff)))
2888
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2889
u_d->name.str, init_msg_buff);
2892
func->max_length=min(initid.max_length,(unsigned long)MAX_BLOB_WIDTH);
2893
func->maybe_null=initid.maybe_null;
2894
const_item_cache=initid.const_item;
2896
Keep used_tables_cache in sync with const_item_cache.
2897
See the comment in Item_udf_func::update_used tables.
2899
if (!const_item_cache && !used_tables_cache)
2900
used_tables_cache= RAND_TABLE_BIT;
2901
func->decimals=min(initid.decimals,(unsigned int)NOT_FIXED_DEC);
2906
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2907
u_d->name.str, ER(ER_UNKNOWN_ERROR));
2914
bool udf_handler::get_arguments()
2917
return 1; // Got an error earlier
2918
char *to= num_buffer;
2920
for (uint i=0; i < f_args.arg_count; i++)
2923
switch (f_args.arg_type[i]) {
2925
case DECIMAL_RESULT:
2927
String *res=args[i]->val_str(&buffers[str_count++]);
2928
if (!(args[i]->null_value))
2930
f_args.args[i]= (char*) res->ptr();
2931
f_args.lengths[i]= res->length();
2936
*((int64_t*) to) = args[i]->val_int();
2937
if (!args[i]->null_value)
2940
to+= ALIGN_SIZE(sizeof(int64_t));
2944
*((double*) to)= args[i]->val_real();
2945
if (!args[i]->null_value)
2948
to+= ALIGN_SIZE(sizeof(double));
2953
// This case should never be chosen
2963
(String*)NULL in case of NULL values
2965
String *udf_handler::val_str(String *str,String *save_str)
2967
uchar is_null_tmp=0;
2970
if (get_arguments())
2972
char * (*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
2973
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
2976
if ((res_length=str->alloced_length()) < MAX_FIELD_WIDTH)
2977
{ // This happens VERY seldom
2978
if (str->alloc(MAX_FIELD_WIDTH))
2984
char *res=func(&initid, &f_args, (char*) str->ptr(), &res_length,
2985
&is_null_tmp, &error);
2986
if (is_null_tmp || !res || error) // The !res is for safety
2990
if (res == str->ptr())
2992
str->length(res_length);
2995
save_str->set(res, res_length, str->charset());
3001
For the moment, UDF functions are returning DECIMAL values as strings
3004
my_decimal *udf_handler::val_decimal(bool *null_value, my_decimal *dec_buf)
3006
char buf[DECIMAL_MAX_STR_LENGTH+1], *end;
3007
ulong res_length= DECIMAL_MAX_STR_LENGTH;
3009
if (get_arguments())
3014
char *(*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
3015
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
3018
char *res= func(&initid, &f_args, buf, &res_length, &is_null, &error);
3019
if (is_null || error)
3024
end= res+ res_length;
3025
str2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf, &end);
3030
void Item_udf_func::cleanup()
3033
Item_func::cleanup();
3037
void Item_udf_func::print(String *str, enum_query_type query_type)
3039
str->append(func_name());
3041
for (uint i=0 ; i < arg_count ; i++)
3045
args[i]->print_item_w_name(str, query_type);
3051
double Item_func_udf_float::val_real()
3054
return(udf.val(&null_value));
3058
String *Item_func_udf_float::val_str(String *str)
3061
double nr= val_real();
3063
return 0; /* purecov: inspected */
3064
str->set_real(nr,decimals,&my_charset_bin);
3069
int64_t Item_func_udf_int::val_int()
3072
return(udf.val_int(&null_value));
3076
String *Item_func_udf_int::val_str(String *str)
3079
int64_t nr=val_int();
3082
str->set_int(nr, unsigned_flag, &my_charset_bin);
3087
int64_t Item_func_udf_decimal::val_int()
3089
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3093
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
3098
double Item_func_udf_decimal::val_real()
3100
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3104
my_decimal2double(E_DEC_FATAL_ERROR, dec, &result);
3109
my_decimal *Item_func_udf_decimal::val_decimal(my_decimal *dec_buf)
3112
return(udf.val_decimal(&null_value, dec_buf));
3116
String *Item_func_udf_decimal::val_str(String *str)
3118
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3121
if (str->length() < DECIMAL_MAX_STR_LENGTH)
3122
str->length(DECIMAL_MAX_STR_LENGTH);
3123
my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
3124
my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, '0', str);
3129
void Item_func_udf_decimal::fix_length_and_dec()
3131
fix_num_length_and_dec();
3135
/* Default max_length is max argument length */
3137
void Item_func_udf_str::fix_length_and_dec()
3140
for (uint i = 0; i < arg_count; i++)
3141
set_if_bigger(max_length,args[i]->max_length);
3145
String *Item_func_udf_str::val_str(String *str)
3148
String *res=udf.val_str(str,&str_value);
3156
This has to come last in the udf_handler methods, or C for AIX
3157
version 6.0.0.0 fails to compile with debugging enabled. (Yes, really.)
3160
udf_handler::~udf_handler()
3162
/* Everything should be properly cleaned up by this moment. */
3163
assert(not_original || !(initialized || buffers));
3167
2719
** User level locks