-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MDEV-16375 Function to normalize a json value
This patch implements JSON_NORMALIZE SQL function. Co-authored-by: Vicențiu Ciorbaru <vicentiu@mariadb.org>
- Loading branch information
1 parent
105e414
commit fcde341
Showing
5 changed files
with
221 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
set names utf8; | ||
create table t1 (json json); | ||
show create table t1; | ||
Table Create Table | ||
t1 CREATE TABLE `t1` ( | ||
`json` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL CHECK (json_valid(`json`)) | ||
) ENGINE=MyISAM DEFAULT CHARSET=latin1 | ||
insert into t1 values | ||
('{ }'), | ||
('[ ]'), | ||
('{ "foo" : "bar" }'), | ||
('{ "foo" : "bar", "baz" : "whatever" }'), | ||
('[ 1.2, 0.0, "text", 0, null, true, false ]'), | ||
('[ "string", { "key" : "val", "a" : "b", "c" : [ 10, 9, 8, "seven", 11 ] }]'), | ||
('{ "ăț€": "val1", "âț€":"val2" }'); | ||
select json, json_normalize(json) from t1 | ||
order by json; | ||
json json_normalize(json) | ||
[ ] [] | ||
[ "string", { "key" : "val", "a" : "b", "c" : [ 10, 9, 8, "seven", 11 ] }] ["string",{"a":"b","c":[1.0E1,9.0E0,8.0E0,"seven",1.1E1],"key":"val"}] | ||
[ 1.2, 0.0, "text", 0, null, true, false ] [1.2E0,0.0E0,"text",0.0E0,null,true,false] | ||
{ } {} | ||
{ "foo" : "bar" } {"foo":"bar"} | ||
{ "foo" : "bar", "baz" : "whatever" } {"baz":"whatever","foo":"bar"} | ||
{ "ăț€": "val1", "âț€":"val2" } {"âț€":"val2","ăț€":"val1"} | ||
create view v1 as (select json, json_normalize(json) norm_json from t1); | ||
show create view v1; | ||
View Create View character_set_client collation_connection | ||
v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS (select `t1`.`json` AS `json`,json_normalize(`t1`.`json`) AS `norm_json` from `t1`) utf8mb3 utf8mb3_general_ci | ||
select * from v1 | ||
order by json; | ||
json norm_json | ||
[ ] [] | ||
[ "string", { "key" : "val", "a" : "b", "c" : [ 10, 9, 8, "seven", 11 ] }] ["string",{"a":"b","c":[1.0E1,9.0E0,8.0E0,"seven",1.1E1],"key":"val"}] | ||
[ 1.2, 0.0, "text", 0, null, true, false ] [1.2E0,0.0E0,"text",0.0E0,null,true,false] | ||
{ } {} | ||
{ "foo" : "bar" } {"foo":"bar"} | ||
{ "foo" : "bar", "baz" : "whatever" } {"baz":"whatever","foo":"bar"} | ||
{ "ăț€": "val1", "âț€":"val2" } {"âț€":"val2","ăț€":"val1"} | ||
select json_normalize(NULL); | ||
json_normalize(NULL) | ||
NULL | ||
select json_normalize('{ "invalid": "no_close"'); | ||
json_normalize('{ "invalid": "no_close"') | ||
NULL | ||
drop table t1; | ||
drop view v1; | ||
create table t1 (text varchar(200) character set 'latin1'); | ||
insert into t1 values (unhex('22E522')); | ||
create table t2 (text varchar(200) character set 'utf8mb4'); | ||
insert into t2 SELECT * FROM t1; | ||
select t1.text, hex(t1.text) from t1; | ||
text hex(t1.text) | ||
"å" 22E522 | ||
select t2.text, hex(t2.text) from t2; | ||
text hex(t2.text) | ||
"å" 22C3A522 | ||
select t1.text | ||
, t2.text | ||
, replace(json_normalize(t1.text), unhex('C3A5'), unhex('C385')) | ||
, replace(json_normalize(t2.text), unhex('C3A5'), unhex('C385')) | ||
, hex(replace(json_normalize(t1.text), unhex('C3A5'), unhex('C385'))) | ||
, hex(replace(json_normalize(t2.text), unhex('C3A5'), unhex('C385'))) | ||
from t1, t2; | ||
text text replace(json_normalize(t1.text), unhex('C3A5'), unhex('C385')) replace(json_normalize(t2.text), unhex('C3A5'), unhex('C385')) hex(replace(json_normalize(t1.text), unhex('C3A5'), unhex('C385'))) hex(replace(json_normalize(t2.text), unhex('C3A5'), unhex('C385'))) | ||
"å" "å" "Å" "Å" 22C38522 22C38522 | ||
drop table t1; | ||
drop table t2; | ||
create table t1 (text varchar(1)); | ||
insert into t1 values ('0'); | ||
select concat_ws(' ', t1.text, t1.text) from t1; | ||
concat_ws(' ', t1.text, t1.text) | ||
0 0 | ||
select concat_ws(' ', json_normalize(t1.text), json_normalize(t1.text)) from t1; | ||
concat_ws(' ', json_normalize(t1.text), json_normalize(t1.text)) | ||
0.0E0 0.0E0 | ||
drop table t1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
set names utf8; | ||
|
||
create table t1 (json json); | ||
show create table t1; | ||
|
||
|
||
insert into t1 values | ||
('{ }'), | ||
('[ ]'), | ||
('{ "foo" : "bar" }'), | ||
('{ "foo" : "bar", "baz" : "whatever" }'), | ||
('[ 1.2, 0.0, "text", 0, null, true, false ]'), | ||
('[ "string", { "key" : "val", "a" : "b", "c" : [ 10, 9, 8, "seven", 11 ] }]'), | ||
('{ "ăț€": "val1", "âț€":"val2" }'); | ||
|
||
select json, json_normalize(json) from t1 | ||
order by json; | ||
|
||
|
||
create view v1 as (select json, json_normalize(json) norm_json from t1); | ||
show create view v1; | ||
|
||
select * from v1 | ||
order by json; | ||
|
||
select json_normalize(NULL); | ||
select json_normalize('{ "invalid": "no_close"'); | ||
|
||
drop table t1; | ||
drop view v1; | ||
|
||
create table t1 (text varchar(200) character set 'latin1'); | ||
insert into t1 values (unhex('22E522')); | ||
|
||
create table t2 (text varchar(200) character set 'utf8mb4'); | ||
insert into t2 SELECT * FROM t1; | ||
|
||
select t1.text, hex(t1.text) from t1; | ||
select t2.text, hex(t2.text) from t2; | ||
|
||
select t1.text | ||
, t2.text | ||
, replace(json_normalize(t1.text), unhex('C3A5'), unhex('C385')) | ||
, replace(json_normalize(t2.text), unhex('C3A5'), unhex('C385')) | ||
, hex(replace(json_normalize(t1.text), unhex('C3A5'), unhex('C385'))) | ||
, hex(replace(json_normalize(t2.text), unhex('C3A5'), unhex('C385'))) | ||
from t1, t2; | ||
|
||
drop table t1; | ||
drop table t2; | ||
|
||
create table t1 (text varchar(1)); | ||
insert into t1 values ('0'); | ||
|
||
select concat_ws(' ', t1.text, t1.text) from t1; | ||
select concat_ws(' ', json_normalize(t1.text), json_normalize(t1.text)) from t1; | ||
|
||
drop table t1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters