MDEV-24931: Fix Bitmap<64>::is_prefix assertion with >64-column NATURAL JOIN on derived table#4756
Open
bodyhedia44 wants to merge 1 commit intoMariaDB:10.6from
Open
MDEV-24931: Fix Bitmap<64>::is_prefix assertion with >64-column NATURAL JOIN on derived table#4756bodyhedia44 wants to merge 1 commit intoMariaDB:10.6from
bodyhedia44 wants to merge 1 commit intoMariaDB:10.6from
Conversation
…AL JOIN on derived table When a NATURAL JOIN operates on a derived table (or view with derived_merge=off) having more than 64 columns, the optimizer's generate_derived_keys_for_table() would: 1) Overflow a 32-bit shift: (key_part_map)(1 << parts) when parts >= 32 2) Create a derived key with more parts than Bitmap<64>/key_part_map can hold 3) Crash on DBUG_ASSERT(prefix_size <= width) in Bitmap<64>::is_prefix(65) Fix: cap derived keys at sizeof(key_part_map)*8 (64) parts. Excess columns are excluded from the key (keyuse->key = MAX_KEY). Also fix the shift to cast before shifting: (key_part_map) 1 << parts.
gkodinov
requested changes
Mar 9, 2026
Member
gkodinov
left a comment
There was a problem hiding this comment.
Thank you for your contribution. This is a preliminary review.
| KEYUSE *first_keyuse= keyuse; | ||
| uint prev_part= keyuse->keypart; | ||
| uint parts= 0; | ||
| uint max_parts= sizeof(key_part_map) * 8; |
Member
There was a problem hiding this comment.
This should not be sizeof(key_part_map) IMHO. I'd use table->file->max_key_parts() to get the info from the SE.
| while (i < count && keyuse->used_tables == first_keyuse->used_tables && | ||
| keyuse->keypart == prev_part); | ||
| parts++; | ||
| if (parts < max_parts) |
Member
There was a problem hiding this comment.
why the extra check here? You are already checking above.
| } | ||
| else | ||
| { | ||
| keyuse->key= MAX_KEY; |
Member
There was a problem hiding this comment.
Add a comment here mentioning what does setting MAX_KEY to a key really mean.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When a
NATURAL JOINoperates on a derived table (or view withderived_merge=off) having more than 64 columns, the server crashes with:Assertion `prefix_size <= width' failed in Bitmap<64u>::is_prefix
UBSAN also reports: shift exponent 32 is too large for 32-bit type 'int'
Root Cause
generate_derived_keys_for_table() in sql_select.cc creates derived keys with no cap on the number of key parts. When a 65-column table is NATURAL JOINed:
1by 32+ bits).How to Reproduce
Fix
Test
Added
main.mdev24931with three cases:derived_merge=off(original bug report).