Skip to content

Fix _path / _file filtering for object storage, file and url - #112968

Merged
alexey-milovidov merged 49 commits into
masterfrom
fix-object-storage-global-in-set
Sep 3, 2026
Merged

alexey-milovidov merged 49 commits into
masterfrom
fix-object-storage-global-in-set

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Aug 2, 2026 •

Copy link
Copy Markdown
Member

Closes: #117806

SELECT ... FROM s3(...) WHERE _path GLOBAL IN (SELECT ...) over a path without globs threw

Logical error: 'Not-ready Set is passed as the second argument for function 'globalIn''

ReadFromObjectStorageStep::applyFilters intentionally leaves the sets of globalIn / globalNotIn unbuilt, so that ReadFromRemote can attach an external table to them first. Plan optimization then moves the subquery plan of such a set into a CreatingSetsStep (DelayedCreatingSetsStep::makePlansForSets), which means the set is only created once the pipeline runs.

StorageObjectStorageSource::createFileIterator prunes an explicit key list while the pipeline is being built, and its buildSetsForDAG call is a no-op for a set whose subquery plan is already gone, so the pruning expression was executed with a set that was not ready yet. GlobIterator is unaffected: it applies the same filter while listing objects, when the set is already built.

Pruning the keys is mostly an optimization - the same predicate is applied by the Filter step above the source - but it must not simply be skipped, because a nonexistent explicit key has its metadata probed before that Filter step ever runs. So KeysIterator now applies such a filter lazily, in next, when the pipeline runs and the set is ready. buildSetsForDAG reports whether every set in the DAG ended up ready, so a caller that executes the DAG right away can tell.

The same deferral was missing for file and url, and fixing it surfaced several neighbouring defects in _path / _file filtering:

  • A non-glob url bypassed the filtering iterator entirely, so a URL excluded by the predicate was still opened. All local URL reads now go through DisclosedGlobIterator, and a URL without globs is no longer split on commas (a comma is literal there).
  • file('archive.zip :: entry.csv') and the object-storage equivalent did not filter on the entry's virtual path before opening the outer archive, so a missing archive excluded by the predicate threw instead of returning no rows. The _path extraction fast path (s3_path_filter_limit) is now skipped for archives, whose _path values are entry paths (<archive>::<member>) rather than object keys.
  • Hive partition columns could be added twice to the temporary _path / _file filter block; one duplicate stayed empty, so applying the one-row filter threw SIZES_OF_COLUMNS_DOESNT_MATCH.
  • getPathSample probed object metadata for an explicitly named key, so hive partition inference failed for a key that does not exist or is filtered out before reading. The path string itself carries the partition columns, so it must not depend on the object being present.
  • collectFiltersForAnalysis named only StorageObjectStorageCluster among the cluster engines. The initiator's plan for a cluster read stops at WithMergeableState, so there is no Filter step above ReadFromCluster to collect the WHERE from, and SelectQueryInfo::filter_actions_dag is the only way a predicate reaches getTaskIteratorExtension. StorageURLCluster and StorageFileCluster therefore always got a null predicate and never pruned a path before handing it to a replica - for url that meant an excluded URL was opened by the replica.

Reproducer (deterministic, fails on master, passes with this change):

SELECT * FROM s3(s3_conn, filename = 'x', format = CSV, structure = 'x UInt64')
WHERE _path GLOBAL IN (SELECT 'no such path');

SET http_max_tries = 1;
SELECT * FROM urlCluster('test_cluster_two_shards_localhost', 'http://localhost:1/missing.tsv', TSV, 'x UInt64')
WHERE _path = 'no such path';

The stress test hit the original one through an AST fuzzer query over test_s3_race:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=1c3181d592a4aaeec15077107e15b7edab5bca8d&name_0=MasterCI&name_1=Stress%20test%20%28arm_release%29

Related: #107619

Changelog category (leave one):

  • Bug Fix (user-visible misbehavior in an official stable release)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Fix _path / _file filtering for object storage, file and url reads. A GLOBAL IN subquery over _path or _file of a path without globs no longer throws Not-ready Set is passed as the second argument for function 'globalIn'; an explicitly named archive member is filtered before the archive is opened; hive partition columns are inferred for an explicitly named key that does not exist; and urlCluster / fileCluster now prune a filtered-out path on the initiator instead of handing it to a replica.


Workflow [PR]
Sync PR [sync-upstream/pr/112968]

Version info

  • Merged into: 26.9.1.647 (included in 26.9 and later)

`SELECT ... FROM s3(...) WHERE _path GLOBAL IN (SELECT ...)` over a path
without globs threw

    Logical error: 'Not-ready Set is passed as the second argument for function 'globalIn''

`ReadFromObjectStorageStep::applyFilters` intentionally leaves the sets of
`globalIn` / `globalNotIn` unbuilt, so that `ReadFromRemote` can attach an
external table to them first. Plan optimization then moves the subquery plan of
such a set into a `CreatingSetsStep` (`DelayedCreatingSetsStep::makePlansForSets`),
which means the set is only created once the pipeline runs.

`StorageObjectStorageSource::createFileIterator` prunes an explicit key list
while the pipeline is being built, and its `buildSetsForDAG` call is a no-op for
a set whose subquery plan is already gone, so the pruning expression was
executed with a set that was not ready yet. `GlobIterator` is unaffected: it
applies the same filter while listing objects, when the set is already built.

Pruning the keys is an optimization - the same predicate is applied by the
`Filter` step above the source - so skip it when a set is not ready.
`buildSetsForDAG` now reports whether every set in the DAG ended up ready.

Reproduced by an AST fuzzer query in the stress test on master:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?REF=master&sha=1c3181d592a4aaeec15077107e15b7edab5bca8d&name_0=MasterCI&name_1=Stress%20test%20%28arm_release%29
@clickhouse-gh

clickhouse-gh Bot commented Aug 2, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [e7f1766]

Summary: ✅


AI Review

Summary

This PR fixes several _path / _file pruning bugs across object storage, file, and url, including the delayed-Set case, archive-member handling, hive-path sampling, and the leading-slash Azure _path inversion. The current head looks correct on those semantics, but one user-visible branch that the PR now claims to fix still lacks a focused regression.

Findings

⚠️ Majors

  • [src/Planner/Planner.cpp:276] The IStorageCluster widening is what extends initiator-side _path / _file pruning to StorageFileCluster, but there is still no focused top-level fileCluster regression for that path. 05048 proves urlCluster, while the existing 05030 test covers a different parallel_distributed_insert_select dispatch stack. A small fileCluster('missing.tsv') case with _path / _file GLOBAL IN would keep this branch from silently regressing.
Final Verdict

Request changes: add a focused top-level fileCluster regression for the new IStorageCluster planner path.

LLVM Coverage Report

Measured on commit e7f1766.

Metric Baseline Current Δ
Lines 88.70% 88.70% +0.00%
Functions 92.10% 92.10% +0.00%
Branches 81.00% 81.00% +0.00%

