#120937·ClickHouse

`prefer_global_in_and_join = 1` does not rewrite `x IN local_table` / `NOT IN local_table` (nor `IN (subquery)` outside WHERE) to GLOBAL IN: the shard fails with 47/60 or silently reads its own copy of the table

Author: zlareb1Created Sep 19, 2026Updated Sep 29, 2026
Labelsbugcomp-distributedcomp-query-analyzer

Describe what's wrong

prefer_global_in_and_join = 1 is documented as replacing IN/JOIN with GLOBAL IN/GLOBAL JOIN, and the documented use case is exactly a local, non-distributed table that exists only on the initiator ("we have query serving nodes that contain local tables, which are not suitable for distribution"). The rewrite is applied to x IN (SELECT ... FROM local_table) and to JOIN local_table, but not to the table-identifier form x IN local_table / x NOT IN local_table, and not to an IN (subquery) outside WHERE (select list, HAVING). Those are shipped to the shards as plain IN, so a shard either fails because it has no such table, or silently reads its own copy of the table instead of the initiator's.

How to reproduce

Two servers, cluster2 with one shard on each; t_shard exists on both, s only on the initiator:

sql
-- both servers
CREATE TABLE t_shard (k UInt32) ENGINE = MergeTree ORDER BY k;
-- initiator: even keys, remote: odd keys
INSERT INTO t_shard SELECT number FROM numbers(1000) WHERE number % 2 = 0;   -- initiator
INSERT INTO t_shard SELECT number FROM numbers(1000) WHERE number % 2 = 1;   -- remote
-- initiator only
CREATE TABLE s (k UInt32) ENGINE = MergeTree ORDER BY k;
INSERT INTO s SELECT number * 5 FROM numbers(50);
CREATE TABLE d AS t_shard ENGINE = Distributed(cluster2, currentDatabase(), t_shard);

SET prefer_global_in_and_join = 1;

SELECT count() FROM d WHERE k IN (SELECT k FROM s);   -- 50   (rewritten to GLOBAL IN, correct)
SELECT count() FROM d WHERE k GLOBAL IN s;            -- 50   (explicit GLOBAL, correct)
SELECT count() FROM d WHERE k IN s;                   -- Code: 47. Unknown expression or table expression identifier `default.s` (from the remote shard)
SELECT count() FROM d WHERE k NOT IN s;               -- Code: 47, same
SELECT count() FROM d PREWHERE k IN s;                -- Code: 47, same
SELECT sum(k IN (SELECT k FROM s)) FROM d;            -- Code: 60. Unknown table expression identifier 'default.s' (from the remote shard)
SELECT k, count() FROM d GROUP BY k HAVING k IN (SELECT k FROM s);   -- Code: 60, same
SELECT count() FROM d AS l INNER JOIN s ON l.k = s.k; -- 50   (JOIN with a bare table IS rewritten to GLOBAL JOIN)

If the remote shard happens to have its own (different) table s, the IN s form silently uses it: with an empty s on the remote, SELECT count() FROM d WHERE k IN s returns 25 (only the initiator's shard is filtered against the initiator's s) while IN (SELECT k FROM s) returns 50.

Mechanism

StorageDistributed applies RewriteInToGlobalInVisitor only to query_node.getWhere(), and the visitor returns early unless the second argument of the in function is a QueryNode:

cpp
auto * query = function_node->getArguments().getNodes()[1]->as<QueryNode>();
if (!query)
    return;

so a TableNode on the right-hand side of IN is never turned into globalIn, and IN functions in the select list or HAVING are never visited. rewriteJoinToGlobalJoinIfNeeded handles the bare-table JOIN case, which is why the join form works.

Expected behavior

With prefer_global_in_and_join = 1, x IN local_table, x NOT IN local_table (also under PREWHERE) and IN (subquery) in any clause should be executed as GLOBAL IN, the same as x IN (SELECT ... FROM local_table) in WHERE and as JOIN local_table already are.

Does it reproduce on the most recent release?

Reproduced on master 26.10.1.121 (official build), 5/5, two plain MergeTree servers, no settings other than prefer_global_in_and_join = 1.

Related: https://github.com/ClickHouse/ClickHouse/issues/66617 (the IN (subquery) form of the same rewrite under the analyzer, fixed)

Found by an automatic optimizer-testing framework (differential testing of optimizer settings, query plans, and equivalent rewrites).