Coverage Report

Created: 2026-07-25 15:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/format_v2/table/hudi_reader.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "format_v2/table/hudi_reader.h"
19
20
#include <utility>
21
22
#include "exprs/vexpr_context.h"
23
#include "format_v2/column_mapper.h"
24
#include "format_v2/jni/hudi_jni_reader.h"
25
#include "format_v2/table/schema_history_util.h"
26
#include "gen_cpp/PlanNodes_types.h"
27
28
namespace doris::format::hudi {
29
30
14
Status HudiReader::prepare_split(const format::SplitReadOptions& options) {
31
14
    {
32
        // Derived schema selection is additive to, not nested around, the common base timers.
33
14
        SCOPED_TIMER(_profile.total_timer);
34
14
        SCOPED_TIMER(_profile.prepare_split_timer);
35
14
        _split_schema_id = -1;
36
14
        if (options.current_range.__isset.table_format_params &&
37
14
            options.current_range.table_format_params.__isset.hudi_params &&
38
14
            options.current_range.table_format_params.hudi_params.__isset.schema_id) {
39
10
            _split_schema_id = options.current_range.table_format_params.hudi_params.schema_id;
40
10
        }
41
14
    }
42
14
    RETURN_IF_ERROR(format::TableReader::prepare_split(options));
43
14
    SCOPED_TIMER(_profile.total_timer);
44
14
    SCOPED_TIMER(_profile.prepare_split_timer);
45
14
    if (current_split_pruned()) {
46
0
        return Status::OK();
47
0
    }
48
    // This native reader only receives Hudi base-file splits that do not require merging delta
49
    // logs. Hudi creates a new versioned base file for updates and compaction instead of modifying
50
    // an existing Parquet/ORC base file in place, so missing mtime does not make its page-cache key
51
    // ambiguous. Merge-on-read log files remain on the JNI path because they may be appended and
52
    // must never be marked immutable here.
53
14
    mark_current_data_file_immutable();
54
14
    return Status::OK();
55
14
}
56
57
16
format::TableColumnMappingMode HudiReader::mapping_mode() const {
58
16
    return format::can_map_by_history_schema(_scan_params, _split_schema_id)
59
16
                   ? format::TableColumnMappingMode::BY_FIELD_ID
60
16
                   : format::TableColumnMappingMode::BY_NAME;
61
16
}
62
63
6
Status HudiReader::annotate_file_schema(std::vector<format::ColumnDefinition>* file_schema) {
64
6
    DORIS_CHECK(file_schema != nullptr);
65
6
    if (mapping_mode() != format::TableColumnMappingMode::BY_FIELD_ID) {
66
4
        return Status::OK();
67
4
    }
68
2
    return format::annotate_file_schema_from_history(_scan_params, _split_schema_id, file_schema);
69
6
}
70
71
4
Status HudiHybridReader::init(format::TableReadOptions&& options) {
72
4
    return format::TableReader::init(std::move(options));
73
4
}
74
75
6
Status HudiHybridReader::prepare_split(const format::SplitReadOptions& options) {
76
    // A newly selected child initializes against the same scanner profile. Keep hybrid dispatch
77
    // outside those shared counters so first-split initialization is counted exactly once.
78
6
    RETURN_IF_ERROR(_ensure_current_split_reader(options));
79
6
    DORIS_CHECK(_current_split_reader != nullptr);
80
6
    return _current_split_reader->prepare_split(options);
81
6
}
82
83
2
Status HudiHybridReader::get_block(Block* block, bool* eos) {
84
2
    DORIS_CHECK(_current_split_reader != nullptr);
85
2
    return _current_split_reader->get_block(block, eos);
86
2
}
87
88
0
bool HudiHybridReader::current_split_pruned() const {
89
0
    DORIS_CHECK(_current_split_reader != nullptr);
90
0
    return _current_split_reader->current_split_pruned();
91
0
}
92
93
2
bool HudiHybridReader::current_split_uses_metadata_count() const {
94
2
    DORIS_CHECK(_current_split_reader != nullptr);
95
2
    return _current_split_reader->current_split_uses_metadata_count();
96
2
}
97
98
0
Status HudiHybridReader::abort_split() {
99
0
    DORIS_CHECK(_current_split_reader != nullptr);
100
0
    return _current_split_reader->abort_split();
101
0
}
102
103
2
Status HudiHybridReader::close() {
104
2
    Status close_status = Status::OK();
105
2
    if (_native_reader != nullptr) {
106
2
        close_status = _native_reader->close();
107
2
    }
108
2
    if (_jni_reader != nullptr) {
109
0
        auto status = _jni_reader->close();
110
0
        if (!status.ok() && close_status.ok()) {
111
0
            close_status = std::move(status);
112
0
        }
113
0
    }
114
2
    _current_split_reader = nullptr;
115
2
    return close_status;
116
2
}
117
118
2
void HudiHybridReader::set_batch_size(size_t batch_size) {
119
2
    format::TableReader::set_batch_size(batch_size);
120
2
    if (_native_reader != nullptr) {
121
2
        _native_reader->set_batch_size(_batch_size);
122
2
    }
123
2
    if (_jni_reader != nullptr) {
124
2
        _jni_reader->set_batch_size(_batch_size);
125
2
    }
126
2
}
127
128
2
int64_t HudiHybridReader::condition_cache_hit_count() const {
129
    // Keep the wrapper count cumulative across native/JNI dispatch so scanner-level delta
130
    // accounting neither loses a child hit nor observes a counter reset on a split switch.
131
2
    return (_native_reader == nullptr ? 0 : _native_reader->condition_cache_hit_count()) +
132
2
           (_jni_reader == nullptr ? 0 : _jni_reader->condition_cache_hit_count());
133
2
}
134
135
6
Status HudiHybridReader::_ensure_current_split_reader(const format::SplitReadOptions& options) {
136
6
    DORIS_CHECK(_scan_params != nullptr);
137
6
    if (_is_jni_split(*_scan_params, options.current_range)) {
138
2
        if (_jni_reader == nullptr) {
139
2
#ifdef BE_TEST
140
2
            if (_test_jni_reader_factory) {
141
2
                _jni_reader = _test_jni_reader_factory();
142
2
            } else {
143
0
                _jni_reader = std::make_unique<format::hudi::HudiJniReader>();
144
0
            }
145
#else
146
            _jni_reader = std::make_unique<format::hudi::HudiJniReader>();
147
#endif
148
2
            RETURN_IF_ERROR(_init_child_reader(_jni_reader.get(), format::FileFormat::JNI));
149
2
        }
150
2
        _current_split_reader = _jni_reader.get();
151
4
    } else {
152
4
        format::FileFormat file_format;
153
4
        RETURN_IF_ERROR(_to_file_format(*_scan_params, options.current_range, &file_format));
154
4
        if (_native_reader == nullptr) {
155
4
#ifdef BE_TEST
156
4
            if (_test_native_reader_factory) {
157
2
                _native_reader = _test_native_reader_factory();
158
2
            } else {
159
2
                _native_reader = format::hudi::HudiReader::create_unique();
160
2
            }
161
#else
162
            _native_reader = format::hudi::HudiReader::create_unique();
163
#endif
164
4
            RETURN_IF_ERROR(_init_child_reader(_native_reader.get(), file_format));
165
4
        }
166
4
        _current_split_reader = _native_reader.get();
167
4
    }
168
6
    return Status::OK();
169
6
}
170
171
Status HudiHybridReader::_init_child_reader(format::TableReader* reader,
172
6
                                            format::FileFormat file_format) {
173
6
    DORIS_CHECK(reader != nullptr);
174
6
    VExprContextSPtrs conjuncts;
175
6
    RETURN_IF_ERROR(_clone_conjuncts(&conjuncts));
176
6
    RETURN_IF_ERROR(reader->init({
177
6
            .projected_columns = _projected_columns,
178
6
            .conjuncts = std::move(conjuncts),
179
6
            .format = file_format,
180
6
            .scan_params = _scan_params,
181
6
            .io_ctx = _io_ctx,
182
6
            .runtime_state = _runtime_state,
183
6
            .scanner_profile = _scanner_profile,
184
6
            .push_down_agg_type = _push_down_agg_type,
185
6
            .push_down_count_columns = _push_down_count_columns,
186
6
            .condition_cache_digest = _condition_cache_digest,
187
6
    }));
188
    // Zero means no adaptive prediction has been produced yet. Preserve the child's normal
189
    // runtime default until FileScannerV2 supplies the first positive prediction.
190
6
    if (_batch_size > 0) {
191
0
        reader->set_batch_size(_batch_size);
192
0
    }
193
6
    return Status::OK();
194
6
}
195
196
6
Status HudiHybridReader::_clone_conjuncts(VExprContextSPtrs* conjuncts) const {
197
6
    DORIS_CHECK(conjuncts != nullptr);
198
6
    conjuncts->clear();
199
6
    conjuncts->reserve(_conjuncts.size());
200
6
    for (const auto& conjunct : _conjuncts) {
201
0
        VExprSPtr root;
202
0
        RETURN_IF_ERROR(format::clone_table_expr_tree(conjunct->root(), &root));
203
0
        conjuncts->push_back(VExprContext::create_shared(std::move(root)));
204
0
    }
205
6
    return Status::OK();
206
6
}
207
208
TFileFormatType::type HudiHybridReader::_range_format_type(const TFileScanRangeParams& params,
209
10
                                                           const TFileRangeDesc& range) {
210
10
    return range.__isset.format_type ? range.format_type : params.format_type;
211
10
}
212
213
bool HudiHybridReader::_is_jni_split(const TFileScanRangeParams& params,
214
6
                                     const TFileRangeDesc& range) {
215
6
    return _range_format_type(params, range) == TFileFormatType::FORMAT_JNI;
216
6
}
217
218
Status HudiHybridReader::_to_file_format(const TFileScanRangeParams& params,
219
                                         const TFileRangeDesc& range,
220
4
                                         format::FileFormat* file_format) {
221
4
    DORIS_CHECK(file_format != nullptr);
222
4
    const auto format_type = _range_format_type(params, range);
223
4
    switch (format_type) {
224
4
    case TFileFormatType::FORMAT_PARQUET:
225
4
        *file_format = format::FileFormat::PARQUET;
226
4
        return Status::OK();
227
0
    case TFileFormatType::FORMAT_ORC:
228
0
        *file_format = format::FileFormat::ORC;
229
0
        return Status::OK();
230
0
    default:
231
0
        return Status::NotSupported("Unsupported native Hudi file format {}",
232
0
                                    to_string(format_type));
233
4
    }
234
4
}
235
236
} // namespace doris::format::hudi