Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::ReservoirSamplePercentage Class Reference

The reservoir sample sample_size class maintains a streaming sample of variable size. More...

#include <duckdb.hpp>

Inheritance diagram for duckdb::ReservoirSamplePercentage:
Collaboration diagram for duckdb::ReservoirSamplePercentage:

Public Member Functions

 ReservoirSamplePercentage (Allocator &allocator, double percentage, int64_t seed=-1)
 ReservoirSamplePercentage (double percentage, int64_t seed, idx_t reservoir_sample_size)
 ReservoirSamplePercentage (double percentage, int64_t seed=-1)
void AddToReservoir (DataChunk &input) override
 Add a chunk of data to the sample.
unique_ptr< BlockingSample > Copy () const override
unique_ptr< DataChunk > GetChunk () override
 Fetches a chunk from the sample. If destory = true this method is descructive.
void Finalize () override
void Serialize (Serializer &serializer) const override
Public Member Functions inherited from duckdb::BlockingSample
 BlockingSample (int64_t seed=-1)
virtual void Destroy ()
std::pair< double, idx_t > PopFromWeightQueue ()
 Helper functions needed to merge two reservoirs while respecting weights of sampled rows.
double GetMinWeightThreshold ()
idx_t GetPriorityQueueSize ()
template<class TARGET>
TARGET & Cast ()
template<class TARGET>
const TARGET & Cast () const

Static Public Member Functions

static unique_ptr< BlockingSample > Deserialize (Deserializer &deserializer)
Static Public Member Functions inherited from duckdb::BlockingSample
static unique_ptr< BlockingSample > Deserialize (Deserializer &deserializer)

Static Public Attributes

static constexpr const SampleType TYPE = SampleType::RESERVOIR_PERCENTAGE_SAMPLE
Static Public Attributes inherited from duckdb::BlockingSample
static constexpr const SampleType TYPE = SampleType::BLOCKING_SAMPLE

Private Attributes

Allocator & allocator
double sample_percentage
 The sample_size to sample.
idx_t reservoir_sample_size
 The fixed sample size of the sub-reservoirs.
unique_ptr< ReservoirSample > current_sample
 The current sample.
vector< unique_ptr< ReservoirSample > > finished_samples
 The set of finished samples of the reservoir sample.
idx_t current_count = 0
 The amount of tuples that have been processed so far (not put in the reservoir, just processed).
bool is_finalized
 Whether or not the stream is finalized. The stream is automatically finalized on the first call to GetChunkAndShrink();.

Static Private Attributes

static constexpr idx_t RESERVOIR_THRESHOLD = 100000

Additional Inherited Members

Public Attributes inherited from duckdb::BlockingSample
unique_ptr< BaseReservoirSampling > base_reservoir_sample
SampleType type
 The sample type.
bool destroyed
 has the sample been destroyed due to updates to the referenced table

Detailed Description

The reservoir sample sample_size class maintains a streaming sample of variable size.

Constructor & Destructor Documentation

◆ ReservoirSamplePercentage() [1/3]

duckdb::ReservoirSamplePercentage::ReservoirSamplePercentage ( Allocator & allocator,
double percentage,
int64_t seed = -1 )
208983 : BlockingSample(seed), allocator(allocator), sample_percentage(percentage / 100.0), current_count(0),
208984 is_finalized(false) {
208985 reservoir_sample_size = (idx_t)(sample_percentage * RESERVOIR_THRESHOLD);
208986 current_sample = make_uniq<ReservoirSample>(allocator, reservoir_sample_size, base_reservoir_sample->random());
208987 type = SampleType::RESERVOIR_PERCENTAGE_SAMPLE;
208988}
SampleType type
The sample type.
Definition duckdb.hpp:47809
idx_t current_count
The amount of tuples that have been processed so far (not put in the reservoir, just processed).
Definition duckdb.hpp:48025
unique_ptr< ReservoirSample > current_sample
The current sample.
Definition duckdb.hpp:48019
bool is_finalized
Whether or not the stream is finalized. The stream is automatically finalized on the first call to Ge...
Definition duckdb.hpp:48028
double sample_percentage
The sample_size to sample.
Definition duckdb.hpp:48014
idx_t reservoir_sample_size
The fixed sample size of the sub-reservoirs.
Definition duckdb.hpp:48016
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ ReservoirSamplePercentage() [2/3]

duckdb::ReservoirSamplePercentage::ReservoirSamplePercentage ( double percentage,
int64_t seed,
idx_t reservoir_sample_size )
208976 : BlockingSample(seed), allocator(Allocator::DefaultAllocator()), sample_percentage(percentage / 100.0),
208978 current_sample = make_uniq<ReservoirSample>(allocator, reservoir_sample_size, base_reservoir_sample->random());
208979 type = SampleType::RESERVOIR_PERCENTAGE_SAMPLE;
208980}

◆ ReservoirSamplePercentage() [3/3]

duckdb::ReservoirSamplePercentage::ReservoirSamplePercentage ( double percentage,
int64_t seed = -1 )
explicit
208991 : ReservoirSamplePercentage(Allocator::DefaultAllocator(), percentage, seed) {
208992}

Member Function Documentation

◆ AddToReservoir()

void duckdb::ReservoirSamplePercentage::AddToReservoir ( DataChunk & input)
overridevirtual

Add a chunk of data to the sample.

Implements duckdb::BlockingSample.

208994 {
208995 base_reservoir_sample->num_entries_seen_total += input.size();
208996 if (current_count + input.size() > RESERVOIR_THRESHOLD) {
208997 // we don't have enough space in our current reservoir
208998 // first check what we still need to append to the current sample
208999 idx_t append_to_current_sample_count = RESERVOIR_THRESHOLD - current_count;
209000 idx_t append_to_next_sample = input.size() - append_to_current_sample_count;
209001 if (append_to_current_sample_count > 0) {
209002 // we have elements remaining, first add them to the current sample
209003 if (append_to_next_sample > 0) {
209004 // we need to also add to the next sample
209005 DataChunk new_chunk;
209006 new_chunk.InitializeEmpty(input.GetTypes());
209007 new_chunk.Slice(input, *FlatVector::IncrementalSelectionVector(), append_to_current_sample_count);
209008 new_chunk.Flatten();
209009 current_sample->AddToReservoir(new_chunk);
209010 } else {
209011 input.Flatten();
209012 input.SetCardinality(append_to_current_sample_count);
209013 current_sample->AddToReservoir(input);
209014 }
209015 }
209016 if (append_to_next_sample > 0) {
209017 // slice the input for the remainder
209018 SelectionVector sel(append_to_next_sample);
209019 for (idx_t i = append_to_current_sample_count; i < append_to_next_sample + append_to_current_sample_count;
209020 i++) {
209021 sel.set_index(i - append_to_current_sample_count, i);
209022 }
209023 input.Slice(sel, append_to_next_sample);
209024 }
209025 // now our first sample is filled: append it to the set of finished samples
209026 finished_samples.push_back(std::move(current_sample));
209027
209028 // allocate a new sample, and potentially add the remainder of the current input to that sample
209029 current_sample = make_uniq<ReservoirSample>(allocator, reservoir_sample_size, base_reservoir_sample->random());
209030 if (append_to_next_sample > 0) {
209031 current_sample->AddToReservoir(input);
209032 }
209033 current_count = append_to_next_sample;
209034 } else {
209035 // we can just append to the current sample
209036 current_count += input.size();
209037 current_sample->AddToReservoir(input);
209038 }
209039}
DUCKDB_API void InitializeEmpty(const vector< LogicalType > &types)
Initializes a DataChunk with the given types and without any vector data allocation.
Definition duckdb.cpp:116783
DUCKDB_API void Flatten()
Turn all the vectors from the chunk into flat vectors.
Definition duckdb.cpp:116989
DUCKDB_API vector< LogicalType > GetTypes() const
Returns a list of types of the vectors of this data chunk.
Definition duckdb.cpp:116995
vector< unique_ptr< ReservoirSample > > finished_samples
The set of finished samples of the reservoir sample.
Definition duckdb.hpp:48022
Here is the call graph for this function:

