Skip to content

Conversation

@yuzefovich
Copy link
Member

@yuzefovich yuzefovich commented Oct 7, 2025

This commit fixes a bug that was added long time ago when we added support for inverted joins on multi-column inverted indexes (i.e. such indexes that have a prefix of "forward" columns that we can constrain using an equality filter) in d3f9a8b. In particular, in that change we added tricky logic which prepended the prefix to the encoded inverted key; however, we then didn't change how we use the span.Builder. That guy assumes that it receives datums for each index column (both prefix and inverted) whereas we only provided the inverted bytes (with the prepended prefix). This led to us being confused about which key column is being encoded during span construction.

Consider an example where we have one prefix key column like i INT DESC (followed by the inverted column). In this case, when calling MakeKeyFromEncDatums we'd try to encode the single EncDatum using the DESC key direction. The EncDatum internally stores the prefix + inverted key using ASC key direction, so we'd try to decode it and fail - because of the prepended prefix. After this patch, we'll correctly only use a single column in the call to MakeKeyFromEncDatums.

Note that if prefix columns had only ASC directions, then we would simply get lucky. Namely, since we have EncDatumRow with a single EncDatum AND that EncDatum already has encoded value of ASC direction stored, we'd just reuse it and exit.

The code here is quite difficult to follow and perhaps it could be refactored (e.g. the comment on SpansFromInvertedSpans says that multi-column inverted indexes are expected to use non-nil constraint, yet in the inverted joiner we always pass nil), but I chose a more targetted fix that seems reasonable.

Additionally, to simplify the change a bit, I inlined the code for rowenc.MakeSpanFromEncDatums into two call sites (which also allows us to avoid constructing the unnecessary end key for the inverted spans case).

Fixes: #153499.
Release note (bug fix): Previously, CockroachDB would hit an internal error when performing an inverted join using an inverted index in which the first prefix column had DESC direction. The bug has been present since 21.1 and is now fixed.

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@yuzefovich yuzefovich force-pushed the fix-ij branch 2 times, most recently from ff28a0a to e8f5377 Compare October 7, 2025 02:49
@yuzefovich yuzefovich requested a review from mgartner October 7, 2025 03:40
@yuzefovich yuzefovich marked this pull request as ready for review October 7, 2025 03:40
@yuzefovich yuzefovich requested a review from a team as a code owner October 7, 2025 03:40
Copy link
Collaborator

@mgartner mgartner left a comment

Choose a reason for hiding this comment

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

:lgtm: Incredible work getting to the bottom of this! I agree this code is messed up in more than a few ways. In the future SpansFromInvertedSpans could be split into two methods: one for constraints and one for when the prefix has already been encoded in the inverted spans.

@mgartner reviewed 1 of 7 files at r1.
Reviewable status: :shipit: complete! 1 of 0 LGTMs obtained

@mgartner mgartner requested a review from a team October 7, 2025 13:59
@mgartner mgartner added the backport-all Flags PRs that need to be backported to all supported release branches label Oct 7, 2025
This commit fixes a bug that was added long time ago when we added
support for inverted joins on multi-column inverted indexes (i.e. such
indexes that have a prefix of "forward" columns that we can constrain
using an equality filter) in d3f9a8b.
In particular, in that change we added tricky logic which prepended the
prefix to the encoded inverted key; however, we then didn't change how
we use the `span.Builder`. That guy assumes that it receives datums for
each index column (both prefix and inverted) whereas we only provided
the inverted bytes (with the prepended prefix). This led to us being
confused about which key column is being encoded during span
construction.

Consider an example where we have one prefix key column like `i INT DESC`
(followed by the inverted column). In this case, when calling
`MakeKeyFromEncDatums` we'd try to encode the single EncDatum using the
DESC key direction. The EncDatum internally stores the prefix + inverted
key using ASC key direction, so we'd try to decode it and fail - because
of the prepended prefix. After this patch, we'll correctly only use
a single column in the call to `MakeKeyFromEncDatums`.

Note that if prefix columns had only ASC directions, then we would
simply get lucky. Namely, since we have EncDatumRow with a single
EncDatum AND that EncDatum already has encoded value of ASC direction
stored, we'd just reuse it and exit.

The code here is quite difficult to follow and perhaps it could be
refactored (e.g. the comment on `SpansFromInvertedSpans` says that
multi-column inverted indexes are expected to use non-nil constraint,
yet in the inverted joiner we always pass nil), but I chose a more
targetted fix that seems reasonable.

Additionally, to simplify the change a bit, I inlined the code for
`rowenc.MakeSpanFromEncDatums` into two call sites (which also allows us
to avoid constructing the unnecessary end key for the inverted spans
case).

Release note (bug fix): Previously, CockroachDB would hit an internal
error when performing an inverted join using an inverted index in which
the first prefix column had DESC direction. The bug has been present
since 21.1 and is now fixed.
@yuzefovich
Copy link
Member Author

I agree about the refactor, left a TODO for that idea. TFTR!

bors r+

craig bot pushed a commit that referenced this pull request Oct 7, 2025
154781: sql: display notice with job ID when waiting for a job r=rafiss a=rafiss

This will allow users to easily see the job ID in case they'd like to look at more details about the job in other introspection interfaces.

related to: #104597
Epic: None
Release note: None

154914: rowexec: fix inverted join with prefix columns with DESC direction r=yuzefovich a=yuzefovich

This commit fixes a bug that was added long time ago when we added support for inverted joins on multi-column inverted indexes (i.e. such indexes that have a prefix of "forward" columns that we can constrain using an equality filter) in d3f9a8b. In particular, in that change we added tricky logic which prepended the prefix to the encoded inverted key; however, we then didn't change how we use the `span.Builder`. That guy assumes that it receives datums for each index column (both prefix and inverted) whereas we only provided the inverted bytes (with the prepended prefix). This led to us being confused about which key column is being encoded during span construction.

Consider an example where we have one prefix key column like `i INT DESC` (followed by the inverted column). In this case, when calling `MakeKeyFromEncDatums` we'd try to encode the single EncDatum using the DESC key direction. The EncDatum internally stores the prefix + inverted key using ASC key direction, so we'd try to decode it and fail - because of the prepended prefix. After this patch, we'll correctly only use a single column in the call to `MakeKeyFromEncDatums`.

Note that if prefix columns had only ASC directions, then we would simply get lucky. Namely, since we have EncDatumRow with a single EncDatum AND that EncDatum already has encoded value of ASC direction stored, we'd just reuse it and exit.

The code here is quite difficult to follow and perhaps it could be refactored (e.g. the comment on `SpansFromInvertedSpans` says that multi-column inverted indexes are expected to use non-nil constraint, yet in the inverted joiner we always pass nil), but I chose a more targetted fix that seems reasonable.

Additionally, to simplify the change a bit, I inlined the code for `rowenc.MakeSpanFromEncDatums` into two call sites (which also allows us to avoid constructing the unnecessary end key for the inverted spans case).

Fixes: #153499.
Release note (bug fix): Previously, CockroachDB would hit an internal error when performing an inverted join using an inverted index in which the first prefix column had DESC direction. The bug has been present since 21.1 and is now fixed.

Co-authored-by: Rafi Shamim <rafi@cockroachlabs.com>
Co-authored-by: Yahor Yuzefovich <yahor@cockroachlabs.com>
@craig
Copy link
Contributor

craig bot commented Oct 7, 2025

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Oct 7, 2025

@craig craig bot merged commit 481e179 into cockroachdb:master Oct 7, 2025
23 of 24 checks passed
@blathers-crl
Copy link

blathers-crl bot commented Oct 7, 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 83d950e to blathers/backport-release-24.1-154914: POST https://api.github.com/repos/yuzefovich/cockroach/merges: 409 Merge conflict []

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

Backport to branch release-24.1 failed. See errors above.


error creating merge commit from 83d950e to blathers/backport-release-24.3-154914: POST https://api.github.com/repos/yuzefovich/cockroach/merges: 409 Merge conflict []

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

Backport to branch release-24.3 failed. See errors above.


error creating backport branch refs/heads/blathers/backport-release-25.3-154914: POST https://api.github.com/repos/yuzefovich/cockroach/git/refs: 422 Reference already exists []

Backport to branch release-25.3 failed. See errors above.


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

@yuzefovich yuzefovich deleted the fix-ij branch October 7, 2025 17:16
@yuzefovich
Copy link
Member Author

blathers backport release-25.4

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

Labels

backport-all Flags PRs that need to be backported to all supported release branches backport-failed target-release-26.1.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

roachtest: internal error: trailing bytes in encoded value with inverted join

3 participants