@@ -2949,10 +2949,22 @@ sp_decl:
2949
2949
uint num_vars= pctx->context_var_count ();
2950
2950
enum enum_field_types var_type= (enum enum_field_types) $4 ;
2951
2951
Item *dflt_value_item= $5 ;
2952
+ const bool has_default_clause = (dflt_value_item != NULL );
2953
+ bool is_const_item = false ;
2954
+
2952
2955
LEX_STRING dflt_value_query= EMPTY_STR;
2953
2956
2954
- if (dflt_value_item)
2955
- {
2957
+ if (has_default_clause) {
2958
+ // Handling a NEG_FUNC wrapping a constant.
2959
+ if (dflt_value_item->type () == Item::FUNC_ITEM) {
2960
+ Item_func *func_item = down_cast<Item_func *>(dflt_value_item);
2961
+ if (func_item->functype () == Item_func::NEG_FUNC) {
2962
+ is_const_item = true ;
2963
+ }
2964
+ } else if (dflt_value_item->const_item ()) {
2965
+ is_const_item = true ;
2966
+ }
2967
+
2956
2968
// sp_opt_default only pushes start ptr for DEFAULT clause.
2957
2969
const char *expr_start_ptr=
2958
2970
sp->m_parser_data .pop_expr_start_ptr ();
@@ -2972,10 +2984,13 @@ sp_decl:
2972
2984
MYSQL_YYABORT;
2973
2985
}
2974
2986
2987
+ sp_variable *first_spvar = NULL ;
2988
+ const uint first_var_num = num_vars - $2 ;
2989
+
2975
2990
// We can have several variables in DECLARE statement.
2976
2991
// We need to create an sp_instr_set instruction for each variable.
2977
2992
2978
- for (uint i = num_vars-$ 2 ; i < num_vars ; i++)
2993
+ for (uint i = first_var_num ; i < num_vars; i++)
2979
2994
{
2980
2995
uint var_idx= pctx->var_context2runtime (i);
2981
2996
sp_variable *spvar= pctx->find_variable (var_idx);
@@ -2984,8 +2999,33 @@ sp_decl:
2984
2999
MYSQL_YYABORT;
2985
3000
2986
3001
spvar->type = var_type;
3002
+
3003
+ // Transforming the following declare statements having non-const
3004
+ // expressions:
3005
+ // DECLARE a, b, c type DEFAULT expr;
3006
+ // to
3007
+ // DECLARE a type DEFAULT expr, b type DEFAULT a,
3008
+ // c type DEFAULT a;
3009
+
3010
+ if (i == first_var_num) {
3011
+ first_spvar = spvar;
3012
+ } else if (has_default_clause && !is_const_item) {
3013
+ Item_splocal *item =
3014
+ NEW_PTN Item_splocal (first_spvar->name , first_spvar->offset ,
3015
+ first_spvar->type , 0 , 0 );
3016
+ if (item == NULL )
3017
+ MYSQL_YYABORT; // OOM
3018
+ #ifndef NDEBUG
3019
+ item->m_sp = lex->sphead ;
3020
+ #endif
3021
+ dflt_value_item = item;
3022
+ }
2987
3023
spvar->default_value = dflt_value_item;
2988
3024
3025
+ if (has_default_clause && !is_const_item && (i > first_var_num)) {
3026
+ dflt_value_query = first_spvar->name ;
3027
+ }
3028
+
2989
3029
if (fill_field_definition (thd, sp, var_type, &spvar->field_def ))
2990
3030
MYSQL_YYABORT;
2991
3031
0 commit comments