Repository navigation
Do not recursively split always-true ranges in PK analysis - #93813
Conversation
|
cc @nihalzp I'm curious what you think about this one -- it seems almost too obvious to me, so I'm worried I'm missing something. Also, would it compatible with your other PR(s) to sparsify and remove Field from PK analysis? |
|
Thanks for the idea. I will have a careful look tomorrow. |
|
@EmeraldShift Some tests seems to have failed and look related. Maybe inspecting them could reveal some edge case? Yes, they should be compatible. |
When bool masks from hyperrectangles are combined, `BoolMask::operator|` combines `can_be_false` with `&&`, which destroys information about one or the other range possibly being false. With the new PK skipping logic, this caused ClickHouse to read more granules than necessary. We now use `BoolMask::combine`, which correctly combines `can_be_false` with `||`. Thinking more about it, it sounds like we should change `operator|` to behave in a similar way, but I'm less confident in correctness of that.
|
I think there was a latent bug in the usage of |
|
Now all tests pass, and there's a good performance improvement for some indexing tests. @nihalzp Would you mind taking a look?
|
| @@ -3473,7 +3458,7 @@ BoolMask KeyCondition::checkInHyperrectangle( | |||
| current_intersection = current_intersection & BoolMask(intersects, !contains); | |||
| } | |||
|
|
|||
| mask = mask | current_intersection; | |||
| mask = BoolMask::combine(mask, current_intersection); | |||
There was a problem hiding this comment.
Yes, that was incorrect and this is the correct fix. Since space-filling curves are analyzed in relaxed mode, this bug currently will not give incorrect results in master.
| auto result = check_in_range(range, BoolMask()); | ||
| if (!result.can_be_true) | ||
| continue; | ||
|
|
||
| if (range.end == range.begin + 1) | ||
| if (!result.can_be_false || range.end == range.begin + 1) |
There was a problem hiding this comment.
This looks okay. There is no correctness issue here. My only concern is that this may lead to less pruning in some case (I feel it should be quite rare) when we are in relaxed mode (as a result, exact_ranges is nullptr); because in relaxed mode, can_be_false is not reliable anymore and can have false negatives.
I will let @amosbird weigh in here because this part is written by him and may have some insights.
There was a problem hiding this comment.
I have two concerns here:
auto result = check_in_range(range, BoolMask());This introduces extra overhead on the hot path. In some cases we end up evaluating can_be_false for the whole range and then again for sub-ranges after splitting, which can effectively double the effort without guaranteed pruning benefit.
if (!result.can_be_false || range.end == range.begin + 1)As @nihalzp pointed out, in relaxed mode (exact_ranges == nullptr), can_be_false has weak semantics and can easily produce false negatives. Relying on it to stop splitting may noticeably reduce pruning effectiveness. This is not a rare case — relaxed mode is quite common in practice (e.g. time functions, IN / IN SET conditions).
There was a problem hiding this comment.
Yes, relaxed mode is quite common. But to get relaxed mode + false negative, it should be rare because IN manually sets can_be_false=true if the atom is relaxed and IN SET can have can_be_false=false in very specific scenario.
There was a problem hiding this comment.
@amosbird I think this can have some benefit for NOT IN relaxed case. Suppose IN has can_be_true=false, then NOT IN will have can_be_false=false but this is not a false negative. If the atom is relaxed, then if we manually set can_be_false=true in most cases, then I think we can avoid all false negative and still can benefit from the NOT ... versions.
What do you think?
There was a problem hiding this comment.
While looking into this change, I realized that the semantics of BoolMask in relaxed mode are quite hard to reason about, especially once NOT is involved.
Conceptually, what we are trying to represent is a three-state outcome for a range:
all true / all false / undecided. BoolMask approximates this using can_be_true and can_be_false, which works reasonably well in non-relaxed mode, but becomes very confusing in relaxed mode due to over-approximation.
In relaxed mode, the guarantees are asymmetric:
can_be_true == falseis a strong and reliable signal (ALL_FALSE)can_be_false == falseis no longer reliable and may be a false ALL_TRUE
This asymmetry is easy to miss and makes optimizations around splitting, pruning, and NOT inversion difficult to reason about and easy to misuse.
Because of this, I’d like to propose a small refactor: instead of relying on BoolMask semantics in relaxed mode, we could explicitly model the result as a tri-state (ALL_FALSE / UNDECIDED / ALL_TRUE). This would make the code easier to reason about and reduce the risk of subtle pruning regressions.
There was a problem hiding this comment.
I see. I didn't realize there was this much subtlety to the modes of checking the key condition. Pending that refactoring, what do you think about applying this optimization iff:
exact_ranges != nullptr || settings['merge_tree_read_all_true_ranges_in_relaxed_mode']
(modulo the setting name, setting is default false)
There was a problem hiding this comment.
I’m a bit concerned that this setting is too subtle. If it defaults to false, chances are it will almost never be enabled or exercised in practice.
Instead, I think we should aim for a more generally applicable optimization strategy—one that delivers a significant improvement while only introducing negligible or at least well-understood regressions.
Concretely, it might make sense to first evaluate the cost of always attempting the check_in_range(range, BoolMask()) derivation. If the overhead turns out to be negligible, then this PR could already apply the optimization unconditionally in the non-relaxed mode, without gating it behind an extra setting.
There was a problem hiding this comment.
Oh, the way I had written it, it would already unconditionally be applied in non-relaxed mode, but be controlled by a setting only in relaxed mode. The performance tests have already run against the unconditional change, and I see mainly speedup. But its possible that those tests aren't stressing this hot loop enough?
There was a problem hiding this comment.
But its possible that those tests aren't stressing this hot loop enough?
We probably need a more deliberate microbenchmark or targeted test to validate this. It may turn out to be negligible, but it’s worth proving either way.
|
Sorry, I will probably not have time to work on this soon. But I do think the optimization is valuable, as long as we can get more confident that Additionally, we should just skip the optimization if I haven't looked into how set index ranges are computed when we start with an empty |
|
@EmeraldShift Yes, I think that this optimization will be quite good. For this, we have to make sure that we never return false negative from But I want to work on removing the |
|
Yes, the existence of |
|
Hi @EmeraldShift @alexey-milovidov — a couple of suggestions to make this PR easier for reviewers and the changelog reader. Description (currently
Ignore this if the current title/body is intentional. |


Motivation
For very large tables, with queries that select one or more large, consecutive ranges of the table, the generic exclusion search algorithm can take a significant fraction of the total time. This is because for each candidate range, it only considers whether the key condition can be true within that range, otherwise it discards the range. But if the range cannot be false, the algorithm currently splits the range into
merge_tree_coarse_index_granularitypieces, and recursively checks them. This leads to O(N * log N) evaluations of the key condition against the inner marks.It turns out KeyCondition already supports checking if the condition can be false within a range. This PR enables the check, and exits the algorithm early if the selected range cannot be false.
Compare performance:
Maybe there are some cases where it is too expensive to check if a range can be false. I claim we should detect those cases with heuristics and selectively disable this optimization.
Part of #92780
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Improved the performance of generic exclusion search for queries that select large consecutive ranges of the table.
Documentation entry for user-facing changes
Version info
26.6.1.269