[Commits] 071d92c: Changes in default handling

Oleksandr Byelkin sanja at mariadb.com
Mon Oct 24 20:03:36 EEST 2016


revision-id: 071d92c3ee768106fde5028e2996b4a0da30d4c2 (mariadb-10.2.2-50-g071d92c)
parent(s): 4a6eed672a41663e2397e2c8aa13d8ae6d370435
committer: Oleksandr Byelkin
timestamp: 2016-10-24 19:03:36 +0200
message:

Changes in default handling

---
 sql/field.cc       |  40 ++++++++++++++++++
 sql/field.h        |   3 ++
 sql/item.cc        | 119 +++++++++++++++--------------------------------------
 sql/item.h         |  24 ++---------
 sql/sql_base.cc    |   4 --
 sql/sql_insert.cc  |  62 ----------------------------
 sql/sql_prepare.cc |   5 +--
 sql/sql_update.cc  |   4 --
 8 files changed, 82 insertions(+), 179 deletions(-)

diff --git a/sql/field.cc b/sql/field.cc
index e4ecb74..609d44f 100644
--- a/sql/field.cc
+++ b/sql/field.cc
@@ -10821,3 +10821,43 @@ bool Field::validate_value_in_record_with_warn(THD *thd, const uchar *record)
   dbug_tmp_restore_column_map(table->read_set, old_map);
   return rc;
 }
+
+
+bool Field::save_in_field_default_value(bool view_error_processing)
+{
+  THD *thd= table->in_use;
+
+  if (flags & NO_DEFAULT_VALUE_FLAG &&
+      real_type() != MYSQL_TYPE_ENUM)
+  {
+    if (reset())
+    {
+      my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
+                 ER_THD(thd, ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
+      return -1;
+    }
+
+    if (view_error_processing)
+    {
+      TABLE_LIST *view= table->pos_in_table_list->top_table();
+      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
+                          ER_NO_DEFAULT_FOR_VIEW_FIELD,
+                          ER_THD(thd, ER_NO_DEFAULT_FOR_VIEW_FIELD),
+                          view->view_db.str,
+                          view->view_name.str);
+    }
+    else
+    {
+      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
+                          ER_NO_DEFAULT_FOR_FIELD,
+                          ER_THD(thd, ER_NO_DEFAULT_FOR_FIELD),
+                          field_name);
+    }
+    return 1;
+  }
+  set_default();
+  return
+    !is_null() &&
+    validate_value_in_record_with_warn(thd, table->record[0]) &&
+    thd->is_error() ? -1 : 0;
+}
diff --git a/sql/field.h b/sql/field.h
index f550dad..c9d7509 100644
--- a/sql/field.h
+++ b/sql/field.h
@@ -1451,6 +1451,9 @@ class Field: public Value_source
     // Exactly the same rules with REF access
     return can_optimize_keypart_ref(cond, item);
   }
+
+  bool save_in_field_default_value(bool view_eror_processing);
+
   friend int cre_myisam(char * name, register TABLE *form, uint options,
 			ulonglong auto_increment_value);
   friend class Copy_field;
diff --git a/sql/item.cc b/sql/item.cc
index e52e7f5..7fe0585 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -818,13 +818,6 @@ bool Item_ident::collect_outer_ref_processor(void *param)
   return FALSE;
 }
 
-void Item_ident::set_default_value_target(Item *item)
-{
-  if ((default_value_target= item) && fixed &&
-      type() == FIELD_ITEM)
-    default_value_target->set_default_value_source(((Item_field *)this)->
-                                                   field);
-}
 
 /**
   Store the pointer to this item field into a list if not already there.
@@ -2547,10 +2540,6 @@ void Item_field::set_field(Field *field_par)
   max_length= adjust_max_effective_column_length(field_par, max_length);
 
   fixed= 1;
-  if (default_value_target != 0)
-  {
-    default_value_target->set_default_value_source(field);
-  }
   if (field->table->s->tmp_table == SYSTEM_TMP_TABLE)
     any_privileges= 0;
 }
@@ -3244,7 +3233,7 @@ Item_param::Item_param(THD *thd, uint pos_in_query_arg):
   Item_basic_value(thd),
   Rewritable_query_parameter(pos_in_query_arg, 1),
   Type_handler_hybrid_field_type(MYSQL_TYPE_VARCHAR),
-  state(NO_VALUE), default_value_ref(NULL), default_value_source(NULL),
+  state(NO_VALUE),
   indicators(0), indicator(STMT_INDICATOR_NONE),
   /* Don't pretend to be a literal unless value for this item is set. */
   item_type(PARAM_ITEM),
