diff --git a/be/src/vec/functions/array/function_array_join.h b/be/src/vec/functions/array/function_array_join.h index 180e65c21f..6524507490 100644 --- a/be/src/vec/functions/array/function_array_join.h +++ b/be/src/vec/functions/array/function_array_join.h @@ -106,9 +106,10 @@ private: } static void _fill_result_string(const std::string& input_str, const std::string& sep_str, - std::string& result_str) { - if (result_str.size() == 0) { + std::string& result_str, bool& is_first_elem) { + if (is_first_elem) { result_str.append(input_str); + is_first_elem = false; } else { result_str.append(sep_str); result_str.append(input_str); @@ -133,12 +134,13 @@ private: size_t prev_src_offset = 0; for (auto curr_src_offset : src_offsets) { std::string result_str; + bool is_first_elem = true; for (size_t j = prev_src_offset; j < curr_src_offset; ++j) { if (src_null_map && src_null_map[j]) { if (null_replace_str.size() == 0) { continue; } else { - _fill_result_string(null_replace_str, sep_str, result_str); + _fill_result_string(null_replace_str, sep_str, result_str, is_first_elem); continue; } } @@ -147,10 +149,10 @@ private: DecimalV2Value decimal_value = (DecimalV2Value)(src_data_concrete->get_data()[j]); std::string decimal_str = decimal_value.to_string(); - _fill_result_string(decimal_str, sep_str, result_str); + _fill_result_string(decimal_str, sep_str, result_str, is_first_elem); } else { std::string elem_str = remove_nullable(nested_type)->to_string(src_column, j); - _fill_result_string(elem_str, sep_str, result_str); + _fill_result_string(elem_str, sep_str, result_str, is_first_elem); } } @@ -174,19 +176,20 @@ private: size_t prev_src_offset = 0; for (auto curr_src_offset : src_offsets) { std::string result_str; + bool is_first_elem = true; for (size_t j = prev_src_offset; j < curr_src_offset; ++j) { if (src_null_map && src_null_map[j]) { if (null_replace_str.size() == 0) { continue; } else { - _fill_result_string(null_replace_str, sep_str, result_str); + _fill_result_string(null_replace_str, sep_str, result_str, is_first_elem); continue; } } StringRef src_str_ref = src_data_concrete->get_data_at(j); std::string elem_str(src_str_ref.data, src_str_ref.size); - _fill_result_string(elem_str, sep_str, result_str); + _fill_result_string(elem_str, sep_str, result_str, is_first_elem); } dest_column_ptr->insert_data(result_str.c_str(), result_str.size()); diff --git a/regression-test/data/query_p0/sql_functions/array_functions/test_array_functions_by_literal.out b/regression-test/data/query_p0/sql_functions/array_functions/test_array_functions_by_literal.out index 0845e2a397..64b7587d61 100644 --- a/regression-test/data/query_p0/sql_functions/array_functions/test_array_functions_by_literal.out +++ b/regression-test/data/query_p0/sql_functions/array_functions/test_array_functions_by_literal.out @@ -254,3 +254,39 @@ false -- !sql -- [0, 0] +-- !sql -- +1_2_3 + +-- !sql -- +1_2_3 + +-- !sql -- +1_2_3 + +-- !sql -- +_2_3 + +-- !sql -- +1_2_ + +-- !sql -- +1_2_ + +-- !sql -- +__3 + +-- !sql -- +1_2__ + +-- !sql -- +1_2__ + +-- !sql -- +any_any_1_2___any + +-- !sql -- + + +-- !sql -- +_ + diff --git a/regression-test/suites/query_p0/sql_functions/array_functions/test_array_functions_by_literal.groovy b/regression-test/suites/query_p0/sql_functions/array_functions/test_array_functions_by_literal.groovy index 9b08dd6315..5867d218e1 100644 --- a/regression-test/suites/query_p0/sql_functions/array_functions/test_array_functions_by_literal.groovy +++ b/regression-test/suites/query_p0/sql_functions/array_functions/test_array_functions_by_literal.groovy @@ -125,4 +125,18 @@ suite("test_array_functions_by_literal") { qt_sql "select [1,2,3][0:]" qt_sql "select [1,2,3][-5:]" qt_sql "select [true, false, false][2:]" + + // array_join function + qt_sql "select array_join([1, 2, 3], '_')" + qt_sql "select array_join(['1', '2', '3', null], '_')" + qt_sql "select array_join([null, '1', '2', '3', null], '_')" + qt_sql "select array_join(['', '2', '3'], '_')" + qt_sql "select array_join(['1', '2', ''], '_')" + qt_sql "select array_join(['1', '2', '', null], '_')" + qt_sql "select array_join(['', '', '3'], '_')" + qt_sql "select array_join(['1', '2', '', ''], '_')" + qt_sql "select array_join([null, null, '1', '2', '', '', null], '_')" + qt_sql "select array_join([null, null, 1, 2, '', '', null], '_', 'any')" + qt_sql "select array_join([''], '_')" + qt_sql "select array_join(['', ''], '_')" }