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")) ...@@ -402,14 +402,14 @@ json_object("a", json_object("b", "abcd"))
select json_object("a", '{"b": "abcd"}'); select json_object("a", '{"b": "abcd"}');
json_object("a", '{"b": "abcd"}') json_object("a", '{"b": "abcd"}')
{"a": "{\"b\": \"abcd\"}"} {"a": "{\"b\": \"abcd\"}"}
select json_object("a", cast('{"b": "abcd"}' as json)); select json_object("a", json_compact('{"b": "abcd"}'));
json_object("a", cast('{"b": "abcd"}' as json)) json_object("a", json_compact('{"b": "abcd"}'))
{"a": {"b": "abcd"}} {"a": {"b": "abcd"}}
select cast(NULL AS JSON); select json_compact(NULL);
cast(NULL AS JSON) json_compact(NULL)
NULL NULL
select json_depth(cast(NULL as JSON)); select json_depth(json_compact(NULL));
json_depth(cast(NULL as JSON)) json_depth(json_compact(NULL))
NULL NULL
select json_depth('[[], {}]'); select json_depth('[[], {}]');
json_depth('[[], {}]') json_depth('[[], {}]')
......
...@@ -7,21 +7,21 @@ ...@@ -7,21 +7,21 @@
# Some extra checks for comparisons between positive and negative zero. # Some extra checks for comparisons between positive and negative zero.
# All should be equal. # All should be equal.
SELECT CAST(0.0e0 AS JSON) = -0.0e0; SELECT JSON_COMPACT(0.0e0) = -0.0e0;
SELECT CAST(CAST(0 AS DECIMAL) AS JSON) = CAST(-0.0e0 AS DECIMAL); SELECT JSON_COMPACT(CAST(0 AS DECIMAL)) = CAST(-0.0e0 AS DECIMAL);
SELECT CAST(0.0e0 AS JSON) = CAST(-0.0e0 AS DECIMAL); SELECT JSON_COMPACT(0.0e0) = CAST(-0.0e0 AS DECIMAL);
SELECT CAST(CAST(0 AS DECIMAL) AS JSON) = -0.0e0; SELECT JSON_COMPACT(CAST(0 AS DECIMAL)) = -0.0e0;
SELECT CAST(CAST(0 AS SIGNED) AS JSON) = -0.0e0; SELECT JSON_COMPACT(CAST(0 AS SIGNED)) = -0.0e0;
SELECT CAST(CAST(0 AS SIGNED) AS JSON) = CAST(-0.0e0 AS DECIMAL); SELECT JSON_COMPACT(CAST(0 AS SIGNED)) = CAST(-0.0e0 AS DECIMAL);
SELECT CAST(CAST(0 AS UNSIGNED) AS JSON) = -0.0e0; SELECT JSON_COMPACT(CAST(0 AS UNSIGNED)) = -0.0e0;
SELECT CAST(CAST(0 AS UNSIGNED) AS JSON) = CAST(-0.0e0 AS DECIMAL); SELECT JSON_COMPACT(CAST(0 AS UNSIGNED)) = CAST(-0.0e0 AS DECIMAL);
# Test that CAST string argument isn't treated as ANY_JSON_ATOM # Test that CAST string argument isn't treated as ANY_JSON_ATOM
# in that a MySQL string needs to be parsed to JSON here; it is not # in that a MySQL string needs to be parsed to JSON here; it is not
# auto-converted to a JSON string as in ANY_JSON_ATOM contexts. # auto-converted to a JSON string as in ANY_JSON_ATOM contexts.
select cast('"abc"' as json); select json_compact('"abc"');
--echo error ER_INVALID_JSON_TEXT_IN_PARAM --echo error ER_INVALID_JSON_TEXT_IN_PARAM
select cast('abc' as json); select json_compact('abc');
--echo --echo
--echo # String literal - valid JSON --echo # String literal - valid JSON
...@@ -50,11 +50,11 @@ set names 'utf8'; ...@@ -50,11 +50,11 @@ set names 'utf8';
--echo --echo
--echo # Json expression --echo # Json expression
select JSON_VALID(cast('[123]' as JSON )); select JSON_VALID(json_compact('[123]'));
--echo --echo
--echo # Json expression NULL --echo # Json expression NULL
select JSON_VALID(cast(NULL as JSON )); select JSON_VALID(json_compact(NULL));
--echo --echo
--echo # Bare NULL --echo # Bare NULL
...@@ -80,7 +80,7 @@ select JSON_VALID( CAST('2015-01-15' AS DATE) ); ...@@ -80,7 +80,7 @@ select JSON_VALID( CAST('2015-01-15' AS DATE) );
--echo # The date string doesn't parse as JSON text, so wrong: --echo # The date string doesn't parse as JSON text, so wrong:
select JSON_VALID( CAST(CAST('2015-01-15' AS DATE) as CHAR CHARACTER SET 'utf8') ); select JSON_VALID( CAST(CAST('2015-01-15' AS DATE) as CHAR CHARACTER SET 'utf8') );
--echo # OK, though: --echo # OK, though:
select JSON_VALID( CAST(CURDATE() as JSON) ); select JSON_VALID( json_compact(CURDATE()) );
--echo --echo
--echo # Function result - NULL --echo # Function result - NULL
...@@ -299,14 +299,14 @@ SELECT JSON_LENGTH ...@@ -299,14 +299,14 @@ SELECT JSON_LENGTH
--echo # Test of JSON_DEPTH function. --echo # Test of JSON_DEPTH function.
--echo # ---------------------------------------------------------------------- --echo # ----------------------------------------------------------------------
select json_depth(null); select json_depth(null);
select json_depth(cast(null as json)); select json_depth(json_compact(null));
#select i, json_depth(j) from t1; #select i, json_depth(j) from t1;
select json_depth(cast('[]' as json)), select json_depth(json_compact('[]')),
json_depth(cast('{}' as json)), json_depth(json_compact('{}')),
json_depth(cast('null' as json)), json_depth(json_compact('null')),
json_depth(json_quote('foo')); json_depth(json_quote('foo'));
select json_depth(cast('[[2], 3, [[[4]]]]' as json)); select json_depth(json_compact('[[2], 3, [[[4]]]]'));
select json_depth(cast('{"a": {"a1": [3]}, "b": {"b1": {"c": {"d": [5]}}}}' as json)); select json_depth(json_compact('{"a": {"a1": [3]}, "b": {"b1": {"c": {"d": [5]}}}}'));
# examples from the wl7909 spec # examples from the wl7909 spec
# returns 1 # returns 1
...@@ -325,7 +325,7 @@ SELECT JSON_DEPTH ...@@ -325,7 +325,7 @@ SELECT JSON_DEPTH
SELECT JSON_DEPTH( '"abc"' ); SELECT JSON_DEPTH( '"abc"' );
# returns 1 # returns 1
SELECT JSON_DEPTH( CAST( '"abc"' AS JSON ) ); SELECT JSON_DEPTH( json_compact( '"abc"') );
--echo error ER_INVALID_TYPE_FOR_JSON --echo error ER_INVALID_TYPE_FOR_JSON
SELECT JSON_DEPTH( 1 ); SELECT JSON_DEPTH( 1 );
...@@ -334,7 +334,7 @@ SELECT JSON_DEPTH( 1 ); ...@@ -334,7 +334,7 @@ SELECT JSON_DEPTH( 1 );
SELECT JSON_DEPTH( 'abc' ); SELECT JSON_DEPTH( 'abc' );
# returns 1 # returns 1
SELECT JSON_DEPTH( CAST( 1 AS JSON ) ); SELECT JSON_DEPTH( json_compact( 1) );
# returns 2 # returns 2
SELECT JSON_DEPTH SELECT JSON_DEPTH
...@@ -608,93 +608,93 @@ select json_type('3.14'); ...@@ -608,93 +608,93 @@ select json_type('3.14');
select json_type(CAST(CAST('2015-01-15' AS DATE) as CHAR CHARACTER SET 'utf8')); select json_type(CAST(CAST('2015-01-15' AS DATE) as CHAR CHARACTER SET 'utf8'));
--echo # ---------------------------------------------------------------------- --echo # ----------------------------------------------------------------------
--echo # Test of CAST(literal AS JSON) --echo # Test of json_compact(literal)
--echo # ---------------------------------------------------------------------- --echo # ----------------------------------------------------------------------
select json_type(cast(cast('2014-11-25 18:00' as datetime) as json)); select json_type(json_compact(cast('2014-11-25 18:00' as datetime)));
select json_type(cast(cast('2014-11-25' as date) as json)); select json_type(json_compact(cast('2014-11-25' as date)));
select json_type(cast(cast('18:00:59' as time) as json)); select json_type(json_compact(cast('18:00:59' as time)));
# select json_type(cast(cast('2014-11-25 18:00' as timestamp) as json)); -- cast target type not supported # select json_type(json_compact(cast('2014-11-25 18:00' as timestamp))); -- cast target type not supported
# select json_type(cast(cast('1999' as year) as json)); -- cast target type not supported # select json_type(json_compact(cast('1999' as year))); -- cast target type not supported
select json_type(cast(127 as json)); select json_type(json_compact(127));
select json_type(cast(255 as json)); select json_type(json_compact(255));
select json_type(cast(32767 as json)); select json_type(json_compact(32767));
select json_type(cast(65535 as json)); select json_type(json_compact(65535));
select json_type(cast(8388607 as json)); select json_type(json_compact(8388607));
select json_type(cast(16777215 as json)); select json_type(json_compact(16777215));
select json_type(cast(2147483647 as json)); select json_type(json_compact(2147483647));
select json_type(cast(4294967295 as json)); select json_type(json_compact(4294967295));
select json_type(cast(9223372036854775807 as json)); select json_type(json_compact(9223372036854775807));
select json_type(cast(18446744073709551615 as json)); select json_type(json_compact(18446744073709551615));
select json_type(cast(true as json)); select json_type(json_compact(true));
select json_type(cast(b'10101' as json)); select json_type(json_compact(b'10101'));
select json_type(cast(cast(3.14 as decimal(5,2)) as json)); select json_type(json_compact(cast(3.14 as decimal(5,2))));
select json_type(cast(3.14 as json)); select json_type(json_compact(3.14));
select json_type(cast(3.14E30 as json)); select json_type(json_compact(3.14E30));
# select json_type(cast(cast(3.14 as numeral(5,2)) as json)); -- cast target type not supported # select json_type(json_compact(cast(3.14 as numeral(5,2)))); -- cast target type not supported
# select json_type(cast(cast(3.14 as double) as json)); -- cast target type not supported # select json_type(json_compact(cast(3.14 as double))); -- cast target type not supported
# select json_type(cast(cast(3.14 as float) as json)); -- cast target type not supported # select json_type(json_compact(cast(3.14 as float))); -- cast target type not supported
# select json_type(cast(cast(b'10101' as bit(10)) as json)); -- cast target type not supported # select json_type(json_compact(cast(b'10101' as bit(10)))); -- cast target type not supported
# select json_type(cast(cast('10101abcde' as blob) as json)); -- cast target type not supported # select json_type(json_compact(cast('10101abcde' as blob))); -- cast target type not supported
select json_type(cast(cast('10101abcde' as binary) as json)); select json_type(json_compact(cast('10101abcde' as binary)));
# select json_type(cast(cast('a' as enum('a','b','c')) as json)); -- cast target type not supported # select json_type(json_compact(cast('a' as enum('a','b','c')))); -- cast target type not supported
# select json_type(cast(cast('a,c' as set('a','b','c')) as json)); -- cast target type not supported # select json_type(json_compact(cast('a,c' as set('a','b','c')))); -- cast target type not supported
select json_type(cast(ST_GeomFromText('POINT(1 1)') as json)); select json_type(json_compact(ST_GeomFromText('POINT(1 1)')));
select json_type(cast(ST_GeomFromText('LINESTRING(0 0,1 1,2 2)') as json)); select json_type(json_compact(ST_GeomFromText('LINESTRING(0 0,1 1,2 2)')));
select json_type(cast(ST_GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0), select json_type(json_compact(ST_GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0),
(5 5,7 5,7 7,5 7, 5 5))') as json)); (5 5,7 5,7 7,5 7, 5 5))')));
select json_type(cast(null as json)); select json_type(json_compact(null));
select json_type(cast(null as json)) is null; # check that it is an SQL NULL select json_type(json_compact(null)) is null; # check that it is an SQL NULL
select json_type(null) is null; # is an SQL NULL select json_type(null) is null; # is an SQL NULL
# #
# same, but now show the printable value: # same, but now show the printable value:
# #
select cast(cast('2014-11-25 18:00' as datetime) as json); select json_compact(cast('2014-11-25 18:00' as datetime));
select cast(cast('2014-11-25' as date) as json); select json_compact(cast('2014-11-25' as date));
select cast(cast('18:00:59' as time) as json); select json_compact(cast('18:00:59' as time));
# select cast(cast('2014-11-25 18:00' as timestamp) as json); -- cast target type not supported # select json_compact(cast('2014-11-25 18:00' as timestamp)); -- cast target type not supported
# select cast(cast('1999' as year) as json); -- cast target type not supported # select json_compact(cast('1999' as year)); -- cast target type not supported
select cast(127 as json); select json_compact(127);
select cast(255 as json); select json_compact(255);
select cast(32767 as json); select json_compact(32767);
select cast(65535 as json); select json_compact(65535);
select cast(8388607 as json); select json_compact(8388607);
select cast(16777215 as json); select json_compact(16777215);
select cast(2147483647 as json); select json_compact(2147483647);
select cast(4294967295 as json); select json_compact(4294967295);
select cast(9223372036854775807 as json); select json_compact(9223372036854775807);
select cast(18446744073709551615 as json); select json_compact(18446744073709551615);
select cast(true as json); select json_compact(true);
select cast(b'10101' as json); select json_compact(b'10101');
select cast(cast(3.14 as decimal(5,2)) as json); select json_compact(cast(3.14 as decimal(5,2)));
select cast(3.14 as json); select json_compact(3.14);
select cast(3.14e0 as json); select json_compact(3.14e0);
# select cast(cast(3.14 as numeral(5,2)) as json); -- cast target type not supported # select json_compact(cast(3.14 as numeral(5,2))); -- cast target type not supported
# select cast(cast(3.14 as double) as json); -- cast target type not supported # select json_compact(cast(3.14 as double)); -- cast target type not supported
# select cast(cast(3.14 as float) as json); -- cast target type not supported # select json_compact(cast(3.14 as float)); -- cast target type not supported
# select cast(cast(b'10101' as bit(10) as json); -- cast target type not supported # select json_compact(cast(b'10101' as bit(10)); -- cast target type not supported
# select cast(cast('10101abcde' as blob) as json); -- cast target type not supported # select json_compact(cast('10101abcde' as blob)); -- cast target type not supported
select cast(cast('10101abcde' as binary) as json); select json_compact(cast('10101abcde' as binary));
# select cast(cast('a' as enum('a','b','c') as json); -- cast target type not supported # select json_compact(cast('a' as enum('a','b','c')); -- cast target type not supported
# select cast(cast('a,c' as set('a','b','c') as json); -- cast target type not supported # select json_compact(cast('a,c' as set('a','b','c')); -- cast target type not supported
select cast(ST_GeomFromText('POINT(1 1)') as json); select json_compact(ST_GeomFromText('POINT(1 1)'));
select cast(ST_GeomFromText('LINESTRING(0 0,1 1,2 2)') as json); select json_compact(ST_GeomFromText('LINESTRING(0 0,1 1,2 2)'));
select cast(ST_GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0), select json_compact(ST_GeomFromText('POLYGON((0 0,10 0,10 10,0 10,0 0),
(5 5,7 5,7 7,5 7, 5 5))') as json); (5 5,7 5,7 7,5 7, 5 5))'));
select cast(null as json); select json_compact(null);
select cast(null as json) is null; # check that it is an SQL NULL select json_compact(null) is null; # check that it is an SQL NULL
--echo # ---------------------------------------------------------------------- --echo # ----------------------------------------------------------------------
--echo # Test of JSON_KEYS function. --echo # Test of JSON_KEYS function.
...@@ -761,7 +761,7 @@ SELECT JSON_KEYS('{}', '$', '$'); ...@@ -761,7 +761,7 @@ SELECT JSON_KEYS('{}', '$', '$');
--echo # conversion tests. --echo # conversion tests.
--echo # ---------------------------------------------------------------------- --echo # ----------------------------------------------------------------------
select cast(json_keys('{"a": 1}') as char); select cast(json_keys('{"a": 1}') as char);
select cast(cast(1 as json) as char); select cast(json_compact(1) as char);
select cast(json_keys(NULL) as char); select cast(json_keys(NULL) as char);
#select cast(j as char) from keys1; #select cast(j as char) from keys1;
...@@ -996,8 +996,8 @@ select json_extract( '[ { "a": [3,4] }, { "b": 2 } ]', '$[0].a', '$[1].a' ) jdoc ...@@ -996,8 +996,8 @@ select json_extract( '[ { "a": [3,4] }, { "b": 2 } ]', '$[0].a', '$[1].a' ) jdoc
--echo # ---------------------------------------------------------------------- --echo # ----------------------------------------------------------------------
# NULLs # NULLs
select json_array_append(NULL, '$.b', cast(1 as json)); select json_array_append(NULL, '$.b', json_compact(1));
select json_array_append('[1,2,3]', NULL, cast(1 as json)); select json_array_append('[1,2,3]', NULL, json_compact(1));
select json_array_append('[1,2,3]', '$', NULL); select json_array_append('[1,2,3]', '$', NULL);
# wrong # args # wrong # args
...@@ -1009,25 +1009,25 @@ select json_array_append(NULL, NULL); ...@@ -1009,25 +1009,25 @@ select json_array_append(NULL, NULL);
select json_array_append(NULL, NULL, NULL, NULL); select json_array_append(NULL, NULL, NULL, NULL);
# auto-wrap # auto-wrap
SELECT JSON_ARRAY_APPEND(cast('1' as json), '$', 3); SELECT JSON_ARRAY_APPEND(json_compact('1'), '$', 3);
SELECT JSON_ARRAY_APPEND(cast('{"a": 3}' as json), '$', 3); SELECT JSON_ARRAY_APPEND(json_compact('{"a": 3}'), '$', 3);
--echo error ER_INVALID_JSON_PATH_WILDCARD --echo error ER_INVALID_JSON_PATH_WILDCARD
select json_array_append(cast('{"a": {"b": [3]}}' as json), '$**[0]', 6); select json_array_append(json_compact('{"a": {"b": [3]}}'), '$**[0]', 6);
# Examples from the specification # Examples from the specification
--echo # Auto-wrapping, since because the paths identify scalars. --echo # Auto-wrapping, since because the paths identify scalars.
--echo # should return {"a": "foo", "b": ["bar", 4], "c": ["wibble", "grape"]} --echo # should return {"a": "foo", "b": ["bar", 4], "c": ["wibble", "grape"]}
SELECT JSON_ARRAY_APPEND('{"a": "foo", "b": "bar", "c": "wibble"}', SELECT JSON_ARRAY_APPEND('{"a": "foo", "b": "bar", "c": "wibble"}',
'$.b', cast(4 as json), '$.b', json_compact(4),
'$.c', cast('"grape"' as json)); '$.c', json_compact('"grape"'));
--echo # should return {"a": "foo", "b": [1, 2, 3, 4], --echo # should return {"a": "foo", "b": [1, 2, 3, 4],
--echo # "c": ["apple", "pear", "grape"]} --echo # "c": ["apple", "pear", "grape"]}
SELECT JSON_ARRAY_APPEND('{"a" : "foo","b": [1, 2, 3], "c": ["apple", "pear"]}', SELECT JSON_ARRAY_APPEND('{"a" : "foo","b": [1, 2, 3], "c": ["apple", "pear"]}',
'$.b', cast(4 as json), '$.b', json_compact(4),
'$.c', cast('"grape"' as json)); '$.c', json_compact('"grape"'));
# without CAST: cf. not required for ANY_JSON_ATOM arguments in specification # without CAST: cf. not required for ANY_JSON_ATOM arguments in specification
SELECT JSON_ARRAY_APPEND('{"a" : "foo","b": [1, 2, 3], "c": ["apple", "pear"]}', SELECT JSON_ARRAY_APPEND('{"a" : "foo","b": [1, 2, 3], "c": ["apple", "pear"]}',
...@@ -1070,8 +1070,8 @@ select json_array_append('3', '$[0][0][0][0]', 100); ...@@ -1070,8 +1070,8 @@ select json_array_append('3', '$[0][0][0][0]', 100);
--echo # ---------------------------------------------------------------------- --echo # ----------------------------------------------------------------------
# NULLs # NULLs
select json_insert(NULL, '$.b', cast(1 as json)); select json_insert(NULL, '$.b', json_compact(1));
select json_insert('[1,2,3]', NULL, cast(1 as json)); select json_insert('[1,2,3]', NULL, json_compact(1));
select json_insert('[1,2,3]', '$[3]', NULL); select json_insert('[1,2,3]', '$[3]', NULL);
# wrong # args # wrong # args
...@@ -1104,35 +1104,35 @@ select json_insert('{"a": 3}', '$.a[1]', 4, '$.a[2]', '5'); ...@@ -1104,35 +1104,35 @@ select json_insert('{"a": 3}', '$.a[1]', 4, '$.a[2]', '5');
# wild card & auto-wrap (scalars) # wild card & auto-wrap (scalars)
--echo error ER_INVALID_JSON_PATH_WILDCARD --echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(cast('{"a": [1], "b": 2}' as json), '$.*[1]', 6); select json_insert(json_compact('{"a": [1], "b": 2}'), '$.*[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD --echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(cast('{"a": 1, "b": 2}' as json), '$.*[1]', 6); select json_insert(json_compact('{"a": 1, "b": 2}'), '$.*[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD --echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(cast('{"a": {"b": 3}}' as json), '$.a.*[1]', 6); select json_insert(json_compact('{"a": {"b": 3}}'), '$.a.*[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD --echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(cast('{"a": {"b": [3]}}' as json), '$.a.*[1]', 6); select json_insert(json_compact('{"a": {"b": [3]}}'), '$.a.*[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD --echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(cast('{"a": {"b": 3}}' as json), '$**[1]', 6); select json_insert(json_compact('{"a": {"b": 3}}'), '$**[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD --echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(cast('{"a": {"b": [3]}}' as json), '$**[1]', 6); select json_insert(json_compact('{"a": {"b": [3]}}'), '$**[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD --echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(cast('[1]' as json), '$[*][1]', 6); select json_insert(json_compact('[1]'), '$[*][1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD --echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(cast('[1]' as json), '$**[1]', 6); select json_insert(json_compact('[1]'), '$**[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD --echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(cast('[1, [2], 3]' as json), '$[*][1]', 6); select json_insert(json_compact('[1, [2], 3]'), '$[*][1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD --echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(cast('[1, [2], 3]' as json), '$**[1]', 6); select json_insert(json_compact('[1, [2], 3]'), '$**[1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD --echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(cast('[[1]]' as json), '$[*][1]', 6); select json_insert(json_compact('[[1]]'), '$[*][1]', 6);
--echo error ER_INVALID_JSON_PATH_WILDCARD --echo error ER_INVALID_JSON_PATH_WILDCARD
select json_insert(cast('[[1]]' as json), '$**[1]', 6); select json_insert(json_compact('[[1]]'), '$**[1]', 6);
# auto-wrap object # auto-wrap object
select json_insert(cast('{"a": 3}' as json), '$[1]', 6); select json_insert(json_compact('{"a": 3}'), '$[1]', 6);
# Examples from the specification # Examples from the specification
...@@ -1270,7 +1270,7 @@ select json_insert('[]', '$[*][1]', 6); ...@@ -1270,7 +1270,7 @@ select json_insert('[]', '$[*][1]', 6);
# multiple paths, # multiple paths,
select json_array_insert( '[ 1, 2, 3 ]', '$[1]', true, '$[1]', false ); select json_array_insert( '[ 1, 2, 3 ]', '$[1]', true, '$[1]', false );
select json_array_insert( '[ 1, 2, 3 ]', '$[1]', select json_array_insert( '[ 1, 2, 3 ]', '$[1]',
cast( '[ "a", "b", "c", "d" ]' as json ), '$[1][2]', false ); json_compact( '[ "a", "b", "c", "d" ]'), '$[1][2]', false );
# test an error while evaluating the document expression # test an error while evaluating the document expression
--echo error ER_INVALID_JSON_TEXT_IN_PARAM --echo error ER_INVALID_JSON_TEXT_IN_PARAM
...@@ -1285,8 +1285,8 @@ select json_array_insert( '[ 1, 2, 3 ]', '$[1]', json_extract( '[', '$' ) ); ...@@ -1285,8 +1285,8 @@ select json_array_insert( '[ 1, 2, 3 ]', '$[1]', json_extract( '[', '$' ) );
--echo # ---------------------------------------------------------------------- --echo # ----------------------------------------------------------------------
# NULLs # NULLs
select json_set(NULL, '$.b', cast(1 as json)); select json_set(NULL, '$.b', json_compact(1));
select json_set('[1,2,3]', NULL, cast(1 as json)); select json_set('[1,2,3]', NULL, json_compact(1));
select json_set('[1,2,3]', '$[3]', NULL); select json_set('[1,2,3]', '$[3]', NULL);
# wrong # args # wrong # args
...@@ -1323,7 +1323,7 @@ select json_set('{"a": 3}', '$.a[1]', 4, '$.a[2]', '5'); ...@@ -1323,7 +1323,7 @@ select json_set('{"a": 3}', '$.a[1]', 4, '$.a[2]', '5');
# auto-wrap plus ellipsis with nested hits should give: {"a": [{"b": [3, 6]}, 6]} # auto-wrap plus ellipsis with nested hits should give: {"a": [{"b": [3, 6]}, 6]}
--echo error ER_INVALID_JSON_PATH_WILDCARD --echo error ER_INVALID_JSON_PATH_WILDCARD
select json_set(cast('{"a": {"b": [3]}}' as json), '$**[1]', 6); select json_set(json_compact('{"a": {"b": [3]}}'), '$**[1]', 6);
# Examples from the specification: Include when missing functions are # Examples from the specification: Include when missing functions are
# available. # available.
...@@ -1341,7 +1341,7 @@ select json_set(cast('{"a": {"b": [3]}}' as json), '$**[1]', 6); ...@@ -1341,7 +1341,7 @@ select json_set(cast('{"a": {"b": [3]}}' as json), '$**[1]', 6);
# # returns { "a" : "foo", "b" : [ 1, 2, 3 ], "c" : [ true, false ] } # # returns { "a" : "foo", "b" : [ 1, 2, 3 ], "c" : [ true, false ] }
# SELECT JSON_SET('{ "a" : "foo", "b" : [ 1, 2, 3 ] }', # SELECT JSON_SET('{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
# '$.c', # '$.c',
# JSON_ARRAY( CAST( 'true' AS JSON ), CAST( 'false' AS JSON ) )); # JSON_ARRAY( json_compact( 'true'), json_compact( 'false') ));
# # returns [ 1, null, null, 2 ] # # returns [ 1, null, null, 2 ]
# SELECT JSON_SET('1', '$[3]', 2); # SELECT JSON_SET('1', '$[3]', 2);
...@@ -1353,19 +1353,19 @@ SELECT JSON_SET('{ "a" : "foo"}', '$.a', ...@@ -1353,19 +1353,19 @@ SELECT JSON_SET('{ "a" : "foo"}', '$.a',
# returns { "a" : {}, "b" : [ 1, 2, 3 ] } # returns { "a" : {}, "b" : [ 1, 2, 3 ] }
select json_set('{ "a" : "foo", "b" : [ 1, 2, 3 ] }', select json_set('{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.a', '$.a',
cast('{}' as json)); json_compact('{}'));
# returns { "a" : "foo", "b" : [ 1, 2, 3 ], "c" : [ true, false ] } # returns { "a" : "foo", "b" : [ 1, 2, 3 ], "c" : [ true, false ] }
select json_set('{ "a" : "foo", "b" : [ 1, 2, 3 ] }', select json_set('{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.c', '$.c',
cast('[true, false]' as json)); json_compact('[true, false]'));
# returns [ 1, null, null, 2 ] # returns [ 1, null, null, 2 ]
select json_set('1', '$[3]', 2); select json_set('1', '$[3]', 2);
# should return { "a": { "b": false, "c": true } } # should return { "a": { "b": false, "c": true } }
select json_set('{ "a" : "foo"}', '$.a', select json_set('{ "a" : "foo"}', '$.a',
cast('{"b": false}' as json), '$.a.c', true); json_compact('{"b": false}'), '$.a.c', true);
# examples from wl7909 spec # examples from wl7909 spec
# returns {"a": {}, "b": [1, 2, 3]} # returns {"a": {}, "b": [1, 2, 3]}
...@@ -1389,7 +1389,7 @@ SELECT JSON_SET ...@@ -1389,7 +1389,7 @@ SELECT JSON_SET
( (
'{ "a" : "foo", "b" : [ 1, 2, 3 ] }', '{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
'$.c', '$.c',
JSON_ARRAY( CAST( 'true' AS JSON ), CAST( 'false' AS JSON ) ) JSON_ARRAY( json_compact( 'true'), json_compact( 'false') )
); );
# returns [1, 2] # returns [1, 2]
...@@ -1413,8 +1413,8 @@ SELECT JSON_SET ...@@ -1413,8 +1413,8 @@ SELECT JSON_SET
--echo # ---------------------------------------------------------------------- --echo # ----------------------------------------------------------------------
# NULLs # NULLs
select json_replace(NULL, '$.b', cast(1 as json)); select json_replace(NULL, '$.b', json_compact(1));
select json_replace('[1,2,3]', NULL, cast(1 as json)); select json_replace('[1,2,3]', NULL, json_compact(1));
select json_replace('[1,2,3]', '$[2]', NULL); select json_replace('[1,2,3]', '$[2]', NULL);
# wrong # args # wrong # args
...@@ -1468,8 +1468,8 @@ SELECT JSON_REPLACE('{ "a" : "foo", "b" : [ 1, 2, 3 ] }', ...@@ -1468,8 +1468,8 @@ SELECT JSON_REPLACE('{ "a" : "foo", "b" : [ 1, 2, 3 ] }',
--echo # ---------------------------------------------------------------------- --echo # ----------------------------------------------------------------------
# NULLs # NULLs
select json_array(NULL, '$.b', cast(1 as json)); select json_array(NULL, '$.b', json_compact(1));
select json_array('[1,2,3]', NULL, cast(1 as json)); select json_array('[1,2,3]', NULL, json_compact(1));
select json_array('[1,2,3]', '$[3]', NULL); select json_array('[1,2,3]', '$[3]', NULL);
...@@ -1477,7 +1477,7 @@ select json_array('[1,2,3]', '$[3]', NULL); ...@@ -1477,7 +1477,7 @@ select json_array('[1,2,3]', '$[3]', NULL);
select json_array(); select json_array();
select json_array(3.14); select json_array(3.14);
select json_array('[1,2,3]'); select json_array('[1,2,3]');
select json_array(cast('[1,2,3]' as json)); select json_array(json_compact('[1,2,3]'));
select json_array(1,2,3); select json_array(1,2,3);
select json_array(b'0', b'1', b'10'); select json_array(b'0', b'1', b'10');
...@@ -1503,8 +1503,8 @@ select json_object(); ...@@ -1503,8 +1503,8 @@ select json_object();
select json_object( 'a', null ); select json_object( 'a', null );
select json_object( 'a', 1 ); select json_object( 'a', 1 );
select json_object( 'a', 1, 'b', 'foo' ); select json_object( 'a', 1, 'b', 'foo' );
select json_object( 'a', 1, 'b', 'foo', 'c', cast( '{ "d": "wibble" }' as json ) ); select json_object( 'a', 1, 'b', 'foo', 'c', json_compact( '{ "d": "wibble" }') );
select json_object( 'a', true, 'b', false, 'c', cast( 'null' as json) ); select json_object( 'a', true, 'b', false, 'c', json_compact( 'null') );
select json_valid( json_object( '"a"', 1 ) ); select json_valid( json_object( '"a"', 1 ) );
# long key # long key
...@@ -1707,29 +1707,29 @@ SELECT JSON_UNQUOTE ...@@ -1707,29 +1707,29 @@ SELECT JSON_UNQUOTE
); );
select json_type(case (null is null) when 1 then select json_type(case (null is null) when 1 then
cast('null' as json) else json_compact('null') else
cast('[1,2,3]' as json) end); json_compact('[1,2,3]') end);
select json_type(case (null is not null) when 1 then select json_type(case (null is not null) when 1 then
cast('null' as json) else json_compact('null') else
cast('[1,2,3]' as json) end); json_compact('[1,2,3]') end);
select json_type( if(null is null, select json_type( if(null is null,
cast('null' as json), json_compact('null'),
cast('[1,2,3]' as json)) ); json_compact('[1,2,3]')) );
select json_type( if(null is not null, select json_type( if(null is not null,
cast('null' as json), json_compact('null'),
cast('[1,2,3]' as json))); json_compact('[1,2,3]')));
select cast(json_extract(cast(concat('[', cast('["A",2]' as json), ']') as json), select cast(json_extract(json_compact(concat('[', json_compact('["A",2]'), ']')),
'$[0][1]') as char) = 2; '$[0][1]') as char) = 2;
--echo # ---------------------------------------------------------------------- --echo # ----------------------------------------------------------------------
--echo # Test of aggregate function MAX, MIN. --echo # Test of aggregate function MAX, MIN.
--echo # ---------------------------------------------------------------------- --echo # ----------------------------------------------------------------------
select max(cast('[1,2,3]' as json)); select max(json_compact('[1,2,3]'));
--echo # ---------------------------------------------------------------------- --echo # ----------------------------------------------------------------------
--echo # Test of JSON_QUOTE, JSON_UNQUOTE --echo # Test of JSON_QUOTE, JSON_UNQUOTE
...@@ -1781,26 +1781,26 @@ select json_unquote(123); # integer not allowed ...@@ -1781,26 +1781,26 @@ select json_unquote(123); # integer not allowed
select json_unquote('""'); # empty string select json_unquote('""'); # empty string
select char_length(json_unquote('""')); # verify empty string select char_length(json_unquote('""')); # verify empty string
select json_unquote('"" '); # unchanged: no final " select json_unquote('"" '); # unchanged: no final "
select json_unquote(cast(json_quote('abc') as json)); # round trip select json_unquote(json_compact(json_quote('abc'))); # round trip
# No change in this JSON string: it is an object # No change in this JSON string: it is an object
select cast('{"abc": "foo"}' as json); select json_compact('{"abc": "foo"}');
select json_unquote(cast('{"abc": "foo"}' as json)); select json_unquote(json_compact('{"abc": "foo"}'));
# This is a JSON string, so it is actually unquoted # This is a JSON string, so it is actually unquoted
select json_extract(cast('{"abc": "foo"}' as json), '$.abc'); select json_extract(json_compact('{"abc": "foo"}'), '$.abc');
select json_unquote(json_extract(cast('{"abc": "foo"}' as json), '$.abc')); select json_unquote(json_extract(json_compact('{"abc": "foo"}'), '$.abc'));
# Bug fix: thse should be the same # Bug fix: thse should be the same
select json_unquote('["a", "b", "c"]'); select json_unquote('["a", "b", "c"]');
select json_unquote(cast('["a", "b", "c"]' as json)); select json_unquote(json_compact('["a", "b", "c"]'));
select charset(json_unquote('"abc"')); select charset(json_unquote('"abc"'));
select json_quote(convert(X'e68891' using utf8)); # chinese "I" (wo3) select json_quote(convert(X'e68891' using utf8)); # chinese "I" (wo3)
select json_quote(convert(X'e68891' using utf8mb4)); # chinese "I" (wo3) select json_quote(convert(X'e68891' using utf8mb4)); # chinese "I" (wo3)
select cast(json_quote(convert(X'e68891' using utf8)) as json); select json_compact(json_quote(convert(X'e68891' using utf8)));
select json_unquote(convert(X'e68891' using utf8)); # chinese "I" (wo3) select json_unquote(convert(X'e68891' using utf8)); # chinese "I" (wo3)
...@@ -1809,12 +1809,12 @@ select json_unquote(json_unquote(json_unquote( # long round trip of it ...@@ -1809,12 +1809,12 @@ select json_unquote(json_unquote(json_unquote( # long round trip of it
json_quote(json_quote(json_quote('abc')))))); json_quote(json_quote(json_quote('abc'))))));
# DATE/TIME will lose their quotes, too: # DATE/TIME will lose their quotes, too:
select cast(cast('2015-01-15 23:24:25' as datetime) as json); select json_compact(cast('2015-01-15 23:24:25' as datetime));
select json_unquote(cast(cast('2015-01-15 23:24:25' as datetime) as json)); select json_unquote(json_compact(cast('2015-01-15 23:24:25' as datetime)));
# as well as opaque values: # as well as opaque values:
select cast(st_geomfromtext('point(1 1)') as json); select json_compact(st_geomfromtext('point(1 1)'));
select json_unquote(cast(st_geomfromtext('point(1 1)') as json)); select json_unquote(json_compact(st_geomfromtext('point(1 1)')));
# examples from the wl7909 spec # examples from the wl7909 spec
# returns the SQL string literal abc # returns the SQL string literal abc
...@@ -1828,7 +1828,7 @@ SELECT JSON_UNQUOTE( 123 ); ...@@ -1828,7 +1828,7 @@ SELECT JSON_UNQUOTE( 123 );
# returns the SQL string literal abc # returns the SQL string literal abc
SELECT JSON_UNQUOTE SELECT JSON_UNQUOTE
( CAST( CAST( '"abc"' AS JSON ) AS CHAR ) ); ( CAST( json_compact( '"abc"') AS CHAR ) );
# returns 1 # returns 1
SELECT JSON_UNQUOTE SELECT JSON_UNQUOTE
...@@ -1853,7 +1853,7 @@ SELECT JSON_QUOTE( 'abc' ); ...@@ -1853,7 +1853,7 @@ SELECT JSON_QUOTE( 'abc' );
SELECT JSON_QUOTE( 123 ); SELECT JSON_QUOTE( 123 );
# returns the JSON document consisting of the string scalar "123" # returns the JSON document consisting of the string scalar "123"
SELECT CAST( JSON_QUOTE( '123' ) AS JSON ); SELECT json_compact( JSON_QUOTE( '123' ));
--echo # ---------------------------------------------------------------------- --echo # ----------------------------------------------------------------------
--echo # Test of JSON_CONTAINS --echo # Test of JSON_CONTAINS
...@@ -1861,47 +1861,47 @@ SELECT CAST( JSON_QUOTE( '123' ) AS JSON ); ...@@ -1861,47 +1861,47 @@ SELECT CAST( JSON_QUOTE( '123' ) AS JSON );
--echo # should give NULL --echo # should give NULL
select json_contains(NULL, NULL); select json_contains(NULL, NULL);
select json_contains(cast('{"a": 1, "b": 2}' as json), NULL); select json_contains(json_compact('{"a": 1, "b": 2}'), NULL);
select json_contains(NULL, cast('null' as json)); select json_contains(NULL, json_compact('null'));
select json_contains(cast('[1]' as json), cast('[1]' as json), NULL); select json_contains(json_compact('[1]'), json_compact('[1]'), NULL);
--echo # should give 0: --echo # should give 0:
select json_contains(cast(3.14 as json), cast(3 as json)); select json_contains(json_compact(3.14), json_compact(3));
--echo # should give 0: not at top level --echo # should give 0: not at top level
select json_contains(cast('{"a": {"b": 7}}' as json), cast('{"b": 7}' as json)); select json_contains(json_compact('{"a": {"b": 7}}'), json_compact('{"b": 7}'));
--echo # but path argument will fix it: --echo # but path argument will fix it:
select json_contains(cast('{"a": {"b": 7}}' as json), cast('{"b": 7}' as json), '$.a'); select json_contains(json_compact('{"a": {"b": 7}}'), json_compact('{"b": 7}'), '$.a');
--echo # but arrays "introspect" --echo # but arrays "introspect"
select json_contains(cast('[1,[2.0, 3.0]]' as json), cast('[2.0]' as json)); select json_contains(json_compact('[1,[2.0, 3.0]]'), json_compact('[2.0]'));
select json_contains(cast('[1, 2, [3, [4, 5]], 6, 7]' as json), cast('5' as json)); select json_contains(json_compact('[1, 2, [3, [4, 5]], 6, 7]'), json_compact('5'));
--echo # should give 0: just a key --echo # should give 0: just a key
select json_contains(cast('{"a": 1, "b": 2}' as json), cast('"a"' as json)); select json_contains(json_compact('{"a": 1, "b": 2}'), json_compact('"a"'));
--echo # should give 0: one candidate element doesn't match --echo # should give 0: one candidate element doesn't match
select json_contains(cast('[1]' as json), cast('[1,2]' as json)); select json_contains(json_compact('[1]'), json_compact('[1,2]'));
--echo # should all give 1 --echo # should all give 1
select json_contains(cast('null' as json), cast('null' as json)); select json_contains(json_compact('null'), json_compact('null'));
--echo # simple object subset --echo # simple object subset
select json_contains(cast('{"a": 1, "b": 2}' as json), cast( '{"a": 1}' as json)); select json_contains(json_compact('{"a": 1, "b": 2}'), json_compact( '{"a": 1}'));
--echo # simple vector subset --echo # simple vector subset
select json_contains(cast('[1, 2, 3]' as json), cast('[1, 3]' as json)); select json_contains(json_compact('[1, 2, 3]'), json_compact('[1, 3]'));
--echo # auto-wrap, should give 1 --echo # auto-wrap, should give 1
select json_contains(cast('[1, 2, 3]' as json), cast(3 as json)); select json_contains(json_compact('[1, 2, 3]'), json_compact(3));
--echo # ok even with nested cast off elements --echo # ok even with nested cast off elements
select json_contains(cast('{"person": {"id": 1, "country": "norway"}}' as json), select json_contains(json_compact('{"person": {"id": 1, "country": "norway"}}'),
cast('{"person": {"country": "norway"}}' as json)); json_compact('{"person": {"country": "norway"}}'));
--echo # vector reordering and duplicates is ok --echo # vector reordering and duplicates is ok
select json_contains(cast('[1,3,5]' as json), cast('[5,3,1,5]' as json)); select json_contains(json_compact('[1,3,5]'), json_compact('[5,3,1,5]'));
--echo # ok even with more elts in candidate than in doc --echo # ok even with more elts in candidate than in doc
select json_contains(cast('[{"b": 4, "a":7}]' as json), cast('[{"a":7},{"b":4}]' as json)); select json_contains(json_compact('[{"b": 4, "a":7}]'), json_compact('[{"a":7},{"b":4}]'));
select json_contains(cast('[{"b": 4, "a":7}, 5]' as json), cast('[5, {"a":7, "b":4}]' as json)); select json_contains(json_compact('[{"b": 4, "a":7}, 5]'), json_compact('[5, {"a":7, "b":4}]'));
--echo # ok even with mixed number types that compare equal --echo # ok even with mixed number types that compare equal
select json_contains(cast('[{"b": 4, "a":7}, 5.0]' as json), cast('[5, {"a":7.0E0, "b":4}]' as json)); select json_contains(json_compact('[{"b": 4, "a":7}, 5.0]'), json_compact('[5, {"a":7.0E0, "b":4}]'));
# Bug discovered by Rick: used to give 1 (true). # Bug discovered by Rick: used to give 1 (true).
select json_contains( '{"customer": "cust3"}', '{"customer": "cust1"}' ); select json_contains( '{"customer": "cust3"}', '{"customer": "cust1"}' );
...@@ -1919,36 +1919,36 @@ SELECT JSON_CONTAINS('{"a":1}', '{"a":1,"b":2}'); ...@@ -1919,36 +1919,36 @@ SELECT JSON_CONTAINS('{"a":1}', '{"a":1,"b":2}');
# returns 1 # returns 1
SELECT JSON_CONTAINS SELECT JSON_CONTAINS
( (
CAST('[1, 4, 6]' AS JSON), json_compact('[1, 4, 6]'),
CAST('[1, 6]' AS JSON) json_compact('[1, 6]')
); );
# returns 1; even with nested cast off elements # returns 1; even with nested cast off elements
SELECT JSON_CONTAINS SELECT JSON_CONTAINS
( (
CAST('{"person": {"id": 1, "country": "norway"}}' AS JSON), json_compact('{"person": {"id": 1, "country": "norway"}}'),
CAST('{"person": {"country": "norway"}}' AS JSON) json_compact('{"person": {"country": "norway"}}')
); );
# returns 1; reordering and duplicates are ok # returns 1; reordering and duplicates are ok
SELECT JSON_CONTAINS SELECT JSON_CONTAINS
( (
CAST('[1,3,5]' AS JSON), json_compact('[1,3,5]'),
CAST('[5,3,1,5]' AS JSON) json_compact('[5,3,1,5]')
); );
# return 0; no type conversion is performed # return 0; no type conversion is performed
SELECT JSON_CONTAINS SELECT JSON_CONTAINS
( (
CAST('[3.14]' AS JSON), json_compact('[3.14]'),
CAST('[3]' AS JSON) json_compact('[3]')
); );
# returns 1, due to auto-wrapping # returns 1, due to auto-wrapping
SELECT JSON_CONTAINS SELECT JSON_CONTAINS
( (
CAST('[1, 2, 3]' AS JSON), json_compact('[1, 2, 3]'),
CAST(3 AS JSON) json_compact(3)
); );
--error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT --error ER_WRONG_PARAMCOUNT_TO_NATIVE_FCT
...@@ -1963,20 +1963,20 @@ SELECT JSON_CONTAINS('[1]', '[1]', '$', '$[0]'); ...@@ -1963,20 +1963,20 @@ SELECT JSON_CONTAINS('[1]', '[1]', '$', '$[0]');
--echo # Item_func_json_quote::fix_length_and_dec. Bug found by Knut. --echo # Item_func_json_quote::fix_length_and_dec. Bug found by Knut.
--echo # Similar issue for JSON_UNQUOTE and JSON_TYPE. --echo # Similar issue for JSON_UNQUOTE and JSON_TYPE.
--echo # ---------------------------------------------------------------------- --echo # ----------------------------------------------------------------------
select json_object("a", ifnull(json_quote('test'), cast('null' as json))); select json_object("a", ifnull(json_quote('test'), json_compact('null')));
select cast(concat('[', json_quote('ab'), ']') as json); select json_compact(concat('[', json_quote('ab'), ']'));
select cast(concat('[', json_unquote('"12"'), ']') as json); select json_compact(concat('[', json_unquote('"12"'), ']'));
select cast(concat('["', json_type(cast(1 as json)), '"]') as json); select json_compact(concat('["', json_type( json_compact(1)), '"]'));
--echo # --echo #
--echo # Bug#20912438: ITEM_TYPE_HOLDER::DISPLAY_LENGTH(ITEM*): ASSERTION `0' FAILED --echo # Bug#20912438: ITEM_TYPE_HOLDER::DISPLAY_LENGTH(ITEM*): ASSERTION `0' FAILED
--echo # --echo #
(SELECT JSON_KEYS('{ "key80": "2015-04-20 11:53:55"}')) UNION ALL (SELECT JSON_KEYS('{ "key80": "2015-04-20 11:53:55"}')) UNION ALL
(SELECT JSON_KEYS('{ "key80": "2015-04-20 11:53:55" }') LIMIT 0); (SELECT JSON_KEYS('{ "key80": "2015-04-20 11:53:55" }') LIMIT 0);
SELECT CAST(1 AS JSON) UNION ALL SELECT CAST(1 AS JSON); SELECT json_compact(1) UNION ALL SELECT json_compact(1);
# Exercise NULL handling and error handling in Item_copy_json::copy(). # Exercise NULL handling and error handling in Item_copy_json::copy().
SELECT COUNT(*), CAST(NULL AS JSON); SELECT COUNT(*), json_compact(NULL);
--echo error ER_INVALID_JSON_TEXT_IN_PARAM --echo error ER_INVALID_JSON_TEXT_IN_PARAM
SELECT COUNT(*), JSON_EXTRACT('not valid json!', '$'); SELECT COUNT(*), JSON_EXTRACT('not valid json!', '$');
...@@ -2118,7 +2118,7 @@ execute json_stmt12 USING @mypath; ...@@ -2118,7 +2118,7 @@ execute json_stmt12 USING @mypath;
--echo # --echo #
select json_quote( json_type( json_object() ) ); select json_quote( json_type( json_object() ) );
select json_quote( json_type( cast('{}' as json) ) ); select json_quote( json_type( json_compact('{}') ) );
--echo # --echo #
--echo # Bug#21148020 OUTPUT FROM JSON_TYPE() IS TRUNCATED --echo # Bug#21148020 OUTPUT FROM JSON_TYPE() IS TRUNCATED
...@@ -2130,9 +2130,9 @@ CREATE VIEW v1 AS SELECT JSON_TYPE(JSON_OBJECT()); ...@@ -2130,9 +2130,9 @@ CREATE VIEW v1 AS SELECT JSON_TYPE(JSON_OBJECT());
SELECT * FROM v1; SELECT * FROM v1;
drop view v1; drop view v1;
# SELECT JSON_TYPE(CAST(CAST('2015-05-25 11:23:55' AS DATETIME) AS JSON)); # SELECT JSON_TYPE(json_compact(CAST('2015-05-25 11:23:55' AS DATETIME)));
# CREATE VIEW v2 AS SELECT JSON_TYPE(CAST(CAST('2015-05-25 11:23:55' AS # CREATE VIEW v2 AS SELECT JSON_TYPE(json_compact(CAST('2015-05-25 11:23:55' AS
# DATETIME) AS JSON)); # DATETIME)));
# SELECT * FROM v2; # SELECT * FROM v2;
# drop view v2; # drop view v2;
...@@ -2284,5 +2284,5 @@ SELECT JSON_REPLACE('[[[1]]]', '$[0][0][0]', 100); ...@@ -2284,5 +2284,5 @@ SELECT JSON_REPLACE('[[[1]]]', '$[0][0][0]', 100);
# a mix of NULLs and JSON values. # a mix of NULLs and JSON values.
SELECT JSON_ARRAY(LEAST(NULL, NULL), GREATEST(NULL, NULL), LEAST(j1, NULL), SELECT JSON_ARRAY(LEAST(NULL, NULL), GREATEST(NULL, NULL), LEAST(j1, NULL),
GREATEST(NULL, j2), LEAST(j1, j2), GREATEST(j1, j2)) AS j GREATEST(NULL, j2), LEAST(j1, j2), GREATEST(j1, j2)) AS j
FROM (SELECT CAST('1' AS JSON) AS j1, CAST('2' AS JSON) AS j2) t; FROM (SELECT json_compact('1') AS j1, json_compact('2') AS j2) t;
...@@ -161,10 +161,10 @@ select json_unquote('abc'); ...@@ -161,10 +161,10 @@ select json_unquote('abc');
select json_object("a", json_object("b", "abcd")); select json_object("a", json_object("b", "abcd"));
select json_object("a", '{"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_compact(NULL);
select json_depth(cast(NULL as JSON)); select json_depth(json_compact(NULL));
select json_depth('[[], {}]'); select json_depth('[[], {}]');
select json_depth('[[[1,2,3],"s"], {}, []]'); select json_depth('[[[1,2,3],"s"], {}, []]');
select json_depth('[10, {"a": 20}]'); select json_depth('[10, {"a": 20}]');
......
...@@ -1095,6 +1095,8 @@ class Item: public Value_source, ...@@ -1095,6 +1095,8 @@ class Item: public Value_source,
Returns the val_str() value converted to the given character set. Returns the val_str() value converted to the given character set.
*/ */
String *val_str(String *str, String *converter, CHARSET_INFO *to); 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. Return decimal representation of item with fixed point.
......
...@@ -7228,9 +7228,6 @@ create_func_cast(THD *thd, Item *a, Cast_target cast_type, ...@@ -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); res= new (thd->mem_root) Item_char_typecast(thd, a, len, real_cs);
break; break;
} }
case ITEM_CAST_JSON:
res= new (thd->mem_root) Item_json_typecast(thd, a);
break;
default: default:
{ {
DBUG_ASSERT(0); DBUG_ASSERT(0);
......
...@@ -2221,7 +2221,7 @@ enum Cast_target ...@@ -2221,7 +2221,7 @@ enum Cast_target
{ {
ITEM_CAST_BINARY, ITEM_CAST_SIGNED_INT, ITEM_CAST_UNSIGNED_INT, 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_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, ...@@ -373,7 +373,7 @@ static int path_setup_nwc(json_path_t *p, CHARSET_INFO *i_cs,
longlong Item_func_json_valid::val_int() 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; json_engine_t je;
if ((null_value= args[0]->null_value)) if ((null_value= args[0]->null_value))
...@@ -401,7 +401,7 @@ longlong Item_func_json_exists::val_int() ...@@ -401,7 +401,7 @@ longlong Item_func_json_exists::val_int()
json_engine_t je; json_engine_t je;
uint array_counters[JSON_DEPTH_LIMIT]; 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) if (!path.parsed)
{ {
...@@ -454,7 +454,7 @@ void Item_func_json_value::fix_length_and_dec() ...@@ -454,7 +454,7 @@ void Item_func_json_value::fix_length_and_dec()
String *Item_func_json_value::val_str(String *str) String *Item_func_json_value::val_str(String *str)
{ {
json_engine_t je; json_engine_t je;
String *js= args[0]->val_str(&tmp_js); String *js= args[0]->val_json(&tmp_js);
int error= 0; int error= 0;
uint array_counters[JSON_DEPTH_LIMIT]; uint array_counters[JSON_DEPTH_LIMIT];
...@@ -587,7 +587,7 @@ void Item_func_json_unquote::fix_length_and_dec() ...@@ -587,7 +587,7 @@ void Item_func_json_unquote::fix_length_and_dec()
String *Item_func_json_unquote::val_str(String *str) 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; json_engine_t je;
int c_len; int c_len;
...@@ -712,7 +712,7 @@ static bool path_ok(const json_path_with_flags *paths_list, int n_paths, ...@@ -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 *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_engine_t je, sav_je;
json_path_t p; json_path_t p;
const uchar *value; const uchar *value;
...@@ -817,7 +817,7 @@ String *Item_func_json_extract::val_str(String *str) ...@@ -817,7 +817,7 @@ String *Item_func_json_extract::val_str(String *str)
longlong Item_func_json_extract::val_int() 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; json_engine_t je;
uint n_arg; uint n_arg;
uint array_counters[JSON_DEPTH_LIMIT]; uint array_counters[JSON_DEPTH_LIMIT];
...@@ -1040,7 +1040,7 @@ static int check_contains(json_engine_t *js, json_engine_t *value) ...@@ -1040,7 +1040,7 @@ static int check_contains(json_engine_t *js, json_engine_t *value)
longlong Item_func_json_contains::val_int() 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; json_engine_t je, ve;
int result; int result;
...@@ -1049,7 +1049,7 @@ longlong Item_func_json_contains::val_int() ...@@ -1049,7 +1049,7 @@ longlong Item_func_json_contains::val_int()
if (!a2_parsed) if (!a2_parsed)
{ {
val= args[1]->val_str(&tmp_val); val= args[1]->val_json(&tmp_val);
a2_parsed= a2_constant; a2_parsed= a2_constant;
} }
...@@ -1179,7 +1179,7 @@ static int parse_one_or_all(const Item_func *f, Item *ooa_arg, ...@@ -1179,7 +1179,7 @@ static int parse_one_or_all(const Item_func *f, Item *ooa_arg,
#ifdef DUMMY #ifdef DUMMY
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; json_engine_t je;
uint n_arg; uint n_arg;
longlong result; longlong result;
...@@ -1247,7 +1247,7 @@ longlong Item_func_json_contains_path::val_int() ...@@ -1247,7 +1247,7 @@ longlong Item_func_json_contains_path::val_int()
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; json_engine_t je;
uint n_arg; uint n_arg;
longlong result; longlong result;
...@@ -1351,7 +1351,7 @@ static int append_json_value(String *str, Item *item, String *tmp_val) ...@@ -1351,7 +1351,7 @@ static int append_json_value(String *str, Item *item, String *tmp_val)
return str->append(t_f, t_f_len); 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) if (item->null_value)
goto append_null; goto append_null;
if (item->is_json_type()) if (item->is_json_type())
...@@ -1461,7 +1461,7 @@ void Item_func_json_array_append::fix_length_and_dec() ...@@ -1461,7 +1461,7 @@ void Item_func_json_array_append::fix_length_and_dec()
String *Item_func_json_array_append::val_str(String *str) String *Item_func_json_array_append::val_str(String *str)
{ {
json_engine_t je; 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; uint n_arg, n_path, str_rest_len;
const uchar *ar_end; const uchar *ar_end;
...@@ -1588,7 +1588,7 @@ String *Item_func_json_array_append::val_str(String *str) ...@@ -1588,7 +1588,7 @@ String *Item_func_json_array_append::val_str(String *str)
String *Item_func_json_array_insert::val_str(String *str) String *Item_func_json_array_insert::val_str(String *str)
{ {
json_engine_t je; 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; uint n_arg, n_path;
DBUG_ASSERT(fixed == 1); DBUG_ASSERT(fixed == 1);
...@@ -1959,7 +1959,7 @@ String *Item_func_json_merge::val_str(String *str) ...@@ -1959,7 +1959,7 @@ String *Item_func_json_merge::val_str(String *str)
{ {
DBUG_ASSERT(fixed == 1); DBUG_ASSERT(fixed == 1);
json_engine_t je1, je2; 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; uint n_arg;
if (args[0]->null_value) if (args[0]->null_value)
...@@ -1970,7 +1970,7 @@ String *Item_func_json_merge::val_str(String *str) ...@@ -1970,7 +1970,7 @@ String *Item_func_json_merge::val_str(String *str)
str->set_charset(js1->charset()); str->set_charset(js1->charset());
str->length(0); 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) if (args[n_arg]->null_value)
goto null_return; goto null_return;
...@@ -2028,7 +2028,7 @@ void Item_func_json_length::fix_length_and_dec() ...@@ -2028,7 +2028,7 @@ void Item_func_json_length::fix_length_and_dec()
longlong Item_func_json_length::val_int() 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; json_engine_t je;
uint length= 0; uint length= 0;
uint array_counters[JSON_DEPTH_LIMIT]; uint array_counters[JSON_DEPTH_LIMIT];
...@@ -2105,7 +2105,7 @@ longlong Item_func_json_length::val_int() ...@@ -2105,7 +2105,7 @@ longlong Item_func_json_length::val_int()
longlong Item_func_json_depth::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; json_engine_t je;
uint depth= 0, c_depth= 0; uint depth= 0, c_depth= 0;
bool inc_depth= TRUE; bool inc_depth= TRUE;
...@@ -2164,7 +2164,7 @@ void Item_func_json_type::fix_length_and_dec() ...@@ -2164,7 +2164,7 @@ void Item_func_json_type::fix_length_and_dec()
String *Item_func_json_type::val_str(String *str) 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; json_engine_t je;
const char *type; const char *type;
...@@ -2232,7 +2232,7 @@ void Item_func_json_insert::fix_length_and_dec() ...@@ -2232,7 +2232,7 @@ void Item_func_json_insert::fix_length_and_dec()
String *Item_func_json_insert::val_str(String *str) String *Item_func_json_insert::val_str(String *str)
{ {
json_engine_t je; 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; uint n_arg, n_path;
json_string_t key_name; json_string_t key_name;
...@@ -2478,7 +2478,7 @@ void Item_func_json_remove::fix_length_and_dec() ...@@ -2478,7 +2478,7 @@ void Item_func_json_remove::fix_length_and_dec()
String *Item_func_json_remove::val_str(String *str) String *Item_func_json_remove::val_str(String *str)
{ {
json_engine_t je; 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; uint n_arg, n_path;
json_string_t key_name; json_string_t key_name;
...@@ -2661,7 +2661,7 @@ void Item_func_json_keys::fix_length_and_dec() ...@@ -2661,7 +2661,7 @@ void Item_func_json_keys::fix_length_and_dec()
String *Item_func_json_keys::val_str(String *str) String *Item_func_json_keys::val_str(String *str)
{ {
json_engine_t je; 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 n_keys= 0;
uint array_counters[JSON_DEPTH_LIMIT]; uint array_counters[JSON_DEPTH_LIMIT];
...@@ -2837,7 +2837,7 @@ static int append_json_path(String *str, const json_path_t *p) ...@@ -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 *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); String *s_str= args[2]->val_str(&tmp_js);
json_engine_t je; json_engine_t je;
json_path_t p, sav_path; json_path_t p, sav_path;
...@@ -2934,21 +2934,6 @@ String *Item_func_json_search::val_str(String *str) ...@@ -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 const char *Item_func_json_format::func_name() const
{ {
switch (fmt) switch (fmt)
...@@ -2976,7 +2961,7 @@ void Item_func_json_format::fix_length_and_dec() ...@@ -2976,7 +2961,7 @@ void Item_func_json_format::fix_length_and_dec()
String *Item_func_json_format::val_str(String *str) 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; json_engine_t je;
int tab_size= 4; int tab_size= 4;
...@@ -3016,3 +3001,12 @@ String *Item_func_json_format::val_str(String *str) ...@@ -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 ...@@ -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 class Item_func_json_format: public Item_str_func
{ {
public: public:
...@@ -449,6 +436,8 @@ class Item_func_json_format: public Item_str_func ...@@ -449,6 +436,8 @@ class Item_func_json_format: public Item_str_func
const char *func_name() const; const char *func_name() const;
void fix_length_and_dec(); void fix_length_and_dec();
String *val_str(String *str); 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) Item *get_copy(THD *thd, MEM_ROOT *mem_root)
{ return get_item_copy<Item_func_json_format>(thd, mem_root, this); } { return get_item_copy<Item_func_json_format>(thd, mem_root, this); }
}; };
......
...@@ -7417,7 +7417,7 @@ ER_BINLOG_UNCOMPRESS_ERROR ...@@ -7417,7 +7417,7 @@ ER_BINLOG_UNCOMPRESS_ERROR
ER_JSON_BAD_CHR ER_JSON_BAD_CHR
eng "Broken JSON string in argument %d to function '%s' at position %d" eng "Broken JSON string in argument %d to function '%s' at position %d"
ER_JSON_NOT_JSON_CHR 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 ER_JSON_EOS
eng "Unexpected end of JSON text in argument %d to function '%s'" eng "Unexpected end of JSON text in argument %d to function '%s'"
ER_JSON_SYNTAX ER_JSON_SYNTAX
......
...@@ -10714,7 +10714,6 @@ cast_type: ...@@ -10714,7 +10714,6 @@ cast_type:
} }
| cast_type_numeric { $$= $1; Lex->charset= NULL; } | cast_type_numeric { $$= $1; Lex->charset= NULL; }
| cast_type_temporal { $$= $1; Lex->charset= NULL; } | cast_type_temporal { $$= $1; Lex->charset= NULL; }
| JSON_SYM { $$.set(ITEM_CAST_JSON); }
; ;
cast_type_numeric: 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