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::TableStatistics Class Reference
Collaboration diagram for duckdb::TableStatistics:

Public Member Functions

void Initialize (const vector< LogicalType > &types, PersistentTableData &data)
void InitializeEmpty (const vector< LogicalType > &types)
void InitializeEmpty (const TableStatistics &other)
void InitializeAddColumn (TableStatistics &parent, const LogicalType &new_column_type)
void InitializeRemoveColumn (TableStatistics &parent, idx_t removed_column)
void InitializeAlterType (TableStatistics &parent, idx_t changed_idx, const LogicalType &new_type)
void InitializeAddConstraint (TableStatistics &parent)
void MergeStats (TableStatistics &other)
void MergeStats (idx_t i, BaseStatistics &stats)
void MergeStats (TableStatisticsLock &lock, idx_t i, BaseStatistics &stats)
void SetStats (TableStatistics &other)
void CopyStats (TableStatistics &other)
void CopyStats (TableStatisticsLock &lock, TableStatistics &other)
unique_ptr< BaseStatistics > CopyStats (const StorageIndex &i)
ColumnStatistics & GetStats (TableStatisticsLock &lock, idx_t i)
 Get a reference to the stats - this requires us to hold the lock. The reference can only be safely accessed while the lock is held.
unique_ptr< BlockingSample > GetTableSample (TableStatisticsLock &lock)
 Get a reference to the table sample - this requires us to hold the lock.
void SetTableSample (TableStatisticsLock &lock, unique_ptr< BlockingSample > sample)
void DestroyTableSample (TableStatisticsLock &lock) const
void AppendToTableSample (TableStatisticsLock &lock, unique_ptr< BlockingSample > sample)
bool Empty ()
unique_ptr< TableStatisticsLock > GetLock ()
void Serialize (Serializer &serializer) const
void Deserialize (Deserializer &deserializer, ColumnList &columns)

Private Attributes

shared_ptr< mutex > stats_lock
 The statistics lock.
vector< shared_ptr< ColumnStatistics > > column_stats
 Column statistics.
unique_ptr< BlockingSample > table_sample
 The table sample.

Member Function Documentation

◆ Initialize()

void duckdb::TableStatistics::Initialize ( const vector< LogicalType > & types,
PersistentTableData & data )
438464 {
438465 D_ASSERT(Empty());
438466 D_ASSERT(!table_sample);
438467
438468 stats_lock = make_shared_ptr<mutex>();
438469 column_stats = std::move(data.table_stats.column_stats);
438470 if (data.table_stats.table_sample) {
438471 table_sample = std::move(data.table_stats.table_sample);
438472 } else {
438473 table_sample = make_uniq<ReservoirSample>(static_cast<idx_t>(FIXED_SAMPLE_SIZE));
438474 }
438475 if (column_stats.size() != types.size()) { // LCOV_EXCL_START
438476 throw IOException("Table statistics column count is not aligned with table column count. Corrupt file?");
438477 } // LCOV_EXCL_STOP
438478}
shared_ptr< mutex > stats_lock
The statistics lock.
Definition duckdb.hpp:48426
vector< shared_ptr< ColumnStatistics > > column_stats
Column statistics.
Definition duckdb.hpp:48428
unique_ptr< BlockingSample > table_sample
The table sample.
Definition duckdb.hpp:48430
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ InitializeEmpty() [1/2]

void duckdb::TableStatistics::InitializeEmpty ( const vector< LogicalType > & types)
438507 {
438508 D_ASSERT(Empty());
438509 D_ASSERT(!table_sample);
438510
438511 stats_lock = make_shared_ptr<mutex>();
438512 table_sample = make_uniq<ReservoirSample>(static_cast<idx_t>(FIXED_SAMPLE_SIZE));
438513 for (auto &type : types) {
438514 column_stats.push_back(ColumnStatistics::CreateEmptyStats(type));
438515 }
438516}

◆ InitializeEmpty() [2/2]

void duckdb::TableStatistics::InitializeEmpty ( const TableStatistics & other)
438480 {
438481 D_ASSERT(Empty());
438482 D_ASSERT(!table_sample);
438483
438484 stats_lock = make_shared_ptr<mutex>();
438485 if (other.table_sample) {
438486 D_ASSERT(other.table_sample->type == SampleType::RESERVOIR_SAMPLE);
438487 auto &res = other.table_sample->Cast<ReservoirSample>();
438488 table_sample = res.Copy();
438489 } else {
438490 table_sample = make_uniq<ReservoirSample>(static_cast<idx_t>(FIXED_SAMPLE_SIZE));
438491 }
438492
438493 for (auto &stats : other.column_stats) {
438494 auto new_column_stats = ColumnStatistics::CreateEmptyStats(stats->Statistics().GetType());
438495 if (stats->HasDistinctStats()) {
438496 new_column_stats->SetDistinct(stats->DistinctStats().Copy());
438497 }
438498
438499 auto &base_stats = new_column_stats->Statistics();
438500 if (new_column_stats->HasDistinctStats()) {
438501 base_stats.SetDistinctCount(new_column_stats->DistinctStats().GetCount());
438502 }
438503 column_stats.push_back(new_column_stats);
438504 }
438505}

◆ InitializeAddColumn()

void duckdb::TableStatistics::InitializeAddColumn ( TableStatistics & parent,
const LogicalType & new_column_type )
438518 {
438519 D_ASSERT(Empty());
438520 D_ASSERT(parent.stats_lock);
438521
438522 stats_lock = parent.stats_lock;
438523 lock_guard<mutex> lock(*stats_lock);
438524 for (idx_t i = 0; i < parent.column_stats.size(); i++) {
438525 column_stats.push_back(parent.column_stats[i]);
438526 }
438527 column_stats.push_back(ColumnStatistics::CreateEmptyStats(new_column_type));
438528 if (parent.table_sample) {
438529 table_sample = std::move(parent.table_sample);
438530 }
438531 if (table_sample) {
438532 table_sample->Destroy();
438533 }
438534}

◆ InitializeRemoveColumn()

void duckdb::TableStatistics::InitializeRemoveColumn ( TableStatistics & parent,
idx_t removed_column )
438536 {
438537 D_ASSERT(Empty());
438538 D_ASSERT(parent.stats_lock);
438539
438540 stats_lock = parent.stats_lock;
438541 lock_guard<mutex> lock(*stats_lock);
438542 for (idx_t i = 0; i < parent.column_stats.size(); i++) {
438543 if (i != removed_column) {
438544 column_stats.push_back(parent.column_stats[i]);
438545 }
438546 }
438547 if (parent.table_sample) {
438548 table_sample = std::move(parent.table_sample);
438549 }
438550 if (table_sample) {
438551 table_sample->Destroy();
438552 }
438553}

◆ InitializeAlterType()

void duckdb::TableStatistics::InitializeAlterType ( TableStatistics & parent,
idx_t changed_idx,
const LogicalType & new_type )
438555 {
438556 D_ASSERT(Empty());
438557 D_ASSERT(parent.stats_lock);
438558
438559 stats_lock = parent.stats_lock;
438560 lock_guard<mutex> lock(*stats_lock);
438561 for (idx_t i = 0; i < parent.column_stats.size(); i++) {
438562 if (i == changed_idx) {
438563 column_stats.push_back(ColumnStatistics::CreateEmptyStats(new_type));
438564 } else {
438565 column_stats.push_back(parent.column_stats[i]);
438566 }
438567 }
438568 if (parent.table_sample) {
438569 table_sample = std::move(parent.table_sample);
438570 }
438571 if (table_sample) {
438572 table_sample->Destroy();
438573 }
438574}

◆ InitializeAddConstraint()

