Coverage Report

Created: 2026-10-28 15:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/common/util.hpp
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 <thrift/protocol/TJSONProtocol.h>
21
22
#include <boost/shared_ptr.hpp>
23
24
#include "core/block/block.h"
25
#include "core/column/column.h"
26
#include "core/column/column_nullable.h"
27
#include "exprs/vexpr.h"
28
#include "exprs/vexpr_context.h"
29
#include "runtime/descriptors.h"
30
#include "util/simd/bits.h"
31
32
namespace doris {
33
class VectorizedUtils {
34
public:
35
110k
    static Block create_empty_columnswithtypename(const RowDescriptor& row_desc) {
36
        // Block block;
37
110k
        return create_columns_with_type_and_name(row_desc);
38
110k
    }
39
    static ScopedMutableBlock build_scoped_mutable_mem_reuse_block(Block* block,
40
444k
                                                                   const RowDescriptor& row_desc) {
41
444k
        if (!block->mem_reuse()) {
42
148k
            MutableBlock tmp(VectorizedUtils::create_columns_with_type_and_name(row_desc));
43
148k
            block->swap(tmp.to_block());
44
148k
        }
45
444k
        return ScopedMutableBlock(block);
46
444k
    }
47
    static ScopedMutableBlock build_scoped_mutable_mem_reuse_block(Block* block,
48
341k
                                                                   const Block& other) {
49
341k
        if (!block->mem_reuse()) {
50
297k
            MutableBlock tmp(other.clone_empty());
51
297k
            block->swap(tmp.to_block());
52
297k
        }
53
341k
        return ScopedMutableBlock(block);
54
341k
    }
55
    static ScopedMutableBlock build_scoped_mutable_mem_reuse_block(
56
10.7k
            Block* block, const std::vector<SlotDescriptor*>& slots) {
57
10.7k
        if (!block->mem_reuse()) {
58
6.86k
            size_t column_size = slots.size();
59
6.86k
            MutableColumns columns(column_size);
60
30.2k
            for (size_t i = 0; i < column_size; i++) {
61
23.3k
                columns[i] = slots[i]->get_empty_mutable_column();
62
23.3k
            }
63
6.86k
            int n_columns = 0;
64
23.3k
            for (const auto slot_desc : slots) {
65
23.3k
                block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
66
23.3k
                                                    slot_desc->type(), slot_desc->col_name()));
67
23.3k
            }
68
6.86k
        }
69
10.7k
        return ScopedMutableBlock(block);
70
10.7k
    }
71
72
306k
    static ColumnsWithTypeAndName create_columns_with_type_and_name(const RowDescriptor& row_desc) {
73
306k
        ColumnsWithTypeAndName columns_with_type_and_name;
74
306k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
75
1.21M
            for (const auto& slot_desc : tuple_desc->slots()) {
76
1.21M
                columns_with_type_and_name.emplace_back(nullptr, slot_desc->type(),
77
1.21M
                                                        slot_desc->col_name());
78
1.21M
            }
79
306k
        }
80
306k
        return columns_with_type_and_name;
81
306k
    }
82
83
308k
    static NameAndTypePairs create_name_and_data_types(const RowDescriptor& row_desc) {
84
308k
        NameAndTypePairs name_with_types;
85
308k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
86
1.72M
            for (const auto& slot_desc : tuple_desc->slots()) {
87
1.72M
                name_with_types.emplace_back(slot_desc->col_name(), slot_desc->type());
88
1.72M
            }
89
308k
        }
90
308k
        return name_with_types;
91
308k
    }
92
93
233k
    static ColumnsWithTypeAndName create_empty_block(const RowDescriptor& row_desc) {
94
233k
        ColumnsWithTypeAndName columns_with_type_and_name;
95
238k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
96
963k
            for (const auto& slot_desc : tuple_desc->slots()) {
97
963k
                columns_with_type_and_name.emplace_back(slot_desc->type()->create_column(),
98
963k
                                                        slot_desc->type(), slot_desc->col_name());
99
963k
            }
100
238k
        }
101
233k
        return columns_with_type_and_name;
102
233k
    }
103
104
    // Helper function to extract null map from column (including ColumnConst cases)
105
21.0k
    static const NullMap* get_null_map(const ColumnPtr& col) {
106
21.0k
        if (const auto* nullable = check_and_get_column<ColumnNullable>(col.get())) {
107
11.6k
            return &nullable->get_null_map_data();
108
11.6k
        }
109
        // Handle Const(Nullable) case
110
9.46k
        if (const auto* const_col = check_and_get_column<ColumnConst>(col.get());
111
9.46k
            const_col != nullptr && const_col->is_nullable()) {
112
391
            return &static_cast<const ColumnNullable&>(const_col->get_data_column())
113
391
                            .get_null_map_data();
114
391
        }
115
9.07k
        return nullptr;
116
9.46k
    };
