Coverage Report

Created: 2026-09-20 04:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exprs/aggregate/aggregate_function_group_concat.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
18
#pragma once
19
20
#include <string.h>
21
22
#include <memory>
23
#include <string>
24
25
#include "common/exception.h"
26
#include "core/assert_cast.h"
27
#include "core/column/column_string.h"
28
#include "core/data_type/data_type_string.h"
29
#include "core/string_ref.h"
30
#include "core/types.h"
31
#include "exprs/aggregate/aggregate_function.h"
32
#include "exprs/aggregate/aggregate_function_simple_factory.h"
33
34
namespace doris {
35
class Arena;
36
class BufferReadable;
37
class BufferWritable;
38
class IColumn;
39
} // namespace doris
40
41
namespace doris {
42
43
struct AggregateFunctionGroupConcatData {
44
    ColumnString::Chars data;
45
    std::string separator;
46
    bool inited = false;
47
48
168
    void add(StringRef ref, StringRef sep) {
49
168
        auto delta_size = ref.size;
50
168
        if (!inited) {
51
154
            separator.assign(sep.data, sep.data + sep.size);
52
154
        } else {
53
14
            delta_size += separator.size();
54
14
        }
55
168
        auto offset = data.size();
56
168
        data.resize(data.size() + delta_size);
57
58
168
        if (!inited) {
59
154
            inited = true;
60
154
        } else {
61
14
            memcpy(data.data() + offset, separator.data(), separator.size());
62
14
            offset += separator.size();
63
14
        }
64
168
        memcpy(data.data() + offset, ref.data, ref.size);
65
168
    }
66
67
108
    void merge(const AggregateFunctionGroupConcatData& rhs) {
68
108
        if (!rhs.inited) {
69
30
            return;
70
30
        }
71
72
78
        if (!inited) {
73
40
            inited = true;
74
40
            separator = rhs.separator;
75
40
            data.assign(rhs.data);
76
40
        } else {
77
38
            if (UNLIKELY(separator != rhs.separator)) {
78
24
                throw Exception(ErrorCode::INVALID_ARGUMENT,
79
24
                                "group_concat aggregate states have incompatible separators");
80
24
            }
81
14
            auto offset = data.size();
82
83
14
            auto delta_size = separator.size() + rhs.data.size();
84
14
            data.resize(data.size() + delta_size);
85
86
14
            memcpy(data.data() + offset, separator.data(), separator.size());
87
14
            offset += separator.size();
88
14
            memcpy(data.data() + offset, rhs.data.data(), rhs.data.size());
89
14
        }
90
78
    }
91
92
120
    StringRef get() const { return StringRef {data.data(), data.size()}; }
93
94
51
    void write(BufferWritable& buf) const {
95
51
        buf.write_binary(data);
96
51
        buf.write_binary(separator);
97
51
        buf.write_binary(inited);
98
51
    }
99
100
63
    void read(BufferReadable& buf) {
101
63
        buf.read_binary(data);
102
63
        buf.read_binary(separator);
103
63
        buf.read_binary(inited);
104
63
    }
105
106
6
    void reset() {
107
6
        data.clear();
108
6
        separator = "";
109
6
        inited = false;
110
6
    }
111
};
112
113
struct AggregateFunctionGroupConcatImplStr {
114
    static const std::string separator;
115
    static void add(AggregateFunctionGroupConcatData& __restrict place, const IColumn** columns,
116
0
                    size_t row_num) {
117
0
        place.add(assert_cast<const ColumnString&, TypeCheckOnRelease::DISABLE>(*columns[0])
118
0
                          .get_data_at(row_num),
119
0
                  StringRef(separator.data(), separator.length()));
120
0
    }
121
};
122
123
struct AggregateFunctionGroupConcatImplStrStr {
124
    static void add(AggregateFunctionGroupConcatData& __restrict place, const IColumn** columns,
125
168
                    size_t row_num) {
126
168
        place.add(assert_cast<const ColumnString&, TypeCheckOnRelease::DISABLE>(*columns[0])
127
168
                          .get_data_at(row_num),
128
168
                  assert_cast<const ColumnString&, TypeCheckOnRelease::DISABLE>(*columns[1])
129
168
                          .get_data_at(row_num));
130
168
    }
131
};
132
133
template <typename Impl>
134
class AggregateFunctionGroupConcat final
135
        : public IAggregateFunctionDataHelper<AggregateFunctionGroupConcatData,
136
                                              AggregateFunctionGroupConcat<Impl>>,
137
          VarargsExpression,
138
          NullableAggregateFunction {
139
public:
140
    AggregateFunctionGroupConcat(const DataTypes& argument_types_)
141
7
            : IAggregateFunctionDataHelper<AggregateFunctionGroupConcatData,
142
7
                                           AggregateFunctionGroupConcat<Impl>>(argument_types_) {}
_ZN5doris28AggregateFunctionGroupConcatINS_35AggregateFunctionGroupConcatImplStrEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
141
3
            : IAggregateFunctionDataHelper<AggregateFunctionGroupConcatData,
142
3
                                           AggregateFunctionGroupConcat<Impl>>(argument_types_) {}
_ZN5doris28AggregateFunctionGroupConcatINS_38AggregateFunctionGroupConcatImplStrStrEEC2ERKSt6vectorISt10shared_ptrIKNS_9IDataTypeEESaIS7_EE
Line
Count
Source
141
4
            : IAggregateFunctionDataHelper<AggregateFunctionGroupConcatData,
142
4
                                           AggregateFunctionGroupConcat<Impl>>(argument_types_) {}
143
144
3
    String get_name() const override { return "group_concat"; }
_ZNK5doris28AggregateFunctionGroupConcatINS_35AggregateFunctionGroupConcatImplStrEE8get_nameB5cxx11Ev
Line
Count
Source
144
3
    String get_name() const override { return "group_concat"; }
Unexecuted instantiation: _ZNK5doris28AggregateFunctionGroupConcatINS_38AggregateFunctionGroupConcatImplStrStrEE8get_nameB5cxx11Ev
145
146
123
    DataTypePtr get_return_type() const override { return std::make_shared<DataTypeString>(); }
_ZNK5doris28AggregateFunctionGroupConcatINS_35AggregateFunctionGroupConcatImplStrEE15get_return_typeEv
Line
Count
Source
146
3
    DataTypePtr get_return_type() const override { return std::make_shared<DataTypeString>(); }
_ZNK5doris28AggregateFunctionGroupConcatINS_38AggregateFunctionGroupConcatImplStrStrEE15get_return_typeEv
Line
Count
Source
146
120
    DataTypePtr get_return_type() const override { return std::make_shared<DataTypeString>(); }
147
148
    void add(AggregateDataPtr __restrict place, const IColumn** columns, ssize_t row_num,
149
168
             Arena&) const override {
150
168
        Impl::add(this->data(place), columns, row_num);
151
168
    }
Unexecuted instantiation: _ZNK5doris28AggregateFunctionGroupConcatINS_35AggregateFunctionGroupConcatImplStrEE3addEPcPPKNS_7IColumnElRNS_5ArenaE
_ZNK5doris28AggregateFunctionGroupConcatINS_38AggregateFunctionGroupConcatImplStrStrEE3addEPcPPKNS_7IColumnElRNS_5ArenaE
Line
Count
Source
149
168
             Arena&) const override {
150
168
        Impl::add(this->data(place), columns, row_num);
151
168
    }
152
153
2
    void check_input_columns_type(const IColumn** columns) const override {
154
4
        for (size_t i = 0; i < this->argument_types.size(); ++i) {
155
2
            this->template check_argument_column_type<ColumnString>(columns[i]);
156
2
        }
157
2
    }
_ZNK5doris28AggregateFunctionGroupConcatINS_35AggregateFunctionGroupConcatImplStrEE24check_input_columns_typeEPPKNS_7IColumnE
Line
Count
Source
153
2
    void check_input_columns_type(const IColumn** columns) const override {
154
4
        for (size_t i = 0; i < this->argument_types.size(); ++i) {
155
2
            this->template check_argument_column_type<ColumnString>(columns[i]);
156
2
        }
157
2
    }
Unexecuted instantiation: _ZNK5doris28AggregateFunctionGroupConcatINS_38AggregateFunctionGroupConcatImplStrStrEE24check_input_columns_typeEPPKNS_7IColumnE
158
159
1
    void check_result_column_type(const IColumn& column) const override {
160
1
        this->template check_result_column_type_as<ColumnString>(column);
161
1
    }
_ZNK5doris28AggregateFunctionGroupConcatINS_35AggregateFunctionGroupConcatImplStrEE24check_result_column_typeERKNS_7IColumnE
Line
Count
Source
159
1
    void check_result_column_type(const IColumn& column) const override {
160
1
        this->template check_result_column_type_as<ColumnString>(column);
161
1
    }
Unexecuted instantiation: _ZNK5doris28AggregateFunctionGroupConcatINS_38AggregateFunctionGroupConcatImplStrStrEE24check_result_column_typeERKNS_7IColumnE
162
163
6
    void reset(AggregateDataPtr place) const override { this->data(place).reset(); }
Unexecuted instantiation: _ZNK5doris28AggregateFunctionGroupConcatINS_35AggregateFunctionGroupConcatImplStrEE5resetEPc
_ZNK5doris28AggregateFunctionGroupConcatINS_38AggregateFunctionGroupConcatImplStrStrEE5resetEPc
Line
Count
Source
163
6
    void reset(AggregateDataPtr place) const override { this->data(place).reset(); }
164
165
    void merge(AggregateDataPtr __restrict place, ConstAggregateDataPtr rhs,
166
108
               Arena&) const override {
167
108
        this->data(place).merge(this->data(rhs));
168
108
    }
Unexecuted instantiation: _ZNK5doris28AggregateFunctionGroupConcatINS_35AggregateFunctionGroupConcatImplStrEE5mergeEPcPKcRNS_5ArenaE
_ZNK5doris28AggregateFunctionGroupConcatINS_38AggregateFunctionGroupConcatImplStrStrEE5mergeEPcPKcRNS_5ArenaE
Line
Count
Source
166
108
               Arena&) const override {
167
108
        this->data(place).merge(this->data(rhs));
168
108
    }
