Skip to content

Commit d552e09

Browse files
committed
MDEV-10075: Provide index of error causing error in array INSERT
use existing Warning_info::m_current_row_for_warning instead of a newly introduced counter. But use m_current_row_for_warning to count rows also in the parser and during prepare.
1 parent 0ff8976 commit d552e09

File tree

13 files changed

+43
-82
lines changed

13 files changed

+43
-82
lines changed

mysql-test/main/get_diagnostics.result

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,7 +1470,7 @@ ERROR 42000: Column 'id1' specified twice
14701470
GET DIAGNOSTICS CONDITION 1 @var118= ERROR_INDEX;
14711471
SELECT @var118;
14721472
@var118
1473-
0
1473+
1
14741474
CREATE FUNCTION f1() RETURNS INT
14751475
BEGIN
14761476
INSERT INTO t1 VALUES (1);
@@ -1481,38 +1481,38 @@ ERROR HY000: Can't update table 't1' in stored function/trigger because it is al
14811481
GET DIAGNOSTICS CONDITION 1 @var119= ERROR_INDEX;
14821482
SELECT @var119;
14831483
@var119
1484-
0
1484+
2
14851485
INSERT INTO t1 VALUES (1) RETURNING id2;
14861486
ERROR 42S22: Unknown column 'id2' in 'field list'
14871487
GET DIAGNOSTICS CONDITION 1 @var120= ERROR_INDEX;
14881488
SELECT @var120;
14891489
@var120
1490-
0
1490+
1
14911491
INSERT INTO t1(id2) VALUES(1);
14921492
ERROR 42S22: Unknown column 'id2' in 'field list'
14931493
GET DIAGNOSTICS CONDITION 1 @var121= ERROR_INDEX;
14941494
SELECT @var121;
14951495
@var121
1496-
0
1496+
1
14971497
INSERT INTO v VALUES(1,2);
14981498
ERROR HY000: Can not insert into join view 'test.v' without fields list
14991499
GET DIAGNOSTICS CONDITION 1 @var122= ERROR_INDEX;
15001500
SELECT @var122;
15011501
@var122
1502-
0
1502+
1
15031503
INSERT INTO v(a,b) VALUES (1,2);
15041504
ERROR HY000: Can not modify more than one base table through a join view 'test.v'
15051505
GET DIAGNOSTICS CONDITION 1 @var123= ERROR_INDEX;
15061506
SELECT @var123;
15071507
@var123
1508-
0
1508+
1
15091509
# REPLACE STATEMENT
15101510
REPLACE INTO t1(id1, id1) VALUES (1,1);
15111511
ERROR 42000: Column 'id1' specified twice
15121512
GET DIAGNOSTICS CONDITION 1 @var124= ERROR_INDEX;
15131513
SELECT @var124;
15141514
@var124
1515-
0
1515+
1
15161516
CREATE FUNCTION f2() RETURNS INT
15171517
BEGIN
15181518
REPLACE INTO t1 VALUES (1);
@@ -1523,31 +1523,31 @@ ERROR HY000: Can't update table 't1' in stored function/trigger because it is al
15231523
GET DIAGNOSTICS CONDITION 1 @var125= ERROR_INDEX;
15241524
SELECT @var125;
15251525
@var125
1526-
0
1526+
2
15271527
REPLACE INTO t1 VALUES (1) RETURNING id2;
15281528
ERROR 42S22: Unknown column 'id2' in 'field list'
15291529
GET DIAGNOSTICS CONDITION 1 @var126= ERROR_INDEX;
15301530
SELECT @var126;
15311531
@var126
1532-
0
1532+
1
15331533
REPLACE INTO t1(id2) VALUES(1);
15341534
ERROR 42S22: Unknown column 'id2' in 'field list'
15351535
GET DIAGNOSTICS CONDITION 1 @var127= ERROR_INDEX;
15361536
SELECT @var127;
15371537
@var127
1538-
0
1538+
1
15391539
REPLACE INTO v VALUES(1,2);
15401540
ERROR HY000: Can not insert into join view 'test.v' without fields list
15411541
GET DIAGNOSTICS CONDITION 1 @var128= ERROR_INDEX;
15421542
SELECT @var128;
15431543
@var128
1544-
0
1544+
1
15451545
REPLACE INTO v(a,b) VALUES (1,2);
15461546
ERROR HY000: Can not modify more than one base table through a join view 'test.v'
15471547
GET DIAGNOSTICS CONDITION 1 @var129= ERROR_INDEX;
15481548
SELECT @var129;
15491549
@var129
1550-
0
1550+
1
15511551
DROP TABLE t1,t2;
15521552
DROP FUNCTION f1;
15531553
DROP FUNCTION f2;

sql/lex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ SYMBOL symbols[] = {
222222
{ "ENUM",SYM(ENUM)},
223223
{ "ERROR", SYM(ERROR_SYM)},
224224
{ "ERRORS",SYM(ERRORS)},
225-
{ "ERROR_INDEX", SYM(ERROR_INDEX_SYM)},
225+
{ "ERROR_INDEX", SYM(ERROR_INDEX_SYM)},
226226
{ "ESCAPE",SYM(ESCAPE_SYM)},
227227
{ "ESCAPED",SYM(ESCAPED)},
228228
{ "EVENT",SYM(EVENT_SYM)},

sql/sp_rcontext.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,8 @@ bool sp_rcontext::handle_sql_condition(THD *thd,
518518
found_condition=
519519
new (callers_arena->mem_root) Sql_condition(callers_arena->mem_root,
520520
da->get_error_condition_identity(),
521-
da->message(), 0);
521+
da->message(),
522+
da->current_row_for_warning());
522523
}
523524
}
524525
else if (da->current_statement_warn_count())

sql/sql_class.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,6 @@ THD::THD(my_thread_id id, bool is_wsrep_applier)
908908
org_charset= 0;
909909
/* Restore THR_THD */
910910
set_current_thd(old_THR_THD);
911-
current_insert_index= 0;
912911
}
913912

914913

@@ -1056,8 +1055,6 @@ Sql_condition* THD::raise_condition(uint sql_errno,
10561055
{
10571056
Diagnostics_area *da= get_stmt_da();
10581057
Sql_condition *cond= NULL;
1059-
ulonglong saved_error_index;
1060-
10611058
DBUG_ENTER("THD::raise_condition");
10621059
DBUG_ASSERT(level < Sql_condition::WARN_LEVEL_END);
10631060

@@ -1152,10 +1149,7 @@ Sql_condition* THD::raise_condition(uint sql_errno,
11521149
if (likely(!(is_fatal_error && (sql_errno == EE_OUTOFMEMORY ||
11531150
sql_errno == ER_OUTOFMEMORY))))
11541151
{
1155-
saved_error_index= this->current_insert_index;
1156-
this->current_insert_index= this->correct_error_index(sql_errno);
11571152
cond= da->push_warning(this, sql_errno, sqlstate, level, ucid, msg);
1158-
this->current_insert_index= saved_error_index;
11591153
}
11601154
DBUG_RETURN(cond);
11611155
}

sql/sql_class.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5502,29 +5502,6 @@ class THD: public THD_count, /* this must be first */
55025502
{
55035503
lex= backup_lex;
55045504
}
5505-
5506-
/*
5507-
Stores the the processed record during INSERT/REPLACE. Used for assigning
5508-
value of error_index in case of warning or error.
5509-
*/
5510-
ulonglong current_insert_index;
5511-
5512-
/*
5513-
Error may take place in prepare phase and it might not be because of
5514-
rows/values we are inserting into the table, it could be because of say
5515-
something like wrong field name. In such case we want to return 0
5516-
for error index.
5517-
*/
5518-
ulonglong correct_error_index(uint error_no)
5519-
{
5520-
if (error_no == ER_FIELD_SPECIFIED_TWICE ||
5521-
error_no == ER_BAD_FIELD_ERROR ||
5522-
error_no == ER_VIEW_NO_INSERT_FIELD_LIST ||
5523-
error_no == ER_VIEW_MULTIUPDATE)
5524-
return 0;
5525-
5526-
return current_insert_index;
5527-
}
55285505
};
55295506

55305507

sql/sql_error.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ Sql_condition *Warning_info::push_warning(THD *thd,
673673
m_warn_list.elements() < thd->variables.max_error_count)
674674
{
675675
cond= new (& m_warn_root) Sql_condition(& m_warn_root, *value, msg,
676-
thd->current_insert_index);
676+
m_current_row_for_warning);
677677
if (cond)
678678
m_warn_list.push_back(cond);
679679
}

