Skip to content

Broken use_query_condition_cache=true behavior #96060

Closed

Description

@HSEhomework

Company or project name

Higher School of Economics

Describe what's wrong

Invalid behavior of query

Does it reproduce on the most recent release?

Yes

How to reproduce

It is extremely hard to reproduce isolated.

This is the original query:

with block_0 as (
select activity_year
       ,toString(row_index) as loan_id_ai
       ,add_months('2022-12-01'::date, -0) as matching_report_month
from hmda.hmda_results  ),

block_1 as (
select  * from block_0 where (activity_year = toYear(matching_report_month)) or (activity_year = toYear(matching_report_month) - 1))
select  *
from block_1  limit 100 settings use_query_cache=0, enable_analyzer=1

it is broken and does not returns any result, while it SHOULD.

And this is a small change in query (RENAME matching_report_month -> rm):

with block_0 as (
select activity_year
       ,toString(row_index) as loan_id_ai
       ,add_months('2022-12-01'::date, -0) as rm
from hmda.hmda_results  ),

block_1 as (
select  * from block_0 where (activity_year = toYear(rm)) or (activity_year = toYear(rm) - 1))
select  *
from block_1  limit 99 settings use_query_cache=0, prefer_column_name_to_alias=1, enable_analyzer=1

This one works!
Only rename happened.

If I make small type adjustment, it starts to work again!

with block_0 as (
select activity_year::int
,toString(row_index) as loan_id_ai
,add_months('2022-12-01'::date, -0) as matching_report_month
from hmda.hmda_results ),

block_1 as (
select * from block_0 where (activity_year = toYear(matching_report_month)) or (activity_year = toYear(matching_report_month) - 1))
select *
from block_1 limit 100 settings use_query_cache=0, enable_analyzer=1

so just type adjustment and it again works!

If I disable enable_analyzer it works as well.

How to explain this ? How to mitigate?

Expected behavior

No response

Error message and/or stacktrace

No response

Additional context

No response

Version info

Activity

  1. added
    potential bug
    on Feb 5, 2026
  2. den-crane commented on Feb 5, 2026

    @den-crane
    Contributor

    There is no function add_months in ClickHouse. What version/build do you use?

    What is a data type of activity_year ?

    Can you share show create table hmda.hmda_results ?

    Can you share for both queries

    explain indexes = 1
    with block_0 as ( ..
    
  3. alexey-milovidov commented on Feb 5, 2026

    @alexey-milovidov
    Member

    I investigated this issue but could not reproduce it on the latest master (commit 3561bc3, version 26.2.1.1).

    I tried many combinations:

    • Different column types for activity_year (UInt16, Int32, Nullable(UInt16), etc.)
    • Tables with and without a column named matching_report_month (to test alias shadowing)
    • Different table engines (MergeTree, View)
    • Various settings (prefer_column_name_to_alias, enable_scopes_for_with_statement, optimize_time_filter_with_preimage, enable_optimize_predicate_expression)
    • addMonths with -0 and ::date cast syntax
    • UDF add_months as a wrapper for addMonths

    All tests return the correct results with enable_analyzer=1.

    Could you please provide:

    1. Your exact ClickHouse version (SELECT version())
    2. The table schema (SHOW CREATE TABLE hmda.hmda_results)
    3. The output of EXPLAIN QUERY TREE for both the broken query (with matching_report_month alias) and the working query (with rm alias)

    This will help determine whether the issue was already fixed in a recent commit or if it requires specific table/data characteristics to reproduce.

  4. added
    st-need-info
    on Feb 5, 2026
  5. HSEhomework commented on Feb 6, 2026

    @HSEhomework
    Author

    I investigated as well. I am almost sure it is connected to option use_query_condition_cache
    My query was executed for all reports in a cycle. Thats why I have add_months('2022-12-01'::date, -0).

    So for some reason invalid condition cache was stored.

    add_months is defined like this:
    "CREATE FUNCTION add_months AS (d, n) -> addMonths(d::Nullable(date), n)")

    • Version: 25.12.4.35
    CREATE TABLE hmda.hmda_results ( `activity_year` Int16, `row_index` Nullable(Int64), ..... ) ENGINE = MergeTree ORDER BY activity_year SETTINGS index_granularity = 8192 --
  6. changed the title [-]Broken enable_analyzer=1 behavior[/-] [+]Broken use_query_condition_cache=true behavior[/+] on Feb 6, 2026
  7. added a commit that references this issue on Feb 6, 2026
    da49549
  8. added
    bug
    and removed
    st-need-info
    potential bug
    on Feb 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions