Skip to content

Invalid number of rows in Chunk with query_plan_join_swap_table=auto #87401

@PHaroZ

Description

@PHaroZ

Company or project name

Biron-BI, data analytics

Describe what's wrong

After migrate from CH 24.12 to 25.8, I got a new error "Invalid number of rows in Chunk" in some particular circumstances when querying a view with JOIN.
This issue could be get around by disabling query_plan_join_swap_table

Does it reproduce on the most recent release?

Yes

How to reproduce

With a fresh Clickhouse container started with

docker run --rm --name testch clickhouse/clickhouse-server:25.8.4.13

connect to it (docker exec -it testch clickhouse-client).

Then, execute the following setup script

DROP TABLE IF EXISTS b_customers;
CREATE TABLE b_customers (customer_id Int64, first_order_id Int64) ENGINE = MergeTree ORDER BY customer_id;
INSERT INTO b_customers SELECT number, number * 20000000 FROM system.numbers LIMIT 2,100000; -- will work with LIMIT 10000
INSERT INTO b_customers SELECT number * -1, -1 FROM system.numbers LIMIT 2; -- will work without this line or LIMIT 1

DROP TABLE IF EXISTS b_orders;
CREATE TABLE b_orders (order_id Int64, address_id String) ENGINE = MergeTree ORDER BY order_id;
INSERT INTO b_orders SELECT number, 'fake' FROM system.numbers LIMIT 80000; -- will work with LIMIT 70000
INSERT INTO b_orders SELECT first_order_id, 'fake' FROM b_customers GROUP BY first_order_id;

DROP TABLE IF EXISTS b_addresses;
CREATE TABLE b_addresses (address_id String) ENGINE = MergeTree ORDER BY address_id;

DROP VIEW IF EXISTS viewWithJoin;
CREATE VIEW viewWithJoin AS
	SELECT customers.customer_id AS dim_customers_id
	FROM b_customers AS customers ANY
		LEFT JOIN b_orders AS orders ON orders.order_id = customers.first_order_id ANY
		LEFT JOIN b_addresses AS shipping_addresses ON shipping_addresses.address_id = orders.address_id;

This query fails with "Invalid number of rows in Chunk"

select count() from viewWithJoin;

If I disable query_plan_join_swap_table (which has a value auto by default) the same query succeed

set query_plan_join_swap_table=0;
select count() from viewWithJoin;

Expected behavior

It should never fail, especially since the parameter query_plan_join_swap_table normally concerns JOIN ALL and not JOIN ANY.

Error message and/or stacktrace

Received exception from server (version 25.8.4):
Code: 49. DB::Exception: Received from localhost:9000. DB::Exception: Invalid number of rows in Chunk  String(size = 65409) Int64(size = 65537) column Int64 at position 1: expected 65409, got 65537: While executing JoiningTransform. (LOGICAL_ERROR)

Additional context

No response

Metadata

Metadata

Assignees

Labels

bugConfirmed user-visible misbehaviour in official release

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions