From 3dffdee667666df9ade9f2c458bf1ea495ffba02 Mon Sep 17 00:00:00 2001 From: Oleksandr Byelkin Date: Thu, 25 Apr 2019 13:43:31 +0200 Subject: MDEV-17036: BULK with replace doesn't take the first parameter in account INSERT and REPLACE served by the same function, so flags (and processing) should be the same. --- sql/sql_parse.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'sql') diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 146c4d2d02e..68a08dc2b3f 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -609,7 +609,8 @@ void init_update_queries(void) CF_CAN_GENERATE_ROW_EVENTS | CF_OPTIMIZER_TRACE | CF_CAN_BE_EXPLAINED | - CF_INSERTS_DATA | CF_SP_BULK_SAFE; + CF_INSERTS_DATA | CF_SP_BULK_SAFE | + CF_SP_BULK_OPTIMIZED; sql_command_flags[SQLCOM_REPLACE_SELECT]= CF_CHANGES_DATA | CF_REEXECUTION_FRAGILE | CF_CAN_GENERATE_ROW_EVENTS | CF_OPTIMIZER_TRACE | -- cgit v1.2.1 From 4e01bc8c963d9513625dd984cd1aca24b8a7b516 Mon Sep 17 00:00:00 2001 From: Oleksandr Byelkin Date: Thu, 20 Dec 2018 09:52:34 +0100 Subject: MDEV-16240: Assertion `0' failed in row_sel_convert_mysql_key_to_innobase Set table in row ID position mode before using this function. --- sql/handler.cc | 15 +++++++++++-- sql/item_subselect.cc | 9 ++++++-- sql/item_subselect.h | 2 +- sql/log_event.cc | 7 ++++++ sql/sql_insert.cc | 60 +++++++++++++++++++++++++++++++++++++++++---------- sql/sql_load.cc | 7 ++++++ sql/sql_select.cc | 16 ++++++++++++++ sql/sql_update.cc | 8 +++++++ 8 files changed, 108 insertions(+), 16 deletions(-) (limited to 'sql') diff --git a/sql/handler.cc b/sql/handler.cc index ad1bad59efa..e5b452f9649 100644 --- a/sql/handler.cc +++ b/sql/handler.cc @@ -2649,8 +2649,7 @@ int handler::ha_rnd_pos(uchar *buf, uchar *pos) DBUG_ENTER("handler::ha_rnd_pos"); DBUG_ASSERT(table_share->tmp_table != NO_TMP_TABLE || m_lock_type != F_UNLCK); - /* TODO: Find out how to solve ha_rnd_pos when finding duplicate update. */ - /* DBUG_ASSERT(inited == RND); */ + DBUG_ASSERT(inited == RND); TABLE_IO_WAIT(tracker, m_psi, PSI_TABLE_FETCH_ROW, MAX_KEY, 0, { result= rnd_pos(buf, pos); }) @@ -3308,6 +3307,10 @@ void handler::get_auto_increment(ulonglong offset, ulonglong increment, ulonglong nr; int error; MY_BITMAP *old_read_set; + bool rnd_inited= (inited == RND); + + if (rnd_inited && ha_rnd_end()) + return; old_read_set= table->prepare_for_keyread(table->s->next_number_index); @@ -3317,6 +3320,10 @@ void handler::get_auto_increment(ulonglong offset, ulonglong increment, DBUG_ASSERT(0); (void) extra(HA_EXTRA_NO_KEYREAD); *first_value= ULONGLONG_MAX; + if (rnd_inited && ha_rnd_init_with_error(0)) + { + //TODO: it would be nice to return here an error + } return; } @@ -3363,6 +3370,10 @@ void handler::get_auto_increment(ulonglong offset, ulonglong increment, ha_index_end(); table->restore_column_maps_after_keyread(old_read_set); *first_value= nr; + if (rnd_inited && ha_rnd_init_with_error(0)) + { + //TODO: it would be nice to return here an error + } return; } diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index 8a9dd083911..8cff8f3a5c4 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -5855,12 +5855,16 @@ Ordered_key::cmp_keys_by_row_data_and_rownum(Ordered_key *key, } -void Ordered_key::sort_keys() +bool Ordered_key::sort_keys() { + if (tbl->file->ha_rnd_init_with_error(0)) + return TRUE; my_qsort2(key_buff, (size_t) key_buff_elements, sizeof(rownum_t), (qsort2_cmp) &cmp_keys_by_row_data_and_rownum, (void*) this); /* Invalidate the current row position. */ cur_key_idx= HA_POS_ERROR; + tbl->file->ha_rnd_end(); + return FALSE; } @@ -6313,7 +6317,8 @@ subselect_rowid_merge_engine::init(MY_BITMAP *non_null_key_parts, /* Sort the keys in each of the indexes. */ for (uint i= 0; i < merge_keys_count; i++) - merge_keys[i]->sort_keys(); + if (merge_keys[i]->sort_keys()) + return TRUE; if (init_queue(&pq, merge_keys_count, 0, FALSE, subselect_rowid_merge_engine::cmp_keys_by_cur_rownum, NULL, diff --git a/sql/item_subselect.h b/sql/item_subselect.h index bd6a1bdc498..5366c759795 100644 --- a/sql/item_subselect.h +++ b/sql/item_subselect.h @@ -1287,7 +1287,7 @@ public: ++cur_key_idx; } - void sort_keys(); + bool sort_keys(); double null_selectivity(); /* diff --git a/sql/log_event.cc b/sql/log_event.cc index 8990e1953b6..7ebc75dd1bf 100644 --- a/sql/log_event.cc +++ b/sql/log_event.cc @@ -12845,6 +12845,12 @@ Rows_log_event::write_row(rpl_group_info *rgi, if (table->file->ha_table_flags() & HA_DUPLICATE_POS) { DBUG_PRINT("info",("Locating offending record using rnd_pos()")); + + if ((error= table->file->ha_rnd_init_with_error(0))) + { + DBUG_RETURN(error); + } + error= table->file->ha_rnd_pos(table->record[1], table->file->dup_ref); if (error) { @@ -12854,6 +12860,7 @@ Rows_log_event::write_row(rpl_group_info *rgi, table->file->print_error(error, MYF(0)); DBUG_RETURN(error); } + table->file->ha_rnd_end(); } else { diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index c623336fdba..4005153cb64 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -881,7 +881,12 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, #endif /* EMBEDDED_LIBRARY */ { if (duplic != DUP_ERROR || ignore) + { table->file->extra(HA_EXTRA_IGNORE_DUP_KEY); + if (table->file->ha_table_flags() & HA_DUPLICATE_POS && + table->file->ha_rnd_init_with_error(0)) + goto abort; + } /** 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 @@ -942,7 +947,10 @@ bool mysql_insert(THD *thd,TABLE_LIST *table_list, { DBUG_PRINT("info", ("iteration %llu", iteration)); if (iteration && bulk_parameters_set(thd)) - goto abort; + { + error= 1; + goto values_loop_end; + } while ((values= its++)) { @@ -1094,7 +1102,11 @@ values_loop_end: error=1; } if (duplic != DUP_ERROR || ignore) + { table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); + if (table->file->ha_table_flags() & HA_DUPLICATE_POS) + table->file->ha_rnd_end(); + } transactional_table= table->file->has_transactions(); @@ -1705,6 +1717,7 @@ int write_record(THD *thd, TABLE *table,COPY_INFO *info) goto err; if (table->file->ha_table_flags() & HA_DUPLICATE_POS) { + DBUG_ASSERT(table->file->inited == handler::RND); if (table->file->ha_rnd_pos(table->record[1],table->file->dup_ref)) goto err; } @@ -3177,6 +3190,9 @@ bool Delayed_insert::handle_inserts(void) max_rows= ULONG_MAX; // Do as much as possible } + if (table->file->ha_rnd_init_with_error(0)) + goto err; + /* 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 @@ -3352,6 +3368,8 @@ bool Delayed_insert::handle_inserts(void) } } + table->file->ha_rnd_end(); + if (WSREP((&thd))) thd_proc_info(&thd, "insert done"); else @@ -3649,7 +3667,12 @@ select_insert::prepare(List &values, SELECT_LEX_UNIT *u) thd->cuted_fields=0; if (info.ignore || info.handle_duplicates != DUP_ERROR) + { table->file->extra(HA_EXTRA_IGNORE_DUP_KEY); + if (table->file->ha_table_flags() & HA_DUPLICATE_POS && + table->file->ha_rnd_init_with_error(0)) + DBUG_RETURN(1); + } if (info.handle_duplicates == DUP_REPLACE && (!table->triggers || !table->triggers->has_delete_triggers())) table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE); @@ -3818,6 +3841,9 @@ bool select_insert::prepare_eof() if (!error && thd->is_error()) error= thd->get_stmt_da()->sql_errno(); + if (info.ignore || info.handle_duplicates != DUP_ERROR) + if (table->file->ha_table_flags() & HA_DUPLICATE_POS) + table->file->ha_rnd_end(); table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE); @@ -3929,6 +3955,11 @@ void select_insert::abort_result_set() { if (thd->locked_tables_mode <= LTM_LOCK_TABLES) table->file->ha_end_bulk_insert(); + if (table->file->inited) + table->file->ha_rnd_end(); + table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); + table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE); + /* 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 @@ -4341,7 +4372,12 @@ select_create::prepare(List &values, SELECT_LEX_UNIT *u) 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 (table->file->ha_table_flags() & HA_DUPLICATE_POS && + table->file->ha_rnd_init_with_error(0)) + DBUG_RETURN(1); + } if (info.handle_duplicates == DUP_REPLACE && (!table->triggers || !table->triggers->has_delete_triggers())) table->file->extra(HA_EXTRA_WRITE_CAN_REPLACE); @@ -4522,9 +4558,6 @@ bool select_create::send_eof() */ 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) @@ -4600,13 +4633,6 @@ void select_create::abort_result_set() 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; @@ -4617,9 +4643,21 @@ void select_create::abort_result_set() thd->restore_tmp_table_share(saved_tmp_table_share); } + if (table->file->inited && + (info.ignore || info.handle_duplicates != DUP_ERROR) && + (table->file->ha_table_flags() & HA_DUPLICATE_POS)) + table->file->ha_rnd_end(); table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY); table->file->extra(HA_EXTRA_WRITE_CANNOT_REPLACE); table->auto_increment_field_not_null= FALSE; + + if (m_plock) + { + mysql_unlock_tables(thd, *m_plock); + *m_plock= NULL; + m_plock= NULL; + } + 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()) diff --git a/sql/sql_load.cc b/sql/sql_load.cc index 49558f8b694..0fcc4efbccd 100644 --- a/sql/sql_load.cc +++ b/sql/sql_load.cc @@ -660,6 +660,10 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, thd->abort_on_warning= !ignore && thd->is_strict_mode(); + if ((table_list->table->file->ha_table_flags() & HA_DUPLICATE_POS) && + (error= table_list->table->file->ha_rnd_init_with_error(0))) + goto err; + thd_progress_init(thd, 2); if (table_list->table->validate_default_values_of_unset_fields(thd)) { @@ -679,6 +683,9 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, set_fields, set_values, read_info, *ex->enclosed, skip_lines, ignore); + if (table_list->table->file->ha_table_flags() & HA_DUPLICATE_POS) + table_list->table->file->ha_rnd_end(); + thd_proc_info(thd, "End bulk insert"); if (!error) thd_progress_next_stage(thd); diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 3c1cea6be51..291560dc098 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -20318,6 +20318,15 @@ end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)), table->file->print_error(error,MYF(0)); /* purecov: inspected */ DBUG_RETURN(NESTED_LOOP_ERROR); /* purecov: inspected */ } + /* Prepare table for random positioning */ + bool rnd_inited= (table->file->inited == handler::RND); + if (!rnd_inited && + ((error= table->file->ha_index_end()) || + (error= table->file->ha_rnd_init(0)))) + { + table->file->print_error(error, MYF(0)); + DBUG_RETURN(NESTED_LOOP_ERROR); + } if (table->file->ha_rnd_pos(table->record[1],table->file->dup_ref)) { table->file->print_error(error,MYF(0)); /* purecov: inspected */ @@ -20331,6 +20340,13 @@ end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)), table->file->print_error(error,MYF(0)); /* purecov: inspected */ DBUG_RETURN(NESTED_LOOP_ERROR); /* purecov: inspected */ } + if (!rnd_inited && + ((error= table->file->ha_rnd_end()) || + (error= table->file->ha_index_init(0, 0)))) + { + table->file->print_error(error, MYF(0)); + DBUG_RETURN(NESTED_LOOP_ERROR); + } } if (join->thd->check_killed()) { diff --git a/sql/sql_update.cc b/sql/sql_update.cc index 80ecd820046..1da265d7c16 100644 --- a/sql/sql_update.cc +++ b/sql/sql_update.cc @@ -211,6 +211,14 @@ static void prepare_record_for_error_message(int error, TABLE *table) bitmap_union(table->read_set, &unique_map); /* Tell the engine about the new set. */ table->file->column_bitmaps_signal(); + + if ((error= table->file->ha_index_or_rnd_end()) || + (error= table->file->ha_rnd_init(0))) + { + table->file->print_error(error, MYF(0)); + DBUG_VOID_RETURN; + } + /* Read record that is identified by table->file->ref. */ (void) table->file->ha_rnd_pos(table->record[1], table->file->ref); /* Copy the newly read columns into the new record. */ -- cgit v1.2.1 From 5cfc7799a33576bb638652bac6e341570c80a7e1 Mon Sep 17 00:00:00 2001 From: Alexander Barkov Date: Fri, 26 Apr 2019 14:01:21 +0400 Subject: MDEV-16518 MYSQL57_GENERATED_FIELD: The code in TABLE_SHARE::init_from_binary_frm_image() is not safe --- sql/table.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'sql') diff --git a/sql/table.cc b/sql/table.cc index 2cf21c889e2..5337a506215 100644 --- a/sql/table.cc +++ b/sql/table.cc @@ -1839,7 +1839,8 @@ int TABLE_SHARE::init_from_binary_frm_image(THD *thd, bool write, goto err; vcol_info= new (&share->mem_root) Virtual_column_info(); vcol_info_length= uint2korr(vcol_screen_pos + 1); - DBUG_ASSERT(vcol_info_length); + if (!vcol_info_length) // Expect non-empty expression + goto err; vcol_info->stored_in_db= vcol_screen_pos[3]; vcol_info->utf8= 0; vcol_screen_pos+= vcol_info_length + MYSQL57_GCOL_HEADER_SIZE;; -- cgit v1.2.1