void duckdb::TableStatistics::InitializeAddConstraint ( TableStatistics & parent)
438576 {
438577 D_ASSERT(Empty());
438578 D_ASSERT(parent.stats_lock);
438579
438580 stats_lock = parent.stats_lock;
438581 lock_guard<mutex> lock(*stats_lock);
438582 for (idx_t i = 0; i < parent.column_stats.size(); i++) {
438583 column_stats.push_back(parent.column_stats[i]);
438584 }
438585}

◆ MergeStats() [1/3]

void duckdb::TableStatistics::MergeStats ( TableStatistics & other)
438587 {
438588 auto l = GetLock();
438589 D_ASSERT(column_stats.size() == other.column_stats.size());
438590 if (table_sample) {
438591 if (other.table_sample) {
438592 D_ASSERT(table_sample->type == SampleType::RESERVOIR_SAMPLE);
438593 auto &this_reservoir = table_sample->Cast<ReservoirSample>();
438594 D_ASSERT(other.table_sample->type == SampleType::RESERVOIR_SAMPLE);
438595 this_reservoir.Merge(std::move(other.table_sample));
438596 }
438597 // if no other.table sample, do nothig
438598 } else {
438599 if (other.table_sample) {
438600 auto &other_reservoir = other.table_sample->Cast<ReservoirSample>();
438601 auto other_table_sample_copy = other_reservoir.Copy();
438602 table_sample = std::move(other_table_sample_copy);
438603 }
438604 }
438605 for (idx_t i = 0; i < column_stats.size(); i++) {
438606 if (column_stats[i]) {
438607 D_ASSERT(other.column_stats[i]);
438608 column_stats[i]->Merge(*other.column_stats[i]);
438609 }
438610 }
438611}

◆ MergeStats() [2/3]

void duckdb::TableStatistics::MergeStats ( idx_t i,
BaseStatistics & stats )
438613 {
438614 auto l = GetLock();
438615 MergeStats(*l, i, stats);
438616}

◆ MergeStats() [3/3]

void duckdb::TableStatistics::MergeStats ( TableStatisticsLock & lock,
idx_t i,
BaseStatistics & stats )
438618 {
438619 column_stats[i]->Statistics().Merge(stats);
438620}

◆ SetStats()

void duckdb::TableStatistics::SetStats ( TableStatistics & other)
438645 {
438646 TableStatisticsLock lock(*stats_lock);
438647 column_stats = std::move(other.column_stats);
438648 table_sample = std::move(other.table_sample);
438649}

◆ CopyStats() [1/3]

void duckdb::TableStatistics::CopyStats ( TableStatistics & other)
438666 {
438667 TableStatisticsLock lock(*stats_lock);
438668 CopyStats(lock, other);
438669}

◆ CopyStats() [2/3]

void duckdb::TableStatistics::CopyStats ( TableStatisticsLock & lock,
TableStatistics & other )
438671 {
438672 D_ASSERT(other.Empty());
438673 other.stats_lock = make_shared_ptr<mutex>();
438674 for (auto &stats : column_stats) {
438675 other.column_stats.push_back(stats->Copy());
438676 }
438677
438678 if (table_sample) {
438679 D_ASSERT(table_sample->type == SampleType::RESERVOIR_SAMPLE);
438680 auto &res = table_sample->Cast<ReservoirSample>();
438681 other.table_sample = res.Copy();
438682 }
438683}

◆ CopyStats() [3/3]

unique_ptr< BaseStatistics > duckdb::TableStatistics::CopyStats ( const StorageIndex & i)
438651 {
438652 lock_guard<mutex> l(*stats_lock);
438653
438654 auto column_index = index.GetPrimaryIndex();
438655 auto &stats = *column_stats[column_index];
438656 auto result = stats.Statistics().Copy();
438657 if (stats.HasDistinctStats()) {
438658 result.SetDistinctCount(stats.DistinctStats().GetCount());
438659 }
438660 if (index.IsPushdownExtract()) {
438661 return result.PushdownExtract(index.GetChildIndexes()[0]);
438662 }
438663 return result.ToUnique();
438664}
index

