Coverage Report

Created: 2026-05-30 07:50

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