Skip to content

Logical error when reading part with granule smaller than index_granularity #89691

@fastio

Description

@fastio

Company or project name

Tencent.

Describe what's wrong

When reading from a MergeTree table with parts whose granule contains fewer rows than index_granularity, ClickHouse may throw a LOGICAL_ERROR during query execution.
This happens when MergeTreeReaderIndex is used as the first reader in the ReadersChain.

Two corner cases have been observed:

  • Single-granule part — The part has only one granule, and its total number of rows is less than index_granularity.
  • Last granule smaller than index_granularity — The part’s final granule has fewer rows than index_granularity.

In both cases, MergeTreeReaderIndex incorrectly reports index_granularity rows, while the next reader in the chain reads the actual number of rows, causing an internal row count mismatch and a LOGICAL_ERROR during continueReadingChain.

Does it reproduce on the most recent release?

Yes

How to reproduce

Single-granule part

SET use_skip_indexes_on_data_read=1;
SET use_skip_indexes = 1;
SET use_query_condition_cache=0;

DROP TABLE IF EXISTS tab;

CREATE TABLE tab
(
    `i` Int64,
    `s` String,
    INDEX by_s s TYPE bloom_filter(0.001) GRANULARITY 1,
)
ENGINE = MergeTree
ORDER BY i
SETTINGS index_granularity = 4,index_granularity_bytes = 0, min_bytes_for_wide_part = 0;

insert into tab select 100, 'aaa';

SELECT i FROM tab WHERE s = 'aaa';

Last granule smaller than index_granularity

SET use_skip_indexes_on_data_read=1;
SET use_skip_indexes = 1;
SET use_query_condition_cache=0;

DROP TABLE IF EXISTS tab;

CREATE TABLE tab
(
    `i` Int64,
    `s` String,
    INDEX by_s s TYPE bloom_filter(0.001) GRANULARITY 1,
)
ENGINE = MergeTree
ORDER BY i
SETTINGS index_granularity = 4,index_granularity_bytes = 0, min_bytes_for_wide_part = 0;

INSERT INTO tab SELECT number, toString(number) FROM numbers(6);

SELECT i FROM tab WHERE s = '5';

Expected behavior

Logical error: 'RangeReader read 2 rows, but 4 expected.'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed user-visible misbehaviour in official release

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions