Commit 6f6d0531 authored by Sergei Golubchik's avatar Sergei Golubchik

MDEV-11439 No data type JSON, but CAST(something AS JSON) pretends to work

Use JSON_COMPACT(X) instead of CAST(X AS JSON).
Bonus - X is validated to be a valid JSON.
Fix a typo in the error message.
parent e0fa2ce4
......@@ -402,14 +402,14 @@ json_object("a", json_object("b", "abcd"))
select json_object("a", '{"b": "abcd"}');
json_object("a", '{"b": "abcd"}')
{"a": "{\"b\": \"abcd\"}"}
select json_object("a", cast('{"b": "abcd"}' as json));
json_object("a", cast('{"b": "abcd"}' as json))
select json_object("a", json_compact('{"b": "abcd"}'));
json_object("a", json_compact('{"b": "abcd"}'))
{"a": {"b": "abcd"}}
select cast(NULL AS JSON);
cast(NULL AS JSON)
select json_compact(NULL);
json_compact(NULL)
NULL
select json_depth(cast(NULL as JSON));
json_depth(cast(NULL as JSON))
select json_depth(json_compact(NULL));
json_depth(json_compact(NULL))
NULL
select json_depth('[[], {}]');
json_depth('[[], {}]')
......
This diff is collapsed.
......@@ -161,10 +161,10 @@ select json_unquote('abc');
select json_object("a", json_object("b", "abcd"));
select json_object("a", '{"b": "abcd"}');
select json_object("a", cast('{"b": "abcd"}' as json));
select json_object("a", json_compact('{"b": "abcd"}'));
select cast(NULL AS JSON);
select json_depth(cast(NULL as JSON));
select json_compact(NULL);
select json_depth(json_compact(NULL));
select json_depth('[[], {}]');
select json_depth('[[[1,2,3],"s"], {}, []]');
select json_depth('[10, {"a": 20}]');
......
......@@ -1095,6 +1095,8 @@ class Item: public Value_source,
Returns the val_str() value converted to the given character set.
*/
String *val_str(String *str, String *converter, CHARSET_INFO *to);
virtual String *val_json(String *str) { return val_str(str); }
/*
Return decimal representation of item with fixed point.
......
......@@ -7228,9 +7228,6 @@ create_func_cast(THD *thd, Item *a, Cast_target cast_type,
res= new (thd->mem_root) Item_char_typecast(thd, a, len, real_cs);
break;
}
case ITEM_CAST_JSON:
res= new (thd->mem_root) Item_json_typecast(thd, a);
break;
default:
{
DBUG_ASSERT(0);
......
......@@ -2221,7 +2221,7 @@ enum Cast_target
{
ITEM_CAST_BINARY, ITEM_CAST_SIGNED_INT, ITEM_CAST_UNSIGNED_INT,
ITEM_CAST_DATE, ITEM_CAST_TIME, ITEM_CAST_DATETIME, ITEM_CAST_CHAR,
ITEM_CAST_DECIMAL, ITEM_CAST_DOUBLE, ITEM_CAST_JSON
ITEM_CAST_DECIMAL, ITEM_CAST_DOUBLE
};
......
......@@ -373,7 +373,7 @@ static int path_setup_nwc(json_path_t *p, CHARSET_INFO *i_cs,
longlong Item_func_json_valid::val_int()
{
String *js= args[0]->val_str(&tmp_value);
String *js= args[0]->val_json(&tmp_value);
json_engine_t je;
if ((null_value= args[0]->null_value))
......@@ -401,7 +401,7 @@ longlong Item_func_json_exists::val_int()
json_engine_t je;
uint array_counters[JSON_DEPTH_LIMIT];
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
if (!path.parsed)
{
......@@ -454,7 +454,7 @@ void Item_func_json_value::fix_length_and_dec()
String *Item_func_json_value::val_str(String *str)
{
json_engine_t je;
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
int error= 0;
uint array_counters[JSON_DEPTH_LIMIT];
......@@ -587,7 +587,7 @@ void Item_func_json_unquote::fix_length_and_dec()
String *Item_func_json_unquote::val_str(String *str)
{
String *js= args[0]->val_str(&tmp_s);
String *js= args[0]->val_json(&tmp_s);
json_engine_t je;
int c_len;
......@@ -712,7 +712,7 @@ static bool path_ok(const json_path_with_flags *paths_list, int n_paths,
String *Item_func_json_extract::val_str(String *str)
{
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
json_engine_t je, sav_je;
json_path_t p;
const uchar *value;
......@@ -817,7 +817,7 @@ String *Item_func_json_extract::val_str(String *str)
longlong Item_func_json_extract::val_int()
{
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
json_engine_t je;
uint n_arg;
uint array_counters[JSON_DEPTH_LIMIT];
......@@ -1040,7 +1040,7 @@ static int check_contains(json_engine_t *js, json_engine_t *value)
longlong Item_func_json_contains::val_int()
{
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
json_engine_t je, ve;
int result;
......@@ -1049,7 +1049,7 @@ longlong Item_func_json_contains::val_int()
if (!a2_parsed)
{
val= args[1]->val_str(&tmp_val);
val= args[1]->val_json(&tmp_val);
a2_parsed= a2_constant;
}
......@@ -1179,7 +1179,7 @@ static int parse_one_or_all(const Item_func *f, Item *ooa_arg,
#ifdef DUMMY
longlong Item_func_json_contains_path::val_int()
{
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
json_engine_t je;
uint n_arg;
longlong result;
......@@ -1247,7 +1247,7 @@ longlong Item_func_json_contains_path::val_int()
longlong Item_func_json_contains_path::val_int()
{
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
json_engine_t je;
uint n_arg;
longlong result;
......@@ -1351,7 +1351,7 @@ static int append_json_value(String *str, Item *item, String *tmp_val)
return str->append(t_f, t_f_len);
}
{
String *sv= item->val_str(tmp_val);
String *sv= item->val_json(tmp_val);
if (item->null_value)
goto append_null;
if (item->is_json_type())
......@@ -1461,7 +1461,7 @@ void Item_func_json_array_append::fix_length_and_dec()
String *Item_func_json_array_append::val_str(String *str)
{
json_engine_t je;
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
uint n_arg, n_path, str_rest_len;
const uchar *ar_end;
......@@ -1588,7 +1588,7 @@ String *Item_func_json_array_append::val_str(String *str)
String *Item_func_json_array_insert::val_str(String *str)
{
json_engine_t je;
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
uint n_arg, n_path;
DBUG_ASSERT(fixed == 1);
......@@ -1959,7 +1959,7 @@ String *Item_func_json_merge::val_str(String *str)
{
DBUG_ASSERT(fixed == 1);
json_engine_t je1, je2;
String *js1= args[0]->val_str(&tmp_js1), *js2;
String *js1= args[0]->val_json(&tmp_js1), *js2;
uint n_arg;
if (args[0]->null_value)
......@@ -1970,7 +1970,7 @@ String *Item_func_json_merge::val_str(String *str)
str->set_charset(js1->charset());
str->length(0);
js2= args[n_arg]->val_str(&tmp_js2);
js2= args[n_arg]->val_json(&tmp_js2);
if (args[n_arg]->null_value)
goto null_return;
......@@ -2028,7 +2028,7 @@ void Item_func_json_length::fix_length_and_dec()
longlong Item_func_json_length::val_int()
{
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
json_engine_t je;
uint length= 0;
uint array_counters[JSON_DEPTH_LIMIT];
......@@ -2105,7 +2105,7 @@ longlong Item_func_json_length::val_int()
longlong Item_func_json_depth::val_int()
{
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
json_engine_t je;
uint depth= 0, c_depth= 0;
bool inc_depth= TRUE;
......@@ -2164,7 +2164,7 @@ void Item_func_json_type::fix_length_and_dec()
String *Item_func_json_type::val_str(String *str)
{
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
json_engine_t je;
const char *type;
......@@ -2232,7 +2232,7 @@ void Item_func_json_insert::fix_length_and_dec()
String *Item_func_json_insert::val_str(String *str)
{
json_engine_t je;
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
uint n_arg, n_path;
json_string_t key_name;
......@@ -2478,7 +2478,7 @@ void Item_func_json_remove::fix_length_and_dec()
String *Item_func_json_remove::val_str(String *str)
{
json_engine_t je;
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
uint n_arg, n_path;
json_string_t key_name;
......@@ -2661,7 +2661,7 @@ void Item_func_json_keys::fix_length_and_dec()
String *Item_func_json_keys::val_str(String *str)
{
json_engine_t je;
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
uint n_keys= 0;
uint array_counters[JSON_DEPTH_LIMIT];
......@@ -2837,7 +2837,7 @@ static int append_json_path(String *str, const json_path_t *p)
String *Item_func_json_search::val_str(String *str)
{
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
String *s_str= args[2]->val_str(&tmp_js);
json_engine_t je;
json_path_t p, sav_path;
......@@ -2934,21 +2934,6 @@ String *Item_func_json_search::val_str(String *str)
}
void Item_json_typecast::fix_length_and_dec()
{
maybe_null= args[0]->maybe_null;
max_length= args[0]->max_length;
}
String *Item_json_typecast::val_str(String *str)
{
String *vs= args[0]->val_str(str);
null_value= args[0]->null_value;
return vs;
}
const char *Item_func_json_format::func_name() const
{
switch (fmt)
......@@ -2976,7 +2961,7 @@ void Item_func_json_format::fix_length_and_dec()
String *Item_func_json_format::val_str(String *str)
{
String *js= args[0]->val_str(&tmp_js);
String *js= args[0]->val_json(&tmp_js);
json_engine_t je;
int tab_size= 4;
......@@ -3016,3 +3001,12 @@ String *Item_func_json_format::val_str(String *str)
}
String *Item_func_json_format::val_json(String *str)
{
String *js= args[0]->val_json(&tmp_js);
if ((null_value= args[0]->null_value))
return 0;
return js;
}
......@@ -414,19 +414,6 @@ class Item_func_json_search: public Item_json_str_multipath
};
class Item_json_typecast: public Item_str_func
{
public:
Item_json_typecast(THD *thd, Item *a): Item_str_func(thd, a) {}
const char *func_name() const { return "cast_as_json"; }
bool is_json_type() { return true; }
void fix_length_and_dec();
String *val_str(String *str);
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_json_typecast>(thd, mem_root, this); }
};
class Item_func_json_format: public Item_str_func
{
public:
......@@ -449,6 +436,8 @@ class Item_func_json_format: public Item_str_func
const char *func_name() const;
void fix_length_and_dec();
String *val_str(String *str);
String *val_json(String *str);
bool is_json_type() { return true; }
Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_func_json_format>(thd, mem_root, this); }
};
......
......@@ -7417,7 +7417,7 @@ ER_BINLOG_UNCOMPRESS_ERROR
ER_JSON_BAD_CHR
eng "Broken JSON string in argument %d to function '%s' at position %d"
ER_JSON_NOT_JSON_CHR
eng "Character disallowd in JSON in argument %d to function '%s' at position %d"
eng "Character disallowed in JSON in argument %d to function '%s' at position %d"
ER_JSON_EOS
eng "Unexpected end of JSON text in argument %d to function '%s'"
ER_JSON_SYNTAX
......
......@@ -10714,7 +10714,6 @@ cast_type:
}
| cast_type_numeric { $$= $1; Lex->charset= NULL; }
| cast_type_temporal { $$= $1; Lex->charset= NULL; }
| JSON_SYM { $$.set(ITEM_CAST_JSON); }
;
cast_type_numeric:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment