← Back to team overview

maria-developers team mailing list archive

Re: REVIEW: Fwd: [Commits] 8cd4778: MDEV-9114: Bulk operations (Array binding)

 

Hi, Monty!

On 17.10.2016 20:12, Michael Widenius wrote:
Hi!

On Sun, Oct 9, 2016 at 1:45 AM, Andrea<montyw@xxxxxxxxxxxx>  wrote:
--- Forwarded message ---
From: Oleksandr Byelkin<sanja@xxxxxxxxxxxxxxxx>
Date: 8 October 2016 1:01:58 p.m.
Subject: REVIEW: Fwd: [Commits] 8cd4778: MDEV-9114: Bulk operations (Array
binding)
To: Michael Widenius<monty@xxxxxxxxxxx>

Hi, Monty!


It is the patch. There left 2 "TODO" marks about embedded I think that
best variant leave there it as is, but maybe you have other opinion.
Embedded is ok to not have bulk insert (for now), but we should give
an error, not an assert if this is used!

Review of bulk operations patch


diff --git a/include/mysql.h.pp b/include/mysql.h.pp
index 857f5b9..c985792 100644
--- a/include/mysql.h.pp
+++ b/include/mysql.h.pp
@@ -11,11 +11,17 @@ enum enum_server_command
    COM_STMT_RESET, COM_SET_OPTION, COM_STMT_FETCH, COM_DAEMON,
    COM_MDB_GAP_BEG,
    COM_MDB_GAP_END=250,
-  COM_SLAVE_WORKER,
-  COM_SLAVE_IO,
-  COM_SLAVE_SQL,
-  COM_MULTI,
-  COM_END
+  COM_SLAVE_WORKER=251,
+  COM_SLAVE_IO=252,
+  COM_SLAVE_SQL=253,
+  COM_MULTI=254,
+  COM_END=255
+};
Why the numbering ?
(Just curious as this shouldn't be needed)

To make it easy to add something new without a making mistake. (because I made it first)

+enum enum_indicator_type
+{
+  STMT_INDICATOR_NONE= 0,
+  STMT_INDICATOR_NULL,
+  STMT_INDICATOR_DEFAULT
  };
Please add comment what this enum is used for.
Note that this enum_indicator_type isn't used in this commit.
(I have a fix for this later on)
You should probably just rename it to indicator_type.
(On can always use 'enum indicator_type' if one wants to point out in the code
that it's an enum).
I'll put comment.

enum values defined here is used. I'll check where variables type could be changed on the enum.


diff --git a/sql/item.cc b/sql/item.cc
index 61635ea..41a7aaf 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -812,6 +818,13 @@ 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 &&
Please add a comment under which circumstances fixed could be false.
Also add a comment what it means when set_default_value_source isn't called.
Will it be called later or isn't it needed ?
As it appeared there was cases, but it is not relevent anymore (default value handling changed)

Actually all this dance around the fact that in relation "field=?" components could be fixed in different order and also setting value of parameter can be assigned also before and after. (not relevant any more)

+      type() == FIELD_ITEM)
+    default_value_target->set_default_value_source(((Item_field *)this)->
+                                                   field);
+}
@@ -7242,6 +7300,7 @@ 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);
Add a comment like: /* Note that fld->fixed isn't set here */
(not relevent any more)

@@ -8428,36 +8487,38 @@ bool Item_default_value::eq(const Item *item, bool binary_cmp) const
  bool Item_default_value::fix_fields(THD *thd, Item **items)
  {
    Item *real_arg;
-  Item_field *field_arg;
    Field *def_field;
    DBUG_ASSERT(fixed == 0);

-  if (!arg)
+  if (!arg && !arg_fld)
    {
      fixed= 1;
      return FALSE;
    }
-  if (!arg->fixed && arg->fix_fields(thd, &arg))
-    goto error;
+  if (arg)
+  {
+    if (!arg->fixed && arg->fix_fields(thd, &arg))
+      goto error;


-  real_arg= arg->real_item();
-  if (real_arg->type() != FIELD_ITEM)
-  {
-    my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->name);
-    goto error;
-  }
+    real_arg= arg->real_item();
+    if (real_arg->type() != FIELD_ITEM)
+    {
+      my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->name);
+      goto error;
+    }

-  field_arg= (Item_field *)real_arg;
-  if ((field_arg->field->flags & NO_DEFAULT_VALUE_FLAG))
+    arg_fld= ((Item_field *)real_arg)->field;
+  }
What is the reason from having arg_field (type field) instead of
field_arg (type Item_field) ?

arg_field is a bad name for a class variable as it doesn't tell us
what the variable actually contains. A variable name in a function is
not that critical to name right as one can understand what it contains
by looking at is usage.  For a class variable this isn't the case.

field_in_table or just field would be a bit more clear (but not yet perfect).

I looked at the useage arg_field and don't understand why this needs to be
part of the class and not just a local variable.
As def_field is basically a copy of def_field, then another possible name
for arg_fld would be def_field_org.
OK should not be relevant any more
--- a/sql/item.h
+++ b/sql/item.h
@@ -1872,6 +1872,9 @@ class Item: public Value_source,
    {
      marker &= ~EXTRACTION_MASK;
    }
+
+  virtual void set_default_value_source(Field *fld) {};
+  virtual bool set_default_if_needed() { return FALSE; };
  };


@@ -2351,6 +2354,8 @@ class Item_ident :public Item_result_field
    const char *orig_table_name;
    const char *orig_field_name;

remove the extra empty line here
OK
+  Item *default_value_target;
+
@@ -2727,6 +2736,15 @@ class Item_param :public Item_basic_value,
      DECIMAL_VALUE
    } state;

+  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;
@@ -4980,14 +5011,19 @@ class Item_default_value : public Item_field
    void calculate();
  public:
    Item *arg;
+  Field *arg_fld;
Why is the above needed ?
How is it used ?
The same problem of fix fields call in different time for relation field/parameter.
but now it is rolled back.
+++ b/sql/sql_base.cc
@@ -7810,9 +7810,10 @@ fill_record(THD *thd, TABLE *table_arg, List<Item> &fields, List<Item> &values,
      if (table->next_number_field &&
          rfield->field_index ==  table->next_number_field->field_index)
        table->auto_increment_field_not_null= TRUE;
-    if (rfield->vcol_info &&
-        value->type() != Item::DEFAULT_VALUE_ITEM &&
-        value->type() != Item::NULL_ITEM &&
+    Item::Type type= value->type();
+    if (rfield->vcol_info &&
+        type != Item::DEFAULT_VALUE_ITEM &&
+        type != Item::NULL_ITEM &&
          table->s->table_category != TABLE_CATEGORY_TEMPORARY)
The above code is slower than the original as it will requre an extra
function call.

Better to do:

if (rfield->vcol_info)
{
   Item::Type type= value->type();
   if (type != Item::DEFAULT_VALUE_ITEM &&
       type != Item::NULL_ITEM &&
       table->s->table_category != TABLE_CATEGORY_TEMPORARY)
...
OK
@@ -7820,6 +7821,8 @@ 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;
For which use case is the above needed?
(Would like to understand the exact usage case and preferably have this
documented in the code as it's far from clear why this is needed).

Normal default handling should be handled by the following code:

   if (!update && table_arg->default_field &&
       table_arg->update_default_fields(0, ignore_errors))
     goto err;

Why doesn't this work for bulk operations?
If value is 'default', then just not storing it into field should have the
above code to store the default value into the field.
It is not about default table in the table, it is about default value of the repated field in accigning parameter :

field=?

then we set ? to default value of related field (if such exists).

But is not relevant any more (removed)

@@ -8060,9 +8063,10 @@ fill_record(THD *thd, TABLE *table, Field **ptr, List<Item> &values,
      value=v++;
      if (field->field_index == autoinc_index)
        table->auto_increment_field_not_null= TRUE;
-    if (field->vcol_info &&
-        value->type() != Item::DEFAULT_VALUE_ITEM &&
-        value->type() != Item::NULL_ITEM &&
+    Item::Type type= value->type();
+    if (field->vcol_info &&
+        type != Item::DEFAULT_VALUE_ITEM &&
+        type != Item::NULL_ITEM &&
          table->s->table_category != TABLE_CATEGORY_TEMPORARY)
Move calling of value->type() down (see other comment above for similar code)

@@ -8070,6 +8074,8 @@ 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;
Why is the above code needed ?
setting value "default" for a parameter (if it is needed).
but removed already
+++ b/sql/sql_class.cc
@@ -5766,6 +5767,17 @@ int THD::decide_logging_format(TABLE_LIST *tables)
        !(wsrep_binlog_format() == BINLOG_FORMAT_STMT &&
          !binlog_filter->db_ok(db)))
    {
+
+    if (is_bulk_op())
+    {
+      if (wsrep_binlog_format() == BINLOG_FORMAT_STMT)
+      {
+        my_error(ER_BINLOG_NON_SUPPORTED_BULK, MYF(0));
+        DBUG_PRINT("info",
+                   ("decision: no logging since an error was generated"));
no logging -> aborting exceution
actually in other cases used phrase like this, so I think it should be consistent?
diff --git a/sql/sql_class.h b/sql/sql_class.h
index 51642ec..b444b36 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -2465,6 +2465,8 @@ class THD :public Statement,
    */
    Query_arena *stmt_arena;

+  void *bulk_param;
+
    /*
      map for tables that will be updated for a multi-table update query
      statement, for other query statements, this will be zero.
@@ -3440,6 +3442,12 @@ class THD :public Statement,
      To raise this flag, use my_error().
    */
    inline bool is_error() const { return m_stmt_da->is_error(); }
+  void set_bulk_execution(void *bulk)
+  {
+    bulk_param= bulk;
+    m_stmt_da->set_bulk_execution(MY_TEST(bulk));
+  }
+  bool is_bulk_op() const { return m_stmt_da->is_bulk_op(); }
Why not instead do:

bool is_bulk_op() const { return MY_TEST(bulk_param); }

If this isn't the same it would be good to know why.
OK, should be the same, I'll fix.
index 1d234c5..34d608f 100644
--- a/sql/sql_error.cc
+++ b/sql/sql_error.cc
@@ -320,7 +320,7 @@ Sql_condition::set_sqlstate(const char* sqlstate)
  }

  Diagnostics_area::Diagnostics_area(bool initialize)
-  : m_main_wi(0, false, initialize)
+  : is_bulk_execution(0), m_main_wi(0, false, initialize)
  {
Please add a comment why you need is_bulk_execution both in THD and
Diagnostics_area.  If this is the same and this is just a cache, it would be
good to do an assert check in set_ok_status() to verify this
in THD it is reference, in diagnostic area it is mode of the diagnostic area (it has no access to THD).

@@ -376,7 +377,7 @@ Diagnostics_area::set_ok_status(ulonglong affected_rows,
                                  const char *message)
  {
    DBUG_ENTER("set_ok_status");
-  DBUG_ASSERT(! is_set());
+  DBUG_ASSERT(!is_set() || (m_status == DA_OK_BULK && is_bulk_op()));
    /*
      In production, refuse to overwrite an error or a custom response
      with an OK packet.
@@ -384,14 +385,23 @@ Diagnostics_area::set_ok_status(ulonglong affected_rows,
    if (is_error() || is_disabled())
      return;

-  m_statement_warn_count= current_statement_warn_count();
-  m_affected_rows= affected_rows;
Add here the following comment:

/*
   When running a bulk operation, m_status will be DA_OK for the first operation
   and set to DA_OK_BULK for all following operations
*/
ok

+  if (m_status == DA_OK_BULK)
+  {
+    DBUG_ASSERT(is_bulk_op());
You don't need the above assert as you have already tested this a few lines
above.
yes

+    m_statement_warn_count+= current_statement_warn_count();
+    m_affected_rows+= affected_rows;
+  }
+  else
+  {
+    m_statement_warn_count= current_statement_warn_count();
+    m_affected_rows= affected_rows;
+    m_status= (is_bulk_op() ? DA_OK_BULK : DA_OK);
+  }
--- a/sql/protocol.cc
+++ b/sql/protocol.cc
@@ -572,6 +572,7 @@ void Protocol::end_statement()
                      thd->get_stmt_da()->statement_warn_count());
      break;
    case Diagnostics_area::DA_OK:
+  case Diagnostics_area::DA_OK_BULK:
      error= send_ok(thd->server_status,
                     thd->get_stmt_da()->statement_warn_count(),
                     thd->get_stmt_da()->affected_rows(),
For bulk operations, why do we send a a packet for every array element?
isn't it enough with just sending a summary last?
It is actually not send, but I'll need it to collect INSERT-IDs (required by JDBC)

diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
@@ -637,6 +638,66 @@ static void save_insert_query_plan(THD* thd, TABLE_LIST *table_list)
  }

Please add a detailed comment what the following function does

+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;
Why test fixed ?
Shouldn't all items should be fixed this late in the game.
not relevant any more (removed)
+    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);
+  }
+}
+
Please add a detailed comment what the following function does.
For example, is this only needed to be exceuted when using bulk operations?

+void setup_deault_parameters(TABLE_LIST *table, List<Item> *fields,
+                             List<Item> *values)
Typo. should be setup_default_parameters
not relevant any more (removed)


+{
+
+  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)
I checked both calls to setup_default_parameters and it looks impossible that
table could be NULL when this function is called. (Both calls has
table_list->next_local= 0 just before the call).

Please add instead of the above test an DBUG_ASSERT(table) in the
beginning of this function.
not relevant any more (removed)


+  {
+    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
+    {
I assume this if for multi-table update ?
If yes, please add a comment
not relevant any more (removed)

+      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);
+      }
+    }
+  }
+}
I have to acknowledge that I am not completely sure when one should use
set_default_value_source, set_defaults_relation or set_default_value_target.
Can you please document somewhere in the code the purpose of the above
functions and when they should be used.


not relevant any more (removed)
@@ -770,6 +833,7 @@ 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);
Why do we go loop over all values an extra time below?
If this is only needed for bulk operations, then we should have
some extra tests there!

+    setup_deault_parameters(table_list, &fields, values);
@@ -885,105 +949,113 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
        goto values_loop_end;
      }
    }
-
-  while ((values= its++))
+  for (ulong iteration= 0; iteration < bulk_iterations; iteration++)
Change to do--while as we are always executing this loop once.
OK

    {
-    if (fields.elements || !value_count)
+
+    if (iteration && bulk_parameters_set(thd))
+      goto abort;
Wouldn't a better name be 'get_bulk_arguments_from_client()'
This would more closely reflect what the function does.

Can you please send me a copy of your sql_insert.cc. It was very hard
to review some parts of this file as there was many indententation
changes. I tried to apply your patch to my 10.2 version, but it didn't
apply.


@@ -1445,6 +1517,7 @@ bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
    /* Prepare the fields in the statement. */
    if (values)
    {
+
Remove extra empty line

      /* if we have INSERT ... VALUES () we cannot have a GROUP BY clause */
      DBUG_ASSERT (!select_lex->group_list.elements);

@@ -1463,6 +1536,10 @@ 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);
Is the above call always needed?

diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc
@@ -960,11 +973,65 @@ static bool insert_params(Prepared_statement *stmt, uchar *null_array,
  }


+static bool insert_bulk_params(Prepared_statement *stmt,
+                               uchar **read_pos, uchar *data_end,
+                               bool reset)
+{
+  Item_param **begin= stmt->param_array;
+  Item_param **end= begin + stmt->param_count;
+
+  DBUG_ENTER("insert_params");
+
+  for (Item_param **it= begin; it < end; ++it)
+  {
+    Item_param *param= *it;
+    if (reset)
+      param->reset();
+    if (param->state != Item_param::LONG_DATA_VALUE)
+    {
+      if (param->indicators)
+        param->indicator= *((*read_pos)++);
+      else
+        param->indicator= STMT_INDICATOR_NONE;
+      if ((*read_pos) > data_end)
+        DBUG_RETURN(1);
+      switch (param->indicator) {
move { to next line
OK

+      case STMT_INDICATOR_NONE:
+        if ((*read_pos) >= data_end)
+          DBUG_RETURN(1);
+        param->set_param_func(param, read_pos, (uint) (data_end - (*read_pos)));
+        if (param->state == Item_param::NO_VALUE)
+          DBUG_RETURN(1);
+        break;
+      case STMT_INDICATOR_NULL:
+        param->set_null();
+        break;
+      case STMT_INDICATOR_DEFAULT:
+        if (param->set_default(TRUE))
+          DBUG_RETURN(1);
If all the default handling is about above handling, we should be able to
do things much simpler by just not marking the field to have given a value and
let the normal default handling take over.
fixed


+        break;
+      }
+    }
+    /*
+      A long data stream was supplied for this parameter marker.
+      This was done after prepare, prior to providing a placeholder
+      type (the types are supplied at execute). Check that the
+      supplied type of placeholder can accept a data stream.
+    */
+    else
Move comment after else. Check also that comment is accurate as there
is no checks below.
OK, it is irrelevant, removed.
+      DBUG_RETURN(1); // long is not supported here
+  }
+  DBUG_RETURN(0);
@@ -2982,12 +3056,16 @@ void mysqld_stmt_execute(THD *thd, char *packet_arg, uint packet_length)
    thd->profiling.set_query_source(stmt->query(), stmt->query_length());
  #endif
    DBUG_PRINT("exec_query", ("%s", stmt->query()));
-  DBUG_PRINT("info",("stmt: 0x%lx", (long) stmt));
+  DBUG_PRINT("info",("stmt: 0x%lx iterations: %lu", (long) stmt, iterations));

When changing a DBUG_PRINT that contains 0x%lx, please change to %p
and remove the cast to long.
OK
<cut>

+my_bool Prepared_statement::set_bulk_parameters(bool reset)
+{
+  DBUG_ENTER("Prepared_statement::set_bulk_parameters");
+  DBUG_PRINT("info", ("iteration: %lu", iterations));
+  if (iterations)
+  {
+#ifndef EMBEDDED_LIBRARY
+    if ((*set_bulk_params)(this, &packet, packet_end, reset))
+#else
+      DBUG_ASSERT(0); //TODO: support bulk parameters for embedded server
+#endif
Please add an error instead when trying to do a bulk operations on the
client side in the embedded server. We shouldn't crash because
something isn't supported.
done

+bool
+Prepared_statement::execute_bulk_loop(String *expanded_query,
+                                      bool open_cursor,
+                                      uchar *packet_arg,
+                                      uchar *packet_end_arg,
+                                      ulong iterations_arg)
+{
+  Reprepare_observer reprepare_observer;
+  bool error= 0;
+  packet= packet_arg;
+  packet_end= packet_end_arg;
+  iterations= iterations_arg;
+  start_param= true;
+#ifndef DBUG_OFF
+  Item *free_list_state= thd->free_list;
+#endif
+  thd->select_number= select_number_after_prepare;
+  thd->set_bulk_execution((void *)this);
+  /* Check if we got an error when sending long data */
+  if (state == Query_arena::STMT_ERROR)
+  {
+    my_message(last_errno, last_error, MYF(0));
+    thd->set_bulk_execution(0);
+    return TRUE;
+  }
+
+  if (!(sql_command_flags[lex->sql_command] & CF_SP_BULK_SAFE))
+  {
+    my_error(ER_UNSUPPORTED_PS, MYF(0));
+    thd->set_bulk_execution(0);
+    return TRUE;
+  }
+
+#ifndef EMBEDDED_LIBRARY
+  if (setup_conversion_functions(this, &packet, packet_end, TRUE))
+#else
+  DBUG_ASSERT(0); //TODO: support bulk parameters for embedded server
+#endif
+  {
+    my_error(ER_WRONG_ARGUMENTS, MYF(0),
+            "mysqld_stmt_bulk_execute");
+    reset_stmt_params(this);
+    thd->set_bulk_execution(0);
+    return true;
+  }
+
+#ifdef NOT_YET_FROM_MYSQL_5_6
+  if (unlikely(thd->security_ctx->password_expired &&
+               !lex->is_change_password))
+  {
+    my_error(ER_MUST_CHANGE_PASSWORD, MYF(0));
+    thd->set_bulk_execution(0);
+    return true;
+  }
+#endif
+
+  while((iterations || start_param) && !error && !thd->is_error())
Please add space after while

Add also a comment how the code works.
As 'iterations' doesn't change at all in the loop, the above while is a bit
strange.
OK
+  {
+    int reprepare_attempt= 0;
+
Add a comment how the following code works.
I understand that if there is an bulk insert, then the loop that is fetching
data will happen mysql_insert(). However I don't understand how this will work
with other bulk operations that doesn't have SP_BULK_OPTIMZED set.
OK
+    if (!(sql_command_flags[lex->sql_command] & CF_SP_BULK_OPTIMIZED))
+    {
+      if (set_bulk_parameters(TRUE))
+      {
+        thd->set_bulk_execution(0);
+        return true;
+      }
+    }
+
+reexecute:
---------------------

Conclusion:

Most of bulk insert code looks ok. Some missing features (embedded
server error handling, no binlog logging) that must be fixed before
pushing (at least binlog logging is critical).
There is binloging, but only row mode (you idea BTW).

However, I don't like the default handling in the new code:
- Too much new code and complex code (a lot of similar functions that
   works a slightly different)
- A lot of extra overhead to loop over all fields and values, that is not
   needed.
- Totally different DEFAULT handling compared to how things are normally done.

I would prefer that all the new default handling would be removed and instead
we should do things the "normal way":
- If a field is given value of DEFAULT, it should not be given a value. In
   this case the normal default handling should be able to take over and
   handle the case.

   If this is impossible, then we should try to ensure that the code will work
   exactly as if we would have given column=DEFAULT in SQL.
   As we don't need any loops over all values for handling the above case
   we shouldn't need that for bulk inserts either.
default rewritten

I will be back working normally on Friday, so then we can discuss the DEFAULT
handling on IRC then.

I attached 2 diffs (all and only postreview changes as well as sql_insert.cc (also it rebased over relative modern 10.2)

Regards,
Monty


diff --git a/include/mysql.h.pp b/include/mysql.h.pp
index 857f5b9..c985792 100644
--- a/include/mysql.h.pp
+++ b/include/mysql.h.pp
@@ -11,11 +11,17 @@ enum enum_server_command
   COM_STMT_RESET, COM_SET_OPTION, COM_STMT_FETCH, COM_DAEMON,
   COM_MDB_GAP_BEG,
   COM_MDB_GAP_END=250,
-  COM_SLAVE_WORKER,
-  COM_SLAVE_IO,
-  COM_SLAVE_SQL,
-  COM_MULTI,
-  COM_END
+  COM_SLAVE_WORKER=251,
+  COM_SLAVE_IO=252,
+  COM_SLAVE_SQL=253,
+  COM_MULTI=254,
+  COM_END=255
+};
+enum enum_indicator_type
+{
+  STMT_INDICATOR_NONE= 0,
+  STMT_INDICATOR_NULL,
+  STMT_INDICATOR_DEFAULT
 };
 struct st_vio;
 typedef struct st_vio Vio;
diff --git a/include/mysql_com.h b/include/mysql_com.h
index 461800f..28d2d75 100644
--- a/include/mysql_com.h
+++ b/include/mysql_com.h
@@ -114,12 +114,23 @@ enum enum_server_command
   /* don't forget to update const char *command_name[] in sql_parse.cc */
   COM_MDB_GAP_BEG,
   COM_MDB_GAP_END=250,
-  COM_SLAVE_WORKER,
-  COM_SLAVE_IO,
-  COM_SLAVE_SQL,
-  COM_MULTI,
+  COM_SLAVE_WORKER=251,
+  COM_SLAVE_IO=252,
+  COM_SLAVE_SQL=253,
+  COM_MULTI=254,
   /* Must be last */
-  COM_END
+  COM_END=255
+};
+
+
+/*
+  Bulk PS protocol indicator value:
+*/
+enum enum_indicator_type
+{
+  STMT_INDICATOR_NONE= 0,
+  STMT_INDICATOR_NULL,
+  STMT_INDICATOR_DEFAULT
 };
 
 /* sql type stored in .frm files for virtual fields */
@@ -256,6 +267,8 @@ enum enum_server_command
 #define MARIADB_CLIENT_PROGRESS (1ULL << 32)
 /* support COM_MULTI */
 #define MARIADB_CLIENT_COM_MULTI (1ULL << 33)
+/* support of array binding */
+#define MARIADB_CLIENT_STMT_BULK_OPERATIONS (1UL << 34)
 
 #ifdef HAVE_COMPRESS
 #define CAN_CLIENT_COMPRESS CLIENT_COMPRESS
@@ -295,7 +308,8 @@ enum enum_server_command
                            CLIENT_SESSION_TRACK |\
                            CLIENT_DEPRECATE_EOF |\
                            CLIENT_CONNECT_ATTRS |\
-                           MARIADB_CLIENT_COM_MULTI)
+                           MARIADB_CLIENT_COM_MULTI |\
+                           MARIADB_CLIENT_STMT_BULK_OPERATIONS)
 
 /*
   To be added later:
diff --git a/libmariadb b/libmariadb
index c8dd089..923e619 160000
--- a/libmariadb
+++ b/libmariadb
@@ -1 +1 @@
-Subproject commit c8dd0899d484ad698ec2da5bc8e3d19ff8b623b9
+Subproject commit 923e6199b9d98c0d947c827ec6b2e0ba29d1f07a-dirty
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 448e34b..56d0b04 100644
--- a/sql/item.cc
+++ b/sql/item.cc
@@ -724,7 +724,9 @@ Item_ident::Item_ident(THD *thd, Name_resolution_context *context_arg,
 		       const char *field_name_arg)
   :Item_result_field(thd), orig_db_name(db_name_arg),
    orig_table_name(table_name_arg),
-   orig_field_name(field_name_arg), context(context_arg),
+   orig_field_name(field_name_arg),
+   default_value_target(0),
+   context(context_arg),
    db_name(db_name_arg), table_name(table_name_arg),
    field_name(field_name_arg),
    alias_name_used(FALSE), cached_field_index(NO_CACHED_FIELD_INDEX),
@@ -737,7 +739,9 @@ Item_ident::Item_ident(THD *thd, Name_resolution_context *context_arg,
 Item_ident::Item_ident(THD *thd, TABLE_LIST *view_arg, const char *field_name_arg)
   :Item_result_field(thd), orig_db_name(NullS),
    orig_table_name(view_arg->table_name),
-   orig_field_name(field_name_arg), context(&view_arg->view->select_lex.context),
+   orig_field_name(field_name_arg),
+   default_value_target(0),
+   context(&view_arg->view->select_lex.context),
    db_name(NullS), table_name(view_arg->alias),
    field_name(field_name_arg),
    alias_name_used(FALSE), cached_field_index(NO_CACHED_FIELD_INDEX),
@@ -756,6 +760,7 @@ Item_ident::Item_ident(THD *thd, Item_ident *item)
    orig_db_name(item->orig_db_name),
    orig_table_name(item->orig_table_name), 
    orig_field_name(item->orig_field_name),
+   default_value_target(item->default_value_target),
    context(item->context),
    db_name(item->db_name),
    table_name(item->table_name),
@@ -784,6 +789,7 @@ void Item_ident::cleanup()
     */
     can_be_depended= MY_TEST(depended_from);
   }
+  default_value_target= NULL;
   DBUG_VOID_RETURN;
 }
 
@@ -3228,6 +3234,7 @@ Item_param::Item_param(THD *thd, uint pos_in_query_arg):
   Rewritable_query_parameter(pos_in_query_arg, 1),
   Type_handler_hybrid_field_type(MYSQL_TYPE_VARCHAR),
   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),
   set_param_func(default_set_param_func),
@@ -3592,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);
@@ -3637,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);
   }
@@ -3664,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:
@@ -3691,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:
@@ -3726,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:
@@ -3804,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:
@@ -3854,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:
@@ -3886,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:
@@ -3909,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];
@@ -3960,6 +3996,11 @@ Item_param::set_param_type_and_swap_value(Item_param *src)
 }
 
 
+void Item_param::set_default()
+{
+  state= DEFAULT_VALUE;
+}
+
 /**
   This operation is intended to store some item value in Item_param to be
   used later.
@@ -8571,42 +8612,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 28a1445..f3736a4 100644
--- a/sql/item.h
+++ b/sql/item.h
@@ -2425,6 +2425,8 @@ class Item_ident :public Item_result_field
   const char *orig_table_name;
   const char *orig_field_name;
 
+  Item *default_value_target;
+
 public:
   Name_resolution_context *context;
   const char *db_name;
@@ -2776,6 +2778,9 @@ class Item_null_result :public Item_null
   }
 };
 
+
+class Item_default_value;
+
 /*
   Item represents one placeholder ('?') of prepared statement
 
@@ -2798,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
@@ -2843,6 +2848,13 @@ class Item_param :public Item_basic_value,
   };
 
   /*
+    Used for bulk protocol. Indicates if we should expect
+    indicators byte before value of the parameter
+  */
+  my_bool indicators;
+  enum enum_indicator_type indicator;
+
+  /*
     A buffer for string and long data values. Historically all allocated
     values returned from val_str() were treated as eligible to
     modification. I. e. in some cases Item_func_concat can append it's
@@ -2882,6 +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);
 
+  void set_default();
   void set_null();
   void set_int(longlong i, uint32 max_length_arg);
   void set_double(double i);
@@ -5086,6 +5099,10 @@ class Item_default_value : public Item_field
     :Item_field(thd, context_arg, (const char *)NULL, (const char *)NULL,
                 (const char *)NULL),
      arg(a) {}
+  Item_default_value(THD *thd, Name_resolution_context *context_arg, Field *a)
+    :Item_field(thd, context_arg, (const char *)NULL, (const char *)NULL,
+                (const char *)NULL),
+     arg(NULL) {}
   enum Type type() const { return DEFAULT_VALUE_ITEM; }
   bool eq(const Item *item, bool binary_cmp) const;
   bool fix_fields(THD *, Item **);
diff --git a/sql/protocol.cc b/sql/protocol.cc
index a3085c6..f8b68c0 100644
--- a/sql/protocol.cc
+++ b/sql/protocol.cc
@@ -572,6 +572,7 @@ void Protocol::end_statement()
                     thd->get_stmt_da()->statement_warn_count());
     break;
   case Diagnostics_area::DA_OK:
+  case Diagnostics_area::DA_OK_BULK:
     error= send_ok(thd->server_status,
                    thd->get_stmt_da()->statement_warn_count(),
                    thd->get_stmt_da()->affected_rows(),
diff --git a/sql/share/errmsg-utf8.txt b/sql/share/errmsg-utf8.txt
index d42611b..ade78862 100644
--- a/sql/share/errmsg-utf8.txt
+++ b/sql/share/errmsg-utf8.txt
@@ -7232,3 +7232,8 @@ ER_PARTITION_DEFAULT_ERROR
         ukr "Припустимо мати тільки один DEFAULT розділ" 
 ER_REFERENCED_TRG_DOES_NOT_EXIST
   eng "Referenced trigger '%s' for the given action time and event type does not exist"
+ER_INVALID_DEFAULT_PARAM
+        eng "Default value is not supported for such parameter usage"
+        ukr "Значення за замовчуванням не підтримано для цього випадку використання параьетра"
+ER_BINLOG_NON_SUPPORTED_BULK
+        eng "Only row based replication supported for bulk operations"
diff --git a/sql/sql_base.cc b/sql/sql_base.cc
index d3832a7..2d40631 100644
--- a/sql/sql_base.cc
+++ b/sql/sql_base.cc
@@ -7810,9 +7810,10 @@ fill_record(THD *thd, TABLE *table_arg, List<Item> &fields, List<Item> &values,
     if (table->next_number_field &&
         rfield->field_index ==  table->next_number_field->field_index)
       table->auto_increment_field_not_null= TRUE;
-    if (rfield->vcol_info && 
-        value->type() != Item::DEFAULT_VALUE_ITEM && 
-        value->type() != Item::NULL_ITEM &&
+    Item::Type type= value->type();
+    if (rfield->vcol_info &&
+        type != Item::DEFAULT_VALUE_ITEM &&
+        type != Item::NULL_ITEM &&
         table->s->table_category != TABLE_CATEGORY_TEMPORARY)
     {
       push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
@@ -8060,15 +8061,18 @@ fill_record(THD *thd, TABLE *table, Field **ptr, List<Item> &values,
     value=v++;
     if (field->field_index == autoinc_index)
       table->auto_increment_field_not_null= TRUE;
-    if (field->vcol_info && 
-        value->type() != Item::DEFAULT_VALUE_ITEM && 
-        value->type() != Item::NULL_ITEM &&
-        table->s->table_category != TABLE_CATEGORY_TEMPORARY)
+    if (field->vcol_info)
     {
-      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
-                          ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN,
-                          ER_THD(thd, ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
-                          field->field_name, table->s->table_name.str);
+      Item::Type type= value->type();
+      if (type != Item::DEFAULT_VALUE_ITEM &&
+          type != Item::NULL_ITEM &&
+          table->s->table_category != TABLE_CATEGORY_TEMPORARY)
+      {
+        push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
+                            ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN,
+                            ER_THD(thd, ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
+                            field->field_name, table->s->table_name.str);
+      }
     }
 
     if (use_value)
diff --git a/sql/sql_class.cc b/sql/sql_class.cc
index 1af3b9a..b896f45 100644
--- a/sql/sql_class.cc
+++ b/sql/sql_class.cc
@@ -854,6 +854,7 @@ THD::THD(my_thread_id id, bool is_wsrep_applier)
    in_sub_stmt(0), log_all_errors(0),
    binlog_unsafe_warning_flags(0),
    binlog_table_maps(0),
+   bulk_param(0),
    table_map_for_update(0),
    m_examined_row_count(0),
    accessed_rows_and_keys(0),
@@ -5701,6 +5702,17 @@ int THD::decide_logging_format(TABLE_LIST *tables)
       !(wsrep_binlog_format() == BINLOG_FORMAT_STMT &&
         !binlog_filter->db_ok(db)))
   {
+
+    if (is_bulk_op())
+    {
+      if (wsrep_binlog_format() == BINLOG_FORMAT_STMT)
+      {
+        my_error(ER_BINLOG_NON_SUPPORTED_BULK, MYF(0));
+        DBUG_PRINT("info",
+                   ("decision: no logging since an error was generated"));
+        DBUG_RETURN(-1);
+      }
+    }
     /*
       Compute one bit field with the union of all the engine
       capabilities, and one with the intersection of all the engine
@@ -5959,7 +5971,7 @@ int THD::decide_logging_format(TABLE_LIST *tables)
         */
         my_error((error= ER_BINLOG_ROW_INJECTION_AND_STMT_ENGINE), MYF(0));
       }
-      else if (wsrep_binlog_format() == BINLOG_FORMAT_ROW &&
+      else if ((wsrep_binlog_format() == BINLOG_FORMAT_ROW || is_bulk_op()) &&
                sqlcom_can_generate_row_events(this))
       {
         /*
@@ -6032,7 +6044,8 @@ int THD::decide_logging_format(TABLE_LIST *tables)
       else
       {
         if (lex->is_stmt_unsafe() || lex->is_stmt_row_injection()
-            || (flags_write_all_set & HA_BINLOG_STMT_CAPABLE) == 0)
+            || (flags_write_all_set & HA_BINLOG_STMT_CAPABLE) == 0 ||
+            is_bulk_op())
         {
           /* log in row format! */
           set_current_stmt_binlog_format_row_if_mixed();
diff --git a/sql/sql_class.h b/sql/sql_class.h
index 994a161..93d3a4a 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -2463,6 +2463,8 @@ class THD :public Statement,
   */
   Query_arena *stmt_arena;
 
+  void *bulk_param;
+
   /*
     map for tables that will be updated for a multi-table update query
     statement, for other query statements, this will be zero.
@@ -3438,6 +3440,12 @@ class THD :public Statement,
     To raise this flag, use my_error().
   */
   inline bool is_error() const { return m_stmt_da->is_error(); }
+  void set_bulk_execution(void *bulk)
+  {
+    bulk_param= bulk;
+    m_stmt_da->set_bulk_execution(MY_TEST(bulk));
+  }
+  bool is_bulk_op() const { return MY_TEST(bulk_param); }
 
   /// Returns Diagnostics-area for the current statement.
   Diagnostics_area *get_stmt_da()
@@ -5510,6 +5518,15 @@ class select_dumpvar :public select_result_interceptor {
 */
 #define CF_UPDATES_DATA (1U << 18)
 
+/**
+  SP Bulk execution safe
+*/
+#define CF_SP_BULK_SAFE (1U << 19)
+/**
+  SP Bulk execution optimized
+*/
+#define CF_SP_BULK_OPTIMIZED (1U << 20)
+
 /* Bits in server_command_flags */
 
 /**
diff --git a/sql/sql_error.cc b/sql/sql_error.cc
index 1d234c5..d14c7b8 100644
--- a/sql/sql_error.cc
+++ b/sql/sql_error.cc
@@ -320,7 +320,7 @@ Sql_condition::set_sqlstate(const char* sqlstate)
 }
 
 Diagnostics_area::Diagnostics_area(bool initialize)
-  : m_main_wi(0, false, initialize)
+  : is_bulk_execution(0), m_main_wi(0, false, initialize)
 {
   push_warning_info(&m_main_wi);
 
@@ -330,7 +330,8 @@ Diagnostics_area::Diagnostics_area(bool initialize)
 Diagnostics_area::Diagnostics_area(ulonglong warning_info_id,
                                    bool allow_unlimited_warnings,
                                    bool initialize)
-  : m_main_wi(warning_info_id, allow_unlimited_warnings, initialize)
+  : is_bulk_execution(0),
+  m_main_wi(warning_info_id, allow_unlimited_warnings, initialize)
 {
   push_warning_info(&m_main_wi);
 
@@ -376,22 +377,33 @@ Diagnostics_area::set_ok_status(ulonglong affected_rows,
                                 const char *message)
 {
   DBUG_ENTER("set_ok_status");
-  DBUG_ASSERT(! is_set());
+  DBUG_ASSERT(!is_set() || (m_status == DA_OK_BULK && is_bulk_op()));
   /*
     In production, refuse to overwrite an error or a custom response
     with an OK packet.
   */
   if (is_error() || is_disabled())
     return;
-
-  m_statement_warn_count= current_statement_warn_count();
-  m_affected_rows= affected_rows;
+  /*
+    When running a bulk operation, m_status will be DA_OK for the first
+    operation and set to DA_OK_BULK for all following operations.
+  */
+  if (m_status == DA_OK_BULK)
+  {
+    m_statement_warn_count+= current_statement_warn_count();
+    m_affected_rows+= affected_rows;
+  }
+  else
+  {
+    m_statement_warn_count= current_statement_warn_count();
+    m_affected_rows= affected_rows;
+    m_status= (is_bulk_op() ? DA_OK_BULK : DA_OK);
+  }
   m_last_insert_id= last_insert_id;
   if (message)
     strmake_buf(m_message, message);
   else
     m_message[0]= '\0';
-  m_status= DA_OK;
   DBUG_VOID_RETURN;
 }
 
diff --git a/sql/sql_error.h b/sql/sql_error.h
index 8fb1aba..aa8e6c6b 100644
--- a/sql/sql_error.h
+++ b/sql/sql_error.h
@@ -658,6 +658,8 @@ class Diagnostics_area
     DA_OK,
     /** Set whenever one calls my_eof(). */
     DA_EOF,
+    /** Set whenever one calls my_ok() in PS bulk mode. */
+    DA_OK_BULK,
     /** Set whenever one calls my_error() or my_message(). */
     DA_ERROR,
     /** Set in case of a custom response, such as one from COM_STMT_PREPARE. */
@@ -699,13 +701,21 @@ class Diagnostics_area
 
   bool is_disabled() const { return m_status == DA_DISABLED; }
 
+  void set_bulk_execution(bool bulk) { is_bulk_execution= bulk; }
+
+  bool is_bulk_op() const { return is_bulk_execution; }
+
   enum_diagnostics_status status() const { return m_status; }
 
   const char *message() const
-  { DBUG_ASSERT(m_status == DA_ERROR || m_status == DA_OK); return m_message; }
+  { DBUG_ASSERT(m_status == DA_ERROR || m_status == DA_OK ||
+                m_status == DA_OK_BULK); return m_message; }
 
   bool skip_flush() const
-  { DBUG_ASSERT(m_status == DA_OK); return m_skip_flush; }
+  {
+    DBUG_ASSERT(m_status == DA_OK || m_status == DA_OK_BULK);
+    return m_skip_flush;
+  }
 
   void set_skip_flush()
   { m_skip_flush= TRUE; }
@@ -717,14 +727,21 @@ class Diagnostics_area
   { DBUG_ASSERT(m_status == DA_ERROR); return m_sqlstate; }
 
   ulonglong affected_rows() const
-  { DBUG_ASSERT(m_status == DA_OK); return m_affected_rows; }
+  {
+    DBUG_ASSERT(m_status == DA_OK || m_status == DA_OK_BULK);
+    return m_affected_rows;
+  }
 
   ulonglong last_insert_id() const
-  { DBUG_ASSERT(m_status == DA_OK); return m_last_insert_id; }
+  {
+    DBUG_ASSERT(m_status == DA_OK || m_status == DA_OK_BULK);
+    return m_last_insert_id;
+  }
 
   uint statement_warn_count() const
   {
-    DBUG_ASSERT(m_status == DA_OK || m_status == DA_EOF);
+    DBUG_ASSERT(m_status == DA_OK || m_status == DA_OK_BULK ||
+                m_status == DA_EOF);
     return m_statement_warn_count;
   }
 
@@ -907,6 +924,8 @@ class Diagnostics_area
 
   enum_diagnostics_status m_status;
 
+  my_bool is_bulk_execution;
+
   Warning_info m_main_wi;
 
   Warning_info_list m_wi_stack;
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index 1abfd59..432f8b6 100644
--- a/sql/sql_insert.cc
+++ b/sql/sql_insert.cc
@@ -77,6 +77,7 @@
 #include "transaction.h"
 #include "sql_audit.h"
 #include "sql_derived.h"                        // mysql_handle_derived
+#include "sql_prepare.h"
 
 #include "debug_sync.h"
 
@@ -661,7 +662,9 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
   bool using_bulk_insert= 0;
   uint value_count;
   ulong counter = 1;
+  ulong iteration= 0;
   ulonglong id;
+  ulong bulk_iterations= bulk_parameters_iterations(thd);
   COPY_INFO info;
   TABLE *table= 0;
   List_iterator_fast<List_item> its(values_list);
@@ -725,8 +728,11 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
   THD_STAGE_INFO(thd, stage_init);
   thd->lex->used_tables=0;
   values= its++;
+  if (bulk_parameters_set(thd))
+    DBUG_RETURN(TRUE);
   value_count= values->elements;
 
+  DBUG_ASSERT(bulk_iterations > 0);
   if (mysql_prepare_insert(thd, table_list, table, fields, values,
 			   update_fields, update_values, duplic, &unused_conds,
                            FALSE,
@@ -885,106 +891,114 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
       goto values_loop_end;
     }
   }
-
-  while ((values= its++))
+  do
   {
-    if (fields.elements || !value_count)
+    if (iteration && bulk_parameters_set(thd))
+      goto abort;
+
+    while ((values= its++))
     {
-      /*
-        There are possibly some default values:
-        INSERT INTO t1 (fields) VALUES ...
-        INSERT INTO t1 VALUES ()
-      */
-      restore_record(table,s->default_values);	// Get empty record
-      table->reset_default_fields();
-      if (fill_record_n_invoke_before_triggers(thd, table, fields, *values, 0,
-                                               TRG_EVENT_INSERT))
+      if (fields.elements || !value_count)
       {
-	if (values_list.elements != 1 && ! thd->is_error())
-	{
-	  info.records++;
-	  continue;
-	}
-	/*
-	  TODO: set thd->abort_on_warning if values_list.elements == 1
-	  and check that all items return warning in case of problem with
-	  storing field.
+        /*
+          There are possibly some default values:
+          INSERT INTO t1 (fields) VALUES ...
+          INSERT INTO t1 VALUES ()
         */
-	error=1;
-	break;
+        restore_record(table,s->default_values);	// Get empty record
+        table->reset_default_fields();
+        if (fill_record_n_invoke_before_triggers(thd, table, fields, *values, 0,
+                                                 TRG_EVENT_INSERT))
+        {
+          if (values_list.elements != 1 && ! thd->is_error())
+          {
+            info.records++;
+            continue;
+          }
+          /*
+            TODO: set thd->abort_on_warning if values_list.elements == 1
+	    and check that all items return warning in case of problem with
+	    storing field.
+          */
+	  error=1;
+	  break;
+        }
       }
-    }
-    else
-    {
-      /*
-        No field list, all fields are set explicitly:
-        INSERT INTO t1 VALUES (values)
-      */
-      if (thd->lex->used_tables)		      // Column used in values()
-	restore_record(table,s->default_values);	// Get empty record
       else
       {
-        TABLE_SHARE *share= table->s;
-
         /*
-          Fix delete marker. No need to restore rest of record since it will
-          be overwritten by fill_record() anyway (and fill_record() does not
-          use default values in this case).
+          No field list, all fields are set explicitly:
+          INSERT INTO t1 VALUES (values)
         */
-#ifdef HAVE_valgrind
-        if (table->file->ha_table_flags() && HA_RECORD_MUST_BE_CLEAN_ON_WRITE)
-          restore_record(table,s->default_values);	// Get empty record
+        if (thd->lex->used_tables)		      // Column used in values()
+	  restore_record(table,s->default_values);	// Get empty record
         else
+        {
+          TABLE_SHARE *share= table->s;
+
+          /*
+            Fix delete marker. No need to restore rest of record since it will
+            be overwritten by fill_record() anyway (and fill_record() does not
+            use default values in this case).
+          */
+#ifdef HAVE_valgrind
+          if (table->file->ha_table_flags() && HA_RECORD_MUST_BE_CLEAN_ON_WRITE)
+            restore_record(table,s->default_values);	// Get empty record
+          else
 #endif
-          table->record[0][0]= share->default_values[0];
+            table->record[0][0]= share->default_values[0];
 
-        /* Fix undefined null_bits. */
-        if (share->null_bytes > 1 && share->last_null_bit_pos)
+          /* Fix undefined null_bits. */
+          if (share->null_bytes > 1 && share->last_null_bit_pos)
+          {
+            table->record[0][share->null_bytes - 1]=
+              share->default_values[share->null_bytes - 1];
+          }
+        }
+        if (fill_record_n_invoke_before_triggers(thd, table,
+                                                 table->field_to_fill(),
+                                                 *values, 0, TRG_EVENT_INSERT))
         {
-          table->record[0][share->null_bytes - 1]= 
-            share->default_values[share->null_bytes - 1];
+          if (values_list.elements != 1 && ! thd->is_error())
+	  {
+	    info.records++;
+	    continue;
+	  }
+	  error=1;
+	  break;
         }
       }
-      if (fill_record_n_invoke_before_triggers(thd, table, table->field_to_fill(),
-                                               *values, 0, TRG_EVENT_INSERT))
+
+      if ((res= table_list->view_check_option(thd,
+                                              (values_list.elements == 1 ?
+                                               0 :
+                                               ignore))) ==
+          VIEW_CHECK_SKIP)
+        continue;
+      else if (res == VIEW_CHECK_ERROR)
       {
-	if (values_list.elements != 1 && ! thd->is_error())
-	{
-	  info.records++;
-	  continue;
-	}
-	error=1;
-	break;
+        error= 1;
+        break;
       }
-    }
-
-    if ((res= table_list->view_check_option(thd,
-					    (values_list.elements == 1 ?
-					     0 :
-					     ignore))) ==
-        VIEW_CHECK_SKIP)
-      continue;
-    else if (res == VIEW_CHECK_ERROR)
-    {
-      error= 1;
-      break;
-    }
 #ifndef EMBEDDED_LIBRARY
-    if (lock_type == TL_WRITE_DELAYED)
-    {
-      LEX_STRING const st_query = { query, thd->query_length() };
-      DEBUG_SYNC(thd, "before_write_delayed");
-      error=write_delayed(thd, table, duplic, st_query, ignore, log_on);
-      DEBUG_SYNC(thd, "after_write_delayed");
-      query=0;
-    }
-    else
+      if (lock_type == TL_WRITE_DELAYED)
+      {
+        LEX_STRING const st_query = { query, thd->query_length() };
+        DEBUG_SYNC(thd, "before_write_delayed");
+        error=write_delayed(thd, table, duplic, st_query, ignore, log_on);
+        DEBUG_SYNC(thd, "after_write_delayed");
+        query=0;
+      }
+      else
 #endif
-      error=write_record(thd, table ,&info);
-    if (error)
-      break;
-    thd->get_stmt_da()->inc_current_row_for_warning();
-  }
+        error=write_record(thd, table ,&info);
+      if (error)
+        break;
+      thd->get_stmt_da()->inc_current_row_for_warning();
+    }
+    its.rewind();
+    iteration++;
+  } while (iteration < bulk_iterations);
 
 values_loop_end:
   free_underlaid_joins(thd, &thd->lex->select_lex);
@@ -1131,7 +1145,7 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
     retval= thd->lex->explain->send_explain(thd);
     goto abort;
   }