sql/sql_error.h

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ class Sql_condition_items
306306
/** SQL CURSOR_NAME condition item. */
307307
String m_cursor_name;
308308

309+
ulong m_error_index;
310+
309311
Sql_condition_items()
310312
:m_class_origin((const char*) NULL, 0, & my_charset_utf8mb3_bin),
311313
m_subclass_origin((const char*) NULL, 0, & my_charset_utf8mb3_bin),
@@ -316,7 +318,8 @@ class Sql_condition_items
316318
m_schema_name((const char*) NULL, 0, & my_charset_utf8mb3_bin),
317319
m_table_name((const char*) NULL, 0, & my_charset_utf8mb3_bin),
318320
m_column_name((const char*) NULL, 0, & my_charset_utf8mb3_bin),
319-
m_cursor_name((const char*) NULL, 0, & my_charset_utf8mb3_bin)
321+
m_cursor_name((const char*) NULL, 0, & my_charset_utf8mb3_bin),
322+
m_error_index(0)
320323
{ }
321324

322325
void clear()
@@ -331,6 +334,7 @@ class Sql_condition_items
331334
m_table_name.length(0);
332335
m_column_name.length(0);
333336
m_cursor_name.length(0);
337+
m_error_index= 0;
334338
}
335339
};
336340

@@ -396,7 +400,7 @@ class Sql_condition : public Sql_alloc,
396400
*/
397401
Sql_condition()
398402
:m_mem_root(NULL)
399-
{ error_index= 0; }
403+
{ }
400404

401405
/**
402406
Complete the Sql_condition initialisation.
@@ -419,15 +423,13 @@ class Sql_condition : public Sql_alloc,
419423
:m_mem_root(mem_root)
420424
{
421425
DBUG_ASSERT(mem_root != NULL);
422-
error_index= 0;
423426
}
424427

425428
Sql_condition(MEM_ROOT *mem_root, const Sql_user_condition_identity &ucid)
426429
:Sql_condition_identity(Sql_state_errno_level(), ucid),
427430
m_mem_root(mem_root)
428431
{
429432
DBUG_ASSERT(mem_root != NULL);
430-
error_index= 0;
431433
}
432434
/**
433435
Constructor for a fixed message text.
@@ -436,18 +438,15 @@ class Sql_condition : public Sql_alloc,
436438
@param level - the error level for this condition
437439
@param msg - the message text for this condition
438440
*/
439-
Sql_condition(MEM_ROOT *mem_root,
440-
const Sql_condition_identity &value,
441-
const char *msg,
442-
ulonglong current_error_index)
443-
:Sql_condition_identity(value),
444-
m_mem_root(mem_root)
441+
Sql_condition(MEM_ROOT *mem_root, const Sql_condition_identity &value,
442+
const char *msg, ulong current_row_for_warning)
443+
: Sql_condition_identity(value), m_mem_root(mem_root)
445444
{
446445
DBUG_ASSERT(mem_root != NULL);
447446
DBUG_ASSERT(value.get_sql_errno() != 0);
448447
DBUG_ASSERT(msg != NULL);
449448
set_builtin_message_text(msg);
450-
error_index= current_error_index;
449+
m_error_index= current_row_for_warning;
451450
}
452451

453452
/** Destructor. */
@@ -501,9 +500,6 @@ class Sql_condition : public Sql_alloc,
501500

502501
/** Memory root to use to hold condition item values. */
503502
MEM_ROOT *m_mem_root;
504-
505-
/* Index of error for INSERT/REPLACE statement. */
506-
ulonglong error_index;
507503
};
508504

509505
///////////////////////////////////////////////////////////////////////////

sql/sql_get_diagnostics.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ Condition_information_item::get_value(THD *thd, const Sql_condition *cond)
339339
value= make_utf8_string_item(thd, &str);
340340
break;
341341
case ERROR_INDEX:
342-
value= new (thd->mem_root) Item_uint(thd, cond->error_index);
342+
value= new (thd->mem_root) Item_uint(thd, cond->m_error_index);
343343
}
344344

345345
DBUG_RETURN(value);

sql/sql_insert.cc

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,6 @@ bool mysql_insert(THD *thd, TABLE_LIST *table_list,
699699
const bool was_insert_delayed= (table_list->lock_type == TL_WRITE_DELAYED);
700700
bool using_bulk_insert= 0;
701701
uint value_count;
702-
ulong counter = 1;
703702
/* counter of iteration in bulk PS operation*/
704703
ulonglong iteration= 0;
705704
ulonglong id;
@@ -711,7 +710,6 @@ bool mysql_insert(THD *thd, TABLE_LIST *table_list,
711710
Name_resolution_context_state ctx_state;
712711
SELECT_LEX *returning= thd->lex->has_returning() ? thd->lex->returning() : 0;
713712
unsigned char *readbuff= NULL;
714-
thd->current_insert_index= 0;
715713

716714
#ifndef EMBEDDED_LIBRARY
717715
char *query= thd->query();
@@ -831,10 +829,11 @@ bool mysql_insert(THD *thd, TABLE_LIST *table_list,
831829

832830
while ((values= its++))
833831
{
834-
thd->current_insert_index= ++counter;
832+
thd->get_stmt_da()->inc_current_row_for_warning();
835833
if (values->elements != value_count)
836834
{
837-
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
835+
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0),
836+
thd->get_stmt_da()->current_row_for_warning());
838837
goto abort;
839838
}
840839
if (setup_fields(thd, Ref_ptr_array(),
@@ -843,7 +842,7 @@ bool mysql_insert(THD *thd, TABLE_LIST *table_list,
843842
switch_to_nullable_trigger_fields(*values, table);
844843
}
845844
its.rewind ();
846-
thd->current_insert_index= 0;
845+
thd->get_stmt_da()->reset_current_row_for_warning();
847846

848847
/* Restore the current context. */
849848
ctx_state.restore_state(context, table_list);
@@ -1010,7 +1009,6 @@ bool mysql_insert(THD *thd, TABLE_LIST *table_list,
10101009

10111010
while ((values= its++))
10121011
{
1013-
thd->current_insert_index++;
10141012
if (fields.elements || !value_count)
10151013
{
10161014
/*
@@ -1134,7 +1132,6 @@ bool mysql_insert(THD *thd, TABLE_LIST *table_list,
11341132
} while (bulk_parameters_iterations(thd));
11351133

11361134
values_loop_end:
1137-
thd->current_insert_index= 0;
11381135
free_underlaid_joins(thd, thd->lex->first_select_lex());
11391136
joins_freed= TRUE;
11401137

@@ -1610,8 +1607,6 @@ int mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
16101607
bool res= 0;
16111608
table_map map= 0;
16121609
TABLE *table;
1613-
thd->current_insert_index= 1;
1614-
16151610
DBUG_ENTER("mysql_prepare_insert");
16161611
DBUG_PRINT("enter", ("table_list: %p view: %d",
16171612
table_list, (int) insert_into_view));
@@ -1665,7 +1660,6 @@ int mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
16651660
if (!res)
16661661
res= setup_fields(thd, Ref_ptr_array(),
16671662
update_values, MARK_COLUMNS_READ, 0, NULL, 0);
1668-
thd->current_insert_index= 0;
16691663

16701664
if (!res && duplic == DUP_UPDATE)
16711665
{

sql/sql_parse.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7978,7 +7978,6 @@ void mysql_parse(THD *thd, char *rawbuf, uint length,
79787978
LEX *lex= thd->lex;
79797979

79807980
bool err= parse_sql(thd, parser_state, NULL, true);
7981-
thd->current_insert_index= 0;
79827981

79837982
if (likely(!err))
79847983
{

0 commit comments

Comments
 (0)