Coverage Report

Created: 2026-07-20 19:42

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