2711
2742
return (int64_t) my_count_bits(value);
2746
/****************************************************************************
2747
** Functions to handle dynamic loadable functions
2748
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2749
** Rewritten by monty.
2750
****************************************************************************/
2752
void udf_handler::cleanup()
2758
if (u_d->func_deinit != NULL)
2760
Udf_func_deinit deinit= u_d->func_deinit;
2766
if (buffers) // Because of bug in ecc
2774
udf_handler::fix_fields(THD *thd, Item_result_field *func,
2775
uint arg_count, Item **arguments)
2777
uchar buff[STACK_BUFF_ALLOC]; // Max argument in function
2779
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2780
return(true); // Fatal error flag is set!
2782
udf_func *tmp_udf=find_udf(u_d->name.str,(uint) u_d->name.length);
2786
my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str, errno);
2792
/* Fix all arguments */
2794
used_tables_cache=0;
2797
if ((f_args.arg_count=arg_count))
2799
if (!(f_args.arg_type= (Item_result*)
2800
sql_alloc(f_args.arg_count*sizeof(Item_result))))
2806
Item **arg,**arg_end;
2807
for (i=0, arg=arguments, arg_end=arguments+arg_count;
2811
if (!(*arg)->fixed &&
2812
(*arg)->fix_fields(thd, arg))
2814
// we can't assign 'item' before, because fix_fields() can change arg
2816
if (item->check_cols(1))
2819
TODO: We should think about this. It is not always
2820
right way just to set an UDF result to return my_charset_bin
2821
if one argument has binary sorting order.
2822
The result collation should be calculated according to arguments
2823
derivations in some cases and should not in other cases.
2824
Moreover, some arguments can represent a numeric input
2825
which doesn't effect the result character set and collation.
2826
There is no a general rule for UDF. Everything depends on
2827
the particular user defined function.
2829
if (item->collation.collation->state & MY_CS_BINSORT)
2830
func->collation.set(&my_charset_bin);
2831
if (item->maybe_null)
2833
func->with_sum_func= func->with_sum_func || item->with_sum_func;
2834
used_tables_cache|=item->used_tables();
2835
const_item_cache&=item->const_item();
2836
f_args.arg_type[i]=item->result_type();
2838
//TODO: why all following memory is not allocated with 1 call of sql_alloc?
2839
if (!(buffers=new String[arg_count]) ||
2840
!(f_args.args= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2841
!(f_args.lengths= (ulong*) sql_alloc(arg_count * sizeof(long))) ||
2842
!(f_args.maybe_null= (char*) sql_alloc(arg_count * sizeof(char))) ||
2843
!(num_buffer= (char*) sql_alloc(arg_count *
2844
ALIGN_SIZE(sizeof(double)))) ||
2845
!(f_args.attributes= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2846
!(f_args.attribute_lengths= (ulong*) sql_alloc(arg_count *
2852
func->fix_length_and_dec();
2853
initid.max_length=func->max_length;
2854
initid.maybe_null=func->maybe_null;
2855
initid.const_item=const_item_cache;
2856
initid.decimals=func->decimals;
2861
char init_msg_buff[MYSQL_ERRMSG_SIZE];
2862
char *to=num_buffer;
2863
for (uint i=0; i < arg_count; i++)
2866
For a constant argument i, args->args[i] points to the argument value.
2867
For non-constant, args->args[i] is NULL.
2869
f_args.args[i]= NULL; /* Non-const unless updated below. */
2871
f_args.lengths[i]= arguments[i]->max_length;
2872
f_args.maybe_null[i]= (char) arguments[i]->maybe_null;
2873
f_args.attributes[i]= arguments[i]->name;
2874
f_args.attribute_lengths[i]= arguments[i]->name_length;
2876
if (arguments[i]->const_item())
2878
switch (arguments[i]->result_type())
2881
case DECIMAL_RESULT:
2883
String *res= arguments[i]->val_str(&buffers[i]);
2884
if (arguments[i]->null_value)
2886
f_args.args[i]= (char*) res->c_ptr();
2887
f_args.lengths[i]= res->length();
2891
*((int64_t*) to)= arguments[i]->val_int();
2892
if (arguments[i]->null_value)
2895
to+= ALIGN_SIZE(sizeof(int64_t));
2898
*((double*) to)= arguments[i]->val_real();
2899
if (arguments[i]->null_value)
2902
to+= ALIGN_SIZE(sizeof(double));
2906
// This case should never be chosen
2912
Udf_func_init init= u_d->func_init;
2913
if ((error=(uchar) init(&initid, &f_args, init_msg_buff)))
2915
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2916
u_d->name.str, init_msg_buff);
2919
func->max_length=min(initid.max_length,MAX_BLOB_WIDTH);
2920
func->maybe_null=initid.maybe_null;
2921
const_item_cache=initid.const_item;
2923
Keep used_tables_cache in sync with const_item_cache.
2924
See the comment in Item_udf_func::update_used tables.
2926
if (!const_item_cache && !used_tables_cache)
2927
used_tables_cache= RAND_TABLE_BIT;
2928
func->decimals=min(initid.decimals,NOT_FIXED_DEC);
2933
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2934
u_d->name.str, ER(ER_UNKNOWN_ERROR));
2941
bool udf_handler::get_arguments()
2944
return 1; // Got an error earlier
2945
char *to= num_buffer;
2947
for (uint i=0; i < f_args.arg_count; i++)
2950
switch (f_args.arg_type[i]) {
2952
case DECIMAL_RESULT:
2954
String *res=args[i]->val_str(&buffers[str_count++]);
2955
if (!(args[i]->null_value))
2957
f_args.args[i]= (char*) res->ptr();
2958
f_args.lengths[i]= res->length();
2963
*((int64_t*) to) = args[i]->val_int();
2964
if (!args[i]->null_value)
2967
to+= ALIGN_SIZE(sizeof(int64_t));
2971
*((double*) to)= args[i]->val_real();
2972
if (!args[i]->null_value)
2975
to+= ALIGN_SIZE(sizeof(double));
2980
// This case should never be chosen
2990
(String*)NULL in case of NULL values
2992
String *udf_handler::val_str(String *str,String *save_str)
2994
uchar is_null_tmp=0;
2997
if (get_arguments())
2999
char * (*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
3000
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
3003
if ((res_length=str->alloced_length()) < MAX_FIELD_WIDTH)
3004
{ // This happens VERY seldom
3005
if (str->alloc(MAX_FIELD_WIDTH))
3011
char *res=func(&initid, &f_args, (char*) str->ptr(), &res_length,
3012
&is_null_tmp, &error);
3013
if (is_null_tmp || !res || error) // The !res is for safety
3017
if (res == str->ptr())
3019
str->length(res_length);
3022
save_str->set(res, res_length, str->charset());
3028
For the moment, UDF functions are returning DECIMAL values as strings
3031
my_decimal *udf_handler::val_decimal(my_bool *null_value, my_decimal *dec_buf)
3033
char buf[DECIMAL_MAX_STR_LENGTH+1], *end;
3034
ulong res_length= DECIMAL_MAX_STR_LENGTH;
3036
if (get_arguments())
3041
char *(*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
3042
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
3045
char *res= func(&initid, &f_args, buf, &res_length, &is_null, &error);
3046
if (is_null || error)
3051
end= res+ res_length;
3052
str2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf, &end);
3057
void Item_udf_func::cleanup()
3060
Item_func::cleanup();
3064
void Item_udf_func::print(String *str, enum_query_type query_type)
3066
str->append(func_name());
3068
for (uint i=0 ; i < arg_count ; i++)
3072
args[i]->print_item_w_name(str, query_type);
3078
double Item_func_udf_float::val_real()
3081
return(udf.val(&null_value));
3085
String *Item_func_udf_float::val_str(String *str)
3088
double nr= val_real();
3090
return 0; /* purecov: inspected */
3091
str->set_real(nr,decimals,&my_charset_bin);
3096
int64_t Item_func_udf_int::val_int()
3099
return(udf.val_int(&null_value));
3103
String *Item_func_udf_int::val_str(String *str)
3106
int64_t nr=val_int();
3109
str->set_int(nr, unsigned_flag, &my_charset_bin);
3114
int64_t Item_func_udf_decimal::val_int()
3116
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3120
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
3125
double Item_func_udf_decimal::val_real()
3127
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3131
my_decimal2double(E_DEC_FATAL_ERROR, dec, &result);
3136
my_decimal *Item_func_udf_decimal::val_decimal(my_decimal *dec_buf)
3139
return(udf.val_decimal(&null_value, dec_buf));
3143
String *Item_func_udf_decimal::val_str(String *str)
3145
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3148
if (str->length() < DECIMAL_MAX_STR_LENGTH)
3149
str->length(DECIMAL_MAX_STR_LENGTH);
3150
my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
3151
my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, '0', str);
3156
void Item_func_udf_decimal::fix_length_and_dec()
3158
fix_num_length_and_dec();
3162
/* Default max_length is max argument length */
3164
void Item_func_udf_str::fix_length_and_dec()
3167
for (uint i = 0; i < arg_count; i++)
3168
set_if_bigger(max_length,args[i]->max_length);
3172
String *Item_func_udf_str::val_str(String *str)
3175
String *res=udf.val_str(str,&str_value);
3183
This has to come last in the udf_handler methods, or C for AIX
3184
version 6.0.0.0 fails to compile with debugging enabled. (Yes, really.)
3187
udf_handler::~udf_handler()
3189
/* Everything should be properly cleaned up by this moment. */
3190
assert(not_original || !(initialized || buffers));
2715
3194
** User level locks