@@ -3577,8 +3566,6 @@ void Item_param::reset()
   state= NO_VALUE;
   maybe_null= 1;
   null_value= 0;
-  default_value_source= NULL;
-  default_value_ref= NULL;
   /*
     Don't reset item_type to PARAM_ITEM: it's only needed to guard
     us from item optimizations at prepare stage, when item doesn't yet
@@ -3612,6 +3599,10 @@ int Item_param::save_in_field(Field *field, bool no_conversions)
                         str_value.charset());
   case NULL_VALUE:
     return set_field_to_null_with_conversions(field, no_conversions);
+  case DEFAULT_VALUE:
+    return field->save_in_field_default_value(field->table->pos_in_table_list->
+                                              top_table() !=
+                                              field->table->pos_in_table_list);
   case NO_VALUE:
   default:
     DBUG_ASSERT(0);
@@ -3657,6 +3648,9 @@ double Item_param::val_real()
     return TIME_to_double(&value.time);
   case NULL_VALUE:
     return 0.0;
+  case DEFAULT_VALUE:
+    my_message(ER_INVALID_DEFAULT_PARAM,
+               ER_THD(current_thd, ER_INVALID_DEFAULT_PARAM), MYF(0));
   default:
     DBUG_ASSERT(0);
   }
@@ -3684,6 +3678,9 @@ longlong Item_param::val_int()
     }
   case TIME_VALUE:
     return (longlong) TIME_to_ulonglong(&value.time);
+  case DEFAULT_VALUE:
+    my_message(ER_INVALID_DEFAULT_PARAM,
+               ER_THD(current_thd, ER_INVALID_DEFAULT_PARAM), MYF(0));
   case NULL_VALUE:
     return 0; 
   default:
@@ -3711,6 +3708,9 @@ my_decimal *Item_param::val_decimal(my_decimal *dec)
   {
     return TIME_to_my_decimal(&value.time, dec);
   }
+  case DEFAULT_VALUE:
+    my_message(ER_INVALID_DEFAULT_PARAM,
+               ER_THD(current_thd, ER_INVALID_DEFAULT_PARAM), MYF(0));
   case NULL_VALUE:
     return 0; 
   default:
@@ -3746,6 +3746,9 @@ String *Item_param::val_str(String* str)
     str->set_charset(&my_charset_bin);
     return str;
   }
+  case DEFAULT_VALUE:
+    my_message(ER_INVALID_DEFAULT_PARAM,
+               ER_THD(current_thd, ER_INVALID_DEFAULT_PARAM), MYF(0));
   case NULL_VALUE:
     return NULL; 
   default:
@@ -3824,6 +3827,9 @@ const String *Item_param::query_val_str(THD *thd, String* str) const
                           thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES);
       break;
     }
+  case DEFAULT_VALUE:
+    my_message(ER_INVALID_DEFAULT_PARAM,
+               ER_THD(current_thd, ER_INVALID_DEFAULT_PARAM), MYF(0));
   case NULL_VALUE:
     return &my_null_string;
   default:
@@ -3874,6 +3880,9 @@ Item_param::clone_item(THD *thd)
 {
   MEM_ROOT *mem_root= thd->mem_root;
   switch (state) {
+  case DEFAULT_VALUE:
+    my_message(ER_INVALID_DEFAULT_PARAM,
+               ER_THD(current_thd, ER_INVALID_DEFAULT_PARAM), MYF(0));
   case NULL_VALUE:
     return new (mem_root) Item_null(thd, name);
   case INT_VALUE:
@@ -3906,6 +3915,9 @@ Item_param::eq(const Item *item, bool binary_cmp) const
     return FALSE;
 
   switch (state) {
+  case DEFAULT_VALUE:
+    my_message(ER_INVALID_DEFAULT_PARAM,
+               ER_THD(current_thd, ER_INVALID_DEFAULT_PARAM), MYF(0));
   case NULL_VALUE:
     return null_eq(item);
   case INT_VALUE:
@@ -3929,6 +3941,10 @@ void Item_param::print(String *str, enum_query_type query_type)
   {
     str->append('?');
   }
+  else if (state == DEFAULT_VALUE)
+  {
+    str->append("default");
+  }
   else
   {
     char buffer[STRING_BUFFER_USUAL_SIZE];
@@ -3973,9 +3989,6 @@ Item_param::set_param_type_and_swap_value(Item_param *src)
   null_value= src->null_value;
   state= src->state;
   value= src->value;
-  default_value_ref= src->default_value_ref;
-  default_value_source= src->default_value_source;
-
 
   decimal_value.swap(src->decimal_value);
   str_value.swap(src->str_value);
@@ -3983,28 +3996,9 @@ Item_param::set_param_type_and_swap_value(Item_param *src)
 }
 
 
-bool Item_param::set_default(bool can_be_missed)
+void Item_param::set_default()
 {
-  if (!default_value_ref)
-  {
-    if (can_be_missed)
-      return FALSE;
-    my_message(ER_INVALID_DEFAULT_PARAM,
-               ER_THD(current_thd, ER_INVALID_DEFAULT_PARAM), MYF(0));
-    return TRUE;
-  }
-  THD *thd= default_value_ref->table->in_use;
-  if (!default_value_source)
-  {
-    default_value_source= new (thd->mem_root)
-      Item_default_value(thd, &thd->lex->select_lex.context, default_value_ref);
-    if (!default_value_source ||
-        default_value_source->fix_fields(thd, (Item **)&default_value_source))
-      return TRUE;
-    bitmap_set_bit(default_value_ref->table->read_set,
-                   default_value_ref->field_index);
-  }
-  return set_value(thd, NULL, (Item**)&default_value_source);
+  state= DEFAULT_VALUE;
 }
 
 /**
@@ -4166,16 +4160,6 @@ bool Item_param::append_for_log(THD *thd, String *str)
 }
 
 
-bool Item_param::walk(Item_processor processor, bool walk_subquery, void *arg)
-{
-  if (default_value_source &&
-      default_value_source->walk(processor, walk_subquery, arg))
-  {
-    return TRUE;
-  }
-  return (this->*processor)(arg);
-}
-
 /****************************************************************************
   Item_copy
 ****************************************************************************/
@@ -7334,7 +7318,6 @@ bool Item_ref::fix_fields(THD *thd, Item **reference)
         Item_field* fld;
         if (!(fld= new (thd->mem_root) Item_field(thd, from_field)))
           goto error;
-        fld->set_default_value_target(default_value_target);
         thd->change_item_tree(reference, fld);
         mark_as_dependent(thd, last_checked_context->select_lex,
                           current_sel, fld, fld);
@@ -8632,42 +8615,8 @@ int Item_default_value::save_in_field(Field *field_arg, bool no_conversions)
     calculate();
   else
   {
-    TABLE *table= field_arg->table;
-    THD *thd= table->in_use;
-
-    if (field_arg->flags & NO_DEFAULT_VALUE_FLAG &&
-        field_arg->real_type() != MYSQL_TYPE_ENUM)
-    {
-      if (field_arg->reset())
-      {
-        my_message(ER_CANT_CREATE_GEOMETRY_OBJECT,
-                   ER_THD(thd, ER_CANT_CREATE_GEOMETRY_OBJECT), MYF(0));
-        return -1;
-      }
-
-      if (context->error_processor == &view_error_processor)
-      {
-        TABLE_LIST *view= table->pos_in_table_list->top_table();
-        push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
-                            ER_NO_DEFAULT_FOR_VIEW_FIELD,
-                            ER_THD(thd, ER_NO_DEFAULT_FOR_VIEW_FIELD),
-                            view->view_db.str,
-                            view->view_name.str);
-      }
-      else
-      {
-        push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
-                            ER_NO_DEFAULT_FOR_FIELD,
-                            ER_THD(thd, ER_NO_DEFAULT_FOR_FIELD),
-                            field_arg->field_name);
-      }
-      return 1;
-    }
-    field_arg->set_default();
-    return
-      !field_arg->is_null() &&
-       field_arg->validate_value_in_record_with_warn(thd, table->record[0]) &&
-       thd->is_error() ? -1 : 0;
+    return field_arg->save_in_field_default_value(context->error_processor ==
+                                                  &view_error_processor);
   }
   return Item_field::save_in_field(field_arg, no_conversions);
 }
