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

Code before:

        if ((compareValues(selected_lower_bound.value, part_upper_bound, false) > 0) ||
            (compareValues(selected_upper_bound.value, part_lower_bound, false) < 0))
        {
            continue; /// early exit, intersection infeasible in this part
        }

        /// The selected lower bound could be 'fqrst' and granule PK ranges could be -
        ///      abcde,...,fcedr, ffagj, fqrst, fqrst, fqrst, ghyrw ....
        /// candidates_start needs to point to granule starting at "ffagj"
        auto candidates_start = index_access.findRightmostMarkLessThanValueInRange(part_index, selected_lower_bound.value, MarkRange{part_ranges_after_pk_analysis_begin, part_ranges_after_pk_analysis_end + 1}, false);
        if (!candidates_start)
            candidates_start = 0;

        auto candidates_end = index_access.findLeftmostMarkGreaterThanValueInRange(part_index, selected_upper_bound.value, MarkRange{part_ranges_after_pk_analysis_begin, part_ranges_after_pk_analysis_end + 1}, false);
        if (!candidates_end)
            candidates_end = part_ranges_after_pk_analysis_end;
        if (candidates_end != 0) /// Come back by 1 because we are now 1 past the upper bound or at the final mark
            candidates_end = candidates_end.value() - 1;

        for (const auto & part_candidate_range : ranges_in_data_parts[part_index].ranges_snapshot_after_pk_analysis.value())
        {
            for (auto range_begin = part_candidate_range.begin; range_begin < part_candidate_range.end; range_begin++)
            {
                if (range_begin < candidates_start || range_begin > candidates_end)
                    continue;
                if (std::binary_search(part_selected_ranges[part_index].begin(), part_selected_ranges[part_index].end(), range_begin))
                    continue;
                MarkRange rejected_range(range_begin, range_begin + 1);
                rejected_ranges.push_back(
                    {index_access.getValue(part_index, rejected_range.begin), false, rejected_range, part_index,
                    PartsRangesIterator::EventType::RangeStart, false});
            }
        }
    }

    ::sort(rejected_ranges.begin(), rejected_ranges.end());

    std::vector<PartsRangesIterator>::iterator selected_ranges_iter = selected_ranges.begin();
    std::vector<PartsRangesIterator>::iterator rejected_ranges_iter = rejected_ranges.begin();
    size_t more_ranges_added = 0;

    while (selected_ranges_iter != selected_ranges.end() && rejected_ranges_iter != rejected_ranges.end())
    {
        auto selected_range_start = selected_ranges_iter->value;
        auto selected_range_end = index_access.getValue(selected_ranges_iter->part_index, selected_ranges_iter->range.end);
        auto rejected_range_start = rejected_ranges_iter->value;

        int result1 = compareValues(rejected_range_start, selected_range_start, false);
        int result2 = compareValues(rejected_range_start, selected_range_end, false);


Review comment (suggestion):
It is not a big deal, but just for the sake of premature optimisation. 

```suggestion
            for (auto range_begin = max(part_candidate_range.begin, candidates_start); range_begin < max(part_candidate_range.end, candidates_end+1); range_begin++)
            {
```

Code after:
        const auto & part_lower_bound = index_access.getValue(part_index, part_ranges_after_pk_analysis_begin);
        const auto & part_upper_bound = index_access.getValue(part_index, part_ranges_after_pk_analysis_end);

        if ((compareValues(selected_lower_bound.value, part_upper_bound, false) > 0) ||
            (compareValues(selected_upper_bound.value, part_lower_bound, false) < 0))
        {
            continue; /// early exit, intersection infeasible in this part
        }

        /// The selected lower bound could be 'fqrst' and granule PK ranges could be -
        ///      abcde,...,fcedr, ffagj, fqrst, fqrst, fqrst, ghyrw ....
        /// candidates_start needs to point to granule starting at "ffagj"
        auto candidates_start = index_access.findRightmostMarkLessThanValueInRange(part_index, selected_lower_bound.value, MarkRange{part_ranges_after_pk_analysis_begin, part_ranges_after_pk_analysis_end + 1}, false);
        if (!candidates_start)
            candidates_start = 0;

        auto candidates_end = index_access.findLeftmostMarkGreaterThanValueInRange(part_index, selected_upper_bound.value, MarkRange{part_ranges_after_pk_analysis_begin, part_ranges_after_pk_analysis_end + 1}, false);
        if (!candidates_end)
            candidates_end = part_ranges_after_pk_analysis_end;
        if (candidates_end != 0) /// Come back by 1 because we are now 1 past the upper bound or at the final mark
            candidates_end = candidates_end.value() - 1;

        for (const auto & part_candidate_range : ranges)
        {
            for (auto range_begin = std::max(part_candidate_range.begin, *candidates_start); range_begin < std::min(part_candidate_range.end, *candidates_end + 1); range_begin++)
            {
                if (std::binary_search(part_selected_ranges[part_index].begin(), part_selected_ranges[part_index].end(), range_begin))
                    continue;

                MarkRange rejected_range(range_begin, range_begin + 1);
                rejected_ranges.push_back(
                    {index_access.getValue(part_index, rejected_range.begin), false, rejected_range, part_index,
                    PartsRangesIterator::EventType::RangeStart, false});
            }
        }
    }

    ::sort(rejected_ranges.begin(), rejected_ranges.end());

    std::vector<PartsRangesIterator>::iterator selected_ranges_iter = selected_ranges.begin();
    std::vector<PartsRangesIterator>::iterator rejected_ranges_iter = rejected_ranges.begin();
    size_t more_ranges_added = 0;

    while (selected_ranges_iter != selected_ranges.end() && rejected_ranges_iter != rejected_ranges.end())
    {
        auto selected_range_start = selected_ranges_iter->value;
        auto selected_range_end = index_access.getValue(selected_ranges_iter->part_index, selected_ranges_iter->range.end);
        auto rejected_range_start = rejected_ranges_iter->value;

        int result1 = compareValues(rejected_range_start, selected_range_start, false);
        int result2 = compareValues(rejected_range_start, selected_range_end, false);