This is an automated email from the ASF dual-hosted git repository.

lxy-9602 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/paimon-cpp.git


The following commit(s) were added to refs/heads/main by this push:
     new 360fa701 feat(reader): lazily initialize sequential file readers (#564)
360fa701 is described below

commit 360fa7014517f9cc89adf4a3aee8fb3fe86cdefe
Author: Nicholas Jiang <[email protected]>
AuthorDate: Tue Nov 17 18:03:27 2026 +0800

    feat(reader): lazily initialize sequential file readers (#564)
---
 benchmark/CMakeLists.txt                           |  19 +
 benchmark/sequential_reader_benchmark.cpp          | 233 ++++++++++
 docs/source/examples/benchmark.rst                 |  28 ++
 docs/source/user_guide/prefetch.rst                |  25 +
 src/paimon/common/reader/concat_batch_reader.cpp   | 155 +++++--
 src/paimon/common/reader/concat_batch_reader.h     |  53 ++-
 .../common/reader/concat_batch_reader_test.cpp     | 515 +++++++++++++++++++--
 src/paimon/core/operation/raw_file_split_read.cpp  |  43 +-
 src/paimon/core/operation/raw_file_split_read.h    |   6 +-
 .../core/operation/raw_file_split_read_test.cpp    | 243 +++++++++-
 .../testing/utils/counting_reader_file_system.h    | 124 +++++
 test/inte/read_inte_test.cpp                       |   4 +-
 12 files changed, 1335 insertions(+), 113 deletions(-)

diff --git a/benchmark/CMakeLists.txt b/benchmark/CMakeLists.txt
index 60ac3fb3..fc788a49 100644
--- a/benchmark/CMakeLists.txt
+++ b/benchmark/CMakeLists.txt
@@ -44,6 +44,25 @@ if(UNIX AND NOT APPLE)
 endif()
 
 if(PAIMON_BUILD_BENCHMARKS)
+    if(PAIMON_ENABLE_ORC)
+        add_paimon_benchmark(sequential_reader_benchmark
+                             SOURCES
+                             sequential_reader_benchmark.cpp
+                             STATIC_LINK_LIBS
+                             arrow
+                             parquet
+                             paimon_static
+                             ${PAIMON_LOCAL_FILE_SYSTEM_STATIC_LINK_LIBS}
+                             ${PAIMON_PARQUET_FILE_FORMAT_STATIC_LINK_LIBS}
+                             ${PAIMON_BLOB_FILE_FORMAT_STATIC_LINK_LIBS}
+                             ${PAIMON_ORC_FILE_FORMAT_STATIC_LINK_LIBS}
+                             ${PAIMON_AVRO_FILE_FORMAT_STATIC_LINK_LIBS}
+                             test_utils_static
+                             Threads::Threads
+                             ${PAIMON_BENCHMARK_PLATFORM_LINK_LIBS}
+                             ${PAIMON_BENCHMARK_LINK_TOOLCHAIN})
+    endif()
+
     add_paimon_benchmark(read_write_benchmark
                          SOURCES
                          benchmark_helpers.cpp
diff --git a/benchmark/sequential_reader_benchmark.cpp 
b/benchmark/sequential_reader_benchmark.cpp
new file mode 100644
index 00000000..2009a4ac
--- /dev/null
+++ b/benchmark/sequential_reader_benchmark.cpp
@@ -0,0 +1,233 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <chrono>
+#include <cstdint>
+#include <filesystem>
+#include <memory>
+#include <string>
+#include <thread>
+#include <utility>
+#include <vector>
+
+#include "arrow/c/abi.h"
+#include "benchmark/benchmark.h"
+#include "fmt/format.h"
+#include "paimon/common/reader/complete_row_kind_batch_reader.h"
+#include "paimon/common/reader/concat_batch_reader.h"
+#include "paimon/common/reader/reader_utils.h"
+#include "paimon/common/utils/arrow/mem_utils.h"
+#include "paimon/common/utils/object_utils.h"
+#include "paimon/core/io/data_file_meta.h"
+#include "paimon/core/operation/raw_file_split_read.h"
+#include "paimon/core/utils/file_store_path_factory.h"
+#include "paimon/format/file_format.h"
+#include "paimon/testing/utils/binary_row_generator.h"
+#include "paimon/testing/utils/counting_reader_file_system.h"
+#include "paimon/testing/utils/testharness.h"
+
+namespace paimon::test {
+namespace {
+
+bool benchmark_failed = false;
+
+struct Fixture {
+    std::unique_ptr<UniqueTestDirectory> directory;
+    std::string table_path;
+    std::shared_ptr<TableSchema> table_schema;
+    std::vector<std::shared_ptr<DataFileMeta>> files;
+};
+
+Result<Fixture> PrepareFiles(int64_t count) {
+    Fixture fixture;
+    fixture.directory = UniqueTestDirectory::Create();
+    if (!fixture.directory) {
+        return Status::IOError("cannot create benchmark directory");
+    }
+    fixture.table_path =
+        GetDataDir() + 
"/orc/multi_partition_append_table.db/multi_partition_append_table";
+    SchemaManager schemas(fixture.directory->GetFileSystem(), 
fixture.table_path);
+    PAIMON_ASSIGN_OR_RAISE(fixture.table_schema, schemas.ReadSchema(0));
+    const std::string source =
+        fixture.table_path + 
"/f1=10/f2=0/bucket-0/data-01b6a930-6564-409b-b8f4-ed1307790d72-0.orc";
+    PAIMON_ASSIGN_OR_RAISE(FileStatus source_status,
+                           
fixture.directory->GetFileSystem()->GetFileStatus(source));
+    for (int64_t i = 0; i < count; i++) {
+        const std::string name = fmt::format("data-{}.orc", i);
+        const std::string path = fixture.directory->Str() + "/" + name;
+        std::error_code error;
+        std::filesystem::copy_file(source, path, error);
+        if (error) {
+            return Status::IOError(error.message());
+        }
+        PAIMON_ASSIGN_OR_RAISE(
+            std::shared_ptr<DataFileMeta> file,
+            DataFileMeta::ForAppend(name, source_status.GetLen(), 
/*row_count=*/3,
+                                    SimpleStats::EmptyStats(), 
/*min_sequence_number=*/0,
+                                    /*max_sequence_number=*/2, /*schema_id=*/0,
+                                    FileSource::Append(), 
/*value_stats_cols=*/std::nullopt, path,
+                                    /*first_row_id=*/std::nullopt, 
/*write_cols=*/std::nullopt));
+        fixture.files.push_back(std::move(file));
+    }
+    return fixture;
+}
+
+struct ScanStats {
+    double seconds = 0;
+    double first_batch_us = 0;
+    uint64_t initial_footer_reads = 0;
+    uint64_t first_batch_opens = 0;
+    uint64_t opens = 0;
+    uint64_t peak_streams = 0;
+    uint64_t pool_peak_bytes = 0;
+    uint64_t read_bytes = 0;
+    int64_t rows = 0;
+};
+
+Result<ScanStats> Scan(const Fixture& fixture, bool lazy, bool first_only, 
int64_t open_delay_us) {
+    std::shared_ptr<MemoryPool> pool = GetMemoryPool();
+    auto fs = std::make_shared<CountingReaderFileSystem>(".orc");
+    if (open_delay_us > 0) {
+        fs->before_open = [open_delay_us](const std::string&) {
+            
std::this_thread::sleep_for(std::chrono::microseconds(open_delay_us));
+            return Status::OK();
+        };
+    }
+    ReadContextBuilder builder(fixture.table_path);
+    builder.WithFileSystem(fs).WithMemoryPool(pool).EnablePrefetch(false);
+    builder.AddOption(Options::FILE_INDEX_READ_ENABLED, "false");
+    PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<ReadContext> read_context, 
builder.Finish());
+    auto read_options = fixture.table_schema->Options();
+    for (const auto& [key, value] : read_context->GetOptions()) {
+        read_options.insert_or_assign(key, value);
+    }
+    PAIMON_ASSIGN_OR_RAISE(
+        std::shared_ptr<InternalReadContext> context,
+        InternalReadContext::Create(read_context, fixture.table_schema, 
read_options));
+    const auto& options = context->GetCoreOptions();
+    auto schema = 
DataField::ConvertDataFieldsToArrowSchema(fixture.table_schema->Fields());
+    PAIMON_ASSIGN_OR_RAISE(
+        std::shared_ptr<FileStorePathFactory> paths,
+        FileStorePathFactory::Create(
+            fixture.table_path, schema, fixture.table_schema->PartitionKeys(),
+            options.GetPartitionDefaultName(), 
options.GetFileFormat()->Identifier(),
+            options.DataFilePrefix(), options.LegacyPartitionNameEnabled(), 
{}, {},
+            options.IndexFileInDataFileDir(), {}, pool));
+    auto partition = BinaryRowGenerator::GenerateRow({10, 0}, pool.get());
+    PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<DataFilePathFactory> data_paths,
+                           paths->CreateDataFilePathFactory(partition, 0));
+    RawFileSplitRead split_read(paths, context, pool, context->GetExecutor());
+    auto arrow_pool = GetArrowPool(pool);
+    const auto start = std::chrono::steady_clock::now();
+    std::unique_ptr<BatchReader> reader;
+    if (lazy) {
+        PAIMON_ASSIGN_OR_RAISE(
+            reader, split_read.CreateReader(partition, 0, fixture.files, 
DeletionVector::Factory(),
+                                            std::nullopt));
+    } else {
+        PAIMON_ASSIGN_OR_RAISE(
+            std::vector<std::unique_ptr<FileBatchReader>> children,
+            split_read.CreateRawFileReaders(partition, fixture.files, 
context->GetReadSchema(),
+                                            nullptr, {}, std::nullopt, 
data_paths, {}));
+        auto concat = std::make_unique<ConcatBatchReader>(
+            
ObjectUtils::MoveVector<std::unique_ptr<BatchReader>>(std::move(children)), 
arrow_pool);
+        reader = 
std::make_unique<CompleteRowKindBatchReader>(std::move(concat), arrow_pool);
+    }
+    ScanStats stats;
+    // With prefetch disabled and before NextBatch, these are the ORC 
footer/setup reads.
+    stats.initial_footer_reads = fs->counts->reads.load();
+    while (true) {
+        PAIMON_ASSIGN_OR_RAISE(BatchReader::ReadBatch batch, 
reader->NextBatch());
+        if (BatchReader::IsEofBatch(batch)) {
+            break;
+        }
+        if (stats.rows == 0) {
+            stats.first_batch_us =
+                std::chrono::duration<double, 
std::micro>(std::chrono::steady_clock::now() - start)
+                    .count();
+            stats.first_batch_opens = fs->counts->opens.load();
+        }
+        stats.rows += batch.first->length;
+        ReaderUtils::ReleaseReadBatch(std::move(batch));
+        if (first_only) {
+            break;
+        }
+    }
+    reader->Close();
+    stats.seconds = 
std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count();
+    stats.opens = fs->counts->opens.load();
+    stats.peak_streams = fs->counts->peak.load();
+    stats.read_bytes = fs->counts->bytes.load();
+    stats.pool_peak_bytes = pool->MaxMemoryUsage();
+    const int64_t expected = first_only ? 3 : 3 * 
static_cast<int64_t>(fixture.files.size());
+    if (stats.rows != expected || fs->counts->live.load() != 0) {
+        return Status::Invalid("unexpected row count or retained streams after 
Close");
+    }
+    return stats;
+}
+
+void BM_SequentialReaders(::benchmark::State& state) {
+    auto fixture = PrepareFiles(state.range(0));
+    if (!fixture.ok()) {
+        benchmark_failed = true;
+        state.SkipWithError(fixture.status().ToString().c_str());
+        return;
+    }
+    int64_t rows = 0;
+    for (auto _ : state) {
+        auto scan = Scan(fixture.value(), state.range(1) != 0, state.range(2) 
!= 0, state.range(3));
+        if (!scan.ok()) {
+            benchmark_failed = true;
+            state.SkipWithError(scan.status().ToString().c_str());
+            return;
+        }
+        const auto& stats = scan.value();
+        state.SetIterationTime(stats.seconds);
+        rows += stats.rows;
+        state.counters["first_batch_us"] += stats.first_batch_us;
+        state.counters["initial_footer_reads"] += stats.initial_footer_reads;
+        state.counters["first_batch_opens"] += stats.first_batch_opens;
+        state.counters["opens"] += stats.opens;
+        state.counters["peak_streams"] += stats.peak_streams;
+        state.counters["pool_peak_bytes"] += stats.pool_peak_bytes;
+        state.counters["read_bytes"] += stats.read_bytes;
+    }
+    for (auto& [name, counter] : state.counters) {
+        counter.flags = ::benchmark::Counter::kAvgIterations;
+    }
+    state.SetItemsProcessed(rows);
+}
+
+BENCHMARK(BM_SequentialReaders)
+    ->ArgsProduct({{128, 1024}, {0, 1}, {0, 1}, {0, 1000}})
+    ->ArgNames({"files", "lazy", "first_only", "open_delay_us"})
+    ->UseManualTime();
+
+}  // namespace
+}  // namespace paimon::test
+
+int32_t main(int32_t argc, char** argv) {
+    ::benchmark::Initialize(&argc, argv);
+    if (::benchmark::ReportUnrecognizedArguments(argc, argv)) {
+        return 1;
+    }
+    ::benchmark::RunSpecifiedBenchmarks();
+    ::benchmark::Shutdown();
+    return paimon::test::benchmark_failed ? 1 : 0;
+}
diff --git a/docs/source/examples/benchmark.rst 
b/docs/source/examples/benchmark.rst
index 01d49cfb..a1833d9d 100644
--- a/docs/source/examples/benchmark.rst
+++ b/docs/source/examples/benchmark.rst
@@ -211,3 +211,31 @@ Read batch size sweep, repeated for a stable comparison::
 Null density on both sides, to see what definition levels cost::
 
    paimon-parquet-format-benchmark 
--benchmark_filter='Parquet(Write|Read)_Nulls'
+
+Sequential small-file reads
+===========================
+
+``paimon-sequential-reader-benchmark`` compares eager construction (``lazy:0``)
+with the bounded raw-reader window (``lazy:1``). It requires ORC and
+``PAIMON_BUILD_BENCHMARKS=ON``. Each case copies the small checked-in ORC 
fixture
+to 128 or 1024 distinct temporary files, outside the measured interval.
+``first_only:1`` closes after the first batch; ``first_only:0`` consumes every
+file. Both paths use the same row-kind wrapper and disable batch prefetch to
+isolate reader construction.
+
+The measured interval includes reader construction, consumption and close.
+Counters report first-batch latency, first-batch and total file opens, initial
+footer/setup read calls, peak retained streams, peak Paimon pool allocation,
+and bytes read. Pool allocation excludes memory allocated outside Paimon's
+pool and is not process RSS. ``items_per_second`` reports returned rows per
+second. An optional 1000-microsecond delay per open simulates remote setup
+latency; it does not model remote bandwidth or cache behavior.
+
+Run a repeated comparison and save the counters::
+
+   cmake --build build --target paimon-sequential-reader-benchmark
+   ./build/debug/paimon-sequential-reader-benchmark \
+       --benchmark_filter='BM_SequentialReaders/files:1024/' \
+       --benchmark_repetitions=5 \
+       --benchmark_out=sequential-readers.json \
+       --benchmark_out_format=json
diff --git a/docs/source/user_guide/prefetch.rst 
b/docs/source/user_guide/prefetch.rst
index e8a3a348..d223d055 100644
--- a/docs/source/user_guide/prefetch.rst
+++ b/docs/source/user_guide/prefetch.rst
@@ -71,6 +71,27 @@ Prefetch is enabled when the estimated compressed size of 
the selected columns
 exceeds the configured threshold; otherwise the reader uses the normal
 single-reader path.
 
+Sequential file preparation
+===========================
+
+Append/raw reads construct a sliding window of at most four data-file reader
+stacks per split, including construction tasks still in flight. The initial
+window is built on the dedicated reader-build executor before ``CreateReader``
+returns. As a file reaches EOF, its reader is closed, its metrics are retained,
+and the next file is submitted for construction while prepared files are read.
+This bounds open streams and format metadata independently of the number of
+files in the split. Per-file metadata and owned creation inputs still scale
+with the split's file count. Merge-on-read keeps its eager input construction.
+
+Plan validation still runs during ``CreateReader``. File-open and reader-build
+errors in the initial window are returned there; errors for later files are
+returned through ``NextBatch`` or ``NextBatchWithBitmap`` when that file is
+reached. These errors are terminal. The returned reader retains its creation
+context, schemas, file system and memory pools, so it may outlive the object
+that created it. Closing or destroying it cancels queued construction, waits
+for already running construction, and releases the prepared readers without
+scheduling the unopened tail.
+
 Warmup
 ======
 
@@ -83,6 +104,10 @@ the default ``loser-tree`` sort engine additionally warms 
the first file of
 every sorted run before merging. It applies to append-table reads and append
 compaction as well, but does not cross split boundaries.
 
+For lazy append/raw readers, warmup uses the next file once its construction
+has completed; reading the current file never waits for lookahead construction.
+The construction window is independent of the warmup level below.
+
 How far the next file is prepared is set with
 ``ReadContextBuilder::SetWarmupLevel()``. Append compaction builds its own read
 context and always uses the default level.
diff --git a/src/paimon/common/reader/concat_batch_reader.cpp 
b/src/paimon/common/reader/concat_batch_reader.cpp
index 167fdc5f..1607cf31 100644
--- a/src/paimon/common/reader/concat_batch_reader.cpp
+++ b/src/paimon/common/reader/concat_batch_reader.cpp
@@ -20,9 +20,11 @@
 #include "paimon/common/reader/concat_batch_reader.h"
 
 #include <algorithm>
+#include <chrono>
 #include <utility>
 
 #include "arrow/c/abi.h"
+#include "paimon/common/executor/future.h"
 #include "paimon/common/metrics/metrics_impl.h"
 #include "paimon/common/reader/reader_utils.h"
 #include "paimon/common/utils/arrow/mem_utils.h"
@@ -33,78 +35,159 @@ class MemoryPool;
 
 
ConcatBatchReader::ConcatBatchReader(std::vector<std::unique_ptr<BatchReader>>&&
 readers,
                                      const std::shared_ptr<arrow::MemoryPool>& 
arrow_pool)
-    : arrow_pool_(arrow_pool),
-      finished_reader_metrics_(std::make_shared<MetricsImpl>()),
-      readers_(std::move(readers)),
-      file_readers_(readers_.size(), nullptr),
-      current_(0) {
-    // Non-file readers keep their null slots so the warmup window stays 
aligned with readers_.
-    for (size_t i = 0; i < readers_.size(); i++) {
-        if (auto* file_reader = 
dynamic_cast<FileBatchReader*>(readers_[i].get())) {
-            file_readers_[i] = file_reader;
+    : arrow_pool_(arrow_pool), 
finished_reader_metrics_(std::make_shared<MetricsImpl>()) {
+    for (auto& reader : readers) {
+        readers_.emplace_back();
+        readers_.back().file_reader = 
dynamic_cast<FileBatchReader*>(reader.get());
+        readers_.back().reader = std::move(reader);
+    }
+}
+
+ConcatBatchReader::~ConcatBatchReader() {
+    ConcatBatchReader::Close();
+}
+
+Result<std::unique_ptr<ConcatBatchReader>> ConcatBatchReader::CreateLazy(
+    size_t reader_count, ReaderFactory factory,
+    const std::shared_ptr<arrow::MemoryPool>& arrow_pool,
+    const std::shared_ptr<Executor>& build_executor) {
+    if (!factory || !build_executor) {
+        return Status::Invalid("lazy reader requires a factory and a build 
executor");
+    }
+    auto reader = 
std::make_unique<ConcatBatchReader>(std::vector<std::unique_ptr<BatchReader>>(),
+                                                      arrow_pool);
+    reader->preparation_ = std::make_shared<Preparation>(std::move(factory));
+    reader->build_executor_ = build_executor;
+    reader->reader_count_ = reader_count;
+    reader->FillWindow();
+    for (auto& state : reader->readers_) {
+        PAIMON_RETURN_NOT_OK(reader->ResolveReader(&state, /*wait=*/true));
+    }
+    return reader;
+}
+
+void ConcatBatchReader::FillWindow() {
+    while (next_reader_ < reader_count_ && readers_.size() < 
kPreparationWindow) {
+        readers_.emplace_back();
+        readers_.back().future =
+            Via(build_executor_.get(),
+                [preparation = preparation_,
+                 index = next_reader_++]() mutable -> 
Result<std::unique_ptr<BatchReader>> {
+                    // Release the task's creator before Via publishes its 
result, so draining
+                    // the future also releases the background owner of the 
creation context.
+                    auto retained = std::move(preparation);
+                    if (retained->cancelled.load()) {
+                        return std::unique_ptr<BatchReader>();
+                    }
+                    return retained->factory(index);
+                });
+    }
+}
+
+Status ConcatBatchReader::ResolveReader(ReaderState* state, bool wait) {
+    if (state->future.valid() &&
+        (wait || state->future.wait_for(std::chrono::seconds(0)) == 
std::future_status::ready)) {
+        auto result = state->future.get();
+        if (!result.ok()) {
+            state->failure = result.status();
+        } else {
+            state->reader = std::move(result).value();
+            state->file_reader = 
dynamic_cast<FileBatchReader*>(state->reader.get());
         }
     }
+    return state->failure;
 }
 
 Result<BatchReader::ReadBatch> ConcatBatchReader::NextBatch() {
     PAIMON_ASSIGN_OR_RAISE(BatchReader::ReadBatchWithBitmap batch_with_bitmap,
                            NextBatchWithBitmap());
-    PAIMON_ASSIGN_OR_RAISE(
-        BatchReader::ReadBatch batch,
-        ReaderUtils::ApplyBitmapToReadBatch(std::move(batch_with_bitmap), 
arrow_pool_));
-    return batch;
+    auto result = 
ReaderUtils::ApplyBitmapToReadBatch(std::move(batch_with_bitmap), arrow_pool_);
+    if (!result.ok()) {
+        failure_ = result.status();
+        Close();
+    }
+    return result;
 }
 
 void ConcatBatchReader::Close() {
-    for (; current_ < readers_.size(); current_++) {
-        CloseAndReleaseReader(current_);
+    if (preparation_) {
+        preparation_->cancelled.store(true);
     }
+    for (auto& state : readers_) {
+        try {
+            (void)ResolveReader(&state, /*wait=*/true);
+        } catch (...) {
+            // Construction exceptions captured by Via must not escape 
destruction or prevent
+            // the remaining tasks from being drained.
+        }
+        CloseAndReleaseReader(&state);
+    }
+    readers_.clear();
+    next_reader_ = reader_count_;
+    preparation_.reset();
 }
 
 std::shared_ptr<Metrics> ConcatBatchReader::GetReaderMetrics() const {
     auto metrics = std::make_shared<MetricsImpl>();
     metrics->Merge(finished_reader_metrics_);
-    metrics->Merge(MetricsImpl::CollectReadMetrics(readers_));
+    for (const auto& state : readers_) {
+        if (state.reader) {
+            metrics->Merge(state.reader->GetReaderMetrics());
+        }
+    }
     return metrics;
 }
 
-void ConcatBatchReader::CloseAndReleaseReader(size_t reader_index) {
-    std::unique_ptr<BatchReader>& reader = readers_[reader_index];
+void ConcatBatchReader::CloseAndReleaseReader(ReaderState* state) {
+    std::unique_ptr<BatchReader>& reader = state->reader;
     if (!reader) {
         return;
     }
     reader->Close();
     finished_reader_metrics_->Merge(reader->GetReaderMetrics());
     reader.reset();
-    file_readers_[reader_index] = nullptr;
+    state->file_reader = nullptr;
 }
 
-void ConcatBatchReader::WarmupRange(size_t idx, size_t count) {
-    const size_t end = std::min(idx + count, file_readers_.size());
-    for (size_t i = idx; i < end; i++) {
-        if (file_readers_[i] != nullptr) {
-            file_readers_[i]->Warmup();
+void ConcatBatchReader::WarmupRange() {
+    const size_t end = std::min(1 + kWarmupLookahead, readers_.size());
+    for (size_t i = 0; i < end; i++) {
+        // Never stall the current file on lookahead. Keep a failed 
preparation until its turn.
+        (void)ResolveReader(&readers_[i], /*wait=*/false);
+        if (readers_[i].file_reader != nullptr) {
+            readers_[i].file_reader->Warmup();
         }
     }
 }
 
 Result<BatchReader::ReadBatchWithBitmap> 
ConcatBatchReader::NextBatchWithBitmap() {
-    while (current_ < readers_.size()) {
+    PAIMON_RETURN_NOT_OK(failure_);
+    while (!readers_.empty()) {
+        failure_ = ResolveReader(&readers_.front(), /*wait=*/true);
+        if (!failure_.ok()) {
+            Close();
+            return failure_;
+        }
         // Lookahead: when the file system reads asynchronously, the next 
file's first read is paid
         // while this file is still being consumed, instead of serially after 
its EOF.
-        WarmupRange(current_, 1 + kWarmupLookahead);
-        auto& current_reader = readers_[current_];
-        PAIMON_ASSIGN_OR_RAISE(BatchReader::ReadBatchWithBitmap result,
-                               current_reader->NextBatchWithBitmap());
-        if (!BatchReader::IsEofBatch(result)) {
-            // current reader not eof, just return
-            return result;
+        WarmupRange();
+        auto& current_reader = readers_.front().reader;
+        if (current_reader) {
+            auto result = current_reader->NextBatchWithBitmap();
+            if (!result.ok()) {
+                failure_ = result.status();
+                Close();
+                return failure_;
+            }
+            if (!BatchReader::IsEofBatch(result.value())) {
+                return result;
+            }
         }
-        // current meets eof, move to next reader
-        CloseAndReleaseReader(current_);
-        current_++;
+        CloseAndReleaseReader(&readers_.front());
+        readers_.pop_front();
+        FillWindow();
     }
-    // read finish
+    preparation_.reset();
     return BatchReader::MakeEofBatchWithBitmap();
 }
 
diff --git a/src/paimon/common/reader/concat_batch_reader.h 
b/src/paimon/common/reader/concat_batch_reader.h
index 031e6187..4c273052 100644
--- a/src/paimon/common/reader/concat_batch_reader.h
+++ b/src/paimon/common/reader/concat_batch_reader.h
@@ -19,14 +19,21 @@
 
 #pragma once
 
+#include <atomic>
 #include <cstddef>
+#include <deque>
+#include <functional>
+#include <future>
 #include <memory>
+#include <utility>
 #include <vector>
 
 #include "arrow/api.h"
+#include "paimon/common/executor/reader_build_executor.h"
 #include "paimon/metrics.h"
 #include "paimon/reader/batch_reader.h"
 #include "paimon/result.h"
+#include "paimon/status.h"
 
 namespace paimon {
 class FileBatchReader;
@@ -36,8 +43,22 @@ class MemoryPool;
 /// is already sorted by key and sequence number, and the key intervals do not 
overlap each other.
 class ConcatBatchReader : public BatchReader {
  public:
+    /// Must own the state needed to build the indexed reader beyond its 
caller's lifetime.
+    /// Called at most once per index, possibly concurrently. A null reader 
means a skipped file.
+    using ReaderFactory = 
std::function<Result<std::unique_ptr<BatchReader>>(size_t)>;
+
+    static constexpr size_t kPreparationWindow = kReaderBuildMaxParallelNum;
+
+    /// Builds the initial window concurrently and refills it as readers reach 
EOF. Later
+    /// construction failures are terminal read errors when the failed reader 
is reached.
+    static Result<std::unique_ptr<ConcatBatchReader>> CreateLazy(
+        size_t reader_count, ReaderFactory factory,
+        const std::shared_ptr<arrow::MemoryPool>& arrow_pool,
+        const std::shared_ptr<Executor>& build_executor = 
GetReaderBuildExecutor());
+
     ConcatBatchReader(std::vector<std::unique_ptr<BatchReader>>&& readers,
                       const std::shared_ptr<arrow::MemoryPool>& arrow_pool);
+    ~ConcatBatchReader() override;
 
     Result<ReadBatch> NextBatch() override;
     Result<ReadBatchWithBitmap> NextBatchWithBitmap() override;
@@ -50,17 +71,31 @@ class ConcatBatchReader : public BatchReader {
     /// overlapped I/O and extra memory.
     static constexpr size_t kWarmupLookahead = 1;
 
-    /// Warms up \p count readers starting at \p idx, stopping at the end of 
the list. Warmup() is
-    /// idempotent, so this is safe to call on every batch.
-    void WarmupRange(size_t idx, size_t count);
-    void CloseAndReleaseReader(size_t reader_index);
+    struct ReaderState {
+        std::unique_ptr<BatchReader> reader;
+        FileBatchReader* file_reader = nullptr;
+        std::future<Result<std::unique_ptr<BatchReader>>> future;
+        Status failure;
+    };
+
+    struct Preparation {
+        explicit Preparation(ReaderFactory factory) : 
factory(std::move(factory)) {}
+        ReaderFactory factory;
+        std::atomic<bool> cancelled = false;
+    };
+
+    void FillWindow();
+    Status ResolveReader(ReaderState* state, bool wait);
+    void WarmupRange();
+    void CloseAndReleaseReader(ReaderState* state);
 
     std::shared_ptr<arrow::MemoryPool> arrow_pool_;
     std::shared_ptr<Metrics> finished_reader_metrics_;
-    std::vector<std::unique_ptr<BatchReader>> readers_;
-    // readers_[i] as a FileBatchReader, or nullptr when it is not one or has 
been released.
-    // Warmup() lives on FileBatchReader, so the cast is done once instead of 
on every batch.
-    std::vector<FileBatchReader*> file_readers_;
-    size_t current_;
+    std::deque<ReaderState> readers_;
+    std::shared_ptr<Preparation> preparation_;
+    std::shared_ptr<Executor> build_executor_;
+    size_t reader_count_ = 0;
+    size_t next_reader_ = 0;
+    Status failure_;
 };
 }  // namespace paimon
diff --git a/src/paimon/common/reader/concat_batch_reader_test.cpp 
b/src/paimon/common/reader/concat_batch_reader_test.cpp
index 4d3ff81c..8be11e3d 100644
--- a/src/paimon/common/reader/concat_batch_reader_test.cpp
+++ b/src/paimon/common/reader/concat_batch_reader_test.cpp
@@ -20,7 +20,12 @@
 #include "paimon/common/reader/concat_batch_reader.h"
 
 #include <algorithm>
+#include <atomic>
+#include <chrono>
 #include <cstdint>
+#include <functional>
+#include <future>
+#include <new>
 #include <string>
 #include <utility>
 
@@ -28,10 +33,13 @@
 #include "arrow/array/array_base.h"
 #include "arrow/array/array_nested.h"
 #include "arrow/ipc/json_simple.h"
+#include "fmt/format.h"
 #include "gtest/gtest.h"
+#include "paimon/common/executor/future.h"
 #include "paimon/common/metrics/metrics_impl.h"
 #include "paimon/common/reader/reader_utils.h"
 #include "paimon/common/utils/arrow/mem_utils.h"
+#include "paimon/common/utils/scope_guard.h"
 #include "paimon/memory/memory_pool.h"
 #include "paimon/status.h"
 #include "paimon/testing/mock/mock_file_batch_reader.h"
@@ -99,6 +107,67 @@ class FixedMetricsBatchReader : public BatchReader {
 };
 
 namespace {
+struct ReaderCounts {
+    std::atomic<size_t> created = 0;
+    std::atomic<size_t> live = 0;
+    std::atomic<size_t> peak = 0;
+    std::atomic<size_t> closed = 0;
+};
+
+class CountedFileBatchReader : public MockFileBatchReader {
+ public:
+    CountedFileBatchReader(const std::shared_ptr<arrow::Array>& data,
+                           const std::shared_ptr<ReaderCounts>& counts, bool 
empty_bitmap = false)
+        : MockFileBatchReader(data, data->type(), 2), counts_(counts), 
empty_bitmap_(empty_bitmap) {
+        EnableRandomizeBatchSize(false);
+        counts_->created++;
+        const size_t live = ++counts_->live;
+        size_t peak = counts_->peak.load();
+        while (peak < live && !counts_->peak.compare_exchange_weak(peak, 
live)) {
+        }
+    }
+
+    ~CountedFileBatchReader() override {
+        counts_->live--;
+    }
+
+    void SelectSecondRow() {
+        bitmap_ = RoaringBitmap32::From({1});
+    }
+
+    void SetCloseCallback(std::function<void()> callback) {
+        close_callback_ = std::move(callback);
+    }
+
+    Result<ReadBatchWithBitmap> NextBatchWithBitmap() override {
+        PAIMON_ASSIGN_OR_RAISE(ReadBatchWithBitmap batch,
+                               MockFileBatchReader::NextBatchWithBitmap());
+        if (empty_bitmap_) {
+            batch.second = RoaringBitmap32();
+        }
+        return batch;
+    }
+
+    void Close() override {
+        counts_->closed++;
+        if (close_callback_) {
+            close_callback_();
+        }
+    }
+
+ private:
+    std::shared_ptr<ReaderCounts> counts_;
+    bool empty_bitmap_;
+    std::function<void()> close_callback_;
+};
+
+std::shared_ptr<arrow::Array> ReaderData(size_t index) {
+    return arrow::ipc::internal::json::ArrayFromJSON(
+               arrow::struct_({arrow::field("f1", arrow::int32())}),
+               fmt::format("[[{}], [{}]]", index * 2, index * 2 + 1))
+        .ValueOrDie();
+}
+
 /// Logs its Warmup() and NextBatchWithBitmap() calls as "warmup <name>" and 
"read <name>" into a
 /// log the test owns, because ConcatBatchReader destroys a child once it 
reaches EOF and the
 /// child's own state goes with it.
@@ -158,33 +227,50 @@ class ConcatBatchReaderTest : public ::testing::Test {
     void CheckResult(const std::vector<std::pair<std::string, 
std::vector<int32_t>>>& batches,
                      const std::string& expected) {
         for (const auto& batch_size : {1, 2, 4, 8}) {
-            std::vector<std::unique_ptr<BatchReader>> readers;
-            for (const auto& [batch_str, bitmap_data] : batches) {
+            SCOPED_TRACE(batch_size);
+            ConcatBatchReader::ReaderFactory factory =
+                [batches, batch_size](size_t index) -> 
Result<std::unique_ptr<BatchReader>> {
+                const auto& [batch_str, bitmap_data] = batches[index];
                 auto f1 = 
arrow::ipc::internal::json::ArrayFromJSON(arrow::int32(), batch_str)
                               .ValueOrDie();
                 std::shared_ptr<arrow::Array> data =
                     arrow::StructArray::Make({f1}, {arrow::field("f1", 
arrow::int32())})
                         .ValueOrDie();
-                auto reader = std::make_unique<MockFileBatchReader>(
+                return std::make_unique<MockFileBatchReader>(
                     data, data->type(), RoaringBitmap32::From(bitmap_data), 
batch_size);
-                readers.push_back(std::move(reader));
-            }
-            auto concat_reader =
-                std::make_unique<ConcatBatchReader>(std::move(readers), 
GetArrowPool(pool_));
-            ASSERT_OK_AND_ASSIGN(auto result_chunk_array,
-                                 
ReadResultCollector::CollectResult(std::move(concat_reader)));
-            if (expected.empty()) {
-                ASSERT_FALSE(result_chunk_array);
-                return;
+            };
+            for (bool lazy : {false, true}) {
+                SCOPED_TRACE(lazy);
+                std::unique_ptr<ConcatBatchReader> concat_reader;
+                if (lazy) {
+                    ASSERT_OK_AND_ASSIGN(concat_reader,
+                                         
ConcatBatchReader::CreateLazy(batches.size(), factory,
+                                                                       
GetArrowPool(pool_)));
+                } else {
+                    std::vector<std::unique_ptr<BatchReader>> readers;
+                    for (size_t i = 0; i < batches.size(); i++) {
+                        ASSERT_OK_AND_ASSIGN(auto reader, factory(i));
+                        readers.push_back(std::move(reader));
+                    }
+                    concat_reader = 
std::make_unique<ConcatBatchReader>(std::move(readers),
+                                                                        
GetArrowPool(pool_));
+                }
+                ASSERT_OK_AND_ASSIGN(auto result_chunk_array,
+                                     
ReadResultCollector::CollectResult(std::move(concat_reader)));
+                if (expected.empty()) {
+                    ASSERT_FALSE(result_chunk_array);
+                    continue;
+                }
+                auto expected_f1 =
+                    arrow::ipc::internal::json::ArrayFromJSON(arrow::int32(), 
expected)
+                        .ValueOrDie();
+                std::shared_ptr<arrow::Array> expected_array =
+                    arrow::StructArray::Make({expected_f1}, 
{arrow::field("f1", arrow::int32())})
+                        .ValueOrDie();
+                auto expected_chunk_array = 
std::make_shared<arrow::ChunkedArray>(expected_array);
+                ASSERT_TRUE(expected_chunk_array->Equals(result_chunk_array))
+                    << result_chunk_array->ToString();
             }
-            auto expected_f1 =
-                arrow::ipc::internal::json::ArrayFromJSON(arrow::int32(), 
expected).ValueOrDie();
-            std::shared_ptr<arrow::Array> expected_array =
-                arrow::StructArray::Make({expected_f1}, {arrow::field("f1", 
arrow::int32())})
-                    .ValueOrDie();
-            auto expected_chunk_array = 
std::make_shared<arrow::ChunkedArray>(expected_array);
-            ASSERT_TRUE(expected_chunk_array->Equals(result_chunk_array))
-                << result_chunk_array->ToString();
         }
     }
 
@@ -321,36 +407,67 @@ TEST_F(ConcatBatchReaderTest, 
TestCollectMetricsAfterReleasingReaders) {
 }
 
 TEST_F(ConcatBatchReaderTest, TestWarmupLooksOneReaderAhead) {
-    std::shared_ptr<arrow::DataType> type = arrow::struct_({arrow::field("f1", 
arrow::int32())});
-    std::vector<std::string> events;
-    std::vector<std::unique_ptr<BatchReader>> readers;
-    readers.push_back(std::make_unique<EventLoggingFileBatchReader>(
-        arrow::ipc::internal::json::ArrayFromJSON(type, "[[1], 
[2]]").ValueOrDie(), "a", &events));
-    readers.push_back(std::make_unique<EventLoggingFileBatchReader>(
-        arrow::ipc::internal::json::ArrayFromJSON(type, "[[3]]").ValueOrDie(), 
"b", &events));
-    readers.push_back(std::make_unique<EventLoggingFileBatchReader>(
-        arrow::ipc::internal::json::ArrayFromJSON(type, "[[4]]").ValueOrDie(), 
"c", &events));
-    ConcatBatchReader reader(std::move(readers), GetArrowPool(pool_));
-    ASSERT_TRUE(events.empty());
-
-    ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatchWithBitmap batch, 
reader.NextBatchWithBitmap());
-    ASSERT_FALSE(BatchReader::IsEofBatch(batch));
-    ReaderUtils::ReleaseReadBatch(std::move(batch.first));
-    ASSERT_LT(FirstEvent(events, "warmup a"), FirstEvent(events, "read a"));
-    ASSERT_LT(FirstEvent(events, "warmup b"), FirstEvent(events, "read a"));
-    ASSERT_EQ(CountEvent(events, "warmup c"), 0);
-
-    // Still inside the first file, so the lookahead does not move on.
-    ASSERT_OK_AND_ASSIGN(batch, reader.NextBatchWithBitmap());
-    ASSERT_FALSE(BatchReader::IsEofBatch(batch));
-    ReaderUtils::ReleaseReadBatch(std::move(batch.first));
-    ASSERT_EQ(CountEvent(events, "warmup c"), 0);
-
-    ASSERT_OK_AND_ASSIGN(batch, reader.NextBatchWithBitmap());
-    ASSERT_FALSE(BatchReader::IsEofBatch(batch));
-    ReaderUtils::ReleaseReadBatch(std::move(batch.first));
-    ASSERT_LT(FirstEvent(events, "warmup c"), FirstEvent(events, "read b"));
-    reader.Close();
+    for (bool lazy : {false, true}) {
+        SCOPED_TRACE(lazy);
+        std::vector<std::string> events;
+        std::vector<std::shared_ptr<arrow::Array>> data;
+        for (size_t i = 0; i < ConcatBatchReader::kPreparationWindow + 2; i++) 
{
+            data.push_back(ReaderData(i)->Slice(0, i == 0 ? 2 : 1));
+        }
+        ConcatBatchReader::ReaderFactory factory =
+            [&data, &events](size_t index) -> 
Result<std::unique_ptr<BatchReader>> {
+            return std::make_unique<EventLoggingFileBatchReader>(
+                data[index], std::string(1, 'a' + index), &events);
+        };
+        ASSERT_OK_AND_ASSIGN(std::shared_ptr<Executor> executor, 
CreateDefaultExecutor(1));
+        std::unique_ptr<ConcatBatchReader> reader;
+        if (lazy) {
+            ASSERT_OK_AND_ASSIGN(reader, ConcatBatchReader::CreateLazy(
+                                             data.size(), factory, 
GetArrowPool(pool_), executor));
+        } else {
+            std::vector<std::unique_ptr<BatchReader>> readers;
+            for (size_t i = 0; i < data.size(); i++) {
+                ASSERT_OK_AND_ASSIGN(auto child, factory(i));
+                readers.push_back(std::move(child));
+            }
+            reader = std::make_unique<ConcatBatchReader>(std::move(readers), 
GetArrowPool(pool_));
+        }
+        ASSERT_TRUE(events.empty());
+
+        ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatchWithBitmap batch, 
reader->NextBatchWithBitmap());
+        ASSERT_FALSE(BatchReader::IsEofBatch(batch));
+        ReaderUtils::ReleaseReadBatch(std::move(batch.first));
+        ASSERT_LT(FirstEvent(events, "warmup a"), FirstEvent(events, "read 
a"));
+        ASSERT_LT(FirstEvent(events, "warmup b"), FirstEvent(events, "read 
a"));
+        ASSERT_EQ(CountEvent(events, "warmup c"), 0);
+
+        ASSERT_OK_AND_ASSIGN(batch, reader->NextBatchWithBitmap());
+        ASSERT_FALSE(BatchReader::IsEofBatch(batch));
+        ReaderUtils::ReleaseReadBatch(std::move(batch.first));
+        ASSERT_EQ(CountEvent(events, "warmup c"), 0);
+
+        ASSERT_OK_AND_ASSIGN(batch, reader->NextBatchWithBitmap());
+        ASSERT_FALSE(BatchReader::IsEofBatch(batch));
+        ReaderUtils::ReleaseReadBatch(std::move(batch.first));
+        ASSERT_LT(FirstEvent(events, "warmup c"), FirstEvent(events, "read 
b"));
+        for (size_t i = 2; i < data.size(); i++) {
+            ASSERT_EQ(Via(executor.get(), []() 
{}).wait_for(std::chrono::seconds(5)),
+                      std::future_status::ready);
+            ASSERT_OK_AND_ASSIGN(batch, reader->NextBatchWithBitmap());
+            ASSERT_FALSE(BatchReader::IsEofBatch(batch));
+            ReaderUtils::ReleaseReadBatch(std::move(batch.first));
+            if (i + 1 < data.size()) {
+                ASSERT_LT(FirstEvent(events, "warmup " + std::string(1, 'a' + 
i + 1)),
+                          FirstEvent(events, "read " + std::string(1, 'a' + 
i)));
+            }
+            if (i + 2 < data.size()) {
+                ASSERT_EQ(CountEvent(events, "warmup " + std::string(1, 'a' + 
i + 2)), 0);
+            }
+        }
+        ASSERT_OK_AND_ASSIGN(batch, reader->NextBatchWithBitmap());
+        ASSERT_TRUE(BatchReader::IsEofBatch(batch));
+        reader->Close();
+    }
 }
 
 TEST_F(ConcatBatchReaderTest, TestWarmupSkipsReaderThatIsNotFileBatchReader) {
@@ -385,4 +502,302 @@ TEST_F(ConcatBatchReaderTest, 
TestWarmupSkipsReaderThatIsNotFileBatchReader) {
     ASSERT_LT(FirstEvent(events, "warmup c"), FirstEvent(events, "read b"));
 }
 
+TEST_F(ConcatBatchReaderTest, LazyWindowPreservesOrderBitmapAndMetrics) {
+    constexpr size_t kFiles = 31;
+    auto counts = std::make_shared<ReaderCounts>();
+    auto lifetime = std::make_shared<int32_t>(0);
+    std::weak_ptr<int32_t> weak_lifetime = lifetime;
+    ConcatBatchReader::ReaderFactory factory =
+        [counts, lifetime](size_t index) -> 
Result<std::unique_ptr<BatchReader>> {
+        if (index % 5 == 0) {
+            return std::unique_ptr<BatchReader>();
+        }
+        auto data = ReaderData(index);
+        if (index % 5 == 1) {
+            data = data->Slice(0, 0);
+        }
+        auto reader = std::make_unique<CountedFileBatchReader>(data, counts);
+        reader->SelectSecondRow();
+        return reader;
+    };
+    ASSERT_OK_AND_ASSIGN(auto reader, ConcatBatchReader::CreateLazy(kFiles, 
std::move(factory),
+                                                                    
GetArrowPool(pool_)));
+    lifetime.reset();
+    ASSERT_LE(counts->created.load(), ConcatBatchReader::kPreparationWindow);
+    uint64_t expected_rows = 0;
+    for (size_t i = 0; i < kFiles; i++) {
+        if (i % 5 <= 1) {
+            continue;
+        }
+        auto expected = ReaderData(i);
+        std::shared_ptr<arrow::Array> actual;
+        if (i % 2 == 0) {
+            ASSERT_OK_AND_ASSIGN(auto batch, reader->NextBatchWithBitmap());
+            ASSERT_OK_AND_ASSIGN(actual, 
ReadResultCollector::GetArray(std::move(batch.first)));
+            ASSERT_EQ(batch.second, RoaringBitmap32::From({1}));
+        } else {
+            ASSERT_OK_AND_ASSIGN(auto batch, reader->NextBatch());
+            ASSERT_OK_AND_ASSIGN(actual, 
ReadResultCollector::GetArray(std::move(batch)));
+            expected = expected->Slice(1, 1);
+        }
+        ASSERT_NE(actual, nullptr);
+        ASSERT_TRUE(actual->Equals(expected)) << actual->ToString();
+        expected_rows += 2;
+    }
+    ASSERT_OK_AND_ASSIGN(auto eof, reader->NextBatch());
+    ASSERT_TRUE(BatchReader::IsEofBatch(eof));
+    ASSERT_LE(counts->peak.load(), ConcatBatchReader::kPreparationWindow);
+    ASSERT_EQ(counts->live.load(), 0);
+    ASSERT_EQ(counts->closed.load(), counts->created.load());
+    ASSERT_TRUE(weak_lifetime.expired());
+    ASSERT_OK_AND_ASSIGN(auto rows, 
reader->GetReaderMetrics()->GetCounter("mock.number.of.rows"));
+    ASSERT_EQ(rows, expected_rows);
+    reader->Close();
+    ASSERT_OK_AND_ASSIGN(auto closed_rows,
+                         
reader->GetReaderMetrics()->GetCounter("mock.number.of.rows"));
+    ASSERT_EQ(rows, closed_rows);
+    ASSERT_OK_AND_ASSIGN(auto eof_with_bitmap, reader->NextBatchWithBitmap());
+    ASSERT_TRUE(BatchReader::IsEofBatch(eof_with_bitmap));
+}
+
+TEST_F(ConcatBatchReaderTest, LazyEarlyCloseAndDestructionReleaseCreators) {
+    for (bool explicit_close : {false, true}) {
+        auto counts = std::make_shared<ReaderCounts>();
+        auto lifetime = std::make_shared<int32_t>(0);
+        std::weak_ptr<int32_t> weak_lifetime = lifetime;
+        ASSERT_OK_AND_ASSIGN(
+            auto reader,
+            ConcatBatchReader::CreateLazy(
+                100,
+                [counts, lifetime](size_t index) -> 
Result<std::unique_ptr<BatchReader>> {
+                    return 
std::make_unique<CountedFileBatchReader>(ReaderData(index), counts);
+                },
+                GetArrowPool(pool_)));
+        lifetime.reset();
+        ASSERT_EQ(counts->created.load(), 
ConcatBatchReader::kPreparationWindow);
+        ASSERT_OK_AND_ASSIGN(auto batch, reader->NextBatchWithBitmap());
+        ASSERT_FALSE(BatchReader::IsEofBatch(batch));
+        ASSERT_EQ(counts->created.load(), 
ConcatBatchReader::kPreparationWindow);
+        if (explicit_close) {
+            reader->Close();
+            reader->Close();
+            ASSERT_OK_AND_ASSIGN(auto eof, reader->NextBatchWithBitmap());
+            ASSERT_TRUE(BatchReader::IsEofBatch(eof));
+            ASSERT_EQ(counts->created.load(), 
ConcatBatchReader::kPreparationWindow);
+        } else {
+            reader.reset();
+        }
+        ASSERT_EQ(counts->live.load(), 0);
+        ASSERT_EQ(counts->closed.load(), 
ConcatBatchReader::kPreparationWindow);
+        ASSERT_TRUE(weak_lifetime.expired());
+        auto array = arrow::ImportArray(batch.first.first.get(), 
batch.first.second.get());
+        ASSERT_TRUE(array.ok()) << array.status().ToString();
+        ASSERT_TRUE(array.ValueOrDie()->Equals(ReaderData(0)));
+    }
+}
+
+TEST_F(ConcatBatchReaderTest, 
LazyEarlyCloseAndDestructionDrainExceptionalBuilds) {
+    for (bool explicit_close : {true, false}) {
+        SCOPED_TRACE(explicit_close);
+        auto counts = std::make_shared<ReaderCounts>();
+        auto lifetime = std::make_shared<int32_t>(0);
+        std::weak_ptr<int32_t> weak_lifetime = lifetime;
+        ASSERT_OK_AND_ASSIGN(std::shared_ptr<Executor> executor, 
CreateDefaultExecutor(1));
+        std::promise<void> release;
+        auto gate = release.get_future().share();
+        ASSERT_OK_AND_ASSIGN(
+            auto reader,
+            ConcatBatchReader::CreateLazy(
+                100,
+                [counts, lifetime, gate](size_t index) -> 
Result<std::unique_ptr<BatchReader>> {
+                    if (index == ConcatBatchReader::kPreparationWindow) {
+                        gate.wait();
+                        throw std::bad_alloc();
+                    }
+                    return 
std::make_unique<CountedFileBatchReader>(ReaderData(index), counts);
+                },
+                GetArrowPool(pool_), executor));
+        ScopeGuard unblock([&release]() { release.set_value(); });
+        lifetime.reset();
+        // Keep the failed build pending until we stop reading, so cleanup 
consumes its exception.
+        for (size_t i = 0; i < ConcatBatchReader::kPreparationWindow; i++) {
+            ASSERT_OK_AND_ASSIGN(auto batch, reader->NextBatch());
+            ASSERT_FALSE(BatchReader::IsEofBatch(batch));
+            ReaderUtils::ReleaseReadBatch(std::move(batch));
+        }
+        release.set_value();
+        unblock.Release();
+        // The single worker completes the failed build and the successful 
builds after it.
+        ASSERT_EQ(Via(executor.get(), []() 
{}).wait_for(std::chrono::seconds(5)),
+                  std::future_status::ready);
+        const size_t created = 2 * ConcatBatchReader::kPreparationWindow - 2;
+        ASSERT_EQ(counts->created.load(), created);
+        ASSERT_EQ(counts->live.load(), ConcatBatchReader::kPreparationWindow - 
1);
+        ASSERT_FALSE(weak_lifetime.expired());
+        if (explicit_close) {
+            ASSERT_NO_THROW(reader->Close());
+            ASSERT_NO_THROW(reader->Close());
+        } else {
+            reader.reset();
+        }
+        ASSERT_EQ(counts->created.load(), created);
+        ASSERT_EQ(counts->closed.load(), created);
+        ASSERT_EQ(counts->live.load(), 0);
+        ASSERT_TRUE(weak_lifetime.expired());
+    }
+}
+
+TEST_F(ConcatBatchReaderTest, LazyInitialFailureDrainsPartialWindow) {
+    auto counts = std::make_shared<ReaderCounts>();
+    ASSERT_OK_AND_ASSIGN(std::shared_ptr<Executor> executor, 
CreateDefaultExecutor(2));
+    auto lifetime = std::make_shared<int32_t>(0);
+    std::weak_ptr<int32_t> weak_lifetime = lifetime;
+    std::promise<void> started;
+    auto started_future = started.get_future().share();
+    std::promise<void> release;
+    auto gate = release.get_future().share();
+    std::promise<void> closing;
+    auto closing_future = closing.get_future();
+    ConcatBatchReader::ReaderFactory factory =
+        [counts, lifetime, started_future, gate, &started,
+         &closing](size_t index) -> Result<std::unique_ptr<BatchReader>> {
+        if (index == 1) {
+            if (started_future.wait_for(std::chrono::seconds(5)) != 
std::future_status::ready) {
+                return Status::IOError("initial builds did not overlap");
+            }
+            return Status::IOError("initial build failed");
+        }
+        if (index == 2) {
+            started.set_value();
+            gate.wait();
+        }
+        auto child = 
std::make_unique<CountedFileBatchReader>(ReaderData(index), counts);
+        if (index == 0) {
+            child->SetCloseCallback([&closing]() { closing.set_value(); });
+        }
+        return child;
+    };
+    auto create = std::async(std::launch::async, [&, factory = 
std::move(factory)]() mutable {
+        return ConcatBatchReader::CreateLazy(100, std::move(factory), 
GetArrowPool(pool_),
+                                             executor);
+    });
+    ScopeGuard unblock([&release]() { release.set_value(); });
+    lifetime.reset();
+    ASSERT_EQ(closing_future.wait_for(std::chrono::seconds(5)), 
std::future_status::ready);
+    EXPECT_EQ(create.wait_for(std::chrono::seconds(0)), 
std::future_status::timeout);
+    EXPECT_FALSE(weak_lifetime.expired());
+    release.set_value();
+    unblock.Release();
+    ASSERT_NOK_WITH_MSG(create.get(), "initial build failed");
+    ASSERT_EQ(counts->live.load(), 0);
+    ASSERT_EQ(counts->created.load(), counts->closed.load());
+    ASSERT_LT(counts->created.load(), ConcatBatchReader::kPreparationWindow);
+    ASSERT_TRUE(weak_lifetime.expired());
+}
+
+TEST_F(ConcatBatchReaderTest, LazyDeferredFailuresAreTerminal) {
+    enum class Failure { Build, Read, Bitmap };
+    for (Failure failure : {Failure::Build, Failure::Read, Failure::Bitmap}) {
+        SCOPED_TRACE(static_cast<int32_t>(failure));
+        auto counts = std::make_shared<ReaderCounts>();
+        ASSERT_OK_AND_ASSIGN(
+            auto reader,
+            ConcatBatchReader::CreateLazy(
+                100,
+                [counts, failure](size_t index) -> 
Result<std::unique_ptr<BatchReader>> {
+                    const bool fail = index == 
ConcatBatchReader::kPreparationWindow;
+                    if (fail && failure == Failure::Build) {
+                        return Status::IOError("deferred failure");
+                    }
+                    auto child = std::make_unique<CountedFileBatchReader>(
+                        ReaderData(index), counts, fail && failure == 
Failure::Bitmap);
+                    if (fail && failure == Failure::Read) {
+                        child->SetNextBatchStatus(Status::IOError("deferred 
failure"));
+                    }
+                    return child;
+                },
+                GetArrowPool(pool_)));
+        for (size_t i = 0; i < ConcatBatchReader::kPreparationWindow; i++) {
+            ASSERT_OK_AND_ASSIGN(auto batch, reader->NextBatch());
+            ASSERT_FALSE(BatchReader::IsEofBatch(batch));
+            ReaderUtils::ReleaseReadBatch(std::move(batch));
+        }
+        const std::string message =
+            failure == Failure::Bitmap ? "at least one valid row" : "deferred 
failure";
+        ASSERT_NOK_WITH_MSG(reader->NextBatch(), message);
+        const size_t created = counts->created.load();
+        ASSERT_NOK_WITH_MSG(reader->NextBatchWithBitmap(), message);
+        ASSERT_NOK_WITH_MSG(reader->NextBatch(), message);
+        reader->Close();
+        ASSERT_EQ(counts->created.load(), created);
+        ASSERT_EQ(counts->live.load(), 0);
+        ASSERT_EQ(counts->closed.load(), created);
+        ASSERT_LE(counts->peak.load(), ConcatBatchReader::kPreparationWindow);
+    }
+}
+
+TEST_F(ConcatBatchReaderTest, 
LazyRefillOverlapsReadingAndCloseCancelsQueuedBuilds) {
+    for (bool explicit_close : {false, true}) {
+        SCOPED_TRACE(explicit_close);
+        auto counts = std::make_shared<ReaderCounts>();
+        ASSERT_OK_AND_ASSIGN(std::shared_ptr<Executor> executor, 
CreateDefaultExecutor(1));
+        std::promise<void> started;
+        auto started_future = started.get_future();
+        std::promise<void> release;
+        auto gate = release.get_future().share();
+        std::promise<void> closing;
+        auto closing_future = closing.get_future();
+        ConcatBatchReader::ReaderFactory factory =
+            [counts, gate, &started,
+             &closing](size_t index) -> Result<std::unique_ptr<BatchReader>> {
+            if (index == ConcatBatchReader::kPreparationWindow) {
+                started.set_value();
+                gate.wait();
+            }
+            auto child = 
std::make_unique<CountedFileBatchReader>(ReaderData(index), counts);
+            if (index == ConcatBatchReader::kPreparationWindow - 1) {
+                child->SetCloseCallback([&closing]() { closing.set_value(); });
+            }
+            return child;
+        };
+        ASSERT_OK_AND_ASSIGN(
+            auto reader,
+            ConcatBatchReader::CreateLazy(100, std::move(factory), 
GetArrowPool(pool_), executor));
+        ScopeGuard unblock([&release]() { release.set_value(); });
+        auto read = std::async(std::launch::async, [&reader]() -> Status {
+            for (size_t i = 0; i < ConcatBatchReader::kPreparationWindow; i++) 
{
+                PAIMON_ASSIGN_OR_RAISE(BatchReader::ReadBatch batch, 
reader->NextBatch());
+                ReaderUtils::ReleaseReadBatch(std::move(batch));
+            }
+            return Status::OK();
+        });
+        EXPECT_EQ(read.wait_for(std::chrono::seconds(5)), 
std::future_status::ready);
+        if (read.wait_for(std::chrono::seconds(0)) != 
std::future_status::ready) {
+            release.set_value();
+            unblock.Release();
+            ASSERT_OK(read.get());
+            return;
+        }
+        ASSERT_OK(read.get());
+        ASSERT_EQ(started_future.wait_for(std::chrono::seconds(5)), 
std::future_status::ready);
+        auto close = std::async(std::launch::async, [&reader, 
explicit_close]() {
+            if (explicit_close) {
+                reader->Close();
+            } else {
+                reader.reset();
+            }
+        });
+        const auto closing_status = 
closing_future.wait_for(std::chrono::seconds(5));
+        const auto close_status = close.wait_for(std::chrono::seconds(0));
+        release.set_value();
+        unblock.Release();
+        close.get();
+        ASSERT_EQ(closing_status, std::future_status::ready);
+        ASSERT_EQ(close_status, std::future_status::timeout);
+        ASSERT_EQ(counts->created.load(), 
ConcatBatchReader::kPreparationWindow + 1);
+        ASSERT_EQ(counts->closed.load(), counts->created.load());
+        ASSERT_EQ(counts->live.load(), 0);
+    }
+}
+
 }  // namespace paimon::test
diff --git a/src/paimon/core/operation/raw_file_split_read.cpp 
b/src/paimon/core/operation/raw_file_split_read.cpp
index 659709bb..3bd92c34 100644
--- a/src/paimon/core/operation/raw_file_split_read.cpp
+++ b/src/paimon/core/operation/raw_file_split_read.cpp
@@ -26,7 +26,7 @@
 #include "fmt/format.h"
 #include "paimon/common/reader/complete_row_kind_batch_reader.h"
 #include "paimon/common/reader/concat_batch_reader.h"
-#include "paimon/common/utils/object_utils.h"
+#include "paimon/common/types/data_field.h"
 #include "paimon/core/core_options.h"
 #include "paimon/core/deletionvectors/deletion_vector.h"
 #include "paimon/core/global_index/indexed_split_impl.h"
@@ -59,6 +59,9 @@ Status ValidateFileLocalRowRanges(const 
std::vector<std::shared_ptr<DataFileMeta
         return Status::Invalid("file-local row ranges require exactly one data 
file");
     }
     const auto& file = data_files.front();
+    if (!file) {
+        return Status::Invalid("raw data file is null");
+    }
     for (const Range& range : local_row_ranges.value()) {
         if (range.from < 0 || range.to < range.from || range.to >= 
file->row_count ||
             range.to >= std::numeric_limits<int32_t>::max()) {
@@ -149,16 +152,36 @@ Result<std::unique_ptr<BatchReader>> 
RawFileSplitRead::CreateReaderWithPredicate
     PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<DataFilePathFactory> 
data_file_path_factory,
                            path_factory_->CreateDataFilePathFactory(partition, 
bucket));
 
+    if (!data_files.empty() && !predicate_provider) {
+        return Status::Invalid("file predicate provider is null");
+    }
+    // Validate the plan before starting any asynchronous file preparation.
+    
PAIMON_RETURN_NOT_OK(DataField::ConvertArrowSchemaToDataFields(read_schema).status());
+    for (const auto& file : data_files) {
+        if (!file) {
+            return Status::Invalid("raw data file is null");
+        }
+    }
+    // The returned reader may outlive this SplitRead and all arguments of 
this call. Keep a
+    // separate creator, including its context, schema manager, file system 
and pools, alive.
+    auto owned_read = std::make_shared<RawFileSplitRead>(path_factory_, 
context_, pool_, executor_);
+    ConcatBatchReader::ReaderFactory factory =
+        [owned_read, partition = partition.Copy(pool_.get()), data_files, 
read_schema,
+         predicate_provider = std::move(predicate_provider), dv_factory, 
local_row_ranges,
+         data_file_path_factory](size_t index) -> 
Result<std::unique_ptr<BatchReader>> {
+        PAIMON_ASSIGN_OR_RAISE(
+            std::vector<std::unique_ptr<FileBatchReader>> readers,
+            owned_read->CreateRawFileReadersWithPredicateProvider(
+                partition, {data_files[index]}, read_schema, 
predicate_provider, dv_factory,
+                local_row_ranges, data_file_path_factory, 
/*extra_format_options=*/{}));
+        if (readers.empty()) {
+            return std::unique_ptr<BatchReader>();
+        }
+        return std::unique_ptr<BatchReader>(std::move(readers.front()));
+    };
     PAIMON_ASSIGN_OR_RAISE(
-        std::vector<std::unique_ptr<FileBatchReader>> raw_file_readers,
-        CreateRawFileReadersWithPredicateProvider(
-            partition, data_files, read_schema, std::move(predicate_provider), 
dv_factory,
-            local_row_ranges, data_file_path_factory, 
/*extra_format_options=*/{}));
-
-    auto raw_readers =
-        
ObjectUtils::MoveVector<std::unique_ptr<BatchReader>>(std::move(raw_file_readers));
-    auto concat_batch_reader =
-        std::make_unique<ConcatBatchReader>(std::move(raw_readers), 
arrow_pool_);
+        std::unique_ptr<ConcatBatchReader> concat_batch_reader,
+        ConcatBatchReader::CreateLazy(data_files.size(), std::move(factory), 
arrow_pool_));
     PAIMON_ASSIGN_OR_RAISE(
         std::unique_ptr<BatchReader> batch_reader,
         ApplyPredicateFilterIfNeeded(
diff --git a/src/paimon/core/operation/raw_file_split_read.h 
b/src/paimon/core/operation/raw_file_split_read.h
index 135b7cdc..1e97a45c 100644
--- a/src/paimon/core/operation/raw_file_split_read.h
+++ b/src/paimon/core/operation/raw_file_split_read.h
@@ -67,6 +67,9 @@ class RawFileSplitRead : public AbstractSplitRead {
                      const std::shared_ptr<MemoryPool>& memory_pool,
                      const std::shared_ptr<Executor>& executor);
 
+    /// Prepares a bounded window of files. Open/build failures beyond the 
initial window are
+    /// reported by NextBatch/NextBatchWithBitmap when that file is reached. 
Close cancels queued
+    /// builds and drains running work. The returned reader owns its deferred 
creation state.
     /// Also accepts an `IndexedSplit` over a single-file data split, in which 
case its row
     /// ranges narrow the read to the given file-local physical positions.
     Result<std::unique_ptr<BatchReader>> CreateReader(const 
std::shared_ptr<Split>& split) override;
@@ -93,7 +96,8 @@ class RawFileSplitRead : public AbstractSplitRead {
         const std::shared_ptr<Predicate>& predicate);
 
     /// Uses file-specific predicates for pushdown and the supplied residual 
predicate for
-    /// optional filtering after concatenation.
+    /// optional filtering after concatenation. Providers and DV factories 
must own their captures;
+    /// they may run concurrently and after CreateReader returns.
     Result<std::unique_ptr<BatchReader>> CreateReaderWithPredicateProvider(
         const BinaryRow& partition, int32_t bucket,
         const std::vector<std::shared_ptr<DataFileMeta>>& files, 
DeletionVector::Factory dv_factory,
diff --git a/src/paimon/core/operation/raw_file_split_read_test.cpp 
b/src/paimon/core/operation/raw_file_split_read_test.cpp
index f259cf12..9fa0c7f8 100644
--- a/src/paimon/core/operation/raw_file_split_read_test.cpp
+++ b/src/paimon/core/operation/raw_file_split_read_test.cpp
@@ -18,9 +18,15 @@
 
 #include "paimon/core/operation/raw_file_split_read.h"
 
+#include <chrono>
+#include <condition_variable>
+#include <future>
+#include <initializer_list>
 #include <map>
+#include <mutex>
 #include <optional>
 #include <string>
+#include <thread>
 #include <utility>
 #include <variant>
 #include <vector>
@@ -31,6 +37,8 @@
 #include "gtest/gtest.h"
 #include "paimon/common/data/binary_row.h"
 #include "paimon/common/reader/concat_batch_reader.h"
+#include "paimon/common/reader/reader_utils.h"
+#include "paimon/common/table/special_fields.h"
 #include "paimon/common/types/data_field.h"
 #include "paimon/common/utils/arrow/mem_utils.h"
 #include "paimon/common/utils/arrow/status_utils.h"
@@ -65,6 +73,7 @@
 #include "paimon/testing/mock/mock_search_file_index.h"
 #include "paimon/testing/utils/binary_row_generator.h"
 #include "paimon/testing/utils/counting_cache_test_utils.h"
+#include "paimon/testing/utils/counting_reader_file_system.h"
 #include "paimon/testing/utils/read_result_collector.h"
 #include "paimon/testing/utils/testharness.h"
 
@@ -120,6 +129,50 @@ class CacheTrackingFileIndexFactory : public 
FileIndexerFactory {
 
 class RawFileSplitReadTest : public ::testing::Test {
  protected:
+    Result<std::unique_ptr<RawFileSplitRead>> CreateCountingRead(
+        const std::shared_ptr<FileSystem>& fs, const std::vector<std::string>& 
field_names = {},
+        bool row_tracking = false) const {
+        const std::string path =
+            GetDataDir() + 
"/orc/multi_partition_append_table.db/multi_partition_append_table";
+        ReadContextBuilder builder(path);
+        builder.WithFileSystem(fs)
+            .WithMemoryPool(pool_)
+            .EnablePrefetch(false)
+            .EnablePredicateFilter(true);
+        builder.AddOption(Options::FILE_INDEX_READ_ENABLED, "false");
+        if (row_tracking) {
+            builder.AddOption(Options::ROW_TRACKING_ENABLED, "true");
+        }
+        if (!field_names.empty()) {
+            builder.SetReadFieldNames(field_names);
+        }
+        PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<ReadContext> read_context, 
builder.Finish());
+        SchemaManager schemas(fs, path);
+        PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<TableSchema> table_schema, 
schemas.ReadSchema(0));
+        auto read_options = table_schema->Options();
+        for (const auto& [key, value] : read_context->GetOptions()) {
+            read_options.insert_or_assign(key, value);
+        }
+        PAIMON_ASSIGN_OR_RAISE(
+            std::shared_ptr<InternalReadContext> context,
+            InternalReadContext::Create(read_context, table_schema, 
read_options));
+        const CoreOptions& options = context->GetCoreOptions();
+        auto schema = 
DataField::ConvertDataFieldsToArrowSchema(table_schema->Fields());
+        PAIMON_ASSIGN_OR_RAISE(std::vector<std::string> external_paths,
+                               options.CreateExternalPaths());
+        PAIMON_ASSIGN_OR_RAISE(std::vector<std::string> global_paths,
+                               options.CreateGlobalIndexExternalPaths());
+        PAIMON_ASSIGN_OR_RAISE(
+            std::shared_ptr<FileStorePathFactory> paths,
+            FileStorePathFactory::Create(
+                path, schema, table_schema->PartitionKeys(), 
options.GetPartitionDefaultName(),
+                options.GetFileFormat()->Identifier(), 
options.DataFilePrefix(),
+                options.LegacyPartitionNameEnabled(), external_paths, 
global_paths,
+                options.IndexFileInDataFileDir(), {}, pool_));
+        PAIMON_ASSIGN_OR_RAISE(std::shared_ptr<Executor> executor, 
CreateDefaultExecutor(2));
+        return std::make_unique<RawFileSplitRead>(paths, context, pool_, 
executor);
+    }
+
     std::vector<std::shared_ptr<DataSplit>> PrepareDataSplits() const {
         auto meta1 = std::make_shared<DataFileMeta>(
             "data-01b6a930-6564-409b-b8f4-ed1307790d72-0.orc", 
/*file_size=*/575, /*row_count=*/3,
@@ -314,6 +367,191 @@ class RawFileSplitReadTest : public ::testing::Test {
     std::shared_ptr<MemoryPool> pool_ = GetDefaultPool();
 };
 
+TEST_F(RawFileSplitReadTest, 
LazyInitialWindowBuildsConcurrentlyAndEarlyCloseStopsOpening) {
+    auto fs = std::make_shared<CountingReaderFileSystem>(".orc");
+    ASSERT_OK_AND_ASSIGN(auto split_read, CreateCountingRead(fs));
+    auto split = 
std::dynamic_pointer_cast<DataSplitImpl>(PrepareDataSplits().front());
+    std::vector<std::shared_ptr<DataFileMeta>> files(100, 
split->DataFiles().front());
+    std::mutex mutex;
+    std::condition_variable changed;
+    size_t started = 0;
+    bool released = false;
+    fs->before_open = [&](const std::string&) {
+        std::unique_lock<std::mutex> lock(mutex);
+        started++;
+        changed.notify_all();
+        changed.wait(lock, [&released]() { return released; });
+        return Status::OK();
+    };
+    auto open = std::async(std::launch::async, [&]() {
+        return split_read->CreateReader(split->Partition(), split->Bucket(), 
files,
+                                        DeletionVector::Factory(), 
std::nullopt);
+    });
+    auto release = [&]() {
+        std::lock_guard<std::mutex> lock(mutex);
+        released = true;
+        changed.notify_all();
+    };
+    ScopeGuard unblock(release);
+    const size_t parallel = 
std::min<size_t>(ConcatBatchReader::kPreparationWindow,
+                                             std::max(1u, 
std::thread::hardware_concurrency()));
+    {
+        std::unique_lock<std::mutex> lock(mutex);
+        ASSERT_TRUE(
+            changed.wait_for(lock, std::chrono::seconds(5), [&]() { return 
started == parallel; }));
+    }
+    ASSERT_EQ(fs->counts->opens.load(), parallel);
+    ASSERT_EQ(open.wait_for(std::chrono::seconds(0)), 
std::future_status::timeout);
+    release();
+    unblock.Release();
+    ASSERT_OK_AND_ASSIGN(auto reader, open.get());
+    ASSERT_EQ(fs->counts->opens.load(), ConcatBatchReader::kPreparationWindow);
+    split_read.reset();
+    files.clear();
+    split.reset();
+    ASSERT_OK_AND_ASSIGN(auto batch, reader->NextBatch());
+    ASSERT_FALSE(BatchReader::IsEofBatch(batch));
+    ASSERT_EQ(batch.first->length, 3);
+    ASSERT_EQ(fs->counts->opens.load(), ConcatBatchReader::kPreparationWindow);
+    reader->Close();
+    ASSERT_EQ(fs->counts->opens.load(), ConcatBatchReader::kPreparationWindow);
+    ASSERT_EQ(fs->counts->live.load(), 0);
+    ASSERT_LE(fs->counts->peak.load(), ConcatBatchReader::kPreparationWindow);
+    ReaderUtils::ReleaseReadBatch(std::move(batch));
+}
+
+TEST_F(RawFileSplitReadTest, 
LazyReaderOutlivesCreatorWithPerFilePredicatesDvAndRowIds) {
+    pool_ = GetMemoryPool();
+    std::weak_ptr<MemoryPool> weak_pool = pool_;
+    auto fs = std::make_shared<CountingReaderFileSystem>(".orc");
+    const auto counts = fs->counts;
+    std::weak_ptr<FileSystem> weak_fs = fs;
+    ASSERT_OK_AND_ASSIGN(auto split_read, CreateCountingRead(fs, {"_ROW_ID", 
"f0"}, true));
+    auto split = 
std::dynamic_pointer_cast<DataSplitImpl>(PrepareDataSplits().front());
+    std::vector<std::shared_ptr<DataFileMeta>> files;
+    constexpr size_t kFiles = 19;
+    for (size_t i = 0; i < kFiles; i++) {
+        auto file = 
std::make_shared<DataFileMeta>(*split->DataFiles().front());
+        file->first_row_id = i * 100;
+        files.push_back(std::move(file));
+    }
+    std::shared_ptr<DeletionVector> dv =
+        std::make_shared<BitmapDeletionVector>(RoaringBitmap32::From({0}));
+    DeletionVector::Factory dv_factory =
+        [dv](const std::string&) -> Result<std::shared_ptr<DeletionVector>> { 
return dv; };
+    auto schema = DataField::ConvertDataFieldsToArrowSchema(
+        {SpecialFields::RowId(), DataField(0, arrow::field("f0", 
arrow::utf8()))});
+    auto predicate =
+        PredicateBuilder::Equal(1, "f0", FieldType::STRING, 
Literal(FieldType::STRING, "Tony", 4));
+    auto predicates = 
std::make_shared<std::vector<std::shared_ptr<Predicate>>>(
+        std::initializer_list<std::shared_ptr<Predicate>>{
+            predicate, PredicateBuilder::Equal(1, "f0", FieldType::STRING,
+                                               Literal(FieldType::STRING, 
"Zoe", 3))});
+    std::weak_ptr<std::vector<std::shared_ptr<Predicate>>> weak_predicates = 
predicates;
+    RawFileSplitRead::PredicateProvider predicate_provider =
+        [predicates](
+            const std::shared_ptr<DataFileMeta>& file) -> 
Result<std::shared_ptr<Predicate>> {
+        return (*predicates)[file->first_row_id.value() / 100 % 
predicates->size()];
+    };
+    ASSERT_OK_AND_ASSIGN(auto reader, 
split_read->CreateReaderWithPredicateProvider(
+                                          split->Partition(), split->Bucket(), 
files, dv_factory,
+                                          std::nullopt, schema, predicate, 
predicate_provider));
+    split_read.reset();
+    files.clear();
+    split.reset();
+    schema.reset();
+    predicate.reset();
+    predicates.reset();
+    predicate_provider = {};
+    dv_factory = {};
+    dv.reset();
+    fs.reset();
+    pool_.reset();
+    ASSERT_FALSE(weak_fs.expired());
+    ASSERT_FALSE(weak_pool.expired());
+    ASSERT_FALSE(weak_predicates.expired());
+    ASSERT_OK_AND_ASSIGN(auto result, 
ReadResultCollector::CollectResult(reader.get()));
+    ASSERT_NE(result, nullptr);
+    ASSERT_TRUE(weak_fs.expired());
+    ASSERT_TRUE(weak_predicates.expired());
+    reader.reset();
+    ASSERT_FALSE(weak_pool.expired());
+    ASSERT_EQ(result->length(), (kFiles + 1) / 2);
+    size_t index = 0;
+    for (const auto& chunk : result->chunks()) {
+        auto rows = std::dynamic_pointer_cast<arrow::StructArray>(chunk);
+        ASSERT_NE(rows, nullptr);
+        auto kinds = 
std::dynamic_pointer_cast<arrow::Int8Array>(rows->field(0));
+        auto ids = 
std::dynamic_pointer_cast<arrow::Int64Array>(rows->field(1));
+        auto names = 
std::dynamic_pointer_cast<arrow::StringArray>(rows->field(2));
+        ASSERT_NE(kinds, nullptr);
+        ASSERT_NE(ids, nullptr);
+        ASSERT_NE(names, nullptr);
+        for (int64_t row = 0; row < rows->length(); row++, index++) {
+            ASSERT_EQ(kinds->Value(row), 0);
+            ASSERT_EQ(ids->Value(row), index * 200 + 2);
+            ASSERT_EQ(names->GetString(row), "Tony");
+        }
+    }
+    ASSERT_EQ(counts->opens.load(), kFiles);
+    ASSERT_LE(counts->peak.load(), ConcatBatchReader::kPreparationWindow);
+    ASSERT_EQ(counts->live.load(), 0);
+    result.reset();
+    ASSERT_TRUE(weak_pool.expired());
+}
+
+TEST_F(RawFileSplitReadTest, LazyValidationAndFileOpenFailures) {
+    auto fs = std::make_shared<CountingReaderFileSystem>(".orc");
+    ASSERT_OK_AND_ASSIGN(auto split_read, CreateCountingRead(fs));
+    auto split = 
std::dynamic_pointer_cast<DataSplitImpl>(PrepareDataSplits().front());
+    std::vector<std::shared_ptr<DataFileMeta>> files(20, 
split->DataFiles().front());
+    ASSERT_NOK_WITH_MSG(
+        split_read->CreateReader(split->Partition(), split->Bucket(), files,
+                                 DeletionVector::Factory(), std::nullopt, 
nullptr, nullptr),
+        "raw file read schema is null");
+    auto invalid_schema = arrow::schema({arrow::field("f0", arrow::utf8())});
+    ASSERT_NOK_WITH_MSG(
+        split_read->CreateReader(split->Partition(), split->Bucket(), files,
+                                 DeletionVector::Factory(), std::nullopt, 
invalid_schema, nullptr),
+        "lack of metadata of field id");
+    auto schema = DataField::ConvertDataFieldsToArrowSchema(
+        {DataField(0, arrow::field("f0", arrow::utf8()))});
+    ASSERT_NOK_WITH_MSG(split_read->CreateReaderWithPredicateProvider(
+                            split->Partition(), split->Bucket(), files, 
DeletionVector::Factory(),
+                            std::nullopt, schema, nullptr, 
/*predicate_provider=*/{}),
+                        "file predicate provider is null");
+    files.back() = nullptr;
+    ASSERT_NOK_WITH_MSG(split_read->CreateReader(split->Partition(), 
split->Bucket(), files,
+                                                 DeletionVector::Factory(), 
std::nullopt),
+                        "raw data file is null");
+    ASSERT_NOK_WITH_MSG(
+        split_read->CreateReader(split->Partition(), split->Bucket(), 
{nullptr},
+                                 DeletionVector::Factory(), 
std::vector<Range>{Range(0, 0)}),
+        "raw data file is null");
+    ASSERT_EQ(fs->counts->opens.load(), 0);
+    files.back() = files.front();
+    auto missing = std::make_shared<DataFileMeta>(*files.front());
+    missing->file_name = "missing.orc";
+    files[ConcatBatchReader::kPreparationWindow] = missing;
+    ASSERT_OK_AND_ASSIGN(auto reader,
+                         split_read->CreateReader(split->Partition(), 
split->Bucket(), files,
+                                                  DeletionVector::Factory(), 
std::nullopt));
+    ASSERT_EQ(fs->counts->opens.load(), ConcatBatchReader::kPreparationWindow);
+    for (size_t i = 0; i < ConcatBatchReader::kPreparationWindow; i++) {
+        ASSERT_OK_AND_ASSIGN(auto batch, reader->NextBatch());
+        ASSERT_FALSE(BatchReader::IsEofBatch(batch));
+        ReaderUtils::ReleaseReadBatch(std::move(batch));
+    }
+    ASSERT_NOK_WITH_MSG(reader->NextBatch(), "missing.orc");
+    ASSERT_NOK_WITH_MSG(reader->NextBatchWithBitmap(), "missing.orc");
+    ASSERT_EQ(fs->counts->live.load(), 0);
+    files.front() = missing;
+    ASSERT_NOK_WITH_MSG(split_read->CreateReader(split->Partition(), 
split->Bucket(), files,
+                                                 DeletionVector::Factory(), 
std::nullopt),
+                        "missing.orc");
+    ASSERT_EQ(fs->counts->live.load(), 0);
+}
+
 // test simple, recall all columns with sequence in data
 TEST_F(RawFileSplitReadTest, TestCreateReader) {
     std::vector<DataField> read_fields = {DataField(0, arrow::field("f0", 
arrow::utf8())),
@@ -384,11 +622,6 @@ TEST_F(RawFileSplitReadTest, 
TestCreateReaderUsesExplicitPredicate) {
         [](const std::shared_ptr<DataFileMeta>&) -> 
Result<std::shared_ptr<Predicate>> {
         return std::shared_ptr<Predicate>();
     };
-    ASSERT_NOK_WITH_MSG(
-        read.CreateReaderWithPredicateProvider(split->Partition(), 
split->Bucket(), {first, second},
-                                               dv_factory, std::nullopt, 
context->GetReadSchema(),
-                                               predicate, 
/*predicate_provider=*/{}),
-        "file predicate provider is null");
     ASSERT_NOK_WITH_MSG(
         read.CreateReaderWithPredicateProvider(split->Partition(), 
split->Bucket(), {first, second},
                                                dv_factory, 
std::vector<Range>{Range(0, 0)},
diff --git a/src/paimon/testing/utils/counting_reader_file_system.h 
b/src/paimon/testing/utils/counting_reader_file_system.h
new file mode 100644
index 00000000..e93daae7
--- /dev/null
+++ b/src/paimon/testing/utils/counting_reader_file_system.h
@@ -0,0 +1,124 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#pragma once
+
+#include <atomic>
+#include <cstdint>
+#include <functional>
+#include <memory>
+#include <string>
+#include <utility>
+
+#include "paimon/common/utils/string_utils.h"
+#include "paimon/fs/local/local_file_system.h"
+
+namespace paimon::test {
+
+class CountingReaderFileSystem : public LocalFileSystem {
+ public:
+    struct Counts {
+        std::atomic<uint64_t> opens = 0;
+        std::atomic<uint64_t> live = 0;
+        std::atomic<uint64_t> peak = 0;
+        std::atomic<uint64_t> reads = 0;
+        std::atomic<uint64_t> bytes = 0;
+    };
+
+    explicit CountingReaderFileSystem(std::string suffix) : 
suffix_(std::move(suffix)) {}
+
+    using LocalFileSystem::Open;
+
+    Result<std::unique_ptr<InputStream>> Open(const std::string& path) const 
override {
+        if (!StringUtils::EndsWith(path, suffix_)) {
+            return LocalFileSystem::Open(path);
+        }
+        counts->opens++;
+        if (before_open) {
+            PAIMON_RETURN_NOT_OK(before_open(path));
+        }
+        PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<InputStream> input, 
LocalFileSystem::Open(path));
+        return std::make_unique<CountedInput>(std::move(input), counts);
+    }
+
+    std::shared_ptr<Counts> counts = std::make_shared<Counts>();
+    std::function<Status(const std::string&)> before_open;
+
+ private:
+    class CountedInput : public InputStream {
+     public:
+        CountedInput(std::unique_ptr<InputStream> input, 
std::shared_ptr<Counts> counts)
+            : input_(std::move(input)), counts_(std::move(counts)) {
+            const uint64_t live = ++counts_->live;
+            uint64_t peak = counts_->peak.load();
+            while (peak < live && !counts_->peak.compare_exchange_weak(peak, 
live)) {
+            }
+        }
+        ~CountedInput() override {
+            counts_->live--;
+        }
+        Status Close() override {
+            return input_->Close();
+        }
+        Status Seek(int64_t offset, SeekOrigin origin) override {
+            return input_->Seek(offset, origin);
+        }
+        Result<int64_t> GetPos() const override {
+            return input_->GetPos();
+        }
+        Result<int64_t> Read(char* buffer, int64_t size) override {
+            counts_->reads++;
+            PAIMON_ASSIGN_OR_RAISE(int64_t bytes, input_->Read(buffer, size));
+            counts_->bytes += bytes;
+            return bytes;
+        }
+        Result<int64_t> Read(char* buffer, int64_t size, int64_t offset) 
override {
+            counts_->reads++;
+            PAIMON_ASSIGN_OR_RAISE(int64_t bytes, input_->Read(buffer, size, 
offset));
+            counts_->bytes += bytes;
+            return bytes;
+        }
+        void ReadAsync(char* buffer, int64_t size, int64_t offset,
+                       std::function<void(Status)>&& callback) override {
+            counts_->reads++;
+            input_->ReadAsync(
+                buffer, size, offset,
+                [counts = counts_, size, callback = 
std::move(callback)](Status status) {
+                    if (status.ok()) {
+                        counts->bytes += size;
+                    }
+                    callback(status);
+                });
+        }
+        Result<std::string> GetUri() const override {
+            return input_->GetUri();
+        }
+        Result<int64_t> Length() const override {
+            return input_->Length();
+        }
+
+     private:
+        std::unique_ptr<InputStream> input_;
+        std::shared_ptr<Counts> counts_;
+    };
+
+    std::string suffix_;
+};
+
+}  // namespace paimon::test
diff --git a/test/inte/read_inte_test.cpp b/test/inte/read_inte_test.cpp
index 1988df48..c9a0391b 100644
--- a/test/inte/read_inte_test.cpp
+++ b/test/inte/read_inte_test.cpp
@@ -414,8 +414,8 @@ TEST_P(ReadInteTest, TestAppendSimple) {
         auto split_concat_batch_reader = 
dynamic_cast<ConcatBatchReader*>(batch_reader.get());
         ASSERT_TRUE(split_concat_batch_reader);
         ASSERT_EQ(1, split_concat_batch_reader->readers_.size());
-        auto complete_batch_reader =
-            
dynamic_cast<CompleteRowKindBatchReader*>(split_concat_batch_reader->readers_[0].get());
+        auto complete_batch_reader = dynamic_cast<CompleteRowKindBatchReader*>(
+            split_concat_batch_reader->readers_[0].reader.get());
         ASSERT_TRUE(complete_batch_reader);
         auto file_concat_batch_reader =
             
dynamic_cast<ConcatBatchReader*>(complete_batch_reader->reader_.get());

Reply via email to