◆ Copy()

unique_ptr< BlockingSample > duckdb::ReservoirSamplePercentage::Copy ( ) const
overridevirtual

Implements duckdb::BlockingSample.

209058 {
209059 throw InternalException("Cannot call Copy on ReservoirSample Percentage");
209060}

◆ GetChunk()

unique_ptr< DataChunk > duckdb::ReservoirSamplePercentage::GetChunk ( )
overridevirtual

Fetches a chunk from the sample. If destory = true this method is descructive.

Implements duckdb::BlockingSample.

209041 {
209042 // reservoir sample percentage should never stay
209043 if (!is_finalized) {
209044 Finalize();
209045 }
209046 while (!finished_samples.empty()) {
209047 auto &front = finished_samples.front();
209048 auto chunk = front->GetChunk();
209049 if (chunk && chunk->size() > 0) {
209050 return chunk;
209051 }
209052 // move to the next sample
209053 finished_samples.erase(finished_samples.begin());
209054 }
209055 return nullptr;
209056}

◆ Finalize()

void duckdb::ReservoirSamplePercentage::Finalize ( )
overridevirtual

Implements duckdb::BlockingSample.

209062 {
209063 // need to finalize the current sample, if any
209064 // we are finializing, so we are starting to return chunks. Our last chunk has
209065 // sample_percentage * RESERVOIR_THRESHOLD entries that hold samples.
209066 // if our current count is less than the sample_percentage * RESERVOIR_THRESHOLD
209067 // then we have sampled too much for the current_sample and we need to redo the sample
209068 // otherwise we can just push the current sample back
209069 // Imagine sampling 70% of 100 rows (so 70 rows). We allocate sample_percentage * RESERVOIR_THRESHOLD
209070 // -----------------------------------------
209071 auto sampled_more_than_required =
209072 static_cast<double>(current_count) > sample_percentage * RESERVOIR_THRESHOLD || finished_samples.empty();
209073 if (current_count > 0 && sampled_more_than_required) {
209074 // create a new sample
209075 auto new_sample_size = static_cast<idx_t>(round(sample_percentage * static_cast<double>(current_count)));
209076 auto new_sample = make_uniq<ReservoirSample>(allocator, new_sample_size, base_reservoir_sample->random());
209077 while (true) {
209078 auto chunk = current_sample->GetChunk();
209079 if (!chunk || chunk->size() == 0) {
209080 break;
209081 }
209082 new_sample->AddToReservoir(*chunk);
209083 }
209084 finished_samples.push_back(std::move(new_sample));
209085 } else {
209086 finished_samples.push_back(std::move(current_sample));
209087 }
209088 // when finalizing, current_sample is null. All samples are now in finished samples.
209089 current_sample = nullptr;
209090 is_finalized = true;
209091}

◆ Serialize()

void duckdb::ReservoirSamplePercentage::Serialize ( Serializer & serializer) const
overridevirtual

Reimplemented from duckdb::BlockingSample.

417639 {
417640 BlockingSample::Serialize(serializer);
417641 serializer.WriteProperty<double>(200, "sample_percentage", sample_percentage);
417642 serializer.WritePropertyWithDefault<idx_t>(201, "reservoir_sample_size", reservoir_sample_size);
417643}

◆ Deserialize()

unique_ptr< BlockingSample > duckdb::ReservoirSamplePercentage::Deserialize ( Deserializer & deserializer)
static
417645 {
417646 auto sample_percentage = deserializer.ReadProperty<double>(200, "sample_percentage");
417647 auto result = duckdb::unique_ptr<ReservoirSamplePercentage>(new ReservoirSamplePercentage(sample_percentage));
417648 deserializer.ReadPropertyWithDefault<idx_t>(201, "reservoir_sample_size", result->reservoir_sample_size);
417649 return std::move(result);
417650}

The documentation for this class was generated from the following files: