Skip to content

Conversation

@ZhouXing19
Copy link
Collaborator

Backport 1/1 commit from #162036


Previously, when a computed column was defined as a identical to another column (e.g., vcol AS (col)), and a generic plan was used with a lookup join on an index containing that column (vcol), the query would fail with "cannot map variable %d to an indexed var".

This occurred because the Factory.Replace function doesn't invoke the replacement callback for leaf nodes like VariableExpr, as it only traverses children. Thus, with the computed column expression a simple VariableExpr, table column references weren't getting replaced with their corresponding placeholder columns. As a result, when constructing the projection, we went through the MergeProjectWithValues rule incorrectly.

This fix adds special handling in the constraint builder to directly apply the replacement function when the computed column expression is a simple VariableExpr, ensuring table columns are properly replaced with their corresponding placeholder columns.

Fixes #161978

Release note (bug fix): Fixed an error that occurred when using generic plan that generates a lookup join on indexes containing identity computed columns.

Release justification: Resolve a correctness issue where queries using identity computed columns with generic plans would fail with an error, preventing customers from executing valid queries.

Previously, when a computed column was defined as a identical to another column
(e.g., vcol AS (col)), and a generic plan was used with a lookup join
on an index containing that column (vcol), the query would fail
with "cannot map variable %d to an indexed var".

This occurred because the `Factory.Replace` function doesn't invoke the
replacement callback for leaf nodes like `VariableExpr`, as it only
traverses children. Thus, with the computed column expression a
simple `VariableExpr`, table column references weren't getting replaced
with their corresponding placeholder columns. As a result, when constructing
the projection, we went through the MergeProjectWithValues rule
incorrectly.

This fix adds special handling in the constraint builder to directly
apply the replacement function when the computed column expression is
a simple VariableExpr, ensuring table columns are properly replaced
with their corresponding placeholder columns.

Fixes cockroachdb#161978

Release note (bug fix): Fixed an error that occurred when using generic
plan that generates a lookup join on indexes containing identity
computed columns.
@ZhouXing19 ZhouXing19 requested a review from michae2 February 1, 2026 03:52
@ZhouXing19 ZhouXing19 requested a review from a team as a code owner February 1, 2026 03:52
@blathers-crl
Copy link

blathers-crl bot commented Feb 1, 2026

Thanks for opening a backport.

Before merging, please confirm that the change does not break backwards compatibility and otherwise complies with the backport policy. Include a brief release justification in the PR description explaining why the backport is appropriate. All backports must be reviewed by the TL for the owning area. While the stricter LTS policy does not yet apply, please exercise judgment and consider gating non-critical changes behind a disabled-by-default feature flag when appropriate.

@blathers-crl blathers-crl bot added backport Label PR's that are backports to older release branches T-sql-queries SQL Queries Team labels Feb 1, 2026
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@ZhouXing19 ZhouXing19 changed the title release 25.3: sql: fix identity computed columns with projection with generic plans Feb 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport Label PR's that are backports to older release branches T-sql-queries SQL Queries Team

2 participants