Coverage Report

Created: 2025-09-15 20:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/vec/utils/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 "runtime/descriptors.h"
25
#include "util/simd/bits.h"
26
#include "vec/columns/column.h"
27
#include "vec/columns/column_nullable.h"
28
#include "vec/core/block.h"
29
#include "vec/exprs/vexpr.h"
30
#include "vec/exprs/vexpr_context.h"
31
32
namespace doris::vectorized {
33
class VectorizedUtils {
34
public:
35
48.4k
    static Block create_empty_columnswithtypename(const RowDescriptor& row_desc) {
36
        // Block block;
37
48.4k
        return create_columns_with_type_and_name(row_desc);
38
48.4k
    }
39
32
    static MutableBlock build_mutable_mem_reuse_block(Block* block, const RowDescriptor& row_desc) {
40
32
        if (!block->mem_reuse()) {
41
32
            MutableBlock tmp(VectorizedUtils::create_columns_with_type_and_name(row_desc));
42
32
            block->swap(tmp.to_block());
43
32
        }
44
32
        return MutableBlock::build_mutable_block(block);
45
32
    }
46
550
    static MutableBlock build_mutable_mem_reuse_block(Block* block, const Block& other) {
47
550
        if (!block->mem_reuse()) {
48
538
            MutableBlock tmp(other.clone_empty());
49
538
            block->swap(tmp.to_block());
50
538
        }
51
550
        return MutableBlock::build_mutable_block(block);
52
550
    }
53
    static MutableBlock build_mutable_mem_reuse_block(Block* block,
54
11
                                                      const std::vector<SlotDescriptor*>& slots) {
55
11
        if (!block->mem_reuse()) {
56
11
            size_t column_size = slots.size();
57
11
            MutableColumns columns(column_size);
58
48
            for (size_t i = 0; i < column_size; i++) {
59
37
                columns[i] = slots[i]->get_empty_mutable_column();
60
37
            }
61
11
            int n_columns = 0;
62
37
            for (const auto slot_desc : slots) {
63
37
                block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
64
37
                                                    slot_desc->get_data_type_ptr(),
65
37
                                                    slot_desc->col_name()));
66
37
            }
67
11
        }
68
11
        return MutableBlock(block);
69
11
    }
70
71
48.5k
    static ColumnsWithTypeAndName create_columns_with_type_and_name(const RowDescriptor& row_desc) {
72
48.5k
        ColumnsWithTypeAndName columns_with_type_and_name;
73
48.5k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
74
94.7k
            for (const auto& slot_desc : tuple_desc->slots()) {
75
94.7k
                if (!slot_desc->is_materialized()) {
76
0
                    continue;
77
0
                }
78
94.7k
                columns_with_type_and_name.emplace_back(nullptr, slot_desc->get_data_type_ptr(),
79
94.7k
                                                        slot_desc->col_name());
80
94.7k
            }
81
48.5k
        }
82
48.5k
        return columns_with_type_and_name;
83
48.5k
    }
84
85
24.0k
    static NameAndTypePairs create_name_and_data_types(const RowDescriptor& row_desc) {
86
24.0k
        NameAndTypePairs name_with_types;
87
24.0k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
88
65.9k
            for (const auto& slot_desc : tuple_desc->slots()) {
89
65.9k
                if (!slot_desc->is_materialized()) {
90
0
                    continue;
91
0
                }
92
65.9k
                name_with_types.emplace_back(slot_desc->col_name(), slot_desc->get_data_type_ptr());
93
65.9k
            }
94
24.0k
        }
95
24.0k
        return name_with_types;
96
24.0k
    }
97
98
    static ColumnsWithTypeAndName create_empty_block(const RowDescriptor& row_desc,
99
869
                                                     bool ignore_trivial_slot = true) {
100
869
        ColumnsWithTypeAndName columns_with_type_and_name;
101
869
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
102
879
            for (const auto& slot_desc : tuple_desc->slots()) {
103
879
                if (ignore_trivial_slot && !slot_desc->is_materialized()) {
104
0
                    continue;
105
0
                }
106
879
                columns_with_type_and_name.emplace_back(
107
879
                        slot_desc->get_data_type_ptr()->create_column(),
108
879
                        slot_desc->get_data_type_ptr(), slot_desc->col_name());
109
879
            }
110
869
        }
111
869
        return columns_with_type_and_name;
112
869
    }
113
114
    // is_single: whether src is null map of a ColumnConst
115
113k
    static void update_null_map(NullMap& dst, const NullMap& src, bool is_single = false) {
116
113k
        size_t size = dst.size();
117
113k
        auto* __restrict l = dst.data();
118
113k
        auto* __restrict r = src.data();
119
113k
        if (is_single) {
120
0
            if (r[0]) {
121
0
                for (size_t i = 0; i < size; ++i) {
122
0
                    l[i] = 1;
123
0
                }
124
0
            }
125
113k
        } else {
126
456k
            for (size_t i = 0; i < size; ++i) {
127
342k
                l[i] |= r[i];
128
342k
            }
129
113k
        }
130
113k
    }
131
132
48.0k
    static DataTypes get_data_types(const RowDescriptor& row_desc) {
133
48.0k
        DataTypes data_types;
134
48.0k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
135
94.2k
            for (const auto& slot_desc : tuple_desc->slots()) {
136
94.2k
                data_types.push_back(slot_desc->get_data_type_ptr());
137
94.2k
            }
138
48.0k
        }
139
48.0k
        return data_types;
140
48.0k
    }
141
142
24.0k
    static std::vector<std::string> get_column_names(const RowDescriptor& row_desc) {
143
24.0k
        std::vector<std::string> column_names;
144
24.0k
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
145
47.1k
            for (const auto& slot_desc : tuple_desc->slots()) {
146
47.1k
                column_names.push_back(slot_desc->col_name());
147
47.1k
            }
148
24.0k
        }
149
24.0k
        return column_names;
150
24.0k
    }
151
152
105k
    static bool all_arguments_are_constant(const Block& block, const ColumnNumbers& args) {
153
114k
        for (const auto& arg : args) {
154
114k
            if (!is_column_const(*block.get_by_position(arg).column)) {
155
103k
                return false;
156
103k
            }
157
114k
        }
158
2.41k
        return true;
159
105k
    }
160
161
    // SIMD helper: find the first not null in the null map
162
0
    static size_t find_first_valid_simd(const NullMap& null_map, size_t start_pos, size_t end_pos) {
163
0
#ifdef __AVX2__
164
        // search by simd
165
0
        for (size_t pos = start_pos; pos + 31 < end_pos; pos += 32) {
166
0
            __m256i null_vec = _mm256_loadu_si256(reinterpret_cast<const __m256i*>(&null_map[pos]));
167
0
            __m256i zero_vec = _mm256_setzero_si256();
168
0
            __m256i cmp_result = _mm256_cmpeq_epi8(null_vec, zero_vec);
169
170
0
            int mask = _mm256_movemask_epi8(cmp_result);
171
0
            if (mask != 0) {
172
                // find the first not null
173
0
                return pos + __builtin_ctz(mask);
174
0
            }
175
0
        }
176
177
        // handle the rest elements
178
0
        for (size_t pos = start_pos + ((end_pos - start_pos) / 32) * 32; pos < end_pos; ++pos) {
179
0
            if (!null_map[pos]) {
180
0
                return pos;
181
0
            }
182
0
        }
183
#else
184
        // standard implementation
185
        for (size_t pos = start_pos; pos < end_pos; ++pos) {
186
            if (!null_map[pos]) {
187
                return pos;
188
            }
189
        }
190
#endif
191
0
        return end_pos;
192
0
    }
193
194
    // SIMD helper: batch set non-null value with [start, end) to 1
195
    static void range_set_nullmap_to_true_simd(NullMap& null_map, size_t start_pos,
196
0
                                               size_t end_pos) {
197
0
#ifdef __AVX2__
198
        // batch set null map to 1 using SIMD (32 bytes at a time)
199
0
        for (size_t pos = start_pos; pos + 31 < end_pos; pos += 32) {
200
0
            __m256i ones_vec = _mm256_set1_epi8(1);
201
0
            _mm256_storeu_si256(reinterpret_cast<__m256i*>(&null_map[pos]), ones_vec);
202
0
        }
203
204
        // handle the rest elements (less than 32 bytes)
205
0
        for (size_t pos = start_pos + ((end_pos - start_pos) / 32) * 32; pos < end_pos; ++pos) {
206
0
            null_map[pos] = 1;
207
0
        }
208
#else
209
        // standard implementation
210
        for (size_t pos = start_pos; pos < end_pos; ++pos) {
211
            null_map[pos] = 1;
212
        }
213
#endif
214
0
    }
215
};
216
217
0
inline bool match_suffix(const std::string& name, const std::string& suffix) {
218
0
    if (name.length() < suffix.length()) {
219
0
        return false;
220
0
    }
221
0
    return name.substr(name.length() - suffix.length()) == suffix;
222
0
}
223
224
0
inline std::string remove_suffix(const std::string& name, const std::string& suffix) {
225
0
    CHECK(match_suffix(name, suffix))
226
0
            << ", suffix not match, name=" << name << ", suffix=" << suffix;
227
0
    return name.substr(0, name.length() - suffix.length());
228
0
};
229
230
0
inline ColumnPtr create_always_true_column(size_t size, bool is_nullable) {
231
0
    ColumnPtr res_data_column = ColumnUInt8::create(1, 1);
232
0
    if (is_nullable) {
233
0
        auto null_map = ColumnUInt8::create(1, 0);
234
0
        res_data_column = ColumnNullable::create(res_data_column, std::move(null_map));
235
0
    }
236
0
    return ColumnConst::create(std::move(res_data_column), size);
237
0
}
238
239
// change null element to true element
240
0
inline void change_null_to_true(ColumnPtr column, ColumnPtr argument = nullptr) {
241
0
    size_t rows = column->size();
242
0
    if (is_column_const(*column)) {
243
0
        change_null_to_true(assert_cast<const ColumnConst*>(column.get())->get_data_column_ptr());
244
0
    } else if (column->has_null()) {
245
0
        auto* nullable =
246
0
                const_cast<ColumnNullable*>(assert_cast<const ColumnNullable*>(column.get()));
247
0
        auto* __restrict data = assert_cast<ColumnUInt8*>(nullable->get_nested_column_ptr().get())
248
0
                                        ->get_data()
249
0
                                        .data();
250
0
        const NullMap& null_map = nullable->get_null_map_data();
251
0
        for (size_t i = 0; i < rows; ++i) {
252
0
            data[i] |= null_map[i];
253
0
        }
254
0
        nullable->fill_false_to_nullmap(rows);
255
0
    } else if (argument && argument->has_null()) {
256
0
        const auto* __restrict null_map =
257
0
                assert_cast<const ColumnNullable*>(argument.get())->get_null_map_data().data();
258
0
        auto* __restrict data =
259
0
                const_cast<ColumnUInt8*>(assert_cast<const ColumnUInt8*>(column.get()))
260
0
                        ->get_data()
261
0
                        .data();
262
0
        for (size_t i = 0; i < rows; ++i) {
263
0
            data[i] |= null_map[i];
264
0
        }
265
0
    }
266
0
}
267
268
0
inline size_t calculate_false_number(ColumnPtr column) {
269
0
    size_t rows = column->size();
270
0
    if (is_column_const(*column)) {
271
0
        return calculate_false_number(
272
0
                       assert_cast<const ColumnConst*>(column.get())->get_data_column_ptr()) *
273
0
               rows;
274
0
    } else if (column->is_nullable()) {
275
0
        const auto* nullable = assert_cast<const ColumnNullable*>(column.get());
276
0
        const auto* data = assert_cast<const ColumnUInt8*>(nullable->get_nested_column_ptr().get())
277
0
                                   ->get_data()
278
0
                                   .data();
279
0
        const auto* __restrict null_map = nullable->get_null_map_data().data();
280
0
        return simd::count_zero_num(reinterpret_cast<const int8_t* __restrict>(data), null_map,
281
0
                                    rows);
282
0
    } else {
283
0
        const auto* data = assert_cast<const ColumnUInt8*>(column.get())->get_data().data();
284
0
        return simd::count_zero_num(reinterpret_cast<const int8_t* __restrict>(data), rows);
285
0
    }
286
0
}
287
288
template <typename T>
289
83
T read_from_json(const std::string& json_str) {
290
83
    auto memBufferIn = std::make_shared<apache::thrift::transport::TMemoryBuffer>(
291
83
            reinterpret_cast<uint8_t*>(const_cast<char*>(json_str.data())),
292
83
            static_cast<uint32_t>(json_str.size()));
293
83
    auto jsonProtocolIn = std::make_shared<apache::thrift::protocol::TJSONProtocol>(memBufferIn);
294
83
    T params;
295
83
    params.read(jsonProtocolIn.get());
296
83
    return params;
297
83
}
_ZN5doris10vectorized14read_from_jsonINS_5TExprEEET_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
289
78
T read_from_json(const std::string& json_str) {
290
78
    auto memBufferIn = std::make_shared<apache::thrift::transport::TMemoryBuffer>(
291
78
            reinterpret_cast<uint8_t*>(const_cast<char*>(json_str.data())),
292
78
            static_cast<uint32_t>(json_str.size()));
293
78
    auto jsonProtocolIn = std::make_shared<apache::thrift::protocol::TJSONProtocol>(memBufferIn);
294
78
    T params;
295
78
    params.read(jsonProtocolIn.get());
296
78
    return params;
297
78
}
_ZN5doris10vectorized14read_from_jsonINS_16TDescriptorTableEEET_RKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE
Line
Count
Source
289
5
T read_from_json(const std::string& json_str) {
290
5
    auto memBufferIn = std::make_shared<apache::thrift::transport::TMemoryBuffer>(
291
5
            reinterpret_cast<uint8_t*>(const_cast<char*>(json_str.data())),
292
5
            static_cast<uint32_t>(json_str.size()));
293
5
    auto jsonProtocolIn = std::make_shared<apache::thrift::protocol::TJSONProtocol>(memBufferIn);
294
5
    T params;
295
5
    params.read(jsonProtocolIn.get());
296
5
    return params;
297
5
}
298
299
} // namespace doris::vectorized
300
301
namespace apache::thrift {
302
template <typename ThriftStruct>
303
14
ThriftStruct from_json_string(const std::string& json_val) {
304
14
    using namespace apache::thrift::transport;
305
14
    using namespace apache::thrift::protocol;
306
14
    ThriftStruct ts;
307
14
    std::shared_ptr<TTransport> trans =
308
14
            std::make_shared<TMemoryBuffer>((uint8_t*)json_val.c_str(), (uint32_t)json_val.size());
309
14
    TJSONProtocol protocol(trans);
310
14
    ts.read(&protocol);
311
14
    return ts;
312
14
}
313
314
} // namespace apache::thrift