Coverage Report

Created: 2026-10-19 11:41

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
48.6k
    static Block create_empty_columnswithtypename(const RowDescriptor& row_desc) {
36
        // Block block;
37
48.6k
        return create_columns_with_type_and_name(row_desc);
38
48.6k
    }
39
    static ScopedMutableBlock build_scoped_mutable_mem_reuse_block(Block* block,
40
39
                                                                   const RowDescriptor& row_desc) {
41
39
        if (!block->mem_reuse()) {
42
37
            MutableBlock tmp(VectorizedUtils::create_columns_with_type_and_name(row_desc));
43
37
            block->swap(tmp.to_block());
44
37
        }
45
39
        return ScopedMutableBlock(block);
46
39
    }
47
    static ScopedMutableBlock build_scoped_mutable_mem_reuse_block(Block* block,
48
786
                                                                   const Block& other) {
49
786
        if (!block->mem_reuse()) {
50
607
            MutableBlock tmp(other.clone_empty());
51
607
            block->swap(tmp.to_block());
52
607
        }
53
786
        return ScopedMutableBlock(block);
54
786
    }
55
    static ScopedMutableBlock build_scoped_mutable_mem_reuse_block(
56
28
            Block* block, const std::vector<SlotDescriptor*>& slots) {
57
28
        if (!block->mem_reuse()) {
58
27
            size_t column_size = slots.size();
59
27
            MutableColumns columns(column_size);
60
112
            for (size_t i = 0; i < column_size; i++) {
61
85
                columns[i] = slots[i]->get_empty_mutable_column();
62
85
            }
63
27
            int n_columns = 0;
64
85
            for (const auto slot_desc : slots) {
65
85
                block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
66
85
                                                    slot_desc->type(), slot_desc->col_name()));
67
85
            }
68
27
        }
69
28
        return ScopedMutableBlock(block);
70
28
    }
71
72
48.7k
    static ColumnsWithTypeAndName create_columns_with_type_and_name(const RowDescriptor& row_desc) {
73
48.7k
        ColumnsWithTypeAndName columns_with_type_and_name;
74
48.7k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
75
94.9k
            for (const auto& slot_desc : tuple_desc->slots()) {
76
94.9k
                columns_with_type_and_name.emplace_back(nullptr, slot_desc->type(),
77
94.9k
                                                        slot_desc->col_name());
78
94.9k
            }
79
48.7k
        }
80
48.7k
        return columns_with_type_and_name;
81
48.7k
    }
82
83
24.0k
    static NameAndTypePairs create_name_and_data_types(const RowDescriptor& row_desc) {
84
24.0k
        NameAndTypePairs name_with_types;
85
24.0k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
86
65.9k
            for (const auto& slot_desc : tuple_desc->slots()) {
87
65.9k
                name_with_types.emplace_back(slot_desc->col_name(), slot_desc->type());
88
65.9k
            }
89
24.0k
        }
90
24.0k
        return name_with_types;
91
24.0k
    }
92
93
1.13k
    static ColumnsWithTypeAndName create_empty_block(const RowDescriptor& row_desc) {
94
1.13k
        ColumnsWithTypeAndName columns_with_type_and_name;
95
1.13k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
96
1.15k
            for (const auto& slot_desc : tuple_desc->slots()) {
97
1.15k
                columns_with_type_and_name.emplace_back(slot_desc->type()->create_column(),
98
1.15k
                                                        slot_desc->type(), slot_desc->col_name());
99
1.15k
            }
100
1.13k
        }
101
1.13k
        return columns_with_type_and_name;
102
1.13k
    }
103
104
    // Helper function to extract null map from column (including ColumnConst cases)
105
228
    static const NullMap* get_null_map(const ColumnPtr& col) {
106
228
        if (const auto* nullable = check_and_get_column<ColumnNullable>(col.get())) {
107
192
            return &nullable->get_null_map_data();
108
192
        }
109
        // Handle Const(Nullable) case
110
36
        if (const auto* const_col = check_and_get_column<ColumnConst>(col.get());
111
36
            const_col != nullptr && const_col->is_nullable()) {
112
36
            return &static_cast<const ColumnNullable&>(const_col->get_data_column())
113
36
                            .get_null_map_data();
114
36
        }
115
0
        return nullptr;
116
36
    };
117
118
    // is_single: whether src is null map of a ColumnConst
119
113k
    static void update_null_map(NullMap& dst, const NullMap& src, bool is_single = false) {
120
113k
        size_t size = dst.size();
121
113k
        auto* __restrict l = dst.data();
122
113k
        auto* __restrict r = src.data();
123
113k
        if (is_single) {
124
40
            if (r[0]) {
125
6
                for (size_t i = 0; i < size; ++i) {
126
5
                    l[i] = 1;
127
5
                }
128
1
            }
129
113k
        } else {
130
458k
            for (size_t i = 0; i < size; ++i) {
131
344k
                l[i] |= r[i];
132
344k
            }
133
113k
        }
134
113k
    }
135
136
48.0k
    static DataTypes get_data_types(const RowDescriptor& row_desc) {
137
48.0k
        DataTypes data_types;
138
48.0k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
139
94.2k
            for (const auto& slot_desc : tuple_desc->slots()) {
140
94.2k
                data_types.push_back(slot_desc->type());
141
94.2k
            }
142
48.0k
        }
143
48.0k
        return data_types;
144
48.0k
    }
145
146
24.0k
    static std::vector<std::string> get_column_names(const RowDescriptor& row_desc) {
147
24.0k
        std::vector<std::string> column_names;
148
24.0k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
149
47.1k
            for (const auto& slot_desc : tuple_desc->slots()) {
150
47.1k
                column_names.push_back(slot_desc->col_name());
151
47.1k
            }
152
24.0k
        }
153
24.0k
        return column_names;
154
24.0k
    }
155
156
91.4k
    static bool all_arguments_are_constant(const Block& block, const ColumnNumbers& args) {
157
128k
        for (const auto& arg : args) {
158
128k
            if (!is_column_const(*block.get_by_position(arg).column)) {
159
85.8k
                return false;
160
85.8k
            }
161
128k
        }
162
5.64k
        return true;
163
91.4k
    }
164
165
    // SIMD helper: find the first not null in the null map
166
14
    static size_t find_first_valid_simd(const NullMap& null_map, size_t start_pos, size_t end_pos) {
167
14
#ifdef __AVX2__
168
        // search by simd
169
14
        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
26
        for (size_t pos = start_pos + ((end_pos - start_pos) / 32) * 32; pos < end_pos; ++pos) {
183
22
            if (!null_map[pos]) {
184
10
                return pos;
185
10
            }
186
22
        }
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
4
        return end_pos;
196
14
    }
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
14
                                               size_t end_pos) {
201
14
#ifdef __AVX2__
202
        // batch set null map to 1 using SIMD (32 bytes at a time)
203
14
        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
26
        for (size_t pos = start_pos + ((end_pos - start_pos) / 32) * 32; pos < end_pos; ++pos) {
210
12
            null_map[pos] = 1;
211
12
        }
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
14
    }
