~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/item_func.cc

  • Committer: Stewart Smith
  • Date: 2008-09-25 08:57:27 UTC
  • mto: This revision was merged to the branch mainline in revision 408.
  • Revision ID: stewart@flamingspork.com-20080925085727-lwemz5gl6tvrp9ku
UDFs are now normal Item_func objects. Simplifies handling them a lot.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2715
2715
  return (int64_t) my_count_bits(value);
2716
2716
}
2717
2717
 
2718
 
 
2719
 
/****************************************************************************
2720
 
** Functions to handle dynamic loadable functions
2721
 
** Original source by: Alexis Mikhailov <root@medinf.chuvashia.su>
2722
 
** Rewritten by monty.
2723
 
****************************************************************************/
2724
 
 
2725
 
void udf_handler::cleanup()
2726
 
{
2727
 
  if (!not_original)
2728
 
  {
2729
 
    if (initialized)
2730
 
    {
2731
 
      if (u_d->func_deinit != NULL)
2732
 
      {
2733
 
        Udf_func_deinit deinit= u_d->func_deinit;
2734
 
        (*deinit)(&initid);
2735
 
      }
2736
 
 
2737
 
      initialized= false;
2738
 
    }
2739
 
    if (buffers)                                // Because of bug in ecc
2740
 
      delete [] buffers;
2741
 
    buffers= 0;
2742
 
  }
2743
 
}
2744
 
 
2745
 
 
2746
 
bool
2747
 
udf_handler::fix_fields(THD *thd, Item_result_field *func,
2748
 
                        uint arg_count, Item **arguments)
2749
 
{
2750
 
  uchar buff[STACK_BUFF_ALLOC];                 // Max argument in function
2751
 
 
2752
 
  if (check_stack_overrun(thd, STACK_MIN_SIZE, buff))
2753
 
    return(true);                               // Fatal error flag is set!
2754
 
 
2755
 
  udf_func *tmp_udf=find_udf(u_d->name.str,(uint) u_d->name.length);
2756
 
 
2757
 
  if (!tmp_udf)
2758
 
  {
2759
 
    my_error(ER_CANT_FIND_UDF, MYF(0), u_d->name.str, errno);
2760
 
    return(true);
2761
 
  }
2762
 
  u_d=tmp_udf;
2763
 
  args=arguments;
2764
 
 
2765
 
  /* Fix all arguments */
2766
 
  func->maybe_null=0;
2767
 
  used_tables_cache=0;
2768
 
  const_item_cache=1;
2769
 
 
2770
 
  if ((f_args.arg_count=arg_count))
2771
 
  {
2772
 
    if (!(f_args.arg_type= (Item_result*)
2773
 
          sql_alloc(f_args.arg_count*sizeof(Item_result))))
2774
 
 
2775
 
    {
2776
 
      return(true);
2777
 
    }
2778
 
    uint i;
2779
 
    Item **arg,**arg_end;
2780
 
    for (i=0, arg=arguments, arg_end=arguments+arg_count;
2781
 
         arg != arg_end ;
2782
 
         arg++,i++)
2783
 
    {
2784
 
      if (!(*arg)->fixed &&
2785
 
          (*arg)->fix_fields(thd, arg))
2786
 
        return(1);
2787
 
      // we can't assign 'item' before, because fix_fields() can change arg
2788
 
      Item *item= *arg;
2789
 
      if (item->check_cols(1))
2790
 
        return(true);
2791
 
      /*
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.
2801
 
      */
2802
 
      if (item->collation.collation->state & MY_CS_BINSORT)
2803
 
        func->collation.set(&my_charset_bin);
2804
 
      if (item->maybe_null)
2805
 
        func->maybe_null=1;
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();
2810
 
    }
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 *
2820
 
                                                       sizeof(long))))
2821
 
    {
2822
 
      return(true);
2823
 
    }
2824
 
  }
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;
2830
 
  initid.ptr=0;
2831
 
 
2832
 
  if (u_d->func_init)
2833
 
  {
2834
 
    char init_msg_buff[DRIZZLE_ERRMSG_SIZE];
2835
 
    char *to=num_buffer;
2836
 
    for (uint i=0; i < arg_count; i++)
2837
 
    {
2838
 
      /*
2839
 
       For a constant argument i, args->args[i] points to the argument value. 
2840
 
       For non-constant, args->args[i] is NULL.
2841
 
      */
2842
 
      f_args.args[i]= NULL;         /* Non-const unless updated below. */
2843
 
 
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;
2848
 
 
2849
 
      if (arguments[i]->const_item())
2850
 
      {
2851
 
        switch (arguments[i]->result_type()) 
2852
 
        {
2853
 
        case STRING_RESULT:
2854
 
        case DECIMAL_RESULT:
2855
 
        {
2856
 
          String *res= arguments[i]->val_str(&buffers[i]);
2857
 
          if (arguments[i]->null_value)
2858
 
            continue;
2859
 
          f_args.args[i]= (char*) res->c_ptr();
2860
 
          f_args.lengths[i]= res->length();
2861
 
          break;
2862
 
        }
2863
 
        case INT_RESULT:
2864
 
          *((int64_t*) to)= arguments[i]->val_int();
2865
 
          if (arguments[i]->null_value)
2866
 
            continue;
2867
 
          f_args.args[i]= to;
2868
 
          to+= ALIGN_SIZE(sizeof(int64_t));
2869
 
          break;
2870
 
        case REAL_RESULT:
2871
 
          *((double*) to)= arguments[i]->val_real();
2872
 
          if (arguments[i]->null_value)
2873
 
            continue;
2874
 
          f_args.args[i]= to;
2875
 
          to+= ALIGN_SIZE(sizeof(double));
2876
 
          break;
2877
 
        case ROW_RESULT:
2878
 
        default:
2879
 
          // This case should never be chosen
2880
 
          assert(0);
2881
 
          break;
2882
 
        }
2883
 
      }
2884
 
    }
2885
 
    Udf_func_init init= u_d->func_init;
2886
 
    if ((error=(uchar) init(&initid, &f_args, init_msg_buff)))
2887
 
    {
2888
 
      my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2889
 
               u_d->name.str, init_msg_buff);
2890
 
      return(true);
2891
 
    }
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;
2895
 
    /* 
2896
 
      Keep used_tables_cache in sync with const_item_cache.
2897
 
      See the comment in Item_udf_func::update_used tables.
2898
 
    */  
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);
2902
 
  }
2903
 
  initialized=1;
2904
 
  if (error)
2905
 
  {
2906
 
    my_error(ER_CANT_INITIALIZE_UDF, MYF(0),
2907
 
             u_d->name.str, ER(ER_UNKNOWN_ERROR));
2908
 
    return(true);
2909
 
  }
2910
 
  return(false);
2911
 
}
2912
 
 
2913
 
 
2914
 
bool udf_handler::get_arguments()
2915
 
{
2916
 
  if (error)
2917
 
    return 1;                                   // Got an error earlier
2918
 
  char *to= num_buffer;
2919
 
  uint str_count=0;
2920
 
  for (uint i=0; i < f_args.arg_count; i++)
2921
 
  {
2922
 
    f_args.args[i]=0;
2923
 
    switch (f_args.arg_type[i]) {
2924
 
    case STRING_RESULT:
2925
 
    case DECIMAL_RESULT:
2926
 
      {
2927
 
        String *res=args[i]->val_str(&buffers[str_count++]);
2928
 
        if (!(args[i]->null_value))
2929
 
        {
2930
 
          f_args.args[i]=    (char*) res->ptr();
2931
 
          f_args.lengths[i]= res->length();
2932
 
          break;
2933
 
        }
2934
 
      }
2935
 
    case INT_RESULT:
2936
 
      *((int64_t*) to) = args[i]->val_int();
2937
 
      if (!args[i]->null_value)
2938
 
      {
2939
 
        f_args.args[i]=to;
2940
 
        to+= ALIGN_SIZE(sizeof(int64_t));
2941
 
      }
2942
 
      break;
2943
 
    case REAL_RESULT:
2944
 
      *((double*) to)= args[i]->val_real();
2945
 
      if (!args[i]->null_value)
2946
 
      {
2947
 
        f_args.args[i]=to;
2948
 
        to+= ALIGN_SIZE(sizeof(double));
2949
 
      }
2950
 
      break;
2951
 
    case ROW_RESULT:
2952
 
    default:
2953
 
      // This case should never be chosen
2954
 
      assert(0);
2955
 
      break;
2956
 
    }
2957
 
  }
2958
 
  return 0;
2959
 
}
2960
 
 
2961
 
/**
2962
 
  @return
2963
 
    (String*)NULL in case of NULL values
2964
 
*/
2965
 
String *udf_handler::val_str(String *str,String *save_str)
2966
 
{
2967
 
  uchar is_null_tmp=0;
2968
 
  ulong res_length;
2969
 
 
2970
 
  if (get_arguments())
2971
 
    return(0);
2972
 
  char * (*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
2973
 
    (char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
2974
 
    u_d->func;
2975
 
 
2976
 
  if ((res_length=str->alloced_length()) < MAX_FIELD_WIDTH)
2977
 
  {                                             // This happens VERY seldom
2978
 
    if (str->alloc(MAX_FIELD_WIDTH))
2979
 
    {
2980
 
      error=1;
2981
 
      return(0);
2982
 
    }
2983
 
  }
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
2987
 
  {
2988
 
    return(0);
2989
 
  }
2990
 
  if (res == str->ptr())
2991
 
  {
2992
 
    str->length(res_length);
2993
 
    return(str);
2994
 
  }
2995
 
  save_str->set(res, res_length, str->charset());
2996
 
  return(save_str);
2997
 
}
2998
 
 
2999
 
 
3000
 
/*
3001
 
  For the moment, UDF functions are returning DECIMAL values as strings
3002
 
*/
3003
 
 
3004
 
my_decimal *udf_handler::val_decimal(bool *null_value, my_decimal *dec_buf)
3005
 
{
3006
 
  char buf[DECIMAL_MAX_STR_LENGTH+1], *end;
3007
 
  ulong res_length= DECIMAL_MAX_STR_LENGTH;
3008
 
 
3009
 
  if (get_arguments())
3010
 
  {
3011
 
    *null_value=1;
3012
 
    return 0;
3013
 
  }
3014
 
  char *(*func)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *)=
3015
 
    (char* (*)(UDF_INIT *, UDF_ARGS *, char *, ulong *, uchar *, uchar *))
3016
 
    u_d->func;
3017
 
 
3018
 
  char *res= func(&initid, &f_args, buf, &res_length, &is_null, &error);
3019
 
  if (is_null || error)
3020
 
  {
3021
 
    *null_value= 1;
3022
 
    return 0;
3023
 
  }
3024
 
  end= res+ res_length;
3025
 
  str2my_decimal(E_DEC_FATAL_ERROR, res, dec_buf, &end);
3026
 
  return dec_buf;
3027
 
}
3028
 
 
3029
 
 
3030
 
void Item_udf_func::cleanup()
3031
 
{
3032
 
  udf.cleanup();
3033
 
  Item_func::cleanup();
3034
 
}
3035
 
 
3036
 
 
3037
 
void Item_udf_func::print(String *str, enum_query_type query_type)
3038
 
{
3039
 
  str->append(func_name());
3040
 
  str->append('(');
3041
 
  for (uint i=0 ; i < arg_count ; i++)
3042
 
  {
3043
 
    if (i != 0)
3044
 
      str->append(',');
3045
 
    args[i]->print_item_w_name(str, query_type);
3046
 
  }
3047
 
  str->append(')');
3048
 
}
3049
 
 
3050
 
 
3051
 
double Item_func_udf_float::val_real()
3052
 
{
3053
 
  assert(fixed == 1);
3054
 
  return(udf.val(&null_value));
3055
 
}
3056
 
 
3057
 
 
3058
 
String *Item_func_udf_float::val_str(String *str)
3059
 
{
3060
 
  assert(fixed == 1);
3061
 
  double nr= val_real();
3062
 
  if (null_value)
3063
 
    return 0;                                   /* purecov: inspected */
3064
 
  str->set_real(nr,decimals,&my_charset_bin);
3065
 
  return str;
3066
 
}
3067
 
 
3068
 
 
3069
 
int64_t Item_func_udf_int::val_int()
3070
 
{
3071
 
  assert(fixed == 1);
3072
 
  return(udf.val_int(&null_value));
3073
 
}
3074
 
 
3075
 
 
3076
 
String *Item_func_udf_int::val_str(String *str)
3077
 
{
3078
 
  assert(fixed == 1);
3079
 
  int64_t nr=val_int();
3080
 
  if (null_value)
3081
 
    return 0;
3082
 
  str->set_int(nr, unsigned_flag, &my_charset_bin);
3083
 
  return str;
3084
 
}
3085
 
 
3086
 
 
3087
 
int64_t Item_func_udf_decimal::val_int()
3088
 
{
3089
 
  my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3090
 
  int64_t result;
3091
 
  if (null_value)
3092
 
    return 0;
3093
 
  my_decimal2int(E_DEC_FATAL_ERROR, dec, unsigned_flag, &result);
3094
 
  return result;
3095
 
}
3096
 
 
3097
 
 
3098
 
double Item_func_udf_decimal::val_real()
3099
 
{
3100
 
  my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3101
 
  double result;
3102
 
  if (null_value)
3103
 
    return 0.0;
3104
 
  my_decimal2double(E_DEC_FATAL_ERROR, dec, &result);
3105
 
  return result;
3106
 
}
3107
 
 
3108
 
 
3109
 
my_decimal *Item_func_udf_decimal::val_decimal(my_decimal *dec_buf)
3110
 
{
3111
 
  assert(fixed == 1);
3112
 
  return(udf.val_decimal(&null_value, dec_buf));
3113
 
}
3114
 
 
3115
 
 
3116
 
String *Item_func_udf_decimal::val_str(String *str)
3117
 
{
3118
 
  my_decimal dec_buf, *dec= udf.val_decimal(&null_value, &dec_buf);
3119
 
  if (null_value)
3120
 
    return 0;
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);
3125
 
  return str;
3126
 
}
3127
 
 
3128
 
 
3129
 
void Item_func_udf_decimal::fix_length_and_dec()
3130
 
{
3131
 
  fix_num_length_and_dec();
3132
 
}
3133
 
 
3134
 
 
3135
 
/* Default max_length is max argument length */
3136
 
 
3137
 
void Item_func_udf_str::fix_length_and_dec()
3138
 
{
3139
 
  max_length=0;
3140
 
  for (uint i = 0; i < arg_count; i++)
3141
 
    set_if_bigger(max_length,args[i]->max_length);
3142
 
  return;
3143
 
}
3144
 
 
3145
 
String *Item_func_udf_str::val_str(String *str)
3146
 
{
3147
 
  assert(fixed == 1);
3148
 
  String *res=udf.val_str(str,&str_value);
3149
 
  null_value = !res;
3150
 
  return res;
3151
 
}
3152
 
 
3153
 
 
3154
 
/**
3155
 
  @note
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.)
3158
 
*/
3159
 
 
3160
 
udf_handler::~udf_handler()
3161
 
{
3162
 
  /* Everything should be properly cleaned up by this moment. */
3163
 
  assert(not_original || !(initialized || buffers));
3164
 
}
3165
 
 
3166
2718
/*
3167
2719
** User level locks
3168
2720
*/