Coverage Report

Created: 2026-03-14 13:33

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
168k
    static Block create_empty_columnswithtypename(const RowDescriptor& row_desc) {
36
        // Block block;
37
168k
        return create_columns_with_type_and_name(row_desc);
38
168k
    }
39
368k
    static MutableBlock build_mutable_mem_reuse_block(Block* block, const RowDescriptor& row_desc) {
40
368k
        if (!block->mem_reuse()) {
41
114k
            MutableBlock tmp(VectorizedUtils::create_columns_with_type_and_name(row_desc));
42
114k
            block->swap(tmp.to_block());
43
114k
        }
44
368k
        return MutableBlock::build_mutable_block(block);
45
368k
    }
46
364k
    static MutableBlock build_mutable_mem_reuse_block(Block* block, const Block& other) {
47
364k
        if (!block->mem_reuse()) {
48
332k
            MutableBlock tmp(other.clone_empty());
49
332k
            block->swap(tmp.to_block());
50
332k
        }
51
364k
        return MutableBlock::build_mutable_block(block);
52
364k
    }
53
    static MutableBlock build_mutable_mem_reuse_block(Block* block,
54
8.07k
                                                      const std::vector<SlotDescriptor*>& slots) {
55
8.07k
        if (!block->mem_reuse()) {
56
5.15k
            size_t column_size = slots.size();
57
5.15k
            MutableColumns columns(column_size);
58
22.8k
            for (size_t i = 0; i < column_size; i++) {
59
17.6k
                columns[i] = slots[i]->get_empty_mutable_column();
60
17.6k
            }
61
5.15k
            int n_columns = 0;
62
17.6k
            for (const auto slot_desc : slots) {
63
17.6k
                block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
64
17.6k
                                                    slot_desc->get_data_type_ptr(),
65
17.6k
                                                    slot_desc->col_name()));
66
17.6k
            }
67
5.15k
        }
68
8.07k
        return MutableBlock(block);
69
8.07k
    }
70
71
333k
    static ColumnsWithTypeAndName create_columns_with_type_and_name(const RowDescriptor& row_desc) {
72
333k
        ColumnsWithTypeAndName columns_with_type_and_name;
73
333k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
74
1.33M
            for (const auto& slot_desc : tuple_desc->slots()) {
75
1.33M
                columns_with_type_and_name.emplace_back(nullptr, slot_desc->get_data_type_ptr(),
76
1.33M
                                                        slot_desc->col_name());
77
1.33M
            }
78
333k
        }
79
333k
        return columns_with_type_and_name;
80
333k
    }
81
82
274k
    static NameAndTypePairs create_name_and_data_types(const RowDescriptor& row_desc) {
83
274k
        NameAndTypePairs name_with_types;
84
274k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
85
1.60M
            for (const auto& slot_desc : tuple_desc->slots()) {
86
1.60M
                name_with_types.emplace_back(slot_desc->col_name(), slot_desc->get_data_type_ptr());
87
1.60M
            }
88
274k
        }
89
274k
        return name_with_types;
90
274k
    }
91
92
202k
    static ColumnsWithTypeAndName create_empty_block(const RowDescriptor& row_desc) {
93
202k
        ColumnsWithTypeAndName columns_with_type_and_name;
94
206k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
95
801k
            for (const auto& slot_desc : tuple_desc->slots()) {
96
801k
                columns_with_type_and_name.emplace_back(
97
801k
                        slot_desc->get_data_type_ptr()->create_column(),
98
801k
                        slot_desc->get_data_type_ptr(), slot_desc->col_name());
99
801k
            }
100
206k
        }
101
202k
        return columns_with_type_and_name;
102
202k
    }
103
104
    // Helper function to extract null map from column (including ColumnConst cases)
105
9.45k
    static const NullMap* get_null_map(const ColumnPtr& col) {
106
9.45k
        if (col->is_nullable()) {
107
4.07k
            return &static_cast<const ColumnNullable&>(*col).get_null_map_data();
108
4.07k
        }
109
        // Handle Const(Nullable) case
110
5.38k
        if (const auto* const_col = check_and_get_column<ColumnConst>(col.get());
111
5.38k
            const_col != nullptr && const_col->is_concrete_nullable()) {
112
45
            return &static_cast<const ColumnNullable&>(const_col->get_data_column())
113
45
                            .get_null_map_data();
114
45
        }
115
5.33k
        return nullptr;
116
5.38k
    };
