From ebe1e01b34628cff5c3893719e4b0af889f147d9 Mon Sep 17 00:00:00 2001 From: Nicholas Jiang Date: Tue, 17 Nov 2026 07:39:02 -0800 Subject: [PATCH] feat(reader): open splits on demand and bound file preparation Append/raw reads prepared up to four files per split. Opening every split before the first batch made the number of live readers grow with the plan, even when the caller stopped after a few rows. Deferring split opening also requires keeping planned real-time views available after their tickets are consumed. - Add the C++ read.preparation-window option (default 4) to bound prepared and in-flight file readers independently of build parallelism, batch prefetch and warmup, addressing #561. - Open append-only and primary-key splits in plan order, closing each before opening the next. Capture the delete-row policy and retain the read state so the reader can outlive its TableRead. - Validate disk split metadata and pin real-time views before consuming tickets. Rejected plans preserve valid tickets for retry. Accepted plans retain their views across expiry and committed-state refreshes. - Return deferred failures in file order and preserve status details when adding split context. Drain pending builds before collecting final metrics and release unused factories on EOF, close or error. --- docs/source/user_guide/prefetch.rst | 66 ++- include/paimon/defs.h | 5 + include/paimon/table/source/table_read.h | 11 +- src/paimon/CMakeLists.txt | 1 + src/paimon/common/defs.cpp | 1 + .../common/reader/concat_batch_reader.cpp | 46 +- .../common/reader/concat_batch_reader.h | 20 +- .../reader/concat_batch_reader_test.cpp | 486 +++++++++++------- src/paimon/core/core_options.cpp | 12 + src/paimon/core/core_options.h | 1 + src/paimon/core/core_options_test.cpp | 25 + .../core/global_index/indexed_split_impl.h | 3 + .../core/operation/raw_file_split_read.cpp | 8 +- .../operation/raw_file_split_read_test.cpp | 214 ++++---- src/paimon/core/table/file_store_table.cpp | 3 +- .../table/format/lazy_concat_batch_reader.cpp | 46 +- .../table/format/lazy_concat_batch_reader.h | 19 +- .../format/lazy_concat_batch_reader_test.cpp | 432 ++++++++++------ .../table/source/append_only_table_read.cpp | 102 ++-- .../table/source/append_only_table_read.h | 11 +- .../table/source/key_value_table_read.cpp | 108 ++-- .../core/table/source/key_value_table_read.h | 15 +- .../core/table/source/table_read_test.cpp | 203 +++++++- .../core/table/source/table_read_utils.cpp | 137 +++++ .../core/table/source/table_read_utils.h | 54 ++ test/inte/nested_column_pruning_inte_test.cpp | 164 +----- test/inte/read_inte_test.cpp | 19 +- test/inte/realtime_write_inte_test.cpp | 252 +++++++-- test/inte/scan_and_read_inte_test.cpp | 4 +- 29 files changed, 1589 insertions(+), 879 deletions(-) create mode 100644 src/paimon/core/table/source/table_read_utils.cpp create mode 100644 src/paimon/core/table/source/table_read_utils.h diff --git a/docs/source/user_guide/prefetch.rst b/docs/source/user_guide/prefetch.rst index d223d055a..1cd89f538 100644 --- a/docs/source/user_guide/prefetch.rst +++ b/docs/source/user_guide/prefetch.rst @@ -74,23 +74,60 @@ 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. +Append/raw reads construct a sliding window of data-file reader stacks per +split, including construction tasks still in flight. The C++-specific option +``read.preparation-window`` accepts a positive 32-bit integer and defaults to +``4``. This limit is independent of the reader-build executor's +parallelism, batch prefetch and warmup settings. A value of ``1`` retains only +the current reader. Larger windows let the next files prepare while the +current file is read. The dedicated reader-build executor constructs the +initial window before ``CreateReader(split)`` returns. At EOF the split reader +closes the finished file reader, records its metrics and submits the next +file for construction. 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. +The following example configures a window of eight readers per split. + +.. code-block:: cpp + + paimon::ReadContextBuilder builder(table_path); + builder.AddOption(paimon::Options::READ_PREPARATION_WINDOW, "8"); + +For ``CreateReader(split)``, plan validation still runs during creation. +Creation returns file-open and reader-build errors from the initial window. +For later files, ``NextBatch`` and ``NextBatchWithBitmap`` return the error +when reading reaches the affected file. 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. + +In the append-only and primary-key implementations, ``CreateReader(splits)`` +opens splits in input order only when they are reached. The previous split +is closed before the next is opened. A list of raw disk splits therefore +retains at most one preparation window. A real-time split can group +multiple disk inputs and open them together when it is reached. Closing +before the first batch opens no data files. +The returned reader owns its creation state and may outlive the ``TableRead`` +and the input split list. Errors opening a split or its initial files are +terminal when reading reaches that split. + +``CreateReader(splits)`` validates disk split metadata, including disk splits +inside real-time splits. It checks for missing inputs, misaligned deletion +files, and mismatches between indexed files, row ranges and scores. +Primary-key real-time merges require ordinary data splits as disk inputs. +Creation also checks each real-time split's version, partition and bucket, +offset bounds and pinned table schema before consuming any tickets. +If validation fails, valid tickets remain available for retry. +Successful creation consumes the tickets and retains the views across +ticket expiry and committed-state refreshes. Close, EOF and read errors +release the remaining creation state and retained views. + +Branch-fallback and query-authorization readers open their split lists +during creation. Warmup ====== @@ -106,7 +143,8 @@ 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. +The construction window is configured independently of the warmup level below. +With ``read.preparation-window=1``, no next-file reader is retained for warmup. How far the next file is prepared is set with ``ReadContextBuilder::SetWarmupLevel()``. Append compaction builds its own read diff --git a/include/paimon/defs.h b/include/paimon/defs.h index 16cf6382a..db94955de 100644 --- a/include/paimon/defs.h +++ b/include/paimon/defs.h @@ -247,6 +247,11 @@ struct PAIMON_EXPORT Options { /// The default value is 1024. static const char READ_BATCH_SIZE[]; + /// "read.preparation-window" - C++-specific limit on file readers per append/raw split, + /// including readers under construction. Accepts positive 32-bit integers and defaults to 4. + /// Build parallelism, batch prefetch and warmup are configured independently. + static const char READ_PREPARATION_WINDOW[]; + /// "write.batch-size" - Write batch size for any file format if it supports. /// The default value is 1024. static const char WRITE_BATCH_SIZE[]; diff --git a/include/paimon/table/source/table_read.h b/include/paimon/table/source/table_read.h index c77b323da..560c85f28 100644 --- a/include/paimon/table/source/table_read.h +++ b/include/paimon/table/source/table_read.h @@ -48,13 +48,12 @@ class PAIMON_EXPORT TableRead { /// @return A Result containing a unique pointer to the `TableRead` instance. static Result> Create(std::unique_ptr context); - /// Creates a `BatchReader` instance for reading data. + /// The append-only and primary-key implementations open one split at a time in input order. + /// Their readers may outlive this TableRead. Creation rejects null splits and validates + /// real-time views before consuming any tickets. The reader retains those views and returns + /// a terminal error if a split fails to open when reached. /// - /// This method creates a BatchReader that will be responsible for reading data from the - /// provided splits. - /// - /// @param splits A vector of shared pointers to `Split` instances representing the - /// data to be read. + /// @param splits The splits to read. /// @return A Result containing a unique pointer to the `BatchReader` instance. /// @note `BatchReader`s created by the same `TableRead` are not thread-safe for /// concurrent reading. diff --git a/src/paimon/CMakeLists.txt b/src/paimon/CMakeLists.txt index c527d8749..88a9a36f8 100644 --- a/src/paimon/CMakeLists.txt +++ b/src/paimon/CMakeLists.txt @@ -503,6 +503,7 @@ set(PAIMON_CORE_SRCS core/table/source/snapshot/snapshot_reader.cpp core/table/source/startup_mode.cpp core/table/source/table_read.cpp + core/table/source/table_read_utils.cpp core/table/source/table_scan.cpp core/table/source/data_evolution_batch_scan.cpp core/table/source/data_evolution_full_text_read.cpp diff --git a/src/paimon/common/defs.cpp b/src/paimon/common/defs.cpp index 6349967e6..9fef4fe1c 100644 --- a/src/paimon/common/defs.cpp +++ b/src/paimon/common/defs.cpp @@ -71,6 +71,7 @@ const char Options::SCAN_MANIFEST_ENTRY_LAZY_DECODE_ENABLED[] = "scan.manifest-entry.lazy-decode.enabled"; const char Options::PREFETCH_IO_METRICS_ENABLED[] = "prefetch.io-metrics.enabled"; const char Options::READ_BATCH_SIZE[] = "read.batch-size"; +const char Options::READ_PREPARATION_WINDOW[] = "read.preparation-window"; const char Options::WRITE_BATCH_SIZE[] = "write.batch-size"; const char Options::WRITE_BUFFER_SIZE[] = "write-buffer-size"; const char Options::WRITE_BUFFER_SPILLABLE[] = "write-buffer-spillable"; diff --git a/src/paimon/common/reader/concat_batch_reader.cpp b/src/paimon/common/reader/concat_batch_reader.cpp index 1607cf317..fcdfeee74 100644 --- a/src/paimon/common/reader/concat_batch_reader.cpp +++ b/src/paimon/common/reader/concat_batch_reader.cpp @@ -21,6 +21,8 @@ #include #include +#include +#include #include #include "arrow/c/abi.h" @@ -43,22 +45,35 @@ ConcatBatchReader::ConcatBatchReader(std::vector>&& } } +ConcatBatchReader::ConcatBatchReader(size_t reader_count, std::shared_ptr preparation, + size_t preparation_window, + const std::shared_ptr& arrow_pool, + const std::shared_ptr& build_executor) + : arrow_pool_(arrow_pool), + finished_reader_metrics_(std::make_shared()), + preparation_(std::move(preparation)), + build_executor_(build_executor), + preparation_window_(preparation_window), + reader_count_(reader_count) {} + ConcatBatchReader::~ConcatBatchReader() { ConcatBatchReader::Close(); } Result> ConcatBatchReader::CreateLazy( - size_t reader_count, ReaderFactory factory, + size_t reader_count, ReaderFactory factory, int32_t preparation_window, const std::shared_ptr& arrow_pool, const std::shared_ptr& build_executor) { if (!factory || !build_executor) { return Status::Invalid("lazy reader requires a factory and a build executor"); } - auto reader = std::make_unique(std::vector>(), - arrow_pool); - reader->preparation_ = std::make_shared(std::move(factory)); - reader->build_executor_ = build_executor; - reader->reader_count_ = reader_count; + if (preparation_window <= 0) { + return Status::Invalid("reader preparation window must be positive"); + } + auto preparation = std::make_shared(std::move(factory)); + auto reader = std::unique_ptr( + new ConcatBatchReader(reader_count, std::move(preparation), + static_cast(preparation_window), arrow_pool, build_executor)); reader->FillWindow(); for (auto& state : reader->readers_) { PAIMON_RETURN_NOT_OK(reader->ResolveReader(&state, /*wait=*/true)); @@ -67,7 +82,7 @@ Result> ConcatBatchReader::CreateLazy( } void ConcatBatchReader::FillWindow() { - while (next_reader_ < reader_count_ && readers_.size() < kPreparationWindow) { + while (next_reader_ < reader_count_ && readers_.size() < preparation_window_) { readers_.emplace_back(); readers_.back().future = Via(build_executor_.get(), @@ -85,8 +100,11 @@ void ConcatBatchReader::FillWindow() { } Status ConcatBatchReader::ResolveReader(ReaderState* state, bool wait) { - if (state->future.valid() && - (wait || state->future.wait_for(std::chrono::seconds(0)) == std::future_status::ready)) { + if (!state->future.valid() || + (!wait && state->future.wait_for(std::chrono::seconds(0)) != std::future_status::ready)) { + return state->failure; + } + try { auto result = state->future.get(); if (!result.ok()) { state->failure = result.status(); @@ -94,6 +112,13 @@ Status ConcatBatchReader::ResolveReader(ReaderState* state, bool wait) { state->reader = std::move(result).value(); state->file_reader = dynamic_cast(state->reader.get()); } + } catch (const std::bad_alloc&) { + state->failure = Status::OutOfMemory("out of memory preparing file reader"); + } catch (const std::exception& e) { + state->failure = Status::UnknownError("file reader preparation failed: ", e.what()); + } catch (...) { + state->failure = + Status::UnknownError("file reader preparation failed with an unknown exception"); } return state->failure; } @@ -117,8 +142,7 @@ void ConcatBatchReader::Close() { try { (void)ResolveReader(&state, /*wait=*/true); } catch (...) { - // Construction exceptions captured by Via must not escape destruction or prevent - // the remaining tasks from being drained. + // Keep draining even if recording a preparation failure runs out of memory. } CloseAndReleaseReader(&state); } diff --git a/src/paimon/common/reader/concat_batch_reader.h b/src/paimon/common/reader/concat_batch_reader.h index 4c273052c..5e5474f3b 100644 --- a/src/paimon/common/reader/concat_batch_reader.h +++ b/src/paimon/common/reader/concat_batch_reader.h @@ -21,6 +21,7 @@ #include #include +#include #include #include #include @@ -29,32 +30,33 @@ #include #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 Executor; class FileBatchReader; class MemoryPool; -/// This reader is to concatenate a list of BatchReaders and read them sequentially. The input list -/// is already sorted by key and sequence number, and the key intervals do not overlap each other. +/// Reads children in input order and releases each reader at EOF. 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>(size_t)>; - static constexpr size_t kPreparationWindow = kReaderBuildMaxParallelNum; + static constexpr size_t kDefaultPreparationWindow = 4; /// 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. + /// The positive preparation window bounds retained readers, including in-flight builds, + /// independently of the build executor's parallelism. static Result> CreateLazy( - size_t reader_count, ReaderFactory factory, + size_t reader_count, ReaderFactory factory, int32_t preparation_window, const std::shared_ptr& arrow_pool, - const std::shared_ptr& build_executor = GetReaderBuildExecutor()); + const std::shared_ptr& build_executor); ConcatBatchReader(std::vector>&& readers, const std::shared_ptr& arrow_pool); @@ -84,6 +86,11 @@ class ConcatBatchReader : public BatchReader { std::atomic cancelled = false; }; + ConcatBatchReader(size_t reader_count, std::shared_ptr preparation, + size_t preparation_window, + const std::shared_ptr& arrow_pool, + const std::shared_ptr& build_executor); + void FillWindow(); Status ResolveReader(ReaderState* state, bool wait); void WarmupRange(); @@ -94,6 +101,7 @@ class ConcatBatchReader : public BatchReader { std::deque readers_; std::shared_ptr preparation_; std::shared_ptr build_executor_; + size_t preparation_window_ = 0; size_t reader_count_ = 0; size_t next_reader_ = 0; Status failure_; diff --git a/src/paimon/common/reader/concat_batch_reader_test.cpp b/src/paimon/common/reader/concat_batch_reader_test.cpp index 8be11e3dc..0fa287919 100644 --- a/src/paimon/common/reader/concat_batch_reader_test.cpp +++ b/src/paimon/common/reader/concat_batch_reader_test.cpp @@ -26,6 +26,8 @@ #include #include #include +#include +#include #include #include @@ -36,6 +38,7 @@ #include "fmt/format.h" #include "gtest/gtest.h" #include "paimon/common/executor/future.h" +#include "paimon/common/executor/reader_build_executor.h" #include "paimon/common/metrics/metrics_impl.h" #include "paimon/common/reader/reader_utils.h" #include "paimon/common/utils/arrow/mem_utils.h" @@ -243,9 +246,11 @@ class ConcatBatchReaderTest : public ::testing::Test { SCOPED_TRACE(lazy); std::unique_ptr concat_reader; if (lazy) { - ASSERT_OK_AND_ASSIGN(concat_reader, - ConcatBatchReader::CreateLazy(batches.size(), factory, - GetArrowPool(pool_))); + ASSERT_OK_AND_ASSIGN( + concat_reader, + ConcatBatchReader::CreateLazy( + batches.size(), factory, ConcatBatchReader::kDefaultPreparationWindow, + GetArrowPool(pool_), GetReaderBuildExecutor())); } else { std::vector> readers; for (size_t i = 0; i < batches.size(); i++) { @@ -407,11 +412,13 @@ TEST_F(ConcatBatchReaderTest, TestCollectMetricsAfterReleasingReaders) { } TEST_F(ConcatBatchReaderTest, TestWarmupLooksOneReaderAhead) { - for (bool lazy : {false, true}) { - SCOPED_TRACE(lazy); + const std::vector> windows = {std::nullopt, 1, 4, 8}; + for (const auto& window : windows) { + SCOPED_TRACE(window.value_or(0)); + const bool lookahead = !window || window.value() > 1; std::vector events; std::vector> data; - for (size_t i = 0; i < ConcatBatchReader::kPreparationWindow + 2; i++) { + for (size_t i = 0; i < ConcatBatchReader::kDefaultPreparationWindow + 2; i++) { data.push_back(ReaderData(i)->Slice(0, i == 0 ? 2 : 1)); } ConcatBatchReader::ReaderFactory factory = @@ -421,9 +428,10 @@ TEST_F(ConcatBatchReaderTest, TestWarmupLooksOneReaderAhead) { }; ASSERT_OK_AND_ASSIGN(std::shared_ptr executor, CreateDefaultExecutor(1)); std::unique_ptr reader; - if (lazy) { - ASSERT_OK_AND_ASSIGN(reader, ConcatBatchReader::CreateLazy( - data.size(), factory, GetArrowPool(pool_), executor)); + if (window) { + ASSERT_OK_AND_ASSIGN(reader, + ConcatBatchReader::CreateLazy(data.size(), factory, window.value(), + GetArrowPool(pool_), executor)); } else { std::vector> readers; for (size_t i = 0; i < data.size(); i++) { @@ -438,7 +446,11 @@ TEST_F(ConcatBatchReaderTest, TestWarmupLooksOneReaderAhead) { 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")); + if (lookahead) { + ASSERT_LT(FirstEvent(events, "warmup b"), FirstEvent(events, "read a")); + } else { + ASSERT_EQ(CountEvent(events, "warmup b"), 0); + } ASSERT_EQ(CountEvent(events, "warmup c"), 0); ASSERT_OK_AND_ASSIGN(batch, reader->NextBatchWithBitmap()); @@ -449,16 +461,27 @@ TEST_F(ConcatBatchReaderTest, TestWarmupLooksOneReaderAhead) { 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")); + ASSERT_LT(FirstEvent(events, "warmup b"), FirstEvent(events, "read b")); + if (lookahead) { + ASSERT_LT(FirstEvent(events, "warmup c"), FirstEvent(events, "read b")); + } else { + ASSERT_EQ(CountEvent(events, "warmup c"), 0); + } 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)); + ASSERT_LT(FirstEvent(events, "warmup " + std::string(1, 'a' + i)), + FirstEvent(events, "read " + std::string(1, 'a' + i))); 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 (lookahead) { + ASSERT_LT(FirstEvent(events, "warmup " + std::string(1, 'a' + i + 1)), + FirstEvent(events, "read " + std::string(1, 'a' + i))); + } else { + ASSERT_EQ(CountEvent(events, "warmup " + std::string(1, 'a' + i + 1)), 0); + } } if (i + 2 < data.size()) { ASSERT_EQ(CountEvent(events, "warmup " + std::string(1, 'a' + i + 2)), 0); @@ -502,97 +525,146 @@ 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(); - auto lifetime = std::make_shared(0); - std::weak_ptr weak_lifetime = lifetime; +TEST_F(ConcatBatchReaderTest, LazyCreationRejectsInvalidInputsBeforeBuilding) { + std::atomic calls = 0; ConcatBatchReader::ReaderFactory factory = - [counts, lifetime](size_t index) -> Result> { - if (index % 5 == 0) { - return std::unique_ptr(); - } - auto data = ReaderData(index); - if (index % 5 == 1) { - data = data->Slice(0, 0); - } - auto reader = std::make_unique(data, counts); - reader->SelectSecondRow(); - return reader; + [&calls](size_t) -> Result> { + ++calls; + return std::unique_ptr(); }; - 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 pool = GetArrowPool(pool_); + auto executor = GetReaderBuildExecutor(); + for (size_t reader_count : {0, 3}) { + SCOPED_TRACE(reader_count); + ASSERT_NOK_WITH_MSG(ConcatBatchReader::CreateLazy(reader_count, {}, 1, pool, executor), + "requires a factory and a build executor"); + ASSERT_NOK_WITH_MSG(ConcatBatchReader::CreateLazy(reader_count, factory, 1, pool, nullptr), + "requires a factory and a build executor"); + for (int32_t window : {0, -1}) { + SCOPED_TRACE(window); + ASSERT_NOK_WITH_MSG( + ConcatBatchReader::CreateLazy(reader_count, factory, window, pool, executor), + "reader preparation window must be positive"); } - auto expected = ReaderData(i); - std::shared_ptr 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_EQ(calls.load(), 0); +} + +TEST_F(ConcatBatchReaderTest, LazyWindowPreservesOrderBitmapAndMetrics) { + constexpr size_t kFiles = 31; + for (uint32_t threads : {1u, 3u}) { + SCOPED_TRACE(threads); + ASSERT_OK_AND_ASSIGN(std::shared_ptr executor, CreateDefaultExecutor(threads)); + for (int32_t window : {1, 2, 4, 8, 32}) { + SCOPED_TRACE(window); + auto counts = std::make_shared(); + auto lifetime = std::make_shared(0); + std::weak_ptr weak_lifetime = lifetime; + std::atomic factory_calls = 0; + ConcatBatchReader::ReaderFactory factory = + [counts, lifetime, + &factory_calls](size_t index) -> Result> { + factory_calls++; + if (index % 5 == 0) { + return std::unique_ptr(); + } + auto data = ReaderData(index); + if (index % 5 == 1) { + data = data->Slice(0, 0); + } + auto reader = std::make_unique(data, counts); + reader->SelectSecondRow(); + return reader; + }; + ASSERT_OK_AND_ASSIGN(auto reader, + ConcatBatchReader::CreateLazy(kFiles, std::move(factory), window, + GetArrowPool(pool_), executor)); + lifetime.reset(); + const size_t initial = std::min(window, kFiles); + ASSERT_EQ(factory_calls.load(), initial); + ASSERT_LE(counts->created.load(), initial); + 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 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_EQ(factory_calls.load(), kFiles); + ASSERT_LE(counts->peak.load(), initial); + 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)); } - 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(); - auto lifetime = std::make_shared(0); - std::weak_ptr weak_lifetime = lifetime; - ASSERT_OK_AND_ASSIGN( - auto reader, - ConcatBatchReader::CreateLazy( - 100, - [counts, lifetime](size_t index) -> Result> { - return std::make_unique(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(); + constexpr size_t kFiles = 11; + for (int32_t window : {1, 2, 4, 8, 32}) { + SCOPED_TRACE(window); + for (bool explicit_close : {false, true}) { + SCOPED_TRACE(explicit_close); + auto counts = std::make_shared(); + auto lifetime = std::make_shared(0); + std::weak_ptr weak_lifetime = lifetime; + ASSERT_OK_AND_ASSIGN( + auto reader, + ConcatBatchReader::CreateLazy( + kFiles, + [counts, lifetime](size_t index) -> Result> { + return std::make_unique(ReaderData(index), counts); + }, + window, GetArrowPool(pool_), GetReaderBuildExecutor())); + lifetime.reset(); + const size_t initial = std::min(window, kFiles); + ASSERT_EQ(counts->created.load(), initial); + ASSERT_EQ(counts->live.load(), initial); + ASSERT_OK_AND_ASSIGN(auto batch, reader->NextBatchWithBitmap()); + ASSERT_FALSE(BatchReader::IsEofBatch(batch)); + ASSERT_EQ(counts->created.load(), initial); + if (explicit_close) { + reader->Close(); + reader->Close(); + ASSERT_OK_AND_ASSIGN(auto eof, reader->NextBatchWithBitmap()); + ASSERT_TRUE(BatchReader::IsEofBatch(eof)); + } else { + reader.reset(); + } + ASSERT_EQ(counts->created.load(), initial); + ASSERT_LE(counts->peak.load(), initial); + ASSERT_EQ(counts->live.load(), 0); + ASSERT_EQ(counts->closed.load(), initial); + 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))); } - 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))); } } @@ -610,17 +682,17 @@ TEST_F(ConcatBatchReaderTest, LazyEarlyCloseAndDestructionDrainExceptionalBuilds ConcatBatchReader::CreateLazy( 100, [counts, lifetime, gate](size_t index) -> Result> { - if (index == ConcatBatchReader::kPreparationWindow) { + if (index == ConcatBatchReader::kDefaultPreparationWindow) { gate.wait(); throw std::bad_alloc(); } return std::make_unique(ReaderData(index), counts); }, - GetArrowPool(pool_), executor)); + ConcatBatchReader::kDefaultPreparationWindow, 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++) { + for (size_t i = 0; i < ConcatBatchReader::kDefaultPreparationWindow; i++) { ASSERT_OK_AND_ASSIGN(auto batch, reader->NextBatch()); ASSERT_FALSE(BatchReader::IsEofBatch(batch)); ReaderUtils::ReleaseReadBatch(std::move(batch)); @@ -630,9 +702,9 @@ TEST_F(ConcatBatchReaderTest, LazyEarlyCloseAndDestructionDrainExceptionalBuilds // 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; + const size_t created = 2 * ConcatBatchReader::kDefaultPreparationWindow - 2; ASSERT_EQ(counts->created.load(), created); - ASSERT_EQ(counts->live.load(), ConcatBatchReader::kPreparationWindow - 1); + ASSERT_EQ(counts->live.load(), ConcatBatchReader::kDefaultPreparationWindow - 1); ASSERT_FALSE(weak_lifetime.expired()); if (explicit_close) { ASSERT_NO_THROW(reader->Close()); @@ -665,7 +737,7 @@ TEST_F(ConcatBatchReaderTest, LazyInitialFailureDrainsPartialWindow) { 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"); + throw std::runtime_error("initial build failed"); } if (index == 2) { started.set_value(); @@ -678,8 +750,9 @@ TEST_F(ConcatBatchReaderTest, LazyInitialFailureDrainsPartialWindow) { return child; }; auto create = std::async(std::launch::async, [&, factory = std::move(factory)]() mutable { - return ConcatBatchReader::CreateLazy(100, std::move(factory), GetArrowPool(pool_), - executor); + return ConcatBatchReader::CreateLazy(100, std::move(factory), + ConcatBatchReader::kDefaultPreparationWindow, + GetArrowPool(pool_), executor); }); ScopeGuard unblock([&release]() { release.set_value(); }); lifetime.reset(); @@ -688,115 +761,134 @@ TEST_F(ConcatBatchReaderTest, LazyInitialFailureDrainsPartialWindow) { EXPECT_FALSE(weak_lifetime.expired()); release.set_value(); unblock.Release(); - ASSERT_NOK_WITH_MSG(create.get(), "initial build failed"); + const auto result = create.get(); + ASSERT_NOK_WITH_MSG(result, "initial build failed"); + ASSERT_TRUE(result.status().IsUnknownError()); ASSERT_EQ(counts->live.load(), 0); ASSERT_EQ(counts->created.load(), counts->closed.load()); - ASSERT_LT(counts->created.load(), ConcatBatchReader::kPreparationWindow); + ASSERT_LT(counts->created.load(), ConcatBatchReader::kDefaultPreparationWindow); 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(failure)); - auto counts = std::make_shared(); - ASSERT_OK_AND_ASSIGN( - auto reader, - ConcatBatchReader::CreateLazy( - 100, - [counts, failure](size_t index) -> Result> { - const bool fail = index == ConcatBatchReader::kPreparationWindow; - if (fail && failure == Failure::Build) { - return Status::IOError("deferred failure"); - } - auto child = std::make_unique( - 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)); + enum class Failure { Build, OutOfMemory, Read, Bitmap }; + for (int32_t window : {1, 2, 4, 8}) { + SCOPED_TRACE(window); + for (Failure failure : + {Failure::Build, Failure::OutOfMemory, Failure::Read, Failure::Bitmap}) { + SCOPED_TRACE(static_cast(failure)); + auto counts = std::make_shared(); + ASSERT_OK_AND_ASSIGN( + auto reader, + ConcatBatchReader::CreateLazy( + 100, + [counts, failure, + window](size_t index) -> Result> { + const bool fail = index == static_cast(window); + if (fail && failure == Failure::Build) { + return Status::IOError("deferred failure"); + } + if (fail && failure == Failure::OutOfMemory) { + throw std::bad_alloc(); + } + auto child = std::make_unique( + ReaderData(index), counts, fail && failure == Failure::Bitmap); + if (fail && failure == Failure::Read) { + child->SetNextBatchStatus(Status::IOError("deferred failure")); + } + return child; + }, + window, GetArrowPool(pool_), GetReaderBuildExecutor())); + for (int32_t i = 0; i < window; i++) { + ASSERT_OK_AND_ASSIGN(auto batch, reader->NextBatch()); + ASSERT_FALSE(BatchReader::IsEofBatch(batch)); + ReaderUtils::ReleaseReadBatch(std::move(batch)); + } + const auto result = reader->NextBatch(); + ASSERT_FALSE(result.ok()); + if (failure == Failure::OutOfMemory) { + ASSERT_TRUE(result.status().IsOutOfMemory()); + } else { + const char* message = + failure == Failure::Bitmap ? "at least one valid row" : "deferred failure"; + ASSERT_NOK_WITH_MSG(result, message); + } + const size_t created = counts->created.load(); + ASSERT_EQ(reader->NextBatchWithBitmap().status(), result.status()); + ASSERT_EQ(reader->NextBatch().status(), result.status()); + 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(), window); } - 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(); - ASSERT_OK_AND_ASSIGN(std::shared_ptr executor, CreateDefaultExecutor(1)); - std::promise started; - auto started_future = started.get_future(); - std::promise release; - auto gate = release.get_future().share(); - std::promise closing; - auto closing_future = closing.get_future(); - ConcatBatchReader::ReaderFactory factory = - [counts, gate, &started, - &closing](size_t index) -> Result> { - if (index == ConcatBatchReader::kPreparationWindow) { - started.set_value(); - gate.wait(); - } - auto child = std::make_unique(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)); + for (int32_t window : {2, 4, 8}) { + SCOPED_TRACE(window); + for (bool explicit_close : {false, true}) { + SCOPED_TRACE(explicit_close); + auto counts = std::make_shared(); + ASSERT_OK_AND_ASSIGN(std::shared_ptr executor, CreateDefaultExecutor(1)); + std::promise started; + auto started_future = started.get_future(); + std::promise release; + auto gate = release.get_future().share(); + std::promise closing; + auto closing_future = closing.get_future(); + ConcatBatchReader::ReaderFactory factory = + [counts, gate, window, &started, + &closing](size_t index) -> Result> { + if (index == static_cast(window)) { + started.set_value(); + gate.wait(); + } + auto child = std::make_unique(ReaderData(index), counts); + if (index == static_cast(window - 1)) { + child->SetCloseCallback([&closing]() { closing.set_value(); }); + } + return child; + }; + ASSERT_OK_AND_ASSIGN(auto reader, + ConcatBatchReader::CreateLazy(100, std::move(factory), window, + GetArrowPool(pool_), executor)); + ScopeGuard unblock([&release]() { release.set_value(); }); + auto read = std::async(std::launch::async, [&reader, window]() -> Status { + for (int32_t i = 0; i < window; 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; } - 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) { + 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(); - ASSERT_OK(read.get()); - return; + close.get(); + ASSERT_EQ(closing_status, std::future_status::ready); + ASSERT_EQ(close_status, std::future_status::timeout); + ASSERT_EQ(counts->created.load(), window + 1); + ASSERT_EQ(counts->closed.load(), counts->created.load()); + ASSERT_EQ(counts->live.load(), 0); } - 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); } } diff --git a/src/paimon/core/core_options.cpp b/src/paimon/core/core_options.cpp index 74affc24e..26f1cb7ee 100644 --- a/src/paimon/core/core_options.cpp +++ b/src/paimon/core/core_options.cpp @@ -29,6 +29,7 @@ #include "paimon/common/fs/resolving_file_system.h" #include "paimon/common/options/memory_size.h" #include "paimon/common/options/time_duration.h" +#include "paimon/common/reader/concat_batch_reader.h" #include "paimon/common/utils/math.h" #include "paimon/common/utils/options_utils.h" #include "paimon/common/utils/path_util.h" @@ -419,6 +420,7 @@ struct CoreOptions::Impl { int32_t manifest_merge_min_count = 30; int32_t scan_manifest_entry_cache_max_snapshots = 0; int32_t read_batch_size = 1024; + int32_t read_preparation_window = ConcatBatchReader::kDefaultPreparationWindow; int32_t write_batch_size = 1024; int32_t local_sort_max_num_file_handles = 128; int32_t commit_max_retries = 10; @@ -532,6 +534,12 @@ struct CoreOptions::Impl { &source_split_open_file_cost)); // Parse read.batch-size - read batch size for file formats PAIMON_RETURN_NOT_OK(parser.Parse(Options::READ_BATCH_SIZE, &read_batch_size)); + PAIMON_RETURN_NOT_OK( + parser.Parse(Options::READ_PREPARATION_WINDOW, &read_preparation_window)); + if (read_preparation_window <= 0) { + return Status::Invalid( + fmt::format("{} must be positive", Options::READ_PREPARATION_WINDOW)); + } // Parse write.batch-size - write batch size for file formats PAIMON_RETURN_NOT_OK(parser.Parse(Options::WRITE_BATCH_SIZE, &write_batch_size)); // Parse write-buffer-size - data to build up in memory before flushing to disk @@ -1333,6 +1341,10 @@ int32_t CoreOptions::GetReadBatchSize() const { return impl_->read_batch_size; } +int32_t CoreOptions::GetReadPreparationWindow() const { + return impl_->read_preparation_window; +} + int32_t CoreOptions::GetWriteBatchSize() const { return impl_->write_batch_size; } diff --git a/src/paimon/core/core_options.h b/src/paimon/core/core_options.h index 584a430a9..c478b2c1c 100644 --- a/src/paimon/core/core_options.h +++ b/src/paimon/core/core_options.h @@ -126,6 +126,7 @@ class PAIMON_EXPORT CoreOptions { StartupMode GetStartupMode() const; int32_t GetReadBatchSize() const; + int32_t GetReadPreparationWindow() const; int32_t GetWriteBatchSize() const; int64_t GetWriteBufferSize() const; bool GetWriteBufferSpillable() const; diff --git a/src/paimon/core/core_options_test.cpp b/src/paimon/core/core_options_test.cpp index ebad7d838..2c0b53989 100644 --- a/src/paimon/core/core_options_test.cpp +++ b/src/paimon/core/core_options_test.cpp @@ -93,6 +93,7 @@ TEST(CoreOptionsTest, TestDefaultValue) { ASSERT_EQ(128 * 1024 * 1024L, core_options.GetSourceSplitTargetSize()); ASSERT_EQ(4 * 1024 * 1024L, core_options.GetSourceSplitOpenFileCost()); ASSERT_EQ(1024, core_options.GetReadBatchSize()); + ASSERT_EQ(4, core_options.GetReadPreparationWindow()); ASSERT_EQ(1024, core_options.GetWriteBatchSize()); ASSERT_EQ(256 * 1024 * 1024, core_options.GetWriteBufferSize()); ASSERT_TRUE(core_options.GetWriteBufferSpillable()); @@ -237,6 +238,7 @@ TEST(CoreOptionsTest, TestFromMap) { {Options::SOURCE_SPLIT_TARGET_SIZE, "24MB"}, {Options::SOURCE_SPLIT_OPEN_FILE_COST, "32MB"}, {Options::READ_BATCH_SIZE, "2048"}, + {Options::READ_PREPARATION_WINDOW, "8"}, {Options::WRITE_BUFFER_SIZE, "16MB"}, {Options::WRITE_BATCH_SIZE, "1234"}, {Options::WRITE_BUFFER_SPILLABLE, "false"}, @@ -392,6 +394,7 @@ TEST(CoreOptionsTest, TestFromMap) { ASSERT_EQ(24 * 1024 * 1024L, core_options.GetSourceSplitTargetSize()); ASSERT_EQ(32 * 1024 * 1024L, core_options.GetSourceSplitOpenFileCost()); ASSERT_EQ(2048, core_options.GetReadBatchSize()); + ASSERT_EQ(8, core_options.GetReadPreparationWindow()); ASSERT_EQ(1234, core_options.GetWriteBatchSize()); ASSERT_EQ(16 * 1024 * 1024, core_options.GetWriteBufferSize()); ASSERT_FALSE(core_options.GetWriteBufferSpillable()); @@ -596,6 +599,26 @@ TEST(CoreOptionsTest, TestInvalidCase) { "must not be negative"); } +TEST(CoreOptionsTest, TestReadPreparationWindow) { + for (int32_t window : {1, std::numeric_limits::max()}) { + SCOPED_TRACE(window); + ASSERT_OK_AND_ASSIGN( + CoreOptions options, + CoreOptions::FromMap({{Options::READ_PREPARATION_WINDOW, std::to_string(window)}})); + ASSERT_EQ(window, options.GetReadPreparationWindow()); + } + for (const std::string value : {"0", "-1", "-2147483648"}) { + SCOPED_TRACE(value); + ASSERT_NOK_WITH_MSG(CoreOptions::FromMap({{Options::READ_PREPARATION_WINDOW, value}}), + "read.preparation-window must be positive"); + } + for (const std::string value : {"invalid", "1.5", "2147483648"}) { + SCOPED_TRACE(value); + ASSERT_NOK_WITH_MSG(CoreOptions::FromMap({{Options::READ_PREPARATION_WINDOW, value}}), + "Invalid Config [read.preparation-window:"); + } +} + TEST(CoreOptionsTest, TestRejectBranchLeavingTableRoot) { // Both branch options name a directory under the table root, so a value that is not a single // path component is rejected before it can be joined into a path. @@ -1210,6 +1233,7 @@ TEST(CoreOptionsTest, TestCopyAssignmentOperator) { {Options::PARTITION_DEFAULT_NAME, "foo"}, {Options::MANIFEST_MERGE_MIN_COUNT, "2"}, {Options::READ_BATCH_SIZE, "2048"}, + {Options::READ_PREPARATION_WINDOW, "8"}, {Options::WRITE_BATCH_SIZE, "1234"}, {Options::WRITE_BUFFER_SIZE, "16MB"}, {Options::WRITE_BUFFER_SPILLABLE, "false"}, @@ -1255,6 +1279,7 @@ TEST(CoreOptionsTest, TestCopyAssignmentOperator) { ASSERT_EQ("foo", target.GetPartitionDefaultName()); ASSERT_EQ(2, target.GetManifestMergeMinCount()); ASSERT_EQ(2048, target.GetReadBatchSize()); + ASSERT_EQ(8, target.GetReadPreparationWindow()); ASSERT_EQ(1234, target.GetWriteBatchSize()); ASSERT_EQ(16 * 1024 * 1024L, target.GetWriteBufferSize()); ASSERT_FALSE(target.GetWriteBufferSpillable()); diff --git a/src/paimon/core/global_index/indexed_split_impl.h b/src/paimon/core/global_index/indexed_split_impl.h index e4844a741..8528bc575 100644 --- a/src/paimon/core/global_index/indexed_split_impl.h +++ b/src/paimon/core/global_index/indexed_split_impl.h @@ -92,6 +92,9 @@ class IndexedSplitImpl : public IndexedSplit { } Status Validate() const { + if (!data_split_) { + return Status::Invalid("indexed split has no data split"); + } if ((row_ranges_.empty() && !data_split_->DataFiles().empty()) || (!row_ranges_.empty() && data_split_->DataFiles().empty())) { return Status::Invalid("Invalid IndexedSplit: row ranges mismatch data files."); diff --git a/src/paimon/core/operation/raw_file_split_read.cpp b/src/paimon/core/operation/raw_file_split_read.cpp index a1fb07f52..cf3b6b35e 100644 --- a/src/paimon/core/operation/raw_file_split_read.cpp +++ b/src/paimon/core/operation/raw_file_split_read.cpp @@ -24,6 +24,7 @@ #include #include "fmt/format.h" +#include "paimon/common/executor/reader_build_executor.h" #include "paimon/common/reader/complete_row_kind_batch_reader.h" #include "paimon/common/reader/concat_batch_reader.h" #include "paimon/common/types/data_field.h" @@ -179,9 +180,10 @@ Result> RawFileSplitRead::CreateReaderWithPredicate } return std::unique_ptr(std::move(readers.front())); }; - PAIMON_ASSIGN_OR_RAISE( - std::unique_ptr concat_batch_reader, - ConcatBatchReader::CreateLazy(data_files.size(), std::move(factory), arrow_pool_)); + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr concat_batch_reader, + ConcatBatchReader::CreateLazy(data_files.size(), std::move(factory), + options_.GetReadPreparationWindow(), + arrow_pool_, GetReaderBuildExecutor())); PAIMON_ASSIGN_OR_RAISE( std::unique_ptr batch_reader, ApplyPredicateFilterIfNeeded( 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 9fa0c7f84..0a86a4762 100644 --- a/src/paimon/core/operation/raw_file_split_read_test.cpp +++ b/src/paimon/core/operation/raw_file_split_read_test.cpp @@ -18,6 +18,7 @@ #include "paimon/core/operation/raw_file_split_read.h" +#include #include #include #include @@ -26,7 +27,6 @@ #include #include #include -#include #include #include #include @@ -36,6 +36,7 @@ #include "arrow/ipc/json_simple.h" #include "gtest/gtest.h" #include "paimon/common/data/binary_row.h" +#include "paimon/common/executor/reader_build_executor.h" #include "paimon/common/reader/concat_batch_reader.h" #include "paimon/common/reader/reader_utils.h" #include "paimon/common/table/special_fields.h" @@ -131,7 +132,8 @@ class RawFileSplitReadTest : public ::testing::Test { protected: Result> CreateCountingRead( const std::shared_ptr& fs, const std::vector& field_names = {}, - bool row_tracking = false) const { + bool row_tracking = false, + const std::optional& preparation_window = std::nullopt) const { const std::string path = GetDataDir() + "/orc/multi_partition_append_table.db/multi_partition_append_table"; ReadContextBuilder builder(path); @@ -140,6 +142,10 @@ class RawFileSplitReadTest : public ::testing::Test { .EnablePrefetch(false) .EnablePredicateFilter(true); builder.AddOption(Options::FILE_INDEX_READ_ENABLED, "false"); + if (preparation_window) { + builder.AddOption(Options::READ_PREPARATION_WINDOW, + std::to_string(preparation_window.value())); + } if (row_tracking) { builder.AddOption(Options::ROW_TRACKING_ENABLED, "true"); } @@ -368,56 +374,61 @@ class RawFileSplitReadTest : public ::testing::Test { }; TEST_F(RawFileSplitReadTest, LazyInitialWindowBuildsConcurrentlyAndEarlyCloseStopsOpening) { - auto fs = std::make_shared(".orc"); - ASSERT_OK_AND_ASSIGN(auto split_read, CreateCountingRead(fs)); - auto split = std::dynamic_pointer_cast(PrepareDataSplits().front()); - std::vector> 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 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 lock(mutex); - released = true; - changed.notify_all(); - }; - ScopeGuard unblock(release); - const size_t parallel = std::min(ConcatBatchReader::kPreparationWindow, - std::max(1u, std::thread::hardware_concurrency())); - { - std::unique_lock lock(mutex); - ASSERT_TRUE( - changed.wait_for(lock, std::chrono::seconds(5), [&]() { return started == parallel; })); + const std::vector> windows = {std::nullopt, 1, 2, 8, 32}; + for (const auto& window : windows) { + SCOPED_TRACE(window.value_or(ConcatBatchReader::kDefaultPreparationWindow)); + auto fs = std::make_shared(".orc"); + ASSERT_OK_AND_ASSIGN(auto split_read, CreateCountingRead(fs, {}, false, window)); + auto split = std::dynamic_pointer_cast(PrepareDataSplits().front()); + std::vector> files(11, split->DataFiles().front()); + const size_t initial = std::min( + window.value_or(ConcatBatchReader::kDefaultPreparationWindow), files.size()); + std::mutex mutex; + std::condition_variable changed; + size_t started = 0; + bool released = false; + fs->before_open = [&](const std::string&) { + std::unique_lock 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 lock(mutex); + released = true; + changed.notify_all(); + }; + ScopeGuard unblock(release); + const size_t parallel = std::min(initial, GetReaderBuildExecutor()->GetThreadNum()); + { + std::unique_lock 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(), initial); + 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(), initial); + reader->Close(); + ASSERT_EQ(fs->counts->opens.load(), initial); + ASSERT_EQ(fs->counts->live.load(), 0); + ASSERT_LE(fs->counts->peak.load(), initial); + ReaderUtils::ReleaseReadBatch(std::move(batch)); } - 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) { @@ -494,62 +505,65 @@ TEST_F(RawFileSplitReadTest, LazyReaderOutlivesCreatorWithPerFilePredicatesDvAnd } } ASSERT_EQ(counts->opens.load(), kFiles); - ASSERT_LE(counts->peak.load(), ConcatBatchReader::kPreparationWindow); + ASSERT_LE(counts->peak.load(), ConcatBatchReader::kDefaultPreparationWindow); ASSERT_EQ(counts->live.load(), 0); result.reset(); ASSERT_TRUE(weak_pool.expired()); } TEST_F(RawFileSplitReadTest, LazyValidationAndFileOpenFailures) { - auto fs = std::make_shared(".orc"); - ASSERT_OK_AND_ASSIGN(auto split_read, CreateCountingRead(fs)); - auto split = std::dynamic_pointer_cast(PrepareDataSplits().front()); - std::vector> 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(0, 0)}), - "raw data file is null"); - ASSERT_EQ(fs->counts->opens.load(), 0); - files.back() = files.front(); - auto missing = std::make_shared(*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)); + for (int32_t window : {1, 2, 4, 8}) { + SCOPED_TRACE(window); + auto fs = std::make_shared(".orc"); + ASSERT_OK_AND_ASSIGN(auto split_read, CreateCountingRead(fs, {}, false, window)); + auto split = std::dynamic_pointer_cast(PrepareDataSplits().front()); + std::vector> 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, {}), + "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(0, 0)}), + "raw data file is null"); + ASSERT_EQ(fs->counts->opens.load(), 0); + files.back() = files.front(); + auto missing = std::make_shared(*files.front()); + missing->file_name = "missing.orc"; + files[window] = 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(), window); + for (int32_t i = 0; i < window; 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); } - 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 diff --git a/src/paimon/core/table/file_store_table.cpp b/src/paimon/core/table/file_store_table.cpp index 0dbc1bf4f..5b3eeb6f4 100644 --- a/src/paimon/core/table/file_store_table.cpp +++ b/src/paimon/core/table/file_store_table.cpp @@ -592,7 +592,8 @@ Result> FileStoreTable::CreateTableRead( return std::make_unique(path_factory, internal_context, memory_pool, executor); } - return KeyValueTableRead::Create(path_factory, internal_context, memory_pool, executor); + return KeyValueTableRead::Create(path_factory, internal_context, /*force_keep_delete=*/false, + memory_pool, executor); } Result> FileStoreTable::NewRead( diff --git a/src/paimon/core/table/format/lazy_concat_batch_reader.cpp b/src/paimon/core/table/format/lazy_concat_batch_reader.cpp index 982999fa2..ad5f4e574 100644 --- a/src/paimon/core/table/format/lazy_concat_batch_reader.cpp +++ b/src/paimon/core/table/format/lazy_concat_batch_reader.cpp @@ -21,7 +21,6 @@ #include #include -// `ReadBatch` holds `unique_ptr`s to these, so destroying one needs their definitions. #include "arrow/c/abi.h" #include "fmt/format.h" #include "paimon/common/metrics/metrics_impl.h" @@ -30,9 +29,8 @@ namespace paimon { namespace { -/// Says which file a failure came from, keeping the status code it carried. -Status WithFileName(const std::string& name, const Status& status) { - return Status(status.code(), fmt::format("cannot read {}: {}", name, status.message())); +Status WithSourceName(const std::string& name, const Status& status) { + return status.WithMessage(fmt::format("cannot read {}: {}", name, status.message())); } } // namespace @@ -50,45 +48,40 @@ void LazyConcatBatchReader::CloseCurrent() { if (current_reader_ == nullptr) { return; } - // Taken before the reader goes, or everything read through it is missing from the totals. - std::shared_ptr metrics = current_reader_->GetReaderMetrics(); + // Closing drains pending preparation and prefetch work before taking the final metrics. current_reader_->Close(); - if (metrics != nullptr) { - closed_metrics_->Merge(metrics); - } - // Released here rather than held to the end: a batch it handed out retains what it needs of - // it, so a partition of many files does not keep one reader per file alive. + closed_metrics_->Merge(current_reader_->GetReaderMetrics()); current_reader_.reset(); current_name_.clear(); } Result LazyConcatBatchReader::NextBatchWithBitmap() { - // `BatchReader` promises a failure is terminal: keep answering with it rather than moving - // on to the next file. if (!failure_.ok()) { return failure_; } while (true) { if (current_reader_ == nullptr) { if (next_source_ >= sources_.size()) { + DoClose(); return BatchReader::MakeEofBatchWithBitmap(); } - Source& source = sources_[next_source_]; + Source source = std::exchange(sources_[next_source_++], Source{}); Result> opened = source.open(); if (!opened.ok()) { - failure_ = WithFileName(source.name, opened.status()); + failure_ = WithSourceName(source.name, opened.status()); + DoClose(); return failure_; } - next_source_++; current_reader_ = std::move(opened).value(); if (current_reader_ == nullptr) { continue; } - current_name_ = source.name; + current_name_ = std::move(source.name); } Result result = current_reader_->NextBatchWithBitmap(); if (!result.ok()) { - failure_ = WithFileName(current_name_, result.status()); + failure_ = WithSourceName(current_name_, result.status()); + DoClose(); return failure_; } if (!BatchReader::IsEofBatch(result.value())) { @@ -101,7 +94,13 @@ Result LazyConcatBatchReader::NextBatchWithBit Result LazyConcatBatchReader::NextBatch() { PAIMON_ASSIGN_OR_RAISE(BatchReader::ReadBatchWithBitmap batch_with_bitmap, NextBatchWithBitmap()); - return ReaderUtils::ApplyBitmapToReadBatch(std::move(batch_with_bitmap), arrow_pool_); + auto result = ReaderUtils::ApplyBitmapToReadBatch(std::move(batch_with_bitmap), arrow_pool_); + if (!result.ok()) { + failure_ = WithSourceName(current_name_, result.status()); + DoClose(); + return failure_; + } + return result; } void LazyConcatBatchReader::Close() { @@ -110,18 +109,15 @@ void LazyConcatBatchReader::Close() { void LazyConcatBatchReader::DoClose() { CloseCurrent(); - // Whatever was never reached was never opened. - next_source_ = sources_.size(); + std::vector().swap(sources_); + next_source_ = 0; } std::shared_ptr LazyConcatBatchReader::GetReaderMetrics() const { auto metrics = std::make_shared(); metrics->Merge(closed_metrics_); if (current_reader_ != nullptr) { - std::shared_ptr current = current_reader_->GetReaderMetrics(); - if (current != nullptr) { - metrics->Merge(current); - } + metrics->Merge(current_reader_->GetReaderMetrics()); } return metrics; } diff --git a/src/paimon/core/table/format/lazy_concat_batch_reader.h b/src/paimon/core/table/format/lazy_concat_batch_reader.h index fcdc24274..5abd0dcf3 100644 --- a/src/paimon/core/table/format/lazy_concat_batch_reader.h +++ b/src/paimon/core/table/format/lazy_concat_batch_reader.h @@ -34,21 +34,14 @@ namespace paimon { class MetricsImpl; -/// Reads a sequence of readers one after another, opening each only when it is reached. -/// -/// A split holds a whole partition's files and a reader holds its file open, so building them all -/// up front could exhaust the file descriptors before returning a row. One file is open at a time, -/// closed as soon as it runs out and its metrics taken first, so what it read stays in the totals. -/// A batch it handed out outlives it, as `BatchReader` requires. -/// -/// Every failure names the file it came from - the only thing telling one file of a partition from -/// another - and is terminal. +/// Opens one source at a time in input order. Returned batches own their resources, so each +/// reader can close at EOF. Failures identify the source and terminate the read. class LazyConcatBatchReader : public BatchReader { public: - /// Opens one reader, at most once, when the previous reader runs out. + /// Called once when its source is reached. A null reader skips the source. Each returned + /// reader owns the dependencies it needs after the factory is released. using ReaderFactory = std::function>()>; - /// One reader and the file it reads, named in every error that reader produces. struct Source { std::string name; ReaderFactory open; @@ -65,19 +58,15 @@ class LazyConcatBatchReader : public BatchReader { std::shared_ptr GetReaderMetrics() const override; private: - /// Closes every reader; called by both `Close` and the destructor. void DoClose(); - /// Closes and releases the reader in hand, keeping its metrics. void CloseCurrent(); std::shared_ptr arrow_pool_; std::vector sources_; size_t next_source_ = 0; std::unique_ptr current_reader_; - /// The file `current_reader_` reads, kept so a failure part way through can name it. std::string current_name_; - /// The failure this reader stopped at, or OK while it has not failed. Status failure_; std::shared_ptr closed_metrics_; }; diff --git a/src/paimon/core/table/format/lazy_concat_batch_reader_test.cpp b/src/paimon/core/table/format/lazy_concat_batch_reader_test.cpp index 82d6add75..70b99bde7 100644 --- a/src/paimon/core/table/format/lazy_concat_batch_reader_test.cpp +++ b/src/paimon/core/table/format/lazy_concat_batch_reader_test.cpp @@ -18,6 +18,10 @@ #include "paimon/core/table/format/lazy_concat_batch_reader.h" +#include +#include +#include +#include #include #include #include @@ -25,26 +29,34 @@ #include "arrow/api.h" #include "arrow/c/bridge.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/concat_batch_reader.h" #include "paimon/common/reader/reader_utils.h" #include "paimon/common/utils/arrow/mem_utils.h" #include "paimon/common/utils/arrow/status_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" +#include "paimon/testing/utils/read_result_collector.h" #include "paimon/testing/utils/testharness.h" namespace paimon::test { namespace { -/// A reader over a fixed number of one-row batches that records when it was closed. class OneColumnBatchReader : public BatchReader { public: - OneColumnBatchReader(int32_t value, int32_t batches, bool* closed_flag) + OneColumnBatchReader(int32_t value, int32_t batches, bool* closed_flag, + bool empty_bitmap = false) : value_(value), remaining_(batches), closed_flag_(closed_flag), + empty_bitmap_(empty_bitmap), metrics_(std::make_shared()) {} Result NextBatch() override { @@ -62,9 +74,18 @@ class OneColumnBatchReader : public BatchReader { auto c_schema = std::make_unique(); PAIMON_RETURN_NOT_OK_FROM_ARROW( arrow::ExportArray(*struct_array, c_array.get(), c_schema.get())); + metrics_->SetCounter("test.rows", ++rows_read_); return std::make_pair(std::move(c_array), std::move(c_schema)); } + Result NextBatchWithBitmap() override { + PAIMON_ASSIGN_OR_RAISE(ReadBatchWithBitmap batch, BatchReader::NextBatchWithBitmap()); + if (empty_bitmap_ && !BatchReader::IsEofBatch(batch)) { + batch.second = RoaringBitmap32(); + } + return batch; + } + std::shared_ptr GetReaderMetrics() const override { return metrics_; } @@ -79,15 +100,17 @@ class OneColumnBatchReader : public BatchReader { int32_t value_; int32_t remaining_; bool* closed_flag_; + bool empty_bitmap_; + uint64_t rows_read_ = 0; std::shared_ptr metrics_; }; -/// A reader that fails on its first batch, standing for a file that opens and then turns out to -/// be unreadable - a truncated record, a value no type can hold. class FailingBatchReader : public BatchReader { public: - explicit FailingBatchReader(Status failure) - : failure_(std::move(failure)), metrics_(std::make_shared()) {} + explicit FailingBatchReader(Status failure, bool* closed_flag = nullptr) + : failure_(std::move(failure)), + closed_flag_(closed_flag), + metrics_(std::make_shared()) {} Result NextBatch() override { return failure_; @@ -97,195 +120,300 @@ class FailingBatchReader : public BatchReader { return metrics_; } - void Close() override {} + void Close() override { + if (closed_flag_) { + *closed_flag_ = true; + } + } private: Status failure_; + bool* closed_flag_; std::shared_ptr metrics_; }; -/// Names factories "file-0", "file-1", ... A source carries the name of the file it reads so a -/// failure can point at one; which name it is does not matter to these tests. +class ReadErrorDetail final : public StatusDetail { + public: + const char* type_id() const override { + return "read-error"; + } + std::string ToString() const override { + return "test read error"; + } +}; + std::vector SourcesOf( std::vector&& factories) { std::vector sources; sources.reserve(factories.size()); for (size_t i = 0; i < factories.size(); i++) { - sources.push_back({"file-" + std::to_string(i), std::move(factories[i])}); + sources.push_back({fmt::format("file-{}", i), std::move(factories[i])}); } return sources; } -int64_t DrainRows(BatchReader* reader) { - int64_t rows = 0; - while (true) { - Result batch = reader->NextBatch(); - if (!batch.ok() || BatchReader::IsEofBatch(batch.value())) { - return rows; - } - rows += batch.value().first->length; - ReaderUtils::ReleaseReadBatch(std::move(batch).value()); - } -} - } // namespace -TEST(LazyConcatBatchReaderTest, TestAReaderIsOpenedOnlyWhenItIsReached) { - // A split holds a whole partition's files, and a reader keeps its file open. Building them all - // up front would hold one descriptor per file before a single row came back. +TEST(LazyConcatBatchReaderTest, TestReadersOpenInOrderAndPreserveMetricsAfterClose) { int32_t opened = 0; - std::vector factories; - for (int32_t i = 0; i < 3; i++) { - factories.push_back([&opened, i]() -> Result> { - opened++; - return std::make_unique(i, /*batches=*/1, nullptr); - }); - } - LazyConcatBatchReader reader(SourcesOf(std::move(factories)), GetArrowPool(GetDefaultPool())); - ASSERT_EQ(opened, 0); - - ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch first, reader.NextBatch()); - ASSERT_FALSE(BatchReader::IsEofBatch(first)); - ReaderUtils::ReleaseReadBatch(std::move(first)); - ASSERT_EQ(opened, 1); - ASSERT_EQ(DrainRows(&reader), 2); - ASSERT_EQ(opened, 3); -} - -TEST(LazyConcatBatchReaderTest, TestAReaderIsClosedAsSoonAsItRunsOut) { bool first_closed = false; bool second_closed = false; + bool first_closed_before_second_opened = false; + auto first_lifetime = std::make_shared(0); + auto second_lifetime = std::make_shared(0); + std::weak_ptr weak_first_lifetime = first_lifetime; + std::weak_ptr weak_second_lifetime = second_lifetime; std::vector factories; - factories.push_back([&first_closed]() -> Result> { - return std::make_unique(0, /*batches=*/1, &first_closed); + factories.push_back([&, first_lifetime]() -> Result> { + ++opened; + return std::make_unique(0, 2, &first_closed); }); - factories.push_back([&second_closed]() -> Result> { - return std::make_unique(1, /*batches=*/1, &second_closed); + factories.push_back([&, second_lifetime]() -> Result> { + ++opened; + first_closed_before_second_opened = first_closed; + return std::make_unique(1, 1, &second_closed); }); LazyConcatBatchReader reader(SourcesOf(std::move(factories)), GetArrowPool(GetDefaultPool())); + first_lifetime.reset(); + second_lifetime.reset(); + ASSERT_EQ(opened, 0); + ASSERT_FALSE(weak_first_lifetime.expired()); + ASSERT_FALSE(weak_second_lifetime.expired()); ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch first, reader.NextBatch()); ASSERT_FALSE(BatchReader::IsEofBatch(first)); - ReaderUtils::ReleaseReadBatch(std::move(first)); + ASSERT_EQ(opened, 1); ASSERT_FALSE(first_closed); - // Reaching the second file's rows means the first file is done with, so it is let go at once - // rather than at the end of the split. + ASSERT_TRUE(weak_first_lifetime.expired()); + ASSERT_FALSE(weak_second_lifetime.expired()); + ASSERT_OK_AND_ASSIGN(uint64_t rows, reader.GetReaderMetrics()->GetCounter("test.rows")); + ASSERT_EQ(rows, 1); ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch second, reader.NextBatch()); ASSERT_FALSE(BatchReader::IsEofBatch(second)); ReaderUtils::ReleaseReadBatch(std::move(second)); - ASSERT_TRUE(first_closed); - ASSERT_FALSE(second_closed); -} - -TEST(LazyConcatBatchReaderTest, TestClosingDoesNotOpenWhatWasNeverReached) { - int32_t opened = 0; - std::vector factories; - for (int32_t i = 0; i < 3; i++) { - factories.push_back([&opened, i]() -> Result> { - opened++; - return std::make_unique(i, /*batches=*/1, nullptr); - }); - } - LazyConcatBatchReader reader(SourcesOf(std::move(factories)), GetArrowPool(GetDefaultPool())); - ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch first, reader.NextBatch()); - ASSERT_FALSE(BatchReader::IsEofBatch(first)); - ReaderUtils::ReleaseReadBatch(std::move(first)); - reader.Close(); ASSERT_EQ(opened, 1); - // And nothing is opened afterwards either. - ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch eof, reader.NextBatch()); + ASSERT_FALSE(first_closed); + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch third, reader.NextBatch()); + ASSERT_FALSE(BatchReader::IsEofBatch(third)); + ASSERT_OK_AND_ASSIGN(std::shared_ptr array, + ReadResultCollector::GetArray(std::move(third))); + auto struct_array = std::dynamic_pointer_cast(array); + ASSERT_NE(struct_array, nullptr); + auto values = std::dynamic_pointer_cast(struct_array->field(0)); + ASSERT_NE(values, nullptr); + ASSERT_EQ(values->length(), 1); + ASSERT_EQ(values->Value(0), 1); + ASSERT_EQ(opened, 2); + ASSERT_TRUE(first_closed_before_second_opened); + ASSERT_FALSE(second_closed); + ASSERT_TRUE(weak_second_lifetime.expired()); + ASSERT_OK_AND_ASSIGN(rows, reader.GetReaderMetrics()->GetCounter("test.rows")); + ASSERT_EQ(rows, 3); + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatchWithBitmap eof, reader.NextBatchWithBitmap()); ASSERT_TRUE(BatchReader::IsEofBatch(eof)); - ASSERT_EQ(opened, 1); + ASSERT_TRUE(second_closed); + reader.Close(); + ASSERT_OK_AND_ASSIGN(rows, reader.GetReaderMetrics()->GetCounter("test.rows")); + ASSERT_EQ(rows, 3); + ASSERT_EQ(opened, 2); + ASSERT_OK_AND_ASSIGN(array, ReadResultCollector::GetArray(std::move(first))); + ASSERT_EQ(array->length(), 1); } -TEST(LazyConcatBatchReaderTest, TestAFailureIsTerminal) { - // `BatchReader` says a failure must not be retried: the reader keeps answering with the same - // error. Moving on to the next file would let a caller that ignored the error read a - // partition with a file silently missing from it. - int32_t opened = 0; +TEST(LazyConcatBatchReaderTest, TestEarlyClosePreservesCompletedPreparationMetrics) { + ASSERT_OK_AND_ASSIGN(std::shared_ptr executor, CreateDefaultExecutor(1)); + auto arrow_pool = GetArrowPool(GetDefaultPool()); + std::atomic prepared = 0; + std::promise release; + auto gate = release.get_future().share(); + ConcatBatchReader::ReaderFactory factory = + [gate, &prepared](size_t index) -> Result> { + if (index == 2) { + gate.wait(); + } + auto reader = + std::make_unique(static_cast(index), 1, nullptr); + reader->GetReaderMetrics()->SetCounter("test.prepared-readers", 1); + ++prepared; + return reader; + }; std::vector factories; - factories.push_back([&opened]() -> Result> { - opened++; - return Status::IOError("cannot open the file"); - }); - factories.push_back([&opened]() -> Result> { - opened++; - return std::make_unique(1, /*batches=*/1, nullptr); + factories.push_back([factory, arrow_pool, executor]() -> Result> { + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr reader, + ConcatBatchReader::CreateLazy(3, factory, 2, arrow_pool, executor)); + return std::unique_ptr(std::move(reader)); }); - LazyConcatBatchReader reader(SourcesOf(std::move(factories)), GetArrowPool(GetDefaultPool())); - - Result first = reader.NextBatch(); - ASSERT_FALSE(first.ok()); - ASSERT_NOK_WITH_MSG(first, "cannot read file-0"); - // The second call answers with the same failure and never reaches the second file. - Result second = reader.NextBatch(); - ASSERT_FALSE(second.ok()); - ASSERT_NOK_WITH_MSG(second, "cannot read file-0"); - ASSERT_TRUE(second.status().IsIOError()); - ASSERT_EQ(opened, 1); + LazyConcatBatchReader reader(SourcesOf(std::move(factories)), arrow_pool); + ScopeGuard unblock([&release]() { release.set_value(); }); + for (int32_t i = 0; i < 2; ++i) { + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatchWithBitmap batch, reader.NextBatchWithBitmap()); + ASSERT_FALSE(BatchReader::IsEofBatch(batch)); + ReaderUtils::ReleaseReadBatch(std::move(batch.first)); + } + release.set_value(); + unblock.Release(); + ASSERT_EQ(Via(executor.get(), []() {}).wait_for(std::chrono::seconds(5)), + std::future_status::ready); + ASSERT_EQ(prepared.load(), 3); + reader.Close(); + reader.Close(); + ASSERT_OK_AND_ASSIGN(uint64_t prepared_readers, + reader.GetReaderMetrics()->GetCounter("test.prepared-readers")); + ASSERT_EQ(prepared_readers, 3); } -TEST(LazyConcatBatchReaderTest, TestAFailureNamesTheFileItCameFrom) { - // A split holds a whole partition, so "the file ended inside a quoted value" says nothing a - // caller could act on until it says which file. Both halves are covered: the file that would - // not open, and the file that opened and then failed part way through. - std::vector failing_open; - failing_open.push_back([]() -> Result> { - return Status::NotImplemented("the compression cannot be decoded"); - }); - LazyConcatBatchReader open_reader(SourcesOf(std::move(failing_open)), - GetArrowPool(GetDefaultPool())); - Result not_opened = open_reader.NextBatch(); - ASSERT_FALSE(not_opened.ok()); - ASSERT_NOK_WITH_MSG(not_opened, "cannot read file-0"); - ASSERT_NOK_WITH_MSG(not_opened, "the compression cannot be decoded"); - // The original code survives, or a caller that tells "not supported" from "bad input" would - // stop being able to. - ASSERT_TRUE(not_opened.status().IsNotImplemented()); - - std::vector failing_read; - failing_read.push_back([]() -> Result> { - return std::make_unique(0, /*batches=*/1, nullptr); - }); - failing_read.push_back([]() -> Result> { - return std::make_unique(Status::Invalid("the file ended mid-record")); - }); - LazyConcatBatchReader read_reader(SourcesOf(std::move(failing_read)), - GetArrowPool(GetDefaultPool())); - ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch first, read_reader.NextBatch()); - ASSERT_FALSE(BatchReader::IsEofBatch(first)); - ReaderUtils::ReleaseReadBatch(std::move(first)); - Result failed = read_reader.NextBatch(); - ASSERT_FALSE(failed.ok()); - // The second file, not the first: the name follows the reader in hand. - ASSERT_NOK_WITH_MSG(failed, "cannot read file-1"); - ASSERT_NOK_WITH_MSG(failed, "the file ended mid-record"); - ASSERT_TRUE(failed.status().IsInvalid()); +TEST(LazyConcatBatchReaderTest, TestClosingDoesNotOpenWhatWasNeverReached) { + for (bool read_first : {false, true}) { + SCOPED_TRACE(read_first); + for (bool explicit_close : {false, true}) { + SCOPED_TRACE(explicit_close); + int32_t opened = 0; + bool first_closed = false; + auto lifetime = std::make_shared(0); + std::weak_ptr weak_lifetime = lifetime; + std::vector factories; + for (int32_t i = 0; i < 3; i++) { + factories.push_back([&opened, &first_closed, i, + lifetime]() -> Result> { + opened++; + return std::make_unique(i, 1, + i == 0 ? &first_closed : nullptr); + }); + } + auto reader = std::make_unique(SourcesOf(std::move(factories)), + GetArrowPool(GetDefaultPool())); + lifetime.reset(); + if (read_first) { + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch first, reader->NextBatch()); + ASSERT_FALSE(BatchReader::IsEofBatch(first)); + ReaderUtils::ReleaseReadBatch(std::move(first)); + } + ASSERT_FALSE(weak_lifetime.expired()); + if (explicit_close) { + reader->Close(); + reader->Close(); + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch eof, reader->NextBatch()); + ASSERT_TRUE(BatchReader::IsEofBatch(eof)); + } else { + reader.reset(); + } + ASSERT_EQ(opened, read_first ? 1 : 0); + ASSERT_EQ(first_closed, read_first); + ASSERT_TRUE(weak_lifetime.expired()); + } + } } -TEST(LazyConcatBatchReaderTest, TestAFactoryMayDeclineToOpenAnything) { - // A factory that returns no reader stands for a file with nothing to read; the next one is - // reached instead of the read ending there. - std::vector factories; - factories.push_back( - []() -> Result> { return std::unique_ptr(); }); - factories.push_back([]() -> Result> { - return std::make_unique(7, /*batches=*/2, nullptr); - }); - LazyConcatBatchReader reader(SourcesOf(std::move(factories)), GetArrowPool(GetDefaultPool())); - ASSERT_EQ(DrainRows(&reader), 2); +TEST(LazyConcatBatchReaderTest, TestFailuresNameSourceAndReleaseResources) { + enum class Failure { Open, Read, Bitmap }; + auto detail = std::make_shared(); + for (Failure failure : {Failure::Open, Failure::Read, Failure::Bitmap}) { + SCOPED_TRACE(static_cast(failure)); + for (int32_t failed_index : {0, 1}) { + SCOPED_TRACE(failed_index); + int32_t opened = 0; + bool closed = false; + auto lifetime = std::make_shared(0); + std::weak_ptr weak_lifetime = lifetime; + const Status expected = + failure == Failure::Open + ? Status::NotImplemented("cannot open the file").WithDetail(detail) + : failure == Failure::Read + ? Status::IOError("cannot read the batch").WithDetail(detail) + : Status::Invalid("at least one valid row"); + std::vector factories; + for (int32_t i = 0; i < 3; ++i) { + factories.push_back([&, i, lifetime]() -> Result> { + ++opened; + if (i == failed_index) { + if (failure == Failure::Open) { + return expected; + } + if (failure == Failure::Read) { + return std::make_unique(expected, &closed); + } + } + return std::make_unique( + i, 1, i == failed_index ? &closed : nullptr, + i == failed_index && failure == Failure::Bitmap); + }); + } + LazyConcatBatchReader reader(SourcesOf(std::move(factories)), + GetArrowPool(GetDefaultPool())); + lifetime.reset(); + for (int32_t i = 0; i < failed_index; ++i) { + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch batch, reader.NextBatch()); + ASSERT_FALSE(BatchReader::IsEofBatch(batch)); + ReaderUtils::ReleaseReadBatch(std::move(batch)); + } + const auto failed = reader.NextBatch(); + ASSERT_NOK_WITH_MSG(failed, fmt::format("cannot read file-{}", failed_index)); + ASSERT_NOK_WITH_MSG(failed, expected.message()); + ASSERT_EQ(failed.status().code(), expected.code()); + ASSERT_EQ(failed.status().detail(), expected.detail()); + ASSERT_EQ(closed, failure != Failure::Open); + ASSERT_TRUE(weak_lifetime.expired()); + ASSERT_NOK_WITH_MSG(reader.NextBatchWithBitmap(), failed.status().message()); + reader.Close(); + ASSERT_NOK_WITH_MSG(reader.NextBatch(), failed.status().message()); + ASSERT_EQ(opened, failed_index + 1); + if (failed_index > 0) { + ASSERT_OK_AND_ASSIGN(uint64_t rows, + reader.GetReaderMetrics()->GetCounter("test.rows")); + ASSERT_EQ(rows, failed_index + (failure == Failure::Bitmap ? 1 : 0)); + } + } + } } -TEST(LazyConcatBatchReaderTest, TestMetricsCoverReadersThatAreAlreadyClosed) { - std::vector factories; - factories.push_back([]() -> Result> { - return std::make_unique(0, /*batches=*/1, nullptr); - }); - LazyConcatBatchReader reader(SourcesOf(std::move(factories)), GetArrowPool(GetDefaultPool())); - ASSERT_EQ(DrainRows(&reader), 1); - // The totals have to survive the reader they were taken from, or everything read through a - // file would vanish from them the moment that file is closed. - ASSERT_NE(reader.GetReaderMetrics(), nullptr); +TEST(LazyConcatBatchReaderTest, TestEmptyAndSkippedSourcesPreserveSelectionAndReleaseFactories) { + auto data = arrow::ipc::internal::json::ArrayFromJSON( + arrow::struct_({arrow::field("id", arrow::int32())}), "[[7], [8], [9], [10]]") + .ValueOrDie(); + for (int32_t source_count : {0, 1, 2, 3}) { + SCOPED_TRACE(source_count); + bool empty_closed = false; + auto lifetime = std::make_shared(0); + std::weak_ptr weak_lifetime = lifetime; + std::vector factories; + for (int32_t i = 0; i < source_count; ++i) { + factories.push_back( + [i, data, lifetime, &empty_closed]() -> Result> { + if (i == 0) { + return std::unique_ptr(); + } + if (i == 1) { + return std::make_unique(7, 0, &empty_closed); + } + auto reader = std::make_unique( + data, data->type(), RoaringBitmap32::From({1, 3}), 2); + reader->EnableRandomizeBatchSize(false); + return reader; + }); + } + LazyConcatBatchReader reader(SourcesOf(std::move(factories)), + GetArrowPool(GetDefaultPool())); + lifetime.reset(); + if (source_count == 3) { + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatchWithBitmap batch, + reader.NextBatchWithBitmap()); + ASSERT_FALSE(BatchReader::IsEofBatch(batch)); + ASSERT_EQ(batch.second, RoaringBitmap32::From({1})); + ASSERT_OK_AND_ASSIGN(std::shared_ptr result, + ReadResultCollector::GetArray(std::move(batch.first))); + ASSERT_TRUE(result->Equals(data->Slice(0, 2))); + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch filtered, reader.NextBatch()); + ASSERT_FALSE(BatchReader::IsEofBatch(filtered)); + ASSERT_OK_AND_ASSIGN(result, ReadResultCollector::GetArray(std::move(filtered))); + ASSERT_TRUE(result->Equals(data->Slice(3, 1))); + } + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatchWithBitmap eof, reader.NextBatchWithBitmap()); + ASSERT_TRUE(BatchReader::IsEofBatch(eof)); + ASSERT_EQ(empty_closed, source_count >= 2); + ASSERT_TRUE(weak_lifetime.expired()); + ASSERT_OK_AND_ASSIGN(eof, reader.NextBatchWithBitmap()); + ASSERT_TRUE(BatchReader::IsEofBatch(eof)); + } } } // namespace paimon::test diff --git a/src/paimon/core/table/source/append_only_table_read.cpp b/src/paimon/core/table/source/append_only_table_read.cpp index cc0cb2a07..5343c6ab9 100644 --- a/src/paimon/core/table/source/append_only_table_read.cpp +++ b/src/paimon/core/table/source/append_only_table_read.cpp @@ -52,6 +52,7 @@ #include "paimon/core/table/source/append_count_reader.h" #include "paimon/core/table/source/data_split_impl.h" #include "paimon/core/table/source/realtime_split.h" +#include "paimon/core/table/source/table_read_utils.h" #include "paimon/predicate/literal.h" #include "paimon/predicate/predicate_builder.h" #include "paimon/predicate/predicate_utils.h" @@ -121,7 +122,10 @@ AppendOnlyTableRead::AppendOnlyTableRead(const std::shared_ptr& context, const std::shared_ptr& memory_pool, const std::shared_ptr& executor) - : context_(context), path_factory_(path_factory) { + : path_factory_(path_factory), + context_(context), + memory_pool_(memory_pool), + executor_(executor) { const auto& core_options = context->GetCoreOptions(); if (!core_options.GetRealtimeDeduplicateKeyFields().empty()) { realtime_file_schema_ = std::make_shared( @@ -146,64 +150,37 @@ Result> AppendOnlyTableRead::CreateReader( if (!realtime_split) { return CreateDiskReader(split); } - return CreateRealtimeReader(realtime_split, /*release_ticket=*/true); + return CreateRealtimeReader(realtime_split); } Result> AppendOnlyTableRead::CreateReader( const std::vector>& splits) { - std::vector> readers; - readers.reserve(splits.size()); - std::vector> realtime_splits; - ScopeGuard cleanup_guard([&]() { - for (const std::unique_ptr& reader : readers) { - if (reader) { - reader->Close(); - } - } - }); - for (const std::shared_ptr& split : splits) { - std::shared_ptr realtime_split = - std::dynamic_pointer_cast(split); - if (realtime_split) { - PAIMON_ASSIGN_OR_RAISE(std::unique_ptr reader, - CreateRealtimeReader(realtime_split, - /*release_ticket=*/false)); - readers.push_back(std::move(reader)); - realtime_splits.push_back(std::move(realtime_split)); - } else { - PAIMON_ASSIGN_OR_RAISE(std::unique_ptr reader, CreateDiskReader(split)); - readers.push_back(std::move(reader)); - } - } - - if (!realtime_splits.empty()) { - const std::shared_ptr realtime_context = context_->GetRealtimeContext(); - if (!realtime_context) { - return Status::Invalid("reading a real-time split requires a real-time context"); - } - PAIMON_ASSIGN_OR_RAISE(std::shared_ptr realtime_context_impl, - RealtimeContextImpl::Cast(realtime_context)); - for (const std::shared_ptr& realtime_split : realtime_splits) { - PAIMON_RETURN_NOT_OK( - realtime_context_impl->ReleaseReadView(realtime_split->OpaqueTicket())); - } - } - return std::make_unique(std::move(readers), context_->GetArrowMemoryPool()); + auto owned_read = + std::make_shared(path_factory_, context_, memory_pool_, executor_); + return TableReadUtils::CreateLazyReader( + splits, owned_read, + [owned_read](const std::shared_ptr& split, + const RealtimePartitionBucketView& view) { + return owned_read->CreateRealtimeReader(split, view); + }, + *context_); } Result> AppendOnlyTableRead::CreateRealtimeReader( - const std::shared_ptr& realtime_split, bool release_ticket) { - if (realtime_split->Version() != RealtimeSplit::kCurrentVersion) { - return Status::Invalid("unsupported real-time split version"); - } - const std::shared_ptr realtime_context = context_->GetRealtimeContext(); - if (!realtime_context) { - return Status::Invalid("reading a real-time split requires a real-time context"); - } - PAIMON_ASSIGN_OR_RAISE(std::shared_ptr realtime_context_impl, - RealtimeContextImpl::Cast(realtime_context)); + const std::shared_ptr& realtime_split) { PAIMON_ASSIGN_OR_RAISE(RealtimePartitionBucketView memory, - realtime_context_impl->ResolveReadView(realtime_split->OpaqueTicket())); + TableReadUtils::ResolveRealtimeReadView(*realtime_split, *context_)); + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr reader, + CreateRealtimeReader(realtime_split, memory)); + PAIMON_ASSIGN_OR_RAISE(std::shared_ptr realtime_context_impl, + RealtimeContextImpl::Cast(context_->GetRealtimeContext())); + PAIMON_RETURN_NOT_OK(realtime_context_impl->ReleaseReadView(realtime_split->OpaqueTicket())); + return reader; +} + +Result> AppendOnlyTableRead::CreateRealtimeReader( + const std::shared_ptr& realtime_split, + const RealtimePartitionBucketView& memory) { std::vector> readers; readers.reserve(realtime_split->DiskSplits().size() + 1); ScopeGuard readers_guard([&readers]() { @@ -213,19 +190,6 @@ Result> AppendOnlyTableRead::CreateRealtimeReader( } } }); - const RealtimePartitionBucket expected_partition_bucket(realtime_split->Partition(), - realtime_split->Bucket()); - if (memory.partition_bucket != expected_partition_bucket) { - return Status::Invalid("real-time read-view ticket belongs to another partition-bucket"); - } - const std::optional memory_range = memory.read_view->GetOffsetRange(); - if (!memory_range || memory_range->end != realtime_split->MemoryEndOffset()) { - return Status::Invalid("real-time read-view ticket does not match the split offset range"); - } - std::shared_ptr table_schema = - DataField::ConvertDataFieldsToArrowSchema(context_->GetTableSchema()->Fields()); - PAIMON_RETURN_NOT_OK(memory.ValidateTableSchema(table_schema)); - for (const std::shared_ptr& disk_split : realtime_split->DiskSplits()) { std::unique_ptr disk_reader; if (memory.mode == RealtimeStoreMode::DEDUPLICATE) { @@ -244,16 +208,14 @@ Result> AppendOnlyTableRead::CreateRealtimeReader( OffsetRange(realtime_split->CommittedEndOffset(), realtime_split->MemoryEndOffset()))); readers.push_back(std::move(memory_reader)); - if (release_ticket) { - PAIMON_RETURN_NOT_OK( - realtime_context_impl->ReleaseReadView(realtime_split->OpaqueTicket())); - } std::unique_ptr result = std::make_unique(std::move(readers), context_->GetArrowMemoryPool()); readers_guard.Release(); return result; } + auto table_schema = + DataField::ConvertDataFieldsToArrowSchema(context_->GetTableSchema()->Fields()); PAIMON_ASSIGN_OR_RAISE(std::unique_ptr schema_layout, RealtimeSchemaLayout::Create(memory.mode, table_schema)); PAIMON_ASSIGN_OR_RAISE(std::unique_ptr pipeline, @@ -308,10 +270,6 @@ Result> AppendOnlyTableRead::CreateRealtimeReader( RealtimeReader::Create(memory.read_view, std::move(memory_reader))); memory_reader = std::move(realtime_reader); } - if (release_ticket) { - PAIMON_RETURN_NOT_OK( - realtime_context_impl->ReleaseReadView(realtime_split->OpaqueTicket())); - } std::unique_ptr result = std::make_unique(std::move(readers), context_->GetArrowMemoryPool()); readers_guard.Release(); diff --git a/src/paimon/core/table/source/append_only_table_read.h b/src/paimon/core/table/source/append_only_table_read.h index 20aae4184..9005183e7 100644 --- a/src/paimon/core/table/source/append_only_table_read.h +++ b/src/paimon/core/table/source/append_only_table_read.h @@ -39,6 +39,7 @@ class MemoryPool; class RealtimeSplit; class RoaringBitmap64; class RealtimeFileSchema; +struct RealtimePartitionBucketView; class AppendOnlyTableRead : public TableRead { public: @@ -58,7 +59,11 @@ class AppendOnlyTableRead : public TableRead { private: Result> CreateRealtimeReader( - const std::shared_ptr& realtime_split, bool release_ticket); + const std::shared_ptr& realtime_split); + + Result> CreateRealtimeReader( + const std::shared_ptr& realtime_split, + const RealtimePartitionBucketView& memory); Result> CreateDiskReader(const std::shared_ptr& split); @@ -67,8 +72,10 @@ class AppendOnlyTableRead : public TableRead { const std::shared_ptr& offset_deletions); std::vector> split_reads_; - std::shared_ptr context_; std::shared_ptr path_factory_; + std::shared_ptr context_; + std::shared_ptr memory_pool_; + std::shared_ptr executor_; std::shared_ptr realtime_file_schema_; }; diff --git a/src/paimon/core/table/source/key_value_table_read.cpp b/src/paimon/core/table/source/key_value_table_read.cpp index 2637f4aca..3f8524932 100644 --- a/src/paimon/core/table/source/key_value_table_read.cpp +++ b/src/paimon/core/table/source/key_value_table_read.cpp @@ -27,7 +27,6 @@ #include "arrow/api.h" #include "arrow/c/bridge.h" -#include "paimon/common/reader/concat_batch_reader.h" #include "paimon/common/types/data_field.h" #include "paimon/common/utils/arrow/status_utils.h" #include "paimon/common/utils/scope_guard.h" @@ -46,6 +45,7 @@ #include "paimon/core/table/source/data_split_impl.h" #include "paimon/core/table/source/pk_count_reader.h" #include "paimon/core/table/source/realtime_split.h" +#include "paimon/core/table/source/table_read_utils.h" #include "paimon/core/utils/nested_projection_utils.h" #include "paimon/core/utils/primary_key_table_utils.h" #include "paimon/predicate/predicate_utils.h" @@ -158,16 +158,19 @@ KeyValueTableRead::KeyValueTableRead( const std::shared_ptr& path_factory, const std::shared_ptr& context, const std::shared_ptr& realtime_primary_key_logical_schema, + bool force_keep_delete, const std::shared_ptr& memory_pool, const std::shared_ptr& executor) : split_reads_(std::move(split_reads)), path_factory_(path_factory), context_(context), realtime_primary_key_logical_schema_(realtime_primary_key_logical_schema), + force_keep_delete_(force_keep_delete), + memory_pool_(memory_pool), executor_(executor) {} -Result> KeyValueTableRead::Create( +Result> KeyValueTableRead::Create( const std::shared_ptr& path_factory, - const std::shared_ptr& context, + const std::shared_ptr& context, bool force_keep_delete, const std::shared_ptr& memory_pool, const std::shared_ptr& executor) { auto raw_file_split_read = std::make_unique(path_factory, context, memory_pool, executor); @@ -183,11 +186,12 @@ Result> KeyValueTableRead::Create( CreateRealtimePrimaryKeyLogicalSchema(merge_file_split_read->GetKeySchema(), merge_file_split_read->GetValueSchema())); } + merge_file_split_read->ForceKeepDelete(force_keep_delete); split_reads.emplace_back(std::move(merge_file_split_read)); - return std::unique_ptr( - new KeyValueTableRead(std::move(split_reads), path_factory, context, - realtime_primary_key_logical_schema, executor)); + return std::unique_ptr(new KeyValueTableRead( + std::move(split_reads), path_factory, context, realtime_primary_key_logical_schema, + force_keep_delete, memory_pool, executor)); } void KeyValueTableRead::ForceKeepDelete(bool force_keep_delete) { @@ -204,7 +208,7 @@ Result> KeyValueTableRead::CreateReader( const std::shared_ptr& split) { std::shared_ptr realtime_split = std::dynamic_pointer_cast(split); if (realtime_split) { - return CreateRealtimeReader(realtime_split, /*release_ticket=*/true); + return CreateRealtimeReader(realtime_split); } std::shared_ptr dispatch_split = split; @@ -260,73 +264,33 @@ Result> KeyValueTableRead::CreateReader( Result> KeyValueTableRead::CreateReader( const std::vector>& splits) { - std::vector> readers; - readers.reserve(splits.size()); - std::vector> realtime_splits; - ScopeGuard cleanup_guard([&]() { - for (const std::unique_ptr& reader : readers) { - if (reader) { - reader->Close(); - } - } - }); - for (const std::shared_ptr& split : splits) { - std::shared_ptr realtime_split = - std::dynamic_pointer_cast(split); - if (realtime_split) { - PAIMON_ASSIGN_OR_RAISE(std::unique_ptr reader, - CreateRealtimeReader(realtime_split, - /*release_ticket=*/false)); - readers.push_back(std::move(reader)); - realtime_splits.push_back(std::move(realtime_split)); - } else { - PAIMON_ASSIGN_OR_RAISE(std::unique_ptr reader, CreateReader(split)); - readers.push_back(std::move(reader)); - } - } - - if (!realtime_splits.empty()) { - const std::shared_ptr realtime_context = context_->GetRealtimeContext(); - if (!realtime_context) { - return Status::Invalid("reading a real-time split requires a real-time context"); - } - PAIMON_ASSIGN_OR_RAISE(std::shared_ptr realtime_context_impl, - RealtimeContextImpl::Cast(realtime_context)); - for (const std::shared_ptr& realtime_split : realtime_splits) { - PAIMON_RETURN_NOT_OK( - realtime_context_impl->ReleaseReadView(realtime_split->OpaqueTicket())); - } - } - return std::make_unique(std::move(readers), context_->GetArrowMemoryPool()); + PAIMON_ASSIGN_OR_RAISE( + std::shared_ptr owned_read, + Create(path_factory_, context_, force_keep_delete_, memory_pool_, executor_)); + return TableReadUtils::CreateLazyReader( + splits, owned_read, + [owned_read](const std::shared_ptr& split, + const RealtimePartitionBucketView& view) { + return owned_read->CreateRealtimeReader(split, view); + }, + *context_); } Result> KeyValueTableRead::CreateRealtimeReader( - const std::shared_ptr& realtime_split, bool release_ticket) { - if (realtime_split->Version() != RealtimeSplit::kCurrentVersion) { - return Status::Invalid("unsupported real-time split version"); - } - if (realtime_split->MemoryEndOffset() < realtime_split->CommittedEndOffset()) { - return Status::Invalid("real-time split memory end offset precedes committed end offset"); - } - const std::shared_ptr realtime_context = context_->GetRealtimeContext(); - if (!realtime_context) { - return Status::Invalid("reading a real-time split requires a real-time context"); - } - PAIMON_ASSIGN_OR_RAISE(std::shared_ptr realtime_context_impl, - RealtimeContextImpl::Cast(realtime_context)); + const std::shared_ptr& realtime_split) { PAIMON_ASSIGN_OR_RAISE(RealtimePartitionBucketView memory, - realtime_context_impl->ResolveReadView(realtime_split->OpaqueTicket())); - const RealtimePartitionBucket expected_partition_bucket(realtime_split->Partition(), - realtime_split->Bucket()); - if (memory.partition_bucket != expected_partition_bucket) { - return Status::Invalid("real-time read-view ticket belongs to another partition-bucket"); - } - const std::optional memory_range = memory.read_view->GetOffsetRange(); - if (!memory_range || memory_range->end != realtime_split->MemoryEndOffset()) { - return Status::Invalid("real-time read-view ticket does not match the split offset range"); - } - PAIMON_RETURN_NOT_OK(memory.ValidateTableSchema( - DataField::ConvertDataFieldsToArrowSchema(context_->GetTableSchema()->Fields()))); + TableReadUtils::ResolveRealtimeReadView(*realtime_split, *context_)); + PAIMON_ASSIGN_OR_RAISE(std::unique_ptr reader, + CreateRealtimeReader(realtime_split, memory)); + PAIMON_ASSIGN_OR_RAISE(std::shared_ptr realtime_context_impl, + RealtimeContextImpl::Cast(context_->GetRealtimeContext())); + PAIMON_RETURN_NOT_OK(realtime_context_impl->ReleaseReadView(realtime_split->OpaqueTicket())); + return reader; +} + +Result> KeyValueTableRead::CreateRealtimeReader( + const std::shared_ptr& realtime_split, + const RealtimePartitionBucketView& memory) { for (const std::unique_ptr& read : split_reads_) { auto* merge_read = dynamic_cast(read.get()); if (merge_read) { @@ -341,10 +305,6 @@ Result> KeyValueTableRead::CreateRealtimeReader( std::move(memory_readers))); PAIMON_ASSIGN_OR_RAISE(std::unique_ptr realtime_reader, RealtimeReader::Create(memory.read_view, std::move(reader))); - if (release_ticket) { - PAIMON_RETURN_NOT_OK( - realtime_context_impl->ReleaseReadView(realtime_split->OpaqueTicket())); - } return std::unique_ptr(std::move(realtime_reader)); } } diff --git a/src/paimon/core/table/source/key_value_table_read.h b/src/paimon/core/table/source/key_value_table_read.h index 431647394..321ac325c 100644 --- a/src/paimon/core/table/source/key_value_table_read.h +++ b/src/paimon/core/table/source/key_value_table_read.h @@ -37,12 +37,13 @@ class FileStorePathFactory; class InternalReadContext; class MemoryPool; class RealtimeSplit; +struct RealtimePartitionBucketView; class KeyValueTableRead : public TableRead { public: - static Result> Create( + static Result> Create( const std::shared_ptr& path_factory, - const std::shared_ptr& context, + const std::shared_ptr& context, bool force_keep_delete, const std::shared_ptr& memory_pool, const std::shared_ptr& executor); Result> CreateReader(const std::shared_ptr& split) override; @@ -60,17 +61,23 @@ class KeyValueTableRead : public TableRead { const std::shared_ptr& path_factory, const std::shared_ptr& context, const std::shared_ptr& realtime_primary_key_logical_schema, + bool force_keep_delete, const std::shared_ptr& memory_pool, const std::shared_ptr& executor); Result> CreateRealtimeReader( - const std::shared_ptr& realtime_split, bool release_ticket); + const std::shared_ptr& realtime_split); + + Result> CreateRealtimeReader( + const std::shared_ptr& realtime_split, + const RealtimePartitionBucketView& memory); std::vector> split_reads_; std::shared_ptr path_factory_; std::shared_ptr context_; std::shared_ptr realtime_primary_key_logical_schema_; + bool force_keep_delete_; + std::shared_ptr memory_pool_; std::shared_ptr executor_; - bool force_keep_delete_ = false; }; } // namespace paimon diff --git a/src/paimon/core/table/source/table_read_test.cpp b/src/paimon/core/table/source/table_read_test.cpp index 893cf0bbc..7e7aff646 100644 --- a/src/paimon/core/table/source/table_read_test.cpp +++ b/src/paimon/core/table/source/table_read_test.cpp @@ -19,6 +19,7 @@ #include "paimon/table/source/table_read.h" +#include #include #include #include @@ -34,6 +35,7 @@ #include "paimon/catalog/table.h" #include "paimon/common/catalog/catalog_context.h" #include "paimon/common/data/binary_row.h" +#include "paimon/common/reader/reader_utils.h" #include "paimon/core/core_options.h" #include "paimon/core/io/data_file_meta.h" #include "paimon/core/operation/abstract_split_read.h" @@ -57,6 +59,7 @@ #include "paimon/table/source/table_scan.h" #include "paimon/testing/mock/mock_search_file_index.h" #include "paimon/testing/utils/binary_row_generator.h" +#include "paimon/testing/utils/counting_reader_file_system.h" #include "paimon/testing/utils/read_result_collector.h" #include "paimon/testing/utils/testharness.h" @@ -363,7 +366,205 @@ TEST(TableReadTest, TestPrimaryKeyFileIndexSearchDoesNotFallBackToMergeReader) { raw_builder.WithSnapshot(1).IsStreaming(false).RawConvertible(true).Build()); ASSERT_OK(reader->CreateReader(raw)); std::vector> splits = {raw, non_raw}; - ASSERT_NOK_WITH_MSG(reader->CreateReader(splits), "merge file reads"); + ASSERT_OK_AND_ASSIGN(std::unique_ptr lazy_reader, reader->CreateReader(splits)); + ASSERT_NOK_WITH_MSG(lazy_reader->NextBatch(), "merge file reads"); + ASSERT_NOK_WITH_MSG(lazy_reader->NextBatchWithBitmap(), "merge file reads"); + + auto* key_value_read = dynamic_cast(reader.get()); + ASSERT_NE(key_value_read, nullptr); + for (bool keep_delete : {false, true}) { + SCOPED_TRACE(keep_delete); + key_value_read->ForceKeepDelete(keep_delete); + ASSERT_OK_AND_ASSIGN(lazy_reader, + reader->CreateReader(std::vector>{raw})); + key_value_read->ForceKeepDelete(!keep_delete); + if (keep_delete) { + ASSERT_NOK_WITH_MSG(lazy_reader->NextBatch(), "merge file reads"); + } else { + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch batch, lazy_reader->NextBatch()); + ASSERT_TRUE(BatchReader::IsEofBatch(batch)); + } + } +} + +TEST(TableReadTest, TestLazySplitsBoundResourcesAndOutliveCreator) { + constexpr int32_t kWindow = 2; + constexpr size_t kFilesPerSplit = 7; + enum class ReadMode { Append, PrimaryKeyRaw, PrimaryKeyMerge }; + for (ReadMode mode : {ReadMode::Append, ReadMode::PrimaryKeyRaw, ReadMode::PrimaryKeyMerge}) { + SCOPED_TRACE(static_cast(mode)); + const bool primary_key = mode != ReadMode::Append; + const bool raw_convertible = mode != ReadMode::PrimaryKeyMerge; + const std::string path = + GetDataDir() + + (mode == ReadMode::PrimaryKeyMerge ? "/orc/pk_table_with_mor.db/pk_table_with_mor" + : primary_key ? "/orc/pk_table_with_dv_cardinality.db/pk_table_with_dv_cardinality" + : "/orc/multi_partition_append_table.db/multi_partition_append_table"); + ScanContextBuilder scan_builder(path); + ASSERT_OK_AND_ASSIGN(std::unique_ptr scan_context, scan_builder.Finish()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr scan, + TableScan::Create(std::move(scan_context))); + ASSERT_OK_AND_ASSIGN(std::shared_ptr plan, scan->CreatePlan()); + ASSERT_FALSE(plan->Splits().empty()); + std::vector> splits; + size_t total_files = 0; + size_t max_files_per_split = 0; + size_t first_split_files = 0; + for (size_t i = 0; i < 3; ++i) { + auto split = + std::dynamic_pointer_cast(plan->Splits()[i % plan->Splits().size()]); + ASSERT_NE(split, nullptr); + ASSERT_FALSE(split->DataFiles().empty()); + if (mode == ReadMode::PrimaryKeyRaw) { + ASSERT_EQ(split->DataFiles()[0]->delete_row_count, std::optional(0)); + } + std::vector> files = split->DataFiles(); + if (raw_convertible) { + files.assign(kFilesPerSplit, split->DataFiles()[0]); + } + total_files += files.size(); + max_files_per_split = std::max(max_files_per_split, files.size()); + if (i == 0) { + first_split_files = files.size(); + } + DataSplitImpl::Builder builder(split->Partition(), split->Bucket(), split->BucketPath(), + std::move(files)); + ASSERT_OK_AND_ASSIGN(std::shared_ptr input_split, + builder.WithSnapshot(1).RawConvertible(raw_convertible).Build()); + splits.push_back(std::move(input_split)); + } + ASSERT_GT(max_files_per_split, kWindow); + const std::map options = { + {Options::READ_PREPARATION_WINDOW, std::to_string(kWindow)}, + {Options::FILE_INDEX_READ_ENABLED, "false"}}; + arrow::ArrayVector expected_chunks; + { + ReadContextBuilder builder(path); + builder.SetOptions(options).EnablePrefetch(false); + ASSERT_OK_AND_ASSIGN(std::unique_ptr context, builder.Finish()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr read, + TableRead::Create(std::move(context))); + for (const auto& split : splits) { + ASSERT_OK_AND_ASSIGN(std::unique_ptr reader, + read->CreateReader(split)); + ASSERT_OK_AND_ASSIGN(std::shared_ptr result, + ReadResultCollector::CollectResult(std::move(reader))); + ASSERT_NE(result, nullptr); + expected_chunks.insert(expected_chunks.end(), result->chunks().begin(), + result->chunks().end()); + } + } + auto expected = std::make_shared(expected_chunks); + auto check_reader = [&](std::vector> input_splits, + int32_t batches_before_close) { + SCOPED_TRACE(batches_before_close); + auto fs = std::make_shared(".orc"); + const auto counts = fs->counts; + std::weak_ptr weak_fs = fs; + std::unique_ptr reader; + { + ReadContextBuilder builder(path); + builder.SetOptions(options).EnablePrefetch(false).WithFileSystem(fs); + ASSERT_OK_AND_ASSIGN(std::unique_ptr context, builder.Finish()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr read, + TableRead::Create(std::move(context))); + ASSERT_OK_AND_ASSIGN(reader, read->CreateReader(input_splits)); + } + input_splits.clear(); + fs.reset(); + ASSERT_EQ(counts->opens.load(), 0); + ASSERT_FALSE(weak_fs.expired()); + if (batches_before_close < 0) { + ASSERT_OK_AND_ASSIGN(std::shared_ptr actual, + ReadResultCollector::CollectResult(reader.get())); + ASSERT_NE(actual, nullptr); + ASSERT_TRUE(actual->Equals(expected)) << actual->ToString(); + ASSERT_EQ(counts->opens.load(), total_files); + ASSERT_TRUE(weak_fs.expired()); + } else if (batches_before_close == 1) { + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch batch, reader->NextBatch()); + ASSERT_FALSE(BatchReader::IsEofBatch(batch)); + if (raw_convertible) { + ASSERT_EQ(counts->opens.load(), kWindow); + } else { + ASSERT_LE(counts->opens.load(), first_split_files); + } + reader->Close(); + ASSERT_OK_AND_ASSIGN(std::shared_ptr array, + ReadResultCollector::GetArray(std::move(batch))); + ASSERT_GT(array->length(), 0); + } + const uint64_t opened = counts->opens.load(); + reader->Close(); + reader->Close(); + ASSERT_EQ(counts->opens.load(), opened); + ASSERT_EQ(counts->live.load(), 0); + ASSERT_LE(counts->peak.load(), raw_convertible ? kWindow : max_files_per_split); + ASSERT_TRUE(weak_fs.expired()); + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatchWithBitmap eof, + reader->NextBatchWithBitmap()); + ASSERT_TRUE(BatchReader::IsEofBatch(eof)); + }; + check_reader(splits, 0); + check_reader(splits, 1); + check_reader(std::move(splits), -1); + } +} + +TEST(TableReadTest, TestLazySplitFailureLeavesLaterSplitsUnopened) { + enum class ReadMode { Append, PrimaryKeyRaw, PrimaryKeyMerge }; + for (ReadMode mode : {ReadMode::Append, ReadMode::PrimaryKeyRaw, ReadMode::PrimaryKeyMerge}) { + SCOPED_TRACE(static_cast(mode)); + const bool raw_convertible = mode != ReadMode::PrimaryKeyMerge; + const std::string path = + GetDataDir() + + (mode == ReadMode::Append + ? "/orc/multi_partition_append_table.db/multi_partition_append_table" + : "/orc/pk_table_with_dv_cardinality.db/pk_table_with_dv_cardinality"); + ScanContextBuilder scan_builder(path); + ASSERT_OK_AND_ASSIGN(std::unique_ptr scan_context, scan_builder.Finish()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr scan, + TableScan::Create(std::move(scan_context))); + ASSERT_OK_AND_ASSIGN(std::shared_ptr plan, scan->CreatePlan()); + ASSERT_FALSE(plan->Splits().empty()); + auto split = std::dynamic_pointer_cast(plan->Splits()[0]); + ASSERT_NE(split, nullptr); + ASSERT_FALSE(split->DataFiles().empty()); + if (mode == ReadMode::PrimaryKeyRaw) { + ASSERT_EQ(split->DataFiles()[0]->delete_row_count, std::optional(0)); + } + auto missing = std::make_shared(*split->DataFiles()[0]); + missing->file_name = "missing.orc"; + DataSplitImpl::Builder valid_builder(split->Partition(), split->Bucket(), + split->BucketPath(), {split->DataFiles()[0]}); + ASSERT_OK_AND_ASSIGN(std::shared_ptr valid, + valid_builder.WithSnapshot(1).RawConvertible(raw_convertible).Build()); + DataSplitImpl::Builder missing_builder(split->Partition(), split->Bucket(), + split->BucketPath(), {missing}); + ASSERT_OK_AND_ASSIGN( + std::shared_ptr invalid, + missing_builder.WithSnapshot(1).RawConvertible(raw_convertible).Build()); + auto fs = std::make_shared(".orc"); + ReadContextBuilder builder(path); + builder.WithFileSystem(fs).EnablePrefetch(false).AddOption(Options::FILE_INDEX_READ_ENABLED, + "false"); + ASSERT_OK_AND_ASSIGN(std::unique_ptr context, builder.Finish()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr read, + TableRead::Create(std::move(context))); + ASSERT_OK_AND_ASSIGN( + std::unique_ptr reader, + read->CreateReader(std::vector>{valid, invalid, valid})); + ASSERT_EQ(fs->counts->opens.load(), 0); + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch batch, reader->NextBatch()); + ASSERT_FALSE(BatchReader::IsEofBatch(batch)); + ReaderUtils::ReleaseReadBatch(std::move(batch)); + ASSERT_NOK_WITH_MSG(ReadResultCollector::CollectResult(reader.get()), "missing.orc"); + ASSERT_NOK_WITH_MSG(reader->NextBatchWithBitmap(), "cannot read split 1"); + reader->Close(); + ASSERT_NOK_WITH_MSG(reader->NextBatch(), "missing.orc"); + ASSERT_EQ(fs->counts->opens.load(), 2); + ASSERT_EQ(fs->counts->live.load(), 0); + } } TEST(TableReadTest, TestCreateAppendOnlyTableRead) { diff --git a/src/paimon/core/table/source/table_read_utils.cpp b/src/paimon/core/table/source/table_read_utils.cpp new file mode 100644 index 000000000..9ad875221 --- /dev/null +++ b/src/paimon/core/table/source/table_read_utils.cpp @@ -0,0 +1,137 @@ +/* + * 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 "paimon/core/table/source/table_read_utils.h" + +#include +#include +#include +#include +#include + +#include "fmt/format.h" +#include "paimon/common/types/data_field.h" +#include "paimon/core/global_index/indexed_split_impl.h" +#include "paimon/core/operation/internal_read_context.h" +#include "paimon/core/table/format/lazy_concat_batch_reader.h" +#include "paimon/core/table/source/data_split_impl.h" +#include "paimon/core/table/source/realtime_split.h" +#include "paimon/table/source/table_read.h" + +namespace paimon { + +namespace { +Status ValidateDiskSplit(std::shared_ptr split, bool allow_indexed) { + if (const auto* indexed = dynamic_cast(split.get())) { + PAIMON_RETURN_NOT_OK(indexed->Validate()); + if (!allow_indexed) { + return Status::Invalid("merge input disk split is not a data split"); + } + split = indexed->GetDataSplit(); + } + const auto* data_split = dynamic_cast(split.get()); + if (!data_split) { + return Status::Invalid("expected a data or indexed split"); + } + const auto& files = data_split->DataFiles(); + const auto& deletion_files = data_split->DeletionFiles(); + if (!deletion_files.empty() && deletion_files.size() != files.size()) { + return Status::Invalid("deletion files must be empty or match data files"); + } + if (std::find(files.begin(), files.end(), nullptr) != files.end()) { + return Status::Invalid("data file is null"); + } + return Status::OK(); +} +} // namespace + +Result TableReadUtils::ResolveRealtimeReadView( + const RealtimeSplit& split, const InternalReadContext& context) { + if (split.Version() != RealtimeSplit::kCurrentVersion) { + return Status::Invalid("unsupported real-time split version"); + } + if (split.CommittedEndOffset() < 0) { + return Status::Invalid("real-time split committed end offset must not be negative"); + } + if (split.MemoryEndOffset() < split.CommittedEndOffset()) { + return Status::Invalid("real-time split memory end offset precedes committed end offset"); + } + for (const auto& disk_split : split.DiskSplits()) { + if (!disk_split) { + return Status::Invalid("real-time disk split is null"); + } + PAIMON_RETURN_NOT_OK(ValidateDiskSplit(disk_split, context.GetPrimaryKeys().empty())); + } + std::shared_ptr realtime_context = context.GetRealtimeContext(); + if (!realtime_context) { + return Status::Invalid("reading a real-time split requires a real-time context"); + } + PAIMON_ASSIGN_OR_RAISE(std::shared_ptr context_impl, + RealtimeContextImpl::Cast(realtime_context)); + PAIMON_ASSIGN_OR_RAISE(RealtimePartitionBucketView memory, + context_impl->ResolveReadView(split.OpaqueTicket())); + if (memory.partition_bucket != RealtimePartitionBucket(split.Partition(), split.Bucket())) { + return Status::Invalid("real-time read-view ticket belongs to another partition-bucket"); + } + const std::optional range = memory.read_view->GetOffsetRange(); + if (!range || range->end != split.MemoryEndOffset()) { + return Status::Invalid("real-time read-view ticket does not match the split offset range"); + } + PAIMON_RETURN_NOT_OK(memory.ValidateTableSchema( + DataField::ConvertDataFieldsToArrowSchema(context.GetTableSchema()->Fields()))); + return memory; +} + +Result> TableReadUtils::CreateLazyReader( + const std::vector>& splits, const std::shared_ptr& read, + const RealtimeReaderFactory& open_realtime, const InternalReadContext& context) { + std::vector sources; + sources.reserve(splits.size()); + std::vector tickets; + for (size_t i = 0; i < splits.size(); ++i) { + const std::shared_ptr& split = splits[i]; + if (!split) { + return Status::Invalid("split is null"); + } + LazyConcatBatchReader::ReaderFactory open; + if (auto realtime_split = std::dynamic_pointer_cast(split)) { + PAIMON_ASSIGN_OR_RAISE(RealtimePartitionBucketView view, + ResolveRealtimeReadView(*realtime_split, context)); + tickets.push_back(realtime_split->OpaqueTicket()); + open = [open_realtime, realtime_split = std::move(realtime_split), + view = std::move(view)]() { return open_realtime(realtime_split, view); }; + } else { + PAIMON_RETURN_NOT_OK(ValidateDiskSplit(split, /*allow_indexed=*/true)); + open = [read, split]() { return read->CreateReader(split); }; + } + sources.push_back({fmt::format("split {}", i), std::move(open)}); + } + auto reader = + std::make_unique(std::move(sources), context.GetArrowMemoryPool()); + if (!tickets.empty()) { + PAIMON_ASSIGN_OR_RAISE(std::shared_ptr context_impl, + RealtimeContextImpl::Cast(context.GetRealtimeContext())); + for (const std::string& ticket : tickets) { + PAIMON_RETURN_NOT_OK(context_impl->ReleaseReadView(ticket)); + } + } + return reader; +} + +} // namespace paimon diff --git a/src/paimon/core/table/source/table_read_utils.h b/src/paimon/core/table/source/table_read_utils.h new file mode 100644 index 000000000..200ef0bf7 --- /dev/null +++ b/src/paimon/core/table/source/table_read_utils.h @@ -0,0 +1,54 @@ +/* + * 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 +#include +#include + +#include "paimon/core/realtime/realtime_context_impl.h" +#include "paimon/reader/batch_reader.h" +#include "paimon/result.h" + +namespace paimon { +class InternalReadContext; +class RealtimeSplit; +class Split; +class TableRead; + +class TableReadUtils { + public: + TableReadUtils() = delete; + ~TableReadUtils() = delete; + + using RealtimeReaderFactory = std::function>( + const std::shared_ptr&, const RealtimePartitionBucketView&)>; + + /// Validates a real-time split and resolves its view without consuming its ticket. + static Result ResolveRealtimeReadView( + const RealtimeSplit& split, const InternalReadContext& context); + + /// The real-time factory must own its read state. It receives the view pinned for its split + /// during validation, even if the caller has since destroyed the TableRead. + static Result> CreateLazyReader( + const std::vector>& splits, const std::shared_ptr& read, + const RealtimeReaderFactory& open_realtime, const InternalReadContext& context); +}; +} // namespace paimon diff --git a/test/inte/nested_column_pruning_inte_test.cpp b/test/inte/nested_column_pruning_inte_test.cpp index b9b1b99ca..ec629c401 100644 --- a/test/inte/nested_column_pruning_inte_test.cpp +++ b/test/inte/nested_column_pruning_inte_test.cpp @@ -33,12 +33,10 @@ #include "paimon/common/types/data_field.h" #include "paimon/common/utils/path_util.h" #include "paimon/common/utils/string_utils.h" -#include "paimon/core/schema/schema_manager.h" #include "paimon/core/schema/table_schema.h" #include "paimon/defs.h" #include "paimon/file_store_commit.h" #include "paimon/file_store_write.h" -#include "paimon/fs/file_system_factory.h" #include "paimon/predicate/literal.h" #include "paimon/predicate/predicate_builder.h" #include "paimon/read_context.h" @@ -419,10 +417,7 @@ TEST_P(NestedColumnPruningInteTest, QueryStructSubFieldsWithNonExistentField) { "does not support schema evolution inside struct"); } -// Test: Nested schema divergence (simulated evolution mismatch) must fail fast -// instead of silently skipping nested fields. -TEST_P(NestedColumnPruningInteTest, QueryStructSubFieldsWithTypeMismatchShouldFail) { - // File schema (old): f1.a is INT32. +TEST_P(NestedColumnPruningInteTest, NestedTypeMismatchFailsWhenReadingSplit) { auto struct_type = arrow::struct_({ arrow::field("a", arrow::int32()), arrow::field("b", arrow::utf8()), @@ -450,148 +445,42 @@ TEST_P(NestedColumnPruningInteTest, QueryStructSubFieldsWithTypeMismatchShouldFa ASSERT_OK_AND_ASSIGN(auto batch, TestHelper::MakeRecordBatch(arrow::struct_(table_fields), data, /*partition_map=*/{}, /*bucket=*/0, {})); - int64_t commit_identifier = 0; - ASSERT_OK_AND_ASSIGN(auto commit_msgs, - helper->WriteAndCommit(std::move(batch), commit_identifier++, - /*expected_commit_messages=*/std::nullopt)); - - // Persist schema-2 on disk so table latest schema diverges from old data files. - std::string file_system_identifier = "local"; - auto fs_iter = options.find(Options::FILE_SYSTEM); - if (fs_iter != options.end()) { - file_system_identifier = StringUtils::ToLowerCase(fs_iter->second); - } - ASSERT_OK_AND_ASSIGN(auto file_system, - FileSystemFactory::Get(file_system_identifier, table_path_, options)); - std::shared_ptr schema_fs(std::move(file_system)); - - SchemaManager schema_manager(schema_fs, table_path_); - ASSERT_OK_AND_ASSIGN(auto latest_schema_opt, schema_manager.Latest()); - ASSERT_TRUE(latest_schema_opt.has_value()); - auto latest_schema = latest_schema_opt.value(); + ASSERT_OK(helper->WriteAndCommit(std::move(batch), 0, std::nullopt)); - auto schema_v2_arrow = arrow::schema({ + auto evolved_schema = arrow::schema({ arrow::field("f0", arrow::int32()), arrow::field("f1", arrow::struct_({ arrow::field("a", arrow::utf8()), arrow::field("b", arrow::utf8()), })), }); - ASSERT_OK_AND_ASSIGN( - auto schema_v2, - TableSchema::Create(/*schema_id=*/latest_schema->Id() + 1, schema_v2_arrow, - latest_schema->PartitionKeys(), latest_schema->PrimaryKeys(), - latest_schema->Options())); - ASSERT_OK_AND_ASSIGN(auto schema_v2_json, schema_v2->ToJsonString()); - auto schema_v2_path = PathUtil::JoinPath(schema_manager.SchemaDirectory(), - "schema-" + std::to_string(schema_v2->Id())); - ASSERT_OK(schema_fs->AtomicStore(schema_v2_path, schema_v2_json)); + ASSERT_OK_AND_ASSIGN(auto evolved_table, + TableSchema::Create(1, evolved_schema, {}, {}, options)); + ASSERT_OK(TestHelper::WriteNextSchema(dir_->GetFileSystem(), table_path_, + evolved_table->Fields(), evolved_table->HighestFieldId(), + options)); ASSERT_OK_AND_ASSIGN(auto data_splits, helper->NewScan(StartupMode::LatestFull(), /*snapshot_id=*/std::nullopt)); ASSERT_FALSE(data_splits.empty()); - ReadContextBuilder read_context_builder(table_path_); - read_context_builder.SetOptions(options); - ASSERT_OK_AND_ASSIGN(auto read_context, read_context_builder.Finish()); - ASSERT_OK_AND_ASSIGN(auto table_read, TableRead::Create(std::move(read_context))); - - ASSERT_NOK_WITH_MSG(table_read->CreateReader(data_splits), - "PruneDataType nested field type mismatch for 'a': read string vs " - "data int32"); -} - -// Test: With SetReadSchema using the new schema, context build should pass, -// and mismatch against old file type should be rejected in reader creation. -TEST_P(NestedColumnPruningInteTest, - QueryStructSubFieldsWithTypeMismatchAndSetReadSchemaFailAtContext) { - // File schema (old): f1.a is INT32. - auto struct_type = arrow::struct_({ - arrow::field("a", arrow::int32()), - arrow::field("b", arrow::utf8()), - }); - arrow::FieldVector table_fields = { - arrow::field("f0", arrow::int32()), - arrow::field("f1", struct_type), - }; - auto table_schema = arrow::schema(table_fields); - - std::map options = { - {Options::FILE_FORMAT, StringUtils::ToUpperCase(file_format_)}, - {Options::TARGET_FILE_SIZE, "1024"}, - {Options::BUCKET, "-1"}, - }; - - ASSERT_OK_AND_ASSIGN( - auto helper, TestHelper::Create(test_dir_, table_schema, /*partition_keys=*/{}, - /*primary_keys=*/{}, options, /*is_streaming_mode=*/false)); - - std::string data = R"([ - [1, [11, "x"]], - [2, [22, "y"]] - ])"; - ASSERT_OK_AND_ASSIGN(auto batch, - TestHelper::MakeRecordBatch(arrow::struct_(table_fields), data, - /*partition_map=*/{}, /*bucket=*/0, {})); - int64_t commit_identifier = 0; - ASSERT_OK_AND_ASSIGN(auto commit_msgs, - helper->WriteAndCommit(std::move(batch), commit_identifier++, - /*expected_commit_messages=*/std::nullopt)); - - // Persist schema-2 on disk so table latest schema diverges from old data files. - std::string file_system_identifier = "local"; - auto fs_iter = options.find(Options::FILE_SYSTEM); - if (fs_iter != options.end()) { - file_system_identifier = StringUtils::ToLowerCase(fs_iter->second); + for (bool set_read_schema : {false, true}) { + SCOPED_TRACE(set_read_schema); + ReadContextBuilder builder(table_path_); + builder.SetOptions(options); + if (set_read_schema) { + auto c_schema = std::make_unique(); + ASSERT_TRUE(arrow::ExportSchema(*evolved_schema, c_schema.get()).ok()); + builder.SetReadSchema(std::move(c_schema)); + } + ASSERT_OK_AND_ASSIGN(auto read_context, builder.Finish()); + ASSERT_OK_AND_ASSIGN(auto table_read, TableRead::Create(std::move(read_context))); + ASSERT_OK_AND_ASSIGN(std::unique_ptr reader, + table_read->CreateReader(data_splits)); + ASSERT_NOK_WITH_MSG(reader->NextBatch(), + "PruneDataType nested field type mismatch for 'a': read string vs " + "data int32"); } - ASSERT_OK_AND_ASSIGN(auto file_system, - FileSystemFactory::Get(file_system_identifier, table_path_, options)); - std::shared_ptr schema_fs(std::move(file_system)); - - SchemaManager schema_manager(schema_fs, table_path_); - ASSERT_OK_AND_ASSIGN(auto latest_schema_opt, schema_manager.Latest()); - ASSERT_TRUE(latest_schema_opt.has_value()); - auto latest_schema = latest_schema_opt.value(); - - auto schema_v2_arrow = arrow::schema({ - arrow::field("f0", arrow::int32()), - arrow::field("f1", arrow::struct_({ - arrow::field("a", arrow::utf8()), - arrow::field("b", arrow::utf8()), - })), - }); - ASSERT_OK_AND_ASSIGN( - auto schema_v2, - TableSchema::Create(/*schema_id=*/latest_schema->Id() + 1, schema_v2_arrow, - latest_schema->PartitionKeys(), latest_schema->PrimaryKeys(), - latest_schema->Options())); - ASSERT_OK_AND_ASSIGN(auto schema_v2_json, schema_v2->ToJsonString()); - auto schema_v2_path = PathUtil::JoinPath(schema_manager.SchemaDirectory(), - "schema-" + std::to_string(schema_v2->Id())); - ASSERT_OK(schema_fs->AtomicStore(schema_v2_path, schema_v2_json)); - - // User-provided read schema uses latest nested type (a:string). - auto projected_schema = arrow::schema({ - arrow::field("f0", arrow::int32()), - arrow::field("f1", arrow::struct_({ - arrow::field("a", arrow::utf8()), - arrow::field("b", arrow::utf8()), - })), - }); - auto c_schema = std::make_unique(); - ASSERT_TRUE(arrow::ExportSchema(*projected_schema, c_schema.get()).ok()); - - ReadContextBuilder read_context_builder(table_path_); - read_context_builder.SetOptions(options).SetReadSchema(std::move(c_schema)); - ASSERT_OK_AND_ASSIGN(auto read_context, read_context_builder.Finish()); - ASSERT_OK_AND_ASSIGN(auto table_read, TableRead::Create(std::move(read_context))); - ASSERT_OK_AND_ASSIGN(auto data_splits, - helper->NewScan(StartupMode::LatestFull(), /*snapshot_id=*/std::nullopt)); - ASSERT_FALSE(data_splits.empty()); - - ASSERT_NOK_WITH_MSG(table_read->CreateReader(data_splits), - "PruneDataType nested field type mismatch for 'a': read string vs " - "data int32"); } // Test: Read only top-level fields, skip struct entirely. @@ -1468,8 +1357,9 @@ TEST_P(NestedColumnPruningInteTest, PruneListStructSubFields) { read_context_builder.SetOptions(options).SetReadSchema(std::move(c_schema)); ASSERT_OK_AND_ASSIGN(auto read_context, read_context_builder.Finish()); ASSERT_OK_AND_ASSIGN(auto table_read, TableRead::Create(std::move(read_context))); - auto create_reader_result = table_read->CreateReader(data_splits); - ASSERT_NOK_WITH_MSG(create_reader_result, "partial projection inside list"); + ASSERT_OK_AND_ASSIGN(std::unique_ptr reader, + table_read->CreateReader(data_splits)); + ASSERT_NOK_WITH_MSG(reader->NextBatch(), "partial projection inside list"); } std::vector GetTestValuesForNestedColumnPruningInteTest() { diff --git a/test/inte/read_inte_test.cpp b/test/inte/read_inte_test.cpp index c9a0391b3..cdb354ccf 100644 --- a/test/inte/read_inte_test.cpp +++ b/test/inte/read_inte_test.cpp @@ -44,8 +44,6 @@ #include "paimon/commit_context.h" #include "paimon/common/data/binary_row.h" #include "paimon/common/factories/io_hook.h" -#include "paimon/common/reader/complete_row_kind_batch_reader.h" -#include "paimon/common/reader/concat_batch_reader.h" #include "paimon/common/table/special_fields.h" #include "paimon/common/types/data_field.h" #include "paimon/common/types/data_type.h" @@ -85,6 +83,7 @@ #include "paimon/table/source/table_read.h" #include "paimon/table/source/table_scan.h" #include "paimon/testing/utils/binary_row_generator.h" +#include "paimon/testing/utils/counting_reader_file_system.h" #include "paimon/testing/utils/io_exception_helper.h" #include "paimon/testing/utils/read_result_collector.h" #include "paimon/testing/utils/test_helper.h" @@ -380,8 +379,9 @@ TEST_P(ReadInteTest, TestAppendSimple) { DataField(2, arrow::field("f2", arrow::int32())), DataField(3, arrow::field("f3", arrow::float64()))}; + auto fs = std::make_shared("." + param.file_format); ReadContextBuilder context_builder(path); - context_builder.AddOption(Options::FILE_FORMAT, param.file_format); + context_builder.WithFileSystem(fs).AddOption(Options::FILE_FORMAT, param.file_format); context_builder.EnablePrefetch(param.enable_prefetch) .AddOption("test.enable-adaptive-prefetch-strategy", "false") .AddOption("orc.read.enable-metrics", "true"); @@ -410,19 +410,12 @@ TEST_P(ReadInteTest, TestAppendSimple) { auto data_splits = CreateDataSplits(input_data_splits, /*snapshot_id=*/3); ASSERT_EQ(data_splits.size(), 1); ASSERT_OK_AND_ASSIGN(auto batch_reader, table_read->CreateReader(data_splits)); + ASSERT_EQ(fs->counts->opens.load(), 0); - auto split_concat_batch_reader = dynamic_cast(batch_reader.get()); - ASSERT_TRUE(split_concat_batch_reader); - ASSERT_EQ(1, split_concat_batch_reader->readers_.size()); - auto complete_batch_reader = dynamic_cast( - split_concat_batch_reader->readers_[0].reader.get()); - ASSERT_TRUE(complete_batch_reader); - auto file_concat_batch_reader = - dynamic_cast(complete_batch_reader->reader_.get()); - ASSERT_TRUE(file_concat_batch_reader); - ASSERT_EQ(2, file_concat_batch_reader->readers_.size()); ASSERT_OK_AND_ASSIGN(auto result_array, ReadResultCollector::CollectResult(batch_reader.get())); + ASSERT_GE(fs->counts->opens.load(), file_list.size()); + ASSERT_NE(result_array, nullptr); std::shared_ptr read_metrics = batch_reader->GetReaderMetrics(); auto fields_with_row_kind = read_fields; diff --git a/test/inte/realtime_write_inte_test.cpp b/test/inte/realtime_write_inte_test.cpp index 83aff9c4a..2b1f74dbd 100644 --- a/test/inte/realtime_write_inte_test.cpp +++ b/test/inte/realtime_write_inte_test.cpp @@ -57,6 +57,7 @@ #include "paimon/core/core_options.h" #include "paimon/core/deletionvectors/bitmap_deletion_vector.h" #include "paimon/core/deletionvectors/deletion_vectors_index_file.h" +#include "paimon/core/global_index/indexed_split_impl.h" #include "paimon/core/io/data_file_path_factory.h" #include "paimon/core/io/file_index_evaluator.h" #include "paimon/core/operation/commit/manifest_entry_changes.h" @@ -204,10 +205,12 @@ class QueryTrackingRealtimeStore final : public DelegatingRealtimeStore { public: QueryTrackingRealtimeStore(const std::shared_ptr& delegate, const std::shared_ptr>& saw_query_predicate, - const std::shared_ptr>& query_view) + const std::shared_ptr>& query_view, + const std::shared_ptr>& query_count = nullptr) : DelegatingRealtimeStore(delegate), saw_query_predicate_(saw_query_predicate), - query_view_(query_view) {} + query_view_(query_view), + query_count_(query_count) {} Result> AcquireReadView() override { PAIMON_ASSIGN_OR_RAISE(std::shared_ptr delegate_view, @@ -219,6 +222,9 @@ class QueryTrackingRealtimeStore final : public DelegatingRealtimeStore { Result>> CreateQueryReaders( const std::shared_ptr& view, const RealtimeQueryContext& context) override { + if (query_count_) { + ++*query_count_; + } if (context.predicate) { saw_query_predicate_->store(true, std::memory_order_release); } @@ -234,6 +240,7 @@ class QueryTrackingRealtimeStore final : public DelegatingRealtimeStore { private: std::shared_ptr> saw_query_predicate_; std::shared_ptr> query_view_; + std::shared_ptr> query_count_; }; // Publishes a competing snapshot after the attempt has already materialized its DVs. @@ -912,8 +919,7 @@ class RealtimeWriteInteTest : public ::testing::Test { return scan->CreatePlan(); } - Result> CreateQueryReader( - const std::shared_ptr& plan, + Result> CreateQueryTableRead( const std::shared_ptr& realtime_context) const { ReadContextBuilder read_builder(table_path_); read_builder.SetOptions(options_) @@ -921,8 +927,14 @@ class RealtimeWriteInteTest : public ::testing::Test { .WithRealtimeContext(realtime_context) .WithMemoryPool(pool_); PAIMON_ASSIGN_OR_RAISE(std::unique_ptr read_context, read_builder.Finish()); + return TableRead::Create(std::move(read_context)); + } + + Result> CreateQueryReader( + const std::shared_ptr& plan, + const std::shared_ptr& realtime_context) const { PAIMON_ASSIGN_OR_RAISE(std::unique_ptr table_read, - TableRead::Create(std::move(read_context))); + CreateQueryTableRead(realtime_context)); return table_read->CreateReader(plan->Splits()); } @@ -1012,8 +1024,10 @@ class RealtimeWriteInteTest : public ::testing::Test { PAIMON_ASSIGN_OR_RAISE(std::shared_ptr read_result, ReadPlan(plan, realtime_context, {"id", "payload", "pt"}, predicate, enable_predicate_filter)); - const std::shared_ptr& result = read_result; + return ToRows(read_result); + } + static Result> ToRows(const std::shared_ptr& result) { std::vector rows; if (!result) { return rows; @@ -4207,16 +4221,16 @@ TEST_F(RealtimeWriteInteTest, TestPkRead) { TableRead::Create(std::move(read_context))); ASSERT_OK_AND_ASSIGN(std::unique_ptr reader, table_read->CreateReader(lifetime_plan->Splits())); - ASSERT_FALSE(query_view->expired()); std::weak_ptr weak_context = realtime_context; table_read.reset(); lifetime_plan.reset(); realtime_context.reset(); - ASSERT_TRUE(weak_context.expired()); - ASSERT_FALSE(query_view->expired()); + ASSERT_FALSE(weak_context.expired()); ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch read_batch, reader->NextBatch()); ASSERT_FALSE(BatchReader::IsEofBatch(read_batch)); + ASSERT_TRUE(weak_context.expired()); + ASSERT_FALSE(query_view->expired()); reader->Close(); reader.reset(); ASSERT_TRUE(query_view->expired()); @@ -6348,8 +6362,8 @@ TEST_F(RealtimeWriteInteTest, TestPlanExcludesRowsWrittenAfterMemoryEndOffset) { ASSERT_OK(PrepareAndClose(writer.get())); } -TEST_F(RealtimeWriteInteTest, TestReadFailsAfterRealtimeSplitTicketExpires) { - options_[Options::REALTIME_READ_VIEW_TTL] = "10 ms"; +TEST_F(RealtimeWriteInteTest, TestTicketExpiryPreservesCreatedReader) { + options_[Options::REALTIME_READ_VIEW_TTL] = "1 s"; CreateTable(/*partition_keys=*/{}); ASSERT_OK_AND_ASSIGN(std::shared_ptr realtime_context, RealtimeContext::Create()); @@ -6359,46 +6373,20 @@ TEST_F(RealtimeWriteInteTest, TestReadFailsAfterRealtimeSplitTicketExpires) { ASSERT_OK_AND_ASSIGN(std::unique_ptr batch, MakeBatch(rows, /*partitioned=*/false)); ASSERT_OK(writer->Write(std::move(batch))); + ASSERT_OK_AND_ASSIGN(std::unique_ptr reader, CreateQueryReader(realtime_context)); ASSERT_OK_AND_ASSIGN(std::shared_ptr plan, CreatePlan(realtime_context, /*predicate=*/nullptr)); - std::this_thread::sleep_for(std::chrono::milliseconds(50)); + std::this_thread::sleep_for(std::chrono::milliseconds(1100)); ASSERT_NOK_WITH_MSG(ReadRows(plan, realtime_context), "ticket does not exist or has expired"); + ASSERT_OK_AND_ASSIGN(std::shared_ptr result, + ReadResultCollector::CollectResult(std::move(reader))); + ASSERT_OK_AND_ASSIGN(std::vector actual, ToRows(result)); + ASSERT_EQ(actual, rows); ASSERT_OK(PrepareAndClose(writer.get())); } -TEST_F(RealtimeWriteInteTest, TestSuccessfulReaderCreationConsumesRealtimeSplitTicket) { - CreateTable(/*partition_keys=*/{}); - ASSERT_OK_AND_ASSIGN(std::shared_ptr realtime_context, - RealtimeContext::Create()); - ASSERT_OK_AND_ASSIGN(std::unique_ptr writer, - CreateRealtimeWriter(realtime_context)); - std::vector rows = MakeRows(/*first_id=*/0, /*count=*/3, /*partition=*/"p0"); - ASSERT_OK_AND_ASSIGN(std::unique_ptr batch, - MakeBatch(rows, /*partitioned=*/false)); - ASSERT_OK(writer->Write(std::move(batch))); - ASSERT_OK_AND_ASSIGN(std::shared_ptr plan, - CreatePlan(realtime_context, /*predicate=*/nullptr)); - - ReadContextBuilder read_builder(table_path_); - read_builder.SetOptions(options_) - .SetReadFieldNames({"id", "payload", "pt"}) - .WithRealtimeContext(realtime_context) - .WithMemoryPool(pool_); - ASSERT_OK_AND_ASSIGN(std::unique_ptr read_context, read_builder.Finish()); - ASSERT_OK_AND_ASSIGN(std::unique_ptr table_read, - TableRead::Create(std::move(read_context))); - ASSERT_OK_AND_ASSIGN(std::unique_ptr first_reader, - table_read->CreateReader(plan->Splits())); - first_reader->Close(); - first_reader.reset(); - - ASSERT_NOK_WITH_MSG(table_read->CreateReader(plan->Splits()), - "ticket does not exist or has expired"); - ASSERT_OK(PrepareAndClose(writer.get())); -} - -TEST_F(RealtimeWriteInteTest, TestFailedReaderCreationPreservesRealtimeSplitTicket) { +TEST_F(RealtimeWriteInteTest, TestRealtimeReaderFailureTimingAndTicketConsumption) { CreateTable(/*partition_keys=*/{}); ASSERT_OK_AND_ASSIGN(std::shared_ptr realtime_context, RealtimeContext::Create()); @@ -6422,14 +6410,188 @@ TEST_F(RealtimeWriteInteTest, TestFailedReaderCreationPreservesRealtimeSplitTick ASSERT_OK_AND_ASSIGN(std::unique_ptr read_context, read_builder.Finish()); ASSERT_OK_AND_ASSIGN(std::unique_ptr table_read, TableRead::Create(std::move(read_context))); - ASSERT_NOK_WITH_MSG(table_read->CreateReader(plan->Splits()), + ASSERT_NOK_WITH_MSG(table_read->CreateReader(plan->Splits().front()), "cannot cast predicate unsupported"); ASSERT_OK_AND_ASSIGN(std::vector actual_rows, ReadRows(plan, realtime_context)); ASSERT_EQ(rows, actual_rows); + ASSERT_OK_AND_ASSIGN(plan, CreatePlan(realtime_context, nullptr)); + ASSERT_OK_AND_ASSIGN(std::unique_ptr reader, + table_read->CreateReader(plan->Splits())); + ASSERT_NOK_WITH_MSG(reader->NextBatch(), "cannot cast predicate unsupported"); + ASSERT_NOK_WITH_MSG(reader->NextBatchWithBitmap(), "cannot cast predicate unsupported"); + reader->Close(); + ASSERT_NOK_WITH_MSG(ReadRows(plan, realtime_context), "ticket does not exist or has expired"); + + read_builder.SetOptions(options_) + .SetReadFieldNames({"id", "payload", "pt"}) + .WithRealtimeContext(realtime_context) + .WithMemoryPool(pool_); + ASSERT_OK_AND_ASSIGN(read_context, read_builder.Finish()); + ASSERT_OK_AND_ASSIGN(table_read, TableRead::Create(std::move(read_context))); + for (bool multi_split : {false, true}) { + SCOPED_TRACE(multi_split); + ASSERT_OK_AND_ASSIGN(plan, CreatePlan(realtime_context, nullptr)); + ASSERT_EQ(plan->Splits().size(), 1); + ASSERT_OK_AND_ASSIGN(reader, multi_split + ? table_read->CreateReader(plan->Splits()) + : table_read->CreateReader(plan->Splits().front())); + reader->Close(); + ASSERT_NOK_WITH_MSG(table_read->CreateReader(plan->Splits().front()), + "ticket does not exist or has expired"); + ASSERT_NOK_WITH_MSG(table_read->CreateReader(plan->Splits()), + "ticket does not exist or has expired"); + } ASSERT_OK(PrepareAndClose(writer.get())); } +TEST_F(RealtimeWriteInteTest, TestLazyMultiSplitReadersRetainViewsAndStopOpening) { + for (bool primary_key : {false, true}) { + SCOPED_TRACE(primary_key); + dir_ = UniqueTestDirectory::Create("local"); + ASSERT_NE(dir_, nullptr); + table_path_ = PathUtil::JoinPath(dir_->Str(), "foo.db/bar"); + if (primary_key) { + CreatePkTable({"pt"}); + } else { + CreateTable({"pt"}); + } + auto query_count = std::make_shared>(0); + auto factory = MakeDecoratingFactory( + std::make_shared>(false), + std::make_shared>(), query_count); + ASSERT_OK_AND_ASSIGN(std::shared_ptr realtime_context, + RealtimeContext::Create(factory)); + ASSERT_OK_AND_ASSIGN(std::shared_ptr context_impl, + RealtimeContextImpl::Cast(realtime_context)); + ASSERT_OK_AND_ASSIGN(std::unique_ptr writer, + CreateRealtimeWriter(realtime_context)); + const std::map ids = {{"p0", 0}, {"p1", 10}, {"p2", 20}}; + for (const auto& [partition, id] : ids) { + ASSERT_OK_AND_ASSIGN(std::unique_ptr batch, + MakeBatch({Row{id, "value", partition}}, true)); + ASSERT_OK(writer->Write(std::move(batch))); + } + for (int32_t batches_before_close : {0, 1, -1}) { + SCOPED_TRACE(batches_before_close); + query_count->store(0); + ASSERT_OK_AND_ASSIGN(std::shared_ptr plan, CreatePlan(realtime_context, nullptr)); + ASSERT_EQ(plan->Splits().size(), ids.size()); + std::vector expected; + std::vector tickets; + for (const auto& split : plan->Splits()) { + auto realtime_split = std::dynamic_pointer_cast(split); + ASSERT_NE(realtime_split, nullptr); + const std::string& partition = realtime_split->Partition().at("pt"); + expected.emplace_back(ids.at(partition), "value", partition); + tickets.push_back(realtime_split->OpaqueTicket()); + } + ASSERT_OK_AND_ASSIGN(std::unique_ptr reader, + CreateQueryReader(plan, realtime_context)); + plan.reset(); + ASSERT_EQ(query_count->load(), 0); + for (const std::string& ticket : tickets) { + ASSERT_NOK_WITH_MSG(context_impl->ResolveReadView(ticket), + "ticket does not exist or has expired"); + } + if (batches_before_close < 0) { + ASSERT_OK_AND_ASSIGN(std::shared_ptr result, + ReadResultCollector::CollectResult(reader.get())); + ASSERT_OK_AND_ASSIGN(std::vector actual, ToRows(result)); + ASSERT_EQ(actual, expected); + ASSERT_EQ(query_count->load(), ids.size()); + } else if (batches_before_close == 1) { + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatchWithBitmap batch, + reader->NextBatchWithBitmap()); + ASSERT_FALSE(BatchReader::IsEofBatch(batch)); + ReaderUtils::ReleaseReadBatch(std::move(batch.first)); + ASSERT_EQ(query_count->load(), 1); + } + const int32_t opened = query_count->load(); + reader->Close(); + reader->Close(); + ASSERT_OK_AND_ASSIGN(BatchReader::ReadBatch eof, reader->NextBatch()); + ASSERT_TRUE(BatchReader::IsEofBatch(eof)); + ASSERT_EQ(query_count->load(), opened); + } + query_count->store(0); + ASSERT_OK_AND_ASSIGN(std::shared_ptr plan, CreatePlan(realtime_context, nullptr)); + ASSERT_EQ(plan->Splits().size(), ids.size()); + ASSERT_OK_AND_ASSIGN(std::unique_ptr read, + CreateQueryTableRead(realtime_context)); + auto first = std::dynamic_pointer_cast(plan->Splits().front()); + ASSERT_NE(first, nullptr); + const int32_t version = first->Version(); + const int32_t bucket = first->Bucket(); + const int64_t committed_end = first->CommittedEndOffset(); + const int64_t memory_end = first->MemoryEndOffset(); + auto check_invalid = [&](int32_t split_version, int32_t split_bucket, int64_t begin, + int64_t end, std::vector> disk_splits, + const char* error) { + SCOPED_TRACE(error); + auto split = std::make_shared( + split_version, first->SnapshotId(), first->Partition(), split_bucket, + std::move(disk_splits), begin, end, first->OpaqueTicket()); + ASSERT_NOK_WITH_MSG( + read->CreateReader(std::vector>{plan->Splits()[1], split}), + error); + ASSERT_NOK_WITH_MSG(read->CreateReader(split), error); + }; + check_invalid(version + 1, bucket, committed_end, memory_end, {}, + "unsupported real-time split version"); + check_invalid(version, bucket + 1, committed_end, memory_end, {}, + "belongs to another partition-bucket"); + check_invalid(version, bucket, -1, memory_end, {}, + "committed end offset must not be negative"); + check_invalid(version, bucket, memory_end + 1, memory_end, {}, + "memory end offset precedes committed end offset"); + check_invalid(version, bucket, committed_end, memory_end + 1, {}, + "does not match the split offset range"); + check_invalid(version, bucket, committed_end, memory_end, {nullptr}, "disk split is null"); + ASSERT_NOK_WITH_MSG(read->CreateReader(std::vector>{first, nullptr}), + "split is null"); + auto check_invalid_disk = [&](const std::shared_ptr& disk_split, const char* error) { + SCOPED_TRACE(error); + ASSERT_NOK_WITH_MSG( + read->CreateReader(std::vector>{first, disk_split}), error); + check_invalid(version, bucket, committed_end, memory_end, {disk_split}, error); + }; + ASSERT_OK_AND_ASSIGN(std::shared_ptr empty, + DataSplitImpl::Builder(BinaryRow::EmptyRow(), bucket, "", {}).Build()); + check_invalid_disk(std::make_shared(nullptr, std::vector{}), + "indexed split has no data split"); + check_invalid_disk(std::make_shared(empty, std::vector{{0, 0}}), + "row ranges mismatch data files"); + if (primary_key) { + auto indexed = std::make_shared(empty, std::vector{}); + check_invalid(version, bucket, committed_end, memory_end, {indexed}, + "merge input disk split is not a data split"); + } + DataSplitImpl::Builder invalid_deletions(BinaryRow::EmptyRow(), bucket, "", {}); + ASSERT_OK_AND_ASSIGN(std::shared_ptr misaligned, + invalid_deletions.WithDataDeletionFiles({std::nullopt}).Build()); + check_invalid_disk(misaligned, "deletion files must be empty or match data files"); + DataSplitImpl::Builder missing_file(BinaryRow::EmptyRow(), bucket, "", {nullptr}); + ASSERT_OK_AND_ASSIGN(std::shared_ptr missing, missing_file.Build()); + check_invalid_disk(missing, "data file is null"); + check_invalid_disk(std::make_shared(), "expected a data or indexed split"); + SchemaManager schemas(dir_->GetFileSystem(), table_path_); + ASSERT_OK_AND_ASSIGN(std::shared_ptr original, schemas.ReadSchema(0)); + std::vector evolved_fields = original->Fields(); + const int32_t new_field_id = original->HighestFieldId() + 1; + evolved_fields.emplace_back(new_field_id, arrow::field("extra", arrow::utf8())); + ASSERT_OK(TestHelper::WriteNextSchema(dir_->GetFileSystem(), table_path_, evolved_fields, + new_field_id, original->Options())); + ASSERT_NOK_WITH_MSG(CreateQueryReader(plan, realtime_context), + "real-time store schema mismatch"); + ASSERT_EQ(query_count->load(), 0); + ASSERT_OK_AND_ASSIGN(std::unique_ptr retry, + read->CreateReader(plan->Splits())); + retry->Close(); + ASSERT_OK(PrepareAndClose(writer.get())); + } +} + TEST_F(RealtimeWriteInteTest, TestCloseWriterWithBuildingDataInvalidatesContext) { CreateTable(/*partition_keys=*/{}); ASSERT_OK_AND_ASSIGN(std::shared_ptr realtime_context, diff --git a/test/inte/scan_and_read_inte_test.cpp b/test/inte/scan_and_read_inte_test.cpp index a19dc374a..7c3f25015 100644 --- a/test/inte/scan_and_read_inte_test.cpp +++ b/test/inte/scan_and_read_inte_test.cpp @@ -1948,8 +1948,10 @@ TEST_P(ScanAndReadInteTest, TestWithPKWithMorWithInvalidAggregateBatchScanSnapsh ASSERT_OK_AND_ASSIGN(auto result_plan, table_scan->CreatePlan()); ASSERT_EQ(result_plan->SnapshotId().value(), 3); + ASSERT_OK_AND_ASSIGN(std::unique_ptr reader, + table_read->CreateReader(result_plan->Splits())); ASSERT_NOK_WITH_MSG( - table_read->CreateReader(result_plan->Splits()), + reader->NextBatch(), "Use unsupported aggregation rbm32 or spell aggregate function incorrectly"); }