Coverage Report

Created: 2024-11-18 10:37

/root/doris/be/src/vec/utils/util.hpp
Line
Count
Source (jump to first uncovered line)
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 "vec/columns/column.h"
26
#include "vec/columns/column_nullable.h"
27
#include "vec/core/block.h"
28
#include "vec/exprs/vexpr.h"
29
#include "vec/exprs/vexpr_context.h"
30
31
namespace doris::vectorized {
32
class VectorizedUtils {
33
public:
34
0
    static Block create_empty_columnswithtypename(const RowDescriptor& row_desc) {
35
        // Block block;
36
0
        return create_columns_with_type_and_name(row_desc);
37
0
    }
38
0
    static MutableBlock build_mutable_mem_reuse_block(Block* block, const RowDescriptor& row_desc) {
39
0
        if (!block->mem_reuse()) {
40
0
            MutableBlock tmp(VectorizedUtils::create_columns_with_type_and_name(row_desc));
41
0
            block->swap(tmp.to_block());
42
0
        }
43
0
        return MutableBlock::build_mutable_block(block);
44
0
    }
45
0
    static MutableBlock build_mutable_mem_reuse_block(Block* block, const Block& other) {
46
0
        if (!block->mem_reuse()) {
47
0
            MutableBlock tmp(other.clone_empty());
48
0
            block->swap(tmp.to_block());
49
0
        }
50
0
        return MutableBlock::build_mutable_block(block);
51
0
    }
52
    static MutableBlock build_mutable_mem_reuse_block(Block* block,
53
0
                                                      std::vector<SlotDescriptor*>& slots) {
54
0
        if (!block->mem_reuse()) {
55
0
            size_t column_size = slots.size();
56
0
            MutableColumns columns(column_size);
57
0
            for (size_t i = 0; i < column_size; i++) {
58
0
                columns[i] = slots[i]->get_empty_mutable_column();
59
0
            }
60
0
            int n_columns = 0;
61
0
            for (const auto slot_desc : slots) {
62
0
                block->insert(ColumnWithTypeAndName(std::move(columns[n_columns++]),
63
0
                                                    slot_desc->get_data_type_ptr(),
64
0
                                                    slot_desc->col_name()));
65
0
            }
66
0
        }
67
0
        return MutableBlock(block);
68
0
    }
69
    static ColumnsWithTypeAndName create_columns_with_type_and_name(
70
0
            const RowDescriptor& row_desc, bool ignore_trivial_slot = true) {
71
0
        ColumnsWithTypeAndName columns_with_type_and_name;
72
0
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
73
0
            for (const auto& slot_desc : tuple_desc->slots()) {
74
0
                if (ignore_trivial_slot && !slot_desc->need_materialize()) {
75
0
                    continue;
76
0
                }
77
0
                columns_with_type_and_name.emplace_back(nullptr, slot_desc->get_data_type_ptr(),
78
0
                                                        slot_desc->col_name());
79
0
            }
80
0
        }
81
0
        return columns_with_type_and_name;
82
0
    }
83
84
    static ColumnsWithTypeAndName create_empty_block(const RowDescriptor& row_desc,
85
0
                                                     bool ignore_trivial_slot = true) {
86
0
        ColumnsWithTypeAndName columns_with_type_and_name;
87
0
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
88
0
            for (const auto& slot_desc : tuple_desc->slots()) {
89
0
                if (ignore_trivial_slot && !slot_desc->need_materialize()) {
90
0
                    continue;
91
0
                }
92
0
                columns_with_type_and_name.emplace_back(
93
0
                        slot_desc->get_data_type_ptr()->create_column(),
94
0
                        slot_desc->get_data_type_ptr(), slot_desc->col_name());
95
0
            }
96
0
        }
97
0
        return columns_with_type_and_name;
98
0
    }
99
100
    // is_single: whether src is null map of a ColumnConst
101
682
    static void update_null_map(NullMap& dst, const NullMap& src, bool is_single = false) {
102
682
        size_t size = dst.size();
103
682
        auto* __restrict l = dst.data();
104
682
        auto* __restrict r = src.data();
105
682
        if (is_single && r[0]) {
106
4
            for (size_t i = 0; i < size; ++i) {
107
2
                l[i] = 1;
108
2
            }
109
680
        } else {
110
4.42k
            for (size_t i = 0; i < size; ++i) {
111
3.74k
                l[i] |= r[i];
112
3.74k
            }
113
680
        }
114
682
    }
115
116
0
    static DataTypes get_data_types(const RowDescriptor& row_desc) {
117
0
        DataTypes data_types;
118
0
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
119
0
            for (const auto& slot_desc : tuple_desc->slots()) {
120
0
                data_types.push_back(slot_desc->get_data_type_ptr());
121
0
            }
122
0
        }
123
0
        return data_types;
124
0
    }
125
126
0
    static std::vector<std::string> get_column_names(const RowDescriptor& row_desc) {
127
0
        std::vector<std::string> column_names;
128
0
        for (const auto& tuple_desc : row_desc.tuple_descriptors()) {
129
0
            for (const auto& slot_desc : tuple_desc->slots()) {
130
0
                column_names.push_back(slot_desc->col_name());
131
0
            }
132
0
        }
133
0
        return column_names;
134
0
    }
135
136
984
    static bool all_arguments_are_constant(const Block& block, const ColumnNumbers& args) {
137
992
        for (const auto& arg : args) {
138
992
            if (!is_column_const(*block.get_by_position(arg).column)) {
139
983
                return false;
140
983
            }
141
992
        }
142
1
        return true;
143
984
    }
144
};
145
146
0
inline bool match_suffix(const std::string& name, const std::string& suffix) {
147
0
    if (name.length() < suffix.length()) {
148
0
        return false;
149
0
    }
150
0
    return name.substr(name.length() - suffix.length()) == suffix;
151
0
}
152
153
0
inline std::string remove_suffix(const std::string& name, const std::string& suffix) {
154
0
    CHECK(match_suffix(name, suffix))
155
0
            << ", suffix not match, name=" << name << ", suffix=" << suffix;
156
0
    return name.substr(0, name.length() - suffix.length());
157
0
};
158
159
} // namespace doris::vectorized
160
161
namespace apache::thrift {
162
template <typename ThriftStruct>
163
2
ThriftStruct from_json_string(const std::string& json_val) {
164
2
    using namespace apache::thrift::transport;
165
2
    using namespace apache::thrift::protocol;
166
2
    ThriftStruct ts;
167
2
    std::shared_ptr<TTransport> trans =
168
2
            std::make_shared<TMemoryBuffer>((uint8_t*)json_val.c_str(), (uint32_t)json_val.size());
169
2
    TJSONProtocol protocol(trans);
170
2
    ts.read(&protocol);
171
2
    return ts;
172
2
}
173
174
} // namespace apache::thrift