Repository navigation
Add support to use column key as condition in WHERE FIELD - #89603
Conversation
Signed-off-by: Aditya Chopra <[email protected]>
Signed-off-by: Aditya Chopra <[email protected]>
WHERE FIEWHERE FIELD
|
Workflow [PR], commit [a497998] Summary: ✅
AI ReviewSummaryThis PR moves bare numeric-key recognition into Findings
Final VerdictNot ready: the core non-projection pruning path looks correct now, but projection-enabled queries can still reject |
Signed-off-by: Aditya Chopra <[email protected]>
Signed-off-by: Aditya Chopra <[email protected]>
|
Hi @yariks5s . Any update on this pr? Please tell if anything else if required for me to add to this pr. |
|
Hi @alexey-milovidov @yariks5s, Is there any update on this PR's review? Please tell me if there is anything missing from my side on this PR. I am keen to contribute further in ClickHouse. |
Hello @adityachopra29, there are failing tests in the |
|
Hi, okay I will make the required changes shortly |
|
Dear @yariks5s, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself. |
When rewriting a bare-column filter such as `WHERE id` into `notEquals(id, 0)`, the zero constant was built with `output->result_type->getDefault`. For a `Nullable` (or `LowCardinality`) key column this yields `NULL`, turning the rewrite into `notEquals(key, NULL)`, which evaluates to `NULL` under three-valued logic and disables index analysis. Take the zero value from the nested non-nullable type instead, while still creating the const column with the original `result_type`. Co-authored-by: Cursor <[email protected]>
The `!isUInt8` guard excluded `Bool`/`UInt8` key columns from the bare-column rewrite, so `WHERE flag` stayed a bare input node. `KeyCondition::extractAtomFromTree` only builds index atoms from function nodes such as `notEquals(flag, 0)`, so the boolean-key case from ClickHouse#89222 produced no index ranges. Remove the guard so a genuine `Bool`/`UInt8` key column is also rewritten to `notEquals(flag, 0)`, and add a regression test covering `ORDER BY flag` with `WHERE flag`. Co-authored-by: Cursor <[email protected]>
The `EXPLAIN` reference for the `Float64` key case expected `(x not in [0, 0])`, but a `Float64` zero renders as `0.`, so update it to `(x not in [0., 0.])`. Also revert an unrelated stray blank line in `KeyCondition::extractAtomFromTree`. Co-authored-by: Cursor <[email protected]>
The previous approach rewrote a bare-column filter (`WHERE id`) into `notEquals(id, 0)` in `analyzeFilter`, which fired for every filter in every query. That had broad side effects: it crashed on `Nothing` typed columns (`materialize(NULL)`), and it broke optimizer passes that match on bare-column filters produced by `EXISTS`/`IN`-to-join rewrites (`query_plan_convert_any_join_to_semi_or_anti_join`, `rewrite_in_to_join`). Revert the planner rewrite and instead recognize a bare numeric key column directly in `KeyCondition::extractAtomFromTree`, treating it as `key not in [0, 0]`. This is scoped to index analysis on real key columns, so it does not disturb the logical plan or the optimizer, handles `Nullable`/`LowCardinality` keys via the nested type, and covers `WHERE NOT key` for free (the surrounding `not` inverts the atom into `key == 0`). See ClickHouse#89222. Co-authored-by: Cursor <[email protected]>
|
Hi @alexey-milovidov, is anything else remaining in this PR? If not, then we can proceed to get it merged... |
|
Merged the latest I verified the merge is semantically sound, since
The three CI failures on the previous head were all pre-existing, unrelated flakes already tracked by open issues, not caused by this index-pruning change (it only affects part/granule selection, never query results):
Waiting on the fresh CI run to go green. |
The bare-numeric-key rule added for ClickHouse#89222 treats `WHERE id` as `id != 0` (a `FUNCTION_NOT_IN_RANGE` over `[0, 0]`). For a `Nullable` (or `LowCardinality(Nullable)`) key, primary-key analysis maps a NULL key value to `+Inf` for `NULLS LAST` ordering, so a granule that holds only NULL looks definitely outside `[0, 0]` and the atom reports it as an exact, definite match. But `WHERE nullable_key` evaluates to NULL for those rows and filters them out. Ordinary pruning only over-reads, so it stays correct, but the exact-count / implicit-projection optimization (`SELECT count() ... WHERE nullable_key` with `optimize_use_implicit_projections = 1`) would count such NULL-only granules without reading them and return a wrong result. Skip the optimization for `Nullable` keys: the atom is left unset (`FUNCTION_UNKNOWN`), reverting to reading and filtering those rows. Add `04490_where_field_nullable_key_exact_count`, which asserts the optimized `count()` equals a full-scan ground truth for `Nullable(Int32)` and `Nullable(Float64)` keys with a NULL-only part. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Addressed the AI-review blocker about The hole. The bare-key rule treats The fix. Skip the bare-key optimization for Regression test. Verification. I confirmed the mechanism on a recent master ASan binary ( Heads-up (pre-existing, out of scope here): the explicit form |
The test exercised the `Nullable`-key exact-count guard with a bare `WHERE id` over a `Nullable(Int32)` / `Nullable(Float64)` key. But a bare `Int32` / `Float64` column in a boolean context is rejected by the projection filter check with `ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER` (an orthogonal, pre-existing limitation), so the query threw before it could ever reach the exact-count path the test means to cover. Switch the key to `Nullable(UInt8)` / `Nullable(Bool)`, which are valid filter types, so `WHERE flag` reaches the implicit-projection / exact-count optimization. With the `KeyCondition` guard the bare-key atom is left `FUNCTION_UNKNOWN` for `Nullable` keys, so the NULL-only granule is read and filtered and the count matches the full-scan ground truth. Without the guard the same NULL-only granule is reported as a definite match for `flag != 0` and over-counted, so the test is a genuine regression test for the fix. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
The latest `KeyCondition` change deliberately leaves a bare `Nullable` key as `FUNCTION_UNKNOWN` (it would otherwise over-count NULL-only granules in the exact-count path, see ClickHouse#89222). With `force_primary_key = 1` on the `Nullable` case, `ReadFromMergeTree::selectRangesToRead` then sees `key_condition.alwaysUnknownOrTrue()` and throws `INDEX_NOT_USED` instead of producing the expected rows. Drop `force_primary_key` from the `Nullable` case: the query correctly falls back to a full scan and filter and returns the same rows. The exact-count correctness for `Nullable` keys is covered by `04490_where_field_nullable_key_exact_count`. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Fixed both
Switched the key to
Note: the equivalent over-count on the explicit |
Resolves the conflict in `optimizeUseAggregateProjection.cpp`. While this PR was pending, `master` independently added the same "don't install the projection-rewrite DAG as the pruning filter when there is no real filter" guard, but as the more precise per-candidate `candidate.has_filter` (introduced in `9b6760887fc`, "Support WHERE clause for aggregate projections") rather than the whole-query `candidates.has_filter` this PR used. `candidate.has_filter` is a strict refinement: it is true only when this candidate's residual filter survives after stripping the projection's own implied `WHERE`, so it installs `nullptr` in a superset of the cases this PR guarded — both when the query has no filter (this PR's case, test `04356`) and when the projection's `WHERE` fully covers the query filter (residual empty), where the DAG's first output is likewise a bare projection key column. Adopted `master`'s version and kept a comment documenting the ClickHouse#89222 bare-key interaction (a bare numeric key column is read as `key != 0`, so it must not be mistaken for a filter predicate). The net PR diff is now the `KeyCondition` bare-key rule (with the `Nullable` guard), this documentation comment, and the three tests. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Merged the latest The conflict is a convergence: while this PR was pending,
As a result the net diff to |
LLVM Coverage Report
Changed lines: Changed C/C++ lines covered: 23/23 (100.00%) · Uncovered code |
|
CI triage after the latest master merge ( Public CI — the only red is CH Inc sync — red from three failures, all unrelated to this change (which only touches the read/index path in
Re-triggered the failed sync jobs. On the AI review's remaining "Not ready" note — The earlier approval was auto-dismissed by the later commits and the master merge. |
2b01bde
…allel-replicas read paths A bare non-UInt8 column in WHERE (implicit `col != 0`) is coerced by the normal read path but was rejected with ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER on the projection and parallel-replicas read paths (issue ClickHouse#110795). The defect no longer reproduces on master (fixed via the WHERE-field-condition work in ClickHouse#89603 and the projection filter fix in ClickHouse#106590), but there was no test covering the non-UInt8 case across both alternative read paths. This adds one. Co-Authored-By: Claude Opus 4.8 <[email protected]>
…upport Add support to use column key as condition in `WHERE` FIELD
…allel-replicas read paths A bare non-UInt8 column in WHERE (implicit `col != 0`) is coerced by the normal read path but was rejected with ILLEGAL_TYPE_OF_COLUMN_FOR_FILTER on the projection and parallel-replicas read paths (issue ClickHouse#110795). The defect no longer reproduces on master (fixed via the WHERE-field-condition work in ClickHouse#89603 and the projection filter fix in ClickHouse#106590), but there was no test covering the non-UInt8 case across both alternative read paths. This adds one. Co-Authored-By: Claude Opus 4.8 <[email protected]>
`optimizePrimaryKeyConditionAndLimit` already passes `limit + offset` of the first `LIMIT` above a read to the read. When that is 0 and the `LIMIT` does not read till end (`exact_rows_before_limit`, totals), the query takes no row from the read, yet `ReadFromMergeTree::selectRangesToRead` ran the whole primary key and skip index analysis for it. On the table of ClickHouse#125812 (244141 marks, `WHERE c2` on a `UInt8` key column, which ClickHouse#89603 turned into a key condition) that is a 41053-step exclusion search per `SELECT ... LIMIT 0`, the query BI tools and drivers issue to discover column types. The pass now marks such a read, and the analysis returns right after partition pruning, statistics pruning and sampling, with no ranges. The read then builds the empty pipe it builds for any read that selects nothing. Kept: `force_primary_key`, `force_index_by_date`, `force_data_skipping_indices`, SAMPLE validation, `rows_before_limit` with `exact_rows_before_limit`. The return precedes the query condition cache, which records the granules missing from a selection as not matching. Not skipped where the ranges are still needed: parallel replicas, `make_distributed_plan`, and forced or preferred projections. Behaviour change: `max_rows_to_read` and `max_partitions_to_read` no longer reject a `LIMIT 0` query, which reads nothing. Closes ClickHouse#125812 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Queries like
SELECT * FROM t WHERE idwill now use index skipping on theidcolumn. Resolves #89222Documentation entry for user-facing changes
Details
This change adds support to use column key as condition in
WHEREFIELD clauses, enabling index skipping optimization for queries.Example:
Will give:
This is the same as for the command:
Resolves #89222