From 4e5b771e980edfdad5c5414aa62c81d409d585a4 Mon Sep 17 00:00:00 2001 From: Rucha Deodhar Date: Tue, 2 May 2023 16:34:07 +0530 Subject: [PATCH] MDEV-30677: Incorrect result for "SELECT JSON_SCHEMA_VALID('{}', NULL)" Analysis: null_value is not set if any one of the arguments is NULL. So it returns 1. Fix: when either argument is NULL, set null_value to true, so that null can be returned --- mysql-test/main/func_json.result | 12 ++++++++++++ mysql-test/main/func_json.test | 7 +++++++ sql/item_jsonfunc.cc | 19 +++++++++++++------ sql/item_jsonfunc.h | 1 + 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/mysql-test/main/func_json.result b/mysql-test/main/func_json.result index afff2c58421..bdda00e256d 100644 --- a/mysql-test/main/func_json.result +++ b/mysql-test/main/func_json.result @@ -4704,4 +4704,16 @@ JSON_SCHEMA_VALID(json_object(), repeat('[', 10000000)) 0 Warnings: Warning 4040 Limit of 32 on JSON nested structures depth is reached in argument 2 to function 'json_schema_valid' at position 32 +# +# MDEV-30677: Incorrect result for "SELECT JSON_SCHEMA_VALID('{}', NULL)" +# +SELECT JSON_SCHEMA_VALID('{}', NULL); +JSON_SCHEMA_VALID('{}', NULL) +NULL +SELECT JSON_SCHEMA_VALID(NULL, '{}'); +JSON_SCHEMA_VALID(NULL, '{}') +NULL +SELECT JSON_SCHEMA_VALID(NULL, NULL); +JSON_SCHEMA_VALID(NULL, NULL) +NULL # End of 11.1 test diff --git a/mysql-test/main/func_json.test b/mysql-test/main/func_json.test index e0da9819785..7fc2f05a57a 100644 --- a/mysql-test/main/func_json.test +++ b/mysql-test/main/func_json.test @@ -3586,4 +3586,11 @@ SELECT JSON_SCHEMA_VALID(repeat('[', 100000), json_object()); SELECT JSON_SCHEMA_VALID(json_object(), repeat('[', 10000000)); +--echo # +--echo # MDEV-30677: Incorrect result for "SELECT JSON_SCHEMA_VALID('{}', NULL)" +--echo # +SELECT JSON_SCHEMA_VALID('{}', NULL); +SELECT JSON_SCHEMA_VALID(NULL, '{}'); +SELECT JSON_SCHEMA_VALID(NULL, NULL); + --echo # End of 11.1 test diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc index 903776ddebf..fa0a8cbec7f 100644 --- a/sql/item_jsonfunc.cc +++ b/sql/item_jsonfunc.cc @@ -4729,18 +4729,22 @@ longlong Item_func_json_schema_valid::val_int() int is_valid= 1; if (!schema_parsed) + { + null_value= 1; + return 0; + } + + val= args[1]->val_json(&tmp_val); + + if (!val) { null_value= 1; return 0; } + null_value= 0; - val= args[1]->val_json(&tmp_val); - - if (!val || !val->length()) - { - null_value= 0; + if (!val->length()) return 1; - } json_scan_start(&ve, val->charset(), (const uchar *) val->ptr(), (const uchar *) val->end()); @@ -4799,7 +4803,10 @@ bool Item_func_json_schema_valid::fix_length_and_dec(THD *thd) String *js= args[0]->val_json(&tmp_js); if ((null_value= args[0]->null_value)) + { + null_value= 1; return 0; + } json_scan_start(&je, js->charset(), (const uchar *) js->ptr(), (const uchar *) js->ptr() + js->length()); if (!create_object_and_handle_keyword(thd, &je, &keyword_list, diff --git a/sql/item_jsonfunc.h b/sql/item_jsonfunc.h index 4e857432b04..b352391c083 100644 --- a/sql/item_jsonfunc.h +++ b/sql/item_jsonfunc.h @@ -807,6 +807,7 @@ public: { val= NULL; schema_parsed= false; + set_maybe_null(); } LEX_CSTRING func_name_cstring() const override {