-  if (values_list.elements == 1 && (!(thd->variables.option_bits & OPTION_WARNINGS) ||
+  if ((bulk_iterations * values_list.elements) == 1 && (!(thd->variables.option_bits & OPTION_WARNINGS) ||
 				    !thd->cuted_fields))
   {
     my_ok(thd, info.copied + info.deleted +
diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc
index ddb5e27..74b5ac7 100644
--- a/sql/sql_parse.cc
+++ b/sql/sql_parse.cc
@@ -570,17 +570,19 @@ void init_update_queries(void)
                                             CF_CAN_GENERATE_ROW_EVENTS |
                                             CF_OPTIMIZER_TRACE |
                                             CF_CAN_BE_EXPLAINED |
-                                            CF_UPDATES_DATA;
+                                            CF_UPDATES_DATA | CF_SP_BULK_SAFE;
   sql_command_flags[SQLCOM_UPDATE_MULTI]=   CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
                                             CF_CAN_GENERATE_ROW_EVENTS |
                                             CF_OPTIMIZER_TRACE |
                                             CF_CAN_BE_EXPLAINED |
-                                            CF_UPDATES_DATA;
+                                            CF_UPDATES_DATA | CF_SP_BULK_SAFE;
   sql_command_flags[SQLCOM_INSERT]=	    CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
                                             CF_CAN_GENERATE_ROW_EVENTS |
                                             CF_OPTIMIZER_TRACE |
                                             CF_CAN_BE_EXPLAINED |
-                                            CF_INSERTS_DATA;
+                                            CF_INSERTS_DATA |
+                                            CF_SP_BULK_SAFE |
+                                            CF_SP_BULK_OPTIMIZED;
   sql_command_flags[SQLCOM_INSERT_SELECT]=  CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
                                             CF_CAN_GENERATE_ROW_EVENTS |
                                             CF_OPTIMIZER_TRACE |
@@ -598,7 +600,7 @@ void init_update_queries(void)
                                             CF_CAN_GENERATE_ROW_EVENTS |
                                             CF_OPTIMIZER_TRACE |
                                             CF_CAN_BE_EXPLAINED |
-                                            CF_INSERTS_DATA;;
+                                            CF_INSERTS_DATA | CF_SP_BULK_SAFE;
   sql_command_flags[SQLCOM_REPLACE_SELECT]= CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE |
                                             CF_CAN_GENERATE_ROW_EVENTS |
                                             CF_OPTIMIZER_TRACE |
diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc
index ac39833..80e6f8f 100644
--- a/sql/sql_prepare.cc
+++ b/sql/sql_prepare.cc
@@ -162,6 +162,9 @@ class Prepared_statement: public Statement
   Select_fetch_protocol_binary result;
   Item_param **param_array;
   Server_side_cursor *cursor;
+  uchar *packet;
+  uchar *packet_end;
+  ulong iterations;
   uint param_count;
   uint last_errno;
   uint flags;
@@ -180,11 +183,15 @@ class Prepared_statement: public Statement
   */
   uint select_number_after_prepare;
   char last_error[MYSQL_ERRMSG_SIZE];
+  my_bool start_param;
 #ifndef EMBEDDED_LIBRARY
   bool (*set_params)(Prepared_statement *st, uchar *data, uchar *data_end,
                      uchar *read_pos, String *expanded_query);
+  bool (*set_bulk_params)(Prepared_statement *st,
+                          uchar **read_pos, uchar *data_end, bool reset);
 #else
   bool (*set_params_data)(Prepared_statement *st, String *expanded_query);
+  /*TODO: add bulk support for builtin server */
 #endif
   bool (*set_params_from_actual_params)(Prepared_statement *stmt,
                                         List<Item> &list,
@@ -204,7 +211,13 @@ class Prepared_statement: public Statement
   bool execute_loop(String *expanded_query,
                     bool open_cursor,
                     uchar *packet_arg, uchar *packet_end_arg);
+  bool execute_bulk_loop(String *expanded_query,
+                         bool open_cursor,
+                         uchar *packet_arg, uchar *packet_end_arg,
+                         ulong iterations);
   bool execute_server_runnable(Server_runnable *server_runnable);
+  my_bool set_bulk_parameters(bool reset);
+  ulong bulk_iterations();
   /* Destroy this statement */
   void deallocate();
   bool execute_immediate(const char *query, uint query_length);
@@ -962,11 +975,59 @@ static bool insert_params(Prepared_statement *stmt, uchar *null_array,
 }
 
 
+static bool insert_bulk_params(Prepared_statement *stmt,
+                               uchar **read_pos, uchar *data_end,
+                               bool reset)
+{
+  Item_param **begin= stmt->param_array;
+  Item_param **end= begin + stmt->param_count;
+
+  DBUG_ENTER("insert_params");
+
+  for (Item_param **it= begin; it < end; ++it)
+  {
+    Item_param *param= *it;
+    if (reset)
+      param->reset();
+    if (param->state != Item_param::LONG_DATA_VALUE)
+    {
+      if (param->indicators)
+        param->indicator= (enum enum_indicator_type) *((*read_pos)++);
+      else
+        param->indicator= STMT_INDICATOR_NONE;
+      if ((*read_pos) > data_end)
+        DBUG_RETURN(1);
+      switch (param->indicator)
+      {
+      case STMT_INDICATOR_NONE:
+        if ((*read_pos) >= data_end)
+          DBUG_RETURN(1);
+        param->set_param_func(param, read_pos, (uint) (data_end - (*read_pos)));
+        if (param->state == Item_param::NO_VALUE)
+          DBUG_RETURN(1);
+        break;
+      case STMT_INDICATOR_NULL:
+        param->set_null();
+        break;
+      case STMT_INDICATOR_DEFAULT:
+        param->set_default();
+        break;
+      }
+    }
+    else
+      DBUG_RETURN(1); // long is not supported here
+  }
+  DBUG_RETURN(0);
+}
+
 static bool setup_conversion_functions(Prepared_statement *stmt,
-                                       uchar **data, uchar *data_end)
+                                       uchar **data, uchar *data_end,
+                                       bool bulk_protocol= 0)
 {
   /* skip null bits */
-  uchar *read_pos= *data + (stmt->param_count+7) / 8;
+  uchar *read_pos= *data;
+  if (!bulk_protocol)
+    read_pos+= (stmt->param_count+7) / 8;
 
   DBUG_ENTER("setup_conversion_functions");
 
@@ -983,6 +1044,7 @@ static bool setup_conversion_functions(Prepared_statement *stmt,
     {
       ushort typecode;
       const uint signed_bit= 1 << 15;
+      const uint indicators_bit= 1 << 14;
 
       if (read_pos >= data_end)
         DBUG_RETURN(1);
@@ -990,7 +1052,10 @@ static bool setup_conversion_functions(Prepared_statement *stmt,
       typecode= sint2korr(read_pos);
       read_pos+= 2;
       (**it).unsigned_flag= MY_TEST(typecode & signed_bit);
-      setup_one_conversion_function(thd, *it, (uchar) (typecode & ~signed_bit));
+      if (bulk_protocol)
+        (**it).indicators= MY_TEST(typecode & indicators_bit);
+      setup_one_conversion_function(thd, *it,
+                                    (uchar) (typecode & 0xff));
     }
   }
   *data= read_pos;
@@ -999,6 +1064,8 @@ static bool setup_conversion_functions(Prepared_statement *stmt,
 
 #else
 
+//TODO: support bulk parameters
+
 /**
   Embedded counterparts of parameter assignment routines.
 
@@ -2996,6 +3063,7 @@ void mysqld_stmt_execute(THD *thd, char *packet_arg, uint packet_length)
   uchar *packet= (uchar*)packet_arg; // GCC 4.0.1 workaround
   ulong stmt_id= uint4korr(packet);
   ulong flags= (ulong) packet[4];
+  ulong iterations= uint4korr(packet + 5);
   /* Query text for binary, general or slow log, if any of them is open */
   String expanded_query;
   uchar *packet_end= packet + packet_length;
@@ -3021,12 +3089,16 @@ void mysqld_stmt_execute(THD *thd, char *packet_arg, uint packet_length)
   thd->profiling.set_query_source(stmt->query(), stmt->query_length());
 #endif
   DBUG_PRINT("exec_query", ("%s", stmt->query()));
-  DBUG_PRINT("info",("stmt: 0x%lx", (long) stmt));
+  DBUG_PRINT("info",("stmt: 0x%p iterations: %lu", stmt, iterations));
 
   open_cursor= MY_TEST(flags & (ulong) CURSOR_TYPE_READ_ONLY);
 
   thd->protocol= &thd->protocol_binary;
-  stmt->execute_loop(&expanded_query, open_cursor, packet, packet_end);
+  if (iterations <= 1)
+    stmt->execute_loop(&expanded_query, open_cursor, packet, packet_end);
+  else
+    stmt->execute_bulk_loop(&expanded_query, open_cursor, packet, packet_end,
+                            iterations);
   thd->protocol= save_protocol;
 
   sp_cache_enforce_limit(thd->sp_proc_cache, stored_program_cache_size);
@@ -3531,9 +3603,13 @@ Prepared_statement::Prepared_statement(THD *thd_arg)
   result(thd_arg),
   param_array(0),
   cursor(0),
+  packet(0),
+  packet_end(0),
+  iterations(0),
   param_count(0),
   last_errno(0),
-  flags((uint) IS_IN_USE)
+  flags((uint) IS_IN_USE),
+  start_param(0)
 {
   init_sql_alloc(&main_mem_root, thd_arg->variables.query_alloc_block_size,
                  thd_arg->variables.query_prealloc_size, MYF(MY_THREAD_SPECIFIC));
@@ -3569,7 +3645,9 @@ void Prepared_statement::setup_set_params()
     set_params_from_actual_params= insert_params_from_actual_params_with_log;
 #ifndef EMBEDDED_LIBRARY
     set_params= insert_params_with_log;
+    set_bulk_params= insert_bulk_params; // TODO: add binlog support
 #else
+    //TODO: add bulk support for bulk parameters
     set_params_data= emb_insert_params_with_log;
 #endif
   }
@@ -3578,7 +3656,9 @@ void Prepared_statement::setup_set_params()
     set_params_from_actual_params= insert_params_from_actual_params;
 #ifndef EMBEDDED_LIBRARY
     set_params= insert_params;
+    set_bulk_params= insert_bulk_params;
 #else
+    //TODO: add bulk support for bulk parameters
     set_params_data= emb_insert_params;
 #endif
   }
@@ -3935,6 +4015,7 @@ Prepared_statement::set_parameters(String *expanded_query,
   @retval  FALSE   successfully executed the statement, perhaps
                    after having reprepared it a few times.
 */
+const static int MAX_REPREPARE_ATTEMPTS= 3;
 
 bool
 Prepared_statement::execute_loop(String *expanded_query,
@@ -3942,10 +4023,10 @@ Prepared_statement::execute_loop(String *expanded_query,
                                  uchar *packet,
                                  uchar *packet_end)
 {
-  const int MAX_REPREPARE_ATTEMPTS= 3;
   Reprepare_observer reprepare_observer;
   bool error;
   int reprepare_attempt= 0;
+  iterations= 0;
 #ifndef DBUG_OFF
   Item *free_list_state= thd->free_list;
 #endif
@@ -4037,6 +4118,199 @@ Prepared_statement::execute_loop(String *expanded_query,
   return error;
 }
 
+my_bool bulk_parameters_set(THD *thd)
+{
+  DBUG_ENTER("bulk_parameters_set");
+  Prepared_statement *stmt= (Prepared_statement *) thd->bulk_param;
+
+  if (stmt && stmt->set_bulk_parameters(FALSE))
+    DBUG_RETURN(TRUE);
+  DBUG_RETURN(FALSE);
+}
+
+ulong bulk_parameters_iterations(THD *thd)
+{
+  Prepared_statement *stmt= (Prepared_statement *) thd->bulk_param;
+  if (!stmt)
+    return 1;
+  return stmt->bulk_iterations();
+}
+
+
+my_bool Prepared_statement::set_bulk_parameters(bool reset)
+{
+  DBUG_ENTER("Prepared_statement::set_bulk_parameters");
+  DBUG_PRINT("info", ("iteration: %lu", iterations));
+  if (iterations)
+  {
+#ifndef EMBEDDED_LIBRARY
+    if ((*set_bulk_params)(this, &packet, packet_end, reset))
+#else
+    // bulk parameters are not supported for embedded, so it will an error
+#endif
+    {
+      my_error(ER_WRONG_ARGUMENTS, MYF(0),
+               "mysqld_stmt_bulk_execute");
+      reset_stmt_params(this);
+      DBUG_RETURN(true);
+    }
+    iterations--;
+  }
+  start_param= 0;
+  DBUG_RETURN(false);
+}
+
+ulong Prepared_statement::bulk_iterations()
+{
+  if (iterations)
+    return iterations;
+  return start_param ? 1 : 0;
+}
+
+bool
+Prepared_statement::execute_bulk_loop(String *expanded_query,
+                                      bool open_cursor,
+                                      uchar *packet_arg,
+                                      uchar *packet_end_arg,
+                                      ulong iterations_arg)
+{
+  Reprepare_observer reprepare_observer;
+  bool error= 0;
+  packet= packet_arg;
+  packet_end= packet_end_arg;
+  iterations= iterations_arg;
+  start_param= true;
+#ifndef DBUG_OFF
+  Item *free_list_state= thd->free_list;
+#endif
+  thd->select_number= select_number_after_prepare;
+  thd->set_bulk_execution((void *)this);
+  /* Check if we got an error when sending long data */
+  if (state == Query_arena::STMT_ERROR)
+  {
+    my_message(last_errno, last_error, MYF(0));
+    thd->set_bulk_execution(0);
+    return TRUE;
+  }
+
+  if (!(sql_command_flags[lex->sql_command] & CF_SP_BULK_SAFE))
+  {
+    my_error(ER_UNSUPPORTED_PS, MYF(0));
+    thd->set_bulk_execution(0);
+    return TRUE;
+  }
+
+#ifndef EMBEDDED_LIBRARY
+  if (setup_conversion_functions(this, &packet, packet_end, TRUE))
+#else
+  // bulk parameters are not supported for embedded, so it will an error
+#endif
+  {
+    my_error(ER_WRONG_ARGUMENTS, MYF(0),
+            "mysqld_stmt_bulk_execute");
+    reset_stmt_params(this);
+    thd->set_bulk_execution(0);
+    return true;
+  }
+
+#ifdef NOT_YET_FROM_MYSQL_5_6
+  if (unlikely(thd->security_ctx->password_expired &&
+               !lex->is_change_password))
+  {
+    my_error(ER_MUST_CHANGE_PASSWORD, MYF(0));
+    thd->set_bulk_execution(0);
+    return true;
+  }
+#endif
+
+  // iterations changed by set_bulk_parameters
+  while ((iterations || start_param) && !error && !thd->is_error())
+  {
+    int reprepare_attempt= 0;
+
+    /*
+      Here we set parameters for not optimized commands,
+      optimized commands do it inside thier internal loop.
+    */
+    if (!(sql_command_flags[lex->sql_command] & CF_SP_BULK_OPTIMIZED))
+    {
+      if (set_bulk_parameters(TRUE))
+      {
+        thd->set_bulk_execution(0);
+        return true;
+      }
+    }
+
+reexecute:
+    /*
+      If the free_list is not empty, we'll wrongly free some externally
+      allocated items when cleaning up after validation of the prepared
+      statement.
+    */
+    DBUG_ASSERT(thd->free_list == free_list_state);
+
+    /*
+      Install the metadata observer. If some metadata version is
+      different from prepare time and an observer is installed,
+      the observer method will be invoked to push an error into
+      the error stack.
+    */
+
+    if (sql_command_flags[lex->sql_command] & CF_REEXECUTION_FRAGILE)
+    {
+      reprepare_observer.reset_reprepare_observer();
+      DBUG_ASSERT(thd->m_reprepare_observer == NULL);
+      thd->m_reprepare_observer= &reprepare_observer;
+    }
+
+    error= execute(expanded_query, open_cursor) || thd->is_error();
+
+    thd->m_reprepare_observer= NULL;
+#ifdef WITH_WSREP
+
+    if (WSREP_ON)
+    {
+      mysql_mutex_lock(&thd->LOCK_wsrep_thd);
+      switch (thd->wsrep_conflict_state)
+      {
+      case CERT_FAILURE:
+        WSREP_DEBUG("PS execute fail for CERT_FAILURE: thd: %lld  err: %d",
+	            (longlong) thd->thread_id,
+                    thd->get_stmt_da()->sql_errno() );
+        thd->wsrep_conflict_state = NO_CONFLICT;
+        break;
+
+      case MUST_REPLAY:
+        (void) wsrep_replay_transaction(thd);
+        break;
+
+      default:
+        break;
+      }
+      mysql_mutex_unlock(&thd->LOCK_wsrep_thd);
+    }
+#endif /* WITH_WSREP */
+
+    if ((sql_command_flags[lex->sql_command] & CF_REEXECUTION_FRAGILE) &&
+        error && !thd->is_fatal_error && !thd->killed &&
+        reprepare_observer.is_invalidated() &&
+        reprepare_attempt++ < MAX_REPREPARE_ATTEMPTS)
+    {
+      DBUG_ASSERT(thd->get_stmt_da()->sql_errno() == ER_NEED_REPREPARE);
+      thd->clear_error();
+
+      error= reprepare();
+
+      if (! error)                                /* Success */
+        goto reexecute;
+    }
+  }
+  reset_stmt_params(this);
+  thd->set_bulk_execution(0);
+
+  return error;
+}
+
 
 bool
 Prepared_statement::execute_server_runnable(Server_runnable *server_runnable)
diff --git a/sql/sql_prepare.h b/sql/sql_prepare.h
index 4a8780c..820cb43 100644
--- a/sql/sql_prepare.h
+++ b/sql/sql_prepare.h
@@ -72,6 +72,7 @@ class Reprepare_observer
 
 void mysqld_stmt_prepare(THD *thd, const char *packet, uint packet_length);
 void mysqld_stmt_execute(THD *thd, char *packet, uint packet_length);
+void mysqld_stmt_bulk_execute(THD *thd, char *packet, uint packet_length);
 void mysqld_stmt_close(THD *thd, char *packet);
 void mysql_sql_stmt_prepare(THD *thd);
 void mysql_sql_stmt_execute(THD *thd);
@@ -82,6 +83,8 @@ void mysqld_stmt_reset(THD *thd, char *packet);
 void mysql_stmt_get_longdata(THD *thd, char *pos, ulong packet_length);
 void reinit_stmt_before_use(THD *thd, LEX *lex);
 
+ulong bulk_parameters_iterations(THD *thd);
+my_bool bulk_parameters_set(THD *thd);
 /**
   Execute a fragment of server code in an isolated context, so that
   it doesn't leave any effect on THD. THD must have no open tables.
diff --git a/sql/wsrep_thd.cc b/sql/wsrep_thd.cc
index 06846e7..03e53dd 100644
--- a/sql/wsrep_thd.cc
+++ b/sql/wsrep_thd.cc
@@ -265,7 +265,8 @@ void wsrep_replay_transaction(THD *thd)
         }
         else if (thd->get_stmt_da()->is_set())
         {
-          if (thd->get_stmt_da()->status() != Diagnostics_area::DA_OK)
+          if (thd->get_stmt_da()->status() != Diagnostics_area::DA_OK &&
+              thd->get_stmt_da()->status() != Diagnostics_area::DA_OK_BULK)
           {
             WSREP_WARN("replay ok, thd has error status %d",
                        thd->get_stmt_da()->status());
diff --git a/storage/perfschema/pfs.cc b/storage/perfschema/pfs.cc
index dbe3241..4cd1a35 100644
--- a/storage/perfschema/pfs.cc
+++ b/storage/perfschema/pfs.cc
@@ -4827,6 +4827,7 @@ static void end_statement_v1(PSI_statement_locker *locker, void *stmt_da)
 
       switch(da->status())
       {
+        case Diagnostics_area::DA_OK_BULK:
         case Diagnostics_area::DA_EMPTY:
           break;
         case Diagnostics_area::DA_OK:
@@ -4960,6 +4961,7 @@ static void end_statement_v1(PSI_statement_locker *locker, void *stmt_da)
 
   switch (da->status())
   {
+    case Diagnostics_area::DA_OK_BULK:
     case Diagnostics_area::DA_EMPTY:
       break;
     case Diagnostics_area::DA_OK:
diff --git a/libmariadb b/libmariadb
index c8dd089..923e619 160000
--- a/libmariadb
+++ b/libmariadb
@@ -1 +1 @@
-Subproject commit c8dd0899d484ad698ec2da5bc8e3d19ff8b623b9
+Subproject commit 923e6199b9d98c0d947c827ec6b2e0ba29d1f07a-dirty
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..56d0b04 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;
 }
 
 /**
@@ -4165,17 +4159,6 @@ bool Item_param::append_for_log(THD *thd, String *str)
   return str->append(*val);
 }
 
-
-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 +7317,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);
@@ -8521,38 +8503,36 @@ bool Item_default_value::eq(const Item *item, bool binary_cmp) const
 bool Item_default_value::fix_fields(THD *thd, Item **items)
 {
   Item *real_arg;
+  Item_field *field_arg;
   Field *def_field;
   DBUG_ASSERT(fixed == 0);
 
-  if (!arg && !arg_fld)
+  if (!arg)
   {
     fixed= 1;
     return FALSE;
   }
-  if (arg)
-  {
-    if (!arg->fixed && arg->fix_fields(thd, &arg))
-      goto error;
-
+  if (!arg->fixed && arg->fix_fields(thd, &arg))
+    goto error;
 
-    real_arg= arg->real_item();
-    if (real_arg->type() != FIELD_ITEM)
-    {
-      my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->name);
-      goto error;
-    }
 
-    arg_fld= ((Item_field *)real_arg)->field;
+  real_arg= arg->real_item();
+  if (real_arg->type() != FIELD_ITEM)
+  {
+    my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg->name);
+    goto error;
   }
-  if ((arg_fld->flags & NO_DEFAULT_VALUE_FLAG))
+
+  field_arg= (Item_field *)real_arg;
+  if ((field_arg->field->flags & NO_DEFAULT_VALUE_FLAG))
   {
-    my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), arg_fld->field_name);
+    my_error(ER_NO_DEFAULT_FOR_FIELD, MYF(0), field_arg->field->field_name);
     goto error;
   }
-  if (!(def_field= (Field*) thd->alloc(arg_fld->size_of())))
+  if (!(def_field= (Field*) thd->alloc(field_arg->field->size_of())))
     goto error;
-  memcpy((void *)def_field, (void *)arg_fld,
-         arg_fld->size_of());
+  memcpy((void *)def_field, (void *)field_arg->field,
+         field_arg->field->size_of());
   def_field->move_field_offset((my_ptrdiff_t)
                                (def_field->table->s->default_values -
                                 def_field->table->record[0]));
@@ -8632,42 +8612,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..f3736a4 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;
 };
@@ -5109,19 +5091,18 @@ class Item_default_value : public Item_field
   void calculate();
 public:
   Item *arg;
-  Field *arg_fld;
   Item_default_value(THD *thd, Name_resolution_context *context_arg)
     :Item_field(thd, context_arg, (const char *)NULL, (const char *)NULL,
                (const char *)NULL),
-     arg(NULL), arg_fld(NULL) {}
+     arg(NULL) {}
   Item_default_value(THD *thd, Name_resolution_context *context_arg, Item *a)
     :Item_field(thd, context_arg, (const char *)NULL, (const char *)NULL,
                 (const char *)NULL),
-     arg(a), arg_fld(NULL) {}
+     arg(a) {}
   Item_default_value(THD *thd, Name_resolution_context *context_arg, Field *a)
     :Item_field(thd, context_arg, (const char *)NULL, (const char *)NULL,
                 (const char *)NULL),
-     arg(NULL), arg_fld(a) {}
+     arg(NULL) {}
   enum Type type() const { return DEFAULT_VALUE_ITEM; }
   bool eq(const Item *item, bool binary_cmp) const;
   bool fix_fields(THD *, Item **);
diff --git a/sql/sql_base.cc b/sql/sql_base.cc
index fe4aec7..2d40631 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)
     {
@@ -8063,19 +8061,19 @@ fill_record(THD *thd, TABLE *table, Field **ptr, List<Item> &values,
     value=v++;
     if (field->field_index == autoinc_index)
       table->auto_increment_field_not_null= TRUE;
-    Item::Type type= value->type();
-    if (field->vcol_info &&
-        type != Item::DEFAULT_VALUE_ITEM &&
-        type != Item::NULL_ITEM &&
-        table->s->table_category != TABLE_CATEGORY_TEMPORARY)
+    if (field->vcol_info)
     {
-      push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
-                          ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN,
-                          ER_THD(thd, ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN),
-                          field->field_name, table->s->table_name.str);
+      Item::Type type= value->type();
+      if (type != Item::DEFAULT_VALUE_ITEM &&
+          type != Item::NULL_ITEM &&
+          table->s->table_category != TABLE_CATEGORY_TEMPORARY)
+      {
+        push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
+                            ER_WARNING_NON_DEFAULT_VALUE_FOR_VIRTUAL_COLUMN,
+                            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_class.h b/sql/sql_class.h
index d6dd45c..93d3a4a 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -3445,7 +3445,7 @@ class THD :public Statement,
     bulk_param= bulk;
     m_stmt_da->set_bulk_execution(MY_TEST(bulk));
   }
-  bool is_bulk_op() const { return m_stmt_da->is_bulk_op(); }
+  bool is_bulk_op() const { return MY_TEST(bulk_param); }
 
   /// Returns Diagnostics-area for the current statement.
   Diagnostics_area *get_stmt_da()
diff --git a/sql/sql_error.cc b/sql/sql_error.cc
index 34d608f..d14c7b8 100644
--- a/sql/sql_error.cc
+++ b/sql/sql_error.cc
@@ -384,10 +384,12 @@ Diagnostics_area::set_ok_status(ulonglong affected_rows,
   */
   if (is_error() || is_disabled())
     return;
-
+  /*
+    When running a bulk operation, m_status will be DA_OK for the first
+    operation and set to DA_OK_BULK for all following operations.
+  */
   if (m_status == DA_OK_BULK)
   {
-    DBUG_ASSERT(is_bulk_op());
     m_statement_warn_count+= current_statement_warn_count();
     m_affected_rows+= affected_rows;
   }
diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc
index be99bdd..432f8b6 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
 
@@ -722,6 +662,7 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
   bool using_bulk_insert= 0;
   uint value_count;
   ulong counter = 1;
+  ulong iteration= 0;
   ulonglong id;
   ulong bulk_iterations= bulk_parameters_iterations(thd);
   COPY_INFO info;
@@ -787,6 +728,8 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
   THD_STAGE_INFO(thd, stage_init);
   thd->lex->used_tables=0;
   values= its++;
+  if (bulk_parameters_set(thd))
+    DBUG_RETURN(TRUE);
   value_count= values->elements;
 
   DBUG_ASSERT(bulk_iterations > 0);
@@ -833,7 +776,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 ();
  
@@ -949,9 +891,8 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
       goto values_loop_end;
     }
   }
-  for (ulong iteration= 0; iteration < bulk_iterations; iteration++)
+  do
   {
-
     if (iteration && bulk_parameters_set(thd))
       goto abort;
 
@@ -1010,7 +951,7 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
           /* Fix undefined null_bits. */
           if (share->null_bytes > 1 && share->last_null_bit_pos)
           {
-            table->record[0][share->null_bytes - 1]= 
+            table->record[0][share->null_bytes - 1]=
               share->default_values[share->null_bytes - 1];
           }
         }
@@ -1056,7 +997,8 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list,
       thd->get_stmt_da()->inc_current_row_for_warning();
     }
     its.rewind();
-  }
+    iteration++;
+  } while (iteration < bulk_iterations);
 
 values_loop_end:
   free_underlaid_joins(thd, &thd->lex->select_lex);
@@ -1517,7 +1459,6 @@ bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
   /* Prepare the fields in the statement. */
   if (values)
   {
-
     /* if we have INSERT ... VALUES () we cannot have a GROUP BY clause */
     DBUG_ASSERT (!select_lex->group_list.elements);
 
@@ -1536,10 +1477,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);
-
     if (!res && check_fields)
     {
       bool saved_abort_on_warning= thd->abort_on_warning;
diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc
index 47f296d..80e6f8f 100644
--- a/sql/sql_prepare.cc
+++ b/sql/sql_prepare.cc
@@ -992,12 +992,13 @@ 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)
         DBUG_RETURN(1);
-      switch (param->indicator) {
+      switch (param->indicator)
+      {
       case STMT_INDICATOR_NONE:
         if ((*read_pos) >= data_end)
           DBUG_RETURN(1);
@@ -1009,17 +1010,10 @@ 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;
       }
     }
-    /*
-      A long data stream was supplied for this parameter marker.
-      This was done after prepare, prior to providing a placeholder
-      type (the types are supplied at execute). Check that the
-      supplied type of placeholder can accept a data stream.
-    */
     else
       DBUG_RETURN(1); // long is not supported here
   }
@@ -3095,7 +3089,7 @@ void mysqld_stmt_execute(THD *thd, char *packet_arg, uint packet_length)
   thd->profiling.set_query_source(stmt->query(), stmt->query_length());
 #endif
   DBUG_PRINT("exec_query", ("%s", stmt->query()));
-  DBUG_PRINT("info",("stmt: 0x%lx iterations: %lu", (long) stmt, iterations));
+  DBUG_PRINT("info",("stmt: 0x%p iterations: %lu", stmt, iterations));
 
   open_cursor= MY_TEST(flags & (ulong) CURSOR_TYPE_READ_ONLY);
 
@@ -4152,7 +4146,7 @@ my_bool Prepared_statement::set_bulk_parameters(bool reset)
 #ifndef EMBEDDED_LIBRARY
     if ((*set_bulk_params)(this, &packet, packet_end, reset))
 #else
-      DBUG_ASSERT(0); //TODO: support bulk parameters for embedded server
+    // bulk parameters are not supported for embedded, so it will an error
 #endif
     {
       my_error(ER_WRONG_ARGUMENTS, MYF(0),
@@ -4209,7 +4203,7 @@ Prepared_statement::execute_bulk_loop(String *expanded_query,
 #ifndef EMBEDDED_LIBRARY
   if (setup_conversion_functions(this, &packet, packet_end, TRUE))
 #else
-  DBUG_ASSERT(0); //TODO: support bulk parameters for embedded server
+  // bulk parameters are not supported for embedded, so it will an error
 #endif
   {
     my_error(ER_WRONG_ARGUMENTS, MYF(0),
@@ -4229,10 +4223,15 @@ Prepared_statement::execute_bulk_loop(String *expanded_query,
   }
 #endif
 
-  while((iterations || start_param) && !error && !thd->is_error())
+  // iterations changed by set_bulk_parameters
+  while ((iterations || start_param) && !error && !thd->is_error())
   {
     int reprepare_attempt= 0;
 
+    /*
+      Here we set parameters for not optimized commands,
+      optimized commands do it inside thier internal loop.
+    */
     if (!(sql_command_flags[lex->sql_command] & CF_SP_BULK_OPTIMIZED))
     {
       if (set_bulk_parameters(TRUE))
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=
/*
   Copyright (c) 2000, 2016, Oracle and/or its affiliates.
   Copyright (c) 2010, 2016, MariaDB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
*/

/* Insert of records */

/*
  INSERT DELAYED

  Insert delayed is distinguished from a normal insert by lock_type ==
  TL_WRITE_DELAYED instead of TL_WRITE. It first tries to open a
  "delayed" table (delayed_get_table()), but falls back to
  open_and_lock_tables() on error and proceeds as normal insert then.

  Opening a "delayed" table means to find a delayed insert thread that
  has the table open already. If this fails, a new thread is created and
  waited for to open and lock the table.

  If accessing the thread succeeded, in
  Delayed_insert::get_local_table() the table of the thread is copied
  for local use. A copy is required because the normal insert logic
  works on a target table, but the other threads table object must not
  be used. The insert logic uses the record buffer to create a record.
  And the delayed insert thread uses the record buffer to pass the
  record to the table handler. So there must be different objects. Also
  the copied table is not included in the lock, so that the statement
  can proceed even if the real table cannot be accessed at this moment.

  Copying a table object is not a trivial operation. Besides the TABLE
  object there are the field pointer array, the field objects and the
  record buffer. After copying the field objects, their pointers into
  the record must be "moved" to point to the new record buffer.

  After this setup the normal insert logic is used. Only that for
  delayed inserts write_delayed() is called instead of write_record().
  It inserts the rows into a queue and signals the delayed insert thread
  instead of writing directly to the table.

  The delayed insert thread awakes from the signal. It locks the table,
  inserts the rows from the queue, unlocks the table, and waits for the
  next signal. It does normally live until a FLUSH TABLES or SHUTDOWN.

*/

#include <my_global.h>                 /* NO_EMBEDDED_ACCESS_CHECKS */
#include "sql_priv.h"
#include "sql_insert.h"
#include "sql_update.h"                         // compare_record
#include "sql_base.h"                           // close_thread_tables
#include "sql_cache.h"                          // query_cache_*
#include "key.h"                                // key_copy
#include "lock.h"                               // mysql_unlock_tables
#include "sp_head.h"
#include "sql_view.h"         // check_key_in_view, insert_view_fields
#include "sql_table.h"        // mysql_create_table_no_lock
#include "sql_acl.h"          // *_ACL, check_grant_all_columns
#include "sql_trigger.h"
#include "sql_select.h"
#include "sql_show.h"
#include "slave.h"
#include "sql_parse.h"                          // end_active_trans
#include "rpl_mi.h"
#include "transaction.h"
#include "sql_audit.h"
#include "sql_derived.h"                        // mysql_handle_derived
#include "sql_prepare.h"

#include "debug_sync.h"

#ifndef EMBEDDED_LIBRARY
static bool delayed_get_table(THD *thd, MDL_request *grl_protection_request,
                              TABLE_LIST *table_list);
static int write_delayed(THD *thd, TABLE *table, enum_duplicates duplic,
                         LEX_STRING query, bool ignore, bool log_on);
static void end_delayed_insert(THD *thd);
pthread_handler_t handle_delayed_insert(void *arg);
static void unlink_blobs(register TABLE *table);
#endif
static bool check_view_insertability(THD *thd, TABLE_LIST *view);

/*
  Check that insert/update fields are from the same single table of a view.

  @param fields            The insert/update fields to be checked.
  @param values            The insert/update values to be checked, NULL if
  checking is not wanted.
  @param view              The view for insert.
  @param map     [in/out]  The insert table map.

  This function is called in 2 cases:
    1. to check insert fields. In this case *map will be set to 0.
       Insert fields are checked to be all from the same single underlying
       table of the given view. Otherwise the error is thrown. Found table
       map is returned in the map parameter.
    2. to check update fields of the ON DUPLICATE KEY UPDATE clause.
       In this case *map contains table_map found on the previous call of
       the function to check insert fields. Update fields are checked to be
       from the same table as the insert fields.

  @returns false if success.
*/

static bool check_view_single_update(List<Item> &fields, List<Item> *values,
                                     TABLE_LIST *view, table_map *map,
                                     bool insert)
{
  /* it is join view => we need to find the table for update */
  List_iterator_fast<Item> it(fields);
  Item *item;
  TABLE_LIST *tbl= 0;            // reset for call to check_single_table()
  table_map tables= 0;

  while ((item= it++))
    tables|= item->used_tables();

  if (values)
  {
    it.init(*values);
    while ((item= it++))
      tables|= item->view_used_tables(view);
  }

  /* Convert to real table bits */
  tables&= ~PSEUDO_TABLE_BITS;

  /* Check found map against provided map */
  if (*map)
  {
    if (tables != *map)
      goto error;
    return FALSE;
  }

  if (view->check_single_table(&tbl, tables, view) || tbl == 0)
    goto error;

  /* view->table should have been set in mysql_derived_merge_for_insert */
  DBUG_ASSERT(view->table);

  /*
    Use buffer for the insert values that was allocated for the merged view.
  */
  tbl->table->insert_values= view->table->insert_values;
  view->table= tbl->table;
  if (!tbl->single_table_updatable())
  {
    if (insert)
      my_error(ER_NON_INSERTABLE_TABLE, MYF(0), view->alias, "INSERT");
    else
      my_error(ER_NON_UPDATABLE_TABLE, MYF(0), view->alias, "UPDATE");
    return TRUE;
  }
  *map= tables;

  return FALSE;

error:
  my_error(ER_VIEW_MULTIUPDATE, MYF(0),
           view->view_db.str, view->view_name.str);
  return TRUE;
}


/*
  Check if insert fields are correct.

  @param thd            The current thread.
  @param table_list     The table we are inserting into (may be view)
  @param fields         The insert fields.
  @param values         The insert values.
  @param check_unique   If duplicate values should be rejected.
  @param fields_and_values_from_different_maps If 'values' are allowed to
  refer to other tables than those of 'fields'
  @param map            See check_view_single_update
  
  @returns 0 if success, -1 if error
*/

static int check_insert_fields(THD *thd, TABLE_LIST *table_list,
                               List<Item> &fields, List<Item> &values,
                               bool check_unique,
                               bool fields_and_values_from_different_maps,
                               table_map *map)
{
  TABLE *table= table_list->table;
  DBUG_ENTER("check_insert_fields");

  if (!table_list->single_table_updatable())
  {
    my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias, "INSERT");
    DBUG_RETURN(-1);
  }

  if (fields.elements == 0 && values.elements != 0)
  {
    if (!table)
    {
      my_error(ER_VIEW_NO_INSERT_FIELD_LIST, MYF(0),
               table_list->view_db.str, table_list->view_name.str);
      DBUG_RETURN(-1);
    }
    if (values.elements != table->s->fields)
    {
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
      DBUG_RETURN(-1);
    }
#ifndef NO_EMBEDDED_ACCESS_CHECKS
    Field_iterator_table_ref field_it;
    field_it.set(table_list);
    if (check_grant_all_columns(thd, INSERT_ACL, &field_it))
      DBUG_RETURN(-1);
#endif
    /*
      No fields are provided so all fields must be provided in the values.
      Thus we set all bits in the write set.
    */
    bitmap_set_all(table->write_set);
  }
  else
  {						// Part field list
    SELECT_LEX *select_lex= &thd->lex->select_lex;
    Name_resolution_context *context= &select_lex->context;
    Name_resolution_context_state ctx_state;
    int res;

    if (fields.elements != values.elements)
    {
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
      DBUG_RETURN(-1);
    }

    thd->dup_field= 0;
    select_lex->no_wrap_view_item= TRUE;

    /* Save the state of the current name resolution context. */
    ctx_state.save_state(context, table_list);

    /*
      Perform name resolution only in the first table - 'table_list',
      which is the table that is inserted into.
    */
    table_list->next_local= 0;
    context->resolve_in_table_list_only(table_list);
    /* 'Unfix' fields to allow correct marking by the setup_fields function. */
    if (table_list->is_view())
      unfix_fields(fields);

    res= setup_fields(thd, Ref_ptr_array(), fields, MARK_COLUMNS_WRITE, 0, 0);

    /* Restore the current context. */
    ctx_state.restore_state(context, table_list);
    thd->lex->select_lex.no_wrap_view_item= FALSE;

    if (res)
      DBUG_RETURN(-1);

    if (table_list->is_view() && table_list->is_merged_derived())
    {
      if (check_view_single_update(fields,
                                   fields_and_values_from_different_maps ?
                                   (List<Item>*) 0 : &values,
                                   table_list, map, true))
        DBUG_RETURN(-1);
      table= table_list->table;
    }

    if (check_unique && thd->dup_field)
    {
      my_error(ER_FIELD_SPECIFIED_TWICE, MYF(0), thd->dup_field->field_name);
      DBUG_RETURN(-1);
    }
  }
  // For the values we need select_priv
#ifndef NO_EMBEDDED_ACCESS_CHECKS
  table->grant.want_privilege= (SELECT_ACL & ~table->grant.privilege);
#endif

  if (check_key_in_view(thd, table_list) ||
      (table_list->view &&
       check_view_insertability(thd, table_list)))
  {
    my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias, "INSERT");
    DBUG_RETURN(-1);
  }

  DBUG_RETURN(0);
}


/**
  Check if update fields are correct.

  @param thd                  The current thread.
  @param insert_table_list    The table we are inserting into (may be view)
  @param update_fields        The update fields.
  @param update_values        The update values.
  @param fields_and_values_from_different_maps If 'update_values' are allowed to
  refer to other tables than those of 'update_fields'
  @param map                  See check_view_single_update

  @note
  If the update fields include an autoinc field, set the
  table->next_number_field_updated flag.

  @returns 0 if success, -1 if error
*/

static int check_update_fields(THD *thd, TABLE_LIST *insert_table_list,
                               List<Item> &update_fields,
                               List<Item> &update_values,
                               bool fields_and_values_from_different_maps,
                               table_map *map)
{
  TABLE *table= insert_table_list->table;
  my_bool UNINIT_VAR(autoinc_mark);

  table->next_number_field_updated= FALSE;

  if (table->found_next_number_field)
  {
    /*
      Unmark the auto_increment field so that we can check if this is modified
      by update_fields
    */
    autoinc_mark= bitmap_test_and_clear(table->write_set,
                                        table->found_next_number_field->
                                        field_index);
  }

  /* Check the fields we are going to modify */
  if (setup_fields(thd, Ref_ptr_array(),
                   update_fields, MARK_COLUMNS_WRITE, 0, 0))
    return -1;

  if (insert_table_list->is_view() &&
      insert_table_list->is_merged_derived() &&
      check_view_single_update(update_fields,
                               fields_and_values_from_different_maps ?
                               (List<Item>*) 0 : &update_values,
                               insert_table_list, map, false))
    return -1;

  if (table->default_field)
    table->mark_default_fields_for_write(FALSE);

  if (table->found_next_number_field)
  {
    if (bitmap_is_set(table->write_set,
                      table->found_next_number_field->field_index))
      table->next_number_field_updated= TRUE;

    if (autoinc_mark)
      bitmap_set_bit(table->write_set,
                     table->found_next_number_field->field_index);
  }

  return 0;
}

/**
  Upgrade table-level lock of INSERT statement to TL_WRITE if
  a more concurrent lock is infeasible for some reason. This is
  necessary for engines without internal locking support (MyISAM).
  An engine with internal locking implementation might later
  downgrade the lock in handler::store_lock() method.
*/

static
void upgrade_lock_type(THD *thd, thr_lock_type *lock_type,
                       enum_duplicates duplic)
{
  if (duplic == DUP_UPDATE ||
      (duplic == DUP_REPLACE && *lock_type == TL_WRITE_CONCURRENT_INSERT))
  {
    *lock_type= TL_WRITE_DEFAULT;
    return;
  }

  if (*lock_type == TL_WRITE_DELAYED)
  {
    /*
      We do not use delayed threads if:
      - we're running in the safe mode or skip-new mode -- the
        feature is disabled in these modes
      - we're executing this statement on a replication slave --
        we need to ensure serial execution of queries on the
        slave
      - it is INSERT .. ON DUPLICATE KEY UPDATE - in this case the
        insert cannot be concurrent
      - this statement is directly or indirectly invoked from
        a stored function or trigger (under pre-locking) - to
        avoid deadlocks, since INSERT DELAYED involves a lock
        upgrade (TL_WRITE_DELAYED -> TL_WRITE) which we should not
        attempt while keeping other table level locks.
      - this statement itself may require pre-locking.
        We should upgrade the lock even though in most cases
        delayed functionality may work. Unfortunately, we can't
        easily identify whether the subject table is not used in
        the statement indirectly via a stored function or trigger:
        if it is used, that will lead to a deadlock between the
        client connection and the delayed thread.
    */
    if (specialflag & (SPECIAL_NO_NEW_FUNC | SPECIAL_SAFE_MODE) ||
        thd->variables.max_insert_delayed_threads == 0 ||
        thd->locked_tables_mode > LTM_LOCK_TABLES ||
        thd->lex->uses_stored_routines() /*||
        thd->lex->describe*/)
    {
      *lock_type= TL_WRITE;
      return;
    }
    if (thd->slave_thread)
    {
      /* Try concurrent insert */
      *lock_type= (duplic == DUP_UPDATE || duplic == DUP_REPLACE) ?
                  TL_WRITE : TL_WRITE_CONCURRENT_INSERT;
      return;
    }

    bool log_on= (thd->variables.option_bits & OPTION_BIN_LOG);
    if (global_system_variables.binlog_format == BINLOG_FORMAT_STMT &&
        log_on && mysql_bin_log.is_open())
    {
      /*
        Statement-based binary logging does not work in this case, because:
        a) two concurrent statements may have their rows intermixed in the
        queue, leading to autoincrement replication problems on slave (because
        the values generated used for one statement don't depend only on the
        value generated for the first row of this statement, so are not
        replicable)
        b) if first row of the statement has an error the full statement is
        not binlogged, while next rows of the statement may be inserted.
        c) if first row succeeds, statement is binlogged immediately with a
        zero error code (i.e. "no error"), if then second row fails, query
        will fail on slave too and slave will stop (wrongly believing that the
        master got no error).
        So we fallback to non-delayed INSERT.
        Note that to be fully correct, we should test the "binlog format which
        the delayed thread is going to use for this row". But in the common case
        where the global binlog format is not changed and the session binlog
        format may be changed, that is equal to the global binlog format.
        We test it without mutex for speed reasons (condition rarely true), and
        in the common case (global not changed) it is as good as without mutex;
        if global value is changed, anyway there is uncertainty as the delayed
        thread may be old and use the before-the-change value.
      */
      *lock_type= TL_WRITE;
    }
  }
}


/**
  Find or create a delayed insert thread for the first table in
  the table list, then open and lock the remaining tables.
  If a table can not be used with insert delayed, upgrade the lock
  and open and lock all tables using the standard mechanism.

  @param thd         thread context
  @param table_list  list of "descriptors" for tables referenced
                     directly in statement SQL text.
                     The first element in the list corresponds to
                     the destination table for inserts, remaining
                     tables, if any, are usually tables referenced
                     by sub-queries in the right part of the
                     INSERT.

  @return Status of the operation. In case of success 'table'
  member of every table_list element points to an instance of
  class TABLE.

  @sa open_and_lock_tables for more information about MySQL table
  level locking
*/

static
bool open_and_lock_for_insert_delayed(THD *thd, TABLE_LIST *table_list)
{
  MDL_request protection_request;
  DBUG_ENTER("open_and_lock_for_insert_delayed");

#ifndef EMBEDDED_LIBRARY
  /* INSERT DELAYED is not allowed in a read only transaction. */
  if (thd->tx_read_only)
  {
    my_error(ER_CANT_EXECUTE_IN_READ_ONLY_TRANSACTION, MYF(0));
    DBUG_RETURN(true);
  }

  /*
    In order for the deadlock detector to be able to find any deadlocks
    caused by the handler thread waiting for GRL or this table, we acquire
    protection against GRL (global IX metadata lock) and metadata lock on
    table to being inserted into inside the connection thread.
    If this goes ok, the tickets are cloned and added to the list of granted
    locks held by the handler thread.
  */
  if (thd->global_read_lock.can_acquire_protection())
    DBUG_RETURN(TRUE);

  protection_request.init(MDL_key::GLOBAL, "", "", MDL_INTENTION_EXCLUSIVE,
                          MDL_STATEMENT);

  if (thd->mdl_context.acquire_lock(&protection_request,
                                    thd->variables.lock_wait_timeout))
    DBUG_RETURN(TRUE);

  if (thd->mdl_context.acquire_lock(&table_list->mdl_request,
                                    thd->variables.lock_wait_timeout))
    /*
      If a lock can't be acquired, it makes no sense to try normal insert.
      Therefore we just abort the statement.
    */
    DBUG_RETURN(TRUE);

  bool error= FALSE;
  if (delayed_get_table(thd, &protection_request, table_list))
    error= TRUE;
  else if (table_list->table)
  {
    /*
      Open tables used for sub-selects or in stored functions, will also
      cache these functions.
    */
    if (open_and_lock_tables(thd, table_list->next_global, TRUE, 0))
    {
      end_delayed_insert(thd);
      error= TRUE;
    }
    else
    {
      /*
        First table was not processed by open_and_lock_tables(),
        we need to set updatability flag "by hand".
      */
      if (!table_list->derived && !table_list->view)
        table_list->updatable= 1;  // usual table
    }
  }

  /*
    We can't release protection against GRL and metadata lock on the table
    being inserted into here. These locks might be required, for example,
    because this INSERT DELAYED calls functions which may try to update
    this or another tables (updating the same table is of course illegal,
    but such an attempt can be discovered only later during statement
    execution).
  */

  /*
    Reset the ticket in case we end up having to use normal insert and
    therefore will reopen the table and reacquire the metadata lock.
  */
  table_list->mdl_request.ticket= NULL;

  if (error || table_list->table)
    DBUG_RETURN(error);
#endif
  /*
    * This is embedded library and we don't have auxiliary
    threads OR
    * a lock upgrade was requested inside delayed_get_table
      because
      - there are too many delayed insert threads OR
      - the table has triggers.
    Use a normal insert.
  */
  table_list->lock_type= TL_WRITE;
  DBUG_RETURN(open_and_lock_tables(thd, table_list, TRUE, 0));
}


/**
  Create a new query string for removing DELAYED keyword for
  multi INSERT DEALAYED statement.

  @param[in] thd                 Thread handler
  @param[in] buf                 Query string

  @return
             0           ok
             1           error
*/
static int
create_insert_stmt_from_insert_delayed(THD *thd, String *buf)
{
  /* Make a copy of thd->query() and then remove the "DELAYED" keyword */
  if (buf->append(thd->query()) ||
      buf->replace(thd->lex->keyword_delayed_begin_offset,
                   thd->lex->keyword_delayed_end_offset -
                   thd->lex->keyword_delayed_begin_offset, 0))
    return 1;
  return 0;
}


static void save_insert_query_plan(THD* thd, TABLE_LIST *table_list)
{
  Explain_insert* explain= new (thd->mem_root) Explain_insert(thd->mem_root);
  explain->table_name.append(table_list->table->alias);

  thd->lex->explain->add_insert_plan(explain);
  
  /* See Update_plan::updating_a_view for details */
  bool skip= MY_TEST(table_list->view);

  /* Save subquery children */
  for (SELECT_LEX_UNIT *unit= thd->lex->select_lex.first_inner_unit();
       unit;
       unit= unit->next_unit())
  {
    if (skip)
    {
      skip= false;
      continue;
    }
    /* 
      Table elimination doesn't work for INSERTS, but let's still have this
      here for consistency
    */
    if (!(unit->item && unit->item->eliminated))
      explain->add_child(unit->first_select()->select_number);
  }
}


/**
  INSERT statement implementation

  @note Like implementations of other DDL/DML in MySQL, this function
  relies on the caller to close the thread tables. This is done in the
  end of dispatch_command().
*/

bool mysql_insert(THD *thd,TABLE_LIST *table_list,
                  List<Item> &fields,
                  List<List_item> &values_list,
                  List<Item> &update_fields,
                  List<Item> &update_values,
                  enum_duplicates duplic,
		  bool ignore)
{
  bool retval= true;
  int error, res;
  bool transactional_table, joins_freed= FALSE;
  bool changed;
  const bool was_insert_delayed= (table_list->lock_type ==  TL_WRITE_DELAYED);
  bool using_bulk_insert= 0;
  uint value_count;
  ulong counter = 1;
  ulong iteration= 0;
  ulonglong id;
  ulong bulk_iterations= bulk_parameters_iterations(thd);
  COPY_INFO info;
  TABLE *table= 0;
  List_iterator_fast<List_item> its(values_list);
  List_item *values;
  Name_resolution_context *context;
  Name_resolution_context_state ctx_state;
#ifndef EMBEDDED_LIBRARY
  char *query= thd->query();
  /*
    log_on is about delayed inserts only.
    By default, both logs are enabled (this won't cause problems if the server
    runs without --log-bin).
  */
  bool log_on= (thd->variables.option_bits & OPTION_BIN_LOG);
#endif
  thr_lock_type lock_type;
  Item *unused_conds= 0;
  DBUG_ENTER("mysql_insert");

  create_explain_query(thd->lex, thd->mem_root);
  /*
    Upgrade lock type if the requested lock is incompatible with
    the current connection mode or table operation.
  */
  upgrade_lock_type(thd, &table_list->lock_type, duplic);

  /*
    We can't write-delayed into a table locked with LOCK TABLES:
    this will lead to a deadlock, since the delayed thread will
    never be able to get a lock on the table.
  */
  if (table_list->lock_type == TL_WRITE_DELAYED &&
      thd->locked_tables_mode &&
      find_locked_table(thd->open_tables, table_list->db,
                        table_list->table_name))
  {
    my_error(ER_DELAYED_INSERT_TABLE_LOCKED, MYF(0),
             table_list->table_name);
    DBUG_RETURN(TRUE);
  }
  /*
    mark the table_list as a target for insert, to skip the DT/view prepare phase 
    for correct access rights checks
    TODO: remove this hack
  */
  table_list->skip_prepare_derived= TRUE;

  if (table_list->lock_type == TL_WRITE_DELAYED)
  {
    if (open_and_lock_for_insert_delayed(thd, table_list))
      DBUG_RETURN(TRUE);
  }
  else
  {
    if (open_and_lock_tables(thd, table_list, TRUE, 0))
      DBUG_RETURN(TRUE);
  }

  lock_type= table_list->lock_type;

  THD_STAGE_INFO(thd, stage_init);
  thd->lex->used_tables=0;
  values= its++;
  if (bulk_parameters_set(thd))
    DBUG_RETURN(TRUE);
  value_count= values->elements;

  DBUG_ASSERT(bulk_iterations > 0);
  if (mysql_prepare_insert(thd, table_list, table, fields, values,
			   update_fields, update_values, duplic, &unused_conds,
                           FALSE,
                           (fields.elements || !value_count ||
                            table_list->view != 0),
                           !ignore && thd->is_strict_mode()))
    goto abort;

  /* mysql_prepare_insert set table_list->table if it was not set */
  table= table_list->table;

  context= &thd->lex->select_lex.context;
  /*
    These three asserts test the hypothesis that the resetting of the name
    resolution context below is not necessary at all since the list of local
    tables for INSERT always consists of one table.
  */
  DBUG_ASSERT(!table_list->next_local);
  DBUG_ASSERT(!context->table_list->next_local);
  DBUG_ASSERT(!context->first_name_resolution_table->next_name_resolution_table);

  /* Save the state of the current name resolution context. */
  ctx_state.save_state(context, table_list);

  /*
    Perform name resolution only in the first table - 'table_list',
    which is the table that is inserted into.
  */
  table_list->next_local= 0;
  context->resolve_in_table_list_only(table_list);
  switch_to_nullable_trigger_fields(*values, table);

  while ((values= its++))
  {
    counter++;
    if (values->elements != value_count)
    {
      my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
      goto abort;
    }
    if (setup_fields(thd, Ref_ptr_array(), *values, MARK_COLUMNS_READ, 0, 0))
      goto abort;
    switch_to_nullable_trigger_fields(*values, table);
  }
  its.rewind ();
 
  /* Restore the current context. */
  ctx_state.restore_state(context, table_list);
  
  if (thd->lex->unit.first_select()->optimize_unflattened_subqueries(false))
  {
    goto abort;
  }
  save_insert_query_plan(thd, table_list);
  if (thd->lex->describe)
  {
    retval= thd->lex->explain->send_explain(thd);
    goto abort;
  }

  /*
    Fill in the given fields and dump it to the table file
  */
  bzero((char*) &info,sizeof(info));
  info.ignore= ignore;
  info.handle_duplicates=duplic;
  info.update_fields= &update_fields;
  info.update_values= &update_values;
  info.view= (table_list->view ? table_list : 0);
  info.table_list= table_list;

  /*
    Count warnings for all inserts.
    For single line insert, generate an error if try to set a NOT NULL field
    to NULL.
  */
  thd->count_cuted_fields= ((values_list.elements == 1 &&
                             !ignore) ?
			    CHECK_FIELD_ERROR_FOR_NULL :
			    CHECK_FIELD_WARN);
  thd->cuted_fields = 0L;
  table->next_number_field=table->found_next_number_field;

#ifdef HAVE_REPLICATION
  if (thd->rgi_slave &&
      (info.handle_duplicates == DUP_UPDATE) &&
      (table->next_number_field != NULL) &&
      rpl_master_has_bug(thd->rgi_slave->rli, 24432, TRUE, NULL, NULL))
    goto abort;
#endif

  error=0;
  THD_STAGE_INFO(thd, stage_update);
  if (duplic == DUP_REPLACE &&
      (!table->triggers || !table->triggers->has_delete_triggers()))
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
  if (duplic == DUP_UPDATE)
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
  /*
    let's *try* to start bulk inserts. It won't necessary
    start them as values_list.elements should be greater than
    some - handler dependent - threshold.
    We should not start bulk inserts if this statement uses
    functions or invokes triggers since they may access
    to the same table and therefore should not see its
    inconsistent state created by this optimization.
    So we call start_bulk_insert to perform nesessary checks on
    values_list.elements, and - if nothing else - to initialize
    the code to make the call of end_bulk_insert() below safe.
  */
#ifndef EMBEDDED_LIBRARY
  if (lock_type != TL_WRITE_DELAYED)
#endif /* EMBEDDED_LIBRARY */
  {
    if (duplic != DUP_ERROR || ignore)
      table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
    /**
      This is a simple check for the case when the table has a trigger
      that reads from it, or when the statement invokes a stored function
      that reads from the table being inserted to.
      Engines can't handle a bulk insert in parallel with a read form the
      same table in the same connection.
    */
    if (thd->locked_tables_mode <= LTM_LOCK_TABLES &&
       values_list.elements > 1)
    {
      using_bulk_insert= 1;
      table->file->ha_start_bulk_insert(values_list.elements);
    }
  }

  thd->abort_on_warning= !ignore && thd->is_strict_mode();

  table->reset_default_fields();
  table->prepare_triggers_for_insert_stmt_or_event();
  table->mark_columns_needed_for_insert();

  if (table_list->prepare_where(thd, 0, TRUE) ||
      table_list->prepare_check_option(thd))
    error= 1;

  switch_to_nullable_trigger_fields(fields, table);
  switch_to_nullable_trigger_fields(update_fields, table);
  switch_to_nullable_trigger_fields(update_values, table);

  if (fields.elements || !value_count)
  {
    /*
      There are possibly some default values:
      INSERT INTO t1 (fields) VALUES ...
      INSERT INTO t1 VALUES ()
    */
    if (table->validate_default_values_of_unset_fields(thd))
    {
      error= 1;
      goto values_loop_end;
    }
  }
  do
  {
    if (iteration && bulk_parameters_set(thd))
      goto abort;

    while ((values= its++))
    {
      if (fields.elements || !value_count)
      {
        /*
          There are possibly some default values:
          INSERT INTO t1 (fields) VALUES ...
          INSERT INTO t1 VALUES ()
        */
        restore_record(table,s->default_values);	// Get empty record
        table->reset_default_fields();
        if (fill_record_n_invoke_before_triggers(thd, table, fields, *values, 0,
                                                 TRG_EVENT_INSERT))
        {
          if (values_list.elements != 1 && ! thd->is_error())
          {
            info.records++;
            continue;
          }
          /*
            TODO: set thd->abort_on_warning if values_list.elements == 1
	    and check that all items return warning in case of problem with
	    storing field.
          */
	  error=1;
	  break;
        }
      }
      else
      {
        /*
          No field list, all fields are set explicitly:
          INSERT INTO t1 VALUES (values)
        */
        if (thd->lex->used_tables)		      // Column used in values()
	  restore_record(table,s->default_values);	// Get empty record
        else
        {
          TABLE_SHARE *share= table->s;

          /*
            Fix delete marker. No need to restore rest of record since it will
            be overwritten by fill_record() anyway (and fill_record() does not
            use default values in this case).
          */
#ifdef HAVE_valgrind
          if (table->file->ha_table_flags() && HA_RECORD_MUST_BE_CLEAN_ON_WRITE)
            restore_record(table,s->default_values);	// Get empty record
          else
#endif
            table->record[0][0]= share->default_values[0];

          /* Fix undefined null_bits. */
          if (share->null_bytes > 1 && share->last_null_bit_pos)
          {
            table->record[0][share->null_bytes - 1]=
              share->default_values[share->null_bytes - 1];
          }
        }
        if (fill_record_n_invoke_before_triggers(thd, table,
                                                 table->field_to_fill(),
                                                 *values, 0, TRG_EVENT_INSERT))
        {
          if (values_list.elements != 1 && ! thd->is_error())
	  {
	    info.records++;
	    continue;
	  }
	  error=1;
	  break;
        }
      }

      if ((res= table_list->view_check_option(thd,
                                              (values_list.elements == 1 ?
                                               0 :
                                               ignore))) ==
          VIEW_CHECK_SKIP)
        continue;
      else if (res == VIEW_CHECK_ERROR)
      {
        error= 1;
        break;
      }
#ifndef EMBEDDED_LIBRARY
      if (lock_type == TL_WRITE_DELAYED)
      {
        LEX_STRING const st_query = { query, thd->query_length() };
        DEBUG_SYNC(thd, "before_write_delayed");
        error=write_delayed(thd, table, duplic, st_query, ignore, log_on);
        DEBUG_SYNC(thd, "after_write_delayed");
        query=0;
      }
      else
#endif
        error=write_record(thd, table ,&info);
      if (error)
        break;
      thd->get_stmt_da()->inc_current_row_for_warning();
    }
    its.rewind();
    iteration++;
  } while (iteration < bulk_iterations);

values_loop_end:
  free_underlaid_joins(thd, &thd->lex->select_lex);
  joins_freed= TRUE;

  /*
    Now all rows are inserted.  Time to update logs and sends response to
    user
  */
#ifndef EMBEDDED_LIBRARY
  if (lock_type == TL_WRITE_DELAYED)
  {
    if (!error)
    {
      info.copied=values_list.elements;
      end_delayed_insert(thd);
    }
  }
  else
#endif
  {
    /*
      Do not do this release if this is a delayed insert, it would steal
      auto_inc values from the delayed_insert thread as they share TABLE.
    */
    table->file->ha_release_auto_increment();
    if (using_bulk_insert && table->file->ha_end_bulk_insert() && !error)
    {
      table->file->print_error(my_errno,MYF(0));
      error=1;
    }
    if (duplic != DUP_ERROR || ignore)
      table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);

    transactional_table= table->file->has_transactions();

    if ((changed= (info.copied || info.deleted || info.updated)))
    {
      /*
        Invalidate the table in the query cache if something changed.
        For the transactional algorithm to work the invalidation must be
        before binlog writing and ha_autocommit_or_rollback
      */
      query_cache_invalidate3(thd, table_list, 1);
    }

    if (thd->transaction.stmt.modified_non_trans_table)
      thd->transaction.all.modified_non_trans_table= TRUE;
    thd->transaction.all.m_unsafe_rollback_flags|=
      (thd->transaction.stmt.m_unsafe_rollback_flags & THD_TRANS::DID_WAIT);

    if (error <= 0 ||
        thd->transaction.stmt.modified_non_trans_table ||
	was_insert_delayed)
    {
      if(WSREP_EMULATE_BINLOG(thd) || mysql_bin_log.is_open())
      {
        int errcode= 0;
	if (error <= 0)
        {
	  /*
	    [Guilhem wrote] Temporary errors may have filled
	    thd->net.last_error/errno.  For example if there has
	    been a disk full error when writing the row, and it was
	    MyISAM, then thd->net.last_error/errno will be set to
            "disk full"... and the mysql_file_pwrite() will wait until free
	    space appears, and so when it finishes then the
	    write_row() was entirely successful
	  */
	  /* todo: consider removing */
	  thd->clear_error();
	}
        else
          errcode= query_error_code(thd, thd->killed == NOT_KILLED);
        
	/* bug#22725:

	A query which per-row-loop can not be interrupted with
	KILLED, like INSERT, and that does not invoke stored
	routines can be binlogged with neglecting the KILLED error.
        
	If there was no error (error == zero) until after the end of
	inserting loop the KILLED flag that appeared later can be
	disregarded since previously possible invocation of stored
	routines did not result in any error due to the KILLED.  In
	such case the flag is ignored for constructing binlog event.
	*/
	DBUG_ASSERT(thd->killed != KILL_BAD_DATA || error > 0);
        if (was_insert_delayed && table_list->lock_type ==  TL_WRITE)
        {
          /* Binlog INSERT DELAYED as INSERT without DELAYED. */
          String log_query;
          if (create_insert_stmt_from_insert_delayed(thd, &log_query))
          {
            sql_print_error("Event Error: An error occurred while creating query string"
                            "for INSERT DELAYED stmt, before writing it into binary log.");

            error= 1;
          }
          else if (thd->binlog_query(THD::ROW_QUERY_TYPE,
                                     log_query.c_ptr(), log_query.length(),
                                     transactional_table, FALSE, FALSE,
                                     errcode))
            error= 1;
        }
        else if (thd->binlog_query(THD::ROW_QUERY_TYPE,
			           thd->query(), thd->query_length(),
			           transactional_table, FALSE, FALSE,
                                   errcode))
	  error= 1;
      }
    }
    DBUG_ASSERT(transactional_table || !changed || 
                thd->transaction.stmt.modified_non_trans_table);
  }
  THD_STAGE_INFO(thd, stage_end);
  /*
    We'll report to the client this id:
    - if the table contains an autoincrement column and we successfully
    inserted an autogenerated value, the autogenerated value.
    - if the table contains no autoincrement column and LAST_INSERT_ID(X) was
    called, X.
    - if the table contains an autoincrement column, and some rows were
    inserted, the id of the last "inserted" row (if IGNORE, that value may not
    have been really inserted but ignored).
  */
  id= (thd->first_successful_insert_id_in_cur_stmt > 0) ?
    thd->first_successful_insert_id_in_cur_stmt :
    (thd->arg_of_last_insert_id_function ?
     thd->first_successful_insert_id_in_prev_stmt :
     ((table->next_number_field && info.copied) ?
     table->next_number_field->val_int() : 0));
  table->next_number_field=0;
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
  table->auto_increment_field_not_null= FALSE;
  if (duplic == DUP_REPLACE &&
      (!table->triggers || !table->triggers->has_delete_triggers()))
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);

  if (error)
    goto abort;
  if (thd->lex->analyze_stmt)
  {
    retval= thd->lex->explain->send_explain(thd);
    goto abort;
  }
  if ((bulk_iterations * values_list.elements) == 1 && (!(thd->variables.option_bits & OPTION_WARNINGS) ||
				    !thd->cuted_fields))
  {
    my_ok(thd, info.copied + info.deleted +
               ((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
                info.touched : info.updated),
          id);
  }
  else
  {
    char buff[160];
    ha_rows updated=((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
                     info.touched : info.updated);
    if (ignore)
      sprintf(buff, ER_THD(thd, ER_INSERT_INFO), (ulong) info.records,
	      (lock_type == TL_WRITE_DELAYED) ? (ulong) 0 :
	      (ulong) (info.records - info.copied),
              (long) thd->get_stmt_da()->current_statement_warn_count());
    else
      sprintf(buff, ER_THD(thd, ER_INSERT_INFO), (ulong) info.records,
	      (ulong) (info.deleted + updated),
              (long) thd->get_stmt_da()->current_statement_warn_count());
    ::my_ok(thd, info.copied + info.deleted + updated, id, buff);
  }
  thd->abort_on_warning= 0;
  if (thd->lex->current_select->first_cond_optimization)
  {
    thd->lex->current_select->save_leaf_tables(thd);
    thd->lex->current_select->first_cond_optimization= 0;
  }
  
  DBUG_RETURN(FALSE);

abort:
#ifndef EMBEDDED_LIBRARY
  if (lock_type == TL_WRITE_DELAYED)
    end_delayed_insert(thd);
#endif
  if (table != NULL)
    table->file->ha_release_auto_increment();

  if (!joins_freed)
    free_underlaid_joins(thd, &thd->lex->select_lex);
  thd->abort_on_warning= 0;
  DBUG_RETURN(retval);
}


/*
  Additional check for insertability for VIEW

  SYNOPSIS
    check_view_insertability()
    thd     - thread handler
    view    - reference on VIEW

  IMPLEMENTATION
    A view is insertable if the folloings are true:
    - All columns in the view are columns from a table
    - All not used columns in table have a default values
    - All field in view are unique (not referring to the same column)

  RETURN
    FALSE - OK
      view->contain_auto_increment is 1 if and only if the view contains an
      auto_increment field

    TRUE  - can't be used for insert
*/

static bool check_view_insertability(THD * thd, TABLE_LIST *view)
{
  uint num= view->view->select_lex.item_list.elements;
  TABLE *table= view->table;
  Field_translator *trans_start= view->field_translation,
		   *trans_end= trans_start + num;
  Field_translator *trans;
  uint used_fields_buff_size= bitmap_buffer_size(table->s->fields);
  uint32 *used_fields_buff= (uint32*)thd->alloc(used_fields_buff_size);
  MY_BITMAP used_fields;
  enum_mark_columns save_mark_used_columns= thd->mark_used_columns;
  DBUG_ENTER("check_key_in_view");

  if (!used_fields_buff)
    DBUG_RETURN(TRUE);  // EOM

  DBUG_ASSERT(view->table != 0 && view->field_translation != 0);

  (void) my_bitmap_init(&used_fields, used_fields_buff, table->s->fields, 0);
  bitmap_clear_all(&used_fields);

  view->contain_auto_increment= 0;
  /* 
    we must not set query_id for fields as they're not 
    really used in this context
  */
  thd->mark_used_columns= MARK_COLUMNS_NONE;
  /* check simplicity and prepare unique test of view */
  for (trans= trans_start; trans != trans_end; trans++)
  {
    if (!trans->item->fixed && trans->item->fix_fields(thd, &trans->item))
    {
      thd->mark_used_columns= save_mark_used_columns;
      DBUG_RETURN(TRUE);
    }
    Item_field *field;
    /* simple SELECT list entry (field without expression) */
    if (!(field= trans->item->field_for_view_update()))
    {
      thd->mark_used_columns= save_mark_used_columns;
      DBUG_RETURN(TRUE);
    }
    if (field->field->unireg_check == Field::NEXT_NUMBER)
      view->contain_auto_increment= 1;
    /* prepare unique test */
    /*
      remove collation (or other transparent for update function) if we have
      it
    */
    trans->item= field;
  }
  thd->mark_used_columns= save_mark_used_columns;
  /* unique test */
  for (trans= trans_start; trans != trans_end; trans++)
  {
    /* Thanks to test above, we know that all columns are of type Item_field */
    Item_field *field= (Item_field *)trans->item;
    /* check fields belong to table in which we are inserting */
    if (field->field->table == table &&
        bitmap_fast_test_and_set(&used_fields, field->field->field_index))
      DBUG_RETURN(TRUE);
  }

  DBUG_RETURN(FALSE);
}


/*
  Check if table can be updated

  SYNOPSIS
     mysql_prepare_insert_check_table()
     thd		Thread handle
     table_list		Table list
     fields		List of fields to be updated
     where		Pointer to where clause
     select_insert      Check is making for SELECT ... INSERT

   RETURN
     FALSE ok
     TRUE  ERROR
*/

static bool mysql_prepare_insert_check_table(THD *thd, TABLE_LIST *table_list,
                                             List<Item> &fields,
                                             bool select_insert)
{
  bool insert_into_view= (table_list->view != 0);
  DBUG_ENTER("mysql_prepare_insert_check_table");

  if (!table_list->single_table_updatable())
  {
    my_error(ER_NON_INSERTABLE_TABLE, MYF(0), table_list->alias, "INSERT");
    DBUG_RETURN(TRUE);
  }
  /*
     first table in list is the one we'll INSERT into, requires INSERT_ACL.
     all others require SELECT_ACL only. the ACL requirement below is for
     new leaves only anyway (view-constituents), so check for SELECT rather
     than INSERT.
  */

  if (setup_tables_and_check_access(thd, &thd->lex->select_lex.context,
                                    &thd->lex->select_lex.top_join_list,
                                    table_list,
                                    thd->lex->select_lex.leaf_tables,
                                    select_insert, INSERT_ACL, SELECT_ACL,
                                    TRUE))
    DBUG_RETURN(TRUE);

  if (insert_into_view && !fields.elements)
  {
    thd->lex->empty_field_list_on_rset= 1;
    if (!thd->lex->select_lex.leaf_tables.head()->table ||
        table_list->is_multitable())
    {
      my_error(ER_VIEW_NO_INSERT_FIELD_LIST, MYF(0),
               table_list->view_db.str, table_list->view_name.str);
      DBUG_RETURN(TRUE);
    }
    DBUG_RETURN(insert_view_fields(thd, &fields, table_list));
  }

  DBUG_RETURN(FALSE);
}


/*
  Get extra info for tables we insert into

  @param table     table(TABLE object) we insert into,
                   might be NULL in case of view
  @param           table(TABLE_LIST object) or view we insert into
*/

static void prepare_for_positional_update(TABLE *table, TABLE_LIST *tables)
{
  if (table)
  {
    if(table->reginfo.lock_type != TL_WRITE_DELAYED)
      table->prepare_for_position();
    return;
  }

  DBUG_ASSERT(tables->view);
  List_iterator<TABLE_LIST> it(*tables->view_tables);
  TABLE_LIST *tbl;
  while ((tbl= it++))
    prepare_for_positional_update(tbl->table, tbl);

  return;
}


/*
  Prepare items in INSERT statement

  SYNOPSIS
    mysql_prepare_insert()
    thd			Thread handler
    table_list	        Global/local table list
    table		Table to insert into (can be NULL if table should
			be taken from table_list->table)    
    where		Where clause (for insert ... select)
    select_insert	TRUE if INSERT ... SELECT statement
    check_fields        TRUE if need to check that all INSERT fields are 
                        given values.
    abort_on_warning    whether to report if some INSERT field is not 
                        assigned as an error (TRUE) or as a warning (FALSE).

  TODO (in far future)
    In cases of:
    INSERT INTO t1 SELECT a, sum(a) as sum1 from t2 GROUP BY a
    ON DUPLICATE KEY ...
    we should be able to refer to sum1 in the ON DUPLICATE KEY part

  WARNING
    You MUST set table->insert_values to 0 after calling this function
    before releasing the table object.
  
  RETURN VALUE
    FALSE OK
    TRUE  error
*/

bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
                          TABLE *table, List<Item> &fields, List_item *values,
                          List<Item> &update_fields, List<Item> &update_values,
                          enum_duplicates duplic,
                          COND **where, bool select_insert,
                          bool check_fields, bool abort_on_warning)
{
  SELECT_LEX *select_lex= &thd->lex->select_lex;
  Name_resolution_context *context= &select_lex->context;
  Name_resolution_context_state ctx_state;
  bool insert_into_view= (table_list->view != 0);
  bool res= 0;
  table_map map= 0;
  DBUG_ENTER("mysql_prepare_insert");
  DBUG_PRINT("enter", ("table_list: 0x%lx  table: 0x%lx  view: %d",
		       (ulong)table_list, (ulong)table,
		       (int)insert_into_view));
  /* INSERT should have a SELECT or VALUES clause */
  DBUG_ASSERT (!select_insert || !values);

  if (mysql_handle_derived(thd->lex, DT_INIT))
    DBUG_RETURN(TRUE); 
  if (table_list->handle_derived(thd->lex, DT_MERGE_FOR_INSERT))
    DBUG_RETURN(TRUE); 
  if (mysql_handle_list_of_derived(thd->lex, table_list, DT_PREPARE))
    DBUG_RETURN(TRUE); 
  /*
    For subqueries in VALUES() we should not see the table in which we are
    inserting (for INSERT ... SELECT this is done by changing table_list,
    because INSERT ... SELECT share SELECT_LEX it with SELECT.
  */
  if (!select_insert)
  {
    for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit();
         un;
         un= un->next_unit())
    {
      for (SELECT_LEX *sl= un->first_select();
           sl;
           sl= sl->next_select())
      {
        sl->context.outer_context= 0;
      }
    }
  }

  if (duplic == DUP_UPDATE)
  {
    /* it should be allocated before Item::fix_fields() */
    if (table_list->set_insert_values(thd->mem_root))
      DBUG_RETURN(TRUE);
  }

  if (mysql_prepare_insert_check_table(thd, table_list, fields, select_insert))
    DBUG_RETURN(TRUE);

  /* Prepare the fields in the statement. */
  if (values)
  {
    /* if we have INSERT ... VALUES () we cannot have a GROUP BY clause */
    DBUG_ASSERT (!select_lex->group_list.elements);

    /* Save the state of the current name resolution context. */
    ctx_state.save_state(context, table_list);

    /*
      Perform name resolution only in the first table - 'table_list',
      which is the table that is inserted into.
     */
    table_list->next_local= 0;
    context->resolve_in_table_list_only(table_list);

    res= (setup_fields(thd, Ref_ptr_array(),
                       *values, MARK_COLUMNS_READ, 0, 0) ||
          check_insert_fields(thd, context->table_list, fields, *values,
                              !insert_into_view, 0, &map));

    if (!res && check_fields)
    {
      bool saved_abort_on_warning= thd->abort_on_warning;
      thd->abort_on_warning= abort_on_warning;
      res= check_that_all_fields_are_given_values(thd, 
                                                  table ? table : 
                                                  context->table_list->table,
                                                  context->table_list);
      thd->abort_on_warning= saved_abort_on_warning;
    }

    if (!res)
      res= setup_fields(thd, Ref_ptr_array(),
                        update_values, MARK_COLUMNS_READ, 0, 0);

    if (!res && duplic == DUP_UPDATE)
    {
      select_lex->no_wrap_view_item= TRUE;
      res= check_update_fields(thd, context->table_list, update_fields,
                               update_values, false, &map);
      select_lex->no_wrap_view_item= FALSE;
    }

    /* Restore the current context. */
    ctx_state.restore_state(context, table_list);
  }

  if (res)
    DBUG_RETURN(res);

  if (!table)
    table= table_list->table;
  if (table->s->virtual_stored_fields)
    thd->lex->unit.insert_table_with_stored_vcol= table;

  if (!select_insert)
  {
    Item *fake_conds= 0;
    TABLE_LIST *duplicate;
    if ((duplicate= unique_table(thd, table_list, table_list->next_global, 1)))
    {
      update_non_unique_table_error(table_list, "INSERT", duplicate);
      DBUG_RETURN(TRUE);
    }
    select_lex->fix_prepare_information(thd, &fake_conds, &fake_conds);
    select_lex->first_execution= 0;
  }
  /*
    Only call prepare_for_posistion() if we are not performing a DELAYED
    operation. It will instead be executed by delayed insert thread.
  */
  if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
    prepare_for_positional_update(table, table_list);
  DBUG_RETURN(FALSE);
}


	/* Check if there is more uniq keys after field */

static int last_uniq_key(TABLE *table,uint keynr)
{
  /*
    When an underlying storage engine informs that the unique key
    conflicts are not reported in the ascending order by setting
    the HA_DUPLICATE_KEY_NOT_IN_ORDER flag, we cannot rely on this
    information to determine the last key conflict.
   
    The information about the last key conflict will be used to
    do a replace of the new row on the conflicting row, rather
    than doing a delete (of old row) + insert (of new row).
   
    Hence check for this flag and disable replacing the last row
    by returning 0 always. Returning 0 will result in doing
    a delete + insert always.
  */
  if (table->file->ha_table_flags() & HA_DUPLICATE_KEY_NOT_IN_ORDER)
    return 0;

  while (++keynr < table->s->keys)
    if (table->key_info[keynr].flags & HA_NOSAME)
      return 0;
  return 1;
}


/*
  Write a record to table with optional deleting of conflicting records,
  invoke proper triggers if needed.

  SYNOPSIS
     write_record()
      thd   - thread context
      table - table to which record should be written
      info  - COPY_INFO structure describing handling of duplicates
              and which is used for counting number of records inserted
              and deleted.

  NOTE
    Once this record will be written to table after insert trigger will
    be invoked. If instead of inserting new record we will update old one
    then both on update triggers will work instead. Similarly both on
    delete triggers will be invoked if we will delete conflicting records.

    Sets thd->transaction.stmt.modified_non_trans_table to TRUE if table which is updated didn't have
    transactions.

  RETURN VALUE
    0     - success
    non-0 - error
*/


int write_record(THD *thd, TABLE *table,COPY_INFO *info)
{
  int error, trg_error= 0;
  char *key=0;
  MY_BITMAP *save_read_set, *save_write_set;
  ulonglong prev_insert_id= table->file->next_insert_id;
  ulonglong insert_id_for_cur_row= 0;
  ulonglong prev_insert_id_for_cur_row= 0;
  DBUG_ENTER("write_record");

  info->records++;
  save_read_set=  table->read_set;
  save_write_set= table->write_set;

  if (info->handle_duplicates == DUP_REPLACE ||
      info->handle_duplicates == DUP_UPDATE)
  {
    while ((error=table->file->ha_write_row(table->record[0])))
    {
      uint key_nr;
      /*
        If we do more than one iteration of this loop, from the second one the
        row will have an explicit value in the autoinc field, which was set at
        the first call of handler::update_auto_increment(). So we must save
        the autogenerated value to avoid thd->insert_id_for_cur_row to become
        0.
      */
      if (table->file->insert_id_for_cur_row > 0)
        insert_id_for_cur_row= table->file->insert_id_for_cur_row;
      else
        table->file->insert_id_for_cur_row= insert_id_for_cur_row;
      bool is_duplicate_key_error;
      if (table->file->is_fatal_error(error, HA_CHECK_ALL))
	goto err;
      is_duplicate_key_error=
        table->file->is_fatal_error(error, HA_CHECK_ALL & ~HA_CHECK_DUP);
      if (!is_duplicate_key_error)
      {
        /*
          We come here when we had an ignorable error which is not a duplicate
          key error. In this we ignore error if ignore flag is set, otherwise
          report error as usual. We will not do any duplicate key processing.
        */
        if (info->ignore)
        {
          table->file->print_error(error, MYF(ME_JUST_WARNING));
          goto ok_or_after_trg_err; /* Ignoring a not fatal error, return 0 */
        }
        goto err;
      }
      if ((int) (key_nr = table->file->get_dup_key(error)) < 0)
      {
	error= HA_ERR_FOUND_DUPP_KEY;         /* Database can't find key */
	goto err;
      }
      DEBUG_SYNC(thd, "write_row_replace");

      /* Read all columns for the row we are going to replace */
      table->use_all_columns();
      /*
	Don't allow REPLACE to replace a row when a auto_increment column
	was used.  This ensures that we don't get a problem when the
	whole range of the key has been used.
      */
      if (info->handle_duplicates == DUP_REPLACE &&
          table->next_number_field &&
          key_nr == table->s->next_number_index &&
	  (insert_id_for_cur_row > 0))
	goto err;
      if (table->file->ha_table_flags() & HA_DUPLICATE_POS)
      {
	if (table->file->ha_rnd_pos(table->record[1],table->file->dup_ref))
	  goto err;
      }
      else
      {
	if (table->file->extra(HA_EXTRA_FLUSH_CACHE)) /* Not needed with NISAM */
	{
	  error=my_errno;
	  goto err;
	}

	if (!key)
	{
	  if (!(key=(char*) my_safe_alloca(table->s->max_unique_length)))
	  {
	    error=ENOMEM;
	    goto err;
	  }
	}
	key_copy((uchar*) key,table->record[0],table->key_info+key_nr,0);
        key_part_map keypart_map= (1 << table->key_info[key_nr].user_defined_key_parts) - 1;
	if ((error= (table->file->ha_index_read_idx_map(table->record[1],
                                                        key_nr, (uchar*) key,
                                                        keypart_map,
                                                        HA_READ_KEY_EXACT))))
	  goto err;
      }
      if (info->handle_duplicates == DUP_UPDATE)
      {
        int res= 0;
        /*
          We don't check for other UNIQUE keys - the first row
          that matches, is updated. If update causes a conflict again,
          an error is returned
        */
	DBUG_ASSERT(table->insert_values != NULL);
        store_record(table,insert_values);
        restore_record(table,record[1]);
        table->reset_default_fields();

        /*
          in INSERT ... ON DUPLICATE KEY UPDATE the set of modified fields can
          change per row. Thus, we have to do reset_default_fields() per row.
          Twice (before insert and before update).
        */
        DBUG_ASSERT(info->update_fields->elements ==
                    info->update_values->elements);
        if (fill_record_n_invoke_before_triggers(thd, table,
                                                 *info->update_fields,
                                                 *info->update_values,
                                                 info->ignore,
                                                 TRG_EVENT_UPDATE))
          goto before_trg_err;

        bool different_records= (!records_are_comparable(table) ||
                                 compare_record(table));
        /*
          Default fields must be updated before checking view updateability.
          This branch of INSERT is executed only when a UNIQUE key was violated
          with the ON DUPLICATE KEY UPDATE option. In this case the INSERT
          operation is transformed to an UPDATE, and the default fields must
          be updated as if this is an UPDATE.
        */
        if (different_records && table->default_field)
        {
          if (table->update_default_fields(1, info->ignore))
            goto err;
        }

        /* CHECK OPTION for VIEW ... ON DUPLICATE KEY UPDATE ... */
        res= info->table_list->view_check_option(table->in_use, info->ignore);
        if (res == VIEW_CHECK_SKIP)
          goto ok_or_after_trg_err;
        if (res == VIEW_CHECK_ERROR)
          goto before_trg_err;

        table->file->restore_auto_increment(prev_insert_id);
        info->touched++;
        if (different_records)
        {
          if ((error=table->file->ha_update_row(table->record[1],
                                                table->record[0])) &&
              error != HA_ERR_RECORD_IS_THE_SAME)
          {
            if (info->ignore &&
                !table->file->is_fatal_error(error, HA_CHECK_ALL))
            {
              if (!(thd->variables.old_behavior &
                    OLD_MODE_NO_DUP_KEY_WARNINGS_WITH_IGNORE))
                table->file->print_error(error, MYF(ME_JUST_WARNING));
              goto ok_or_after_trg_err;
            }
            goto err;
          }

          if (error != HA_ERR_RECORD_IS_THE_SAME)
            info->updated++;
          else
            error= 0;
          /*
            If ON DUP KEY UPDATE updates a row instead of inserting
            one, it's like a regular UPDATE statement: it should not
            affect the value of a next SELECT LAST_INSERT_ID() or
            mysql_insert_id().  Except if LAST_INSERT_ID(#) was in the
            INSERT query, which is handled separately by
            THD::arg_of_last_insert_id_function.
          */
          prev_insert_id_for_cur_row= table->file->insert_id_for_cur_row;
          insert_id_for_cur_row= table->file->insert_id_for_cur_row= 0;
          trg_error= (table->triggers &&
                      table->triggers->process_triggers(thd, TRG_EVENT_UPDATE,
                                                        TRG_ACTION_AFTER, TRUE));
          info->copied++;
        }

        /*
          Only update next_insert_id if the AUTO_INCREMENT value was explicitly
          updated, so we don't update next_insert_id with the value from the
          row being updated. Otherwise reset next_insert_id to what it was
          before the duplicate key error, since that value is unused.
        */
        if (table->next_number_field_updated)
        {
          DBUG_ASSERT(table->next_number_field != NULL);

          table->file->adjust_next_insert_id_after_explicit_value(table->next_number_field->val_int());
        }
        else if (prev_insert_id_for_cur_row)
        {
          table->file->restore_auto_increment(prev_insert_id_for_cur_row);
        }
        goto ok_or_after_trg_err;
      }
      else /* DUP_REPLACE */
      {
	/*
	  The manual defines the REPLACE semantics that it is either
	  an INSERT or DELETE(s) + INSERT; FOREIGN KEY checks in
	  InnoDB do not function in the defined way if we allow MySQL
	  to convert the latter operation internally to an UPDATE.
          We also should not perform this conversion if we have 
          timestamp field with ON UPDATE which is different from DEFAULT.
          Another case when conversion should not be performed is when
          we have ON DELETE trigger on table so user may notice that
          we cheat here. Note that it is ok to do such conversion for
          tables which have ON UPDATE but have no ON DELETE triggers,
          we just should not expose this fact to users by invoking
          ON UPDATE triggers.
	*/
	if (last_uniq_key(table,key_nr) &&
	    !table->file->referenced_by_foreign_key() &&
            (!table->triggers || !table->triggers->has_delete_triggers()))
        {
          if ((error=table->file->ha_update_row(table->record[1],
					        table->record[0])) &&
              error != HA_ERR_RECORD_IS_THE_SAME)
            goto err;
          if (error != HA_ERR_RECORD_IS_THE_SAME)
            info->deleted++;
          else
            error= 0;
          thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
          /*
            Since we pretend that we have done insert we should call
            its after triggers.
          */
          goto after_trg_n_copied_inc;
        }
        else
        {
          if (table->triggers &&
              table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
                                                TRG_ACTION_BEFORE, TRUE))
            goto before_trg_err;
          if ((error=table->file->ha_delete_row(table->record[1])))
            goto err;
          info->deleted++;
          if (!table->file->has_transactions())
            thd->transaction.stmt.modified_non_trans_table= TRUE;
          if (table->triggers &&
              table->triggers->process_triggers(thd, TRG_EVENT_DELETE,
                                                TRG_ACTION_AFTER, TRUE))
          {
            trg_error= 1;
            goto ok_or_after_trg_err;
          }
          /* Let us attempt do write_row() once more */
        }
      }
    }
    
    /*
      If more than one iteration of the above while loop is done, from
      the second one the row being inserted will have an explicit
      value in the autoinc field, which was set at the first call of
      handler::update_auto_increment(). This value is saved to avoid
      thd->insert_id_for_cur_row becoming 0. Use this saved autoinc
      value.
     */
    if (table->file->insert_id_for_cur_row == 0)
      table->file->insert_id_for_cur_row= insert_id_for_cur_row;
      
    thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
    /*
      Restore column maps if they where replaced during an duplicate key
      problem.
    */
    if (table->read_set != save_read_set ||
        table->write_set != save_write_set)
      table->column_bitmaps_set(save_read_set, save_write_set);
  }
  else if ((error=table->file->ha_write_row(table->record[0])))
  {
    DEBUG_SYNC(thd, "write_row_noreplace");
    if (!info->ignore ||
        table->file->is_fatal_error(error, HA_CHECK_ALL))
      goto err;
    if (!(thd->variables.old_behavior &
          OLD_MODE_NO_DUP_KEY_WARNINGS_WITH_IGNORE))
      table->file->print_error(error, MYF(ME_JUST_WARNING));
    table->file->restore_auto_increment(prev_insert_id);
    goto ok_or_after_trg_err;
  }

after_trg_n_copied_inc:
  info->copied++;
  thd->record_first_successful_insert_id_in_cur_stmt(table->file->insert_id_for_cur_row);
  trg_error= (table->triggers &&
              table->triggers->process_triggers(thd, TRG_EVENT_INSERT,
                                                TRG_ACTION_AFTER, TRUE));

ok_or_after_trg_err:
  if (key)
    my_safe_afree(key,table->s->max_unique_length);
  if (!table->file->has_transactions())
    thd->transaction.stmt.modified_non_trans_table= TRUE;
  DBUG_RETURN(trg_error);

err:
  info->last_errno= error;
  table->file->print_error(error,MYF(0));
  
before_trg_err:
  table->file->restore_auto_increment(prev_insert_id);
  if (key)
    my_safe_afree(key, table->s->max_unique_length);
  table->column_bitmaps_set(save_read_set, save_write_set);
  DBUG_RETURN(1);
}


/******************************************************************************
  Check that all fields with arn't null_fields are used
******************************************************************************/

int check_that_all_fields_are_given_values(THD *thd, TABLE *entry,
                                           TABLE_LIST *table_list)
{
  int err= 0;
  MY_BITMAP *write_set= entry->write_set;

  for (Field **field=entry->field ; *field ; field++)
  {
    if (!bitmap_is_set(write_set, (*field)->field_index) &&
        ((*field)->flags & NO_DEFAULT_VALUE_FLAG) &&
        ((*field)->real_type() != MYSQL_TYPE_ENUM))
    {
      bool view= FALSE;
      if (table_list)
      {
        table_list= table_list->top_table();
        view= MY_TEST(table_list->view);
      }
      if (view)
      {
        push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN,
                            ER_NO_DEFAULT_FOR_VIEW_FIELD,
                            ER_THD(thd, ER_NO_DEFAULT_FOR_VIEW_FIELD),
                            table_list->view_db.str,
                            table_list->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)->field_name);
      }
      err= 1;
    }
  }
  return thd->abort_on_warning ? err : 0;
}

/*****************************************************************************
  Handling of delayed inserts
  A thread is created for each table that one uses with the DELAYED attribute.
*****************************************************************************/

#ifndef EMBEDDED_LIBRARY

class delayed_row :public ilink {
public:
  char *record;
  enum_duplicates dup;
  my_time_t start_time;
  ulong start_time_sec_part;
  sql_mode_t sql_mode;
  bool auto_increment_field_not_null;
  bool query_start_used, ignore, log_query, query_start_sec_part_used;
  bool stmt_depends_on_first_successful_insert_id_in_prev_stmt;
  ulonglong first_successful_insert_id_in_prev_stmt;
  ulonglong forced_insert_id;
  ulong auto_increment_increment;
  ulong auto_increment_offset;
  LEX_STRING query;
  Time_zone *time_zone;

  delayed_row(LEX_STRING const query_arg, enum_duplicates dup_arg,
              bool ignore_arg, bool log_query_arg)
    : record(0), dup(dup_arg), ignore(ignore_arg), log_query(log_query_arg),
      forced_insert_id(0), query(query_arg), time_zone(0)
    {}
  ~delayed_row()
  {
    my_free(query.str);
    my_free(record);
  }
};

/**
  Delayed_insert - context of a thread responsible for delayed insert
  into one table. When processing delayed inserts, we create an own
  thread for every distinct table. Later on all delayed inserts directed
  into that table are handled by a dedicated thread.
*/

class Delayed_insert :public ilink {
  uint locks_in_memory;
  thr_lock_type delayed_lock;
public:
  THD thd;
  TABLE *table;
  mysql_mutex_t mutex;
  mysql_cond_t cond, cond_client;
  uint tables_in_use, stacked_inserts;
  volatile bool status;
  bool retry;
  /**
    When the handler thread starts, it clones a metadata lock ticket
    which protects against GRL and ticket for the table to be inserted.
    This is done to allow the deadlock detector to detect deadlocks
    resulting from these locks.
    Before this is done, the connection thread cannot safely exit
    without causing problems for clone_ticket().
    Once handler_thread_initialized has been set, it is safe for the
    connection thread to exit.
    Access to handler_thread_initialized is protected by di->mutex.
  */
  bool handler_thread_initialized;
  COPY_INFO info;
  I_List<delayed_row> rows;
  ulong group_count;
  TABLE_LIST table_list;			// Argument
  /**
    Request for IX metadata lock protecting against GRL which is
    passed from connection thread to the handler thread.
  */
  MDL_request grl_protection;

  Delayed_insert(SELECT_LEX *current_select)
    :locks_in_memory(0), thd(next_thread_id()),
     table(0),tables_in_use(0), stacked_inserts(0),
     status(0), retry(0), handler_thread_initialized(FALSE), group_count(0)
  {
    DBUG_ENTER("Delayed_insert constructor");
    thd.security_ctx->user=(char*) delayed_user;
    thd.security_ctx->host=(char*) my_localhost;
    strmake_buf(thd.security_ctx->priv_user, thd.security_ctx->user);
    thd.current_tablenr=0;
    thd.set_command(COM_DELAYED_INSERT);
    thd.lex->current_select= current_select;
    thd.lex->sql_command= SQLCOM_INSERT;        // For innodb::store_lock()
    /*
      Prevent changes to global.lock_wait_timeout from affecting
      delayed insert threads as any timeouts in delayed inserts
      are not communicated to the client.
    */
    thd.variables.lock_wait_timeout= LONG_TIMEOUT;

    bzero((char*) &thd.net, sizeof(thd.net));		// Safety
    bzero((char*) &table_list, sizeof(table_list));	// Safety
    thd.system_thread= SYSTEM_THREAD_DELAYED_INSERT;
    thd.security_ctx->host_or_ip= "";
    bzero((char*) &info,sizeof(info));
    mysql_mutex_init(key_delayed_insert_mutex, &mutex, MY_MUTEX_INIT_FAST);
    mysql_cond_init(key_delayed_insert_cond, &cond, NULL);
    mysql_cond_init(key_delayed_insert_cond_client, &cond_client, NULL);
    mysql_mutex_lock(&LOCK_thread_count);
    delayed_insert_threads++;
    delayed_lock= global_system_variables.low_priority_updates ?
                                          TL_WRITE_LOW_PRIORITY : TL_WRITE;
    mysql_mutex_unlock(&LOCK_thread_count);
    DBUG_VOID_RETURN;
  }
  ~Delayed_insert()
  {
    /* The following is not really needed, but just for safety */
    delayed_row *row;
    while ((row=rows.get()))
      delete row;
    if (table)
    {
      close_thread_tables(&thd);
      thd.mdl_context.release_transactional_locks();
    }
    mysql_mutex_destroy(&mutex);
    mysql_cond_destroy(&cond);
    mysql_cond_destroy(&cond_client);

    /*
      We could use unlink_not_visible_threads() here, but as
      delayed_insert_threads also needs to be protected by
      the LOCK_thread_count mutex, we open code this.
    */
    mysql_mutex_lock(&LOCK_thread_count);
    thd.unlink();				// Must be unlinked under lock
    delayed_insert_threads--;
    mysql_mutex_unlock(&LOCK_thread_count);

    my_free(thd.query());
    thd.security_ctx->user= 0;
    thd.security_ctx->host= 0;
  }

  /* The following is for checking when we can delete ourselves */
  inline void lock()
  {
    locks_in_memory++;				// Assume LOCK_delay_insert
  }
  void unlock()
  {
    mysql_mutex_lock(&LOCK_delayed_insert);
    if (!--locks_in_memory)
    {
      mysql_mutex_lock(&mutex);
      if (thd.killed && ! stacked_inserts && ! tables_in_use)
      {
        mysql_cond_signal(&cond);
	status=1;
      }
      mysql_mutex_unlock(&mutex);
    }
    mysql_mutex_unlock(&LOCK_delayed_insert);
  }
  inline uint lock_count() { return locks_in_memory; }

  TABLE* get_local_table(THD* client_thd);
  bool open_and_lock_table();
  bool handle_inserts(void);
};


I_List<Delayed_insert> delayed_threads;


/**
  Return an instance of delayed insert thread that can handle
  inserts into a given table, if it exists. Otherwise return NULL.
*/

static
Delayed_insert *find_handler(THD *thd, TABLE_LIST *table_list)
{
  THD_STAGE_INFO(thd, stage_waiting_for_delay_list);
  mysql_mutex_lock(&LOCK_delayed_insert);       // Protect master list
  I_List_iterator<Delayed_insert> it(delayed_threads);
  Delayed_insert *di;
  while ((di= it++))
  {
    if (!strcmp(table_list->db, di->table_list.db) &&
	!strcmp(table_list->table_name, di->table_list.table_name))
    {
      di->lock();
      break;
    }
  }
  mysql_mutex_unlock(&LOCK_delayed_insert); // For unlink from list
  return di;
}


/**
  Attempt to find or create a delayed insert thread to handle inserts
  into this table.

  @return In case of success, table_list->table points to a local copy
          of the delayed table or is set to NULL, which indicates a
          request for lock upgrade. In case of failure, value of
          table_list->table is undefined.
  @retval TRUE  - this thread ran out of resources OR
                - a newly created delayed insert thread ran out of
                  resources OR
                - the created thread failed to open and lock the table
                  (e.g. because it does not exist) OR
                - the table opened in the created thread turned out to
                  be a view
  @retval FALSE - table successfully opened OR
                - too many delayed insert threads OR
                - the table has triggers and we have to fall back to
                  a normal INSERT
                Two latter cases indicate a request for lock upgrade.

  XXX: why do we regard INSERT DELAYED into a view as an error and
  do not simply perform a lock upgrade?

  TODO: The approach with using two mutexes to work with the
  delayed thread list -- LOCK_delayed_insert and
  LOCK_delayed_create -- is redundant, and we only need one of
  them to protect the list.  The reason we have two locks is that
  we do not want to block look-ups in the list while we're waiting
  for the newly created thread to open the delayed table. However,
  this wait itself is redundant -- we always call get_local_table
  later on, and there wait again until the created thread acquires
  a table lock.

  As is redundant the concept of locks_in_memory, since we already
  have another counter with similar semantics - tables_in_use,
  both of them are devoted to counting the number of producers for
  a given consumer (delayed insert thread), only at different
  stages of producer-consumer relationship.

  The 'status' variable in Delayed_insert is redundant
  too, since there is already di->stacked_inserts.
*/

static
bool delayed_get_table(THD *thd, MDL_request *grl_protection_request,
                       TABLE_LIST *table_list)
{
  int error;
  Delayed_insert *di;
  DBUG_ENTER("delayed_get_table");

  /* Must be set in the parser */
  DBUG_ASSERT(table_list->db);

  /* Find the thread which handles this table. */
  if (!(di= find_handler(thd, table_list)))
  {
    /*
      No match. Create a new thread to handle the table, but
      no more than max_insert_delayed_threads.
    */
    if (delayed_insert_threads >= thd->variables.max_insert_delayed_threads)
      DBUG_RETURN(0);
    THD_STAGE_INFO(thd, stage_creating_delayed_handler);
    mysql_mutex_lock(&LOCK_delayed_create);
    /*
      The first search above was done without LOCK_delayed_create.
      Another thread might have created the handler in between. Search again.
    */
    if (! (di= find_handler(thd, table_list)))
    {
      if (!(di= new Delayed_insert(thd->lex->current_select)))
        goto end_create;

      /*
        Annotating delayed inserts is not supported.
      */
      di->thd.variables.binlog_annotate_row_events= 0;

      di->thd.set_db(table_list->db, (uint) strlen(table_list->db));
      di->thd.set_query(my_strdup(table_list->table_name,
                                  MYF(MY_WME | ME_FATALERROR)),
                        0, system_charset_info);
      if (di->thd.db == NULL || di->thd.query() == NULL)
      {
        /* The error is reported */
	delete di;
        goto end_create;
      }
      di->table_list= *table_list;			// Needed to open table
      /* Replace volatile strings with local copies */
      di->table_list.alias= di->table_list.table_name= di->thd.query();
      di->table_list.db= di->thd.db;
      /* We need the tickets so that they can be cloned in handle_delayed_insert */
      di->grl_protection.init(MDL_key::GLOBAL, "", "",
                              MDL_INTENTION_EXCLUSIVE, MDL_STATEMENT);
      di->grl_protection.ticket= grl_protection_request->ticket;
      init_mdl_requests(&di->table_list);
      di->table_list.mdl_request.ticket= table_list->mdl_request.ticket;

      di->lock();
      mysql_mutex_lock(&di->mutex);
      if ((error= mysql_thread_create(key_thread_delayed_insert,
                                      &di->thd.real_id, &connection_attrib,
                                      handle_delayed_insert, (void*) di)))
      {
	DBUG_PRINT("error",
		   ("Can't create thread to handle delayed insert (error %d)",
		    error));
        mysql_mutex_unlock(&di->mutex);
	di->unlock();
	delete di;
	my_error(ER_CANT_CREATE_THREAD, MYF(ME_FATALERROR), error);
        goto end_create;
      }

      /*
        Wait until table is open unless the handler thread or the connection
        thread has been killed. Note that we in all cases must wait until the
        handler thread has been properly initialized before exiting. Otherwise
        we risk doing clone_ticket() on a ticket that is no longer valid.
      */
      THD_STAGE_INFO(thd, stage_waiting_for_handler_open);
      while (!di->handler_thread_initialized ||
             (!di->thd.killed && !di->table && !thd->killed))
      {
        mysql_cond_wait(&di->cond_client, &di->mutex);
      }
      mysql_mutex_unlock(&di->mutex);
      THD_STAGE_INFO(thd, stage_got_old_table);
      if (thd->killed)
      {
        di->unlock();
        goto end_create;
      }
      if (di->thd.killed)
      {
        if (di->thd.is_error() && ! di->retry)
        {
          /*
            Copy the error message. Note that we don't treat fatal
            errors in the delayed thread as fatal errors in the
            main thread. If delayed thread was killed, we don't
            want to send "Server shutdown in progress" in the
            INSERT THREAD.
          */
          my_message(di->thd.get_stmt_da()->sql_errno(),
                     di->thd.get_stmt_da()->message(),
                     MYF(0));
        }
        di->unlock();
        goto end_create;
      }
      mysql_mutex_lock(&LOCK_delayed_insert);
      delayed_threads.append(di);
      mysql_mutex_unlock(&LOCK_delayed_insert);
    }
    mysql_mutex_unlock(&LOCK_delayed_create);
  }

  mysql_mutex_lock(&di->mutex);
  table_list->table= di->get_local_table(thd);
  mysql_mutex_unlock(&di->mutex);
  if (table_list->table)
  {
    DBUG_ASSERT(! thd->is_error());
    thd->di= di;
  }
  /* Unlock the delayed insert object after its last access. */
  di->unlock();
  DBUG_RETURN((table_list->table == NULL));

end_create:
  mysql_mutex_unlock(&LOCK_delayed_create);
  DBUG_RETURN(thd->is_error());
}


/**
  As we can't let many client threads modify the same TABLE
  structure of the dedicated delayed insert thread, we create an
  own structure for each client thread. This includes a row
  buffer to save the column values and new fields that point to
  the new row buffer. The memory is allocated in the client
  thread and is freed automatically.

  @pre This function is called from the client thread.  Delayed
       insert thread mutex must be acquired before invoking this
       function.

  @return Not-NULL table object on success. NULL in case of an error,
                    which is set in client_thd.
*/

TABLE *Delayed_insert::get_local_table(THD* client_thd)
{
  my_ptrdiff_t adjust_ptrs;
  Field **field,**org_field, *found_next_number_field;
  Field **vfield= 0, **dfield_ptr= 0;
  TABLE *copy;
  TABLE_SHARE *share;
  uchar *bitmap;
  char *copy_tmp;
  uint bitmaps_used;
  DBUG_ENTER("Delayed_insert::get_local_table");

  /* First request insert thread to get a lock */
  status=1;
  tables_in_use++;
  if (!thd.lock)				// Table is not locked
  {
    THD_STAGE_INFO(client_thd, stage_waiting_for_handler_lock);
    mysql_cond_signal(&cond);			// Tell handler to lock table
    while (!thd.killed && !thd.lock && ! client_thd->killed)
    {
      mysql_cond_wait(&cond_client, &mutex);
    }
    THD_STAGE_INFO(client_thd, stage_got_handler_lock);
    if (client_thd->killed)
      goto error;
    if (thd.killed)
    {
      /*
        Copy the error message. Note that we don't treat fatal
        errors in the delayed thread as fatal errors in the
        main thread. If delayed thread was killed, we don't
        want to send "Server shutdown in progress" in the
        INSERT THREAD.

        The thread could be killed with an error message if
        di->handle_inserts() or di->open_and_lock_table() fails.
        The thread could be killed without an error message if
        killed using mysql_notify_thread_having_shared_lock() or
        kill_delayed_threads_for_table().
      */
      if (!thd.is_error())
        my_message(ER_QUERY_INTERRUPTED, ER_THD(&thd, ER_QUERY_INTERRUPTED),
                   MYF(0));
      else
        my_message(thd.get_stmt_da()->sql_errno(),
                   thd.get_stmt_da()->message(), MYF(0));
      goto error;
    }
  }
  share= table->s;

  /*
    Allocate memory for the TABLE object, the field pointers array, and
    one record buffer of reclength size. Normally a table has three
    record buffers of rec_buff_length size, which includes alignment
    bytes. Since the table copy is used for creating one record only,
    the other record buffers and alignment are unnecessary.
  */
  THD_STAGE_INFO(client_thd, stage_allocating_local_table);
  copy_tmp= (char*) client_thd->alloc(sizeof(*copy)+
                                      (share->fields+1)*sizeof(Field**)+
                                      share->reclength +
                                      share->column_bitmap_size*4);
  if (!copy_tmp)
    goto error;

  if (share->virtual_fields)
  {
    vfield= (Field **) client_thd->alloc((share->virtual_fields+1)*
                                         sizeof(Field*));
    if (!vfield)
      goto error;
  }

  /* Copy the TABLE object. */
  copy= new (copy_tmp) TABLE;
  *copy= *table;

  /* We don't need to change the file handler here */
  /* Assign the pointers for the field pointers array and the record. */
  field= copy->field= (Field**) (copy + 1);
  bitmap= (uchar*) (field + share->fields + 1);
  copy->record[0]= (bitmap + share->column_bitmap_size*4);
  memcpy((char*) copy->record[0], (char*) table->record[0], share->reclength);
  if (share->default_fields || share->default_expressions)
  {
    copy->default_field= (Field**)
      client_thd->alloc((share->default_fields +
                         share->default_expressions + 1)*
                        sizeof(Field*));
    if (!copy->default_field)
      goto error;
    dfield_ptr= copy->default_field;
  }

  /* Ensure we don't use the table list of the original table */
  copy->pos_in_table_list= 0;

  /*
    Make a copy of all fields.
    The copied fields need to point into the copied record. This is done
    by copying the field objects with their old pointer values and then
    "move" the pointers by the distance between the original and copied
    records. That way we preserve the relative positions in the records.
  */
  adjust_ptrs= PTR_BYTE_DIFF(copy->record[0], table->record[0]);
  found_next_number_field= table->found_next_number_field;
  for (org_field= table->field; *org_field; org_field++, field++)
  {
    if (!(*field= (*org_field)->make_new_field(client_thd->mem_root, copy,
                                               1)))
      goto error;
    (*field)->unireg_check= (*org_field)->unireg_check;
    (*field)->orig_table= copy;			// Remove connection
    (*field)->move_field_offset(adjust_ptrs);	// Point at copy->record[0]
    if (*org_field == found_next_number_field)
      (*field)->table->found_next_number_field= *field;
  }
  *field=0;

  if (share->virtual_fields || share->default_expressions ||
      share->default_fields)
  {
    bool error_reported= FALSE;
    if (!(copy->def_vcol_set= (MY_BITMAP*) alloc_root(client_thd->mem_root,
                                                      sizeof(MY_BITMAP))))
      goto error;
    copy->vfield= vfield;
    for (field= copy->field; *field; field++)
    {
      Virtual_column_info *vcol;
      if ((*field)->vcol_info)
      {
        if (!(vcol= unpack_vcol_info_from_frm(client_thd,
                                              client_thd->mem_root,
                                              copy,
                                              0,
                                              (*field)->vcol_info,
                                              &error_reported)))
          goto error;
        (*field)->vcol_info= vcol;
        *vfield++= *field;
      }
      if ((*field)->default_value)
      {
        if (!(vcol= unpack_vcol_info_from_frm(client_thd,
                                              client_thd->mem_root,
                                              copy,
                                              0,
                                              (*field)->default_value,
                                              &error_reported)))
          goto error;
        (*field)->default_value= vcol;
        *dfield_ptr++= *field;
      }
      else
      if ((*field)->has_update_default_function())
        *dfield_ptr++= *field;
    }
    if (vfield)
      *vfield= 0;
    if (dfield_ptr)
      *dfield_ptr= 0;
  }

  switch_defaults_to_nullable_trigger_fields(copy);

  /* Adjust in_use for pointing to client thread */
  copy->in_use= client_thd;

  /* Adjust lock_count. This table object is not part of a lock. */
  copy->lock_count= 0;

  /* Adjust bitmaps */
  copy->def_read_set.bitmap= (my_bitmap_map*) bitmap;
  copy->def_write_set.bitmap= ((my_bitmap_map*)
                               (bitmap + share->column_bitmap_size));
  bitmaps_used= 2;
  if (share->virtual_fields)
  {
    my_bitmap_init(copy->def_vcol_set,
                   (my_bitmap_map*) (bitmap +
                                     bitmaps_used*share->column_bitmap_size),
                   share->fields, FALSE);
    bitmaps_used++;
    copy->vcol_set= copy->def_vcol_set;
  }
  if (share->default_fields || share->default_expressions)
  {
    if (!(copy->has_value_set= (MY_BITMAP*) alloc_root(client_thd->mem_root,
                                                       sizeof(MY_BITMAP))))
      goto error;
    my_bitmap_init(copy->has_value_set,
                   (my_bitmap_map*) (bitmap +
                                     bitmaps_used*share->column_bitmap_size),
                   share->fields, FALSE);
  }
  copy->tmp_set.bitmap= 0;                      // To catch errors
  bzero((char*) bitmap, share->column_bitmap_size * bitmaps_used);
  copy->read_set=  &copy->def_read_set;
  copy->write_set= &copy->def_write_set;

  DBUG_RETURN(copy);

  /* Got fatal error */
 error:
  tables_in_use--;
  mysql_cond_signal(&cond);                     // Inform thread about abort
  DBUG_RETURN(0);
}


/* Put a question in queue */

static
int write_delayed(THD *thd, TABLE *table, enum_duplicates duplic,
                  LEX_STRING query, bool ignore, bool log_on)
{
  delayed_row *row= 0;
  Delayed_insert *di=thd->di;
  const Discrete_interval *forced_auto_inc;
  DBUG_ENTER("write_delayed");
  DBUG_PRINT("enter", ("query = '%s' length %lu", query.str,
                       (ulong) query.length));

  THD_STAGE_INFO(thd, stage_waiting_for_handler_insert);
  mysql_mutex_lock(&di->mutex);
  while (di->stacked_inserts >= delayed_queue_size && !thd->killed)
    mysql_cond_wait(&di->cond_client, &di->mutex);
  THD_STAGE_INFO(thd, stage_storing_row_into_queue);

  if (thd->killed)
    goto err;

  /*
    Take a copy of the query string, if there is any. The string will
    be free'ed when the row is destroyed. If there is no query string,
    we don't do anything special.
   */

  if (query.str)
  {
    char *str;
    if (!(str= my_strndup(query.str, query.length, MYF(MY_WME))))
      goto err;
    query.str= str;
  }
  row= new delayed_row(query, duplic, ignore, log_on);
  if (row == NULL)
  {
    my_free(query.str);
    goto err;
  }

  /* This can't be THREAD_SPECIFIC as it's freed in delayed thread */
  if (!(row->record= (char*) my_malloc(table->s->reclength,
                                       MYF(MY_WME))))
    goto err;
  memcpy(row->record, table->record[0], table->s->reclength);
  row->start_time=                thd->start_time;
  row->query_start_used=          thd->query_start_used;
  row->start_time_sec_part=       thd->start_time_sec_part;
  row->query_start_sec_part_used= thd->query_start_sec_part_used;
  /*
    those are for the binlog: LAST_INSERT_ID() has been evaluated at this
    time, so record does not need it, but statement-based binlogging of the
    INSERT will need when the row is actually inserted.
    As for SET INSERT_ID, DELAYED does not honour it (BUG#20830).
  */
  row->stmt_depends_on_first_successful_insert_id_in_prev_stmt=
    thd->stmt_depends_on_first_successful_insert_id_in_prev_stmt;
  row->first_successful_insert_id_in_prev_stmt=
    thd->first_successful_insert_id_in_prev_stmt;

  /* Add session variable timezone
     Time_zone object will not be freed even the thread is ended.
     So we can get time_zone object from thread which handling delayed statement.
     See the comment of my_tz_find() for detail.
  */
  if (thd->time_zone_used)
  {
    row->time_zone = thd->variables.time_zone;
  }
  else
  {
    row->time_zone = NULL;
  }
  /* Copy session variables. */
  row->auto_increment_increment= thd->variables.auto_increment_increment;
  row->auto_increment_offset=    thd->variables.auto_increment_offset;
  row->sql_mode=                 thd->variables.sql_mode;
  row->auto_increment_field_not_null= table->auto_increment_field_not_null;

  /* Copy the next forced auto increment value, if any. */
  if ((forced_auto_inc= thd->auto_inc_intervals_forced.get_next()))
  {
    row->forced_insert_id= forced_auto_inc->minimum();
    DBUG_PRINT("delayed", ("transmitting auto_inc: %lu",
                           (ulong) row->forced_insert_id));
  }

  di->rows.push_back(row);
  di->stacked_inserts++;
  di->status=1;
  if (table->s->blob_fields)
    unlink_blobs(table);
  mysql_cond_signal(&di->cond);

  thread_safe_increment(delayed_rows_in_use,&LOCK_delayed_status);
  mysql_mutex_unlock(&di->mutex);
  DBUG_RETURN(0);

 err:
  delete row;
  mysql_mutex_unlock(&di->mutex);
  DBUG_RETURN(1);
}

/**
  Signal the delayed insert thread that this user connection
  is finished using it for this statement.
*/

static void end_delayed_insert(THD *thd)
{
  DBUG_ENTER("end_delayed_insert");
  Delayed_insert *di=thd->di;
  mysql_mutex_lock(&di->mutex);
  DBUG_PRINT("info",("tables in use: %d",di->tables_in_use));
  if (!--di->tables_in_use || di->thd.killed)
  {						// Unlock table
    di->status=1;
    mysql_cond_signal(&di->cond);
  }
  mysql_mutex_unlock(&di->mutex);
  DBUG_VOID_RETURN;
}


/* We kill all delayed threads when doing flush-tables */

void kill_delayed_threads(void)
{
  DBUG_ENTER("kill_delayed_threads");
  mysql_mutex_lock(&LOCK_delayed_insert); // For unlink from list

  I_List_iterator<Delayed_insert> it(delayed_threads);
  Delayed_insert *di;
  while ((di= it++))
  {
    mysql_mutex_lock(&di->thd.LOCK_thd_data);
    if (di->thd.killed < KILL_CONNECTION)
      di->thd.killed= KILL_CONNECTION;
    if (di->thd.mysys_var)
    {
      mysql_mutex_lock(&di->thd.mysys_var->mutex);
      if (di->thd.mysys_var->current_cond)
      {
	/*
	  We need the following test because the main mutex may be locked
	  in handle_delayed_insert()
	*/
	if (&di->mutex != di->thd.mysys_var->current_mutex)
          mysql_mutex_lock(di->thd.mysys_var->current_mutex);
        mysql_cond_broadcast(di->thd.mysys_var->current_cond);
	if (&di->mutex != di->thd.mysys_var->current_mutex)
          mysql_mutex_unlock(di->thd.mysys_var->current_mutex);
      }
      mysql_mutex_unlock(&di->thd.mysys_var->mutex);
    }
    mysql_mutex_unlock(&di->thd.LOCK_thd_data);
  }
  mysql_mutex_unlock(&LOCK_delayed_insert); // For unlink from list
  DBUG_VOID_RETURN;
}


/**
  A strategy for the prelocking algorithm which prevents the
  delayed insert thread from opening tables with engines which
  do not support delayed inserts.

  Particularly it allows to abort open_tables() as soon as we
  discover that we have opened a MERGE table, without acquiring
  metadata locks on underlying tables.
*/

class Delayed_prelocking_strategy : public Prelocking_strategy
{
public:
  virtual bool handle_routine(THD *thd, Query_tables_list *prelocking_ctx,
                              Sroutine_hash_entry *rt, sp_head *sp,
                              bool *need_prelocking);
  virtual bool handle_table(THD *thd, Query_tables_list *prelocking_ctx,
                            TABLE_LIST *table_list, bool *need_prelocking);
  virtual bool handle_view(THD *thd, Query_tables_list *prelocking_ctx,
                           TABLE_LIST *table_list, bool *need_prelocking);
};


bool Delayed_prelocking_strategy::
handle_table(THD *thd, Query_tables_list *prelocking_ctx,
             TABLE_LIST *table_list, bool *need_prelocking)
{
  DBUG_ASSERT(table_list->lock_type == TL_WRITE_DELAYED);

  if (!(table_list->table->file->ha_table_flags() & HA_CAN_INSERT_DELAYED))
  {
    my_error(ER_DELAYED_NOT_SUPPORTED, MYF(0), table_list->table_name);
    return TRUE;
  }
  return FALSE;
}


bool Delayed_prelocking_strategy::
handle_routine(THD *thd, Query_tables_list *prelocking_ctx,
               Sroutine_hash_entry *rt, sp_head *sp,
               bool *need_prelocking)
{
  /* LEX used by the delayed insert thread has no routines. */
  DBUG_ASSERT(0);
  return FALSE;
}


bool Delayed_prelocking_strategy::
handle_view(THD *thd, Query_tables_list *prelocking_ctx,
            TABLE_LIST *table_list, bool *need_prelocking)
{
  /* We don't open views in the delayed insert thread. */
  DBUG_ASSERT(0);
  return FALSE;
}


/**
   Open and lock table for use by delayed thread and check that
   this table is suitable for delayed inserts.

   @retval FALSE - Success.
   @retval TRUE  - Failure.
*/

bool Delayed_insert::open_and_lock_table()
{
  Delayed_prelocking_strategy prelocking_strategy;

  /*
    Use special prelocking strategy to get ER_DELAYED_NOT_SUPPORTED
    error for tables with engines which don't support delayed inserts.

    We can't do auto-repair in insert delayed thread, as it would hang
    when trying to an exclusive MDL_LOCK on the table during repair
    as the connection thread has a SHARED_WRITE lock.
  */
  if (!(table= open_n_lock_single_table(&thd, &table_list,
                                        TL_WRITE_DELAYED,
                                        MYSQL_OPEN_IGNORE_GLOBAL_READ_LOCK |
                                        MYSQL_OPEN_IGNORE_REPAIR,
                                        &prelocking_strategy)))
  {
    /* If table was crashed, then upper level should retry open+repair */
    retry= table_list.crashed;
    thd.fatal_error();                      // Abort waiting inserts
    return TRUE;
  }

  if (table->triggers || table->check_constraints)
  {
    /*
      Table has triggers or check constraints. This is not an error, but we do
      not support these with delayed insert. Terminate the delayed
      thread without an error and thus request lock upgrade.
    */
    return TRUE;
  }
  table->copy_blobs= 1;
  return FALSE;
}


/*
 * Create a new delayed insert thread
*/

pthread_handler_t handle_delayed_insert(void *arg)
{
  Delayed_insert *di=(Delayed_insert*) arg;
  THD *thd= &di->thd;

  pthread_detach_this_thread();
  /* Add thread to THD list so that's it's visible in 'show processlist' */
  thd->set_current_time();
  add_to_active_threads(thd);
  if (abort_loop)
    thd->killed= KILL_CONNECTION;
  else
    thd->reset_killed();

  mysql_thread_set_psi_id(thd->thread_id);

  /*
    Wait until the client runs into mysql_cond_wait(),
    where we free it after the table is opened and di linked in the list.
    If we did not wait here, the client might detect the opened table
    before it is linked to the list. It would release LOCK_delayed_create
    and allow another thread to create another handler for the same table,
    since it does not find one in the list.
  */
  mysql_mutex_lock(&di->mutex);
  if (my_thread_init())
  {
    /* Can't use my_error since store_globals has not yet been called */
    thd->get_stmt_da()->set_error_status(ER_OUT_OF_RESOURCES);
    di->handler_thread_initialized= TRUE;
  }
  else
  {
    DBUG_ENTER("handle_delayed_insert");
    thd->thread_stack= (char*) &thd;
    if (init_thr_lock() || thd->store_globals())
    {
      /* Can't use my_error since store_globals has perhaps failed */
      thd->get_stmt_da()->set_error_status(ER_OUT_OF_RESOURCES);
      di->handler_thread_initialized= TRUE;
      thd->fatal_error();
      goto err;
    }

    thd->lex->sql_command= SQLCOM_INSERT;        // For innodb::store_lock()

    /*
      INSERT DELAYED has to go to row-based format because the time
      at which rows are inserted cannot be determined in mixed mode.
    */
    thd->set_current_stmt_binlog_format_row_if_mixed();

    /*
      Clone tickets representing protection against GRL and the lock on
      the target table for the insert and add them to the list of granted
      metadata locks held by the handler thread. This is safe since the
      handler thread is not holding nor waiting on any metadata locks.
    */
    if (thd->mdl_context.clone_ticket(&di->grl_protection) ||
        thd->mdl_context.clone_ticket(&di->table_list.mdl_request))
    {
      thd->mdl_context.release_transactional_locks();
      di->handler_thread_initialized= TRUE;
      goto err;
    }

    /*
      Now that the ticket has been cloned, it is safe for the connection
      thread to exit.
    */
    di->handler_thread_initialized= TRUE;
    di->table_list.mdl_request.ticket= NULL;

    if (di->open_and_lock_table())
      goto err;

    /*
      INSERT DELAYED generally expects thd->lex->current_select to be NULL,
      since this is not an attribute of the current thread. This can lead to
      problems if the thread that spawned the current one disconnects.
      current_select will then point to freed memory. But current_select is
      required to resolve the partition function. So, after fulfilling that
      requirement, we set the current_select to 0.
    */
    thd->lex->current_select= NULL;

    /* Tell client that the thread is initialized */
    mysql_cond_signal(&di->cond_client);

    /*
      Inform mdl that it needs to call mysql_lock_abort to abort locks
      for delayed insert.
    */
    thd->mdl_context.set_needs_thr_lock_abort(TRUE);

    di->table->mark_columns_needed_for_insert();

    /* Now wait until we get an insert or lock to handle */
    /* We will not abort as long as a client thread uses this thread */

    for (;;)
    {
      if (thd->killed)
      {
        uint lock_count;
        DBUG_PRINT("delayed", ("Insert delayed killed"));
        /*
          Remove this from delay insert list so that no one can request a
          table from this
        */
        mysql_mutex_unlock(&di->mutex);
        mysql_mutex_lock(&LOCK_delayed_insert);
        di->unlink();
        lock_count=di->lock_count();
        mysql_mutex_unlock(&LOCK_delayed_insert);
        mysql_mutex_lock(&di->mutex);
        if (!lock_count && !di->tables_in_use && !di->stacked_inserts &&
            !thd->lock)
          break;					// Time to die
      }

      /* Shouldn't wait if killed or an insert is waiting. */
      DBUG_PRINT("delayed",
                 ("thd->killed: %d  di->status: %d  di->stacked_inserts: %d",
                  thd->killed, di->status, di->stacked_inserts));
      if (!thd->killed && !di->status && !di->stacked_inserts)
      {
        struct timespec abstime;
        set_timespec(abstime, delayed_insert_timeout);

        /* Information for pthread_kill */
        mysql_mutex_unlock(&di->mutex);
        mysql_mutex_lock(&di->thd.mysys_var->mutex);
        di->thd.mysys_var->current_mutex= &di->mutex;
        di->thd.mysys_var->current_cond= &di->cond;
        mysql_mutex_unlock(&di->thd.mysys_var->mutex);
        mysql_mutex_lock(&di->mutex);
        THD_STAGE_INFO(&(di->thd), stage_waiting_for_insert);

        DBUG_PRINT("info",("Waiting for someone to insert rows"));
        while (!thd->killed && !di->status)
        {
          int error;
          mysql_audit_release(thd);
          error= mysql_cond_timedwait(&di->cond, &di->mutex, &abstime);
#ifdef EXTRA_DEBUG
          if (error && error != EINTR && error != ETIMEDOUT)
          {
            fprintf(stderr, "Got error %d from mysql_cond_timedwait\n", error);
            DBUG_PRINT("error", ("Got error %d from mysql_cond_timedwait",
                                 error));
          }
#endif
          if (error == ETIMEDOUT || error == ETIME)
            thd->killed= KILL_CONNECTION;
        }
        /* We can't lock di->mutex and mysys_var->mutex at the same time */
        mysql_mutex_unlock(&di->mutex);
        mysql_mutex_lock(&di->thd.mysys_var->mutex);
        di->thd.mysys_var->current_mutex= 0;
        di->thd.mysys_var->current_cond= 0;
        mysql_mutex_unlock(&di->thd.mysys_var->mutex);
        mysql_mutex_lock(&di->mutex);
      }
      DBUG_PRINT("delayed",
                 ("thd->killed: %d  di->tables_in_use: %d  thd->lock: %d",
                  thd->killed, di->tables_in_use, thd->lock != 0));

      if (di->tables_in_use && ! thd->lock && !thd->killed)
      {
        /*
          Request for new delayed insert.
          Lock the table, but avoid to be blocked by a global read lock.
          If we got here while a global read lock exists, then one or more
          inserts started before the lock was requested. These are allowed
          to complete their work before the server returns control to the
          client which requested the global read lock. The delayed insert
          handler will close the table and finish when the outstanding
          inserts are done.
        */
        if (! (thd->lock= mysql_lock_tables(thd, &di->table, 1, 0)))
        {
          /* Fatal error */
          thd->killed= KILL_CONNECTION;
        }
        mysql_cond_broadcast(&di->cond_client);
      }
      if (di->stacked_inserts)
      {
        if (di->handle_inserts())
        {
          /* Some fatal error */
          thd->killed= KILL_CONNECTION;
        }
      }
      di->status=0;
      if (!di->stacked_inserts && !di->tables_in_use && thd->lock)
      {
        /*
          No one is doing a insert delayed
          Unlock table so that other threads can use it
        */
        MYSQL_LOCK *lock=thd->lock;
        thd->lock=0;
        mysql_mutex_unlock(&di->mutex);
        /*
          We need to release next_insert_id before unlocking. This is
          enforced by handler::ha_external_lock().
        */
        di->table->file->ha_release_auto_increment();
        mysql_unlock_tables(thd, lock);
        trans_commit_stmt(thd);
        di->group_count=0;
        mysql_audit_release(thd);
        mysql_mutex_lock(&di->mutex);
      }
      if (di->tables_in_use)
        mysql_cond_broadcast(&di->cond_client); // If waiting clients
    }

  err:
    DBUG_LEAVE;
  }

  {
    DBUG_ENTER("handle_delayed_insert-cleanup");
    di->table=0;
    mysql_mutex_unlock(&di->mutex);

    /*
      Protect against mdl_locks trying to access open tables
      We use KILL_CONNECTION_HARD here to ensure that
      THD::notify_shared_lock() dosn't try to access open tables after
      this.
    */
    mysql_mutex_lock(&thd->LOCK_thd_data);
    thd->killed= KILL_CONNECTION_HARD;	        // If error
    thd->mdl_context.set_needs_thr_lock_abort(0);
    mysql_mutex_unlock(&thd->LOCK_thd_data);

    close_thread_tables(thd);			// Free the table
    thd->mdl_context.release_transactional_locks();
    mysql_cond_broadcast(&di->cond_client);       // Safety

    mysql_mutex_lock(&LOCK_delayed_create);    // Because of delayed_get_table
    mysql_mutex_lock(&LOCK_delayed_insert);
    /*
      di should be unlinked from the thread handler list and have no active
      clients
    */
    delete di;
    mysql_mutex_unlock(&LOCK_delayed_insert);
    mysql_mutex_unlock(&LOCK_delayed_create);

    DBUG_LEAVE;
  }
  my_thread_end();
  pthread_exit(0);

  return 0;
}


/* Remove pointers from temporary fields to allocated values */

static void unlink_blobs(register TABLE *table)
{
  for (Field **ptr=table->field ; *ptr ; ptr++)
  {
    if ((*ptr)->flags & BLOB_FLAG)
      ((Field_blob *) (*ptr))->clear_temporary();
  }
}

/* Free blobs stored in current row */

static void free_delayed_insert_blobs(register TABLE *table)
{
  for (Field **ptr=table->field ; *ptr ; ptr++)
  {
    if ((*ptr)->flags & BLOB_FLAG)
    {
      uchar *str;
      ((Field_blob *) (*ptr))->get_ptr(&str);
      my_free(str);
      ((Field_blob *) (*ptr))->reset();
    }
  }
}


bool Delayed_insert::handle_inserts(void)
{
  int error;
  ulong max_rows;
  bool has_trans = TRUE;
  bool using_ignore= 0, using_opt_replace= 0,
       using_bin_log= mysql_bin_log.is_open();
  delayed_row *row;
  DBUG_ENTER("handle_inserts");

  /* Allow client to insert new rows */
  mysql_mutex_unlock(&mutex);

  table->next_number_field=table->found_next_number_field;
  table->use_all_columns();

  THD_STAGE_INFO(&thd, stage_upgrading_lock);
  if (thr_upgrade_write_delay_lock(*thd.lock->locks, delayed_lock,
                                   thd.variables.lock_wait_timeout))
  {
    /*
      This can happen if thread is killed either by a shutdown
      or if another thread is removing the current table definition
      from the table cache.
    */
    my_error(ER_DELAYED_CANT_CHANGE_LOCK, MYF(ME_FATALERROR | ME_NOREFRESH),
             table->s->table_name.str);
    goto err;
  }

  THD_STAGE_INFO(&thd, stage_insert);
  max_rows= delayed_insert_limit;
  if (thd.killed || table->s->tdc->flushed)
  {
    thd.killed= KILL_SYSTEM_THREAD;
    max_rows= ULONG_MAX;                     // Do as much as possible
  }

  /*
    We can't use row caching when using the binary log because if
    we get a crash, then binary log will contain rows that are not yet
    written to disk, which will cause problems in replication.
  */
  if (!using_bin_log)
    table->file->extra(HA_EXTRA_WRITE_CACHE);
  mysql_mutex_lock(&mutex);

  while ((row=rows.get()))
  {
    stacked_inserts--;
    mysql_mutex_unlock(&mutex);
    memcpy(table->record[0],row->record,table->s->reclength);

    thd.start_time=row->start_time;
    thd.query_start_used=row->query_start_used;
    thd.start_time_sec_part=row->start_time_sec_part;
    thd.query_start_sec_part_used=row->query_start_sec_part_used;
    /*
      To get the exact auto_inc interval to store in the binlog we must not
      use values from the previous interval (of the previous rows).
    */
    bool log_query= (row->log_query && row->query.str != NULL);
    DBUG_PRINT("delayed", ("query: '%s'  length: %lu", row->query.str ?
                           row->query.str : "[NULL]",
                           (ulong) row->query.length));
    if (log_query)
    {
      /*
        Guaranteed that the INSERT DELAYED STMT will not be here
        in SBR when mysql binlog is enabled.
      */
      DBUG_ASSERT(!(mysql_bin_log.is_open() &&
                  !thd.is_current_stmt_binlog_format_row()));

      /*
        This is the first value of an INSERT statement.
        It is the right place to clear a forced insert_id.
        This is usually done after the last value of an INSERT statement,
        but we won't know this in the insert delayed thread. But before
        the first value is sufficiently equivalent to after the last
        value of the previous statement.
      */
      table->file->ha_release_auto_increment();
      thd.auto_inc_intervals_in_cur_stmt_for_binlog.empty();
    }
    thd.first_successful_insert_id_in_prev_stmt= 
      row->first_successful_insert_id_in_prev_stmt;
    thd.stmt_depends_on_first_successful_insert_id_in_prev_stmt= 
      row->stmt_depends_on_first_successful_insert_id_in_prev_stmt;
    table->auto_increment_field_not_null= row->auto_increment_field_not_null;

    /* Copy the session variables. */
    thd.variables.auto_increment_increment= row->auto_increment_increment;
    thd.variables.auto_increment_offset=    row->auto_increment_offset;
    thd.variables.sql_mode=                 row->sql_mode;

    /* Copy a forced insert_id, if any. */
    if (row->forced_insert_id)
    {
      DBUG_PRINT("delayed", ("received auto_inc: %lu",
                             (ulong) row->forced_insert_id));
      thd.force_one_auto_inc_interval(row->forced_insert_id);
    }

    info.ignore= row->ignore;
    info.handle_duplicates= row->dup;
    if (info.ignore ||
	info.handle_duplicates != DUP_ERROR)
    {
      table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
      using_ignore=1;
    }
    if (info.handle_duplicates == DUP_REPLACE &&
        (!table->triggers ||
         !table->triggers->has_delete_triggers()))
    {
      table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
      using_opt_replace= 1;
    }
    if (info.handle_duplicates == DUP_UPDATE)
      table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
    thd.clear_error(); // reset error for binlog
    if (write_record(&thd, table, &info))
    {
      info.error_count++;				// Ignore errors
      thread_safe_increment(delayed_insert_errors,&LOCK_delayed_status);
      row->log_query = 0;
    }

    if (using_ignore)
    {
      using_ignore=0;
      table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
    }
    if (using_opt_replace)
    {
      using_opt_replace= 0;
      table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
    }

    if (table->s->blob_fields)
      free_delayed_insert_blobs(table);
    thread_safe_decrement(delayed_rows_in_use,&LOCK_delayed_status);
    thread_safe_increment(delayed_insert_writes,&LOCK_delayed_status);
    mysql_mutex_lock(&mutex);

    /*
      Reset the table->auto_increment_field_not_null as it is valid for
      only one row.
    */
    table->auto_increment_field_not_null= FALSE;

    delete row;
    /*
      Let READ clients do something once in a while
      We should however not break in the middle of a multi-line insert
      if we have binary logging enabled as we don't want other commands
      on this table until all entries has been processed
    */
    if (group_count++ >= max_rows && (row= rows.head()) &&
	(!(row->log_query & using_bin_log)))
    {
      group_count=0;
      if (stacked_inserts || tables_in_use)	// Let these wait a while
      {
	if (tables_in_use)
          mysql_cond_broadcast(&cond_client);   // If waiting clients
	THD_STAGE_INFO(&thd, stage_reschedule);
        mysql_mutex_unlock(&mutex);
	if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
	{
	  /* This should never happen */
	  table->file->print_error(error,MYF(0));
	  sql_print_error("%s", thd.get_stmt_da()->message());
          DBUG_PRINT("error", ("HA_EXTRA_NO_CACHE failed in loop"));
	  goto err;
	}
	query_cache_invalidate3(&thd, table, 1);
	if (thr_reschedule_write_lock(*thd.lock->locks,
                                thd.variables.lock_wait_timeout))
	{
          /* This is not known to happen. */
          my_error(ER_DELAYED_CANT_CHANGE_LOCK,
                   MYF(ME_FATALERROR | ME_NOREFRESH),
                   table->s->table_name.str);
          goto err;
	}
	if (!using_bin_log)
	  table->file->extra(HA_EXTRA_WRITE_CACHE);
        mysql_mutex_lock(&mutex);
	THD_STAGE_INFO(&thd, stage_insert);
      }
      if (tables_in_use)
        mysql_cond_broadcast(&cond_client);     // If waiting clients
    }
  }

  if (WSREP((&thd)))
    thd_proc_info(&thd, "insert done");
  else
    thd_proc_info(&thd, 0);
  mysql_mutex_unlock(&mutex);

  /*
    We need to flush the pending event when using row-based
    replication since the flushing normally done in binlog_query() is
    not done last in the statement: for delayed inserts, the insert
    statement is logged *before* all rows are inserted.

    We can flush the pending event without checking the thd->lock
    since the delayed insert *thread* is not inside a stored function
    or trigger.

    TODO: Move the logging to last in the sequence of rows.
  */
  has_trans= thd.lex->sql_command == SQLCOM_CREATE_TABLE ||
              table->file->has_transactions();
  if (thd.is_current_stmt_binlog_format_row() &&
      thd.binlog_flush_pending_rows_event(TRUE, has_trans))
    goto err;

  if ((error=table->file->extra(HA_EXTRA_NO_CACHE)))
  {						// This shouldn't happen
    table->file->print_error(error,MYF(0));
    sql_print_error("%s", thd.get_stmt_da()->message());
    DBUG_PRINT("error", ("HA_EXTRA_NO_CACHE failed after loop"));
    goto err;
  }
  query_cache_invalidate3(&thd, table, 1);
  mysql_mutex_lock(&mutex);
  DBUG_RETURN(0);

 err:
#ifndef DBUG_OFF
  max_rows= 0;                                  // For DBUG output
#endif
  /* Remove all not used rows */
  mysql_mutex_lock(&mutex);
  while ((row=rows.get()))
  {
    if (table->s->blob_fields)
    {
      memcpy(table->record[0],row->record,table->s->reclength);
      free_delayed_insert_blobs(table);
    }
    delete row;
    thread_safe_increment(delayed_insert_errors,&LOCK_delayed_status);
    stacked_inserts--;
#ifndef DBUG_OFF
    max_rows++;
#endif
  }
  DBUG_PRINT("error", ("dropped %lu rows after an error", max_rows));
  thread_safe_increment(delayed_insert_errors, &LOCK_delayed_status);
  DBUG_RETURN(1);
}
#endif /* EMBEDDED_LIBRARY */

/***************************************************************************
  Store records in INSERT ... SELECT *
***************************************************************************/


/*
  make insert specific preparation and checks after opening tables

  SYNOPSIS
    mysql_insert_select_prepare()
    thd         thread handler

  RETURN
    FALSE OK
    TRUE  Error
*/

bool mysql_insert_select_prepare(THD *thd)
{
  LEX *lex= thd->lex;
  SELECT_LEX *select_lex= &lex->select_lex;
  DBUG_ENTER("mysql_insert_select_prepare");


  /*
    SELECT_LEX do not belong to INSERT statement, so we can't add WHERE
    clause if table is VIEW
  */
  
  if (mysql_prepare_insert(thd, lex->query_tables,
                           lex->query_tables->table, lex->field_list, 0,
                           lex->update_list, lex->value_list,
                           lex->duplicates,
                           &select_lex->where, TRUE, FALSE, FALSE))
    DBUG_RETURN(TRUE);

  DBUG_ASSERT(select_lex->leaf_tables.elements != 0);
  List_iterator<TABLE_LIST> ti(select_lex->leaf_tables);
  TABLE_LIST *table;
  uint insert_tables;

  if (select_lex->first_cond_optimization)
  {
    /* Back up leaf_tables list. */
    Query_arena *arena= thd->stmt_arena, backup;
    arena= thd->activate_stmt_arena_if_needed(&backup);  // For easier test

    insert_tables= select_lex->insert_tables;
    while ((table= ti++) && insert_tables--)
    {
      select_lex->leaf_tables_exec.push_back(table);
      table->tablenr_exec= table->table->tablenr;
      table->map_exec= table->table->map;
      table->maybe_null_exec= table->table->maybe_null;
    }
    if (arena)
      thd->restore_active_arena(arena, &backup);
  }
  ti.rewind();
  /*
    exclude first table from leaf tables list, because it belong to
    INSERT
  */
  /* skip all leaf tables belonged to view where we are insert */
  insert_tables= select_lex->insert_tables;
  while ((table= ti++) && insert_tables--)
    ti.remove();

  DBUG_RETURN(FALSE);
}


select_insert::select_insert(THD *thd_arg, TABLE_LIST *table_list_par,
                             TABLE *table_par,
                             List<Item> *fields_par,
                             List<Item> *update_fields,
                             List<Item> *update_values,
                             enum_duplicates duplic,
                             bool ignore_check_option_errors):
  select_result_interceptor(thd_arg),
  table_list(table_list_par), table(table_par), fields(fields_par),
  autoinc_value_of_last_inserted_row(0),
  insert_into_view(table_list_par && table_list_par->view != 0)
{
  bzero((char*) &info,sizeof(info));
  info.handle_duplicates= duplic;
  info.ignore= ignore_check_option_errors;
  info.update_fields= update_fields;
  info.update_values= update_values;
  info.view= (table_list_par->view ? table_list_par : 0);
  info.table_list= table_list_par;
}


int
select_insert::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
{
  LEX *lex= thd->lex;
  int res;
  table_map map= 0;
  SELECT_LEX *lex_current_select_save= lex->current_select;
  DBUG_ENTER("select_insert::prepare");

  unit= u;

  /*
    Since table in which we are going to insert is added to the first
    select, LEX::current_select should point to the first select while
    we are fixing fields from insert list.
  */
  lex->current_select= &lex->select_lex;

  res= (setup_fields(thd, Ref_ptr_array(), values, MARK_COLUMNS_READ, 0, 0) ||
        check_insert_fields(thd, table_list, *fields, values,
                            !insert_into_view, 1, &map));

  if (!res && fields->elements)
  {
    bool saved_abort_on_warning= thd->abort_on_warning;
    thd->abort_on_warning= !info.ignore && thd->is_strict_mode();
    res= check_that_all_fields_are_given_values(thd, table_list->table, 
                                                table_list);
    thd->abort_on_warning= saved_abort_on_warning;
  }

  if (info.handle_duplicates == DUP_UPDATE && !res)
  {
    Name_resolution_context *context= &lex->select_lex.context;
    Name_resolution_context_state ctx_state;

    /* Save the state of the current name resolution context. */
    ctx_state.save_state(context, table_list);

    /* Perform name resolution only in the first table - 'table_list'. */
    table_list->next_local= 0;
    context->resolve_in_table_list_only(table_list);

    lex->select_lex.no_wrap_view_item= TRUE;
    res= res ||
      check_update_fields(thd, context->table_list,
                          *info.update_fields, *info.update_values,
                          /*
                            In INSERT SELECT ON DUPLICATE KEY UPDATE col=x
                            'x' can legally refer to a non-inserted table.
                            'x' is not even resolved yet.
                           */
                          true,
                          &map);
    lex->select_lex.no_wrap_view_item= FALSE;
    /*
      When we are not using GROUP BY and there are no ungrouped aggregate functions 
      we can refer to other tables in the ON DUPLICATE KEY part.
      We use next_name_resolution_table descructively, so check it first (views?)
    */
    DBUG_ASSERT (!table_list->next_name_resolution_table);
    if (lex->select_lex.group_list.elements == 0 &&
        !lex->select_lex.with_sum_func)
      /*
        We must make a single context out of the two separate name resolution contexts :
        the INSERT table and the tables in the SELECT part of INSERT ... SELECT.
        To do that we must concatenate the two lists
      */  
      table_list->next_name_resolution_table= 
        ctx_state.get_first_name_resolution_table();

    res= res || setup_fields(thd, Ref_ptr_array(), *info.update_values,
                             MARK_COLUMNS_READ, 0, 0);
    if (!res)
    {
      /*
        Traverse the update values list and substitute fields from the
        select for references (Item_ref objects) to them. This is done in
        order to get correct values from those fields when the select
        employs a temporary table.
      */
      List_iterator<Item> li(*info.update_values);
      Item *item;

      while ((item= li++))
      {
        item->transform(thd, &Item::update_value_transformer,
                        (uchar*)lex->current_select);
      }
    }

    /* Restore the current context. */
    ctx_state.restore_state(context, table_list);
  }

  lex->current_select= lex_current_select_save;
  if (res)
    DBUG_RETURN(1);
  /*
    if it is INSERT into join view then check_insert_fields already found
    real table for insert
  */
  table= table_list->table;

  /*
    Is table which we are changing used somewhere in other parts of
    query
  */
  if (unique_table(thd, table_list, table_list->next_global, 0))
  {
    /* Using same table for INSERT and SELECT */
    lex->current_select->options|= OPTION_BUFFER_RESULT;
    lex->current_select->join->select_options|= OPTION_BUFFER_RESULT;
  }
  else if (!(lex->current_select->options & OPTION_BUFFER_RESULT) &&
           thd->locked_tables_mode <= LTM_LOCK_TABLES)
  {
    /*
      We must not yet prepare the result table if it is the same as one of the 
      source tables (INSERT SELECT). The preparation may disable 
      indexes on the result table, which may be used during the select, if it
      is the same table (Bug #6034). Do the preparation after the select phase
      in select_insert::prepare2().
      We won't start bulk inserts at all if this statement uses functions or
      should invoke triggers since they may access to the same table too.
    */
    table->file->ha_start_bulk_insert((ha_rows) 0);
  }
  restore_record(table,s->default_values);		// Get empty record
  table->reset_default_fields();
  table->next_number_field=table->found_next_number_field;

#ifdef HAVE_REPLICATION
  if (thd->rgi_slave &&
      (info.handle_duplicates == DUP_UPDATE) &&
      (table->next_number_field != NULL) &&
      rpl_master_has_bug(thd->rgi_slave->rli, 24432, TRUE, NULL, NULL))
    DBUG_RETURN(1);
#endif

  thd->cuted_fields=0;
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
    table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
  if (info.handle_duplicates == DUP_REPLACE &&
      (!table->triggers || !table->triggers->has_delete_triggers()))
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
  if (info.handle_duplicates == DUP_UPDATE)
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
  thd->abort_on_warning= !info.ignore && thd->is_strict_mode();
  res= (table_list->prepare_where(thd, 0, TRUE) ||
        table_list->prepare_check_option(thd));

  if (!res)
  {
     table->prepare_triggers_for_insert_stmt_or_event();
     table->mark_columns_needed_for_insert();
  }

  DBUG_RETURN(res);
}


/*
  Finish the preparation of the result table.

  SYNOPSIS
    select_insert::prepare2()
    void

  DESCRIPTION
    If the result table is the same as one of the source tables (INSERT SELECT),
    the result table is not finally prepared at the join prepair phase.
    Do the final preparation now.
		       
  RETURN
    0   OK
*/

int select_insert::prepare2(void)
{
  DBUG_ENTER("select_insert::prepare2");
  if (thd->lex->current_select->options & OPTION_BUFFER_RESULT &&
      thd->locked_tables_mode <= LTM_LOCK_TABLES &&
      !thd->lex->describe)
    table->file->ha_start_bulk_insert((ha_rows) 0);
  if (table->validate_default_values_of_unset_fields(thd))
    DBUG_RETURN(1);
  DBUG_RETURN(0);
}


void select_insert::cleanup()
{
  /* select_insert/select_create are never re-used in prepared statement */
  DBUG_ASSERT(0);
}

select_insert::~select_insert()
{
  DBUG_ENTER("~select_insert");
  if (table && table->is_created())
  {
    table->next_number_field=0;
    table->auto_increment_field_not_null= FALSE;
    table->file->ha_reset();
  }
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
  thd->abort_on_warning= 0;
  DBUG_VOID_RETURN;
}


int select_insert::send_data(List<Item> &values)
{
  DBUG_ENTER("select_insert::send_data");
  bool error=0;

  if (unit->offset_limit_cnt)
  {						// using limit offset,count
    unit->offset_limit_cnt--;
    DBUG_RETURN(0);
  }
  if (thd->killed == ABORT_QUERY)
    DBUG_RETURN(0);

  thd->count_cuted_fields= CHECK_FIELD_WARN;	// Calculate cuted fields
  store_values(values);
  if (table->default_field && table->update_default_fields(0, info.ignore))
    DBUG_RETURN(1);
  thd->count_cuted_fields= CHECK_FIELD_ERROR_FOR_NULL;
  if (thd->is_error())
  {
    table->auto_increment_field_not_null= FALSE;
    DBUG_RETURN(1);
  }
  if (table_list)                               // Not CREATE ... SELECT
  {
    switch (table_list->view_check_option(thd, info.ignore)) {
    case VIEW_CHECK_SKIP:
      DBUG_RETURN(0);
    case VIEW_CHECK_ERROR:
      DBUG_RETURN(1);
    }
  }

  // Release latches in case bulk insert takes a long time
  ha_release_temporary_latches(thd);

  error= write_record(thd, table, &info);
  table->auto_increment_field_not_null= FALSE;
  
  if (!error)
  {
    if (table->triggers || info.handle_duplicates == DUP_UPDATE)
    {
      /*
        Restore fields of the record since it is possible that they were
        changed by ON DUPLICATE KEY UPDATE clause.
    
        If triggers exist then whey can modify some fields which were not
        originally touched by INSERT ... SELECT, so we have to restore
        their original values for the next row.
      */
      restore_record(table, s->default_values);
    }
    if (table->next_number_field)
    {
      /*
        If no value has been autogenerated so far, we need to remember the
        value we just saw, we may need to send it to client in the end.
      */
      if (thd->first_successful_insert_id_in_cur_stmt == 0) // optimization
        autoinc_value_of_last_inserted_row= 
          table->next_number_field->val_int();
      /*
        Clear auto-increment field for the next record, if triggers are used
        we will clear it twice, but this should be cheap.
      */
      table->next_number_field->reset();
    }
  }
  DBUG_RETURN(error);
}


void select_insert::store_values(List<Item> &values)
{
  if (fields->elements)
    fill_record_n_invoke_before_triggers(thd, table, *fields, values, 1,
                                         TRG_EVENT_INSERT);
  else
    fill_record_n_invoke_before_triggers(thd, table, table->field_to_fill(),
                                         values, 1, TRG_EVENT_INSERT);
}

bool select_insert::prepare_eof()
{
  int error;
  bool const trans_table= table->file->has_transactions();
  bool changed;
  killed_state killed_status= thd->killed;

  DBUG_ENTER("select_insert::prepare_eof");
  DBUG_PRINT("enter", ("trans_table=%d, table_type='%s'",
                       trans_table, table->file->table_type()));

  error = IF_WSREP((thd->wsrep_conflict_state == MUST_ABORT ||
		  thd->wsrep_conflict_state == CERT_FAILURE) ? -1 :, )
	  (thd->locked_tables_mode <= LTM_LOCK_TABLES ?
           table->file->ha_end_bulk_insert() : 0);

  if (!error && thd->is_error())
    error= thd->get_stmt_da()->sql_errno();

  table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
  table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);

  if ((changed= (info.copied || info.deleted || info.updated)))
  {
    /*
      We must invalidate the table in the query cache before binlog writing
      and ha_autocommit_or_rollback.
    */
    query_cache_invalidate3(thd, table, 1);
  }

  if (thd->transaction.stmt.modified_non_trans_table)
    thd->transaction.all.modified_non_trans_table= TRUE;
  thd->transaction.all.m_unsafe_rollback_flags|=
    (thd->transaction.stmt.m_unsafe_rollback_flags & THD_TRANS::DID_WAIT);

  DBUG_ASSERT(trans_table || !changed || 
              thd->transaction.stmt.modified_non_trans_table);

  /*
    Write to binlog before commiting transaction.  No statement will
    be written by the binlog_query() below in RBR mode.  All the
    events are in the transaction cache and will be written when
    ha_autocommit_or_rollback() is issued below.
  */
  if ((WSREP_EMULATE_BINLOG(thd) || mysql_bin_log.is_open()) &&
      (!error || thd->transaction.stmt.modified_non_trans_table))
  {
    int errcode= 0;
    if (!error)
      thd->clear_error();
    else
      errcode= query_error_code(thd, killed_status == NOT_KILLED);
    if (thd->binlog_query(THD::ROW_QUERY_TYPE,
                      thd->query(), thd->query_length(),
                      trans_table, FALSE, FALSE, errcode))
    {
      table->file->ha_release_auto_increment();
      DBUG_RETURN(true);
    }
  }
  table->file->ha_release_auto_increment();

  if (error)
  {
    table->file->print_error(error,MYF(0));
    DBUG_RETURN(true);
  }

  DBUG_RETURN(false);
}

bool select_insert::send_ok_packet() {
  char  message[160];                           /* status message */
  ulong row_count;                              /* rows affected */
  ulong id;                                     /* last insert-id */

  DBUG_ENTER("select_insert::send_ok_packet");

  if (info.ignore)
    my_snprintf(message, sizeof(message), ER(ER_INSERT_INFO),
                (ulong) info.records, (ulong) (info.records - info.copied),
                (long) thd->get_stmt_da()->current_statement_warn_count());
  else
    my_snprintf(message, sizeof(message), ER(ER_INSERT_INFO),
                (ulong) info.records, (ulong) (info.deleted + info.updated),
                (long) thd->get_stmt_da()->current_statement_warn_count());

  row_count= info.copied + info.deleted +
    ((thd->client_capabilities & CLIENT_FOUND_ROWS) ?
     info.touched : info.updated);

  id= (thd->first_successful_insert_id_in_cur_stmt > 0) ?
    thd->first_successful_insert_id_in_cur_stmt :
    (thd->arg_of_last_insert_id_function ?
     thd->first_successful_insert_id_in_prev_stmt :
     (info.copied ? autoinc_value_of_last_inserted_row : 0));

  ::my_ok(thd, row_count, id, message);

  DBUG_RETURN(false);
}

bool select_insert::send_eof()
{
  bool res;
  DBUG_ENTER("select_insert::send_eof");
  res= (prepare_eof() || (!suppress_my_ok && send_ok_packet()));
  DBUG_RETURN(res);
}

void select_insert::abort_result_set() {

  DBUG_ENTER("select_insert::abort_result_set");
  /*
    If the creation of the table failed (due to a syntax error, for
    example), no table will have been opened and therefore 'table'
    will be NULL. In that case, we still need to execute the rollback
    and the end of the function.
   */
  if (table)
  {
    bool changed, transactional_table;
    /*
      If we are not in prelocked mode, we end the bulk insert started
      before.
    */
    if (thd->locked_tables_mode <= LTM_LOCK_TABLES)
      table->file->ha_end_bulk_insert();

    /*
      If at least one row has been inserted/modified and will stay in
      the table (the table doesn't have transactions) we must write to
      the binlog (and the error code will make the slave stop).

      For many errors (example: we got a duplicate key error while
      inserting into a MyISAM table), no row will be added to the table,
      so passing the error to the slave will not help since there will
      be an error code mismatch (the inserts will succeed on the slave
      with no error).

      If table creation failed, the number of rows modified will also be
      zero, so no check for that is made.
    */
    changed= (info.copied || info.deleted || info.updated);
    transactional_table= table->file->has_transactions();
    if (thd->transaction.stmt.modified_non_trans_table ||
        thd->log_current_statement)
    {
        if (!can_rollback_data())
          thd->transaction.all.modified_non_trans_table= TRUE;

        if(WSREP_EMULATE_BINLOG(thd) || mysql_bin_log.is_open())
        {
          int errcode= query_error_code(thd, thd->killed == NOT_KILLED);
          /* error of writing binary log is ignored */
          (void) thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query(),
                                   thd->query_length(),
                                   transactional_table, FALSE, FALSE, errcode);
        }
	if (changed)
	  query_cache_invalidate3(thd, table, 1);
    }
    DBUG_ASSERT(transactional_table || !changed ||
		thd->transaction.stmt.modified_non_trans_table);
    table->file->ha_release_auto_increment();
  }

  DBUG_VOID_RETURN;
}


/***************************************************************************
  CREATE TABLE (SELECT) ...
***************************************************************************/

Field *Item::create_field_for_create_select(TABLE *table)
{
  Field *def_field, *tmp_field;
  return ::create_tmp_field(table->in_use, table, this, type(),
                            (Item ***) 0, &tmp_field, &def_field, 0, 0, 0, 0);
}


/**
  Create table from lists of fields and items (or just return TABLE
  object for pre-opened existing table).

  @param thd           [in]     Thread object
  @param create_info   [in]     Create information (like MAX_ROWS, ENGINE or
                                temporary table flag)
  @param create_table  [in]     Pointer to TABLE_LIST object providing database
                                and name for table to be created or to be open
  @param alter_info    [in/out] Initial list of columns and indexes for the
                                table to be created
  @param items         [in]     List of items which should be used to produce
                                rest of fields for the table (corresponding
                                fields will be added to the end of
                                alter_info->create_list)
  @param lock          [out]    Pointer to the MYSQL_LOCK object for table
                                created will be returned in this parameter.
                                Since this table is not included in THD::lock
                                caller is responsible for explicitly unlocking
                                this table.
  @param hooks         [in]     Hooks to be invoked before and after obtaining
                                table lock on the table being created.

  @note
    This function assumes that either table exists and was pre-opened and
    locked at open_and_lock_tables() stage (and in this case we just emit
    error or warning and return pre-opened TABLE object) or an exclusive
    metadata lock was acquired on table so we can safely create, open and
    lock table in it (we don't acquire metadata lock if this create is
    for temporary table).

  @note
    Since this function contains some logic specific to CREATE TABLE ...
    SELECT it should be changed before it can be used in other contexts.

  @retval non-zero  Pointer to TABLE object for table created or opened
  @retval 0         Error
*/

static TABLE *create_table_from_items(THD *thd,
                                      Table_specification_st *create_info,
                                      TABLE_LIST *create_table,
                                      Alter_info *alter_info,
                                      List<Item> *items,
                                      MYSQL_LOCK **lock,
                                      TABLEOP_HOOKS *hooks)
{
  TABLE tmp_table;		// Used during 'Create_field()'
  TABLE_SHARE share;
  TABLE *table= 0;
  uint select_field_count= items->elements;
  /* Add selected items to field list */
  List_iterator_fast<Item> it(*items);
  Item *item;
  DBUG_ENTER("create_table_from_items");

  tmp_table.alias= 0;
  tmp_table.s= &share;
  init_tmp_table_share(thd, &share, "", 0, "", "");

  tmp_table.s->db_create_options=0;
  tmp_table.null_row= 0;
  tmp_table.maybe_null= 0;
  tmp_table.in_use= thd;

  if (!opt_explicit_defaults_for_timestamp)
    promote_first_timestamp_column(&alter_info->create_list);

  while ((item=it++))
  {
    Field *tmp_field= item->create_field_for_create_select(&tmp_table);

    if (!tmp_field)
      DBUG_RETURN(NULL);

    Field *table_field;

    switch (item->type())
    {
    /*
      We have to take into account both the real table's fields and
      pseudo-fields used in trigger's body. These fields are used
      to copy defaults values later inside constructor of
      the class Create_field.
    */
    case Item::FIELD_ITEM:
    case Item::TRIGGER_FIELD_ITEM:
      table_field= ((Item_field *) item)->field;
      break;
    default:
      table_field= NULL;
    }

    Create_field *cr_field= new (thd->mem_root)
                                  Create_field(thd, tmp_field, table_field);

    if (!cr_field)
      DBUG_RETURN(NULL);

    if (item->maybe_null)
      cr_field->flags &= ~NOT_NULL_FLAG;
    alter_info->create_list.push_back(cr_field, thd->mem_root);
  }

  DEBUG_SYNC(thd,"create_table_select_before_create");

  /* Check if LOCK TABLES + CREATE OR REPLACE of existing normal table*/
  if (thd->locked_tables_mode && create_table->table &&
      !create_info->tmp_table())
  {
    /* Remember information about the locked table */
    create_info->pos_in_locked_tables=
      create_table->table->pos_in_locked_tables;
    create_info->mdl_ticket= create_table->table->mdl_ticket;
  }

  /*
    Create and lock table.

    Note that we either creating (or opening existing) temporary table or
    creating base table on which name we have exclusive lock. So code below
    should not cause deadlocks or races.

    We don't log the statement, it will be logged later.

    If this is a HEAP table, the automatic DELETE FROM which is written to the
    binlog when a HEAP table is opened for the first time since startup, must
    not be written: 1) it would be wrong (imagine we're in CREATE SELECT: we
    don't want to delete from it) 2) it would be written before the CREATE
    TABLE, which is a wrong order. So we keep binary logging disabled when we
    open_table().
  */

  if (!mysql_create_table_no_lock(thd, create_table->db,
                                  create_table->table_name,
                                  create_info, alter_info, NULL,
                                  select_field_count))
  {
    DEBUG_SYNC(thd,"create_table_select_before_open");

    /*
      If we had a temporary table or a table used with LOCK TABLES,
      it was closed by mysql_create()
    */
    create_table->table= 0;

    if (!create_info->tmp_table())
    {
      Open_table_context ot_ctx(thd, MYSQL_OPEN_REOPEN);
      TABLE_LIST::enum_open_strategy save_open_strategy;

      /* Force the newly created table to be opened */
      save_open_strategy= create_table->open_strategy;
      create_table->open_strategy= TABLE_LIST::OPEN_NORMAL;
      /*
        Here we open the destination table, on which we already have
        an exclusive metadata lock.
      */
      if (open_table(thd, create_table, &ot_ctx))
      {
        quick_rm_table(thd, create_info->db_type, create_table->db,
                       table_case_name(create_info, create_table->table_name),
                       0);
      }
      /* Restore */
      create_table->open_strategy= save_open_strategy;
    }
    else
    {
      /*
        The pointer to the newly created temporary table has been stored in
        table->create_info.
      */
      create_table->table= create_info->table;
      if (!create_table->table)
      {
        /*
          This shouldn't happen as creation of temporary table should make
          it preparable for open. Anyway we can't drop temporary table if
          we are unable to find it.
        */
        DBUG_ASSERT(0);
      }
    }
  }
  else
    create_table->table= 0;                     // Create failed
  
  if (!(table= create_table->table))
  {
    if (!thd->is_error())                     // CREATE ... IF NOT EXISTS
      my_ok(thd);                             //   succeed, but did nothing
    DBUG_RETURN(NULL);
  }

  DEBUG_SYNC(thd,"create_table_select_before_lock");

  table->reginfo.lock_type=TL_WRITE;
  hooks->prelock(&table, 1);                    // Call prelock hooks
  /*
    mysql_lock_tables() below should never fail with request to reopen table
    since it won't wait for the table lock (we have exclusive metadata lock on
    the table) and thus can't get aborted.
  */
  if (! ((*lock)= mysql_lock_tables(thd, &table, 1, 0)) ||
        hooks->postlock(&table, 1))
  {
    /* purecov: begin tested */
    /*
      This can happen in innodb when you get a deadlock when using same table
      in insert and select or when you run out of memory.
    */
    my_error(ER_CANT_LOCK, MYF(0), my_errno);
    if (*lock)
    {
      mysql_unlock_tables(thd, *lock);
      *lock= 0;
    }
    drop_open_table(thd, table, create_table->db, create_table->table_name);
    DBUG_RETURN(0);
    /* purecov: end */
  }
  DBUG_RETURN(table);
}


int
select_create::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
{
  MYSQL_LOCK *extra_lock= NULL;
  DBUG_ENTER("select_create::prepare");

  TABLEOP_HOOKS *hook_ptr= NULL;
  /*
    For row-based replication, the CREATE-SELECT statement is written
    in two pieces: the first one contain the CREATE TABLE statement
    necessary to create the table and the second part contain the rows
    that should go into the table.

    For non-temporary tables, the start of the CREATE-SELECT
    implicitly commits the previous transaction, and all events
    forming the statement will be stored the transaction cache. At end
    of the statement, the entire statement is committed as a
    transaction, and all events are written to the binary log.

    On the master, the table is locked for the duration of the
    statement, but since the CREATE part is replicated as a simple
    statement, there is no way to lock the table for accesses on the
    slave.  Hence, we have to hold on to the CREATE part of the
    statement until the statement has finished.
   */
  class MY_HOOKS : public TABLEOP_HOOKS {
  public:
    MY_HOOKS(select_create *x, TABLE_LIST *create_table_arg,
             TABLE_LIST *select_tables_arg)
      : ptr(x),
        create_table(create_table_arg),
        select_tables(select_tables_arg)
      {
      }

  private:
    virtual int do_postlock(TABLE **tables, uint count)
    {
      int error;
      THD *thd= const_cast<THD*>(ptr->get_thd());
      TABLE_LIST *save_next_global= create_table->next_global;

      create_table->next_global= select_tables;

      error= thd->decide_logging_format(create_table);

      create_table->next_global= save_next_global;

      if (error)
        return error;

      TABLE const *const table = *tables;
      if (thd->is_current_stmt_binlog_format_row()  &&
          !table->s->tmp_table)
      {
        if (int error= ptr->binlog_show_create_table(tables, count))
          return error;
      }
      return 0;
    }
    select_create *ptr;
    TABLE_LIST *create_table;
    TABLE_LIST *select_tables;
  };

  MY_HOOKS hooks(this, create_table, select_tables);
  hook_ptr= &hooks;

  unit= u;

  /*
    Start a statement transaction before the create if we are using
    row-based replication for the statement.  If we are creating a
    temporary table, we need to start a statement transaction.
  */
  if (!thd->lex->tmp_table() &&
      thd->is_current_stmt_binlog_format_row() &&
      mysql_bin_log.is_open())
  {
    thd->binlog_start_trans_and_stmt();
  }

  DEBUG_SYNC(thd,"create_table_select_before_check_if_exists");

  if (!(table= create_table_from_items(thd, create_info,
                                       create_table,
                                       alter_info, &values,
                                       &extra_lock, hook_ptr)))
    /* abort() deletes table */
    DBUG_RETURN(-1);

  if (create_info->tmp_table())
  {
    /*
      When the temporary table was created & opened in create_table_impl(),
      the table's TABLE_SHARE (and thus TABLE) object was also linked to THD
      temporary tables lists. So, we must temporarily remove it from the
      list to keep them inaccessible from inner statements.
      e.g. CREATE TEMPORARY TABLE `t1` AS SELECT * FROM `t1`;
    */
    saved_tmp_table_share= thd->save_tmp_table_share(create_table->table);
  }

  if (extra_lock)
  {
    DBUG_ASSERT(m_plock == NULL);

    if (create_info->tmp_table())
      m_plock= &m_lock;
    else
      m_plock= &thd->extra_lock;

    *m_plock= extra_lock;
  }

  if (table->s->fields < values.elements)
  {
    my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), 1L);
    DBUG_RETURN(-1);
  }

  /* First field to copy */
  field= table->field+table->s->fields - values.elements;

  /* Mark all fields that are given values */
  for (Field **f= field ; *f ; f++)
    bitmap_set_bit(table->write_set, (*f)->field_index);

  table->next_number_field=table->found_next_number_field;

  restore_record(table,s->default_values);      // Get empty record
  thd->cuted_fields=0;
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
    table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
  if (info.handle_duplicates == DUP_REPLACE &&
      (!table->triggers || !table->triggers->has_delete_triggers()))
    table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE);
  if (info.handle_duplicates == DUP_UPDATE)
    table->file->extra(HA_EXTRA_INSERT_WITH_UPDATE);
  if (thd->locked_tables_mode <= LTM_LOCK_TABLES)
    table->file->ha_start_bulk_insert((ha_rows) 0);
  thd->abort_on_warning= !info.ignore && thd->is_strict_mode();
  if (check_that_all_fields_are_given_values(thd, table, table_list))
    DBUG_RETURN(1);
  table->mark_columns_needed_for_insert();
  table->file->extra(HA_EXTRA_WRITE_CACHE);
  // Mark table as used
  table->query_id= thd->query_id;
  DBUG_RETURN(0);
}

int
select_create::binlog_show_create_table(TABLE **tables, uint count)
{
  /*
    Note 1: In RBR mode, we generate a CREATE TABLE statement for the
    created table by calling show_create_table().  In the event of an error,
    nothing should be written to the binary log, even if the table is
    non-transactional; therefore we pretend that the generated CREATE TABLE
    statement is for a transactional table.  The event will then be put in the
    transaction cache, and any subsequent events (e.g., table-map events and
    binrow events) will also be put there.  We can then use
    ha_autocommit_or_rollback() to either throw away the entire kaboodle of
    events, or write them to the binary log.

    We write the CREATE TABLE statement here and not in prepare()
    since there potentially are sub-selects or accesses to information
    schema that will do a close_thread_tables(), destroying the
    statement transaction cache.
  */
  DBUG_ASSERT(thd->is_current_stmt_binlog_format_row());
  DBUG_ASSERT(tables && *tables && count > 0);

  char buf[2048];
  String query(buf, sizeof(buf), system_charset_info);
  int result;
  TABLE_LIST tmp_table_list;

  memset(&tmp_table_list, 0, sizeof(tmp_table_list));
  tmp_table_list.table = *tables;
  query.length(0);      // Have to zero it since constructor doesn't

  result= show_create_table(thd, &tmp_table_list, &query,
                            create_info, WITH_DB_NAME);
  DBUG_ASSERT(result == 0); /* show_create_table() always return 0 */

  if (WSREP_EMULATE_BINLOG(thd) || mysql_bin_log.is_open())
  {
    int errcode= query_error_code(thd, thd->killed == NOT_KILLED);
    result= thd->binlog_query(THD::STMT_QUERY_TYPE,
                              query.ptr(), query.length(),
                              /* is_trans */ TRUE,
                              /* direct */ FALSE,
                              /* suppress_use */ FALSE,
                              errcode);
  }

  ha_fake_trx_id(thd);

  return result;
}

void select_create::store_values(List<Item> &values)
{
  fill_record_n_invoke_before_triggers(thd, table, field, values, 1,
                                       TRG_EVENT_INSERT);
}


bool select_create::send_eof()
{
  DBUG_ENTER("select_create::send_eof");
  if (prepare_eof())
  {
    abort_result_set();
    DBUG_RETURN(true);
  }

  if (table->s->tmp_table)
  {
    /*
      Now is good time to add the new table to THD temporary tables list.
      But, before that we need to check if same table got created by the sub-
      statement.
    */
    if (thd->find_tmp_table_share(table->s->table_cache_key.str,
                                  table->s->table_cache_key.length))
    {
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table->alias.c_ptr());
      abort_result_set();
      DBUG_RETURN(true);
    }
    else
    {
      DBUG_ASSERT(saved_tmp_table_share);
      thd->restore_tmp_table_share(saved_tmp_table_share);
    }
  }

  /*
    Do an implicit commit at end of statement for non-temporary
    tables.  This can fail, but we should unlock the table
    nevertheless.
  */
  if (!table->s->tmp_table)
  {
    trans_commit_stmt(thd);
    if (!(thd->variables.option_bits & OPTION_GTID_BEGIN))
      trans_commit_implicit(thd);
#ifdef WITH_WSREP
    if (WSREP_ON)
    {
      mysql_mutex_lock(&thd->LOCK_wsrep_thd);
      if (thd->wsrep_conflict_state != NO_CONFLICT)
      {
        WSREP_DEBUG("select_create commit failed, thd: %lld  err: %d %s",
                    (longlong) thd->thread_id, thd->wsrep_conflict_state,
                    thd->query());
        mysql_mutex_unlock(&thd->LOCK_wsrep_thd);
        abort_result_set();
        DBUG_RETURN(true);
      }
      mysql_mutex_unlock(&thd->LOCK_wsrep_thd);
    }
#endif /* WITH_WSREP */
  }
  else if (!thd->is_current_stmt_binlog_format_row())
    table->s->table_creation_was_logged= 1;

  /*
    exit_done must only be set after last potential call to
    abort_result_set().
  */
  exit_done= 1;                                 // Avoid double calls

  table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
  table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);

  send_ok_packet();

  if (m_plock)
  {
    MYSQL_LOCK *lock= *m_plock;
    *m_plock= NULL;
    m_plock= NULL;

    if (create_info->pos_in_locked_tables)
    {
      /*
        If we are under lock tables, we have created a table that was
        originally locked. We should add back the lock to ensure that
        all tables in the thd->open_list are locked!
      */
      table->mdl_ticket= create_info->mdl_ticket;

      /* The following should never fail, except if out of memory */
      if (!thd->locked_tables_list.restore_lock(thd,
                                                create_info->
                                                pos_in_locked_tables,
                                                table, lock))
        DBUG_RETURN(false);                     // ok
      /* Fail. Continue without locking the table */
    }
    mysql_unlock_tables(thd, lock);
  }
  DBUG_RETURN(false);
}


