Repository navigation
Cherry pick #106682 to 26.3: Reject input() referenced more than once via CTE inlining - #107521
Merged
robot-clickhouse-ci-1 merged 7 commits intoJun 15, 2026
Merged
Conversation
A non-MATERIALIZED CTE that wraps `input()` and is referenced from multiple places (e.g. two sibling CTEs that are CROSS JOINed) gets inlined by the analyzer into multiple `ReadFromInput` plan steps over the same singleton `StorageInput`. The first `initializePipeline` consumed the pipe; the second hit `LOGICAL_ERROR 'Trying to read from input() twice.'`, which aborted the server in debug builds. The AST-level guard in `ASTInsertQuery::tryFindInputFunction` only sees the original AST before CTE inlining, so it could not catch this pattern. Reject the second `readImpl` add at the storage level with a clean `INVALID_USAGE_OF_INPUT` exception. Closes: #106672
Per @nikitamikhaylov suggestion on PR #106682: extend the `INVALID_USAGE_OF_INPUT` thrown from `StorageInput::readImpl` to actively guide users toward the `MATERIALIZED` CTE workaround. The hint also notes that `enable_materialized_cte = 1` is required: without that setting, `MATERIALIZED` is treated as just a hint by the analyzer and the CTE is still inlined into multiple plan sources. The new test case in `04323_input_twice_cte_106672.sh` verifies that the workaround actually works end-to-end (a CROSS JOIN of two child CTEs both reading from a MATERIALIZED parent CTE that wraps `input` succeeds). Related: #106682 Co-Authored-By: Claude Opus 4.8 <[email protected]>
The previous commit (5ea4f5e) added an early was_read_added guard in StorageInput::readImpl to reject the second readImpl call for non-MATERIALIZED CTE inlining. That check fired in legitimate cases too: the existing test 02915_input_table_function_in_subquery uses the shape INSERT INTO ... SELECT t FROM <view-with-aggregation> WHERE toUnixTimestamp64Milli(t) IN (SELECT timestamp FROM input(...)) over HTTP. The IN-subquery + aggregating-view planning calls readImpl twice on the singleton StorageInput (one of which is a planner artifact whose pipeline is never actually initialized), so the early readImpl guard rejected the query. Move the rejection back to ReadFromInput::initializePipeline, where the existing was_pipe_used flag truthfully detects the second pipeline read (only the actually-executed plan steps init their pipeline). Keep the helpful MATERIALIZED CTE / enable_materialized_cte hint message and the INVALID_USAGE_OF_INPUT error code (replacing the old LOGICAL_ERROR that aborted the server in debug builds). After this: - 04323_input_twice_cte_106672: still rejected at run time with INVALID_USAGE_OF_INPUT and the MATERIALIZED CTE hint, server stays alive. - 02915_input_table_function_in_subquery: passes again. Closes: #106672 Co-Authored-By: Claude Opus 4.8 <[email protected]>
The previous guard at `ReadFromInput::initializePipeline` only fired in the HTTP branch, after the `if (!pipe.empty()) return;` early exit. The TCP, gRPC and `clickhouse-local` paths take that early exit because `readImpl` builds a fresh `StorageInputSource` pipe per call site when `getInputBlocksReaderCallback` is set. Two `ReadFromInput` plan steps over the same `StorageInput` singleton therefore attached two sources to the same client-driven block reader and one of them silently observed EOF, dropping rows from the INSERT instead of raising `INVALID_USAGE_OF_INPUT`. Move the one-shot check to the top of `initializePipeline` so it runs before either branch and set `was_pipe_used` in the callback branch as well. Adds a `clickhouse-local` regression test (`04324_input_twice_cte_106672_local.sh`) asserting `INVALID_USAGE_OF_INPUT` is raised on the multi-CTE-ref shape and that single-ref / direct-`input` shapes still work. Verified locally on debug build: 50/50 random runs of `04324`. The new test fails on the parent commit (HEAD before this change) and passes with this change. Companion test `04323` (HTTP) and the existing `02915` callback-path test both still pass. Addresses clickhouse-gh[bot] review on PR #106682: #106682 (review) Co-Authored-By: Claude Opus 4.8 <[email protected]>
CI failure on PR #106682 commit 9c6cda9: Stateless tests (amd_llvm_coverage, old analyzer, s3 storage, DatabaseReplicated, WasmEdge, parallel) -> 04323_input_twice_cte_106672 Q4. Q4 verifies that the MATERIALIZED CTE workaround (recommended in our new INVALID_USAGE_OF_INPUT error message) actually works. enable_materialized_cte is honored only by the new analyzer; on the old-analyzer lane MATERIALIZED is treated as a no-op and the CTE is still inlined, so our one-shot guard fires and the workaround appears not to work. Pin enable_analyzer=1 in Q4's query-level SETTINGS so the workaround can actually run on every lane, including the old-analyzer one. CI: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=106682&sha=9c6cda9e631a25d222c5655cfe7020c5a897d34c PR: #106682 Co-Authored-By: Claude Opus 4.8 <[email protected]>
…-106672 Reject input() referenced more than once via CTE inlining
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original pull-request #106682
Do not merge this PR manually
This pull-request is a first step of an automated backporting.
It contains changes similar to calling
git cherry-picklocally.If you intend to continue backporting the changes, then resolve all conflicts if any.
Otherwise, if you do not want to backport them, then just close this pull-request.
The check results does not matter at this step - you can safely ignore them.
Troubleshooting
If the conflicts were resolved in a wrong way
If this cherry-pick PR is completely screwed by a wrong conflicts resolution, and you want to recreate it:
pr-cherrypicklabel from the PRYou also need to check the Original pull-request for
pr-backports-createdlabel, and delete if it's presented thereThe PR source
The PR is created in the CI job