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 <optional> |
24 | | #include <set> |
25 | | #include <sstream> |
26 | | #include <string_view> |
27 | | #include <utility> |
28 | | #include <vector> |
29 | | |
30 | | #include "common/consts.h" |
31 | | #include "common/exception.h" |
32 | | #include "common/status.h" |
33 | | #include "core/data_type/convert_field_to_type.h" |
34 | | #include "core/data_type/data_type_array.h" |
35 | | #include "core/data_type/data_type_map.h" |
36 | | #include "core/data_type/data_type_nullable.h" |
37 | | #include "core/data_type/data_type_string.h" |
38 | | #include "core/data_type/data_type_struct.h" |
39 | | #include "core/data_type/primitive_type.h" |
40 | | #include "exprs/runtime_filter_expr.h" |
41 | | #include "exprs/short_circuit_evaluation_expr.h" |
42 | | #include "exprs/vcase_expr.h" |
43 | | #include "exprs/vcast_expr.h" |
44 | | #include "exprs/vcondition_expr.h" |
45 | | #include "exprs/vectorized_fn_call.h" |
46 | | #include "exprs/vexpr_context.h" |
47 | | #include "exprs/vin_predicate.h" |
48 | | #include "exprs/vliteral.h" |
49 | | #include "format_v2/column_mapper_nested.h" |
50 | | #include "format_v2/expr/cast.h" |
51 | | #include "format_v2/file_reader.h" |
52 | | #include "format_v2/schema_projection.h" |
53 | | #include "format_v2/table_reader.h" |
54 | | #include "gen_cpp/Exprs_types.h" |
55 | | #include "util/url_coding.h" |
56 | | |
57 | | namespace doris::format { |
58 | | |
59 | | namespace { |
60 | | |
61 | 4.59k | Status build_initial_default_column(const ColumnDefinition& column, ColumnPtr* value) { |
62 | 4.59k | DORIS_CHECK(value != nullptr); |
63 | 4.59k | *value = nullptr; |
64 | 4.59k | if (!column.initial_default_value.has_value()) { |
65 | 4.58k | return Status::OK(); |
66 | 4.58k | } |
67 | 2 | const auto nested_type = remove_nullable(column.type); |
68 | 2 | Field parsed; |
69 | 2 | if (column.initial_default_value_is_base64 || |
70 | 3 | nested_type->get_primitive_type() == TYPE_VARBINARY) { |
71 | 3 | std::string decoded; |
72 | 3 | if (!base64_decode(*column.initial_default_value, &decoded)) { |
73 | 0 | return Status::InvalidArgument("Invalid Base64 Iceberg initial default for field {}", |
74 | 0 | column.name); |
75 | 0 | } |
76 | 3 | parsed = nested_type->get_primitive_type() == TYPE_VARBINARY |
77 | 3 | ? Field::create_field<TYPE_VARBINARY>(StringView(decoded)) |
78 | 3 | : Field::create_field<TYPE_STRING>(decoded); |
79 | | // Variable-width Fields borrow their input. Materialize while decoded is alive so the |
80 | | // resulting column owns the payload before it crosses a mapping/literal boundary. |
81 | 3 | *value = column.type->create_column_const(1, parsed); |
82 | 3 | return Status::OK(); |
83 | 18.4E | } else { |
84 | 18.4E | RETURN_IF_ERROR( |
85 | 18.4E | nested_type->get_serde()->from_fe_string(*column.initial_default_value, parsed)); |
86 | 18.4E | } |
87 | 18.4E | *value = column.type->create_column_const(1, parsed); |
88 | 18.4E | return Status::OK(); |
89 | 2 | } |
90 | | |
91 | 2 | Status build_initial_default_literal(const ColumnDefinition& column, VExprContextSPtr* literal) { |
92 | 2 | DORIS_CHECK(literal != nullptr); |
93 | 2 | ColumnPtr owned_value; |
94 | 2 | RETURN_IF_ERROR(build_initial_default_column(column, &owned_value)); |
95 | 2 | DORIS_CHECK(static_cast<bool>(owned_value)); |
96 | 2 | Field value; |
97 | 2 | owned_value->get(0, value); |
98 | | // VLiteral copies the borrowed Field into its own column while owned_value is still alive. |
99 | 2 | *literal = VExprContext::create_shared(VLiteral::create_shared(column.type, value)); |
100 | 2 | return Status::OK(); |
101 | 2 | } |
102 | | |
103 | 10 | bool has_shared_descendant_field_id(const ColumnDefinition& table, const ColumnDefinition& file) { |
104 | 10 | const auto& table_children = |
105 | 10 | table.identity_children.empty() ? table.children : table.identity_children; |
106 | 10 | for (const auto& table_child : table_children) { |
107 | 10 | if (!table_child.has_identifier_field_id()) { |
108 | 0 | continue; |
109 | 0 | } |
110 | 10 | const auto file_child = |
111 | 10 | std::ranges::find_if(file.children, [&](const ColumnDefinition& candidate) { |
112 | 10 | return candidate.has_identifier_field_id() && |
113 | 10 | candidate.get_identifier_field_id() == |
114 | 9 | table_child.get_identifier_field_id(); |
115 | 10 | }); |
116 | 10 | if (file_child != file.children.end() || |
117 | 10 | std::ranges::any_of(file.children, [&](const ColumnDefinition& candidate) { |
118 | 2 | return has_shared_descendant_field_id(table_child, candidate); |
119 | 9 | })) { |
120 | 9 | return true; |
121 | 9 | } |
122 | 10 | } |
123 | 1 | return false; |
124 | 10 | } |
125 | | |
126 | 16 | std::string mapping_mode_to_string(TableColumnMappingMode mode) { |
127 | 16 | switch (mode) { |
128 | 1 | case TableColumnMappingMode::BY_FIELD_ID: |
129 | 1 | return "BY_FIELD_ID"; |
130 | 14 | case TableColumnMappingMode::BY_NAME: |
131 | 14 | return "BY_NAME"; |
132 | 1 | case TableColumnMappingMode::BY_INDEX: |
133 | 1 | return "BY_INDEX"; |
134 | 16 | } |
135 | 0 | return "UNKNOWN"; |
136 | 16 | } |
137 | | |
138 | 12.6M | bool column_has_name(const ColumnDefinition& column, const std::string& name) { |
139 | 12.6M | if (to_lower(column.name) == to_lower(name)) { |
140 | 290k | return true; |
141 | 290k | } |
142 | 12.3M | if (column.has_identifier_name() && to_lower(column.get_identifier_name()) == to_lower(name)) { |
143 | 0 | return true; |
144 | 0 | } |
145 | 12.3M | return std::ranges::any_of(column.name_mapping, [&](const std::string& alias) { |
146 | 146 | return to_lower(alias) == to_lower(name); |
147 | 146 | }); |
148 | 12.3M | } |
149 | | |
150 | 6.59M | bool column_names_match(const ColumnDefinition& lhs, const ColumnDefinition& rhs) { |
151 | 6.60M | if (!lhs.has_name_mapping) { |
152 | 6.60M | if (column_has_name(rhs, lhs.name)) { |
153 | 290k | return true; |
154 | 290k | } |
155 | 6.31M | if (lhs.has_identifier_name() && column_has_name(rhs, lhs.get_identifier_name())) { |
156 | 1 | return true; |
157 | 1 | } |
158 | 6.31M | } |
159 | | // Explicit Iceberg name mapping is authoritative: an empty alias list represents a field that |
160 | | // did not exist in the imported file, so only transported aliases may match. |
161 | 6.30M | return std::ranges::any_of(lhs.name_mapping, [&](const std::string& alias) { |
162 | 24 | return column_has_name(rhs, alias); |
163 | 24 | }); |
164 | 6.59M | } |
165 | | |
166 | | class ColumnMatcher { |
167 | | public: |
168 | 3 | virtual ~ColumnMatcher() = default; |
169 | | virtual const ColumnDefinition* find( |
170 | | const ColumnDefinition& table_column, |
171 | | const std::vector<ColumnDefinition>& file_schema) const = 0; |
172 | | }; |
173 | | |
174 | | class FieldIdMatcher final : public ColumnMatcher { |
175 | | public: |
176 | | const ColumnDefinition* find(const ColumnDefinition& table_column, |
177 | 127k | const std::vector<ColumnDefinition>& file_schema) const override { |
178 | 127k | if (!table_column.has_identifier_field_id()) { |
179 | 441 | return nullptr; |
180 | 441 | } |
181 | 126k | const auto field_id = table_column.get_identifier_field_id(); |
182 | 806k | const auto field_it = std::ranges::find_if(file_schema, [&](const ColumnDefinition& field) { |
183 | 806k | return field.has_identifier_field_id() && field.get_identifier_field_id() == field_id; |
184 | 806k | }); |
185 | 126k | return field_it == file_schema.end() ? nullptr : &*field_it; |
186 | 127k | } |
187 | | }; |
188 | | |
189 | | class NameMatcher final : public ColumnMatcher { |
190 | | public: |
191 | | const ColumnDefinition* find(const ColumnDefinition& table_column, |
192 | 290k | const std::vector<ColumnDefinition>& file_schema) const override { |
193 | 6.59M | const auto field_it = std::ranges::find_if(file_schema, [&](const ColumnDefinition& field) { |
194 | 6.59M | return column_names_match(table_column, field); |
195 | 6.59M | }); |
196 | 290k | return field_it == file_schema.end() ? nullptr : &*field_it; |
197 | 290k | } |
198 | | }; |
199 | | |
200 | | class PositionMatcher final : public ColumnMatcher { |
201 | | public: |
202 | | const ColumnDefinition* find(const ColumnDefinition& table_column, |
203 | 2 | const std::vector<ColumnDefinition>& file_schema) const override { |
204 | 2 | if (!table_column.has_identifier_field_id()) { |
205 | 2 | return nullptr; |
206 | 2 | } |
207 | 0 | const auto position = table_column.get_identifier_position(); |
208 | 0 | if (position < 0 || static_cast<size_t>(position) >= file_schema.size()) { |
209 | 0 | return nullptr; |
210 | 0 | } |
211 | 0 | return &file_schema[static_cast<size_t>(position)]; |
212 | 0 | } |
213 | | }; |
214 | | |
215 | 418k | const ColumnMatcher& matcher_for_mode(TableColumnMappingMode mode) { |
216 | 418k | static const FieldIdMatcher field_id_matcher; |
217 | 418k | static const NameMatcher name_matcher; |
218 | 418k | static const PositionMatcher position_matcher; |
219 | 418k | switch (mode) { |
220 | 127k | case TableColumnMappingMode::BY_FIELD_ID: |
221 | 127k | return field_id_matcher; |
222 | 291k | case TableColumnMappingMode::BY_NAME: |
223 | 291k | return name_matcher; |
224 | 2 | case TableColumnMappingMode::BY_INDEX: |
225 | 2 | return position_matcher; |
226 | 418k | } |
227 | 0 | return field_id_matcher; |
228 | 418k | } |
229 | | |
230 | 13 | std::string virtual_column_type_to_string(TableVirtualColumnType type) { |
231 | 13 | switch (type) { |
232 | 10 | case TableVirtualColumnType::INVALID: |
233 | 10 | return "INVALID"; |
234 | 1 | case TableVirtualColumnType::ROW_ID: |
235 | 1 | return "ROW_ID"; |
236 | 1 | case TableVirtualColumnType::LAST_UPDATED_SEQUENCE_NUMBER: |
237 | 1 | return "LAST_UPDATED_SEQUENCE_NUMBER"; |
238 | 1 | case TableVirtualColumnType::ICEBERG_ROWID: |
239 | 1 | return "ICEBERG_ROWID"; |
240 | 13 | } |
241 | 0 | return "UNKNOWN"; |
242 | 13 | } |
243 | | |
244 | 13 | std::string filter_conversion_type_to_string(FilterConversionType type) { |
245 | 13 | switch (type) { |
246 | 3 | case FilterConversionType::COPY_DIRECTLY: |
247 | 3 | return "COPY_DIRECTLY"; |
248 | 2 | case FilterConversionType::CAST_FILTER: |
249 | 2 | return "CAST_FILTER"; |
250 | 1 | case FilterConversionType::READER_EXPRESSION: |
251 | 1 | return "READER_EXPRESSION"; |
252 | 6 | case FilterConversionType::FINALIZE_ONLY: |
253 | 6 | return "FINALIZE_ONLY"; |
254 | 1 | case FilterConversionType::CONSTANT: |
255 | 1 | return "CONSTANT"; |
256 | 13 | } |
257 | 0 | return "UNKNOWN"; |
258 | 13 | } |
259 | | |
260 | 50 | std::string data_type_debug_string(const DataTypePtr& type) { |
261 | 50 | return type == nullptr ? "null" : type->get_name(); |
262 | 50 | } |
263 | | |
264 | 11 | std::string field_debug_string(const Field& field) { |
265 | 11 | std::ostringstream out; |
266 | 11 | out << "Field{type=" << type_to_string(field.get_type()) << ", value="; |
267 | 11 | switch (field.get_type()) { |
268 | 0 | case TYPE_NULL: |
269 | 0 | out << "null"; |
270 | 0 | break; |
271 | 9 | case TYPE_INT: |
272 | 9 | out << field.get<TYPE_INT>(); |
273 | 9 | break; |
274 | 0 | case TYPE_BIGINT: |
275 | 0 | out << field.get<TYPE_BIGINT>(); |
276 | 0 | break; |
277 | 2 | case TYPE_STRING: |
278 | 2 | out << field.get<TYPE_STRING>(); |
279 | 2 | break; |
280 | 0 | default: |
281 | 0 | out << field.to_debug_string(0); |
282 | 0 | break; |
283 | 11 | } |
284 | 11 | out << "}"; |
285 | 11 | return out.str(); |
286 | 11 | } |
287 | | |
288 | | template <typename T, typename Formatter> |
289 | 63 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { |
290 | 63 | std::ostringstream out; |
291 | 63 | out << "["; |
292 | 85 | for (size_t i = 0; i < values.size(); ++i) { |
293 | 22 | if (i > 0) { |
294 | 1 | out << ", "; |
295 | 1 | } |
296 | 22 | out << formatter(values[i]); |
297 | 22 | } |
298 | 63 | out << "]"; |
299 | 63 | return out.str(); |
300 | 63 | } column_mapper.cpp:_ZN5doris6format12_GLOBAL__N_118join_debug_stringsINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEZNKS0_16ColumnDefinition12debug_stringEvE3$_0EES8_RKSt6vectorIT_SaISC_EET0_ Line | Count | Source | 289 | 11 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { | 290 | 11 | std::ostringstream out; | 291 | 11 | out << "["; | 292 | 19 | for (size_t i = 0; i < values.size(); ++i) { | 293 | 8 | if (i > 0) { | 294 | 0 | out << ", "; | 295 | 0 | } | 296 | 8 | out << formatter(values[i]); | 297 | 8 | } | 298 | 11 | out << "]"; | 299 | 11 | return out.str(); | 300 | 11 | } |
column_mapper.cpp:_ZN5doris6format12_GLOBAL__N_118join_debug_stringsINS0_16ColumnDefinitionEZNKS3_12debug_stringB5cxx11EvE3$_1EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISC_EET0_ Line | Count | Source | 289 | 11 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { | 290 | 11 | std::ostringstream out; | 291 | 11 | out << "["; | 292 | 12 | for (size_t i = 0; i < values.size(); ++i) { | 293 | 1 | if (i > 0) { | 294 | 0 | out << ", "; | 295 | 0 | } | 296 | 1 | out << formatter(values[i]); | 297 | 1 | } | 298 | 11 | out << "]"; | 299 | 11 | return out.str(); | 300 | 11 | } |
column_mapper.cpp:_ZN5doris6format12_GLOBAL__N_118join_debug_stringsINS0_16ColumnDefinitionEZNKS3_12debug_stringB5cxx11EvE3$_2EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISC_EET0_ Line | Count | Source | 289 | 11 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { | 290 | 11 | std::ostringstream out; | 291 | 11 | out << "["; | 292 | 11 | for (size_t i = 0; i < values.size(); ++i) { | 293 | 0 | if (i > 0) { | 294 | 0 | out << ", "; | 295 | 0 | } | 296 | 0 | out << formatter(values[i]); | 297 | 0 | } | 298 | 11 | out << "]"; | 299 | 11 | return out.str(); | 300 | 11 | } |
column_mapper.cpp:_ZN5doris6format12_GLOBAL__N_118join_debug_stringsINS0_16LocalColumnIndexEZNKS3_12debug_stringB5cxx11EvE3$_0EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISC_EET0_ Line | Count | Source | 289 | 2 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { | 290 | 2 | std::ostringstream out; | 291 | 2 | out << "["; | 292 | 3 | for (size_t i = 0; i < values.size(); ++i) { | 293 | 1 | if (i > 0) { | 294 | 0 | out << ", "; | 295 | 0 | } | 296 | 1 | out << formatter(values[i]); | 297 | 1 | } | 298 | 2 | out << "]"; | 299 | 2 | return out.str(); | 300 | 2 | } |
column_mapper.cpp:_ZN5doris6format12_GLOBAL__N_118join_debug_stringsINS0_16ColumnDefinitionEZNKS0_13ColumnMapping12debug_stringB5cxx11EvE3$_0EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISD_EET0_ Line | Count | Source | 289 | 13 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { | 290 | 13 | std::ostringstream out; | 291 | 13 | out << "["; | 292 | 18 | for (size_t i = 0; i < values.size(); ++i) { | 293 | 5 | if (i > 0) { | 294 | 0 | out << ", "; | 295 | 0 | } | 296 | 5 | out << formatter(values[i]); | 297 | 5 | } | 298 | 13 | out << "]"; | 299 | 13 | return out.str(); | 300 | 13 | } |
column_mapper.cpp:_ZN5doris6format12_GLOBAL__N_118join_debug_stringsINS0_13ColumnMappingEZNKS3_12debug_stringB5cxx11EvE3$_1EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISC_EET0_ Line | Count | Source | 289 | 13 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { | 290 | 13 | std::ostringstream out; | 291 | 13 | out << "["; | 292 | 18 | for (size_t i = 0; i < values.size(); ++i) { | 293 | 5 | if (i > 0) { | 294 | 0 | out << ", "; | 295 | 0 | } | 296 | 5 | out << formatter(values[i]); | 297 | 5 | } | 298 | 13 | out << "]"; | 299 | 13 | return out.str(); | 300 | 13 | } |
column_mapper.cpp:_ZN5doris6format12_GLOBAL__N_118join_debug_stringsINS0_13ColumnMappingEZNKS0_17TableColumnMapper12debug_stringB5cxx11EvE3$_0EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISD_EET0_ Line | Count | Source | 289 | 1 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { | 290 | 1 | std::ostringstream out; | 291 | 1 | out << "["; | 292 | 3 | for (size_t i = 0; i < values.size(); ++i) { | 293 | 2 | if (i > 0) { | 294 | 1 | out << ", "; | 295 | 1 | } | 296 | 2 | out << formatter(values[i]); | 297 | 2 | } | 298 | 1 | out << "]"; | 299 | 1 | return out.str(); | 300 | 1 | } |
column_mapper.cpp:_ZN5doris6format12_GLOBAL__N_118join_debug_stringsINS0_13ColumnMappingEZNKS0_17TableColumnMapper12debug_stringB5cxx11EvE3$_1EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKSt6vectorIT_SaISD_EET0_ Line | Count | Source | 289 | 1 | std::string join_debug_strings(const std::vector<T>& values, Formatter formatter) { | 290 | 1 | std::ostringstream out; | 291 | 1 | out << "["; | 292 | 1 | for (size_t i = 0; i < values.size(); ++i) { | 293 | 0 | if (i > 0) { | 294 | 0 | out << ", "; | 295 | 0 | } | 296 | 0 | out << formatter(values[i]); | 297 | 0 | } | 298 | 1 | out << "]"; | 299 | 1 | return out.str(); | 300 | 1 | } |
|
301 | | |
302 | | } // namespace |
303 | | |
304 | | const ColumnDefinition* find_column_by_name(const ColumnDefinition& table_column, |
305 | 3 | const std::vector<ColumnDefinition>& file_schema) { |
306 | 3 | return matcher_for_mode(TableColumnMappingMode::BY_NAME).find(table_column, file_schema); |
307 | 3 | } |
308 | | |
309 | | const Field* find_partition_value(const ColumnDefinition& table_column, |
310 | 398k | const std::map<std::string, Field>& partition_values) { |
311 | 646k | const auto find_by_name = [&](const std::string& name) -> const Field* { |
312 | 646k | const auto value_it = partition_values.find(name); |
313 | 646k | return value_it == partition_values.end() ? nullptr : &value_it->second; |
314 | 646k | }; |
315 | 398k | if (const auto* value = find_by_name(table_column.name); value != nullptr) { |
316 | 19.5k | return value; |
317 | 19.5k | } |
318 | 378k | if (table_column.has_identifier_name()) { |
319 | 249k | if (const auto* value = find_by_name(table_column.get_identifier_name()); |
320 | 249k | value != nullptr) { |
321 | 0 | return value; |
322 | 0 | } |
323 | 249k | } |
324 | 378k | for (const auto& alias : table_column.name_mapping) { |
325 | 24 | if (const auto* value = find_by_name(alias); value != nullptr) { |
326 | 1 | return value; |
327 | 1 | } |
328 | 24 | } |
329 | 378k | return nullptr; |
330 | 378k | } |
331 | | |
332 | | struct FileSlotRewriteInfo { |
333 | | size_t block_position = 0; |
334 | | DataTypePtr file_type; |
335 | | DataTypePtr table_type; |
336 | | std::string file_column_name; |
337 | | }; |
338 | | |
339 | | struct RewriteContext { |
340 | | RuntimeState* runtime_state = nullptr; |
341 | | std::vector<VExprSPtr> created_exprs {}; |
342 | | |
343 | 48.3k | void add_created_expr(VExprSPtr expr) { created_exprs.push_back(std::move(expr)); } |
344 | | |
345 | 31.1k | Status prepare_created_exprs(VExprContext* context) const { |
346 | 31.1k | DORIS_CHECK(context != nullptr); |
347 | 31.1k | RowDescriptor row_desc; |
348 | 47.5k | for (const auto& expr : created_exprs) { |
349 | 47.5k | if (dynamic_cast<const Cast*>(expr.get()) != nullptr && runtime_state == nullptr) { |
350 | 0 | return Status::InvalidArgument( |
351 | 0 | "RuntimeState is required to prepare rewritten cast expression {}", |
352 | 0 | expr->expr_name()); |
353 | 0 | } |
354 | 47.5k | RETURN_IF_ERROR(expr->prepare(runtime_state, row_desc, context)); |
355 | 47.5k | } |
356 | 31.1k | return Status::OK(); |
357 | 31.1k | } |
358 | | }; |
359 | | |
360 | | static VExprSPtr create_file_slot_ref(const VSlotRef& slot_ref, |
361 | | const FileSlotRewriteInfo& rewrite_info, |
362 | 32.7k | RewriteContext* rewrite_context) { |
363 | 32.7k | auto ref = |
364 | 32.7k | VSlotRef::create_shared(slot_ref.slot_id(), cast_set<int>(rewrite_info.block_position), |
365 | 32.7k | -1, rewrite_info.file_type, rewrite_info.file_column_name); |
366 | 32.7k | rewrite_context->add_created_expr(ref); |
367 | 32.7k | return ref; |
368 | 32.7k | } |
369 | | |
370 | 92.8k | static bool is_cast_expr(const VExprSPtr& expr) { |
371 | 92.8k | return dynamic_cast<const Cast*>(expr.get()) != nullptr; |
372 | 92.8k | } |
373 | | |
374 | 137k | static bool is_binary_comparison_predicate(const VExprSPtr& expr) { |
375 | 137k | if (expr == nullptr || expr->get_num_children() != 2 || |
376 | 137k | (expr->node_type() != TExprNodeType::BINARY_PRED && |
377 | 112k | expr->node_type() != TExprNodeType::NULL_AWARE_BINARY_PRED)) { |
378 | 112k | return false; |
379 | 112k | } |
380 | 25.3k | switch (expr->op()) { |
381 | 12.6k | case TExprOpcode::EQ: |
382 | 12.6k | case TExprOpcode::EQ_FOR_NULL: |
383 | 14.4k | case TExprOpcode::NE: |
384 | 17.1k | case TExprOpcode::GE: |
385 | 21.6k | case TExprOpcode::GT: |
386 | 23.5k | case TExprOpcode::LE: |
387 | 25.4k | case TExprOpcode::LT: |
388 | 25.4k | return true; |
389 | 0 | default: |
390 | 0 | return false; |
391 | 25.3k | } |
392 | 25.3k | } |
393 | | |
394 | 16 | std::string TableColumnMapperOptions::debug_string() const { |
395 | 16 | std::ostringstream out; |
396 | 16 | out << "TableColumnMapperOptions{mode=" << mapping_mode_to_string(mode) |
397 | 16 | << ", allow_idless_complex_wrapper_projection=" << allow_idless_complex_wrapper_projection |
398 | 16 | << ", enable_row_lineage_virtual_columns=" << enable_row_lineage_virtual_columns << "}"; |
399 | 16 | return out.str(); |
400 | 16 | } |
401 | | |
402 | 11 | std::string ColumnDefinition::debug_string() const { |
403 | 11 | std::ostringstream out; |
404 | 11 | out << "ColumnDefinition{name=" << name << ", identifier=" << field_debug_string(identifier) |
405 | 11 | << ", name_mapping=" |
406 | 11 | << join_debug_strings(name_mapping, [](const std::string& name) { return name; }) |
407 | 11 | << ", has_name_mapping=" << has_name_mapping << ", local_id=" << local_id |
408 | 11 | << ", type=" << data_type_debug_string(type) << ", children=" |
409 | 11 | << join_debug_strings(children, |
410 | 11 | [](const ColumnDefinition& child) { return child.debug_string(); }) |
411 | 11 | << ", identity_children=" |
412 | 11 | << join_debug_strings(identity_children, |
413 | 11 | [](const ColumnDefinition& child) { return child.debug_string(); }) |
414 | 11 | << ", has_default_expr=" << (default_expr != nullptr) |
415 | 11 | << ", is_partition_key=" << is_partition_key << "}"; |
416 | 11 | return out.str(); |
417 | 11 | } |
418 | | |
419 | 2 | std::string LocalColumnIndex::debug_string() const { |
420 | 2 | std::ostringstream out; |
421 | 2 | out << "LocalColumnIndex{index=" << index << ", project_all_children=" << project_all_children |
422 | 2 | << ", children=" |
423 | 2 | << join_debug_strings(children, |
424 | 2 | [](const LocalColumnIndex& child) { return child.debug_string(); }) |
425 | 2 | << "}"; |
426 | 2 | return out.str(); |
427 | 2 | } |
428 | | |
429 | 13 | std::string ColumnMapping::debug_string() const { |
430 | 13 | std::ostringstream out; |
431 | 13 | out << "ColumnMapping{global_index=" << global_index |
432 | 13 | << ", table_column_name=" << table_column_name << ", file_local_id="; |
433 | 13 | if (file_local_id.has_value()) { |
434 | 8 | out << *file_local_id; |
435 | 8 | } else { |
436 | 5 | out << "null"; |
437 | 5 | } |
438 | 13 | out << ", constant_index="; |
439 | 13 | if (constant_index.has_value()) { |
440 | 5 | out << *constant_index; |
441 | 8 | } else { |
442 | 8 | out << "null"; |
443 | 8 | } |
444 | 13 | out << ", file_column_name=" << file_column_name |
445 | 13 | << ", original_file_type=" << data_type_debug_string(original_file_type) |
446 | 13 | << ", original_file_children=" |
447 | 13 | << join_debug_strings(original_file_children, |
448 | 13 | [](const ColumnDefinition& child) { return child.debug_string(); }) |
449 | 13 | << ", file_type=" << data_type_debug_string(file_type) |
450 | 13 | << ", table_type=" << data_type_debug_string(table_type) |
451 | 13 | << ", has_projection=" << (projection != nullptr) << ", child_mappings=" |
452 | 13 | << join_debug_strings(child_mappings, |
453 | 13 | [](const ColumnMapping& child) { return child.debug_string(); }) |
454 | 13 | << ", is_trivial=" << is_trivial << ", is_constant=" << constant_index.has_value() |
455 | 13 | << ", filter_conversion=" << filter_conversion_type_to_string(filter_conversion) |
456 | 13 | << ", virtual_column_type=" << virtual_column_type_to_string(virtual_column_type) |
457 | 13 | << ", has_default_expr=" << (default_expr != nullptr) << "}"; |
458 | 13 | return out.str(); |
459 | 13 | } |
460 | | |
461 | 1 | std::string TableColumnMapper::debug_string() const { |
462 | 1 | std::ostringstream out; |
463 | 1 | out << "TableColumnMapper{options=" << _options.debug_string() << ", mappings=" |
464 | 1 | << join_debug_strings(_mappings, |
465 | 2 | [](const ColumnMapping& mapping) { return mapping.debug_string(); }) |
466 | 1 | << ", hidden_mappings=" |
467 | 1 | << join_debug_strings(_hidden_mappings, |
468 | 1 | [](const ColumnMapping& mapping) { return mapping.debug_string(); }) |
469 | 1 | << ", constant_count=" << _constant_map.size() << "}"; |
470 | 1 | return out.str(); |
471 | 1 | } |
472 | | |
473 | | static const FileSlotRewriteInfo* find_slot_rewrite_info( |
474 | | const VExprSPtr& expr, |
475 | | const std::map<GlobalIndex, FileSlotRewriteInfo>& global_to_file_slot, |
476 | 26.1k | const VSlotRef** slot_ref) { |
477 | 26.1k | if (expr == nullptr) { |
478 | 0 | return nullptr; |
479 | 0 | } |
480 | 26.1k | VExprSPtr slot_expr = expr; |
481 | 26.1k | const bool input_is_cast = is_cast_expr(expr) && expr->get_num_children() == 1; |
482 | 26.1k | if (is_cast_expr(expr) && expr->get_num_children() == 1) { |
483 | 437 | slot_expr = expr->children()[0]; |
484 | 437 | } |
485 | 26.1k | if (!slot_expr->is_slot_ref()) { |
486 | 12.0k | return nullptr; |
487 | 12.0k | } |
488 | 14.1k | const auto* candidate_slot_ref = assert_cast<const VSlotRef*>(slot_expr.get()); |
489 | 14.1k | const auto rewrite_it = global_to_file_slot.find(slot_ref_global_index(*candidate_slot_ref)); |
490 | 14.1k | if (rewrite_it == global_to_file_slot.end()) { |
491 | 0 | return nullptr; |
492 | 0 | } |
493 | 14.1k | if (input_is_cast && !expr->data_type()->equals(*rewrite_it->second.table_type)) { |
494 | 425 | return nullptr; |
495 | 425 | } |
496 | 13.7k | if (slot_ref != nullptr) { |
497 | 13.7k | *slot_ref = candidate_slot_ref; |
498 | 13.7k | } |
499 | 13.7k | return &rewrite_it->second; |
500 | 14.1k | } |
501 | | |
502 | 464k | static bool filter_conversion_has_local_source(FilterConversionType conversion) { |
503 | 464k | switch (conversion) { |
504 | 435k | case FilterConversionType::COPY_DIRECTLY: |
505 | 448k | case FilterConversionType::CAST_FILTER: |
506 | 462k | case FilterConversionType::READER_EXPRESSION: |
507 | 462k | return true; |
508 | 1.59k | case FilterConversionType::FINALIZE_ONLY: |
509 | 1.59k | case FilterConversionType::CONSTANT: |
510 | 1.59k | return false; |
511 | 464k | } |
512 | 0 | return false; |
513 | 464k | } |
514 | | |
515 | | static bool table_filter_has_only_local_entries( |
516 | 39.0k | const TableFilter& table_filter, const std::map<GlobalIndex, FilterEntry>& filter_entries) { |
517 | 40.0k | for (const auto global_index : table_filter.global_indices) { |
518 | 40.0k | const auto entry_it = filter_entries.find(global_index); |
519 | 40.0k | if (entry_it == filter_entries.end() || !entry_it->second.is_local()) { |
520 | 5.58k | return false; |
521 | 5.58k | } |
522 | 40.0k | } |
523 | 33.4k | return true; |
524 | 39.0k | } |
525 | | |
526 | | static VExprSPtr unwrap_literal_for_file_cast(const VExprSPtr& expr, |
527 | 18.5k | const DataTypePtr& table_type) { |
528 | 18.5k | if (expr == nullptr) { |
529 | 0 | return nullptr; |
530 | 0 | } |
531 | 18.5k | if (expr->is_literal()) { |
532 | 18.5k | return expr; |
533 | 18.5k | } |
534 | 18 | if (is_cast_expr(expr) && expr->get_num_children() == 1 && expr->children()[0]->is_literal() && |
535 | 18 | expr->children()[0]->data_type()->equals(*table_type)) { |
536 | 0 | return expr->children()[0]; |
537 | 0 | } |
538 | 18 | return nullptr; |
539 | 18 | } |
540 | | |
541 | 0 | static Field literal_field_from_expr(const VExpr& literal_expr) { |
542 | 0 | DORIS_CHECK(literal_expr.is_literal()); |
543 | 0 | const auto* literal = dynamic_cast<const VLiteral*>(&literal_expr); |
544 | 0 | DORIS_CHECK(literal != nullptr); |
545 | 0 | Field field; |
546 | 0 | literal->get_column_ptr()->get(0, field); |
547 | 0 | return field; |
548 | 0 | } |
549 | | |
550 | | // Table filter localization clones an already-prepared table expr and then rewrites it to file |
551 | | // slots. Only split-local literals and BE cast nodes need table-reader-specific clone behavior; |
552 | | // plain slot refs and literals use their own VExpr::clone_node(). |
553 | 550k | static Status clone_table_expr_node(const VExpr& expr, VExprSPtr* cloned_expr) { |
554 | 550k | DORIS_CHECK(cloned_expr != nullptr); |
555 | 550k | if (const auto* split_literal = dynamic_cast<const SplitLocalFileLiteral*>(&expr)) { |
556 | 0 | *cloned_expr = std::make_shared<SplitLocalFileLiteral>( |
557 | 0 | split_literal->data_type(), literal_field_from_expr(expr), |
558 | 0 | split_literal->original_type(), split_literal->original_field()); |
559 | 550k | } else if (const auto* vcast_expr = dynamic_cast<const VCastExpr*>(&expr); |
560 | 550k | vcast_expr != nullptr && vcast_expr->node_type() == TExprNodeType::CAST_EXPR) { |
561 | 6.71k | *cloned_expr = Cast::create_shared(vcast_expr->data_type()); |
562 | 6.71k | } |
563 | 550k | return Status::OK(); |
564 | 550k | } |
565 | | |
566 | 177k | Status clone_table_expr_tree(const VExprSPtr& expr, VExprSPtr* cloned_expr) { |
567 | 177k | DORIS_CHECK(cloned_expr != nullptr); |
568 | 177k | if (expr == nullptr) { |
569 | 0 | *cloned_expr = nullptr; |
570 | 0 | return Status::OK(); |
571 | 0 | } |
572 | 177k | return expr->deep_clone(cloned_expr, clone_table_expr_node); |
573 | 177k | } |
574 | | |
575 | | static VExprSPtr original_table_literal(const VExprSPtr& literal_expr, |
576 | 18.7k | RewriteContext* rewrite_context = nullptr) { |
577 | 18.7k | DORIS_CHECK(literal_expr != nullptr); |
578 | 18.7k | DORIS_CHECK(literal_expr->is_literal()); |
579 | 18.7k | const auto* rewritten_literal = dynamic_cast<const SplitLocalFileLiteral*>(literal_expr.get()); |
580 | 18.7k | if (rewritten_literal == nullptr) { |
581 | 18.7k | return literal_expr; |
582 | 18.7k | } |
583 | 2 | auto literal = VLiteral::create_shared(rewritten_literal->original_type(), |
584 | 2 | rewritten_literal->original_field()); |
585 | 2 | if (rewrite_context != nullptr) { |
586 | 0 | rewrite_context->add_created_expr(literal); |
587 | 0 | } |
588 | 2 | return literal; |
589 | 18.7k | } |
590 | | |
591 | 40.5k | static ColumnDefinition hidden_column_from_slot_ref(const VSlotRef& slot_ref) { |
592 | 40.5k | ColumnDefinition column; |
593 | 40.5k | column.name = slot_ref.column_name(); |
594 | 40.5k | column.identifier = Field::create_field<TYPE_STRING>(column.name); |
595 | 40.5k | column.type = slot_ref.data_type(); |
596 | 40.5k | return column; |
597 | 40.5k | } |
598 | | |
599 | | static void collect_top_level_slot_columns(const VExprSPtr& expr, |
600 | 131k | std::map<GlobalIndex, ColumnDefinition>* columns) { |
601 | 131k | DORIS_CHECK(columns != nullptr); |
602 | 131k | if (expr == nullptr) { |
603 | 0 | return; |
604 | 0 | } |
605 | 131k | if (expr->is_slot_ref()) { |
606 | 40.6k | const auto* slot_ref = assert_cast<const VSlotRef*>(expr.get()); |
607 | 40.6k | columns->try_emplace(slot_ref_global_index(*slot_ref), |
608 | 40.6k | hidden_column_from_slot_ref(*slot_ref)); |
609 | 40.6k | return; |
610 | 40.6k | } |
611 | 92.5k | for (const auto& child : expr->children()) { |
612 | 92.5k | collect_top_level_slot_columns(child, columns); |
613 | 92.5k | } |
614 | 90.7k | } |
615 | | |
616 | 424 | static std::optional<uint8_t> signed_integer_width(PrimitiveType type) { |
617 | 424 | switch (type) { |
618 | 0 | case TYPE_TINYINT: |
619 | 0 | return 8; |
620 | 4 | case TYPE_SMALLINT: |
621 | 4 | return 16; |
622 | 157 | case TYPE_INT: |
623 | 157 | return 32; |
624 | 136 | case TYPE_BIGINT: |
625 | 136 | return 64; |
626 | 0 | case TYPE_LARGEINT: |
627 | 0 | return 128; |
628 | 127 | default: |
629 | 127 | return std::nullopt; |
630 | 424 | } |
631 | 424 | } |
632 | | |
633 | 111 | static std::optional<uint8_t> floating_width(PrimitiveType type) { |
634 | 111 | switch (type) { |
635 | 0 | case TYPE_FLOAT: |
636 | 0 | return 32; |
637 | 1 | case TYPE_DOUBLE: |
638 | 1 | return 64; |
639 | 110 | default: |
640 | 110 | return std::nullopt; |
641 | 111 | } |
642 | 111 | } |
643 | | |
644 | 17 | static std::optional<uint8_t> floating_exact_integer_width(PrimitiveType type) { |
645 | 17 | switch (type) { |
646 | 0 | case TYPE_FLOAT: |
647 | 0 | return 24; |
648 | 3 | case TYPE_DOUBLE: |
649 | 3 | return 53; |
650 | 14 | default: |
651 | 14 | return std::nullopt; |
652 | 17 | } |
653 | 17 | } |
654 | | |
655 | | static bool is_lossless_file_to_table_numeric_cast(const DataTypePtr& file_type, |
656 | 14.4k | const DataTypePtr& table_type) { |
657 | 14.4k | const auto file_nested_type = remove_nullable(file_type); |
658 | 14.4k | const auto table_nested_type = remove_nullable(table_type); |
659 | 14.4k | if (file_nested_type->equals(*table_nested_type)) { |
660 | 14.1k | return true; |
661 | 14.1k | } |
662 | | |
663 | 265 | const auto file_primitive_type = file_nested_type->get_primitive_type(); |
664 | 265 | const auto table_primitive_type = table_nested_type->get_primitive_type(); |
665 | 265 | if (const auto file_width = signed_integer_width(file_primitive_type)) { |
666 | 157 | if (const auto table_width = signed_integer_width(table_primitive_type)) { |
667 | 140 | return *table_width >= *file_width; |
668 | 140 | } |
669 | 17 | if (const auto table_width = floating_exact_integer_width(table_primitive_type)) { |
670 | 3 | return *table_width >= *file_width; |
671 | 3 | } |
672 | 14 | return false; |
673 | 17 | } |
674 | 108 | if (const auto file_width = floating_width(file_primitive_type)) { |
675 | 1 | const auto table_width = floating_width(table_primitive_type); |
676 | 1 | return table_width.has_value() && *table_width >= *file_width; |
677 | 1 | } |
678 | 107 | return false; |
679 | 108 | } |
680 | | |
681 | | static VExprSPtr rewrite_literal_to_file_type(const VExprSPtr& literal_expr, |
682 | | const FileSlotRewriteInfo& rewrite_info, |
683 | 17.3k | RewriteContext* rewrite_context) { |
684 | 17.3k | DORIS_CHECK(literal_expr != nullptr); |
685 | 17.3k | DORIS_CHECK(literal_expr->is_literal()); |
686 | 17.3k | const auto original_literal = original_table_literal(literal_expr, rewrite_context); |
687 | 17.3k | const Field original_field = literal_field(original_literal); |
688 | 17.3k | if (rewrite_info.file_type->equals(*original_literal->data_type())) { |
689 | 2.92k | return original_literal; |
690 | 2.92k | } |
691 | | // A literal round trip alone cannot prove that file-local evaluation is safe: the file slot |
692 | | // itself may lose information when materialized as the table type. For example, DOUBLE 1.5 |
693 | | // becomes BIGINT 1, so table predicate `value = 1` is true while file predicate |
694 | | // `value = 1.0` is false. Complex Field equality also does not compare nested contents. |
695 | | // Restrict localization to scalar numeric casts that preserve every file value; unsupported |
696 | | // and complex casts keep the table predicate and evaluate after materialization. |
697 | 14.4k | if (!is_lossless_file_to_table_numeric_cast(rewrite_info.file_type, |
698 | 14.4k | original_literal->data_type())) { |
699 | 149 | return nullptr; |
700 | 149 | } |
701 | 14.2k | Field file_field; |
702 | 14.2k | try { |
703 | 14.2k | convert_field_to_type(original_field, *rewrite_info.file_type, &file_field, |
704 | 14.2k | original_literal->data_type().get()); |
705 | 14.2k | } catch (const Exception&) { |
706 | 0 | return nullptr; |
707 | 0 | } |
708 | 14.2k | if (file_field.is_null()) { |
709 | 36 | return nullptr; |
710 | 36 | } |
711 | 14.2k | if (file_field.get_type() != remove_nullable(rewrite_info.file_type)->get_primitive_type()) { |
712 | 38 | return nullptr; |
713 | 38 | } |
714 | 14.1k | Field round_trip_field; |
715 | 14.1k | try { |
716 | 14.1k | convert_field_to_type(file_field, *original_literal->data_type(), &round_trip_field, |
717 | 14.1k | rewrite_info.file_type.get()); |
718 | 14.1k | } catch (const Exception&) { |
719 | 1.12k | return nullptr; |
720 | 1.12k | } |
721 | | // The file-to-table type check protects every possible file value. This round trip separately |
722 | | // proves that the specific predicate boundary is exactly representable in the file type. |
723 | 13.0k | if (round_trip_field != original_field) { |
724 | 2 | return nullptr; |
725 | 2 | } |
726 | 13.0k | auto literal = std::make_shared<SplitLocalFileLiteral>( |
727 | 13.0k | rewrite_info.file_type, file_field, original_literal->data_type(), original_field); |
728 | 13.0k | rewrite_context->add_created_expr(literal); |
729 | 13.0k | return literal; |
730 | 13.0k | } |
731 | | |
732 | | static bool rewrite_binary_slot_literal_predicate( |
733 | | const VExprSPtr& expr, |
734 | | const std::map<GlobalIndex, FileSlotRewriteInfo>& global_to_file_slot, |
735 | 75.1k | RewriteContext* rewrite_context) { |
736 | 75.1k | if (!is_binary_comparison_predicate(expr)) { |
737 | 57.2k | return false; |
738 | 57.2k | } |
739 | 17.9k | auto children = expr->children(); |
740 | 17.9k | const VSlotRef* slot_ref = nullptr; |
741 | 17.9k | const FileSlotRewriteInfo* rewrite_info = |
742 | 17.9k | find_slot_rewrite_info(children[0], global_to_file_slot, &slot_ref); |
743 | 17.9k | int slot_child_idx = 0; |
744 | 17.9k | int literal_child_idx = 1; |
745 | 17.9k | if (rewrite_info == nullptr) { |
746 | 6.20k | rewrite_info = find_slot_rewrite_info(children[1], global_to_file_slot, &slot_ref); |
747 | 6.20k | slot_child_idx = 1; |
748 | 6.20k | literal_child_idx = 0; |
749 | 6.20k | } |
750 | 17.9k | if (rewrite_info == nullptr || slot_ref == nullptr) { |
751 | 6.20k | return false; |
752 | 6.20k | } |
753 | 11.7k | auto literal_expr = |
754 | 11.7k | unwrap_literal_for_file_cast(children[literal_child_idx], rewrite_info->table_type); |
755 | 11.7k | if (literal_expr == nullptr) { |
756 | 18 | return false; |
757 | 18 | } |
758 | | |
759 | 11.7k | auto rewritten_literal = |
760 | 11.7k | rewrite_literal_to_file_type(literal_expr, *rewrite_info, rewrite_context); |
761 | 11.7k | if (rewritten_literal == nullptr) { |
762 | 1.26k | children[literal_child_idx] = original_table_literal(literal_expr, rewrite_context); |
763 | 1.26k | expr->set_children(std::move(children)); |
764 | 1.26k | return false; |
765 | 1.26k | } |
766 | | |
767 | 10.4k | children[slot_child_idx] = create_file_slot_ref(*slot_ref, *rewrite_info, rewrite_context); |
768 | 10.4k | children[literal_child_idx] = std::move(rewritten_literal); |
769 | 10.4k | expr->set_children(std::move(children)); |
770 | 10.4k | return true; |
771 | 11.7k | } |
772 | | |
773 | | static bool rewrite_in_slot_literal_predicate( |
774 | | const VExprSPtr& expr, |
775 | | const std::map<GlobalIndex, FileSlotRewriteInfo>& global_to_file_slot, |
776 | 64.6k | RewriteContext* rewrite_context) { |
777 | 64.6k | if (expr->node_type() != TExprNodeType::IN_PRED || expr->get_num_children() < 2) { |
778 | 62.5k | return false; |
779 | 62.5k | } |
780 | 2.12k | auto children = expr->children(); |
781 | 2.12k | const VSlotRef* slot_ref = nullptr; |
782 | 2.12k | const FileSlotRewriteInfo* rewrite_info = |
783 | 2.12k | find_slot_rewrite_info(children[0], global_to_file_slot, &slot_ref); |
784 | 2.12k | if (rewrite_info == nullptr || slot_ref == nullptr) { |
785 | 76 | return false; |
786 | 76 | } |
787 | | |
788 | 2.04k | VExprSPtrs rewritten_literals; |
789 | 2.04k | rewritten_literals.reserve(children.size() - 1); |
790 | 7.20k | for (size_t child_idx = 1; child_idx < children.size(); ++child_idx) { |
791 | 5.21k | auto literal_expr = |
792 | 5.21k | unwrap_literal_for_file_cast(children[child_idx], rewrite_info->table_type); |
793 | 5.21k | if (literal_expr == nullptr) { |
794 | 0 | return false; |
795 | 0 | } |
796 | 5.21k | auto rewritten_literal = |
797 | 5.21k | rewrite_literal_to_file_type(literal_expr, *rewrite_info, rewrite_context); |
798 | 5.21k | if (rewritten_literal == nullptr) { |
799 | 150 | for (size_t restore_idx = 1; restore_idx < children.size(); ++restore_idx) { |
800 | 100 | auto restore_literal = unwrap_literal_for_file_cast(children[restore_idx], |
801 | 100 | rewrite_info->table_type); |
802 | 100 | if (restore_literal != nullptr) { |
803 | 100 | children[restore_idx] = |
804 | 100 | original_table_literal(restore_literal, rewrite_context); |
805 | 100 | } |
806 | 100 | } |
807 | 50 | expr->set_children(std::move(children)); |
808 | 50 | return false; |
809 | 50 | } |
810 | 5.16k | rewritten_literals.push_back(std::move(rewritten_literal)); |
811 | 5.16k | } |
812 | | |
813 | 1.99k | children[0] = create_file_slot_ref(*slot_ref, *rewrite_info, rewrite_context); |
814 | 7.15k | for (size_t literal_idx = 0; literal_idx < rewritten_literals.size(); ++literal_idx) { |
815 | 5.16k | children[literal_idx + 1] = std::move(rewritten_literals[literal_idx]); |
816 | 5.16k | } |
817 | 1.99k | expr->set_children(std::move(children)); |
818 | 1.99k | return true; |
819 | 2.04k | } |
820 | | |
821 | | static VExprSPtr create_file_struct_child_name_literal(const std::string& file_child_name, |
822 | 875 | RewriteContext* rewrite_context) { |
823 | 875 | auto literal = VLiteral::create_shared(std::make_shared<DataTypeString>(), |
824 | 875 | Field::create_field<TYPE_STRING>(file_child_name)); |
825 | 875 | rewrite_context->add_created_expr(literal); |
826 | 875 | return literal; |
827 | 875 | } |
828 | | |
829 | | static bool needs_complex_file_slot_cast(const DataTypePtr& file_type, |
830 | 1.98k | const DataTypePtr& table_type) { |
831 | 1.98k | if (file_type == nullptr || table_type == nullptr || file_type->equals(*table_type)) { |
832 | 0 | return false; |
833 | 0 | } |
834 | 1.98k | const auto file_nested_type = remove_nullable(file_type); |
835 | 1.98k | const auto table_nested_type = remove_nullable(table_type); |
836 | 1.98k | if (file_nested_type->equals(*table_nested_type)) { |
837 | 0 | return false; |
838 | 0 | } |
839 | 1.98k | return is_complex_type(file_nested_type->get_primitive_type()) || |
840 | 1.98k | is_complex_type(table_nested_type->get_primitive_type()); |
841 | 1.98k | } |
842 | | |
843 | 877 | static bool collect_struct_element_chain(const VExprSPtr& expr, std::vector<VExprSPtr>* chain) { |
844 | 877 | DORIS_CHECK(chain != nullptr); |
845 | 877 | if (!is_struct_element_expr(expr)) { |
846 | 0 | return false; |
847 | 0 | } |
848 | 877 | const auto& parent = expr->children()[0]; |
849 | 877 | if (is_struct_element_expr(parent)) { |
850 | 101 | if (!collect_struct_element_chain(parent, chain)) { |
851 | 0 | return false; |
852 | 0 | } |
853 | 776 | } else if (!parent->is_slot_ref()) { |
854 | | // Only support file-local rewrite for struct child chains rooted directly at a top-level |
855 | | // slot, for example `element_at(s, 'a')` or `element_at(element_at(s, 'a'), 'b')`. |
856 | | // |
857 | | // Do not localize computed complex parents such as |
858 | | // `element_at(element_at(map_values(m), 1), 'full_name')`. The intermediate map/array |
859 | | // result has already been reshaped by scan projection and may have a different child order |
860 | | // from the table expression. Partially rewriting that expression against the file block can |
861 | | // silently evaluate the wrong struct child and filter out valid rows. Those predicates must |
862 | | // remain as table-level conjuncts and be evaluated after TableReader materialization. |
863 | 0 | return false; |
864 | 0 | } |
865 | 877 | chain->push_back(expr); |
866 | 877 | return true; |
867 | 877 | } |
868 | | |
869 | | static bool can_filter_before_table_nullability_alignment(const DataTypePtr& file_type, |
870 | 1.65k | const DataTypePtr& table_type) { |
871 | 1.65k | DORIS_CHECK(file_type != nullptr); |
872 | 1.65k | DORIS_CHECK(table_type != nullptr); |
873 | | // File-local conjuncts run before TableReader validates the materialized table schema. A |
874 | | // nullable file value mapped to a required table value must therefore reach |
875 | | // _align_column_nullability(). For example, with file STRUCT<a: Nullable(INT)>, table |
876 | | // STRUCT<a: BIGINT>, rows [NULL, 20], and `s.a > 10`, filtering in the file domain would drop |
877 | | // NULL first and hide the table-contract violation. The reverse direction is safe: a required |
878 | | // file value can always be wrapped as a nullable table value after filtering. |
879 | 1.65k | return !file_type->is_nullable() || table_type->is_nullable(); |
880 | 1.65k | } |
881 | | |
882 | | static bool rewrite_struct_element_path_to_file_expr( |
883 | | const VExprSPtr& expr, const std::vector<ColumnMapping>& mappings, |
884 | | const std::map<GlobalIndex, FileSlotRewriteInfo>& global_to_file_slot, |
885 | 3.60k | RewriteContext* rewrite_context) { |
886 | 3.60k | ResolvedNestedStructPath resolved; |
887 | 3.60k | if (!resolve_nested_struct_expr_for_file(expr, mappings, &resolved)) { |
888 | 2.82k | return false; |
889 | 2.82k | } |
890 | | |
891 | 776 | std::vector<VExprSPtr> struct_element_chain; |
892 | 776 | if (!collect_struct_element_chain(expr, &struct_element_chain) || |
893 | 776 | struct_element_chain.size() != resolved.file_child_names.size() || |
894 | 776 | struct_element_chain.size() != resolved.file_child_types.size()) { |
895 | 0 | return false; |
896 | 0 | } |
897 | | |
898 | 776 | auto root_children = struct_element_chain.front()->children(); |
899 | 776 | if (!root_children[0]->is_slot_ref()) { |
900 | 0 | return false; |
901 | 0 | } |
902 | 776 | const auto* slot_ref = assert_cast<const VSlotRef*>(root_children[0].get()); |
903 | 776 | const auto rewrite_it = global_to_file_slot.find(slot_ref_global_index(*slot_ref)); |
904 | 776 | if (rewrite_it == global_to_file_slot.end()) { |
905 | 0 | return false; |
906 | 0 | } |
907 | | |
908 | | // Check every value-producing level, including the root struct. A nullable parent also makes |
909 | | // a child access nullable even when the child type itself is required, so checking only the |
910 | | // final leaf is insufficient. If any file level is more nullable than its table counterpart, |
911 | | // keep the complete predicate above TableReader so schema validation observes all NULLs before |
912 | | // row filtering. |
913 | 776 | if (!can_filter_before_table_nullability_alignment(rewrite_it->second.file_type, |
914 | 776 | rewrite_it->second.table_type)) { |
915 | 0 | return false; |
916 | 0 | } |
917 | 1.65k | for (size_t idx = 0; idx < struct_element_chain.size(); ++idx) { |
918 | 877 | if (!can_filter_before_table_nullability_alignment( |
919 | 877 | resolved.file_child_types[idx], struct_element_chain[idx]->data_type())) { |
920 | 2 | return false; |
921 | 2 | } |
922 | 877 | } |
923 | | |
924 | | // File-local conjuncts are prepared against the file-reader Block, so both the root slot and |
925 | | // every struct selector must be expressed in file schema terms. For a renamed Iceberg field, |
926 | | // keeping the table selector would prepare `element_at(file_struct<rename>, 'renamed')` and |
927 | | // fail before any rows are read. Rewrite the whole chain while ColumnMapping still preserves |
928 | | // the table-to-file relationship. Example: |
929 | | // table filter: element_at(element_at(s, 'renamed_parent'), 'renamed_leaf') |
930 | | // old file: s<parent<leaf>> |
931 | | // file filter: element_at(element_at(s, 'parent'), 'leaf') |
932 | 774 | root_children[0] = create_file_slot_ref(*slot_ref, rewrite_it->second, rewrite_context); |
933 | 774 | struct_element_chain.front()->set_children(std::move(root_children)); |
934 | 1.64k | for (size_t idx = 0; idx < struct_element_chain.size(); ++idx) { |
935 | 875 | auto children = struct_element_chain[idx]->children(); |
936 | 875 | children[1] = create_file_struct_child_name_literal(resolved.file_child_names[idx], |
937 | 875 | rewrite_context); |
938 | 875 | struct_element_chain[idx]->set_children(std::move(children)); |
939 | | // The selector name and the expression return type must be moved to file schema together. |
940 | | // Example: |
941 | | // table filter: element_at(element_at(s, 'new_a'), 'new_aa') = 50 |
942 | | // old file: s.new_a STRUCT<aa, bb> |
943 | | // file filter: element_at(element_at(s, 'new_a'), 'aa') = 50 |
944 | | // |
945 | | // If the inner element_at keeps the table return type STRUCT<new_aa, bb>, preparing the |
946 | | // outer element_at(..., 'aa') fails before scanning because `aa` is not a table field. |
947 | 875 | struct_element_chain[idx]->data_type() = resolved.file_child_types[idx]; |
948 | 875 | } |
949 | 774 | return true; |
950 | 776 | } |
951 | | |
952 | | static VExprSPtr cast_file_expr_to_table_type(const VExprSPtr& file_expr, |
953 | | const DataTypePtr& table_type, |
954 | 1.45k | RewriteContext* rewrite_context) { |
955 | 1.45k | DORIS_CHECK(file_expr != nullptr); |
956 | 1.45k | DORIS_CHECK(table_type != nullptr); |
957 | 1.45k | DORIS_CHECK(rewrite_context != nullptr); |
958 | 1.45k | auto cast_expr = Cast::create_shared(table_type); |
959 | 1.45k | cast_expr->add_child(file_expr); |
960 | 1.45k | rewrite_context->add_created_expr(cast_expr); |
961 | 1.45k | return cast_expr; |
962 | 1.45k | } |
963 | | |
964 | | // Prefer comparing in the physical file leaf type when a table predicate uses a promoted struct |
965 | | // child. For example, with table STRUCT<a: BIGINT>, old-file STRUCT<a: INT>, and `s.a = 10`, the |
966 | | // localized predicate should be `file_s.a::INT = 10::INT`, not |
967 | | // `CAST(file_s.a::INT AS BIGINT) = 10::BIGINT`. Converting one literal avoids a cast for every row. |
968 | | // |
969 | | // This rewrite is valid only when every possible file value survives file-to-table conversion and |
970 | | // the particular literal survives a table-to-file-to-table round trip. A value such as BIGINT |
971 | | // 2147483648 cannot be represented by an INT file leaf, so that case deliberately falls back to |
972 | | // `CAST(file_s.a AS BIGINT) = 2147483648`, which preserves the original table-level semantics. |
973 | | static bool rewrite_binary_struct_literal_predicate( |
974 | | const VExprSPtr& expr, const std::vector<ColumnMapping>& filter_mappings, |
975 | | const std::map<GlobalIndex, FileSlotRewriteInfo>& global_to_file_slot, |
976 | 62.7k | RewriteContext* rewrite_context, bool* can_localize) { |
977 | 62.7k | DORIS_CHECK(can_localize != nullptr); |
978 | 62.7k | if (!is_binary_comparison_predicate(expr)) { |
979 | 55.2k | return false; |
980 | 55.2k | } |
981 | 7.48k | auto children = expr->children(); |
982 | 7.48k | int struct_child_idx = -1; |
983 | 7.48k | int literal_child_idx = -1; |
984 | 7.48k | if (is_struct_element_expr(children[0])) { |
985 | 1.55k | struct_child_idx = 0; |
986 | 1.55k | literal_child_idx = 1; |
987 | 5.92k | } else if (is_struct_element_expr(children[1])) { |
988 | 2 | struct_child_idx = 1; |
989 | 2 | literal_child_idx = 0; |
990 | 5.92k | } else { |
991 | 5.92k | return false; |
992 | 5.92k | } |
993 | | |
994 | 1.55k | const auto table_leaf_type = children[struct_child_idx]->data_type(); |
995 | 1.55k | DORIS_CHECK(table_leaf_type != nullptr); |
996 | 1.55k | auto table_literal = unwrap_literal_for_file_cast(children[literal_child_idx], table_leaf_type); |
997 | 1.55k | if (table_literal == nullptr || |
998 | 1.55k | !rewrite_struct_element_path_to_file_expr(children[struct_child_idx], filter_mappings, |
999 | 1.55k | global_to_file_slot, rewrite_context)) { |
1000 | 1.11k | return false; |
1001 | 1.11k | } |
1002 | | |
1003 | 439 | const auto file_leaf_type = children[struct_child_idx]->data_type(); |
1004 | 439 | DORIS_CHECK(file_leaf_type != nullptr); |
1005 | 439 | const FileSlotRewriteInfo leaf_rewrite_info { |
1006 | 439 | .block_position = 0, |
1007 | 439 | .file_type = file_leaf_type, |
1008 | 439 | .table_type = table_leaf_type, |
1009 | 439 | .file_column_name = {}, |
1010 | 439 | }; |
1011 | 439 | auto file_literal = |
1012 | 439 | rewrite_literal_to_file_type(table_literal, leaf_rewrite_info, rewrite_context); |
1013 | 439 | if (file_literal != nullptr) { |
1014 | 405 | children[literal_child_idx] = std::move(file_literal); |
1015 | 405 | } else { |
1016 | 34 | if (!is_lossless_file_to_table_numeric_cast(file_leaf_type, table_leaf_type)) { |
1017 | | // A narrowing or otherwise lossy cast can fail or produce NULL while TableReader |
1018 | | // materializes the table schema. Evaluating it here could filter the offending row |
1019 | | // before that validation, so keep the complete predicate above TableReader. |
1020 | 1 | *can_localize = false; |
1021 | 1 | return true; |
1022 | 1 | } |
1023 | 33 | children[struct_child_idx] = cast_file_expr_to_table_type(children[struct_child_idx], |
1024 | 33 | table_leaf_type, rewrite_context); |
1025 | 33 | children[literal_child_idx] = original_table_literal(table_literal, rewrite_context); |
1026 | 33 | } |
1027 | 438 | expr->set_children(std::move(children)); |
1028 | 438 | return true; |
1029 | 439 | } |
1030 | | |
1031 | | // IN must use one comparison type for its probe and every candidate. Rewrite the complete literal |
1032 | | // set only when all values are exactly representable in the file leaf type; one unsafe value makes |
1033 | | // the whole predicate fall back to a table-type cast. For example, an INT file leaf can evaluate |
1034 | | // `BIGINT IN (10, 20)` as `INT IN (10, 20)`, but `BIGINT IN (10, 2147483648)` must stay BIGINT. |
1035 | | static bool rewrite_in_struct_literal_predicate( |
1036 | | const VExprSPtr& expr, const std::vector<ColumnMapping>& filter_mappings, |
1037 | | const std::map<GlobalIndex, FileSlotRewriteInfo>& global_to_file_slot, |
1038 | 62.2k | RewriteContext* rewrite_context, bool* can_localize) { |
1039 | 62.2k | DORIS_CHECK(can_localize != nullptr); |
1040 | 62.2k | if (expr->node_type() != TExprNodeType::IN_PRED || expr->get_num_children() < 2 || |
1041 | 62.2k | !is_struct_element_expr(expr->children()[0])) { |
1042 | 62.2k | return false; |
1043 | 62.2k | } |
1044 | 22 | auto children = expr->children(); |
1045 | 22 | const auto table_leaf_type = children[0]->data_type(); |
1046 | 22 | DORIS_CHECK(table_leaf_type != nullptr); |
1047 | 22 | VExprSPtrs table_literals; |
1048 | 22 | table_literals.reserve(children.size() - 1); |
1049 | 30 | for (size_t child_idx = 1; child_idx < children.size(); ++child_idx) { |
1050 | 8 | auto table_literal = unwrap_literal_for_file_cast(children[child_idx], table_leaf_type); |
1051 | 8 | if (table_literal == nullptr) { |
1052 | 0 | return false; |
1053 | 0 | } |
1054 | 8 | table_literals.push_back(std::move(table_literal)); |
1055 | 8 | } |
1056 | 22 | if (!rewrite_struct_element_path_to_file_expr(children[0], filter_mappings, global_to_file_slot, |
1057 | 22 | rewrite_context)) { |
1058 | 0 | return false; |
1059 | 0 | } |
1060 | | |
1061 | 22 | const auto file_leaf_type = children[0]->data_type(); |
1062 | 22 | DORIS_CHECK(file_leaf_type != nullptr); |
1063 | 22 | const FileSlotRewriteInfo leaf_rewrite_info { |
1064 | 22 | .block_position = 0, |
1065 | 22 | .file_type = file_leaf_type, |
1066 | 22 | .table_type = table_leaf_type, |
1067 | 22 | .file_column_name = {}, |
1068 | 22 | }; |
1069 | 22 | VExprSPtrs file_literals; |
1070 | 22 | file_literals.reserve(table_literals.size()); |
1071 | 22 | for (const auto& table_literal : table_literals) { |
1072 | 8 | auto file_literal = |
1073 | 8 | rewrite_literal_to_file_type(table_literal, leaf_rewrite_info, rewrite_context); |
1074 | 8 | if (file_literal == nullptr) { |
1075 | 1 | if (!is_lossless_file_to_table_numeric_cast(file_leaf_type, table_leaf_type)) { |
1076 | 0 | *can_localize = false; |
1077 | 0 | return true; |
1078 | 0 | } |
1079 | 1 | children[0] = |
1080 | 1 | cast_file_expr_to_table_type(children[0], table_leaf_type, rewrite_context); |
1081 | 3 | for (size_t literal_idx = 0; literal_idx < table_literals.size(); ++literal_idx) { |
1082 | 2 | children[literal_idx + 1] = |
1083 | 2 | original_table_literal(table_literals[literal_idx], rewrite_context); |
1084 | 2 | } |
1085 | 1 | expr->set_children(std::move(children)); |
1086 | 1 | return true; |
1087 | 1 | } |
1088 | 7 | file_literals.push_back(std::move(file_literal)); |
1089 | 7 | } |
1090 | | |
1091 | 27 | for (size_t literal_idx = 0; literal_idx < file_literals.size(); ++literal_idx) { |
1092 | 6 | children[literal_idx + 1] = std::move(file_literals[literal_idx]); |
1093 | 6 | } |
1094 | 21 | expr->set_children(std::move(children)); |
1095 | 21 | return true; |
1096 | 22 | } |
1097 | | |
1098 | | static VExprSPtr rewrite_struct_or_slot_expr_to_file_expr( |
1099 | | const VExprSPtr& expr, |
1100 | | const std::map<GlobalIndex, FileSlotRewriteInfo>& global_to_file_slot, |
1101 | | const std::vector<ColumnMapping>& filter_mappings, RewriteContext* rewrite_context, |
1102 | 21.7k | bool* can_localize) { |
1103 | 21.7k | if (is_struct_element_expr(expr)) { |
1104 | 2.04k | const auto table_leaf_type = expr->data_type(); |
1105 | 2.04k | if (!rewrite_struct_element_path_to_file_expr(expr, filter_mappings, global_to_file_slot, |
1106 | 2.04k | rewrite_context)) { |
1107 | | // The scanner still evaluates the original table-level conjunct after TableReader |
1108 | | // finalizes the output block. Skipping an unlocalizable file conjunct is therefore |
1109 | | // safer than preparing a partially rewritten expression against the wrong struct |
1110 | | // layout. In particular, do not generate file-local conjuncts for computed complex |
1111 | | // parents such as `element_at(element_at(map_values(m), 1), 'field')`; only direct |
1112 | | // slot-rooted struct chains are supported here. |
1113 | 1.71k | *can_localize = false; |
1114 | 1.71k | return expr; |
1115 | 1.71k | } |
1116 | 331 | DORIS_CHECK(table_leaf_type != nullptr); |
1117 | 331 | DORIS_CHECK(expr->data_type() != nullptr); |
1118 | 331 | if (!expr->data_type()->equals(*table_leaf_type)) { |
1119 | 0 | if (!is_lossless_file_to_table_numeric_cast(expr->data_type(), table_leaf_type)) { |
1120 | 0 | *can_localize = false; |
1121 | 0 | return expr; |
1122 | 0 | } |
1123 | | // Path localization changes the leaf to the physical file type. For example, after an |
1124 | | // Iceberg evolution from STRUCT<a: INT> to STRUCT<a: BIGINT>, the localized old-file |
1125 | | // predicate is initially `element_at(file_col, 'a')::INT = 10::BIGINT`. Cast only the |
1126 | | // leaf back to BIGINT so the comparison has matching operands without forcing a cast |
1127 | | // of the entire evolved struct (whose children may also have been added or reordered). |
1128 | 0 | return cast_file_expr_to_table_type(expr, table_leaf_type, rewrite_context); |
1129 | 0 | } |
1130 | 331 | return expr; |
1131 | 331 | } |
1132 | | |
1133 | 19.6k | DORIS_CHECK(expr->is_slot_ref()); |
1134 | 19.6k | const auto* slot_ref = assert_cast<const VSlotRef*>(expr.get()); |
1135 | 19.6k | const auto rewrite_it = global_to_file_slot.find(slot_ref_global_index(*slot_ref)); |
1136 | 19.6k | if (rewrite_it == global_to_file_slot.end()) { |
1137 | 0 | return expr; |
1138 | 0 | } |
1139 | 19.6k | const auto& rewrite_info = rewrite_it->second; |
1140 | 19.6k | auto file_slot = create_file_slot_ref(*slot_ref, rewrite_info, rewrite_context); |
1141 | 19.6k | if (rewrite_info.file_type->equals(*rewrite_info.table_type)) { |
1142 | 17.6k | return file_slot; |
1143 | 17.6k | } |
1144 | 2.00k | if (needs_complex_file_slot_cast(rewrite_info.file_type, rewrite_info.table_type)) { |
1145 | | // Generic file-local expressions cannot safely cast an evolved complex file slot back to |
1146 | | // the table type. For example, ARRAY_CONTAINS(MAP_KEYS(m), 'person5') only reads map keys, |
1147 | | // but CAST(file_m AS table_m) first forces an incompatible old value struct into the new |
1148 | | // layout. Keep such predicates at table level, after TableReader materializes evolution. |
1149 | 566 | *can_localize = false; |
1150 | 566 | return expr; |
1151 | 566 | } |
1152 | 1.43k | return cast_file_expr_to_table_type(file_slot, rewrite_info.table_type, rewrite_context); |
1153 | 2.00k | } |
1154 | | |
1155 | | static VExprSPtr rewrite_table_expr_to_file_expr( |
1156 | | const VExprSPtr& expr, |
1157 | | const std::map<GlobalIndex, FileSlotRewriteInfo>& global_to_file_slot, |
1158 | | const std::vector<ColumnMapping>& filter_mappings, RewriteContext* rewrite_context, |
1159 | 79.6k | bool* can_localize) { |
1160 | 79.6k | if (expr == nullptr) { |
1161 | 0 | return nullptr; |
1162 | 0 | } |
1163 | 79.6k | DORIS_CHECK(rewrite_context != nullptr); |
1164 | 79.6k | DORIS_CHECK(can_localize != nullptr); |
1165 | 79.6k | if (auto* runtime_filter = dynamic_cast<RuntimeFilterExpr*>(expr.get()); |
1166 | 79.6k | runtime_filter != nullptr) { |
1167 | 4.38k | auto impl = runtime_filter->get_impl(); |
1168 | 4.38k | if (impl == nullptr) { |
1169 | 0 | *can_localize = false; |
1170 | 0 | return expr; |
1171 | 0 | } |
1172 | 4.38k | auto localized_impl = rewrite_table_expr_to_file_expr( |
1173 | 4.38k | impl, global_to_file_slot, filter_mappings, rewrite_context, can_localize); |
1174 | 4.38k | if (!*can_localize) { |
1175 | 0 | return expr; |
1176 | 0 | } |
1177 | 4.38k | runtime_filter->set_impl(std::move(localized_impl)); |
1178 | 4.38k | return expr; |
1179 | 4.38k | } |
1180 | 75.2k | if (rewrite_binary_slot_literal_predicate(expr, global_to_file_slot, rewrite_context)) { |
1181 | 10.4k | return expr; |
1182 | 10.4k | } |
1183 | 64.8k | if (rewrite_in_slot_literal_predicate(expr, global_to_file_slot, rewrite_context)) { |
1184 | 1.94k | return expr; |
1185 | 1.94k | } |
1186 | 62.8k | if (rewrite_binary_struct_literal_predicate(expr, filter_mappings, global_to_file_slot, |
1187 | 62.8k | rewrite_context, can_localize)) { |
1188 | 439 | return expr; |
1189 | 439 | } |
1190 | 62.4k | if (rewrite_in_struct_literal_predicate(expr, filter_mappings, global_to_file_slot, |
1191 | 62.4k | rewrite_context, can_localize)) { |
1192 | 4 | return expr; |
1193 | 4 | } |
1194 | 62.4k | if (is_struct_element_expr(expr) || expr->is_slot_ref()) { |
1195 | 21.7k | return rewrite_struct_or_slot_expr_to_file_expr(expr, global_to_file_slot, filter_mappings, |
1196 | 21.7k | rewrite_context, can_localize); |
1197 | 21.7k | } |
1198 | | // The input is a split-local cloned tree. A previous split-local clone may already have |
1199 | | // inserted Cast(slot). Keep that rewrite idempotent: rewrite the cast child from table slot to |
1200 | | // the current split's file slot, and drop the cast when the current split no longer needs it. |
1201 | 40.7k | if (is_cast_expr(expr) && expr->get_num_children() == 1) { |
1202 | 1.62k | const auto& child = expr->children()[0]; |
1203 | 1.62k | if (child->is_slot_ref()) { |
1204 | 1.58k | const auto* slot_ref = assert_cast<const VSlotRef*>(child.get()); |
1205 | 1.58k | const auto rewrite_it = global_to_file_slot.find(slot_ref_global_index(*slot_ref)); |
1206 | 1.58k | if (rewrite_it != global_to_file_slot.end() && |
1207 | 1.58k | expr->data_type()->equals(*rewrite_it->second.table_type)) { |
1208 | 1 | auto rewritten_child = |
1209 | 1 | create_file_slot_ref(*slot_ref, rewrite_it->second, rewrite_context); |
1210 | 1 | if (rewrite_it->second.file_type->equals(*rewrite_it->second.table_type)) { |
1211 | 0 | return rewritten_child; |
1212 | 0 | } |
1213 | 1 | if (needs_complex_file_slot_cast(rewrite_it->second.file_type, |
1214 | 1 | rewrite_it->second.table_type)) { |
1215 | 0 | *can_localize = false; |
1216 | 0 | return expr; |
1217 | 0 | } |
1218 | 1 | expr->set_children({std::move(rewritten_child)}); |
1219 | 1 | return expr; |
1220 | 1 | } |
1221 | 1.58k | } |
1222 | 1.62k | } |
1223 | | |
1224 | 40.7k | VExprSPtrs rewritten_children; |
1225 | 40.7k | rewritten_children.reserve(expr->children().size()); |
1226 | 41.5k | for (const auto& child : expr->children()) { |
1227 | 41.5k | rewritten_children.push_back(rewrite_table_expr_to_file_expr( |
1228 | 41.5k | child, global_to_file_slot, filter_mappings, rewrite_context, can_localize)); |
1229 | 41.5k | } |
1230 | 40.7k | expr->set_children(std::move(rewritten_children)); |
1231 | 40.7k | return expr; |
1232 | 40.7k | } |
1233 | | |
1234 | | static constexpr const char* ROW_LINEAGE_ROW_ID = "_row_id"; |
1235 | | static constexpr const char* ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER = "_last_updated_sequence_number"; |
1236 | | static constexpr int32_t ROW_LINEAGE_ROW_ID_FIELD_ID = 2147483540; |
1237 | | static constexpr int32_t ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER_FIELD_ID = 2147483539; |
1238 | | |
1239 | 13 | static TableVirtualColumnType row_lineage_virtual_column_type(const std::string& column_name) { |
1240 | 13 | if (column_name == ROW_LINEAGE_ROW_ID) { |
1241 | 2 | return TableVirtualColumnType::ROW_ID; |
1242 | 2 | } |
1243 | 11 | if (column_name == ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER) { |
1244 | 2 | return TableVirtualColumnType::LAST_UPDATED_SEQUENCE_NUMBER; |
1245 | 2 | } |
1246 | 9 | return TableVirtualColumnType::INVALID; |
1247 | 11 | } |
1248 | | |
1249 | | static TableVirtualColumnType row_lineage_virtual_column_type_by_field_id( |
1250 | 100k | const ColumnDefinition& column) { |
1251 | 100k | if (!column.has_identifier_field_id()) { |
1252 | 975 | return TableVirtualColumnType::INVALID; |
1253 | 975 | } |
1254 | 99.7k | switch (column.get_identifier_field_id()) { |
1255 | 2.03k | case ROW_LINEAGE_ROW_ID_FIELD_ID: |
1256 | 2.03k | return TableVirtualColumnType::ROW_ID; |
1257 | 1.70k | case ROW_LINEAGE_LAST_UPDATED_SEQ_NUMBER_FIELD_ID: |
1258 | 1.70k | return TableVirtualColumnType::LAST_UPDATED_SEQUENCE_NUMBER; |
1259 | 95.8k | default: |
1260 | 95.8k | return TableVirtualColumnType::INVALID; |
1261 | 99.7k | } |
1262 | 99.7k | } |
1263 | | |
1264 | | static TableVirtualColumnType row_lineage_virtual_column_type(const ColumnDefinition& column, |
1265 | 100k | TableColumnMappingMode mode) { |
1266 | 100k | switch (mode) { |
1267 | 100k | case TableColumnMappingMode::BY_FIELD_ID: |
1268 | 100k | return row_lineage_virtual_column_type_by_field_id(column); |
1269 | 13 | case TableColumnMappingMode::BY_NAME: |
1270 | 13 | case TableColumnMappingMode::BY_INDEX: |
1271 | 13 | return row_lineage_virtual_column_type(column.name); |
1272 | 100k | } |
1273 | 0 | return TableVirtualColumnType::INVALID; |
1274 | 100k | } |
1275 | | |
1276 | | // Returns true when the current file type is not the exact nested type the scan should expose. |
1277 | | // This is about building the projected file-side type/projection, not about whether TableReader |
1278 | | // later needs to rematerialize the complex value back to table layout. |
1279 | 488k | static bool needs_projected_file_type_rebuild(const ColumnMapping& mapping) { |
1280 | 488k | if (!is_complex_type(mapping.file_type->get_primitive_type())) { |
1281 | 273k | return false; |
1282 | 273k | } |
1283 | 215k | if (mapping.child_mappings.empty()) { |
1284 | 0 | return false; |
1285 | 0 | } |
1286 | 215k | DORIS_CHECK(mapping.file_type != nullptr); |
1287 | 215k | DORIS_CHECK(mapping.table_type != nullptr); |
1288 | 215k | if (remove_nullable(mapping.file_type)->get_primitive_type() != |
1289 | 215k | remove_nullable(mapping.table_type)->get_primitive_type()) { |
1290 | 0 | return true; |
1291 | 0 | } |
1292 | 215k | if (!mapping.table_type->equals(*mapping.file_type)) { |
1293 | 16.8k | return true; |
1294 | 16.8k | } |
1295 | 301k | for (const auto& child_mapping : mapping.child_mappings) { |
1296 | | // Rename-only child mappings do not change the file-side projected shape. If field-id |
1297 | | // matching maps table child `renamed_b` to file child `b`, the file reader can still expose |
1298 | | // the original file type as long as child count/order/types are unchanged. |
1299 | 301k | if (!child_mapping.file_local_id.has_value() || |
1300 | 301k | needs_projected_file_type_rebuild(child_mapping)) { |
1301 | 689 | return true; |
1302 | 689 | } |
1303 | 301k | } |
1304 | 197k | return false; |
1305 | 198k | } |
1306 | | |
1307 | | static std::optional<size_t> file_child_ordinal_in_scan_type(const ColumnMapping& mapping, |
1308 | 274k | const ColumnMapping& child_mapping) { |
1309 | 274k | if (!child_mapping.file_local_id.has_value()) { |
1310 | 279 | return std::nullopt; |
1311 | 279 | } |
1312 | 273k | const auto& file_children = !mapping.projected_file_children.empty() |
1313 | 274k | ? mapping.projected_file_children |
1314 | 18.4E | : mapping.original_file_children; |
1315 | 380k | const auto child_it = std::ranges::find_if(file_children, [&](const ColumnDefinition& child) { |
1316 | 380k | return child.file_local_id() == *child_mapping.file_local_id; |
1317 | 380k | }); |
1318 | 273k | if (child_it == file_children.end()) { |
1319 | 0 | return std::nullopt; |
1320 | 0 | } |
1321 | 273k | return static_cast<size_t>(std::distance(file_children.begin(), child_it)); |
1322 | 273k | } |
1323 | | |
1324 | 1.06M | static bool needs_complex_rematerialize(const ColumnMapping& mapping) { |
1325 | 1.06M | if (mapping.child_mappings.empty()) { |
1326 | 877k | return false; |
1327 | 877k | } |
1328 | 192k | if (mapping.table_type == nullptr || mapping.file_type == nullptr || |
1329 | 192k | !mapping.table_type->equals(*mapping.file_type)) { |
1330 | 10.3k | return true; |
1331 | 10.3k | } |
1332 | 453k | for (size_t table_child_idx = 0; table_child_idx < mapping.child_mappings.size(); |
1333 | 274k | ++table_child_idx) { |
1334 | 274k | const auto& child_mapping = mapping.child_mappings[table_child_idx]; |
1335 | 274k | const auto file_child_idx = file_child_ordinal_in_scan_type(mapping, child_mapping); |
1336 | 274k | if (!file_child_idx.has_value() || *file_child_idx != table_child_idx || |
1337 | 274k | needs_complex_rematerialize(child_mapping) || |
1338 | 274k | (child_mapping.table_type != nullptr && child_mapping.file_type != nullptr && |
1339 | 272k | !child_mapping.table_type->equals(*child_mapping.file_type))) { |
1340 | 1.87k | return true; |
1341 | 1.87k | } |
1342 | 274k | } |
1343 | 179k | return false; |
1344 | 181k | } |
1345 | | |
1346 | 833k | static bool mapping_can_use_file_column_directly(const ColumnMapping& mapping) { |
1347 | 833k | if (mapping.table_type == nullptr || mapping.file_type == nullptr) { |
1348 | 0 | return false; |
1349 | 0 | } |
1350 | 833k | const auto table_type = remove_nullable(mapping.table_type); |
1351 | 833k | const auto file_type = remove_nullable(mapping.file_type); |
1352 | 833k | const bool same_timestamptz_with_different_scale = |
1353 | 833k | table_type->get_primitive_type() == TYPE_TIMESTAMPTZ && |
1354 | 833k | file_type->get_primitive_type() == TYPE_TIMESTAMPTZ; |
1355 | 833k | if (!mapping.table_type->equals(*mapping.file_type) && !same_timestamptz_with_different_scale) { |
1356 | 57.7k | return false; |
1357 | 57.7k | } |
1358 | 776k | return !needs_complex_rematerialize(mapping); |
1359 | 833k | } |
1360 | | |
1361 | 1.04M | static bool type_contains_varbinary(const DataTypePtr& type) { |
1362 | 1.04M | DORIS_CHECK(type != nullptr); |
1363 | 1.04M | const auto nested_type = remove_nullable(type); |
1364 | 1.04M | switch (nested_type->get_primitive_type()) { |
1365 | 587 | case TYPE_VARBINARY: |
1366 | 587 | return true; |
1367 | 99.2k | case TYPE_ARRAY: |
1368 | 99.2k | return type_contains_varbinary( |
1369 | 99.2k | assert_cast<const DataTypeArray&>(*nested_type).get_nested_type()); |
1370 | 71.4k | case TYPE_MAP: { |
1371 | 71.4k | const auto& map_type = assert_cast<const DataTypeMap&>(*nested_type); |
1372 | 71.4k | return type_contains_varbinary(map_type.get_key_type()) || |
1373 | 71.5k | type_contains_varbinary(map_type.get_value_type()); |
1374 | 0 | } |
1375 | 71.5k | case TYPE_STRUCT: |
1376 | 71.5k | return std::ranges::any_of( |
1377 | 71.5k | assert_cast<const DataTypeStruct&>(*nested_type).get_elements(), |
1378 | 135k | [](const DataTypePtr& child_type) { return type_contains_varbinary(child_type); }); |
1379 | 807k | default: |
1380 | 807k | return false; |
1381 | 1.04M | } |
1382 | 1.04M | } |
1383 | | |
1384 | 670k | static FilterConversionType direct_filter_conversion(const ColumnMapping& mapping) { |
1385 | 670k | DORIS_CHECK(mapping.table_type != nullptr); |
1386 | 670k | DORIS_CHECK(mapping.file_type != nullptr); |
1387 | | // FileScanOperator deliberately keeps VARBINARY predicates above external readers. Their |
1388 | | // physical binary representations are not uniformly supported by reader-side expression and |
1389 | | // metadata filtering, so localizing a late runtime filter here can incorrectly reject rows. |
1390 | | // Apply the same rule to a complex root because generic array/map/struct expressions rewrite |
1391 | | // the root slot and can otherwise expose a nested VARBINARY child to the reader. |
1392 | 670k | if (type_contains_varbinary(mapping.table_type)) { |
1393 | 593 | return FilterConversionType::FINALIZE_ONLY; |
1394 | 593 | } |
1395 | 670k | const auto table_type = remove_nullable(mapping.table_type); |
1396 | 670k | const auto file_type = remove_nullable(mapping.file_type); |
1397 | | // TIMESTAMPTZ scale mismatch is intentionally materialized as pass-through: a SQL cast rounds |
1398 | | // fractional seconds. A file-local cast would therefore filter different instants from the |
1399 | | // scanner-level predicate evaluated on the pass-through value. |
1400 | 670k | if (table_type->get_primitive_type() == TYPE_TIMESTAMPTZ && |
1401 | 670k | file_type->get_primitive_type() == TYPE_TIMESTAMPTZ && |
1402 | 670k | !mapping.table_type->equals(*mapping.file_type)) { |
1403 | 1 | return FilterConversionType::FINALIZE_ONLY; |
1404 | 1 | } |
1405 | 670k | return mapping.is_trivial ? FilterConversionType::COPY_DIRECTLY |
1406 | 670k | : FilterConversionType::CAST_FILTER; |
1407 | 670k | } |
1408 | | |
1409 | 17.1k | static FilterConversionType projected_filter_conversion(const ColumnMapping& mapping) { |
1410 | 17.1k | const auto conversion = direct_filter_conversion(mapping); |
1411 | 17.1k | return !mapping.is_trivial && conversion != FilterConversionType::FINALIZE_ONLY |
1412 | 17.1k | ? FilterConversionType::READER_EXPRESSION |
1413 | 17.1k | : conversion; |
1414 | 17.1k | } |
1415 | | |
1416 | | static const ColumnDefinition* find_file_child_for_mapping(const ColumnDefinition& table_child, |
1417 | | const ColumnDefinition& file_parent, |
1418 | | TableColumnMappingMode mode, |
1419 | | size_t table_child_idx, |
1420 | 291k | bool allow_ordinal_fallback) { |
1421 | 291k | const auto file_parent_type = remove_nullable(file_parent.type)->get_primitive_type(); |
1422 | 291k | switch (file_parent_type) { |
1423 | 83.0k | case TYPE_ARRAY: |
1424 | 83.0k | DORIS_CHECK(file_parent.children.size() == 1); |
1425 | 83.0k | return &file_parent.children[0]; |
1426 | 130k | case TYPE_MAP: |
1427 | 130k | DORIS_CHECK(file_parent.children.size() == 2); |
1428 | 130k | if (table_child.name == "key") { |
1429 | 65.3k | return &file_parent.children[0]; |
1430 | 65.3k | } |
1431 | 65.4k | if (table_child.name == "value") { |
1432 | 65.3k | return &file_parent.children[1]; |
1433 | 65.3k | } |
1434 | 76 | if (table_child.local_id == 0 || table_child.local_id == 1) { |
1435 | 0 | return &file_parent.children[table_child.local_id]; |
1436 | 0 | } |
1437 | 76 | return nullptr; |
1438 | 77.8k | default: |
1439 | | // Hive BY_INDEX is a top-level column matching rule. Once a complex root is selected by |
1440 | | // file position, nested struct children follow Hive reader's historical name matching |
1441 | | // semantics; their integer identifiers can be field ids, not file positions. |
1442 | 77.8k | const auto nested_mode = |
1443 | 77.8k | mode == TableColumnMappingMode::BY_INDEX ? TableColumnMappingMode::BY_NAME : mode; |
1444 | 77.8k | if (const auto* file_child = |
1445 | 77.8k | matcher_for_mode(nested_mode).find(table_child, file_parent.children); |
1446 | 77.8k | file_child != nullptr) { |
1447 | 73.1k | return file_child; |
1448 | 73.1k | } |
1449 | 4.65k | if (allow_ordinal_fallback && mode == TableColumnMappingMode::BY_FIELD_ID && |
1450 | 4.65k | !table_child.has_identifier_field_id()) { |
1451 | | // Synthetic children are derived from the table DataType when nested ColumnDefinition |
1452 | | // metadata has been pruned away. They do not carry Iceberg field ids, so try a name |
1453 | | // match before falling back to ordinal order. Example: |
1454 | | // table value type: Struct(age, full_name, gender) |
1455 | | // old file value: Struct(name, age) |
1456 | | // Name matching keeps `age -> age`; the later unused-child fallback can then map the |
1457 | | // renamed `full_name -> name` instead of consuming `age` twice. |
1458 | 3 | if (const auto* file_child = NameMatcher().find(table_child, file_parent.children); |
1459 | 3 | file_child != nullptr) { |
1460 | 1 | return file_child; |
1461 | 1 | } |
1462 | 3 | } |
1463 | | // Some callers only carry the full complex DataType for a projected table column, without |
1464 | | // expanded nested ColumnDefinitions. In that case we can still preserve full materialization |
1465 | | // by walking table/file struct fields by ordinal. This is a fallback only: explicit |
1466 | | // ColumnDefinition children keep using the requested table-format matching rule, which is |
1467 | | // required for precise schema evolution. |
1468 | 4.65k | if (allow_ordinal_fallback && table_child_idx < file_parent.children.size()) { |
1469 | 3 | return &file_parent.children[table_child_idx]; |
1470 | 3 | } |
1471 | 4.65k | return nullptr; |
1472 | 291k | } |
1473 | 291k | } |
1474 | | |
1475 | | static ColumnDefinition synthetic_child_definition(const std::string& name, DataTypePtr type, |
1476 | 77.7k | int32_t local_id) { |
1477 | 77.7k | ColumnDefinition child; |
1478 | 77.7k | child.identifier = Field::create_field<TYPE_STRING>(name); |
1479 | 77.7k | child.local_id = local_id; |
1480 | 77.7k | child.name = name; |
1481 | 77.7k | child.type = std::move(type); |
1482 | 77.7k | return child; |
1483 | 77.7k | } |
1484 | | |
1485 | | static std::vector<ColumnDefinition> synthesize_complex_children_from_type( |
1486 | 40.4k | const DataTypePtr& type) { |
1487 | 40.4k | std::vector<ColumnDefinition> children; |
1488 | 40.4k | if (type == nullptr) { |
1489 | 0 | return children; |
1490 | 0 | } |
1491 | 40.4k | const auto nested_type = remove_nullable(type); |
1492 | 40.4k | switch (nested_type->get_primitive_type()) { |
1493 | 0 | case TYPE_ARRAY: { |
1494 | 0 | const auto* array_type = assert_cast<const DataTypeArray*>(nested_type.get()); |
1495 | 0 | children.push_back(synthetic_child_definition("element", array_type->get_nested_type(), 0)); |
1496 | 0 | break; |
1497 | 0 | } |
1498 | 1 | case TYPE_MAP: { |
1499 | 1 | const auto* map_type = assert_cast<const DataTypeMap*>(nested_type.get()); |
1500 | 1 | children.push_back(synthetic_child_definition("key", map_type->get_key_type(), 0)); |
1501 | 1 | children.push_back(synthetic_child_definition("value", map_type->get_value_type(), 1)); |
1502 | 1 | break; |
1503 | 0 | } |
1504 | 40.4k | case TYPE_STRUCT: { |
1505 | 40.4k | const auto* struct_type = assert_cast<const DataTypeStruct*>(nested_type.get()); |
1506 | 40.4k | children.reserve(struct_type->get_elements().size()); |
1507 | 118k | for (size_t idx = 0; idx < struct_type->get_elements().size(); ++idx) { |
1508 | 77.8k | children.push_back(synthetic_child_definition(struct_type->get_element_name(idx), |
1509 | 77.8k | struct_type->get_element(idx), |
1510 | 77.8k | cast_set<int32_t>(idx))); |
1511 | 77.8k | } |
1512 | 40.4k | break; |
1513 | 0 | } |
1514 | 0 | default: |
1515 | 0 | break; |
1516 | 40.4k | } |
1517 | 40.4k | return children; |
1518 | 40.4k | } |
1519 | | |
1520 | | static void align_struct_child_types_with_parent(const DataTypePtr& parent_type, |
1521 | 40.4k | std::vector<ColumnDefinition>& children) { |
1522 | 40.4k | const auto nested_parent_type = remove_nullable(parent_type); |
1523 | 40.4k | DORIS_CHECK(nested_parent_type->get_primitive_type() == TYPE_STRUCT); |
1524 | 40.4k | const auto type_children = synthesize_complex_children_from_type(parent_type); |
1525 | 77.8k | for (auto& child : children) { |
1526 | 77.8k | const auto type_child = std::ranges::find_if( |
1527 | 131k | type_children, [&](const auto& candidate) { return candidate.name == child.name; }); |
1528 | 18.4E | DORIS_CHECK(type_child != type_children.end()) |
1529 | 18.4E | << "Complex child '" << child.name |
1530 | 18.4E | << "' is absent from its parent table type: " << parent_type->get_name(); |
1531 | | // The parent DataType is the authoritative output contract. Nested schema descriptors can |
1532 | | // omit child nullability even though the parent struct still declares Nullable(String). |
1533 | | // For example, the Iceberg full-schema-change case maps nullable `location` to `city`, but |
1534 | | // its child descriptor carries String. Keeping String here makes rematerialization strip |
1535 | | // the child's null map and creates Struct(String) under a Struct(Nullable(String)) type. |
1536 | 77.8k | child.type = type_child->type; |
1537 | 77.8k | } |
1538 | 40.4k | } |
1539 | | |
1540 | | static bool has_table_child_named(const std::vector<ColumnDefinition>& children, |
1541 | 9.00k | std::string_view name) { |
1542 | 13.5k | return std::ranges::any_of(children, [&](const ColumnDefinition& child) { |
1543 | 13.5k | return std::string_view(child.name) == name; |
1544 | 13.5k | }); |
1545 | 9.00k | } |
1546 | | |
1547 | | static void complete_required_complex_children_from_type(const DataTypePtr& type, |
1548 | 16.5k | std::vector<ColumnDefinition>& children) { |
1549 | 16.5k | if (type == nullptr) { |
1550 | 0 | return; |
1551 | 0 | } |
1552 | 16.5k | const auto nested_type = remove_nullable(type); |
1553 | 16.5k | switch (nested_type->get_primitive_type()) { |
1554 | 4.51k | case TYPE_MAP: { |
1555 | 4.51k | const auto* map_type = assert_cast<const DataTypeMap*>(nested_type.get()); |
1556 | | // MAP key/value are structural children, not independently materializable table fields. |
1557 | | // A key-only projection can still be attached to a whole-map output root, for example: |
1558 | | // SELECT * FROM t WHERE ARRAY_CONTAINS(MAP_KEYS(new_map_column), 'person5') |
1559 | | // |
1560 | | // In that shape the scanner keeps the value stream readable, but the table projection can |
1561 | | // carry only the key child. Add the missing value child so recursive mapping can evolve the |
1562 | | // value type instead of letting TableReader cast old/new value structs directly. |
1563 | 4.51k | if (has_table_child_named(children, "key") && !has_table_child_named(children, "value")) { |
1564 | 2 | children.push_back(synthetic_child_definition("value", map_type->get_value_type(), 1)); |
1565 | 2 | } |
1566 | 4.51k | break; |
1567 | 0 | } |
1568 | 5.01k | case TYPE_ARRAY: |
1569 | | // ARRAY has only one required structural child (`element`), so a non-empty projection is |
1570 | | // already rooted at the element path. |
1571 | 5.01k | break; |
1572 | 7.06k | case TYPE_STRUCT: |
1573 | | // STRUCT children are real fields and must remain prunable. Completing missing struct |
1574 | | // fields here would turn `SELECT s.a` into a full-struct read and undo nested projection. |
1575 | 7.06k | break; |
1576 | 0 | default: |
1577 | 0 | break; |
1578 | 16.5k | } |
1579 | 16.5k | } |
1580 | | |
1581 | | struct PreparedTableChildren { |
1582 | | std::vector<ColumnDefinition> children; |
1583 | | bool synthesized_from_type = false; |
1584 | | }; |
1585 | | |
1586 | | static PreparedTableChildren prepare_table_children_for_mapping( |
1587 | 655k | const ColumnDefinition& table_column, const DataTypePtr& file_type) { |
1588 | 655k | PreparedTableChildren prepared {.children = table_column.children}; |
1589 | 655k | const auto nested_table_type = remove_nullable(table_column.type); |
1590 | | |
1591 | | // Some scan paths, especially SELECT *, only carry the complete complex DataType for a table |
1592 | | // column and leave ColumnDefinition::children empty. Synthesize the hierarchy so recursive |
1593 | | // mapping can evolve nested fields instead of falling back to an invalid whole-column cast. |
1594 | 655k | prepared.synthesized_from_type = prepared.children.empty() && |
1595 | 655k | is_complex_type(nested_table_type->get_primitive_type()) && |
1596 | 655k | !table_column.type->equals(*file_type); |
1597 | 655k | if (prepared.synthesized_from_type) { |
1598 | 4 | prepared.children = synthesize_complex_children_from_type(table_column.type); |
1599 | 655k | } else if (!prepared.children.empty() && !table_column.type->equals(*file_type)) { |
1600 | 16.5k | complete_required_complex_children_from_type(table_column.type, prepared.children); |
1601 | 16.5k | } |
1602 | | |
1603 | 655k | if (!prepared.children.empty() && nested_table_type->get_primitive_type() == TYPE_STRUCT) { |
1604 | | // Struct children are table fields, so the parent Struct type is authoritative for their |
1605 | | // nullability. ARRAY and MAP children are format-level structural wrappers and keep the |
1606 | | // descriptor types used by their recursive mappings. |
1607 | 40.4k | align_struct_child_types_with_parent(table_column.type, prepared.children); |
1608 | 40.4k | } |
1609 | 655k | return prepared; |
1610 | 655k | } |
1611 | | |
1612 | 188k | static Status validate_file_schema_children(const ColumnDefinition& file_field) { |
1613 | 188k | if (file_field.type == nullptr) { |
1614 | 0 | return Status::InternalError("File column '{}' has null type", file_field.name); |
1615 | 0 | } |
1616 | 188k | const auto nested_type = remove_nullable(file_field.type); |
1617 | 188k | size_t expected_children = 0; |
1618 | 188k | bool complex_with_fixed_children = true; |
1619 | 188k | switch (nested_type->get_primitive_type()) { |
1620 | 83.0k | case TYPE_ARRAY: |
1621 | 83.0k | expected_children = 1; |
1622 | 83.0k | break; |
1623 | 65.3k | case TYPE_MAP: |
1624 | 65.3k | expected_children = 2; |
1625 | 65.3k | break; |
1626 | 40.4k | case TYPE_STRUCT: |
1627 | 40.4k | expected_children = |
1628 | 40.4k | assert_cast<const DataTypeStruct*>(nested_type.get())->get_elements().size(); |
1629 | 40.4k | break; |
1630 | 0 | default: |
1631 | 0 | complex_with_fixed_children = false; |
1632 | 0 | break; |
1633 | 188k | } |
1634 | 188k | if (!complex_with_fixed_children || file_field.children.size() == expected_children) { |
1635 | 188k | return Status::OK(); |
1636 | 188k | } |
1637 | 18.4E | return Status::InternalError( |
1638 | 18.4E | "Malformed complex file schema for column '{}': type={}, expected_children={}, " |
1639 | 18.4E | "actual_children={}", |
1640 | 18.4E | file_field.name, file_field.type->get_name(), expected_children, |
1641 | 18.4E | file_field.children.size()); |
1642 | 188k | } |
1643 | | |
1644 | 288k | static bool has_projected_file_children(const ColumnMapping& mapping) { |
1645 | 288k | if (mapping.original_file_children.empty() || mapping.projected_file_children.empty()) { |
1646 | 222k | return false; |
1647 | 222k | } |
1648 | 65.9k | if (mapping.original_file_children.size() != mapping.projected_file_children.size()) { |
1649 | 4.91k | return true; |
1650 | 4.91k | } |
1651 | 160k | for (size_t idx = 0; idx < mapping.original_file_children.size(); ++idx) { |
1652 | 98.9k | if (mapping.original_file_children[idx].file_local_id() != |
1653 | 98.9k | mapping.projected_file_children[idx].file_local_id()) { |
1654 | 0 | return true; |
1655 | 0 | } |
1656 | 98.9k | } |
1657 | 61.0k | return false; |
1658 | 61.0k | } |
1659 | | |
1660 | 288k | static bool needs_nested_file_projection(const ColumnMapping& mapping) { |
1661 | 288k | if (has_projected_file_children(mapping)) { |
1662 | | // Return True if the projected child column is missing / re-ordered |
1663 | 4.92k | return true; |
1664 | 4.92k | } |
1665 | 283k | return std::ranges::any_of(mapping.child_mappings, [](const ColumnMapping& child_mapping) { |
1666 | 100k | return needs_nested_file_projection(child_mapping); |
1667 | 100k | }); |
1668 | 288k | } |
1669 | | |
1670 | | static Status build_complex_projection(const ColumnMapping& mapping, LocalColumnIndex* projection); |
1671 | | |
1672 | | // Build the projected file children/type according to the pruned complex projection. For example, |
1673 | | // if we have a struct column `s` with children `id` and `name`, and the projection only keeps |
1674 | | // `s.name`, then the file reader should expose `STRUCT<name ...>`. |
1675 | | static Status rebuild_projected_file_children_and_type( |
1676 | | const DataTypePtr& file_type, const std::vector<ColumnDefinition>& original_file_children, |
1677 | | const std::vector<ColumnMapping>& child_mappings, |
1678 | 17.2k | std::vector<ColumnDefinition>* projected_file_children, DataTypePtr* projected_type) { |
1679 | 17.2k | DORIS_CHECK(file_type != nullptr); |
1680 | 17.2k | DORIS_CHECK(projected_file_children != nullptr); |
1681 | 17.2k | DORIS_CHECK(projected_type != nullptr); |
1682 | 17.2k | ColumnDefinition field; |
1683 | 17.2k | field.type = file_type; |
1684 | 17.2k | field.children = original_file_children; |
1685 | 17.2k | LocalColumnIndex projection = LocalColumnIndex::partial_local(-1); |
1686 | 17.2k | projection.children.reserve(child_mappings.size()); |
1687 | 24.6k | for (const auto* child_mapping : present_child_mappings_in_file_order(child_mappings)) { |
1688 | 24.6k | DORIS_CHECK(child_mapping->file_local_id.has_value()); |
1689 | 24.6k | LocalColumnIndex child_projection; |
1690 | 24.6k | RETURN_IF_ERROR(build_complex_projection(*child_mapping, &child_projection)); |
1691 | 24.6k | projection.children.push_back(std::move(child_projection)); |
1692 | 24.6k | } |
1693 | | |
1694 | 17.2k | ColumnDefinition projected_field; |
1695 | 17.2k | RETURN_IF_ERROR(project_column_definition(field, projection, &projected_field)); |
1696 | 17.2k | *projected_file_children = std::move(projected_field.children); |
1697 | 17.2k | *projected_type = std::move(projected_field.type); |
1698 | 17.2k | return Status::OK(); |
1699 | 17.2k | } |
1700 | | |
1701 | | // Build the complex column projection according to the ColumnMapping which is re-ordered by the |
1702 | | // file-schema's order. |
1703 | | // |
1704 | | // For MAP, a partial projection represents value-subtree pruning only. The key child is not a |
1705 | | // projected output shape; file readers still read full keys to construct ColumnMap offsets and keep |
1706 | | // key semantics unchanged. If a caller tries to project only/prune the key child, the common schema |
1707 | | // projection helper rejects it. |
1708 | 45.4k | static Status build_complex_projection(const ColumnMapping& mapping, LocalColumnIndex* projection) { |
1709 | 45.4k | if (projection == nullptr) { |
1710 | 0 | return Status::InvalidArgument("projection is null"); |
1711 | 0 | } |
1712 | 45.4k | DORIS_CHECK(mapping.file_local_id.has_value()); |
1713 | 45.4k | *projection = LocalColumnIndex::local(*mapping.file_local_id); |
1714 | 45.4k | projection->project_all_children = mapping.child_mappings.empty(); |
1715 | 45.4k | projection->children.clear(); |
1716 | 45.4k | const auto present_children = present_child_mappings_in_file_order(mapping.child_mappings); |
1717 | 45.4k | if (!projection->project_all_children && present_children.empty()) { |
1718 | | // All requested table children under this complex node are missing/default-only. The file |
1719 | | // reader cannot expose an empty complex projection, but TableReader can still rematerialize |
1720 | | // the table shape from a full file subtree and fill the missing children with defaults. |
1721 | 260 | projection->project_all_children = true; |
1722 | 260 | return Status::OK(); |
1723 | 260 | } |
1724 | 45.1k | for (const auto* child_mapping : present_children) { |
1725 | 15.9k | LocalColumnIndex child_projection; |
1726 | 15.9k | RETURN_IF_ERROR(build_complex_projection(*child_mapping, &child_projection)); |
1727 | 15.9k | projection->children.push_back(std::move(child_projection)); |
1728 | 15.9k | } |
1729 | 45.1k | if (!projection->project_all_children && projection->children.empty()) { |
1730 | 0 | return Status::NotSupported("Projection for complex column {} contains no file children", |
1731 | 0 | mapping.file_column_name); |
1732 | 0 | } |
1733 | 45.1k | return Status::OK(); |
1734 | 45.1k | } |
1735 | | |
1736 | | using FilterProjectionMap = std::map<LocalColumnId, LocalColumnIndex>; |
1737 | | |
1738 | | // Update the mapping's file type according to the projection, and determine whether the projection |
1739 | | // is trivial (i.e. the projected file type is the same as the table type, so no need to |
1740 | | // rematerialize the complex value back to table layout after reading from file). |
1741 | | static Status apply_projection_to_mapping_file_type(const LocalColumnIndex& projection, |
1742 | 368k | ColumnMapping* mapping) { |
1743 | 368k | DORIS_CHECK(mapping != nullptr); |
1744 | 368k | if (mapping->original_file_type == nullptr) { |
1745 | 0 | mapping->original_file_type = mapping->file_type; |
1746 | 0 | } |
1747 | 368k | if (mapping->original_file_type == nullptr || |
1748 | 368k | !is_complex_type(remove_nullable(mapping->original_file_type)->get_primitive_type())) { |
1749 | 206k | return Status::OK(); |
1750 | 206k | } |
1751 | 162k | ColumnDefinition field; |
1752 | 162k | field.type = mapping->original_file_type; |
1753 | 162k | field.children = mapping->original_file_children; |
1754 | 162k | ColumnDefinition projected_field; |
1755 | 162k | RETURN_IF_ERROR(project_column_definition(field, projection, &projected_field)); |
1756 | 162k | mapping->file_type = std::move(projected_field.type); |
1757 | 162k | mapping->projected_file_children = std::move(projected_field.children); |
1758 | 162k | mapping->is_trivial = mapping_can_use_file_column_directly(*mapping); |
1759 | 162k | return Status::OK(); |
1760 | 162k | } |
1761 | | |
1762 | | static Status merge_filter_projection(const FilterProjectionMap* filter_projections, |
1763 | 34.1k | LocalColumnIndex* projection) { |
1764 | 34.1k | DORIS_CHECK(projection != nullptr); |
1765 | 34.1k | if (filter_projections == nullptr) { |
1766 | 0 | return Status::OK(); |
1767 | 0 | } |
1768 | 34.1k | const auto filter_projection_it = filter_projections->find(projection->column_id()); |
1769 | 34.1k | if (filter_projection_it == filter_projections->end()) { |
1770 | 33.3k | return Status::OK(); |
1771 | 33.3k | } |
1772 | | // Merge predicate-only nested paths into the root projection that is about to be scanned. |
1773 | | // Example: `SELECT s.a WHERE s.b > 1` first builds the output projection `s -> a` from |
1774 | | // ColumnMapping, while build_nested_struct_filter_projection_map() records `s -> b`. This merge |
1775 | | // produces one file scan projection `s -> a,b`. |
1776 | 780 | RETURN_IF_ERROR(merge_local_column_index(projection, filter_projection_it->second)); |
1777 | 780 | return Status::OK(); |
1778 | 780 | } |
1779 | | |
1780 | 98 | static bool table_root_is_map(const ColumnMapping& mapping) { |
1781 | 98 | if (mapping.table_type == nullptr) { |
1782 | 0 | return false; |
1783 | 0 | } |
1784 | 98 | return remove_nullable(mapping.table_type)->get_primitive_type() == TYPE_MAP; |
1785 | 98 | } |
1786 | | |
1787 | | static Status add_scan_column(FileScanRequest* file_request, ColumnMapping* mapping, |
1788 | | bool is_predicate_column, bool force_full_complex_scan_projection, |
1789 | 374k | const FilterProjectionMap* filter_projections = nullptr) { |
1790 | 374k | const auto file_column_id = LocalColumnId(mapping->file_local_id.value()); |
1791 | 374k | LocalColumnIndex projection = LocalColumnIndex::top_level(file_column_id); |
1792 | | // Columnar readers can turn a complex mapping into a nested file projection, but |
1793 | | // row-oriented readers must scan the full top-level complex field because all children are |
1794 | | // encoded in the same text cell. |
1795 | 374k | if (!force_full_complex_scan_projection && needs_nested_file_projection(*mapping)) { |
1796 | 4.92k | RETURN_IF_ERROR(build_complex_projection(*mapping, &projection)); |
1797 | 4.92k | } |
1798 | 374k | if (is_predicate_column && !force_full_complex_scan_projection) { |
1799 | 34.1k | DCHECK(filter_projections != nullptr); |
1800 | | // If a projected complex root is also used by a predicate, rebuild the predicate scan |
1801 | | // projection from the output mapping before merging predicate-only children. For |
1802 | | // `SELECT s.a WHERE s.b > 1`, build_complex_projection() produces `s -> a` and |
1803 | | // merge_filter_projection() adds `s -> b`, so the predicate column reads both children. |
1804 | 34.1k | RETURN_IF_ERROR(merge_filter_projection(filter_projections, &projection)); |
1805 | 34.1k | } |
1806 | 374k | FileScanRequestBuilder builder(file_request); |
1807 | 374k | if (is_predicate_column) { |
1808 | 34.1k | return builder.add_predicate_column(std::move(projection)); |
1809 | 34.1k | } |
1810 | 340k | return builder.add_non_predicate_column(std::move(projection)); |
1811 | 374k | } |
1812 | | |
1813 | | static const LocalColumnIndex* find_scan_projection( |
1814 | 708k | const std::vector<LocalColumnIndex>& scan_columns, LocalColumnId file_column_id) { |
1815 | 708k | const auto projection_it = |
1816 | 7.63M | std::ranges::find_if(scan_columns, [&](const LocalColumnIndex& projection) { |
1817 | 7.63M | return projection.column_id() == file_column_id; |
1818 | 7.63M | }); |
1819 | 708k | return projection_it == scan_columns.end() ? nullptr : &*projection_it; |
1820 | 708k | } |
1821 | | |
1822 | | // Apply the final scan projection of one root file column back to its ColumnMapping. This updates |
1823 | | // mapping.file_type/projected_file_children from the original file schema to the exact shape that |
1824 | | // FileReader will return. |
1825 | | // |
1826 | | // Example: for `SELECT s.a WHERE s.b > 1`, add_scan_column() keeps only one predicate scan |
1827 | | // projection `s -> a,b`. Applying that projection changes the mapping's file type from the full |
1828 | | // file struct `s<a,b,c>` to the projected file struct `s<a,b>`, so later filter rewrite and |
1829 | | // TableReader final materialization use the same column shape as the file-local block. |
1830 | | static Status apply_scan_projection_to_mapping_file_type(const FileScanRequest& file_request, |
1831 | 369k | ColumnMapping* mapping) { |
1832 | 369k | DORIS_CHECK(mapping != nullptr); |
1833 | 369k | DORIS_CHECK(mapping->file_local_id.has_value()); |
1834 | 369k | const auto file_column_id = LocalColumnId(*mapping->file_local_id); |
1835 | | // Predicate columns are the actual scan projection when a column is used by row-level filters: |
1836 | | // add_scan_column() removes the duplicate non-predicate projection in that case. |
1837 | 369k | const auto* projection = find_scan_projection(file_request.predicate_columns, file_column_id); |
1838 | 369k | if (projection == nullptr) { |
1839 | 340k | projection = find_scan_projection(file_request.non_predicate_columns, file_column_id); |
1840 | 340k | } |
1841 | 369k | DORIS_CHECK(projection != nullptr); |
1842 | 369k | return apply_projection_to_mapping_file_type(*projection, mapping); |
1843 | 369k | } |
1844 | | |
1845 | | // Build extra scan projections required only by row-level filters on nested struct children. |
1846 | | // |
1847 | | // Example: for `SELECT s.a FROM t WHERE s.b.c > 1`, the output projection may only contain `s.a`, |
1848 | | // but the file reader must also read `s.b.c` to evaluate the predicate. This function collects the |
1849 | | // table-side filter path, resolves it through ColumnMapping first, and records the corresponding |
1850 | | // file-side projection in filter_projections. This keeps renamed fields consistent between the scan |
1851 | | // projection and row-level conjunct rewrite. Example: |
1852 | | // table filter path: s -> renamed_b -> c |
1853 | | // old file path: s -> b -> c |
1854 | | // recorded path: s -> b -> c |
1855 | | // When add_scan_column() adds the same root as a predicate column, it rebuilds that root from the |
1856 | | // output mapping, merges this filter-only projection into it, and removes the duplicate |
1857 | | // non-predicate root entry. |
1858 | | static Status build_nested_struct_filter_projection_map( |
1859 | | const std::vector<TableFilter>& table_filters, const std::vector<ColumnMapping>& mappings, |
1860 | 60.9k | FilterProjectionMap* filter_projections) { |
1861 | 60.9k | DORIS_CHECK(filter_projections != nullptr); |
1862 | 60.9k | filter_projections->clear(); |
1863 | 60.9k | for (const auto& table_filter : table_filters) { |
1864 | 39.0k | if (table_filter.conjunct == nullptr) { |
1865 | 0 | continue; |
1866 | 0 | } |
1867 | | // Collect all nested struct paths in the table filter. For example, for |
1868 | | // `s.id > 5 AND element_at(s, 'renamed_name') = 'abc'`, collect the table paths |
1869 | | // `s -> id` and `s -> renamed_name`, then resolve each one to its file-side projection. |
1870 | 39.0k | std::vector<NestedStructPath> paths; |
1871 | 39.0k | collect_nested_struct_paths(table_filter.conjunct->root(), &paths); |
1872 | 39.0k | for (const auto& path : paths) { |
1873 | 3.65k | auto mapping_it = std::ranges::find_if(mappings, [&](const ColumnMapping& mapping) { |
1874 | 3.65k | return mapping.global_index == path.root_global_index; |
1875 | 3.65k | }); |
1876 | 1.25k | if (mapping_it == mappings.end() || !mapping_it->file_local_id.has_value() || |
1877 | 1.25k | path.selectors.empty()) { |
1878 | 382 | continue; |
1879 | 382 | } |
1880 | | |
1881 | 873 | ResolvedNestedStructPath resolved; |
1882 | 873 | LocalColumnIndex root_projection; |
1883 | 873 | if (!resolve_nested_struct_path_for_file(path, mappings, &resolved)) { |
1884 | 98 | if (!table_root_is_map(*mapping_it)) { |
1885 | 97 | continue; |
1886 | 97 | } |
1887 | | // Direct map value filters such as `m.value.a > 1` need the value leaf for row |
1888 | | // evaluation even when the query only projects another value child. This is only a |
1889 | | // scan projection fallback; complex map/array expressions are still not rewritten |
1890 | | // into file-local conjuncts. |
1891 | 1 | LocalColumnIndex child_projection; |
1892 | 1 | RETURN_IF_ERROR(build_file_child_projection_from_schema( |
1893 | 1 | mapping_it->original_file_children, path.selectors, &child_projection)); |
1894 | 1 | if (child_projection.local_id() < 0) { |
1895 | 0 | continue; |
1896 | 0 | } |
1897 | 1 | root_projection = LocalColumnIndex::partial_local(*mapping_it->file_local_id); |
1898 | 1 | root_projection.children.push_back(std::move(child_projection)); |
1899 | 775 | } else { |
1900 | 775 | root_projection = std::move(resolved.file_projection); |
1901 | 775 | } |
1902 | 776 | auto filter_projection_it = filter_projections->find(root_projection.column_id()); |
1903 | 776 | if (filter_projection_it == filter_projections->end()) { |
1904 | 708 | filter_projections->emplace(root_projection.column_id(), |
1905 | 708 | std::move(root_projection)); |
1906 | 708 | continue; |
1907 | 708 | } |
1908 | 68 | RETURN_IF_ERROR( |
1909 | 68 | merge_local_column_index(&filter_projection_it->second, root_projection)); |
1910 | 68 | } |
1911 | 39.0k | } |
1912 | 60.9k | return Status::OK(); |
1913 | 60.9k | } |
1914 | | |
1915 | 368k | static void rebuild_projection(ColumnMapping* mapping, LocalIndex block_position) { |
1916 | 368k | DORIS_CHECK(mapping->file_local_id.has_value()); |
1917 | 368k | if (mapping->is_trivial || needs_complex_rematerialize(*mapping)) { |
1918 | 359k | mapping->projection = VExprContext::create_shared(VSlotRef::create_shared( |
1919 | 359k | cast_set<int>(block_position.value()), cast_set<int>(block_position.value()), -1, |
1920 | 359k | mapping->file_type, mapping->file_column_name)); |
1921 | 359k | return; |
1922 | 359k | } |
1923 | | |
1924 | 8.75k | auto expr = Cast::create_shared(mapping->table_type); |
1925 | 8.75k | expr->add_child(VSlotRef::create_shared(cast_set<int>(block_position.value()), |
1926 | 8.75k | cast_set<int>(block_position.value()), -1, |
1927 | 8.75k | mapping->file_type, mapping->file_column_name)); |
1928 | 8.75k | mapping->projection = VExprContext::create_shared(expr); |
1929 | 8.75k | } |
1930 | | |
1931 | | // Build file slot rewrite info from the localized filter targets. Only local targets can enter |
1932 | | // file-reader expressions; constant and unset targets stay above the file reader. |
1933 | | static std::map<GlobalIndex, FileSlotRewriteInfo> build_file_slot_rewrite_map( |
1934 | | const std::vector<ColumnMapping>& mappings, |
1935 | 61.0k | const std::map<GlobalIndex, FilterEntry>& filter_entries) { |
1936 | 61.0k | std::map<GlobalIndex, FileSlotRewriteInfo> global_to_file_slot; |
1937 | 391k | for (const auto& mapping : mappings) { |
1938 | 391k | const auto entry_it = filter_entries.find(mapping.global_index); |
1939 | 391k | if (entry_it == filter_entries.end() || !entry_it->second.is_local()) { |
1940 | 24.0k | continue; |
1941 | 24.0k | } |
1942 | 367k | DORIS_CHECK(mapping.file_local_id.has_value()); |
1943 | 367k | global_to_file_slot.emplace( |
1944 | 367k | mapping.global_index, |
1945 | 367k | FileSlotRewriteInfo {.block_position = entry_it->second.local_index().value(), |
1946 | 367k | .file_type = mapping.file_type, |
1947 | 367k | .table_type = mapping.table_type, |
1948 | 367k | .file_column_name = mapping.file_column_name}); |
1949 | 367k | } |
1950 | 61.0k | return global_to_file_slot; |
1951 | 61.0k | } |
1952 | | |
1953 | | Status TableColumnMapper::_create_by_index_mapping(const ColumnDefinition& table_column, |
1954 | | const std::vector<ColumnDefinition>& file_schema, |
1955 | 33.5k | ColumnMapping* mapping) { |
1956 | 33.5k | DORIS_CHECK(mapping != nullptr); |
1957 | 33.5k | DORIS_CHECK(!table_column.is_partition_key); |
1958 | | |
1959 | | // Key contract: in BY_INDEX mode, `ColumnDefinition::identifier` TYPE_INT is interpreted as the |
1960 | | // 0-based position of this column inside `file_schema`. FE writes the physical file position |
1961 | | // of each non-partition projected column into that identifier. This interpretation allows: |
1962 | | // - sparse projection: read only a subset of file columns (for example only `_col2` |
1963 | | // and `_col4`); |
1964 | | // - column reordering: table column order differs from file column order; |
1965 | | // - no many-to-one mapping: FE must guarantee that each file position is referenced by at |
1966 | | // most one table column. |
1967 | 33.5k | const auto file_index = table_column.get_identifier_position(); |
1968 | | |
1969 | | // Case A: file_index is in range, so build a direct positional mapping. |
1970 | | // The file column name (for example `_col0`) is intentionally ignored here. |
1971 | 33.6k | if (file_index >= 0 && static_cast<size_t>(file_index) < file_schema.size()) { |
1972 | 33.1k | return _create_direct_mapping(table_column, file_schema[static_cast<size_t>(file_index)], |
1973 | 33.1k | mapping); |
1974 | 33.1k | } |
1975 | | |
1976 | | // Case B: file_index is out of range, which means the file does not contain this column. |
1977 | | // Route it through the missing-column path used by schema evolution. |
1978 | 531 | if (table_column.default_expr != nullptr) { |
1979 | 531 | _set_constant_mapping(mapping, table_column.default_expr); |
1980 | 531 | return Status::OK(); |
1981 | 531 | } |
1982 | | // Keep the mapping empty (`file_local_id` remains `nullopt`) and let the upper finalize |
1983 | | // stage fill NULL/default values. |
1984 | 18.4E | return Status::OK(); |
1985 | 399 | } |
1986 | | |
1987 | 19.3k | void TableColumnMapper::_set_constant_mapping(ColumnMapping* mapping, VExprContextSPtr expr) { |
1988 | 19.3k | DORIS_CHECK(mapping != nullptr); |
1989 | 19.3k | DORIS_CHECK(expr != nullptr); |
1990 | 19.3k | mapping->default_expr = std::move(expr); |
1991 | 19.3k | mapping->constant_index = _constant_map.add(ConstantEntry { |
1992 | 19.3k | .global_index = mapping->global_index, |
1993 | 19.3k | .expr = mapping->default_expr, |
1994 | 19.3k | .type = mapping->table_type, |
1995 | 19.3k | }); |
1996 | 19.3k | mapping->filter_conversion = FilterConversionType::CONSTANT; |
1997 | 19.3k | } |
1998 | | |
1999 | | Status TableColumnMapper::_create_mapping_for_column(const ColumnDefinition& table_column, |
2000 | | GlobalIndex global_index, |
2001 | 391k | ColumnMapping* mapping) { |
2002 | 391k | DORIS_CHECK(mapping != nullptr); |
2003 | 391k | *mapping = ColumnMapping {}; |
2004 | 391k | mapping->global_index = global_index; |
2005 | 391k | mapping->table_column_name = table_column.name; |
2006 | 391k | mapping->table_type = table_column.type; |
2007 | | // Row-lineage names are Iceberg metadata contracts, not reserved names in generic Hive, |
2008 | | // Hudi, or Paimon schemas. Only the Iceberg reader may opt into virtual synthesis. |
2009 | 391k | const auto row_lineage_type = |
2010 | 391k | _options.enable_row_lineage_virtual_columns |
2011 | 391k | ? row_lineage_virtual_column_type(table_column, _options.mode) |
2012 | 391k | : TableVirtualColumnType::INVALID; |
2013 | 391k | if (const auto* partition_value = find_partition_value(table_column, _partition_values); |
2014 | 391k | table_column.is_partition_key && partition_value != nullptr) { |
2015 | | // Partition values are split constants and must take precedence over defaults. |
2016 | 13.1k | _set_constant_mapping(mapping, VExprContext::create_shared(VLiteral::create_shared( |
2017 | 13.1k | mapping->table_type, *partition_value))); |
2018 | 378k | } else if (_options.mode == TableColumnMappingMode::BY_INDEX && |
2019 | 378k | !table_column.is_partition_key && table_column.has_identifier_field_id()) { |
2020 | | // BY_INDEX interprets ColumnDefinition::identifier as physical file position. |
2021 | 33.6k | RETURN_IF_ERROR(_create_by_index_mapping(table_column, _file_schema, mapping)); |
2022 | 344k | } else if (const auto* file_field = _find_file_field(table_column, _file_schema)) { |
2023 | | // Normal physical file column mapping. |
2024 | 335k | RETURN_IF_ERROR(_create_direct_mapping(table_column, *file_field, mapping)); |
2025 | 335k | if (row_lineage_type != TableVirtualColumnType::INVALID) { |
2026 | | // Iceberg v3 rewritten files may physically contain row lineage metadata fields. |
2027 | | // File non-null values must be preserved, while file NULLs still inherit from data file |
2028 | | // metadata in IcebergTableReader. Therefore the mapping has a real file source plus a |
2029 | | // virtual post-materialization step, and filters must wait for finalize output. |
2030 | 626 | mapping->virtual_column_type = row_lineage_type; |
2031 | 626 | mapping->filter_conversion = FilterConversionType::FINALIZE_ONLY; |
2032 | 626 | } |
2033 | 335k | } else if (row_lineage_type != TableVirtualColumnType::INVALID) { |
2034 | | // Iceberg row lineage metadata fields are optional in data files. Missing fields are exposed |
2035 | | // as all-NULL table columns first; IcebergTableReader fills inherited values only when the |
2036 | | // split carries first_row_id / last_updated_sequence_number metadata. |
2037 | | // FE may attach a default_expr to these hidden metadata columns, but the Iceberg v3 |
2038 | | // inheritance rule must take precedence over the generic missing-column default path. |
2039 | 3.11k | mapping->virtual_column_type = row_lineage_type; |
2040 | 5.56k | } else if (table_column.name == BeConsts::ICEBERG_ROWID_COL) { |
2041 | | // Doris internal Iceberg row locator is never a physical Iceberg data column. It is built |
2042 | | // from file path, row position and partition metadata for delete/update/merge. |
2043 | 434 | mapping->virtual_column_type = TableVirtualColumnType::ICEBERG_ROWID; |
2044 | 5.13k | } else if (table_column.initial_default_value.has_value()) { |
2045 | 2 | VExprContextSPtr initial_default; |
2046 | 2 | RETURN_IF_ERROR(build_initial_default_literal(table_column, &initial_default)); |
2047 | | // Iceberg metadata is the authoritative logical value for files written before the field |
2048 | | // existed; the generic FE expression may still contain its Base64 transport text. |
2049 | 2 | _set_constant_mapping(mapping, std::move(initial_default)); |
2050 | 5.64k | } else if (table_column.default_expr != nullptr) { |
2051 | | // Missing schema-evolution column with an explicit default expression. |
2052 | 5.64k | _set_constant_mapping(mapping, table_column.default_expr); |
2053 | 18.4E | } else { |
2054 | 18.4E | if (table_column.is_partition_key) { |
2055 | 0 | return Status::InvalidArgument( |
2056 | 0 | "Table column '{}' (global_index={}) does not have a matching partition value", |
2057 | 0 | table_column.name, mapping->global_index.value()); |
2058 | 0 | } |
2059 | 18.4E | } |
2060 | 391k | return Status::OK(); |
2061 | 391k | } |
2062 | | |
2063 | | Status TableColumnMapper::_create_hidden_filter_mapping(const ColumnDefinition& table_column, |
2064 | | GlobalIndex global_index, |
2065 | 2 | ColumnMapping* mapping) { |
2066 | 2 | auto status = _create_mapping_for_column(table_column, global_index, mapping); |
2067 | 2 | if (mapping->file_local_id.has_value() || mapping->constant_index.has_value() || |
2068 | 2 | mapping->virtual_column_type != TableVirtualColumnType::INVALID) { |
2069 | 0 | return Status::OK(); |
2070 | 0 | } |
2071 | 2 | if (_options.mode == TableColumnMappingMode::BY_NAME) { |
2072 | 0 | return status; |
2073 | 0 | } |
2074 | | |
2075 | | // Predicate-only slot refs carry the table name/type but do not carry the table-format field |
2076 | | // id used by BY_FIELD_ID or the file position used by BY_INDEX. Use a name fallback only for |
2077 | | // hidden filter localization; projected columns still obey the requested mapping mode. |
2078 | 2 | const auto* file_field = |
2079 | 2 | matcher_for_mode(TableColumnMappingMode::BY_NAME).find(table_column, _file_schema); |
2080 | 2 | if (file_field == nullptr) { |
2081 | 0 | return status; |
2082 | 0 | } |
2083 | 2 | ColumnMapping fallback_mapping; |
2084 | 2 | fallback_mapping.global_index = global_index; |
2085 | 2 | fallback_mapping.table_column_name = table_column.name; |
2086 | 2 | fallback_mapping.table_type = table_column.type; |
2087 | 2 | RETURN_IF_ERROR(_create_direct_mapping(table_column, *file_field, &fallback_mapping)); |
2088 | 2 | *mapping = std::move(fallback_mapping); |
2089 | 2 | return Status::OK(); |
2090 | 2 | } |
2091 | | |
2092 | | Status TableColumnMapper::_build_hidden_filter_mappings( |
2093 | 60.8k | const std::vector<TableFilter>& table_filters) { |
2094 | 60.8k | _hidden_mappings.clear(); |
2095 | | |
2096 | 60.8k | std::map<GlobalIndex, ColumnDefinition> filter_columns; |
2097 | 60.8k | for (const auto& table_filter : table_filters) { |
2098 | 39.0k | if (table_filter.conjunct != nullptr) { |
2099 | 39.0k | collect_top_level_slot_columns(table_filter.conjunct->root(), &filter_columns); |
2100 | 39.0k | } |
2101 | 39.0k | } |
2102 | | |
2103 | 60.8k | for (const auto& [global_index, table_column] : filter_columns) { |
2104 | 33.2k | if (_find_mapping(global_index) != nullptr) { |
2105 | | // Ignore columns that are already mapped by the projected columns |
2106 | 33.2k | continue; |
2107 | 33.2k | } |
2108 | 28 | ColumnMapping mapping; |
2109 | 28 | RETURN_IF_ERROR(_create_hidden_filter_mapping(table_column, global_index, &mapping)); |
2110 | 28 | if (mapping.file_local_id.has_value() || mapping.constant_index.has_value() || |
2111 | 28 | mapping.virtual_column_type != TableVirtualColumnType::INVALID) { |
2112 | 2 | _hidden_mappings.push_back(std::move(mapping)); |
2113 | 2 | } |
2114 | 28 | } |
2115 | 60.8k | return Status::OK(); |
2116 | 60.8k | } |
2117 | | |
2118 | | Status TableColumnMapper::create_mapping(const std::vector<ColumnDefinition>& projected_columns, |
2119 | | const std::map<std::string, Field>& partition_values, |
2120 | 60.8k | const std::vector<ColumnDefinition>& file_schema) { |
2121 | 60.8k | clear(); |
2122 | 60.8k | _partition_values = partition_values; |
2123 | 60.8k | _file_schema = file_schema; |
2124 | 452k | for (size_t column_idx = 0; column_idx < projected_columns.size(); ++column_idx) { |
2125 | 391k | ColumnMapping mapping; |
2126 | 391k | RETURN_IF_ERROR(_create_mapping_for_column(projected_columns[column_idx], |
2127 | 391k | GlobalIndex(column_idx), &mapping)); |
2128 | 391k | _mappings.push_back(std::move(mapping)); |
2129 | 391k | } |
2130 | 60.8k | return Status::OK(); |
2131 | 60.8k | } |
2132 | | |
2133 | 182k | std::vector<ColumnMapping> TableColumnMapper::_filter_visible_mappings() const { |
2134 | 182k | std::vector<ColumnMapping> mappings; |
2135 | 182k | mappings.reserve(_mappings.size() + _hidden_mappings.size()); |
2136 | 182k | mappings.insert(mappings.end(), _mappings.begin(), _mappings.end()); |
2137 | 182k | mappings.insert(mappings.end(), _hidden_mappings.begin(), _hidden_mappings.end()); |
2138 | 182k | return mappings; |
2139 | 182k | } |
2140 | | |
2141 | 60.8k | Status TableColumnMapper::_build_filter_entries(const FileScanRequest& file_request) { |
2142 | 60.8k | _filter_entries.clear(); |
2143 | 60.8k | const auto mappings = _filter_visible_mappings(); |
2144 | 392k | for (const auto& mapping : mappings) { |
2145 | 392k | FilterEntry entry; |
2146 | 392k | if (mapping.constant_index.has_value()) { |
2147 | 19.3k | entry = FilterEntry::constant(*mapping.constant_index); |
2148 | 372k | } else if (mapping.file_local_id.has_value() && |
2149 | 372k | filter_conversion_has_local_source(mapping.filter_conversion)) { |
2150 | 367k | const auto local_position_it = |
2151 | 367k | file_request.local_positions.find(LocalColumnId(*mapping.file_local_id)); |
2152 | 367k | if (local_position_it != file_request.local_positions.end()) { |
2153 | 367k | entry = FilterEntry::local(local_position_it->second); |
2154 | 367k | } |
2155 | 367k | } |
2156 | 392k | _filter_entries.emplace(mapping.global_index, entry); |
2157 | 392k | } |
2158 | 60.8k | return Status::OK(); |
2159 | 60.8k | } |
2160 | | |
2161 | | Status TableColumnMapper::create_scan_request( |
2162 | | const std::vector<TableFilter>& table_filters, |
2163 | | const std::vector<ColumnDefinition>& projected_columns, FileScanRequest* file_request, |
2164 | 61.0k | RuntimeState* runtime_state) { |
2165 | | // FileReader evaluates expressions against a file-local block. This mapper owns the |
2166 | | // table-column to file-column conversion, so it also owns the file-local block positions. |
2167 | 61.0k | file_request->predicate_columns.clear(); |
2168 | 61.0k | file_request->non_predicate_columns.clear(); |
2169 | 61.0k | file_request->predicate_only_columns.clear(); |
2170 | 61.0k | file_request->local_positions.clear(); |
2171 | 61.0k | file_request->conjuncts.clear(); |
2172 | 61.0k | file_request->delete_conjuncts.clear(); |
2173 | 61.0k | _filter_entries.clear(); |
2174 | | // 1. Build referenced non-predicate columns |
2175 | 452k | for (size_t column_idx = 0; column_idx < projected_columns.size(); ++column_idx) { |
2176 | 391k | const auto global_index = GlobalIndex(column_idx); |
2177 | 391k | auto* mapping = _find_mapping(global_index); |
2178 | 391k | if (mapping != nullptr && mapping->file_local_id.has_value()) { |
2179 | | // A file column can be read lazily as a non-predicate column only when it is not used |
2180 | | // by row-level expression filters. |
2181 | 368k | bool used_by_filter = false; |
2182 | 368k | for (const auto& table_filter : table_filters) { |
2183 | 85.9k | const auto& global_indices = table_filter.global_indices; |
2184 | 85.9k | if (std::find(global_indices.begin(), global_indices.end(), global_index) != |
2185 | 85.9k | global_indices.end() && |
2186 | 85.9k | filter_conversion_has_local_source(mapping->filter_conversion)) { |
2187 | 28.2k | used_by_filter = true; |
2188 | 28.2k | break; |
2189 | 28.2k | } |
2190 | 85.9k | } |
2191 | 368k | if (!used_by_filter || !enable_lazy_materialization()) { |
2192 | 340k | RETURN_IF_ERROR(add_scan_column(file_request, mapping, false, |
2193 | 340k | force_full_complex_scan_projection())); |
2194 | 340k | } |
2195 | 368k | } |
2196 | 391k | } |
2197 | | // 2. Build referenced predicate columns |
2198 | | // 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. |
2199 | 61.0k | RETURN_IF_ERROR(_build_hidden_filter_mappings(table_filters)); |
2200 | 61.0k | RETURN_IF_ERROR(localize_filters(table_filters, file_request, runtime_state)); |
2201 | 61.0k | for (const auto& mapping : _hidden_mappings) { |
2202 | 2 | if (!mapping.file_local_id.has_value()) { |
2203 | 0 | continue; |
2204 | 0 | } |
2205 | 2 | const auto local_id = LocalColumnId(*mapping.file_local_id); |
2206 | 2 | const bool is_visible_output = |
2207 | 2 | std::ranges::any_of(_mappings, [local_id](const ColumnMapping& visible_mapping) { |
2208 | 2 | return visible_mapping.file_local_id.has_value() && |
2209 | 2 | LocalColumnId(*visible_mapping.file_local_id) == local_id; |
2210 | 2 | }); |
2211 | 2 | if (is_visible_output) { |
2212 | 0 | continue; |
2213 | 0 | } |
2214 | | // File-local filtering is an optimization; Scanner still evaluates the original |
2215 | | // table-level conjunct after TableReader returns. Only truly hidden mappings are absent |
2216 | | // from that scanner-visible block and may safely discard their payload here. |
2217 | 2 | if (std::ranges::any_of(file_request->predicate_columns, |
2218 | 2 | [local_id](const LocalColumnIndex& projection) { |
2219 | 2 | return projection.column_id() == local_id; |
2220 | 2 | }) && |
2221 | 2 | !file_request->is_predicate_only(local_id)) { |
2222 | 2 | file_request->predicate_only_columns.push_back(local_id); |
2223 | 2 | } |
2224 | 2 | } |
2225 | | // 3. Rebuild output projection expressions for projected columns. localize_filters() has |
2226 | | // already applied the final scan projection to mapping.file_type/projected_file_children before |
2227 | | // rewriting filter expressions. |
2228 | 391k | for (auto& mapping : _mappings) { |
2229 | 391k | if (!mapping.file_local_id.has_value()) { |
2230 | 22.8k | continue; |
2231 | 22.8k | } |
2232 | 368k | auto position_it = |
2233 | 368k | file_request->local_positions.find(LocalColumnId(*mapping.file_local_id)); |
2234 | 18.4E | DORIS_CHECK(position_it != file_request->local_positions.end()) |
2235 | 18.4E | << file_request->local_positions.size() << " " << *mapping.file_local_id << " " |
2236 | 18.4E | << mapping.file_column_name; |
2237 | 368k | rebuild_projection(&mapping, position_it->second); |
2238 | 368k | } |
2239 | 61.0k | return Status::OK(); |
2240 | 61.0k | } |
2241 | | |
2242 | 496k | ColumnMapping* TableColumnMapper::_find_mapping(GlobalIndex global_index) { |
2243 | 7.96M | for (auto& mapping : _mappings) { |
2244 | 7.96M | if (mapping.global_index == global_index) { |
2245 | 496k | return &mapping; |
2246 | 496k | } |
2247 | 7.96M | } |
2248 | 18.4E | return nullptr; |
2249 | 496k | } |
2250 | | |
2251 | 72.0k | ColumnMapping* TableColumnMapper::_find_filter_mapping(GlobalIndex global_index) { |
2252 | 72.0k | if (auto* mapping = _find_mapping(global_index); mapping != nullptr) { |
2253 | 72.0k | return mapping; |
2254 | 72.0k | } |
2255 | 18.4E | for (auto& mapping : _hidden_mappings) { |
2256 | 4 | if (mapping.global_index == global_index) { |
2257 | 4 | return &mapping; |
2258 | 4 | } |
2259 | 4 | } |
2260 | 18.4E | return nullptr; |
2261 | 18.4E | } |
2262 | | |
2263 | | Status TableColumnMapper::localize_filters(const std::vector<TableFilter>& table_filters, |
2264 | | FileScanRequest* file_request, |
2265 | 60.9k | RuntimeState* runtime_state) { |
2266 | 60.9k | std::set<LocalColumnId> localized_predicate_columns; |
2267 | 60.9k | FilterProjectionMap filter_projections; |
2268 | 60.9k | auto filter_mappings = _filter_visible_mappings(); |
2269 | 60.9k | RETURN_IF_ERROR(build_nested_struct_filter_projection_map(table_filters, filter_mappings, |
2270 | 60.9k | &filter_projections)); |
2271 | 60.9k | for (const auto& table_filter : table_filters) { |
2272 | 40.2k | for (const auto& global_index : table_filter.global_indices) { |
2273 | 40.2k | auto* mapping = _find_filter_mapping(global_index); |
2274 | 40.2k | if (mapping == nullptr || !mapping->file_local_id.has_value() || |
2275 | 40.2k | !filter_conversion_has_local_source(mapping->filter_conversion)) { |
2276 | 5.83k | continue; |
2277 | 5.83k | } |
2278 | 34.3k | RETURN_IF_ERROR(add_scan_column(file_request, mapping, enable_lazy_materialization(), |
2279 | 34.3k | force_full_complex_scan_projection(), |
2280 | 34.3k | &filter_projections)); |
2281 | 34.3k | } |
2282 | 39.0k | } |
2283 | | // Rebuild the file type for every scan-local mapping before expression rewrite. Predicate-only |
2284 | | // hidden mappings must see the same projected file type as the file reader will produce. |
2285 | 391k | for (auto& mapping : _mappings) { |
2286 | 391k | if (mapping.file_local_id.has_value() && |
2287 | 391k | file_request->local_positions.contains(LocalColumnId(*mapping.file_local_id))) { |
2288 | 368k | RETURN_IF_ERROR(apply_scan_projection_to_mapping_file_type(*file_request, &mapping)); |
2289 | 368k | } |
2290 | 391k | } |
2291 | 60.9k | for (auto& mapping : _hidden_mappings) { |
2292 | 2 | if (mapping.file_local_id.has_value() && |
2293 | 2 | file_request->local_positions.contains(LocalColumnId(*mapping.file_local_id))) { |
2294 | 2 | RETURN_IF_ERROR(apply_scan_projection_to_mapping_file_type(*file_request, &mapping)); |
2295 | 2 | } |
2296 | 2 | } |
2297 | 60.9k | RETURN_IF_ERROR(_build_filter_entries(*file_request)); |
2298 | | |
2299 | | // Build the complete table-slot rewrite map after all predicate columns have been assigned. |
2300 | | // This keeps expression localization independent from filter iteration order. |
2301 | 60.9k | filter_mappings = _filter_visible_mappings(); |
2302 | 60.9k | const auto global_to_file_slot = build_file_slot_rewrite_map(filter_mappings, _filter_entries); |
2303 | 60.9k | for (const auto& table_filter : table_filters) { |
2304 | 39.2k | if (table_filter.conjunct != nullptr && table_filter.conjunct->root() != nullptr) { |
2305 | 39.2k | const auto root = table_filter.conjunct->root(); |
2306 | 39.2k | const auto impl = root->get_impl(); |
2307 | 39.2k | const auto predicate = impl != nullptr ? impl : root; |
2308 | 39.2k | if (!predicate->is_deterministic() || |
2309 | 39.2k | !table_filter_has_only_local_entries(table_filter, _filter_entries)) { |
2310 | 5.58k | continue; |
2311 | 5.58k | } |
2312 | | // Scanner evaluates the original conjunct after final materialization. Only predicates |
2313 | | // whose result is stable across repeated execution may also run as a file-local copy. |
2314 | 33.6k | RewriteContext rewrite_context {.runtime_state = runtime_state}; |
2315 | 33.6k | VExprSPtr rewrite_root; |
2316 | 33.6k | Status clone_status; |
2317 | 33.6k | try { |
2318 | 33.6k | clone_status = clone_table_expr_tree(table_filter.conjunct->root(), &rewrite_root); |
2319 | 33.6k | } catch ([[maybe_unused]] const Exception& e) { |
2320 | | // Some table filters contain complex intermediate values, for example |
2321 | | // `element_at(MAP_VALUES(m)[1], 'age') > 30`. The current file-local rewrite only |
2322 | | // understands top-level slots and struct-element paths rooted at top-level slots; |
2323 | | // cloning such expressions can hit the generic TExpr complex-type limitation. |
2324 | | // Leave them above TableReader, where Scanner evaluates the original table-level |
2325 | | // conjunct after final materialization. |
2326 | 0 | #ifndef NDEBUG |
2327 | 0 | return Status::InternalError( |
2328 | 0 | "Failed to clone table filter for file-local rewrite: {}, expr={}", |
2329 | 0 | e.to_string(), table_filter.conjunct->root()->debug_string()); |
2330 | | #else |
2331 | | continue; |
2332 | | #endif |
2333 | 0 | } catch ([[maybe_unused]] const std::exception& e) { |
2334 | 0 | #ifndef NDEBUG |
2335 | 0 | return Status::InternalError( |
2336 | 0 | "Failed to clone table filter for file-local rewrite: {}, expr={}", |
2337 | 0 | e.what(), table_filter.conjunct->root()->debug_string()); |
2338 | | #else |
2339 | | continue; |
2340 | | #endif |
2341 | 0 | } |
2342 | 33.5k | if (!clone_status.ok()) { |
2343 | 0 | #ifndef NDEBUG |
2344 | 0 | return Status::InternalError( |
2345 | 0 | "Failed to clone table filter for file-local rewrite: {}, expr={}", |
2346 | 0 | clone_status.to_string(), table_filter.conjunct->root()->debug_string()); |
2347 | | #else |
2348 | | continue; |
2349 | | #endif |
2350 | 0 | } |
2351 | 33.5k | bool can_localize = true; |
2352 | 33.5k | auto localized_root = rewrite_table_expr_to_file_expr(rewrite_root, global_to_file_slot, |
2353 | 33.5k | filter_mappings, &rewrite_context, |
2354 | 33.5k | &can_localize); |
2355 | 33.5k | if (!can_localize) { |
2356 | 2.27k | continue; |
2357 | 2.27k | } |
2358 | 31.2k | auto localized_conjunct = VExprContext::create_shared(std::move(localized_root)); |
2359 | 31.2k | RETURN_IF_ERROR(rewrite_context.prepare_created_exprs(localized_conjunct.get())); |
2360 | 31.2k | file_request->conjuncts.push_back(std::move(localized_conjunct)); |
2361 | 31.9k | for (const auto global_index : table_filter.global_indices) { |
2362 | 31.9k | const auto* mapping = _find_filter_mapping(global_index); |
2363 | 31.9k | if (mapping != nullptr && mapping->file_local_id.has_value() && |
2364 | 31.9k | filter_conversion_has_local_source(mapping->filter_conversion)) { |
2365 | 31.8k | localized_predicate_columns.emplace(*mapping->file_local_id); |
2366 | 31.8k | } |
2367 | 31.9k | } |
2368 | 31.2k | } |
2369 | 39.0k | } |
2370 | | |
2371 | | // Candidate columns are added before expression rewriting because their file-block positions |
2372 | | // are needed to localize slot refs. If rewriting rejects every filter that references a visible |
2373 | | // column, move its already-merged output/filter projection to the lazy non-predicate set |
2374 | | // instead of forcing it through the eager predicate path. |
2375 | 391k | for (auto& mapping : _mappings) { |
2376 | 391k | if (!mapping.file_local_id.has_value()) { |
2377 | 22.8k | continue; |
2378 | 22.8k | } |
2379 | 368k | const auto local_id = LocalColumnId(*mapping.file_local_id); |
2380 | 368k | if (localized_predicate_columns.contains(local_id)) { |
2381 | 25.7k | continue; |
2382 | 25.7k | } |
2383 | 343k | const auto predicate_it = std::ranges::find_if( |
2384 | 343k | file_request->predicate_columns, [local_id](const LocalColumnIndex& projection) { |
2385 | 43.7k | return projection.column_id() == local_id; |
2386 | 43.7k | }); |
2387 | 343k | if (predicate_it == file_request->predicate_columns.end()) { |
2388 | 340k | continue; |
2389 | 340k | } |
2390 | 2.43k | file_request->non_predicate_columns.push_back(std::move(*predicate_it)); |
2391 | 2.43k | file_request->predicate_columns.erase(predicate_it); |
2392 | 2.43k | } |
2393 | 60.9k | return Status::OK(); |
2394 | 60.9k | } |
2395 | | |
2396 | | const ColumnDefinition* TableColumnMapper::_find_file_field( |
2397 | | const ColumnDefinition& table_column, |
2398 | 347k | const std::vector<ColumnDefinition>& file_schema) const { |
2399 | 347k | if (table_column.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) { |
2400 | 258k | const auto field_it = std::ranges::find_if(file_schema, [](const ColumnDefinition& field) { |
2401 | 258k | return field.column_type == ColumnType::GLOBAL_ROWID; |
2402 | 258k | }); |
2403 | 6.21k | return field_it == file_schema.end() ? nullptr : &*field_it; |
2404 | 6.21k | } |
2405 | 340k | const auto* matched = matcher_for_mode(_options.mode).find(table_column, file_schema); |
2406 | 340k | if (matched != nullptr || _options.mode != TableColumnMappingMode::BY_FIELD_ID || |
2407 | 340k | !_options.allow_idless_complex_wrapper_projection || table_column.children.empty()) { |
2408 | 340k | return matched; |
2409 | 340k | } |
2410 | 810 | const ColumnDefinition* wrapper = nullptr; |
2411 | 3.33k | for (const auto& candidate : file_schema) { |
2412 | 3.33k | if (candidate.has_identifier_field_id() || candidate.children.empty() || |
2413 | 3.33k | !has_shared_descendant_field_id(table_column, candidate)) { |
2414 | 3.32k | continue; |
2415 | 3.32k | } |
2416 | 6 | if (wrapper != nullptr) { |
2417 | 0 | return nullptr; |
2418 | 0 | } |
2419 | 6 | wrapper = &candidate; |
2420 | 6 | } |
2421 | | // Iceberg Parquet's PruneColumns retains an ID-less complex wrapper when a nested field ID is |
2422 | | // selected. Descendant IDs, not aliases, identify that wrapper; ambiguity remains unmapped. |
2423 | 810 | return wrapper; |
2424 | 810 | } |
2425 | | |
2426 | | Status TableColumnMapper::_create_direct_mapping(const ColumnDefinition& table_column, |
2427 | | const ColumnDefinition& file_field, |
2428 | 655k | ColumnMapping* mapping) const { |
2429 | 655k | DORIS_CHECK(mapping != nullptr); |
2430 | 655k | DORIS_CHECK(file_field.local_id >= 0 || file_field.local_id == GLOBAL_ROWID_COLUMN_ID); |
2431 | 655k | mapping->file_local_id = file_field.local_id; |
2432 | 655k | mapping->table_column_name = table_column.name; |
2433 | 655k | mapping->file_column_name = file_field.name; |
2434 | 655k | mapping->original_file_type = file_field.type; |
2435 | 655k | mapping->original_file_children = file_field.children; |
2436 | 655k | mapping->projected_file_children = file_field.children; |
2437 | 655k | mapping->file_type = file_field.type; |
2438 | 655k | mapping->is_trivial = mapping_can_use_file_column_directly(*mapping); |
2439 | 655k | mapping->filter_conversion = direct_filter_conversion(*mapping); |
2440 | 655k | mapping->child_mappings.clear(); |
2441 | | |
2442 | 655k | auto [table_children, synthesized_table_children] = |
2443 | 655k | prepare_table_children_for_mapping(table_column, mapping->file_type); |
2444 | | |
2445 | 655k | if (!table_children.empty()) { |
2446 | 188k | if (!is_complex_type(remove_nullable(mapping->file_type)->get_primitive_type())) { |
2447 | 0 | return Status::NotSupported( |
2448 | 0 | "Cannot map complex table column '{}' to scalar parquet column '{}', table " |
2449 | 0 | "type={}, file type={}", |
2450 | 0 | table_column.name, file_field.name, mapping->table_type->get_name(), |
2451 | 0 | mapping->file_type->get_name()); |
2452 | 0 | } |
2453 | 188k | RETURN_IF_ERROR(validate_file_schema_children(file_field)); |
2454 | 188k | std::vector<int32_t> synthesized_used_file_child_ids; |
2455 | 480k | for (size_t table_child_idx = 0; table_child_idx < table_children.size(); |
2456 | 291k | ++table_child_idx) { |
2457 | 291k | const auto& table_child = table_children[table_child_idx]; |
2458 | 291k | const auto* file_child = |
2459 | 291k | find_file_child_for_mapping(table_child, file_field, _options.mode, |
2460 | 291k | table_child_idx, synthesized_table_children); |
2461 | 291k | if (file_child == nullptr && !synthesized_table_children && |
2462 | 291k | _options.mode == TableColumnMappingMode::BY_FIELD_ID && |
2463 | 291k | _options.allow_idless_complex_wrapper_projection) { |
2464 | | // Parquet can retain an ID-less wrapper at any depth when a selected descendant |
2465 | | // has an ID; apply the same opt-in fallback used for root lookup recursively. |
2466 | 2.24k | file_child = _find_file_field(table_child, file_field.children); |
2467 | 2.24k | } |
2468 | 291k | if (synthesized_table_children && file_child != nullptr) { |
2469 | 8 | const auto file_child_id = file_child->file_local_id(); |
2470 | 8 | if (std::ranges::find(synthesized_used_file_child_ids, file_child_id) != |
2471 | 8 | synthesized_used_file_child_ids.end()) { |
2472 | 2 | file_child = nullptr; |
2473 | 2 | for (const auto& candidate : file_field.children) { |
2474 | 2 | const auto candidate_id = candidate.file_local_id(); |
2475 | 2 | if (std::ranges::find(synthesized_used_file_child_ids, candidate_id) == |
2476 | 2 | synthesized_used_file_child_ids.end()) { |
2477 | 2 | file_child = &candidate; |
2478 | 2 | break; |
2479 | 2 | } |
2480 | 2 | } |
2481 | 2 | } |
2482 | 8 | if (file_child != nullptr) { |
2483 | 8 | synthesized_used_file_child_ids.push_back(file_child->file_local_id()); |
2484 | 8 | } |
2485 | 8 | } |
2486 | 291k | if (file_child == nullptr) { |
2487 | 4.59k | ColumnMapping child_mapping; |
2488 | 4.59k | child_mapping.table_column_name = table_child.name; |
2489 | 4.59k | child_mapping.file_column_name = table_child.name; |
2490 | 4.59k | child_mapping.table_type = table_child.type; |
2491 | 4.59k | child_mapping.file_type = table_child.type; |
2492 | 4.59k | child_mapping.filter_conversion = FilterConversionType::FINALIZE_ONLY; |
2493 | | // A missing nested field still has its Iceberg initial-default value in every row |
2494 | | // written before the field was added; carry it into recursive materialization. |
2495 | 4.59k | RETURN_IF_ERROR(build_initial_default_column( |
2496 | 4.59k | table_child, &child_mapping.initial_default_column)); |
2497 | 4.59k | mapping->child_mappings.push_back(std::move(child_mapping)); |
2498 | 4.59k | continue; |
2499 | 4.59k | } |
2500 | 286k | ColumnMapping child_mapping; |
2501 | 286k | child_mapping.table_column_name = table_child.name; |
2502 | 286k | child_mapping.table_type = table_child.type; |
2503 | 286k | RETURN_IF_ERROR(_create_direct_mapping(table_child, *file_child, &child_mapping)); |
2504 | 286k | mapping->child_mappings.push_back(std::move(child_mapping)); |
2505 | 286k | } |
2506 | 188k | if (needs_projected_file_type_rebuild(*mapping)) { |
2507 | | // 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. |
2508 | 17.1k | RETURN_IF_ERROR(rebuild_projected_file_children_and_type( |
2509 | 17.1k | mapping->file_type, mapping->original_file_children, mapping->child_mappings, |
2510 | 17.1k | &mapping->projected_file_children, &mapping->file_type)); |
2511 | 17.1k | DCHECK(mapping->table_type != nullptr); |
2512 | 17.1k | mapping->is_trivial = mapping_can_use_file_column_directly(*mapping); |
2513 | 17.1k | mapping->filter_conversion = projected_filter_conversion(*mapping); |
2514 | 17.1k | } |
2515 | 188k | } |
2516 | 655k | return Status::OK(); |
2517 | 655k | } |
2518 | | |
2519 | | } // namespace doris::format |