be/src/format_v2/table_reader.cpp
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 | | #include "format_v2/table_reader.h" |
19 | | |
20 | | #include <gen_cpp/ExternalTableSchema_types.h> |
21 | | #include <gen_cpp/PlanNodes_types.h> |
22 | | #include <gen_cpp/Types_types.h> |
23 | | |
24 | | #include <algorithm> |
25 | | #include <memory> |
26 | | #include <ranges> |
27 | | #include <set> |
28 | | #include <sstream> |
29 | | #include <utility> |
30 | | #include <vector> |
31 | | |
32 | | #include "common/cast_set.h" |
33 | | #include "common/status.h" |
34 | | #include "core/assert_cast.h" |
35 | | #include "core/data_type/data_type_array.h" |
36 | | #include "core/data_type/data_type_factory.hpp" |
37 | | #include "core/data_type/data_type_map.h" |
38 | | #include "core/data_type/data_type_struct.h" |
39 | | #include "core/data_type/primitive_type.h" |
40 | | #include "exprs/vexpr_context.h" |
41 | | #include "exprs/vslot_ref.h" |
42 | | #include "format/table/deletion_vector_reader.h" |
43 | | #include "format/table/iceberg_delete_file_reader_helper.h" |
44 | | #include "format/table/iceberg_scan_semantics.h" |
45 | | #include "format/table/paimon_reader.h" |
46 | | #include "format_v2/column_mapper.h" |
47 | | #include "format_v2/delimited_text/csv_reader.h" |
48 | | #include "format_v2/delimited_text/text_reader.h" |
49 | | #include "format_v2/json/json_reader.h" |
50 | | #include "format_v2/native/native_reader.h" |
51 | | #include "format_v2/orc/orc_reader.h" |
52 | | #include "format_v2/parquet/parquet_reader.h" |
53 | | #include "runtime/file_scan_profile.h" |
54 | | #include "storage/segment/condition_cache.h" |
55 | | #include "util/debug_points.h" |
56 | | #include "util/string_util.h" |
57 | | |
58 | | namespace doris::format { |
59 | | namespace { |
60 | | |
61 | | template <typename T, typename Formatter> |
62 | 61 | std::string join_table_reader_debug_strings(const std::vector<T>& values, Formatter formatter) { |
63 | 61 | std::ostringstream out; |
64 | 61 | out << "["; |
65 | 70 | for (size_t i = 0; i < values.size(); ++i) { |
66 | 9 | if (i > 0) { |
67 | 3 | out << ", "; |
68 | 3 | } |
69 | 9 | out << formatter(values[i]); |
70 | 9 | } |
71 | 61 | out << "]"; |
72 | 61 | return out.str(); |
73 | 61 | } table_reader.cpp:_ZN5doris6format12_GLOBAL__N_131join_table_reader_debug_stringsINS0_16ColumnDefinitionEZNKS0_11TableReader12debug_stringB5cxx11EvE3$_0EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISD_EET0_ Line | Count | Source | 62 | 12 | std::string join_table_reader_debug_strings(const std::vector<T>& values, Formatter formatter) { | 63 | 12 | std::ostringstream out; | 64 | 12 | out << "["; | 65 | 14 | for (size_t i = 0; i < values.size(); ++i) { | 66 | 2 | if (i > 0) { | 67 | 1 | out << ", "; | 68 | 1 | } | 69 | 2 | out << formatter(values[i]); | 70 | 2 | } | 71 | 12 | out << "]"; | 72 | 12 | return out.str(); | 73 | 12 | } |
table_reader.cpp:_ZN5doris6format12_GLOBAL__N_131join_table_reader_debug_stringsINS0_11TableFilterEZNKS0_11TableReader12debug_stringB5cxx11EvE3$_1EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISD_EET0_ Line | Count | Source | 62 | 12 | std::string join_table_reader_debug_strings(const std::vector<T>& values, Formatter formatter) { | 63 | 12 | std::ostringstream out; | 64 | 12 | out << "["; | 65 | 13 | for (size_t i = 0; i < values.size(); ++i) { | 66 | 1 | if (i > 0) { | 67 | 0 | out << ", "; | 68 | 0 | } | 69 | 1 | out << formatter(values[i]); | 70 | 1 | } | 71 | 12 | out << "]"; | 72 | 12 | return out.str(); | 73 | 12 | } |
table_reader.cpp:_ZN5doris6format12_GLOBAL__N_131join_table_reader_debug_stringsINS0_11GlobalIndexEZNS1_25table_filter_debug_stringB5cxx11ERKNS0_11TableFilterEE3$_0EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISF_EET0_ Line | Count | Source | 62 | 1 | std::string join_table_reader_debug_strings(const std::vector<T>& values, Formatter formatter) { | 63 | 1 | std::ostringstream out; | 64 | 1 | out << "["; | 65 | 2 | for (size_t i = 0; i < values.size(); ++i) { | 66 | 1 | if (i > 0) { | 67 | 0 | out << ", "; | 68 | 0 | } | 69 | 1 | out << formatter(values[i]); | 70 | 1 | } | 71 | 1 | out << "]"; | 72 | 1 | return out.str(); | 73 | 1 | } |
table_reader.cpp:_ZN5doris6format12_GLOBAL__N_131join_table_reader_debug_stringsISt10shared_ptrINS_12VExprContextEEZNKS0_11TableReader12debug_stringB5cxx11EvE3$_2EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISF_EET0_ Line | Count | Source | 62 | 12 | std::string join_table_reader_debug_strings(const std::vector<T>& values, Formatter formatter) { | 63 | 12 | std::ostringstream out; | 64 | 12 | out << "["; | 65 | 13 | for (size_t i = 0; i < values.size(); ++i) { | 66 | 1 | if (i > 0) { | 67 | 0 | out << ", "; | 68 | 0 | } | 69 | 1 | out << formatter(values[i]); | 70 | 1 | } | 71 | 12 | out << "]"; | 72 | 12 | return out.str(); | 73 | 12 | } |
table_reader.cpp:_ZN5doris6format12_GLOBAL__N_131join_table_reader_debug_stringsINS0_16ColumnDefinitionEZNKS0_11TableReader12debug_stringB5cxx11EvE3$_3EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISD_EET0_ Line | Count | Source | 62 | 12 | std::string join_table_reader_debug_strings(const std::vector<T>& values, Formatter formatter) { | 63 | 12 | std::ostringstream out; | 64 | 12 | out << "["; | 65 | 14 | for (size_t i = 0; i < values.size(); ++i) { | 66 | 2 | if (i > 0) { | 67 | 1 | out << ", "; | 68 | 1 | } | 69 | 2 | out << formatter(values[i]); | 70 | 2 | } | 71 | 12 | out << "]"; | 72 | 12 | return out.str(); | 73 | 12 | } |
table_reader.cpp:_ZN5doris6format12_GLOBAL__N_131join_table_reader_debug_stringsINS0_11TableReader15FileBlockColumnEZNKS3_12debug_stringB5cxx11EvE3$_4EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISD_EET0_ Line | Count | Source | 62 | 12 | std::string join_table_reader_debug_strings(const std::vector<T>& values, Formatter formatter) { | 63 | 12 | std::ostringstream out; | 64 | 12 | out << "["; | 65 | 14 | for (size_t i = 0; i < values.size(); ++i) { | 66 | 2 | if (i > 0) { | 67 | 1 | out << ", "; | 68 | 1 | } | 69 | 2 | out << formatter(values[i]); | 70 | 2 | } | 71 | 12 | out << "]"; | 72 | 12 | return out.str(); | 73 | 12 | } |
|
74 | | |
75 | 12 | std::string file_format_to_string(FileFormat format) { |
76 | 12 | switch (format) { |
77 | 5 | case FileFormat::PARQUET: |
78 | 5 | return "PARQUET"; |
79 | 1 | case FileFormat::ORC: |
80 | 1 | return "ORC"; |
81 | 1 | case FileFormat::CSV: |
82 | 1 | return "CSV"; |
83 | 1 | case FileFormat::JSON: |
84 | 1 | return "JSON"; |
85 | 1 | case FileFormat::TEXT: |
86 | 1 | return "TEXT"; |
87 | 1 | case FileFormat::JNI: |
88 | 1 | return "JNI"; |
89 | 1 | case FileFormat::NATIVE: |
90 | 1 | return "NATIVE"; |
91 | 1 | case FileFormat::ARROW: |
92 | 1 | return "ARROW"; |
93 | 0 | case FileFormat::WAL: |
94 | 0 | return "WAL"; |
95 | 12 | } |
96 | 0 | return "UNKNOWN"; |
97 | 12 | } |
98 | | |
99 | 12 | std::string push_down_agg_to_string(TPushAggOp::type op) { |
100 | 12 | switch (op) { |
101 | 8 | case TPushAggOp::NONE: |
102 | 8 | return "NONE"; |
103 | 1 | case TPushAggOp::COUNT: |
104 | 1 | return "COUNT"; |
105 | 1 | case TPushAggOp::MINMAX: |
106 | 1 | return "MINMAX"; |
107 | 1 | case TPushAggOp::MIX: |
108 | 1 | return "MIX"; |
109 | 1 | case TPushAggOp::COUNT_ON_INDEX: |
110 | 1 | return "COUNT_ON_INDEX"; |
111 | 12 | } |
112 | 0 | return "UNKNOWN"; |
113 | 12 | } |
114 | | |
115 | 12 | std::string current_file_debug_string(const std::unique_ptr<ScanTask>& task) { |
116 | 12 | if (task == nullptr || task->data_file == nullptr) { |
117 | 11 | return "null"; |
118 | 11 | } |
119 | 1 | const auto& file = *task->data_file; |
120 | 1 | std::ostringstream out; |
121 | 1 | out << "FileDescription{path=" << file.path << ", file_size=" << file.file_size |
122 | 1 | << ", range_start_offset=" << file.range_start_offset << ", range_size=" << file.range_size |
123 | 1 | << ", mtime=" << file.mtime << ", fs_name=" << file.fs_name |
124 | 1 | << ", is_immutable=" << file.is_immutable |
125 | 1 | << ", file_cache_admission=" << file.file_cache_admission << "}"; |
126 | 1 | return out.str(); |
127 | 12 | } |
128 | | |
129 | 12 | std::string partition_values_debug_string(const std::map<std::string, Field>& partition_values) { |
130 | 12 | std::ostringstream out; |
131 | 12 | out << "{"; |
132 | 12 | size_t idx = 0; |
133 | 12 | for (const auto& [key, _] : partition_values) { |
134 | 1 | if (idx++ > 0) { |
135 | 0 | out << ", "; |
136 | 0 | } |
137 | 1 | out << key; |
138 | 1 | } |
139 | 12 | out << "}"; |
140 | 12 | return out.str(); |
141 | 12 | } |
142 | | |
143 | 121 | const schema::external::TField* get_field_ptr(const schema::external::TFieldPtr& field_ptr) { |
144 | 121 | if (!field_ptr.__isset.field_ptr || field_ptr.field_ptr == nullptr) { |
145 | 0 | return nullptr; |
146 | 0 | } |
147 | 121 | return field_ptr.field_ptr.get(); |
148 | 121 | } |
149 | | |
150 | | const schema::external::TField* find_external_field_by_id( |
151 | 11 | const schema::external::TStructField* root, int32_t field_id) { |
152 | 11 | if (root == nullptr || !root->__isset.fields) { |
153 | 0 | return nullptr; |
154 | 0 | } |
155 | 21 | for (const auto& field_ptr : root->fields) { |
156 | 21 | const auto* field = get_field_ptr(field_ptr); |
157 | 21 | if (field == nullptr) { |
158 | 0 | continue; |
159 | 0 | } |
160 | 21 | if (field->__isset.id && field->id == field_id) { |
161 | 10 | return field; |
162 | 10 | } |
163 | 11 | if (!field->__isset.nestedField) { |
164 | 11 | continue; |
165 | 11 | } |
166 | 0 | if (field->nestedField.__isset.struct_field) { |
167 | 0 | if (const auto* result = |
168 | 0 | find_external_field_by_id(&field->nestedField.struct_field, field_id); |
169 | 0 | result != nullptr) { |
170 | 0 | return result; |
171 | 0 | } |
172 | 0 | } else if (field->nestedField.__isset.array_field && |
173 | 0 | field->nestedField.array_field.__isset.item_field) { |
174 | 0 | const auto* child = get_field_ptr(field->nestedField.array_field.item_field); |
175 | 0 | if (child != nullptr) { |
176 | 0 | schema::external::TStructField child_root; |
177 | 0 | child_root.__set_fields({field->nestedField.array_field.item_field}); |
178 | 0 | if (const auto* result = find_external_field_by_id(&child_root, field_id); |
179 | 0 | result != nullptr) { |
180 | 0 | return result; |
181 | 0 | } |
182 | 0 | } |
183 | 0 | } else if (field->nestedField.__isset.map_field) { |
184 | 0 | schema::external::TStructField child_root; |
185 | 0 | std::vector<schema::external::TFieldPtr> children; |
186 | 0 | if (field->nestedField.map_field.__isset.key_field) { |
187 | 0 | children.push_back(field->nestedField.map_field.key_field); |
188 | 0 | } |
189 | 0 | if (field->nestedField.map_field.__isset.value_field) { |
190 | 0 | children.push_back(field->nestedField.map_field.value_field); |
191 | 0 | } |
192 | 0 | child_root.__set_fields(children); |
193 | 0 | if (const auto* result = find_external_field_by_id(&child_root, field_id); |
194 | 0 | result != nullptr) { |
195 | 0 | return result; |
196 | 0 | } |
197 | 0 | } |
198 | 0 | } |
199 | 1 | return nullptr; |
200 | 11 | } |
201 | | |
202 | | bool find_column_identity_path_by_id(const std::vector<ColumnDefinition>& fields, int32_t field_id, |
203 | 11 | std::vector<ColumnDefinition>* path) { |
204 | 11 | DORIS_CHECK(path != nullptr); |
205 | 11 | for (const auto& field : fields) { |
206 | 11 | path->push_back(field); |
207 | 11 | if (field.has_identifier_field_id() && field.get_identifier_field_id() == field_id) { |
208 | 6 | return true; |
209 | 6 | } |
210 | 5 | if (find_column_identity_path_by_id(field.children, field_id, path)) { |
211 | 0 | return true; |
212 | 0 | } |
213 | 5 | path->pop_back(); |
214 | 5 | } |
215 | 5 | return false; |
216 | 11 | } |
217 | | |
218 | 39 | ColumnDefinition build_schema_identity_from_external_field(const schema::external::TField& field) { |
219 | 39 | ColumnDefinition identity; |
220 | 39 | if (field.__isset.id) { |
221 | 39 | identity.identifier = Field::create_field<TYPE_INT>(field.id); |
222 | 39 | } |
223 | 39 | identity.name = field.__isset.name ? field.name : ""; |
224 | 39 | identity.name_mapping = |
225 | 39 | field.__isset.name_mapping ? field.name_mapping : std::vector<std::string> {}; |
226 | 39 | identity.has_name_mapping = |
227 | 39 | field.__isset.name_mapping_is_authoritative && field.name_mapping_is_authoritative; |
228 | 39 | if (!field.__isset.nestedField) { |
229 | 37 | return identity; |
230 | 37 | } |
231 | 2 | if (field.nestedField.__isset.struct_field && field.nestedField.struct_field.__isset.fields) { |
232 | 4 | for (const auto& child_ptr : field.nestedField.struct_field.fields) { |
233 | 4 | if (const auto* child = get_field_ptr(child_ptr); child != nullptr) { |
234 | 4 | identity.children.push_back(build_schema_identity_from_external_field(*child)); |
235 | 4 | } |
236 | 4 | } |
237 | 2 | } else if (field.nestedField.__isset.array_field && |
238 | 0 | field.nestedField.array_field.__isset.item_field) { |
239 | 0 | if (const auto* child = get_field_ptr(field.nestedField.array_field.item_field); |
240 | 0 | child != nullptr) { |
241 | 0 | identity.children.push_back(build_schema_identity_from_external_field(*child)); |
242 | 0 | identity.children.back().name = "element"; |
243 | 0 | } |
244 | 0 | } else if (field.nestedField.__isset.map_field) { |
245 | 0 | if (field.nestedField.map_field.__isset.key_field) { |
246 | 0 | if (const auto* child = get_field_ptr(field.nestedField.map_field.key_field); |
247 | 0 | child != nullptr) { |
248 | 0 | identity.children.push_back(build_schema_identity_from_external_field(*child)); |
249 | 0 | identity.children.back().name = "key"; |
250 | 0 | } |
251 | 0 | } |
252 | 0 | if (field.nestedField.map_field.__isset.value_field) { |
253 | 0 | if (const auto* child = get_field_ptr(field.nestedField.map_field.value_field); |
254 | 0 | child != nullptr) { |
255 | 0 | identity.children.push_back(build_schema_identity_from_external_field(*child)); |
256 | 0 | identity.children.back().name = "value"; |
257 | 0 | } |
258 | 0 | } |
259 | 0 | } |
260 | 2 | return identity; |
261 | 39 | } |
262 | | |
263 | | const ColumnDefinition* find_identity_child(const ColumnDefinition& projected_child, |
264 | 2 | const ColumnDefinition& identity_parent) { |
265 | 2 | const auto child_it = std::ranges::find_if( |
266 | 3 | identity_parent.children, [&](const ColumnDefinition& identity_child) { |
267 | 3 | if (projected_child.has_identifier_field_id() && |
268 | 3 | identity_child.has_identifier_field_id()) { |
269 | 3 | return projected_child.get_identifier_field_id() == |
270 | 3 | identity_child.get_identifier_field_id(); |
271 | 3 | } |
272 | 0 | if (to_lower(projected_child.name) == to_lower(identity_child.name)) { |
273 | 0 | return true; |
274 | 0 | } |
275 | 0 | return std::ranges::any_of( |
276 | 0 | identity_child.name_mapping, [&](const std::string& alias) { |
277 | 0 | return to_lower(projected_child.name) == to_lower(alias); |
278 | 0 | }); |
279 | 0 | }); |
280 | 2 | return child_it == identity_parent.children.end() ? nullptr : &*child_it; |
281 | 2 | } |
282 | | |
283 | 25 | void attach_full_schema_identity(ColumnDefinition* projected, const ColumnDefinition& identity) { |
284 | 25 | DORIS_CHECK(projected != nullptr); |
285 | | // Access-path children control materialization, but wrapper discovery needs sibling IDs that |
286 | | // were pruned from that projection. Keep the complete identity tree on a separate channel. |
287 | 25 | projected->identity_children = identity.children; |
288 | 25 | for (auto& projected_child : projected->children) { |
289 | 2 | if (const auto* identity_child = find_identity_child(projected_child, identity); |
290 | 2 | identity_child != nullptr) { |
291 | 2 | attach_full_schema_identity(&projected_child, *identity_child); |
292 | 2 | } |
293 | 2 | } |
294 | 25 | } |
295 | | |
296 | 13 | void clear_initial_default_metadata(ColumnDefinition* column) { |
297 | 13 | DORIS_CHECK(column != nullptr); |
298 | 13 | column->initial_default_value.reset(); |
299 | 13 | column->initial_default_value_is_base64 = false; |
300 | 13 | for (auto& child : column->children) { |
301 | 6 | clear_initial_default_metadata(&child); |
302 | 6 | } |
303 | 13 | } |
304 | | |
305 | 15 | bool external_field_matches_name(const schema::external::TField& field, const std::string& name) { |
306 | 15 | if (field.__isset.name && to_lower(field.name) == to_lower(name)) { |
307 | 5 | return true; |
308 | 5 | } |
309 | 10 | return field.__isset.name_mapping && |
310 | 10 | std::ranges::any_of(field.name_mapping, [&](const std::string& alias) { |
311 | 8 | return to_lower(alias) == to_lower(name); |
312 | 8 | }); |
313 | 15 | } |
314 | | |
315 | | DataTypePtr find_struct_child_type_by_external_field(const DataTypeStruct& struct_type, |
316 | | const schema::external::TField& field, |
317 | 18 | bool prefer_current_name) { |
318 | 18 | if (prefer_current_name && field.__isset.name) { |
319 | 22 | for (size_t field_idx = 0; field_idx < struct_type.get_elements().size(); ++field_idx) { |
320 | 18 | if (to_lower(field.name) == to_lower(struct_type.get_element_name(field_idx))) { |
321 | 11 | return struct_type.get_element(field_idx); |
322 | 11 | } |
323 | 18 | } |
324 | 15 | } |
325 | 10 | for (size_t field_idx = 0; field_idx < struct_type.get_elements().size(); ++field_idx) { |
326 | 8 | const auto& element_name = struct_type.get_element_name(field_idx); |
327 | 8 | if (external_field_matches_name(field, element_name)) { |
328 | 5 | return struct_type.get_element(field_idx); |
329 | 5 | } |
330 | 8 | } |
331 | 2 | return nullptr; |
332 | 7 | } |
333 | | |
334 | | DataTypePtr restore_current_primitive_type(const schema::external::TField& field, |
335 | 59 | DataTypePtr fallback_type) { |
336 | 59 | if (!field.__isset.type) { |
337 | 36 | return fallback_type; |
338 | 36 | } |
339 | 23 | DORIS_CHECK(fallback_type != nullptr); |
340 | 23 | const auto primitive_type = thrift_to_type(field.type.type); |
341 | 23 | if (is_complex_type(primitive_type)) { |
342 | 2 | return fallback_type; |
343 | 2 | } |
344 | | // The delete file can expose an older physical type, but initial defaults belong to the |
345 | | // current table field. Restore that type from FE before parsing the default and let the table |
346 | | // reader apply the normal promotion cast to the delete-key type. |
347 | 21 | return DataTypeFactory::instance().create_data_type( |
348 | 21 | primitive_type, fallback_type->is_nullable(), |
349 | 21 | field.type.__isset.precision ? field.type.precision : 0, |
350 | 21 | field.type.__isset.scale ? field.type.scale : 0, |
351 | 21 | field.type.__isset.len ? field.type.len : -1); |
352 | 23 | } |
353 | | |
354 | | // NOLINTNEXTLINE(readability-function-size): keep recursive Iceberg type reconstruction together. |
355 | | ColumnDefinition build_schema_column_from_external_field(const schema::external::TField& field, |
356 | | DataTypePtr type, |
357 | 59 | bool prefer_current_name) { |
358 | 59 | type = restore_current_primitive_type(field, std::move(type)); |
359 | 59 | ColumnDefinition column { |
360 | 59 | .identifier = field.__isset.id ? Field::create_field<TYPE_INT>(field.id) : Field {}, |
361 | 59 | .name = field.__isset.name ? field.name : "", |
362 | 59 | .name_mapping = |
363 | 59 | field.__isset.name_mapping ? field.name_mapping : std::vector<std::string> {}, |
364 | 59 | .has_name_mapping = field.__isset.name_mapping_is_authoritative && |
365 | 59 | field.name_mapping_is_authoritative, |
366 | 59 | .type = std::move(type), |
367 | 59 | .children = {}, |
368 | 59 | .default_expr = nullptr, |
369 | 59 | .initial_default_value = field.__isset.initial_default_value |
370 | 59 | ? std::make_optional(field.initial_default_value) |
371 | 59 | : std::nullopt, |
372 | 59 | .initial_default_value_is_base64 = field.__isset.initial_default_value_is_base64 && |
373 | 59 | field.initial_default_value_is_base64, |
374 | 59 | .is_optional = field.__isset.is_optional ? std::make_optional(field.is_optional) |
375 | 59 | : std::nullopt, |
376 | 59 | .is_partition_key = false, |
377 | 59 | }; |
378 | 59 | if (column.type == nullptr || !field.__isset.nestedField) { |
379 | 43 | return column; |
380 | 43 | } |
381 | | |
382 | 16 | const auto nested_type = remove_nullable(column.type); |
383 | 16 | switch (nested_type->get_primitive_type()) { |
384 | 10 | case TYPE_STRUCT: { |
385 | 10 | if (!field.nestedField.__isset.struct_field || |
386 | 10 | !field.nestedField.struct_field.__isset.fields) { |
387 | 0 | return column; |
388 | 0 | } |
389 | 10 | const auto& struct_type = assert_cast<const DataTypeStruct&>(*nested_type); |
390 | 18 | for (const auto& child_ptr : field.nestedField.struct_field.fields) { |
391 | 18 | const auto* child_field = get_field_ptr(child_ptr); |
392 | 18 | if (child_field == nullptr || !child_field->__isset.name) { |
393 | 0 | continue; |
394 | 0 | } |
395 | 18 | auto child_type = find_struct_child_type_by_external_field(struct_type, *child_field, |
396 | 18 | prefer_current_name); |
397 | 18 | if (child_type == nullptr) { |
398 | 2 | continue; |
399 | 2 | } |
400 | 16 | column.children.push_back(build_schema_column_from_external_field( |
401 | 16 | *child_field, child_type, prefer_current_name)); |
402 | 16 | } |
403 | 10 | break; |
404 | 10 | } |
405 | 3 | case TYPE_ARRAY: { |
406 | 3 | if (!field.nestedField.__isset.array_field || |
407 | 3 | !field.nestedField.array_field.__isset.item_field) { |
408 | 0 | return column; |
409 | 0 | } |
410 | 3 | const auto* item_field = get_field_ptr(field.nestedField.array_field.item_field); |
411 | 3 | if (item_field == nullptr) { |
412 | 0 | return column; |
413 | 0 | } |
414 | 3 | const auto& array_type = assert_cast<const DataTypeArray&>(*nested_type); |
415 | 3 | auto child = build_schema_column_from_external_field( |
416 | 3 | *item_field, array_type.get_nested_type(), prefer_current_name); |
417 | 3 | child.name = "element"; |
418 | 3 | if (child.has_identifier_name()) { |
419 | 0 | child.identifier = Field::create_field<TYPE_STRING>(child.name); |
420 | 0 | } |
421 | 3 | column.children.push_back(std::move(child)); |
422 | 3 | break; |
423 | 3 | } |
424 | 3 | case TYPE_MAP: { |
425 | 3 | if (!field.nestedField.__isset.map_field || |
426 | 3 | !field.nestedField.map_field.__isset.key_field || |
427 | 3 | !field.nestedField.map_field.__isset.value_field) { |
428 | 0 | return column; |
429 | 0 | } |
430 | 3 | const auto& map_type = assert_cast<const DataTypeMap&>(*nested_type); |
431 | 3 | const auto* key_field = get_field_ptr(field.nestedField.map_field.key_field); |
432 | 3 | if (key_field != nullptr) { |
433 | 3 | auto child = build_schema_column_from_external_field( |
434 | 3 | *key_field, map_type.get_key_type(), prefer_current_name); |
435 | 3 | child.name = "key"; |
436 | 3 | if (child.has_identifier_name()) { |
437 | 0 | child.identifier = Field::create_field<TYPE_STRING>(child.name); |
438 | 0 | } |
439 | 3 | column.children.push_back(std::move(child)); |
440 | 3 | } |
441 | 3 | const auto* value_field = get_field_ptr(field.nestedField.map_field.value_field); |
442 | 3 | if (value_field != nullptr) { |
443 | 3 | auto child = build_schema_column_from_external_field( |
444 | 3 | *value_field, map_type.get_value_type(), prefer_current_name); |
445 | 3 | child.name = "value"; |
446 | 3 | if (child.has_identifier_name()) { |
447 | 0 | child.identifier = Field::create_field<TYPE_STRING>(child.name); |
448 | 0 | } |
449 | 3 | column.children.push_back(std::move(child)); |
450 | 3 | } |
451 | 3 | break; |
452 | 3 | } |
453 | 0 | default: |
454 | 0 | break; |
455 | 16 | } |
456 | 16 | return column; |
457 | 16 | } |
458 | | |
459 | | const schema::external::TField* find_external_root_field(const TFileScanRangeParams* params, |
460 | 94 | const ColumnDefinition& column) { |
461 | 94 | if (params == nullptr || !params->__isset.history_schema_info || |
462 | 94 | params->history_schema_info.empty()) { |
463 | 46 | return nullptr; |
464 | 46 | } |
465 | 48 | const auto* schema = ¶ms->history_schema_info.front(); |
466 | 48 | if (params->__isset.current_schema_id) { |
467 | 49 | for (const auto& candidate_schema : params->history_schema_info) { |
468 | 49 | if (candidate_schema.__isset.schema_id && |
469 | 49 | candidate_schema.schema_id == params->current_schema_id) { |
470 | 48 | schema = &candidate_schema; |
471 | 48 | break; |
472 | 48 | } |
473 | 49 | } |
474 | 48 | } |
475 | 48 | if (!schema->__isset.root_field || !schema->root_field.__isset.fields) { |
476 | 0 | return nullptr; |
477 | 0 | } |
478 | 48 | if (!supports_iceberg_scan_semantics_v1(params)) { |
479 | | // Old BEs used one ordered current-name/alias pass. Preserve that result for old-FE plans |
480 | | // until the explicit scan-semantics marker makes exact-name precedence cluster-wide. |
481 | 7 | for (const auto& field_ptr : schema->root_field.fields) { |
482 | 7 | const auto* field = get_field_ptr(field_ptr); |
483 | 7 | if (field != nullptr && external_field_matches_name(*field, column.name)) { |
484 | 7 | return field; |
485 | 7 | } |
486 | 7 | } |
487 | 0 | return nullptr; |
488 | 7 | } |
489 | | // A reused name identifies the newly added field, not an older sibling that retained that |
490 | | // spelling as an alias. Exhaust exact current names before consulting historical aliases. |
491 | 49 | for (const auto& field_ptr : schema->root_field.fields) { |
492 | 49 | const auto* field = get_field_ptr(field_ptr); |
493 | 49 | if (field != nullptr && field->__isset.name && |
494 | 49 | to_lower(field->name) == to_lower(column.name)) { |
495 | 40 | return field; |
496 | 40 | } |
497 | 49 | } |
498 | 1 | for (const auto& field_ptr : schema->root_field.fields) { |
499 | 1 | const auto* field = get_field_ptr(field_ptr); |
500 | 1 | if (field != nullptr && field->__isset.name_mapping && |
501 | 1 | std::ranges::any_of(field->name_mapping, [&](const std::string& alias) { |
502 | 0 | return to_lower(alias) == to_lower(column.name); |
503 | 0 | })) { |
504 | 0 | return field; |
505 | 0 | } |
506 | 1 | } |
507 | 1 | return nullptr; |
508 | 1 | } |
509 | | |
510 | 2 | std::string expr_context_debug_string(const VExprContextSPtr& context) { |
511 | 2 | if (context == nullptr) { |
512 | 0 | return "null"; |
513 | 0 | } |
514 | 2 | const auto root = context->root(); |
515 | 2 | if (root == nullptr) { |
516 | 0 | return "VExprContext{root=null}"; |
517 | 0 | } |
518 | 2 | std::ostringstream out; |
519 | 2 | out << "VExprContext{root_name=" << root->expr_name() << ", root_debug=" << root->debug_string() |
520 | 2 | << "}"; |
521 | 2 | return out.str(); |
522 | 2 | } |
523 | | |
524 | 1 | std::string table_filter_debug_string(const TableFilter& filter) { |
525 | 1 | std::ostringstream out; |
526 | 1 | out << "TableFilter{conjunct=" << expr_context_debug_string(filter.conjunct) |
527 | 1 | << ", global_indices=" |
528 | 1 | << join_table_reader_debug_strings( |
529 | 1 | filter.global_indices, |
530 | 1 | [](GlobalIndex global_index) { return std::to_string(global_index.value()); }) |
531 | 1 | << "}"; |
532 | 1 | return out.str(); |
533 | 1 | } |
534 | | |
535 | 4 | bool contains_runtime_filter(const VExprContextSPtrs& conjuncts) { |
536 | 4 | return std::ranges::any_of(conjuncts, [](const auto& conjunct) { |
537 | 4 | return conjunct != nullptr && conjunct->root() != nullptr && |
538 | 4 | conjunct->root()->is_rf_wrapper(); |
539 | 4 | }); |
540 | 4 | } |
541 | | |
542 | 114 | void collect_global_indices(const VExprSPtr& expr, std::set<GlobalIndex>* global_indices) { |
543 | 114 | if (expr == nullptr) { |
544 | 0 | return; |
545 | 0 | } |
546 | 114 | if (expr->is_rf_wrapper()) { |
547 | | // RuntimeFilterExpr wraps a real predicate expression but its own thrift node can still |
548 | | // look like SLOT_REF. Collect indices from the wrapped predicate; do not cast the wrapper |
549 | | // itself to VSlotRef. |
550 | 7 | collect_global_indices(expr->get_impl(), global_indices); |
551 | 7 | return; |
552 | 7 | } |
553 | 107 | if (expr->is_slot_ref()) { |
554 | 36 | const auto* slot_ref = assert_cast<const VSlotRef*>(expr.get()); |
555 | 36 | DORIS_CHECK(slot_ref->column_id() >= 0); |
556 | 36 | global_indices->insert(GlobalIndex(cast_set<size_t>(slot_ref->column_id()))); |
557 | 36 | } |
558 | 107 | for (const auto& child : expr->children()) { |
559 | 70 | collect_global_indices(child, global_indices); |
560 | 70 | } |
561 | 107 | } |
562 | | |
563 | | Status build_table_filters_from_conjunct(const VExprContextSPtr& conjunct, RuntimeState* state, |
564 | 35 | std::vector<TableFilter>* table_filters) { |
565 | 35 | if (conjunct == nullptr) { |
566 | 0 | return Status::OK(); |
567 | 0 | } |
568 | 35 | std::set<GlobalIndex> global_indices; |
569 | 35 | collect_global_indices(conjunct->root(), &global_indices); |
570 | 35 | if (!global_indices.empty()) { |
571 | 33 | TableFilter table_filter; |
572 | 33 | VExprSPtr filter_root; |
573 | 33 | RETURN_IF_ERROR(clone_table_expr_tree(conjunct->root(), &filter_root)); |
574 | 33 | table_filter.conjunct = VExprContext::create_shared(std::move(filter_root)); |
575 | 34 | for (const auto global_index : global_indices) { |
576 | 34 | table_filter.global_indices.push_back(global_index); |
577 | 34 | } |
578 | 33 | table_filters->push_back(std::move(table_filter)); |
579 | 33 | } |
580 | 35 | return Status::OK(); |
581 | 35 | } |
582 | | |
583 | | Status parse_deletion_vector(const char* buf, size_t buffer_size, DeleteFileDesc::Format format, |
584 | 5 | DeletionVector* deletion_vector) { |
585 | 5 | DORIS_CHECK(buf != nullptr); |
586 | 5 | DORIS_CHECK(deletion_vector != nullptr); |
587 | 5 | DORIS_CHECK(format == DeleteFileDesc::Format::PAIMON || |
588 | 5 | format == DeleteFileDesc::Format::ICEBERG); |
589 | | |
590 | 5 | if (format == DeleteFileDesc::Format::PAIMON) { |
591 | 1 | RETURN_IF_ERROR(decode_paimon_deletion_vector_buffer(buf, buffer_size, deletion_vector)); |
592 | 1 | return Status::OK(); |
593 | 1 | } |
594 | | |
595 | 4 | return decode_iceberg_deletion_vector_buffer(buf, buffer_size, deletion_vector); |
596 | 5 | } |
597 | | |
598 | | } // namespace |
599 | | |
600 | | std::shared_ptr<io::FileSystemProperties> create_system_properties( |
601 | 190 | const TFileScanRangeParams* scan_params) { |
602 | 190 | auto system_properties = std::make_shared<io::FileSystemProperties>(); |
603 | 190 | if (scan_params == nullptr || !scan_params->__isset.file_type) { |
604 | 121 | system_properties->system_type = TFileType::FILE_LOCAL; |
605 | 121 | return system_properties; |
606 | 121 | } |
607 | 69 | system_properties->system_type = scan_params->file_type; |
608 | 69 | system_properties->properties = scan_params->properties; |
609 | 69 | system_properties->hdfs_params = scan_params->hdfs_params; |
610 | 69 | if (scan_params->__isset.broker_addresses) { |
611 | 0 | system_properties->broker_addresses.assign(scan_params->broker_addresses.begin(), |
612 | 0 | scan_params->broker_addresses.end()); |
613 | 0 | } |
614 | 69 | return system_properties; |
615 | 190 | } |
616 | | |
617 | 12 | std::string TableReader::debug_string() const { |
618 | 12 | std::ostringstream out; |
619 | 12 | out << "TableReader{format=" << file_format_to_string(_format) |
620 | 12 | << ", push_down_agg_type=" << push_down_agg_to_string(_push_down_agg_type) |
621 | 12 | << ", aggregate_pushdown_tried=" << _aggregate_pushdown_tried |
622 | 12 | << ", has_current_reader=" << (_data_reader.reader != nullptr) |
623 | 12 | << ", has_current_task=" << (_current_task != nullptr) |
624 | 12 | << ", current_file=" << current_file_debug_string(_current_task) |
625 | 12 | << ", has_delete_rows=" << (_delete_rows != nullptr) |
626 | 12 | << ", delete_row_count=" << (_delete_rows == nullptr ? 0 : _delete_rows->size()) |
627 | 12 | << ", has_deletion_vector=" << (_deletion_vector != nullptr) |
628 | 12 | << ", deletion_vector_cardinality=" |
629 | 12 | << (_deletion_vector == nullptr ? 0 : _deletion_vector->cardinality()) |
630 | 12 | << ", has_system_properties=" << (_system_properties != nullptr) << ", system_type=" |
631 | 12 | << (_system_properties == nullptr ? static_cast<int>(TFileType::FILE_LOCAL) |
632 | 12 | : static_cast<int>(_system_properties->system_type)) |
633 | 12 | << ", has_scan_params=" << (_scan_params != nullptr) |
634 | 12 | << ", has_io_ctx=" << (_io_ctx != nullptr) |
635 | 12 | << ", has_runtime_state=" << (_runtime_state != nullptr) |
636 | 12 | << ", has_scanner_profile=" << (_scanner_profile != nullptr) |
637 | 12 | << ", mapper_options=" << _mapper_options.debug_string() << ", projected_columns=" |
638 | 12 | << join_table_reader_debug_strings( |
639 | 12 | _projected_columns, |
640 | 12 | [](const ColumnDefinition& column) { return column.debug_string(); }) |
641 | 12 | << ", partition_values=" << partition_values_debug_string(_partition_values) |
642 | 12 | << ", table_filters=" |
643 | 12 | << join_table_reader_debug_strings( |
644 | 12 | _table_filters, |
645 | 12 | [](const TableFilter& filter) { return table_filter_debug_string(filter); }) |
646 | 12 | << ", conjunct_count=" << _conjuncts.size() << ", conjuncts=" |
647 | 12 | << join_table_reader_debug_strings(_conjuncts, |
648 | 12 | [](const VExprContextSPtr& conjunct) { |
649 | 1 | return expr_context_debug_string(conjunct); |
650 | 1 | }) |
651 | 12 | << ", file_schema=" |
652 | 12 | << join_table_reader_debug_strings( |
653 | 12 | _data_reader.file_schema, |
654 | 12 | [](const ColumnDefinition& field) { return field.debug_string(); }) |
655 | 12 | << ", file_block_layout=" |
656 | 12 | << join_table_reader_debug_strings( |
657 | 12 | _data_reader.file_block_layout, |
658 | 12 | [](const FileBlockColumn& column) { |
659 | 2 | std::ostringstream column_out; |
660 | 2 | column_out << "FileBlockColumn{file_column_id=" << column.file_column_id |
661 | 2 | << ", name=" << column.name << ", type=" |
662 | 2 | << (column.type == nullptr ? "null" : column.type->get_name()) |
663 | 2 | << "}"; |
664 | 2 | return column_out.str(); |
665 | 2 | }) |
666 | 12 | << ", block_template_columns=" << _data_reader.block_template.columns() |
667 | 12 | << ", column_mapper=" |
668 | 12 | << (_data_reader.column_mapper == nullptr ? "null" |
669 | 12 | : _data_reader.column_mapper->debug_string()) |
670 | 12 | << "}"; |
671 | 12 | return out.str(); |
672 | 12 | } |
673 | | |
674 | | Status TableReader::annotate_projected_column(const TFileScanSlotInfo& slot_info, |
675 | | ProjectedColumnBuildContext* context, |
676 | 31 | ColumnDefinition* column) const { |
677 | 31 | (void)slot_info; |
678 | 31 | DORIS_CHECK(context != nullptr); |
679 | 31 | DORIS_CHECK(column != nullptr); |
680 | 31 | context->schema_column.reset(); |
681 | 31 | const auto* schema_field = find_external_root_field(context->scan_params, *column); |
682 | 31 | if (schema_field == nullptr) { |
683 | 7 | return Status::OK(); |
684 | 7 | } |
685 | 24 | const bool use_current_semantics = supports_iceberg_scan_semantics_v1(context->scan_params); |
686 | 24 | context->schema_column = build_schema_column_from_external_field(*schema_field, column->type, |
687 | 24 | use_current_semantics); |
688 | 24 | if (!use_current_semantics) { |
689 | | // IDs and encoded defaults predate the result-changing semantics. Strip only the new |
690 | | // default channel so an old-FE plan keeps the same generic root/nested values on every BE. |
691 | 7 | clear_initial_default_metadata(&*context->schema_column); |
692 | 7 | } |
693 | 24 | column->identifier = context->schema_column->identifier; |
694 | 24 | column->name_mapping = context->schema_column->name_mapping; |
695 | 24 | column->has_name_mapping = context->schema_column->has_name_mapping; |
696 | | // Projected roots already carry a generic FE default expression, but Iceberg binary defaults |
697 | | // need the raw Base64 marker so missing-file materialization can decode rather than copy text. |
698 | 24 | column->initial_default_value = context->schema_column->initial_default_value; |
699 | 24 | column->initial_default_value_is_base64 = |
700 | 24 | context->schema_column->initial_default_value_is_base64; |
701 | 24 | return Status::OK(); |
702 | 31 | } |
703 | | |
704 | | std::optional<ColumnDefinition> TableReader::_find_table_column_by_field_id( |
705 | 11 | int32_t field_id, DataTypePtr type, bool include_historical_schemas) const { |
706 | 11 | if (_scan_params == nullptr || !_scan_params->__isset.history_schema_info || |
707 | 11 | _scan_params->history_schema_info.empty()) { |
708 | 1 | return std::nullopt; |
709 | 1 | } |
710 | 11 | const auto find_field = [field_id](const schema::external::TSchema& schema) { |
711 | 11 | return schema.__isset.root_field ? find_external_field_by_id(&schema.root_field, field_id) |
712 | 11 | : nullptr; |
713 | 11 | }; |
714 | | |
715 | 10 | const auto* current_schema = &_scan_params->history_schema_info.front(); |
716 | 10 | if (_scan_params->__isset.current_schema_id) { |
717 | 10 | for (const auto& candidate_schema : _scan_params->history_schema_info) { |
718 | 10 | if (candidate_schema.__isset.schema_id && |
719 | 10 | candidate_schema.schema_id == _scan_params->current_schema_id) { |
720 | 10 | current_schema = &candidate_schema; |
721 | 10 | break; |
722 | 10 | } |
723 | 10 | } |
724 | 10 | } |
725 | 10 | if (const auto* field = find_field(*current_schema); field != nullptr) { |
726 | 9 | return build_schema_column_from_external_field( |
727 | 9 | *field, std::move(type), supports_iceberg_scan_semantics_v1(_scan_params)); |
728 | 9 | } |
729 | 1 | if (!include_historical_schemas) { |
730 | 0 | return std::nullopt; |
731 | 0 | } |
732 | | |
733 | 1 | const schema::external::TSchema* latest_schema = nullptr; |
734 | 1 | const schema::external::TField* latest_field = nullptr; |
735 | 2 | for (const auto& candidate_schema : _scan_params->history_schema_info) { |
736 | 2 | if (&candidate_schema == current_schema) { |
737 | 1 | continue; |
738 | 1 | } |
739 | 1 | const auto* candidate_field = find_field(candidate_schema); |
740 | 1 | if (candidate_field == nullptr) { |
741 | 0 | continue; |
742 | 0 | } |
743 | 1 | if (latest_schema == nullptr || (candidate_schema.__isset.schema_id && |
744 | 0 | (!latest_schema->__isset.schema_id || |
745 | 1 | candidate_schema.schema_id > latest_schema->schema_id))) { |
746 | 1 | latest_schema = &candidate_schema; |
747 | 1 | latest_field = candidate_field; |
748 | 1 | } |
749 | 1 | } |
750 | 1 | if (latest_field == nullptr) { |
751 | 0 | return std::nullopt; |
752 | 0 | } |
753 | 1 | return build_schema_column_from_external_field( |
754 | 1 | *latest_field, std::move(type), supports_iceberg_scan_semantics_v1(_scan_params)); |
755 | 1 | } |
756 | | |
757 | | std::optional<std::vector<ColumnDefinition>> |
758 | | TableReader::_find_table_column_identity_path_by_field_id(int32_t field_id, |
759 | 7 | bool include_historical_schemas) const { |
760 | 7 | if (_scan_params == nullptr || !_scan_params->__isset.history_schema_info || |
761 | 7 | _scan_params->history_schema_info.empty()) { |
762 | 1 | return std::nullopt; |
763 | 1 | } |
764 | 6 | const auto find_path = [field_id](const schema::external::TSchema& schema) |
765 | 6 | -> std::optional<std::vector<ColumnDefinition>> { |
766 | 6 | if (!schema.__isset.root_field || !schema.root_field.__isset.fields) { |
767 | 0 | return std::nullopt; |
768 | 0 | } |
769 | 6 | std::vector<ColumnDefinition> roots; |
770 | 6 | roots.reserve(schema.root_field.fields.size()); |
771 | 12 | for (const auto& field_ptr : schema.root_field.fields) { |
772 | 12 | const auto* field = get_field_ptr(field_ptr); |
773 | 12 | if (field != nullptr) { |
774 | 12 | roots.push_back(build_schema_identity_from_external_field(*field)); |
775 | 12 | } |
776 | 12 | } |
777 | 6 | std::vector<ColumnDefinition> path; |
778 | 6 | if (find_column_identity_path_by_id(roots, field_id, &path)) { |
779 | 6 | return path; |
780 | 6 | } |
781 | 0 | return std::nullopt; |
782 | 6 | }; |
783 | | |
784 | 6 | const auto* current_schema = &_scan_params->history_schema_info.front(); |
785 | 6 | if (_scan_params->__isset.current_schema_id) { |
786 | 6 | for (const auto& candidate_schema : _scan_params->history_schema_info) { |
787 | 6 | if (candidate_schema.__isset.schema_id && |
788 | 6 | candidate_schema.schema_id == _scan_params->current_schema_id) { |
789 | 6 | current_schema = &candidate_schema; |
790 | 6 | break; |
791 | 6 | } |
792 | 6 | } |
793 | 6 | } |
794 | 6 | if (auto path = find_path(*current_schema); path.has_value()) { |
795 | 6 | return path; |
796 | 6 | } |
797 | 0 | if (!include_historical_schemas) { |
798 | 0 | return std::nullopt; |
799 | 0 | } |
800 | | |
801 | 0 | const schema::external::TSchema* latest_schema = nullptr; |
802 | 0 | std::optional<std::vector<ColumnDefinition>> latest_path; |
803 | 0 | for (const auto& candidate_schema : _scan_params->history_schema_info) { |
804 | 0 | if (&candidate_schema == current_schema) { |
805 | 0 | continue; |
806 | 0 | } |
807 | 0 | auto candidate_path = find_path(candidate_schema); |
808 | 0 | if (!candidate_path.has_value()) { |
809 | 0 | continue; |
810 | 0 | } |
811 | 0 | if (latest_schema == nullptr || (candidate_schema.__isset.schema_id && |
812 | 0 | (!latest_schema->__isset.schema_id || |
813 | 0 | candidate_schema.schema_id > latest_schema->schema_id))) { |
814 | 0 | latest_schema = &candidate_schema; |
815 | 0 | latest_path = std::move(candidate_path); |
816 | 0 | } |
817 | 0 | } |
818 | 0 | return latest_path; |
819 | 0 | } |
820 | | |
821 | 190 | Status TableReader::init(TableReadOptions&& options) { |
822 | 190 | _scanner_profile = options.scanner_profile; |
823 | 190 | if (_scanner_profile != nullptr) { |
824 | 80 | const auto hierarchy = file_scan_profile::ensure_hierarchy(_scanner_profile); |
825 | 80 | static const char* table_profile = file_scan_profile::TABLE_READER; |
826 | 80 | static const char* file_reader_profile = file_scan_profile::FILE_READER; |
827 | 80 | _profile.total_timer = hierarchy.table_reader; |
828 | 80 | _profile.file_reader_total_timer = hierarchy.file_reader; |
829 | 80 | _profile.init_timer = |
830 | 80 | ADD_CHILD_TIMER_WITH_LEVEL(_scanner_profile, "InitTime", table_profile, 1); |
831 | 80 | _profile.num_delete_files = ADD_CHILD_COUNTER_WITH_LEVEL(_scanner_profile, "NumDeleteFiles", |
832 | 80 | TUnit::UNIT, table_profile, 1); |
833 | 80 | _profile.num_delete_rows = ADD_CHILD_COUNTER_WITH_LEVEL(_scanner_profile, "NumDeleteRows", |
834 | 80 | TUnit::UNIT, table_profile, 1); |
835 | 80 | _profile.parse_delete_file_time = ADD_CHILD_TIMER_WITH_LEVEL( |
836 | 80 | _scanner_profile, "ParseDeleteFileTime", table_profile, 1); |
837 | 80 | _profile.decoded_dv_cache_hit_count = |
838 | 80 | ADD_CHILD_COUNTER_WITH_LEVEL(_scanner_profile, "DeletionVectorDecodedCacheHitCount", |
839 | 80 | TUnit::UNIT, table_profile, 1); |
840 | 80 | _profile.decoded_dv_cache_miss_count = ADD_CHILD_COUNTER_WITH_LEVEL( |
841 | 80 | _scanner_profile, "DeletionVectorDecodedCacheMissCount", TUnit::UNIT, table_profile, |
842 | 80 | 1); |
843 | 80 | _profile.dv_file_cache_hit_count = ADD_CHILD_COUNTER_WITH_LEVEL( |
844 | 80 | _scanner_profile, "DeletionVectorFileCacheHitCount", TUnit::UNIT, table_profile, 1); |
845 | 80 | _profile.dv_file_cache_miss_count = |
846 | 80 | ADD_CHILD_COUNTER_WITH_LEVEL(_scanner_profile, "DeletionVectorFileCacheMissCount", |
847 | 80 | TUnit::UNIT, table_profile, 1); |
848 | 80 | _profile.dv_file_cache_peer_read_count = ADD_CHILD_COUNTER_WITH_LEVEL( |
849 | 80 | _scanner_profile, "DeletionVectorFileCachePeerReadCount", TUnit::UNIT, |
850 | 80 | table_profile, 1); |
851 | 80 | _profile.exec_timer = |
852 | 80 | ADD_CHILD_TIMER_WITH_LEVEL(_scanner_profile, "GetBlockTime", table_profile, 1); |
853 | 80 | _profile.prepare_split_timer = |
854 | 80 | ADD_CHILD_TIMER_WITH_LEVEL(_scanner_profile, "PrepareSplitTime", table_profile, 1); |
855 | 80 | _profile.finalize_timer = |
856 | 80 | ADD_CHILD_TIMER_WITH_LEVEL(_scanner_profile, "FinalizeBlockTime", table_profile, 1); |
857 | 80 | _profile.create_reader_timer = |
858 | 80 | ADD_CHILD_TIMER_WITH_LEVEL(_scanner_profile, "CreateReaderTime", table_profile, 1); |
859 | 80 | _profile.pushdown_agg_timer = |
860 | 80 | ADD_CHILD_TIMER_WITH_LEVEL(_scanner_profile, "PushDownAggTime", table_profile, 1); |
861 | 80 | _profile.open_reader_timer = |
862 | 80 | ADD_CHILD_TIMER_WITH_LEVEL(_scanner_profile, "OpenReaderTime", table_profile, 1); |
863 | 80 | _profile.runtime_filter_partition_prune_timer = ADD_CHILD_TIMER_WITH_LEVEL( |
864 | 80 | _scanner_profile, "FileScannerRuntimeFilterPartitionPruningTime", table_profile, 1); |
865 | 80 | _profile.runtime_filter_partition_pruned_range_counter = ADD_CHILD_COUNTER_WITH_LEVEL( |
866 | 80 | _scanner_profile, "RuntimeFilterPartitionPrunedRangeNum", TUnit::UNIT, |
867 | 80 | table_profile, 1); |
868 | 80 | _profile.close_timer = |
869 | 80 | ADD_CHILD_TIMER_WITH_LEVEL(_scanner_profile, "CloseTime", table_profile, 1); |
870 | | // Lifecycle timer names remain globally unique because RuntimeProfile's visual hierarchy |
871 | | // does not namespace counters that share the same display parent. |
872 | 80 | _profile.file_reader_init_timer = ADD_CHILD_TIMER_WITH_LEVEL( |
873 | 80 | _scanner_profile, "FileReaderInitTime", file_reader_profile, 1); |
874 | 80 | _profile.file_reader_schema_timer = ADD_CHILD_TIMER_WITH_LEVEL( |
875 | 80 | _scanner_profile, "FileReaderGetSchemaTime", file_reader_profile, 1); |
876 | 80 | _profile.file_reader_mapper_timer = ADD_CHILD_TIMER_WITH_LEVEL( |
877 | 80 | _scanner_profile, "FileReaderCreateColumnMapperTime", file_reader_profile, 1); |
878 | 80 | _profile.file_reader_open_timer = ADD_CHILD_TIMER_WITH_LEVEL( |
879 | 80 | _scanner_profile, "FileReaderOpenTime", file_reader_profile, 1); |
880 | 80 | _profile.file_reader_get_block_timer = ADD_CHILD_TIMER_WITH_LEVEL( |
881 | 80 | _scanner_profile, "FileReaderGetBlockTime", file_reader_profile, 1); |
882 | 80 | _profile.file_reader_aggregate_timer = ADD_CHILD_TIMER_WITH_LEVEL( |
883 | 80 | _scanner_profile, "FileReaderAggregatePushDownTime", file_reader_profile, 1); |
884 | 80 | _profile.file_reader_close_timer = ADD_CHILD_TIMER_WITH_LEVEL( |
885 | 80 | _scanner_profile, "FileReaderCloseTime", file_reader_profile, 1); |
886 | 80 | } |
887 | | // Establish lifecycle timers before consuming options or constructing filesystem properties; |
888 | | // placing these scopes at the tail records only scope teardown and hides expensive init work. |
889 | 190 | SCOPED_TIMER(_profile.total_timer); |
890 | 190 | SCOPED_TIMER(_profile.init_timer); |
891 | 190 | _scan_params = options.scan_params; |
892 | 190 | _format = options.format; |
893 | 190 | _io_ctx = options.io_ctx; |
894 | 190 | _runtime_state = options.runtime_state; |
895 | 190 | _file_slot_descs = options.file_slot_descs; |
896 | 190 | _push_down_agg_type = options.push_down_agg_type; |
897 | 190 | _push_down_count_columns = options.push_down_count_columns; |
898 | 190 | _initial_condition_cache_digest = options.condition_cache_digest; |
899 | 190 | _condition_cache_digest = _initial_condition_cache_digest; |
900 | 190 | _projected_columns = std::move(options.projected_columns); |
901 | 190 | if (supports_iceberg_scan_semantics_v1(_scan_params)) { |
902 | 63 | for (auto& projected_column : _projected_columns) { |
903 | 63 | const auto* schema_field = find_external_root_field(_scan_params, projected_column); |
904 | 63 | if (schema_field != nullptr) { |
905 | 23 | attach_full_schema_identity( |
906 | 23 | &projected_column, |
907 | 23 | build_schema_identity_from_external_field(*schema_field)); |
908 | 23 | } |
909 | 63 | } |
910 | 55 | } |
911 | 190 | _system_properties = create_system_properties(_scan_params); |
912 | 190 | _mapper_options.mode = TableColumnMappingMode::BY_NAME; |
913 | 190 | _conjuncts = std::move(options.conjuncts); |
914 | 190 | return Status::OK(); |
915 | 190 | } |
916 | | |
917 | 134 | Status TableReader::_build_table_filters_from_conjuncts() { |
918 | 134 | _table_filters.clear(); |
919 | 134 | _constant_pruning_safe_filter_count = 0; |
920 | 134 | bool in_safe_prefix = true; |
921 | 134 | for (const auto& conjunct : _conjuncts) { |
922 | 35 | DORIS_CHECK(conjunct != nullptr); |
923 | 35 | DORIS_CHECK(conjunct->root() != nullptr); |
924 | | // `_table_filters` omits expressions without slot references, but such an expression still |
925 | | // occupies a position in the row-level conjunct order. Record how many localized filters |
926 | | // precede the first unsafe original conjunct so constant pruning cannot jump over a |
927 | | // slotless non-deterministic/error-preserving barrier. Unsafe predicates remain solely on |
928 | | // Scanner's original row-level path because localizing a clone would execute their state |
929 | | // twice with independent state. |
930 | 35 | if (in_safe_prefix && !_is_safe_to_pre_execute(conjunct)) { |
931 | 5 | in_safe_prefix = false; |
932 | 5 | } |
933 | 35 | RETURN_IF_ERROR( |
934 | 35 | build_table_filters_from_conjunct(conjunct, _runtime_state, &_table_filters)); |
935 | 35 | if (in_safe_prefix) { |
936 | 28 | _constant_pruning_safe_filter_count = _table_filters.size(); |
937 | 28 | } |
938 | 35 | } |
939 | 134 | return Status::OK(); |
940 | 134 | } |
941 | | |
942 | 132 | Status TableReader::_open_local_filter_exprs(const FileScanRequest& file_request) { |
943 | 132 | RowDescriptor row_desc; |
944 | 132 | for (const auto& conjunct : file_request.conjuncts) { |
945 | 24 | RETURN_IF_ERROR(conjunct->prepare(_runtime_state, row_desc)); |
946 | 24 | RETURN_IF_ERROR(conjunct->open(_runtime_state)); |
947 | 24 | } |
948 | 132 | for (const auto& delete_conjunct : file_request.delete_conjuncts) { |
949 | 43 | RETURN_IF_ERROR(delete_conjunct->prepare(_runtime_state, row_desc)); |
950 | 43 | RETURN_IF_ERROR(delete_conjunct->open(_runtime_state)); |
951 | 43 | } |
952 | 132 | return Status::OK(); |
953 | 132 | } |
954 | | |
955 | 132 | bool TableReader::_should_enable_condition_cache(const FileScanRequest& file_request) const { |
956 | 132 | if (_condition_cache_digest == 0 || _push_down_agg_type == TPushAggOp::type::COUNT || |
957 | 132 | _current_file_description == std::nullopt || _data_reader.reader == nullptr) { |
958 | 125 | return false; |
959 | 125 | } |
960 | | // Condition cache is populated by file readers after evaluating file-local row-level |
961 | | // conjuncts. Metadata pruning can skip row groups/pages, but it does not produce a per-row |
962 | | // survivor bitmap that can safely populate the cache. |
963 | 7 | if (file_request.conjuncts.empty()) { |
964 | 1 | return false; |
965 | 1 | } |
966 | | // Delete files/deletion vectors are table-format state. They may change independently of the |
967 | | // data file path/mtime/size used by the external cache key, so caching their result can become |
968 | | // stale. Keep delete filtering enabled, but do not read or write condition cache. |
969 | 6 | if (_delete_rows != nullptr || _deletion_vector != nullptr || |
970 | 6 | !file_request.delete_conjuncts.empty()) { |
971 | 1 | return false; |
972 | 1 | } |
973 | | // Only scanner-driven splits provide a digest rebuilt from the exact RF snapshot. Keep the |
974 | | // conservative behavior for standalone TableReader callers: their initial digest may describe |
975 | | // only static predicate P and must not store P AND RF under that key. |
976 | 5 | return _condition_cache_digest_covers_current_split || |
977 | 5 | !contains_runtime_filter(file_request.conjuncts); |
978 | 6 | } |
979 | | |
980 | 132 | Status TableReader::_init_reader_condition_cache(const FileScanRequest& file_request) { |
981 | 132 | _condition_cache = nullptr; |
982 | 132 | _condition_cache_ctx = nullptr; |
983 | 132 | if (!_should_enable_condition_cache(file_request)) { |
984 | 128 | return Status::OK(); |
985 | 128 | } |
986 | | |
987 | 4 | auto* cache = segment_v2::ConditionCache::instance(); |
988 | 4 | if (cache == nullptr) { |
989 | 0 | return Status::OK(); |
990 | 0 | } |
991 | 4 | const auto& file = *_current_file_description; |
992 | 4 | _condition_cache_key = segment_v2::ConditionCache::ExternalCacheKey( |
993 | 4 | file.path, file.mtime, file.file_size, _condition_cache_digest, file.range_start_offset, |
994 | 4 | file.range_size, |
995 | 4 | segment_v2::ConditionCache::ExternalCacheKey::BASE_GRANULE_AWARE_VERSION); |
996 | | |
997 | 4 | segment_v2::ConditionCacheHandle handle; |
998 | 4 | const bool condition_cache_hit = cache->lookup(_condition_cache_key, &handle); |
999 | 4 | if (condition_cache_hit) { |
1000 | 0 | _condition_cache = handle.get_filter_result(); |
1001 | 0 | ++_condition_cache_hit_count; |
1002 | 4 | } else { |
1003 | 4 | const int64_t total_rows = _data_reader.reader->get_total_rows(); |
1004 | 4 | if (total_rows <= 0) { |
1005 | 0 | return Status::OK(); |
1006 | 0 | } |
1007 | | // Add one guard granule for split ranges that start in the middle of a granule. A guard |
1008 | | // false bit beyond the real range never overlaps real rows, but avoids boundary overflow |
1009 | | // when a reader marks the last partial granule. |
1010 | 4 | const size_t num_granules = (total_rows + ConditionCacheContext::GRANULE_SIZE - 1) / |
1011 | 4 | ConditionCacheContext::GRANULE_SIZE; |
1012 | 4 | _condition_cache = std::make_shared<std::vector<bool>>(num_granules + 1, false); |
1013 | 4 | } |
1014 | | |
1015 | 4 | if (_condition_cache != nullptr) { |
1016 | 4 | _condition_cache_ctx = std::make_shared<ConditionCacheContext>(); |
1017 | 4 | _condition_cache_ctx->is_hit = condition_cache_hit; |
1018 | 4 | _condition_cache_ctx->filter_result = _condition_cache; |
1019 | 4 | _condition_cache_ctx->num_granules = _condition_cache->size(); |
1020 | 4 | if (condition_cache_hit) { |
1021 | 0 | _condition_cache_ctx->base_granule = handle.get_base_granule(); |
1022 | 0 | } |
1023 | 4 | _data_reader.reader->set_condition_cache_context(_condition_cache_ctx); |
1024 | 4 | } |
1025 | 4 | return Status::OK(); |
1026 | 4 | } |
1027 | | |
1028 | 134 | void TableReader::_finalize_reader_condition_cache() { |
1029 | 134 | if (_condition_cache_ctx == nullptr || _condition_cache_ctx->is_hit) { |
1030 | 130 | _condition_cache = nullptr; |
1031 | 130 | _condition_cache_ctx = nullptr; |
1032 | 130 | return; |
1033 | 130 | } |
1034 | | // LIMIT or scanner cancellation may close a reader before all selected row ranges are visited. |
1035 | | // Unvisited granules remain false in a MISS bitmap, so inserting a partial bitmap would make a |
1036 | | // later HIT skip valid rows. Only publish cache entries after the physical reader reaches EOF. |
1037 | 4 | if (!_current_reader_reached_eof) { |
1038 | 2 | _condition_cache = nullptr; |
1039 | 2 | _condition_cache_ctx = nullptr; |
1040 | 2 | return; |
1041 | 2 | } |
1042 | 2 | DORIS_CHECK(_condition_cache_ctx->num_granules <= _condition_cache->size()); |
1043 | 2 | _condition_cache->resize(_condition_cache_ctx->num_granules); |
1044 | 2 | segment_v2::ConditionCache::instance()->insert( |
1045 | 2 | _condition_cache_key, std::move(_condition_cache), _condition_cache_ctx->base_granule); |
1046 | 2 | _condition_cache = nullptr; |
1047 | 2 | _condition_cache_ctx = nullptr; |
1048 | 2 | } |
1049 | | |
1050 | 181 | Status TableReader::create_next_reader(bool* eos) { |
1051 | 181 | SCOPED_TIMER(_profile.create_reader_timer); |
1052 | 181 | DCHECK(_data_reader.reader == nullptr); |
1053 | 181 | if (_current_task == nullptr) { |
1054 | 46 | *eos = true; |
1055 | 46 | return Status::OK(); |
1056 | 46 | } |
1057 | | |
1058 | 135 | RETURN_IF_ERROR(create_file_reader(&_data_reader.reader)); |
1059 | 135 | DORIS_CHECK(_data_reader.reader != nullptr); |
1060 | 135 | if (_batch_size > 0) { |
1061 | 0 | _data_reader.reader->set_batch_size(_batch_size); |
1062 | 0 | } |
1063 | 135 | Status st; |
1064 | 135 | { |
1065 | 135 | SCOPED_TIMER(_profile.file_reader_total_timer); |
1066 | 135 | SCOPED_TIMER(_profile.file_reader_init_timer); |
1067 | 135 | st = _data_reader.reader->init(_runtime_state); |
1068 | 135 | } |
1069 | 135 | if (!st.ok()) { |
1070 | 1 | if (_io_ctx != nullptr && _io_ctx->should_stop && st.is<ErrorCode::END_OF_FILE>()) { |
1071 | 0 | *eos = true; |
1072 | 0 | _data_reader.reader.reset(); |
1073 | 0 | return Status::OK(); |
1074 | 0 | } |
1075 | 1 | return st; |
1076 | 1 | } |
1077 | 134 | st = open_reader(); |
1078 | 134 | if (!st.ok()) { |
1079 | 1 | if (_io_ctx != nullptr && _io_ctx->should_stop && st.is<ErrorCode::END_OF_FILE>()) { |
1080 | 0 | *eos = true; |
1081 | 0 | _data_reader.reader.reset(); |
1082 | 0 | return Status::OK(); |
1083 | 0 | } |
1084 | 1 | return st; |
1085 | 1 | } |
1086 | 133 | if (_data_reader.reader == nullptr) { |
1087 | 1 | *eos = _current_task == nullptr; |
1088 | 1 | return Status::OK(); |
1089 | 1 | } |
1090 | 132 | *eos = false; |
1091 | 132 | return Status::OK(); |
1092 | 133 | } |
1093 | | |
1094 | 108 | Status TableReader::create_file_reader(std::unique_ptr<FileReader>* reader) { |
1095 | 108 | DORIS_CHECK(reader != nullptr); |
1096 | 108 | const bool enable_mapping_timestamp_tz = _scan_params != nullptr && |
1097 | 108 | _scan_params->__isset.enable_mapping_timestamp_tz && |
1098 | 108 | _scan_params->enable_mapping_timestamp_tz; |
1099 | 108 | const bool enable_mapping_varbinary = _scan_params != nullptr && |
1100 | 108 | _scan_params->__isset.enable_mapping_varbinary && |
1101 | 108 | _scan_params->enable_mapping_varbinary; |
1102 | 108 | if (_format == FileFormat::PARQUET) { |
1103 | | // V2 must honor the scan contract directly; otherwise Hive STRING columns backed by an |
1104 | | // unannotated BYTE_ARRAY are silently exposed as VARBINARY and predicate bytes no longer |
1105 | | // match the table type. |
1106 | 103 | *reader = std::make_unique<format::parquet::ParquetReader>( |
1107 | 103 | _system_properties, _current_task->data_file, _io_ctx, _scanner_profile, |
1108 | 103 | _global_rowid_context, enable_mapping_timestamp_tz, enable_mapping_varbinary); |
1109 | 103 | return Status::OK(); |
1110 | 103 | } |
1111 | 5 | if (_format == FileFormat::ORC) { |
1112 | 5 | *reader = std::make_unique<format::orc::OrcReader>( |
1113 | 5 | _system_properties, _current_task->data_file, _io_ctx, _scanner_profile, |
1114 | 5 | _global_rowid_context, enable_mapping_timestamp_tz); |
1115 | 5 | return Status::OK(); |
1116 | 5 | } |
1117 | 0 | if (_format == FileFormat::CSV) { |
1118 | 0 | if (_file_slot_descs == nullptr) { |
1119 | 0 | return Status::InvalidArgument("CSV reader requires file slot descriptors"); |
1120 | 0 | } |
1121 | | // CSV has no embedded schema. TableReader owns table-level mapping, while CsvReader needs |
1122 | | // only the physical file slots plus scan text parameters to build a file-local schema. |
1123 | | // Non-file columns such as partitions/defaults/virtual row ids are intentionally excluded |
1124 | | // from `_file_slot_descs` and are materialized during finalize_chunk(). |
1125 | 0 | *reader = std::make_unique<format::csv::CsvReader>( |
1126 | 0 | _system_properties, _current_task->data_file, _io_ctx, _scanner_profile, |
1127 | 0 | _scan_params, *_file_slot_descs, _current_range_compress_type, |
1128 | 0 | _current_range_load_id); |
1129 | 0 | return Status::OK(); |
1130 | 0 | } |
1131 | 0 | if (_format == FileFormat::TEXT) { |
1132 | 0 | if (_file_slot_descs == nullptr) { |
1133 | 0 | return Status::InvalidArgument("Text reader requires file slot descriptors"); |
1134 | 0 | } |
1135 | | // Text files have no embedded schema. As with CSV, TableReader handles table-level mapping |
1136 | | // and only passes physical file slots to the v2 TextReader. |
1137 | 0 | *reader = std::make_unique<format::text::TextReader>( |
1138 | 0 | _system_properties, _current_task->data_file, _io_ctx, _scanner_profile, |
1139 | 0 | _scan_params, *_file_slot_descs, _current_range_compress_type, |
1140 | 0 | _current_range_load_id); |
1141 | 0 | return Status::OK(); |
1142 | 0 | } |
1143 | 0 | if (_format == FileFormat::JSON) { |
1144 | 0 | if (_file_slot_descs == nullptr) { |
1145 | 0 | return Status::InvalidArgument("JSON reader requires file slot descriptors"); |
1146 | 0 | } |
1147 | 0 | *reader = std::make_unique<format::json::JsonReader>( |
1148 | 0 | _system_properties, _current_task->data_file, _io_ctx, _scanner_profile, |
1149 | 0 | _scan_params, _current_file_range_desc, *_file_slot_descs, |
1150 | 0 | _current_range_compress_type, _current_range_load_id); |
1151 | 0 | return Status::OK(); |
1152 | 0 | } |
1153 | 0 | if (_format == FileFormat::NATIVE) { |
1154 | 0 | *reader = std::make_unique<format::native::NativeReader>( |
1155 | 0 | _system_properties, _current_task->data_file, _io_ctx, _scanner_profile); |
1156 | 0 | return Status::OK(); |
1157 | 0 | } |
1158 | 0 | return Status::NotSupported("TableReader does not support file format {}", |
1159 | 0 | file_format_to_string(_format)); |
1160 | 0 | } |
1161 | | |
1162 | 166 | std::unique_ptr<io::FileDescription> create_file_description(const TFileRangeDesc& range) { |
1163 | 166 | auto file_description = std::make_unique<io::FileDescription>(); |
1164 | 166 | file_description->path = range.path; |
1165 | 166 | file_description->file_size = range.__isset.file_size ? range.file_size : -1; |
1166 | 166 | file_description->mtime = range.__isset.modification_time ? range.modification_time : 0; |
1167 | 166 | file_description->range_start_offset = range.__isset.start_offset ? range.start_offset : 0; |
1168 | 166 | file_description->range_size = range.__isset.size ? range.size : -1; |
1169 | 166 | if (range.__isset.fs_name) { |
1170 | 5 | file_description->fs_name = range.fs_name; |
1171 | 5 | } |
1172 | 166 | if (range.__isset.file_cache_admission) { |
1173 | 0 | file_description->file_cache_admission = range.file_cache_admission; |
1174 | 0 | } |
1175 | 166 | return file_description; |
1176 | 166 | } |
1177 | | |
1178 | 167 | Status TableReader::prepare_split(const SplitReadOptions& options) { |
1179 | 167 | SCOPED_TIMER(_profile.total_timer); |
1180 | 167 | SCOPED_TIMER(_profile.prepare_split_timer); |
1181 | 167 | _current_split_pruned = false; |
1182 | 167 | _all_runtime_filters_applied_for_split = options.all_runtime_filters_applied; |
1183 | 167 | _condition_cache_digest_covers_current_split = options.condition_cache_digest.has_value(); |
1184 | 167 | if (options.condition_cache_digest.has_value()) { |
1185 | | // The split snapshot may include RFs that arrived after TableReader::init(). Use the digest |
1186 | | // computed from that exact snapshot. Example: an initial P digest must not be used to store |
1187 | | // the bitmap for P AND late RF{7, 9}; the scanner supplies digest(P AND RF{7, 9}) here. |
1188 | 1 | _condition_cache_digest = *options.condition_cache_digest; |
1189 | 166 | } else { |
1190 | | // An explicit scanner digest is split-scoped. Restore the init-time digest when a later |
1191 | | // standalone split omits it instead of leaking the previous split's RF payload into its key. |
1192 | 166 | _condition_cache_digest = _initial_condition_cache_digest; |
1193 | 166 | } |
1194 | 167 | if (options.conjuncts.has_value()) { |
1195 | 2 | _conjuncts = *options.conjuncts; |
1196 | 2 | } |
1197 | | // Update to current split format to handle ORC/PARQUET files in one table. |
1198 | 167 | _format = options.current_split_format; |
1199 | 167 | _partition_values = std::move(options.partition_values); |
1200 | 167 | _current_task.reset(); |
1201 | 167 | _current_file_description.reset(); |
1202 | 167 | _current_file_range_desc = options.current_range; |
1203 | 167 | _current_range_compress_type = options.current_range.__isset.compress_type |
1204 | 167 | ? options.current_range.compress_type |
1205 | 167 | : TFileCompressType::UNKNOWN; |
1206 | 167 | _current_range_load_id = options.current_range.__isset.load_id |
1207 | 167 | ? std::make_optional(options.current_range.load_id) |
1208 | 167 | : std::nullopt; |
1209 | 167 | _global_rowid_context = options.global_rowid_context; |
1210 | 167 | _delete_rows = nullptr; |
1211 | 167 | _deletion_vector = nullptr; |
1212 | 167 | _aggregate_pushdown_tried = false; |
1213 | 167 | _remaining_table_level_count = -1; |
1214 | 167 | _remaining_file_level_count = -1; |
1215 | 167 | _current_split_uses_metadata_count = false; |
1216 | 167 | _current_reader_reached_eof = false; |
1217 | 167 | RETURN_IF_ERROR(_evaluate_partition_prune_conjuncts(options.partition_prune_conjuncts, |
1218 | 167 | &_current_split_pruned)); |
1219 | 167 | if (_current_split_pruned) { |
1220 | 1 | COUNTER_UPDATE(_profile.runtime_filter_partition_pruned_range_counter, 1); |
1221 | 1 | return Status::OK(); |
1222 | 1 | } |
1223 | 166 | _current_task = std::make_unique<ScanTask>(); |
1224 | 166 | _current_task->data_file = create_file_description(options.current_range); |
1225 | 166 | _current_file_description = *_current_task->data_file; |
1226 | | // A table-level row count is only equivalent to scanning the split when no row predicate is |
1227 | | // active and no predicate can arrive later. The metadata path can return several batches for |
1228 | | // one split; after its first synthetic batch there is no way to recover the real rows if a |
1229 | | // runtime filter arrives before the next scheduler turn. |
1230 | | // Table-level metadata only contains the number of rows; it cannot evaluate an expression or |
1231 | | // the NULL state of a COUNT argument. Require the new FE's explicit empty argument list, which |
1232 | | // means COUNT(*)/COUNT(1). A non-empty list means COUNT(col), while nullopt comes from an old FE |
1233 | | // whose COUNT semantics are unknown during a BE-first rolling upgrade. |
1234 | 166 | if (_push_down_agg_type == TPushAggOp::type::COUNT && _push_down_count_columns.has_value() && |
1235 | 166 | _push_down_count_columns->empty() && options.all_runtime_filters_applied && |
1236 | 166 | _conjuncts.empty() && options.current_range.__isset.table_format_params && |
1237 | 166 | options.current_range.table_format_params.__isset.table_level_row_count) { |
1238 | 3 | DORIS_CHECK(options.current_range.table_format_params.table_level_row_count >= -1); |
1239 | 3 | _remaining_table_level_count = |
1240 | 3 | options.current_range.table_format_params.table_level_row_count; |
1241 | 3 | _current_split_uses_metadata_count = _is_table_level_count_active(); |
1242 | 3 | } |
1243 | 166 | if (_is_table_level_count_active()) { |
1244 | 2 | return Status::OK(); |
1245 | 2 | } |
1246 | 164 | return _parse_delete_predicates(options); |
1247 | 166 | } |
1248 | | |
1249 | | Status TableReader::_evaluate_partition_prune_conjuncts(const VExprContextSPtrs& conjuncts, |
1250 | 167 | bool* can_filter_all) { |
1251 | 167 | DORIS_CHECK(can_filter_all != nullptr); |
1252 | 167 | SCOPED_TIMER(_profile.runtime_filter_partition_prune_timer); |
1253 | 167 | *can_filter_all = false; |
1254 | 167 | if (conjuncts.empty() || _partition_values.empty()) { |
1255 | 164 | return Status::OK(); |
1256 | 164 | } |
1257 | | |
1258 | 3 | VExprContextSPtrs partition_conjuncts; |
1259 | 3 | for (const auto& conjunct : conjuncts) { |
1260 | 3 | DORIS_CHECK(conjunct != nullptr); |
1261 | 3 | DORIS_CHECK(conjunct->root() != nullptr); |
1262 | | // Keep only the safe prefix of the original conjunct order. If an unsafe conjunct is |
1263 | | // skipped, a later predicate could prune the split before the unsafe one reaches its |
1264 | | // normal row-level evaluation point. |
1265 | 3 | if (!_is_safe_to_pre_execute(conjunct)) { |
1266 | 1 | break; |
1267 | 1 | } |
1268 | 2 | std::set<GlobalIndex> global_indices; |
1269 | 2 | collect_global_indices(conjunct->root(), &global_indices); |
1270 | 2 | if (global_indices.empty()) { |
1271 | 0 | continue; |
1272 | 0 | } |
1273 | 2 | const bool partition_only = std::ranges::all_of(global_indices, [&](GlobalIndex index) { |
1274 | 2 | if (index.value() >= _projected_columns.size()) { |
1275 | 0 | return false; |
1276 | 0 | } |
1277 | 2 | const auto& column = _projected_columns[index.value()]; |
1278 | 2 | return column.is_partition_key && |
1279 | 2 | find_partition_value(column, _partition_values) != nullptr; |
1280 | 2 | }); |
1281 | 2 | if (partition_only) { |
1282 | 2 | partition_conjuncts.push_back(conjunct); |
1283 | 2 | } |
1284 | 2 | } |
1285 | 3 | if (partition_conjuncts.empty()) { |
1286 | 1 | return Status::OK(); |
1287 | 1 | } |
1288 | | |
1289 | 2 | Block block; |
1290 | 2 | RETURN_IF_ERROR(_build_partition_prune_block(&block)); |
1291 | 2 | RowDescriptor row_desc; |
1292 | 2 | for (const auto& conjunct : partition_conjuncts) { |
1293 | 2 | RETURN_IF_ERROR(conjunct->prepare(_runtime_state, row_desc)); |
1294 | 2 | RETURN_IF_ERROR(conjunct->open(_runtime_state)); |
1295 | 2 | } |
1296 | 2 | IColumn::Filter result_filter(block.rows(), 1); |
1297 | 2 | return VExprContext::execute_conjuncts(partition_conjuncts, nullptr, &block, &result_filter, |
1298 | 2 | can_filter_all); |
1299 | 2 | } |
1300 | | |
1301 | 64 | bool TableReader::_is_safe_to_pre_execute(const VExprContextSPtr& conjunct) { |
1302 | 64 | DORIS_CHECK(conjunct != nullptr); |
1303 | 64 | DORIS_CHECK(conjunct->root() != nullptr); |
1304 | 64 | const auto root = conjunct->root(); |
1305 | 64 | const auto impl = root->get_impl(); |
1306 | 64 | const auto predicate = impl != nullptr ? impl : root; |
1307 | | // Split pruning evaluates a predicate once before any file rows are read. Reordering |
1308 | | // non-deterministic or error-preserving expressions can change their row-level semantics, |
1309 | | // even when every referenced slot is a partition column or maps to a constant entry. |
1310 | 64 | return predicate->is_safe_to_execute_on_selected_rows(); |
1311 | 64 | } |
1312 | | |
1313 | 2 | Status TableReader::_build_partition_prune_block(Block* block) const { |
1314 | 2 | DORIS_CHECK(block != nullptr); |
1315 | 2 | DORIS_CHECK(!_projected_columns.empty()); |
1316 | 2 | block->clear(); |
1317 | 2 | for (const auto& column : _projected_columns) { |
1318 | 2 | DORIS_CHECK(column.type != nullptr); |
1319 | 2 | ColumnPtr value_column = column.type->create_column_const_with_default_value(1); |
1320 | 2 | if (column.is_partition_key) { |
1321 | 2 | const auto* partition_value = find_partition_value(column, _partition_values); |
1322 | 2 | if (partition_value != nullptr) { |
1323 | 2 | value_column = column.type->create_column_const(1, *partition_value); |
1324 | 2 | } |
1325 | 2 | } |
1326 | 2 | block->insert({std::move(value_column), column.type, column.name}); |
1327 | 2 | } |
1328 | 2 | return Status::OK(); |
1329 | 2 | } |
1330 | | |
1331 | 164 | Status TableReader::_parse_delete_predicates(const SplitReadOptions& options) { |
1332 | 164 | DeleteFileDesc desc {.fs_name = options.current_range.fs_name}; |
1333 | 164 | bool has_delete_file = false; |
1334 | 164 | RETURN_IF_ERROR(_parse_deletion_vector_file(options.current_range.table_format_params, &desc, |
1335 | 164 | &has_delete_file)); |
1336 | 164 | if (has_delete_file) { |
1337 | 7 | DORIS_CHECK(options.cache != nullptr); |
1338 | 7 | Status create_status = Status::OK(); |
1339 | | |
1340 | 7 | bool decoded_cache_hit = false; |
1341 | 7 | _deletion_vector = options.cache->get<DeletionVector>( |
1342 | 7 | desc.key, |
1343 | 7 | [&]() -> DeletionVector* { |
1344 | 7 | auto deletion_vector = std::make_unique<DeletionVector>(); |
1345 | | |
1346 | 7 | DeletionVectorReader dv_reader(_runtime_state, _scanner_profile, *_scan_params, |
1347 | 7 | desc, _io_ctx.get()); |
1348 | 7 | create_status = dv_reader.open(); |
1349 | 7 | if (!create_status.ok()) [[unlikely]] { |
1350 | 0 | return nullptr; |
1351 | 0 | } |
1352 | | |
1353 | 7 | size_t bytes_read = desc.size; |
1354 | 7 | std::vector<char> buffer(bytes_read); |
1355 | 7 | DBUG_EXECUTE_IF("TableReader.parse_deletion_vector.io_error", { |
1356 | 7 | create_status = |
1357 | 7 | Status::IOError("injected format v2 deletion vector read failure"); |
1358 | 7 | return nullptr; |
1359 | 7 | }); |
1360 | 6 | DBUG_EXECUTE_IF("TableReader.parse_deletion_vector.should_stop", { |
1361 | 6 | create_status = Status::EndOfFile("stop read."); |
1362 | 6 | return nullptr; |
1363 | 6 | }); |
1364 | 5 | create_status = |
1365 | 5 | dv_reader.read_at(desc.start_offset, {buffer.data(), bytes_read}); |
1366 | 5 | const auto& file_cache_stats = dv_reader.file_cache_statistics(); |
1367 | 5 | COUNTER_UPDATE(_profile.dv_file_cache_hit_count, |
1368 | 5 | file_cache_stats.num_local_io_total); |
1369 | 5 | COUNTER_UPDATE(_profile.dv_file_cache_miss_count, |
1370 | 5 | file_cache_stats.num_remote_io_total); |
1371 | 5 | COUNTER_UPDATE(_profile.dv_file_cache_peer_read_count, |
1372 | 5 | file_cache_stats.num_peer_io_total); |
1373 | 5 | if (!create_status.ok()) [[unlikely]] { |
1374 | 0 | return nullptr; |
1375 | 0 | } |
1376 | | |
1377 | 5 | const char* buf = buffer.data(); |
1378 | 5 | SCOPED_TIMER(_profile.parse_delete_file_time); |
1379 | 5 | create_status = parse_deletion_vector(buf, bytes_read, desc.format, |
1380 | 5 | deletion_vector.get()); |
1381 | 5 | if (!create_status.ok()) [[unlikely]] { |
1382 | 1 | return nullptr; |
1383 | 1 | } |
1384 | 4 | COUNTER_UPDATE(_profile.num_delete_rows, deletion_vector->cardinality()); |
1385 | 4 | return deletion_vector.release(); |
1386 | 5 | }, |
1387 | 7 | &decoded_cache_hit); |
1388 | 7 | RETURN_IF_ERROR(create_status); |
1389 | 4 | COUNTER_UPDATE(decoded_cache_hit ? _profile.decoded_dv_cache_hit_count |
1390 | 4 | : _profile.decoded_dv_cache_miss_count, |
1391 | 4 | 1); |
1392 | 4 | } |
1393 | | |
1394 | 161 | return Status::OK(); |
1395 | 164 | } |
1396 | | } // namespace doris::format |