Coverage Report

Created: 2026-06-23 16:02

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