From e3073edd6f868afe999480f02f69976ca1d17417 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 20 Dec 2024 00:42:31 +0800 Subject: [PATCH] branch-3.0: [Opt](function) Speed up function string and add regression #45062 (#45107) Cherry-picked from #45062 Co-authored-by: zclllhhjj --- be/src/vec/functions/function_string.cpp | 19 ++++++++++--------- .../scalar_function/S.groovy | 1 + 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/be/src/vec/functions/function_string.cpp b/be/src/vec/functions/function_string.cpp index 1876ed499f4d9e..1f88c0f5bcf627 100644 --- a/be/src/vec/functions/function_string.cpp +++ b/be/src/vec/functions/function_string.cpp @@ -888,16 +888,17 @@ struct StringSpace { ColumnString::Offsets& res_offsets) { res_offsets.resize(data.size()); size_t input_size = res_offsets.size(); - std::vector> buffer; + // sample to get approximate best reserve size + if (input_size > 4) { + res_data.reserve(((data[0] + data[input_size >> 1] + data[input_size >> 2] + + data[input_size - 1]) >> + 2) * + input_size); + } for (size_t i = 0; i < input_size; ++i) { - buffer.clear(); - if (data[i] > 0) { - buffer.resize(data[i]); - for (size_t j = 0; j < data[i]; ++j) { - buffer[j] = ' '; - } - StringOP::push_value_string(std::string_view(buffer.data(), buffer.size()), i, - res_data, res_offsets); + if (data[i] > 0) [[likely]] { + res_data.resize_fill(res_data.size() + data[i], ' '); + res_offsets[i] = res_data.size(); } else { StringOP::push_empty_string(i, res_data, res_offsets); } diff --git a/regression-test/suites/nereids_function_p0/scalar_function/S.groovy b/regression-test/suites/nereids_function_p0/scalar_function/S.groovy index 98a8685a88840b..ea0d80d8adce85 100644 --- a/regression-test/suites/nereids_function_p0/scalar_function/S.groovy +++ b/regression-test/suites/nereids_function_p0/scalar_function/S.groovy @@ -126,6 +126,7 @@ suite("nereids_scalar_fn_S") { sql "select space(10) from fn_test order by kint" sql "select space(10) from fn_test_not_nullable order by kint" + sql """select k from (select length(space(number)) k from numbers("number" = "10"))t;""" // before #44919 will crash qt_sql_split_part_Varchar_Varchar_Integer "select split_part(kvchrs1, ' ', 1) from fn_test order by kvchrs1" qt_sql_split_part_Varchar_Varchar_Integer_notnull "select split_part(kvchrs1, ' ', 1) from fn_test_not_nullable order by kvchrs1" qt_sql_split_part_String_String_Integer "select split_part(kstr, ' ', 1) from fn_test order by kstr"