be/src/format_v2/column_mapper.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/column_mapper.h" |
19 | | |
20 | | #include <algorithm> |
21 | | #include <cstddef> |
22 | | #include <memory> |
23 | | #include <sstream> |
24 | | #include <string_view> |
25 | | #include <utility> |
26 | | #include <vector> |
27 | | |
28 | | #include "common/consts.h" |
29 | | #include "common/exception.h" |
30 | | #include "common/status.h" |
31 | | #include "core/data_type/convert_field_to_type.h" |
32 | | #include "core/data_type/data_type_array.h" |
33 | | #include "core/data_type/data_type_map.h" |
34 | | #include "core/data_type/data_type_nullable.h" |
35 | | #include "core/data_type/data_type_string.h" |
36 | | #include "core/data_type/data_type_struct.h" |
37 | | #include "core/data_type/primitive_type.h" |
38 | | #include "exprs/runtime_filter_expr.h" |
39 | | #include "exprs/short_circuit_evaluation_expr.h" |
40 | | #include "exprs/vcase_expr.h" |
41 | | #include "exprs/vcast_expr.h" |
42 | | #include "exprs/vcondition_expr.h" |
43 | | #include "exprs/vectorized_fn_call.h" |
44 | | #include "exprs/vexpr_context.h" |
45 | | #include "exprs/vin_predicate.h" |
46 | | #include "exprs/vliteral.h" |
47 | | #include "format_v2/column_mapper_nested.h" |
48 | | #include "format_v2/expr/cast.h" |
49 | | #include "format_v2/file_reader.h" |
50 | | #include "format_v2/schema_projection.h" |
51 | | #include "format_v2/table_reader.h" |
52 | | #include "gen_cpp/Exprs_types.h" |
53 | | |
54 | | namespace doris::format { |
55 | | |
56 | | namespace { |
57 | | |
58 | 16 | std::string mapping_mode_to_string(TableColumnMappingMode mode) { |
59 | 16 | switch (mode) { |
60 | 1 | case TableColumnMappingMode::BY_FIELD_ID: |
61 | 1 | return "BY_FIELD_ID"; |
62 | 14 | case TableColumnMappingMode::BY_NAME: |
63 | 14 | return "BY_NAME"; |
64 | 1 | case TableColumnMappingMode::BY_INDEX: |
65 | 1 | return "BY_INDEX"; |
66 | 16 | } |
67 | 0 | return "UNKNOWN"; |
68 | 16 | } |
69 | | |
70 | 371 | bool column_has_name(const ColumnDefinition& column, const std::string& name) { |
71 | 371 | if (to_lower(column.name) == to_lower(name)) { |
72 | 157 | return true; |
73 | 157 | } |
74 | 214 | if (column.has_identifier_name() && to_lower(column.get_identifier_name()) == to_lower(name)) { |
75 | 0 | return true; |
76 | 0 | } |
77 | 214 | return std::ranges::any_of(column.name_mapping, [&](const std::string& alias) { |
78 | 1 | return to_lower(alias) == to_lower(name); |
79 | 1 | }); |
80 | 214 | } |
81 | | |
82 | 251 | bool column_names_match(const ColumnDefinition& lhs, const ColumnDefinition& rhs) { |
83 | 251 | if (column_has_name(rhs, lhs.name)) { |
84 | 145 | return true; |
85 | 145 | } |
86 | 106 | if (lhs.has_identifier_name() && column_has_name(rhs, lhs.get_identifier_name())) { |
87 | 1 | return true; |
88 | 1 | } |
89 | 105 | return std::ranges::any_of(lhs.name_mapping, [&](const std::string& alias) { |
90 | 18 | return column_has_name(rhs, alias); |
91 | 18 | }); |
92 | 106 | } |
93 | | |
94 | | class ColumnMatcher { |
95 | | public: |
96 | 3 | virtual ~ColumnMatcher() = default; |
97 | | virtual const ColumnDefinition* find( |
98 | | const ColumnDefinition& table_column, |
99 | | const std::vector<ColumnDefinition>& file_schema) const = 0; |
100 | | }; |
101 | | |
102 | | class FieldIdMatcher final : public ColumnMatcher { |
103 | | public: |
104 | | const ColumnDefinition* find(const ColumnDefinition& table_column, |
105 | 108 | const std::vector<ColumnDefinition>& file_schema) const override { |
106 | 108 | if (!table_column.has_identifier_field_id()) { |
107 | 7 | return nullptr; |
108 | 7 | } |
109 | 101 | const auto field_id = table_column.get_identifier_field_id(); |
110 | 165 | const auto field_it = std::ranges::find_if(file_schema, [&](const ColumnDefinition& field) { |
111 | 165 | return field.has_identifier_field_id() && field.get_identifier_field_id() == field_id; |
112 | 165 | }); |
113 | 101 | return field_it == file_schema.end() ? nullptr : &*field_it; |
114 | 108 | } |
115 | | }; |
116 | | |
117 | | class NameMatcher final : public ColumnMatcher { |
118 | | public: |
119 | | const ColumnDefinition* find(const ColumnDefinition& table_column, |
120 | 182 | const std::vector<ColumnDefinition>& file_schema) const override { |
121 | 251 | const auto field_it = std::ranges::find_if(file_schema, [&](const ColumnDefinition& field) { |
122 | 251 | return column_names_match(table_column, field); |
123 | 251 | }); |
124 | 182 | return field_it == file_schema.end() ? nullptr : &*field_it; |
125 | 182 | } |
126 | | }; |
127 | | |
128 | | class PositionMatcher final : public ColumnMatcher { |
129 | | public: |
130 | | const ColumnDefinition* find(const ColumnDefinition& table_column, |
131 | 2 | const std::vector<ColumnDefinition>& file_schema) const override { |
132 | 2 | if (!table_column.has_identifier_field_id()) { |
133 | 2 | return nullptr; |
134 | 2 | } |
135 | 0 | const auto position = table_column.get_identifier_position(); |
136 | 0 | if (position < 0 || static_cast<size_t>(position) >= file_schema.size()) { |
137 | 0 | return nullptr; |
138 | 0 | } |
139 | 0 | return &file_schema[static_cast<size_t>(position)]; |
140 | 0 | } |
141 | | }; |
142 | | |
143 | 289 | const ColumnMatcher& matcher_for_mode(TableColumnMappingMode mode) { |
144 | 289 | static const FieldIdMatcher field_id_matcher; |
145 | 289 | static const NameMatcher name_matcher; |
146 | 289 | static const PositionMatcher position_matcher; |
147 | 289 | switch (mode) { |
148 | 108 | case TableColumnMappingMode::BY_FIELD_ID: |
149 | 108 | return field_id_matcher; |
150 | 179 | case TableColumnMappingMode::BY_NAME: |
151 | 179 | return name_matcher; |
152 | 2 | case TableColumnMappingMode::BY_INDEX: |
153 | 2 | return position_matcher; |
154 | 289 | } |
155 | 0 | return field_id_matcher; |
156 | 289 | } |
157 | | |
158 | 12 | std::string virtual_column_type_to_string(TableVirtualColumnType type) { |
159 | 12 | switch (type) { |
160 | 9 | case TableVirtualColumnType::INVALID: |
161 | 9 | return "INVALID"; |
162 | 1 | case TableVirtualColumnType::ROW_ID: |
163 | 1 | return "ROW_ID"; |
164 | 1 | case TableVirtualColumnType::LAST_UPDATED_SEQUENCE_NUMBER: |
165 | 1 | return "LAST_UPDATED_SEQUENCE_NUMBER"; |
166 | 1 | case TableVirtualColumnType::ICEBERG_ROWID: |
167 | 1 | return "ICEBERG_ROWID"; |
168 | 12 | } |
169 | 0 | return "UNKNOWN"; |
170 | 12 | } |
171 | | |
172 | 12 | std::string filter_conversion_type_to_string(FilterConversionType type) { |
173 | 12 | switch (type) { |
174 | 3 | case FilterConversionType::COPY_DIRECTLY: |
175 | 3 | return "COPY_DIRECTLY"; |
176 | 1 | case FilterConversionType::CAST_FILTER: |
177 | 1 | return "CAST_FILTER"; |
178 | 1 | case FilterConversionType::READER_EXPRESSION: |
179 | 1 | return "READER_EXPRESSION"; |
180 | 6 | case FilterConversionType::FINALIZE_ONLY: |
181 | 6 | return "FINALIZE_ONLY"; |
182 | 1 | case FilterConversionType::CONSTANT: |
183 | 1 | return "CONSTANT"; |
184 | 12 | } |
185 | 0 | return "UNKNOWN"; |
186 | 12 | } |
187 | | |
188 | 47 | std::string data_type_debug_string(const DataTypePtr& type) { |
189 | 47 | return type == nullptr ? "null" : type->get_name(); |
190 | 47 | } |
191 | | |
192 | 11 | std::string field_debug_string(const Field& field) { |
193 | 11 | std::ostringstream out; |
194 | 11 | out << "Field{type=" << type_to_string(field.get_type()) << ", value="; |
195 | 11 | switch (field.get_type()) { |
196 | 0 | case TYPE_NULL: |
197 | 0 | out << "null"; |
198 | 0 | break; |
199 | 9 | case TYPE_INT: |
200 | 9 | out << field.get<TYPE_INT>(); |
201 | 9 | break; |
202 | 0 | case TYPE_BIGINT: |
203 | 0 | out << field.get<TYPE_BIGINT>(); |
204 | 0 | break; |
205 | 2 | case TYPE_STRING: |
206 | 2 | out << field.get<TYPE_STRING>(); |
207 | 2 | break; |
208 | 0 | default: |
209 | 0 | out << field.to_debug_string(0); |
210 | 0 | break; |
211 | 11 | } |
212 | 11 | out << "}"; |
213 | 11 | return out.str(); |
214 | 11 | } |
215 | | |
216 | | template <typename T, typename Formatter> |
217 | 50 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { |
218 | 50 | std::ostringstream out; |
219 | 50 | out << "["; |
220 | 72 | for (size_t i = 0; i < values.size(); ++i) { |
221 | 22 | if (i > 0) { |
222 | 1 | out << ", "; |
223 | 1 | } |
224 | 22 | out << formatter(values[i]); |
225 | 22 | } |
226 | 50 | out << "]"; |
227 | 50 | return out.str(); |
228 | 50 | } column_mapper.cpp:_ZN5doris6format12_GLOBAL__N_118join_debug_stringsINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEZNKS0_16ColumnDefinition12debug_stringEvE3$_0EES8_RKSt6vectorIT_SaISC_EET0_ Line | Count | Source | 217 | 11 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { | 218 | 11 | std::ostringstream out; | 219 | 11 | out << "["; | 220 | 19 | for (size_t i = 0; i < values.size(); ++i) { | 221 | 8 | if (i > 0) { | 222 | 0 | out << ", "; | 223 | 0 | } | 224 | 8 | out << formatter(values[i]); | 225 | 8 | } | 226 | 11 | out << "]"; | 227 | 11 | return out.str(); | 228 | 11 | } |
column_mapper.cpp:_ZN5doris6format12_GLOBAL__N_118join_debug_stringsINS0_16ColumnDefinitionEZNKS3_12debug_stringB5cxx11EvE3$_1EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISC_EET0_ Line | Count | Source | 217 | 11 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { | 218 | 11 | std::ostringstream out; | 219 | 11 | out << "["; | 220 | 12 | for (size_t i = 0; i < values.size(); ++i) { | 221 | 1 | if (i > 0) { | 222 | 0 | out << ", "; | 223 | 0 | } | 224 | 1 | out << formatter(values[i]); | 225 | 1 | } | 226 | 11 | out << "]"; | 227 | 11 | return out.str(); | 228 | 11 | } |
column_mapper.cpp:_ZN5doris6format12_GLOBAL__N_118join_debug_stringsINS0_16LocalColumnIndexEZNKS3_12debug_stringB5cxx11EvE3$_0EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISC_EET0_ Line | Count | Source | 217 | 2 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { | 218 | 2 | std::ostringstream out; | 219 | 2 | out << "["; | 220 | 3 | for (size_t i = 0; i < values.size(); ++i) { | 221 | 1 | if (i > 0) { | 222 | 0 | out << ", "; | 223 | 0 | } | 224 | 1 | out << formatter(values[i]); | 225 | 1 | } | 226 | 2 | out << "]"; | 227 | 2 | return out.str(); | 228 | 2 | } |
column_mapper.cpp:_ZN5doris6format12_GLOBAL__N_118join_debug_stringsINS0_16ColumnDefinitionEZNKS0_13ColumnMapping12debug_stringB5cxx11EvE3$_0EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISD_EET0_ Line | Count | Source | 217 | 12 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { | 218 | 12 | std::ostringstream out; | 219 | 12 | out << "["; | 220 | 17 | for (size_t i = 0; i < values.size(); ++i) { | 221 | 5 | if (i > 0) { | 222 | 0 | out << ", "; | 223 | 0 | } | 224 | 5 | out << formatter(values[i]); | 225 | 5 | } | 226 | 12 | out << "]"; | 227 | 12 | return out.str(); | 228 | 12 | } |
column_mapper.cpp:_ZN5doris6format12_GLOBAL__N_118join_debug_stringsINS0_13ColumnMappingEZNKS3_12debug_stringB5cxx11EvE3$_1EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISC_EET0_ Line | Count | Source | 217 | 12 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { | 218 | 12 | std::ostringstream out; | 219 | 12 | out << "["; | 220 | 17 | for (size_t i = 0; i < values.size(); ++i) { | 221 | 5 | if (i > 0) { | 222 | 0 | out << ", "; | 223 | 0 | } | 224 | 5 | out << formatter(values[i]); | 225 | 5 | } | 226 | 12 | out << "]"; | 227 | 12 | return out.str(); | 228 | 12 | } |
column_mapper.cpp:_ZN5doris6format12_GLOBAL__N_118join_debug_stringsINS0_13ColumnMappingEZNKS0_17TableColumnMapper12debug_stringB5cxx11EvE3$_0EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISD_EET0_ Line | Count | Source | 217 | 1 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { | 218 | 1 | std::ostringstream out; | 219 | 1 | out << "["; | 220 | 3 | for (size_t i = 0; i < values.size(); ++i) { | 221 | 2 | if (i > 0) { | 222 | 1 | out << ", "; | 223 | 1 | } | 224 | 2 | out << formatter(values[i]); | 225 | 2 | } | 226 | 1 | out << "]"; | 227 | 1 | return out.str(); | 228 | 1 | } |
column_mapper.cpp:_ZN5doris6format12_GLOBAL__N_118join_debug_stringsINS0_13ColumnMappingEZNKS0_17TableColumnMapper12debug_stringB5cxx11EvE3$_1EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISD_EET0_ Line | Count | Source | 217 | 1 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { | 218 | 1 | std::ostringstream out; | 219 | 1 | out << "["; | 220 | 1 | for (size_t i = 0; i < values.size(); ++i) { | 221 | 0 | if (i > 0) { | 222 | 0 | out << ", "; | 223 | 0 | } | 224 | 0 | out << formatter(values[i]); | 225 | 0 | } | 226 | 1 | out << "]"; | 227 | 1 | return out.str(); | 228 | 1 | } |
|
229 | | |
230 | | } // namespace |
231 | | |
232 | | const Field* find_partition_value(const ColumnDefinition& table_column, |
233 | 227 | const std::map<std::string, Field>& partition_values) { |
234 | 361 | const auto find_by_name = [&](const std::string& name) -> const Field* { |
235 | 361 | const auto value_it = partition_values.find(name); |
236 | 361 | return value_it == partition_values.end() ? nullptr : &value_it->second; |
237 | 361 | }; |
238 | 227 | if (const auto* value = find_by_name(table_column.name); value != nullptr) { |
239 | 9 | return value; |
240 | 9 | } |
241 | 218 | if (table_column.has_identifier_name()) { |
242 | 127 | if (const auto* value = find_by_name(table_column.get_identifier_name()); |
243 | 127 | value != nullptr) { |
244 | 0 | return value; |
245 | 0 | } |
246 | 127 | } |
247 | 218 | for (const auto& alias : table_column.name_mapping) { |
248 | 7 | if (const auto* value = find_by_name(alias); value != nullptr) { |
249 | 1 | return value; |
250 | 1 | } |
251 | 7 | } |
252 | 217 | return nullptr; |
253 | 218 | } |
254 | | |
255 | | struct FileSlotRewriteInfo { |
256 | | size_t block_position = 0; |
257 | | DataTypePtr file_type; |
258 | | DataTypePtr table_type; |
259 | | std::string file_column_name; |
260 | | }; |
261 | | |
262 | | struct RewriteContext { |
263 | | RuntimeState* runtime_state = nullptr; |
264 | | std::vector<VExprSPtr> created_exprs {}; |
265 | | |
266 | 124 | void add_created_expr(VExprSPtr expr) { created_exprs.push_back(std::move(expr)); } |
267 | | |
268 | 60 | Status prepare_created_exprs(VExprContext* context) const { |
269 | 60 | DORIS_CHECK(context != nullptr); |
270 | 60 | RowDescriptor row_desc; |
271 | 123 | for (const auto& expr : created_exprs) { |
272 | 123 | if (dynamic_cast<const Cast*>(expr.get()) != nullptr && runtime_state == nullptr) { |
273 | 0 | return Status::InvalidArgument( |
274 | 0 | "RuntimeState is required to prepare rewritten cast expression {}", |
275 | 0 | expr->expr_name()); |
276 | 0 | } |
277 | 123 | RETURN_IF_ERROR(expr->prepare(runtime_state, row_desc, context)); |
278 | 123 | } |
279 | 60 | return Status::OK(); |
280 | 60 | } |
281 | | }; |
282 | | |
283 | | static VExprSPtr create_file_slot_ref(const VSlotRef& slot_ref, |
284 | | const FileSlotRewriteInfo& rewrite_info, |
285 | 66 | RewriteContext* rewrite_context) { |
286 | 66 | auto ref = |
287 | 66 | VSlotRef::create_shared(slot_ref.slot_id(), cast_set<int>(rewrite_info.block_position), |
288 | 66 | -1, rewrite_info.file_type, rewrite_info.file_column_name); |
289 | 66 | rewrite_context->add_created_expr(ref); |
290 | 66 | return ref; |
291 | 66 | } |
292 | | |
293 | 220 | static bool is_cast_expr(const VExprSPtr& expr) { |
294 | 220 | return dynamic_cast<const Cast*>(expr.get()) != nullptr; |
295 | 220 | } |
296 | | |
297 | 142 | static bool is_binary_comparison_predicate(const VExprSPtr& expr) { |
298 | 142 | if (expr == nullptr || expr->get_num_children() != 2 || |
299 | 142 | (expr->node_type() != TExprNodeType::BINARY_PRED && |
300 | 95 | expr->node_type() != TExprNodeType::NULL_AWARE_BINARY_PRED)) { |
301 | 95 | return false; |
302 | 95 | } |
303 | 47 | switch (expr->op()) { |
304 | 1 | case TExprOpcode::EQ: |
305 | 1 | case TExprOpcode::EQ_FOR_NULL: |
306 | 1 | case TExprOpcode::NE: |
307 | 1 | case TExprOpcode::GE: |
308 | 44 | case TExprOpcode::GT: |
309 | 44 | case TExprOpcode::LE: |
310 | 47 | case TExprOpcode::LT: |
311 | 47 | return true; |
312 | 0 | default: |
313 | 0 | return false; |
314 | 47 | } |
315 | 47 | } |
316 | | |
317 | 16 | std::string TableColumnMapperOptions::debug_string() const { |
318 | 16 | std::ostringstream out; |
319 | 16 | out << "TableColumnMapperOptions{mode=" << mapping_mode_to_string(mode) << "}"; |
320 | 16 | return out.str(); |
321 | 16 | } |
322 | | |
323 | 11 | std::string ColumnDefinition::debug_string() const { |
324 | 11 | std::ostringstream out; |
325 | 11 | out << "ColumnDefinition{name=" << name << ", identifier=" << field_debug_string(identifier) |
326 | 11 | << ", name_mapping=" |
327 | 11 | << join_debug_strings(name_mapping, [](const std::string& name) { return name; }) |
328 | 11 | << ", local_id=" << local_id << ", type=" << data_type_debug_string(type) << ", children=" |
329 | 11 | << join_debug_strings(children, |
330 | 11 | [](const ColumnDefinition& child) { return child.debug_string(); }) |
331 | 11 | << ", has_default_expr=" << (default_expr != nullptr) |
332 | 11 | << ", is_partition_key=" << is_partition_key << "}"; |
333 | 11 | return out.str(); |
334 | 11 | } |
335 | | |
336 | 2 | std::string LocalColumnIndex::debug_string() const { |
337 | 2 | std::ostringstream out; |
338 | 2 | out << "LocalColumnIndex{index=" << index << ", project_all_children=" << project_all_children |
339 | 2 | << ", children=" |
340 | 2 | << join_debug_strings(children, |
341 | 2 | [](const LocalColumnIndex& child) { return child.debug_string(); }) |
342 | 2 | << "}"; |
343 | 2 | return out.str(); |
344 | 2 | } |
345 | | |
346 | 12 | std::string ColumnMapping::debug_string() const { |
347 | 12 | std::ostringstream out; |
348 | 12 | out << "ColumnMapping{global_index=" << global_index |
349 | 12 | << ", table_column_name=" << table_column_name << ", file_local_id="; |
350 | 12 | if (file_local_id.has_value()) { |
351 | 7 | out << *file_local_id; |
352 | 7 | } else { |
353 | 5 | out << "null"; |
354 | 5 | } |
355 | 12 | out << ", constant_index="; |
356 | 12 | if (constant_index.has_value()) { |
357 | 5 | out << *constant_index; |
358 | 7 | } else { |
359 | 7 | out << "null"; |
360 | 7 | } |
361 | 12 | out << ", file_column_name=" << file_column_name |
362 | 12 | << ", original_file_type=" << data_type_debug_string(original_file_type) |
363 | 12 | << ", original_file_children=" |
364 | 12 | << join_debug_strings(original_file_children, |
365 | 12 | [](const ColumnDefinition& child) { return child.debug_string(); }) |
366 | 12 | << ", file_type=" << data_type_debug_string(file_type) |
367 | 12 | << ", table_type=" << data_type_debug_string(table_type) |
368 | 12 | << ", has_projection=" << (projection != nullptr) << ", child_mappings=" |
369 | 12 | << join_debug_strings(child_mappings, |
370 | 12 | [](const ColumnMapping& child) { return child.debug_string(); }) |
371 | 12 | << ", is_trivial=" << is_trivial << ", is_constant=" << constant_index.has_value() |
372 | 12 | << ", filter_conversion=" << filter_conversion_type_to_string(filter_conversion) |
373 | 12 | << ", virtual_column_type=" << virtual_column_type_to_string(virtual_column_type) |
374 | 12 | << ", has_default_expr=" << (default_expr != nullptr) << "}"; |
375 | 12 | return out.str(); |
376 | 12 | } |
377 | | |
378 | 1 | std::string TableColumnMapper::debug_string() const { |
379 | 1 | std::ostringstream out; |
380 | 1 | out << "TableColumnMapper{options=" << _options.debug_string() << ", mappings=" |
381 | 1 | << join_debug_strings(_mappings, |
382 | 2 | [](const ColumnMapping& mapping) { return mapping.debug_string(); }) |
383 | 1 | << ", hidden_mappings=" |
384 | 1 | << join_debug_strings(_hidden_mappings, |
385 | 1 | [](const ColumnMapping& mapping) { return mapping.debug_string(); }) |
386 | 1 | << ", constant_count=" << _constant_map.size() << "}"; |
387 | 1 | return out.str(); |
388 | 1 | } |
389 | | |
390 | | static const FileSlotRewriteInfo* find_slot_rewrite_info( |
391 | | const VExprSPtr& expr, |
392 | | const std::map<GlobalIndex, FileSlotRewriteInfo>& global_to_file_slot, |
393 | 73 | const VSlotRef** slot_ref) { |
394 | 73 | if (expr == nullptr) { |
395 | 0 | return nullptr; |
396 | 0 | } |
397 | 73 | VExprSPtr slot_expr = expr; |
398 | 73 | const bool input_is_cast = is_cast_expr(expr) && expr->get_num_children() == 1; |
399 | 73 | if (is_cast_expr(expr) && expr->get_num_children() == 1) { |
400 | 3 | slot_expr = expr->children()[0]; |
401 | 3 | } |
402 | 73 | if (!slot_expr->is_slot_ref()) { |
403 | 40 | return nullptr; |
404 | 40 | } |
405 | 33 | const auto* candidate_slot_ref = assert_cast<const VSlotRef*>(slot_expr.get()); |
406 | 33 | const auto rewrite_it = global_to_file_slot.find(slot_ref_global_index(*candidate_slot_ref)); |
407 | 33 | if (rewrite_it == global_to_file_slot.end()) { |
408 | 0 | return nullptr; |
409 | 0 | } |
410 | 33 | if (input_is_cast && !expr->data_type()->equals(*rewrite_it->second.table_type)) { |
411 | 1 | return nullptr; |
412 | 1 | } |
413 | 32 | if (slot_ref != nullptr) { |
414 | 32 | *slot_ref = candidate_slot_ref; |
415 | 32 | } |
416 | 32 | return &rewrite_it->second; |
417 | 33 | } |
418 | | |
419 | 284 | static bool filter_conversion_has_local_source(FilterConversionType conversion) { |
420 | 284 | switch (conversion) { |
421 | 209 | case FilterConversionType::COPY_DIRECTLY: |
422 | 251 | case FilterConversionType::CAST_FILTER: |
423 | 266 | case FilterConversionType::READER_EXPRESSION: |
424 | 266 | return true; |
425 | 18 | case FilterConversionType::FINALIZE_ONLY: |
426 | 18 | case FilterConversionType::CONSTANT: |
427 | 18 | return false; |
428 | 284 | } |
429 | 0 | return false; |
430 | 284 | } |
431 | | |
432 | | static bool table_filter_has_only_local_entries( |
433 | 75 | const TableFilter& table_filter, const std::map<GlobalIndex, FilterEntry>& filter_entries) { |
434 | 76 | for (const auto global_index : table_filter.global_indices) { |
435 | 76 | const auto entry_it = filter_entries.find(global_index); |
436 | 76 | if (entry_it == filter_entries.end() || !entry_it->second.is_local()) { |
437 | 11 | return false; |
438 | 11 | } |
439 | 76 | } |
440 | 64 | return true; |
441 | 75 | } |
442 | | |
443 | | static VExprSPtr unwrap_literal_for_file_cast(const VExprSPtr& expr, |
444 | 37 | const DataTypePtr& table_type) { |
445 | 37 | if (expr == nullptr) { |
446 | 0 | return nullptr; |
447 | 0 | } |
448 | 37 | if (expr->is_literal()) { |
449 | 37 | return expr; |
450 | 37 | } |
451 | 0 | if (is_cast_expr(expr) && expr->get_num_children() == 1 && expr->children()[0]->is_literal() && |
452 | 0 | expr->children()[0]->data_type()->equals(*table_type)) { |
453 | 0 | return expr->children()[0]; |
454 | 0 | } |
455 | 0 | return nullptr; |
456 | 0 | } |
457 | | |
458 | 0 | static Field literal_field_from_expr(const VExpr& literal_expr) { |
459 | 0 | DORIS_CHECK(literal_expr.is_literal()); |
460 | 0 | const auto* literal = dynamic_cast<const VLiteral*>(&literal_expr); |
461 | 0 | DORIS_CHECK(literal != nullptr); |
462 | 0 | Field field; |
463 | 0 | literal->get_column_ptr()->get(0, field); |
464 | 0 | return field; |
465 | 0 | } |
466 | | |
467 | | // Table filter localization clones an already-prepared table expr and then rewrites it to file |
468 | | // slots. Only split-local literals and BE cast nodes need table-reader-specific clone behavior; |
469 | | // plain slot refs and literals use their own VExpr::clone_node(). |
470 | 375 | static Status clone_table_expr_node(const VExpr& expr, VExprSPtr* cloned_expr) { |
471 | 375 | DORIS_CHECK(cloned_expr != nullptr); |
472 | 375 | if (const auto* split_literal = dynamic_cast<const SplitLocalFileLiteral*>(&expr)) { |
473 | 0 | *cloned_expr = std::make_shared<SplitLocalFileLiteral>( |
474 | 0 | split_literal->data_type(), literal_field_from_expr(expr), |
475 | 0 | split_literal->original_type(), split_literal->original_field()); |
476 | 375 | } else if (const auto* vcast_expr = dynamic_cast<const VCastExpr*>(&expr); |
477 | 375 | vcast_expr != nullptr && vcast_expr->node_type() == TExprNodeType::CAST_EXPR) { |
478 | 0 | *cloned_expr = Cast::create_shared(vcast_expr->data_type()); |
479 | 0 | } |
480 | 375 | return Status::OK(); |
481 | 375 | } |
482 | | |
483 | 98 | Status clone_table_expr_tree(const VExprSPtr& expr, VExprSPtr* cloned_expr) { |
484 | 98 | DORIS_CHECK(cloned_expr != nullptr); |
485 | 98 | if (expr == nullptr) { |
486 | 0 | *cloned_expr = nullptr; |
487 | 0 | return Status::OK(); |
488 | 0 | } |
489 | 98 | return expr->deep_clone(cloned_expr, clone_table_expr_node); |
490 | 98 | } |
491 | | |
492 | | static VExprSPtr original_table_literal(const VExprSPtr& literal_expr, |
493 | 38 | RewriteContext* rewrite_context = nullptr) { |
494 | 38 | DORIS_CHECK(literal_expr != nullptr); |
495 | 38 | DORIS_CHECK(literal_expr->is_literal()); |
496 | 38 | const auto* rewritten_literal = dynamic_cast<const SplitLocalFileLiteral*>(literal_expr.get()); |
497 | 38 | if (rewritten_literal == nullptr) { |
498 | 38 | return literal_expr; |
499 | 38 | } |
500 | 0 | auto literal = VLiteral::create_shared(rewritten_literal->original_type(), |
501 | 0 | rewritten_literal->original_field()); |
502 | 0 | if (rewrite_context != nullptr) { |
503 | 0 | rewrite_context->add_created_expr(literal); |
504 | 0 | } |
505 | 0 | return literal; |
506 | 38 | } |
507 | | |
508 | 74 | static ColumnDefinition hidden_column_from_slot_ref(const VSlotRef& slot_ref) { |
509 | 74 | ColumnDefinition column; |
510 | 74 | column.name = slot_ref.column_name(); |
511 | 74 | column.identifier = Field::create_field<TYPE_STRING>(column.name); |
512 | 74 | column.type = slot_ref.data_type(); |
513 | 74 | return column; |
514 | 74 | } |
515 | | |
516 | | static void collect_top_level_slot_columns(const VExprSPtr& expr, |
517 | 284 | std::map<GlobalIndex, ColumnDefinition>* columns) { |
518 | 284 | DORIS_CHECK(columns != nullptr); |
519 | 284 | if (expr == nullptr) { |
520 | 0 | return; |
521 | 0 | } |
522 | 284 | if (expr->is_slot_ref()) { |
523 | 74 | const auto* slot_ref = assert_cast<const VSlotRef*>(expr.get()); |
524 | 74 | columns->try_emplace(slot_ref_global_index(*slot_ref), |
525 | 74 | hidden_column_from_slot_ref(*slot_ref)); |
526 | 74 | return; |
527 | 74 | } |
528 | 215 | for (const auto& child : expr->children()) { |
529 | 215 | collect_top_level_slot_columns(child, columns); |
530 | 215 | } |
531 | 210 | } |
532 | | |
533 | | static VExprSPtr rewrite_literal_to_file_type(const VExprSPtr& literal_expr, |
534 | | const FileSlotRewriteInfo& rewrite_info, |
535 | 35 | RewriteContext* rewrite_context) { |
536 | 35 | DORIS_CHECK(literal_expr != nullptr); |
537 | 35 | DORIS_CHECK(literal_expr->is_literal()); |
538 | 35 | const auto original_literal = original_table_literal(literal_expr, rewrite_context); |
539 | 35 | const Field original_field = literal_field(original_literal); |
540 | 35 | if (rewrite_info.file_type->equals(*original_literal->data_type())) { |
541 | 5 | return original_literal; |
542 | 5 | } |
543 | 30 | Field file_field; |
544 | 30 | try { |
545 | 30 | convert_field_to_type(original_field, *rewrite_info.file_type, &file_field, |
546 | 30 | original_literal->data_type().get()); |
547 | 30 | } catch (const Exception&) { |
548 | 2 | return nullptr; |
549 | 2 | } |
550 | 28 | if (file_field.is_null()) { |
551 | 0 | return nullptr; |
552 | 0 | } |
553 | 28 | if (file_field.get_type() != remove_nullable(rewrite_info.file_type)->get_primitive_type()) { |
554 | 0 | return nullptr; |
555 | 0 | } |
556 | 28 | auto literal = std::make_shared<SplitLocalFileLiteral>( |
557 | 28 | rewrite_info.file_type, file_field, original_literal->data_type(), original_field); |
558 | 28 | rewrite_context->add_created_expr(literal); |
559 | 28 | return literal; |
560 | 28 | } |
561 | | |
562 | | static bool rewrite_binary_slot_literal_predicate( |
563 | | const VExprSPtr& expr, |
564 | | const std::map<GlobalIndex, FileSlotRewriteInfo>& global_to_file_slot, |
565 | 142 | RewriteContext* rewrite_context) { |
566 | 142 | if (!is_binary_comparison_predicate(expr)) { |
567 | 95 | return false; |
568 | 95 | } |
569 | 47 | auto children = expr->children(); |
570 | 47 | const VSlotRef* slot_ref = nullptr; |
571 | 47 | const FileSlotRewriteInfo* rewrite_info = |
572 | 47 | find_slot_rewrite_info(children[0], global_to_file_slot, &slot_ref); |
573 | 47 | int slot_child_idx = 0; |
574 | 47 | int literal_child_idx = 1; |
575 | 47 | if (rewrite_info == nullptr) { |
576 | 20 | rewrite_info = find_slot_rewrite_info(children[1], global_to_file_slot, &slot_ref); |
577 | 20 | slot_child_idx = 1; |
578 | 20 | literal_child_idx = 0; |
579 | 20 | } |
580 | 47 | if (rewrite_info == nullptr || slot_ref == nullptr) { |
581 | 19 | return false; |
582 | 19 | } |
583 | 28 | auto literal_expr = |
584 | 28 | unwrap_literal_for_file_cast(children[literal_child_idx], rewrite_info->table_type); |
585 | 28 | if (literal_expr == nullptr) { |
586 | 0 | return false; |
587 | 0 | } |
588 | | |
589 | 28 | auto rewritten_literal = |
590 | 28 | rewrite_literal_to_file_type(literal_expr, *rewrite_info, rewrite_context); |
591 | 28 | if (rewritten_literal == nullptr) { |
592 | 1 | children[literal_child_idx] = original_table_literal(literal_expr, rewrite_context); |
593 | 1 | expr->set_children(std::move(children)); |
594 | 1 | return false; |
595 | 1 | } |
596 | | |
597 | 27 | children[slot_child_idx] = create_file_slot_ref(*slot_ref, *rewrite_info, rewrite_context); |
598 | 27 | children[literal_child_idx] = std::move(rewritten_literal); |
599 | 27 | expr->set_children(std::move(children)); |
600 | 27 | return true; |
601 | 28 | } |
602 | | |
603 | | static bool rewrite_in_slot_literal_predicate( |
604 | | const VExprSPtr& expr, |
605 | | const std::map<GlobalIndex, FileSlotRewriteInfo>& global_to_file_slot, |
606 | 115 | RewriteContext* rewrite_context) { |
607 | 115 | if (expr->node_type() != TExprNodeType::IN_PRED || expr->get_num_children() < 2) { |
608 | 109 | return false; |
609 | 109 | } |
610 | 6 | auto children = expr->children(); |
611 | 6 | const VSlotRef* slot_ref = nullptr; |
612 | 6 | const FileSlotRewriteInfo* rewrite_info = |
613 | 6 | find_slot_rewrite_info(children[0], global_to_file_slot, &slot_ref); |
614 | 6 | if (rewrite_info == nullptr || slot_ref == nullptr) { |
615 | 2 | return false; |
616 | 2 | } |
617 | | |
618 | 4 | VExprSPtrs rewritten_literals; |
619 | 4 | rewritten_literals.reserve(children.size() - 1); |
620 | 10 | for (size_t child_idx = 1; child_idx < children.size(); ++child_idx) { |
621 | 7 | auto literal_expr = |
622 | 7 | unwrap_literal_for_file_cast(children[child_idx], rewrite_info->table_type); |
623 | 7 | if (literal_expr == nullptr) { |
624 | 0 | return false; |
625 | 0 | } |
626 | 7 | auto rewritten_literal = |
627 | 7 | rewrite_literal_to_file_type(literal_expr, *rewrite_info, rewrite_context); |
628 | 7 | if (rewritten_literal == nullptr) { |
629 | 3 | for (size_t restore_idx = 1; restore_idx < children.size(); ++restore_idx) { |
630 | 2 | auto restore_literal = unwrap_literal_for_file_cast(children[restore_idx], |
631 | 2 | rewrite_info->table_type); |
632 | 2 | if (restore_literal != nullptr) { |
633 | 2 | children[restore_idx] = |
634 | 2 | original_table_literal(restore_literal, rewrite_context); |
635 | 2 | } |
636 | 2 | } |
637 | 1 | expr->set_children(std::move(children)); |
638 | 1 | return false; |
639 | 1 | } |
640 | 6 | rewritten_literals.push_back(std::move(rewritten_literal)); |
641 | 6 | } |
642 | | |
643 | 3 | children[0] = create_file_slot_ref(*slot_ref, *rewrite_info, rewrite_context); |
644 | 9 | for (size_t literal_idx = 0; literal_idx < rewritten_literals.size(); ++literal_idx) { |
645 | 6 | children[literal_idx + 1] = std::move(rewritten_literals[literal_idx]); |
646 | 6 | } |
647 | 3 | expr->set_children(std::move(children)); |
648 | 3 | return true; |
649 | 4 | } |
650 | | |
651 | | static VExprSPtr create_file_struct_child_name_literal(const std::string& file_child_name, |
652 | 23 | RewriteContext* rewrite_context) { |
653 | 23 | auto literal = VLiteral::create_shared(std::make_shared<DataTypeString>(), |
654 | 23 | Field::create_field<TYPE_STRING>(file_child_name)); |
655 | 23 | rewrite_context->add_created_expr(literal); |
656 | 23 | return literal; |
657 | 23 | } |
658 | | |
659 | | static bool needs_complex_file_slot_cast(const DataTypePtr& file_type, |
660 | 9 | const DataTypePtr& table_type) { |
661 | 9 | if (file_type == nullptr || table_type == nullptr || file_type->equals(*table_type)) { |
662 | 0 | return false; |
663 | 0 | } |
664 | 9 | const auto file_nested_type = remove_nullable(file_type); |
665 | 9 | const auto table_nested_type = remove_nullable(table_type); |
666 | 9 | if (file_nested_type->equals(*table_nested_type)) { |
667 | 0 | return false; |
668 | 0 | } |
669 | 9 | return is_complex_type(file_nested_type->get_primitive_type()) || |
670 | 9 | is_complex_type(table_nested_type->get_primitive_type()); |
671 | 9 | } |
672 | | |
673 | 23 | static bool collect_struct_element_chain(const VExprSPtr& expr, std::vector<VExprSPtr>* chain) { |
674 | 23 | DORIS_CHECK(chain != nullptr); |
675 | 23 | if (!is_struct_element_expr(expr)) { |
676 | 0 | return false; |
677 | 0 | } |
678 | 23 | const auto& parent = expr->children()[0]; |
679 | 23 | if (is_struct_element_expr(parent)) { |
680 | 3 | if (!collect_struct_element_chain(parent, chain)) { |
681 | 0 | return false; |
682 | 0 | } |
683 | 20 | } else if (!parent->is_slot_ref()) { |
684 | | // Only support file-local rewrite for struct child chains rooted directly at a top-level |
685 | | // slot, for example `element_at(s, 'a')` or `element_at(element_at(s, 'a'), 'b')`. |
686 | | // |
687 | | // Do not localize computed complex parents such as |
688 | | // `element_at(element_at(map_values(m), 1), 'full_name')`. The intermediate map/array |
689 | | // result has already been reshaped by scan projection and may have a different child order |
690 | | // from the table expression. Partially rewriting that expression against the file block can |
691 | | // silently evaluate the wrong struct child and filter out valid rows. Those predicates must |
692 | | // remain as table-level conjuncts and be evaluated after TableReader materialization. |
693 | 0 | return false; |
694 | 0 | } |
695 | 23 | chain->push_back(expr); |
696 | 23 | return true; |
697 | 23 | } |
698 | | |
699 | | static bool rewrite_struct_element_path_to_file_expr( |
700 | | const VExprSPtr& expr, const std::vector<ColumnMapping>& mappings, |
701 | | const std::map<GlobalIndex, FileSlotRewriteInfo>& global_to_file_slot, |
702 | 23 | RewriteContext* rewrite_context) { |
703 | 23 | ResolvedNestedStructPath resolved; |
704 | 23 | if (!resolve_nested_struct_expr_for_file(expr, mappings, &resolved)) { |
705 | 3 | return false; |
706 | 3 | } |
707 | | |
708 | 20 | std::vector<VExprSPtr> struct_element_chain; |
709 | 20 | if (!collect_struct_element_chain(expr, &struct_element_chain) || |
710 | 20 | struct_element_chain.size() != resolved.file_child_names.size() || |
711 | 20 | struct_element_chain.size() != resolved.file_child_types.size()) { |
712 | 0 | return false; |
713 | 0 | } |
714 | | |
715 | 20 | auto root_children = struct_element_chain.front()->children(); |
716 | 20 | if (!root_children[0]->is_slot_ref()) { |
717 | 0 | return false; |
718 | 0 | } |
719 | 20 | const auto* slot_ref = assert_cast<const VSlotRef*>(root_children[0].get()); |
720 | 20 | const auto rewrite_it = global_to_file_slot.find(slot_ref_global_index(*slot_ref)); |
721 | 20 | if (rewrite_it == global_to_file_slot.end()) { |
722 | 0 | return false; |
723 | 0 | } |
724 | | |
725 | | // File-local conjuncts are prepared against the file-reader Block, so both the root slot and |
726 | | // every struct selector must be expressed in file schema terms. For a renamed Iceberg field, |
727 | | // keeping the table selector would prepare `element_at(file_struct<rename>, 'renamed')` and |
728 | | // fail before any rows are read. Rewrite the whole chain while ColumnMapping still preserves |
729 | | // the table-to-file relationship. Example: |
730 | | // table filter: element_at(element_at(s, 'renamed_parent'), 'renamed_leaf') |
731 | | // old file: s<parent<leaf>> |
732 | | // file filter: element_at(element_at(s, 'parent'), 'leaf') |
733 | 20 | root_children[0] = create_file_slot_ref(*slot_ref, rewrite_it->second, rewrite_context); |
734 | 20 | struct_element_chain.front()->set_children(std::move(root_children)); |
735 | 43 | for (size_t idx = 0; idx < struct_element_chain.size(); ++idx) { |
736 | 23 | auto children = struct_element_chain[idx]->children(); |
737 | 23 | children[1] = create_file_struct_child_name_literal(resolved.file_child_names[idx], |
738 | 23 | rewrite_context); |
739 | 23 | struct_element_chain[idx]->set_children(std::move(children)); |
740 | | // The selector name and the expression return type must be moved to file schema together. |
741 | | // Example: |
742 | | // table filter: element_at(element_at(s, 'new_a'), 'new_aa') = 50 |
743 | | // old file: s.new_a STRUCT<aa, bb> |
744 | | // file filter: element_at(element_at(s, 'new_a'), 'aa') = 50 |
745 | | // |
746 | | // If the inner element_at keeps the table return type STRUCT<new_aa, bb>, preparing the |
747 | | // outer element_at(..., 'aa') fails before scanning because `aa` is not a table field. |
748 | 23 | struct_element_chain[idx]->data_type() = resolved.file_child_types[idx]; |
749 | 23 | } |
750 | 20 | return true; |
751 | 20 | } |
752 | | |
753 | | static VExprSPtr rewrite_table_expr_to_file_expr( |
754 | | const VExprSPtr& expr, |
755 | | const std::map<GlobalIndex, FileSlotRewriteInfo>& global_to_file_slot, |
756 | | const std::vector<ColumnMapping>& filter_mappings, RewriteContext* rewrite_context, |
757 | 143 | bool* can_localize) { |
758 | 143 | if (expr == nullptr) { |
759 | 0 | return nullptr; |
760 | 0 | } |
761 | 143 | DORIS_CHECK(rewrite_context != nullptr); |
762 | 143 | DORIS_CHECK(can_localize != nullptr); |
763 | 143 | if (auto* runtime_filter = dynamic_cast<RuntimeFilterExpr*>(expr.get()); |
764 | 143 | runtime_filter != nullptr) { |
765 | 1 | auto impl = runtime_filter->get_impl(); |
766 | 1 | if (impl == nullptr) { |
767 | 0 | *can_localize = false; |
768 | 0 | return expr; |
769 | 0 | } |
770 | 1 | auto localized_impl = rewrite_table_expr_to_file_expr( |
771 | 1 | impl, global_to_file_slot, filter_mappings, rewrite_context, can_localize); |
772 | 1 | if (!*can_localize) { |
773 | 0 | return expr; |
774 | 0 | } |
775 | 1 | runtime_filter->set_impl(std::move(localized_impl)); |
776 | 1 | return expr; |
777 | 1 | } |
778 | 142 | if (rewrite_binary_slot_literal_predicate(expr, global_to_file_slot, rewrite_context)) { |
779 | 27 | return expr; |
780 | 27 | } |
781 | 115 | if (rewrite_in_slot_literal_predicate(expr, global_to_file_slot, rewrite_context)) { |
782 | 3 | return expr; |
783 | 3 | } |
784 | 112 | if (is_struct_element_expr(expr)) { |
785 | 23 | if (!rewrite_struct_element_path_to_file_expr(expr, filter_mappings, global_to_file_slot, |
786 | 23 | rewrite_context)) { |
787 | | // The scanner still evaluates the original table-level conjunct after TableReader |
788 | | // finalizes the output block. Skipping an unlocalizable file conjunct is therefore |
789 | | // safer than preparing a partially rewritten expression against the wrong struct |
790 | | // layout. In particular, do not generate file-local conjuncts for computed complex |
791 | | // parents such as `element_at(element_at(map_values(m), 1), 'field')`; only direct |
792 | | // slot-rooted struct chains are supported here. |
793 | 3 | *can_localize = false; |
794 | 3 | } |
795 | 23 | return expr; |
796 | 23 | } |
797 | 89 | if (expr->is_slot_ref()) { |
798 | 15 | const auto* slot_ref = assert_cast<const VSlotRef*>(expr.get()); |
799 | 15 | const auto rewrite_it = global_to_file_slot.find(slot_ref_global_index(*slot_ref)); |
800 | 15 | if (rewrite_it != global_to_file_slot.end()) { |
801 | 15 | const auto& rewrite_info = rewrite_it->second; |
802 | 15 | auto file_slot = create_file_slot_ref(*slot_ref, rewrite_info, rewrite_context); |
803 | 15 | if (rewrite_info.file_type->equals(*rewrite_info.table_type)) { |
804 | 7 | return file_slot; |
805 | 7 | } |
806 | 8 | if (needs_complex_file_slot_cast(rewrite_info.file_type, rewrite_info.table_type)) { |
807 | | // Generic file-local expressions cannot safely cast an evolved complex file slot |
808 | | // back to the table type. Example: |
809 | | // |
810 | | // table filter: ARRAY_CONTAINS(MAP_KEYS(m), 'person5') |
811 | | // old file: m MAP<STRING, STRUCT<name, age>> |
812 | | // table: m MAP<STRING, STRUCT<age, full_name, gender>> |
813 | | // |
814 | | // Although MAP_KEYS only reads the key column, wrapping the file slot as |
815 | | // `CAST(file_m AS table_m)` forces the value struct cast first and fails because |
816 | | // the old and new value structs have different fields. Keep such filters at the |
817 | | // table level, where TableReader materializes the evolved complex value before |
818 | | // Scanner evaluates the original conjunct. Direct slot-rooted struct child paths |
819 | | // are handled by rewrite_struct_element_path_to_file_expr() above. |
820 | 1 | *can_localize = false; |
821 | 1 | return expr; |
822 | 1 | } |
823 | 7 | auto cast_expr = Cast::create_shared(rewrite_info.table_type); |
824 | 7 | cast_expr->add_child(std::move(file_slot)); |
825 | 7 | rewrite_context->add_created_expr(cast_expr); |
826 | 7 | return cast_expr; |
827 | 8 | } |
828 | 0 | return expr; |
829 | 15 | } |
830 | | // The input is a split-local cloned tree. A previous split-local clone may already have |
831 | | // inserted Cast(slot). Keep that rewrite idempotent: rewrite the cast child from table slot to |
832 | | // the current split's file slot, and drop the cast when the current split no longer needs it. |
833 | 74 | if (is_cast_expr(expr) && expr->get_num_children() == 1) { |
834 | 4 | const auto& child = expr->children()[0]; |
835 | 4 | if (child->is_slot_ref()) { |
836 | 2 | const auto* slot_ref = assert_cast<const VSlotRef*>(child.get()); |
837 | 2 | const auto rewrite_it = global_to_file_slot.find(slot_ref_global_index(*slot_ref)); |
838 | 2 | if (rewrite_it != global_to_file_slot.end() && |
839 | 2 | expr->data_type()->equals(*rewrite_it->second.table_type)) { |
840 | 1 | auto rewritten_child = |
841 | 1 | create_file_slot_ref(*slot_ref, rewrite_it->second, rewrite_context); |
842 | 1 | if (rewrite_it->second.file_type->equals(*rewrite_it->second.table_type)) { |
843 | 0 | return rewritten_child; |
844 | 0 | } |
845 | 1 | if (needs_complex_file_slot_cast(rewrite_it->second.file_type, |
846 | 1 | rewrite_it->second.table_type)) { |
847 | 0 | *can_localize = false; |
848 | 0 | return expr; |
849 | 0 | } |
850 | 1 | expr->set_children({std::move(rewritten_child)}); |
851 | 1 | return expr; |
852 | 1 | } |
853 | 2 | } |
854 | 4 | } |
855 | | |
856 | 73 | VExprSPtrs rewritten_children; |
857 | 73 | rewritten_children.reserve(expr->children().size()); |
858 | 78 | for (const auto& child : expr->children()) { |
859 | 78 | rewritten_children.push_back(rewrite_table_expr_to_file_expr( |
860 | 78 | child, global_to_file_slot, filter_mappings, rewrite_context, can_localize)); |
861 | 78 | } |
862 | 73 | expr->set_children(std::move(rewritten_children)); |
863 | 73 | return expr; |
864 | 74 | } |
865 | | |
866 | | static constexpr const char* ROW_LINEAGE_ROW_ID = "_row_id"; |
867 | | static constexpr const char* ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER = "_last_updated_sequence_number"; |
868 | | static constexpr int32_t ROW_LINEAGE_ROW_ID_FIELD_ID = 2147483540; |
869 | | static constexpr int32_t ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER_FIELD_ID = 2147483539; |
870 | | |
871 | 152 | static TableVirtualColumnType row_lineage_virtual_column_type(const std::string& column_name) { |
872 | 152 | if (column_name == ROW_LINEAGE_ROW_ID) { |
873 | 2 | return TableVirtualColumnType::ROW_ID; |
874 | 2 | } |
875 | 150 | if (column_name == ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER) { |
876 | 2 | return TableVirtualColumnType::LAST_UPDATED_SEQUENCE_NUMBER; |
877 | 2 | } |
878 | 148 | return TableVirtualColumnType::INVALID; |
879 | 150 | } |
880 | | |
881 | | static TableVirtualColumnType row_lineage_virtual_column_type_by_field_id( |
882 | 75 | const ColumnDefinition& column) { |
883 | 75 | if (!column.has_identifier_field_id()) { |
884 | 5 | return TableVirtualColumnType::INVALID; |
885 | 5 | } |
886 | 70 | switch (column.get_identifier_field_id()) { |
887 | 10 | case ROW_LINEAGE_ROW_ID_FIELD_ID: |
888 | 10 | return TableVirtualColumnType::ROW_ID; |
889 | 9 | case ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER_FIELD_ID: |
890 | 9 | return TableVirtualColumnType::LAST_UPDATED_SEQUENCE_NUMBER; |
891 | 51 | default: |
892 | 51 | return TableVirtualColumnType::INVALID; |
893 | 70 | } |
894 | 70 | } |
895 | | |
896 | | static TableVirtualColumnType row_lineage_virtual_column_type(const ColumnDefinition& column, |
897 | 227 | TableColumnMappingMode mode) { |
898 | 227 | switch (mode) { |
899 | 75 | case TableColumnMappingMode::BY_FIELD_ID: |
900 | 75 | return row_lineage_virtual_column_type_by_field_id(column); |
901 | 130 | case TableColumnMappingMode::BY_NAME: |
902 | 152 | case TableColumnMappingMode::BY_INDEX: |
903 | 152 | return row_lineage_virtual_column_type(column.name); |
904 | 227 | } |
905 | 0 | return TableVirtualColumnType::INVALID; |
906 | 227 | } |
907 | | |
908 | | // Returns true when the current file type is not the exact nested type the scan should expose. |
909 | | // This is about building the projected file-side type/projection, not about whether TableReader |
910 | | // later needs to rematerialize the complex value back to table layout. |
911 | 104 | static bool needs_projected_file_type_rebuild(const ColumnMapping& mapping) { |
912 | 104 | if (!is_complex_type(mapping.file_type->get_primitive_type())) { |
913 | 26 | return false; |
914 | 26 | } |
915 | 78 | if (mapping.child_mappings.empty()) { |
916 | 0 | return false; |
917 | 0 | } |
918 | 78 | DORIS_CHECK(mapping.file_type != nullptr); |
919 | 78 | DORIS_CHECK(mapping.table_type != nullptr); |
920 | 78 | if (remove_nullable(mapping.file_type)->get_primitive_type() != |
921 | 78 | remove_nullable(mapping.table_type)->get_primitive_type()) { |
922 | 0 | return true; |
923 | 0 | } |
924 | 78 | if (!mapping.table_type->equals(*mapping.file_type)) { |
925 | 61 | return true; |
926 | 61 | } |
927 | 30 | for (const auto& child_mapping : mapping.child_mappings) { |
928 | | // Rename-only child mappings do not change the file-side projected shape. If field-id |
929 | | // matching maps table child `renamed_b` to file child `b`, the file reader can still expose |
930 | | // the original file type as long as child count/order/types are unchanged. |
931 | 30 | if (!child_mapping.file_local_id.has_value() || |
932 | 30 | needs_projected_file_type_rebuild(child_mapping)) { |
933 | 1 | return true; |
934 | 1 | } |
935 | 30 | } |
936 | 16 | return false; |
937 | 17 | } |
938 | | |
939 | | static std::optional<size_t> file_child_ordinal_in_scan_type(const ColumnMapping& mapping, |
940 | 76 | const ColumnMapping& child_mapping) { |
941 | 76 | if (!child_mapping.file_local_id.has_value()) { |
942 | 1 | return std::nullopt; |
943 | 1 | } |
944 | 75 | const auto& file_children = !mapping.projected_file_children.empty() |
945 | 75 | ? mapping.projected_file_children |
946 | 75 | : mapping.original_file_children; |
947 | 90 | const auto child_it = std::ranges::find_if(file_children, [&](const ColumnDefinition& child) { |
948 | 90 | return child.file_local_id() == *child_mapping.file_local_id; |
949 | 90 | }); |
950 | 75 | if (child_it == file_children.end()) { |
951 | 0 | return std::nullopt; |
952 | 0 | } |
953 | 75 | return static_cast<size_t>(std::distance(file_children.begin(), child_it)); |
954 | 75 | } |
955 | | |
956 | 375 | static bool needs_complex_rematerialize(const ColumnMapping& mapping) { |
957 | 375 | if (mapping.child_mappings.empty()) { |
958 | 286 | return false; |
959 | 286 | } |
960 | 89 | if (mapping.table_type == nullptr || mapping.file_type == nullptr || |
961 | 89 | !mapping.table_type->equals(*mapping.file_type)) { |
962 | 24 | return true; |
963 | 24 | } |
964 | 133 | for (size_t table_child_idx = 0; table_child_idx < mapping.child_mappings.size(); |
965 | 76 | ++table_child_idx) { |
966 | 76 | const auto& child_mapping = mapping.child_mappings[table_child_idx]; |
967 | 76 | const auto file_child_idx = file_child_ordinal_in_scan_type(mapping, child_mapping); |
968 | 76 | if (!file_child_idx.has_value() || *file_child_idx != table_child_idx || |
969 | 76 | needs_complex_rematerialize(child_mapping) || |
970 | 76 | (child_mapping.table_type != nullptr && child_mapping.file_type != nullptr && |
971 | 68 | !child_mapping.table_type->equals(*child_mapping.file_type))) { |
972 | 8 | return true; |
973 | 8 | } |
974 | 76 | } |
975 | 57 | return false; |
976 | 65 | } |
977 | | |
978 | 394 | static bool mapping_can_use_file_column_directly(const ColumnMapping& mapping) { |
979 | 394 | if (mapping.table_type == nullptr || mapping.file_type == nullptr) { |
980 | 0 | return false; |
981 | 0 | } |
982 | 394 | const auto table_type = remove_nullable(mapping.table_type); |
983 | 394 | const auto file_type = remove_nullable(mapping.file_type); |
984 | 394 | const bool same_timestamptz_with_different_scale = |
985 | 394 | table_type->get_primitive_type() == TYPE_TIMESTAMPTZ && |
986 | 394 | file_type->get_primitive_type() == TYPE_TIMESTAMPTZ; |
987 | 394 | if (!mapping.table_type->equals(*mapping.file_type) && !same_timestamptz_with_different_scale) { |
988 | 131 | return false; |
989 | 131 | } |
990 | 263 | return !needs_complex_rematerialize(mapping); |
991 | 394 | } |
992 | | |
993 | | static const ColumnDefinition* find_file_child_for_mapping(const ColumnDefinition& table_child, |
994 | | const ColumnDefinition& file_parent, |
995 | | TableColumnMappingMode mode, |
996 | | size_t table_child_idx, |
997 | 118 | bool allow_ordinal_fallback) { |
998 | 118 | const auto file_parent_type = remove_nullable(file_parent.type)->get_primitive_type(); |
999 | 118 | switch (file_parent_type) { |
1000 | 9 | case TYPE_ARRAY: |
1001 | 9 | DORIS_CHECK(file_parent.children.size() == 1); |
1002 | 9 | return &file_parent.children[0]; |
1003 | 19 | case TYPE_MAP: |
1004 | 19 | DORIS_CHECK(file_parent.children.size() == 2); |
1005 | 19 | if (table_child.name == "key") { |
1006 | 6 | return &file_parent.children[0]; |
1007 | 6 | } |
1008 | 13 | if (table_child.name == "value") { |
1009 | 13 | return &file_parent.children[1]; |
1010 | 13 | } |
1011 | 0 | if (table_child.local_id == 0 || table_child.local_id == 1) { |
1012 | 0 | return &file_parent.children[table_child.local_id]; |
1013 | 0 | } |
1014 | 0 | return nullptr; |
1015 | 90 | default: |
1016 | | // Hive BY_INDEX is a top-level column matching rule. Once a complex root is selected by |
1017 | | // file position, nested struct children follow Hive reader's historical name matching |
1018 | | // semantics; their integer identifiers can be field ids, not file positions. |
1019 | 90 | const auto nested_mode = |
1020 | 90 | mode == TableColumnMappingMode::BY_INDEX ? TableColumnMappingMode::BY_NAME : mode; |
1021 | 90 | if (const auto* file_child = |
1022 | 90 | matcher_for_mode(nested_mode).find(table_child, file_parent.children); |
1023 | 90 | file_child != nullptr) { |
1024 | 71 | return file_child; |
1025 | 71 | } |
1026 | 19 | if (allow_ordinal_fallback && mode == TableColumnMappingMode::BY_FIELD_ID && |
1027 | 19 | !table_child.has_identifier_field_id()) { |
1028 | | // Synthetic children are derived from the table DataType when nested ColumnDefinition |
1029 | | // metadata has been pruned away. They do not carry Iceberg field ids, so try a name |
1030 | | // match before falling back to ordinal order. Example: |
1031 | | // table value type: Struct(age, full_name, gender) |
1032 | | // old file value: Struct(name, age) |
1033 | | // Name matching keeps `age -> age`; the later unused-child fallback can then map the |
1034 | | // renamed `full_name -> name` instead of consuming `age` twice. |
1035 | 3 | if (const auto* file_child = NameMatcher().find(table_child, file_parent.children); |
1036 | 3 | file_child != nullptr) { |
1037 | 1 | return file_child; |
1038 | 1 | } |
1039 | 3 | } |
1040 | | // Some callers only carry the full complex DataType for a projected table column, without |
1041 | | // expanded nested ColumnDefinitions. In that case we can still preserve full materialization |
1042 | | // by walking table/file struct fields by ordinal. This is a fallback only: explicit |
1043 | | // ColumnDefinition children keep using the requested table-format matching rule, which is |
1044 | | // required for precise schema evolution. |
1045 | 18 | if (allow_ordinal_fallback && table_child_idx < file_parent.children.size()) { |
1046 | 3 | return &file_parent.children[table_child_idx]; |
1047 | 3 | } |
1048 | 15 | return nullptr; |
1049 | 118 | } |
1050 | 118 | } |
1051 | | |
1052 | | static ColumnDefinition synthetic_child_definition(const std::string& name, DataTypePtr type, |
1053 | 13 | int32_t local_id) { |
1054 | 13 | ColumnDefinition child; |
1055 | 13 | child.identifier = Field::create_field<TYPE_STRING>(name); |
1056 | 13 | child.local_id = local_id; |
1057 | 13 | child.name = name; |
1058 | 13 | child.type = std::move(type); |
1059 | 13 | return child; |
1060 | 13 | } |
1061 | | |
1062 | | static std::vector<ColumnDefinition> synthesize_complex_children_from_type( |
1063 | 4 | const DataTypePtr& type) { |
1064 | 4 | std::vector<ColumnDefinition> children; |
1065 | 4 | if (type == nullptr) { |
1066 | 0 | return children; |
1067 | 0 | } |
1068 | 4 | const auto nested_type = remove_nullable(type); |
1069 | 4 | switch (nested_type->get_primitive_type()) { |
1070 | 0 | case TYPE_ARRAY: { |
1071 | 0 | const auto* array_type = assert_cast<const DataTypeArray*>(nested_type.get()); |
1072 | 0 | children.push_back(synthetic_child_definition("element", array_type->get_nested_type(), 0)); |
1073 | 0 | break; |
1074 | 0 | } |
1075 | 1 | case TYPE_MAP: { |
1076 | 1 | const auto* map_type = assert_cast<const DataTypeMap*>(nested_type.get()); |
1077 | 1 | children.push_back(synthetic_child_definition("key", map_type->get_key_type(), 0)); |
1078 | 1 | children.push_back(synthetic_child_definition("value", map_type->get_value_type(), 1)); |
1079 | 1 | break; |
1080 | 0 | } |
1081 | 3 | case TYPE_STRUCT: { |
1082 | 3 | const auto* struct_type = assert_cast<const DataTypeStruct*>(nested_type.get()); |
1083 | 3 | children.reserve(struct_type->get_elements().size()); |
1084 | 12 | for (size_t idx = 0; idx < struct_type->get_elements().size(); ++idx) { |
1085 | 9 | children.push_back(synthetic_child_definition(struct_type->get_element_name(idx), |
1086 | 9 | struct_type->get_element(idx), |
1087 | 9 | cast_set<int32_t>(idx))); |
1088 | 9 | } |
1089 | 3 | break; |
1090 | 0 | } |
1091 | 0 | default: |
1092 | 0 | break; |
1093 | 4 | } |
1094 | 4 | return children; |
1095 | 4 | } |
1096 | | |
1097 | | static bool has_table_child_named(const std::vector<ColumnDefinition>& children, |
1098 | 15 | std::string_view name) { |
1099 | 17 | return std::ranges::any_of(children, [&](const ColumnDefinition& child) { |
1100 | 17 | return std::string_view(child.name) == name; |
1101 | 17 | }); |
1102 | 15 | } |
1103 | | |
1104 | | static void complete_required_complex_children_from_type(const DataTypePtr& type, |
1105 | 57 | std::vector<ColumnDefinition>* children) { |
1106 | 57 | DORIS_CHECK(children != nullptr); |
1107 | 57 | if (type == nullptr) { |
1108 | 0 | return; |
1109 | 0 | } |
1110 | 57 | const auto nested_type = remove_nullable(type); |
1111 | 57 | switch (nested_type->get_primitive_type()) { |
1112 | 11 | case TYPE_MAP: { |
1113 | 11 | const auto* map_type = assert_cast<const DataTypeMap*>(nested_type.get()); |
1114 | | // MAP key/value are structural children, not independently materializable table fields. |
1115 | | // A key-only projection can still be attached to a whole-map output root, for example: |
1116 | | // SELECT * FROM t WHERE ARRAY_CONTAINS(MAP_KEYS(new_map_column), 'person5') |
1117 | | // |
1118 | | // In that shape the scanner keeps the value stream readable, but the table projection can |
1119 | | // carry only the key child. Add the missing value child so recursive mapping can evolve the |
1120 | | // value type instead of letting TableReader cast old/new value structs directly. |
1121 | 11 | if (has_table_child_named(*children, "key") && !has_table_child_named(*children, "value")) { |
1122 | 2 | children->push_back(synthetic_child_definition("value", map_type->get_value_type(), 1)); |
1123 | 2 | } |
1124 | 11 | break; |
1125 | 0 | } |
1126 | 8 | case TYPE_ARRAY: |
1127 | | // ARRAY has only one required structural child (`element`), so a non-empty projection is |
1128 | | // already rooted at the element path. |
1129 | 8 | break; |
1130 | 38 | case TYPE_STRUCT: |
1131 | | // STRUCT children are real fields and must remain prunable. Completing missing struct |
1132 | | // fields here would turn `SELECT s.a` into a full-struct read and undo nested projection. |
1133 | 38 | break; |
1134 | 0 | default: |
1135 | 0 | break; |
1136 | 57 | } |
1137 | 57 | } |
1138 | | |
1139 | 76 | static Status validate_file_schema_children(const ColumnDefinition& file_field) { |
1140 | 76 | if (file_field.type == nullptr) { |
1141 | 0 | return Status::InternalError("File column '{}' has null type", file_field.name); |
1142 | 0 | } |
1143 | 76 | const auto nested_type = remove_nullable(file_field.type); |
1144 | 76 | size_t expected_children = 0; |
1145 | 76 | bool complex_with_fixed_children = true; |
1146 | 76 | switch (nested_type->get_primitive_type()) { |
1147 | 9 | case TYPE_ARRAY: |
1148 | 9 | expected_children = 1; |
1149 | 9 | break; |
1150 | 13 | case TYPE_MAP: |
1151 | 13 | expected_children = 2; |
1152 | 13 | break; |
1153 | 54 | case TYPE_STRUCT: |
1154 | 54 | expected_children = |
1155 | 54 | assert_cast<const DataTypeStruct*>(nested_type.get())->get_elements().size(); |
1156 | 54 | break; |
1157 | 0 | default: |
1158 | 0 | complex_with_fixed_children = false; |
1159 | 0 | break; |
1160 | 76 | } |
1161 | 76 | if (!complex_with_fixed_children || file_field.children.size() == expected_children) { |
1162 | 75 | return Status::OK(); |
1163 | 75 | } |
1164 | 1 | return Status::InternalError( |
1165 | 1 | "Malformed complex file schema for column '{}': type={}, expected_children={}, " |
1166 | 1 | "actual_children={}", |
1167 | 1 | file_field.name, file_field.type->get_name(), expected_children, |
1168 | 1 | file_field.children.size()); |
1169 | 76 | } |
1170 | | |
1171 | 181 | static bool has_projected_file_children(const ColumnMapping& mapping) { |
1172 | 181 | if (mapping.original_file_children.empty() || mapping.projected_file_children.empty()) { |
1173 | 135 | return false; |
1174 | 135 | } |
1175 | 46 | if (mapping.original_file_children.size() != mapping.projected_file_children.size()) { |
1176 | 21 | return true; |
1177 | 21 | } |
1178 | 65 | for (size_t idx = 0; idx < mapping.original_file_children.size(); ++idx) { |
1179 | 40 | if (mapping.original_file_children[idx].file_local_id() != |
1180 | 40 | mapping.projected_file_children[idx].file_local_id()) { |
1181 | 0 | return true; |
1182 | 0 | } |
1183 | 40 | } |
1184 | 25 | return false; |
1185 | 25 | } |
1186 | | |
1187 | 181 | static bool needs_nested_file_projection(const ColumnMapping& mapping) { |
1188 | 181 | if (has_projected_file_children(mapping)) { |
1189 | | // Return True if the projected child column is missing / re-ordered |
1190 | 21 | return true; |
1191 | 21 | } |
1192 | 160 | return std::ranges::any_of(mapping.child_mappings, [](const ColumnMapping& child_mapping) { |
1193 | 40 | return needs_nested_file_projection(child_mapping); |
1194 | 40 | }); |
1195 | 181 | } |
1196 | | |
1197 | | static Status build_complex_projection(const ColumnMapping& mapping, LocalColumnIndex* projection); |
1198 | | |
1199 | | // Build the projected file children/type according to the pruned complex projection. For example, |
1200 | | // if we have a struct column `s` with children `id` and `name`, and the projection only keeps |
1201 | | // `s.name`, then the file reader should expose `STRUCT<name ...>`. |
1202 | | static Status rebuild_projected_file_children_and_type( |
1203 | | const DataTypePtr& file_type, const std::vector<ColumnDefinition>& original_file_children, |
1204 | | const std::vector<ColumnMapping>& child_mappings, |
1205 | 62 | std::vector<ColumnDefinition>* projected_file_children, DataTypePtr* projected_type) { |
1206 | 62 | DORIS_CHECK(file_type != nullptr); |
1207 | 62 | DORIS_CHECK(projected_file_children != nullptr); |
1208 | 62 | DORIS_CHECK(projected_type != nullptr); |
1209 | 62 | ColumnDefinition field; |
1210 | 62 | field.type = file_type; |
1211 | 62 | field.children = original_file_children; |
1212 | 62 | LocalColumnIndex projection = LocalColumnIndex::partial_local(-1); |
1213 | 62 | projection.children.reserve(child_mappings.size()); |
1214 | 80 | for (const auto* child_mapping : present_child_mappings_in_file_order(child_mappings)) { |
1215 | 80 | DORIS_CHECK(child_mapping->file_local_id.has_value()); |
1216 | 80 | LocalColumnIndex child_projection; |
1217 | 80 | RETURN_IF_ERROR(build_complex_projection(*child_mapping, &child_projection)); |
1218 | 80 | projection.children.push_back(std::move(child_projection)); |
1219 | 80 | } |
1220 | | |
1221 | 62 | ColumnDefinition projected_field; |
1222 | 62 | RETURN_IF_ERROR(project_column_definition(field, projection, &projected_field)); |
1223 | 62 | *projected_file_children = std::move(projected_field.children); |
1224 | 62 | *projected_type = std::move(projected_field.type); |
1225 | 62 | return Status::OK(); |
1226 | 62 | } |
1227 | | |
1228 | | // Build the complex column projection according to the ColumnMapping which is re-ordered by the |
1229 | | // file-schema's order. |
1230 | | // |
1231 | | // For MAP, a partial projection represents value-subtree pruning only. The key child is not a |
1232 | | // projected output shape; file readers still read full keys to construct ColumnMap offsets and keep |
1233 | | // key semantics unchanged. If a caller tries to project only/prune the key child, the common schema |
1234 | | // projection helper rejects it. |
1235 | 160 | static Status build_complex_projection(const ColumnMapping& mapping, LocalColumnIndex* projection) { |
1236 | 160 | if (projection == nullptr) { |
1237 | 0 | return Status::InvalidArgument("projection is null"); |
1238 | 0 | } |
1239 | 160 | DORIS_CHECK(mapping.file_local_id.has_value()); |
1240 | 160 | *projection = LocalColumnIndex::local(*mapping.file_local_id); |
1241 | 160 | projection->project_all_children = mapping.child_mappings.empty(); |
1242 | 160 | projection->children.clear(); |
1243 | 160 | const auto present_children = present_child_mappings_in_file_order(mapping.child_mappings); |
1244 | 160 | if (!projection->project_all_children && present_children.empty()) { |
1245 | | // All requested table children under this complex node are missing/default-only. The file |
1246 | | // reader cannot expose an empty complex projection, but TableReader can still rematerialize |
1247 | | // the table shape from a full file subtree and fill the missing children with defaults. |
1248 | 4 | projection->project_all_children = true; |
1249 | 4 | return Status::OK(); |
1250 | 4 | } |
1251 | 156 | for (const auto* child_mapping : present_children) { |
1252 | 59 | LocalColumnIndex child_projection; |
1253 | 59 | RETURN_IF_ERROR(build_complex_projection(*child_mapping, &child_projection)); |
1254 | 59 | projection->children.push_back(std::move(child_projection)); |
1255 | 59 | } |
1256 | 156 | if (!projection->project_all_children && projection->children.empty()) { |
1257 | 0 | return Status::NotSupported("Projection for complex column {} contains no file children", |
1258 | 0 | mapping.file_column_name); |
1259 | 0 | } |
1260 | 156 | return Status::OK(); |
1261 | 156 | } |
1262 | | |
1263 | | using FilterProjectionMap = std::map<LocalColumnId, LocalColumnIndex>; |
1264 | | |
1265 | | // Update the mapping's file type according to the projection, and determine whether the projection |
1266 | | // is trivial (i.e. the projected file type is the same as the table type, so no need to |
1267 | | // rematerialize the complex value back to table layout after reading from file). |
1268 | | static Status apply_projection_to_mapping_file_type(const LocalColumnIndex& projection, |
1269 | 146 | ColumnMapping* mapping) { |
1270 | 146 | DORIS_CHECK(mapping != nullptr); |
1271 | 146 | if (mapping->original_file_type == nullptr) { |
1272 | 0 | mapping->original_file_type = mapping->file_type; |
1273 | 0 | } |
1274 | 146 | if (mapping->original_file_type == nullptr || |
1275 | 146 | !is_complex_type(remove_nullable(mapping->original_file_type)->get_primitive_type())) { |
1276 | 103 | return Status::OK(); |
1277 | 103 | } |
1278 | 43 | ColumnDefinition field; |
1279 | 43 | field.type = mapping->original_file_type; |
1280 | 43 | field.children = mapping->original_file_children; |
1281 | 43 | ColumnDefinition projected_field; |
1282 | 43 | RETURN_IF_ERROR(project_column_definition(field, projection, &projected_field)); |
1283 | 43 | mapping->file_type = std::move(projected_field.type); |
1284 | 43 | mapping->projected_file_children = std::move(projected_field.children); |
1285 | 43 | mapping->is_trivial = mapping_can_use_file_column_directly(*mapping); |
1286 | 43 | return Status::OK(); |
1287 | 43 | } |
1288 | | |
1289 | | static Status merge_filter_projection(const FilterProjectionMap* filter_projections, |
1290 | 67 | LocalColumnIndex* projection) { |
1291 | 67 | DORIS_CHECK(projection != nullptr); |
1292 | 67 | if (filter_projections == nullptr) { |
1293 | 0 | return Status::OK(); |
1294 | 0 | } |
1295 | 67 | const auto filter_projection_it = filter_projections->find(projection->column_id()); |
1296 | 67 | if (filter_projection_it == filter_projections->end()) { |
1297 | 52 | return Status::OK(); |
1298 | 52 | } |
1299 | | // Merge predicate-only nested paths into the root projection that is about to be scanned. |
1300 | | // Example: `SELECT s.a WHERE s.b > 1` first builds the output projection `s -> a` from |
1301 | | // ColumnMapping, while build_nested_struct_filter_projection_map() records `s -> b`. This merge |
1302 | | // produces one file scan projection `s -> a,b`. |
1303 | 15 | RETURN_IF_ERROR(merge_local_column_index(projection, filter_projection_it->second)); |
1304 | 15 | return Status::OK(); |
1305 | 15 | } |
1306 | | |
1307 | 2 | static bool table_root_is_map(const ColumnMapping& mapping) { |
1308 | 2 | if (mapping.table_type == nullptr) { |
1309 | 0 | return false; |
1310 | 0 | } |
1311 | 2 | return remove_nullable(mapping.table_type)->get_primitive_type() == TYPE_MAP; |
1312 | 2 | } |
1313 | | |
1314 | | static Status add_scan_column(FileScanRequest* file_request, ColumnMapping* mapping, |
1315 | | bool is_predicate_column, bool force_full_complex_scan_projection, |
1316 | 148 | const FilterProjectionMap* filter_projections = nullptr) { |
1317 | 148 | const auto file_column_id = LocalColumnId(mapping->file_local_id.value()); |
1318 | 148 | LocalColumnIndex projection = LocalColumnIndex::top_level(file_column_id); |
1319 | | // Columnar readers can turn a complex mapping into a nested file projection, but |
1320 | | // row-oriented readers must scan the full top-level complex field because all children are |
1321 | | // encoded in the same text cell. |
1322 | 148 | if (!force_full_complex_scan_projection && needs_nested_file_projection(*mapping)) { |
1323 | 21 | RETURN_IF_ERROR(build_complex_projection(*mapping, &projection)); |
1324 | 21 | } |
1325 | 148 | if (is_predicate_column && !force_full_complex_scan_projection) { |
1326 | 67 | DCHECK(filter_projections != nullptr); |
1327 | | // If a projected complex root is also used by a predicate, rebuild the predicate scan |
1328 | | // projection from the output mapping before merging predicate-only children. For |
1329 | | // `SELECT s.a WHERE s.b > 1`, build_complex_projection() produces `s -> a` and |
1330 | | // merge_filter_projection() adds `s -> b`, so the predicate column reads both children. |
1331 | 67 | RETURN_IF_ERROR(merge_filter_projection(filter_projections, &projection)); |
1332 | 67 | } |
1333 | 148 | FileScanRequestBuilder builder(file_request); |
1334 | 148 | if (is_predicate_column) { |
1335 | 67 | return builder.add_predicate_column(std::move(projection)); |
1336 | 67 | } |
1337 | 81 | return builder.add_non_predicate_column(std::move(projection)); |
1338 | 148 | } |
1339 | | |
1340 | | static const LocalColumnIndex* find_scan_projection( |
1341 | 226 | const std::vector<LocalColumnIndex>& scan_columns, LocalColumnId file_column_id) { |
1342 | 226 | const auto projection_it = |
1343 | 226 | std::ranges::find_if(scan_columns, [&](const LocalColumnIndex& projection) { |
1344 | 185 | return projection.column_id() == file_column_id; |
1345 | 185 | }); |
1346 | 226 | return projection_it == scan_columns.end() ? nullptr : &*projection_it; |
1347 | 226 | } |
1348 | | |
1349 | | // Apply the final scan projection of one root file column back to its ColumnMapping. This updates |
1350 | | // mapping.file_type/projected_file_children from the original file schema to the exact shape that |
1351 | | // FileReader will return. |
1352 | | // |
1353 | | // Example: for `SELECT s.a WHERE s.b > 1`, add_scan_column() keeps only one predicate scan |
1354 | | // projection `s -> a,b`. Applying that projection changes the mapping's file type from the full |
1355 | | // file struct `s<a,b,c>` to the projected file struct `s<a,b>`, so later filter rewrite and |
1356 | | // TableReader final materialization use the same column shape as the file-local block. |
1357 | | static Status apply_scan_projection_to_mapping_file_type(const FileScanRequest& file_request, |
1358 | 146 | ColumnMapping* mapping) { |
1359 | 146 | DORIS_CHECK(mapping != nullptr); |
1360 | 146 | DORIS_CHECK(mapping->file_local_id.has_value()); |
1361 | 146 | const auto file_column_id = LocalColumnId(*mapping->file_local_id); |
1362 | | // Predicate columns are the actual scan projection when a column is used by row-level filters: |
1363 | | // add_scan_column() removes the duplicate non-predicate projection in that case. |
1364 | 146 | const auto* projection = find_scan_projection(file_request.predicate_columns, file_column_id); |
1365 | 146 | if (projection == nullptr) { |
1366 | 80 | projection = find_scan_projection(file_request.non_predicate_columns, file_column_id); |
1367 | 80 | } |
1368 | 146 | DORIS_CHECK(projection != nullptr); |
1369 | 146 | return apply_projection_to_mapping_file_type(*projection, mapping); |
1370 | 146 | } |
1371 | | |
1372 | | // Build extra scan projections required only by row-level filters on nested struct children. |
1373 | | // |
1374 | | // Example: for `SELECT s.a FROM t WHERE s.b.c > 1`, the output projection may only contain `s.a`, |
1375 | | // but the file reader must also read `s.b.c` to evaluate the predicate. This function collects the |
1376 | | // table-side filter path, resolves it through ColumnMapping first, and records the corresponding |
1377 | | // file-side projection in filter_projections. This keeps renamed fields consistent between the scan |
1378 | | // projection and row-level conjunct rewrite. Example: |
1379 | | // table filter path: s -> renamed_b -> c |
1380 | | // old file path: s -> b -> c |
1381 | | // recorded path: s -> b -> c |
1382 | | // When add_scan_column() adds the same root as a predicate column, it rebuilds that root from the |
1383 | | // output mapping, merges this filter-only projection into it, and removes the duplicate |
1384 | | // non-predicate root entry. |
1385 | | static Status build_nested_struct_filter_projection_map( |
1386 | | const std::vector<TableFilter>& table_filters, const std::vector<ColumnMapping>& mappings, |
1387 | 127 | FilterProjectionMap* filter_projections) { |
1388 | 127 | DORIS_CHECK(filter_projections != nullptr); |
1389 | 127 | filter_projections->clear(); |
1390 | 127 | for (const auto& table_filter : table_filters) { |
1391 | 77 | if (table_filter.conjunct == nullptr) { |
1392 | 2 | continue; |
1393 | 2 | } |
1394 | | // Collect all nested struct paths in the table filter. For example, for |
1395 | | // `s.id > 5 AND element_at(s, 'renamed_name') = 'abc'`, collect the table paths |
1396 | | // `s -> id` and `s -> renamed_name`, then resolve each one to its file-side projection. |
1397 | 75 | std::vector<NestedStructPath> paths; |
1398 | 75 | collect_nested_struct_paths(table_filter.conjunct->root(), &paths); |
1399 | 75 | for (const auto& path : paths) { |
1400 | 23 | auto mapping_it = std::ranges::find_if(mappings, [&](const ColumnMapping& mapping) { |
1401 | 23 | return mapping.global_index == path.root_global_index; |
1402 | 23 | }); |
1403 | 22 | if (mapping_it == mappings.end() || !mapping_it->file_local_id.has_value() || |
1404 | 22 | path.selectors.empty()) { |
1405 | 0 | continue; |
1406 | 0 | } |
1407 | | |
1408 | 22 | ResolvedNestedStructPath resolved; |
1409 | 22 | LocalColumnIndex root_projection; |
1410 | 22 | if (!resolve_nested_struct_path_for_file(path, mappings, &resolved)) { |
1411 | 2 | if (!table_root_is_map(*mapping_it)) { |
1412 | 1 | continue; |
1413 | 1 | } |
1414 | | // Direct map value filters such as `m.value.a > 1` need the value leaf for row |
1415 | | // evaluation even when the query only projects another value child. This is only a |
1416 | | // scan projection fallback; complex map/array expressions are still not rewritten |
1417 | | // into file-local conjuncts. |
1418 | 1 | LocalColumnIndex child_projection; |
1419 | 1 | RETURN_IF_ERROR(build_file_child_projection_from_schema( |
1420 | 1 | mapping_it->original_file_children, path.selectors, &child_projection)); |
1421 | 1 | if (child_projection.local_id() < 0) { |
1422 | 0 | continue; |
1423 | 0 | } |
1424 | 1 | root_projection = LocalColumnIndex::partial_local(*mapping_it->file_local_id); |
1425 | 1 | root_projection.children.push_back(std::move(child_projection)); |
1426 | 20 | } else { |
1427 | 20 | root_projection = std::move(resolved.file_projection); |
1428 | 20 | } |
1429 | 21 | auto filter_projection_it = filter_projections->find(root_projection.column_id()); |
1430 | 21 | if (filter_projection_it == filter_projections->end()) { |
1431 | 17 | filter_projections->emplace(root_projection.column_id(), |
1432 | 17 | std::move(root_projection)); |
1433 | 17 | continue; |
1434 | 17 | } |
1435 | 4 | RETURN_IF_ERROR( |
1436 | 4 | merge_local_column_index(&filter_projection_it->second, root_projection)); |
1437 | 4 | } |
1438 | 75 | } |
1439 | 127 | return Status::OK(); |
1440 | 127 | } |
1441 | | |
1442 | 138 | static void rebuild_projection(ColumnMapping* mapping, LocalIndex block_position) { |
1443 | 138 | DORIS_CHECK(mapping->file_local_id.has_value()); |
1444 | 138 | if (mapping->is_trivial || needs_complex_rematerialize(*mapping)) { |
1445 | 122 | mapping->projection = VExprContext::create_shared(VSlotRef::create_shared( |
1446 | 122 | cast_set<int>(block_position.value()), cast_set<int>(block_position.value()), -1, |
1447 | 122 | mapping->file_type, mapping->file_column_name)); |
1448 | 122 | return; |
1449 | 122 | } |
1450 | | |
1451 | 16 | auto expr = Cast::create_shared(mapping->table_type); |
1452 | 16 | expr->add_child(VSlotRef::create_shared(cast_set<int>(block_position.value()), |
1453 | 16 | cast_set<int>(block_position.value()), -1, |
1454 | 16 | mapping->file_type, mapping->file_column_name)); |
1455 | 16 | mapping->projection = VExprContext::create_shared(expr); |
1456 | 16 | } |
1457 | | |
1458 | | // Build file slot rewrite info from the localized filter targets. Only local targets can enter |
1459 | | // file-reader expressions; constant and unset targets stay above the file reader. |
1460 | | static std::map<GlobalIndex, FileSlotRewriteInfo> build_file_slot_rewrite_map( |
1461 | | const std::vector<ColumnMapping>& mappings, |
1462 | 127 | const std::map<GlobalIndex, FilterEntry>& filter_entries) { |
1463 | 127 | std::map<GlobalIndex, FileSlotRewriteInfo> global_to_file_slot; |
1464 | 169 | for (const auto& mapping : mappings) { |
1465 | 169 | const auto entry_it = filter_entries.find(mapping.global_index); |
1466 | 169 | if (entry_it == filter_entries.end() || !entry_it->second.is_local()) { |
1467 | 33 | continue; |
1468 | 33 | } |
1469 | 136 | DORIS_CHECK(mapping.file_local_id.has_value()); |
1470 | 136 | global_to_file_slot.emplace( |
1471 | 136 | mapping.global_index, |
1472 | 136 | FileSlotRewriteInfo {.block_position = entry_it->second.local_index().value(), |
1473 | 136 | .file_type = mapping.file_type, |
1474 | 136 | .table_type = mapping.table_type, |
1475 | 136 | .file_column_name = mapping.file_column_name}); |
1476 | 136 | } |
1477 | 127 | return global_to_file_slot; |
1478 | 127 | } |
1479 | | |
1480 | | Status TableColumnMapper::_create_by_index_mapping(const ColumnDefinition& table_column, |
1481 | | const std::vector<ColumnDefinition>& file_schema, |
1482 | 19 | ColumnMapping* mapping) { |
1483 | 19 | DORIS_CHECK(mapping != nullptr); |
1484 | 19 | DORIS_CHECK(!table_column.is_partition_key); |
1485 | | |
1486 | | // Key contract: in BY_INDEX mode, `ColumnDefinition::identifier` TYPE_INT is interpreted as the |
1487 | | // 0-based position of this column inside `file_schema`. FE writes the physical file position |
1488 | | // of each non-partition projected column into that identifier. This interpretation allows: |
1489 | | // - sparse projection: read only a subset of file columns (for example only `_col2` |
1490 | | // and `_col4`); |
1491 | | // - column reordering: table column order differs from file column order; |
1492 | | // - no many-to-one mapping: FE must guarantee that each file position is referenced by at |
1493 | | // most one table column. |
1494 | 19 | const auto file_index = table_column.get_identifier_position(); |
1495 | | |
1496 | | // Case A: file_index is in range, so build a direct positional mapping. |
1497 | | // The file column name (for example `_col0`) is intentionally ignored here. |
1498 | 19 | if (file_index >= 0 && static_cast<size_t>(file_index) < file_schema.size()) { |
1499 | 16 | return _create_direct_mapping(table_column, file_schema[static_cast<size_t>(file_index)], |
1500 | 16 | mapping); |
1501 | 16 | } |
1502 | | |
1503 | | // Case B: file_index is out of range, which means the file does not contain this column. |
1504 | | // Route it through the missing-column path used by schema evolution. |
1505 | 3 | if (table_column.default_expr != nullptr) { |
1506 | 1 | _set_constant_mapping(mapping, table_column.default_expr); |
1507 | 1 | return Status::OK(); |
1508 | 1 | } |
1509 | | // Keep the mapping empty (`file_local_id` remains `nullopt`) and let the upper finalize |
1510 | | // stage fill NULL/default values. |
1511 | 2 | return Status::OK(); |
1512 | 3 | } |
1513 | | |
1514 | 17 | void TableColumnMapper::_set_constant_mapping(ColumnMapping* mapping, VExprContextSPtr expr) { |
1515 | 17 | DORIS_CHECK(mapping != nullptr); |
1516 | 17 | DORIS_CHECK(expr != nullptr); |
1517 | 17 | mapping->default_expr = std::move(expr); |
1518 | 17 | mapping->constant_index = _constant_map.add(ConstantEntry { |
1519 | 17 | .global_index = mapping->global_index, |
1520 | 17 | .expr = mapping->default_expr, |
1521 | 17 | .type = mapping->table_type, |
1522 | 17 | }); |
1523 | 17 | mapping->filter_conversion = FilterConversionType::CONSTANT; |
1524 | 17 | } |
1525 | | |
1526 | | Status TableColumnMapper::_create_mapping_for_column(const ColumnDefinition& table_column, |
1527 | | GlobalIndex global_index, |
1528 | 227 | ColumnMapping* mapping) { |
1529 | 227 | DORIS_CHECK(mapping != nullptr); |
1530 | 227 | *mapping = ColumnMapping {}; |
1531 | 227 | mapping->global_index = global_index; |
1532 | 227 | mapping->table_column_name = table_column.name; |
1533 | 227 | mapping->table_type = table_column.type; |
1534 | 227 | const auto row_lineage_type = row_lineage_virtual_column_type(table_column, _options.mode); |
1535 | 227 | if (const auto* partition_value = find_partition_value(table_column, _partition_values); |
1536 | 227 | table_column.is_partition_key && partition_value != nullptr) { |
1537 | | // Partition values are split constants and must take precedence over defaults. |
1538 | 10 | _set_constant_mapping(mapping, VExprContext::create_shared(VLiteral::create_shared( |
1539 | 10 | mapping->table_type, *partition_value))); |
1540 | 217 | } else if (_options.mode == TableColumnMappingMode::BY_INDEX && |
1541 | 217 | !table_column.is_partition_key && table_column.has_identifier_field_id()) { |
1542 | | // BY_INDEX interprets ColumnDefinition::identifier as physical file position. |
1543 | 19 | RETURN_IF_ERROR(_create_by_index_mapping(table_column, _file_schema, mapping)); |
1544 | 198 | } else if (const auto* file_field = _find_file_field(table_column, _file_schema)) { |
1545 | | // Normal physical file column mapping. |
1546 | 168 | RETURN_IF_ERROR(_create_direct_mapping(table_column, *file_field, mapping)); |
1547 | 167 | if (row_lineage_type != TableVirtualColumnType::INVALID) { |
1548 | | // Iceberg v3 rewritten files may physically contain row lineage metadata fields. |
1549 | | // File non-null values must be preserved, while file NULLs still inherit from data file |
1550 | | // metadata in IcebergTableReader. Therefore the mapping has a real file source plus a |
1551 | | // virtual post-materialization step, and filters must wait for finalize output. |
1552 | 10 | mapping->virtual_column_type = row_lineage_type; |
1553 | 10 | mapping->filter_conversion = FilterConversionType::FINALIZE_ONLY; |
1554 | 10 | } |
1555 | 167 | } else if (row_lineage_type != TableVirtualColumnType::INVALID) { |
1556 | | // Iceberg row lineage metadata fields are optional in data files. Missing fields are exposed |
1557 | | // as all-NULL table columns first; IcebergTableReader fills inherited values only when the |
1558 | | // split carries first_row_id / last_updated_sequence_number metadata. |
1559 | | // FE may attach a default_expr to these hidden metadata columns, but the Iceberg v3 |
1560 | | // inheritance rule must take precedence over the generic missing-column default path. |
1561 | 13 | mapping->virtual_column_type = row_lineage_type; |
1562 | 17 | } else if (table_column.name == BeConsts::ICEBERG_ROWID_COL) { |
1563 | | // Doris internal Iceberg row locator is never a physical Iceberg data column. It is built |
1564 | | // from file path, row position and partition metadata for delete/update/merge. |
1565 | 2 | mapping->virtual_column_type = TableVirtualColumnType::ICEBERG_ROWID; |
1566 | 15 | } else if (table_column.default_expr != nullptr) { |
1567 | | // Missing schema-evolution column with an explicit default expression. |
1568 | 6 | _set_constant_mapping(mapping, table_column.default_expr); |
1569 | 9 | } else { |
1570 | 9 | if (table_column.is_partition_key) { |
1571 | 0 | return Status::InvalidArgument( |
1572 | 0 | "Table column '{}' (global_index={}) does not have a matching partition value", |
1573 | 0 | table_column.name, mapping->global_index.value()); |
1574 | 0 | } |
1575 | 9 | } |
1576 | 226 | return Status::OK(); |
1577 | 227 | } |
1578 | | |
1579 | | Status TableColumnMapper::_create_hidden_filter_mapping(const ColumnDefinition& table_column, |
1580 | | GlobalIndex global_index, |
1581 | 2 | ColumnMapping* mapping) { |
1582 | 2 | auto status = _create_mapping_for_column(table_column, global_index, mapping); |
1583 | 2 | if (mapping->file_local_id.has_value() || mapping->constant_index.has_value() || |
1584 | 2 | mapping->virtual_column_type != TableVirtualColumnType::INVALID) { |
1585 | 0 | return Status::OK(); |
1586 | 0 | } |
1587 | 2 | if (_options.mode == TableColumnMappingMode::BY_NAME) { |
1588 | 0 | return status; |
1589 | 0 | } |
1590 | | |
1591 | | // Predicate-only slot refs carry the table name/type but do not carry the table-format field |
1592 | | // id used by BY_FIELD_ID or the file position used by BY_INDEX. Use a name fallback only for |
1593 | | // hidden filter localization; projected columns still obey the requested mapping mode. |
1594 | 2 | const auto* file_field = |
1595 | 2 | matcher_for_mode(TableColumnMappingMode::BY_NAME).find(table_column, _file_schema); |
1596 | 2 | if (file_field == nullptr) { |
1597 | 0 | return status; |
1598 | 0 | } |
1599 | 2 | ColumnMapping fallback_mapping; |
1600 | 2 | fallback_mapping.global_index = global_index; |
1601 | 2 | fallback_mapping.table_column_name = table_column.name; |
1602 | 2 | fallback_mapping.table_type = table_column.type; |
1603 | 2 | RETURN_IF_ERROR(_create_direct_mapping(table_column, *file_field, &fallback_mapping)); |
1604 | 2 | *mapping = std::move(fallback_mapping); |
1605 | 2 | return Status::OK(); |
1606 | 2 | } |
1607 | | |
1608 | | Status TableColumnMapper::_build_hidden_filter_mappings( |
1609 | 121 | const std::vector<TableFilter>& table_filters) { |
1610 | 121 | _hidden_mappings.clear(); |
1611 | | |
1612 | 121 | std::map<GlobalIndex, ColumnDefinition> filter_columns; |
1613 | 121 | for (const auto& table_filter : table_filters) { |
1614 | 71 | if (table_filter.conjunct != nullptr) { |
1615 | 69 | collect_top_level_slot_columns(table_filter.conjunct->root(), &filter_columns); |
1616 | 69 | } |
1617 | 71 | } |
1618 | | |
1619 | 121 | for (const auto& [global_index, table_column] : filter_columns) { |
1620 | 70 | if (_find_mapping(global_index) != nullptr) { |
1621 | | // Ignore columns that are already mapped by the projected columns |
1622 | 68 | continue; |
1623 | 68 | } |
1624 | 2 | ColumnMapping mapping; |
1625 | 2 | RETURN_IF_ERROR(_create_hidden_filter_mapping(table_column, global_index, &mapping)); |
1626 | 2 | if (mapping.file_local_id.has_value() || mapping.constant_index.has_value() || |
1627 | 2 | mapping.virtual_column_type != TableVirtualColumnType::INVALID) { |
1628 | 2 | _hidden_mappings.push_back(std::move(mapping)); |
1629 | 2 | } |
1630 | 2 | } |
1631 | 121 | return Status::OK(); |
1632 | 121 | } |
1633 | | |
1634 | | Status TableColumnMapper::create_mapping(const std::vector<ColumnDefinition>& projected_columns, |
1635 | | const std::map<std::string, Field>& partition_values, |
1636 | 158 | const std::vector<ColumnDefinition>& file_schema) { |
1637 | 158 | clear(); |
1638 | 158 | _partition_values = partition_values; |
1639 | 158 | _file_schema = file_schema; |
1640 | 382 | for (size_t column_idx = 0; column_idx < projected_columns.size(); ++column_idx) { |
1641 | 225 | ColumnMapping mapping; |
1642 | 225 | RETURN_IF_ERROR(_create_mapping_for_column(projected_columns[column_idx], |
1643 | 225 | GlobalIndex(column_idx), &mapping)); |
1644 | 224 | _mappings.push_back(std::move(mapping)); |
1645 | 224 | } |
1646 | 157 | return Status::OK(); |
1647 | 158 | } |
1648 | | |
1649 | 381 | std::vector<ColumnMapping> TableColumnMapper::_filter_visible_mappings() const { |
1650 | 381 | std::vector<ColumnMapping> mappings; |
1651 | 381 | mappings.reserve(_mappings.size() + _hidden_mappings.size()); |
1652 | 381 | mappings.insert(mappings.end(), _mappings.begin(), _mappings.end()); |
1653 | 381 | mappings.insert(mappings.end(), _hidden_mappings.begin(), _hidden_mappings.end()); |
1654 | 381 | return mappings; |
1655 | 381 | } |
1656 | | |
1657 | 127 | Status TableColumnMapper::_build_filter_entries(const FileScanRequest& file_request) { |
1658 | 127 | _filter_entries.clear(); |
1659 | 127 | const auto mappings = _filter_visible_mappings(); |
1660 | 169 | for (const auto& mapping : mappings) { |
1661 | 169 | FilterEntry entry; |
1662 | 169 | if (mapping.constant_index.has_value()) { |
1663 | 10 | entry = FilterEntry::constant(*mapping.constant_index); |
1664 | 159 | } else if (mapping.file_local_id.has_value() && |
1665 | 159 | filter_conversion_has_local_source(mapping.filter_conversion)) { |
1666 | 136 | const auto local_position_it = |
1667 | 136 | file_request.local_positions.find(LocalColumnId(*mapping.file_local_id)); |
1668 | 136 | if (local_position_it != file_request.local_positions.end()) { |
1669 | 136 | entry = FilterEntry::local(local_position_it->second); |
1670 | 136 | } |
1671 | 136 | } |
1672 | 169 | _filter_entries.emplace(mapping.global_index, entry); |
1673 | 169 | } |
1674 | 127 | return Status::OK(); |
1675 | 127 | } |
1676 | | |
1677 | | Status TableColumnMapper::create_scan_request( |
1678 | | const std::vector<TableFilter>& table_filters, |
1679 | | const std::vector<ColumnDefinition>& projected_columns, FileScanRequest* file_request, |
1680 | 121 | RuntimeState* runtime_state) { |
1681 | | // FileReader evaluates expressions against a file-local block. This mapper owns the |
1682 | | // table-column to file-column conversion, so it also owns the file-local block positions. |
1683 | 121 | file_request->predicate_columns.clear(); |
1684 | 121 | file_request->non_predicate_columns.clear(); |
1685 | 121 | file_request->local_positions.clear(); |
1686 | 121 | file_request->conjuncts.clear(); |
1687 | 121 | file_request->delete_conjuncts.clear(); |
1688 | 121 | _filter_entries.clear(); |
1689 | | // 1. Build referenced non-predicate columns |
1690 | 281 | for (size_t column_idx = 0; column_idx < projected_columns.size(); ++column_idx) { |
1691 | 160 | const auto global_index = GlobalIndex(column_idx); |
1692 | 160 | auto* mapping = _find_mapping(global_index); |
1693 | 160 | if (mapping != nullptr && mapping->file_local_id.has_value()) { |
1694 | | // A file column can be read lazily as a non-predicate column only when it is not used |
1695 | | // by row-level expression filters. |
1696 | 138 | bool used_by_filter = false; |
1697 | 138 | for (const auto& table_filter : table_filters) { |
1698 | 87 | const auto& global_indices = table_filter.global_indices; |
1699 | 87 | if (std::find(global_indices.begin(), global_indices.end(), global_index) != |
1700 | 87 | global_indices.end() && |
1701 | 87 | filter_conversion_has_local_source(mapping->filter_conversion)) { |
1702 | 61 | used_by_filter = true; |
1703 | 61 | break; |
1704 | 61 | } |
1705 | 87 | } |
1706 | 138 | if (!used_by_filter || !enable_lazy_materialization()) { |
1707 | 79 | RETURN_IF_ERROR(add_scan_column(file_request, mapping, false, |
1708 | 79 | force_full_complex_scan_projection())); |
1709 | 79 | } |
1710 | 138 | } |
1711 | 160 | } |
1712 | | // 2. Build referenced predicate columns |
1713 | | // Hidden filter mappings must be built before localizing filters, so that they can be localized together with visible mappings and referenced by localized filter expressions. |
1714 | 121 | RETURN_IF_ERROR(_build_hidden_filter_mappings(table_filters)); |
1715 | 121 | RETURN_IF_ERROR(localize_filters(table_filters, file_request, runtime_state)); |
1716 | | // 3. Rebuild output projection expressions for projected columns. localize_filters() has |
1717 | | // already applied the final scan projection to mapping.file_type/projected_file_children before |
1718 | | // rewriting filter expressions. |
1719 | 160 | for (auto& mapping : _mappings) { |
1720 | 160 | if (!mapping.file_local_id.has_value()) { |
1721 | 22 | continue; |
1722 | 22 | } |
1723 | 138 | auto position_it = |
1724 | 138 | file_request->local_positions.find(LocalColumnId(*mapping.file_local_id)); |
1725 | 138 | DORIS_CHECK(position_it != file_request->local_positions.end()) |
1726 | 0 | << file_request->local_positions.size() << " " << *mapping.file_local_id << " " |
1727 | 0 | << mapping.file_column_name; |
1728 | 138 | rebuild_projection(&mapping, position_it->second); |
1729 | 138 | } |
1730 | 121 | return Status::OK(); |
1731 | 121 | } |
1732 | | |
1733 | 310 | ColumnMapping* TableColumnMapper::_find_mapping(GlobalIndex global_index) { |
1734 | 402 | for (auto& mapping : _mappings) { |
1735 | 402 | if (mapping.global_index == global_index) { |
1736 | 306 | return &mapping; |
1737 | 306 | } |
1738 | 402 | } |
1739 | 4 | return nullptr; |
1740 | 310 | } |
1741 | | |
1742 | 80 | ColumnMapping* TableColumnMapper::_find_filter_mapping(GlobalIndex global_index) { |
1743 | 80 | if (auto* mapping = _find_mapping(global_index); mapping != nullptr) { |
1744 | 78 | return mapping; |
1745 | 78 | } |
1746 | 2 | for (auto& mapping : _hidden_mappings) { |
1747 | 2 | if (mapping.global_index == global_index) { |
1748 | 2 | return &mapping; |
1749 | 2 | } |
1750 | 2 | } |
1751 | 0 | return nullptr; |
1752 | 2 | } |
1753 | | |
1754 | | Status TableColumnMapper::localize_filters(const std::vector<TableFilter>& table_filters, |
1755 | | FileScanRequest* file_request, |
1756 | 127 | RuntimeState* runtime_state) { |
1757 | 127 | FilterProjectionMap filter_projections; |
1758 | 127 | auto filter_mappings = _filter_visible_mappings(); |
1759 | 127 | RETURN_IF_ERROR(build_nested_struct_filter_projection_map(table_filters, filter_mappings, |
1760 | 127 | &filter_projections)); |
1761 | 127 | for (const auto& table_filter : table_filters) { |
1762 | 80 | for (const auto& global_index : table_filter.global_indices) { |
1763 | 80 | auto* mapping = _find_filter_mapping(global_index); |
1764 | 80 | if (mapping == nullptr || !mapping->file_local_id.has_value() || |
1765 | 80 | !filter_conversion_has_local_source(mapping->filter_conversion)) { |
1766 | 11 | continue; |
1767 | 11 | } |
1768 | 69 | RETURN_IF_ERROR(add_scan_column(file_request, mapping, enable_lazy_materialization(), |
1769 | 69 | force_full_complex_scan_projection(), |
1770 | 69 | &filter_projections)); |
1771 | 69 | } |
1772 | 77 | } |
1773 | | // Rebuild the file type for every scan-local mapping before expression rewrite. Predicate-only |
1774 | | // hidden mappings must see the same projected file type as the file reader will produce. |
1775 | 167 | for (auto& mapping : _mappings) { |
1776 | 167 | if (mapping.file_local_id.has_value() && |
1777 | 167 | file_request->local_positions.contains(LocalColumnId(*mapping.file_local_id))) { |
1778 | 144 | RETURN_IF_ERROR(apply_scan_projection_to_mapping_file_type(*file_request, &mapping)); |
1779 | 144 | } |
1780 | 167 | } |
1781 | 127 | for (auto& mapping : _hidden_mappings) { |
1782 | 2 | if (mapping.file_local_id.has_value() && |
1783 | 2 | file_request->local_positions.contains(LocalColumnId(*mapping.file_local_id))) { |
1784 | 2 | RETURN_IF_ERROR(apply_scan_projection_to_mapping_file_type(*file_request, &mapping)); |
1785 | 2 | } |
1786 | 2 | } |
1787 | 127 | RETURN_IF_ERROR(_build_filter_entries(*file_request)); |
1788 | | |
1789 | | // Build the complete table-slot rewrite map after all predicate columns have been assigned. |
1790 | | // This keeps expression localization independent from filter iteration order. |
1791 | 127 | filter_mappings = _filter_visible_mappings(); |
1792 | 127 | const auto global_to_file_slot = build_file_slot_rewrite_map(filter_mappings, _filter_entries); |
1793 | 127 | for (const auto& table_filter : table_filters) { |
1794 | 77 | if (table_filter.conjunct != nullptr && |
1795 | 77 | table_filter_has_only_local_entries(table_filter, _filter_entries)) { |
1796 | 64 | RewriteContext rewrite_context {.runtime_state = runtime_state}; |
1797 | 64 | VExprSPtr rewrite_root; |
1798 | 64 | Status clone_status; |
1799 | 64 | try { |
1800 | 64 | clone_status = clone_table_expr_tree(table_filter.conjunct->root(), &rewrite_root); |
1801 | 64 | } catch ([[maybe_unused]] const Exception& e) { |
1802 | | // Some table filters contain complex intermediate values, for example |
1803 | | // `element_at(MAP_VALUES(m)[1], 'age') > 30`. The current file-local rewrite only |
1804 | | // understands top-level slots and struct-element paths rooted at top-level slots; |
1805 | | // cloning such expressions can hit the generic TExpr complex-type limitation. |
1806 | | // Leave them above TableReader, where Scanner evaluates the original table-level |
1807 | | // conjunct after final materialization. |
1808 | 0 | #ifndef NDEBUG |
1809 | 0 | return Status::InternalError( |
1810 | 0 | "Failed to clone table filter for file-local rewrite: {}, expr={}", |
1811 | 0 | e.to_string(), table_filter.conjunct->root()->debug_string()); |
1812 | | #else |
1813 | | continue; |
1814 | | #endif |
1815 | 0 | } catch ([[maybe_unused]] const std::exception& e) { |
1816 | 0 | #ifndef NDEBUG |
1817 | 0 | return Status::InternalError( |
1818 | 0 | "Failed to clone table filter for file-local rewrite: {}, expr={}", |
1819 | 0 | e.what(), table_filter.conjunct->root()->debug_string()); |
1820 | | #else |
1821 | | continue; |
1822 | | #endif |
1823 | 0 | } |
1824 | 64 | if (!clone_status.ok()) { |
1825 | 0 | #ifndef NDEBUG |
1826 | 0 | return Status::InternalError( |
1827 | 0 | "Failed to clone table filter for file-local rewrite: {}, expr={}", |
1828 | 0 | clone_status.to_string(), table_filter.conjunct->root()->debug_string()); |
1829 | | #else |
1830 | | continue; |
1831 | | #endif |
1832 | 0 | } |
1833 | 64 | bool can_localize = true; |
1834 | 64 | auto localized_root = rewrite_table_expr_to_file_expr(rewrite_root, global_to_file_slot, |
1835 | 64 | filter_mappings, &rewrite_context, |
1836 | 64 | &can_localize); |
1837 | 64 | if (!can_localize) { |
1838 | 4 | continue; |
1839 | 4 | } |
1840 | 60 | auto localized_conjunct = VExprContext::create_shared(std::move(localized_root)); |
1841 | 60 | RETURN_IF_ERROR(rewrite_context.prepare_created_exprs(localized_conjunct.get())); |
1842 | 60 | file_request->conjuncts.push_back(std::move(localized_conjunct)); |
1843 | 60 | } |
1844 | 77 | } |
1845 | 127 | return Status::OK(); |
1846 | 127 | } |
1847 | | |
1848 | | const ColumnDefinition* TableColumnMapper::_find_file_field( |
1849 | | const ColumnDefinition& table_column, |
1850 | 198 | const std::vector<ColumnDefinition>& file_schema) const { |
1851 | 198 | if (table_column.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) { |
1852 | 2 | const auto field_it = std::ranges::find_if(file_schema, [](const ColumnDefinition& field) { |
1853 | 2 | return field.column_type == ColumnType::GLOBAL_ROWID; |
1854 | 2 | }); |
1855 | 1 | return field_it == file_schema.end() ? nullptr : &*field_it; |
1856 | 1 | } |
1857 | 197 | return matcher_for_mode(_options.mode).find(table_column, file_schema); |
1858 | 198 | } |
1859 | | |
1860 | | Status TableColumnMapper::_create_direct_mapping(const ColumnDefinition& table_column, |
1861 | | const ColumnDefinition& file_field, |
1862 | 289 | ColumnMapping* mapping) const { |
1863 | 289 | DORIS_CHECK(mapping != nullptr); |
1864 | 289 | DORIS_CHECK(file_field.local_id >= 0 || file_field.local_id == GLOBAL_ROWID_COLUMN_ID); |
1865 | 289 | mapping->file_local_id = file_field.local_id; |
1866 | 289 | mapping->table_column_name = table_column.name; |
1867 | 289 | mapping->file_column_name = file_field.name; |
1868 | 289 | mapping->original_file_type = file_field.type; |
1869 | 289 | mapping->original_file_children = file_field.children; |
1870 | 289 | mapping->projected_file_children = file_field.children; |
1871 | 289 | mapping->file_type = file_field.type; |
1872 | 289 | mapping->is_trivial = mapping_can_use_file_column_directly(*mapping); |
1873 | 289 | mapping->filter_conversion = mapping->is_trivial ? FilterConversionType::COPY_DIRECTLY |
1874 | 289 | : FilterConversionType::CAST_FILTER; |
1875 | 289 | mapping->child_mappings.clear(); |
1876 | | |
1877 | 289 | auto table_children = table_column.children; |
1878 | 289 | const auto nested_table_type = remove_nullable(mapping->table_type); |
1879 | | // Some scan paths, especially SELECT *, only carry the complete complex DataType for a table |
1880 | | // column and leave ColumnDefinition::children empty. If the file type is an older complex |
1881 | | // schema, treating this as a leaf mapping would make TableReader fall back to a plain CAST. |
1882 | | // That is invalid for evolved structs with different field counts. |
1883 | | // |
1884 | | // Example: |
1885 | | // table column type: Map(String, Struct(age, full_name, gender)) |
1886 | | // old file type: Map(String, Struct(age, name)) |
1887 | | // table children: empty |
1888 | | // |
1889 | | // Synthesize key/value/struct-field children from the table type so the normal recursive |
1890 | | // mapping path can rematerialize `name -> full_name` and fill missing `gender` with defaults, |
1891 | | // instead of trying to CAST Struct(age, name) to Struct(age, full_name, gender). |
1892 | 289 | const bool synthesized_table_children = |
1893 | 289 | table_children.empty() && is_complex_type(nested_table_type->get_primitive_type()) && |
1894 | 289 | !mapping->table_type->equals(*mapping->file_type); |
1895 | 289 | if (synthesized_table_children) { |
1896 | 4 | table_children = synthesize_complex_children_from_type(mapping->table_type); |
1897 | 285 | } else if (!table_children.empty() && !mapping->table_type->equals(*mapping->file_type)) { |
1898 | 57 | complete_required_complex_children_from_type(mapping->table_type, &table_children); |
1899 | 57 | } |
1900 | | |
1901 | 289 | if (!table_children.empty()) { |
1902 | 76 | if (!is_complex_type(remove_nullable(mapping->file_type)->get_primitive_type())) { |
1903 | 0 | return Status::NotSupported( |
1904 | 0 | "Cannot map complex table column '{}' to scalar parquet column '{}', table " |
1905 | 0 | "type={}, file type={}", |
1906 | 0 | table_column.name, file_field.name, mapping->table_type->get_name(), |
1907 | 0 | mapping->file_type->get_name()); |
1908 | 0 | } |
1909 | 76 | RETURN_IF_ERROR(validate_file_schema_children(file_field)); |
1910 | 75 | std::vector<int32_t> synthesized_used_file_child_ids; |
1911 | 193 | for (size_t table_child_idx = 0; table_child_idx < table_children.size(); |
1912 | 118 | ++table_child_idx) { |
1913 | 118 | const auto& table_child = table_children[table_child_idx]; |
1914 | 118 | const auto* file_child = |
1915 | 118 | find_file_child_for_mapping(table_child, file_field, _options.mode, |
1916 | 118 | table_child_idx, synthesized_table_children); |
1917 | 118 | if (synthesized_table_children && file_child != nullptr) { |
1918 | 8 | const auto file_child_id = file_child->file_local_id(); |
1919 | 8 | if (std::ranges::find(synthesized_used_file_child_ids, file_child_id) != |
1920 | 8 | synthesized_used_file_child_ids.end()) { |
1921 | 2 | file_child = nullptr; |
1922 | 2 | for (const auto& candidate : file_field.children) { |
1923 | 2 | const auto candidate_id = candidate.file_local_id(); |
1924 | 2 | if (std::ranges::find(synthesized_used_file_child_ids, candidate_id) == |
1925 | 2 | synthesized_used_file_child_ids.end()) { |
1926 | 2 | file_child = &candidate; |
1927 | 2 | break; |
1928 | 2 | } |
1929 | 2 | } |
1930 | 2 | } |
1931 | 8 | if (file_child != nullptr) { |
1932 | 8 | synthesized_used_file_child_ids.push_back(file_child->file_local_id()); |
1933 | 8 | } |
1934 | 8 | } |
1935 | 118 | if (file_child == nullptr) { |
1936 | 15 | ColumnMapping child_mapping; |
1937 | 15 | child_mapping.table_column_name = table_child.name; |
1938 | 15 | child_mapping.file_column_name = table_child.name; |
1939 | 15 | child_mapping.table_type = table_child.type; |
1940 | 15 | child_mapping.file_type = table_child.type; |
1941 | 15 | child_mapping.filter_conversion = FilterConversionType::FINALIZE_ONLY; |
1942 | 15 | mapping->child_mappings.push_back(std::move(child_mapping)); |
1943 | 15 | continue; |
1944 | 15 | } |
1945 | 103 | ColumnMapping child_mapping; |
1946 | 103 | child_mapping.table_column_name = table_child.name; |
1947 | 103 | child_mapping.table_type = table_child.type; |
1948 | 103 | RETURN_IF_ERROR(_create_direct_mapping(table_child, *file_child, &child_mapping)); |
1949 | 103 | mapping->child_mappings.push_back(std::move(child_mapping)); |
1950 | 103 | } |
1951 | 75 | if (needs_projected_file_type_rebuild(*mapping)) { |
1952 | | // If complex projection prunes some children, we have to rebuild the projected file type to make sure the reader expression can find the correct child types by name. |
1953 | 62 | RETURN_IF_ERROR(rebuild_projected_file_children_and_type( |
1954 | 62 | mapping->file_type, mapping->original_file_children, mapping->child_mappings, |
1955 | 62 | &mapping->projected_file_children, &mapping->file_type)); |
1956 | 62 | DCHECK(mapping->table_type != nullptr); |
1957 | 62 | mapping->is_trivial = mapping_can_use_file_column_directly(*mapping); |
1958 | | // TODO: ? READER_EXPRESSION |
1959 | 62 | mapping->filter_conversion = mapping->is_trivial |
1960 | 62 | ? FilterConversionType::COPY_DIRECTLY |
1961 | 62 | : FilterConversionType::READER_EXPRESSION; |
1962 | 62 | } |
1963 | 75 | } |
1964 | 288 | return Status::OK(); |
1965 | 289 | } |
1966 | | |
1967 | | } // namespace doris::format |