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

Public Member Functions

 LocalTableStorage (ClientContext &context, DataTable &table)
 LocalTableStorage (ClientContext &context, DataTable &new_data_table, LocalTableStorage &parent, const idx_t alter_column_index, const LogicalType &target_type, const vector< StorageIndex > &bound_columns, Expression &cast_expr)
 Create a LocalTableStorage from an ALTER TYPE.
 LocalTableStorage (DataTable &new_data_table, LocalTableStorage &parent, const idx_t drop_column_index)
 Create a LocalTableStorage from a DROP COLUMN.
 LocalTableStorage (ClientContext &context, DataTable &table, LocalTableStorage &parent, ColumnDefinition &new_column, ExpressionExecutor &default_executor)
void InitializeScan (CollectionScanState &state, optional_ptr< TableFilterSet > table_filters=nullptr)
void WriteNewRowGroup ()
 Write a new row group to disk (if possible).
void FlushBlocks ()
void Rollback ()
idx_t EstimatedSize ()
void AppendToIndexes (DuckTransaction &transaction, TableAppendState &append_state)
void AppendToTable (DuckTransaction &transaction, TableAppendState &append_state)
ErrorData AppendToIndexes (DuckTransaction &transaction, RowGroupCollection &source, TableIndexList &index_list, const vector< LogicalType > &table_types, row_t &start_row)
void AppendToDeleteIndexes (Vector &row_ids, DataChunk &delete_chunk)
PhysicalIndex CreateOptimisticCollection (unique_ptr< OptimisticWriteCollection > collection)
 Create an optimistic row group collection for this table. Returns the index into the optimistic_collections vector for newly created collection.
OptimisticWriteCollection & GetOptimisticCollection (const PhysicalIndex collection_index)
 Returns the optimistic row group collection corresponding to the index.
void ResetOptimisticCollection (const PhysicalIndex collection_index)
 Resets the optimistic row group collection corresponding to the index.
OptimisticDataWriter & GetOptimisticWriter ()
 Returns the optimistic writer.
RowGroupCollection & GetCollection ()
OptimisticWriteCollection & GetPrimaryCollection ()
Public Member Functions inherited from duckdb::enable_shared_from_this< LocalTableStorage >
shared_ptr< LocalTableStorage > shared_from_this ()

Public Attributes

QueryContext context
reference< DataTable > table_ref
Allocator & allocator
unique_ptr< OptimisticWriteCollection > row_groups
 The main row group collection.
TableIndexList append_indexes
 The set of unique append indexes.
TableIndexList delete_indexes
 The set of delete indexes.
IndexAppendMode index_append_mode = IndexAppendMode::DEFAULT
 Set to INSERT_DUPLICATES, if we are skipping constraint checking during, e.g., WAL replay.
idx_t deleted_rows
 The number of deleted rows.
vector< unique_ptr< OptimisticWriteCollection > > optimistic_collections
 The optimistic row group collections associated with this table.
OptimisticDataWriter optimistic_writer
 The main optimistic data writer associated with this table.
bool is_dropped = false
 Whether or not the storage was dropped.

Private Attributes

mutex collections_lock

Additional Inherited Members

Protected Member Functions inherited from duckdb::enable_shared_from_this< LocalTableStorage >
enable_shared_from_this & operator= (enable_shared_from_this const &) noexcept

Constructor & Destructor Documentation

◆ LocalTableStorage() [1/4]

duckdb::LocalTableStorage::LocalTableStorage ( ClientContext & context,
DataTable & table )
explicit
413487 : context(context), table_ref(table), allocator(Allocator::Get(table.db)), deleted_rows(0),
413488 optimistic_writer(context, table) {
413489 auto types = table.GetTypes();
413490 auto data_table_info = table.GetDataTableInfo();
413491 row_groups = optimistic_writer.CreateCollection(table, types, OptimisticWritePartialManagers::GLOBAL);
413492 auto &collection = *row_groups->collection;
413493 collection.InitializeEmpty();
413494
413495 for (auto &index : data_table_info->GetIndexes().Indexes()) {
413496 auto constraint = index.GetConstraintType();
413497 if (constraint == IndexConstraintType::NONE) {
413498 continue;
413499 }
413500 if (!index.IsBound()) {
413501 continue;
413502 }
413503 auto &bound_index = index.Cast<BoundIndex>();
413504 if (!bound_index.SupportsDeltaIndexes()) {
413505 continue;
413506 }
413507
413508 // Create a delete index and a local index.
413509 auto delete_index = bound_index.CreateDeltaIndex(DeltaIndexType::LOCAL_DELETE);
413510 delete_indexes.AddIndex(std::move(delete_index));
413511
413512 auto append_index = bound_index.CreateDeltaIndex(DeltaIndexType::LOCAL_APPEND);
413513 append_indexes.AddIndex(std::move(append_index));
413514 }
413515}
TableIndexList append_indexes
The set of unique append indexes.
Definition duckdb.hpp:50802
unique_ptr< OptimisticWriteCollection > row_groups
The main row group collection.
Definition duckdb.hpp:50800
idx_t deleted_rows
The number of deleted rows.
Definition duckdb.hpp:50808
TableIndexList delete_indexes
The set of delete indexes.
Definition duckdb.hpp:50804
OptimisticDataWriter optimistic_writer
The main optimistic data writer associated with this table.
Definition duckdb.hpp:50813
index

