Repository: ClickHouse/ClickHouse
File: src/Processors/QueryPlan/ReadFromMergeTree.cpp (C++)

Code before:

                auto index_description = indexes->key_condition.getDescription();
                result.index_stats.emplace_back(IndexStat{
                    .type = IndexType::PrimaryKey,
                    .condition = index_description.condition,
                    .used_keys = index_description.used_keys,
                    .num_parts_after = received_parts,
                    .num_granules_after = received_granules,
                    .distributed = std::move(distributed_index_stats),
                });
            }

            LOG_DEBUG(log, "Received parts ranges for {} parts via distributed index analysis", analyzed_parts_ranges.size());

            RangesInDataParts result_parts_ranges;
            for (const auto & [part_name, ranges] : analyzed_parts_ranges)
            {
                auto part_range_info = *parts_ranges_map.at(part_name);
                chassert(part_range_info.ranges.size() == 1);
                chassert(part_range_info.exact_ranges.empty());

                part_range_info.ranges = ranges;
                result_parts_ranges.push_back(part_range_info);
            }

            /// Parts should be sorted by part_index_in_query for QCC
            std::sort(result_parts_ranges.begin(), result_parts_ranges.end(),
                [](const auto & a, const auto & b) { return a.part_index_in_query < b.part_index_in_query; });

            result.parts_with_ranges = std::move(result_parts_ranges);
        }

        std::optional<size_t> condition_hash;
        if (reader_settings.use_query_condition_cache && query_info_.filter_actions_dag && !query_info_.isFinal())
        {
            const auto & outputs = query_info_.filter_actions_dag->getOutputs();
            if (outputs.size() == 1 && VirtualColumnUtils::isDeterministic(outputs.front()))
                condition_hash = outputs.front()->getHash();
        }

        /// Fill query condition cache with ranges excluded by index analysis.
        if (condition_hash)
        {
            RangesInDataParts remaining;

            auto it_parts = res_parts.begin();
            auto it_result = result.parts_with_ranges.begin();

            while (it_parts != res_parts.end())
            {
                if (it_result != result.parts_with_ranges.end() && it_parts->part_index_in_query == it_result->part_index_in_query)

Review comment (performance):
```suggestion
            /// Parts should be sorted by part_index_in_query for Query Condition Cache
```

Code after:

                auto index_description = indexes->key_condition.getDescription();
                result.index_stats.emplace_back(IndexStat{
                    .type = IndexType::PrimaryKey,
                    .condition = index_description.condition,
                    .used_keys = index_description.used_keys,
                    .num_parts_after = received_parts,
                    .num_granules_after = received_granules,
                    .distributed = std::move(distributed_index_stats),
                });
            }

            LOG_DEBUG(log, "Received parts ranges for {} parts via distributed index analysis", analyzed_parts_ranges.size());

            RangesInDataParts result_parts_ranges;
            for (const auto & [part_name, ranges] : analyzed_parts_ranges)
            {
                auto part_range_info = *parts_ranges_map.at(part_name);
                chassert(part_range_info.ranges.size() == 1);
                chassert(part_range_info.exact_ranges.empty());

                part_range_info.ranges = ranges;
                result_parts_ranges.push_back(part_range_info);
            }

            /// Parts should be sorted by part_index_in_query for Query Condition Cache
            std::sort(result_parts_ranges.begin(), result_parts_ranges.end(),
                [](const auto & a, const auto & b) { return a.part_index_in_query < b.part_index_in_query; });

            result.parts_with_ranges = std::move(result_parts_ranges);
        }

        std::optional<size_t> condition_hash;
        if (reader_settings.use_query_condition_cache && query_info_.filter_actions_dag && !query_info_.isFinal())
        {
            const auto & outputs = query_info_.filter_actions_dag->getOutputs();
            if (outputs.size() == 1 && VirtualColumnUtils::isDeterministic(outputs.front()))
                condition_hash = outputs.front()->getHash();
        }

        /// Fill query condition cache with ranges excluded by index analysis.
        if (condition_hash)
        {
            RangesInDataParts remaining;

            auto it_parts = res_parts.begin();
            auto it_result = result.parts_with_ranges.begin();

            while (it_parts != res_parts.end())
            {
                if (it_result != result.parts_with_ranges.end() && it_parts->part_index_in_query == it_result->part_index_in_query)