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 | 5.32k | Status build_initial_default_column(const ColumnDefinition& column, ColumnPtr* value) { |
62 | 5.32k | DORIS_CHECK(value != nullptr); |
63 | 5.32k | *value = nullptr; |
64 | 5.32k | if (!column.initial_default_value.has_value()) { |
65 | 5.32k | return Status::OK(); |
66 | 5.32k | } |
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.8M | bool column_has_name(const ColumnDefinition& column, const std::string& name) { |
139 | 12.8M | if (to_lower(column.name) == to_lower(name)) { |
140 | 294k | return true; |
141 | 294k | } |
142 | 12.5M | if (column.has_identifier_name() && to_lower(column.get_identifier_name()) == to_lower(name)) { |
143 | 0 | return true; |
144 | 0 | } |
145 | 12.5M | 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.5M | } |
149 | | |
150 | 6.73M | bool column_names_match(const ColumnDefinition& lhs, const ColumnDefinition& rhs) { |
151 | 6.74M | if (!lhs.has_name_mapping) { |
152 | 6.74M | if (column_has_name(rhs, lhs.name)) { |
153 | 294k | return true; |
154 | 294k | } |
155 | 6.44M | if (lhs.has_identifier_name() && column_has_name(rhs, lhs.get_identifier_name())) { |
156 | 1 | return true; |
157 | 1 | } |
158 | 6.44M | } |
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.44M | return std::ranges::any_of(lhs.name_mapping, [&](const std::string& alias) { |
162 | 24 | return column_has_name(rhs, alias); |
163 | 24 | }); |
164 | 6.73M | } |
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 | 261k | const std::vector<ColumnDefinition>& file_schema) const override { |
178 | 261k | if (!table_column.has_identifier_field_id()) { |
179 | 7 | return nullptr; |
180 | 7 | } |
181 | 261k | const auto field_id = table_column.get_identifier_field_id(); |
182 | 2.15M | const auto field_it = std::ranges::find_if(file_schema, [&](const ColumnDefinition& field) { |
183 | 2.15M | return field.has_identifier_field_id() && field.get_identifier_field_id() == field_id; |
184 | 2.15M | }); |
185 | 261k | return field_it == file_schema.end() ? nullptr : &*field_it; |
186 | 261k | } |
187 | | }; |
188 | | |
189 | | class NameMatcher final : public ColumnMatcher { |
190 | | public: |
191 | | const ColumnDefinition* find(const ColumnDefinition& table_column, |
192 | 295k | const std::vector<ColumnDefinition>& file_schema) const override { |
193 | 6.73M | const auto field_it = std::ranges::find_if(file_schema, [&](const ColumnDefinition& field) { |
194 | 6.73M | return column_names_match(table_column, field); |
195 | 6.73M | }); |
196 | 295k | return field_it == file_schema.end() ? nullptr : &*field_it; |
197 | 295k | } |
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 | 557k | const ColumnMatcher& matcher_for_mode(TableColumnMappingMode mode) { |
216 | 557k | static const FieldIdMatcher field_id_matcher; |
217 | 557k | static const NameMatcher name_matcher; |
218 | 557k | static const PositionMatcher position_matcher; |
219 | 557k | switch (mode) { |
220 | 261k | case TableColumnMappingMode::BY_FIELD_ID: |
221 | 261k | return field_id_matcher; |
222 | 295k | case TableColumnMappingMode::BY_NAME: |
223 | 295k | return name_matcher; |
224 | 2 | case TableColumnMappingMode::BY_INDEX: |
225 | 2 | return position_matcher; |
226 | 557k | } |
227 | 0 | return field_id_matcher; |
228 | 557k | } |
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 | 535k | const std::map<std::string, Field>& partition_values) { |
311 | 789k | const auto find_by_name = [&](const std::string& name) -> const Field* { |
312 | 789k | const auto value_it = partition_values.find(name); |
313 | 789k | return value_it == partition_values.end() ? nullptr : &value_it->second; |
314 | 789k | }; |
315 | 535k | if (const auto* value = find_by_name(table_column.name); value != nullptr) { |
316 | 25.0k | return value; |
317 | 25.0k | } |
318 | 510k | if (table_column.has_identifier_name()) { |
319 | 254k | if (const auto* value = find_by_name(table_column.get_identifier_name()); |
320 | 254k | value != nullptr) { |
321 | 0 | return value; |
322 | 0 | } |
323 | 254k | } |
324 | 510k | 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 | 510k | return nullptr; |
330 | 510k | } |
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 | 126k | void add_created_expr(VExprSPtr expr) { created_exprs.push_back(std::move(expr)); } |
344 | | |
345 | 71.8k | Status prepare_created_exprs(VExprContext* context) const { |
346 | 71.8k | DORIS_CHECK(context != nullptr); |
347 | 71.8k | RowDescriptor row_desc; |
348 | 125k | for (const auto& expr : created_exprs) { |
349 | 125k | 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 | 125k | RETURN_IF_ERROR(expr->prepare(runtime_state, row_desc, context)); |
355 | 125k | } |
356 | 71.8k | return Status::OK(); |
357 | 71.8k | } |
358 | | }; |
359 | | |
360 | | static VExprSPtr create_file_slot_ref(const VSlotRef& slot_ref, |
361 | | const FileSlotRewriteInfo& rewrite_info, |
362 | 73.7k | RewriteContext* rewrite_context) { |
363 | 73.7k | auto ref = |
364 | 73.7k | VSlotRef::create_shared(slot_ref.slot_id(), cast_set<int>(rewrite_info.block_position), |
365 | 73.7k | -1, rewrite_info.file_type, rewrite_info.file_column_name); |
366 | 73.7k | rewrite_context->add_created_expr(ref); |
367 | 73.7k | return ref; |
368 | 73.7k | } |
369 | | |
370 | 172k | static bool is_cast_expr(const VExprSPtr& expr) { |
371 | 172k | return dynamic_cast<const Cast*>(expr.get()) != nullptr; |
372 | 172k | } |
373 | | |
374 | 202k | static bool is_binary_comparison_predicate(const VExprSPtr& expr) { |
375 | 202k | if (expr == nullptr || expr->get_num_children() != 2 || |
376 | 202k | (expr->node_type() != TExprNodeType::BINARY_PRED && |
377 | 141k | expr->node_type() != TExprNodeType::NULL_AWARE_BINARY_PRED)) { |
378 | 141k | return false; |
379 | 141k | } |
380 | 60.8k | switch (expr->op()) { |
381 | 13.8k | case TExprOpcode::EQ: |
382 | 13.8k | case TExprOpcode::EQ_FOR_NULL: |
383 | 15.6k | case TExprOpcode::NE: |
384 | 18.3k | case TExprOpcode::GE: |
385 | 57.1k | case TExprOpcode::GT: |
386 | 59.0k | case TExprOpcode::LE: |
387 | 60.9k | case TExprOpcode::LT: |
388 | 60.9k | return true; |
389 | 0 | default: |
390 | 0 | return false; |
391 | 60.8k | } |
392 | 60.8k | } |
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 | 61.6k | const VSlotRef** slot_ref) { |
477 | 61.6k | if (expr == nullptr) { |
478 | 0 | return nullptr; |
479 | 0 | } |
480 | 61.6k | VExprSPtr slot_expr = expr; |
481 | 61.6k | const bool input_is_cast = is_cast_expr(expr) && expr->get_num_children() == 1; |
482 | 61.6k | if (is_cast_expr(expr) && expr->get_num_children() == 1) { |
483 | 461 | slot_expr = expr->children()[0]; |
484 | 461 | } |
485 | 61.6k | if (!slot_expr->is_slot_ref()) { |
486 | 13.4k | return nullptr; |
487 | 13.4k | } |
488 | 48.2k | const auto* candidate_slot_ref = assert_cast<const VSlotRef*>(slot_expr.get()); |
489 | 48.2k | const auto rewrite_it = global_to_file_slot.find(slot_ref_global_index(*candidate_slot_ref)); |
490 | 48.2k | if (rewrite_it == global_to_file_slot.end()) { |
491 | 0 | return nullptr; |
492 | 0 | } |
493 | 48.2k | if (input_is_cast && !expr->data_type()->equals(*rewrite_it->second.table_type)) { |
494 | 425 | return nullptr; |
495 | 425 | } |
496 | 47.7k | if (slot_ref != nullptr) { |
497 | 47.7k | *slot_ref = candidate_slot_ref; |
498 | 47.7k | } |
499 | 47.7k | return &rewrite_it->second; |
500 | 48.2k | } |
501 | | |
502 | 714k | static bool filter_conversion_has_local_source(FilterConversionType conversion) { |
503 | 714k | switch (conversion) { |
504 | 673k | case FilterConversionType::COPY_DIRECTLY: |
505 | 697k | case FilterConversionType::CAST_FILTER: |
506 | 713k | case FilterConversionType::READER_EXPRESSION: |
507 | 713k | return true; |
508 | 1.62k | case FilterConversionType::FINALIZE_ONLY: |
509 | 1.62k | case FilterConversionType::CONSTANT: |
510 | 1.62k | return false; |
511 | 714k | } |
512 | 0 | return false; |
513 | 714k | } |
514 | | |
515 | | static bool table_filter_has_only_local_entries( |
516 | 82.7k | const TableFilter& table_filter, const std::map<GlobalIndex, FilterEntry>& filter_entries) { |
517 | 83.7k | for (const auto global_index : table_filter.global_indices) { |
518 | 83.7k | const auto entry_it = filter_entries.find(global_index); |
519 | 83.7k | if (entry_it == filter_entries.end() || !entry_it->second.is_local()) { |
520 | 8.12k | return false; |
521 | 8.12k | } |
522 | 83.7k | } |
523 | 74.6k | return true; |
524 | 82.7k | } |
525 | | |
526 | | static VExprSPtr unwrap_literal_for_file_cast(const VExprSPtr& expr, |
527 | 53.3k | const DataTypePtr& table_type) { |
528 | 53.3k | if (expr == nullptr) { |
529 | 0 | return nullptr; |
530 | 0 | } |
531 | 53.3k | if (expr->is_literal()) { |
532 | 53.3k | return expr; |
533 | 53.3k | } |
534 | 16 | if (is_cast_expr(expr) && expr->get_num_children() == 1 && expr->children()[0]->is_literal() && |
535 | 16 | expr->children()[0]->data_type()->equals(*table_type)) { |
536 | 0 | return expr->children()[0]; |
537 | 0 | } |
538 | 16 | return nullptr; |
539 | 16 | } |
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 | 1.07M | static Status clone_table_expr_node(const VExpr& expr, VExprSPtr* cloned_expr) { |
554 | 1.07M | DORIS_CHECK(cloned_expr != nullptr); |
555 | 1.07M | 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 | 1.07M | } else if (const auto* vcast_expr = dynamic_cast<const VCastExpr*>(&expr); |
560 | 1.07M | vcast_expr != nullptr && vcast_expr->node_type() == TExprNodeType::CAST_EXPR) { |
561 | 6.85k | *cloned_expr = Cast::create_shared(vcast_expr->data_type()); |
562 | 6.85k | } |
563 | 1.07M | return Status::OK(); |
564 | 1.07M | } |
565 | | |
566 | 362k | Status clone_table_expr_tree(const VExprSPtr& expr, VExprSPtr* cloned_expr) { |
567 | 362k | DORIS_CHECK(cloned_expr != nullptr); |
568 | 362k | if (expr == nullptr) { |
569 | 0 | *cloned_expr = nullptr; |
570 | 0 | return Status::OK(); |
571 | 0 | } |
572 | 362k | return expr->deep_clone(cloned_expr, clone_table_expr_node); |
573 | 362k | } |
574 | | |
575 | | static VExprSPtr original_table_literal(const VExprSPtr& literal_expr, |
576 | 53.3k | RewriteContext* rewrite_context = nullptr) { |
577 | 53.3k | DORIS_CHECK(literal_expr != nullptr); |
578 | 53.3k | DORIS_CHECK(literal_expr->is_literal()); |
579 | 53.3k | const auto* rewritten_literal = dynamic_cast<const SplitLocalFileLiteral*>(literal_expr.get()); |
580 | 53.3k | if (rewritten_literal == nullptr) { |
581 | 53.3k | return literal_expr; |
582 | 53.3k | } |
583 | 0 | auto literal = VLiteral::create_shared(rewritten_literal->original_type(), |
584 | 0 | rewritten_literal->original_field()); |
585 | 0 | if (rewrite_context != nullptr) { |
586 | 0 | rewrite_context->add_created_expr(literal); |
587 | 0 | } |
588 | 0 | return literal; |
589 | 53.3k | } |
590 | | |
591 | 84.3k | static ColumnDefinition hidden_column_from_slot_ref(const VSlotRef& slot_ref) { |
592 | 84.3k | ColumnDefinition column; |
593 | 84.3k | column.name = slot_ref.column_name(); |
594 | 84.3k | column.identifier = Field::create_field<TYPE_STRING>(column.name); |
595 | 84.3k | column.type = slot_ref.data_type(); |
596 | 84.3k | return column; |
597 | 84.3k | } |
598 | | |
599 | | static void collect_top_level_slot_columns(const VExprSPtr& expr, |
600 | 259k | std::map<GlobalIndex, ColumnDefinition>* columns) { |
601 | 259k | DORIS_CHECK(columns != nullptr); |
602 | 259k | if (expr == nullptr) { |
603 | 0 | return; |
604 | 0 | } |
605 | 259k | if (expr->is_slot_ref()) { |
606 | 84.4k | const auto* slot_ref = assert_cast<const VSlotRef*>(expr.get()); |
607 | 84.4k | columns->try_emplace(slot_ref_global_index(*slot_ref), |
608 | 84.4k | hidden_column_from_slot_ref(*slot_ref)); |
609 | 84.4k | return; |
610 | 84.4k | } |
611 | 176k | for (const auto& child : expr->children()) { |
612 | 176k | collect_top_level_slot_columns(child, columns); |
613 | 176k | } |
614 | 174k | } |
615 | | |
616 | 484 | static std::optional<uint8_t> signed_integer_width(PrimitiveType type) { |
617 | 484 | switch (type) { |
618 | 0 | case TYPE_TINYINT: |
619 | 0 | return 8; |
620 | 4 | case TYPE_SMALLINT: |
621 | 4 | return 16; |
622 | 185 | case TYPE_INT: |
623 | 185 | return 32; |
624 | 164 | case TYPE_BIGINT: |
625 | 164 | return 64; |
626 | 0 | case TYPE_LARGEINT: |
627 | 0 | return 128; |
628 | 131 | default: |
629 | 131 | return std::nullopt; |
630 | 484 | } |
631 | 484 | } |
632 | | |
633 | 115 | static std::optional<uint8_t> floating_width(PrimitiveType type) { |
634 | 115 | switch (type) { |
635 | 0 | case TYPE_FLOAT: |
636 | 0 | return 32; |
637 | 1 | case TYPE_DOUBLE: |
638 | 1 | return 64; |
639 | 114 | default: |
640 | 114 | return std::nullopt; |
641 | 115 | } |
642 | 115 | } |
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 | 48.9k | const DataTypePtr& table_type) { |
657 | 48.9k | const auto file_nested_type = remove_nullable(file_type); |
658 | 48.9k | const auto table_nested_type = remove_nullable(table_type); |
659 | 48.9k | if (file_nested_type->equals(*table_nested_type)) { |
660 | 48.6k | return true; |
661 | 48.6k | } |
662 | | |
663 | 299 | const auto file_primitive_type = file_nested_type->get_primitive_type(); |
664 | 299 | const auto table_primitive_type = table_nested_type->get_primitive_type(); |
665 | 299 | if (const auto file_width = signed_integer_width(file_primitive_type)) { |
666 | 185 | if (const auto table_width = signed_integer_width(table_primitive_type)) { |
667 | 168 | return *table_width >= *file_width; |
668 | 168 | } |
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 | 114 | 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 | 113 | return false; |
679 | 114 | } |
680 | | |
681 | | static VExprSPtr rewrite_literal_to_file_type(const VExprSPtr& literal_expr, |
682 | | const FileSlotRewriteInfo& rewrite_info, |
683 | 51.8k | RewriteContext* rewrite_context) { |
684 | 51.8k | DORIS_CHECK(literal_expr != nullptr); |
685 | 51.8k | DORIS_CHECK(literal_expr->is_literal()); |
686 | 51.8k | const auto original_literal = original_table_literal(literal_expr, rewrite_context); |
687 | 51.8k | const Field original_field = literal_field(original_literal); |
688 | 51.8k | if (rewrite_info.file_type->equals(*original_literal->data_type())) { |
689 | 2.89k | return original_literal; |
690 | 2.89k | } |
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 | 48.9k | if (!is_lossless_file_to_table_numeric_cast(rewrite_info.file_type, |
698 | 48.9k | original_literal->data_type())) { |
699 | 153 | return nullptr; |
700 | 153 | } |
701 | 48.8k | Field file_field; |
702 | 48.8k | try { |
703 | 48.8k | convert_field_to_type(original_field, *rewrite_info.file_type, &file_field, |
704 | 48.8k | original_literal->data_type().get()); |
705 | 48.8k | } catch (const Exception&) { |
706 | 0 | return nullptr; |
707 | 0 | } |
708 | 48.8k | if (file_field.is_null()) { |
709 | 56 | return nullptr; |
710 | 56 | } |
711 | 48.7k | if (file_field.get_type() != remove_nullable(rewrite_info.file_type)->get_primitive_type()) { |
712 | 50 | return nullptr; |
713 | 50 | } |
714 | 48.6k | Field round_trip_field; |
715 | 48.6k | try { |
716 | 48.6k | convert_field_to_type(file_field, *original_literal->data_type(), &round_trip_field, |
717 | 48.6k | rewrite_info.file_type.get()); |
718 | 48.6k | } catch (const Exception&) { |
719 | 1.18k | return nullptr; |
720 | 1.18k | } |
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 | 47.5k | if (round_trip_field != original_field) { |
724 | 2 | return nullptr; |
725 | 2 | } |
726 | 47.5k | auto literal = std::make_shared<SplitLocalFileLiteral>( |
727 | 47.5k | rewrite_info.file_type, file_field, original_literal->data_type(), original_field); |
728 | 47.5k | rewrite_context->add_created_expr(literal); |
729 | 47.5k | return literal; |
730 | 47.5k | } |
731 | | |
732 | | static bool rewrite_binary_slot_literal_predicate( |
733 | | const VExprSPtr& expr, |
734 | | const std::map<GlobalIndex, FileSlotRewriteInfo>& global_to_file_slot, |
735 | 124k | RewriteContext* rewrite_context) { |
736 | 124k | if (!is_binary_comparison_predicate(expr)) { |
737 | 71.9k | return false; |
738 | 71.9k | } |
739 | 52.6k | auto children = expr->children(); |
740 | 52.6k | const VSlotRef* slot_ref = nullptr; |
741 | 52.6k | const FileSlotRewriteInfo* rewrite_info = |
742 | 52.6k | find_slot_rewrite_info(children[0], global_to_file_slot, &slot_ref); |
743 | 52.6k | int slot_child_idx = 0; |
744 | 52.6k | int literal_child_idx = 1; |
745 | 52.6k | if (rewrite_info == nullptr) { |
746 | 6.91k | rewrite_info = find_slot_rewrite_info(children[1], global_to_file_slot, &slot_ref); |
747 | 6.91k | slot_child_idx = 1; |
748 | 6.91k | literal_child_idx = 0; |
749 | 6.91k | } |
750 | 52.6k | if (rewrite_info == nullptr || slot_ref == nullptr) { |
751 | 6.91k | return false; |
752 | 6.91k | } |
753 | 45.7k | auto literal_expr = |
754 | 45.7k | unwrap_literal_for_file_cast(children[literal_child_idx], rewrite_info->table_type); |
755 | 45.7k | if (literal_expr == nullptr) { |
756 | 18 | return false; |
757 | 18 | } |
758 | | |
759 | 45.6k | auto rewritten_literal = |
760 | 45.6k | rewrite_literal_to_file_type(literal_expr, *rewrite_info, rewrite_context); |
761 | 45.6k | if (rewritten_literal == nullptr) { |
762 | 1.35k | children[literal_child_idx] = original_table_literal(literal_expr, rewrite_context); |
763 | 1.35k | expr->set_children(std::move(children)); |
764 | 1.35k | return false; |
765 | 1.35k | } |
766 | | |
767 | 44.3k | children[slot_child_idx] = create_file_slot_ref(*slot_ref, *rewrite_info, rewrite_context); |
768 | 44.3k | children[literal_child_idx] = std::move(rewritten_literal); |
769 | 44.3k | expr->set_children(std::move(children)); |
770 | 44.3k | return true; |
771 | 45.6k | } |
772 | | |
773 | | static bool rewrite_in_slot_literal_predicate( |
774 | | const VExprSPtr& expr, |
775 | | const std::map<GlobalIndex, FileSlotRewriteInfo>& global_to_file_slot, |
776 | 80.1k | RewriteContext* rewrite_context) { |
777 | 80.1k | if (expr->node_type() != TExprNodeType::IN_PRED || expr->get_num_children() < 2) { |
778 | 78.0k | return false; |
779 | 78.0k | } |
780 | 2.16k | auto children = expr->children(); |
781 | 2.16k | const VSlotRef* slot_ref = nullptr; |
782 | 2.16k | const FileSlotRewriteInfo* rewrite_info = |
783 | 2.16k | find_slot_rewrite_info(children[0], global_to_file_slot, &slot_ref); |
784 | 2.16k | if (rewrite_info == nullptr || slot_ref == nullptr) { |
785 | 76 | return false; |
786 | 76 | } |
787 | | |
788 | 2.08k | VExprSPtrs rewritten_literals; |
789 | 2.08k | rewritten_literals.reserve(children.size() - 1); |
790 | 7.47k | for (size_t child_idx = 1; child_idx < children.size(); ++child_idx) { |
791 | 5.43k | auto literal_expr = |
792 | 5.43k | unwrap_literal_for_file_cast(children[child_idx], rewrite_info->table_type); |
793 | 5.43k | if (literal_expr == nullptr) { |
794 | 0 | return false; |
795 | 0 | } |
796 | 5.43k | auto rewritten_literal = |
797 | 5.43k | rewrite_literal_to_file_type(literal_expr, *rewrite_info, rewrite_context); |
798 | 5.43k | 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.38k | rewritten_literals.push_back(std::move(rewritten_literal)); |
811 | 5.38k | } |
812 | | |
813 | 2.03k | children[0] = create_file_slot_ref(*slot_ref, *rewrite_info, rewrite_context); |
814 | 7.42k | for (size_t literal_idx = 0; literal_idx < rewritten_literals.size(); ++literal_idx) { |
815 | 5.38k | children[literal_idx + 1] = std::move(rewritten_literals[literal_idx]); |
816 | 5.38k | } |
817 | 2.03k | expr->set_children(std::move(children)); |
818 | 2.03k | return true; |
819 | 2.08k | } |
820 | | |
821 | | static VExprSPtr create_file_struct_child_name_literal(const std::string& file_child_name, |
822 | 1.30k | RewriteContext* rewrite_context) { |
823 | 1.30k | auto literal = VLiteral::create_shared(std::make_shared<DataTypeString>(), |
824 | 1.30k | Field::create_field<TYPE_STRING>(file_child_name)); |
825 | 1.30k | rewrite_context->add_created_expr(literal); |
826 | 1.30k | return literal; |
827 | 1.30k | } |
828 | | |
829 | | static bool needs_complex_file_slot_cast(const DataTypePtr& file_type, |
830 | 4.51k | const DataTypePtr& table_type) { |
831 | 4.51k | if (file_type == nullptr || table_type == nullptr || file_type->equals(*table_type)) { |
832 | 0 | return false; |
833 | 0 | } |
834 | 4.51k | const auto file_nested_type = remove_nullable(file_type); |
835 | 4.51k | const auto table_nested_type = remove_nullable(table_type); |
836 | 4.51k | if (file_nested_type->equals(*table_nested_type)) { |
837 | 0 | return false; |
838 | 0 | } |
839 | 4.51k | return is_complex_type(file_nested_type->get_primitive_type()) || |
840 | 4.51k | is_complex_type(table_nested_type->get_primitive_type()); |
841 | 4.51k | } |
842 | | |
843 | 1.31k | static bool collect_struct_element_chain(const VExprSPtr& expr, std::vector<VExprSPtr>* chain) { |
844 | 1.31k | DORIS_CHECK(chain != nullptr); |
845 | 1.31k | if (!is_struct_element_expr(expr)) { |
846 | 0 | return false; |
847 | 0 | } |
848 | 1.31k | const auto& parent = expr->children()[0]; |
849 | 1.31k | if (is_struct_element_expr(parent)) { |
850 | 157 | if (!collect_struct_element_chain(parent, chain)) { |
851 | 0 | return false; |
852 | 0 | } |
853 | 1.15k | } 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 | 1.31k | chain->push_back(expr); |
866 | 1.31k | return true; |
867 | 1.31k | } |
868 | | |
869 | | static bool can_filter_before_table_nullability_alignment(const DataTypePtr& file_type, |
870 | 2.46k | const DataTypePtr& table_type) { |
871 | 2.46k | DORIS_CHECK(file_type != nullptr); |
872 | 2.46k | 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 | 2.46k | return !file_type->is_nullable() || table_type->is_nullable(); |
880 | 2.46k | } |
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 | 4.52k | RewriteContext* rewrite_context) { |
886 | 4.52k | ResolvedNestedStructPath resolved; |
887 | 4.52k | if (!resolve_nested_struct_expr_for_file(expr, mappings, &resolved)) { |
888 | 3.36k | return false; |
889 | 3.36k | } |
890 | | |
891 | 1.15k | std::vector<VExprSPtr> struct_element_chain; |
892 | 1.15k | if (!collect_struct_element_chain(expr, &struct_element_chain) || |
893 | 1.15k | struct_element_chain.size() != resolved.file_child_names.size() || |
894 | 1.15k | struct_element_chain.size() != resolved.file_child_types.size()) { |
895 | 0 | return false; |
896 | 0 | } |
897 | | |
898 | 1.15k | auto root_children = struct_element_chain.front()->children(); |
899 | 1.15k | if (!root_children[0]->is_slot_ref()) { |
900 | 0 | return false; |
901 | 0 | } |
902 | 1.15k | const auto* slot_ref = assert_cast<const VSlotRef*>(root_children[0].get()); |
903 | 1.15k | const auto rewrite_it = global_to_file_slot.find(slot_ref_global_index(*slot_ref)); |
904 | 1.15k | 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 | 1.15k | if (!can_filter_before_table_nullability_alignment(rewrite_it->second.file_type, |
914 | 1.15k | rewrite_it->second.table_type)) { |
915 | 0 | return false; |
916 | 0 | } |
917 | 2.46k | for (size_t idx = 0; idx < struct_element_chain.size(); ++idx) { |
918 | 1.31k | if (!can_filter_before_table_nullability_alignment( |
919 | 1.31k | resolved.file_child_types[idx], struct_element_chain[idx]->data_type())) { |
920 | 2 | return false; |
921 | 2 | } |
922 | 1.31k | } |
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 | 1.15k | root_children[0] = create_file_slot_ref(*slot_ref, rewrite_it->second, rewrite_context); |
933 | 1.15k | struct_element_chain.front()->set_children(std::move(root_children)); |
934 | 2.46k | for (size_t idx = 0; idx < struct_element_chain.size(); ++idx) { |
935 | 1.31k | auto children = struct_element_chain[idx]->children(); |
936 | 1.31k | children[1] = create_file_struct_child_name_literal(resolved.file_child_names[idx], |
937 | 1.31k | rewrite_context); |
938 | 1.31k | 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 | 1.31k | struct_element_chain[idx]->data_type() = resolved.file_child_types[idx]; |
948 | 1.31k | } |
949 | 1.15k | return true; |
950 | 1.15k | } |
951 | | |
952 | | static VExprSPtr cast_file_expr_to_table_type(const VExprSPtr& file_expr, |
953 | | const DataTypePtr& table_type, |
954 | 3.95k | RewriteContext* rewrite_context) { |
955 | 3.95k | DORIS_CHECK(file_expr != nullptr); |
956 | 3.95k | DORIS_CHECK(table_type != nullptr); |
957 | 3.95k | DORIS_CHECK(rewrite_context != nullptr); |
958 | 3.95k | auto cast_expr = Cast::create_shared(table_type); |
959 | 3.95k | cast_expr->add_child(file_expr); |
960 | 3.95k | rewrite_context->add_created_expr(cast_expr); |
961 | 3.95k | return cast_expr; |
962 | 3.95k | } |
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 | 78.1k | RewriteContext* rewrite_context, bool* can_localize) { |
977 | 78.1k | DORIS_CHECK(can_localize != nullptr); |
978 | 78.1k | if (!is_binary_comparison_predicate(expr)) { |
979 | 69.8k | return false; |
980 | 69.8k | } |
981 | 8.29k | auto children = expr->children(); |
982 | 8.29k | int struct_child_idx = -1; |
983 | 8.29k | int literal_child_idx = -1; |
984 | 8.29k | if (is_struct_element_expr(children[0])) { |
985 | 2.08k | struct_child_idx = 0; |
986 | 2.08k | literal_child_idx = 1; |
987 | 6.21k | } else if (is_struct_element_expr(children[1])) { |
988 | 2 | struct_child_idx = 1; |
989 | 2 | literal_child_idx = 0; |
990 | 6.21k | } else { |
991 | 6.21k | return false; |
992 | 6.21k | } |
993 | | |
994 | 2.08k | const auto table_leaf_type = children[struct_child_idx]->data_type(); |
995 | 2.08k | DORIS_CHECK(table_leaf_type != nullptr); |
996 | 2.08k | auto table_literal = unwrap_literal_for_file_cast(children[literal_child_idx], table_leaf_type); |
997 | 2.08k | if (table_literal == nullptr || |
998 | 2.08k | !rewrite_struct_element_path_to_file_expr(children[struct_child_idx], filter_mappings, |
999 | 2.08k | global_to_file_slot, rewrite_context)) { |
1000 | 1.37k | return false; |
1001 | 1.37k | } |
1002 | | |
1003 | 703 | const auto file_leaf_type = children[struct_child_idx]->data_type(); |
1004 | 703 | DORIS_CHECK(file_leaf_type != nullptr); |
1005 | 703 | const FileSlotRewriteInfo leaf_rewrite_info { |
1006 | 703 | .block_position = 0, |
1007 | 703 | .file_type = file_leaf_type, |
1008 | 703 | .table_type = table_leaf_type, |
1009 | 703 | .file_column_name = {}, |
1010 | 703 | }; |
1011 | 703 | auto file_literal = |
1012 | 703 | rewrite_literal_to_file_type(table_literal, leaf_rewrite_info, rewrite_context); |
1013 | 703 | if (file_literal != nullptr) { |
1014 | 669 | children[literal_child_idx] = std::move(file_literal); |
1015 | 669 | } 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 | 702 | expr->set_children(std::move(children)); |
1028 | 702 | return true; |
1029 | 703 | } |
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 | 77.4k | RewriteContext* rewrite_context, bool* can_localize) { |
1039 | 77.4k | DORIS_CHECK(can_localize != nullptr); |
1040 | 77.4k | if (expr->node_type() != TExprNodeType::IN_PRED || expr->get_num_children() < 2 || |
1041 | 77.4k | !is_struct_element_expr(expr->children()[0])) { |
1042 | 77.4k | return false; |
1043 | 77.4k | } |
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 | 28.6k | bool* can_localize) { |
1103 | 28.6k | if (is_struct_element_expr(expr)) { |
1104 | 2.43k | const auto table_leaf_type = expr->data_type(); |
1105 | 2.43k | if (!rewrite_struct_element_path_to_file_expr(expr, filter_mappings, global_to_file_slot, |
1106 | 2.43k | 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.98k | *can_localize = false; |
1114 | 1.98k | return expr; |
1115 | 1.98k | } |
1116 | 447 | DORIS_CHECK(table_leaf_type != nullptr); |
1117 | 447 | DORIS_CHECK(expr->data_type() != nullptr); |
1118 | 447 | 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 | 447 | return expr; |
1131 | 447 | } |
1132 | | |
1133 | 26.2k | DORIS_CHECK(expr->is_slot_ref()); |
1134 | 26.2k | const auto* slot_ref = assert_cast<const VSlotRef*>(expr.get()); |
1135 | 26.2k | const auto rewrite_it = global_to_file_slot.find(slot_ref_global_index(*slot_ref)); |
1136 | 26.2k | if (rewrite_it == global_to_file_slot.end()) { |
1137 | 0 | return expr; |
1138 | 0 | } |
1139 | 26.2k | const auto& rewrite_info = rewrite_it->second; |
1140 | 26.2k | auto file_slot = create_file_slot_ref(*slot_ref, rewrite_info, rewrite_context); |
1141 | 26.2k | if (rewrite_info.file_type->equals(*rewrite_info.table_type)) { |
1142 | 21.7k | return file_slot; |
1143 | 21.7k | } |
1144 | 4.48k | 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 | 590 | *can_localize = false; |
1150 | 590 | return expr; |
1151 | 590 | } |
1152 | 3.89k | return cast_file_expr_to_table_type(file_slot, rewrite_info.table_type, rewrite_context); |
1153 | 4.48k | } |
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 | 128k | bool* can_localize) { |
1160 | 128k | if (expr == nullptr) { |
1161 | 0 | return nullptr; |
1162 | 0 | } |
1163 | 128k | DORIS_CHECK(rewrite_context != nullptr); |
1164 | 128k | DORIS_CHECK(can_localize != nullptr); |
1165 | 128k | if (auto* runtime_filter = dynamic_cast<RuntimeFilterExpr*>(expr.get()); |
1166 | 128k | runtime_filter != nullptr) { |
1167 | 4.34k | auto impl = runtime_filter->get_impl(); |
1168 | 4.34k | if (impl == nullptr) { |
1169 | 0 | *can_localize = false; |
1170 | 0 | return expr; |
1171 | 0 | } |
1172 | 4.34k | auto localized_impl = rewrite_table_expr_to_file_expr( |
1173 | 4.34k | impl, global_to_file_slot, filter_mappings, rewrite_context, can_localize); |
1174 | 4.34k | if (!*can_localize) { |
1175 | 0 | return expr; |
1176 | 0 | } |
1177 | 4.34k | runtime_filter->set_impl(std::move(localized_impl)); |
1178 | 4.34k | return expr; |
1179 | 4.34k | } |
1180 | 124k | if (rewrite_binary_slot_literal_predicate(expr, global_to_file_slot, rewrite_context)) { |
1181 | 44.3k | return expr; |
1182 | 44.3k | } |
1183 | 80.3k | if (rewrite_in_slot_literal_predicate(expr, global_to_file_slot, rewrite_context)) { |
1184 | 2.01k | return expr; |
1185 | 2.01k | } |
1186 | 78.2k | if (rewrite_binary_struct_literal_predicate(expr, filter_mappings, global_to_file_slot, |
1187 | 78.2k | rewrite_context, can_localize)) { |
1188 | 703 | return expr; |
1189 | 703 | } |
1190 | 77.5k | if (rewrite_in_struct_literal_predicate(expr, filter_mappings, global_to_file_slot, |
1191 | 77.5k | rewrite_context, can_localize)) { |
1192 | 4 | return expr; |
1193 | 4 | } |
1194 | 77.5k | if (is_struct_element_expr(expr) || expr->is_slot_ref()) { |
1195 | 28.6k | return rewrite_struct_or_slot_expr_to_file_expr(expr, global_to_file_slot, filter_mappings, |
1196 | 28.6k | rewrite_context, can_localize); |
1197 | 28.6k | } |
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 | 48.9k | if (is_cast_expr(expr) && expr->get_num_children() == 1) { |
1202 | 1.66k | const auto& child = expr->children()[0]; |
1203 | 1.66k | if (child->is_slot_ref()) { |
1204 | 1.60k | const auto* slot_ref = assert_cast<const VSlotRef*>(child.get()); |
1205 | 1.60k | const auto rewrite_it = global_to_file_slot.find(slot_ref_global_index(*slot_ref)); |
1206 | 1.60k | if (rewrite_it != global_to_file_slot.end() && |
1207 | 1.60k | 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.60k | } |
1222 | 1.66k | } |
1223 | | |
1224 | 48.9k | VExprSPtrs rewritten_children; |
1225 | 48.9k | rewritten_children.reserve(expr->children().size()); |
1226 | 49.8k | for (const auto& child : expr->children()) { |
1227 | 49.8k | rewritten_children.push_back(rewrite_table_expr_to_file_expr( |
1228 | 49.8k | child, global_to_file_slot, filter_mappings, rewrite_context, can_localize)); |
1229 | 49.8k | } |
1230 | 48.9k | expr->set_children(std::move(rewritten_children)); |
1231 | 48.9k | return expr; |
1232 | 48.9k | } |
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 | 106k | const ColumnDefinition& column) { |
1251 | 106k | if (!column.has_identifier_field_id()) { |
1252 | 541 | return TableVirtualColumnType::INVALID; |
1253 | 541 | } |
1254 | 105k | 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 | 102k | default: |
1260 | 102k | return TableVirtualColumnType::INVALID; |
1261 | 105k | } |
1262 | 105k | } |
1263 | | |
1264 | | static TableVirtualColumnType row_lineage_virtual_column_type(const ColumnDefinition& column, |
1265 | 106k | TableColumnMappingMode mode) { |
1266 | 106k | switch (mode) { |
1267 | 106k | case TableColumnMappingMode::BY_FIELD_ID: |
1268 | 106k | 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 | 106k | } |
1273 | 0 | return TableVirtualColumnType::INVALID; |
1274 | 106k | } |
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 | 537k | static bool needs_projected_file_type_rebuild(const ColumnMapping& mapping) { |
1280 | 537k | if (!is_complex_type(mapping.file_type->get_primitive_type())) { |
1281 | 303k | return false; |
1282 | 303k | } |
1283 | 233k | if (mapping.child_mappings.empty()) { |
1284 | 0 | return false; |
1285 | 0 | } |
1286 | 233k | DORIS_CHECK(mapping.file_type != nullptr); |
1287 | 233k | DORIS_CHECK(mapping.table_type != nullptr); |
1288 | 233k | if (remove_nullable(mapping.file_type)->get_primitive_type() != |
1289 | 233k | remove_nullable(mapping.table_type)->get_primitive_type()) { |
1290 | 0 | return true; |
1291 | 0 | } |
1292 | 233k | if (!mapping.table_type->equals(*mapping.file_type)) { |
1293 | 18.5k | return true; |
1294 | 18.5k | } |
1295 | 334k | 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 | 334k | if (!child_mapping.file_local_id.has_value() || |
1300 | 334k | needs_projected_file_type_rebuild(child_mapping)) { |
1301 | 688 | return true; |
1302 | 688 | } |
1303 | 334k | } |
1304 | 214k | return false; |
1305 | 215k | } |
1306 | | |
1307 | | static std::optional<size_t> file_child_ordinal_in_scan_type(const ColumnMapping& mapping, |
1308 | 300k | const ColumnMapping& child_mapping) { |
1309 | 300k | if (!child_mapping.file_local_id.has_value()) { |
1310 | 279 | return std::nullopt; |
1311 | 279 | } |
1312 | 300k | const auto& file_children = !mapping.projected_file_children.empty() |
1313 | 300k | ? mapping.projected_file_children |
1314 | 18.4E | : mapping.original_file_children; |
1315 | 421k | const auto child_it = std::ranges::find_if(file_children, [&](const ColumnDefinition& child) { |
1316 | 421k | return child.file_local_id() == *child_mapping.file_local_id; |
1317 | 421k | }); |
1318 | 300k | if (child_it == file_children.end()) { |
1319 | 0 | return std::nullopt; |
1320 | 0 | } |
1321 | 300k | return static_cast<size_t>(std::distance(file_children.begin(), child_it)); |
1322 | 300k | } |
1323 | | |
1324 | 1.26M | static bool needs_complex_rematerialize(const ColumnMapping& mapping) { |
1325 | 1.26M | if (mapping.child_mappings.empty()) { |
1326 | 1.05M | return false; |
1327 | 1.05M | } |
1328 | 208k | if (mapping.table_type == nullptr || mapping.file_type == nullptr || |
1329 | 208k | !mapping.table_type->equals(*mapping.file_type)) { |
1330 | 11.6k | return true; |
1331 | 11.6k | } |
1332 | 494k | for (size_t table_child_idx = 0; table_child_idx < mapping.child_mappings.size(); |
1333 | 301k | ++table_child_idx) { |
1334 | 301k | const auto& child_mapping = mapping.child_mappings[table_child_idx]; |
1335 | 301k | const auto file_child_idx = file_child_ordinal_in_scan_type(mapping, child_mapping); |
1336 | 301k | if (!file_child_idx.has_value() || *file_child_idx != table_child_idx || |
1337 | 301k | needs_complex_rematerialize(child_mapping) || |
1338 | 301k | (child_mapping.table_type != nullptr && child_mapping.file_type != nullptr && |
1339 | 298k | !child_mapping.table_type->equals(*child_mapping.file_type))) { |
1340 | 2.24k | return true; |
1341 | 2.24k | } |
1342 | 301k | } |
1343 | 193k | return false; |
1344 | 195k | } |
1345 | | |
1346 | 1.00M | static bool mapping_can_use_file_column_directly(const ColumnMapping& mapping) { |
1347 | 1.00M | if (mapping.table_type == nullptr || mapping.file_type == nullptr) { |
1348 | 0 | return false; |
1349 | 0 | } |
1350 | 1.00M | const auto table_type = remove_nullable(mapping.table_type); |
1351 | 1.00M | const auto file_type = remove_nullable(mapping.file_type); |
1352 | 1.00M | const bool same_timestamptz_with_different_scale = |
1353 | 1.00M | table_type->get_primitive_type() == TYPE_TIMESTAMPTZ && |
1354 | 1.00M | file_type->get_primitive_type() == TYPE_TIMESTAMPTZ; |
1355 | 1.00M | if (!mapping.table_type->equals(*mapping.file_type) && !same_timestamptz_with_different_scale) { |
1356 | 67.9k | return false; |
1357 | 67.9k | } |
1358 | 940k | return !needs_complex_rematerialize(mapping); |
1359 | 1.00M | } |
1360 | | |
1361 | 1.25M | static bool type_contains_varbinary(const DataTypePtr& type) { |
1362 | 1.25M | DORIS_CHECK(type != nullptr); |
1363 | 1.25M | const auto nested_type = remove_nullable(type); |
1364 | 1.25M | switch (nested_type->get_primitive_type()) { |
1365 | 655 | case TYPE_VARBINARY: |
1366 | 655 | return true; |
1367 | 103k | case TYPE_ARRAY: |
1368 | 103k | return type_contains_varbinary( |
1369 | 103k | assert_cast<const DataTypeArray&>(*nested_type).get_nested_type()); |
1370 | 80.7k | case TYPE_MAP: { |
1371 | 80.7k | const auto& map_type = assert_cast<const DataTypeMap&>(*nested_type); |
1372 | 80.7k | return type_contains_varbinary(map_type.get_key_type()) || |
1373 | 80.7k | type_contains_varbinary(map_type.get_value_type()); |
1374 | 0 | } |
1375 | 80.4k | case TYPE_STRUCT: |
1376 | 80.4k | return std::ranges::any_of( |
1377 | 80.4k | assert_cast<const DataTypeStruct&>(*nested_type).get_elements(), |
1378 | 156k | [](const DataTypePtr& child_type) { return type_contains_varbinary(child_type); }); |
1379 | 991k | default: |
1380 | 991k | return false; |
1381 | 1.25M | } |
1382 | 1.25M | } |
1383 | | |
1384 | 832k | static FilterConversionType direct_filter_conversion(const ColumnMapping& mapping) { |
1385 | 832k | DORIS_CHECK(mapping.table_type != nullptr); |
1386 | 832k | 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 | 832k | if (type_contains_varbinary(mapping.table_type)) { |
1393 | 657 | return FilterConversionType::FINALIZE_ONLY; |
1394 | 657 | } |
1395 | 832k | const auto table_type = remove_nullable(mapping.table_type); |
1396 | 832k | 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 | 832k | if (table_type->get_primitive_type() == TYPE_TIMESTAMPTZ && |
1401 | 832k | file_type->get_primitive_type() == TYPE_TIMESTAMPTZ && |
1402 | 832k | !mapping.table_type->equals(*mapping.file_type)) { |
1403 | 1 | return FilterConversionType::FINALIZE_ONLY; |
1404 | 1 | } |
1405 | 832k | return mapping.is_trivial ? FilterConversionType::COPY_DIRECTLY |
1406 | 832k | : FilterConversionType::CAST_FILTER; |
1407 | 832k | } |
1408 | | |
1409 | 18.8k | static FilterConversionType projected_filter_conversion(const ColumnMapping& mapping) { |
1410 | 18.8k | const auto conversion = direct_filter_conversion(mapping); |
1411 | 18.8k | return !mapping.is_trivial && conversion != FilterConversionType::FINALIZE_ONLY |
1412 | 18.8k | ? FilterConversionType::READER_EXPRESSION |
1413 | 18.8k | : conversion; |
1414 | 18.8k | } |
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 | 321k | bool allow_ordinal_fallback) { |
1421 | 321k | const auto file_parent_type = remove_nullable(file_parent.type)->get_primitive_type(); |
1422 | 321k | switch (file_parent_type) { |
1423 | 86.8k | case TYPE_ARRAY: |
1424 | 86.8k | DORIS_CHECK(file_parent.children.size() == 1); |
1425 | 86.8k | return &file_parent.children[0]; |
1426 | 146k | case TYPE_MAP: |
1427 | 146k | DORIS_CHECK(file_parent.children.size() == 2); |
1428 | 146k | if (table_child.name == "key") { |
1429 | 73.0k | return &file_parent.children[0]; |
1430 | 73.0k | } |
1431 | 73.1k | if (table_child.name == "value") { |
1432 | 73.0k | return &file_parent.children[1]; |
1433 | 73.0k | } |
1434 | 104 | if (table_child.local_id == 0 || table_child.local_id == 1) { |
1435 | 0 | return &file_parent.children[table_child.local_id]; |
1436 | 0 | } |
1437 | 104 | return nullptr; |
1438 | 88.9k | 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 | 88.9k | const auto nested_mode = |
1443 | 88.9k | mode == TableColumnMappingMode::BY_INDEX ? TableColumnMappingMode::BY_NAME : mode; |
1444 | 88.9k | if (const auto* file_child = |
1445 | 88.9k | matcher_for_mode(nested_mode).find(table_child, file_parent.children); |
1446 | 88.9k | file_child != nullptr) { |
1447 | 83.4k | return file_child; |
1448 | 83.4k | } |
1449 | 5.43k | if (allow_ordinal_fallback && mode == TableColumnMappingMode::BY_FIELD_ID && |
1450 | 5.43k | !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 | 5.42k | if (allow_ordinal_fallback && table_child_idx < file_parent.children.size()) { |
1469 | 3 | return &file_parent.children[table_child_idx]; |
1470 | 3 | } |
1471 | 5.42k | return nullptr; |
1472 | 321k | } |
1473 | 321k | } |
1474 | | |
1475 | | static ColumnDefinition synthetic_child_definition(const std::string& name, DataTypePtr type, |
1476 | 88.7k | int32_t local_id) { |
1477 | 88.7k | ColumnDefinition child; |
1478 | 88.7k | child.identifier = Field::create_field<TYPE_STRING>(name); |
1479 | 88.7k | child.local_id = local_id; |
1480 | 88.7k | child.name = name; |
1481 | 88.7k | child.type = std::move(type); |
1482 | 88.7k | return child; |
1483 | 88.7k | } |
1484 | | |
1485 | | static std::vector<ColumnDefinition> synthesize_complex_children_from_type( |
1486 | 45.1k | const DataTypePtr& type) { |
1487 | 45.1k | std::vector<ColumnDefinition> children; |
1488 | 45.1k | if (type == nullptr) { |
1489 | 0 | return children; |
1490 | 0 | } |
1491 | 45.1k | const auto nested_type = remove_nullable(type); |
1492 | 45.1k | 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 | 45.1k | case TYPE_STRUCT: { |
1505 | 45.1k | const auto* struct_type = assert_cast<const DataTypeStruct*>(nested_type.get()); |
1506 | 45.1k | children.reserve(struct_type->get_elements().size()); |
1507 | 134k | for (size_t idx = 0; idx < struct_type->get_elements().size(); ++idx) { |
1508 | 88.9k | children.push_back(synthetic_child_definition(struct_type->get_element_name(idx), |
1509 | 88.9k | struct_type->get_element(idx), |
1510 | 88.9k | cast_set<int32_t>(idx))); |
1511 | 88.9k | } |
1512 | 45.1k | break; |
1513 | 0 | } |
1514 | 0 | default: |
1515 | 0 | break; |
1516 | 45.1k | } |
1517 | 45.1k | return children; |
1518 | 45.1k | } |
1519 | | |
1520 | | static void align_struct_child_types_with_parent(const DataTypePtr& parent_type, |
1521 | 45.1k | std::vector<ColumnDefinition>& children) { |
1522 | 45.1k | const auto nested_parent_type = remove_nullable(parent_type); |
1523 | 45.1k | DORIS_CHECK(nested_parent_type->get_primitive_type() == TYPE_STRUCT); |
1524 | 45.1k | const auto type_children = synthesize_complex_children_from_type(parent_type); |
1525 | 88.9k | for (auto& child : children) { |
1526 | 88.9k | const auto type_child = std::ranges::find_if( |
1527 | 152k | 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 | 88.9k | child.type = type_child->type; |
1537 | 88.9k | } |
1538 | 45.1k | } |
1539 | | |
1540 | | static bool has_table_child_named(const std::vector<ColumnDefinition>& children, |
1541 | 10.3k | std::string_view name) { |
1542 | 15.4k | return std::ranges::any_of(children, [&](const ColumnDefinition& child) { |
1543 | 15.4k | return std::string_view(child.name) == name; |
1544 | 15.4k | }); |
1545 | 10.3k | } |
1546 | | |
1547 | | static void complete_required_complex_children_from_type(const DataTypePtr& type, |
1548 | 18.2k | std::vector<ColumnDefinition>& children) { |
1549 | 18.2k | if (type == nullptr) { |
1550 | 0 | return; |
1551 | 0 | } |
1552 | 18.2k | const auto nested_type = remove_nullable(type); |
1553 | 18.2k | switch (nested_type->get_primitive_type()) { |
1554 | 5.17k | case TYPE_MAP: { |
1555 | 5.17k | 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 | 5.17k | 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 | 5.17k | break; |
1567 | 0 | } |
1568 | 5.18k | 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.18k | break; |
1572 | 7.92k | 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.92k | break; |
1576 | 0 | default: |
1577 | 0 | break; |
1578 | 18.2k | } |
1579 | 18.2k | } |
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 | 817k | const ColumnDefinition& table_column, const DataTypePtr& file_type) { |
1588 | 817k | PreparedTableChildren prepared {.children = table_column.children}; |
1589 | 817k | 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 | 817k | prepared.synthesized_from_type = prepared.children.empty() && |
1595 | 817k | is_complex_type(nested_table_type->get_primitive_type()) && |
1596 | 817k | !table_column.type->equals(*file_type); |
1597 | 817k | if (prepared.synthesized_from_type) { |
1598 | 5 | prepared.children = synthesize_complex_children_from_type(table_column.type); |
1599 | 817k | } else if (!prepared.children.empty() && !table_column.type->equals(*file_type)) { |
1600 | 18.2k | complete_required_complex_children_from_type(table_column.type, prepared.children); |
1601 | 18.2k | } |
1602 | | |
1603 | 817k | 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 | 45.1k | align_struct_child_types_with_parent(table_column.type, prepared.children); |
1608 | 45.1k | } |
1609 | 817k | return prepared; |
1610 | 817k | } |
1611 | | |
1612 | 205k | static Status validate_file_schema_children(const ColumnDefinition& file_field) { |
1613 | 205k | if (file_field.type == nullptr) { |
1614 | 0 | return Status::InternalError("File column '{}' has null type", file_field.name); |
1615 | 0 | } |
1616 | 205k | const auto nested_type = remove_nullable(file_field.type); |
1617 | 205k | size_t expected_children = 0; |
1618 | 205k | bool complex_with_fixed_children = true; |
1619 | 205k | switch (nested_type->get_primitive_type()) { |
1620 | 86.8k | case TYPE_ARRAY: |
1621 | 86.8k | expected_children = 1; |
1622 | 86.8k | break; |
1623 | 73.0k | case TYPE_MAP: |
1624 | 73.0k | expected_children = 2; |
1625 | 73.0k | break; |
1626 | 45.1k | case TYPE_STRUCT: |
1627 | 45.1k | expected_children = |
1628 | 45.1k | assert_cast<const DataTypeStruct*>(nested_type.get())->get_elements().size(); |
1629 | 45.1k | break; |
1630 | 0 | default: |
1631 | 0 | complex_with_fixed_children = false; |
1632 | 0 | break; |
1633 | 205k | } |
1634 | 204k | if (!complex_with_fixed_children || file_field.children.size() == expected_children) { |
1635 | 204k | return Status::OK(); |
1636 | 204k | } |
1637 | 13 | return Status::InternalError( |
1638 | 13 | "Malformed complex file schema for column '{}': type={}, expected_children={}, " |
1639 | 13 | "actual_children={}", |
1640 | 13 | file_field.name, file_field.type->get_name(), expected_children, |
1641 | 13 | file_field.children.size()); |
1642 | 204k | } |
1643 | | |
1644 | 445k | static bool has_projected_file_children(const ColumnMapping& mapping) { |
1645 | 445k | if (mapping.original_file_children.empty() || mapping.projected_file_children.empty()) { |
1646 | 366k | return false; |
1647 | 366k | } |
1648 | 79.0k | if (mapping.original_file_children.size() != mapping.projected_file_children.size()) { |
1649 | 5.35k | return true; |
1650 | 5.35k | } |
1651 | 197k | for (size_t idx = 0; idx < mapping.original_file_children.size(); ++idx) { |
1652 | 123k | if (mapping.original_file_children[idx].file_local_id() != |
1653 | 123k | mapping.projected_file_children[idx].file_local_id()) { |
1654 | 0 | return true; |
1655 | 0 | } |
1656 | 123k | } |
1657 | 73.6k | return false; |
1658 | 73.6k | } |
1659 | | |
1660 | 445k | static bool needs_nested_file_projection(const ColumnMapping& mapping) { |
1661 | 445k | if (has_projected_file_children(mapping)) { |
1662 | | // Return True if the projected child column is missing / re-ordered |
1663 | 5.35k | return true; |
1664 | 5.35k | } |
1665 | 439k | return std::ranges::any_of(mapping.child_mappings, [](const ColumnMapping& child_mapping) { |
1666 | 125k | return needs_nested_file_projection(child_mapping); |
1667 | 125k | }); |
1668 | 445k | } |
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 | 18.9k | std::vector<ColumnDefinition>* projected_file_children, DataTypePtr* projected_type) { |
1679 | 18.9k | DORIS_CHECK(file_type != nullptr); |
1680 | 18.9k | DORIS_CHECK(projected_file_children != nullptr); |
1681 | 18.9k | DORIS_CHECK(projected_type != nullptr); |
1682 | 18.9k | ColumnDefinition field; |
1683 | 18.9k | field.type = file_type; |
1684 | 18.9k | field.children = original_file_children; |
1685 | 18.9k | LocalColumnIndex projection = LocalColumnIndex::partial_local(-1); |
1686 | 18.9k | projection.children.reserve(child_mappings.size()); |
1687 | 27.5k | for (const auto* child_mapping : present_child_mappings_in_file_order(child_mappings)) { |
1688 | 27.5k | DORIS_CHECK(child_mapping->file_local_id.has_value()); |
1689 | 27.5k | LocalColumnIndex child_projection; |
1690 | 27.5k | RETURN_IF_ERROR(build_complex_projection(*child_mapping, &child_projection)); |
1691 | 27.5k | projection.children.push_back(std::move(child_projection)); |
1692 | 27.5k | } |
1693 | | |
1694 | 18.9k | ColumnDefinition projected_field; |
1695 | 18.9k | RETURN_IF_ERROR(project_column_definition(field, projection, &projected_field)); |
1696 | 18.9k | *projected_file_children = std::move(projected_field.children); |
1697 | 18.9k | *projected_type = std::move(projected_field.type); |
1698 | 18.9k | return Status::OK(); |
1699 | 18.9k | } |
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 | 50.4k | static Status build_complex_projection(const ColumnMapping& mapping, LocalColumnIndex* projection) { |
1709 | 50.4k | if (projection == nullptr) { |
1710 | 0 | return Status::InvalidArgument("projection is null"); |
1711 | 0 | } |
1712 | 50.4k | DORIS_CHECK(mapping.file_local_id.has_value()); |
1713 | 50.4k | *projection = LocalColumnIndex::local(*mapping.file_local_id); |
1714 | 50.4k | projection->project_all_children = mapping.child_mappings.empty(); |
1715 | 50.4k | projection->children.clear(); |
1716 | 50.4k | const auto present_children = present_child_mappings_in_file_order(mapping.child_mappings); |
1717 | 50.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 | 50.1k | for (const auto* child_mapping : present_children) { |
1725 | 17.6k | LocalColumnIndex child_projection; |
1726 | 17.6k | RETURN_IF_ERROR(build_complex_projection(*child_mapping, &child_projection)); |
1727 | 17.6k | projection->children.push_back(std::move(child_projection)); |
1728 | 17.6k | } |
1729 | 50.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 | 50.1k | return Status::OK(); |
1734 | 50.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 | 500k | ColumnMapping* mapping) { |
1743 | 500k | DORIS_CHECK(mapping != nullptr); |
1744 | 500k | if (mapping->original_file_type == nullptr) { |
1745 | 0 | mapping->original_file_type = mapping->file_type; |
1746 | 0 | } |
1747 | 500k | if (mapping->original_file_type == nullptr || |
1748 | 500k | !is_complex_type(remove_nullable(mapping->original_file_type)->get_primitive_type())) { |
1749 | 325k | return Status::OK(); |
1750 | 325k | } |
1751 | 174k | ColumnDefinition field; |
1752 | 174k | field.type = mapping->original_file_type; |
1753 | 174k | field.children = mapping->original_file_children; |
1754 | 174k | ColumnDefinition projected_field; |
1755 | 174k | RETURN_IF_ERROR(project_column_definition(field, projection, &projected_field)); |
1756 | 174k | mapping->file_type = std::move(projected_field.type); |
1757 | 174k | mapping->projected_file_children = std::move(projected_field.children); |
1758 | 174k | mapping->is_trivial = mapping_can_use_file_column_directly(*mapping); |
1759 | 174k | return Status::OK(); |
1760 | 174k | } |
1761 | | |
1762 | | static Status merge_filter_projection(const FilterProjectionMap* filter_projections, |
1763 | 75.3k | LocalColumnIndex* projection) { |
1764 | 75.3k | DORIS_CHECK(projection != nullptr); |
1765 | 75.3k | if (filter_projections == nullptr) { |
1766 | 0 | return Status::OK(); |
1767 | 0 | } |
1768 | 75.3k | const auto filter_projection_it = filter_projections->find(projection->column_id()); |
1769 | 75.3k | if (filter_projection_it == filter_projections->end()) { |
1770 | 74.1k | return Status::OK(); |
1771 | 74.1k | } |
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 | 1.14k | RETURN_IF_ERROR(merge_local_column_index(projection, filter_projection_it->second)); |
1777 | 1.14k | return Status::OK(); |
1778 | 1.14k | } |
1779 | | |
1780 | 156 | static bool table_root_is_map(const ColumnMapping& mapping) { |
1781 | 156 | if (mapping.table_type == nullptr) { |
1782 | 0 | return false; |
1783 | 0 | } |
1784 | 156 | return remove_nullable(mapping.table_type)->get_primitive_type() == TYPE_MAP; |
1785 | 156 | } |
1786 | | |
1787 | | static Status add_scan_column(FileScanRequest* file_request, ColumnMapping* mapping, |
1788 | | bool is_predicate_column, bool force_full_complex_scan_projection, |
1789 | 509k | const FilterProjectionMap* filter_projections = nullptr) { |
1790 | 509k | const auto file_column_id = LocalColumnId(mapping->file_local_id.value()); |
1791 | 509k | 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 | 509k | if (!force_full_complex_scan_projection && needs_nested_file_projection(*mapping)) { |
1796 | 5.35k | RETURN_IF_ERROR(build_complex_projection(*mapping, &projection)); |
1797 | 5.35k | } |
1798 | 509k | if (is_predicate_column && !force_full_complex_scan_projection) { |
1799 | 75.3k | 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 | 75.3k | RETURN_IF_ERROR(merge_filter_projection(filter_projections, &projection)); |
1805 | 75.3k | } |
1806 | 509k | FileScanRequestBuilder builder(file_request); |
1807 | 509k | if (is_predicate_column) { |
1808 | 75.3k | return builder.add_predicate_column(std::move(projection)); |
1809 | 75.3k | } |
1810 | 434k | return builder.add_non_predicate_column(std::move(projection)); |
1811 | 509k | } |
1812 | | |
1813 | | static const LocalColumnIndex* find_scan_projection( |
1814 | 932k | const std::vector<LocalColumnIndex>& scan_columns, LocalColumnId file_column_id) { |
1815 | 932k | const auto projection_it = |
1816 | 8.80M | std::ranges::find_if(scan_columns, [&](const LocalColumnIndex& projection) { |
1817 | 8.80M | return projection.column_id() == file_column_id; |
1818 | 8.80M | }); |
1819 | 932k | return projection_it == scan_columns.end() ? nullptr : &*projection_it; |
1820 | 932k | } |
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 | 500k | ColumnMapping* mapping) { |
1832 | 500k | DORIS_CHECK(mapping != nullptr); |
1833 | 500k | DORIS_CHECK(mapping->file_local_id.has_value()); |
1834 | 500k | 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 | 500k | const auto* projection = find_scan_projection(file_request.predicate_columns, file_column_id); |
1838 | 500k | if (projection == nullptr) { |
1839 | 434k | projection = find_scan_projection(file_request.non_predicate_columns, file_column_id); |
1840 | 434k | } |
1841 | 500k | DORIS_CHECK(projection != nullptr); |
1842 | 500k | return apply_projection_to_mapping_file_type(*projection, mapping); |
1843 | 500k | } |
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 | 122k | FilterProjectionMap* filter_projections) { |
1861 | 122k | DORIS_CHECK(filter_projections != nullptr); |
1862 | 122k | filter_projections->clear(); |
1863 | 122k | for (const auto& table_filter : table_filters) { |
1864 | 82.7k | 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 | 82.7k | std::vector<NestedStructPath> paths; |
1871 | 82.7k | collect_nested_struct_paths(table_filter.conjunct->root(), &paths); |
1872 | 82.7k | for (const auto& path : paths) { |
1873 | 5.22k | auto mapping_it = std::ranges::find_if(mappings, [&](const ColumnMapping& mapping) { |
1874 | 5.22k | return mapping.global_index == path.root_global_index; |
1875 | 5.22k | }); |
1876 | 1.75k | if (mapping_it == mappings.end() || !mapping_it->file_local_id.has_value() || |
1877 | 1.75k | path.selectors.empty()) { |
1878 | 440 | continue; |
1879 | 440 | } |
1880 | | |
1881 | 1.31k | ResolvedNestedStructPath resolved; |
1882 | 1.31k | LocalColumnIndex root_projection; |
1883 | 1.31k | if (!resolve_nested_struct_path_for_file(path, mappings, &resolved)) { |
1884 | 156 | if (!table_root_is_map(*mapping_it)) { |
1885 | 155 | continue; |
1886 | 155 | } |
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 | 1.15k | } else { |
1900 | 1.15k | root_projection = std::move(resolved.file_projection); |
1901 | 1.15k | } |
1902 | 1.16k | auto filter_projection_it = filter_projections->find(root_projection.column_id()); |
1903 | 1.16k | if (filter_projection_it == filter_projections->end()) { |
1904 | 1.09k | filter_projections->emplace(root_projection.column_id(), |
1905 | 1.09k | std::move(root_projection)); |
1906 | 1.09k | continue; |
1907 | 1.09k | } |
1908 | 70 | RETURN_IF_ERROR( |
1909 | 70 | merge_local_column_index(&filter_projection_it->second, root_projection)); |
1910 | 70 | } |
1911 | 82.7k | } |
1912 | 122k | return Status::OK(); |
1913 | 122k | } |
1914 | | |
1915 | 499k | static void rebuild_projection(ColumnMapping* mapping, LocalIndex block_position) { |
1916 | 499k | DORIS_CHECK(mapping->file_local_id.has_value()); |
1917 | 499k | if (mapping->is_trivial || needs_complex_rematerialize(*mapping)) { |
1918 | 486k | mapping->projection = VExprContext::create_shared(VSlotRef::create_shared( |
1919 | 486k | cast_set<int>(block_position.value()), cast_set<int>(block_position.value()), -1, |
1920 | 486k | mapping->file_type, mapping->file_column_name)); |
1921 | 486k | return; |
1922 | 486k | } |
1923 | | |
1924 | 13.0k | auto expr = Cast::create_shared(mapping->table_type); |
1925 | 13.0k | expr->add_child(VSlotRef::create_shared(cast_set<int>(block_position.value()), |
1926 | 13.0k | cast_set<int>(block_position.value()), -1, |
1927 | 13.0k | mapping->file_type, mapping->file_column_name)); |
1928 | 13.0k | mapping->projection = VExprContext::create_shared(expr); |
1929 | 13.0k | } |
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 | 122k | const std::map<GlobalIndex, FilterEntry>& filter_entries) { |
1936 | 122k | std::map<GlobalIndex, FileSlotRewriteInfo> global_to_file_slot; |
1937 | 525k | for (const auto& mapping : mappings) { |
1938 | 525k | const auto entry_it = filter_entries.find(mapping.global_index); |
1939 | 526k | if (entry_it == filter_entries.end() || !entry_it->second.is_local()) { |
1940 | 27.3k | continue; |
1941 | 27.3k | } |
1942 | 498k | DORIS_CHECK(mapping.file_local_id.has_value()); |
1943 | 498k | global_to_file_slot.emplace( |
1944 | 498k | mapping.global_index, |
1945 | 498k | FileSlotRewriteInfo {.block_position = entry_it->second.local_index().value(), |
1946 | 498k | .file_type = mapping.file_type, |
1947 | 498k | .table_type = mapping.table_type, |
1948 | 498k | .file_column_name = mapping.file_column_name}); |
1949 | 498k | } |
1950 | 122k | return global_to_file_slot; |
1951 | 122k | } |
1952 | | |
1953 | | Status TableColumnMapper::_create_by_index_mapping(const ColumnDefinition& table_column, |
1954 | | const std::vector<ColumnDefinition>& file_schema, |
1955 | 36.8k | ColumnMapping* mapping) { |
1956 | 36.8k | DORIS_CHECK(mapping != nullptr); |
1957 | 36.8k | 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 | 36.8k | 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 | 36.9k | if (file_index >= 0 && static_cast<size_t>(file_index) < file_schema.size()) { |
1972 | 36.4k | return _create_direct_mapping(table_column, file_schema[static_cast<size_t>(file_index)], |
1973 | 36.4k | mapping); |
1974 | 36.4k | } |
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 | 529 | if (table_column.default_expr != nullptr) { |
1979 | 529 | _set_constant_mapping(mapping, table_column.default_expr); |
1980 | 529 | return Status::OK(); |
1981 | 529 | } |
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 | 383 | } |
1986 | | |
1987 | 22.5k | void TableColumnMapper::_set_constant_mapping(ColumnMapping* mapping, VExprContextSPtr expr) { |
1988 | 22.5k | DORIS_CHECK(mapping != nullptr); |
1989 | 22.5k | DORIS_CHECK(expr != nullptr); |
1990 | 22.5k | mapping->default_expr = std::move(expr); |
1991 | 22.5k | mapping->constant_index = _constant_map.add(ConstantEntry { |
1992 | 22.5k | .global_index = mapping->global_index, |
1993 | 22.5k | .expr = mapping->default_expr, |
1994 | 22.5k | .type = mapping->table_type, |
1995 | 22.5k | }); |
1996 | 22.5k | mapping->filter_conversion = FilterConversionType::CONSTANT; |
1997 | 22.5k | } |
1998 | | |
1999 | | Status TableColumnMapper::_create_mapping_for_column(const ColumnDefinition& table_column, |
2000 | | GlobalIndex global_index, |
2001 | 525k | ColumnMapping* mapping) { |
2002 | 525k | DORIS_CHECK(mapping != nullptr); |
2003 | 525k | *mapping = ColumnMapping {}; |
2004 | 525k | mapping->global_index = global_index; |
2005 | 525k | mapping->table_column_name = table_column.name; |
2006 | 525k | 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 | 525k | const auto row_lineage_type = |
2010 | 525k | _options.enable_row_lineage_virtual_columns |
2011 | 525k | ? row_lineage_virtual_column_type(table_column, _options.mode) |
2012 | 525k | : TableVirtualColumnType::INVALID; |
2013 | 525k | if (const auto* partition_value = find_partition_value(table_column, _partition_values); |
2014 | 525k | table_column.is_partition_key && partition_value != nullptr) { |
2015 | | // Partition values are split constants and must take precedence over defaults. |
2016 | 16.1k | _set_constant_mapping(mapping, VExprContext::create_shared(VLiteral::create_shared( |
2017 | 16.1k | mapping->table_type, *partition_value))); |
2018 | 509k | } else if (_options.mode == TableColumnMappingMode::BY_INDEX && |
2019 | 509k | !table_column.is_partition_key && table_column.has_identifier_field_id()) { |
2020 | | // BY_INDEX interprets ColumnDefinition::identifier as physical file position. |
2021 | 37.0k | RETURN_IF_ERROR(_create_by_index_mapping(table_column, _file_schema, mapping)); |
2022 | 472k | } else if (const auto* file_field = _find_file_field(table_column, _file_schema)) { |
2023 | | // Normal physical file column mapping. |
2024 | 464k | RETURN_IF_ERROR(_create_direct_mapping(table_column, *file_field, mapping)); |
2025 | 464k | 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 | 464k | } 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.12k | mapping->virtual_column_type = row_lineage_type; |
2040 | 5.36k | } 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 | 490 | mapping->virtual_column_type = TableVirtualColumnType::ICEBERG_ROWID; |
2044 | 4.87k | } 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.84k | } else if (table_column.default_expr != nullptr) { |
2051 | | // Missing schema-evolution column with an explicit default expression. |
2052 | 5.84k | _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 | 525k | return Status::OK(); |
2061 | 525k | } |
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 | 122k | const std::vector<TableFilter>& table_filters) { |
2094 | 122k | _hidden_mappings.clear(); |
2095 | | |
2096 | 122k | std::map<GlobalIndex, ColumnDefinition> filter_columns; |
2097 | 122k | for (const auto& table_filter : table_filters) { |
2098 | 82.6k | if (table_filter.conjunct != nullptr) { |
2099 | 82.6k | collect_top_level_slot_columns(table_filter.conjunct->root(), &filter_columns); |
2100 | 82.6k | } |
2101 | 82.6k | } |
2102 | | |
2103 | 122k | for (const auto& [global_index, table_column] : filter_columns) { |
2104 | 71.5k | if (_find_mapping(global_index) != nullptr) { |
2105 | | // Ignore columns that are already mapped by the projected columns |
2106 | 71.5k | continue; |
2107 | 71.5k | } |
2108 | 12 | ColumnMapping mapping; |
2109 | 12 | RETURN_IF_ERROR(_create_hidden_filter_mapping(table_column, global_index, &mapping)); |
2110 | 12 | if (mapping.file_local_id.has_value() || mapping.constant_index.has_value() || |
2111 | 12 | mapping.virtual_column_type != TableVirtualColumnType::INVALID) { |
2112 | 2 | _hidden_mappings.push_back(std::move(mapping)); |
2113 | 2 | } |
2114 | 12 | } |
2115 | 122k | return Status::OK(); |
2116 | 122k | } |
2117 | | |
2118 | | Status TableColumnMapper::create_mapping(const std::vector<ColumnDefinition>& projected_columns, |
2119 | | const std::map<std::string, Field>& partition_values, |
2120 | 122k | const std::vector<ColumnDefinition>& file_schema) { |
2121 | 122k | clear(); |
2122 | 122k | _partition_values = partition_values; |
2123 | 122k | _file_schema = file_schema; |
2124 | 649k | for (size_t column_idx = 0; column_idx < projected_columns.size(); ++column_idx) { |
2125 | 526k | ColumnMapping mapping; |
2126 | 526k | RETURN_IF_ERROR(_create_mapping_for_column(projected_columns[column_idx], |
2127 | 526k | GlobalIndex(column_idx), &mapping)); |
2128 | 526k | _mappings.push_back(std::move(mapping)); |
2129 | 526k | } |
2130 | 122k | return Status::OK(); |
2131 | 122k | } |
2132 | | |
2133 | 367k | std::vector<ColumnMapping> TableColumnMapper::_filter_visible_mappings() const { |
2134 | 367k | std::vector<ColumnMapping> mappings; |
2135 | 367k | mappings.reserve(_mappings.size() + _hidden_mappings.size()); |
2136 | 367k | mappings.insert(mappings.end(), _mappings.begin(), _mappings.end()); |
2137 | 367k | mappings.insert(mappings.end(), _hidden_mappings.begin(), _hidden_mappings.end()); |
2138 | 367k | return mappings; |
2139 | 367k | } |
2140 | | |
2141 | 122k | Status TableColumnMapper::_build_filter_entries(const FileScanRequest& file_request) { |
2142 | 122k | _filter_entries.clear(); |
2143 | 122k | const auto mappings = _filter_visible_mappings(); |
2144 | 527k | for (const auto& mapping : mappings) { |
2145 | 527k | FilterEntry entry; |
2146 | 527k | if (mapping.constant_index.has_value()) { |
2147 | 22.5k | entry = FilterEntry::constant(*mapping.constant_index); |
2148 | 504k | } else if (mapping.file_local_id.has_value() && |
2149 | 504k | filter_conversion_has_local_source(mapping.filter_conversion)) { |
2150 | 499k | const auto local_position_it = |
2151 | 499k | file_request.local_positions.find(LocalColumnId(*mapping.file_local_id)); |
2152 | 499k | if (local_position_it != file_request.local_positions.end()) { |
2153 | 499k | entry = FilterEntry::local(local_position_it->second); |
2154 | 499k | } |
2155 | 499k | } |
2156 | 527k | _filter_entries.emplace(mapping.global_index, entry); |
2157 | 527k | } |
2158 | 122k | return Status::OK(); |
2159 | 122k | } |
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 | 122k | 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 | 122k | file_request->predicate_columns.clear(); |
2168 | 122k | file_request->non_predicate_columns.clear(); |
2169 | 122k | file_request->predicate_only_columns.clear(); |
2170 | 122k | file_request->local_positions.clear(); |
2171 | 122k | file_request->conjuncts.clear(); |
2172 | 122k | file_request->delete_conjuncts.clear(); |
2173 | 122k | _filter_entries.clear(); |
2174 | | // 1. Build referenced non-predicate columns |
2175 | 648k | for (size_t column_idx = 0; column_idx < projected_columns.size(); ++column_idx) { |
2176 | 525k | const auto global_index = GlobalIndex(column_idx); |
2177 | 525k | auto* mapping = _find_mapping(global_index); |
2178 | 525k | 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 | 499k | bool used_by_filter = false; |
2182 | 499k | for (const auto& table_filter : table_filters) { |
2183 | 144k | const auto& global_indices = table_filter.global_indices; |
2184 | 144k | if (std::find(global_indices.begin(), global_indices.end(), global_index) != |
2185 | 144k | global_indices.end() && |
2186 | 144k | filter_conversion_has_local_source(mapping->filter_conversion)) { |
2187 | 64.9k | used_by_filter = true; |
2188 | 64.9k | break; |
2189 | 64.9k | } |
2190 | 144k | } |
2191 | 499k | if (!used_by_filter || !enable_lazy_materialization()) { |
2192 | 434k | RETURN_IF_ERROR(add_scan_column(file_request, mapping, false, |
2193 | 434k | force_full_complex_scan_projection())); |
2194 | 434k | } |
2195 | 499k | } |
2196 | 525k | } |
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 | 122k | RETURN_IF_ERROR(_build_hidden_filter_mappings(table_filters)); |
2200 | 122k | RETURN_IF_ERROR(localize_filters(table_filters, file_request, runtime_state)); |
2201 | 122k | 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 | 524k | for (auto& mapping : _mappings) { |
2229 | 524k | if (!mapping.file_local_id.has_value()) { |
2230 | 26.1k | continue; |
2231 | 26.1k | } |
2232 | 498k | auto position_it = |
2233 | 498k | 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 | 498k | rebuild_projection(&mapping, position_it->second); |
2238 | 498k | } |
2239 | 122k | return Status::OK(); |
2240 | 122k | } |
2241 | | |
2242 | 753k | ColumnMapping* TableColumnMapper::_find_mapping(GlobalIndex global_index) { |
2243 | 9.27M | for (auto& mapping : _mappings) { |
2244 | 9.27M | if (mapping.global_index == global_index) { |
2245 | 753k | return &mapping; |
2246 | 753k | } |
2247 | 9.27M | } |
2248 | 18.4E | return nullptr; |
2249 | 753k | } |
2250 | | |
2251 | 156k | ColumnMapping* TableColumnMapper::_find_filter_mapping(GlobalIndex global_index) { |
2252 | 156k | if (auto* mapping = _find_mapping(global_index); mapping != nullptr) { |
2253 | 156k | return mapping; |
2254 | 156k | } |
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 | 122k | RuntimeState* runtime_state) { |
2266 | 122k | std::set<LocalColumnId> localized_predicate_columns; |
2267 | 122k | FilterProjectionMap filter_projections; |
2268 | 122k | auto filter_mappings = _filter_visible_mappings(); |
2269 | 122k | RETURN_IF_ERROR(build_nested_struct_filter_projection_map(table_filters, filter_mappings, |
2270 | 122k | &filter_projections)); |
2271 | 122k | for (const auto& table_filter : table_filters) { |
2272 | 83.9k | for (const auto& global_index : table_filter.global_indices) { |
2273 | 83.9k | auto* mapping = _find_filter_mapping(global_index); |
2274 | 83.9k | if (mapping == nullptr || !mapping->file_local_id.has_value() || |
2275 | 83.9k | !filter_conversion_has_local_source(mapping->filter_conversion)) { |
2276 | 8.38k | continue; |
2277 | 8.38k | } |
2278 | 75.5k | RETURN_IF_ERROR(add_scan_column(file_request, mapping, enable_lazy_materialization(), |
2279 | 75.5k | force_full_complex_scan_projection(), |
2280 | 75.5k | &filter_projections)); |
2281 | 75.5k | } |
2282 | 82.6k | } |
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 | 525k | for (auto& mapping : _mappings) { |
2286 | 525k | if (mapping.file_local_id.has_value() && |
2287 | 525k | file_request->local_positions.contains(LocalColumnId(*mapping.file_local_id))) { |
2288 | 500k | RETURN_IF_ERROR(apply_scan_projection_to_mapping_file_type(*file_request, &mapping)); |
2289 | 500k | } |
2290 | 525k | } |
2291 | 122k | 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 | 122k | 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 | 122k | filter_mappings = _filter_visible_mappings(); |
2302 | 122k | const auto global_to_file_slot = build_file_slot_rewrite_map(filter_mappings, _filter_entries); |
2303 | 122k | for (const auto& table_filter : table_filters) { |
2304 | 82.8k | if (table_filter.conjunct != nullptr && table_filter.conjunct->root() != nullptr) { |
2305 | 82.8k | const auto root = table_filter.conjunct->root(); |
2306 | 82.8k | const auto impl = root->get_impl(); |
2307 | 82.8k | const auto predicate = impl != nullptr ? impl : root; |
2308 | 82.8k | if (!predicate->is_deterministic() || |
2309 | 82.8k | !table_filter_has_only_local_entries(table_filter, _filter_entries)) { |
2310 | 8.12k | continue; |
2311 | 8.12k | } |
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 | 74.7k | RewriteContext rewrite_context {.runtime_state = runtime_state}; |
2315 | 74.7k | VExprSPtr rewrite_root; |
2316 | 74.7k | Status clone_status; |
2317 | 74.7k | try { |
2318 | 74.7k | clone_status = clone_table_expr_tree(table_filter.conjunct->root(), &rewrite_root); |
2319 | 74.7k | } 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 | 74.6k | 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 | 74.6k | bool can_localize = true; |
2352 | 74.6k | auto localized_root = rewrite_table_expr_to_file_expr(rewrite_root, global_to_file_slot, |
2353 | 74.6k | filter_mappings, &rewrite_context, |
2354 | 74.6k | &can_localize); |
2355 | 74.6k | if (!can_localize) { |
2356 | 2.57k | continue; |
2357 | 2.57k | } |
2358 | 72.0k | auto localized_conjunct = VExprContext::create_shared(std::move(localized_root)); |
2359 | 72.0k | RETURN_IF_ERROR(rewrite_context.prepare_created_exprs(localized_conjunct.get())); |
2360 | 72.0k | file_request->conjuncts.push_back(std::move(localized_conjunct)); |
2361 | 72.7k | for (const auto global_index : table_filter.global_indices) { |
2362 | 72.7k | const auto* mapping = _find_filter_mapping(global_index); |
2363 | 72.7k | if (mapping != nullptr && mapping->file_local_id.has_value() && |
2364 | 72.7k | filter_conversion_has_local_source(mapping->filter_conversion)) { |
2365 | 72.7k | localized_predicate_columns.emplace(*mapping->file_local_id); |
2366 | 72.7k | } |
2367 | 72.7k | } |
2368 | 72.0k | } |
2369 | 82.7k | } |
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 | 525k | for (auto& mapping : _mappings) { |
2376 | 525k | if (!mapping.file_local_id.has_value()) { |
2377 | 26.1k | continue; |
2378 | 26.1k | } |
2379 | 499k | const auto local_id = LocalColumnId(*mapping.file_local_id); |
2380 | 499k | if (localized_predicate_columns.contains(local_id)) { |
2381 | 62.0k | continue; |
2382 | 62.0k | } |
2383 | 437k | const auto predicate_it = std::ranges::find_if( |
2384 | 437k | file_request->predicate_columns, [local_id](const LocalColumnIndex& projection) { |
2385 | 59.4k | return projection.column_id() == local_id; |
2386 | 59.4k | }); |
2387 | 437k | if (predicate_it == file_request->predicate_columns.end()) { |
2388 | 435k | continue; |
2389 | 435k | } |
2390 | 2.48k | file_request->non_predicate_columns.push_back(std::move(*predicate_it)); |
2391 | 2.48k | file_request->predicate_columns.erase(predicate_it); |
2392 | 2.48k | } |
2393 | 122k | return Status::OK(); |
2394 | 122k | } |
2395 | | |
2396 | | const ColumnDefinition* TableColumnMapper::_find_file_field( |
2397 | | const ColumnDefinition& table_column, |
2398 | 475k | const std::vector<ColumnDefinition>& file_schema) const { |
2399 | 475k | if (table_column.name.starts_with(BeConsts::GLOBAL_ROWID_COL)) { |
2400 | 1.04M | const auto field_it = std::ranges::find_if(file_schema, [](const ColumnDefinition& field) { |
2401 | 1.04M | return field.column_type == ColumnType::GLOBAL_ROWID; |
2402 | 1.04M | }); |
2403 | 7.47k | return field_it == file_schema.end() ? nullptr : &*field_it; |
2404 | 7.47k | } |
2405 | 468k | const auto* matched = matcher_for_mode(_options.mode).find(table_column, file_schema); |
2406 | 468k | if (matched != nullptr || _options.mode != TableColumnMappingMode::BY_FIELD_ID || |
2407 | 468k | !_options.allow_idless_complex_wrapper_projection || table_column.children.empty()) { |
2408 | 467k | return matched; |
2409 | 467k | } |
2410 | 782 | const ColumnDefinition* wrapper = nullptr; |
2411 | 3.36k | for (const auto& candidate : file_schema) { |
2412 | 3.36k | if (candidate.has_identifier_field_id() || candidate.children.empty() || |
2413 | 3.36k | !has_shared_descendant_field_id(table_column, candidate)) { |
2414 | 3.36k | continue; |
2415 | 3.36k | } |
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 | 782 | return wrapper; |
2424 | 782 | } |
2425 | | |
2426 | | Status TableColumnMapper::_create_direct_mapping(const ColumnDefinition& table_column, |
2427 | | const ColumnDefinition& file_field, |
2428 | 816k | ColumnMapping* mapping) const { |
2429 | 816k | DORIS_CHECK(mapping != nullptr); |
2430 | 816k | DORIS_CHECK(file_field.local_id >= 0 || file_field.local_id == GLOBAL_ROWID_COLUMN_ID); |
2431 | 816k | mapping->file_local_id = file_field.local_id; |
2432 | 816k | mapping->table_column_name = table_column.name; |
2433 | 816k | mapping->file_column_name = file_field.name; |
2434 | 816k | mapping->original_file_type = file_field.type; |
2435 | 816k | mapping->original_file_children = file_field.children; |
2436 | 816k | mapping->projected_file_children = file_field.children; |
2437 | 816k | mapping->file_type = file_field.type; |
2438 | 816k | mapping->is_trivial = mapping_can_use_file_column_directly(*mapping); |
2439 | 816k | mapping->filter_conversion = direct_filter_conversion(*mapping); |
2440 | 816k | mapping->child_mappings.clear(); |
2441 | | |
2442 | 816k | auto [table_children, synthesized_table_children] = |
2443 | 816k | prepare_table_children_for_mapping(table_column, mapping->file_type); |
2444 | | |
2445 | 816k | if (!table_children.empty()) { |
2446 | 204k | 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 | 204k | RETURN_IF_ERROR(validate_file_schema_children(file_field)); |
2454 | 204k | std::vector<int32_t> synthesized_used_file_child_ids; |
2455 | 526k | for (size_t table_child_idx = 0; table_child_idx < table_children.size(); |
2456 | 321k | ++table_child_idx) { |
2457 | 321k | const auto& table_child = table_children[table_child_idx]; |
2458 | 321k | const auto* file_child = |
2459 | 321k | find_file_child_for_mapping(table_child, file_field, _options.mode, |
2460 | 321k | table_child_idx, synthesized_table_children); |
2461 | 321k | if (file_child == nullptr && !synthesized_table_children && |
2462 | 321k | _options.mode == TableColumnMappingMode::BY_FIELD_ID && |
2463 | 321k | _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.30k | file_child = _find_file_field(table_child, file_field.children); |
2467 | 2.30k | } |
2468 | 321k | if (synthesized_table_children && file_child != nullptr) { |
2469 | 10 | const auto file_child_id = file_child->file_local_id(); |
2470 | 10 | if (std::ranges::find(synthesized_used_file_child_ids, file_child_id) != |
2471 | 10 | 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 | 10 | if (file_child != nullptr) { |
2483 | 10 | synthesized_used_file_child_ids.push_back(file_child->file_local_id()); |
2484 | 10 | } |
2485 | 10 | } |
2486 | 321k | if (file_child == nullptr) { |
2487 | 5.34k | ColumnMapping child_mapping; |
2488 | 5.34k | child_mapping.table_column_name = table_child.name; |
2489 | 5.34k | child_mapping.file_column_name = table_child.name; |
2490 | 5.34k | child_mapping.table_type = table_child.type; |
2491 | 5.34k | child_mapping.file_type = table_child.type; |
2492 | 5.34k | 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 | 5.34k | RETURN_IF_ERROR(build_initial_default_column( |
2496 | 5.34k | table_child, &child_mapping.initial_default_column)); |
2497 | 5.34k | mapping->child_mappings.push_back(std::move(child_mapping)); |
2498 | 5.34k | continue; |
2499 | 5.34k | } |
2500 | 316k | ColumnMapping child_mapping; |
2501 | 316k | child_mapping.table_column_name = table_child.name; |
2502 | 316k | child_mapping.table_type = table_child.type; |
2503 | 316k | RETURN_IF_ERROR(_create_direct_mapping(table_child, *file_child, &child_mapping)); |
2504 | 316k | mapping->child_mappings.push_back(std::move(child_mapping)); |
2505 | 316k | } |
2506 | 204k | 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 | 18.8k | RETURN_IF_ERROR(rebuild_projected_file_children_and_type( |
2509 | 18.8k | mapping->file_type, mapping->original_file_children, mapping->child_mappings, |
2510 | 18.8k | &mapping->projected_file_children, &mapping->file_type)); |
2511 | 18.8k | DCHECK(mapping->table_type != nullptr); |
2512 | 18.8k | mapping->is_trivial = mapping_can_use_file_column_directly(*mapping); |
2513 | 18.8k | mapping->filter_conversion = projected_filter_conversion(*mapping); |
2514 | 18.8k | } |
2515 | 204k | } |
2516 | 816k | return Status::OK(); |
2517 | 816k | } |
2518 | | |
2519 | | } // namespace doris::format |