MDEV-37864 json_array_intersect result has incorrect length#5114
MDEV-37864 json_array_intersect result has incorrect length#5114grooverdan wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request refactors the Item_func_json_array_intersect class by moving the result length calculation and collation setting from prepare_json_and_create_hash to fix_length_and_dec. Feedback suggests consolidating the duplicated collation.set calls at the end: label to ensure all execution paths are covered correctly. Additionally, the length calculation formula 2 * MY_MIN(...) should be adjusted to prevent potential JSON truncation for small input values.
| collation.set(args[0]->collation); | ||
| parse_for_each_row= true; | ||
| goto end; | ||
| } | ||
| } | ||
|
|
||
| collation.set(args[0]->collation); |
There was a problem hiding this comment.
The call to collation.set(args[0]->collation) is duplicated at lines 5552 and 5558. Furthermore, it appears to be missing from the if (args[0]->const_item() && args[1]->const_item()) path (the portion not shown in the diff).
A more robust and cleaner approach would be to move this call to the end: label, just before fix_char_length_ulonglong. This ensures the collation is set exactly once for all successful paths, including those that use goto end;.
| fix_char_length_ulonglong((ulonglong) | ||
| 2* MY_MIN(args[0]->max_char_length(), | ||
| args[1]->max_char_length())); |
There was a problem hiding this comment.
The formula 2 * MY_MIN(...) used for length calculation may result in a value that is too small for very short inputs, potentially leading to truncation of the JSON result.
For example, if the minimum input length is 1 (e.g., a single-character string 'a'), the calculated length would be 2 * 1 = 2. However, the resulting JSON array ["a"] requires 5 characters. While this logic was moved from prepare_json_and_create_hash, moving it to fix_length_and_dec is a good opportunity to ensure a safer upper bound (e.g., MY_MIN(...) * 2 + 2 or a minimum floor) to avoid invalid JSON output due to truncation.
MDEV-37864 added a length setting to the function Item_func_json_array_intersect::prepare_json_and_create_hash however some paths of Item_func_json_array_intersect::fix_length_and_dec never reach this function. Leave the length calculation the same but place in the ::fix_length_and_dec function. Collation is based on the arg[0], but potentially after they are swapped. Corrects cursor protocol for the test case added in MDEV-36808.
MDEV-37864 added a length setting to the function
Item_func_json_array_intersect::prepare_json_and_create_hash however some paths of Item_func_json_array_intersect::fix_length_and_dec never reach this function.
Leave the length calculation the same but place in the ::fix_length_and_dec function.
Collation is based on the arg[0], but potentially after they are swapped.
Corrects cursor protocol for the test case added in MDEV-36808.