◆ LocalTableStorage() [2/4]

duckdb::LocalTableStorage::LocalTableStorage ( ClientContext & context,
DataTable & new_data_table,
LocalTableStorage & parent,
const idx_t alter_column_index,
const LogicalType & target_type,
const vector< StorageIndex > & bound_columns,
Expression & cast_expr )

Create a LocalTableStorage from an ALTER TYPE.

413520 : context(context), table_ref(new_data_table), allocator(Allocator::Get(new_data_table.db)),
413521 deleted_rows(parent.deleted_rows), optimistic_collections(std::move(parent.optimistic_collections)),
413522 optimistic_writer(new_data_table, parent.optimistic_writer) {
413523 // Alter the column type.
413524 auto &parent_collection = *parent.row_groups->collection;
413525 auto new_collection =
413526 parent_collection.AlterType(context, alter_column_index, target_type, bound_columns, cast_expr);
413527 parent_collection.CommitDropColumn(alter_column_index);
413528 row_groups = std::move(parent.row_groups);
413529 row_groups->collection = std::move(new_collection);
413530
413531 append_indexes.Move(parent.append_indexes);
413532}
vector< unique_ptr< OptimisticWriteCollection > > optimistic_collections
The optimistic row group collections associated with this table.
Definition duckdb.hpp:50811

◆ LocalTableStorage() [3/4]

duckdb::LocalTableStorage::LocalTableStorage ( DataTable & new_data_table,
LocalTableStorage & parent,
const idx_t drop_column_index )

Create a LocalTableStorage from a DROP COLUMN.

413536 : table_ref(new_data_table), allocator(Allocator::Get(new_data_table.db)), deleted_rows(parent.deleted_rows),
413537 optimistic_collections(std::move(parent.optimistic_collections)),
413538 optimistic_writer(new_data_table, parent.optimistic_writer) {
413539 // Remove the column from the previous table storage.
413540 auto &parent_collection = *parent.row_groups->collection;
413541 auto new_collection = parent_collection.RemoveColumn(drop_column_index);
413542 parent_collection.CommitDropColumn(drop_column_index);
413543 row_groups = std::move(parent.row_groups);
413544 row_groups->collection = std::move(new_collection);
413545
413546 append_indexes.Move(parent.append_indexes);
413547}

◆ LocalTableStorage() [4/4]

duckdb::LocalTableStorage::LocalTableStorage ( ClientContext & context,
DataTable & table,
LocalTableStorage & parent,
ColumnDefinition & new_column,
ExpressionExecutor & default_executor )
413551 : table_ref(new_dt), allocator(Allocator::Get(new_dt.db)), deleted_rows(parent.deleted_rows),
413552 optimistic_collections(std::move(parent.optimistic_collections)),
413553 optimistic_writer(new_dt, parent.optimistic_writer) {
413554 auto &parent_collection = *parent.row_groups->collection;
413555 auto new_collection = parent_collection.AddColumn(context, new_column, default_executor);
413556 row_groups = std::move(parent.row_groups);
413557 row_groups->collection = std::move(new_collection);
413558 append_indexes.Move(parent.append_indexes);
413559}

◆ ~LocalTableStorage()

duckdb::LocalTableStorage::~LocalTableStorage ( )
413561 {
413562}

Member Function Documentation

◆ InitializeScan()

void duckdb::LocalTableStorage::InitializeScan ( CollectionScanState & state,
optional_ptr< TableFilterSet > table_filters = nullptr )
413564 {
413565 auto &collection = *row_groups->collection;
413566 if (collection.GetTotalRows() == 0) {
413567 throw InternalException("No rows in LocalTableStorage row group for scan");
413568 }
413569 collection.InitializeScan(context, state, state.GetColumnIds(), table_filters.get());
413570}

