From ef74a745f9f9c4dee21ac00658071a80dab4e64d Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Tue, 18 Jan 2022 14:25:45 +0800 Subject: [PATCH] fix cast expr when arg is zero length string (#3882) (#3885) --- dbms/src/Functions/FunctionsString.cpp | 24 +++++++++++++++ .../fullstack-test/expr/cast_nullability.test | 30 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/dbms/src/Functions/FunctionsString.cpp b/dbms/src/Functions/FunctionsString.cpp index 3a2200b5f8c..f1d32f75d4e 100644 --- a/dbms/src/Functions/FunctionsString.cpp +++ b/dbms/src/Functions/FunctionsString.cpp @@ -2839,6 +2839,12 @@ class tidbPadImpl vec_result_null_map[i] = tidbPadOneRow(&string_data[string_prev_offset], string_offsets[i] - string_prev_offset, static_cast(target_len), &(*padding_data)[padding_prev_offset], (*padding_offsets)[i] - padding_prev_offset, result_data, res_prev_offset); } } + else + { + result_data.resize(result_data.size() + 1); + result_data[res_prev_offset] = '\0'; + res_prev_offset++; + } string_prev_offset = string_offsets[i]; padding_prev_offset = (*padding_offsets)[i]; @@ -2870,6 +2876,12 @@ class tidbPadImpl vec_result_null_map[i] = tidbPadOneRow(&string_data[string_prev_offset], string_offsets[i] - string_prev_offset, static_cast(target_len), padding, padding_size, result_data, res_prev_offset); } } + else + { + result_data.resize(result_data.size() + 1); + result_data[res_prev_offset] = '\0'; + res_prev_offset++; + } string_prev_offset = string_offsets[i]; result_offsets[i] = res_prev_offset; @@ -2900,6 +2912,12 @@ class tidbPadImpl vec_result_null_map[i] = tidbPadOneRow(reinterpret_cast(str_val.c_str()), str_val.size() + 1, static_cast(target_len), &(*padding_data)[padding_prev_offset], (*padding_offsets)[i] - padding_prev_offset, result_data, res_prev_offset); } } + else + { + result_data.resize(result_data.size() + 1); + result_data[res_prev_offset] = '\0'; + res_prev_offset++; + } padding_prev_offset = (*padding_offsets)[i]; result_offsets[i] = res_prev_offset; @@ -2929,6 +2947,12 @@ class tidbPadImpl vec_result_null_map[i] = tidbPadOneRow(reinterpret_cast(str_val.c_str()), str_val.size() + 1, static_cast(target_len), padding, padding_size, result_data, res_prev_offset); } } + else + { + result_data.resize(result_data.size() + 1); + result_data[res_prev_offset] = '\0'; + res_prev_offset++; + } result_offsets[i] = res_prev_offset; } diff --git a/tests/fullstack-test/expr/cast_nullability.test b/tests/fullstack-test/expr/cast_nullability.test index 622e1366d92..a53e62e1492 100644 --- a/tests/fullstack-test/expr/cast_nullability.test +++ b/tests/fullstack-test/expr/cast_nullability.test @@ -2,8 +2,13 @@ mysql> drop table if exists test.t mysql> create table test.t(a int not null ) mysql> insert into test.t values(1),(2) mysql> alter table test.t set tiflash replica 1 +mysql> drop table if exists test.t1; +mysql> create table test.t1(c1 varchar(100)); +mysql> insert into test.t1 values(null); +mysql> alter table test.t1 set tiflash replica 1; func> wait_table test t +func> wait_table test t1 mysql> use test; set @@tidb_isolation_read_engines='tiflash'; select * from test.t union all select * from test.t +---+ @@ -24,3 +29,28 @@ mysql> use test; set @@tidb_isolation_read_engines='tiflash'; select count(*) , +----------+----+ mysql> drop table if exists test.t + +mysql> set tidb_isolation_read_engines='tiflash'; set tidb_enforce_mpp=1; select cast(lpad(test.t1.c1, 1, 'b') as datetime) from test.t1; ++--------------------------------------------+ +| cast(lpad(test.t1.c1, 1, 'b') as datetime) | ++--------------------------------------------+ +| NULL | ++--------------------------------------------+ +mysql> set tidb_isolation_read_engines='tiflash'; set tidb_enforce_mpp=1; select cast(lpad(test.t1.c1, 1, 'b') as decimal) from test.t1; ++-------------------------------------------+ +| cast(lpad(test.t1.c1, 1, 'b') as decimal) | ++-------------------------------------------+ +| NULL | ++-------------------------------------------+ +mysql> set tidb_isolation_read_engines='tiflash'; set tidb_enforce_mpp=1; select cast(lpad(test.t1.c1, 1, 'b') as signed) from test.t1; ++------------------------------------------+ +| cast(lpad(test.t1.c1, 1, 'b') as signed) | ++------------------------------------------+ +| NULL | ++------------------------------------------+ +mysql> set tidb_isolation_read_engines='tiflash'; set tidb_enforce_mpp=1; select cast(lpad(test.t1.c1, 1, 'b') as double) from test.t1; ++------------------------------------------+ +| cast(lpad(test.t1.c1, 1, 'b') as double) | ++------------------------------------------+ +| NULL | ++------------------------------------------+