MDEV-10526: Add binary string support to bitwise operators#5190
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces support for binary-to-binary bitwise operations (AND, OR, XOR, NOT, and bitwise shifts) on binary strings by implementing new handler classes and adding corresponding error messages for mismatched operand sizes. The review feedback highlights several critical issues: a potential integer overflow/wrap-around in the left-shift index calculation on 32-bit systems, incorrect ASCII string conversion of numeric arguments in mixed-type bitwise operations, and potential undefined behavior when handling empty binary strings due to passing a null pointer to memset.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| size_t src_idx= i + byte_shift; | ||
| if (src_idx < len) | ||
| { |
There was a problem hiding this comment.
The calculation size_t src_idx = i + byte_shift is susceptible to integer overflow/wrap-around on 32-bit systems if len is very large (e.g., close to size_t max). If i + byte_shift overflows, src_idx wraps around and can evaluate to a value less than len, leading to incorrect data copying from wrapped-around indices instead of zero-filling. To prevent this, perform the bounds check using subtraction (byte_shift < len - i) before computing src_idx.
if (byte_shift < len - i)
{
size_t src_idx= i + byte_shift;| StringBuffer<128> b_buf; | ||
| String *b= item->arguments()[1]->val_str(&b_buf); |
There was a problem hiding this comment.
In mixed-type bitwise operations (e.g., binary_string & numeric), calling val_str on the numeric argument converts it to its decimal string representation (e.g., 255 becomes the 3-byte ASCII string "255" / 0x323535), rather than its binary representation (e.g., 0xFF). This causes mismatched length errors (e.g., VARBINARY(1) & 255 compares length 1 with length 3) or incorrect bitwise operations if the lengths happen to match. To align with standard bitwise behavior (and MySQL 8.0 compatibility), numeric arguments should be converted to a binary string representation of their integer value, padded or truncated to match the length of the other binary string operand.
| size_t len= a->length(); | ||
|
|
||
| if (to->realloc(len)) |
There was a problem hiding this comment.
When the input binary string a is empty (len == 0), calling to->realloc(0) can return a null pointer or do nothing. Subsequently, passing a null pointer to memset (e.g., memset(out_ptr, 0, len)) is technically undefined behavior in C/C++, even if the length is 0. Adding an early exit for len == 0 avoids this potential undefined behavior and improves efficiency by bypassing unnecessary allocation and loop overhead.
size_t len= a->length();
if (len == 0)
{
to->length(0);
to->set_charset(&my_charset_bin);
item->null_value= false;
return to;
}
if (to->realloc(len))fa5cd7c to
862a606
Compare
grooverdan
left a comment
There was a problem hiding this comment.
Should be off the main branch as a new feature.
from @abarkov
"Would be nice to add tests that uuid, inet6, inet4, geometry are not allowed for bit operations. Later we can probably implement bit operations for uuid, inet6, inet4. But to avoid compatibility problems we need to make sure they return error now."
|
|
||
| bool fix_length_and_dec(Item_handled_func *item) const override | ||
| { | ||
| item->max_length= item->arguments()[0]->max_length; |
There was a problem hiding this comment.
from @abarkov
"check that max_length is calculated correctly in all functions"
This will show up in mtr --cursor-protocol on test that have truncated test result output on fields.
221664d to
6b9dd9e
Compare
gkodinov
left a comment
There was a problem hiding this comment.
Thank you for your contribution! This is a preliminary review.
Please squash your commits to 1 per feature. Maybe keep the spider part in a separate commit and the rest of it in another? Just a suggestion. But there definitely should not be 13 commits.
| # ========================================================================= | ||
| CREATE TABLE t1 (a VARBINARY(4), b VARBINARY(4)); | ||
| INSERT INTO t1 VALUES (x'FFFF0000', x'FF00FF00'); | ||
| # Expected output: FF000000, FFFFFF00, 00FFFF00, 0000FFFF, FFFE0000, 7FFF8000 |
There was a problem hiding this comment.
Here and everywhere: you do not need this line. The expected result is already recorded.
| SELECT HEX(INET6_ATON('ffff:ffff::') & INET6_ATON('2001:db8::1')); | ||
| HEX(INET6_ATON('ffff:ffff::') & INET6_ATON('2001:db8::1')) | ||
| 20010DB8000000000000000000000000 | ||
| # Expected output: TBD - verify via --record |
There was a problem hiding this comment.
especially this needs to go away
| # ========================================================================= | ||
| CREATE TABLE t_null (a VARBINARY(4), b VARBINARY(4)); | ||
| INSERT INTO t_null VALUES (x'FFFF0000', NULL); | ||
| # Expected output: NULL, NULL, NULL, NULL, NULL, NULL |
There was a problem hiding this comment.
do you really need a table for that?
…ates
Extends all six scalar bitwise operators (&, |, ^, ~, <<, >>)
and aggregate functions (BIT_AND, BIT_OR, BIT_XOR) to work
byte-by-byte on BINARY/VARBINARY columns, returning VARBINARY
of the same length.
Previously all operators silently cast binary arguments to
BIGINT (64-bit), truncating values wider than 64 bits. This
broke operations on INET6 addresses, UUIDs, and any binary
column wider than 8 bytes.
Binary mode activates when both operands have a true non-hybrid
binary string type_handler (Type_handler_longstr with my_charset_bin
collation, excluding hex hybrids like 0xFF). Uses the existing
Item_handled_func pluggable Handler pattern, adding new
Handler_str subclasses per operator alongside existing
Handler_ulonglong handlers.
Aggregate functions gain a parallel String accumulator with
correct neutral elements (0xFF for AND, 0x00 for OR/XOR).
Window function support added via a dynamically-sized uint32
bit-counter array (same sliding-window technique as the
existing 64-bit bit_counters[], extended to arbitrary lengths).
Key fixes included:
- reset_field()/update_field() binary mode (crash fix:
raw int8store on string-typed temp table field caused SIGSEGV)
- return_type_handler() varies by max_length following the
blob_type_handler/type_handler_varchar/type_handler_string
pattern from Item_char_typecast_func_handler_fbt_to_binary
- DERIVATION_COERCIBLE (not IMPLICIT) for binary results
- MY_MAX() overflow pattern replaced with plain increment
in window function counters
- Mismatched lengths push warning (not hard error), escalating
to error under strict SQL mode via THD::raise_condition()
- Regressions in main.gis (geometry entering binary mode via
STRING_RESULT+my_charset_bin) and main.func_json (JSON
over-rejected by stricter check_arguments() override) fixed
- GEOMETRY and hex hybrids excluded from binary mode detection
New error codes:
ER_INVALID_BITWISE_OPERANDS_SIZE
ER_INVALID_BITWISE_AGGREGATE_OPERANDS_SIZE
Closes: MDEV-10526
Implements Spider direct_add() support for BIT_AND/BIT_OR/BIT_XOR, allowing each remote shard to compute its own partial aggregate merged locally, mirroring the existing SUM/COUNT/MIN/MAX pattern. BIT_AND/OR/XOR are associative and commutative so partial results from shards can be merged correctly via direct_add(). Also fixes a pre-existing Spider bug: row->val_int() uses atoi() which silently overflows for BIGINT UNSIGNED values above INT_MAX. Fixed for this code path using my_strtoll10() instead. Closes: MDEV-10526
934bb95 to
75cfdf6
Compare
gkodinov
left a comment
There was a problem hiding this comment.
LGTM. Please stand by for the final review.
| --echo # ========================================================================= | ||
|
|
||
| SELECT HEX(INET6_ATON('ffff:ffff::') & INET6_ATON('2001:db8::1')); | ||
|
|
|
I'm reviewing the spider bit |
Draft for review. Implements byte-by-byte binary string mode for all scalar bitwise operators. Aggregate function
support (BIT_AND/BIT_OR/BIT_XOR) to follow.
Tested: