Skip to content

Support skip index analysis for mixed AND/OR filters - #87781

Merged
shankar-iyer merged 49 commits into
ClickHouse:masterfrom
shankar-iyer:or_predicates_2nd_attempt
Nov 29, 2025
Merged

shankar-iyer merged 49 commits into
ClickHouse:masterfrom
shankar-iyer:or_predicates_2nd_attempt

Conversation

@shankar-iyer

@shankar-iyer shankar-iyer commented Sep 28, 2025 •

Copy link
Copy Markdown
Member

Resolves #75228

Background

Design

Assume this WHERE clause: A = 5 OR B = 5 OR C = 5.

The corresponding RPN is A, B, OR, C, OR.

We replace the literals by unknown and get unknown(A), unknown(B), OR, unknown(C), OR.

unknown is supposed to be TRUE (which makes it a no-op in the existing skip index analysis for AND).

Now evaluate the skip index on A granule-by-granule. Suppose the skip index says "there could be matches". The sequence of RPN evaluation results will be TRUE, TRUE, TRUE, TRUE, TRUE. The PR introduces a callback UpdatePartialDisjunctionResultFn which updates results for each RPN element (= column). Skip index A evaluation will assume TRUE for unknown(B) and unknown(C).

Next, evaluate granule 2 using skip index A and suppose the skip index says "no row matches". Then the sequence of results for this granule is FALSE, TRUE, TRUE, TRUE, TRUE.

Evaluate all other granules in A using the skip index and save the result for each granule.

Now repeat the same for column B. Let’s say for granule 2, the check B = 5 again fails with skip index B. Then the sequence of RPN evaluation will be TRUE, FALSE, TRUE, TRUE, TRUE. The earlier saved partial result after skip index A for granule 2 was FALSE, TRUE, TRUE, TRUE, TRUE. Now the saved partial result will be updated to FALSE, FALSE, TRUE, TRUE, TRUE.

Let’s say granule 2 fails the skip index C check also. Then the final partial result state for granule 2 will be - FALSE, FALSE, TRUE, FALSE, TRUE

Once all 3 skip indexes have been evaluated on a part, we enter mergePartialResultsForDisjunction(). Here, we go back to the original RPN skeleton -

unknown(A), unknown(B), OR, unknown(C), OR

and now for granule 2, we will evaluate the RPN skeleton with its corresponding partial results - FALSE, FALSE, TRUE, FALSE, TRUE. unknown(A) is FALSE, unknown(B) is FALSE, unknown(C) is also FALSE for granule 2. The RPN to be evaluated then is FALSE, FALSE, OR, FALSE, OR. The final result will be FALSE for granule 2 and we can skip reading the actual granule.

The code uses boost::dynamic_bitset<> partial_eval_results for tracking partial results for each mark as that is the fastest and low-memory instead of using something like unordered_map<size_t, vector<bool>>

This PR has a caveat: it doesn't support short-circuiting. All skip indexes are evaluated, even if an early evaluation of a skip index forces the WHERE clause to be false. Example: x = 3 AND (y = 4 OR z = 5). If the skip index for x says for a granule that there are no matches, we probe y and z as well.

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

ClickHouse now uses skip indexes to perform index analysis on WHERE clauses with mixed AND- and OR-connected filter conditions. Previously, the WHERE clause needed to be a conjunction (AND) of filter conditions to utilize skip indexes. A new setting use_skip_indexes_for_disjunctions (default: on) controls this feature. (issue #75228)

@clickhouse-gh

clickhouse-gh Bot commented Sep 28, 2025 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [c3b3a2c]

Summary: ❌

job_name test_name status info comment
Stateless tests (amd_msan, parallel) failure
00816_long_concurrent_alter_column FAIL cidb, issue
BuzzHouse (amd_debug) failure
Logical error: 'Inconsistent AST formatting: the query: FAIL cidb, issue
BuzzHouse (amd_ubsan) failure
UndefinedBehaviorSanitizer: undefined behavior (STID: 3022-517e) FAIL cidb
Stateless tests (arm_asan, targeted) error

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Sep 28, 2025
@shankar-iyer
shankar-iyer marked this pull request as draft September 28, 2025 11:41
@shankar-iyer

Copy link
Copy Markdown
Member Author

The failures in CI for b4455ef are unrelated to the PR.

@shankar-iyer

Copy link
Copy Markdown
Member Author

Clean CI run at 9ecb84f - where the feature was turned ON by default and with default use_skip_indexes_on_data_read = 1.

@shankar-iyer

This comment was marked as resolved.

@rschu1ze

rschu1ze commented Nov 27, 2025 •

Copy link
Copy Markdown
Member

@shankar-iyer I plowed a bit through the code (sorry), you will nevertheless able to recognize it again.

What we need to do to merge this PR:

  • we need to test with more indexes (not as comprehensively as with min-max indexes, but we ideally have 1 SELECT query per index type). By "more indexes" I mean set, bloom filter indexes, text index.
  • we need to add a longer comment in the code somewhere (e.g. where the callback function is first created) that explains the whole logic again (otherwise, people will need to come back to this PR to understand what is going on).
  • I also thought we could make a test push to master that has the setting force-enabled (to see if there are crashes etc.), but the setting is already on by default, so this isn't needed.

@shankar-iyer

Copy link
Copy Markdown
Member Author

@shankar-iyer I plowed a bit through the code (sorry), you will nevertheless able to recognize it again.

What we need to do to merge this PR:

  • we need to test with more indexes (not as comprehensively as with min-max indexes, but we ideally have 1 SELECT query per index type). By "more indexes" I mean set, bloom filter indexes, text index.
  • we need to add a longer comment in the code somewhere (e.g. where the callback function is first created) that explains the whole logic again (otherwise, people will need to come back to this PR to understand what is going on).
  • I also thought we could make a test push to master that has the setting force-enabled (to see if there are crashes etc.), but the setting is already on by default, so this isn't needed.

I plowed a bit through the code (sorry), you will nevertheless able to recognize it again

No problem, only improves the code quality and reading!

  • we need to test with more indexes (not as comprehensively as with min-max indexes, but we ideally have 1 SELECT query per index type). By "more indexes" I mean set, bloom filter indexes, text index.

I am adding a testcase in the next upload which has combinations of these 4 types. I have been manually testing over the last few weeks.

  • we need to add a longer comment in the code somewhere (e.g. where the callback function is first created) that explains the whole logic again (otherwise, people will need to come back to this PR to understand what is going on).

Will do

  • I also thought we could make a test push to master that has the setting force-enabled (to see if there are crashes etc.), but the setting is already on by default, so this isn't needed.

Yes, thanks! So, currently a predicate like (A = 5 OR B = 5) will anyways read all the granules of both skip indexes A and B, and end up shortlisting all granules without any pruning. The skip index reading does not change with this PR. The PR spends a bit more CPU (tiny!) and will result in accurate range pruning. I have a couple of followup optimizations - i) Implement short circuit and ii) implement some kind of heuristics to abort skip index search if it is not helping.

}

indexes->use_skip_indexes_for_disjunctions = settings[Setting::use_skip_indexes_for_disjunctions]
&& !skip_indexes.useful_indices.empty()

@shankar-iyer shankar-iyer Nov 28, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

03100_lwu_36_json_skip_indexes failure in previous commit :

Needs skip_indexes.useful_indices.size() > 1 so that extra processing is done only if 2 or more skip indices are involved. Otherwise, the extra processing will be done for a condition like A = 5 OR A = 6 which is redundant (existing code already handles A = 5 OR A = 6)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry, you are right.

@rschu1ze rschu1ze changed the title Support conditions with ORs / AND in skip index analysis Support skip index analysis for mixed AND/OR filters Nov 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Skip Indexes are not used when the predicate has OR on two different columns

5 participants