169
170
51
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
171
51
        this->data(place).write(buf);
172
51
    }
Unexecuted instantiation: _ZNK5doris28AggregateFunctionGroupConcatINS_35AggregateFunctionGroupConcatImplStrEE9serializeEPKcRNS_14BufferWritableE
_ZNK5doris28AggregateFunctionGroupConcatINS_38AggregateFunctionGroupConcatImplStrStrEE9serializeEPKcRNS_14BufferWritableE
Line
Count
Source
170
51
    void serialize(ConstAggregateDataPtr __restrict place, BufferWritable& buf) const override {
171
51
        this->data(place).write(buf);
172
51
    }
173
174
    void deserialize(AggregateDataPtr __restrict place, BufferReadable& buf,
175
63
                     Arena&) const override {
176
63
        this->data(place).read(buf);
177
63
    }
Unexecuted instantiation: _ZNK5doris28AggregateFunctionGroupConcatINS_35AggregateFunctionGroupConcatImplStrEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE
_ZNK5doris28AggregateFunctionGroupConcatINS_38AggregateFunctionGroupConcatImplStrStrEE11deserializeEPcRNS_14BufferReadableERNS_5ArenaE
Line
Count
Source
175
63
                     Arena&) const override {
176
63
        this->data(place).read(buf);
177
63
    }
178
179
120
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
180
120
        const auto result = this->data(place).get();
181
120
        assert_cast<ColumnString&, TypeCheckOnRelease::DISABLE>(to).insert_data(result.data,
182
120
                                                                                result.size);
183
120
    }
Unexecuted instantiation: _ZNK5doris28AggregateFunctionGroupConcatINS_35AggregateFunctionGroupConcatImplStrEE18insert_result_intoEPKcRNS_7IColumnE
_ZNK5doris28AggregateFunctionGroupConcatINS_38AggregateFunctionGroupConcatImplStrStrEE18insert_result_intoEPKcRNS_7IColumnE
Line
Count
Source
179
120
    void insert_result_into(ConstAggregateDataPtr __restrict place, IColumn& to) const override {
180
120
        const auto result = this->data(place).get();
181
120
        assert_cast<ColumnString&, TypeCheckOnRelease::DISABLE>(to).insert_data(result.data,
182
120
                                                                                result.size);
183
120
    }
184
};
185
186
} // namespace doris