-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
MDEV-40180 Cache constant args in JSON_EQUALS and JSON_OVERLAPS #5455
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
base: 10.11
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -622,48 +622,107 @@ bool Item_func_json_equals::val_bool() | |
| longlong result= 0; | ||
| int arg_num= 0; | ||
| String a_tmp, b_tmp; | ||
| String *a= nullptr, *b= nullptr; | ||
| THD *thd; | ||
| json_engine_t je; | ||
| bool a_const= args[0]->const_item(), b_const= args[1]->const_item(); | ||
|
|
||
| if ((null_value= args[0]->null_value || args[1]->null_value)) | ||
| return 1; | ||
|
|
||
| String *a= args[0]->val_json(&a_tmp); | ||
| if ((null_value= a == nullptr)) | ||
| return 1; | ||
| String *b= args[1]->val_json(&b_tmp); | ||
| if ((null_value= b == nullptr)) | ||
| return 1; | ||
|
|
||
| DYNAMIC_STRING a_res; | ||
| if (init_dynamic_string(&a_res, NULL, 0, 0)) | ||
| { | ||
| null_value= 1; | ||
| return 1; | ||
| } | ||
| thd= current_thd; | ||
| JSON_DO_PAUSE_EXECUTION(thd, 0.0002); | ||
| je.killed_ptr= (uint32_t *) &thd->killed; | ||
|
|
||
| DYNAMIC_STRING b_res; | ||
| if (init_dynamic_string(&b_res, NULL, 0, 0)) | ||
| /* Process First Argument */ | ||
| if (a_const && a_parsed) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| { | ||
| dynstr_free(&a_res); | ||
| null_value= 1; | ||
| return 1; | ||
| if(a_null) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. space after |
||
| goto return_null; | ||
| } | ||
| else | ||
| { | ||
| a= args[0]->val_json(&a_tmp); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if (!a) | ||
| goto set_a_null; | ||
|
|
||
| if (!cached_a.str) | ||
| { | ||
| if (init_dynamic_string(&cached_a, NULL, 0, 0)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As this is going to contain a normalized form of The failure of this is a memory allocation failure. While it hasn't been done well in other examples in this file yet, the response is: As a pushed error it shouldn't return here |
||
| goto set_a_null; | ||
| } | ||
| else | ||
| { | ||
| cached_a.length= 0; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a comment in code here about resetting string for next value. |
||
| } | ||
|
|
||
| thd= current_thd; | ||
| JSON_DO_PAUSE_EXECUTION(thd, 0.0002); | ||
| je.killed_ptr= (uint32_t *) &thd->killed; | ||
| if (json_normalize_engine(&je, &cached_a, a->ptr(), a->length(), a->charset())) | ||
| { | ||
| goto set_a_null; | ||
| } | ||
|
|
||
| if (json_normalize_engine(&je, &a_res, a->ptr(), a->length(), a->charset())) | ||
| goto return_null; | ||
| if (a_const) | ||
| { | ||
| a_null= false; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think can set |
||
| a_parsed= true; | ||
| } | ||
| } | ||
|
|
||
| arg_num++; | ||
| if (json_normalize_engine(&je, &b_res, b->ptr(), b->length(), b->charset())) | ||
| goto return_null; | ||
|
|
||
| result= strcmp(a_res.str, b_res.str) ? 0 : 1; | ||
| /* Process Second Argument */ | ||
| if (b_const && b_parsed) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same comments on first arg parsing apply here too. |
||
| { | ||
| if(b_null) | ||
| goto return_null; | ||
| } | ||
| else | ||
| { | ||
| b= args[1]->val_json(&b_tmp); | ||
| if (!b) | ||
| goto set_b_null; | ||
|
|
||
| if (!cached_b.str) | ||
| { | ||
| if (init_dynamic_string(&cached_b, NULL, 0, 0)) | ||
| goto set_b_null; | ||
| } | ||
| else | ||
| { | ||
| cached_b.length= 0; | ||
| } | ||
|
|
||
| if (json_normalize_engine(&je, &cached_b, b->ptr(), b->length(), b->charset())) | ||
| { | ||
| goto set_b_null; | ||
| } | ||
|
|
||
| if (b_const) | ||
| { | ||
| b_null= false; | ||
| b_parsed= true; | ||
| } | ||
| } | ||
|
|
||
| result= strcmp(cached_a.str, cached_b.str) ? 0 : 1; | ||
| goto end; | ||
|
|
||
| set_a_null: | ||
| if (a_const) | ||
| { | ||
| a_null= true; | ||
| a_parsed= true; | ||
| } | ||
| goto return_null; | ||
|
|
||
| set_b_null: | ||
| if (b_const) | ||
| { | ||
| b_null= true; | ||
| b_parsed= true; | ||
| } | ||
|
|
||
| return_null: | ||
| null_value= 1; | ||
|
|
||
|
|
@@ -675,8 +734,6 @@ bool Item_func_json_equals::val_bool() | |
| a= b; | ||
| report_json_error(a, &je, arg_num); | ||
| } | ||
| dynstr_free(&b_res); | ||
| dynstr_free(&a_res); | ||
| return result; | ||
| } | ||
|
|
||
|
|
@@ -5046,17 +5103,34 @@ bool Item_func_json_overlaps::val_bool() | |
| json_engine_t je, ve; | ||
| int result; | ||
| THD *thd; | ||
| bool b_const= args[1]->const_item(); | ||
|
|
||
| if ((null_value= (js == nullptr) || args[0]->null_value)) | ||
| return 0; | ||
|
|
||
| thd= current_thd; | ||
| JSON_DO_PAUSE_EXECUTION(thd, 0.0002); | ||
|
|
||
| if (!a2_parsed) | ||
| if (b_const && a2_parsed) | ||
| { | ||
| val= args[1]->val_json(&tmp_val); | ||
| a2_parsed= a2_constant; | ||
| val= (cached_val.is_alloced() || cached_val.length()) ? &cached_val : 0; | ||
| } | ||
| else | ||
| { | ||
| String *v= args[1]->val_json(&tmp_val); | ||
| if (v) | ||
| { | ||
| cached_val.copy(v->ptr(), v->length(), v->charset()); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. still copying here. (incomplete) |
||
| val= &cached_val; | ||
| } | ||
| else | ||
| { | ||
| cached_val.length(0); | ||
| val= 0; | ||
| } | ||
|
|
||
| if (b_const) | ||
| a2_parsed= true; | ||
| } | ||
|
|
||
| if (val == 0) | ||
|
|
@@ -5092,8 +5166,6 @@ bool Item_func_json_overlaps::val_bool() | |
|
|
||
| bool Item_func_json_overlaps::fix_length_and_dec(THD *thd) | ||
| { | ||
| a2_constant= args[1]->const_item(); | ||
| a2_parsed= FALSE; | ||
| set_maybe_null(); | ||
|
|
||
| return Item_bool_func::fix_length_and_dec(thd); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This goes beyond the scope of the preliminary review, so consider it optional. But I wanted to air it out anyway:
I believe that arguments to these functions broadly fall into the following categories:
This is very similar to the regular expressions case IMHO.
In case 1 you know that a constant is a constant and can pre-parse at "compile time" (e.g. at fix_length_and_dec as Item_func_regexp_match() does).
Case 2 should be optimized differently to start with: index or something. Failing that, It is highly unlikely IMHO that arguments in this case would be grouped together (sorted on one of the arguments). So you'd end up just maintaining the cache and looking for a hit. And this might even make things worse speed-wise. And shouldn't be a target of optimization. If such an optimization is to be implemented, it needs to be implemented as a source transformation of the arguments during query compilation, e.g. using some specialized form of Item_cache that can hold the parsed JSON, if there isn't one already.
Case 3 is so unlikely that I believe that detecting and optimizing that is just hard.
Lazy caching at runtime is IMHO just making things worse: for actual constants you just add extra instructions per hit (is this the same string as what's cached, if yes, then reuse; if not cache) and this will make things worse compared to the approach taken by the regexp functions.
Thus I strongly believe that caching in fix_length_and_dec() is the right phase to do this. But it needs to be done correctly wrt prepared statements. You're saying there's a bug in these. Maybe fix that instead?