diff --git a/be/src/vec/functions/function_string.cpp b/be/src/vec/functions/function_string.cpp index f98589b1965ccc..9f37d9d8d05d09 100644 --- a/be/src/vec/functions/function_string.cpp +++ b/be/src/vec/functions/function_string.cpp @@ -740,16 +740,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"