Coverage Report

Created: 2026-04-09 19:46

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_array_view.h"
22
#include "core/column/column_const.h"
23
#include "core/column/column_execute_util.h"
24
#include "core/data_type/data_type_array.h"
25
#include "core/data_type/data_type_string.h"
26
#include "core/string_ref.h"
27
28
namespace doris {
29
30
#include "common/compile_check_begin.h"
31
struct NameArrayJoin {
32
    static constexpr auto name = "array_join";
33
};
34
35
struct ArrayJoinImpl {
36
public:
37
    using column_type = ColumnArray;
38
    using NullMapType = PaddedPODArray<UInt8>;
39
40
1
    static bool _is_variadic() { return true; }
41
42
0
    static size_t _get_number_of_arguments() { return 0; }
43
44
0
    static DataTypePtr get_return_type(const DataTypes& arguments) {
45
0
        DCHECK(arguments[0]->get_primitive_type() == TYPE_ARRAY)
46
0
                << "first argument for function: array_join should be DataTypeArray"
47
0
                << " and arguments[0] is " << arguments[0]->get_name();
48
0
        DCHECK(is_string_type(arguments[1]->get_primitive_type()))
49
0
                << "second argument for function: array_join should be DataTypeString"
50
0
                << ", and arguments[1] is " << arguments[1]->get_name();
51
0
        if (arguments.size() > 2) {
52
0
            DCHECK(is_string_type(arguments[2]->get_primitive_type()))
53
0
                    << "third argument for function: array_join should be DataTypeString"
54
0
                    << ", and arguments[2] is " << arguments[2]->get_name();
55
0
        }
56
57
0
        return std::make_shared<DataTypeString>();
58
0
    }
59
60
    static Status execute(Block& block, const ColumnNumbers& arguments, uint32_t result,
61
0
                          const DataTypeArray* data_type_array, const ColumnArray& array) {
62
0
        ColumnPtr src_column = block.get_by_position(arguments[0]).column;
63
0
        auto array_view = ColumnArrayView<TYPE_STRING>::create(src_column);
64
65
0
        auto dest_column_ptr = ColumnString::create();
66
67
0
        auto& dest_chars = dest_column_ptr->get_chars();
68
0
        auto& dest_offsets = dest_column_ptr->get_offsets();
69
70
0
        dest_offsets.resize_fill(array_view.size(), 0);
71
72
0
        auto sep_column =
73
0
                ColumnView<TYPE_STRING>::create(block.get_by_position(arguments[1]).column);
74
75
0
        if (arguments.size() > 2) {
76
0
            auto null_replace_column =
77
0
                    ColumnView<TYPE_STRING>::create(block.get_by_position(arguments[2]).column);
78
79
0
            _execute_string(array_view, sep_column, null_replace_column, dest_chars, dest_offsets);
80
81
0
        } else {
82
0
            auto tmp_column_string = ColumnString::create();
83
            // insert default value for null replacement, which is empty string
84
0
            tmp_column_string->insert_default();
85
0
            ColumnPtr tmp_const_column =
86
0
                    ColumnConst::create(std::move(tmp_column_string), sep_column.size());
87
88
0
            auto null_replace_column = ColumnView<TYPE_STRING>::create(tmp_const_column);
89
90
0
            _execute_string(array_view, sep_column, null_replace_column, dest_chars, dest_offsets);
91
0
        }
92
93
0
        block.replace_by_position(result, std::move(dest_column_ptr));
94
0
        return Status::OK();
95
0
    }
96
97
private:
98
    // same as ColumnString::insert_data
99
    static void insert_to_chars(int64_t i, ColumnString::Chars& chars, uint32_t& total_size,
100
0
                                const char* pos, size_t length) {
101
0
        const size_t old_size = chars.size();
102
0
        const size_t new_size = old_size + length;
103
104
0
        if (length) {
105
0
            ColumnString::check_chars_length(new_size, i);
106
0
            chars.resize(new_size);
107
0
            memcpy(chars.data() + old_size, pos, length);
108
0
            total_size += length;
109
0
        }
110
0
    }
111
112
    static void _fill_result_string(int64_t i, const StringRef& input_str, const StringRef& sep_str,
113
                                    ColumnString::Chars& dest_chars, uint32_t& total_size,
114
0
                                    bool& is_first_elem) {
115
0
        if (is_first_elem) {
116
0
            insert_to_chars(i, dest_chars, total_size, input_str.data, input_str.size);
117
0
            is_first_elem = false;
118
0
        } else {
119
0
            insert_to_chars(i, dest_chars, total_size, sep_str.data, sep_str.size);
120
0
            insert_to_chars(i, dest_chars, total_size, input_str.data, input_str.size);
121
0
        }
122
0
    }
123
124
    static void _execute_string(const ColumnArrayView<TYPE_STRING>& array_view,
125
                                ColumnView<TYPE_STRING>& sep_column,
126
                                ColumnView<TYPE_STRING>& null_replace_column,
127
                                ColumnString::Chars& dest_chars,
128
0
                                ColumnString::Offsets& dest_offsets) {
129
0
        uint32_t total_size = 0;
130
131
0
        for (int64_t i = 0; i < array_view.size(); ++i) {
132
0
            auto arr = array_view[i];
133
134
0
            auto sep_str = sep_column.value_at(i);
135
0
            auto null_replace_str = null_replace_column.value_at(i);
136
137
0
            bool is_first_elem = true;
138
139
0
            for (size_t j = 0; j < arr.size(); ++j) {
140
0
                if (arr.is_null_at(j)) {
141
0
                    if (null_replace_str.size != 0) {
142
0
                        _fill_result_string(i, null_replace_str, sep_str, dest_chars, total_size,
143
0
                                            is_first_elem);
144
0
                    }
145
0
                    continue;
146
0
                }
147
148
0
                StringRef src_str_ref = arr.value_at(j);
149
0
                _fill_result_string(i, src_str_ref, sep_str, dest_chars, total_size, is_first_elem);
150
0
            }
151
152
0
            dest_offsets[i] = total_size;
153
0
        }
154
0
    }
155
};
156
157
#include "common/compile_check_end.h"
158
} // namespace doris