Skip to content

sql: fix identity computed columns with projection with generic plans#162036

Merged
craig[bot] merged 1 commit intocockroachdb:masterfrom
ZhouXing19:virtual-computed-col-idx-generic
Jan 31, 2026
Merged

sql: fix identity computed columns with projection with generic plans#162036
craig[bot] merged 1 commit intocockroachdb:masterfrom
ZhouXing19:virtual-computed-col-idx-generic

Conversation

@ZhouXing19
Copy link
Collaborator

@ZhouXing19 ZhouXing19 commented Jan 29, 2026

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 original column is incorrectly included in the lookup join conditions.
For example, if we have y_cp AS (y), with a WHERE x = $1::INT AND y = $2::INT, we are expecting
($1 -> x, $2 -> y) as the lookup join conditions. However, the bug would wrongly include a y -> y_cp_eq
projection as well.

The root cause is that 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.

@blathers-crl
Copy link

blathers-crl bot commented Jan 29, 2026

It looks like your PR touches production code but doesn't add or edit any test code. Did you consider adding tests to your PR?

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@ZhouXing19 ZhouXing19 force-pushed the virtual-computed-col-idx-generic branch from ba9a4f6 to dcb4e0e Compare January 30, 2026 18:14
@ZhouXing19 ZhouXing19 changed the title [WIP] fix virtual computed col index sql: fix identity virtual computed columns with generic plans Jan 30, 2026
@ZhouXing19 ZhouXing19 force-pushed the virtual-computed-col-idx-generic branch 5 times, most recently from 20b3495 to 4eb513b Compare January 30, 2026 19:56
@ZhouXing19 ZhouXing19 changed the title sql: fix identity virtual computed columns with generic plans sql: fix identity computed columns with projection with generic plans Jan 30, 2026
@ZhouXing19 ZhouXing19 requested a review from michae2 January 30, 2026 19:57
@ZhouXing19 ZhouXing19 marked this pull request as ready for review January 30, 2026 19:57
@ZhouXing19 ZhouXing19 requested a review from a team as a code owner January 30, 2026 19:57
@ZhouXing19 ZhouXing19 requested a review from a team January 30, 2026 19:57
@ZhouXing19 ZhouXing19 added backport-25.2.x Flags PRs that need to be backported to 25.2 backport-25.3.x Flags PRs that need to be backported to 25.3 backport-25.4.x Flags PRs that need to be backported to 25.4 backport-26.1.x Flags PRs that need to be backported to 26.1 labels Jan 30, 2026
Copy link
Collaborator

@michae2 michae2 left a comment

Choose a reason for hiding this comment

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

:lgtm: Nice job!!

@michae2 reviewed 3 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @ZhouXing19).

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 force-pushed the virtual-computed-col-idx-generic branch from 4eb513b to 88d5777 Compare January 30, 2026 21:36
@ZhouXing19
Copy link
Collaborator Author

Thanks for the prompt review!

I also just realized i wrote the test under pkg/sql/opt/exec/execbuilder/testdata/virtual_columns, for i thought virtual column is a must to trigger this error. But turns out it is not, so i moved it to pkg/sql/opt/exec/execbuilder/testdata/prepare.

Copy link
Member

@yuzefovich yuzefovich left a comment

Choose a reason for hiding this comment

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

Nice find!

@yuzefovich reviewed 4 files and all commit messages, and made 1 comment.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (and 1 stale) (waiting on @ZhouXing19).

@ZhouXing19
Copy link
Collaborator Author

TFTRs!
bors r+

@ZhouXing19
Copy link
Collaborator Author

bors r+

@craig
Copy link
Contributor

craig bot commented Jan 30, 2026

🕐 Waiting for PR status (GitHub check) to be set, probably by CI. Bors will automatically try to run when all required PR statuses are set.

@ZhouXing19
Copy link
Collaborator Author

bors r-

@craig
Copy link
Contributor

craig bot commented Jan 30, 2026

GitHub status checks took too long to complete, so bors is giving up. You can adjust bors configuration to have it wait longer if you like.

@ZhouXing19
Copy link
Collaborator Author

bors r+

@craig
Copy link
Contributor

craig bot commented Jan 31, 2026

🕐 Waiting for PR status (GitHub check) to be set, probably by CI. Bors will automatically try to run when all required PR statuses are set.

@yuzefovich
Copy link
Member

cla check was stuck, so I nudged it

bors r+

@ZhouXing19
Copy link
Collaborator Author

Thank you Yahor! ❤️

@yuzefovich
Copy link
Member

bors r+

@craig
Copy link
Contributor

craig bot commented Jan 31, 2026

@craig craig bot merged commit 0d609bc into cockroachdb:master Jan 31, 2026
26 checks passed
@blathers-crl
Copy link

blathers-crl bot commented Jan 31, 2026

Based on the specified backports for this PR, I applied new labels to the following linked issue(s). Please adjust the labels as needed to match the branches actually affected by the issue(s), including adding any known older branches.


Issue #161978: branch-release-25.2, branch-release-25.3, branch-release-25.4, branch-release-26.1.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@blathers-crl
Copy link

blathers-crl bot commented Jan 31, 2026

Encountered an error creating backports. Some common things that can go wrong:

  1. The backport branch might have already existed.
  2. There was a merge conflict.
  3. The backport branch contained merge commits.

You might need to create your backport manually using the backport tool.


💡 Consider backporting to the fork repo instead of the main repo. See instructions for more details.

error creating merge commit from 88d5777 to blathers/backport-release-25.2-162036: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 25.2.x failed. See errors above.


💡 Consider backporting to the fork repo instead of the main repo. See instructions for more details.

error creating merge commit from 88d5777 to blathers/backport-release-25.3-162036: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 25.3.x failed. See errors above.


💡 Consider backporting to the fork repo instead of the main repo. See instructions for more details.

error creating merge commit from 88d5777 to blathers/backport-release-25.4-162036: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 25.4.x failed. See errors above.


💡 Consider backporting to the fork repo instead of the main repo. See instructions for more details.

error creating merge commit from 88d5777 to blathers/backport-release-26.1-162036: POST https://api.github.com/repos/cockroachdb/cockroach/merges: 409 Merge conflict []

you may need to manually resolve merge conflicts with the backport tool.

Backport to branch 26.1.x failed. See errors above.


🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

@yuzefovich
Copy link
Member

Is this something we want to get into the next release train? The SHAs are picked on 02/04 and we'd need to backport into RC branches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-25.2.x Flags PRs that need to be backported to 25.2 backport-25.3.x Flags PRs that need to be backported to 25.3 backport-25.4.x Flags PRs that need to be backported to 25.4 backport-26.1.x Flags PRs that need to be backported to 26.1 backport-failed target-release-26.2.0 v26.2.0-prerelease

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sql: generic query plan caused "cannot map variable %d to an indexed var" error

4 participants