github-actions[bot] commented on code in PR #62359:
URL: https://github.com/apache/doris/pull/62359#discussion_r3077688822


##########
be/src/exprs/function/function_string_concat.h:
##########
@@ -695,117 +779,223 @@ class FunctionStringPad : public IFunction {
         res_offsets.resize(input_rows_count);
 
         const auto* strcol = assert_cast<const ColumnString*>(col[0].get());
-        const auto& strcol_offsets = strcol->get_offsets();
-        const auto& strcol_chars = strcol->get_chars();
-
         const auto* col_len = assert_cast<const ColumnInt32*>(col[1].get());
         const auto& col_len_data = col_len->get_data();
 
         const auto* padcol = assert_cast<const ColumnString*>(col[2].get());
-        const auto& padcol_offsets = padcol->get_offsets();
-        const auto& padcol_chars = padcol->get_chars();
-        std::visit(
-                [&](auto str_const, auto len_const, auto pad_const) {
-                    execute_utf8<str_const, len_const, pad_const>(
-                            strcol_offsets, strcol_chars, col_len_data, 
padcol_offsets,
-                            padcol_chars, res_offsets, res_chars, 
null_map_data, input_rows_count);
-                },
-                make_bool_variant(col_const[0]), 
make_bool_variant(col_const[1]),
-                make_bool_variant(col_const[2]));
+
+        if (col_const[1] && col_const[2]) {
+            auto pad = padcol->get_data_at(0);
+            const bool pad_all_ascii =
+                    simd::VStringFunctions::is_ascii({pad.data, 
static_cast<size_t>(pad.size)});
+            const bool all_ascii = pad_all_ascii && strcol->is_ascii();
+            std::visit(
+                    [&](auto str_const) {
+                        if (all_ascii) {
+                            execute_const_len_const_pad<true, str_const>(
+                                    *strcol, col_len_data, *padcol, 
res_offsets, res_chars,
+                                    null_map_data, input_rows_count);
+                        } else {
+                            execute_const_len_const_pad<false, str_const>(
+                                    *strcol, col_len_data, *padcol, 
res_offsets, res_chars,
+                                    null_map_data, input_rows_count);
+                        }
+                    },
+                    make_bool_variant(col_const[0]));
+        } else {
+            std::visit(
+                    [&](auto str_const) {
+                        execute_general<str_const>(*strcol, col_len_data, 
col_const[1], *padcol,
+                                                   col_const[2], res_offsets, 
res_chars,
+                                                   null_map_data, 
input_rows_count);
+                    },
+                    make_bool_variant(col_const[0]));
+        }
 
         block.get_by_position(result).column =
                 ColumnNullable::create(std::move(res), std::move(null_map));
         return Status::OK();
     }
 
