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