435658 {
435659 auto &collection = checkpoint_state.collection;
435660 const idx_t row_group_size = collection.GetRowGroupSize();
435661 auto &types = collection.GetTypes();
435662
435663 vector<unique_ptr<RowGroup>> new_row_groups;
435664 vector<idx_t> append_counts;
435665 idx_t row_group_rows = merge_rows;
435666 for (
idx_t target_idx = 0; target_idx < target_count; target_idx++) {
435667 idx_t current_row_group_rows = MinValue<idx_t>(row_group_rows, row_group_size);
435668 auto new_row_group = make_uniq<RowGroup>(collection, current_row_group_rows);
435669 new_row_group->InitializeEmpty(types, ColumnDataType::MAIN_TABLE);
435670 new_row_groups.push_back(std::move(new_row_group));
435671 append_counts.push_back(0);
435672
435673 row_group_rows -= current_row_group_rows;
435674 }
435675
435676 DataChunk scan_chunk;
435677 scan_chunk.
Initialize(Allocator::DefaultAllocator(), types);
435678
435679 vector<StorageIndex> column_ids;
435680 for (
idx_t c = 0; c < types.size(); c++) {
435681 column_ids.emplace_back(c);
435682 }
435683
435684 idx_t current_append_idx = 0;
435685
435686
435687 TableAppendState append_state;
435688 new_row_groups[current_append_idx]->InitializeAppend(append_state.row_group_append_state);
435689
435690 TableScanState scan_state;
435691 scan_state.Initialize(column_ids);
435692 scan_state.table_state.Initialize(QueryContext(), types);
435693 scan_state.table_state.max_row =
idx_t(-1);
435694 idx_t merged_groups = 0;
435695 idx_t total_row_groups = vacuum_state.row_group_counts.size();
435696 optional_idx row_start;
435697 for (
idx_t c_idx = segment_idx; merged_groups < merge_count && c_idx < total_row_groups; c_idx++) {
435698 if (vacuum_state.row_group_counts[c_idx] == 0) {
435699 continue;
435700 }
435701 merged_groups++;
435702
435703 auto current_segment = checkpoint_state.GetSegment(c_idx);
435704 if (!row_start.IsValid()) {
435705 row_start = current_segment->GetRowStart();
435706 }
435707 auto ¤t_row_group = current_segment->GetNode();
435708
435709 current_row_group.InitializeScan(scan_state.table_state, *current_segment);
435710 while (true) {
435711 scan_chunk.
Reset();
435712
435714 if (scan_chunk.size() == 0) {
435715 break;
435716 }
435718 idx_t remaining = scan_chunk.size();
435719 while (remaining > 0) {
435720 idx_t append_count = MinValue<idx_t>(remaining, row_group_size - append_counts[current_append_idx]);
435721 new_row_groups[current_append_idx]->Append(append_state.row_group_append_state, scan_chunk,
435722 append_count);
435723 append_counts[current_append_idx] += append_count;
435724 remaining -= append_count;
435725 const bool row_group_full = append_counts[current_append_idx] == row_group_size;
435726 const bool last_row_group = current_append_idx + 1 >= new_row_groups.size();
435727 if (remaining > 0 || (row_group_full && !last_row_group)) {
435728
435729 current_append_idx++;
435730 new_row_groups[current_append_idx]->InitializeAppend(append_state.row_group_append_state);
435731
435732 scan_chunk.Slice(append_count, remaining);
435733 }
435734 }
435735 }
435736
435737 current_row_group.CommitDrop();
435738 checkpoint_state.DropSegment(c_idx);
435739 }
435740 idx_t total_append_count = 0;
435741 for (
idx_t target_idx = 0; target_idx < target_count; target_idx++) {
435742 auto &row_group = new_row_groups[target_idx];
435743 row_group->Verify();
435744
435745
435746 checkpoint_state.SetSegment(row_start.GetIndex() + total_append_count, segment_idx + target_idx,
435747 std::move(row_group));
435748 total_append_count += append_counts[target_idx];
435749 }
435750 if (total_append_count != merge_rows) {
435751 throw InternalException(
435752 "Mismatch in row group count %d vs verify count %d in RowGroupCollection::Checkpoint", merge_rows,
435753 total_append_count);
435754 }
435755
435756 for (
idx_t i = 0; i < target_count; i++) {
435757 auto checkpoint_task = collection.GetCheckpointTask(checkpoint_state, segment_idx + i);
435758 if (!checkpoint_task) {
435759 throw InternalException("Failed to get checkpoint task for row group we just merged");
435760 }
435761 checkpoint_task->ExecuteTask();
435762 }
435763 }
DUCKDB_API void Reset()
Definition duckdb.cpp:116840
DUCKDB_API void Initialize(ClientContext &context, const vector< LogicalType > &types, idx_t capacity=STANDARD_VECTOR_SIZE)
Definition duckdb.cpp:116791
DUCKDB_API void Flatten()
Turn all the vectors from the chunk into flat vectors.
Definition duckdb.cpp:116989
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
@ TABLE_SCAN_COMMITTED_ROWS
Scan the latest committed rows.
Definition duckdb.hpp:22678