Skip to content

Commit a555f45

Browse files
[fix](array-type) fix the wrong result of array_join function (#13477)
this pr is used to fix the wrong result of array_join function. before the change, the array_join function will return wrong result. MySQL [example_db]> select array_join(["", "1", "2"], ''); +--------------------------------------+ | array_join(ARRAY('', '1', '2'), '') | +--------------------------------------+ | 1_2 | +--------------------------------------+ 3.after the change, the array_join function will return correct result. MySQL [example_db]> select array_join(["", "1", "2"], ''); +--------------------------------------+ | array_join(ARRAY('', '1', '2'), '') | +--------------------------------------+ | _1_2 | +--------------------------------------+ Issue Number: #7570
1 parent ddc08ee commit a555f45

3 files changed

Lines changed: 60 additions & 7 deletions

File tree

‎be/src/vec/functions/array/function_array_join.h‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,10 @@ struct ArrayJoinImpl {
106106
}
107107

108108
static void _fill_result_string(const std::string& input_str, const std::string& sep_str,
109-
std::string& result_str) {
110-
if (result_str.size() == 0) {
109+
std::string& result_str, bool& is_first_elem) {
110+
if (is_first_elem) {
111111
result_str.append(input_str);
112+
is_first_elem = false;
112113
} else {
113114
result_str.append(sep_str);
114115
result_str.append(input_str);
@@ -133,12 +134,13 @@ struct ArrayJoinImpl {
133134
size_t prev_src_offset = 0;
134135
for (auto curr_src_offset : src_offsets) {
135136
std::string result_str;
137+
bool is_first_elem = true;
136138
for (size_t j = prev_src_offset; j < curr_src_offset; ++j) {
137139
if (src_null_map && src_null_map[j]) {
138140
if (null_replace_str.size() == 0) {
139141
continue;
140142
} else {
141-
_fill_result_string(null_replace_str, sep_str, result_str);
143+
_fill_result_string(null_replace_str, sep_str, result_str, is_first_elem);
142144
continue;
143145
}
144146
}
@@ -147,10 +149,10 @@ struct ArrayJoinImpl {
147149
DecimalV2Value decimal_value =
148150
(DecimalV2Value)(src_data_concrete->get_data()[j]);
149151
std::string decimal_str = decimal_value.to_string();
150-
_fill_result_string(decimal_str, sep_str, result_str);
152+
_fill_result_string(decimal_str, sep_str, result_str, is_first_elem);
151153
} else {
152154
std::string elem_str = remove_nullable(nested_type)->to_string(src_column, j);
153-
_fill_result_string(elem_str, sep_str, result_str);
155+
_fill_result_string(elem_str, sep_str, result_str, is_first_elem);
154156
}
155157
}
156158

@@ -174,19 +176,20 @@ struct ArrayJoinImpl {
174176
size_t prev_src_offset = 0;
175177
for (auto curr_src_offset : src_offsets) {
176178
std::string result_str;
179+
bool is_first_elem = true;
177180
for (size_t j = prev_src_offset; j < curr_src_offset; ++j) {
178181
if (src_null_map && src_null_map[j]) {
179182
if (null_replace_str.size() == 0) {
180183
continue;
181184
} else {
182-
_fill_result_string(null_replace_str, sep_str, result_str);
185+
_fill_result_string(null_replace_str, sep_str, result_str, is_first_elem);
183186
continue;
184187
}
185188
}
186189

187190
StringRef src_str_ref = src_data_concrete->get_data_at(j);
188191
std::string elem_str(src_str_ref.data, src_str_ref.size);
189-
_fill_result_string(elem_str, sep_str, result_str);
192+
_fill_result_string(elem_str, sep_str, result_str, is_first_elem);
190193
}
191194

192195
dest_column_ptr->insert_data(result_str.c_str(), result_str.size());

‎regression-test/data/query_p0/sql_functions/array_functions/test_array_functions_by_literal.out‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,39 @@ false
254254
-- !sql --
255255
[0, 0]
256256

257+
-- !sql --
258+
1_2_3
259+
260+
-- !sql --
261+
1_2_3
262+
263+
-- !sql --
264+
1_2_3
265+
266+
-- !sql --
267+
_2_3
268+
269+
-- !sql --
270+
1_2_
271+
272+
-- !sql --
273+
1_2_
274+
275+
-- !sql --
276+
__3
277+
278+
-- !sql --
279+
1_2__
280+
281+
-- !sql --
282+
1_2__
283+
284+
-- !sql --
285+
any_any_1_2___any
286+
287+
-- !sql --
288+
289+
290+
-- !sql --
291+
_
292+

‎regression-test/suites/query_p0/sql_functions/array_functions/test_array_functions_by_literal.groovy‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,18 @@ suite("test_array_functions_by_literal") {
125125
qt_sql "select [1,2,3][0:]"
126126
qt_sql "select [1,2,3][-5:]"
127127
qt_sql "select [true, false, false][2:]"
128+
129+
// array_join function
130+
qt_sql "select array_join([1, 2, 3], '_')"
131+
qt_sql "select array_join(['1', '2', '3', null], '_')"
132+
qt_sql "select array_join([null, '1', '2', '3', null], '_')"
133+
qt_sql "select array_join(['', '2', '3'], '_')"
134+
qt_sql "select array_join(['1', '2', ''], '_')"
135+
qt_sql "select array_join(['1', '2', '', null], '_')"
136+
qt_sql "select array_join(['', '', '3'], '_')"
137+
qt_sql "select array_join(['1', '2', '', ''], '_')"
138+
qt_sql "select array_join([null, null, '1', '2', '', '', null], '_')"
139+
qt_sql "select array_join([null, null, 1, 2, '', '', null], '_', 'any')"
140+
qt_sql "select array_join([''], '_')"
141+
qt_sql "select array_join(['', ''], '_')"
128142
}

0 commit comments

Comments
 (0)