117
118
    // is_single: whether src is null map of a ColumnConst
119
282k
    static void update_null_map(NullMap& dst, const NullMap& src, bool is_single = false) {
120
282k
        size_t size = dst.size();
121
282k
        auto* __restrict l = dst.data();
122
282k
        auto* __restrict r = src.data();
123
282k
        if (is_single) {
124
47
            if (r[0]) {
125
0
                for (size_t i = 0; i < size; ++i) {
126
0
                    l[i] = 1;
127
0
                }
128
0
            }
129
282k
        } else {
130
101M
            for (size_t i = 0; i < size; ++i) {
131
101M
                l[i] |= r[i];
132
101M
            }
133
282k
        }
134
282k
    }
135
136
70.7k
    static DataTypes get_data_types(const RowDescriptor& row_desc) {
137
70.7k
        DataTypes data_types;
138
70.8k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
139
176k
            for (const auto& slot_desc : tuple_desc->slots()) {
140
176k
                data_types.push_back(slot_desc->get_data_type_ptr());
141
176k
            }
142
70.8k
        }
143
70.7k
        return data_types;
144
70.7k
    }
145
146
32.9k
    static std::vector<std::string> get_column_names(const RowDescriptor& row_desc) {
147
32.9k
        std::vector<std::string> column_names;
148
32.9k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
149
68.6k
            for (const auto& slot_desc : tuple_desc->slots()) {
150
68.6k
                column_names.push_back(slot_desc->col_name());
151
68.6k
            }
152
32.9k
        }
153
32.9k
        return column_names;
154
32.9k
    }
155
156
671k
    static bool all_arguments_are_constant(const Block& block, const ColumnNumbers& args) {
157
705k
        for (const auto& arg : args) {
158
705k
            if (!is_column_const(*block.get_by_position(arg).column)) {
159
602k
                return false;
160
602k
            }
161
705k
        }
162
68.8k
        return true;
163
671k
    }
164
165
    // SIMD helper: find the first not null in the null map
166
95
    static size_t find_first_valid_simd(const NullMap& null_map, size_t start_pos, size_t end_pos) {
167
95
#ifdef __AVX2__
168
        // search by simd
169
95
        for (size_t pos = start_pos; pos + 31 < end_pos; pos += 32) {
170
1
            __m256i null_vec = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&null_map[pos]));
171
1
            __m256i zero_vec = _mm256_setzero_si256();
172
1
            __m256i cmp_result = _mm256_cmpeq_epi8(null_vec, zero_vec);
173
174
1
            int mask = _mm256_movemask_epi8(cmp_result);
175
1
            if (mask != 0) {
176
                // find the first not null
177
1
                return pos + __builtin_ctz(mask);
178
1
            }
179
1
        }
180
181
        // handle the rest elements
182
128
        for (size_t pos = start_pos + ((end_pos - start_pos) / 32) * 32; pos < end_pos; ++pos) {
183
111
            if (!null_map[pos]) {
184
77
                return pos;
185
77
            }
186
111
        }
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
17
        return end_pos;
196
94
    }
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
95
                                               size_t end_pos) {
201
95
#ifdef __AVX2__
202
        // batch set null map to 1 using SIMD (32 bytes at a time)
203
95
        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
129
        for (size_t pos = start_pos + ((end_pos - start_pos) / 32) * 32; pos < end_pos; ++pos) {
210
34
            null_map[pos] = 1;
211
34
        }
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
95
    }
219
};
220
221
6.51k
inline bool match_suffix(const std::string& name, const std::string& suffix) {
222
6.51k
    if (name.length() < suffix.length()) {
223
0
        return false;
224
0
    }
225
6.51k
    return name.substr(name.length() - suffix.length()) == suffix;
226
6.51k
}
227
228
726
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
726
    return name.substr(0, name.length() - suffix.length());
