Cherry pick #108423 to 26.5: Throw `ILLEGAL_COLUMN` when `_distance` is selected directly in vector search queries by robot-ch-test-poll4 · Pull Request #108747 · ClickHouse/ClickHouse · GitHub
Skip to content

Cherry pick #108423 to 26.5: Throw ILLEGAL_COLUMN when _distance is selected directly in vector search queries#108747

Merged
robot-ch-test-poll4 merged 4 commits into
backport/26.5/108423from
cherrypick/26.5/108423
Jun 28, 2026
Merged

Cherry pick #108423 to 26.5: Throw ILLEGAL_COLUMN when _distance is selected directly in vector search queries#108747
robot-ch-test-poll4 merged 4 commits into
backport/26.5/108423from
cherrypick/26.5/108423

Conversation

@robot-ch-test-poll4

Copy link
Copy Markdown
Contributor

Original pull-request #108423

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

StackSlayerAI and others added 4 commits June 24, 2026 22:15
…arch queries

The `_distance` virtual column is internal to the vector search optimization and
populated only when the optimized plan rewrites the query. Previously, referencing
it directly in SELECT with an ORDER BY distance function caused a LOGICAL_ERROR
("Vector column unexpectedly already replaced") because the optimizer tried to add
`_distance` to the read list while it was already there from the user's SELECT.

Now this case throws a user-facing ILLEGAL_COLUMN error with a clear message
directing users to use the distance function (L2Distance, cosineDistance) in
ORDER BY instead.

The check is placed in both the first pass (tryUseVectorSearch) and the second pass
(optimizeVectorSearchSecondPass) for defense-in-depth coverage.

Closes: ClickHouse/clickhouse-core-incidents#1654

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Throw `ILLEGAL_COLUMN` when `_distance` is selected directly in vector search queries
@robot-ch-test-poll4 robot-ch-test-poll4 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 28, 2026
@robot-ch-test-poll4 robot-ch-test-poll4 merged commit 796c94b into backport/26.5/108423 Jun 28, 2026
@CLAassistant

CLAassistant commented Jun 28, 2026

Copy link
Copy Markdown

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.

6 participants