Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

opt/execbuilder: pick correct canary column ordinal #141728

Merged
merged 2 commits into from
Feb 20, 2025

Conversation

mgartner
Copy link
Collaborator

opt/execbuilder: pick correct canary column ordinal

The execbuilder previously used the colOrdMap of an Upsert's input to
find the ordinal of the canary column. In rare cases, the canary column
could also be used as a partial index DEL column. Because a colOrdMap
can only map each column ID to a single ordinal, and because partial
index columns are positioned after the canary column, the ordinal
fetched from the colOrdMap would be the partial index DEL column
ordinal, not the canary column ordinal.

This could cause node-crashing panics during execution when trying to
access the canary canary column in the input row after the partial index
columns are sliced off.

As an example, consider the table and mutation:

CREATE TABLE t (c BOOL PRIMARY KEY, INDEX (c) WHERE c)

UPSERT INTO t VALUES (false)

The input to the Upsert operator produces 4 physical columns:

  1. An insert column with the value to insert if a row with the same
    PK does not already exist. We'll call this logical column new_c.
  2. A canary column which is non-NULL if a row with the same PK already
    exists. This is always a PK column fetched from the table—in our
    example this is c. Let's call this logical column old_c.
  3. A partial index PUT column which is true if the new row should be
    added to the partial index. Since the partial index predicate
    expression is the PK column, this is the same logical column as
    new_c.
  4. A partial index DEL column which is true if the old row should be
    removed from the partial index. This is the same logical column as
    old_c.

So, the physical columns produced by the input of the Upsert are:

insert column canary column partial index PUT partial index DEL
new_c old_c new_c old_c

Using the colOrdMap to find the ordinal of old_c yields 3, instead
of 1.

During execution the partial index columns are sliced off, and the row
becomes:

insert column canary column
new_c old_c

When the execution engine attempts to access index 3 of this row, the
process panics with an out-of-bounds exception.

This has been fixed by using the lengths of the insert (and other)
columns to find the canary column ordinal, instead of the colOrdMap.

Fixes #136458

Release note (bug fix): A bug has been fixed that could cause gateway
nodes to panic when performing an UPSERT on a table with a BOOL
primary key column and a partial index with the PK column as the
predicate expression.

opt/execbuilder: remove colOrdMap return value from Builder.buildMutationInput

Release note: None

The execbuilder previously used the `colOrdMap` of an Upsert's input to
find the ordinal of the canary column. In rare cases, the canary column
could also be used as a partial index DEL column. Because a `colOrdMap`
can only map each column ID to a single ordinal, and because partial
index columns are positioned after the canary column, the ordinal
fetched from the `colOrdMap` would be the partial index DEL column
ordinal, not the canary column ordinal.

This could cause node-crashing panics during execution when trying to
access the canary canary column in the input row after the partial index
columns are sliced off.

As an example, consider the table and mutation:

    CREATE TABLE t (c BOOL PRIMARY KEY, INDEX (c) WHERE c)

    UPSERT INTO t VALUES (false)

The input to the Upsert operator produces 4 physical columns:

  1. An insert column with the value to insert if a row with the same
     PK does not already exist. We'll call this logical column `new_c`.
  2. A canary column which is non-NULL if a row with the same PK already
     exists. This is always a PK column fetched from the table—in our
     example this is `c`. Let's call this logical column `old_c`.
  3. A partial index PUT column which is true if the new row should be
     added to the partial index. Since the partial index predicate
     expression is the PK column, this is the same logical column as
    `new_c`.
  4. A partial index DEL column which is true if the old row should be
     removed from the partial index. This is the same logical column as
    `old_c`.

So, the physical columns produced by the input of the Upsert are:

| insert column | canary column | partial index PUT | partial index DEL
| ------------- | ------------- | ----------------- | -----------------
|    `new_c`    |    `old_c`    |     `new_c`       |     `old_c`

Using the `colOrdMap` to find the ordinal of `old_c` yields `3`, instead
of `1`.

During execution the partial index columns are sliced off, and the row
becomes:

| insert column | canary column |
| ------------- | ------------- |
|    `new_c`    |    `old_c`    |

When the execution engine attempts to access index `3` of this row, the
process panics with an out-of-bounds exception.

This has been fixed by using the lengths of the insert (and other)
columns to find the canary column ordinal, instead of the `colOrdMap`.

Fixes cockroachdb#136458

Release note (bug fix): A bug has been fixed that could cause gateway
nodes to panic when performing an `UPSERT` on a table with a `BOOL`
primary key column and a partial index with the PK column as the
predicate expression.
@mgartner mgartner requested a review from a team February 19, 2025 15:12
@mgartner mgartner requested a review from a team as a code owner February 19, 2025 15:12
@mgartner mgartner requested review from michae2 and DrewKimball and removed request for a team February 19, 2025 15:12
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@mgartner mgartner added backport-23.2.x Flags PRs that need to be backported to 23.2. backport-24.1.x Flags PRs that need to be backported to 24.1. backport-24.3.x Flags PRs that need to be backported to 24.3 backport-25.1.x Flags PRs that need to be backported to 25.1 labels Feb 19, 2025
Copy link
Collaborator

@DrewKimball DrewKimball 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 fix!

Do you think we're incorrectly using a ColMap like this anywhere else during planning? I wouldn't be surprised if there are other places where we can map the same column ID to different ordinal locations in the exec plan.

Reviewed 3 of 3 files at r1, 1 of 1 files at r2, all commit messages.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @michae2)

@mgartner
Copy link
Collaborator Author

Do you think we're incorrectly using a ColMap like this anywhere else during planning? I wouldn't be surprised if there are other places where we can map the same column ID to different ordinal locations in the exec plan.

Yes, I think it's possible. Note that it may not surface as a panic or correctness bug though—so it's not necessarily a problem.

@mgartner
Copy link
Collaborator Author

TFTR!

bors r+

@craig craig bot merged commit 7b04347 into cockroachdb:master Feb 20, 2025
24 checks passed
Copy link

blathers-crl bot commented Feb 20, 2025

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 #136458: branch-release-23.2, branch-release-24.1, branch-release-24.3, branch-release-25.1.


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

Copy link

blathers-crl bot commented Feb 20, 2025

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.


error creating merge commit from ca34bce to blathers/backport-release-23.2-141728: 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 23.2.x failed. See errors above.


error creating merge commit from ca34bce to blathers/backport-release-24.1-141728: 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 24.1.x failed. See errors above.


error creating merge commit from ca34bce to blathers/backport-release-24.3-141728: 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 24.3.x failed. See errors above.


error creating merge commit from 2d108ac to blathers/backport-release-25.1-141728: 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.1.x failed. See errors above.


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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-23.2.x Flags PRs that need to be backported to 23.2. backport-24.1.x Flags PRs that need to be backported to 24.1. backport-24.3.x Flags PRs that need to be backported to 24.3 backport-25.1.x Flags PRs that need to be backported to 25.1 target-release-25.2.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

runtime error: index out of range [3] with length 2
3 participants