◆ GetStats()

ColumnStatistics & duckdb::TableStatistics::GetStats ( TableStatisticsLock & lock,
idx_t i )

Get a reference to the stats - this requires us to hold the lock. The reference can only be safely accessed while the lock is held.

438622 {
438623 return *column_stats[i];
438624}
Here is the caller graph for this function:

◆ GetTableSample()

unique_ptr< BlockingSample > duckdb::TableStatistics::GetTableSample ( TableStatisticsLock & lock)

Get a reference to the table sample - this requires us to hold the lock.

Take ownership of the sample, needed for merging. Requires the lock

438631 {
438632 return std::move(table_sample);
438633}

◆ SetTableSample()

void duckdb::TableStatistics::SetTableSample ( TableStatisticsLock & lock,
unique_ptr< BlockingSample > sample )
438635 {
438636 table_sample = std::move(sample);
438637}

◆ DestroyTableSample()

void duckdb::TableStatistics::DestroyTableSample ( TableStatisticsLock & lock) const
438639 {
438640 if (table_sample) {
438641 table_sample->Destroy();
438642 }
438643}

◆ Empty()

bool duckdb::TableStatistics::Empty ( )
438733 {
438734 D_ASSERT(column_stats.empty() == (stats_lock.get() == nullptr));
438735 return column_stats.empty();
438736}

◆ GetLock()

unique_ptr< TableStatisticsLock > duckdb::TableStatistics::GetLock ( )
438728 {
438729 D_ASSERT(stats_lock);
438730 return make_uniq<TableStatisticsLock>(*stats_lock);
438731}

◆ Serialize()

void duckdb::TableStatistics::Serialize ( Serializer & serializer) const
438685 {
438686 serializer.WriteProperty(100, "column_stats", column_stats);
438687 unique_ptr<BlockingSample> to_serialize = nullptr;
438688 if (table_sample) {
438689 D_ASSERT(table_sample->type == SampleType::RESERVOIR_SAMPLE);
438690 auto &reservoir_sample = table_sample->Cast<ReservoirSample>();
438691 to_serialize = unique_ptr_cast<BlockingSample, ReservoirSample>(reservoir_sample.Copy());
438692 auto &res_serialize = to_serialize->Cast<ReservoirSample>();
438693 res_serialize.EvictOverBudgetSamples();
438694 }
438695 serializer.WritePropertyWithDefault<unique_ptr<BlockingSample>>(101, "table_sample", to_serialize, nullptr);
438696}

◆ Deserialize()

void duckdb::TableStatistics::Deserialize ( Deserializer & deserializer,
ColumnList & columns )
438698 {
438699 auto physical_columns = columns.Physical();
438700
438701 auto iter = physical_columns.begin();
438702 deserializer.ReadList(100, "column_stats", [&](Deserializer::List &list, idx_t i) {
438703 auto &col = *iter;
438704 iter.operator++();
438705
438706 auto type = col.GetType();
438707 deserializer.Set<LogicalType &>(type);
438708
438709 column_stats.push_back(list.ReadElement<shared_ptr<ColumnStatistics>>());
438710
438711 deserializer.Unset<LogicalType>();
438712 });
438713 table_sample = deserializer.ReadPropertyWithDefault<unique_ptr<BlockingSample>>(101, "table_sample");
438714 if (table_sample) {
438715 D_ASSERT(table_sample->type == SampleType::RESERVOIR_SAMPLE);
438716#ifdef DEBUG
438717 if (table_sample) {
438718 auto &reservoir_sample = table_sample->Cast<ReservoirSample>();
438719 reservoir_sample.Verify();
438720 }
438721#endif
438722 } else {
438723 table_sample = make_uniq<ReservoirSample>(static_cast<idx_t>(FIXED_SAMPLE_SIZE));
438724 table_sample->Destroy();
438725 }
438726}

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