void select_create::abort_result_set()
{
  ulonglong save_option_bits;
  DBUG_ENTER("select_create::abort_result_set");

  /* Avoid double calls, could happen in case of out of memory on cleanup */
  if (exit_done)
    DBUG_VOID_RETURN;
  exit_done= 1;

  /*
    In select_insert::abort_result_set() we roll back the statement, including
    truncating the transaction cache of the binary log. To do this, we
    pretend that the statement is transactional, even though it might
    be the case that it was not.

    We roll back the statement prior to deleting the table and prior
    to releasing the lock on the table, since there might be potential
    for failure if the rollback is executed after the drop or after
    unlocking the table.

    We also roll back the statement regardless of whether the creation
    of the table succeeded or not, since we need to reset the binary
    log state.
    
    However if there was an original table that was deleted, as part of
    create or replace table, then we must log the statement.
  */

  save_option_bits= thd->variables.option_bits;
  thd->variables.option_bits&= ~OPTION_BIN_LOG;
  select_insert::abort_result_set();
  thd->transaction.stmt.modified_non_trans_table= FALSE;
  thd->variables.option_bits= save_option_bits;

  /* possible error of writing binary log is ignored deliberately */
  (void) thd->binlog_flush_pending_rows_event(TRUE, TRUE);

  if (create_info->table_was_deleted)
  {
    /* Unlock locked table that was dropped by CREATE */
    thd->locked_tables_list.unlock_locked_table(thd,
                                                create_info->mdl_ticket);
  }
  if (m_plock)
  {
    mysql_unlock_tables(thd, *m_plock);
    *m_plock= NULL;
    m_plock= NULL;
  }

  if (table)
  {
    bool tmp_table= table->s->tmp_table;

    if (tmp_table)
    {
      DBUG_ASSERT(saved_tmp_table_share);
      thd->restore_tmp_table_share(saved_tmp_table_share);
    }

    table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
    table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
    table->auto_increment_field_not_null= FALSE;
    drop_open_table(thd, table, create_table->db, create_table->table_name);
    table=0;                                    // Safety
    if (thd->log_current_statement && mysql_bin_log.is_open())
    {
      /* Remove logging of drop, create + insert rows */
      binlog_reset_cache(thd);
      /* Original table was deleted. We have to log it */
      log_drop_table(thd, create_table->db, create_table->db_length,
                     create_table->table_name, create_table->table_name_length,
                     tmp_table);
    }
  }
  DBUG_VOID_RETURN;
}

References