193162 {
193163 auto &g_state = g_state_p.Cast<BatchInsertGlobalState>();
193164 auto &l_state = l_state_p.Cast<BatchInsertLocalState>();
193165
193166
193167 if (!l_state.optimistic_writer) {
193168 l_state.optimistic_writer = make_uniq<OptimisticDataWriter>(context, g_state.table.GetStorage());
193169 }
193170 auto result_collection_index = g_state.MergeCollections(context, merge_collections, *l_state.optimistic_writer);
193171 merge_collections.clear();
193172
193173 lock_guard<mutex> l(g_state.lock);
193174 auto &result_collection = g_state.table.GetStorage().GetOptimisticCollection(context, result_collection_index);
193175 RowGroupBatchEntry new_entry(result_collection, merged_batch_index, result_collection_index,
193176 RowGroupBatchType::FLUSHED);
193177
193178
193179 auto it = std::lower_bound(
193180 g_state.collections.begin(), g_state.collections.end(), new_entry,
193181 [&](const RowGroupBatchEntry &a, const RowGroupBatchEntry &b) { return a.batch_idx < b.batch_idx; });
193182 if (it->batch_idx != merged_batch_index) {
193183 throw InternalException("Merged batch index was no longer present in collection");
193184 }
193185 it->collection_index = new_entry.collection_index;
193186 }