Repository: duckdb/duckdb
File: src/execution/operator/persistent/physical_batch_insert.cpp (C++)

Code before:
		if (Empty()) {
			return PhysicalIndex(DConstants::INVALID_INDEX);
		}

		auto result_collection_index = collection_indexes[0];
		auto &optimistic_collection = data_table.GetOptimisticCollection(context, result_collection_index);
		auto &result_collection = *optimistic_collection.collection;

		if (collection_indexes.size() > 1) {
			// Merge all collections into one result collection.
			auto &types = result_collection.GetTypes();
			TableAppendState append_state;
			result_collection.InitializeAppend(append_state);

			DataChunk scan_chunk;
			scan_chunk.Initialize(context, types);

			vector<StorageIndex> column_ids;
			for (idx_t i = 0; i < types.size(); i++) {
				column_ids.emplace_back(i);
			}
			for (idx_t i = 1; i < collection_indexes.size(); i++) {
				auto &collection = data_table.GetOptimisticCollection(context, collection_indexes[i]);
				TableScanState scan_state;
				scan_state.Initialize(column_ids);
				collection.collection->InitializeScan(QueryContext(context), scan_state.local_state, column_ids,
				                                      nullptr);

				while (true) {
					scan_chunk.Reset();
					scan_state.local_state.ScanCommitted(scan_chunk, TableScanType::TABLE_SCAN_COMMITTED_ROWS);
					if (scan_chunk.size() == 0) {
						break;
					}
					auto new_row_group = result_collection.Append(scan_chunk, append_state);
					if (new_row_group) {
						writer.WriteNewRowGroup(optimistic_collection);
					}
				}
				data_table.ResetOptimisticCollection(context, collection_indexes[i]);
			}
			result_collection.FinalizeAppend(TransactionData(0, 0), append_state);
			writer.WriteLastRowGroup(optimistic_collection);
		} else if (batch_type == RowGroupBatchType::NOT_FLUSHED) {
			writer.WriteLastRowGroup(optimistic_collection);
		}

		collection_indexes.clear();
		return result_collection_index;
	}
};

Review comment (suggestion):
We should be able to just do `context` here because of the implicit conversion

Code after:
		if (Empty()) {
			return PhysicalIndex(DConstants::INVALID_INDEX);
		}

		auto result_collection_index = collection_indexes[0];
		auto &optimistic_collection = data_table.GetOptimisticCollection(context, result_collection_index);
		auto &result_collection = *optimistic_collection.collection;

		if (collection_indexes.size() > 1) {
			// Merge all collections into one result collection.
			auto &types = result_collection.GetTypes();
			TableAppendState append_state;
			result_collection.InitializeAppend(append_state);

			DataChunk scan_chunk;
			scan_chunk.Initialize(context, types);

			vector<StorageIndex> column_ids;
			for (idx_t i = 0; i < types.size(); i++) {
				column_ids.emplace_back(i);
			}
			for (idx_t i = 1; i < collection_indexes.size(); i++) {
				auto &collection = data_table.GetOptimisticCollection(context, collection_indexes[i]);
				TableScanState scan_state;
				scan_state.Initialize(column_ids);
				collection.collection->InitializeScan(context, scan_state.local_state, column_ids, nullptr);

				while (true) {
					scan_chunk.Reset();
					scan_state.local_state.ScanCommitted(scan_chunk, TableScanType::TABLE_SCAN_COMMITTED_ROWS);
					if (scan_chunk.size() == 0) {
						break;
					}
					auto new_row_group = result_collection.Append(scan_chunk, append_state);
					if (new_row_group) {
						writer.WriteNewRowGroup(optimistic_collection);
					}
				}
				data_table.ResetOptimisticCollection(context, collection_indexes[i]);
			}
			result_collection.FinalizeAppend(TransactionData(0, 0), append_state);
			writer.WriteLastRowGroup(optimistic_collection);
		} else if (batch_type == RowGroupBatchType::NOT_FLUSHED) {
			writer.WriteLastRowGroup(optimistic_collection);
		}

		collection_indexes.clear();
		return result_collection_index;
	}
};