Coverage Report

Created: 2026-04-13 22:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/function/array/function_array_join.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
#pragma once
18
19
#include "core/block/block.h"
20
#include "core/column/column_array.h"
21
#include "core/column/column_const.h"
22
#include "core/column/column_execute_util.h"
23
#include "core/data_type/data_type_array.h"
24
#include "core/data_type/data_type_string.h"
25
#include "core/string_ref.h"
26
#include "exprs/function/array/function_array_utils.h"
27
28
namespace doris {
29
30
struct NameArrayJoin {
31
    static constexpr auto name = "array_join";
32
};
33
34
struct ArrayJoinImpl {
35
public:
36
    using column_type = ColumnArray;
37
    using NullMapType = PaddedPODArray<UInt8>;
38
39
1
    static bool _is_variadic() { return true; }
40
41
0
    static size_t _get_number_of_arguments() { return 0; }
42
43
0
    static DataTypePtr get_return_type(const DataTypes& arguments) {
44
0
        DCHECK(arguments[0]->get_primitive_type() == TYPE_ARRAY)
45
0
                << "first argument for function: array_join should be DataTypeArray"
46
0
                << " and arguments[0] is " << arguments[0]->get_name();
47
0
        DCHECK(is_string_type(arguments[1]->get_primitive_type()))
48
0
                << "second argument for function: array_join should be DataTypeString"
49
0
                << ", and arguments[1] is " << arguments[1]->get_name();
50
0
        if (arguments.size() > 2) {
51
0
            DCHECK(is_string_type(arguments[2]->get_primitive_type()))
52
0
                    << "third argument for function: array_join should be DataTypeString"
53
0
                    << ", and arguments[2] is " << arguments[2]->get_name();
54
0
        }
55
56
0
        return std::make_shared<DataTypeString>();
57
0
    }
58
59
    static Status execute(Block& block, const ColumnNumbers& arguments, uint32_t result,
60
0
                          const DataTypeArray* data_type_array, const ColumnArray& array) {
61
0
        ColumnPtr src_column =
62
0
                block.get_by_position(arguments[0]).column->convert_to_full_column_if_const();
63
0
        ColumnArrayExecutionData src;
64
0
        if (!extract_column_array_info(*src_column, src)) {
65
0
            return Status::RuntimeError(fmt::format(
66
0
                    "execute failed, unsupported types for function {}({})", "array_join",
67
0
                    block.get_by_position(arguments[0]).type->get_name()));
68
0
        }
69
70
0
        auto nested_type = data_type_array->get_nested_type();
71
0
        auto dest_column_ptr = ColumnString::create();
72
73
0
        auto& dest_chars = dest_column_ptr->get_chars();
74
0
        auto& dest_offsets = dest_column_ptr->get_offsets();
75
76
0
        dest_offsets.resize_fill(src_column->size(), 0);
77
78
0
        auto sep_column =
79
0
                ColumnView<TYPE_STRING>::create(block.get_by_position(arguments[1]).column);
80
81
0
        if (arguments.size() > 2) {
82
0
            auto null_replace_column =
83
0
                    ColumnView<TYPE_STRING>::create(block.get_by_position(arguments[2]).column);
84
85
0
            _execute_string(*src.nested_col, *src.offsets_ptr, src.nested_nullmap_data, sep_column,
86
0
                            null_replace_column, dest_chars, dest_offsets);
87
88
0
        } else {
89
0
            auto tmp_column_string = ColumnString::create();
90
            // insert default value for null replacement, which is empty string
91
0
            tmp_column_string->insert_default();
92
0
            ColumnPtr tmp_const_column =
93
0
                    ColumnConst::create(std::move(tmp_column_string), sep_column.size());
94
95
0
            auto null_replace_column = ColumnView<TYPE_STRING>::create(tmp_const_column);
96
97
0
            _execute_string(*src.nested_col, *src.offsets_ptr, src.nested_nullmap_data, sep_column,
98
0
                            null_replace_column, dest_chars, dest_offsets);
99
0
        }
100
101
0
        block.replace_by_position(result, std::move(dest_column_ptr));
102
0
        return Status::OK();
103
0
    }
104
105
private:
106
    // same as ColumnString::insert_data
107
    static void insert_to_chars(int64_t i, ColumnString::Chars& chars, uint32_t& total_size,
108
0
                                const char* pos, size_t length) {
109
0
        const size_t old_size = chars.size();
110
0
        const size_t new_size = old_size + length;
111
112
0
        if (length) {
113
0
            ColumnString::check_chars_length(new_size, i);
114
0
            chars.resize(new_size);
115
0
            memcpy(chars.data() + old_size, pos, length);
116
0
            total_size += length;
117
0
        }
118
0
    }
119
120
    static void _fill_result_string(int64_t i, const StringRef& input_str, const StringRef& sep_str,
121
                                    ColumnString::Chars& dest_chars, uint32_t& total_size,
122
0
                                    bool& is_first_elem) {
123
0
        if (is_first_elem) {
124
0
            insert_to_chars(i, dest_chars, total_size, input_str.data, input_str.size);
125
0
            is_first_elem = false;
126
0
        } else {
127
0
            insert_to_chars(i, dest_chars, total_size, sep_str.data, sep_str.size);
128
0
            insert_to_chars(i, dest_chars, total_size, input_str.data, input_str.size);
129
0
        }
130
0
    }
131
132
    static void _execute_string(const IColumn& src_column,
133
                                const ColumnArray::Offsets64& src_offsets,
134
                                const UInt8* src_null_map, ColumnView<TYPE_STRING>& sep_column,
135
                                ColumnView<TYPE_STRING>& null_replace_column,
136
                                ColumnString::Chars& dest_chars,
137
0
                                ColumnString::Offsets& dest_offsets) {
138
0
        const auto& src_data = assert_cast<const ColumnString&>(src_column);
139
140
0
        uint32_t total_size = 0;
141
142
0
        for (int64_t i = 0; i < src_offsets.size(); ++i) {
143
0
            auto begin = src_offsets[i - 1];
144
0
            auto end = src_offsets[i];
145
146
0
            auto sep_str = sep_column.value_at(i);
147
0
            auto null_replace_str = null_replace_column.value_at(i);
148
149
0
            bool is_first_elem = true;
150
151
0
            for (size_t j = begin; j < end; ++j) {
152
0
                if (src_null_map && src_null_map[j]) {
153
0
                    if (null_replace_str.size != 0) {
154
0
                        _fill_result_string(i, null_replace_str, sep_str, dest_chars, total_size,
155
0
                                            is_first_elem);
156
0
                    }
157
0
                    continue;
158
0
                }
159
160
0
                StringRef src_str_ref = src_data.get_data_at(j);
161
0
                _fill_result_string(i, src_str_ref, sep_str, dest_chars, total_size, is_first_elem);
162
0
            }
163
164
0
            dest_offsets[i] = total_size;
165
0
        }
166
0
    }
167
};
168
169
} // namespace doris