Skip to content

Query condition cache serves stale entries after a table is re-created with the same UUID #116202

Closed

Description

@HanziJiang

Company or project name

No response

Describe what's wrong

Reproduced at: https://fiddle.clickhouse.com/659af1f3-6a90-4d37-b1c8-54c74f84794d

QueryConditionCache keys its entries on (table UUID, part name, condition hash): code

This assumes (table UUID, part name) permanently identifies one set of rows. It does not. Block numbering restarts for a freshly created table, so after DROP TABLE + CREATE TABLE with the same UUID a brand-new part reuses the old part's name under the same UUID. An entry written against the old part is then applied to the new one, and granules that do contain matching rows get skipped.

The result is silent wrong results.

Does it reproduce on the most recent release?

Yes

How to reproduce

See https://fiddle.clickhouse.com/659af1f3-6a90-4d37-b1c8-54c74f84794d

SYSTEM DROP QUERY CONDITION CACHE;

DROP TABLE IF EXISTS qcc_bug SYNC;
CREATE TABLE qcc_bug UUID '22222222-3333-4444-5555-666666666666' (a Int64, s String)
ENGINE = MergeTree ORDER BY a;
INSERT INTO qcc_bug VALUES (1, 'no');          -- does NOT match the predicate

SELECT 'part (incarnation 1)      :', name    FROM system.parts WHERE table='qcc_bug' AND active;
SELECT 'cache entries before      :', count() FROM system.query_condition_cache;
SELECT 'prime, expect 0           :', count() FROM qcc_bug WHERE s = 'yes';
SELECT 'cache entries after       :', count() FROM system.query_condition_cache;

-- re-create with the SAME UUID
DROP TABLE qcc_bug SYNC;
CREATE TABLE qcc_bug UUID '22222222-3333-4444-5555-666666666666' (a Int64, s String)
ENGINE = MergeTree ORDER BY a;
INSERT INTO qcc_bug VALUES (2, 'yes');         -- DOES match the predicate

SELECT 'part (incarnation 2)      :', name    FROM system.parts WHERE table='qcc_bug' AND active;
SELECT 'BUG   cache on,  expect 1 :', count() FROM qcc_bug WHERE s = 'yes';
SELECT 'OK    cache off, expect 1 :', count() FROM qcc_bug WHERE s = 'yes' SETTINGS use_query_condition_cache = 0;

Actual output on 26.7.5:

part (incarnation 1)      :    all_1_1_0
cache entries before      :    0
prime, expect 0           :    0
cache entries after       :    1
part (incarnation 2)      :    all_1_1_0      <- same part name, different data
BUG   cache on,  expect 1 :    0              <- WRONG
OK    cache off, expect 1 :    1              <- correct

Expected behavior

The last SELECT count() FROM qcc_bug WHERE s = 'yes' should return 1, same as with use_query_condition_cache = 0. A cache shouldn't change what a query returns.

Error message and/or stacktrace

No response

Related issues and pull requests

Caused by (original implementation): #69236
Related (sibling fix): #109310

Additional context

No response

Activity

  1. self-assigned this
    on Aug 24, 2026
  2. added
    potential bug
    on Aug 24, 2026
  3. added and removed
    potential bug
    on Aug 24, 2026
  4. alexey-milovidov commented on Aug 24, 2026

    @alexey-milovidov
    Member

    This isn't a bug. Tables with the same UUID are assumed to be the same.
    You don't usually specify a UUID on creation manually.

  5. added
    close in a month if not active
    on Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

close in a month if not activeinvalid

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions