Skip to content

Commit b0f8613

Browse files
BiteTheDDDDtHappenLee
authored andcommitted
[Bug]fix pad wrong answer (apache#140)
* fix pad wrong answer * add commit to explain pad logic
1 parent c7fe1e3 commit b0f8613

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

‎be/src/vec/functions/function_string.h‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ class FunctionStringPad : public IFunction {
617617
pad_index.clear();
618618
buffer.clear();
619619
if (null_map_data[i] || col_len_data[i] < 0) {
620+
// return NULL when input string is NULL or input length is invalid number
621+
null_map_data[i] = true;
620622
StringOP::push_empty_string(i, res_chars, res_offsets);
621623
} else {
622624
int str_len = strcol_offsets[i] - strcol_offsets[i - 1] - 1;
@@ -632,6 +634,25 @@ class FunctionStringPad : public IFunction {
632634
size_t pad_char_size =
633635
get_char_len(std::string_view(pad_data, pad_len), &pad_index);
634636

637+
if (col_len_data[i] <= str_char_size) {
638+
// truncate the input string
639+
if (col_len_data[i] < str_char_size) {
640+
buffer.append(str_data, str_data + str_index[col_len_data[i]]);
641+
} else {
642+
buffer.append(str_data, str_data + str_len);
643+
}
644+
645+
StringOP::push_value_string(std::string_view(buffer.data(), buffer.size()), i,
646+
res_chars, res_offsets);
647+
continue;
648+
}
649+
if (pad_char_size == 0) {
650+
// return NULL when the string to be paded is missing
651+
null_map_data[i] = true;
652+
StringOP::push_empty_string(i, res_chars, res_offsets);
653+
continue;
654+
}
655+
635656
int32_t pad_byte_len = 0;
636657
int32_t pad_times = (col_len_data[i] - str_char_size) / pad_char_size;
637658
int32_t pad_remainder = (col_len_data[i] - str_char_size) % pad_char_size;

0 commit comments

Comments
 (0)