Skip to content

Fix parameterized view used twice in same query#105170

Merged
alexey-milovidov merged 3 commits into
masterfrom
fix-parameterized-view-twice-105153
May 19, 2026
Merged

Fix parameterized view used twice in same query#105170
alexey-milovidov merged 3 commits into
masterfrom
fix-parameterized-view-twice-105153

Conversation

@alexey-milovidov
Copy link
Copy Markdown
Member

Fixes #105153.

Two calls to the same parameterized view with different argument values in one query (e.g. id IN (SELECT id FROM test_view(id = 'a')) AND id IN (SELECT id FROM test_view(id = 'b'))) were being deduplicated, so the second filter was silently dropped.

Root cause: each call to buildParameterizedViewStorage creates a fresh StorageView carrying the substituted inner query, but it reuses the original view's StorageID. TableNode::isEqualImpl / updateTreeHashImpl only looked at StorageID, so the two TableNodes compared equal and CollectSets (which keys PreparedSets by getTreeHash) collapsed them into one IN-set.

Fix: when the storage is a parameterized view, mix the substituted inner query into the tree hash and the equality check. Non-parameterized tables and views are unaffected.

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 incorrect results when the same parameterized view is referenced more than once in the same query with different argument values. Previously, the analyzer collapsed the calls into one, silently dropping all but the first filter.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

…n the same query

Fixes #105153.

When a parameterized view is referenced multiple times in the same query
with different argument values, e.g.

    SELECT id FROM testing
    WHERE id IN (SELECT id FROM test_view(id = 'a'))
      AND id IN (SELECT id FROM test_view(id = 'b'));

the analyzer was treating both subqueries as identical. As a result,
`CollectSets` keyed the IN-set by `getTreeHash`, found that both subqueries
hashed to the same value, and reused the first set for the second IN,
silently dropping the second filter.

The root cause is in `TableNode::isEqualImpl` and
`TableNode::updateTreeHashImpl`: each call to `buildParameterizedViewStorage`
creates a fresh `StorageView` whose inner query has the substituted argument
values, but the new storage shares the same `StorageID` as the original view.
Equality and hashing only looked at `StorageID`, so the two `TableNode`s for
`test_view(id = 'a')` and `test_view(id = 'b')` compared as equal and hashed
identically.

Fix it by mixing the substituted inner query into the tree hash and the
equality check whenever the storage is a parameterized view. Non-parameterized
tables and views are unaffected.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh Bot commented May 17, 2026

Workflow [PR], commit [5182958]

Summary:


AI Review

Summary

This PR fixes incorrect deduplication of IN subquery sets when the same parameterized view is referenced multiple times with different argument values. The change updates TableNode equality/hash behavior for parameterized views to include the substituted inner query and adds a focused stateless regression test. Based on the current diff and CI state, the fix is correct and sufficiently covered for the reported bug.

Final Verdict

Status: ✅ Approve

@clickhouse-gh clickhouse-gh Bot added the pr-bugfix Pull request with bugfix, not backported by default label May 17, 2026
The new test exposes a pre-existing limitation: parameterized views do
not work correctly with parallel replicas because parameter substitution
happens on the initiator only, and the substituted body is not propagated
to remote replicas, which see only the placeholder-bearing view body and
fail to resolve identifiers (UNKNOWN_IDENTIFIER).

Tracked separately in #84188. CI report:
https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=105170&sha=324ad58a31419a96f217af5c16f857bbd45eff3f&name_0=PR&name_1=Stateless%20tests%20%28amd_llvm_coverage%2C%20ParallelReplicas%2C%20s3%20storage%2C%20parallel%29
@clickhouse-gh
Copy link
Copy Markdown
Contributor

clickhouse-gh Bot commented May 18, 2026

LLVM Coverage Report

Metric Baseline Current Δ
Lines 84.10% 84.10% +0.00%
Functions 91.40% 91.40% +0.00%
Branches 76.50% 76.60% +0.10%

Changed lines: 82.61% (19/23) · Uncovered code

Full report · Diff report

Copy link
Copy Markdown
Member Author

@alexey-milovidov alexey-milovidov left a comment

Choose a reason for hiding this comment

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

Good.

@alexey-milovidov alexey-milovidov self-assigned this May 18, 2026
@alexey-milovidov alexey-milovidov added this pull request to the merge queue May 18, 2026
Merged via the queue into master with commit 195d857 May 19, 2026
166 checks passed
@alexey-milovidov alexey-milovidov deleted the fix-parameterized-view-twice-105153 branch May 19, 2026 00:00
@robot-ch-test-poll2 robot-ch-test-poll2 added the pr-synced-to-cloud The PR is synced to the cloud repo label May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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.

Using Parameterized View twice in same query

2 participants