Skip to content

Commit 7de7609

Browse files
ti-chi-botguo-shaogeti-chi-bot[bot]
authored
fix lpad/rpad trailing zero miss (#9610) (#9633)
close #9465 Signed-off-by: guo-shaoge <shaoge1994@163.com> Co-authored-by: guo-shaoge <shaoge1994@163.com> Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
1 parent 7bee6e8 commit 7de7609

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

dbms/src/Functions/FunctionsString.cpp

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3146,6 +3146,13 @@ class FunctionTiDBTrim : public IFunction
31463146

31473147
class TidbPadImpl
31483148
{
3149+
static void addTrailingZero(ColumnString::Chars_t & res, ColumnString::Offset & res_offset)
3150+
{
3151+
res.resize(res.size() + 1);
3152+
res[res_offset] = '\0';
3153+
++res_offset;
3154+
}
3155+
31493156
public:
31503157
template <typename IntType, bool IsUTF8, bool IsLeft>
31513158
static void tidbExecutePadImpl(
@@ -3435,9 +3442,7 @@ class TidbPadImpl
34353442
}
34363443
else
34373444
{
3438-
result_data.resize(result_data.size() + 1);
3439-
result_data[res_prev_offset] = '\0';
3440-
res_prev_offset++;
3445+
addTrailingZero(result_data, res_prev_offset);
34413446
}
34423447

34433448
string_prev_offset = string_offsets[i];
@@ -3496,9 +3501,7 @@ class TidbPadImpl
34963501
}
34973502
else
34983503
{
3499-
result_data.resize(result_data.size() + 1);
3500-
result_data[res_prev_offset] = '\0';
3501-
res_prev_offset++;
3504+
addTrailingZero(result_data, res_prev_offset);
35023505
}
35033506

35043507
string_prev_offset = string_offsets[i];
@@ -3555,9 +3558,7 @@ class TidbPadImpl
35553558
}
35563559
else
35573560
{
3558-
result_data.resize(result_data.size() + 1);
3559-
result_data[res_prev_offset] = '\0';
3560-
res_prev_offset++;
3561+
addTrailingZero(result_data, res_prev_offset);
35613562
}
35623563

35633564
padding_prev_offset = (*padding_offsets)[i];
@@ -3613,9 +3614,7 @@ class TidbPadImpl
36133614
}
36143615
else
36153616
{
3616-
result_data.resize(result_data.size() + 1);
3617-
result_data[res_prev_offset] = '\0';
3618-
res_prev_offset++;
3617+
addTrailingZero(result_data, res_prev_offset);
36193618
}
36203619

36213620
result_offsets[i] = res_prev_offset;
@@ -3640,6 +3639,7 @@ class TidbPadImpl
36403639

36413640
if (target_len < 0 || (data_len < static_cast<ColumnString::Offset>(target_len) && pad_len == 0))
36423641
{
3642+
addTrailingZero(res, res_offset);
36433643
return true;
36443644
}
36453645

@@ -3691,10 +3691,7 @@ class TidbPadImpl
36913691
++left;
36923692
}
36933693
}
3694-
// Add trailing zero.
3695-
res.resize(res.size() + 1);
3696-
res[res_offset] = '\0';
3697-
res_offset++;
3694+
addTrailingZero(res, res_offset);
36983695
return false;
36993696
}
37003697

@@ -3714,6 +3711,7 @@ class TidbPadImpl
37143711

37153712
if (target_len < 0 || (data_len < static_cast<ColumnString::Offset>(target_len) && pad_len == 0))
37163713
{
3714+
addTrailingZero(res, res_offset);
37173715
return true;
37183716
}
37193717

@@ -3766,10 +3764,7 @@ class TidbPadImpl
37663764
copyResult(res, res_offset, data, 0, tmp_target_len);
37673765
res_offset += tmp_target_len;
37683766
}
3769-
// Add trailing zero.
3770-
res.resize(res.size() + 1);
3771-
res[res_offset] = '\0';
3772-
res_offset++;
3767+
addTrailingZero(res, res_offset);
37733768
return false;
37743769
}
37753770

tests/fullstack-test/expr/pad.test

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,27 @@ mysql> set tidb_isolation_read_engines='tiflash'; set tidb_enforce_mpp=1; select
111111

112112
mysql> set tidb_isolation_read_engines='tiflash'; set tidb_enforce_mpp=1; SELECT max(lpad('y',0,c1)) FROM test.t2
113113
max(lpad('y',0,c1))
114+
115+
116+
mysql> drop table if exists test.t1
117+
mysql> create table test.t1(c1 varchar(100), c2 int)
118+
mysql> alter table test.t1 set tiflash replica 1
119+
mysql> insert into test.t1 values('a', -1)
120+
func> wait_table test t1
121+
122+
# crc32 will call ColumnString::getDataAt(i), which assume all string rows ends with '\0'.
123+
mysql> set tidb_isolation_read_engines='tiflash'; set tidb_enforce_mpp=1; select crc32(lpad(c1, c2, 'b')) from test.t1
124+
+--------------------------+
125+
| crc32(lpad(c1, c2, 'b')) |
126+
+--------------------------+
127+
| NULL |
128+
+--------------------------+
129+
130+
mysql> set tidb_isolation_read_engines='tiflash'; set tidb_enforce_mpp=1; select crc32(rpad(c1, c2, 'b')) from test.t1
131+
+--------------------------+
132+
| crc32(rpad(c1, c2, 'b')) |
133+
+--------------------------+
134+
| NULL |
135+
+--------------------------+
136+
137+
mysql> drop table if exists test.t1

0 commit comments

Comments
 (0)