Query sometimes fails at planning time with `CANNOT_READ_ALL_DATA` when its table has an active Wide part
Company or project name
https://noemata.sh, Observability based on ClickHouse.
Describe what's wrong
A query fails at planning time with CANNOT_READ_ALL_DATA when its table has an active Wide part with 0 rows and the query reads a subcolumn whose serialization has a prefix (for example a Map(LowCardinality(String), String) key subcolumn, a LowCardinality Tuple element, a JSON path, or a Variant subcolumn) and has a WHERE clause.
ClickHouse creates such parts itself. A TTL drop of an expired part (TTLDropMerge, with or without ttl_only_drop_parts) and a mutation that deletes every row of a part both leave an active part with rows = 0. Its column .bin files are 0 bytes. The part stays active until the cleanup thread removes it. In production we see bursts of failures in the minute or two after each TTL drop, several times a day, on OpenTelemetry tables with Map(LowCardinality(String), String) attribute columns.
The query plan optimization optimizePrewhere calls MergeTreeData::getColumnSizes, which calls IMergeTreeDataPart::getSubcolumnSize for every active part. For a Wide part, MergeTreeDataPartWide::calculateSubcolumnSize calls getListOfStreamsForColumn, which creates a reader and deserializes the column's serialization prefixes. The empty part has no prefix to read, so SerializationLowCardinality::deserializeBinaryBulkStatePrefix throws. calculateSubcolumnSize returns early only when checksums.empty(), and the empty part's checksums list its 0-byte files. Data reads never open such a part because it has no marks.
Does it reproduce on the most recent release?
Yes
How to reproduce
clickhouse-client --multiquery < empty_part_subcolumn_size.sql (attached). The parallel-replicas query uses the parallel_replicas cluster from tests/config/config.d/clusters.xml.
CREATE TABLE t_empty_part (p UInt8, m Map(LowCardinality(String), String))
ENGINE = MergeTree PARTITION BY p ORDER BY tuple()
SETTINGS min_bytes_for_wide_part = 0, remove_empty_parts = 0;
INSERT INTO t_empty_part VALUES (1, {'k': 'v'});
INSERT INTO t_empty_part VALUES (2, {'k': 'v'});
ALTER TABLE t_empty_part DELETE WHERE p = 1 SETTINGS mutations_sync = 2;
-- 26.6 to 26.8: CANNOT_READ_ALL_DATA
SELECT count() FROM t_empty_part WHERE m['k'] = 'v';
-- 26.9: CANNOT_READ_ALL_DATA
SELECT count() FROM t_empty_part WHERE m['k'] = 'v'
SETTINGS enable_parallel_replicas = 1, automatic_parallel_replicas_mode = 2,
automatic_parallel_replicas_min_bytes_per_replica = 0, max_parallel_replicas = 2,
cluster_for_parallel_replicas = 'parallel_replicas', parallel_replicas_for_non_replicated_merge_tree = 1;remove_empty_parts = 0 keeps the empty part active so the result is deterministic. With the default, the same queries fail until the cleanup thread removes the part. The same failure occurs when the empty part comes from a TTL drop: replace the ALTER ... DELETE with TTL on a date column, an expired row, and OPTIMIZE TABLE ... FINAL.
Expected behavior
The size estimate skips parts with 0 rows (or treats their subcolumn size as 0), and both queries return 1.
Error message and/or stacktrace
Code: 33. DB::Exception: Cannot read all data. Bytes read: 0. Bytes expected: 8: (while reading prefix of column m.key_k). (CANNOT_READ_ALL_DATA) (version 26.9.5.3 (official build))
5. DB::ReadBuffer::readStrict(char*, unsigned long)
6. DB::SerializationLowCardinality::deserializeBinaryBulkStatePrefix(...)
7. DB::SerializationNamed::deserializeBinaryBulkStatePrefix(...)
8. DB::SerializationTuple::deserializeBinaryBulkStatePrefix(...)
9. DB::SerializationArray::deserializeBinaryBulkStatePrefix(...)
10. DB::SerializationMapKeyValue::deserializeBinaryBulkStatePrefix(...)
11. DB::MergeTreeReaderWide::deserializePrefix
12. DB::MergeTreeReaderWide::deserializePrefixForAllColumnsImpl(...)
14. DB::MergeTreeReaderWide::getAllColumnsSubstreams()
15. DB::MergeTreeDataPartWide::getListOfStreamsForColumn(DB::NameAndTypePair const&) const
16. DB::MergeTreeDataPartWide::calculateSubcolumnSize(String const&) const
17. DB::IMergeTreeDataPart::getSubcolumnSize(String const&) const
18. DB::MergeTreeData::getColumnSizes
19. DB::QueryPlanOptimizations::optimizePrewhere(DB::QueryPlan::Node&, bool, bool)
20. DB::QueryPlanOptimizations::optimizeTreeSecondPass(...)
21. DB::QueryPlan::optimize(DB::QueryPlanOptimizationSettings const&)The query fails as ExceptionBeforeStart.
Related issues and pull requests
https://github.com/clickhouse/clickhouse/pull/119607
Additional context
I've seen this error personally over the last few weeks, possibly months. It seems intermittent. The exact reproduction scenario was written by Opus 5.5. Apologies if this is noise.
This reproduces on 26.9.5.3 when automatic parallel replicas are enabled. On the default 26.9 path the error no longer occurs, because #119607 estimates sizes from the parts left after pruning, and the empty part is pruned. The fallback to the table-wide storage.getColumnSizes remains for plans that have neither indexes nor an analyzed result, and reaches the same code.
| Version | Default query | Automatic parallel replicas | With allow_calculating_subcolumns_sizes_for_merge_tree_reading = 0 |
|---|---|---|---|
| 26.4.3.29 | OK | OK | OK |
| 26.5.1.882 | OK | not tested | OK |
| 26.6.4.55 | fails | not tested | fails |
| 26.7.4.54 | fails | fails | fails |
| 26.8.5.11 | fails | fails | OK |
| 26.9.5.3 | OK | fails | OK |
Our production server runs 26.8.10.6 and shows the default-query failure. The failures start with #110623 (merged into 26.7, backported to 26.6.2.82 and 26.5.6.55). #114146 (26.8.1) made the size estimate respect allow_calculating_subcolumns_sizes_for_merge_tree_reading. #119607 (26.9.1) removed the default-path failure. The MergeTreeDataPartWide.cpp code is unchanged on master. 26.8.13.2-lts, the newest 26.8 release, does not contain #119607.
- Other types that fail the same way on 26.8.5.11 (TTL-created empty part):
Map(String, LowCardinality(String))value subcolumn,Tuple(a LowCardinality(String))element,JSONpath,Variant(String, UInt64)subcolumn.Map(String, String),Array(LowCardinality(String)).size0,Nullable(String).null, and a wholeLowCardinality(String)column are not affected. - Querying only the empty part's partition also fails on 26.8, because the size estimate runs over all active parts before partition pruning.
- Workaround on 26.8 and 26.9:
allow_calculating_subcolumns_sizes_for_merge_tree_reading = 0. On our workload it did not change bytes read or latency.
Source: ClickHouse/ClickHouse