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/paimon_reader.h" |
45 | | #include "format_v2/column_mapper.h" |
46 | | #include "format_v2/delimited_text/csv_reader.h" |
47 | | #include "format_v2/delimited_text/text_reader.h" |
48 | | #include "format_v2/json/json_reader.h" |
49 | | #include "format_v2/native/native_reader.h" |
50 | | #include "format_v2/orc/orc_reader.h" |
51 | | #include "format_v2/parquet/parquet_reader.h" |
52 | | #include "storage/segment/condition_cache.h" |
53 | | #include "util/debug_points.h" |
54 | | #include "util/string_util.h" |
55 | | |
56 | | namespace doris::format { |
57 | | namespace { |
58 | | |
59 | | template <typename T, typename Formatter> |
60 | 61 | std::string join_table_reader_debug_strings(const std::vector<T>& values, Formatter formatter) { |
61 | 61 | std::ostringstream out; |
62 | 61 | out << "["; |
63 | 70 | for (size_t i = 0; i < values.size(); ++i) { |
64 | 9 | if (i > 0) { |
65 | 3 | out << ", "; |
66 | 3 | } |
67 | 9 | out << formatter(values[i]); |
68 | 9 | } |
69 | 61 | out << "]"; |
70 | 61 | return out.str(); |
71 | 61 | } table_reader.cpp:_ZN5doris6format12_GLOBAL__N_131join_table_reader_debug_stringsINS0_16ColumnDefinitionEZNKS0_11TableReader12debug_stringB5cxx11EvE3$_0EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISD_EET0_ Line | Count | Source | 60 | 12 | std::string join_table_reader_debug_strings(const std::vector<T>& values, Formatter formatter) { | 61 | 12 | std::ostringstream out; | 62 | 12 | out << "["; | 63 | 14 | for (size_t i = 0; i < values.size(); ++i) { | 64 | 2 | if (i > 0) { | 65 | 1 | out << ", "; | 66 | 1 | } | 67 | 2 | out << formatter(values[i]); | 68 | 2 | } | 69 | 12 | out << "]"; | 70 | 12 | return out.str(); | 71 | 12 | } |
table_reader.cpp:_ZN5doris6format12_GLOBAL__N_131join_table_reader_debug_stringsINS0_11TableFilterEZNKS0_11TableReader12debug_stringB5cxx11EvE3$_1EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISD_EET0_ Line | Count | Source | 60 | 12 | std::string join_table_reader_debug_strings(const std::vector<T>& values, Formatter formatter) { | 61 | 12 | std::ostringstream out; | 62 | 12 | out << "["; | 63 | 13 | for (size_t i = 0; i < values.size(); ++i) { | 64 | 1 | if (i > 0) { | 65 | 0 | out << ", "; | 66 | 0 | } | 67 | 1 | out << formatter(values[i]); | 68 | 1 | } | 69 | 12 | out << "]"; | 70 | 12 | return out.str(); | 71 | 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 | 60 | 1 | std::string join_table_reader_debug_strings(const std::vector<T>& values, Formatter formatter) { | 61 | 1 | std::ostringstream out; | 62 | 1 | out << "["; | 63 | 2 | for (size_t i = 0; i < values.size(); ++i) { | 64 | 1 | if (i > 0) { | 65 | 0 | out << ", "; | 66 | 0 | } | 67 | 1 | out << formatter(values[i]); | 68 | 1 | } | 69 | 1 | out << "]"; | 70 | 1 | return out.str(); | 71 | 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 | 60 | 12 | std::string join_table_reader_debug_strings(const std::vector<T>& values, Formatter formatter) { | 61 | 12 | std::ostringstream out; | 62 | 12 | out << "["; | 63 | 13 | for (size_t i = 0; i < values.size(); ++i) { | 64 | 1 | if (i > 0) { | 65 | 0 | out << ", "; | 66 | 0 | } | 67 | 1 | out << formatter(values[i]); | 68 | 1 | } | 69 | 12 | out << "]"; | 70 | 12 | return out.str(); | 71 | 12 | } |
table_reader.cpp:_ZN5doris6format12_GLOBAL__N_131join_table_reader_debug_stringsINS0_16ColumnDefinitionEZNKS0_11TableReader12debug_stringB5cxx11EvE3$_3EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISD_EET0_ Line | Count | Source | 60 | 12 | std::string join_table_reader_debug_strings(const std::vector<T>& values, Formatter formatter) { | 61 | 12 | std::ostringstream out; | 62 | 12 | out << "["; | 63 | 14 | for (size_t i = 0; i < values.size(); ++i) { | 64 | 2 | if (i > 0) { | 65 | 1 | out << ", "; | 66 | 1 | } | 67 | 2 | out << formatter(values[i]); | 68 | 2 | } | 69 | 12 | out << "]"; | 70 | 12 | return out.str(); | 71 | 12 | } |
table_reader.cpp:_ZN5doris6format12_GLOBAL__N_131join_table_reader_debug_stringsINS0_11TableReader15FileBlockColumnEZNKS3_12debug_stringB5cxx11EvE3$_4EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISD_EET0_ Line | Count | Source | 60 | 12 | std::string join_table_reader_debug_strings(const std::vector<T>& values, Formatter formatter) { | 61 | 12 | std::ostringstream out; | 62 | 12 | out << "["; | 63 | 14 | for (size_t i = 0; i < values.size(); ++i) { | 64 | 2 | if (i > 0) { | 65 | 1 | out << ", "; | 66 | 1 | } | 67 | 2 | out << formatter(values[i]); | 68 | 2 | } | 69 | 12 | out << "]"; | 70 | 12 | return out.str(); | 71 | 12 | } |
|
72 | | |
73 | 12 | std::string file_format_to_string(FileFormat format) { |
74 | 12 | switch (format) { |
75 | 5 | case FileFormat::PARQUET: |
76 | 5 | return "PARQUET"; |
77 | 1 | case FileFormat::ORC: |
78 | 1 | return "ORC"; |
79 | 1 | case FileFormat::CSV: |
80 | 1 | return "CSV"; |
81 | 1 | case FileFormat::JSON: |
82 | 1 | return "JSON"; |
83 | 1 | case FileFormat::TEXT: |
84 | 1 | return "TEXT"; |
85 | 1 | case FileFormat::JNI: |
86 | 1 | return "JNI"; |
87 | 1 | case FileFormat::NATIVE: |
88 | 1 | return "NATIVE"; |
89 | 1 | case FileFormat::ARROW: |
90 | 1 | return "ARROW"; |
91 | 12 | } |
92 | 0 | return "UNKNOWN"; |
93 | 12 | } |
94 | | |
95 | 12 | std::string push_down_agg_to_string(TPushAggOp::type op) { |
96 | 12 | switch (op) { |
97 | 8 | case TPushAggOp::NONE: |
98 | 8 | return "NONE"; |
99 | 1 | case TPushAggOp::COUNT: |
100 | 1 | return "COUNT"; |
101 | 1 | case TPushAggOp::MINMAX: |
102 | 1 | return "MINMAX"; |
103 | 1 | case TPushAggOp::MIX: |
104 | 1 | return "MIX"; |
105 | 1 | case TPushAggOp::COUNT_ON_INDEX: |
106 | 1 | return "COUNT_ON_INDEX"; |
107 | 12 | } |
108 | 0 | return "UNKNOWN"; |
109 | 12 | } |
110 | | |
111 | 12 | std::string current_file_debug_string(const std::unique_ptr<ScanTask>& task) { |
112 | 12 | if (task == nullptr || task->data_file == nullptr) { |
113 | 11 | return "null"; |
114 | 11 | } |
115 | 1 | const auto& file = *task->data_file; |
116 | 1 | std::ostringstream out; |
117 | 1 | out << "FileDescription{path=" << file.path << ", file_size=" << file.file_size |
118 | 1 | << ", range_start_offset=" << file.range_start_offset << ", range_size=" << file.range_size |
119 | 1 | << ", mtime=" << file.mtime << ", fs_name=" << file.fs_name |
120 | 1 | << ", file_cache_admission=" << file.file_cache_admission << "}"; |
121 | 1 | return out.str(); |
122 | 12 | } |
123 | | |
124 | 12 | std::string partition_values_debug_string(const std::map<std::string, Field>& partition_values) { |
125 | 12 | std::ostringstream out; |
126 | 12 | out << "{"; |
127 | 12 | size_t idx = 0; |
128 | 12 | for (const auto& [key, _] : partition_values) { |
129 | 1 | if (idx++ > 0) { |
130 | 0 | out << ", "; |
131 | 0 | } |
132 | 1 | out << key; |
133 | 1 | } |
134 | 12 | out << "}"; |
135 | 12 | return out.str(); |
136 | 12 | } |
137 | | |
138 | 679k | const schema::external::TField* get_field_ptr(const schema::external::TFieldPtr& field_ptr) { |
139 | 679k | if (!field_ptr.__isset.field_ptr || field_ptr.field_ptr == nullptr) { |
140 | 0 | return nullptr; |
141 | 0 | } |
142 | 679k | return field_ptr.field_ptr.get(); |
143 | 679k | } |
144 | | |
145 | 666k | bool external_field_matches_name(const schema::external::TField& field, const std::string& name) { |
146 | 668k | if (field.__isset.name && to_lower(field.name) == to_lower(name)) { |
147 | 77.4k | return true; |
148 | 77.4k | } |
149 | 589k | return field.__isset.name_mapping && |
150 | 589k | std::ranges::any_of(field.name_mapping, [&](const std::string& alias) { |
151 | 16 | return to_lower(alias) == to_lower(name); |
152 | 16 | }); |
153 | 666k | } |
154 | | |
155 | | DataTypePtr find_struct_child_type_by_external_field(const DataTypeStruct& struct_type, |
156 | 21.1k | const schema::external::TField& field) { |
157 | 37.4k | for (size_t field_idx = 0; field_idx < struct_type.get_elements().size(); ++field_idx) { |
158 | 35.1k | if (external_field_matches_name(field, struct_type.get_element_name(field_idx))) { |
159 | 18.9k | return struct_type.get_element(field_idx); |
160 | 18.9k | } |
161 | 35.1k | } |
162 | 2.24k | return nullptr; |
163 | 21.1k | } |
164 | | |
165 | | DataTypePtr restore_current_primitive_type(const schema::external::TField& field, |
166 | 102k | DataTypePtr fallback_type) { |
167 | 102k | if (!field.__isset.type) { |
168 | 8 | return fallback_type; |
169 | 8 | } |
170 | 102k | const auto primitive_type = thrift_to_type(field.type.type); |
171 | 102k | if (is_complex_type(primitive_type)) { |
172 | 26.3k | return fallback_type; |
173 | 26.3k | } |
174 | | // The delete file can expose an older physical type, but initial defaults belong to the |
175 | | // current table field. Restore that type from FE before parsing the default and let the table |
176 | | // reader apply the normal promotion cast to the delete-key type. |
177 | 76.1k | return DataTypeFactory::instance().create_data_type( |
178 | 76.1k | primitive_type, false, field.type.__isset.precision ? field.type.precision : 0, |
179 | 76.1k | field.type.__isset.scale ? field.type.scale : 0, |
180 | 76.1k | field.type.__isset.len ? field.type.len : -1); |
181 | 102k | } |
182 | | |
183 | | ColumnDefinition build_schema_column_from_external_field(const schema::external::TField& field, |
184 | 102k | DataTypePtr type) { |
185 | 102k | type = restore_current_primitive_type(field, std::move(type)); |
186 | 102k | ColumnDefinition column { |
187 | 18.4E | .identifier = field.__isset.id ? Field::create_field<TYPE_INT>(field.id) : Field {}, |
188 | 102k | .name = field.__isset.name ? field.name : "", |
189 | 102k | .name_mapping = |
190 | 102k | field.__isset.name_mapping ? field.name_mapping : std::vector<std::string> {}, |
191 | 102k | .type = std::move(type), |
192 | 102k | .children = {}, |
193 | 102k | .default_expr = nullptr, |
194 | 102k | .initial_default_value = field.__isset.initial_default_value |
195 | 102k | ? std::make_optional(field.initial_default_value) |
196 | 102k | : std::nullopt, |
197 | 102k | .initial_default_value_is_base64 = field.__isset.initial_default_value_is_base64 && |
198 | 102k | field.initial_default_value_is_base64, |
199 | 102k | .is_partition_key = false, |
200 | 102k | }; |
201 | 102k | if (column.type == nullptr || !field.__isset.nestedField) { |
202 | 76.1k | return column; |
203 | 76.1k | } |
204 | | |
205 | 26.3k | const auto nested_type = remove_nullable(column.type); |
206 | 26.3k | switch (nested_type->get_primitive_type()) { |
207 | 8.99k | case TYPE_STRUCT: { |
208 | 8.99k | if (!field.nestedField.__isset.struct_field || |
209 | 8.99k | !field.nestedField.struct_field.__isset.fields) { |
210 | 0 | return column; |
211 | 0 | } |
212 | 8.99k | const auto& struct_type = assert_cast<const DataTypeStruct&>(*nested_type); |
213 | 21.1k | for (const auto& child_ptr : field.nestedField.struct_field.fields) { |
214 | 21.1k | const auto* child_field = get_field_ptr(child_ptr); |
215 | 21.1k | if (child_field == nullptr || !child_field->__isset.name) { |
216 | 0 | continue; |
217 | 0 | } |
218 | 21.1k | auto child_type = find_struct_child_type_by_external_field(struct_type, *child_field); |
219 | 21.1k | if (child_type == nullptr) { |
220 | 2.24k | continue; |
221 | 2.24k | } |
222 | 18.9k | column.children.push_back( |
223 | 18.9k | build_schema_column_from_external_field(*child_field, child_type)); |
224 | 18.9k | } |
225 | 8.99k | break; |
226 | 8.99k | } |
227 | 9.74k | case TYPE_ARRAY: { |
228 | 9.74k | if (!field.nestedField.__isset.array_field || |
229 | 9.74k | !field.nestedField.array_field.__isset.item_field) { |
230 | 0 | return column; |
231 | 0 | } |
232 | 9.74k | const auto* item_field = get_field_ptr(field.nestedField.array_field.item_field); |
233 | 9.74k | if (item_field == nullptr) { |
234 | 0 | return column; |
235 | 0 | } |
236 | 9.74k | const auto& array_type = assert_cast<const DataTypeArray&>(*nested_type); |
237 | 9.74k | auto child = |
238 | 9.74k | build_schema_column_from_external_field(*item_field, array_type.get_nested_type()); |
239 | 9.74k | child.name = "element"; |
240 | 9.74k | if (child.has_identifier_name()) { |
241 | 0 | child.identifier = Field::create_field<TYPE_STRING>(child.name); |
242 | 0 | } |
243 | 9.74k | column.children.push_back(std::move(child)); |
244 | 9.74k | break; |
245 | 9.74k | } |
246 | 7.66k | case TYPE_MAP: { |
247 | 7.66k | if (!field.nestedField.__isset.map_field || |
248 | 7.66k | !field.nestedField.map_field.__isset.key_field || |
249 | 7.66k | !field.nestedField.map_field.__isset.value_field) { |
250 | 0 | return column; |
251 | 0 | } |
252 | 7.66k | const auto& map_type = assert_cast<const DataTypeMap&>(*nested_type); |
253 | 7.66k | const auto* key_field = get_field_ptr(field.nestedField.map_field.key_field); |
254 | 7.66k | if (key_field != nullptr) { |
255 | 7.66k | auto child = |
256 | 7.66k | build_schema_column_from_external_field(*key_field, map_type.get_key_type()); |
257 | 7.66k | child.name = "key"; |
258 | 7.66k | if (child.has_identifier_name()) { |
259 | 0 | child.identifier = Field::create_field<TYPE_STRING>(child.name); |
260 | 0 | } |
261 | 7.66k | column.children.push_back(std::move(child)); |
262 | 7.66k | } |
263 | 7.66k | const auto* value_field = get_field_ptr(field.nestedField.map_field.value_field); |
264 | 7.66k | if (value_field != nullptr) { |
265 | 7.66k | auto child = build_schema_column_from_external_field(*value_field, |
266 | 7.66k | map_type.get_value_type()); |
267 | 7.66k | child.name = "value"; |
268 | 7.66k | if (child.has_identifier_name()) { |
269 | 0 | child.identifier = Field::create_field<TYPE_STRING>(child.name); |
270 | 0 | } |
271 | 7.66k | column.children.push_back(std::move(child)); |
272 | 7.66k | } |
273 | 7.66k | break; |
274 | 7.66k | } |
275 | 0 | default: |
276 | 0 | break; |
277 | 26.3k | } |
278 | 26.3k | return column; |
279 | 26.3k | } |
280 | | |
281 | | const schema::external::TField* find_external_root_field(const TFileScanRangeParams* params, |
282 | 236k | const ColumnDefinition& column) { |
283 | 236k | if (params == nullptr || !params->__isset.history_schema_info || |
284 | 236k | params->history_schema_info.empty()) { |
285 | 177k | return nullptr; |
286 | 177k | } |
287 | 58.9k | const auto* schema = ¶ms->history_schema_info.front(); |
288 | 58.9k | if (params->__isset.current_schema_id) { |
289 | 58.9k | for (const auto& candidate_schema : params->history_schema_info) { |
290 | 58.9k | if (candidate_schema.__isset.schema_id && |
291 | 58.9k | candidate_schema.schema_id == params->current_schema_id) { |
292 | 58.9k | schema = &candidate_schema; |
293 | 58.9k | break; |
294 | 58.9k | } |
295 | 58.9k | } |
296 | 58.9k | } |
297 | 58.9k | if (!schema->__isset.root_field || !schema->root_field.__isset.fields) { |
298 | 0 | return nullptr; |
299 | 0 | } |
300 | 633k | for (const auto& field_ptr : schema->root_field.fields) { |
301 | 633k | const auto* field = get_field_ptr(field_ptr); |
302 | 633k | if (field == nullptr) { |
303 | 0 | continue; |
304 | 0 | } |
305 | 633k | if (external_field_matches_name(*field, column.name)) { |
306 | 58.5k | return field; |
307 | 58.5k | } |
308 | 633k | } |
309 | 372 | return nullptr; |
310 | 58.9k | } |
311 | | |
312 | 2 | std::string expr_context_debug_string(const VExprContextSPtr& context) { |
313 | 2 | if (context == nullptr) { |
314 | 0 | return "null"; |
315 | 0 | } |
316 | 2 | const auto root = context->root(); |
317 | 2 | if (root == nullptr) { |
318 | 0 | return "VExprContext{root=null}"; |
319 | 0 | } |
320 | 2 | std::ostringstream out; |
321 | 2 | out << "VExprContext{root_name=" << root->expr_name() << ", root_debug=" << root->debug_string() |
322 | 2 | << "}"; |
323 | 2 | return out.str(); |
324 | 2 | } |
325 | | |
326 | 1 | std::string table_filter_debug_string(const TableFilter& filter) { |
327 | 1 | std::ostringstream out; |
328 | 1 | out << "TableFilter{conjunct=" << expr_context_debug_string(filter.conjunct) |
329 | 1 | << ", global_indices=" |
330 | 1 | << join_table_reader_debug_strings( |
331 | 1 | filter.global_indices, |
332 | 1 | [](GlobalIndex global_index) { return std::to_string(global_index.value()); }) |
333 | 1 | << "}"; |
334 | 1 | return out.str(); |
335 | 1 | } |
336 | | |
337 | 9.71k | bool contains_runtime_filter(const VExprContextSPtrs& conjuncts) { |
338 | 10.6k | return std::ranges::any_of(conjuncts, [](const auto& conjunct) { |
339 | 10.6k | return conjunct != nullptr && conjunct->root() != nullptr && |
340 | 10.6k | conjunct->root()->is_rf_wrapper(); |
341 | 10.6k | }); |
342 | 9.71k | } |
343 | | |
344 | 146k | void collect_global_indices(const VExprSPtr& expr, std::set<GlobalIndex>* global_indices) { |
345 | 146k | if (expr == nullptr) { |
346 | 0 | return; |
347 | 0 | } |
348 | 146k | if (expr->is_rf_wrapper()) { |
349 | | // RuntimeFilterExpr wraps a real predicate expression but its own thrift node can still |
350 | | // look like SLOT_REF. Collect indices from the wrapped predicate; do not cast the wrapper |
351 | | // itself to VSlotRef. |
352 | 6.05k | collect_global_indices(expr->get_impl(), global_indices); |
353 | 6.05k | return; |
354 | 6.05k | } |
355 | 140k | if (expr->is_slot_ref()) { |
356 | 45.4k | const auto* slot_ref = assert_cast<const VSlotRef*>(expr.get()); |
357 | 45.4k | DORIS_CHECK(slot_ref->column_id() >= 0); |
358 | 45.4k | global_indices->insert(GlobalIndex(cast_set<size_t>(slot_ref->column_id()))); |
359 | 45.4k | } |
360 | 140k | for (const auto& child : expr->children()) { |
361 | 97.0k | collect_global_indices(child, global_indices); |
362 | 97.0k | } |
363 | 140k | } |
364 | | |
365 | | Status build_table_filters_from_conjunct(const VExprContextSPtr& conjunct, RuntimeState* state, |
366 | 39.9k | std::vector<TableFilter>* table_filters) { |
367 | 39.9k | if (conjunct == nullptr) { |
368 | 0 | return Status::OK(); |
369 | 0 | } |
370 | 39.9k | std::set<GlobalIndex> global_indices; |
371 | 39.9k | collect_global_indices(conjunct->root(), &global_indices); |
372 | 39.9k | if (!global_indices.empty()) { |
373 | 39.9k | TableFilter table_filter; |
374 | 39.9k | VExprSPtr filter_root; |
375 | 39.9k | RETURN_IF_ERROR(clone_table_expr_tree(conjunct->root(), &filter_root)); |
376 | 39.9k | table_filter.conjunct = VExprContext::create_shared(std::move(filter_root)); |
377 | 41.0k | for (const auto global_index : global_indices) { |
378 | 41.0k | table_filter.global_indices.push_back(global_index); |
379 | 41.0k | } |
380 | 39.9k | table_filters->push_back(std::move(table_filter)); |
381 | 39.9k | } |
382 | 39.9k | return Status::OK(); |
383 | 39.9k | } |
384 | | |
385 | | Status parse_deletion_vector(const char* buf, size_t buffer_size, DeleteFileDesc::Format format, |
386 | 1.43k | DeletionVector* deletion_vector) { |
387 | 1.43k | DORIS_CHECK(buf != nullptr); |
388 | 1.43k | DORIS_CHECK(deletion_vector != nullptr); |
389 | 1.43k | DORIS_CHECK(format == DeleteFileDesc::Format::PAIMON || |
390 | 1.43k | format == DeleteFileDesc::Format::ICEBERG); |
391 | | |
392 | 1.43k | if (format == DeleteFileDesc::Format::PAIMON) { |
393 | 1 | RETURN_IF_ERROR(decode_paimon_deletion_vector_buffer(buf, buffer_size, deletion_vector)); |
394 | 1 | return Status::OK(); |
395 | 1 | } |
396 | | |
397 | 1.43k | return decode_iceberg_deletion_vector_buffer(buf, buffer_size, deletion_vector); |
398 | 1.43k | } |
399 | | |
400 | | } // namespace |
401 | | |
402 | | std::shared_ptr<io::FileSystemProperties> create_system_properties( |
403 | 39.4k | const TFileScanRangeParams* scan_params) { |
404 | 39.4k | auto system_properties = std::make_shared<io::FileSystemProperties>(); |
405 | 39.4k | if (scan_params == nullptr || !scan_params->__isset.file_type) { |
406 | 610 | system_properties->system_type = TFileType::FILE_LOCAL; |
407 | 610 | return system_properties; |
408 | 610 | } |
409 | 38.8k | system_properties->system_type = scan_params->file_type; |
410 | 38.8k | system_properties->properties = scan_params->properties; |
411 | 38.8k | system_properties->hdfs_params = scan_params->hdfs_params; |
412 | 38.8k | if (scan_params->__isset.broker_addresses) { |
413 | 0 | system_properties->broker_addresses.assign(scan_params->broker_addresses.begin(), |
414 | 0 | scan_params->broker_addresses.end()); |
415 | 0 | } |
416 | 38.8k | return system_properties; |
417 | 39.4k | } |
418 | | |
419 | 12 | std::string TableReader::debug_string() const { |
420 | 12 | std::ostringstream out; |
421 | 12 | out << "TableReader{format=" << file_format_to_string(_format) |
422 | 12 | << ", push_down_agg_type=" << push_down_agg_to_string(_push_down_agg_type) |
423 | 12 | << ", aggregate_pushdown_tried=" << _aggregate_pushdown_tried |
424 | 12 | << ", has_current_reader=" << (_data_reader.reader != nullptr) |
425 | 12 | << ", has_current_task=" << (_current_task != nullptr) |
426 | 12 | << ", current_file=" << current_file_debug_string(_current_task) |
427 | 12 | << ", has_delete_rows=" << (_delete_rows != nullptr) |
428 | 12 | << ", delete_row_count=" << (_delete_rows == nullptr ? 0 : _delete_rows->size()) |
429 | 12 | << ", has_deletion_vector=" << (_deletion_vector != nullptr) |
430 | 12 | << ", deletion_vector_cardinality=" |
431 | 12 | << (_deletion_vector == nullptr ? 0 : _deletion_vector->cardinality()) |
432 | 12 | << ", has_system_properties=" << (_system_properties != nullptr) << ", system_type=" |
433 | 12 | << (_system_properties == nullptr ? static_cast<int>(TFileType::FILE_LOCAL) |
434 | 12 | : static_cast<int>(_system_properties->system_type)) |
435 | 12 | << ", has_scan_params=" << (_scan_params != nullptr) |
436 | 12 | << ", has_io_ctx=" << (_io_ctx != nullptr) |
437 | 12 | << ", has_runtime_state=" << (_runtime_state != nullptr) |
438 | 12 | << ", has_scanner_profile=" << (_scanner_profile != nullptr) |
439 | 12 | << ", mapper_options=" << _mapper_options.debug_string() << ", projected_columns=" |
440 | 12 | << join_table_reader_debug_strings( |
441 | 12 | _projected_columns, |
442 | 12 | [](const ColumnDefinition& column) { return column.debug_string(); }) |
443 | 12 | << ", partition_values=" << partition_values_debug_string(_partition_values) |
444 | 12 | << ", table_filters=" |
445 | 12 | << join_table_reader_debug_strings( |
446 | 12 | _table_filters, |
447 | 12 | [](const TableFilter& filter) { return table_filter_debug_string(filter); }) |
448 | 12 | << ", conjunct_count=" << _conjuncts.size() << ", conjuncts=" |
449 | 12 | << join_table_reader_debug_strings(_conjuncts, |
450 | 12 | [](const VExprContextSPtr& conjunct) { |
451 | 1 | return expr_context_debug_string(conjunct); |
452 | 1 | }) |
453 | 12 | << ", file_schema=" |
454 | 12 | << join_table_reader_debug_strings( |
455 | 12 | _data_reader.file_schema, |
456 | 12 | [](const ColumnDefinition& field) { return field.debug_string(); }) |
457 | 12 | << ", file_block_layout=" |
458 | 12 | << join_table_reader_debug_strings( |
459 | 12 | _data_reader.file_block_layout, |
460 | 12 | [](const FileBlockColumn& column) { |
461 | 2 | std::ostringstream column_out; |
462 | 2 | column_out << "FileBlockColumn{file_column_id=" << column.file_column_id |
463 | 2 | << ", name=" << column.name << ", type=" |
464 | 2 | << (column.type == nullptr ? "null" : column.type->get_name()) |
465 | 2 | << "}"; |
466 | 2 | return column_out.str(); |
467 | 2 | }) |
468 | 12 | << ", block_template_columns=" << _data_reader.block_template.columns() |
469 | 12 | << ", column_mapper=" |
470 | 12 | << (_data_reader.column_mapper == nullptr ? "null" |
471 | 12 | : _data_reader.column_mapper->debug_string()) |
472 | 12 | << "}"; |
473 | 12 | return out.str(); |
474 | 12 | } |
475 | | |
476 | | Status TableReader::annotate_projected_column(const TFileScanSlotInfo& slot_info, |
477 | | ProjectedColumnBuildContext* context, |
478 | 236k | ColumnDefinition* column) const { |
479 | 236k | (void)slot_info; |
480 | 236k | DORIS_CHECK(context != nullptr); |
481 | 236k | DORIS_CHECK(column != nullptr); |
482 | 236k | context->schema_column.reset(); |
483 | 236k | const auto* schema_field = find_external_root_field(context->scan_params, *column); |
484 | 236k | if (schema_field == nullptr) { |
485 | 177k | return Status::OK(); |
486 | 177k | } |
487 | 58.5k | context->schema_column = build_schema_column_from_external_field(*schema_field, column->type); |
488 | 58.5k | column->identifier = context->schema_column->identifier; |
489 | 58.5k | column->name_mapping = context->schema_column->name_mapping; |
490 | 58.5k | return Status::OK(); |
491 | 236k | } |
492 | | |
493 | | std::optional<ColumnDefinition> TableReader::_find_current_table_column_by_field_id( |
494 | 9 | int32_t field_id, DataTypePtr type) const { |
495 | 9 | if (_scan_params == nullptr || !_scan_params->__isset.history_schema_info || |
496 | 9 | _scan_params->history_schema_info.empty()) { |
497 | 3 | return std::nullopt; |
498 | 3 | } |
499 | 6 | const auto* schema = &_scan_params->history_schema_info.front(); |
500 | 6 | if (_scan_params->__isset.current_schema_id) { |
501 | 6 | for (const auto& candidate_schema : _scan_params->history_schema_info) { |
502 | 6 | if (candidate_schema.__isset.schema_id && |
503 | 6 | candidate_schema.schema_id == _scan_params->current_schema_id) { |
504 | 6 | schema = &candidate_schema; |
505 | 6 | break; |
506 | 6 | } |
507 | 6 | } |
508 | 6 | } |
509 | 6 | if (!schema->__isset.root_field || !schema->root_field.__isset.fields) { |
510 | 0 | return std::nullopt; |
511 | 0 | } |
512 | 11 | for (const auto& field_ptr : schema->root_field.fields) { |
513 | 11 | const auto* field = get_field_ptr(field_ptr); |
514 | 11 | if (field != nullptr && field->__isset.id && field->id == field_id) { |
515 | 6 | return build_schema_column_from_external_field(*field, std::move(type)); |
516 | 6 | } |
517 | 11 | } |
518 | 0 | return std::nullopt; |
519 | 6 | } |
520 | | |
521 | 39.4k | Status TableReader::init(TableReadOptions&& options) { |
522 | 39.4k | _scan_params = options.scan_params; |
523 | 39.4k | _format = options.format; |
524 | 39.4k | _io_ctx = options.io_ctx; |
525 | 39.4k | _runtime_state = options.runtime_state; |
526 | 39.4k | _scanner_profile = options.scanner_profile; |
527 | 39.4k | _file_slot_descs = options.file_slot_descs; |
528 | 39.4k | _push_down_agg_type = options.push_down_agg_type; |
529 | 39.4k | _condition_cache_digest = options.condition_cache_digest; |
530 | 39.4k | _projected_columns = std::move(options.projected_columns); |
531 | 39.4k | _system_properties = create_system_properties(_scan_params); |
532 | 39.4k | _mapper_options.mode = TableColumnMappingMode::BY_NAME; |
533 | 39.4k | _conjuncts = std::move(options.conjuncts); |
534 | | |
535 | 39.4k | if (_scanner_profile != nullptr) { |
536 | 39.3k | static const char* table_profile = "TableReader"; |
537 | 39.3k | ADD_TIMER_WITH_LEVEL(_scanner_profile, table_profile, 1); |
538 | 39.3k | _profile.num_delete_files = ADD_CHILD_COUNTER_WITH_LEVEL(_scanner_profile, "NumDeleteFiles", |
539 | 39.3k | TUnit::UNIT, table_profile, 1); |
540 | 39.3k | _profile.num_delete_rows = ADD_CHILD_COUNTER_WITH_LEVEL(_scanner_profile, "NumDeleteRows", |
541 | 39.3k | TUnit::UNIT, table_profile, 1); |
542 | 39.3k | _profile.parse_delete_file_time = ADD_CHILD_TIMER_WITH_LEVEL( |
543 | 39.3k | _scanner_profile, "ParseDeleteFileTime", table_profile, 1); |
544 | 39.3k | _profile.decoded_dv_cache_hit_count = |
545 | 39.3k | ADD_CHILD_COUNTER_WITH_LEVEL(_scanner_profile, "DeletionVectorDecodedCacheHitCount", |
546 | 39.3k | TUnit::UNIT, table_profile, 1); |
547 | 39.3k | _profile.decoded_dv_cache_miss_count = ADD_CHILD_COUNTER_WITH_LEVEL( |
548 | 39.3k | _scanner_profile, "DeletionVectorDecodedCacheMissCount", TUnit::UNIT, table_profile, |
549 | 39.3k | 1); |
550 | 39.3k | _profile.dv_file_cache_hit_count = ADD_CHILD_COUNTER_WITH_LEVEL( |
551 | 39.3k | _scanner_profile, "DeletionVectorFileCacheHitCount", TUnit::UNIT, table_profile, 1); |
552 | 39.3k | _profile.dv_file_cache_miss_count = |
553 | 39.3k | ADD_CHILD_COUNTER_WITH_LEVEL(_scanner_profile, "DeletionVectorFileCacheMissCount", |
554 | 39.3k | TUnit::UNIT, table_profile, 1); |
555 | 39.3k | _profile.dv_file_cache_peer_read_count = ADD_CHILD_COUNTER_WITH_LEVEL( |
556 | 39.3k | _scanner_profile, "DeletionVectorFileCachePeerReadCount", TUnit::UNIT, |
557 | 39.3k | table_profile, 1); |
558 | 39.3k | _profile.exec_timer = |
559 | 39.3k | ADD_CHILD_TIMER_WITH_LEVEL(_scanner_profile, "GetBlockTime", table_profile, 1); |
560 | 39.3k | _profile.prepare_split_timer = |
561 | 39.3k | ADD_CHILD_TIMER_WITH_LEVEL(_scanner_profile, "PrepareSplitTime", table_profile, 1); |
562 | 39.3k | _profile.finalize_timer = |
563 | 39.3k | ADD_CHILD_TIMER_WITH_LEVEL(_scanner_profile, "FinalizeBlockTime", table_profile, 1); |
564 | 39.3k | _profile.create_reader_timer = |
565 | 39.3k | ADD_CHILD_TIMER_WITH_LEVEL(_scanner_profile, "CreateReaderTime", table_profile, 1); |
566 | 39.3k | _profile.pushdown_agg_timer = |
567 | 39.3k | ADD_CHILD_TIMER_WITH_LEVEL(_scanner_profile, "PushDownAggTime", table_profile, 1); |
568 | 39.3k | _profile.open_reader_timer = |
569 | 39.3k | ADD_CHILD_TIMER_WITH_LEVEL(_scanner_profile, "OpenReaderTime", table_profile, 1); |
570 | 39.3k | _profile.runtime_filter_partition_prune_timer = ADD_TIMER_WITH_LEVEL( |
571 | 39.3k | _scanner_profile, "FileScannerRuntimeFilterPartitionPruningTime", 1); |
572 | 39.3k | _profile.runtime_filter_partition_pruned_range_counter = ADD_COUNTER_WITH_LEVEL( |
573 | 39.3k | _scanner_profile, "RuntimeFilterPartitionPrunedRangeNum", TUnit::UNIT, 1); |
574 | 39.3k | } |
575 | 39.4k | return Status::OK(); |
576 | 39.4k | } |
577 | | |
578 | 60.0k | Status TableReader::_build_table_filters_from_conjuncts() { |
579 | 60.0k | _table_filters.clear(); |
580 | 60.0k | _constant_pruning_safe_filter_count = 0; |
581 | 60.0k | bool in_safe_prefix = true; |
582 | 60.0k | for (const auto& conjunct : _conjuncts) { |
583 | 39.9k | DORIS_CHECK(conjunct != nullptr); |
584 | 39.9k | DORIS_CHECK(conjunct->root() != nullptr); |
585 | | // `_table_filters` omits expressions without slot references, but such an expression still |
586 | | // occupies a position in the row-level conjunct order. Record how many localized filters |
587 | | // precede the first unsafe original conjunct so constant pruning cannot jump over a |
588 | | // slotless non-deterministic/error-preserving barrier. |
589 | 39.9k | if (in_safe_prefix && !_is_safe_to_pre_execute(conjunct)) { |
590 | 3 | in_safe_prefix = false; |
591 | 3 | } |
592 | 39.9k | if (!in_safe_prefix) { |
593 | 5 | continue; |
594 | 5 | } |
595 | 39.9k | RETURN_IF_ERROR( |
596 | 39.9k | build_table_filters_from_conjunct(conjunct, _runtime_state, &_table_filters)); |
597 | 39.9k | _constant_pruning_safe_filter_count = _table_filters.size(); |
598 | 39.9k | } |
599 | 60.0k | return Status::OK(); |
600 | 60.0k | } |
601 | | |
602 | 58.8k | Status TableReader::_open_local_filter_exprs(const FileScanRequest& file_request) { |
603 | 58.8k | RowDescriptor row_desc; |
604 | 58.8k | for (const auto& conjunct : file_request.conjuncts) { |
605 | 32.7k | RETURN_IF_ERROR(conjunct->prepare(_runtime_state, row_desc)); |
606 | 32.7k | RETURN_IF_ERROR(conjunct->open(_runtime_state)); |
607 | 32.7k | } |
608 | 58.8k | for (const auto& delete_conjunct : file_request.delete_conjuncts) { |
609 | 7.98k | RETURN_IF_ERROR(delete_conjunct->prepare(_runtime_state, row_desc)); |
610 | 7.98k | RETURN_IF_ERROR(delete_conjunct->open(_runtime_state)); |
611 | 7.98k | } |
612 | 58.8k | return Status::OK(); |
613 | 58.8k | } |
614 | | |
615 | 58.9k | bool TableReader::_should_enable_condition_cache(const FileScanRequest& file_request) const { |
616 | 58.9k | if (_condition_cache_digest == 0 || _push_down_agg_type == TPushAggOp::type::COUNT || |
617 | 58.9k | _current_file_description == std::nullopt || _data_reader.reader == nullptr) { |
618 | 13.1k | return false; |
619 | 13.1k | } |
620 | | // Condition cache is populated by file readers after evaluating file-local row-level |
621 | | // conjuncts. Metadata pruning can skip row groups/pages, but it does not produce a per-row |
622 | | // survivor bitmap that can safely populate the cache. |
623 | 45.8k | if (file_request.conjuncts.empty()) { |
624 | 33.2k | return false; |
625 | 33.2k | } |
626 | | // Delete files/deletion vectors are table-format state. They may change independently of the |
627 | | // data file path/mtime/size used by the external cache key, so caching their result can become |
628 | | // stale. Keep delete filtering enabled, but do not read or write condition cache. |
629 | 12.5k | if (_delete_rows != nullptr || _deletion_vector != nullptr || |
630 | 12.5k | !file_request.delete_conjuncts.empty()) { |
631 | 2.79k | return false; |
632 | 2.79k | } |
633 | | // Runtime filters can arrive late and their payload is not guaranteed to be represented by the |
634 | | // scan-local digest. Without a read-only mode, a MISS could insert a bitmap for P AND RF under |
635 | | // the digest for only P. This mirrors the old FileScanner guard. |
636 | 9.79k | return !contains_runtime_filter(file_request.conjuncts); |
637 | 12.5k | } |
638 | | |
639 | 58.8k | Status TableReader::_init_reader_condition_cache(const FileScanRequest& file_request) { |
640 | 58.8k | _condition_cache = nullptr; |
641 | 58.8k | _condition_cache_ctx = nullptr; |
642 | 58.8k | if (!_should_enable_condition_cache(file_request)) { |
643 | 50.1k | return Status::OK(); |
644 | 50.1k | } |
645 | | |
646 | 8.67k | auto* cache = segment_v2::ConditionCache::instance(); |
647 | 8.67k | if (cache == nullptr) { |
648 | 0 | return Status::OK(); |
649 | 0 | } |
650 | 8.67k | const auto& file = *_current_file_description; |
651 | 8.67k | _condition_cache_key = segment_v2::ConditionCache::ExternalCacheKey( |
652 | 8.67k | file.path, file.mtime, file.file_size, _condition_cache_digest, file.range_start_offset, |
653 | 8.67k | file.range_size, |
654 | 8.67k | segment_v2::ConditionCache::ExternalCacheKey::BASE_GRANULE_AWARE_VERSION); |
655 | | |
656 | 8.67k | segment_v2::ConditionCacheHandle handle; |
657 | 8.67k | const bool condition_cache_hit = cache->lookup(_condition_cache_key, &handle); |
658 | 8.67k | if (condition_cache_hit) { |
659 | 3.15k | _condition_cache = handle.get_filter_result(); |
660 | 3.15k | ++_condition_cache_hit_count; |
661 | 5.52k | } else { |
662 | 5.52k | const int64_t total_rows = _data_reader.reader->get_total_rows(); |
663 | 5.52k | if (total_rows <= 0) { |
664 | 1.71k | return Status::OK(); |
665 | 1.71k | } |
666 | | // Add one guard granule for split ranges that start in the middle of a granule. A guard |
667 | | // false bit beyond the real range never overlaps real rows, but avoids boundary overflow |
668 | | // when a reader marks the last partial granule. |
669 | 3.80k | const size_t num_granules = (total_rows + ConditionCacheContext::GRANULE_SIZE - 1) / |
670 | 3.80k | ConditionCacheContext::GRANULE_SIZE; |
671 | 3.80k | _condition_cache = std::make_shared<std::vector<bool>>(num_granules + 1, false); |
672 | 3.80k | } |
673 | | |
674 | 6.95k | if (_condition_cache != nullptr) { |
675 | 6.89k | _condition_cache_ctx = std::make_shared<ConditionCacheContext>(); |
676 | 6.89k | _condition_cache_ctx->is_hit = condition_cache_hit; |
677 | 6.89k | _condition_cache_ctx->filter_result = _condition_cache; |
678 | 6.89k | _condition_cache_ctx->num_granules = _condition_cache->size(); |
679 | 6.89k | if (condition_cache_hit) { |
680 | 3.15k | _condition_cache_ctx->base_granule = handle.get_base_granule(); |
681 | 3.15k | } |
682 | 6.89k | _data_reader.reader->set_condition_cache_context(_condition_cache_ctx); |
683 | 6.89k | } |
684 | 6.95k | return Status::OK(); |
685 | 8.67k | } |
686 | | |
687 | 60.2k | void TableReader::_finalize_reader_condition_cache() { |
688 | 60.2k | if (_condition_cache_ctx == nullptr || _condition_cache_ctx->is_hit) { |
689 | 56.5k | _condition_cache = nullptr; |
690 | 56.5k | _condition_cache_ctx = nullptr; |
691 | 56.5k | return; |
692 | 56.5k | } |
693 | | // LIMIT or scanner cancellation may close a reader before all selected row ranges are visited. |
694 | | // Unvisited granules remain false in a MISS bitmap, so inserting a partial bitmap would make a |
695 | | // later HIT skip valid rows. Only publish cache entries after the physical reader reaches EOF. |
696 | 3.73k | if (!_current_reader_reached_eof) { |
697 | 78 | _condition_cache = nullptr; |
698 | 78 | _condition_cache_ctx = nullptr; |
699 | 78 | return; |
700 | 78 | } |
701 | 3.65k | DORIS_CHECK(_condition_cache_ctx->num_granules <= _condition_cache->size()); |
702 | 3.65k | _condition_cache->resize(_condition_cache_ctx->num_granules); |
703 | 3.65k | segment_v2::ConditionCache::instance()->insert( |
704 | 3.65k | _condition_cache_key, std::move(_condition_cache), _condition_cache_ctx->base_granule); |
705 | 3.65k | _condition_cache = nullptr; |
706 | 3.65k | _condition_cache_ctx = nullptr; |
707 | 3.65k | } |
708 | | |
709 | 118k | Status TableReader::create_next_reader(bool* eos) { |
710 | 118k | SCOPED_TIMER(_profile.create_reader_timer); |
711 | 118k | DCHECK(_data_reader.reader == nullptr); |
712 | 118k | if (_current_task == nullptr) { |
713 | 58.3k | *eos = true; |
714 | 58.3k | return Status::OK(); |
715 | 58.3k | } |
716 | | |
717 | 60.1k | RETURN_IF_ERROR(create_file_reader(&_data_reader.reader)); |
718 | 60.1k | DORIS_CHECK(_data_reader.reader != nullptr); |
719 | 60.1k | if (_batch_size > 0) { |
720 | 57.9k | _data_reader.reader->set_batch_size(_batch_size); |
721 | 57.9k | } |
722 | 60.1k | Status st = _data_reader.reader->init(_runtime_state); |
723 | 60.1k | if (!st.ok()) { |
724 | 15 | if (_io_ctx != nullptr && _io_ctx->should_stop && st.is<ErrorCode::END_OF_FILE>()) { |
725 | 0 | *eos = true; |
726 | 0 | _data_reader.reader.reset(); |
727 | 0 | return Status::OK(); |
728 | 0 | } |
729 | 15 | return st; |
730 | 15 | } |
731 | 60.1k | st = open_reader(); |
732 | 60.1k | if (!st.ok()) { |
733 | 0 | if (_io_ctx != nullptr && _io_ctx->should_stop && st.is<ErrorCode::END_OF_FILE>()) { |
734 | 0 | *eos = true; |
735 | 0 | _data_reader.reader.reset(); |
736 | 0 | return Status::OK(); |
737 | 0 | } |
738 | 0 | return st; |
739 | 0 | } |
740 | 60.1k | if (_data_reader.reader == nullptr) { |
741 | 1.25k | *eos = _current_task == nullptr; |
742 | 1.25k | return Status::OK(); |
743 | 1.25k | } |
744 | 58.8k | *eos = false; |
745 | 58.8k | return Status::OK(); |
746 | 60.1k | } |
747 | | |
748 | 60.0k | Status TableReader::create_file_reader(std::unique_ptr<FileReader>* reader) { |
749 | 60.0k | DORIS_CHECK(reader != nullptr); |
750 | 60.0k | const bool enable_mapping_timestamp_tz = _scan_params != nullptr && |
751 | 60.0k | _scan_params->__isset.enable_mapping_timestamp_tz && |
752 | 60.0k | _scan_params->enable_mapping_timestamp_tz; |
753 | 60.0k | if (_format == FileFormat::PARQUET) { |
754 | 31.4k | *reader = std::make_unique<format::parquet::ParquetReader>( |
755 | 31.4k | _system_properties, _current_task->data_file, _io_ctx, _scanner_profile, |
756 | 31.4k | _global_rowid_context, enable_mapping_timestamp_tz); |
757 | 31.4k | return Status::OK(); |
758 | 31.4k | } |
759 | 28.5k | if (_format == FileFormat::ORC) { |
760 | 22.4k | *reader = std::make_unique<format::orc::OrcReader>( |
761 | 22.4k | _system_properties, _current_task->data_file, _io_ctx, _scanner_profile, |
762 | 22.4k | _global_rowid_context, enable_mapping_timestamp_tz); |
763 | 22.4k | return Status::OK(); |
764 | 22.4k | } |
765 | 6.16k | if (_format == FileFormat::CSV) { |
766 | 952 | if (_file_slot_descs == nullptr) { |
767 | 0 | return Status::InvalidArgument("CSV reader requires file slot descriptors"); |
768 | 0 | } |
769 | | // CSV has no embedded schema. TableReader owns table-level mapping, while CsvReader needs |
770 | | // only the physical file slots plus scan text parameters to build a file-local schema. |
771 | | // Non-file columns such as partitions/defaults/virtual row ids are intentionally excluded |
772 | | // from `_file_slot_descs` and are materialized during finalize_chunk(). |
773 | 952 | *reader = std::make_unique<format::csv::CsvReader>( |
774 | 952 | _system_properties, _current_task->data_file, _io_ctx, _scanner_profile, |
775 | 952 | _scan_params, *_file_slot_descs, _current_range_compress_type, |
776 | 952 | _current_range_load_id); |
777 | 952 | return Status::OK(); |
778 | 952 | } |
779 | 5.20k | if (_format == FileFormat::TEXT) { |
780 | 4.70k | if (_file_slot_descs == nullptr) { |
781 | 0 | return Status::InvalidArgument("Text reader requires file slot descriptors"); |
782 | 0 | } |
783 | | // Text files have no embedded schema. As with CSV, TableReader handles table-level mapping |
784 | | // and only passes physical file slots to the v2 TextReader. |
785 | 4.70k | *reader = std::make_unique<format::text::TextReader>( |
786 | 4.70k | _system_properties, _current_task->data_file, _io_ctx, _scanner_profile, |
787 | 4.70k | _scan_params, *_file_slot_descs, _current_range_compress_type, |
788 | 4.70k | _current_range_load_id); |
789 | 4.70k | return Status::OK(); |
790 | 4.70k | } |
791 | 596 | if (_format == FileFormat::JSON) { |
792 | 596 | if (_file_slot_descs == nullptr) { |
793 | 0 | return Status::InvalidArgument("JSON reader requires file slot descriptors"); |
794 | 0 | } |
795 | 596 | *reader = std::make_unique<format::json::JsonReader>( |
796 | 596 | _system_properties, _current_task->data_file, _io_ctx, _scanner_profile, |
797 | 596 | _scan_params, _current_file_range_desc, *_file_slot_descs, |
798 | 596 | _current_range_compress_type, _current_range_load_id); |
799 | 596 | return Status::OK(); |
800 | 596 | } |
801 | 18.4E | if (_format == FileFormat::NATIVE) { |
802 | 2 | *reader = std::make_unique<format::native::NativeReader>( |
803 | 2 | _system_properties, _current_task->data_file, _io_ctx, _scanner_profile); |
804 | 2 | return Status::OK(); |
805 | 2 | } |
806 | 18.4E | return Status::NotSupported("TableReader does not support file format {}", |
807 | 18.4E | file_format_to_string(_format)); |
808 | 18.4E | } |
809 | | |
810 | 60.4k | std::unique_ptr<io::FileDescription> create_file_description(const TFileRangeDesc& range) { |
811 | 60.4k | auto file_description = std::make_unique<io::FileDescription>(); |
812 | 60.4k | file_description->path = range.path; |
813 | 60.4k | file_description->file_size = range.__isset.file_size ? range.file_size : -1; |
814 | 60.4k | file_description->mtime = range.__isset.modification_time ? range.modification_time : 0; |
815 | 60.4k | file_description->range_start_offset = range.__isset.start_offset ? range.start_offset : 0; |
816 | 60.4k | file_description->range_size = range.__isset.size ? range.size : -1; |
817 | 60.4k | if (range.__isset.fs_name) { |
818 | 33.2k | file_description->fs_name = range.fs_name; |
819 | 33.2k | } |
820 | 60.4k | if (range.__isset.file_cache_admission) { |
821 | 60.2k | file_description->file_cache_admission = range.file_cache_admission; |
822 | 60.2k | } |
823 | 60.4k | return file_description; |
824 | 60.4k | } |
825 | | |
826 | 60.7k | Status TableReader::prepare_split(const SplitReadOptions& options) { |
827 | 60.7k | SCOPED_TIMER(_profile.prepare_split_timer); |
828 | 60.7k | _current_split_pruned = false; |
829 | 60.7k | _all_runtime_filters_applied_for_split = options.all_runtime_filters_applied; |
830 | 60.7k | if (options.conjuncts.has_value()) { |
831 | 60.6k | _conjuncts = *options.conjuncts; |
832 | 60.6k | } |
833 | | // Update to current split format to handle ORC/PARQUET files in one table. |
834 | 60.7k | _format = options.current_split_format; |
835 | 60.7k | _partition_values = std::move(options.partition_values); |
836 | 60.7k | _current_task.reset(); |
837 | 60.7k | _current_file_description.reset(); |
838 | 60.7k | _current_file_range_desc = options.current_range; |
839 | 60.7k | _current_range_compress_type = options.current_range.__isset.compress_type |
840 | 60.7k | ? options.current_range.compress_type |
841 | 60.7k | : TFileCompressType::UNKNOWN; |
842 | 60.7k | _current_range_load_id = options.current_range.__isset.load_id |
843 | 60.7k | ? std::make_optional(options.current_range.load_id) |
844 | 60.7k | : std::nullopt; |
845 | 60.7k | _global_rowid_context = options.global_rowid_context; |
846 | 60.7k | _delete_rows = nullptr; |
847 | 60.7k | _deletion_vector = nullptr; |
848 | 60.7k | _aggregate_pushdown_tried = false; |
849 | 60.7k | _remaining_table_level_count = -1; |
850 | 60.7k | _current_reader_reached_eof = false; |
851 | 60.7k | RETURN_IF_ERROR(_evaluate_partition_prune_conjuncts(options.partition_prune_conjuncts, |
852 | 60.7k | &_current_split_pruned)); |
853 | 60.7k | if (_current_split_pruned) { |
854 | 279 | COUNTER_UPDATE(_profile.runtime_filter_partition_pruned_range_counter, 1); |
855 | 279 | return Status::OK(); |
856 | 279 | } |
857 | 60.4k | _current_task = std::make_unique<ScanTask>(); |
858 | 60.4k | _current_task->data_file = create_file_description(options.current_range); |
859 | 60.4k | _current_file_description = *_current_task->data_file; |
860 | | // A table-level row count is only equivalent to scanning the split when no row predicate is |
861 | | // active and no predicate can arrive later. The metadata path can return several batches for |
862 | | // one split; after its first synthetic batch there is no way to recover the real rows if a |
863 | | // runtime filter arrives before the next scheduler turn. |
864 | 60.4k | if (_push_down_agg_type == TPushAggOp::type::COUNT && options.all_runtime_filters_applied && |
865 | 60.4k | _conjuncts.empty() && options.current_range.__isset.table_format_params && |
866 | 60.4k | options.current_range.table_format_params.__isset.table_level_row_count) { |
867 | 2.12k | DORIS_CHECK(options.current_range.table_format_params.table_level_row_count >= -1); |
868 | 2.12k | _remaining_table_level_count = |
869 | 2.12k | options.current_range.table_format_params.table_level_row_count; |
870 | 2.12k | } |
871 | 60.4k | if (_is_table_level_count_active()) { |
872 | 148 | return Status::OK(); |
873 | 148 | } |
874 | 60.2k | return _parse_delete_predicates(options); |
875 | 60.4k | } |
876 | | |
877 | | Status TableReader::_evaluate_partition_prune_conjuncts(const VExprContextSPtrs& conjuncts, |
878 | 60.6k | bool* can_filter_all) { |
879 | 60.6k | DORIS_CHECK(can_filter_all != nullptr); |
880 | 60.6k | SCOPED_TIMER(_profile.runtime_filter_partition_prune_timer); |
881 | 60.6k | *can_filter_all = false; |
882 | 60.6k | if (conjuncts.empty() || _partition_values.empty()) { |
883 | 57.5k | return Status::OK(); |
884 | 57.5k | } |
885 | | |
886 | 3.09k | VExprContextSPtrs partition_conjuncts; |
887 | 4.36k | for (const auto& conjunct : conjuncts) { |
888 | 4.36k | DORIS_CHECK(conjunct != nullptr); |
889 | 4.36k | DORIS_CHECK(conjunct->root() != nullptr); |
890 | | // Keep only the safe prefix of the original conjunct order. If an unsafe conjunct is |
891 | | // skipped, a later predicate could prune the split before the unsafe one reaches its |
892 | | // normal row-level evaluation point. |
893 | 4.36k | if (!_is_safe_to_pre_execute(conjunct)) { |
894 | 1 | break; |
895 | 1 | } |
896 | 4.36k | std::set<GlobalIndex> global_indices; |
897 | 4.36k | collect_global_indices(conjunct->root(), &global_indices); |
898 | 4.36k | if (global_indices.empty()) { |
899 | 0 | continue; |
900 | 0 | } |
901 | 4.43k | const bool partition_only = std::ranges::all_of(global_indices, [&](GlobalIndex index) { |
902 | 4.43k | if (index.value() >= _projected_columns.size()) { |
903 | 0 | return false; |
904 | 0 | } |
905 | 4.43k | const auto& column = _projected_columns[index.value()]; |
906 | 4.43k | return column.is_partition_key && |
907 | 4.43k | find_partition_value(column, _partition_values) != nullptr; |
908 | 4.43k | }); |
909 | 4.36k | if (partition_only) { |
910 | 2.74k | partition_conjuncts.push_back(conjunct); |
911 | 2.74k | } |
912 | 4.36k | } |
913 | 3.09k | if (partition_conjuncts.empty()) { |
914 | 1.18k | return Status::OK(); |
915 | 1.18k | } |
916 | | |
917 | 1.91k | Block block; |
918 | 1.91k | RETURN_IF_ERROR(_build_partition_prune_block(&block)); |
919 | 1.91k | RowDescriptor row_desc; |
920 | 2.75k | for (const auto& conjunct : partition_conjuncts) { |
921 | 2.75k | RETURN_IF_ERROR(conjunct->prepare(_runtime_state, row_desc)); |
922 | 2.75k | RETURN_IF_ERROR(conjunct->open(_runtime_state)); |
923 | 2.75k | } |
924 | 1.91k | IColumn::Filter result_filter(block.rows(), 1); |
925 | 1.91k | return VExprContext::execute_conjuncts(partition_conjuncts, nullptr, &block, &result_filter, |
926 | 1.91k | can_filter_all); |
927 | 1.91k | } |
928 | | |
929 | 84.0k | bool TableReader::_is_safe_to_pre_execute(const VExprContextSPtr& conjunct) { |
930 | 84.0k | DORIS_CHECK(conjunct != nullptr); |
931 | 84.0k | DORIS_CHECK(conjunct->root() != nullptr); |
932 | 84.0k | const auto root = conjunct->root(); |
933 | 84.0k | const auto impl = root->get_impl(); |
934 | 84.0k | const auto predicate = impl != nullptr ? impl : root; |
935 | | // Split pruning evaluates a predicate once before any file rows are read. Reordering |
936 | | // non-deterministic or error-preserving expressions can change their row-level semantics, |
937 | | // even when every referenced slot is a partition column or maps to a constant entry. |
938 | 84.0k | return predicate->is_safe_to_execute_on_selected_rows(); |
939 | 84.0k | } |
940 | | |
941 | 1.97k | Status TableReader::_build_partition_prune_block(Block* block) const { |
942 | 1.97k | DORIS_CHECK(block != nullptr); |
943 | 1.97k | DORIS_CHECK(!_projected_columns.empty()); |
944 | 1.97k | block->clear(); |
945 | 5.63k | for (const auto& column : _projected_columns) { |
946 | 5.63k | DORIS_CHECK(column.type != nullptr); |
947 | 5.63k | ColumnPtr value_column = column.type->create_column_const_with_default_value(1); |
948 | 5.63k | if (column.is_partition_key) { |
949 | 2.78k | const auto* partition_value = find_partition_value(column, _partition_values); |
950 | 2.78k | if (partition_value != nullptr) { |
951 | 2.78k | value_column = column.type->create_column_const(1, *partition_value); |
952 | 2.78k | } |
953 | 2.78k | } |
954 | 5.63k | block->insert({std::move(value_column), column.type, column.name}); |
955 | 5.63k | } |
956 | 1.97k | return Status::OK(); |
957 | 1.97k | } |
958 | | |
959 | 60.1k | Status TableReader::_parse_delete_predicates(const SplitReadOptions& options) { |
960 | 60.1k | DeleteFileDesc desc {.fs_name = options.current_range.fs_name}; |
961 | 60.1k | bool has_delete_file = false; |
962 | 60.1k | RETURN_IF_ERROR(_parse_deletion_vector_file(options.current_range.table_format_params, &desc, |
963 | 60.1k | &has_delete_file)); |
964 | 60.1k | if (has_delete_file) { |
965 | 3.62k | DORIS_CHECK(options.cache != nullptr); |
966 | 3.62k | Status create_status = Status::OK(); |
967 | | |
968 | 3.62k | bool decoded_cache_hit = false; |
969 | 3.62k | _deletion_vector = options.cache->get<DeletionVector>( |
970 | 3.62k | desc.key, |
971 | 3.62k | [&]() -> DeletionVector* { |
972 | 1.44k | auto deletion_vector = std::make_unique<DeletionVector>(); |
973 | | |
974 | 1.44k | DeletionVectorReader dv_reader(_runtime_state, _scanner_profile, *_scan_params, |
975 | 1.44k | desc, _io_ctx.get()); |
976 | 1.44k | create_status = dv_reader.open(); |
977 | 1.44k | if (!create_status.ok()) [[unlikely]] { |
978 | 0 | return nullptr; |
979 | 0 | } |
980 | | |
981 | 1.44k | size_t bytes_read = desc.size; |
982 | 1.44k | std::vector<char> buffer(bytes_read); |
983 | 1.44k | DBUG_EXECUTE_IF("TableReader.parse_deletion_vector.io_error", { |
984 | 1.44k | create_status = |
985 | 1.44k | Status::IOError("injected format v2 deletion vector read failure"); |
986 | 1.44k | return nullptr; |
987 | 1.44k | }); |
988 | 1.43k | DBUG_EXECUTE_IF("TableReader.parse_deletion_vector.should_stop", { |
989 | 1.43k | create_status = Status::EndOfFile("stop read."); |
990 | 1.43k | return nullptr; |
991 | 1.43k | }); |
992 | 1.43k | create_status = |
993 | 1.43k | dv_reader.read_at(desc.start_offset, {buffer.data(), bytes_read}); |
994 | 1.43k | const auto& file_cache_stats = dv_reader.file_cache_statistics(); |
995 | 1.43k | COUNTER_UPDATE(_profile.dv_file_cache_hit_count, |
996 | 1.43k | file_cache_stats.num_local_io_total); |
997 | 1.43k | COUNTER_UPDATE(_profile.dv_file_cache_miss_count, |
998 | 1.43k | file_cache_stats.num_remote_io_total); |
999 | 1.43k | COUNTER_UPDATE(_profile.dv_file_cache_peer_read_count, |
1000 | 1.43k | file_cache_stats.num_peer_io_total); |
1001 | 1.43k | if (!create_status.ok()) [[unlikely]] { |
1002 | 0 | return nullptr; |
1003 | 0 | } |
1004 | | |
1005 | 1.43k | const char* buf = buffer.data(); |
1006 | 1.43k | SCOPED_TIMER(_profile.parse_delete_file_time); |
1007 | 1.43k | create_status = parse_deletion_vector(buf, bytes_read, desc.format, |
1008 | 1.43k | deletion_vector.get()); |
1009 | 1.43k | if (!create_status.ok()) [[unlikely]] { |
1010 | 1 | return nullptr; |
1011 | 1 | } |
1012 | 1.43k | COUNTER_UPDATE(_profile.num_delete_rows, deletion_vector->cardinality()); |
1013 | 1.43k | return deletion_vector.release(); |
1014 | 1.43k | }, |
1015 | 3.62k | &decoded_cache_hit); |
1016 | 3.62k | RETURN_IF_ERROR(create_status); |
1017 | 3.62k | COUNTER_UPDATE(decoded_cache_hit ? _profile.decoded_dv_cache_hit_count |
1018 | 3.62k | : _profile.decoded_dv_cache_miss_count, |
1019 | 3.62k | 1); |
1020 | 3.62k | } |
1021 | | |
1022 | 60.1k | return Status::OK(); |
1023 | 60.1k | } |
1024 | | } // namespace doris::format |