Cherry pick #126942 to 26.3: Fix wrong `count()` for `Date32` equality to `1900-01-01` in the sparsity trivial-count rewrite #127072

Closed
robot-ch-test-poll opened 10:23pm on October 20, 2026 wanted to merge 2 ฮ” into clickhouse/clickhouse backport/26.3/126942 from
cherrypick/26.3/126942
Reviewing
39 fewer changed lines
(89% less) vs GitHub
This saves about 155.8 hours per year vs conventional diff tools

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 of 1970-01-01 rows;
  • count() WHERE d != toDate32('1900-01-01') returns the number of 1900-01-01 rows (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 in 26.11 and later)
  • Backported to: 26.10.2.52, 26.9.24.7, 26.8.33.8, 26.7.36.9 <!-- ch-version-info:end -->
robot-ch-test-poll and 1 other authored
of work between October 20 and October 21
Diff Delta:
2
About 1 Diff Delta/hour
Classified as:  Bug Fix

robot-ch-test-poll's Description of Work #126856 `count() WHERE date32_col = toDate32('1900-01-01')` returns the number of `1970-01-01` rows (sparsity trivial-count rewrite uses the wrong Date32 default)

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-cherrypick label 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

4 total changed files
(2 files ignored)
Loading changes...
Loading changes...
End of PR:
PR review complete, one step closer to shipping ๐Ÿš€