From 98437aae6439454e6a0c63a6e45c700c31cc1fb6 Mon Sep 17 00:00:00 2001 From: lxy264173 Date: Thu, 12 Nov 2026 14:21:32 +0800 Subject: [PATCH 1/3] perf(global-index): materialize bitmap results eagerly and batch unions --- .../global_index/bitmap_global_index_result.h | 21 ++--- .../paimon/global_index/global_index_result.h | 3 +- .../bitmap/bitmap_global_index_test.cpp | 7 ++ .../bitmap/bitmap_index_reader.cpp | 4 +- .../global_index/bitmap/bitmap_index_reader.h | 2 +- .../bitmap_global_index_result.cpp | 70 +++++---------- .../bitmap_global_index_result_test.cpp | 88 +++++++++++++------ .../bitmap_scored_global_index_result.cpp | 4 +- ...bitmap_scored_global_index_result_test.cpp | 6 +- .../btree/btree_global_index_reader.cpp | 34 +++---- .../btree/lazy_filtered_btree_reader.cpp | 4 +- .../global_index/global_index_result.cpp | 7 +- .../global_index/global_index_result_test.cpp | 3 +- .../common/global_index/global_index_utils.h | 35 ++++++++ .../global_index/global_index_utils_test.cpp | 50 +++++++++++ .../sorted_file_global_index_reader.cpp | 16 ++-- .../union_global_index_reader.cpp | 11 +-- .../wrap/file_index_reader_wrapper.h | 13 ++- .../wrap/file_index_reader_wrapper_test.cpp | 8 +- src/paimon/common/utils/roaring_bitmap64.cpp | 6 +- .../global_index_evaluator_impl.cpp | 12 ++- .../global_index_evaluator_impl_test.cpp | 3 +- .../table/source/filtered_row_id_reader.cpp | 3 +- .../source/primary_key_sorted_index_scan.cpp | 4 +- .../primary_key_sorted_index_scan_test.cpp | 4 +- .../testing/mock/mock_global_index_reader.h | 4 +- 26 files changed, 253 insertions(+), 169 deletions(-) diff --git a/include/paimon/global_index/bitmap_global_index_result.h b/include/paimon/global_index/bitmap_global_index_result.h index 8587459f8..93133e60e 100644 --- a/include/paimon/global_index/bitmap_global_index_result.h +++ b/include/paimon/global_index/bitmap_global_index_result.h @@ -19,6 +19,7 @@ #pragma once +#include #include #include #include @@ -29,15 +30,15 @@ #include "paimon/visibility.h" namespace paimon { -/// Represents a global index query result backed by a Roaring bitmap. Supplier-backed results -/// materialize their matching row ids on the first GetBitmap() call, avoiding unnecessary work -/// when the result is not used. OR between bitmap results materializes both operands immediately -/// and returns a cached bitmap without retaining the input results. +/// Represents a global index query result backed by a Roaring bitmap. class PAIMON_EXPORT BitmapGlobalIndexResult : public GlobalIndexResult { public: using BitmapSupplier = std::function()>; - explicit BitmapGlobalIndexResult(BitmapSupplier bitmap_supplier) - : bitmap_supplier_(bitmap_supplier) {} + explicit BitmapGlobalIndexResult(RoaringBitmap64 bitmap) : bitmap_(std::move(bitmap)) {} + + /// Executes the supplier immediately and returns its bitmap, or propagates its error. + /// The returned result does not retain the supplier or its captures. + static Result> Create(BitmapSupplier bitmap_supplier); class Iterator : public GlobalIndexResult::Iterator { public: @@ -75,9 +76,7 @@ class PAIMON_EXPORT BitmapGlobalIndexResult : public GlobalIndexResult { /// @return A non-owning, const pointer to the bitmap. The returned pointer is valid as long as /// this BitmapGlobalIndexResult object is alive. The caller must not modify the bitmap. - /// @note Supplier-backed results construct the bitmap on the first call to this method. - /// Precomputed results and subsequent calls return the cached instance. - /// Materialization may involve non-trivial CPU/IO cost. **Not thread-safe**. + /// @note Returns the stored bitmap without performing computation or I/O. Result GetBitmap() const; /// Creates `BitmapGlobalIndexResult` for all row ids in the given ranges. @@ -85,8 +84,6 @@ class PAIMON_EXPORT BitmapGlobalIndexResult : public GlobalIndexResult { static std::shared_ptr FromRanges(const std::vector& ranges); private: - mutable bool initialized_ = false; - BitmapSupplier bitmap_supplier_; - mutable RoaringBitmap64 bitmap_; + RoaringBitmap64 bitmap_; }; } // namespace paimon diff --git a/include/paimon/global_index/global_index_result.h b/include/paimon/global_index/global_index_result.h index bb16d6e6d..179267646 100644 --- a/include/paimon/global_index/global_index_result.h +++ b/include/paimon/global_index/global_index_result.h @@ -52,8 +52,7 @@ class PAIMON_EXPORT GlobalIndexResult : public std::enable_shared_from_this` where: /// - `true` indicates the result is empty (no matching rows), /// - `false` indicates at least one matching row exists, - /// - An error is returned only if internal state is corrupted or I/O fails - /// (e.g., during lazy loading of index data). + /// - An error is returned if the implementation cannot inspect its state. virtual Result IsEmpty() const = 0; /// Creates a new iterator over the selected row ids. diff --git a/src/paimon/common/global_index/bitmap/bitmap_global_index_test.cpp b/src/paimon/common/global_index/bitmap/bitmap_global_index_test.cpp index 972fb00c9..914fe77af 100644 --- a/src/paimon/common/global_index/bitmap/bitmap_global_index_test.cpp +++ b/src/paimon/common/global_index/bitmap/bitmap_global_index_test.cpp @@ -114,6 +114,8 @@ TEST_F(BitmapGlobalIndexTest, WriteAndReadStringPredicates) { CreateReader(field->type(), metas[0])); ASSERT_FALSE(reader->IsThreadSafe()); ASSERT_EQ(reader->GetIndexType(), "bitmap"); + ASSERT_OK_AND_ASSIGN(std::shared_ptr saved_result, + reader->VisitEqual(StringLiteral("apple"))); CheckResult(reader->VisitIsNull(), {2, 6}); CheckResult(reader->VisitIsNotNull(), {0, 1, 3, 4, 5}); CheckResult(reader->VisitEqual(StringLiteral("apple")), {0, 1}); @@ -184,6 +186,11 @@ TEST_F(BitmapGlobalIndexTest, WriteAndReadStringPredicates) { ASSERT_NOK_WITH_MSG(reader->VisitFullTextSearch(nullptr), "Full text search is not supported in BitmapIndexReader"); ASSERT_OK(reader->Close()); + ASSERT_NOK_WITH_MSG(reader->VisitEqual(StringLiteral("apple")), "closed BitmapIndexReader"); + std::weak_ptr weak_reader = reader; + reader.reset(); + ASSERT_TRUE(weak_reader.expired()); + CheckResult(saved_result, {0, 1}); } TEST_F(BitmapGlobalIndexTest, WriteAndReadLogicalIntegerOrder) { diff --git a/src/paimon/common/global_index/bitmap/bitmap_index_reader.cpp b/src/paimon/common/global_index/bitmap/bitmap_index_reader.cpp index baa41c9a3..5a88e6dcf 100644 --- a/src/paimon/common/global_index/bitmap/bitmap_index_reader.cpp +++ b/src/paimon/common/global_index/bitmap/bitmap_index_reader.cpp @@ -62,9 +62,9 @@ BitmapIndexReader::~BitmapIndexReader() { [[maybe_unused]] Status close_status = Close(); } -std::shared_ptr BitmapIndexReader::CreateResult( +Result> BitmapIndexReader::CreateResult( std::function()> supplier) { - return std::make_shared(std::move(supplier)); + return BitmapGlobalIndexResult::Create(std::move(supplier)); } Result> BitmapIndexReader::VisitIsNotNull() { diff --git a/src/paimon/common/global_index/bitmap/bitmap_index_reader.h b/src/paimon/common/global_index/bitmap/bitmap_index_reader.h index 350a6dada..161661262 100644 --- a/src/paimon/common/global_index/bitmap/bitmap_index_reader.h +++ b/src/paimon/common/global_index/bitmap/bitmap_index_reader.h @@ -98,7 +98,7 @@ class BitmapIndexReader : public GlobalIndexReader, file_size_(file_size), pool_(std::move(pool)) {} - std::shared_ptr CreateResult( + Result> CreateResult( std::function()> supplier); Result IsNull(); diff --git a/src/paimon/common/global_index/bitmap_global_index_result.cpp b/src/paimon/common/global_index/bitmap_global_index_result.cpp index ea9a03a18..4924032d4 100644 --- a/src/paimon/common/global_index/bitmap_global_index_result.cpp +++ b/src/paimon/common/global_index/bitmap_global_index_result.cpp @@ -19,6 +19,12 @@ #include "paimon/global_index/bitmap_global_index_result.h" namespace paimon { +Result> BitmapGlobalIndexResult::Create( + BitmapSupplier bitmap_supplier) { + PAIMON_ASSIGN_OR_RAISE(RoaringBitmap64 bitmap, bitmap_supplier()); + return std::make_shared(std::move(bitmap)); +} + Result> BitmapGlobalIndexResult::CreateIterator() const { PAIMON_ASSIGN_OR_RAISE(const RoaringBitmap64* bitmap, GetBitmap()); @@ -30,13 +36,8 @@ Result> BitmapGlobalIndexResult::And( const std::shared_ptr& other) { auto typed_result = std::dynamic_pointer_cast(other); if (typed_result) { - auto supplier = [typed_result, result = std::dynamic_pointer_cast( - shared_from_this())]() -> Result { - PAIMON_ASSIGN_OR_RAISE(const RoaringBitmap64* r1, result->GetBitmap()); - PAIMON_ASSIGN_OR_RAISE(const RoaringBitmap64* r2, typed_result->GetBitmap()); - return RoaringBitmap64::And(*r1, *r2); - }; - return std::make_shared(supplier); + return std::make_shared( + RoaringBitmap64::And(bitmap_, typed_result->bitmap_)); } return GlobalIndexResult::And(other); } @@ -45,16 +46,8 @@ Result> BitmapGlobalIndexResult::Or( const std::shared_ptr& other) { auto typed_result = std::dynamic_pointer_cast(other); if (typed_result) { - // Repeated lazy ORs build deep supplier chains that retain operands and materialized - // intermediate unions. Merge eagerly to avoid deep recursion and let previous results - // be released when callers drop them. AND remains lazy so callers that short-circuit - // can avoid materializing unused intersections. - PAIMON_ASSIGN_OR_RAISE(const RoaringBitmap64* r1, GetBitmap()); - PAIMON_ASSIGN_OR_RAISE(const RoaringBitmap64* r2, typed_result->GetBitmap()); - auto result = std::make_shared(BitmapSupplier{}); - result->bitmap_ = RoaringBitmap64::Or(*r1, *r2); - result->initialized_ = true; - return result; + return std::make_shared( + RoaringBitmap64::Or(bitmap_, typed_result->bitmap_)); } return GlobalIndexResult::Or(other); } @@ -63,49 +56,32 @@ Result> BitmapGlobalIndexResult::AddOffset(in if (offset == 0) { return shared_from_this(); } - auto supplier = [offset, result = std::dynamic_pointer_cast( - shared_from_this())]() -> Result { - PAIMON_ASSIGN_OR_RAISE(const RoaringBitmap64* bitmap, result->GetBitmap()); - RoaringBitmap64 bitmap64; - for (auto iter = bitmap->Begin(); iter != bitmap->End(); ++iter) { - bitmap64.Add(offset + (*iter)); - } - bitmap64.RunOptimize(); - return bitmap64; - }; - return std::make_shared(supplier); + RoaringBitmap64 bitmap64; + for (auto iter = bitmap_.Begin(); iter != bitmap_.End(); ++iter) { + bitmap64.Add(offset + (*iter)); + } + bitmap64.RunOptimize(); + return std::make_shared(std::move(bitmap64)); } Result BitmapGlobalIndexResult::GetBitmap() const { - if (!initialized_) { - PAIMON_ASSIGN_OR_RAISE(bitmap_, bitmap_supplier_()); - initialized_ = true; - } return &bitmap_; } Result BitmapGlobalIndexResult::IsEmpty() const { - PAIMON_ASSIGN_OR_RAISE(const RoaringBitmap64* bitmap, GetBitmap()); - return bitmap->IsEmpty(); + return bitmap_.IsEmpty(); } std::string BitmapGlobalIndexResult::ToString() const { - auto bitmap = GetBitmap(); - if (!bitmap.ok()) { - return bitmap.status().ToString(); - } - return bitmap.value()->ToString(); + return bitmap_.ToString(); } std::shared_ptr BitmapGlobalIndexResult::FromRanges( const std::vector& ranges) { - BitmapGlobalIndexResult::BitmapSupplier supplier = [ranges]() -> Result { - RoaringBitmap64 bitmap; - for (const auto& range : ranges) { - bitmap.AddRange(range.from, range.to + 1); - } - return bitmap; - }; - return std::make_shared(supplier); + RoaringBitmap64 bitmap; + for (const auto& range : ranges) { + bitmap.AddRange(range.from, range.to + 1); + } + return std::make_shared(std::move(bitmap)); } } // namespace paimon diff --git a/src/paimon/common/global_index/bitmap_global_index_result_test.cpp b/src/paimon/common/global_index/bitmap_global_index_result_test.cpp index e72330434..22d9fc76e 100644 --- a/src/paimon/common/global_index/bitmap_global_index_result_test.cpp +++ b/src/paimon/common/global_index/bitmap_global_index_result_test.cpp @@ -77,7 +77,8 @@ TEST_F(BitmapGlobalIndexResultTest, TestIterator) { auto bitmap_supplier = [&]() -> Result { return RoaringBitmap64::From(expected_ids); }; - auto index_result = std::make_shared(bitmap_supplier); + ASSERT_OK_AND_ASSIGN(std::shared_ptr index_result, + BitmapGlobalIndexResult::Create(bitmap_supplier)); if (expected_ids.empty()) { ASSERT_TRUE(index_result->IsEmpty().value()); } @@ -101,11 +102,13 @@ TEST_F(BitmapGlobalIndexResultTest, TestAnd) { auto bitmap_supplier1 = [&]() -> Result { return RoaringBitmap64::From(left); }; - auto index_result1 = std::make_shared(bitmap_supplier1); + ASSERT_OK_AND_ASSIGN(std::shared_ptr index_result1, + BitmapGlobalIndexResult::Create(bitmap_supplier1)); auto bitmap_supplier2 = [&]() -> Result { return RoaringBitmap64::From(right); }; - auto index_result2 = std::make_shared(bitmap_supplier2); + ASSERT_OK_AND_ASSIGN(std::shared_ptr index_result2, + BitmapGlobalIndexResult::Create(bitmap_supplier2)); ASSERT_OK_AND_ASSIGN(auto result, index_result1->And(index_result2)); ASSERT_EQ(result->ToString(), expected_str); @@ -123,7 +126,8 @@ TEST_F(BitmapGlobalIndexResultTest, TestBitmapResultAndOtherResult) { auto bitmap_supplier1 = [&]() -> Result { return RoaringBitmap64::From({1, 2, 3}); }; - auto index_result1 = std::make_shared(bitmap_supplier1); + ASSERT_OK_AND_ASSIGN(std::shared_ptr index_result1, + BitmapGlobalIndexResult::Create(bitmap_supplier1)); auto fake_result = std::make_shared(std::vector({1l, 2l, 7l})); @@ -137,11 +141,13 @@ TEST_F(BitmapGlobalIndexResultTest, TestOr) { auto bitmap_supplier1 = [&]() -> Result { return RoaringBitmap64::From(left); }; - auto index_result1 = std::make_shared(bitmap_supplier1); + ASSERT_OK_AND_ASSIGN(std::shared_ptr index_result1, + BitmapGlobalIndexResult::Create(bitmap_supplier1)); auto bitmap_supplier2 = [&]() -> Result { return RoaringBitmap64::From(right); }; - auto index_result2 = std::make_shared(bitmap_supplier2); + ASSERT_OK_AND_ASSIGN(std::shared_ptr index_result2, + BitmapGlobalIndexResult::Create(bitmap_supplier2)); ASSERT_OK_AND_ASSIGN(auto result, index_result1->Or(index_result2)); ASSERT_EQ(result->ToString(), expected_str); @@ -155,27 +161,29 @@ TEST_F(BitmapGlobalIndexResultTest, TestOr) { "{1,2,3,9223372036854775807}"); } -TEST_F(BitmapGlobalIndexResultTest, TestOrPropagatesSupplierErrorsImmediately) { - for (bool fail_left : {true, false}) { - auto left = - std::make_shared([fail_left]() -> Result { - if (fail_left) { - return Status::Invalid("left supplier failed"); - } - return RoaringBitmap64::From({1}); - }); - auto right = std::make_shared( - []() -> Result { return Status::Invalid("right supplier failed"); }); - const char* expected_error = fail_left ? "left supplier failed" : "right supplier failed"; - ASSERT_NOK_WITH_MSG(left->Or(right), expected_error); - } +TEST_F(BitmapGlobalIndexResultTest, TestOperationsDoNotRetainOperands) { + auto left = std::make_shared(RoaringBitmap64::From({1, 2})); + auto right = std::make_shared(RoaringBitmap64::From({2, 3})); + std::weak_ptr weak_left = left; + std::weak_ptr weak_right = right; + ASSERT_OK_AND_ASSIGN(std::shared_ptr intersection, left->And(right)); + ASSERT_OK_AND_ASSIGN(std::shared_ptr union_result, left->Or(right)); + ASSERT_OK_AND_ASSIGN(std::shared_ptr offset_result, left->AddOffset(10)); + left.reset(); + right.reset(); + ASSERT_TRUE(weak_left.expired()); + ASSERT_TRUE(weak_right.expired()); + ASSERT_EQ(intersection->ToString(), "{2}"); + ASSERT_EQ(union_result->ToString(), "{1,2,3}"); + ASSERT_EQ(offset_result->ToString(), "{11,12}"); } TEST_F(BitmapGlobalIndexResultTest, TestBitmapResultOrOtherResult) { auto bitmap_supplier1 = [&]() -> Result { return RoaringBitmap64::From({1, 2, 3}); }; - auto index_result1 = std::make_shared(bitmap_supplier1); + ASSERT_OK_AND_ASSIGN(std::shared_ptr index_result1, + BitmapGlobalIndexResult::Create(bitmap_supplier1)); auto fake_result = std::make_shared(std::vector({1l, 2l, 7l})); @@ -187,8 +195,33 @@ TEST_F(BitmapGlobalIndexResultTest, TestInvalidBitmapResult) { auto bitmap_supplier = [&]() -> Result { return Status::Invalid("invalid supplier"); }; - auto result = std::make_shared(bitmap_supplier); - ASSERT_TRUE(result->ToString().find("Invalid: invalid supplier") != std::string::npos); + ASSERT_NOK_WITH_MSG(BitmapGlobalIndexResult::Create(bitmap_supplier), "invalid supplier"); +} + +TEST_F(BitmapGlobalIndexResultTest, TestCreateMaterializesAndReleasesSupplier) { + auto source = std::make_shared(RoaringBitmap64::From({1, 4, 7})); + std::weak_ptr weak_source = source; + int32_t supplier_calls = 0; + ASSERT_OK_AND_ASSIGN( + std::shared_ptr result, + BitmapGlobalIndexResult::Create( + [source = std::move(source), &supplier_calls]() -> Result { + ++supplier_calls; + return *source; + })); + ASSERT_EQ(supplier_calls, 1); + ASSERT_TRUE(weak_source.expired()); + ASSERT_OK_AND_ASSIGN(const RoaringBitmap64* first, result->GetBitmap()); + ASSERT_OK_AND_ASSIGN(const RoaringBitmap64* second, result->GetBitmap()); + ASSERT_EQ(first, second); + ASSERT_EQ(result->ToString(), "{1,4,7}"); + ASSERT_OK_AND_ASSIGN(bool empty, result->IsEmpty()); + ASSERT_FALSE(empty); + ASSERT_OK_AND_ASSIGN(std::unique_ptr iterator, + result->CreateIterator()); + ASSERT_TRUE(iterator->HasNext()); + ASSERT_EQ(iterator->Next(), 1); + ASSERT_EQ(supplier_calls, 1); } TEST_F(BitmapGlobalIndexResultTest, TestFromRanges) { @@ -209,15 +242,16 @@ TEST_F(BitmapGlobalIndexResultTest, TestFromRanges) { TEST_F(BitmapGlobalIndexResultTest, TestAddOffset) { { int32_t supplier_calls = 0; - auto result = std::make_shared( - [&supplier_calls]() -> Result { + ASSERT_OK_AND_ASSIGN( + std::shared_ptr result, + BitmapGlobalIndexResult::Create([&supplier_calls]() -> Result { ++supplier_calls; return RoaringBitmap64::From({0, 1, 2, 3, 4, 5}); - }); + })); ASSERT_OK_AND_ASSIGN(std::shared_ptr result_with_offset, result->AddOffset(0)); ASSERT_EQ(result_with_offset.get(), result.get()); - ASSERT_EQ(supplier_calls, 0); + ASSERT_EQ(supplier_calls, 1); ASSERT_EQ(result_with_offset->ToString(), "{0,1,2,3,4,5}"); ASSERT_EQ(supplier_calls, 1); diff --git a/src/paimon/common/global_index/bitmap_scored_global_index_result.cpp b/src/paimon/common/global_index/bitmap_scored_global_index_result.cpp index 4beb10eaa..de1f9c632 100644 --- a/src/paimon/common/global_index/bitmap_scored_global_index_result.cpp +++ b/src/paimon/common/global_index/bitmap_scored_global_index_result.cpp @@ -69,7 +69,7 @@ Result> BitmapScoredGlobalIndexResult::And( PAIMON_ASSIGN_OR_RAISE(const RoaringBitmap64* r2, result->GetBitmap()); return RoaringBitmap64::And(*r1, *r2); }; - return std::make_shared(supplier); + return BitmapGlobalIndexResult::Create(std::move(supplier)); } auto bitmap_other = std::dynamic_pointer_cast(other); if (bitmap_other) { @@ -119,7 +119,7 @@ Result> BitmapScoredGlobalIndexResult::Or( PAIMON_ASSIGN_OR_RAISE(const RoaringBitmap64* r2, result->GetBitmap()); return RoaringBitmap64::Or(*r1, *r2); }; - return std::make_shared(supplier); + return BitmapGlobalIndexResult::Create(std::move(supplier)); } return GlobalIndexResult::Or(other); } diff --git a/src/paimon/common/global_index/bitmap_scored_global_index_result_test.cpp b/src/paimon/common/global_index/bitmap_scored_global_index_result_test.cpp index 25540dad1..b7e1ed76e 100644 --- a/src/paimon/common/global_index/bitmap_scored_global_index_result_test.cpp +++ b/src/paimon/common/global_index/bitmap_scored_global_index_result_test.cpp @@ -143,7 +143,8 @@ TEST_F(BitmapScoredGlobalIndexResultTest, TestAndBitmapResult) { auto bitmap_supplier2 = [&]() -> Result { return RoaringBitmap64::From(right_ids); }; - auto index_result2 = std::make_shared(bitmap_supplier2); + ASSERT_OK_AND_ASSIGN(std::shared_ptr index_result2, + BitmapGlobalIndexResult::Create(bitmap_supplier2)); ASSERT_OK_AND_ASSIGN(auto result, index_result1->And(index_result2)); ASSERT_EQ(result->ToString(), expected_str); @@ -204,7 +205,8 @@ TEST_F(BitmapScoredGlobalIndexResultTest, TestOrBitmapResult) { auto bitmap_supplier2 = [&]() -> Result { return RoaringBitmap64::From(right_ids); }; - auto index_result2 = std::make_shared(bitmap_supplier2); + ASSERT_OK_AND_ASSIGN(std::shared_ptr index_result2, + BitmapGlobalIndexResult::Create(bitmap_supplier2)); ASSERT_OK_AND_ASSIGN(auto result, index_result1->Or(index_result2)); ASSERT_EQ(result->ToString(), expected_str); diff --git a/src/paimon/common/global_index/btree/btree_global_index_reader.cpp b/src/paimon/common/global_index/btree/btree_global_index_reader.cpp index f42d13f85..793e9e687 100644 --- a/src/paimon/common/global_index/btree/btree_global_index_reader.cpp +++ b/src/paimon/common/global_index/btree/btree_global_index_reader.cpp @@ -67,14 +67,14 @@ BTreeGlobalIndexReader::BTreeGlobalIndexReader( comparator_(key_serializer_->CreateComparator()) {} Result> BTreeGlobalIndexReader::VisitIsNotNull() { - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this()]() -> Result { return reader->AllNonNullRows(); }); } Result> BTreeGlobalIndexReader::VisitIsNull() { - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this()]() -> Result { if (!reader->row_id_filter_) { return reader->null_bitmap_; @@ -92,7 +92,7 @@ Result> BTreeGlobalIndexReader::VisitStartsWi if (prefix.GetType() == FieldType::STRING) { auto prefix_str = prefix.GetValue(); if (prefix_str.empty()) { - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this()]() -> Result { return reader->AllNonNullRows(); }); @@ -114,7 +114,7 @@ Result> BTreeGlobalIndexReader::VisitStartsWi if (!overflow) { Literal upper_bound(FieldType::STRING, upper_str.data(), upper_str.size()); - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this(), prefix = prefix, upper_bound = std::move(upper_bound)]() -> Result { return reader->RangeQuery(prefix, upper_bound, /*from_inclusive=*/true, @@ -123,14 +123,14 @@ Result> BTreeGlobalIndexReader::VisitStartsWi } // All bytes were 0xFF, use max_key_ as upper bound - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this(), prefix = prefix]() -> Result { return reader->RangeQuery(prefix, reader->max_key_, /*from_inclusive=*/true, /*to_inclusive=*/true); }); } - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this()]() -> Result { return reader->AllNonNullRows(); }); @@ -138,7 +138,7 @@ Result> BTreeGlobalIndexReader::VisitStartsWi Result> BTreeGlobalIndexReader::VisitEndsWith( const Literal& suffix) { - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this()]() -> Result { return reader->AllNonNullRows(); }); @@ -146,7 +146,7 @@ Result> BTreeGlobalIndexReader::VisitEndsWith Result> BTreeGlobalIndexReader::VisitContains( const Literal& literal) { - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this()]() -> Result { return reader->AllNonNullRows(); }); @@ -176,7 +176,7 @@ Result> BTreeGlobalIndexReader::VisitLike( return VisitStartsWith(prefix_literal); } } - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this()]() -> Result { return reader->AllNonNullRows(); }); @@ -184,7 +184,7 @@ Result> BTreeGlobalIndexReader::VisitLike( Result> BTreeGlobalIndexReader::VisitLessThan( const Literal& literal) { - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this(), literal = literal]() -> Result { return reader->RangeQuery(reader->min_key_, literal, /*from_inclusive=*/true, /*to_inclusive=*/false); @@ -193,7 +193,7 @@ Result> BTreeGlobalIndexReader::VisitLessThan Result> BTreeGlobalIndexReader::VisitGreaterOrEqual( const Literal& literal) { - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this(), literal = literal]() -> Result { return reader->RangeQuery(literal, reader->max_key_, /*from_inclusive=*/true, /*to_inclusive=*/true); @@ -202,7 +202,7 @@ Result> BTreeGlobalIndexReader::VisitGreaterO Result> BTreeGlobalIndexReader::VisitNotEqual( const Literal& literal) { - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this(), literal = literal]() -> Result { PAIMON_ASSIGN_OR_RAISE(RoaringBitmap64 result, reader->AllNonNullRows()); PAIMON_ASSIGN_OR_RAISE(RoaringBitmap64 equal_result, @@ -215,7 +215,7 @@ Result> BTreeGlobalIndexReader::VisitNotEqual Result> BTreeGlobalIndexReader::VisitLessOrEqual( const Literal& literal) { - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this(), literal = literal]() -> Result { return reader->RangeQuery(reader->min_key_, literal, /*from_inclusive=*/true, /*to_inclusive=*/true); @@ -224,7 +224,7 @@ Result> BTreeGlobalIndexReader::VisitLessOrEq Result> BTreeGlobalIndexReader::VisitEqual( const Literal& literal) { - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this(), literal = literal]() -> Result { return reader->PointQuery(literal); }); @@ -232,7 +232,7 @@ Result> BTreeGlobalIndexReader::VisitEqual( Result> BTreeGlobalIndexReader::VisitGreaterThan( const Literal& literal) { - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this(), literal = literal]() -> Result { return reader->RangeQuery(literal, reader->max_key_, /*from_inclusive=*/false, /*to_inclusive=*/true); @@ -241,7 +241,7 @@ Result> BTreeGlobalIndexReader::VisitGreaterT Result> BTreeGlobalIndexReader::VisitIn( const std::vector& literals) { - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this(), literals = literals]() -> Result { RoaringBitmap64 result; for (const auto& literal : literals) { @@ -274,7 +274,7 @@ Result BTreeGlobalIndexReader::PointQuery(const Literal& litera Result> BTreeGlobalIndexReader::VisitNotIn( const std::vector& literals) { - return std::make_shared( + return BitmapGlobalIndexResult::Create( [reader = shared_from_this(), literals = literals]() -> Result { PAIMON_ASSIGN_OR_RAISE(RoaringBitmap64 result, reader->AllNonNullRows()); PAIMON_ASSIGN_OR_RAISE(std::shared_ptr in_result, diff --git a/src/paimon/common/global_index/btree/lazy_filtered_btree_reader.cpp b/src/paimon/common/global_index/btree/lazy_filtered_btree_reader.cpp index 0dd9aa737..cf28da939 100644 --- a/src/paimon/common/global_index/btree/lazy_filtered_btree_reader.cpp +++ b/src/paimon/common/global_index/btree/lazy_filtered_btree_reader.cpp @@ -217,9 +217,7 @@ Result> LazyFilteredBTreeReader::VisitGreater Result> LazyFilteredBTreeReader::FullResult() const { assert(total_row_count_ > 0); if (row_id_filter_) { - RoaringBitmap64 filter = row_id_filter_.value(); - return std::make_shared( - [filter = std::move(filter)]() -> Result { return filter; }); + return std::make_shared(row_id_filter_.value()); } return BitmapGlobalIndexResult::FromRanges({Range(0, total_row_count_ - 1)}); } diff --git a/src/paimon/common/global_index/global_index_result.cpp b/src/paimon/common/global_index/global_index_result.cpp index d2b94f216..93af7fd56 100644 --- a/src/paimon/common/global_index/global_index_result.cpp +++ b/src/paimon/common/global_index/global_index_result.cpp @@ -62,7 +62,7 @@ Result> GlobalIndexResult::And( bitmap1 &= bitmap2; return bitmap1; }; - return std::make_shared(supplier); + return BitmapGlobalIndexResult::Create(std::move(supplier)); } Result> GlobalIndexResult::Or( @@ -81,7 +81,7 @@ Result> GlobalIndexResult::Or( } return bitmap; }; - return std::make_shared(supplier); + return BitmapGlobalIndexResult::Create(std::move(supplier)); } Result> GlobalIndexResult::Serialize( @@ -122,8 +122,7 @@ Result> GlobalIndexResult::Deserialize( PAIMON_ASSIGN_OR_RAISE(int32_t score_len, in.ReadValue()); if (score_len == 0) { - return std::make_shared( - [bitmap]() -> Result { return bitmap; }); + return std::make_shared(std::move(bitmap)); } if (score_len != bitmap.Cardinality()) { return Status::Invalid("row id count mismatches score count"); diff --git a/src/paimon/common/global_index/global_index_result_test.cpp b/src/paimon/common/global_index/global_index_result_test.cpp index 3179e6710..065686272 100644 --- a/src/paimon/common/global_index/global_index_result_test.cpp +++ b/src/paimon/common/global_index/global_index_result_test.cpp @@ -114,8 +114,7 @@ TEST_F(GlobalIndexResultTest, TestSerializeAndDeserializeSimple) { auto bitmap = RoaringBitmap64::From( {1l, 2l, 3l, 4l, 5l, 10l, 2247483647l, 2147483647l, 2147483648l, 2147483649l, 2147483650l}); - auto expected_result = std::make_shared( - [bitmap]() -> Result { return bitmap; }); + auto expected_result = std::make_shared(std::move(bitmap)); ASSERT_EQ(expected_result->ToString(), typed_result->ToString()); ASSERT_OK_AND_ASSIGN(auto serialize_bytes, GlobalIndexResult::Serialize(index_result, pool)); ASSERT_EQ(byte_buffer, std::vector(serialize_bytes->data(), diff --git a/src/paimon/common/global_index/global_index_utils.h b/src/paimon/common/global_index/global_index_utils.h index a9676538b..408172413 100644 --- a/src/paimon/common/global_index/global_index_utils.h +++ b/src/paimon/common/global_index/global_index_utils.h @@ -18,12 +18,15 @@ */ #pragma once +#include #include +#include #include "arrow/c/abi.h" #include "arrow/c/helpers.h" #include "fmt/format.h" #include "paimon/common/utils/scope_guard.h" +#include "paimon/global_index/bitmap_global_index_result.h" #include "paimon/status.h" #include "paimon/utils/range.h" #include "paimon/utils/roaring_bitmap64.h" @@ -33,6 +36,38 @@ class GlobalIndexUtils { GlobalIndexUtils() = delete; ~GlobalIndexUtils() = delete; + /// Unions non-null results, returning nullptr for an empty input. + /// Bitmap results are merged in one batch; other results use their virtual Or methods. + static Result> UnionResults( + const std::vector>& results) { + if (results.empty()) { + return std::shared_ptr(); + } + std::vector bitmaps; + bitmaps.reserve(results.size()); + for (const std::shared_ptr& result : results) { + if (result == nullptr) { + return Status::Invalid("Cannot union a null global index result."); + } + auto bitmap_result = std::dynamic_pointer_cast(result); + if (bitmap_result != nullptr) { + PAIMON_ASSIGN_OR_RAISE(const RoaringBitmap64* bitmap, bitmap_result->GetBitmap()); + bitmaps.push_back(bitmap); + } + } + if (results.size() == 1) { + return results.front(); + } + if (bitmaps.size() == results.size()) { + return std::make_shared(RoaringBitmap64::FastUnion(bitmaps)); + } + std::shared_ptr merged_result = results.front(); + for (size_t i = 1; i < results.size(); ++i) { + PAIMON_ASSIGN_OR_RAISE(merged_result, merged_result->Or(results[i])); + } + return merged_result; + } + static Status CheckRelativeRowIds(::ArrowArray* c_arrow_array, const std::vector& relative_row_ids, std::optional expected_next_row_id) { diff --git a/src/paimon/common/global_index/global_index_utils_test.cpp b/src/paimon/common/global_index/global_index_utils_test.cpp index 3894e74eb..27b8664b0 100644 --- a/src/paimon/common/global_index/global_index_utils_test.cpp +++ b/src/paimon/common/global_index/global_index_utils_test.cpp @@ -21,12 +21,14 @@ #include #include +#include #include #include #include "arrow/api.h" #include "arrow/c/bridge.h" #include "gtest/gtest.h" +#include "paimon/global_index/bitmap_scored_global_index_result.h" #include "paimon/testing/utils/testharness.h" #include "paimon/utils/range.h" #include "paimon/utils/roaring_bitmap64.h" @@ -49,6 +51,54 @@ class GlobalIndexUtilsTest : public ::testing::Test { } }; +TEST_F(GlobalIndexUtilsTest, TestUnionResults) { + ASSERT_OK_AND_ASSIGN(std::shared_ptr unavailable, + GlobalIndexUtils::UnionResults({})); + ASSERT_EQ(unavailable, nullptr); + ASSERT_NOK_WITH_MSG(GlobalIndexUtils::UnionResults({nullptr}), "null global index result"); + + const int64_t high_row_id = int64_t{1} << 32; + auto left = std::make_shared(RoaringBitmap64::From({1, high_row_id})); + auto right = std::make_shared(RoaringBitmap64::From({2, high_row_id})); + auto empty = std::make_shared(RoaringBitmap64()); + ASSERT_OK_AND_ASSIGN(std::shared_ptr single, + GlobalIndexUtils::UnionResults({left})); + ASSERT_EQ(single, left); + ASSERT_OK_AND_ASSIGN(std::shared_ptr merged, + GlobalIndexUtils::UnionResults({left, empty, right})); + auto bitmap_result = std::dynamic_pointer_cast(merged); + ASSERT_TRUE(bitmap_result); + ASSERT_OK_AND_ASSIGN(const RoaringBitmap64* bitmap, bitmap_result->GetBitmap()); + ASSERT_EQ(*bitmap, RoaringBitmap64::From({1, 2, high_row_id})); + ASSERT_OK_AND_ASSIGN(const RoaringBitmap64* left_bitmap, left->GetBitmap()); + ASSERT_EQ(*left_bitmap, RoaringBitmap64::From({1, high_row_id})); + ASSERT_OK_AND_ASSIGN(const RoaringBitmap64* right_bitmap, right->GetBitmap()); + ASSERT_EQ(*right_bitmap, RoaringBitmap64::From({2, high_row_id})); +} + +TEST_F(GlobalIndexUtilsTest, TestUnionResultsPreservesScoredSemantics) { + auto left = std::make_shared(RoaringBitmap64::From({1}), + std::vector{0.5f}); + auto right = std::make_shared(RoaringBitmap64::From({2}), + std::vector{1.5f}); + ASSERT_OK_AND_ASSIGN(std::shared_ptr merged, + GlobalIndexUtils::UnionResults({left, right})); + auto scored_result = std::dynamic_pointer_cast(merged); + ASSERT_TRUE(scored_result); + ASSERT_EQ(scored_result->GetScores(), (std::vector{0.5f, 1.5f})); + ASSERT_OK_AND_ASSIGN(const RoaringBitmap64* bitmap, scored_result->GetBitmap()); + ASSERT_EQ(*bitmap, RoaringBitmap64::From({1, 2})); + + ASSERT_NOK_WITH_MSG(GlobalIndexUtils::UnionResults({left, right, left}), "same row id"); + + auto unscored = std::make_shared(RoaringBitmap64::From({3})); + ASSERT_OK_AND_ASSIGN(merged, GlobalIndexUtils::UnionResults({left, unscored})); + auto bitmap_result = std::dynamic_pointer_cast(merged); + ASSERT_TRUE(bitmap_result); + ASSERT_OK_AND_ASSIGN(bitmap, bitmap_result->GetBitmap()); + ASSERT_EQ(*bitmap, RoaringBitmap64::From({1, 3})); +} + TEST_F(GlobalIndexUtilsTest, TestNullArrowArray) { std::vector row_ids = {0, 1, 2}; ASSERT_NOK_WITH_MSG(GlobalIndexUtils::CheckRelativeRowIds(nullptr, row_ids, std::nullopt), diff --git a/src/paimon/common/global_index/sorted_file_global_index_reader.cpp b/src/paimon/common/global_index/sorted_file_global_index_reader.cpp index d647e7eb1..9624d5ace 100644 --- a/src/paimon/common/global_index/sorted_file_global_index_reader.cpp +++ b/src/paimon/common/global_index/sorted_file_global_index_reader.cpp @@ -26,6 +26,7 @@ #include #include "paimon/common/executor/future.h" +#include "paimon/common/global_index/global_index_utils.h" #include "paimon/common/predicate/like_optimization.h" #include "paimon/global_index/bitmap_global_index_result.h" #include "paimon/utils/roaring_bitmap64.h" @@ -34,7 +35,7 @@ namespace paimon { namespace { std::shared_ptr EmptyResult() { - return std::make_shared([]() { return RoaringBitmap64(); }); + return std::make_shared(RoaringBitmap64()); } } // namespace @@ -268,20 +269,17 @@ Result> SortedFileGlobalIndexReader::VisitSel results = CollectAll(futures); } - std::shared_ptr merged_result = nullptr; + std::vector> index_results; + index_results.reserve(results.size()); for (ReaderResult& result_or_status : results) { PAIMON_ASSIGN_OR_RAISE(std::shared_ptr result, std::move(result_or_status)); if (result == nullptr) { continue; } - if (merged_result == nullptr) { - merged_result = std::move(result); - } else { - PAIMON_ASSIGN_OR_RAISE(merged_result, merged_result->Or(result)); - } + index_results.push_back(std::move(result)); } - return merged_result; + return GlobalIndexUtils::UnionResults(index_results); } Result> SortedFileGlobalIndexReader::GetOrCreateReader( @@ -312,7 +310,7 @@ Result> SortedFileGlobalIndexReader::Compleme } return result; }; - return std::make_shared(std::move(supplier)); + return BitmapGlobalIndexResult::Create(std::move(supplier)); } bool SortedFileGlobalIndexReader::FallbackScanEnabled( diff --git a/src/paimon/common/global_index/union_global_index_reader.cpp b/src/paimon/common/global_index/union_global_index_reader.cpp index c6744f234..e47448130 100644 --- a/src/paimon/common/global_index/union_global_index_reader.cpp +++ b/src/paimon/common/global_index/union_global_index_reader.cpp @@ -254,21 +254,18 @@ Result> UnionGlobalIndexReader::UnionSc Result> UnionGlobalIndexReader::Union(ReaderAction action) { auto results = ExecuteAllReaders>>(action); - std::shared_ptr merged_result = nullptr; + std::vector> index_results; + index_results.reserve(results.size()); for (auto& result_or_status : results) { PAIMON_ASSIGN_OR_RAISE(std::shared_ptr result, std::move(result_or_status)); if (result == nullptr) { return std::shared_ptr(nullptr); } - if (merged_result == nullptr) { - merged_result = std::move(result); - } else { - PAIMON_ASSIGN_OR_RAISE(merged_result, merged_result->Or(result)); - } + index_results.push_back(std::move(result)); } - return merged_result; + return GlobalIndexUtils::UnionResults(index_results); } bool UnionGlobalIndexReader::IsThreadSafe() const { diff --git a/src/paimon/common/global_index/wrap/file_index_reader_wrapper.h b/src/paimon/common/global_index/wrap/file_index_reader_wrapper.h index 8b2154b94..7cdba935b 100644 --- a/src/paimon/common/global_index/wrap/file_index_reader_wrapper.h +++ b/src/paimon/common/global_index/wrap/file_index_reader_wrapper.h @@ -145,15 +145,12 @@ class FileIndexReaderWrapper : public GlobalIndexReader { if (auto remain = std::dynamic_pointer_cast(result)) { return std::shared_ptr(); } else if (auto skip = std::dynamic_pointer_cast(result)) { - return std::make_shared( - []() -> Result { return RoaringBitmap64(); }); + return std::make_shared(RoaringBitmap64()); } else if (auto bitmap_result = std::dynamic_pointer_cast(result)) { - return std::make_shared( - [bitmap_result]() -> Result { - PAIMON_ASSIGN_OR_RAISE(const RoaringBitmap32* bitmap, - bitmap_result->GetBitmap()); - return RoaringBitmap64(*bitmap); - }); + return BitmapGlobalIndexResult::Create([bitmap_result]() -> Result { + PAIMON_ASSIGN_OR_RAISE(const RoaringBitmap32* bitmap, bitmap_result->GetBitmap()); + return RoaringBitmap64(*bitmap); + }); } return Status::Invalid( "invalid FileIndexResult, supposed to be Remain or Skip or BitmapIndexResult"); diff --git a/src/paimon/common/global_index/wrap/file_index_reader_wrapper_test.cpp b/src/paimon/common/global_index/wrap/file_index_reader_wrapper_test.cpp index 932460598..366434a93 100644 --- a/src/paimon/common/global_index/wrap/file_index_reader_wrapper_test.cpp +++ b/src/paimon/common/global_index/wrap/file_index_reader_wrapper_test.cpp @@ -52,12 +52,18 @@ TEST(FileIndexReaderWrapperTest, TestToGlobalIndexResult) { check_result(global_result, {}); } { - auto bitmap_supplier = []() -> Result { + int32_t supplier_calls = 0; + auto bitmap_supplier = [&supplier_calls]() -> Result { + ++supplier_calls; return RoaringBitmap32::From({1, 4, 2147483647}); }; auto file_result = std::make_shared(bitmap_supplier); ASSERT_OK_AND_ASSIGN(auto global_result, FileIndexReaderWrapper::ToGlobalIndexResult(file_result)); + ASSERT_EQ(supplier_calls, 1); + std::weak_ptr weak_file_result = file_result; + file_result.reset(); + ASSERT_TRUE(weak_file_result.expired()); check_result(global_result, {1l, 4l, 2147483647l}); } { diff --git a/src/paimon/common/utils/roaring_bitmap64.cpp b/src/paimon/common/utils/roaring_bitmap64.cpp index 97f7bf7ca..ff6a63365 100644 --- a/src/paimon/common/utils/roaring_bitmap64.cpp +++ b/src/paimon/common/utils/roaring_bitmap64.cpp @@ -345,9 +345,9 @@ std::string RoaringBitmap64::ToString() const { } RoaringBitmap64 RoaringBitmap64::And(const RoaringBitmap64& lhs, const RoaringBitmap64& rhs) { - RoaringBitmap64 res; - GetRoaringBitmap(res.roaring_bitmap_) = - (GetRoaringBitmap(lhs.roaring_bitmap_) & GetRoaringBitmap(rhs.roaring_bitmap_)); + const bool copy_lhs = lhs.GetSizeInBytes() <= rhs.GetSizeInBytes(); + RoaringBitmap64 res(copy_lhs ? lhs : rhs); + res &= (copy_lhs ? rhs : lhs); return res; } diff --git a/src/paimon/core/global_index/global_index_evaluator_impl.cpp b/src/paimon/core/global_index/global_index_evaluator_impl.cpp index 32f31f4c3..7d429802f 100644 --- a/src/paimon/core/global_index/global_index_evaluator_impl.cpp +++ b/src/paimon/core/global_index/global_index_evaluator_impl.cpp @@ -23,6 +23,7 @@ #include #include "fmt/format.h" +#include "paimon/common/global_index/global_index_utils.h" #include "paimon/global_index/bitmap_global_index_result.h" #include "paimon/predicate/leaf_predicate.h" #include "paimon/predicate/predicate_builder.h" @@ -242,21 +243,18 @@ GlobalIndexEvaluatorImpl::EvaluateCompoundPredicate( evaluation.declined_field_ids.end()); }; if (compound_predicate->GetFunction().GetType() == Function::Type::OR) { + std::vector> results; + results.reserve(compound_predicate->Children().size()); for (const auto& child : compound_predicate->Children()) { PAIMON_ASSIGN_OR_RAISE(std::optional sub_evaluation, EvaluatePredicate(child)); if (!sub_evaluation) { return std::optional(); } - if (!compound_result) { - compound_result = sub_evaluation->result; - } else { - PAIMON_ASSIGN_OR_RAISE(std::shared_ptr or_result, - compound_result->Or(sub_evaluation->result)); - compound_result = or_result; - } + results.push_back(std::move(sub_evaluation->result)); merge_fields(*sub_evaluation); } + PAIMON_ASSIGN_OR_RAISE(compound_result, GlobalIndexUtils::UnionResults(results)); } else if (compound_predicate->GetFunction().GetType() == Function::Type::AND) { for (const auto& child : compound_predicate->Children()) { PAIMON_ASSIGN_OR_RAISE(std::optional sub_evaluation, diff --git a/src/paimon/core/global_index/global_index_evaluator_impl_test.cpp b/src/paimon/core/global_index/global_index_evaluator_impl_test.cpp index b33ca72aa..99fad093b 100644 --- a/src/paimon/core/global_index/global_index_evaluator_impl_test.cpp +++ b/src/paimon/core/global_index/global_index_evaluator_impl_test.cpp @@ -132,8 +132,7 @@ class RecordingGlobalIndexReader : public MockGlobalIndexReader { for (int64_t position : positions) { bitmap.Add(position); } - return std::make_shared( - [bitmap = std::move(bitmap)]() -> Result { return bitmap; }); + return std::make_shared(std::move(bitmap)); } static std::shared_ptr NotEvaluable() { diff --git a/src/paimon/core/table/source/filtered_row_id_reader.cpp b/src/paimon/core/table/source/filtered_row_id_reader.cpp index d3c092d7f..d7c8c1757 100644 --- a/src/paimon/core/table/source/filtered_row_id_reader.cpp +++ b/src/paimon/core/table/source/filtered_row_id_reader.cpp @@ -168,8 +168,7 @@ Result FilteredRowIdReader::MatchingRowIds(const RoaringBitmap6 ScanContextBuilder scan_builder(root_path_); scan_builder.SetOptions(pinned_options) .SetPredicate(partition_filter_) - .SetGlobalIndexResult(std::make_shared( - [rows]() -> Result { return rows; })) + .SetGlobalIndexResult(std::make_shared(rows)) .WithFileSystem(options_.GetFileSystem()) .WithMemoryPool(pool_) .WithExecutor(executor_); diff --git a/src/paimon/core/table/source/primary_key_sorted_index_scan.cpp b/src/paimon/core/table/source/primary_key_sorted_index_scan.cpp index bd6cc4d28..3b7ce3a9d 100644 --- a/src/paimon/core/table/source/primary_key_sorted_index_scan.cpp +++ b/src/paimon/core/table/source/primary_key_sorted_index_scan.cpp @@ -175,9 +175,7 @@ class SharedGroupReader { std::vector> localized; localized.reserve(source_count); for (RoaringBitmap64& partition : partitions) { - auto bitmap = std::make_shared(std::move(partition)); - localized.push_back(std::make_shared( - [bitmap]() -> Result { return *bitmap; })); + localized.push_back(std::make_shared(std::move(partition))); } return localized; } diff --git a/src/paimon/core/table/source/primary_key_sorted_index_scan_test.cpp b/src/paimon/core/table/source/primary_key_sorted_index_scan_test.cpp index 5c28048a8..1e2d66d2f 100644 --- a/src/paimon/core/table/source/primary_key_sorted_index_scan_test.cpp +++ b/src/paimon/core/table/source/primary_key_sorted_index_scan_test.cpp @@ -122,9 +122,7 @@ class StubGlobalIndexReader : public MockGlobalIndexReader { if (equal_call_count_ != nullptr) { (*equal_call_count_)++; } - RoaringBitmap64 copy = equal_result_; - return std::make_shared( - [bitmap = std::move(copy)]() -> Result { return bitmap; }); + return std::make_shared(equal_result_); } Result> VisitNotEqual(const Literal& literal) override { return NotEvaluable(); diff --git a/src/paimon/testing/mock/mock_global_index_reader.h b/src/paimon/testing/mock/mock_global_index_reader.h index 9dd9ffc3d..4e90ec301 100644 --- a/src/paimon/testing/mock/mock_global_index_reader.h +++ b/src/paimon/testing/mock/mock_global_index_reader.h @@ -209,9 +209,7 @@ class MockGlobalIndexReader : public GlobalIndexReader { if (return_nullptr_) { return std::shared_ptr(nullptr); } - auto ids = default_result_; - return std::make_shared( - [ids]() { return RoaringBitmap64::From(ids); }); + return std::make_shared(RoaringBitmap64::From(default_result_)); } private: From df66269579a0642d9f5f72cac1ea684f3cb487ed Mon Sep 17 00:00:00 2001 From: lxy264173 Date: Thu, 12 Nov 2026 15:50:51 +0800 Subject: [PATCH 2/3] fix comments paipai --- .../global_index/global_index_evaluator_impl.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/paimon/core/global_index/global_index_evaluator_impl.cpp b/src/paimon/core/global_index/global_index_evaluator_impl.cpp index 7d429802f..32f31f4c3 100644 --- a/src/paimon/core/global_index/global_index_evaluator_impl.cpp +++ b/src/paimon/core/global_index/global_index_evaluator_impl.cpp @@ -23,7 +23,6 @@ #include #include "fmt/format.h" -#include "paimon/common/global_index/global_index_utils.h" #include "paimon/global_index/bitmap_global_index_result.h" #include "paimon/predicate/leaf_predicate.h" #include "paimon/predicate/predicate_builder.h" @@ -243,18 +242,21 @@ GlobalIndexEvaluatorImpl::EvaluateCompoundPredicate( evaluation.declined_field_ids.end()); }; if (compound_predicate->GetFunction().GetType() == Function::Type::OR) { - std::vector> results; - results.reserve(compound_predicate->Children().size()); for (const auto& child : compound_predicate->Children()) { PAIMON_ASSIGN_OR_RAISE(std::optional sub_evaluation, EvaluatePredicate(child)); if (!sub_evaluation) { return std::optional(); } - results.push_back(std::move(sub_evaluation->result)); + if (!compound_result) { + compound_result = sub_evaluation->result; + } else { + PAIMON_ASSIGN_OR_RAISE(std::shared_ptr or_result, + compound_result->Or(sub_evaluation->result)); + compound_result = or_result; + } merge_fields(*sub_evaluation); } - PAIMON_ASSIGN_OR_RAISE(compound_result, GlobalIndexUtils::UnionResults(results)); } else if (compound_predicate->GetFunction().GetType() == Function::Type::AND) { for (const auto& child : compound_predicate->Children()) { PAIMON_ASSIGN_OR_RAISE(std::optional sub_evaluation, From 19f68d7c254f0320ff46ddd28d27742de72811bb Mon Sep 17 00:00:00 2001 From: lxy264173 Date: Thu, 12 Nov 2026 18:31:28 +0800 Subject: [PATCH 3/3] fix comments --- src/paimon/common/global_index/global_index_utils.h | 2 +- src/paimon/common/utils/roaring_bitmap64.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/paimon/common/global_index/global_index_utils.h b/src/paimon/common/global_index/global_index_utils.h index 408172413..749820ace 100644 --- a/src/paimon/common/global_index/global_index_utils.h +++ b/src/paimon/common/global_index/global_index_utils.h @@ -36,7 +36,7 @@ class GlobalIndexUtils { GlobalIndexUtils() = delete; ~GlobalIndexUtils() = delete; - /// Unions non-null results, returning nullptr for an empty input. + /// Unions non-null results, returning nullptr for an empty vector. /// Bitmap results are merged in one batch; other results use their virtual Or methods. static Result> UnionResults( const std::vector>& results) { diff --git a/src/paimon/common/utils/roaring_bitmap64.cpp b/src/paimon/common/utils/roaring_bitmap64.cpp index ff6a63365..97f7bf7ca 100644 --- a/src/paimon/common/utils/roaring_bitmap64.cpp +++ b/src/paimon/common/utils/roaring_bitmap64.cpp @@ -345,9 +345,9 @@ std::string RoaringBitmap64::ToString() const { } RoaringBitmap64 RoaringBitmap64::And(const RoaringBitmap64& lhs, const RoaringBitmap64& rhs) { - const bool copy_lhs = lhs.GetSizeInBytes() <= rhs.GetSizeInBytes(); - RoaringBitmap64 res(copy_lhs ? lhs : rhs); - res &= (copy_lhs ? rhs : lhs); + RoaringBitmap64 res; + GetRoaringBitmap(res.roaring_bitmap_) = + (GetRoaringBitmap(lhs.roaring_bitmap_) & GetRoaringBitmap(rhs.roaring_bitmap_)); return res; }