Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions mysql-test/main/opt_context_replay_basic.result
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,24 @@ id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE NULL NULL NULL NULL NULL NULL 3 Deleting all rows
set optimizer_replay_context='';
drop table t1;
#
# MDEV-39410: ucs2 data not stored correctly in the context
#
create table t1 (a varchar(5) character set ucs2 collate ucs2_bin);
insert into t1 values (0x00410000);
set optimizer_record_context=1;
explain select hex(a) from t1 where a like 'A_';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 system NULL NULL NULL NULL 1
select context into dumpfile "../../tmp/dump1.sql"
from information_schema.optimizer_context;
set optimizer_record_context=0;
drop table t1;
set optimizer_replay_context='opt_context';
# Same query as above, must have same explain:
explain select hex(a) from t1 where a like 'A_';
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 system NULL NULL NULL NULL 1
set optimizer_replay_context='';
drop table t1;
drop database db1;
26 changes: 26 additions & 0 deletions mysql-test/main/opt_context_replay_basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,30 @@ set optimizer_replay_context='';
--remove_file "$MYSQLTEST_VARDIR/tmp/dump1.sql"
drop table t1;

--echo #
--echo # MDEV-39410: ucs2 data not stored correctly in the context
--echo #
create table t1 (a varchar(5) character set ucs2 collate ucs2_bin);
insert into t1 values (0x00410000);

set optimizer_record_context=1;
explain select hex(a) from t1 where a like 'A_';

select context into dumpfile "../../tmp/dump1.sql"
from information_schema.optimizer_context;
set optimizer_record_context=0;
drop table t1;
--disable_query_log
--disable_result_log
--source "$MYSQLTEST_VARDIR/tmp/dump1.sql"
--enable_query_log
--enable_result_log
set optimizer_replay_context='opt_context';
--echo # Same query as above, must have same explain:
explain select hex(a) from t1 where a like 'A_';

set optimizer_replay_context='';
--remove_file "$MYSQLTEST_VARDIR/tmp/dump1.sql"
drop table t1;

drop database db1;
5 changes: 3 additions & 2 deletions sql/filesort.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3039,7 +3039,8 @@ void format_and_store_row(TABLE *table, const uchar *rec, bool print_names,
{
Field **pfield;
char row_buff_tmp[512];
String tmp(row_buff_tmp, sizeof(row_buff_tmp), &my_charset_bin);
// charset will be overwritten below using field's charset
String tmp(row_buff_tmp, sizeof(row_buff_tmp), &my_charset_utf8mb4_bin);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This change appears to be unnecessary and could be confusing. The charset of the tmp string, initialized here, is immediately overwritten by the call to field->val_str(&tmp) on line 3128. For most field types, val_str invokes String::set, which updates both the content and the charset of the string object. Consequently, the initial charset specified in the String constructor is never actually used.

Setting it to &my_charset_utf8mb4_bin might mislead future developers into thinking this specific charset is important for the logic that follows. The original &my_charset_bin is a more generic and suitable choice for a temporary buffer. To enhance code clarity, I recommend reverting this change.

  String tmp(row_buff_tmp, sizeof(row_buff_tmp), &my_charset_bin);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good.

@bsrikanth-mariadb , please add a comment // charset will be overwritten


auto move_back_lambda= [table, rec]() mutable {
table->move_fields(table->field, table->record[0], rec);
Expand Down Expand Up @@ -3136,7 +3137,7 @@ void format_and_store_row(TABLE *table, const uchar *rec, bool print_names,
if (require_quote)
output.append('\'');
output.append_for_single_quote_opt_convert(tmp.ptr(), tmp.length(),
&my_charset_utf8mb4_bin);
field->charset());
if (require_quote)
output.append('\'');
}
Expand Down
Loading