ClickHouse / ClickHouse/ClickHouse
ntile(constant) over a multi-shard Distributed/cluster() read fails with BAD_ARGUMENTS: argument loses constness in distributed shipping
- Dominant language
- C++
- Stars
- 49.9k
- Forks
- 9k
- Avg merge
- 21h 32m
- Merged PRs (30d)
- 515
Description
**Describe what's wrong**
`ntile(N) OVER (...)` with a literal constant `N` fails with `Code: 36` `Argument of 'ntile' function must be a constant` when the query reads from a multi-shard `cluster()`/`Distributed` source. The same query works locally and through single-shard `remote(...)`. The literal loses its constness on the distributed two-stage path (the window function is computed on the initiator over shard streams, and the argument column arrives materialized instead of const), so a valid query errors depending only on the topology.
Sibling constant-argument window functions (`nth_value(k, 2)`, `lagInFrame(k, 1)`) work over the same multi-shard source — only `ntile`'s validation rejects the de-consted column.
**How to reproduce**
With any cluster of two or more shards (`two_loop` below is two loopback shards):
```sql
CREATE TABLE br (k UInt32, w Int64) ENGINE = MergeTree ORDER BY k;
INSERT INTO br SELECT number, number * 2 FROM numbers(100);
SELECT k, ntile(3) OVER (PARTITION BY w ORDER BY k) FROM br LIMIT 2; -- OK
SELECT k, ntile(3) OVER (PARTITION BY w ORDER BY k)
FROM cluster(two_loop, default, br) LIMIT 2;
```
```
Code: 36. DB::Exception: Argument of 'ntile' function must be a constant. (BAD_ARGUMENTS)
```
Also reproduces through an `ENGINE = Distributed` table over a 2-shard cluster, through the dead-shard (`skip_unavailable_shards`) read path, and under parallel replicas. Single-shard `remote('host', db.br)` works.
* Version: 26.8.1.329 (official build).
* Non-default settings: none.
**Expected behavior**
The literal argument stays constant through distributed query shipping (or the validation accepts a uniform materialized literal); topology must not change a valid query into `BAD_ARGUMENTS`.
Found by the optimizer test framework (`tests/optimizer_tester`) topology oracle on night 2026-07-29 (22 signatures across the parallel-replicas, dead-shard, and real-replica arms — one root).
Contributor guide
Assessment
This issue has not been assessed yet.