Skip to content

Commit

Permalink
fix lpad/rpad trailing zero miss (#9610) (#9635)
Browse files Browse the repository at this point in the history
close #9465

Signed-off-by: guo-shaoge <[email protected]>

Co-authored-by: guo-shaoge <[email protected]>
  • Loading branch information
ti-chi-bot and guo-shaoge authored Nov 20, 2024
1 parent e9c2e2b commit d183ff0
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
35 changes: 15 additions & 20 deletions dbms/src/Functions/FunctionsString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2951,6 +2951,13 @@ class FunctionTiDBTrim : public IFunction

class TidbPadImpl
{
static void addTrailingZero(ColumnString::Chars_t & res, ColumnString::Offset & res_offset)
{
res.resize(res.size() + 1);
res[res_offset] = '\0';
++res_offset;
}

public:
template <typename IntType, bool IsUTF8, bool IsLeft>
static void tidbExecutePadImpl(Block & block, const ColumnNumbers & arguments, const size_t result, const String & func_name)
Expand Down Expand Up @@ -3130,9 +3137,7 @@ class TidbPadImpl
}
else
{
result_data.resize(result_data.size() + 1);
result_data[res_prev_offset] = '\0';
res_prev_offset++;
addTrailingZero(result_data, res_prev_offset);
}

string_prev_offset = string_offsets[i];
Expand Down Expand Up @@ -3167,9 +3172,7 @@ class TidbPadImpl
}
else
{
result_data.resize(result_data.size() + 1);
result_data[res_prev_offset] = '\0';
res_prev_offset++;
addTrailingZero(result_data, res_prev_offset);
}

string_prev_offset = string_offsets[i];
Expand Down Expand Up @@ -3203,9 +3206,7 @@ class TidbPadImpl
}
else
{
result_data.resize(result_data.size() + 1);
result_data[res_prev_offset] = '\0';
res_prev_offset++;
addTrailingZero(result_data, res_prev_offset);
}

padding_prev_offset = (*padding_offsets)[i];
Expand Down Expand Up @@ -3238,9 +3239,7 @@ class TidbPadImpl
}
else
{
result_data.resize(result_data.size() + 1);
result_data[res_prev_offset] = '\0';
res_prev_offset++;
addTrailingZero(result_data, res_prev_offset);
}

result_offsets[i] = res_prev_offset;
Expand All @@ -3258,6 +3257,7 @@ class TidbPadImpl

if (target_len < 0 || (data_len < static_cast<ColumnString::Offset>(target_len) && pad_len == 0))
{
addTrailingZero(res, res_offset);
return true;
}

Expand Down Expand Up @@ -3309,10 +3309,7 @@ class TidbPadImpl
++left;
}
}
// Add trailing zero.
res.resize(res.size() + 1);
res[res_offset] = '\0';
res_offset++;
addTrailingZero(res, res_offset);
return false;
}

Expand All @@ -3325,6 +3322,7 @@ class TidbPadImpl

if (target_len < 0 || (data_len < static_cast<ColumnString::Offset>(target_len) && pad_len == 0))
{
addTrailingZero(res, res_offset);
return true;
}

Expand Down Expand Up @@ -3377,10 +3375,7 @@ class TidbPadImpl
copyResult(res, res_offset, data, 0, tmp_target_len);
res_offset += tmp_target_len;
}
// Add trailing zero.
res.resize(res.size() + 1);
res[res_offset] = '\0';
res_offset++;
addTrailingZero(res, res_offset);
return false;
}

Expand Down
24 changes: 24 additions & 0 deletions tests/fullstack-test/expr/pad.test
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,27 @@ mysql> set tidb_isolation_read_engines='tiflash'; set tidb_enforce_mpp=1; select

mysql> set tidb_isolation_read_engines='tiflash'; set tidb_enforce_mpp=1; SELECT max(lpad('y',0,c1)) FROM test.t2
max(lpad('y',0,c1))


mysql> drop table if exists test.t1
mysql> create table test.t1(c1 varchar(100), c2 int)
mysql> alter table test.t1 set tiflash replica 1
mysql> insert into test.t1 values('a', -1)
func> wait_table test t1

# crc32 will call ColumnString::getDataAt(i), which assume all string rows ends with '\0'.
mysql> set tidb_isolation_read_engines='tiflash'; set tidb_enforce_mpp=1; select crc32(lpad(c1, c2, 'b')) from test.t1
+--------------------------+
| crc32(lpad(c1, c2, 'b')) |
+--------------------------+
| NULL |
+--------------------------+

mysql> set tidb_isolation_read_engines='tiflash'; set tidb_enforce_mpp=1; select crc32(rpad(c1, c2, 'b')) from test.t1
+--------------------------+
| crc32(rpad(c1, c2, 'b')) |
+--------------------------+
| NULL |
+--------------------------+

mysql> drop table if exists test.t1

0 comments on commit d183ff0

Please sign in to comment.