193219 {
193220 idx_t current_idx;
193221 vector<BatchMergeTask> to_be_scheduled_tasks;
193222
193223 BatchMergeTask current_task(next_start);
193224 for (current_idx = current_task.start_index; current_idx < collections.size(); current_idx++) {
193225 auto &entry = collections[current_idx];
193226 if (entry.batch_idx > min_batch_index) {
193227
193228
193229 if (ReadyToMerge(current_task.total_count)) {
193230 current_task.end_index = current_idx;
193231 to_be_scheduled_tasks.push_back(current_task);
193232 }
193233 break;
193234 }
193235 if (entry.type == RowGroupBatchType::FLUSHED) {
193236
193237 if (current_task.total_count > 0) {
193238 current_task.end_index = current_idx;
193239 to_be_scheduled_tasks.push_back(current_task);
193240 }
193241 current_task.start_index = current_idx + 1;
193242 if (current_task.start_index > next_start) {
193243
193244 next_start = current_task.start_index;
193245 }
193246 current_task.total_count = 0;
193247 continue;
193248 }
193249
193250 current_task.total_count += entry.total_rows;
193251 if (ReadyToMerge(current_task.total_count)) {
193252
193253 current_task.end_index = current_idx + 1;
193254 to_be_scheduled_tasks.push_back(current_task);
193255 current_task.start_index = current_idx + 1;
193256 current_task.total_count = 0;
193257 }
193258 }
193259
193260 if (to_be_scheduled_tasks.empty()) {
193261 return;
193262 }
193263 for (auto &scheduled_task : to_be_scheduled_tasks) {
193264 D_ASSERT(scheduled_task.total_count > 0);
193265 D_ASSERT(current_idx > scheduled_task.start_index);
193266 auto merged_batch_index = collections[scheduled_task.start_index].batch_idx;
193267 vector<RowGroupBatchEntry> merge_collections;
193268 for (
idx_t idx = scheduled_task.start_index; idx < scheduled_task.end_index; idx++) {
193269 auto &entry = collections[idx];
193270 if (!entry.collection_index.IsValid() || entry.type == RowGroupBatchType::FLUSHED) {
193271 throw InternalException("Adding a row group collection that should not be flushed");
193272 }
193273 auto &collection = table.GetStorage().GetOptimisticCollection(context, entry.collection_index);
193274 RowGroupBatchEntry added_entry(collection, collections[scheduled_task.start_index].batch_idx,
193275 entry.collection_index, RowGroupBatchType::FLUSHED);
193276 added_entry.unflushed_memory = entry.unflushed_memory;
193277 merge_collections.push_back(added_entry);
193278 entry.total_rows = scheduled_task.total_count;
193279 entry.type = RowGroupBatchType::FLUSHED;
193281 }
193282 task_manager.AddTask(make_uniq<MergeCollectionTask>(std::move(merge_collections), merged_batch_index));
193283 }
193284
193285 for (
idx_t i = to_be_scheduled_tasks.size(); i > 0; i--) {
193286 auto &scheduled_task = to_be_scheduled_tasks[i - 1];
193287 if (scheduled_task.start_index + 1 < scheduled_task.end_index) {
193288
193289 collections.erase(collections.begin() + NumericCast<int64_t>(scheduled_task.start_index) + 1,
193290 collections.begin() + NumericCast<int64_t>(scheduled_task.end_index));
193291 }
193292 }
193293}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
static constexpr const idx_t INVALID_INDEX
The value used to signify an invalid index entry.
Definition duckdb.hpp:1117