diff options
author | Alexander Barkov <bar@mariadb.com> | 2018-03-20 13:02:44 +0400 |
---|---|---|
committer | Alexander Barkov <bar@mariadb.com> | 2019-02-23 17:43:59 +0400 |
commit | 80c3fd184d4eeb66cd520079c3d23595e52cfdc0 (patch) | |
tree | 1785816792a2a8b8616803de27366bd171f425b5 /sql/sql_load.cc | |
parent | 8036ad541e9da4073a6136052e41c22c758b770e (diff) | |
download | mariadb-git-80c3fd184d4eeb66cd520079c3d23595e52cfdc0.tar.gz |
Backporting MDEV-15597 Add class Load_data_outvar and avoid using Item::STRING_ITEM for Item_user_var_as_out_param detection
This is a part of "MDEV-18045 Backporting the MDEV-15497 changes to 10.2 branch"
Diffstat (limited to 'sql/sql_load.cc')
-rw-r--r-- | sql/sql_load.cc | 323 |
1 files changed, 99 insertions, 224 deletions
diff --git a/sql/sql_load.cc b/sql/sql_load.cc index 53b43ebb1f5..37ac168de43 100644 --- a/sql/sql_load.cc +++ b/sql/sql_load.cc @@ -134,11 +134,10 @@ static bool wsrep_load_data_split(THD *thd, const TABLE *table, #define WSREP_LOAD_DATA_SPLIT(thd,table,info) /* empty */ #endif /* WITH_WSREP */ -class READ_INFO { +class READ_INFO: public Load_data_param +{ File file; String data; /* Read buffer */ - uint fixed_length; /* Length of the fixed length record */ - uint max_length; /* Max length of row */ Term_string m_field_term; /* FIELDS TERMINATED BY 'string' */ Term_string m_line_term; /* LINES TERMINATED BY 'string' */ Term_string m_line_start; /* LINES STARTING BY 'string' */ @@ -191,7 +190,7 @@ class READ_INFO { bool read_mbtail(String *str) { int chlen; - if ((chlen= my_charlen(read_charset, str->end() - 1, str->end())) == 1) + if ((chlen= my_charlen(charset(), str->end() - 1, str->end())) == 1) return false; // Single byte character found for (uint32 length0= str->length() - 1 ; MY_CS_IS_TOOSMALL(chlen); ) { @@ -202,7 +201,7 @@ class READ_INFO { return true; // EOF } str->append(chr); - chlen= my_charlen(read_charset, str->ptr() + length0, str->end()); + chlen= my_charlen(charset(), str->ptr() + length0, str->end()); if (chlen == MY_CS_ILSEQ) { /** @@ -224,10 +223,9 @@ public: bool error,line_cuted,found_null,enclosed; uchar *row_start, /* Found row starts here */ *row_end; /* Found row ends here */ - CHARSET_INFO *read_charset; LOAD_FILE_IO_CACHE cache; - READ_INFO(THD *thd, File file, uint tot_length, CHARSET_INFO *cs, + READ_INFO(THD *thd, File file, const Load_data_param ¶m, String &field_term,String &line_start,String &line_term, String &enclosed,int escape,bool get_it_from_net, bool is_fifo); ~READ_INFO(); @@ -282,6 +280,31 @@ static bool write_execute_load_query_log_event(THD *, sql_exchange*, const char*, const char*, bool, enum enum_duplicates, bool, bool, int); #endif /* EMBEDDED_LIBRARY */ + +bool Load_data_param::add_outvar_field(THD *thd, const Field *field) +{ + if (field->flags & BLOB_FLAG) + { + m_use_blobs= true; + m_fixed_length+= 256; // Will be extended if needed + } + else + m_fixed_length+= field->field_length; + return false; +} + + +bool Load_data_param::add_outvar_user_var(THD *thd) +{ + if (m_is_fixed_length) + { + my_error(ER_LOAD_FROM_FIXED_SIZE_ROWS_TO_VAR, MYF(0)); + return true; + } + return false; +} + + /* Execute LOAD DATA query @@ -313,8 +336,6 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, File file; TABLE *table= NULL; int error= 0; - String *field_term=ex->field_term,*escaped=ex->escaped; - String *enclosed=ex->enclosed; bool is_fifo=0; #ifndef EMBEDDED_LIBRARY killed_state killed_status; @@ -343,7 +364,7 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, read_file_from_client = 0; //server is always in the same process #endif - if (escaped->length() > 1 || enclosed->length() > 1) + if (ex->escaped->length() > 1 || ex->enclosed->length() > 1) { my_message(ER_WRONG_FIELD_TERMINATORS, ER_THD(thd, ER_WRONG_FIELD_TERMINATORS), @@ -352,8 +373,8 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, } /* Report problems with non-ascii separators */ - if (!escaped->is_ascii() || !enclosed->is_ascii() || - !field_term->is_ascii() || + if (!ex->escaped->is_ascii() || !ex->enclosed->is_ascii() || + !ex->field_term->is_ascii() || !ex->line_term->is_ascii() || !ex->line_start->is_ascii()) { push_warning(thd, Sql_condition::WARN_LEVEL_WARN, @@ -450,39 +471,21 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, table->prepare_triggers_for_insert_stmt_or_event(); table->mark_columns_needed_for_insert(); - uint tot_length=0; - bool use_blobs= 0, use_vars= 0; + Load_data_param param(ex->cs ? ex->cs : thd->variables.collation_database, + !ex->field_term->length() && !ex->enclosed->length()); List_iterator_fast<Item> it(fields_vars); Item *item; while ((item= it++)) { - Item *real_item= item->real_item(); - - if (real_item->type() == Item::FIELD_ITEM) - { - Field *field= ((Item_field*)real_item)->field; - if (field->flags & BLOB_FLAG) - { - use_blobs= 1; - tot_length+= 256; // Will be extended if needed - } - else - tot_length+= field->field_length; - } - else if (item->type() == Item::STRING_ITEM) - use_vars= 1; + const Load_data_outvar *var= item->get_load_data_outvar_or_error(); + if (!var || var->load_data_add_outvar(thd, ¶m)) + DBUG_RETURN(true); } - if (use_blobs && !ex->line_term->length() && !field_term->length()) + if (param.use_blobs() && !ex->line_term->length() && !ex->field_term->length()) { my_message(ER_BLOBS_AND_NO_TERMINATED, - ER_THD(thd, ER_BLOBS_AND_NO_TERMINATED), - MYF(0)); - DBUG_RETURN(TRUE); - } - if (use_vars && !field_term->length() && !enclosed->length()) - { - my_error(ER_LOAD_FROM_FIXED_SIZE_ROWS_TO_VAR, MYF(0)); + ER_THD(thd, ER_BLOBS_AND_NO_TERMINATED), MYF(0)); DBUG_RETURN(TRUE); } @@ -572,13 +575,13 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, bzero((char*) &info,sizeof(info)); info.ignore= ignore; info.handle_duplicates=handle_duplicates; - info.escape_char= (escaped->length() && (ex->escaped_given() || + info.escape_char= (ex->escaped->length() && (ex->escaped_given() || !(thd->variables.sql_mode & MODE_NO_BACKSLASH_ESCAPES))) - ? (*escaped)[0] : INT_MAX; + ? (*ex->escaped)[0] : INT_MAX; - READ_INFO read_info(thd, file, tot_length, - ex->cs ? ex->cs : thd->variables.collation_database, - *field_term,*ex->line_start, *ex->line_term, *enclosed, + READ_INFO read_info(thd, file, param, + *ex->field_term, *ex->line_start, + *ex->line_term, *ex->enclosed, info.escape_char, read_file_from_client, is_fifo); if (read_info.error) { @@ -639,14 +642,14 @@ int mysql_load(THD *thd,sql_exchange *ex,TABLE_LIST *table_list, error= read_xml_field(thd, info, table_list, fields_vars, set_fields, set_values, read_info, *(ex->line_term), skip_lines, ignore); - else if (!field_term->length() && !enclosed->length()) + else if (read_info.is_fixed_length()) error= read_fixed_length(thd, info, table_list, fields_vars, set_fields, set_values, read_info, skip_lines, ignore); else error= read_sep_field(thd, info, table_list, fields_vars, set_fields, set_values, read_info, - *enclosed, skip_lines, ignore); + *ex->enclosed, skip_lines, ignore); thd_proc_info(thd, "End bulk insert"); if (!error) @@ -850,14 +853,9 @@ static bool write_execute_load_query_log_event(THD *thd, sql_exchange* ex, { if (n++) query_str.append(", "); - if (item->real_type() == Item::FIELD_ITEM) - append_identifier(thd, &query_str, item->name, strlen(item->name)); - else - { - /* Actually Item_user_var_as_out_param despite claiming STRING_ITEM. */ - DBUG_ASSERT(item->type() == Item::STRING_ITEM); - ((Item_user_var_as_out_param *)item)->print_for_load(thd, &query_str); - } + const Load_data_outvar *var= item->get_load_data_outvar(); + DBUG_ASSERT(var); + var->load_data_print_for_log_event(thd, &query_str); } query_str.append(")"); } @@ -905,9 +903,9 @@ read_fixed_length(THD *thd, COPY_INFO &info, TABLE_LIST *table_list, ulong skip_lines, bool ignore_check_option_errors) { List_iterator_fast<Item> it(fields_vars); - Item_field *sql_field; + Item *item; TABLE *table= table_list->table; - bool err, progress_reports, auto_increment_field_not_null=false; + bool err, progress_reports; ulonglong counter, time_to_report_progress; DBUG_ENTER("read_fixed_length"); @@ -917,12 +915,6 @@ read_fixed_length(THD *thd, COPY_INFO &info, TABLE_LIST *table_list, if ((thd->progress.max_counter= read_info.file_length()) == ~(my_off_t) 0) progress_reports= 0; - while ((sql_field= (Item_field*) it++)) - { - if (sql_field->field == table->next_number_field) - auto_increment_field_not_null= true; - } - while (!read_info.read_fixed_length()) { if (thd->killed) @@ -958,36 +950,27 @@ read_fixed_length(THD *thd, COPY_INFO &info, TABLE_LIST *table_list, #endif restore_record(table, s->default_values); - /* - There is no variables in fields_vars list in this format so - this conversion is safe. - */ - while ((sql_field= (Item_field*) it++)) + + while ((item= it++)) { - Field *field= sql_field->field; - table->auto_increment_field_not_null= auto_increment_field_not_null; + Load_data_outvar *dst= item->get_load_data_outvar(); + DBUG_ASSERT(dst); if (pos == read_info.row_end) { - if (field->load_data_set_no_data(thd, true)) + if (dst->load_data_set_no_data(thd, &read_info)) DBUG_RETURN(1); - thd->cuted_fields++; /* Not enough fields */ - push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, - ER_WARN_TOO_FEW_RECORDS, - ER_THD(thd, ER_WARN_TOO_FEW_RECORDS), - thd->get_stmt_da()->current_row_for_warning()); } else { - uint length; - uchar save_chr; - if ((length=(uint) (read_info.row_end-pos)) > - field->field_length) - length=field->field_length; - save_chr=pos[length]; pos[length]='\0'; // Safeguard aganst malloc - field->load_data_set_value((char*) pos,length,read_info.read_charset); - pos[length]=save_chr; - if ((pos+=length) > read_info.row_end) - pos= read_info.row_end; /* Fills rest with space */ + uint length, fixed_length= dst->load_data_fixed_length(); + uchar save_chr; + if ((length=(uint) (read_info.row_end - pos)) > fixed_length) + length= fixed_length; + save_chr= pos[length]; pos[length]= '\0'; // Safeguard aganst malloc + dst->load_data_set_value(thd, (const char *) pos, length, &read_info); + pos[length]= save_chr; + if ((pos+= length) > read_info.row_end) + pos= read_info.row_end; // Fills rest with space } } if (pos != read_info.row_end) @@ -1087,8 +1070,6 @@ read_sep_field(THD *thd, COPY_INFO &info, TABLE_LIST *table_list, { uint length; uchar *pos; - Item_field *real_item; - if (read_info.read_field()) break; @@ -1099,48 +1080,22 @@ read_sep_field(THD *thd, COPY_INFO &info, TABLE_LIST *table_list, pos=read_info.row_start; length=(uint) (read_info.row_end-pos); - real_item= item->field_for_view_update(); + Load_data_outvar *dst= item->get_load_data_outvar_or_error(); + DBUG_ASSERT(dst); if ((!read_info.enclosed && (enclosed_length && length == 4 && !memcmp(pos, STRING_WITH_LEN("NULL")))) || (length == 1 && read_info.found_null)) { - if (item->type() == Item::STRING_ITEM) - { - ((Item_user_var_as_out_param *)item)->set_null_value( - read_info.read_charset); - } - else if (!real_item) - { - my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), item->name); + if (dst->load_data_set_null(thd, &read_info)) DBUG_RETURN(1); - } - else - { - DBUG_ASSERT(real_item->field->table == table); - if (real_item->field->load_data_set_null(thd)) - DBUG_RETURN(1); - } - - continue; - } - - if (item->type() == Item::STRING_ITEM) - { - ((Item_user_var_as_out_param *)item)->set_value((char*) pos, length, - read_info.read_charset); - } - else if (!real_item) - { - my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), item->name); - DBUG_RETURN(1); } else { - Field *field= real_item->field; - read_info.row_end[0]=0; // Safe to change end marker - field->load_data_set_value((char*) pos, length, read_info.read_charset); + read_info.row_end[0]= 0; // Safe to change end marker + if (dst->load_data_set_value(thd, (const char *) pos, length, &read_info)) + DBUG_RETURN(1); } } @@ -1161,34 +1116,10 @@ read_sep_field(THD *thd, COPY_INFO &info, TABLE_LIST *table_list, break; for (; item ; item= it++) { - Item_field *real_item= item->field_for_view_update(); - if (item->type() == Item::STRING_ITEM) - { - ((Item_user_var_as_out_param *)item)->set_null_value( - read_info.read_charset); - } - else if (!real_item) - { - my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), item->name); + Load_data_outvar *dst= item->get_load_data_outvar_or_error(); + DBUG_ASSERT(dst); + if (dst->load_data_set_no_data(thd, &read_info)) DBUG_RETURN(1); - } - else - { - Field *field= real_item->field; - if (field->load_data_set_no_data(thd, false)) - DBUG_RETURN(1); - /* - TODO: We probably should not throw warning for each field. - But how about intention to always have the same number - of warnings in THD::cuted_fields (and get rid of cuted_fields - in the end ?) - */ - thd->cuted_fields++; - push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, - ER_WARN_TOO_FEW_RECORDS, - ER_THD(thd, ER_WARN_TOO_FEW_RECORDS), - thd->get_stmt_da()->current_row_for_warning()); - } } } @@ -1250,7 +1181,6 @@ read_xml_field(THD *thd, COPY_INFO &info, TABLE_LIST *table_list, Item *item; TABLE *table= table_list->table; bool no_trans_update_stmt; - CHARSET_INFO *cs= read_info.read_charset; DBUG_ENTER("read_xml_field"); no_trans_update_stmt= !table->file->has_transactions(); @@ -1296,41 +1226,14 @@ read_xml_field(THD *thd, COPY_INFO &info, TABLE_LIST *table_list, while(tag && strcmp(tag->field.c_ptr(), item->name) != 0) tag= xmlit++; - - Item_field *real_item= item->field_for_view_update(); - if (!tag) // found null - { - if (item->type() == Item::STRING_ITEM) - ((Item_user_var_as_out_param *) item)->set_null_value(cs); - else if (!real_item) - { - my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), item->name); - DBUG_RETURN(1); - } - else - { - DBUG_ASSERT(real_item->field->table == table); - if (real_item->field->load_data_set_null(thd)) - DBUG_RETURN(1); - } - continue; - } - if (item->type() == Item::STRING_ITEM) - ((Item_user_var_as_out_param *) item)->set_value( - (char *) tag->value.ptr(), - tag->value.length(), cs); - else if (!real_item) - { - my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), item->name); + Load_data_outvar *dst= item->get_load_data_outvar_or_error(); + DBUG_ASSERT(dst); + if (!tag ? dst->load_data_set_null(thd, &read_info) : + dst->load_data_set_value(thd, tag->value.ptr(), + tag->value.length(), + &read_info)) DBUG_RETURN(1); - } - else - { - - Field *field= ((Item_field *)item)->field; - field->load_data_set_value(tag->value.ptr(), tag->value.length(), cs); - } } if (read_info.error) @@ -1341,39 +1244,8 @@ read_xml_field(THD *thd, COPY_INFO &info, TABLE_LIST *table_list, skip_lines--; continue; } - - if (item) - { - /* Have not read any field, thus input file is simply ended */ - if (item == fields_vars.head()) - break; - - for ( ; item; item= it++) - { - Item_field *real_item= item->field_for_view_update(); - if (item->type() == Item::STRING_ITEM) - ((Item_user_var_as_out_param *)item)->set_null_value(cs); - else if (!real_item) - { - my_error(ER_NONUPDATEABLE_COLUMN, MYF(0), item->name); - DBUG_RETURN(1); - } - else - { - /* - QQ: We probably should not throw warning for each field. - But how about intention to always have the same number - of warnings in THD::cuted_fields (and get rid of cuted_fields - in the end ?) - */ - thd->cuted_fields++; - push_warning_printf(thd, Sql_condition::WARN_LEVEL_WARN, - ER_WARN_TOO_FEW_RECORDS, - ER_THD(thd, ER_WARN_TOO_FEW_RECORDS), - thd->get_stmt_da()->current_row_for_warning()); - } - } - } + + DBUG_ASSERT(!item); if (thd->killed || fill_record_n_invoke_before_triggers(thd, table, set_fields, set_values, @@ -1435,14 +1307,16 @@ READ_INFO::unescape(char chr) */ -READ_INFO::READ_INFO(THD *thd, File file_par, uint tot_length, CHARSET_INFO *cs, +READ_INFO::READ_INFO(THD *thd, File file_par, + const Load_data_param ¶m, String &field_term, String &line_start, String &line_term, String &enclosed_par, int escape, bool get_it_from_net, bool is_fifo) - :file(file_par), fixed_length(tot_length), + :Load_data_param(param), + file(file_par), m_field_term(field_term), m_line_term(line_term), m_line_start(line_start), escape_char(escape), found_end_of_line(false), eof(false), - error(false), line_cuted(false), found_null(false), read_charset(cs) + error(false), line_cuted(false), found_null(false) { data.set_thread_specific(); /* @@ -1459,12 +1333,13 @@ READ_INFO::READ_INFO(THD *thd, File file_par, uint tot_length, CHARSET_INFO *cs, enclosed_char= enclosed_par.length() ? (uchar) enclosed_par[0] : INT_MAX; /* Set of a stack for unget if long terminators */ - uint length= MY_MAX(cs->mbmaxlen, MY_MAX(m_field_term.length(), - m_line_term.length())) + 1; + uint length= MY_MAX(charset()->mbmaxlen, MY_MAX(m_field_term.length(), + m_line_term.length())) + 1; set_if_bigger(length,line_start.length()); stack= stack_pos= (int*) thd->alloc(sizeof(int) * length); - if (data.reserve(tot_length)) + DBUG_ASSERT(m_fixed_length < UINT_MAX32); + if (data.reserve((size_t) m_fixed_length)) error=1; /* purecov: inspected */ else { @@ -1606,7 +1481,7 @@ int READ_INFO::read_field() for (;;) { // Make sure we have enough space for the longest multi-byte character. - while (data.length() + read_charset->mbmaxlen <= data.alloced_length()) + while (data.length() + charset()->mbmaxlen <= data.alloced_length()) { chr = GET; if (chr == my_b_EOF) @@ -1692,7 +1567,7 @@ int READ_INFO::read_field() } } data.append(chr); - if (use_mb(read_charset) && read_mbtail(&data)) + if (use_mb(charset()) && read_mbtail(&data)) goto found_eof; } /* @@ -1738,7 +1613,7 @@ int READ_INFO::read_fixed_length() return 1; } - for (data.length(0); data.length() < fixed_length ; ) + for (data.length(0); data.length() < m_fixed_length ; ) { if ((chr=GET) == my_b_EOF) goto found_eof; @@ -1791,8 +1666,8 @@ int READ_INFO::next_line() if (getbyte(&buf[0])) return 1; // EOF - if (use_mb(read_charset) && - (chlen= my_charlen(read_charset, buf, buf + 1)) != 1) + if (use_mb(charset()) && + (chlen= my_charlen(charset(), buf, buf + 1)) != 1) { uint i; for (i= 1; MY_CS_IS_TOOSMALL(chlen); ) @@ -1801,7 +1676,7 @@ int READ_INFO::next_line() DBUG_ASSERT(chlen != 1); if (getbyte(&buf[i++])) return 1; // EOF - chlen= my_charlen(read_charset, buf, buf + i); + chlen= my_charlen(charset(), buf, buf + i); } /* @@ -1972,7 +1847,7 @@ int READ_INFO::read_value(int delim, String *val) else { val->append(chr); - if (use_mb(read_charset) && read_mbtail(val)) + if (use_mb(charset()) && read_mbtail(val)) return my_b_EOF; } } |