Coverage Report

Created: 2026-10-16 17:05

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
438k
                                                                   const RowDescriptor& row_desc) {
41
438k
        if (!block->mem_reuse()) {
42
140k
            MutableBlock tmp(VectorizedUtils::create_columns_with_type_and_name(row_desc));
43
140k
            block->swap(tmp.to_block());
44
140k
        }
45
438k
        return ScopedMutableBlock(block);
46
438k
    }
47
    static ScopedMutableBlock build_scoped_mutable_mem_reuse_block(Block* block,
48
388k
                                                                   const Block& other) {
49
388k
        if (!block->mem_reuse()) {
50
344k
            MutableBlock tmp(other.clone_empty());
51
344k
            block->swap(tmp.to_block());
52
344k
        }
53
388k
        return ScopedMutableBlock(block);
54
388k
    }
55
    static ScopedMutableBlock build_scoped_mutable_mem_reuse_block(
56
10.4k
            Block* block, const std::vector<SlotDescriptor*>& slots) {
57
10.4k
        if (!block->mem_reuse()) {
58
6.45k
            size_t column_size = slots.size();
59
6.45k
            MutableColumns columns(column_size);
60
27.7k
            for (size_t i = 0; i < column_size; i++) {
61
21.2k
                columns[i] = slots[i]->get_empty_mutable_column();
62
21.2k
            }
63
6.45k
            int n_columns = 0;
64
21.2k
            for (const auto slot_desc : slots) {
65
21.2k
                block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
66
21.2k
                                                    slot_desc->type(), slot_desc->col_name()));
67
21.2k
            }
68
6.45k
        }
69
10.4k
        return ScopedMutableBlock(block);
70
10.4k
    }
71
72
298k
    static ColumnsWithTypeAndName create_columns_with_type_and_name(const RowDescriptor& row_desc) {
73
298k
        ColumnsWithTypeAndName columns_with_type_and_name;
74
298k
        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
298k
        }
80
298k
        return columns_with_type_and_name;
81
298k
    }
82
83
307k
    static NameAndTypePairs create_name_and_data_types(const RowDescriptor& row_desc) {
84
307k
        NameAndTypePairs name_with_types;
85
307k
        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
307k
        }
90
307k
        return name_with_types;
91
307k
    }
92
93
277k
    static ColumnsWithTypeAndName create_empty_block(const RowDescriptor& row_desc) {
94
277k
        ColumnsWithTypeAndName columns_with_type_and_name;
95
283k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
96
1.14M
            for (const auto& slot_desc : tuple_desc->slots()) {
97
1.14M
                columns_with_type_and_name.emplace_back(slot_desc->type()->create_column(),
98
1.14M
                                                        slot_desc->type(), slot_desc->col_name());
99
1.14M
            }
100
283k
        }
101
277k
        return columns_with_type_and_name;
102
277k
    }
103
104
    // Helper function to extract null map from column (including ColumnConst cases)
105
21.5k
    static const NullMap* get_null_map(const ColumnPtr& col) {
106
21.5k
        if (const auto* nullable = check_and_get_column<ColumnNullable>(col.get())) {
107
12.1k
            return &nullable->get_null_map_data();
108
12.1k
        }
109
        // Handle Const(Nullable) case
110
9.38k
        if (const auto* const_col = check_and_get_column<ColumnConst>(col.get());
111
9.38k
            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
8.99k
        return nullptr;
116
9.38k
    };
117
118
    // is_single: whether src is null map of a ColumnConst
119
248k
    static void update_null_map(NullMap& dst, const NullMap& src, bool is_single = false) {
120
248k
        size_t size = dst.size();
121
248k
        auto* __restrict l = dst.data();
122
248k
        auto* __restrict r = src.data();
123
248k
        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
248k
        } else {
130
93.9M
            for (size_t i = 0; i < size; ++i) {
131
93.7M
                l[i] |= r[i];
132
93.7M
            }
133
248k
        }
134
248k
    }
135
136
75.2k
    static DataTypes get_data_types(const RowDescriptor& row_desc) {
137
75.2k
        DataTypes data_types;
138
75.2k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
139
188k
            for (const auto& slot_desc : tuple_desc->slots()) {
140
188k
                data_types.push_back(slot_desc->type());
141
188k
            }
142
75.2k
        }
143
75.2k
        return data_types;
144
75.2k
    }
145
146
35.0k
    static std::vector<std::string> get_column_names(const RowDescriptor& row_desc) {
147
35.0k
        std::vector<std::string> column_names;
148
35.0k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
149
73.9k
            for (const auto& slot_desc : tuple_desc->slots()) {
150
73.9k
                column_names.push_back(slot_desc->col_name());
151
73.9k
            }
152
35.0k
        }
153
35.0k
        return column_names;
154
35.0k
    }
155
156
875k
    static bool all_arguments_are_constant(const Block& block, const ColumnNumbers& args) {
157
972k
        for (const auto& arg : args) {
158
972k
            if (!is_column_const(*block.get_by_position(arg).column)) {
159
766k
                return false;
160
766k
            }
161
972k
        }
162
108k
        return true;
163
875k
    }
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
12.5k
inline bool match_suffix(const std::string& name, const std::string& suffix) {
222
12.5k
    if (name.length() < suffix.length()) {
223
25
        return false;
224
25
    }
225
12.4k
    return name.substr(name.length() - suffix.length()) == suffix;
226
12.5k
}
227
228
978
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
978
    return name.substr(0, name.length() - suffix.length());
232
978
};
233
234
1.13k
inline ColumnPtr create_always_true_column(size_t size, bool is_nullable) {
235
1.13k
    ColumnPtr res_data_column = ColumnUInt8::create(1, 1);
236
1.13k
    if (is_nullable) {
237
1.11k
        auto null_map = ColumnUInt8::create(1, 0);
238
1.11k
        res_data_column = ColumnNullable::create(res_data_column, std::move(null_map));
239
1.11k
    }
240
1.13k
    return ColumnConst::create(std::move(res_data_column), size);
241
1.13k
}
242
243
// change null element to true element
244
297
inline ColumnPtr change_null_to_true(ColumnPtr&& column, const ColumnPtr& argument = nullptr) {
245
297
    size_t rows = column->size();
246
297
    if (is_column_const(*column)) {
247
6
        auto nested_column = assert_cast<const ColumnConst*>(column.get())->get_data_column_ptr();
248
6
        auto nested = change_null_to_true(std::move(nested_column));
249
6
        return ColumnConst::create(std::move(nested), rows);
250
6
    }
251
252
291
    auto mutable_column = IColumn::mutate(std::move(column));
253
291
    if (auto* nullable = check_and_get_column<ColumnNullable>(*mutable_column)) {
254
278
        auto* __restrict data = assert_cast<ColumnUInt8*>(nullable->get_nested_column_ptr().get())
255
278
                                        ->get_data()
256
278
                                        .data();
257
278
        NullMap& null_map = nullable->get_null_map_data();
258
49.8k
        for (size_t i = 0; i < rows; ++i) {
259
49.5k
            data[i] |= null_map[i];
260
49.5k
        }
261
278
        nullable->fill_false_to_nullmap(rows);
262
278
        return mutable_column;
263
278
    }
264
265
13
    if (argument && argument->has_null()) {
266
10
        const auto* __restrict null_map =
267
10
                assert_cast<const ColumnNullable*>(argument.get())->get_null_map_data().data();
268
10
        auto* __restrict data = assert_cast<ColumnUInt8*>(mutable_column.get())->get_data().data();
269
228
        for (size_t i = 0; i < rows; ++i) {
270
218
            data[i] |= null_map[i];
271
218
        }
272
10
    }
273
13
    return mutable_column;
274
291
}
275
276
8.70k
inline size_t calculate_false_number(ColumnPtr column) {
277
8.70k
    size_t rows = column->size();
278
8.70k
    if (is_column_const(*column)) {
279
714
        return calculate_false_number(
280
714
                       assert_cast<const ColumnConst*>(column.get())->get_data_column_ptr()) *
281
714
               rows;
282
7.99k
    } else if (is_column_nullable(*column)) {
283
4.28k
        const auto* nullable = assert_cast<const ColumnNullable*>(column.get());
284
4.28k
        const auto* data = assert_cast<const ColumnUInt8*>(nullable->get_nested_column_ptr().get())
285
4.28k
                                   ->get_data()
286
4.28k
                                   .data();
287
4.28k
        const auto* __restrict null_map = nullable->get_null_map_data().data();
288
4.28k
        return simd::count_zero_num(reinterpret_cast<const int8_t* __restrict>(data), null_map,
289
4.28k
                                    rows);
290
4.28k
    } else {
291
3.70k
        const auto* data = assert_cast<const ColumnUInt8*>(column.get())->get_data().data();
292
3.70k
        return simd::count_zero_num(reinterpret_cast<const int8_t* __restrict>(data), rows);
293
3.70k
    }
294
8.70k
}
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