Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix crash in sample #2904

Merged
merged 2 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/common/algorithm/ReservoirSampling.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,13 @@ class ReservoirSampling final {
return false;
}

std::vector<T>&& samples() && { return std::move(samples_); }
std::vector<T> samples() {
auto result = std::move(samples_);
samples_.clear();
samples_.reserve(num_);
cnt_ = 0;
return result;
}

private:
std::vector<T> samples_;
Expand Down
22 changes: 12 additions & 10 deletions src/common/algorithm/test/ReservoirSamplingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ TEST(ReservoirSamplingTest, Sample) {
sampler.sampling(std::move(i));
}

auto result = std::move(sampler).samples();
auto result = sampler.samples();
EXPECT_EQ(5, result.size());
for (auto i : result) {
EXPECT_LE(0, i);
Expand All @@ -27,16 +27,18 @@ TEST(ReservoirSamplingTest, Sample) {
}
{
ReservoirSampling<int64_t> sampler(5);
std::vector<int64_t> sampleSpace = {0, 1, 2};
for (auto i : sampleSpace) {
sampler.sampling(std::move(i));
}
for (size_t count = 0; count < 10; count++) {
std::vector<int64_t> sampleSpace = {0, 1, 2};
for (auto i : sampleSpace) {
sampler.sampling(std::move(i));
}

auto result = std::move(sampler).samples();
EXPECT_EQ(3, result.size());
EXPECT_EQ(0, result[0]);
EXPECT_EQ(1, result[1]);
EXPECT_EQ(2, result[2]);
auto result = sampler.samples();
EXPECT_EQ(3, result.size());
EXPECT_EQ(0, result[0]);
EXPECT_EQ(1, result[1]);
EXPECT_EQ(2, result[2]);
}
}
}
} // namespace algorithm
Expand Down
2 changes: 1 addition & 1 deletion src/storage/exec/GetNeighborsNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class GetNeighborsSampleNode : public GetNeighborsNode {
}

RowReaderWrapper reader;
auto samples = std::move(*sampler_).samples();
auto samples = sampler_->samples();
for (auto& sample : samples) {
auto columnIdx = std::get<4>(sample);
// add edge prop value to the target column
Expand Down