Skip to content

Cherry pick #106682 to 26.3: Reject input() referenced more than once via CTE inlining - #107521

Merged
robot-clickhouse-ci-1 merged 7 commits into
backport/26.3/106682from
cherrypick/26.3/106682
Jun 15, 2026
Merged

robot-clickhouse-ci-1 merged 7 commits into
backport/26.3/106682from
cherrypick/26.3/106682

Conversation

@robot-clickhouse-ci-1

Copy link
Copy Markdown
Contributor

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-pick locally.
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:

  • delete the pr-cherrypick label from the PR
  • delete this branch from the repository

You also need to check the Original pull-request for pr-backports-created label, and delete if it's presented there

The PR source

The PR is created in the CI job

groeneai and others added 7 commits June 7, 2026 16:02
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
@robot-clickhouse-ci-1 robot-clickhouse-ci-1 added pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only! do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default labels Jun 15, 2026
@robot-clickhouse-ci-1
robot-clickhouse-ci-1 merged commit ff75d16 into backport/26.3/106682 Jun 15, 2026
@robot-clickhouse-ci-1
robot-clickhouse-ci-1 deleted the cherrypick/26.3/106682 branch June 15, 2026 14:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do not test disable testing on pull request pr-bugfix Pull request with bugfix, not backported by default pr-cherrypick Cherry-pick of merge-commit before backporting. Do not use manually - automated use only!

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants