Skip to content

Commit

Permalink
fix cast expr when arg is zero length string (#3882) (#3885)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Jan 18, 2022
1 parent 01793a4 commit ef74a74
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
24 changes: 24 additions & 0 deletions dbms/src/Functions/FunctionsString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2839,6 +2839,12 @@ class tidbPadImpl
vec_result_null_map[i] = tidbPadOneRow<IsLeft>(&string_data[string_prev_offset], string_offsets[i] - string_prev_offset, static_cast<int32_t>(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];
Expand Down Expand Up @@ -2870,6 +2876,12 @@ class tidbPadImpl
vec_result_null_map[i] = tidbPadOneRow<IsLeft>(&string_data[string_prev_offset], string_offsets[i] - string_prev_offset, static_cast<int32_t>(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;
Expand Down Expand Up @@ -2900,6 +2912,12 @@ class tidbPadImpl
vec_result_null_map[i] = tidbPadOneRow<IsLeft>(reinterpret_cast<const UInt8 *>(str_val.c_str()), str_val.size() + 1, static_cast<int32_t>(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;
Expand Down Expand Up @@ -2929,6 +2947,12 @@ class tidbPadImpl
vec_result_null_map[i] = tidbPadOneRow<IsLeft>(reinterpret_cast<const UInt8 *>(str_val.c_str()), str_val.size() + 1, static_cast<int32_t>(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;
}
Expand Down
30 changes: 30 additions & 0 deletions tests/fullstack-test/expr/cast_nullability.test
Original file line number Diff line number Diff line change
Expand Up @@ -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
+---+
Expand All @@ -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 |
+------------------------------------------+

0 comments on commit ef74a74

Please sign in to comment.