Skip to content

Commit 0008f7a

Browse files
authored
test: add ut for release-0.3 (alibaba#451)
1 parent ac2e446 commit 0008f7a

16 files changed

Lines changed: 919 additions & 212 deletions

src/paimon/common/global_index/union_global_index_reader.cpp

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,92 +39,92 @@ Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::VisitIsNull()
3939

4040
Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::VisitEqual(
4141
const Literal& literal) {
42-
return Union([&literal](const std::shared_ptr<GlobalIndexReader>& reader) {
42+
return Union([literal](const std::shared_ptr<GlobalIndexReader>& reader) {
4343
return reader->VisitEqual(literal);
4444
});
4545
}
4646

4747
Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::VisitNotEqual(
4848
const Literal& literal) {
49-
return Union([&literal](const std::shared_ptr<GlobalIndexReader>& reader) {
49+
return Union([literal](const std::shared_ptr<GlobalIndexReader>& reader) {
5050
return reader->VisitNotEqual(literal);
5151
});
5252
}
5353

5454
Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::VisitLessThan(
5555
const Literal& literal) {
56-
return Union([&literal](const std::shared_ptr<GlobalIndexReader>& reader) {
56+
return Union([literal](const std::shared_ptr<GlobalIndexReader>& reader) {
5757
return reader->VisitLessThan(literal);
5858
});
5959
}
6060

6161
Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::VisitLessOrEqual(
6262
const Literal& literal) {
63-
return Union([&literal](const std::shared_ptr<GlobalIndexReader>& reader) {
63+
return Union([literal](const std::shared_ptr<GlobalIndexReader>& reader) {
6464
return reader->VisitLessOrEqual(literal);
6565
});
6666
}
6767

6868
Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::VisitGreaterThan(
6969
const Literal& literal) {
70-
return Union([&literal](const std::shared_ptr<GlobalIndexReader>& reader) {
70+
return Union([literal](const std::shared_ptr<GlobalIndexReader>& reader) {
7171
return reader->VisitGreaterThan(literal);
7272
});
7373
}
7474

7575
Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::VisitGreaterOrEqual(
7676
const Literal& literal) {
77-
return Union([&literal](const std::shared_ptr<GlobalIndexReader>& reader) {
77+
return Union([literal](const std::shared_ptr<GlobalIndexReader>& reader) {
7878
return reader->VisitGreaterOrEqual(literal);
7979
});
8080
}
8181

8282
Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::VisitIn(
8383
const std::vector<Literal>& literals) {
84-
return Union([&literals](const std::shared_ptr<GlobalIndexReader>& reader) {
84+
return Union([literals](const std::shared_ptr<GlobalIndexReader>& reader) {
8585
return reader->VisitIn(literals);
8686
});
8787
}
8888

8989
Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::VisitNotIn(
9090
const std::vector<Literal>& literals) {
91-
return Union([&literals](const std::shared_ptr<GlobalIndexReader>& reader) {
91+
return Union([literals](const std::shared_ptr<GlobalIndexReader>& reader) {
9292
return reader->VisitNotIn(literals);
9393
});
9494
}
9595

9696
Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::VisitStartsWith(
9797
const Literal& prefix) {
98-
return Union([&prefix](const std::shared_ptr<GlobalIndexReader>& reader) {
98+
return Union([prefix](const std::shared_ptr<GlobalIndexReader>& reader) {
9999
return reader->VisitStartsWith(prefix);
100100
});
101101
}
102102

103103
Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::VisitEndsWith(
104104
const Literal& suffix) {
105-
return Union([&suffix](const std::shared_ptr<GlobalIndexReader>& reader) {
105+
return Union([suffix](const std::shared_ptr<GlobalIndexReader>& reader) {
106106
return reader->VisitEndsWith(suffix);
107107
});
108108
}
109109

110110
Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::VisitContains(
111111
const Literal& literal) {
112-
return Union([&literal](const std::shared_ptr<GlobalIndexReader>& reader) {
112+
return Union([literal](const std::shared_ptr<GlobalIndexReader>& reader) {
113113
return reader->VisitContains(literal);
114114
});
115115
}
116116

117117
Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::VisitLike(
118118
const Literal& literal) {
119-
return Union([&literal](const std::shared_ptr<GlobalIndexReader>& reader) {
119+
return Union([literal](const std::shared_ptr<GlobalIndexReader>& reader) {
120120
return reader->VisitLike(literal);
121121
});
122122
}
123123

124124
Result<std::shared_ptr<ScoredGlobalIndexResult>> UnionGlobalIndexReader::VisitVectorSearch(
125125
const std::shared_ptr<VectorSearch>& vector_search) {
126126
auto results = ExecuteAllReaders<Result<std::shared_ptr<ScoredGlobalIndexResult>>>(
127-
[&vector_search](const std::shared_ptr<GlobalIndexReader>& reader)
127+
[vector_search](const std::shared_ptr<GlobalIndexReader>& reader)
128128
-> Result<std::shared_ptr<ScoredGlobalIndexResult>> {
129129
return reader->VisitVectorSearch(vector_search);
130130
});
@@ -155,15 +155,13 @@ Result<std::shared_ptr<ScoredGlobalIndexResult>> UnionGlobalIndexReader::VisitVe
155155

156156
Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::VisitFullTextSearch(
157157
const std::shared_ptr<FullTextSearch>& full_text_search) {
158-
return Union([&full_text_search](const std::shared_ptr<GlobalIndexReader>& reader) {
158+
return Union([full_text_search](const std::shared_ptr<GlobalIndexReader>& reader) {
159159
return reader->VisitFullTextSearch(full_text_search);
160160
});
161161
}
162162

163163
Result<std::shared_ptr<GlobalIndexResult>> UnionGlobalIndexReader::Union(ReaderAction action) {
164-
auto results = ExecuteAllReaders<Result<std::shared_ptr<GlobalIndexResult>>>(
165-
[&action](const std::shared_ptr<GlobalIndexReader>& reader)
166-
-> Result<std::shared_ptr<GlobalIndexResult>> { return action(reader); });
164+
auto results = ExecuteAllReaders<Result<std::shared_ptr<GlobalIndexResult>>>(action);
167165

168166
std::shared_ptr<GlobalIndexResult> merged_result = nullptr;
169167
for (auto& result_or_status : results) {
@@ -207,8 +205,7 @@ std::vector<R> UnionGlobalIndexReader::ExecuteAllReaders(
207205
std::vector<std::future<R>> futures;
208206
futures.reserve(readers_.size());
209207
for (const auto& reader : readers_) {
210-
futures.push_back(
211-
Via(executor_.get(), [&action, reader]() -> R { return action(reader); }));
208+
futures.push_back(Via(executor_.get(), [action, reader]() -> R { return action(reader); }));
212209
}
213210
return CollectAll(futures);
214211
}

src/paimon/common/global_index/union_global_index_reader_test.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include <atomic>
2020
#include <map>
2121
#include <memory>
22+
#include <queue>
23+
#include <stdexcept>
2224
#include <string>
2325
#include <vector>
2426

@@ -54,6 +56,11 @@ class FakeReader : public GlobalIndexReader {
5456
error_message_ = message;
5557
}
5658

59+
void SetThrowException(const std::string& message) {
60+
throw_exception_ = true;
61+
exception_message_ = message;
62+
}
63+
5764
/// Sets a scored result returned by VisitVectorSearch.
5865
void SetScoredResult(const std::vector<int64_t>& row_ids, const std::vector<float>& scores) {
5966
scored_row_ids_ = row_ids;
@@ -161,6 +168,9 @@ class FakeReader : public GlobalIndexReader {
161168
private:
162169
Result<std::shared_ptr<GlobalIndexResult>> MakeResult() {
163170
invocation_count_++;
171+
if (throw_exception_) {
172+
throw std::runtime_error(exception_message_);
173+
}
164174
if (return_error_) {
165175
return Status::Invalid(error_message_);
166176
}
@@ -176,14 +186,50 @@ class FakeReader : public GlobalIndexReader {
176186
std::vector<int64_t> default_result_;
177187
bool return_nullptr_ = false;
178188
bool return_error_ = false;
189+
bool throw_exception_ = false;
179190
std::string error_message_;
191+
std::string exception_message_;
180192
std::vector<int64_t> scored_row_ids_;
181193
std::vector<float> scored_scores_;
182194
bool has_scored_result_ = false;
183195
bool thread_safe_ = true;
184196
std::atomic<int32_t> invocation_count_{0};
185197
};
186198

199+
// Runs the first task immediately and defers the rest. This makes it possible to verify that
200+
// queued tasks own their action even if collecting an earlier future throws.
201+
class DeferAfterFirstExecutor : public Executor {
202+
public:
203+
void Add(std::function<void()> func) override {
204+
if (submission_count_++ == 0) {
205+
func();
206+
} else {
207+
pending_tasks_.push(std::move(func));
208+
}
209+
}
210+
211+
void ShutdownNow() override {
212+
std::queue<std::function<void()>> empty;
213+
pending_tasks_.swap(empty);
214+
}
215+
216+
uint32_t GetThreadNum() const override {
217+
return 1;
218+
}
219+
220+
void RunPendingTasks() {
221+
while (!pending_tasks_.empty()) {
222+
std::function<void()> task = std::move(pending_tasks_.front());
223+
pending_tasks_.pop();
224+
task();
225+
}
226+
}
227+
228+
private:
229+
uint32_t submission_count_ = 0;
230+
std::queue<std::function<void()>> pending_tasks_;
231+
};
232+
187233
class UnionGlobalIndexReaderTest : public ::testing::Test {
188234
public:
189235
static void CheckResult(const std::shared_ptr<GlobalIndexResult>& result,
@@ -341,6 +387,23 @@ TEST_F(UnionGlobalIndexReaderTest, TestErrorPropagationWithExecutor) {
341387
ASSERT_NOK_WITH_MSG(union_reader.VisitIsNotNull(), "Unknown error for reader2");
342388
}
343389

390+
TEST_F(UnionGlobalIndexReaderTest, TestDeferredTaskOwnsActionAfterEarlierFutureThrows) {
391+
auto throwing_reader = std::make_shared<FakeReader>();
392+
auto deferred_reader = std::make_shared<FakeReader>();
393+
throwing_reader->SetThrowException("reader exception");
394+
deferred_reader->SetDefaultResult({2});
395+
396+
std::vector<std::shared_ptr<GlobalIndexReader>> readers = {throwing_reader, deferred_reader};
397+
auto executor = std::make_shared<DeferAfterFirstExecutor>();
398+
UnionGlobalIndexReader union_reader(std::move(readers), executor);
399+
ASSERT_THROW(
400+
{ [[maybe_unused]] auto result = union_reader.VisitIsNotNull(); }, std::runtime_error);
401+
ASSERT_EQ(deferred_reader->InvocationCount(), 0);
402+
403+
executor->RunPendingTasks();
404+
ASSERT_EQ(deferred_reader->InvocationCount(), 1);
405+
}
406+
344407
TEST_F(UnionGlobalIndexReaderTest, TestVisitEqualUnion) {
345408
auto reader1 = std::make_shared<FakeReader>();
346409
auto reader2 = std::make_shared<FakeReader>();

0 commit comments

Comments
 (0)