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

Public Member Functions

 ColumnDataCheckpointer (vector< reference< ColumnCheckpointState > > &states, StorageManager &storage_manager, const RowGroup &row_group, ColumnCheckpointInfo &checkpoint_info)
void Checkpoint ()
void FinalizeCheckpoint ()

Private Member Functions

void ScanSegments (const std::function< void(Vector &, idx_t)> &callback)
vector< CheckpointAnalyzeResult > DetectBestCompressionMethod ()
void WriteToDisk ()
void WritePersistentSegments (ColumnCheckpointState &state)
bool HasChanges (ColumnData &col_data)
void InitAnalyze ()
void DropSegments ()
bool ValidityCoveredByBasedata (vector< CheckpointAnalyzeResult > &result)

Private Attributes

vector< reference< ColumnCheckpointState > > & checkpoint_states
StorageManager & storage_manager
const RowGroup & row_group
Vector intermediate
ColumnCheckpointInfo & checkpoint_info
bool has_changes = false
vector< vector< optional_ptr< const CompressionFunction > > > compression_functions
 For every column data that is being checkpointed, the applicable functions.
vector< vector< unique_ptr< AnalyzeState > > > analyze_states
 For every column data that is being checkpointed, the analyze state of functions being tried.

Constructor & Destructor Documentation

◆ ColumnDataCheckpointer()

duckdb::ColumnDataCheckpointer::ColumnDataCheckpointer ( vector< reference< ColumnCheckpointState > > & states,
StorageManager & storage_manager,
const RowGroup & row_group,
ColumnCheckpointInfo & checkpoint_info )
430152 : checkpoint_states(checkpoint_states), storage_manager(storage_manager), row_group(row_group),
430153 intermediate(CreateIntermediateVector(checkpoint_states)), checkpoint_info(checkpoint_info) {
430154 auto &db = storage_manager.GetDatabase();
430155 auto &config = DBConfig::GetConfig(db);
430156 compression_functions.resize(checkpoint_states.size());
430157 for (idx_t i = 0; i < checkpoint_states.size(); i++) {
430158 auto &col_data = checkpoint_states[i].get().original_column;
430159 auto to_add = config.GetCompressionFunctions(col_data.type.InternalType());
430160 auto &functions = compression_functions[i];
430161 for (auto &func : to_add) {
430162 functions.push_back(&func.get());
430163 }
430164 }
430165}
vector< vector< optional_ptr< const CompressionFunction > > > compression_functions
For every column data that is being checkpointed, the applicable functions.
Definition duckdb.cpp:276656
db
static Vector CreateIntermediateVector(vector< reference< ColumnCheckpointState > > &states)
ColumnDataCheckpointer.
Definition duckdb.cpp:430134
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

Member Function Documentation

◆ Checkpoint()

void duckdb::ColumnDataCheckpointer::Checkpoint ( )
430486 {
430487 for (idx_t i = 0; i < checkpoint_states.size(); i++) {
430488 auto &state = checkpoint_states[i];
430489 auto &col_data = state.get().original_column;
430490 if (col_data.HasChanges()) {
430491 has_changes = true;
430492 break;
430493 }
430494 }
430495
430496 if (!has_changes) {
430497 // Nothing has undergone any changes, no need to checkpoint
430498 // just move on to finalizing
430499 // mark block ids as checkpointed
430500 CheckpointBlockIdMarker marker(storage_manager.GetBlockManager());
430501 for (idx_t i = 0; i < checkpoint_states.size(); i++) {
430502 auto &state = checkpoint_states[i];
430503 auto &col_data = state.get().original_column;
430504 col_data.VisitBlockIds(marker);
430505 }
430506 return;
430507 }
430508
430509 WriteToDisk();
430510}

◆ FinalizeCheckpoint()

void duckdb::ColumnDataCheckpointer::FinalizeCheckpoint ( )
430512 {
430513 if (has_changes) {
430514 // something has undergone changes, we rewrote everything
430515 // write the new data - not the old data
430516 return;
430517 }
430518 // no changes - copy over the original columns
430519 for (idx_t i = 0; i < checkpoint_states.size(); i++) {
430520 auto &state = checkpoint_states[i].get();
430521 WritePersistentSegments(state);
430522 }
430523}

◆ ScanSegments()

void duckdb::ColumnDataCheckpointer::ScanSegments ( const std::function< void(Vector &, idx_t)> & callback)
private
430167 {
430168 Vector scan_vector(intermediate.GetType(), nullptr);
430169 auto &first_state = checkpoint_states[0];
430170 auto &col_data = first_state.get().original_column;
430171
430172 // TODO: scan all the nodes from all segments, no need for CheckpointScan to virtualize this I think..
430173 for (auto &segment_node : col_data.data.SegmentNodes()) {
430174 auto &segment = segment_node.GetNode();
430175 ColumnScanState scan_state(nullptr);
430176 scan_state.current = segment_node;
430177 segment.InitializeScan(scan_state);
430178
430179 for (idx_t base_row_index = 0; base_row_index < segment.count; base_row_index += STANDARD_VECTOR_SIZE) {
430180 scan_vector.Reference(intermediate);
430181
430182 idx_t count = MinValue<idx_t>(segment.count - base_row_index, STANDARD_VECTOR_SIZE);
430183 scan_state.offset_in_column = segment_node.GetRowStart() + base_row_index;
430184
430185 col_data.CheckpointScan(segment, scan_state, count, scan_vector);
430186 callback(scan_vector, count);
430187 }
430188 }
430189}

◆ DetectBestCompressionMethod()

vector< CheckpointAnalyzeResult > duckdb::ColumnDataCheckpointer::DetectBestCompressionMethod ( )
private

Check if the method type is the forced method (if forced is used)

The finalize method can return this value from final_analyze to indicate it should not be used.

If we have found the forced method, we're done

430246 {
430247 D_ASSERT(!compression_functions.empty());
430248 auto &db = storage_manager.GetDatabase();
430249 auto &config = DBConfig::GetConfig(db);
430250 vector<CompressionType> forced_methods(checkpoint_states.size(), CompressionType::COMPRESSION_AUTO);
430251
430252 auto compression_type = checkpoint_info.GetCompressionType();
430253 for (idx_t i = 0; i < checkpoint_states.size(); i++) {
430254 auto &functions = compression_functions[i];
430255 if (compression_type != CompressionType::COMPRESSION_AUTO) {
430256 forced_methods[i] = ForceCompression(storage_manager, functions, compression_type);
430257 }
430258 if (compression_type == CompressionType::COMPRESSION_AUTO) {
430259 auto force_compression = Settings::Get<ForceCompressionSetting>(config);
430260 if (force_compression != CompressionType::COMPRESSION_AUTO) {
430261 forced_methods[i] = ForceCompression(storage_manager, functions, force_compression);
430262 }
430263 }
430264 }
430265
430266 InitAnalyze();
430267
430268 // scan over all the segments and run the analyze step
430269 ScanSegments([&](Vector &scan_vector, idx_t count) {
430270 for (idx_t i = 0; i < checkpoint_states.size(); i++) {
430271 auto &functions = compression_functions[i];
430272 auto &states = analyze_states[i];
430273 for (idx_t j = 0; j < functions.size(); j++) {
430274 auto &state = states[j];
430275 auto &func = functions[j];
430276
430277 if (!state) {
430278 continue;
430279 }
430280 if (!func->analyze(*state, scan_vector, count)) {
430281 state = nullptr;
430282 func = nullptr;
430283 }
430284 }
430285 }
430286 });
430287
430288 vector<CheckpointAnalyzeResult> result;
430289 result.resize(checkpoint_states.size());
430290
430291 for (idx_t i = 0; i < checkpoint_states.size(); i++) {
430292 auto &functions = compression_functions[i];
430293 auto &states = analyze_states[i];
430294 auto &forced_method = forced_methods[i];
430295
430296 unique_ptr<AnalyzeState> chosen_state;
430297 idx_t best_score = NumericLimits<idx_t>::Maximum();
430298 idx_t compression_idx = DConstants::INVALID_INDEX;
430299
430300 D_ASSERT(functions.size() == states.size());
430301 for (idx_t j = 0; j < functions.size(); j++) {
430302 auto &function = functions[j];
430303 auto &state = states[j];
430304
430305 if (!state) {
430306 continue;
430307 }
430308
430310 bool forced_method_found = function->type == forced_method;
430311 // now that we have passed over all the data, we need to figure out the best method
430312 // we do this using the final_analyze method
430313 auto score = function->final_analyze(*state);
430314
430316 if (score == DConstants::INVALID_INDEX) {
430317 continue;
430318 }
430319
430320 if (score < best_score || forced_method_found) {
430321 compression_idx = j;
430322 best_score = score;
430323 chosen_state = std::move(state);
430324 }
430326 if (forced_method_found) {
430327 break;
430328 }
430329 }
430330
430331 auto &checkpoint_state = checkpoint_states[i];
430332 auto &col_data = checkpoint_state.get().GetResultColumn();
430333 if (!chosen_state) {
430334 throw FatalException("No suitable compression/storage method found to store column of type %s",
430335 col_data.type.ToString());
430336 }
430337 D_ASSERT(compression_idx != DConstants::INVALID_INDEX);
430338
430339 auto &best_function = *functions[compression_idx];
430340 DUCKDB_LOG_TRACE(db, "ColumnDataCheckpointer FinalAnalyze(%s) result for %s.%s.%d(%s): %d",
430341 EnumUtil::ToString(best_function.type), col_data.info.GetSchemaName(),
430342 col_data.info.GetTableName(), col_data.column_index, col_data.type.ToString(), best_score);
430343 result[i] = CheckpointAnalyzeResult(std::move(chosen_state), best_function);
430344 }
430345 return result;
430346}
vector< vector< unique_ptr< AnalyzeState > > > analyze_states
For every column data that is being checkpointed, the analyze state of functions being tried.
Definition duckdb.cpp:276658
static constexpr const idx_t INVALID_INDEX
The value used to signify an invalid index entry.
Definition duckdb.hpp:1117
Here is the call graph for this function:
Here is the caller graph for this function:

◆ WriteToDisk()

void duckdb::ColumnDataCheckpointer::WriteToDisk ( )
private
430385 { // Analyze the candidate functions to select one of them to use for
430386 // compression
430387 auto analyze_result = DetectBestCompressionMethod();
430388 if (ValidityCoveredByBasedata(analyze_result)) {
430389 D_ASSERT(analyze_result.size() == 2);
430390 auto &validity = analyze_result[1];
430391 auto &db = storage_manager.GetDatabase();
430392 auto &config = DBConfig::GetConfig(db);
430393 // Override the function to the COMPRESSION_EMPTY
430394 // turning the compression+final compress steps into a no-op, saving a single empty segment
430395 validity.function = config.GetCompressionFunction(CompressionType::COMPRESSION_EMPTY, PhysicalType::BIT).get();
430396 }
430397
430398 // Initialize the compression for the selected function
430399 D_ASSERT(analyze_result.size() == checkpoint_states.size());
430400 vector<ColumnDataCheckpointData> checkpoint_data(checkpoint_states.size());
430401 vector<unique_ptr<CompressionState>> compression_states(checkpoint_states.size());
430402 for (idx_t i = 0; i < analyze_result.size(); i++) {
430403 auto &analyze_state = analyze_result[i].analyze_state;
430404 auto &function = analyze_result[i].function;
430405
430406 auto &checkpoint_state = checkpoint_states[i];
430407 auto &col_data = checkpoint_state.get().GetResultColumn();
430408
430409 checkpoint_data[i] =
430410 ColumnDataCheckpointData(checkpoint_state, col_data, col_data.GetDatabase(), row_group, storage_manager);
430411 compression_states[i] = function->init_compression(checkpoint_data[i], std::move(analyze_state));
430412 }
430413
430414 // Scan over the existing segment + changes and compress the data
430415 ScanSegments([&](Vector &scan_vector, idx_t count) {
430416 for (idx_t i = 0; i < checkpoint_states.size(); i++) {
430417 auto &function = analyze_result[i].function;
430418 auto &compression_state = compression_states[i];
430419 function->compress(*compression_state, scan_vector, count);
430420 }
430421 });
430422
430423 // Finalize the compression
430424 for (idx_t i = 0; i < checkpoint_states.size(); i++) {
430425 auto &function = analyze_result[i].function;
430426 auto &compression_state = compression_states[i];
430427 function->compress_finalize(*compression_state);
430428 }
430429
430430 // after we finish checkpointing we can drop this segment
430431 DropSegments();
430432}
vector< CheckpointAnalyzeResult > DetectBestCompressionMethod()
Definition duckdb.cpp:430246
@ BIT
Boolean as 1 bit, LSB bit-packed ordering.
Definition duckdb.hpp:2852

◆ WritePersistentSegments()

void duckdb::ColumnDataCheckpointer::WritePersistentSegments ( ColumnCheckpointState & state)
private
430438 {
430439 // all segments are persistent and there are no updates
430440 // we only need to write the metadata
430441
430442 auto &col_data = state.original_column;
430443
430444 optional_idx error_segment_start;
430445 idx_t current_row = 0;
430446 for (auto &segment_node : col_data.data.SegmentNodes()) {
430447 auto &segment = segment_node.GetNode();
430448 auto segment_start = segment_node.GetRowStart();
430449 if (segment_start != current_row) {
430450 error_segment_start = segment_start;
430451 break;
430452 }
430453 auto pointer = segment.GetDataPointer(current_row);
430454 current_row += segment.count;
430455
430456 // merge the persistent stats into the global column stats
430457 state.global_stats->Merge(segment.stats.statistics);
430458 state.data_pointers.push_back(std::move(pointer));
430459 }
430460 if (error_segment_start.IsValid()) {
430461 string extra_info;
430462 for (auto &s : col_data.data.SegmentNodes()) {
430463 extra_info += "\n";
430464 extra_info += StringUtil::Format("Start %d, count %d", s.GetRowStart(), s.GetNode().count.load());
430465 }
430466 throw InternalException(
430467 "Failure in RowGroup::Checkpoint - column data pointer is unaligned with row group "
430468 "start\nRow group start: %d\nRow group count %d\nCurrent row: %d\nSegment start: %d\nColumn index: "
430469 "%d\nColumn type: %s\nRoot type: %s\nTable: %s.%s\nAll segments:%s",
430470 row_group.count.load(), current_row, error_segment_start.GetIndex(), col_data.column_index, col_data.type,
430471 col_data.type, col_data.info.GetSchemaName(), col_data.info.GetTableName(), extra_info);
430472 }
430473}
static string Format(const string fmt_str, ARGS... params)
Format a string using printf semantics.
Definition duckdb.hpp:4002

◆ HasChanges()

bool duckdb::ColumnDataCheckpointer::HasChanges ( ColumnData & col_data)
private
430434 {
430435 return col_data.HasAnyChanges();
430436}

◆ InitAnalyze()

void duckdb::ColumnDataCheckpointer::InitAnalyze ( )
private
430228 {
430229 analyze_states.resize(checkpoint_states.size());
430230 for (idx_t i = 0; i < checkpoint_states.size(); i++) {
430231 auto &functions = compression_functions[i];
430232 auto &states = analyze_states[i];
430233 auto &checkpoint_state = checkpoint_states[i];
430234 auto &coldata = checkpoint_state.get().GetResultColumn();
430235 states.resize(functions.size());
430236 for (idx_t j = 0; j < functions.size(); j++) {
430237 auto &func = functions[j];
430238 if (!func) {
430239 continue;
430240 }
430241 states[j] = func->init_analyze(coldata, coldata.type.InternalType());
430242 }
430243 }
430244}

◆ DropSegments()

void duckdb::ColumnDataCheckpointer::DropSegments ( )
private
430359 {
430360 // first we check the current segments
430361 // if there are any persistent segments, we will mark their old block ids as modified
430362 // since the segments will be rewritten their old on disk data is no longer required
430363
430364 for (idx_t i = 0; i < checkpoint_states.size(); i++) {
430365 auto &state = checkpoint_states[i];
430366 auto &col_data = state.get().original_column;
430367
430368 // Drop the segments, as we'll be replacing them with new ones, because there are changes
430369 CheckpointBlockIdDropper dropper(storage_manager.GetBlockManager());
430370 for (auto &segment : col_data.data.Segments()) {
430371 segment.VisitBlockIds(dropper);
430372 }
430373 }
430374}

◆ ValidityCoveredByBasedata()

bool duckdb::ColumnDataCheckpointer::ValidityCoveredByBasedata ( vector< CheckpointAnalyzeResult > & result)
private
430376 {
430377 if (result.size() != 2) {
430378 return false;
430379 }
430380 auto &base = result[0];
430381 D_ASSERT(base.function);
430382 return base.function->validity == CompressionValidity::NO_VALIDITY_REQUIRED;
430383}

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