219
};
220
221
14
inline bool match_suffix(const std::string& name, const std::string& suffix) {
222
14
    if (name.length() < suffix.length()) {
223
0
        return false;
224
0
    }
225
14
    return name.substr(name.length() - suffix.length()) == suffix;
226
14
}
227
228
4
inline std::string remove_suffix(const std::string& name, const std::string& suffix) {
229
4
    CHECK(match_suffix(name, suffix))
230
0
            << ", suffix not match, name=" << name << ", suffix=" << suffix;
231
4
    return name.substr(0, name.length() - suffix.length());
232
4
};
233
234
1
inline ColumnPtr create_always_true_column(size_t size, bool is_nullable) {
235
1
    ColumnPtr res_data_column = ColumnUInt8::create(1, 1);
236
1
    if (is_nullable) {
237
1
        auto null_map = ColumnUInt8::create(1, 0);
238
1
        res_data_column = ColumnNullable::create(res_data_column, std::move(null_map));
239
1
    }
240
1
    return ColumnConst::create(std::move(res_data_column), size);
241
1
}
242
243
// change null element to true element
244
2
inline ColumnPtr change_null_to_true(ColumnPtr&& column, const ColumnPtr& argument = nullptr) {
245
2
    size_t rows = column->size();
246
2
    if (is_column_const(*column)) {
247
0
        auto nested_column = assert_cast<const ColumnConst*>(column.get())->get_data_column_ptr();
248
0
        auto nested = change_null_to_true(std::move(nested_column));
249
0
        return ColumnConst::create(std::move(nested), rows);
250
0
    }
251
252
2
    auto mutable_column = IColumn::mutate(std::move(column));
253
2
    if (auto* nullable = check_and_get_column<ColumnNullable>(*mutable_column)) {
254
0
        auto* __restrict data = assert_cast<ColumnUInt8*>(nullable->get_nested_column_ptr().get())
255
0
                                        ->get_data()
256
0
                                        .data();
257
0
        NullMap& null_map = nullable->get_null_map_data();
258
0
        for (size_t i = 0; i < rows; ++i) {
259
0
            data[i] |= null_map[i];
260
0
        }
261
0
        nullable->fill_false_to_nullmap(rows);
262
0
        return mutable_column;
263
0
    }
264
265
2
    if (argument && argument->has_null()) {
266
1
        const auto* __restrict null_map =
267
1
                assert_cast<const ColumnNullable*>(argument.get())->get_null_map_data().data();
268
1
        auto* __restrict data = assert_cast<ColumnUInt8*>(mutable_column.get())->get_data().data();
269
201
        for (size_t i = 0; i < rows; ++i) {
270
200
            data[i] |= null_map[i];
271
200
        }
272
1
    }
273
2
    return mutable_column;
274
2
}
275
276
0
inline size_t calculate_false_number(ColumnPtr column) {
277
0
    size_t rows = column->size();
278
0
    if (is_column_const(*column)) {
279
0
        return calculate_false_number(
280
0
                       assert_cast<const ColumnConst*>(column.get())->get_data_column_ptr()) *
281
0
               rows;
282
0
    } else if (is_column_nullable(*column)) {
283
0
        const auto* nullable = assert_cast<const ColumnNullable*>(column.get());
284
0
        const auto* data = assert_cast<const ColumnUInt8*>(nullable->get_nested_column_ptr().get())
285
0
                                   ->get_data()
286
0
                                   .data();
287
0
        const auto* __restrict null_map = nullable->get_null_map_data().data();
288
0
        return simd::count_zero_num(reinterpret_cast<const int8_t* __restrict>(data), null_map,
289
0
                                    rows);
290
0
    } else {
291
0
        const auto* data = assert_cast<const ColumnUInt8*>(column.get())->get_data().data();
292
0
        return simd::count_zero_num(reinterpret_cast<const int8_t* __restrict>(data), rows);
293
0
    }
294
0
}
295
296
template <typename T>
297
110
T read_from_json(std::string& json_str) {
298
110
    auto memBufferIn = std::make_shared<apache::thrift::transport::TMemoryBuffer>(
299
110
            reinterpret_cast<uint8_t*>(json_str.data()), static_cast<uint32_t>(json_str.size()));
300
110
    auto jsonProtocolIn = std::make_shared<apache::thrift::protocol::TJSONProtocol>(memBufferIn);
301
110
    T params;
302
110
    params.read(jsonProtocolIn.get());
303
110
    return params;
304
110
}
_ZN5doris14read_from_jsonINS_5TExprEEET_RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
297
102
T read_from_json(std::string& json_str) {
298
102
    auto memBufferIn = std::make_shared<apache::thrift::transport::TMemoryBuffer>(
299
102
            reinterpret_cast<uint8_t*>(json_str.data()), static_cast<uint32_t>(json_str.size()));
300
102
    auto jsonProtocolIn = std::make_shared<apache::thrift::protocol::TJSONProtocol>(memBufferIn);
301
102
    T params;
302
102
    params.read(jsonProtocolIn.get());
303
102
    return params;
304
102
}
_ZN5doris14read_from_jsonINS_16TDescriptorTableEEET_RNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
297
8
T read_from_json(std::string& json_str) {
298
8
    auto memBufferIn = std::make_shared<apache::thrift::transport::TMemoryBuffer>(
299
8
            reinterpret_cast<uint8_t*>(json_str.data()), static_cast<uint32_t>(json_str.size()));
300
8
    auto jsonProtocolIn = std::make_shared<apache::thrift::protocol::TJSONProtocol>(memBufferIn);
301
8
    T params;
302
8
    params.read(jsonProtocolIn.get());
303
8
    return params;
304
8
}
305
306
} // namespace doris
307
308
namespace apache::thrift {
309
template <typename ThriftStruct>
310
14
ThriftStruct from_json_string(const std::string& json_val) {
311
14
    using namespace apache::thrift::transport;
312
14
    using namespace apache::thrift::protocol;
313
14
    ThriftStruct ts;
314
14
    std::shared_ptr<TTransport> trans =
315
14
            std::make_shared<TMemoryBuffer>((uint8_t*)json_val.c_str(), (uint32_t)json_val.size());
316
14
    TJSONProtocol protocol(trans);
317
14
    ts.read(&protocol);
318
14
    return ts;
319
14
}
320
321
} // namespace apache::thrift