117
118
    // is_single: whether src is null map of a ColumnConst
119
222k
    static void update_null_map(NullMap& dst, const NullMap& src, bool is_single = false) {
120
222k
        size_t size = dst.size();
121
222k
        auto* __restrict l = dst.data();
122
222k
        auto* __restrict r = src.data();
123
222k
        if (is_single) {
124
399
            if (r[0]) {
125
10
                for (size_t i = 0; i < size; ++i) {
126
7
                    l[i] = 1;
127
7
                }
128
3
            }
129
221k
        } else {
130
137M
            for (size_t i = 0; i < size; ++i) {
131
136M
                l[i] |= r[i];
132
136M
            }
133
221k
        }
134
222k
    }
135
136
75.5k
    static DataTypes get_data_types(const RowDescriptor& row_desc) {
137
75.5k
        DataTypes data_types;
138
75.5k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
139
190k
            for (const auto& slot_desc : tuple_desc->slots()) {
140
190k
                data_types.push_back(slot_desc->type());
141
190k
            }
142
75.5k
        }
143
75.5k
        return data_types;
144
75.5k
    }
145
146
35.2k
    static std::vector<std::string> get_column_names(const RowDescriptor& row_desc) {
147
35.2k
        std::vector<std::string> column_names;
148
35.2k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
149
75.0k
            for (const auto& slot_desc : tuple_desc->slots()) {
150
75.0k
                column_names.push_back(slot_desc->col_name());
151
75.0k
            }
152
35.2k
        }
153
35.2k
        return column_names;
154
35.2k
    }
155
156
686k
    static bool all_arguments_are_constant(const Block& block, const ColumnNumbers& args) {
157
904k
        for (const auto& arg : args) {
158
904k
            if (!is_column_const(*block.get_by_position(arg).column)) {
159
639k
                return false;
160
639k
            }
161
904k
        }
162
46.7k
        return true;
163
686k
    }
164
165
    // SIMD helper: find the first not null in the null map
166
208
    static size_t find_first_valid_simd(const NullMap& null_map, size_t start_pos, size_t end_pos) {
167
208
#ifdef __AVX2__
168
        // search by simd
169
208
        for (size_t pos = start_pos; pos + 31 < end_pos; pos += 32) {
170
0
            __m256i null_vec = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&null_map[pos]));
171
0
            __m256i zero_vec = _mm256_setzero_si256();
172
0
            __m256i cmp_result = _mm256_cmpeq_epi8(null_vec, zero_vec);
173
174
0
            int mask = _mm256_movemask_epi8(cmp_result);
175
0
            if (mask != 0) {
176
                // find the first not null
177
0
                return pos + __builtin_ctz(mask);
178
0
            }
179
0
        }
180
181
        // handle the rest elements
182
272
        for (size_t pos = start_pos + ((end_pos - start_pos) / 32) * 32; pos < end_pos; ++pos) {
183
217
            if (!null_map[pos]) {
184
153
                return pos;
185
153
            }
186
217
        }
187
#else
188
        // standard implementation
189
        for (size_t pos = start_pos; pos < end_pos; ++pos) {
190
            if (!null_map[pos]) {
191
                return pos;
192
            }
193
        }
194
#endif
195
55
        return end_pos;
196
208
    }
197
198
    // SIMD helper: batch set non-null value with [start, end) to 1
199
    static void range_set_nullmap_to_true_simd(NullMap& null_map, size_t start_pos,
200
208
                                               size_t end_pos) {
201
208
#ifdef __AVX2__
202
        // batch set null map to 1 using SIMD (32 bytes at a time)
203
208
        for (size_t pos = start_pos; pos + 31 < end_pos; pos += 32) {
204
0
            __m256i ones_vec = _mm256_set1_epi8(1);
205
0
            _mm256_storeu_si256(reinterpret_cast<__m256i*>(&null_map[pos]), ones_vec);
206
0
        }
207
208
        // handle the rest elements (less than 32 bytes)
209
272
        for (size_t pos = start_pos + ((end_pos - start_pos) / 32) * 32; pos < end_pos; ++pos) {
210
64
            null_map[pos] = 1;
211
64
        }
212
#else
213
        // standard implementation
214
        for (size_t pos = start_pos; pos < end_pos; ++pos) {
215
            null_map[pos] = 1;
216
        }
217
#endif
218
208
    }
219
};
220
221
13.0k
inline bool match_suffix(const std::string& name, const std::string& suffix) {
222
13.0k
    if (name.length() < suffix.length()) {
223
27
        return false;
224
27
    }
225
13.0k
    return name.substr(name.length() - suffix.length()) == suffix;
226
13.0k
}
227
228
1.13k
inline std::string remove_suffix(const std::string& name, const std::string& suffix) {
229
18.4E
    CHECK(match_suffix(name, suffix))
230
18.4E
            << ", suffix not match, name=" << name << ", suffix=" << suffix;
231
1.13k
    return name.substr(0, name.length() - suffix.length());
232
1.13k
};
233
234
1.35k
inline ColumnPtr create_always_true_column(size_t size, bool is_nullable) {
235
1.35k
    ColumnPtr res_data_column = ColumnUInt8::create(1, 1);
236
1.35k
    if (is_nullable) {
237
1.32k
        auto null_map = ColumnUInt8::create(1, 0);
238
1.32k
        res_data_column = ColumnNullable::create(res_data_column, std::move(null_map));
239
1.32k
    }
240
1.35k
    return ColumnConst::create(std::move(res_data_column), size);
241
1.35k
}
242
243
// change null element to true element
244
157
inline ColumnPtr change_null_to_true(ColumnPtr&& column, const ColumnPtr& argument = nullptr) {
245
157
    size_t rows = column->size();
246
157
    if (is_column_const(*column)) {
247
14
        auto nested_column = assert_cast<const ColumnConst*>(column.get())->get_data_column_ptr();
248
14
        auto nested = change_null_to_true(std::move(nested_column));
249
14
        return ColumnConst::create(std::move(nested), rows);
250
14
    }
251
252
143
    auto mutable_column = IColumn::mutate(std::move(column));
253
143
    if (auto* nullable = check_and_get_column<ColumnNullable>(*mutable_column)) {
254
128
        auto* __restrict data = assert_cast<ColumnUInt8*>(nullable->get_nested_column_ptr().get())
255
128
                                        ->get_data()
256
128
                                        .data();
257
128
        NullMap& null_map = nullable->get_null_map_data();
258
908
        for (size_t i = 0; i < rows; ++i) {
259
780
            data[i] |= null_map[i];
260
780
        }
261
128
        nullable->fill_false_to_nullmap(rows);
262
128
        return mutable_column;
263
128
    }
264
265
19
    if (argument && argument->has_null()) {
266
8
        const auto* __restrict null_map =
267
8
                assert_cast<const ColumnNullable*>(argument.get())->get_null_map_data().data();
268
8
        auto* __restrict data = assert_cast<ColumnUInt8*>(mutable_column.get())->get_data().data();
269
278
        for (size_t i = 0; i < rows; ++i) {
270
270
            data[i] |= null_map[i];
271
270
        }
272
8
    }
273
15
    return mutable_column;
274
143
}
275
276
9.43k
inline size_t calculate_false_number(ColumnPtr column) {
277
9.43k
    size_t rows = column->size();
278
9.43k
    if (is_column_const(*column)) {
279
822
        return calculate_false_number(
280
822
                       assert_cast<const ColumnConst*>(column.get())->get_data_column_ptr()) *
281
822
               rows;
282
8.61k
    } else if (is_column_nullable(*column)) {
283
4.52k
        const auto* nullable = assert_cast<const ColumnNullable*>(column.get());
284
4.52k
        const auto* data = assert_cast<const ColumnUInt8*>(nullable->get_nested_column_ptr().get())
285
4.52k
                                   ->get_data()
286
4.52k
                                   .data();
287
4.52k
        const auto* __restrict null_map = nullable->get_null_map_data().data();
288
4.52k
        return simd::count_zero_num(reinterpret_cast<const int8_t* __restrict>(data), null_map,
289
4.52k
                                    rows);
290
4.52k
    } else {
291
4.09k
        const auto* data = assert_cast<const ColumnUInt8*>(column.get())->get_data().data();
292
4.09k
        return simd::count_zero_num(reinterpret_cast<const int8_t* __restrict>(data), rows);
293
4.09k
    }
294
9.43k
}
295
296
template <typename T>
297
T read_from_json(std::string& json_str) {
298
    auto memBufferIn = std::make_shared<apache::thrift::transport::TMemoryBuffer>(
299
            reinterpret_cast<uint8_t*>(json_str.data()), static_cast<uint32_t>(json_str.size()));
300
    auto jsonProtocolIn = std::make_shared<apache::thrift::protocol::TJSONProtocol>(memBufferIn);
301
    T params;
302
    params.read(jsonProtocolIn.get());
303
    return params;
304
}
305
306
} // namespace doris
307
308
namespace apache::thrift {
309
template <typename ThriftStruct>
310
ThriftStruct from_json_string(const std::string& json_val) {
311
    using namespace apache::thrift::transport;
312
    using namespace apache::thrift::protocol;
313
    ThriftStruct ts;
314
    std::shared_ptr<TTransport> trans =
315
            std::make_shared<TMemoryBuffer>((uint8_t*)json_val.c_str(), (uint32_t)json_val.size());
316
    TJSONProtocol protocol(trans);
317
    ts.read(&protocol);
318
    return ts;
319
}
320
321
} // namespace apache::thrift