Pull Request Overview
- Opened on October 21, 2026
- Status Closed
- Commit count 2 with first commit October 20, 2026
Total Delta
Open Days
Test Delta
How long has this pull request spent in each phase of its lifecycle?
| Fraction of total time | Business days | Phase |
|---|---|---|
| N/A | 0 days | Issue creation to work beginning |
|
|
1 day | Authoring 2 commits before pull request opened for review |
|
|
0 days | Awaiting first review |
Total time for pull request 1 business day from first commit to closed PR
Cherry pick #126942 to 26.3: Fix wrong `count()` for `Date32` equality to `1900-01-01` in the sparsity trivial-count rewrite
Original pull-request https://github.com/ClickHouse/ClickHouse/pull/126942
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling git cherry-pick locally.
If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Before you resolve anything
Conflicts are often caused by a prerequisite change that has not been backported yet, rather than by a real divergence. The bot re-tries this cherry-pick against the release branch on every run, so if that is the case here it will merge itself as soon as the prerequisite lands, and you will see a comment saying so. Manual resolution is only needed while the conflict persists.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
- delete the
pr-cherrypicklabel from the PR - delete this branch from the repository
You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there
The PR source
The PR is created in the CI job
Company or project name
No response
Describe what's wrong
With default settings, SELECT count() FROM t WHERE d = toDate32('1900-01-01') on a MergeTree Date32 column is answered by the "trivial count with sparsity filter" optimization from the part's num_defaults counter. That counter counts rows whose stored value is bitwise zero, i.e. 1970-01-01. 1900-01-01, however, is what DataTypeDate32::getDefault() returns. So:
count() WHERE d = toDate32('1900-01-01')returns the number of1970-01-01rows;count() WHERE d != toDate32('1900-01-01')returns the number of1900-01-01rows (it should be all other rows).
The rows themselves are intact: SELECT ... WHERE d = toDate32('1900-01-01'), count(d), countIf(d = toDate32('1900-01-01')) and the same query with optimize_trivial_count_with_sparsity_filter = 0 all return the right number. INSERT ... SELECT count() is rewritten too, so the wrong count gets persisted: INSERT INTO s SELECT count() FROM t WHERE d = toDate32('1900-01-01') stores 980 (master 26.11.1.144, 39618113). The column doesn't have to be sparse: on a table holding just 1900-01-01 and 2000-02-29 the count is 0. Typed constants trigger it (toDate32('1900-01-01'), CAST('1900-01-01' AS Date32), defaultValueOfTypeName('Date32')); a plain string literal d = '1900-01-01' isn't rewritten and is correct.
1900-01-01 is the minimum and default value of Date32, so it's a common "unknown date" sentinel, and count() WHERE col = <sentinel> is a typical data-quality check.
Does it reproduce on the most recent release?
Yes
How to reproduce
Default settings, any interface.
CREATE TABLE t (id UInt32, d Date32) ENGINE = MergeTree ORDER BY id;
INSERT INTO t SELECT number, if(number % 50 = 7, toDate32('1900-01-01'), toDate32('1970-01-01')) FROM numbers(1000);
SELECT count() FROM (SELECT id FROM t WHERE d = toDate32('1900-01-01')); -- 20 (correct)
SELECT count() FROM t WHERE d = toDate32('1900-01-01'); -- 980 (wrong: the number of 1970-01-01 rows)
SELECT count() FROM t WHERE d != toDate32('1900-01-01'); -- 20 (wrong: should be 980)
SELECT count() FROM t WHERE d = toDate32('1900-01-01')
SETTINGS optimize_trivial_count_with_sparsity_filter = 0; -- 20 (correct)
EXPLAIN SELECT count() FROM t WHERE d = toDate32('1900-01-01');
-- ... ReadFromPreparedSource (Optimized trivial count with sparsity filter)
Non-sparse column:
CREATE TABLE t2 (d Date32) ENGINE = MergeTree ORDER BY tuple();
INSERT INTO t2 VALUES ('1900-01-01'), ('2000-02-29');
SELECT count() FROM t2 WHERE d = toDate32('1900-01-01'); -- 0, expected 1
Expected behavior
count() WHERE d = toDate32('1900-01-01') returns 20 and != returns 980, the same as without the rewrite.
Error message and/or stacktrace
None: the result is silently wrong.
Related issues and pull requests
Related: https://github.com/ClickHouse/ClickHouse/pull/105890 Related: https://github.com/ClickHouse/ClickHouse/pull/111816 Related: https://github.com/ClickHouse/ClickHouse/issues/124769 Related: https://github.com/ClickHouse/ClickHouse/issues/126035 Related: https://github.com/ClickHouse/ClickHouse/pull/101622
Additional context
classifySparsityPredicate treats col = c as "matches exactly the default rows" when typeMatchesDefaultnessStat(col.type) holds and constantEqualsTypeDefault(c) compares c with column_type->getDefault() (SparsityFilter.cpp#L309-L318, #L30-L42). typeMatchesDefaultnessStat lists the types for which SQL equality to the type default is the same row set as the bitwise IColumn::isDefaultAt that produces num_defaults, and it includes Date32 (SparsityFilter.cpp#L63-L86). But DataTypeDate32::getDefault() is -getDayNumOffsetEpoch() = 1900-01-01 (DataTypeDate32.cpp#L18-L21), while the stored zero is 1970-01-01. The comment above that function excludes Enum for the same reason (type default not bitwise 0). Date32 should be excluded too, or the constant should be compared with the column's zero value rather than getDefault().
The same Date32 default/zero mismatch has been reported for other code paths: #124769 (bloom filter) and #126035 (aggregates on empty input).
<!-- ch-version-info:start -->
Version info
- Resolved by: #126942
- Merged into:
26.11.1.368-master(included in26.11and later) - Backported to:
26.10.2.52,26.9.24.7,26.8.33.8,26.7.36.9<!-- ch-version-info:end -->
Comments Threads Pending Resolution
Resolved Comment Threads
No resolved comments have been left on this PR.