Changed lines: Changed C/C++ lines covered: 286/319 (89.66%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label Aug 2, 2026
@clickhouse-gh

clickhouse-gh Bot commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

LLVM Coverage Report

Metric Baseline Current Δ
Lines 86.50% 86.50% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 78.70% 78.80% +0.10%

Changed lines: Changed C/C++ lines covered: 14/14 (100.00%) · Uncovered code

Full report · Diff report

`QUALIFY` is not supported in the old analyzer, so the test failed with
`QUALIFY clause is not supported in the old analyzer` in the old-analyzer
stateless run. The function form of `globalNotIn` in `WHERE` exercises the same
pruning path and works with both analyzers.
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The only CI red, Stress test (arm_msan), is the known master issue #113165 (Logical error: ColumnBLOB should be converted to a regular column before usage in an ExpressionTransform over marshalled distributed blocks) — unrelated to this PR's object-storage _path/_file filter change. A fix is already in progress: #111997. Merged master and pushed to re-run CI.

alexey-milovidov and others added 2 commits August 5, 2026 03:25
Skipping the pruning entirely was not semantics-preserving: `KeysIterator::next`
still probed the metadata of every explicit key before the `Filter` step above
the source ever ran, so with `s3_ignore_file_doesnt_exist = 0` a nonexistent
key that the predicate excludes threw FILE_DOESNT_EXIST instead of returning
no rows. Defer the filter to `KeysIterator::next` instead: it runs when the
pipeline runs, after `CreatingSetsStep` has built the set, and it is applied
before the metadata probe, mirroring how `GlobIterator` filters while listing.

Co-Authored-By: Claude Fable 5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The AST fuzzer (amd_debug) red is unrelated to this PR: Logical error: 'or_argument_nodes.size() > 1' in tryExtractCommonExpressions (LogicalExpressionOptimizerPass) fires for a single-argument or in a JOIN ON clause, e.g. SELECT 1 FROM t1 JOIN t1 AS t2 ON or(t1.c0 = t2.c0) — no object storage involved. It reproduces on a debug build without this PR's changes. I will open a separate fix PR and link it here.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The fix for the unrelated AST fuzzer (amd_debug) failure is up: #113412 (single-argument or reaching tryExtractCommonExpressions when an argument has the type Nothing).

When `s3` runs in cluster mode (for example when `parallel_replicas_for_cluster_engines`
converts it to `s3Cluster`), `StorageObjectStorageCluster::getPathSample` builds a
`KeysIterator` during query analysis to infer hive partitioning. For an explicitly
specified key, `throw_on_zero_files_match = false` is not enough: `KeysIterator::next`
probes the object metadata, and the probe throws `S3_ERROR` (HTTP 404) for a key that
does not exist, so the query failed during analysis even when the `_path` / `_file`
predicate excludes that key and the answer is zero rows - which is what the
non-cluster path returns.

Sample the path with `ignore_non_existent_file = true`: a missing key leaves the
sample empty, as the existing comment in `getPathSample` already intends. A key that
is really needed for reading is probed again by the reader, which does report the
error.

This is what made the new test `04669_object_storage_path_filter_global_in` fail in
the `Stateless tests (amd_llvm_coverage, ParallelReplicas, s3 storage, parallel)`
report: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=112968&sha=5f0602ae3ca8c39bc8049c12fa40f4732221eed2&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29
PR: #112968
@alexey-milovidov

Copy link
Copy Markdown
Member Author

🕵 The only CI red, AST fuzzer (amd_debug), is unrelated to this PR: Logical error: 'database_name != "_temporary_and_external_tables"' in ASTSelectQuery::replaceDatabaseAndTable when merge(REGEXP('^'), '...') matches the internal database of temporary tables (StorageMerge builds a child plan for it) — no object storage involved. The fix is already open: #113224. Once it merges, I will re-merge master here to clear the red.

The `_path` extraction fast path swaps `GlobIterator` for a `KeysIterator` over the
validated candidates. A listing never yields a key that does not exist, so a candidate
that fits the glob but names no object must be skipped, not probed with an exception:
`azureBlobStorage(..., '/*.csv') WHERE _path = '<container>/missing.csv'` threw on the
metadata lookup instead of returning zero rows, and a mixed `IN` threw after reading
the existing blob. The iterator now ignores missing keys and fetches the metadata even
when the caller would defer it, since the probe is what identifies a missing candidate.
Comment thread src/Planner/Planner.cpp
/// `WithMergeableState` (`IStorageCluster::getQueryProcessingStage`), so there is no `Filter` step
/// above `ReadFromCluster` to collect the `WHERE` from - without this the predicate is null and
/// no `_path` / `_file` pruning happens at all.
if (dynamic_cast<const IStorageCluster *>(raw))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This IStorageCluster widening is what makes StorageFileCluster claim the same initiator-side _path / _file pruning fix as StorageURLCluster, but there is still no focused top-level fileCluster regression for it. 05048 only proves the urlCluster path, and the existing 05030 parallel_distributed_insert_select test exercises a different task-dispatch stack. A small fileCluster('missing.tsv') case with _path / _file GLOBAL IN would close the gap and would fail immediately if StorageFileCluster::getTaskIteratorExtension ever stopped receiving the collected predicate again.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

@zlareb1

zlareb1 commented Sep 6, 2026

Copy link
Copy Markdown
Member

TL;DR: one of the defects this PR fixes is a silent wrong result at default settings on the latest release, and the fix has no backport.

On 26.8.2.7 (and older releases), any exact WHERE on _path/_file over an archive read from object storage returns an empty result: the pre-archive pruning evaluates the predicate against the archive object's values (_file = 'arch.tar') while rows emit the entry values (_file = 'a.csv'), so the two filter stages never both pass — filtering on the exact value the query itself displays silently loses every row. Verified fixed by this PR on a master build (26.9.1.706); full probe battery with controls in #118403 (closed against this PR).

There is no backport of this PR to a release branch (the last 100 commits of 26.8 carry no trace). Given the silent-wrong-result severity on defaults, is a 26.8 backport planned?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

blocker This issue / pr blocks a new release pr-bugfix Pull request with bugfix, not backported by default pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Logical error: Not-ready Set is passed as the second argument for function 'A (STID: 0250-4e52)

5 participants