diff --git a/sql/item.h b/sql/item.h
index 0915cc2..c932d7d 100644
--- a/sql/item.h
+++ b/sql/item.h
@@ -1946,9 +1946,6 @@ class Item: public Value_source,
   {
     marker &= ~EXTRACTION_MASK;
   }
-
-  virtual void set_default_value_source(Field *fld) {};
-  virtual bool set_default_if_needed() { return FALSE; };
 };
 
 
@@ -2474,7 +2471,6 @@ class Item_ident :public Item_result_field
   virtual void print(String *str, enum_query_type query_type);
   virtual bool change_context_processor(void *cntx)
     { context= (Name_resolution_context *)cntx; return FALSE; }
-  void set_default_value_target(Item *item);
   /**
     Collect outer references
   */
@@ -2807,7 +2803,7 @@ class Item_param :public Item_basic_value,
   {
     NO_VALUE, NULL_VALUE, INT_VALUE, REAL_VALUE,
     STRING_VALUE, TIME_VALUE, LONG_DATA_VALUE,
-    DECIMAL_VALUE
+    DECIMAL_VALUE, DEFAULT_VALUE
   } state;
 
   struct CONVERSION_INFO
@@ -2851,14 +2847,12 @@ class Item_param :public Item_basic_value,
     }
   };
 
-  Field *default_value_ref;
-  Item_default_value *default_value_source;
   /*
     Used for bulk protocol. Indicates if we should expect
     indicators byte before value of the parameter
   */
   my_bool indicators;
-  uint indicator;
+  enum enum_indicator_type indicator;
 
   /*
     A buffer for string and long data values. Historically all allocated
@@ -2900,10 +2894,7 @@ class Item_param :public Item_basic_value,
   bool get_date(MYSQL_TIME *tm, ulonglong fuzzydate);
   int  save_in_field(Field *field, bool no_conversions);
 
-  virtual void set_default_value_source(Field *fld)
-  { default_value_ref= fld; }
-
-  bool set_default(bool can_be_missed);
+  void set_default();
   void set_null();
   void set_int(longlong i, uint32 max_length_arg);
   void set_double(double i);
@@ -2972,19 +2963,10 @@ class Item_param :public Item_basic_value,
   virtual void set_out_param_info(Send_field *info);
 
 public:
-  bool set_default_if_needed()
-  {
-    if (state == NO_VALUE && indicator == STMT_INDICATOR_DEFAULT &&
-        set_default(FALSE))
-      return TRUE;
-    return FALSE;
-  }
-
   virtual const Send_field *get_out_param_info() const;
 
   virtual void make_field(THD *thd, Send_field *field);
 
-  virtual bool walk(Item_processor processor, bool walk_subquery, void *arg);
 private:
   Send_field *m_out_param_info;
 };
diff --git a/sql/sql_base.cc b/sql/sql_base.cc
index fe4aec7..78220f8 100644
--- a/sql/sql_base.cc
+++ b/sql/sql_base.cc
@@ -7821,8 +7821,6 @@ fill_record(THD *thd, TABLE *table_arg, List<Item> &fields, List<Item> &values,
                           ER_THD(thd, ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
                           rfield->field_name, table->s->table_name.str);
     }
-    if (value->set_default_if_needed())
-      goto err;
     if (rfield->stored_in_db() &&
         (value->save_in_field(rfield, 0)) < 0 && !ignore_errors)
     {
@@ -8074,8 +8072,6 @@ fill_record(THD *thd, TABLE *table, Field **ptr, List<Item> &values,
                           ER_THD(thd, ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
                           field->field_name, table->s->table_name.str);
     }
-    if (value->set_default_if_needed())
-      goto err;
 
     if (use_value)
       value->save_val(field);
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index be99bdd..a532a04 100644
--- a/sql/sql_insert.cc
+++ b/sql/sql_insert.cc
@@ -638,66 +638,6 @@ static void save_insert_query_plan(THD* thd, TABLE_LIST *table_list)
 }
 
 
-inline static void set_defaults_relation(Item *fld, Item *val)
-{
-  Item::Type type= fld->type();
-  if (type == Item::FIELD_ITEM)
-  {
-    Item_field *item_field= (Item_field *)fld;
-    if (item_field->fixed)
-      val->set_default_value_source(item_field->field);
-    else
-      item_field->set_default_value_target(val);
-  }
-  else if (type == Item::REF_ITEM)
-  {
-    Item_ref *item_field= (Item_ref *)fld;
-    // may turn to Item_field after fix_fields()
-    if (!item_field->fixed)
-      item_field->set_default_value_target(val);
-  }
-}
-
-void setup_deault_parameters(TABLE_LIST *table, List<Item> *fields,
-                             List<Item> *values)
-{
-
-  List_iterator_fast<Item> itv(*values);
-  Item *val;
-  if (fields->elements)
-  {
-    List_iterator_fast<Item> itf(*fields);
-    Item *fld;
-    while((fld= itf++) && (val= itv++))
-    {
-      set_defaults_relation(fld->real_item(), val);
-    }
-  }
-  else if (table != NULL)
-  {
-    if (table->view)
-    {
-      Field_iterator_view field_it;
-      field_it.set(table);
-      for (; !field_it.end_of_fields() && (val= itv++); field_it.next())
-      {
-        set_defaults_relation(field_it.item()->real_item(), val);
-      }
-    }
-    else
-    {
-      Field_iterator_table_ref field_it;
-      field_it.set(table);
-      for (; !field_it.end_of_fields() && (val= itv++); field_it.next())
-      {
-        Field *fld= field_it.field();
-        val->set_default_value_source(fld);
-      }
-    }
-  }
-}
-
-
 /**
   INSERT statement implementation
 
@@ -833,7 +773,6 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
     if (setup_fields(thd, Ref_ptr_array(), *values, MARK_COLUMNS_READ, 0, 0))
       goto abort;
     switch_to_nullable_trigger_fields(*values, table);
-    setup_deault_parameters(table_list, &fields, values);
   }
   its.rewind ();
  
@@ -1536,7 +1475,6 @@ bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
           check_insert_fields(thd, context->table_list, fields, *values,
                               !insert_into_view, 0, &map));
 
-    setup_deault_parameters(table_list, &fields, values);
     if (bulk_parameters_set(thd))
       DBUG_RETURN(TRUE);
 
diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc
index 47f296d..9332da9 100644
--- a/sql/sql_prepare.cc
+++ b/sql/sql_prepare.cc
@@ -992,7 +992,7 @@ static bool insert_bulk_params(Prepared_statement *stmt,
     if (param->state != Item_param::LONG_DATA_VALUE)
     {
       if (param->indicators)
-        param->indicator= *((*read_pos)++);
+        param->indicator= (enum enum_indicator_type) *((*read_pos)++);
       else
         param->indicator= STMT_INDICATOR_NONE;
       if ((*read_pos) > data_end)
@@ -1009,8 +1009,7 @@ static bool insert_bulk_params(Prepared_statement *stmt,
         param->set_null();
         break;
       case STMT_INDICATOR_DEFAULT:
-        if (param->set_default(TRUE))
-          DBUG_RETURN(1);
+        param->set_default();
         break;
       }
     }
diff --git a/sql/sql_update.cc b/sql/sql_update.cc
index ecd32e2..b452e4f 100644
--- a/sql/sql_update.cc
+++ b/sql/sql_update.cc
@@ -221,8 +221,6 @@ static void prepare_record_for_error_message(int error, TABLE *table)
   DBUG_VOID_RETURN;
 }
 
-void setup_deault_parameters(TABLE_LIST *table, List<Item> *fields,
-                             List<Item> *values);
 
 /*
   Process usual UPDATE
@@ -356,8 +354,6 @@ int mysql_update(THD *thd,
     DBUG_RETURN(1);
   }
 
-  setup_deault_parameters(NULL, &fields, &values);
-
 #ifndef NO_EMBEDDED_ACCESS_CHECKS
   /* Check values */
   table_list->grant.want_privilege= table->grant.want_privilege=


More information about the commits mailing list