Repository navigation
Description
Describe the bug
A WHERE predicate applied to a view over a cluster table function (icebergLocalCluster or fileCluster) is not sent to the shards. The same predicate applied directly to the cluster table function is sent, and the shards use it to prune Parquet row groups. Through the view, each shard reads every row group and returns every row to the initiator, which applies the filter afterwards. The result is correct, so this is a lost pushdown rather than a wrong result. It shows up in system.query_log as secondary queries without a WHERE clause, with read_rows equal to the full table and no pruned row groups.
To Reproduce
The script starts a clickhouse server on 127.0.0.1 with a cluster of three loopback shards that all point to the same server. It writes an Iceberg table with the IcebergLocal engine and a Parquet file (eight row groups in the full read), then compares a direct cluster read with a read through a view over the same cluster table function. The secondary queries are read from system.query_log with is_initial_query = 0.
Steps, run in an empty directory:
- Create the subdirectories
store,tmpanduser_filesin the working directory. Write a server configuration fileconfig.xmlthere with these settings, using absolute paths for the three directories:
path,tmp_pathanduser_files_pathset tostore,tmpanduser_files;listen_host127.0.0.1 andtcp_port19000;- the
defaultuser with an empty password, allowed from 127.0.0.1 only, with profiledefault, quotadefaultandaccess_management1; - the
defaultprofile withlog_queries1, and thedefaultquota without limits; - the
query_logtable in thesystemdatabase, withflush_interval_milliseconds500; - in
remote_servers, one clusterloopback3of three shards, each with one replica at 127.0.0.1 port 19000, so all three shards are this server; - the logger at level
warning, writing to the console.
- Start the server from that directory:
clickhouse server --config-file=config.xml- Save the SQL below as
repro.sqlin the same directory, and run it from another shell:
clickhouse client --port 19000 --multiquery --queries-file repro.sqlSELECT version();
SET allow_insert_into_iceberg = 1;
SET allow_experimental_insert_into_iceberg = 1;
CREATE TABLE ice (ts Int64, v String) ENGINE = IcebergLocal($$user_files/ice$$);
INSERT INTO ice SELECT toInt64(number) AS ts, toString(number) AS v FROM numbers(1000000);
INSERT INTO ice SELECT toInt64(1000000 + number) AS ts, toString(number) AS v FROM numbers(1000000);
INSERT INTO ice SELECT toInt64(2000000 + number) AS ts, toString(number) AS v FROM numbers(1000000);
INSERT INTO FUNCTION file($$fc/part.parquet$$, $$Parquet$$, $$ts Int64, v String$$) SELECT toInt64(number) AS ts, toString(number) AS v FROM numbers(1000000) SETTINGS output_format_parquet_row_group_size = 100000;
SELECT count() AS ice_rows, sum(ts) AS ice_sum_ts FROM ice;
SELECT count() AS file_rows, sum(ts) AS file_sum_ts FROM file($$fc/part.parquet$$, $$Parquet$$, $$ts Int64, v String$$);
SELECT count() AS expected_rows FROM ice WHERE ts BETWEEN 1500000 AND 1500999;
SELECT sum(ts) AS ice_full_sum_ts FROM icebergLocalCluster($$loopback3$$, $$user_files/ice$$) SETTINGS log_comment = $$ice-full$$;
SELECT count() AS ice_direct_rows FROM icebergLocalCluster($$loopback3$$, $$user_files/ice$$) WHERE ts BETWEEN 1500000 AND 1500999 SETTINGS log_comment = $$ice-direct$$;
CREATE VIEW ice_v AS SELECT * FROM icebergLocalCluster($$loopback3$$, $$user_files/ice$$);
SELECT count() AS ice_view_rows FROM ice_v WHERE ts BETWEEN 1500000 AND 1500999 SETTINGS log_comment = $$ice-view$$;
SELECT sum(ts) AS fc_full_sum_ts FROM fileCluster($$loopback3$$, $$fc/part.parquet$$, $$Parquet$$, $$ts Int64, v String$$) SETTINGS log_comment = $$fc-full$$;
SELECT count() AS fc_direct_rows FROM fileCluster($$loopback3$$, $$fc/part.parquet$$, $$Parquet$$, $$ts Int64, v String$$) WHERE ts BETWEEN 500000 AND 500999 SETTINGS log_comment = $$fc-direct$$;
CREATE VIEW fc_v AS SELECT * FROM fileCluster($$loopback3$$, $$fc/part.parquet$$, $$Parquet$$, $$ts Int64, v String$$);
SELECT count() AS fc_view_rows FROM fc_v WHERE ts BETWEEN 500000 AND 500999 SETTINGS log_comment = $$fc-view$$;
SYSTEM FLUSH LOGS;
SELECT
log_comment,
countIf(is_initial_query = 0) AS secondary_queries,
countIf(is_initial_query = 0 AND position(query, $$WHERE$$) > 0) AS secondary_queries_with_where,
sumIf(read_rows, is_initial_query = 0) AS secondary_read_rows,
sumIf(ProfileEvents[$$ParquetReadRowGroups$$], is_initial_query = 0) AS secondary_row_groups_read,
sumIf(ProfileEvents[$$ParquetPrunedRowGroups$$], is_initial_query = 0) AS secondary_row_groups_pruned
FROM system.query_log
WHERE type = $$QueryFinish$$ AND log_comment != $$$$
GROUP BY log_comment
ORDER BY log_comment
FORMAT PrettyCompactMonoBlock;
SELECT
log_comment,
arrayStringConcat(groupUniqArray(replaceAll(replaceRegexpAll(query, $$\s+$$, $$ $$), char(39), $$"$$)), $$ ;; $$) AS secondary_query_text
FROM system.query_log
WHERE type = $$QueryFinish$$ AND is_initial_query = 0 AND log_comment != $$$$
GROUP BY log_comment
ORDER BY log_comment
FORMAT Vertical;
SELECT
multiIf(
countIf(log_comment = $$fc-view$$ AND is_initial_query = 0) != 1 OR countIf(log_comment = $$fc-direct$$ AND is_initial_query = 0 AND position(query, $$WHERE$$) > 0) != 1, $$INVALID: fileCluster secondary query shape unexpected$$,
countIf(log_comment = $$fc-view$$ AND is_initial_query = 0 AND position(query, $$WHERE$$) > 0) = 1, $$PASS: fileCluster view secondary query carries the WHERE clause$$,
$$BUG: fileCluster view secondary query has no WHERE clause$$) AS fc_verdict,
multiIf(
countIf(log_comment = $$ice-view$$ AND is_initial_query = 0) != 1 OR countIf(log_comment = $$ice-direct$$ AND is_initial_query = 0 AND position(query, $$WHERE$$) > 0) != 1, $$INVALID: icebergLocalCluster secondary query shape unexpected$$,
countIf(log_comment = $$ice-view$$ AND is_initial_query = 0 AND position(query, $$WHERE$$) > 0) = 1, $$PASS: icebergLocalCluster view secondary query carries the WHERE clause$$,
$$BUG: icebergLocalCluster view secondary query has no WHERE clause$$) AS ice_verdict
FROM system.query_log
WHERE type = $$QueryFinish$$
FORMAT Vertical;Output on Antalya 26.6.8.20001 (the shell harness lines are removed, and the secondary query texts of fc-direct and ice-direct are left out of the second result: both carry the WHERE clause, as secondary_queries_with_where shows in the first):
26.6.8.20001.altinityantalya
3000000 4499998500000
1000000 499999500000
1000
4499998500000
1000
1000
499999500000
1000
1000
┌─log_comment─┬─secondary_queries─┬─secondary_queries_with_where─┬─secondary_read_rows─┬─secondary_row_groups_read─┬─secondary_row_groups_pruned─┐
1. │ fc-direct │ 1 │ 1 │ 130818 │ 1 │ 7 │
2. │ fc-full │ 1 │ 0 │ 1000000 │ 8 │ 0 │
3. │ fc-view │ 1 │ 0 │ 1000000 │ 8 │ 0 │
4. │ ice-direct │ 1 │ 1 │ 131072 │ 1 │ 0 │
5. │ ice-full │ 1 │ 0 │ 3000000 │ 3 │ 0 │
6. │ ice-view │ 1 │ 0 │ 3000000 │ 3 │ 0 │
└─────────────┴───────────────────┴──────────────────────────────┴─────────────────────┴───────────────────────────┴─────────────────────────────┘
Row 2:
──────
log_comment: fc-full
secondary_query_text: SELECT sum(__table1.ts) AS fc_full_sum_ts FROM fileCluster("loopback3", "fc/part.parquet", "Parquet", "ts Int64, v String") AS __table1 SETTINGS log_comment = "fc-full"
Row 3:
──────
log_comment: fc-view
secondary_query_text: SELECT __table2.ts AS ts FROM fileCluster("loopback3", "fc/part.parquet", "Parquet", "ts Int64, v String") AS __table2
Row 5:
──────
log_comment: ice-full
secondary_query_text: SELECT sum(__table1.ts) AS ice_full_sum_ts FROM icebergLocalCluster("loopback3", "user_files/ice") AS __table1 SETTINGS log_comment = "ice-full"
Row 6:
──────
log_comment: ice-view
secondary_query_text: SELECT __table2.ts AS ts FROM icebergLocalCluster("loopback3", "user_files/ice") AS __table2
Row 1:
──────
fc_verdict: BUG: fileCluster view secondary query has no WHERE clause
ice_verdict: BUG: icebergLocalCluster view secondary query has no WHERE clause
Expected behavior
- The secondary query of each view case (
fc-view,ice-view) carries the sameWHEREclause as the direct case. - For the view,
secondary_read_rowsandsecondary_row_groups_readshould match the direct read: 130818 rows, one row group read and seven pruned forfileCluster, and 131072 rows and one row group read foricebergLocalCluster. Instead the views read 1000000 rows in eight row groups and 3000000 rows in three row groups. - The verdict lines should print
PASS. All queries return 1000 rows in every case.
Root cause
IStorageCluster::readinsrc/Storages/IStorageCluster.cppsetsquery_to_sendonce fromquery_info.query, which is the inner query of the view, and passes it toReadFromCluster. This is the text sent to each shard.- The
WHEREof the outer query reachesReadFromCluster::applyFilters(same file) as a filter DAG. That DAG is stored inlisting_filter_dag, andReadFromCluster::createExtensionuses it only for object listing and task assignment. Nothing writes it back intoquery_to_send. - For a direct read the predicate is already part of
query_info.query, so the shard prunes row groups. Through a view it is not, so the shard reads and returns all rows. - Line numbers refer to the
antalya-26.6branch source as read at the time of checking (readnear line 560,applyFiltersnear lines 137 to 156). This was derived from the source and the observed symptoms, not from instrumenting the build. The officialv26.6.8.7-stableandv26.9.21.9-stablesources do not containlisting_filter_dag, so the mechanism there is not traced; only the symptom is confirmed.
Proposed fix
Candidate, not yet confirmed. Before ReadFromCluster::initializePipeline formats query_to_send for the shards, AND the predicate from listing_filter_dag into its WHERE clause. The predicate is rendered from the filter DAG back to an AST. Column nodes must use the bare column name, because a qualified outer name such as __table1.ts does not resolve in the scope of the inner query of the view. Nodes that cannot be rendered safely, for example non-deterministic functions, must cancel the merge instead of sending a partial predicate. The excerpt below shows the call and the WHERE merge only; the DAG-to-AST helper is omitted.
@@ ReadFromCluster::initializePipeline
createExtension();
+ mergeListingFilterIntoQueryToSend();
@@ ReadFromCluster::mergeListingFilterIntoQueryToSend (excerpt)
+ if (const auto & existing_where = select_query->where())
+ predicate_ast = makeASTFunction("and", existing_where->clone(), std::move(predicate_ast));
+ select_query->setExpression(ASTSelectQuery::Expression::WHERE, std::move(predicate_ast));We have not yet confirmed that this candidate fixes the reproducer.
Key information
- Antalya builds
26.6.8.20001.altinityantalyaand26.6.8.20002.altinityantalya(official): the bug reproduces on both, with the same secondary query text and read counts. - Upstream
v26.6.8.7-stableandv26.9.21.9-stable: the same symptom reproduces on both (the view secondary query has noWHEREclause). The exact counts differ on 26.9.21.9, for example 48 row groups for the full Iceberg read. - Data is on the local filesystem, with the
IcebergLocalengine and a plain Parquet file written withfile. There is no catalog. The cluster is three loopback shards on one server. - Both
icebergLocalClusterandfileClustershow the same behavior. No object storage is used.
How this affects us
We run an Iceberg data lake on Antalya 26.6 and read it with cluster table functions. We found this in our own testing, by comparing a filtered read of a table through a plain view with the same read made directly: the direct read prunes row groups, the view read does not. In that test, a filtered read through a view makes each shard read every row group and return every row to the initiator, so the cost grows with the table size instead of the size of the result. We have not measured this in production.
Related
- No existing Altinity/ClickHouse issue was found for this defect. The search covered view filter pushdown with cluster functions, cluster filter pushdown, and
icebergLocalCluster. - ClickHouse/ClickHouse Send the filter on a table read by a cluster function to the replicas ClickHouse/ClickHouse#125615 (open): "Send the filter on a table read by a cluster function to the replicas". It sends the deterministic conjuncts of a filter to the replicas of a cluster function in multi-table queries. We have not checked whether it covers the view case.
Activity
aadant commentedon Nov 5, 2026
Fixed on icebergLocalCluster