Skip to content

Commit

Permalink
[Bug]fix pad wrong answer (apache#140)
Browse files Browse the repository at this point in the history
* fix pad wrong answer
* add commit to explain pad logic
  • Loading branch information
BiteTheDDDDt authored and HappenLee committed Sep 7, 2021
1 parent c7fe1e3 commit b0f8613
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions be/src/vec/functions/function_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,8 @@ class FunctionStringPad : public IFunction {
pad_index.clear();
buffer.clear();
if (null_map_data[i] || col_len_data[i] < 0) {
// return NULL when input string is NULL or input length is invalid number
null_map_data[i] = true;
StringOP::push_empty_string(i, res_chars, res_offsets);
} else {
int str_len = strcol_offsets[i] - strcol_offsets[i - 1] - 1;
Expand All @@ -632,6 +634,25 @@ class FunctionStringPad : public IFunction {
size_t pad_char_size =
get_char_len(std::string_view(pad_data, pad_len), &pad_index);

if (col_len_data[i] <= str_char_size) {
// truncate the input string
if (col_len_data[i] < str_char_size) {
buffer.append(str_data, str_data + str_index[col_len_data[i]]);
} else {
buffer.append(str_data, str_data + str_len);
}

StringOP::push_value_string(std::string_view(buffer.data(), buffer.size()), i,
res_chars, res_offsets);
continue;
}
if (pad_char_size == 0) {
// return NULL when the string to be paded is missing
null_map_data[i] = true;
StringOP::push_empty_string(i, res_chars, res_offsets);
continue;
}

int32_t pad_byte_len = 0;
int32_t pad_times = (col_len_data[i] - str_char_size) / pad_char_size;
int32_t pad_remainder = (col_len_data[i] - str_char_size) % pad_char_size;
Expand Down

0 comments on commit b0f8613

Please sign in to comment.