Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
vuvova committed Sep 16, 2021
1 parent 0ff8976 commit d552e09
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 82 deletions.
24 changes: 12 additions & 12 deletions mysql-test/main/get_diagnostics.result
Expand Up @@ -1470,7 +1470,7 @@ ERROR 42000: Column 'id1' specified twice
GET DIAGNOSTICS CONDITION 1 @var118= ERROR_INDEX;
SELECT @var118;
@var118
0
1
CREATE FUNCTION f1() RETURNS INT
BEGIN
INSERT INTO t1 VALUES (1);
Expand All @@ -1481,38 +1481,38 @@ ERROR HY000: Can't update table 't1' in stored function/trigger because it is al
GET DIAGNOSTICS CONDITION 1 @var119= ERROR_INDEX;
SELECT @var119;
@var119
0
2
INSERT INTO t1 VALUES (1) RETURNING id2;
ERROR 42S22: Unknown column 'id2' in 'field list'
GET DIAGNOSTICS CONDITION 1 @var120= ERROR_INDEX;
SELECT @var120;
@var120
0
1
INSERT INTO t1(id2) VALUES(1);
ERROR 42S22: Unknown column 'id2' in 'field list'
GET DIAGNOSTICS CONDITION 1 @var121= ERROR_INDEX;
SELECT @var121;
@var121
0
1
INSERT INTO v VALUES(1,2);
ERROR HY000: Can not insert into join view 'test.v' without fields list
GET DIAGNOSTICS CONDITION 1 @var122= ERROR_INDEX;
SELECT @var122;
@var122
0
1
INSERT INTO v(a,b) VALUES (1,2);
ERROR HY000: Can not modify more than one base table through a join view 'test.v'
GET DIAGNOSTICS CONDITION 1 @var123= ERROR_INDEX;
SELECT @var123;
@var123
0
1
# REPLACE STATEMENT
REPLACE INTO t1(id1, id1) VALUES (1,1);
ERROR 42000: Column 'id1' specified twice
GET DIAGNOSTICS CONDITION 1 @var124= ERROR_INDEX;
SELECT @var124;
@var124
0
1
CREATE FUNCTION f2() RETURNS INT
BEGIN
REPLACE INTO t1 VALUES (1);
Expand All @@ -1523,31 +1523,31 @@ ERROR HY000: Can't update table 't1' in stored function/trigger because it is al
GET DIAGNOSTICS CONDITION 1 @var125= ERROR_INDEX;
SELECT @var125;
@var125
0
2
REPLACE INTO t1 VALUES (1) RETURNING id2;
ERROR 42S22: Unknown column 'id2' in 'field list'
GET DIAGNOSTICS CONDITION 1 @var126= ERROR_INDEX;
SELECT @var126;
@var126
0
1
REPLACE INTO t1(id2) VALUES(1);
ERROR 42S22: Unknown column 'id2' in 'field list'
GET DIAGNOSTICS CONDITION 1 @var127= ERROR_INDEX;
SELECT @var127;
@var127
0
1
REPLACE INTO v VALUES(1,2);
ERROR HY000: Can not insert into join view 'test.v' without fields list
GET DIAGNOSTICS CONDITION 1 @var128= ERROR_INDEX;
SELECT @var128;
@var128
0
1
REPLACE INTO v(a,b) VALUES (1,2);
ERROR HY000: Can not modify more than one base table through a join view 'test.v'
GET DIAGNOSTICS CONDITION 1 @var129= ERROR_INDEX;
SELECT @var129;
@var129
0
1
DROP TABLE t1,t2;
DROP FUNCTION f1;
DROP FUNCTION f2;
Expand Down
2 changes: 1 addition & 1 deletion sql/lex.h
Expand Up @@ -222,7 +222,7 @@ SYMBOL symbols[] = {
{ "ENUM", SYM(ENUM)},
{ "ERROR", SYM(ERROR_SYM)},
{ "ERRORS", SYM(ERRORS)},
{ "ERROR_INDEX", SYM(ERROR_INDEX_SYM)},
{ "ERROR_INDEX", SYM(ERROR_INDEX_SYM)},
{ "ESCAPE", SYM(ESCAPE_SYM)},
{ "ESCAPED", SYM(ESCAPED)},
{ "EVENT", SYM(EVENT_SYM)},
Expand Down
3 changes: 2 additions & 1 deletion sql/sp_rcontext.cc
Expand Up @@ -518,7 +518,8 @@ bool sp_rcontext::handle_sql_condition(THD *thd,
found_condition=
new (callers_arena->mem_root) Sql_condition(callers_arena->mem_root,
da->get_error_condition_identity(),
da->message(), 0);
da->message(),
da->current_row_for_warning());
}
}
else if (da->current_statement_warn_count())
Expand Down
6 changes: 0 additions & 6 deletions sql/sql_class.cc
Expand Up @@ -908,7 +908,6 @@ THD::THD(my_thread_id id, bool is_wsrep_applier)
org_charset= 0;
/* Restore THR_THD */
set_current_thd(old_THR_THD);
current_insert_index= 0;
}


Expand Down Expand Up @@ -1056,8 +1055,6 @@ Sql_condition* THD::raise_condition(uint sql_errno,
{
Diagnostics_area *da= get_stmt_da();
Sql_condition *cond= NULL;
ulonglong saved_error_index;

DBUG_ENTER("THD::raise_condition");
DBUG_ASSERT(level < Sql_condition::WARN_LEVEL_END);

Expand Down Expand Up @@ -1152,10 +1149,7 @@ Sql_condition* THD::raise_condition(uint sql_errno,
if (likely(!(is_fatal_error && (sql_errno == EE_OUTOFMEMORY ||
sql_errno == ER_OUTOFMEMORY))))
{
saved_error_index= this->current_insert_index;
this->current_insert_index= this->correct_error_index(sql_errno);
cond= da->push_warning(this, sql_errno, sqlstate, level, ucid, msg);
this->current_insert_index= saved_error_index;
}
DBUG_RETURN(cond);
}
Expand Down
23 changes: 0 additions & 23 deletions sql/sql_class.h
Expand Up @@ -5502,29 +5502,6 @@ class THD: public THD_count, /* this must be first */
{
lex= backup_lex;
}

/*
Stores the the processed record during INSERT/REPLACE. Used for assigning
value of error_index in case of warning or error.
*/
ulonglong current_insert_index;

/*
Error may take place in prepare phase and it might not be because of
rows/values we are inserting into the table, it could be because of say
something like wrong field name. In such case we want to return 0
for error index.
*/
ulonglong correct_error_index(uint error_no)
{
if (error_no == ER_FIELD_SPECIFIED_TWICE ||
error_no == ER_BAD_FIELD_ERROR ||
error_no == ER_VIEW_NO_INSERT_FIELD_LIST ||
error_no == ER_VIEW_MULTIUPDATE)
return 0;

return current_insert_index;
}
};


Expand Down
2 changes: 1 addition & 1 deletion sql/sql_error.cc
Expand Up @@ -673,7 +673,7 @@ Sql_condition *Warning_info::push_warning(THD *thd,
m_warn_list.elements() < thd->variables.max_error_count)
{
cond= new (& m_warn_root) Sql_condition(& m_warn_root, *value, msg,
thd->current_insert_index);
m_current_row_for_warning);
if (cond)
m_warn_list.push_back(cond);
}
Expand Down
24 changes: 10 additions & 14 deletions sql/sql_error.h
Expand Up @@ -306,6 +306,8 @@ class Sql_condition_items
/** SQL CURSOR_NAME condition item. */
String m_cursor_name;

ulong m_error_index;

Sql_condition_items()
:m_class_origin((const char*) NULL, 0, & my_charset_utf8mb3_bin),
m_subclass_origin((const char*) NULL, 0, & my_charset_utf8mb3_bin),
Expand All @@ -316,7 +318,8 @@ class Sql_condition_items
m_schema_name((const char*) NULL, 0, & my_charset_utf8mb3_bin),
m_table_name((const char*) NULL, 0, & my_charset_utf8mb3_bin),
m_column_name((const char*) NULL, 0, & my_charset_utf8mb3_bin),
m_cursor_name((const char*) NULL, 0, & my_charset_utf8mb3_bin)
m_cursor_name((const char*) NULL, 0, & my_charset_utf8mb3_bin),
m_error_index(0)
{ }

void clear()
Expand All @@ -331,6 +334,7 @@ class Sql_condition_items
m_table_name.length(0);
m_column_name.length(0);
m_cursor_name.length(0);
m_error_index= 0;
}
};

Expand Down Expand Up @@ -396,7 +400,7 @@ class Sql_condition : public Sql_alloc,
*/
Sql_condition()
:m_mem_root(NULL)
{ error_index= 0; }
{ }

/**
Complete the Sql_condition initialisation.
Expand All @@ -419,15 +423,13 @@ class Sql_condition : public Sql_alloc,
:m_mem_root(mem_root)
{
DBUG_ASSERT(mem_root != NULL);
error_index= 0;
}

Sql_condition(MEM_ROOT *mem_root, const Sql_user_condition_identity &ucid)
:Sql_condition_identity(Sql_state_errno_level(), ucid),
m_mem_root(mem_root)
{
DBUG_ASSERT(mem_root != NULL);
error_index= 0;
}
/**
Constructor for a fixed message text.
Expand All @@ -436,18 +438,15 @@ class Sql_condition : public Sql_alloc,
@param level - the error level for this condition
@param msg - the message text for this condition
*/
Sql_condition(MEM_ROOT *mem_root,
const Sql_condition_identity &value,
const char *msg,
ulonglong current_error_index)
:Sql_condition_identity(value),
m_mem_root(mem_root)
Sql_condition(MEM_ROOT *mem_root, const Sql_condition_identity &value,
const char *msg, ulong current_row_for_warning)
: Sql_condition_identity(value), m_mem_root(mem_root)
{
DBUG_ASSERT(mem_root != NULL);
DBUG_ASSERT(value.get_sql_errno() != 0);
DBUG_ASSERT(msg != NULL);
set_builtin_message_text(msg);
error_index= current_error_index;
m_error_index= current_row_for_warning;
}

/** Destructor. */
Expand Down Expand Up @@ -501,9 +500,6 @@ class Sql_condition : public Sql_alloc,

/** Memory root to use to hold condition item values. */
MEM_ROOT *m_mem_root;

/* Index of error for INSERT/REPLACE statement. */
ulonglong error_index;
};

///////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion sql/sql_get_diagnostics.cc
Expand Up @@ -339,7 +339,7 @@ Condition_information_item::get_value(THD *thd, const Sql_condition *cond)
value= make_utf8_string_item(thd, &str);
break;
case ERROR_INDEX:
value= new (thd->mem_root) Item_uint(thd, cond->error_index);
value= new (thd->mem_root) Item_uint(thd, cond->m_error_index);
}

DBUG_RETURN(value);
Expand Down
14 changes: 4 additions & 10 deletions sql/sql_insert.cc
Expand Up @@ -699,7 +699,6 @@ bool mysql_insert(THD *thd, TABLE_LIST *table_list,
const bool was_insert_delayed= (table_list->lock_type == TL_WRITE_DELAYED);
bool using_bulk_insert= 0;
uint value_count;
ulong counter = 1;
/* counter of iteration in bulk PS operation*/
ulonglong iteration= 0;
ulonglong id;
Expand All @@ -711,7 +710,6 @@ bool mysql_insert(THD *thd, TABLE_LIST *table_list,
Name_resolution_context_state ctx_state;
SELECT_LEX *returning= thd->lex->has_returning() ? thd->lex->returning() : 0;
unsigned char *readbuff= NULL;
thd->current_insert_index= 0;

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

while ((values= its++))
{
thd->current_insert_index= ++counter;
thd->get_stmt_da()->inc_current_row_for_warning();
if (values->elements != value_count)
{
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0),
thd->get_stmt_da()->current_row_for_warning());
goto abort;
}
if (setup_fields(thd, Ref_ptr_array(),
Expand All @@ -843,7 +842,7 @@ bool mysql_insert(THD *thd, TABLE_LIST *table_list,
switch_to_nullable_trigger_fields(*values, table);
}
its.rewind ();
thd->current_insert_index= 0;
thd->get_stmt_da()->reset_current_row_for_warning();

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

while ((values= its++))
{
thd->current_insert_index++;
if (fields.elements || !value_count)
{
/*
Expand Down Expand Up @@ -1134,7 +1132,6 @@ bool mysql_insert(THD *thd, TABLE_LIST *table_list,
} while (bulk_parameters_iterations(thd));

values_loop_end:
thd->current_insert_index= 0;
free_underlaid_joins(thd, thd->lex->first_select_lex());
joins_freed= TRUE;

Expand Down Expand Up @@ -1610,8 +1607,6 @@ int mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
bool res= 0;
table_map map= 0;
TABLE *table;
thd->current_insert_index= 1;

DBUG_ENTER("mysql_prepare_insert");
DBUG_PRINT("enter", ("table_list: %p view: %d",
table_list, (int) insert_into_view));
Expand Down Expand Up @@ -1665,7 +1660,6 @@ int mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
if (!res)
res= setup_fields(thd, Ref_ptr_array(),
update_values, MARK_COLUMNS_READ, 0, NULL, 0);
thd->current_insert_index= 0;

if (!res && duplic == DUP_UPDATE)
{
Expand Down
1 change: 0 additions & 1 deletion sql/sql_parse.cc
Expand Up @@ -7978,7 +7978,6 @@ void mysql_parse(THD *thd, char *rawbuf, uint length,
LEX *lex= thd->lex;

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

if (likely(!err))
{
Expand Down
9 changes: 4 additions & 5 deletions sql/sql_prepare.cc
Expand Up @@ -1314,7 +1314,6 @@ static bool mysql_test_insert_common(Prepared_statement *stmt,
if ((values= its++))
{
uint value_count;
ulong counter= 0;
Item *unused_conds= 0;

if (table_list->table)
Expand All @@ -1338,20 +1337,20 @@ static bool mysql_test_insert_common(Prepared_statement *stmt,
table_list->table_name.str));
goto error;
}
thd->current_insert_index= 0;
while ((values= its++))
{
thd->current_insert_index= ++counter;
if (values->elements != value_count)
{
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0), counter);
my_error(ER_WRONG_VALUE_COUNT_ON_ROW, MYF(0),
thd->get_stmt_da()->current_row_for_warning());
goto error;
}
if (setup_fields(thd, Ref_ptr_array(),
*values, COLUMNS_READ, 0, NULL, 0))
goto error;
thd->get_stmt_da()->inc_current_row_for_warning();
}
thd->current_insert_index= 0;
thd->get_stmt_da()->reset_current_row_for_warning();
}
DBUG_RETURN(FALSE);

Expand Down
3 changes: 2 additions & 1 deletion sql/sql_signal.cc
Expand Up @@ -419,7 +419,8 @@ bool Sql_cmd_resignal::execute(THD *thd)
DBUG_RETURN(result);
}

Sql_condition signaled_err(thd->mem_root, *signaled, signaled->message, 0);
Sql_condition signaled_err(thd->mem_root, *signaled, signaled->message,
da->current_row_for_warning());

if (m_cond)
{
Expand Down

0 comments on commit d552e09

Please sign in to comment.