2711
2734
return (int64_t) my_count_bits(value);
2738
/****************************************************************************
2739
** Functions to handle dynamic loadable functions
2740
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2741
** Rewritten by monty.
2742
****************************************************************************/
2744
void udf_handler::cleanup()
2750
if (u_d->func_deinit != NULL)
2752
Udf_func_deinit deinit= u_d->func_deinit;
2758
if (buffers) // Because of bug in ecc
2766
udf_handler::fix_fields(THD *thd, Item_result_field *func,
2767
uint arg_count, Item **arguments)
2769
uchar buff[STACK_BUFF_ALLOC]; // Max argument in function
2771
if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2772
return(true); // Fatal error flag is set!
2774
udf_func *tmp_udf=find_udf(u_d->name.str,(uint) u_d->name.length);
2778
my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str, errno);
2784
/* Fix all arguments */
2786
used_tables_cache=0;
2789
if ((f_args.arg_count=arg_count))
2791
if (!(f_args.arg_type= (Item_result*)
2792
sql_alloc(f_args.arg_count*sizeof(Item_result))))
2798
Item **arg,**arg_end;
2799
for (i=0, arg=arguments, arg_end=arguments+arg_count;
2803
if (!(*arg)->fixed &&
2804
(*arg)->fix_fields(thd, arg))
2806
// we can't assign 'item' before, because fix_fields() can change arg
2808
if (item->check_cols(1))
2811
TODO: We should think about this. It is not always
2812
right way just to set an UDF result to return my_charset_bin
2813
if one argument has binary sorting order.
2814
The result collation should be calculated according to arguments
2815
derivations in some cases and should not in other cases.
2816
Moreover, some arguments can represent a numeric input
2817
which doesn't effect the result character set and collation.
2818
There is no a general rule for UDF. Everything depends on
2819
the particular user defined function.
2821
if (item->collation.collation->state & MY_CS_BINSORT)
2822
func->collation.set(&my_charset_bin);
2823
if (item->maybe_null)
2825
func->with_sum_func= func->with_sum_func || item->with_sum_func;
2826
used_tables_cache|=item->used_tables();
2827
const_item_cache&=item->const_item();
2828
f_args.arg_type[i]=item->result_type();
2830
//TODO: why all following memory is not allocated with 1 call of sql_alloc?
2831
if (!(buffers=new String[arg_count]) ||
2832
!(f_args.args= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2833
!(f_args.lengths= (ulong*) sql_alloc(arg_count * sizeof(long))) ||
2834
!(f_args.maybe_null= (char*) sql_alloc(arg_count * sizeof(char))) ||
2835
!(num_buffer= (char*) sql_alloc(arg_count *
2836
ALIGN_SIZE(sizeof(double)))) ||
2837
!(f_args.attributes= (char**) sql_alloc(arg_count * sizeof(char *))) ||
2838
!(f_args.attribute_lengths= (ulong*) sql_alloc(arg_count *
2844
func->fix_length_and_dec();
2845
initid.max_length=func->max_length;
2846
initid.maybe_null=func->maybe_null;
2847
initid.const_item=const_item_cache;
2848
initid.decimals=func->decimals;
2853
char init_msg_buff[DRIZZLE_ERRMSG_SIZE];
2854
char *to=num_buffer;
2855
for (uint i=0; i < arg_count; i++)
2858
For a constant argument i, args->args[i] points to the argument value.
2859
For non-constant, args->args[i] is NULL.
2861
f_args.args[i]= NULL; /* Non-const unless updated below. */
2863
f_args.lengths[i]= arguments[i]->max_length;
2864
f_args.maybe_null[i]= (char) arguments[i]->maybe_null;
2865
f_args.attributes[i]= arguments[i]->name;
2866
f_args.attribute_lengths[i]= arguments[i]->name_length;
2868
if (arguments[i]->const_item())
2870
switch (arguments[i]->result_type())
2873
case DECIMAL_RESULT:
2875
String *res= arguments[i]->val_str(&buffers[i]);
2876
if (arguments[i]->null_value)
2878
f_args.args[i]= (char*) res->c_ptr();
2879
f_args.lengths[i]= res->length();
2883
*((int64_t*) to)= arguments[i]->val_int();
2884
if (arguments[i]->null_value)
2887
to+= ALIGN_SIZE(sizeof(int64_t));
2890
*((double*) to)= arguments[i]->val_real();
2891
if (arguments[i]->null_value)
2894
to+= ALIGN_SIZE(sizeof(double));
2898
// This case should never be chosen
2904
Udf_func_init init= u_d->func_init;
2905
if ((error=(uchar) init(&initid, &f_args, init_msg_buff)))
2907
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2908
u_d->name.str, init_msg_buff);
2911
func->max_length=min(initid.max_length,MAX_BLOB_WIDTH);
2912
func->maybe_null=initid.maybe_null;
2913
const_item_cache=initid.const_item;
2915
Keep used_tables_cache in sync with const_item_cache.
2916
See the comment in Item_udf_func::update_used tables.
2918
if (!const_item_cache && !used_tables_cache)
2919
used_tables_cache= RAND_TABLE_BIT;
2920
func->decimals=min(initid.decimals,NOT_FIXED_DEC);
2925
my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2926
u_d->name.str, ER(ER_UNKNOWN_ERROR));
2933
bool udf_handler::get_arguments()
2936
return 1; // Got an error earlier
2937
char *to= num_buffer;
2939
for (uint i=0; i < f_args.arg_count; i++)
2942
switch (f_args.arg_type[i]) {
2944
case DECIMAL_RESULT:
2946
String *res=args[i]->val_str(&buffers[str_count++]);
2947
if (!(args[i]->null_value))
2949
f_args.args[i]= (char*) res->ptr();
2950
f_args.lengths[i]= res->length();
2955
*((int64_t*) to) = args[i]->val_int();
2956
if (!args[i]->null_value)
2959
to+= ALIGN_SIZE(sizeof(int64_t));
2963
*((double*) to)= args[i]->val_real();
2964
if (!args[i]->null_value)
2967
to+= ALIGN_SIZE(sizeof(double));
2972
// This case should never be chosen
2982
(String*)NULL in case of NULL values
2984
String *udf_handler::val_str(String *str,String *save_str)
2986
uchar is_null_tmp=0;
2989
if (get_arguments())
2991
char * (*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
2992
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
2995
if ((res_length=str->alloced_length()) < MAX_FIELD_WIDTH)
2996
{ // This happens VERY seldom
2997
if (str->alloc(MAX_FIELD_WIDTH))
3003
char *res=func(&initid, &f_args, (char*) str->ptr(), &res_length,
3004
&is_null_tmp, &error);
3005
if (is_null_tmp || !res || error) // The !res is for safety
3009
if (res == str->ptr())
3011
str->length(res_length);
3014
save_str->set(res, res_length, str->charset());
3020
For the moment, UDF functions are returning DECIMAL values as strings
3023
my_decimal *udf_handler::val_decimal(my_bool *null_value, my_decimal *dec_buf)
3025
char buf[DECIMAL_MAX_STR_LENGTH+1], *end;
3026
ulong res_length= DECIMAL_MAX_STR_LENGTH;
3028
if (get_arguments())
3033
char *(*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
3034
(char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
3037
char *res= func(&initid, &f_args, buf, &res_length, &is_null, &error);
3038
if (is_null || error)
3043
end= res+ res_length;
3044
str2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf, &end);
3049
void Item_udf_func::cleanup()
3052
Item_func::cleanup();
3056
void Item_udf_func::print(String *str, enum_query_type query_type)
3058
str->append(func_name());
3060
for (uint i=0 ; i < arg_count ; i++)
3064
args[i]->print_item_w_name(str, query_type);
3070
double Item_func_udf_float::val_real()
3073
return(udf.val(&null_value));
3077
String *Item_func_udf_float::val_str(String *str)
3080
double nr= val_real();
3082
return 0; /* purecov: inspected */
3083
str->set_real(nr,decimals,&my_charset_bin);
3088
int64_t Item_func_udf_int::val_int()
3091
return(udf.val_int(&null_value));
3095
String *Item_func_udf_int::val_str(String *str)
3098
int64_t nr=val_int();
3101
str->set_int(nr, unsigned_flag, &my_charset_bin);
3106
int64_t Item_func_udf_decimal::val_int()
3108
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3112
my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
3117
double Item_func_udf_decimal::val_real()
3119
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3123
my_decimal2double(E_DEC_FATAL_ERROR, dec, &result);
3128
my_decimal *Item_func_udf_decimal::val_decimal(my_decimal *dec_buf)
3131
return(udf.val_decimal(&null_value, dec_buf));
3135
String *Item_func_udf_decimal::val_str(String *str)
3137
my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3140
if (str->length() < DECIMAL_MAX_STR_LENGTH)
3141
str->length(DECIMAL_MAX_STR_LENGTH);
3142
my_decimal_round(E_DEC_FATAL_ERROR, dec, decimals, false, &dec_buf);
3143
my_decimal2string(E_DEC_FATAL_ERROR, &dec_buf, 0, 0, '0', str);
3148
void Item_func_udf_decimal::fix_length_and_dec()
3150
fix_num_length_and_dec();
3154
/* Default max_length is max argument length */
3156
void Item_func_udf_str::fix_length_and_dec()
3159
for (uint i = 0; i < arg_count; i++)
3160
set_if_bigger(max_length,args[i]->max_length);
3164
String *Item_func_udf_str::val_str(String *str)
3167
String *res=udf.val_str(str,&str_value);
3175
This has to come last in the udf_handler methods, or C for AIX
3176
version 6.0.0.0 fails to compile with debugging enabled. (Yes, really.)
3179
udf_handler::~udf_handler()
3181
/* Everything should be properly cleaned up by this moment. */
3182
assert(not_original || !(initialized || buffers));
2715
3186
** User level locks