◆ WriteNewRowGroup()

void duckdb::LocalTableStorage::WriteNewRowGroup ( )

Write a new row group to disk (if possible).

413610 {
413611 if (deleted_rows != 0) {
413612 // we have deletes - we cannot merge row groups
413613 return;
413614 }
413615 optimistic_writer.WriteNewRowGroup(*row_groups);
413616}

◆ FlushBlocks()

void duckdb::LocalTableStorage::FlushBlocks ( )
413618 {
413619 auto &collection = *row_groups->collection;
413620 const idx_t row_group_size = collection.GetRowGroupSize();
413621 if (collection.GetTotalRows() >= row_group_size) {
413622 // write any unflushed row groups
413623 optimistic_writer.WriteUnflushedRowGroups(*row_groups);
413624 }
413625 optimistic_writer.FinalFlush();
413626}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ Rollback()

void duckdb::LocalTableStorage::Rollback ( )
413756 {
413757 optimistic_writer.Rollback();
413758
413759 for (auto &collection : optimistic_collections) {
413760 if (!collection) {
413761 continue;
413762 }
413763 collection->collection->CommitDropTable();
413764 }
413765 optimistic_collections.clear();
413766 row_groups->collection->CommitDropTable();
413767}

◆ EstimatedSize()

idx_t duckdb::LocalTableStorage::EstimatedSize ( )
413572 {
413573 // count the appended rows
413574 auto &collection = *row_groups->collection;
413575 idx_t data_size = 0;
413576
413577 if (collection.GetTotalRows() >= collection.GetRowGroupSize() && deleted_rows == 0) {
413578 // Optimistic insertion does not generate many WAL logs, so we estimate the size of the Data Block Pointers here
413579 idx_t row_group_count = row_groups->complete_row_groups + 1;
413580 idx_t column_count = collection.GetTypes().size();
413581
413582 data_size = row_group_count * (sizeof(PersistentRowGroupData) +
413583 column_count * (sizeof(PersistentColumnData) + sizeof(DataPointer)));
413584 } else {
413585 idx_t appended_rows = collection.GetTotalRows() - deleted_rows;
413586
413587 // get the (estimated) size of a row (no compressions, etc.)
413588 idx_t row_size = 0;
413589 auto &types = collection.GetTypes();
413590 for (auto &type : types) {
413591 row_size += GetTypeIdSize(type.InternalType());
413592 }
413593
413594 data_size = appended_rows * row_size;
413595 }
413596
413597 // get the index size
413598 idx_t index_sizes = 0;
413599 for (auto &index : append_indexes.Indexes()) {
413600 if (!index.IsBound()) {
413601 continue;
413602 }
413603 index_sizes += index.Cast<BoundIndex>().GetInMemorySize();
413604 }
413605
413606 // return the size of the appended rows and the index size
413607 return data_size + index_sizes;
413608}

◆ AppendToIndexes() [1/2]

void duckdb::LocalTableStorage::AppendToIndexes ( DuckTransaction & transaction,
TableAppendState & append_state )
413695 {
413696 // In this function, we might scan all table columns,
413697 // as we might also append to the table itself (append_to_table).
413698 auto &table = table_ref.get();
413699 if (!table.HasIndexes()) {
413700 // no indexes to append to
413701 return;
413702 }
413703 auto data_table_info = table.GetDataTableInfo();
413704 auto &index_list = data_table_info->GetIndexes();
413705 auto &collection = *row_groups->collection;
413706 auto error = AppendToIndexes(transaction, collection, index_list, table.GetTypes(), append_state.current_row);
413707 if (error.HasError()) {
413708 // Revert all appended row IDs.
413709 row_t current_row = append_state.row_start;
413710 // Remove the data from the indexes, if any.
413711 for (auto &chunk : collection.Chunks(transaction)) {
413712 if (current_row >= append_state.current_row) {
413713 // Finished deleting all rows from the index.
413714 break;
413715 }
413716 // Remove the chunk.
413717 try {
413718 table.RevertIndexAppend(append_state, chunk, current_row);
413719 } catch (std::exception &ex) { // LCOV_EXCL_START
413720 error = ErrorData(ex);
413721 break;
413722 } // LCOV_EXCL_STOP
413723
413724 current_row += UnsafeNumericCast<row_t>(chunk.size());
413725 }
413726
413727#ifdef DEBUG
413728 // Verify that our index memory is stable.
413729 table.VerifyIndexBuffers();
413730#endif
413731 error.Throw();
413732 }
413733}
int64_t row_t
The type used for row identifiers.
Definition duckdb.hpp:240

