Repository: duckdb/duckdb
File: src/storage/table/scan_state.cpp (C++)

Code before:
		last_row_group_stats = row_group_stats;
		qualifying_tuples = seen_tuples;
	} else {
		if (!previous_order_by.IsNull() && order_by_value != previous_order_by) {
			// Only if the opposite boundaries are inequal, we can guarantee to have >= 1 distinct qualifying rows.
			// We need distinctness as there may be secondary orders that qualify rows in later row groups.
			qualifying_tuples++;
		}
	}
	if (qualifying_tuples >= row_limit.GetIndex()) {
		return true;
	}

	seen_tuples += current_row_group.get().count;
	ordered_row_groups.emplace_back(current_row_group);
	return false;
}

optional_ptr<RowGroup> RowGroupReorderer::GetRootSegment(RowGroupSegmentTree &row_groups) {
	if (initialized) {
		return ordered_row_groups.empty() ? nullptr : ordered_row_groups[0];
	}

	initialized = true;

	multimap<Value, pair<unique_ptr<BaseStatistics>, reference<RowGroup>>> row_group_map;
	for (auto &row_group : row_groups.Segments()) {
		auto stats = row_group.GetStatistics(column_idx);
		Value comparison_value = RetrieveStat(*stats, order_by, column_type);
		row_group_map.emplace(std::piecewise_construct, std::forward_as_tuple(comparison_value),
		                      std::forward_as_tuple(std::move(stats), row_group));
	}

	if (row_group_map.empty()) {
		return nullptr;
	}

	if (row_limit.IsValid()) {
		SetRowGroupVectorWithLimit(row_group_map);
	} else {
		ordered_row_groups.reserve(row_group_map.size());
		if (order_type == RowGroupOrderType::ASC) {
			for (auto &row_group : row_group_map) {
				ordered_row_groups.emplace_back(row_group.second.second);
			}
		} else {
			for (auto it = row_group_map.rbegin(); it != row_group_map.rend(); ++it) {
				ordered_row_groups.emplace_back(it->second.second);
			}
		}
	}

Review comment (performance):
We prefer not to use `pair` as it is optimized for writing code and not for reading code (`entry.stats / entry.row_group` reads a lot better than `entry.first` and `entry.second`). Could you turn this into a class?

Code after:
void SetRowGroupVectorWithLimit(const multimap<Value, RowGroupMapEntry> &row_group_map, const optional_idx row_limit,
                                const RowGroupOrderType order_type, const OrderByColumnType column_type,
                                vector<reference<SegmentNode<RowGroup>>> &ordered_row_groups) {
	D_ASSERT(row_limit.IsValid());

	const auto stat_type = order_type == RowGroupOrderType::ASC ? OrderByStatistics::MIN : OrderByStatistics::MAX;
	ordered_row_groups.reserve(row_group_map.size());

	Value previous_key;
	if (order_type == RowGroupOrderType::ASC) {
		auto it = row_group_map.begin();
		auto end = row_group_map.end();
		AddRowGroups(it, end, ordered_row_groups, row_limit.GetIndex(), column_type, stat_type);
	} else {
		auto it = row_group_map.rbegin();
		auto end = row_group_map.rend();
		AddRowGroups(it, end, ordered_row_groups, row_limit.GetIndex(), column_type, stat_type);
	}
}

optional_ptr<SegmentNode<RowGroup>> RowGroupReorderer::GetRootSegment(RowGroupSegmentTree &row_groups) {
	if (initialized) {
		if (ordered_row_groups.empty()) {
			return nullptr;
		}
		return ordered_row_groups[0].get();
	}

	initialized = true;

	multimap<Value, RowGroupMapEntry> row_group_map;
	for (auto &row_group : row_groups.SegmentNodes()) {
		auto stats = row_group.node->GetStatistics(column_idx);
		Value comparison_value = RetrieveStat(*stats, order_by, column_type);
		auto entry = RowGroupMapEntry {row_group, std::move(stats)};
		row_group_map.emplace(comparison_value, std::move(entry));
	}

	if (row_group_map.empty()) {
		return nullptr;
	}

	if (row_limit.IsValid()) {
		SetRowGroupVectorWithLimit(row_group_map, row_limit, order_type, column_type, ordered_row_groups);
	} else {
		ordered_row_groups.reserve(row_group_map.size());
		if (order_type == RowGroupOrderType::ASC) {
			for (auto &row_group : row_group_map) {
				ordered_row_groups.emplace_back(row_group.second.row_group);
			}
		} else {