summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
authorAlexander Barkov <bar@mariadb.com>2020-06-22 15:43:53 +0400
committerAlexander Barkov <bar@mariadb.com>2020-06-22 15:43:53 +0400
commit30903c37432e518e09b87610f6edd0a1d039669c (patch)
treed62ca4a19569f0cec9a7cce80aec7ecdd2a9d5f0 /sql
parent009ef36d9ae01a44ddf5e1af35f54740838e1e70 (diff)
downloadmariadb-git-30903c37432e518e09b87610f6edd0a1d039669c.tar.gz
MDEV-22976 CAST(JSON_EXTRACT() AS DECIMAL) does not handle boolean values
Item_func_json_extract did not implement val_decimal(), so CAST(JSON_EXTRACT('{"x":true}', '$.x') AS DECIMAL) erroneously returned 0 with a warning because of convertion from the string "true" to decimal. Implementing val_decimal(), so boolean values are correctly handled.
Diffstat (limited to 'sql')
-rw-r--r--sql/item_jsonfunc.cc35
-rw-r--r--sql/item_jsonfunc.h1
2 files changed, 36 insertions, 0 deletions
diff --git a/sql/item_jsonfunc.cc b/sql/item_jsonfunc.cc
index 4db4c563d97..b3c8366907a 100644
--- a/sql/item_jsonfunc.cc
+++ b/sql/item_jsonfunc.cc
@@ -962,6 +962,41 @@ double Item_func_json_extract::val_real()
}
+my_decimal *Item_func_json_extract::val_decimal(my_decimal *to)
+{
+ json_value_types type;
+ char *value;
+ int value_len;
+
+ if (read_json(NULL, &type, &value, &value_len) != NULL)
+ {
+ switch (type)
+ {
+ case JSON_VALUE_STRING:
+ case JSON_VALUE_NUMBER:
+ {
+ my_decimal *res= decimal_from_string_with_check(to, collation.collation,
+ value,
+ value + value_len);
+ null_value= res == NULL;
+ return res;
+ }
+ case JSON_VALUE_TRUE:
+ int2my_decimal(E_DEC_FATAL_ERROR, 1, false/*unsigned_flag*/, to);
+ return to;
+ case JSON_VALUE_OBJECT:
+ case JSON_VALUE_ARRAY:
+ case JSON_VALUE_FALSE:
+ case JSON_VALUE_NULL:
+ break;
+ };
+ }
+ int2my_decimal(E_DEC_FATAL_ERROR, 0, false/*unsigned_flag*/, to);
+ return to;
+}
+
+
+
bool Item_func_json_contains::fix_length_and_dec()
{
a2_constant= args[1]->const_item();
diff --git a/sql/item_jsonfunc.h b/sql/item_jsonfunc.h
index d7f804a1b60..0277cb15154 100644
--- a/sql/item_jsonfunc.h
+++ b/sql/item_jsonfunc.h
@@ -171,6 +171,7 @@ public:
String *val_str(String *);
longlong val_int();
double val_real();
+ my_decimal *val_decimal(my_decimal *);
uint get_n_paths() const { return arg_count - 1; }
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_func_json_extract>(thd, mem_root, this); }