232
726
};
233
234
632
inline ColumnPtr create_always_true_column(size_t size, bool is_nullable) {
235
632
    ColumnPtr res_data_column = ColumnUInt8::create(1, 1);
236
632
    if (is_nullable) {
237
632
        auto null_map = ColumnUInt8::create(1, 0);
238
632
        res_data_column = ColumnNullable::create(res_data_column, std::move(null_map));
239
632
    }
240
632
    return ColumnConst::create(std::move(res_data_column), size);
241
632
}
242
243
// change null element to true element
244
262
inline void change_null_to_true(MutableColumnPtr column, ColumnPtr argument = nullptr) {
245
262
    size_t rows = column->size();
246
262
    if (is_column_const(*column)) {
247
14
        change_null_to_true(
248
14
                assert_cast<ColumnConst*>(column.get())->get_data_column_ptr()->assume_mutable());
249
248
    } else if (column->has_null()) {
250
30
        auto* nullable = assert_cast<ColumnNullable*>(column.get());
251
30
        auto* __restrict data = assert_cast<ColumnUInt8*>(nullable->get_nested_column_ptr().get())
252
30
                                        ->get_data()
253
30
                                        .data();
254
30
        const NullMap& null_map = nullable->get_null_map_data();
255
65.3k
        for (size_t i = 0; i < rows; ++i) {
256
65.3k
            data[i] |= null_map[i];
257
65.3k
        }
258
30
        nullable->fill_false_to_nullmap(rows);
259
218
    } else if (argument && argument->has_null()) {
260
2
        const auto* __restrict null_map =
261
2
                assert_cast<const ColumnNullable*>(argument.get())->get_null_map_data().data();
262
2
        auto* __restrict data = assert_cast<ColumnUInt8*>(column.get())->get_data().data();
263
6
        for (size_t i = 0; i < rows; ++i) {
264
4
            data[i] |= null_map[i];
265
4
        }
266
2
    }
267
262
}
268
269
1.24k
inline size_t calculate_false_number(ColumnPtr column) {
270
1.24k
    size_t rows = column->size();
271
1.24k
    if (is_column_const(*column)) {
272
207
        return calculate_false_number(
273
207
                       assert_cast<const ColumnConst*>(column.get())->get_data_column_ptr()) *
274
207
               rows;
275
1.03k
    } else if (column->is_nullable()) {
276
423
        const auto* nullable = assert_cast<const ColumnNullable*>(column.get());
277
423
        const auto* data = assert_cast<const ColumnUInt8*>(nullable->get_nested_column_ptr().get())
278
423
                                   ->get_data()
279
423
                                   .data();
280
423
        const auto* __restrict null_map = nullable->get_null_map_data().data();
281
423
        return simd::count_zero_num(reinterpret_cast<const int8_t* __restrict>(data), null_map,
282
423
                                    rows);
283
614
    } else {
284
614
        const auto* data = assert_cast<const ColumnUInt8*>(column.get())->get_data().data();
285
614
        return simd::count_zero_num(reinterpret_cast<const int8_t* __restrict>(data), rows);
286
614
    }
287
1.24k
}
288
289
template <typename T>
290
T read_from_json(std::string& json_str) {
291
    auto memBufferIn = std::make_shared<apache::thrift::transport::TMemoryBuffer>(
292
            reinterpret_cast<uint8_t*>(json_str.data()), static_cast<uint32_t>(json_str.size()));
293
    auto jsonProtocolIn = std::make_shared<apache::thrift::protocol::TJSONProtocol>(memBufferIn);
294
    T params;
295
    params.read(jsonProtocolIn.get());
296
    return params;
297
}
298
299
} // namespace doris
300
301
namespace apache::thrift {
302
template <typename ThriftStruct>
303
ThriftStruct from_json_string(const std::string& json_val) {
304
    using namespace apache::thrift::transport;
305
    using namespace apache::thrift::protocol;
306
    ThriftStruct ts;
307
    std::shared_ptr<TTransport> trans =
308
            std::make_shared<TMemoryBuffer>((uint8_t*)json_val.c_str(), (uint32_t)json_val.size());
309
    TJSONProtocol protocol(trans);
310
    ts.read(&protocol);
311
    return ts;
312
}
313
314
} // namespace apache::thrift