22 #include "my_global.h"
25 #include "sql_update.h"
26 #include "sql_cache.h"
28 #include "sql_parse.h"
29 #include "sql_partition.h"
33 #include "sql_trigger.h"
34 #include "probes_mysql.h"
41 #include "opt_explain.h"
42 #include "sql_derived.h"
47 #include "sql_optimizer.h"
48 #include "sql_resolver.h"
54 bool records_are_comparable(
const TABLE *
table) {
55 return ((table->file->
ha_table_flags() & HA_PARTIAL_COLUMN_READ) == 0) ||
56 bitmap_is_subset(table->write_set, table->read_set);
73 bool compare_records(
const TABLE *table)
75 DBUG_ASSERT(records_are_comparable(table));
84 for (
Field **ptr= table->field ; *ptr != NULL; ptr++)
87 if (bitmap_is_set(table->write_set, field->field_index))
91 uchar null_byte_index= field->null_offset();
93 if (((table->record[0][null_byte_index]) & field->null_bit) !=
94 ((table->record[1][null_byte_index]) & field->null_bit))
97 if (field->cmp_binary_offset(table->s->rec_buff_length))
109 if (table->s->blob_fields + table->s->varchar_fields == 0)
111 return cmp_record(table,
record[1]);
113 if (memcmp(table->null_flags,
114 table->null_flags+table->s->rec_buff_length,
115 table->s->null_bytes))
118 for (
Field **ptr= table->field ; *ptr ; ptr++)
120 if (bitmap_is_set(table->write_set, (*ptr)->field_index) &&
121 (*ptr)->cmp_binary_offset(table->s->rec_buff_length))
149 if (!(field= item->field_for_view_update()))
152 my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), item->item_name.
ptr());
159 thd->change_item_tree(it.ref(),
new Item_field(thd, field));
174 static bool check_constant_expressions(
List<Item> &values)
178 DBUG_ENTER(
"check_constant_expressions");
182 if (!value->const_item())
184 DBUG_PRINT(
"exit", (
"expression is not constant"));
188 DBUG_PRINT(
"exit", (
"expression is constant"));
214 int mysql_update(THD *thd,
219 uint order_num,
ORDER *order,
221 enum enum_duplicates handle_duplicates,
bool ignore,
222 ha_rows *found_return, ha_rows *updated_return)
224 bool using_limit= limit != HA_POS_ERROR;
225 bool safe_update=
test(thd->variables.option_bits & OPTION_SAFE_UPDATES);
226 bool used_key_is_modified= FALSE, transactional_table, will_batch;
230 uint used_index, dup_key_found;
231 bool need_sort= TRUE;
234 bool read_removal=
false;
235 #ifndef NO_EMBEDDED_ACCESS_CHECKS
238 ha_rows updated, found;
243 SELECT_LEX *select_lex= &thd->lex->select_lex;
246 THD::killed_state killed_status= THD::NOT_KILLED;
247 COPY_INFO update(COPY_INFO::UPDATE_OPERATION, &fields, &values);
249 DBUG_ENTER(
"mysql_update");
254 if (table_list->multitable_view)
256 DBUG_ASSERT(table_list->view != 0);
257 DBUG_PRINT(
"info", (
"Switch to multi-update"));
262 THD_STAGE_INFO(thd, stage_init);
263 table= table_list->table;
265 if (!table_list->updatable)
267 my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias,
"UPDATE");
272 table->covering_keys= table->s->keys_in_use;
273 table->quick_keys.clear_all();
274 table->possible_quick_keys.clear_all();
276 #ifndef NO_EMBEDDED_ACCESS_CHECKS
278 want_privilege= (table_list->view ? UPDATE_ACL :
281 if (mysql_prepare_update(thd, table_list, &conds, order_num, order))
284 old_covering_keys= table->covering_keys;
286 #ifndef NO_EMBEDDED_ACCESS_CHECKS
288 table_list->register_want_access(want_privilege);
291 fields, MARK_COLUMNS_WRITE, 0, 0))
293 if (table_list->view && check_fields(thd, fields))
297 if (!table_list->updatable || check_key_in_view(thd, table_list))
299 my_error(ER_NON_UPDATABLE_TABLE, MYF(0), table_list->alias,
"UPDATE");
303 if (update.add_function_default_columns(table, table->write_set))
306 #ifndef NO_EMBEDDED_ACCESS_CHECKS
311 if (setup_fields(thd,
Ref_ptr_array(), values, MARK_COLUMNS_READ, 0, 0))
317 if (select_lex->inner_refs_list.elements &&
318 fix_inner_refs(thd, all_fields, select_lex, select_lex->ref_pointer_array))
321 if ((table->file->
ha_table_flags() & HA_PARTIAL_COLUMN_READ) != 0 &&
322 update.function_defaults_apply(table))
330 bitmap_union(table->read_set, table->write_set);
333 table->covering_keys.clear_all();
342 #ifdef WITH_PARTITION_STORAGE_ENGINE
343 if (table->part_info)
345 if (prune_partitions(thd, table, conds))
347 if (table->all_partitions_pruned_away)
350 if (thd->lex->describe)
352 error= explain_no_table(thd,
353 "No matching rows after partition pruning");
354 goto exit_without_my_ok;
361 if (
lock_tables(thd, table_list, thd->lex->table_count, 0))
368 Item::cond_result result;
369 if (table_list->check_option)
406 select_lex->join_list, &cond_equal);
410 conds=
optimize_cond(thd, conds, &cond_equal, select_lex->join_list,
413 if (result == Item::COND_FALSE)
416 if (thd->lex->describe)
418 error= explain_no_table(thd,
"Impossible WHERE");
419 goto exit_without_my_ok;
425 conds->update_used_tables();
429 #ifdef WITH_PARTITION_STORAGE_ENGINE
434 if (table->part_info)
436 if (prune_partitions(thd, table, conds))
438 if (table->all_partitions_pruned_away)
441 if (thd->lex->describe)
443 error= explain_no_table(thd,
444 "No matching rows after partition pruning");
445 goto exit_without_my_ok;
453 table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
456 select= make_select(table, 0, 0, conds, 0, &error);
460 wrapper.add_utf8_table(table);
462 if (error || !limit ||
463 (select && select->check_quick(thd, safe_update, limit)))
465 if (thd->lex->describe && !error && !thd->is_error())
467 error= explain_no_table(thd,
"Impossible WHERE");
468 goto exit_without_my_ok;
479 if (error || thd->is_error())
484 char buff[MYSQL_ERRMSG_SIZE];
485 my_snprintf(buff,
sizeof(buff), ER(ER_UPDATE_INFO), 0, 0,
486 (ulong) thd->get_stmt_da()->current_statement_warn_count());
487 my_ok(thd, 0, 0, buff);
489 DBUG_PRINT(
"info",(
"0 records updated"));
495 if (table->quick_keys.is_clear_all())
497 thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
498 if (safe_update && !using_limit)
500 my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
501 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
502 goto exit_without_my_ok;
505 init_ftfuncs(thd, select_lex, 1);
511 &need_sort, &reverse);
514 used_index= table->file->key_used_on_scan;
516 if (used_index != MAX_KEY)
518 used_key_is_modified= is_key_used(table, used_index, table->write_set);
520 else if (select && select->quick)
526 used_key_is_modified= (!select->quick->unique_key_range() &&
527 select->quick->is_keys_used(table->write_set));
530 #ifdef WITH_PARTITION_STORAGE_ENGINE
531 used_key_is_modified|= partition_key_modified(table, table->write_set);
534 using_filesort= order && (need_sort||used_key_is_modified);
535 if (thd->lex->describe)
537 const bool using_tmp_table= !using_filesort &&
538 (used_key_is_modified || order);
539 error= explain_single_table_modification(thd, table, select, used_index,
540 limit, using_tmp_table,
543 used_key_is_modified);
544 goto exit_without_my_ok;
547 if (used_key_is_modified || order)
554 if (used_index < MAX_KEY && old_covering_keys.is_set(used_index))
555 table->set_keyread(
true);
565 ha_rows examined_rows;
567 Filesort fsort(order, limit, select);
570 MYF(MY_FAE | MY_ZEROFILL));
571 if ((table->sort.found_records=
filesort(thd, table, &fsort,
true,
572 &examined_rows, &found_rows))
575 goto exit_without_my_ok;
577 thd->inc_examined_row_count(examined_rows);
595 if (open_cached_file(&tempfile, mysql_tmpdir,TEMP_PREFIX,
596 DISK_BUFFER_SIZE, MYF(MY_WME)))
597 goto exit_without_my_ok;
600 if (select && select->quick && (error= select->quick->reset()))
602 close_cached_file(&tempfile);
604 goto exit_without_my_ok;
619 if (used_index == MAX_KEY || (select && select->quick))
620 error= init_read_record(&info, thd, table, select, 0, 1, FALSE);
626 close_cached_file(&tempfile);
627 goto exit_without_my_ok;
630 THD_STAGE_INFO(thd, stage_searching_rows_for_update);
631 ha_rows tmp_limit=
limit;
633 while (!(error=info.read_record(&info)) && !thd->killed)
635 thd->inc_examined_row_count(1);
636 bool skip_record= FALSE;
637 if (select && select->skip_record(thd, &skip_record))
651 table->file->position(table->record[0]);
652 if (my_b_write(&tempfile,table->file->ref,
658 if (!--limit && using_limit)
665 table->file->unlock_row();
667 if (thd->killed && !error)
671 end_read_record(&info);
676 select->set_quick(NULL);
677 if (select->free_cond)
686 if (reinit_io_cache(&tempfile,READ_CACHE,0L,0,0))
688 select->file=tempfile;
690 goto exit_without_my_ok;
692 if (used_index < MAX_KEY && old_covering_keys.is_set(used_index))
693 table->set_keyread(
false);
697 table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
699 if (select && select->quick && (error= select->quick->reset()))
702 goto exit_without_my_ok;
705 if ((error= init_read_record(&info, thd, table, select, 0, 1, FALSE)))
706 goto exit_without_my_ok;
713 thd->count_cuted_fields= CHECK_FIELD_WARN;
714 thd->cuted_fields=0L;
715 THD_STAGE_INFO(thd, stage_updating);
717 transactional_table= table->file->has_transactions();
718 thd->abort_on_warning= (!ignore && thd->is_strict_mode());
720 if (table->triggers &&
721 table->triggers->has_triggers(TRG_EVENT_UPDATE,
729 (void) table->file->extra(HA_EXTRA_UPDATE_CANNOT_BATCH);
735 if ((table->file->
ha_table_flags() & HA_READ_BEFORE_WRITE_REMOVAL) &&
736 !ignore && !using_limit &&
737 select && select->quick && select->quick->index != MAX_KEY &&
738 check_constant_expressions(values))
741 while (!(error=info.read_record(&info)) && !thd->killed)
743 thd->inc_examined_row_count(1);
745 if (!select || (!select->skip_record(thd, &skip_record) && !skip_record))
750 store_record(table,
record[1]);
751 if (fill_record_n_invoke_before_triggers(thd, fields, values, 0,
758 if (!records_are_comparable(table) || compare_records(table))
760 if ((res= table_list->view_check_option(thd, ignore)) !=
764 if (res == VIEW_CHECK_SKIP)
766 else if (res == VIEW_CHECK_ERROR)
778 update.set_function_defaults(table);
811 limit+= dup_key_found;
812 updated-= dup_key_found;
817 error= table->file->ha_update_row(table->record[1],
820 if (!error || error == HA_ERR_RECORD_IS_THE_SAME)
822 if (error != HA_ERR_RECORD_IS_THE_SAME)
837 flags|= ME_FATALERROR;
845 if (table->triggers &&
847 TRG_ACTION_AFTER, TRUE))
853 if (!--limit && using_limit)
885 limit= dup_key_found;
886 updated-= dup_key_found;
899 else if (!thd->is_error())
900 table->file->unlock_row();
906 thd->get_stmt_da()->inc_current_row_for_warning();
913 table->auto_increment_field_not_null= FALSE;
923 killed_status= thd->killed;
925 DBUG_EXECUTE_IF(
"simulate_kill_bug27571",
927 thd->killed= THD::KILL_QUERY;
929 error= (killed_status == THD::NOT_KILLED)? error : 1;
944 table->file->
print_error(loc_error,MYF(ME_FATALERROR));
949 updated-= dup_key_found;
958 if (!records_are_comparable(table))
962 if (!transactional_table && updated > 0)
963 thd->transaction.stmt.mark_modified_non_trans_table();
965 end_read_record(&info);
967 THD_STAGE_INFO(thd, stage_end);
968 (void) table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
976 query_cache_invalidate3(thd, table_list, 1);
988 if ((error < 0) || thd->transaction.stmt.cannot_safely_rollback())
990 if (mysql_bin_log.is_open())
996 errcode= query_error_code(thd, killed_status == THD::NOT_KILLED);
998 if (thd->binlog_query(THD::ROW_QUERY_TYPE,
999 thd->query(), thd->query_length(),
1000 transactional_table, FALSE, FALSE, errcode))
1006 DBUG_ASSERT(transactional_table || !updated ||
1007 thd->transaction.stmt.cannot_safely_rollback());
1011 id= thd->arg_of_last_insert_id_function ?
1012 thd->first_successful_insert_id_in_prev_stmt : 0;
1016 char buff[MYSQL_ERRMSG_SIZE];
1017 my_snprintf(buff,
sizeof(buff), ER(ER_UPDATE_INFO), (ulong) found,
1019 (ulong) thd->get_stmt_da()->current_statement_warn_count());
1020 my_ok(thd, (thd->client_capabilities & CLIENT_FOUND_ROWS) ? found : updated,
1022 DBUG_PRINT(
"info",(
"%ld records updated", (
long) updated));
1024 thd->count_cuted_fields= CHECK_FIELD_IGNORE;
1025 thd->abort_on_warning= 0;
1026 *found_return= found;
1027 *updated_return= updated;
1028 DBUG_RETURN((error >= 0 || thd->is_error()) ? 1 : 0);
1033 table->set_keyread(FALSE);
1034 thd->abort_on_warning= 0;
1053 bool mysql_prepare_update(THD *thd,
TABLE_LIST *table_list,
1054 Item **conds, uint order_num,
ORDER *order)
1056 Item *fake_conds= 0;
1057 #ifndef NO_EMBEDDED_ACCESS_CHECKS
1058 TABLE *table= table_list->table;
1061 SELECT_LEX *select_lex= &thd->lex->select_lex;
1062 DBUG_ENTER(
"mysql_prepare_update");
1064 #ifndef NO_EMBEDDED_ACCESS_CHECKS
1067 table_list->register_want_access(SELECT_ACL);
1070 thd->lex->allow_sum_func= 0;
1073 &select_lex->top_join_list,
1075 &select_lex->leaf_tables,
1076 FALSE, UPDATE_ACL, SELECT_ACL) ||
1077 setup_conds(thd, table_list, select_lex->leaf_tables, conds) ||
1078 select_lex->setup_ref_array(thd, order_num) ||
1080 table_list, all_fields, all_fields, order) ||
1081 setup_ftfuncs(select_lex))
1087 if ((duplicate=
unique_table(thd, table_list, table_list->next_global, 0)))
1089 update_non_unique_table_error(table_list,
"UPDATE", duplicate);
1093 select_lex->fix_prepare_information(thd, conds, &fake_conds);
1106 static table_map get_table_map(
List<Item> *items)
1113 map|= item->used_tables();
1114 DBUG_PRINT(
"info", (
"table_map: 0x%08lx", (
long) map));
1148 bool unsafe_key_update(
TABLE_LIST *leaves, table_map tables_for_update)
1152 for (tl= leaves; tl ; tl= tl->next_leaf)
1154 if (tl->table->map & tables_for_update)
1156 TABLE *table1= tl->table;
1157 bool primkey_clustered= (table1->file->primary_key_is_clustered() &&
1158 table1->s->primary_key != MAX_KEY);
1160 bool table_partitioned=
false;
1161 #ifdef WITH_PARTITION_STORAGE_ENGINE
1162 table_partitioned= (table1->part_info != NULL);
1165 if (!table_partitioned && !primkey_clustered)
1168 for (
TABLE_LIST* tl2= tl->next_leaf; tl2 ; tl2= tl2->next_leaf)
1174 TABLE *table2= tl2->table;
1175 if (table2->map & tables_for_update && table1->s == table2->s)
1177 #ifdef WITH_PARTITION_STORAGE_ENGINE
1179 if (table_partitioned &&
1180 (partition_key_modified(table1, table1->write_set) ||
1181 partition_key_modified(table2, table2->write_set)))
1184 my_error(ER_MULTI_UPDATE_KEY_CONFLICT, MYF(0),
1185 tl->belong_to_view ? tl->belong_to_view->alias
1187 tl2->belong_to_view ? tl2->belong_to_view->alias
1193 if (primkey_clustered)
1196 KEY key_info= table1->key_info[table1->s->primary_key];
1201 for (;key_part != key_part_end; ++key_part)
1203 if (bitmap_is_set(table1->write_set, key_part->fieldnr-1) ||
1204 bitmap_is_set(table2->write_set, key_part->fieldnr-1))
1207 my_error(ER_MULTI_UPDATE_KEY_CONFLICT, MYF(0),
1208 tl->belong_to_view ? tl->belong_to_view->alias
1210 tl2->belong_to_view ? tl2->belong_to_view->alias
1251 static bool multi_update_check_table_access(THD *thd,
TABLE_LIST *table,
1252 table_map tables_for_update,
1257 bool updated=
false;
1265 DBUG_ASSERT(table->merge_underlying_list ||
1266 (!table->updatable &&
1267 !(table->table->map & tables_for_update)));
1269 for (
TABLE_LIST *tbl= table->merge_underlying_list; tbl;
1270 tbl= tbl->next_local)
1272 if (multi_update_check_table_access(thd, tbl, tables_for_update, &updated))
1278 *updated_arg|= updated;
1285 const bool updated= table->table->map & tables_for_update;
1289 *updated_arg|= updated;
1313 int mysql_multi_update_prepare(THD *thd)
1318 List<Item> *fields= &lex->select_lex.item_list;
1319 table_map tables_for_update;
1320 bool update_view= 0;
1321 const bool using_lock_tables= thd->locked_tables_mode != LTM_NONE;
1322 bool original_multiupdate= (thd->lex->sql_command == SQLCOM_UPDATE_MULTI);
1323 DBUG_ENTER(
"mysql_multi_update_prepare");
1326 thd->lex->sql_command= SQLCOM_UPDATE_MULTI;
1335 if (original_multiupdate &&
1337 (thd->stmt_arena->is_stmt_prepare() ?
1338 MYSQL_OPEN_FORCE_SHARED_MDL : 0)))
1346 if (setup_tables(thd, &lex->select_lex.context,
1347 &lex->select_lex.top_join_list,
1348 table_list, &lex->select_lex.leaf_tables,
1353 *fields, MARK_COLUMNS_WRITE, 0, 0))
1360 for (tl= table_list; tl ; tl= tl->next_local)
1365 tl->updating=
false;
1369 if (update_view && check_fields(thd, *fields))
1374 thd->table_map_for_update= tables_for_update= get_table_map(fields);
1376 leaves= lex->select_lex.leaf_tables;
1378 if (unsafe_key_update(leaves, tables_for_update))
1384 for (tl= leaves; tl; tl= tl->next_leaf)
1386 TABLE *table= tl->table;
1389 if (table->map & tables_for_update)
1391 if (!tl->updatable || check_key_in_view(thd, tl))
1393 my_error(ER_NON_UPDATABLE_TABLE, MYF(0), tl->alias,
"UPDATE");
1397 DBUG_PRINT(
"info",(
"setting table `%s` for update", tl->alias));
1405 DBUG_PRINT(
"info",(
"setting table `%s` for read-only", tl->alias));
1418 DBUG_ASSERT(tl->prelocking_placeholder ==
false);
1419 tl->lock_type= read_lock_type_for_table(thd, lex, tl,
true);
1422 if (!tl->placeholder() && !using_lock_tables)
1423 tl->table->reginfo.lock_type= tl->lock_type;
1432 for (tl= table_list; tl; tl= tl->next_local)
1434 bool not_used=
false;
1435 if (multi_update_check_table_access(thd, tl, tables_for_update, ¬_used))
1440 for (tl= table_list; tl; tl= tl->next_local)
1445 if (tl->check_single_table(&for_update, tables_for_update, tl))
1447 my_error(ER_VIEW_MULTIUPDATE, MYF(0),
1448 tl->view_db.str, tl->view_name.str);
1460 lex->select_lex.exclude_from_table_unique_test= TRUE;
1461 for (tl= leaves; tl; tl= tl->next_leaf)
1463 if (tl->lock_type != TL_READ &&
1464 tl->lock_type != TL_READ_NO_INSERT)
1469 update_non_unique_table_error(table_list,
"UPDATE", duplicate);
1478 lex->select_lex.exclude_from_table_unique_test= FALSE;
1479 DBUG_RETURN (FALSE);
1487 bool mysql_multi_update(THD *thd,
1493 enum enum_duplicates handle_duplicates,
1495 SELECT_LEX_UNIT *unit,
1496 SELECT_LEX *select_lex,
1497 multi_update **result)
1500 DBUG_ENTER(
"mysql_multi_update");
1502 if (!(*result=
new multi_update(table_list,
1503 thd->lex->select_lex.leaf_tables,
1505 handle_duplicates, ignore)))
1510 thd->abort_on_warning= (!ignore && thd->is_strict_mode());
1512 if (thd->lex->describe)
1513 res= explain_multi_table_modification(thd, *result);
1519 table_list, select_lex->with_wild,
1523 options | SELECT_NO_JOIN_CACHE | SELECT_NO_UNLOCK |
1525 *result, unit, select_lex);
1527 DBUG_PRINT(
"info",(
"res: %d report_error: %d",res, (
int) thd->is_error()));
1528 res|= thd->is_error();
1532 (*result)->send_error(ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR));
1533 (*result)->abort_result_set();
1536 thd->abort_on_warning= 0;
1541 multi_update::multi_update(
TABLE_LIST *table_list,
1544 enum enum_duplicates handle_duplicates_arg,
1546 :all_tables(table_list), leaves(leaves_list), update_tables(0),
1547 tmp_tables(0), updated(0), found(0), fields(field_list),
1548 values(value_list), table_count(0), copy_field(0),
1549 handle_duplicates(handle_duplicates_arg), do_update(1), trans_safe(1),
1550 transactional_tables(0), ignore(ignore_arg), error_handled(0),
1551 update_operations(NULL)
1559 int multi_update::prepare(
List<Item> ¬_used_values,
1560 SELECT_LEX_UNIT *lex_unit)
1564 table_map tables_to_update;
1569 uint leaf_table_count= 0;
1570 DBUG_ENTER(
"multi_update::prepare");
1572 thd->count_cuted_fields= CHECK_FIELD_WARN;
1573 thd->cuted_fields=0L;
1574 THD_STAGE_INFO(thd, stage_updating_main_table);
1576 tables_to_update= get_table_map(fields);
1578 if (!tables_to_update)
1580 my_message(ER_NO_TABLES_USED, ER(ER_NO_TABLES_USED), MYF(0));
1589 for (table_ref= leaves; table_ref; table_ref= table_ref->next_leaf)
1591 TABLE *table= table_ref->table;
1592 if (tables_to_update & table->map)
1594 DBUG_ASSERT(table->read_set == &table->def_read_set);
1595 table->read_set= &table->tmp_set;
1596 bitmap_clear_all(table->read_set);
1606 *values, MARK_COLUMNS_READ, 0, 0);
1608 for (table_ref= leaves; table_ref; table_ref= table_ref->next_leaf)
1610 TABLE *table= table_ref->table;
1611 if (tables_to_update & table->map)
1613 table->read_set= &table->def_read_set;
1614 bitmap_union(table->read_set, &table->tmp_set);
1628 for (table_ref= leaves; table_ref; table_ref= table_ref->next_leaf)
1631 TABLE *table=table_ref->table;
1633 if (tables_to_update & table->map)
1639 update.link_in_list(tl, &tl->next_local);
1640 tl->shared= table_count++;
1641 table->no_keyread=1;
1642 table->covering_keys.clear_all();
1643 table->pos_in_table_list= tl;
1644 if (table->triggers &&
1645 table->triggers->has_triggers(TRG_EVENT_UPDATE,
1653 (void) table->file->extra(HA_EXTRA_UPDATE_CANNOT_BATCH);
1659 table_count= update.elements;
1660 update_tables= update.
first;
1662 tmp_tables = (
TABLE**) thd->calloc(
sizeof(
TABLE *) * table_count);
1663 tmp_table_param = (TMP_TABLE_PARAM*) thd->calloc(
sizeof(TMP_TABLE_PARAM) *
1670 DBUG_ASSERT(update_operations == NULL);
1674 if (thd->is_fatal_error)
1676 for (i=0 ; i < table_count ; i++)
1681 if (thd->is_fatal_error)
1688 Item *value= value_it++;
1689 uint
offset= item->field->table->pos_in_table_list->shared;
1690 fields_for_table[
offset]->push_back(item);
1691 values_for_table[
offset]->push_back(value);
1693 if (thd->is_fatal_error)
1698 for (i=0 ; i < table_count ; i++)
1699 set_if_bigger(max_fields, fields_for_table[i]->elements + leaf_table_count);
1703 for (
TABLE_LIST *ref= leaves; ref != NULL; ref= ref->next_leaf)
1705 TABLE *table= ref->table;
1706 if (tables_to_update & table->map)
1708 const uint position= table->pos_in_table_list->shared;
1709 List<Item> *cols= fields_for_table[position];
1710 List<Item> *vals= values_for_table[position];
1712 new (thd->mem_root)
COPY_INFO(COPY_INFO::UPDATE_OPERATION, cols, vals);
1713 if (update == NULL ||
1717 update_operations[position]= update;
1719 if ((table->file->
ha_table_flags() & HA_PARTIAL_COLUMN_READ) != 0 &&
1728 bitmap_union(table->read_set, table->write_set);
1731 if (table->triggers)
1736 DBUG_RETURN(thd->is_fatal_error != 0);
1778 static bool safe_update_on_fly(THD *thd,
JOIN_TAB *join_tab,
1781 TABLE *table= join_tab->table;
1784 switch (join_tab->type) {
1790 case JT_REF_OR_NULL:
1791 return !is_key_used(table, join_tab->ref.
key, table->write_set);
1793 if (bitmap_is_overlapping(&table->tmp_set, table->write_set))
1796 if (join_tab->quick)
1797 return !join_tab->quick->is_keys_used(table->write_set);
1799 if ((table->file->
ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
1800 table->s->primary_key < MAX_KEY)
1801 return !is_key_used(table, table->s->primary_key, table->write_set);
1821 multi_update::initialize_tables(
JOIN *join)
1824 DBUG_ENTER(
"initialize_tables");
1828 main_table=join->join_tab->table;
1832 DBUG_ASSERT(fields->elements);
1839 TABLE *first_table_for_update= ((
Item_field *) fields->head())->field->table;
1842 for (table_ref= update_tables; table_ref; table_ref= table_ref->next_local)
1844 TABLE *table=table_ref->table;
1845 uint cnt= table_ref->shared;
1848 TMP_TABLE_PARAM *tmp_param;
1851 table->file->extra(HA_EXTRA_IGNORE_DUP_KEY);
1852 if (table == main_table)
1854 if (safe_update_on_fly(thd, join->join_tab, table_ref, all_tables))
1857 table_to_update=
table;
1869 if (table_ref->check_option && !join->
select_lex->uncacheable)
1871 SELECT_LEX_UNIT *tmp_unit;
1873 for (tmp_unit= join->
select_lex->first_inner_unit();
1875 tmp_unit= tmp_unit->next_unit())
1877 for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
1879 if (sl->master_unit()->item)
1881 join->
select_lex->uncacheable|= UNCACHEABLE_CHECKOPTION;
1889 if (table == first_table_for_update && table_ref->check_option)
1891 table_map unupdated_tables= table_ref->check_option->used_tables() &
1892 ~first_table_for_update->map;
1894 unupdated_tables && tbl_ref;
1895 tbl_ref= tbl_ref->next_leaf)
1897 if (unupdated_tables & tbl_ref->table->map)
1898 unupdated_tables&= ~tbl_ref->table->map;
1901 if (unupdated_check_opt_tables.push_back(tbl_ref->table))
1906 tmp_param= tmp_table_param+cnt;
1928 tbl->alias, &my_charset_bin);
1936 field->can_alter_field_type= 0;
1940 ifield->maybe_null= 0;
1941 if (temp_fields.push_back(ifield))
1943 }
while ((tbl= tbl_it++));
1945 temp_fields.concat(fields_for_table[cnt]);
1948 memset(&group, 0,
sizeof(group));
1949 group.direction= ORDER::ORDER_ASC;
1950 group.item= (
Item**) temp_fields.head_ref();
1952 tmp_param->quick_group=1;
1953 tmp_param->field_count=temp_fields.elements;
1954 tmp_param->group_parts=1;
1955 tmp_param->group_length= table->file->
ref_length;
1957 my_bool save_big_tables= thd->variables.big_tables;
1958 thd->variables.big_tables= FALSE;
1959 tmp_tables[cnt]=create_tmp_table(thd, tmp_param, temp_fields,
1960 (
ORDER*) &group, 0, 0,
1961 TMP_TABLE_ALL_COLUMNS, HA_POS_ERROR,
"");
1962 thd->variables.big_tables= save_big_tables;
1963 if (!tmp_tables[cnt])
1965 tmp_tables[cnt]->file->extra(HA_EXTRA_WRITE_CACHE);
1971 multi_update::~multi_update()
1974 for (table= update_tables ;
table; table= table->next_local)
1976 table->table->no_keyread= table->table->no_cache= 0;
1978 table->table->file->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1983 for (uint cnt = 0; cnt < table_count; cnt++)
1985 if (tmp_tables[cnt])
1987 free_tmp_table(thd, tmp_tables[cnt]);
1988 tmp_table_param[cnt].cleanup();
1993 delete [] copy_field;
1994 thd->count_cuted_fields= CHECK_FIELD_IGNORE;
1995 DBUG_ASSERT(trans_safe || !updated ||
1996 thd->transaction.stmt.cannot_safely_rollback());
1998 if (update_operations != NULL)
1999 for (uint i= 0; i < table_count; i++)
2000 delete update_operations[i];
2004 bool multi_update::send_data(
List<Item> ¬_used_values)
2007 DBUG_ENTER(
"multi_update::send_data");
2009 for (cur_table= update_tables; cur_table; cur_table= cur_table->next_local)
2011 TABLE *table= cur_table->table;
2012 uint offset= cur_table->shared;
2025 if (table->status & (STATUS_NULL_ROW | STATUS_UPDATED))
2028 if (table == table_to_update)
2030 table->status|= STATUS_UPDATED;
2031 store_record(table,
record[1]);
2032 if (fill_record_n_invoke_before_triggers(thd,
2033 *fields_for_table[offset],
2034 *values_for_table[offset],
2044 table->auto_increment_field_not_null= FALSE;
2046 if (!records_are_comparable(table) || compare_records(table))
2048 update_operations[
offset]->set_function_defaults(table);
2051 if ((error= cur_table->view_check_option(thd, ignore)) !=
2055 if (error == VIEW_CHECK_SKIP)
2057 else if (error == VIEW_CHECK_ERROR)
2067 main_table->file->extra(HA_EXTRA_PREPARE_FOR_UPDATE);
2069 if ((error=table->file->ha_update_row(table->record[1],
2070 table->record[0])) &&
2071 error != HA_ERR_RECORD_IS_THE_SAME)
2084 flags|= ME_FATALERROR;
2092 if (error == HA_ERR_RECORD_IS_THE_SAME)
2099 if (table->file->has_transactions())
2100 transactional_tables= TRUE;
2104 thd->transaction.stmt.mark_modified_non_trans_table();
2108 if (table->triggers &&
2110 TRG_ACTION_AFTER, TRUE))
2126 tbl->file->position(tbl->record[0]);
2127 memcpy((
char*) tmp_table->field[field_num]->ptr,
2128 (
char*) tbl->file->ref, tbl->file->
ref_length);
2134 tmp_table->field[field_num]->set_notnull();
2136 }
while ((tbl= tbl_it++));
2140 tmp_table->field + 1 + unupdated_check_opt_tables.elements,
2141 *values_for_table[offset], 1, NULL);
2144 error= tmp_table->file->ha_write_row(tmp_table->record[0]);
2145 if (error != HA_ERR_FOUND_DUPP_KEY && error != HA_ERR_FOUND_DUPP_UNIQUE)
2148 create_myisam_from_heap(thd, tmp_table,
2149 tmp_table_param[offset].start_recinfo,
2150 &tmp_table_param[offset].recinfo,
2164 void multi_update::send_error(uint errcode,
const char *err)
2167 my_error(errcode, MYF(0), err);
2171 void multi_update::abort_result_set()
2174 if (error_handled ||
2175 (!thd->transaction.stmt.cannot_safely_rollback() && !updated))
2180 query_cache_invalidate3(thd, update_tables, 1);
2188 DBUG_ASSERT(thd->transaction.stmt.cannot_safely_rollback());
2189 if (do_update && table_count > 1)
2196 (void) do_updates();
2199 if (thd->transaction.stmt.cannot_safely_rollback())
2205 if (mysql_bin_log.is_open())
2212 int errcode= query_error_code(thd, thd->killed == THD::NOT_KILLED);
2214 (void)thd->binlog_query(THD::ROW_QUERY_TYPE,
2215 thd->query(), thd->query_length(),
2216 transactional_tables, FALSE, FALSE, errcode);
2219 DBUG_ASSERT(trans_safe || !updated || thd->transaction.stmt.cannot_safely_rollback());
2223 int multi_update::do_updates()
2227 ha_rows org_updated;
2230 DBUG_ENTER(
"multi_update::do_updates");
2235 for (cur_table= update_tables; cur_table; cur_table= cur_table->next_local)
2237 uint offset= cur_table->shared;
2239 table = cur_table->table;
2240 if (table == table_to_update)
2242 org_updated= updated;
2243 tmp_table= tmp_tables[cur_table->shared];
2244 tmp_table->file->extra(HA_EXTRA_CACHE);
2247 table->file->extra(HA_EXTRA_NO_CACHE);
2249 check_opt_it.rewind();
2250 while(
TABLE *tbl= check_opt_it++)
2254 tbl->file->extra(HA_EXTRA_CACHE);
2261 Field **field= tmp_table->field +
2262 1 + unupdated_check_opt_tables.elements;
2263 Copy_field *copy_field_ptr= copy_field, *copy_field_end;
2264 for ( ; *field ; field++)
2267 (copy_field_ptr++)->
set(item->field, *field, 0);
2269 copy_field_end=copy_field_ptr;
2271 if ((local_error = tmp_table->file->
ha_rnd_init(1)))
2276 if (thd->killed && trans_safe)
2278 if ((local_error=tmp_table->file->
ha_rnd_next(tmp_table->record[0])))
2280 if (local_error == HA_ERR_END_OF_FILE)
2282 if (local_error == HA_ERR_RECORD_DELETED)
2288 check_opt_it.rewind();
2295 (uchar *) tmp_table->field[field_num]->ptr)))
2298 }
while((tbl= check_opt_it++));
2300 table->status|= STATUS_UPDATED;
2301 store_record(table,
record[1]);
2304 for (copy_field_ptr=copy_field;
2305 copy_field_ptr != copy_field_end;
2307 (*copy_field_ptr->do_copy)(copy_field_ptr);
2309 if (table->triggers &&
2311 TRG_ACTION_BEFORE, TRUE))
2314 if (!records_are_comparable(table) || compare_records(table))
2316 update_operations[
offset]->set_function_defaults(table);
2318 if ((error= cur_table->view_check_option(thd, ignore)) !=
2321 if (error == VIEW_CHECK_SKIP)
2323 else if (error == VIEW_CHECK_ERROR)
2326 local_error= table->file->ha_update_row(table->record[1],
2330 else if (local_error == HA_ERR_RECORD_IS_THE_SAME)
2339 if (table->triggers &&
2341 TRG_ACTION_AFTER, TRUE))
2345 if (updated != org_updated)
2347 if (table->file->has_transactions())
2348 transactional_tables= TRUE;
2352 thd->transaction.stmt.mark_modified_non_trans_table();
2357 check_opt_it.rewind();
2358 while (
TABLE *tbl= check_opt_it++)
2366 table->file->
print_error(local_error,MYF(ME_FATALERROR));
2370 if (table->file->inited)
2372 if (tmp_table->file->inited)
2374 check_opt_it.rewind();
2375 while (
TABLE *tbl= check_opt_it++)
2377 if (tbl->file->inited)
2381 if (updated != org_updated)
2383 if (table->file->has_transactions())
2384 transactional_tables= TRUE;
2388 thd->transaction.stmt.mark_modified_non_trans_table();
2397 bool multi_update::send_eof()
2399 char buff[STRING_BUFFER_USUAL_SIZE];
2401 THD::killed_state killed_status= THD::NOT_KILLED;
2402 DBUG_ENTER(
"multi_update::send_eof");
2403 THD_STAGE_INFO(thd, stage_updating_reference_tables);
2409 int local_error= thd->is_error();
2411 local_error = (table_count) ? do_updates() : 0;
2416 killed_status= (local_error == 0)? THD::NOT_KILLED : thd->killed;
2417 THD_STAGE_INFO(thd, stage_end);
2424 query_cache_invalidate3(thd, update_tables, 1);
2435 if (local_error == 0 || thd->transaction.stmt.cannot_safely_rollback())
2437 if (mysql_bin_log.is_open())
2440 if (local_error == 0)
2443 errcode= query_error_code(thd, killed_status == THD::NOT_KILLED);
2444 if (thd->binlog_query(THD::ROW_QUERY_TYPE,
2445 thd->query(), thd->query_length(),
2446 transactional_tables, FALSE, FALSE, errcode))
2452 DBUG_ASSERT(trans_safe || !updated ||
2453 thd->transaction.stmt.cannot_safely_rollback());
2455 if (local_error != 0)
2456 error_handled= TRUE;
2458 if (local_error > 0)
2461 my_message(ER_UNKNOWN_ERROR,
"An error occured in multi-table update",
2466 id= thd->arg_of_last_insert_id_function ?
2467 thd->first_successful_insert_id_in_prev_stmt : 0;
2468 my_snprintf(buff,
sizeof(buff), ER(ER_UPDATE_INFO),
2469 (ulong) found, (ulong) updated, (ulong) thd->cuted_fields);
2470 ::my_ok(thd, (thd->client_capabilities & CLIENT_FOUND_ROWS) ? found : updated,