◆ AppendToTable()

void duckdb::LocalTableStorage::AppendToTable ( DuckTransaction & transaction,
TableAppendState & append_state )
413684 {
413685 auto &table = table_ref.get();
413686 table.InitializeAppend(transaction, append_state);
413687 auto &collection = *row_groups->collection;
413688 for (auto &table_chunk : collection.Chunks(transaction)) {
413689 // Append to the base table.
413690 table.Append(table_chunk, append_state);
413691 }
413692 table.FinalizeAppend(transaction, append_state);
413693}

◆ AppendToIndexes() [2/2]

ErrorData duckdb::LocalTableStorage::AppendToIndexes ( DuckTransaction & transaction,
RowGroupCollection & source,
TableIndexList & index_list,
const vector< LogicalType > & table_types,
row_t & start_row )
413630 {
413631 // mapped_column_ids contains the physical column indices of each Indexed column in the table.
413632 // This mapping is used to retrieve the physical column index for the corresponding vector of an index chunk scan.
413633 // For example, if we are processing data for index_chunk.data[i], we can retrieve the physical column index
413634 // by getting the value at mapped_column_ids[i].
413635 // An important note is that the index_chunk orderings are created in accordance with this mapping, not the other
413636 // way around. (Check the scan code below, where the mapped_column_ids is passed as a parameter to the scan.
413637 // The index_chunk inside of that lambda is ordered according to the mapping that is a parameter to the scan).
413638
413639 // mapped_column_ids is used in two places:
413640 // 1) To create the physical table chunk in this function.
413641 // 2) If we are in an unbound state (i.e., WAL replay is happening right now), this mapping and the index_chunk
413642 // are buffered in unbound_index. However, there can also be buffered deletes happening, so it is important
413643 // to maintain a canonical representation of the mapping, which is just sorting.
413644 D_ASSERT(!index_list.Empty());
413645 auto indexed_columns = index_list.GetRequiredColumns();
413646 vector<StorageIndex> mapped_column_ids;
413647 for (auto &col : indexed_columns) {
413648 mapped_column_ids.emplace_back(col);
413649 }
413650 std::sort(mapped_column_ids.begin(), mapped_column_ids.end());
413651 auto active_checkpoint = transaction.GetTransactionManager().Cast<DuckTransactionManager>().GetActiveCheckpoint();
413652 auto checkpoint_id = active_checkpoint == MAX_TRANSACTION_ID ? optional_idx() : active_checkpoint;
413653
413654 // However, because the bound expressions of the indexes (and their bound
413655 // column references) are in relation to ALL table columns, we create an
413656 // empty table chunk based on the table types. It references the indexed columns,
413657 // and contains nothing for all non-indexed columns.
413658 DataChunk table_chunk;
413659 table_chunk.InitializeEmpty(table_types);
413660
413661 // index_chunk scans are created here in the mapped_column_ids ordering (see note above).
413662 ErrorData error;
413663 for (auto &index_chunk : source.Chunks(transaction, mapped_column_ids)) {
413664 D_ASSERT(index_chunk.ColumnCount() == mapped_column_ids.size());
413665 for (idx_t i = 0; i < mapped_column_ids.size(); i++) {
413666 auto col_id = mapped_column_ids[i].GetPrimaryIndex();
413667 table_chunk.data[col_id].Reference(index_chunk.data[i]);
413668 }
413669 table_chunk.SetCardinality(index_chunk);
413670
413671 // Pass both the table and the index chunk.
413672 // We need the table chunk for the bound indexes,
413673 // and the index chunk for the unbound indexes (to buffer it).
413674 error = DataTable::AppendToIndexes(index_list, delete_indexes, table_chunk, index_chunk, mapped_column_ids,
413675 start_row, index_append_mode, checkpoint_id);
413676 if (error.HasError()) {
413677 break;
413678 }
413679 start_row += UnsafeNumericCast<row_t>(index_chunk.size());
413680 }
413681 return error;
413682}
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
vector< Vector > data
The vectors owned by the DataChunk.
Definition duckdb.hpp:11445
static ErrorData AppendToIndexes(TableIndexList &indexes, optional_ptr< TableIndexList > delete_indexes, DataChunk &table_chunk, DataChunk &index_chunk, const vector< StorageIndex > &mapped_column_ids, row_t row_start, const IndexAppendMode index_append_mode, optional_idx active_checkpoint)
Appends a chunk with the row ids [row_start, ..., row_start + chunk.size()] to all indexes of the tab...
Definition duckdb.cpp:412695
IndexAppendMode index_append_mode
Set to INSERT_DUPLICATES, if we are skipping constraint checking during, e.g., WAL replay.
Definition duckdb.hpp:50806

◆ AppendToDeleteIndexes()

void duckdb::LocalTableStorage::AppendToDeleteIndexes ( Vector & row_ids,
DataChunk & delete_chunk )
413902 {
413903 if (delete_chunk.size() == 0) {
413904 return;
413905 }
413906
413907 // Only committed row IDs (< MAX_ROW_ID) belong in the delete indexes.
413908 // Local row IDs (>= MAX_ROW_ID) live in transaction-local storage and are
413909 // handled directly by LocalStorage::Delete.
413910 row_ids.Flatten(delete_chunk.size());
413911 auto flat_row_ids = FlatVector::GetData<row_t>(row_ids);
413912 idx_t committed_count = 0;
413913 SelectionVector committed_sel(delete_chunk.size());
413914 for (idx_t i = 0; i < delete_chunk.size(); i++) {
413915 if (flat_row_ids[i] < MAX_ROW_ID) {
413916 committed_sel.set_index(committed_count++, i);
413917 }
413918 }
413919
413920 if (committed_count == 0) {
413921 return;
413922 }
413923
413924 DataChunk committed_chunk;
413925 committed_chunk.Initialize(Allocator::DefaultAllocator(), delete_chunk.GetTypes());
413926 committed_chunk.Slice(delete_chunk, committed_sel, committed_count);
413927
413928 Vector committed_row_ids(row_ids, committed_sel, committed_count);
413929 committed_row_ids.Flatten(committed_count);
413930
413931 for (auto &index : delete_indexes.Indexes()) {
413932 D_ASSERT(index.IsBound());
413933 if (!index.IsUnique()) {
413934 continue;
413935 }
413936 IndexAppendInfo index_append_info(IndexAppendMode::IGNORE_DUPLICATES, nullptr);
413937 auto result = index.Cast<BoundIndex>().Append(committed_chunk, committed_row_ids, index_append_info);
413938 if (result.HasError()) {
413939 throw InternalException("unexpected constraint violation on delete ART: ", result.Message());
413940 }
413941 }
413942}
DUCKDB_API void Initialize(ClientContext &context, const vector< LogicalType > &types, idx_t capacity=STANDARD_VECTOR_SIZE)
Definition duckdb.cpp:116791
DUCKDB_API vector< LogicalType > GetTypes() const
Returns a list of types of the vectors of this data chunk.
Definition duckdb.cpp:116995
const row_t MAX_ROW_ID
The maximum row identifier used in tables.
Definition duckdb.cpp:50301

◆ CreateOptimisticCollection()

PhysicalIndex duckdb::LocalTableStorage::CreateOptimisticCollection ( unique_ptr< OptimisticWriteCollection > collection)

Create an optimistic row group collection for this table. Returns the index into the optimistic_collections vector for newly created collection.

413735 {
413736 lock_guard<mutex> l(collections_lock);
413737 optimistic_collections.push_back(std::move(collection));
413738 return PhysicalIndex(optimistic_collections.size() - 1);
413739}

◆ GetOptimisticCollection()

OptimisticWriteCollection & duckdb::LocalTableStorage::GetOptimisticCollection ( const PhysicalIndex collection_index)

Returns the optimistic row group collection corresponding to the index.

413741 {
413742 lock_guard<mutex> l(collections_lock);
413743 auto &collection = optimistic_collections[collection_index.index];
413744 return *collection;
413745}

◆ ResetOptimisticCollection()

void duckdb::LocalTableStorage::ResetOptimisticCollection ( const PhysicalIndex collection_index)

Resets the optimistic row group collection corresponding to the index.

413747 {
413748 lock_guard<mutex> l(collections_lock);
413749 optimistic_collections[collection_index.index].reset();
413750}

◆ GetOptimisticWriter()

OptimisticDataWriter & duckdb::LocalTableStorage::GetOptimisticWriter ( )

Returns the optimistic writer.

413752 {
413753 return optimistic_writer;
413754}

◆ GetCollection()

RowGroupCollection & duckdb::LocalTableStorage::GetCollection ( )
413876 {
413877 return *row_groups->collection;
413878}

◆ GetPrimaryCollection()

OptimisticWriteCollection & duckdb::LocalTableStorage::GetPrimaryCollection ( )
413880 {
413881 return *row_groups;
413882}

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