Repository navigation
Description
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=1it 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=1This 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
- Resolved by: Fix query condition cache hash collision for CTE folded constants #96172
- Merged into:
26.2.1.364 - Backported to:
26.1.3.22,25.8.22.5
Activity
den-crane commentedon Feb 5, 2026
There is no function
add_monthsin 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
alexey-milovidov commentedon Feb 5, 2026
I investigated this issue but could not reproduce it on the latest master (commit 3561bc3, version 26.2.1.1).
I tried many combinations:
activity_year(UInt16,Int32,Nullable(UInt16), etc.)matching_report_month(to test alias shadowing)prefer_column_name_to_alias,enable_scopes_for_with_statement,optimize_time_filter_with_preimage,enable_optimize_predicate_expression)addMonthswith-0and::datecast syntaxadd_monthsas a wrapper foraddMonthsAll tests return the correct results with
enable_analyzer=1.Could you please provide:
SELECT version())SHOW CREATE TABLE hmda.hmda_results)EXPLAIN QUERY TREEfor both the broken query (withmatching_report_monthalias) and the working query (withrmalias)This will help determine whether the issue was already fixed in a recent commit or if it requires specific table/data characteristics to reproduce.
HSEhomework commentedon Feb 6, 2026
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 --[-]Broken enable_analyzer=1 behavior[/-][+]Broken use_query_condition_cache=true behavior[/+]Fix query condition cache hash collision for CTE folded constants