-    template <bool str_const, bool len_const, bool pad_const>
-    void execute_utf8(const ColumnString::Offsets& strcol_offsets,
-                      const ColumnString::Chars& strcol_chars,
-                      const ColumnInt32::Container& col_len_data,
-                      const ColumnString::Offsets& padcol_offsets,
-                      const ColumnString::Chars& padcol_chars, 
ColumnString::Offsets& res_offsets,
-                      ColumnString::Chars& res_chars, ColumnUInt8::Container& 
null_map_data,
-                      size_t input_rows_count) const {
-        std::vector<size_t> pad_index;
-        size_t const_pad_char_size = 0;
-        // If pad_const = true, initialize pad_index only once.
-        // The same logic applies to the if constexpr (!pad_const) condition 
below.
-        if constexpr (pad_const) {
-            const_pad_char_size = simd::VStringFunctions::get_char_len(
-                    (const char*)padcol_chars.data(), padcol_offsets[0], 
pad_index);
+private:
+    template <bool is_utf8>
+    static size_t get_char_length(const uint8_t* str_data, size_t 
str_byte_len) {
+        if constexpr (is_utf8) {
+            return simd::VStringFunctions::get_char_len(reinterpret_cast<const 
char*>(str_data),
+                                                        str_byte_len);
         }
+        return str_byte_len;
+    }
 
-        fmt::memory_buffer buffer;
-        buffer.resize(strcol_chars.size());
-        size_t buffer_len = 0;
+    template <bool is_utf8>
+    static size_t get_truncated_byte_length(const uint8_t* str_data, size_t 
str_byte_len,
+                                            size_t str_char_len, size_t 
target_len) {
+        if constexpr (!is_utf8) {
+            return target_len;
+        }
+        if (str_char_len == target_len) {
+            return str_byte_len;
+        }
+        auto [byte_len, _] = 
simd::VStringFunctions::iterate_utf8_with_limit_length(
+                reinterpret_cast<const char*>(str_data),
+                reinterpret_cast<const char*>(str_data) + str_byte_len, 
target_len);
+        return byte_len;
+    }
 
-        for (size_t i = 0; i < input_rows_count; ++i) {
-            if constexpr (!pad_const) {
-                pad_index.clear();
-            }
-            const auto len = col_len_data[index_check_const<len_const>(i)];
-            if (len < 0) {
-                // return NULL when input length is invalid number
-                null_map_data[i] = true;
-                res_offsets[i] = buffer_len;
-            } else {
-                const auto str_idx = index_check_const<str_const>(i);
-                const int str_len = strcol_offsets[str_idx] - 
strcol_offsets[str_idx - 1];
-                const auto* str_data = &strcol_chars[strcol_offsets[str_idx - 
1]];
-                const auto pad_idx = index_check_const<pad_const>(i);
-                const int pad_len = padcol_offsets[pad_idx] - 
padcol_offsets[pad_idx - 1];
-                const auto* pad_data = &padcol_chars[padcol_offsets[pad_idx - 
1]];
-
-                auto [iterate_byte_len, iterate_char_len] =
-                        simd::VStringFunctions::iterate_utf8_with_limit_length(
-                                (const char*)str_data, (const char*)str_data + 
str_len, len);
-                // If iterate_char_len equals len, it indicates that the str 
length is greater than or equal to len
-                if (iterate_char_len == len) {
-                    buffer.resize(buffer_len + iterate_byte_len);
-                    memcpy(buffer.data() + buffer_len, str_data, 
iterate_byte_len);
-                    buffer_len += iterate_byte_len;
-                    res_offsets[i] = buffer_len;
-                    continue;
-                }
-                size_t pad_char_size;
-                if constexpr (!pad_const) {
-                    pad_char_size = 
simd::VStringFunctions::get_char_len((const char*)pad_data,
-                                                                         
pad_len, pad_index);
-                } else {
-                    pad_char_size = const_pad_char_size;
-                }
+    static void ensure_capacity(ColumnString::Chars& res_chars, size_t needed, 
size_t row) {
+        if (needed <= res_chars.size()) {
+            return;
+        }
+        ColumnString::check_chars_length(needed, row);
+        res_chars.resize(std::max(needed, res_chars.size() * 3 / 2));
+    }
 
-                // make compatible with mysql. return empty string if pad is 
empty
-                if (pad_char_size == 0) {
-                    res_offsets[i] = buffer_len;
-                    continue;
-                }
-                const size_t str_char_size = iterate_char_len;
-                const size_t pad_times = (len - str_char_size) / pad_char_size;
-                const size_t pad_remainder_len = pad_index[(len - 
str_char_size) % pad_char_size];
-                const size_t new_capacity = str_len + size_t(pad_times + 1) * 
pad_len;
-                ColumnString::check_chars_length(buffer_len + new_capacity, i);
-                buffer.resize(buffer_len + new_capacity);
-                if constexpr (!Impl::is_lpad) {
-                    memcpy(buffer.data() + buffer_len, str_data, str_len);
-                    buffer_len += str_len;
-                }
-                // Prepend chars of pad.
-                StringOP::fast_repeat((uint8_t*)buffer.data() + buffer_len, 
pad_data, pad_len,
-                                      pad_times);
-                buffer_len += pad_times * pad_len;
+    template <bool is_utf8>
+    static size_t estimate_const_output_bytes(const ColumnString::Chars& 
strcol_chars,
+                                              int target_len, size_t 
input_rows_count,
+                                              const PaddingChars<is_utf8>* 
padding) {
+        if (target_len <= 0) {
+            return 0;
+        }
+        if constexpr (!is_utf8) {
+            return static_cast<size_t>(target_len) * input_rows_count;
+        }
+        if (padding != nullptr && padding->num_chars() > 0) {
+            size_t pad_bytes_per_char =
+                    (padding->pad_string.size() + padding->num_chars() - 1) / 
padding->num_chars();
+            return strcol_chars.size() +
+                   static_cast<size_t>(target_len) * pad_bytes_per_char * 
input_rows_count;
+        }
+        return strcol_chars.size();
+    }
+
+    template <bool is_utf8>
+    static void append_result_row(const uint8_t* str_data, size_t 
str_byte_len, int target_len,
+                                  const PaddingChars<is_utf8>* padding,
+                                  ColumnString::Chars& res_chars,
+                                  ColumnString::Offsets& res_offsets,
+                                  ColumnUInt8::Container& null_map_data, 
size_t row,
+                                  size_t& dst_offset) {
+        if (target_len < 0) {
+            null_map_data[row] = true;
+            res_offsets[row] = dst_offset;
+            return;
+        }
+
+        const size_t str_char_len = get_char_length<is_utf8>(str_data, 
str_byte_len);
+        const size_t target_char_len = static_cast<size_t>(target_len);

Review Comment:
   This changes `lpad`/`rpad` semantics for non-UTF8 byte strings. 
`simd::VStringFunctions::get_char_len()` counts only bytes `> 0xBF`, while the 
previous implementation decided whether the source already reached `len` via 
`iterate_utf8_with_limit_length()` (`vstring_function.h:236`), which advances 
by `UTF8_BYTE_LENGTH[*p]` and therefore treats continuation bytes `0x80..0xBF` 
as one character.
   
   A concrete regression is a one-byte string like `unhex('80')` with target 
length `1`: before this refactor the function kept the original byte, but now 
`str_char_len` becomes `0`, so we pad and return two bytes. Doris string 
columns can contain arbitrary byte sequences, so this is a behavior break. 
Please keep the source-string length/truncation logic consistent with the old 
`iterate_utf8_with_limit_length()` behavior (or make an explicit 
validated-UTF8-only change with tests if that is intentional).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to