Repository: duckdb/duckdb
File: src/common/sort/sorted_run_merger.cpp (C++)

Code before:
	const SortedRunMerger &merger;
	const idx_t num_runs;
	const idx_t num_partitions;

	const BlockIteratorStateType iterator_state_type;
	const SortKeyType sort_key_type;

	idx_t next_partition_idx;
	vector<unique_ptr<SortedRunMergePartition>> partitions;
	atomic<idx_t> total_scanned;

	mutex destroy_lock;
	idx_t destroy_partition_idx;

	mutex materialized_partition_lock;
	vector<unique_ptr<SortedRun>> materialized_partitions;
};

//===--------------------------------------------------------------------===//
// Local State Implementation
//===--------------------------------------------------------------------===//
SortedRunMergerLocalState::SortedRunMergerLocalState(SortedRunMergerGlobalState &gstate)
    : iterator_state_type(gstate.iterator_state_type), sort_key_type(gstate.sort_key_type),
      task(SortedRunMergerTask::FINISHED), run_boundaries(gstate.num_runs),
      merged_partition_count(DConstants::INVALID_INDEX), merged_partition_index(DConstants::INVALID_INDEX),
      sorted_run_scan_state(gstate.context, gstate.merger.sort), sort_key_pointers(LogicalType::POINTER) {
	for (const auto &run : gstate.merger.sorted_runs) {
		auto &key_data = *run->key_data;
		switch (iterator_state_type) {
		case BlockIteratorStateType::IN_MEMORY:
			in_memory_states.push_back(BlockIteratorState<BlockIteratorStateType::IN_MEMORY>(key_data));
			break;
		case BlockIteratorStateType::EXTERNAL:
			external_states.push_back(
			    BlockIteratorState<BlockIteratorStateType::EXTERNAL>(key_data, run->payload_data.get()));
			break;
		default:
			throw NotImplementedException("SortedRunMergerLocalState::SortedRunMergerLocalState for %s",
			                              EnumUtil::ToString(iterator_state_type));
		}
	}
}

bool SortedRunMergerLocalState::TaskFinished() const {
	switch (task) {
	case SortedRunMergerTask::COMPUTE_BOUNDARIES:
	case SortedRunMergerTask::ACQUIRE_BOUNDARIES:
	case SortedRunMergerTask::MERGE_PARTITION:
	case SortedRunMergerTask::SCAN_PARTITION:
		D_ASSERT(partition_idx.IsValid());
		return false;

Review comment (bug):
```sh
In file included from /home/vscode/duckdb/build/debug/src/common/sort/ub_duckdb_sort.cpp:8:
/home/vscode/duckdb/src/common/sort/sorted_run_merger.cpp: In constructor 'duckdb::SortedRunMergerLocalState::SortedRunMergerLocalState(duckdb::SortedRunMergerGlobalState&)':
/home/vscode/duckdb/src/common/sort/sorted_run_merger.cpp:162:28: warning: 'duckdb::SortedRunMergerLocalState::sorted_run_scan_state' will be initialized after [-Wreorder]
  162 |         SortedRunScanState sorted_run_scan_state;
      |                            ^~~~~~~~~~~~~~~~~~~~~
/home/vscode/duckdb/src/common/sort/sorted_run_merger.cpp:161:16: warning:   'duckdb::Vector duckdb::SortedRunMergerLocalState::sort_key_pointers' [-Wreorder]
  161 |         Vector sort_key_pointers;
      |                ^~~~~~~~~~~~~~~~~
/home/vscode/duckdb/src/common/sort/sorted_run_merger.cpp:296:1: warning:   when initialized here [-Wreorder]
  296 | SortedRunMergerLocalState::SortedRunMergerLocalState(SortedRunMergerGlobalState &gstate)
      | ^~~~~~~~~~~~~~~~~~~~~~~~~
```

Code after:
	const SortedRunMerger &merger;
	const idx_t num_runs;
	const idx_t num_partitions;

	const BlockIteratorStateType iterator_state_type;
	const SortKeyType sort_key_type;

	idx_t next_partition_idx;
	vector<unique_ptr<SortedRunMergePartition>> partitions;
	atomic<idx_t> total_scanned;

	mutex destroy_lock;
	idx_t destroy_partition_idx;

	mutex materialized_partition_lock;
	vector<unique_ptr<SortedRun>> materialized_partitions;
};

//===--------------------------------------------------------------------===//
// Local State Implementation
//===--------------------------------------------------------------------===//
SortedRunMergerLocalState::SortedRunMergerLocalState(SortedRunMergerGlobalState &gstate)
    : iterator_state_type(gstate.iterator_state_type), sort_key_type(gstate.sort_key_type),
      task(SortedRunMergerTask::FINISHED), run_boundaries(gstate.num_runs),
      merged_partition_count(DConstants::INVALID_INDEX), merged_partition_index(DConstants::INVALID_INDEX),
      sort_key_pointers(LogicalType::POINTER), sorted_run_scan_state(gstate.context, gstate.merger.sort) {
	for (const auto &run : gstate.merger.sorted_runs) {
		auto &key_data = *run->key_data;
		switch (iterator_state_type) {
		case BlockIteratorStateType::IN_MEMORY:
			in_memory_states.push_back(BlockIteratorState<BlockIteratorStateType::IN_MEMORY>(key_data));
			break;
		case BlockIteratorStateType::EXTERNAL:
			external_states.push_back(
			    BlockIteratorState<BlockIteratorStateType::EXTERNAL>(key_data, run->payload_data.get()));
			break;
		default:
			throw NotImplementedException("SortedRunMergerLocalState::SortedRunMergerLocalState for %s",
			                              EnumUtil::ToString(iterator_state_type));
		}
	}
}

bool SortedRunMergerLocalState::TaskFinished() const {
	switch (task) {
	case SortedRunMergerTask::COMPUTE_BOUNDARIES:
	case SortedRunMergerTask::ACQUIRE_BOUNDARIES:
	case SortedRunMergerTask::MERGE_PARTITION:
	case SortedRunMergerTask::SCAN_PARTITION:
		D_ASSERT(partition_idx.IsValid());
		return false;