be/src/runtime/descriptor_helper.h
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 <gen_cpp/Descriptors_types.h> |
21 | | #include <gen_cpp/Types_types.h> |
22 | | |
23 | | #include <vector> |
24 | | |
25 | | #include "core/data_type/define_primitive_type.h" |
26 | | #include "core/data_type/primitive_type.h" |
27 | | |
28 | | namespace doris { |
29 | | |
30 | | class TDescriptorTableBuilder { |
31 | | public: |
32 | 767k | TSlotId next_slot_id() { return _next_slot_id++; } |
33 | 292k | TTupleId next_tuple_id() { return _next_tuple_id++; } |
34 | | |
35 | 292k | void add_slots(const std::vector<TSlotDescriptor>& slots) { |
36 | 292k | _desc_tbl.__isset.slotDescriptors = true; |
37 | 292k | _desc_tbl.slotDescriptors.insert(_desc_tbl.slotDescriptors.end(), slots.begin(), |
38 | 292k | slots.end()); |
39 | 292k | } |
40 | 292k | void add_tuple(const TTupleDescriptor& tuple) { _desc_tbl.tupleDescriptors.push_back(tuple); } |
41 | | |
42 | 76.1k | TDescriptorTable desc_tbl() { return _desc_tbl; } |
43 | | |
44 | 6 | TDescriptorTableBuilder& append_slotDescriptors(TSlotDescriptor& desc) { |
45 | 6 | _desc_tbl.slotDescriptors.push_back(desc); |
46 | 6 | return *this; |
47 | 6 | } |
48 | 5 | TDescriptorTableBuilder& append_tupleDescriptors(TTupleDescriptor& desc) { |
49 | 5 | _desc_tbl.tupleDescriptors.push_back(desc); |
50 | 5 | return *this; |
51 | 5 | } |
52 | 0 | TDescriptorTableBuilder& append_tableDescriptors(TTableDescriptor& desc) { |
53 | 0 | _desc_tbl.tableDescriptors.push_back(desc); |
54 | 0 | return *this; |
55 | 0 | } |
56 | | |
57 | 3 | TDescriptorTable& build() { return _desc_tbl; } |
58 | | |
59 | | private: |
60 | | TSlotId _next_slot_id = 0; |
61 | | TTupleId _next_tuple_id = 0; |
62 | | TDescriptorTable _desc_tbl; |
63 | | }; |
64 | | |
65 | | class TTupleDescriptorBuilder; |
66 | | class TSlotDescriptorBuilder { |
67 | | public: |
68 | 371k | TTypeDesc get_common_type(TPrimitiveType::type type) { |
69 | 371k | TTypeNode node; |
70 | 371k | node.type = TTypeNodeType::SCALAR; |
71 | 371k | node.__isset.scalar_type = true; |
72 | 371k | node.scalar_type.type = type; |
73 | | |
74 | 371k | TTypeDesc type_desc; |
75 | 371k | type_desc.types.push_back(node); |
76 | | |
77 | 371k | return type_desc; |
78 | 371k | } |
79 | | |
80 | 371k | TSlotDescriptorBuilder() = default; |
81 | 347k | TSlotDescriptorBuilder& type(PrimitiveType type) { |
82 | 347k | _slot_desc.slotType = get_common_type(to_thrift(type)); |
83 | 347k | return *this; |
84 | 347k | } |
85 | 8.09k | TSlotDescriptorBuilder& decimal_type(int precision, int scale) { |
86 | 8.09k | _slot_desc.slotType = get_common_type(to_thrift(TYPE_DECIMALV2)); |
87 | 8.09k | _slot_desc.slotType.types[0].scalar_type.__set_precision(precision); |
88 | 8.09k | _slot_desc.slotType.types[0].scalar_type.__set_scale(scale); |
89 | 8.09k | return *this; |
90 | 8.09k | } |
91 | 16.2k | TSlotDescriptorBuilder& string_type(int len) { |
92 | 16.2k | _slot_desc.slotType = get_common_type(to_thrift(TYPE_VARCHAR)); |
93 | 16.2k | _slot_desc.slotType.types[0].scalar_type.__set_len(len); |
94 | 16.2k | return *this; |
95 | 16.2k | } |
96 | 327k | TSlotDescriptorBuilder& nullable(bool nullable) { |
97 | 327k | _slot_desc.nullIndicatorByte = (nullable) ? 0 : -1; |
98 | 327k | return *this; |
99 | 327k | } |
100 | 89.3k | TSlotDescriptorBuilder& column_name(const std::string& name) { |
101 | 89.3k | _slot_desc.colName = name; |
102 | 89.3k | return *this; |
103 | 89.3k | } |
104 | 89.3k | TSlotDescriptorBuilder& column_pos(int column_pos) { |
105 | 89.3k | _slot_desc.columnPos = column_pos; |
106 | 89.3k | return *this; |
107 | 89.3k | } |
108 | 371k | TSlotDescriptor build() { return _slot_desc; } |
109 | | |
110 | 6 | TSlotDescriptorBuilder& set_id(TTupleId id) { |
111 | 6 | _slot_desc.id = id; |
112 | 6 | return *this; |
113 | 6 | } |
114 | 6 | TSlotDescriptorBuilder& set_parent(TTupleDescriptor& parent) { |
115 | 6 | _slot_desc.parent = parent.id; |
116 | 6 | return *this; |
117 | 6 | } |
118 | 6 | TSlotDescriptorBuilder& set_slotType(TTypeDesc& slotType) { |
119 | 6 | _slot_desc.slotType = slotType; |
120 | 6 | return *this; |
121 | 6 | } |
122 | 6 | TSlotDescriptorBuilder& set_nullIndicatorBit(int nullIndicatorBit) { |
123 | 6 | _slot_desc.nullIndicatorBit = nullIndicatorBit; |
124 | 6 | return *this; |
125 | 6 | } |
126 | 6 | TSlotDescriptorBuilder& set_byteOffset(int byteOffset) { |
127 | 6 | _slot_desc.byteOffset = byteOffset; |
128 | 6 | return *this; |
129 | 6 | } |
130 | 6 | TSlotDescriptorBuilder& set_slotIdx(int slotIdx) { |
131 | 6 | _slot_desc.slotIdx = slotIdx; |
132 | 6 | return *this; |
133 | 6 | } |
134 | 6 | TSlotDescriptorBuilder& set_colName(std::string colName) { |
135 | 6 | _slot_desc.colName = colName; |
136 | 6 | return *this; |
137 | 6 | } |
138 | | |
139 | | private: |
140 | | friend TTupleDescriptorBuilder; |
141 | | TSlotDescriptor _slot_desc; |
142 | | }; |
143 | | |
144 | | class TTupleDescriptorBuilder { |
145 | | public: |
146 | 767k | TTupleDescriptorBuilder& add_slot(const TSlotDescriptor& slot_desc) { |
147 | 767k | _slot_descs.push_back(slot_desc); |
148 | 767k | return *this; |
149 | 767k | } |
150 | 292k | void build(TDescriptorTableBuilder* tb) { |
151 | | // build slot desc |
152 | 292k | _tuple_id = tb->next_tuple_id(); |
153 | 292k | int null_offset = 0; |
154 | 1.05M | for (int i = 0; i < _slot_descs.size(); ++i) { |
155 | 767k | auto& slot_desc = _slot_descs[i]; |
156 | 767k | slot_desc.id = tb->next_slot_id(); |
157 | 767k | slot_desc.parent = _tuple_id; |
158 | 767k | if (slot_desc.nullIndicatorByte >= 0) { |
159 | 440k | slot_desc.nullIndicatorBit = null_offset % 8; |
160 | 440k | slot_desc.nullIndicatorByte = null_offset / 8; |
161 | 440k | null_offset++; |
162 | 440k | } else { |
163 | 327k | slot_desc.nullIndicatorByte = 0; |
164 | 327k | slot_desc.nullIndicatorBit = -1; |
165 | 327k | } |
166 | 767k | slot_desc.slotIdx = i; |
167 | 767k | } |
168 | | |
169 | 292k | _tuple_desc.id = _tuple_id; |
170 | | // Useless not set it. |
171 | 292k | _tuple_desc.byteSize = 0; |
172 | 292k | _tuple_desc.numNullBytes = 0; |
173 | 292k | _tuple_desc.numNullSlots = _slot_descs.size(); |
174 | | |
175 | 292k | tb->add_slots(_slot_descs); |
176 | 292k | tb->add_tuple(_tuple_desc); |
177 | 292k | } |
178 | | |
179 | 5 | TTupleDescriptorBuilder& set_id(TTupleId id) { |
180 | 5 | _tuple_desc.id = id; |
181 | 5 | return *this; |
182 | 5 | } |
183 | | |
184 | 5 | TTupleDescriptor& build() { return _tuple_desc; } |
185 | | |
186 | | private: |
187 | | TTupleId _tuple_id; |
188 | | std::vector<TSlotDescriptor> _slot_descs; |
189 | | TTupleDescriptor _tuple_desc; |
190 | | }; |
191 | | |
192 | | } // namespace doris |