Commit a9f6abed authored by Alexey Botchkov's avatar Alexey Botchkov Committed by Rucha Deodhar

MDEV-25875: JSON_TABLE: extract document fragment into JSON column

Accept JSON values for the JSON fields.
parent 6b6d745b
...@@ -953,6 +953,20 @@ converted original ...@@ -953,6 +953,20 @@ converted original
Warnings: Warnings:
Warning 1264 Out of range value for column 'converted' at row 2 Warning 1264 Out of range value for column 'converted' at row 2
Warning 1366 Incorrect integer value: 'foo' for column ``.`(temporary)`.`converted` at row 3 Warning 1366 Incorrect integer value: 'foo' for column ``.`(temporary)`.`converted` at row 3
select * from
json_table('[{"color": "blue", "price": { "high": 10, "low": 5}},
{"color": "white", "price": "pretty low"},
{"color": "yellow", "price": 256.20},
{"color": "red", "price": { "high": 20, "low": 8}}]',
'$[*]' columns(color varchar(100) path '$.color',
price json path '$.price'
)
) as T;
color price
blue { "high": 10, "low": 5}
white "pretty low"
yellow 256.20
red { "high": 20, "low": 8}
# #
# MDEV-27696 Json table columns accept redundant COLLATE syntax # MDEV-27696 Json table columns accept redundant COLLATE syntax
# #
......
...@@ -40,7 +40,7 @@ id jpath jsn_path jexst ...@@ -40,7 +40,7 @@ id jpath jsn_path jexst
2 2 2 0 2 2 2 0
3 33 {"x":33} 1 3 33 {"x":33} 1
4 0 0 0 4 0 0 0
5 66 NULL 0 5 66 [1,2] 0
select * from select * from
json_table( json_table(
'[{"a":"3"},{"a":2},{"b":1},{"a":0.33},{"a":"asd"}]', '[{"a":"3"},{"a":2},{"b":1},{"a":0.33},{"a":"asd"}]',
...@@ -55,11 +55,11 @@ jsn_path json path '$.a' default '{"x":33}' on empty, ...@@ -55,11 +55,11 @@ jsn_path json path '$.a' default '{"x":33}' on empty,
jexst int exists path '$.b') jexst int exists path '$.b')
) as tt; ) as tt;
id jpath_i jpath_r jsn_path jexst id jpath_i jpath_r jsn_path jexst
1 3 3 3 0 1 3 3 "3" 0
2 2 2 2 0 2 2 2 2 0
3 33 33.3 {"x":33} 1 3 33 33.3 {"x":33} 1
4 0 0.33 0.33 0 4 0 0.33 0.33 0
5 0 0 asd 0 5 0 0 "asd" 0
Warnings: Warnings:
Warning 1366 Incorrect integer value: 'asd' for column ``.`(temporary)`.`jpath_i` at row 5 Warning 1366 Incorrect integer value: 'asd' for column ``.`(temporary)`.`jpath_i` at row 5
Warning 1366 Incorrect double value: 'asd' for column ``.`(temporary)`.`jpath_r` at row 5 Warning 1366 Incorrect double value: 'asd' for column ``.`(temporary)`.`jpath_r` at row 5
...@@ -78,7 +78,7 @@ id jpath jsn_path jexst ...@@ -78,7 +78,7 @@ id jpath jsn_path jexst
2 2 2 0 2 2 2 0
3 33 {"x":33} 1 3 33 {"x":33} 1
4 0 0 0 4 0 0 0
5 66 NULL 0 5 66 [1,2] 0
select * from select * from
json_table( json_table(
'[{"a":"3"},{"a":2},{"b":1},{"a":0}]', '[{"a":"3"},{"a":2},{"b":1},{"a":0}]',
...@@ -88,7 +88,7 @@ json_path json path '$.a', ...@@ -88,7 +88,7 @@ json_path json path '$.a',
jexst int exists path '$.b') jexst int exists path '$.b')
) as tt; ) as tt;
id jpath json_path jexst id jpath json_path jexst
1 3 3 0 1 3 "3" 0
2 2 2 0 2 2 2 0
3 NULL NULL 1 3 NULL NULL 1
4 0 0 0 4 0 0 0
...@@ -315,24 +315,24 @@ id1 jpath jexst id2 id3 jpath_3 id4 jpath_4 ...@@ -315,24 +315,24 @@ id1 jpath jexst id2 id3 jpath_3 id4 jpath_4
1 3 0 2 1 a1 NULL NULL 1 3 0 2 1 a1 NULL NULL
1 3 0 2 2 a2 NULL NULL 1 3 0 2 2 a2 NULL NULL
1 3 0 3 1 c NULL NULL 1 3 0 3 1 c NULL NULL
1 3 0 NULL NULL NULL 1 NULL 1 3 0 NULL NULL NULL 1 {"ll":["b1","b2","b3"]}
1 3 0 NULL NULL NULL 2 NULL 1 3 0 NULL NULL NULL 2 {"ll": ["a1","a2"]}
1 3 0 NULL NULL NULL 3 NULL 1 3 0 NULL NULL NULL 3 {"ll":["c"]}
2 2 0 1 1 1 NULL NULL 2 2 0 1 1 1 NULL NULL
2 2 0 1 2 11 NULL NULL 2 2 0 1 2 11 NULL NULL
2 2 0 1 3 111 NULL NULL 2 2 0 1 3 111 NULL NULL
2 2 0 2 1 2 NULL NULL 2 2 0 2 1 2 NULL NULL
2 2 0 NULL NULL NULL 1 NULL 2 2 0 NULL NULL NULL 1 {"ll":[1,11,111]}
2 2 0 NULL NULL NULL 2 NULL 2 2 0 NULL NULL NULL 2 {"ll":[2]}
3 NULL 1 1 1 zzz NULL NULL 3 NULL 1 1 1 zzz NULL NULL
3 NULL 1 NULL NULL NULL 1 NULL 3 NULL 1 NULL NULL NULL 1 {"ll":["zzz"]}
4 0 0 1 1 0.1 NULL NULL 4 0 0 1 1 0.1 NULL NULL
4 0 0 1 2 0.01 NULL NULL 4 0 0 1 2 0.01 NULL NULL
4 0 0 2 1 0.02 NULL NULL 4 0 0 2 1 0.02 NULL NULL
4 0 0 2 2 0.002 NULL NULL 4 0 0 2 2 0.002 NULL NULL
4 0 0 2 3 0.0002 NULL NULL 4 0 0 2 3 0.0002 NULL NULL
4 0 0 NULL NULL NULL 1 NULL 4 0 0 NULL NULL NULL 1 {"ll":[0.1,0.01]}
4 0 0 NULL NULL NULL 2 NULL 4 0 0 NULL NULL NULL 2 {"ll":[0.02,0.002,0.0002]}
ord should be 1,1,1,2, which tells that first two values of 'l' are ord should be 1,1,1,2, which tells that first two values of 'l' are
from the same object, and next two are from different objects from the same object, and next two are from different objects
SELECT * SELECT *
...@@ -551,10 +551,12 @@ JSON_TABLE('{}', '$' COLUMNS (x INT PATH '$.x' DEFAULT NULL ON ERROR)) jt; ...@@ -551,10 +551,12 @@ JSON_TABLE('{}', '$' COLUMNS (x INT PATH '$.x' DEFAULT NULL ON ERROR)) jt;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NULL ON ERROR)) jt' at line 2 ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'NULL ON ERROR)) jt' at line 2
SELECT * FROM SELECT * FROM
JSON_TABLE('{}', '$' COLUMNS (x INT PATH '$.x' DEFAULT 0 ON EMPTY)) jt; JSON_TABLE('{}', '$' COLUMNS (x INT PATH '$.x' DEFAULT 0 ON EMPTY)) jt;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '0 ON EMPTY)) jt' at line 2 x
0
SELECT * FROM SELECT * FROM
JSON_TABLE('{}', '$' COLUMNS (x INT PATH '$.x' DEFAULT 0 ON ERROR)) jt; JSON_TABLE('{}', '$' COLUMNS (x INT PATH '$.x' DEFAULT 0 ON ERROR)) jt;
ERROR 42000: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '0 ON ERROR)) jt' at line 2 x
NULL
SELECT * FROM SELECT * FROM
JSON_TABLE('{}', '$' COLUMNS (x DATE JSON_TABLE('{}', '$' COLUMNS (x DATE
PATH '$.x' PATH '$.x'
...@@ -604,7 +606,7 @@ SELECT * FROM JSON_TABLE('{"a":"1"}', ...@@ -604,7 +606,7 @@ SELECT * FROM JSON_TABLE('{"a":"1"}',
o FOR ORDINALITY)) AS jt o FOR ORDINALITY)) AS jt
WHERE o = 1; WHERE o = 1;
jpath o jpath o
1 1 "1" 1
# #
# Bug#25427982: ASSERTION `DERIVED' FAILED IN SQL/TABLE.H # Bug#25427982: ASSERTION `DERIVED' FAILED IN SQL/TABLE.H
# #
......
...@@ -814,6 +814,16 @@ select * from json_table('{"a":"foo", "b":1, "c":1000}', '$.*' columns(converted ...@@ -814,6 +814,16 @@ select * from json_table('{"a":"foo", "b":1, "c":1000}', '$.*' columns(converted
select * from json_table('{"a":"foo", "b":1, "c":1000}', '$.*' columns(converted tinyint path '$', original text path '$')) as jt order by original; select * from json_table('{"a":"foo", "b":1, "c":1000}', '$.*' columns(converted tinyint path '$', original text path '$')) as jt order by original;
select * from
json_table('[{"color": "blue", "price": { "high": 10, "low": 5}},
{"color": "white", "price": "pretty low"},
{"color": "yellow", "price": 256.20},
{"color": "red", "price": { "high": 20, "low": 8}}]',
'$[*]' columns(color varchar(100) path '$.color',
price json path '$.price'
)
) as T;
--echo # --echo #
--echo # MDEV-27696 Json table columns accept redundant COLLATE syntax --echo # MDEV-27696 Json table columns accept redundant COLLATE syntax
--echo # --echo #
......
...@@ -453,13 +453,11 @@ SELECT * FROM ...@@ -453,13 +453,11 @@ SELECT * FROM
SELECT * FROM SELECT * FROM
JSON_TABLE('{}', '$' COLUMNS (x INT PATH '$.x' DEFAULT NULL ON ERROR)) jt; JSON_TABLE('{}', '$' COLUMNS (x INT PATH '$.x' DEFAULT NULL ON ERROR)) jt;
# The DEFAULT value must be a string on JSON format for now.
--error 1064
SELECT * FROM SELECT * FROM
JSON_TABLE('{}', '$' COLUMNS (x INT PATH '$.x' DEFAULT 0 ON EMPTY)) jt; JSON_TABLE('{}', '$' COLUMNS (x INT PATH '$.x' DEFAULT 0 ON EMPTY)) jt;
--error 1064
SELECT * FROM SELECT * FROM
JSON_TABLE('{}', '$' COLUMNS (x INT PATH '$.x' DEFAULT 0 ON ERROR)) jt; JSON_TABLE('{}', '$' COLUMNS (x INT PATH '$.x' DEFAULT 0 ON ERROR)) jt;
# We don't accept dates in DEFAULT
--error 1064 --error 1064
SELECT * FROM SELECT * FROM
JSON_TABLE('{}', '$' COLUMNS (x DATE JSON_TABLE('{}', '$' COLUMNS (x DATE
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include "sql_priv.h" #include "sql_priv.h"
#include "sql_class.h" /* TMP_TABLE_PARAM */ #include "sql_class.h" /* TMP_TABLE_PARAM */
#include "table.h" #include "table.h"
#include "sql_type_json.h"
#include "item_jsonfunc.h" #include "item_jsonfunc.h"
#include "json_table.h" #include "json_table.h"
#include "sql_show.h" #include "sql_show.h"
...@@ -377,6 +378,25 @@ static void store_json_in_field(Field *f, const json_engine_t *je) ...@@ -377,6 +378,25 @@ static void store_json_in_field(Field *f, const json_engine_t *je)
} }
static int store_json_in_json(Field *f, json_engine_t *je)
{
const uchar *from= je->value_begin;
const uchar *to;
if (json_value_scalar(je))
to= je->value_end;
else
{
int error;
if ((error= json_skip_level(je)))
return error;
to= je->s.c_str;
}
f->store((const char *) from, (uint32) (to - from), je->s.cs);
return 0;
}
bool Json_table_nested_path::check_error(const char *str) bool Json_table_nested_path::check_error(const char *str)
{ {
if (m_engine.s.error) if (m_engine.s.error)
...@@ -541,7 +561,12 @@ int ha_json_table::fill_column_values(THD *thd, uchar * buf, uchar *pos) ...@@ -541,7 +561,12 @@ int ha_json_table::fill_column_values(THD *thd, uchar * buf, uchar *pos)
} }
else else
{ {
if (!(error= !json_value_scalar(&je))) if (jc->m_format_json)
{
if (!(error= store_json_in_json(*f, &je)))
error= er_handler.errors;
}
else if (!(error= !json_value_scalar(&je)))
{ {
store_json_in_field(*f, &je); store_json_in_field(*f, &je);
error= er_handler.errors; error= er_handler.errors;
...@@ -870,6 +895,10 @@ int Json_table_column::set(THD *thd, enum_type ctype, const LEX_CSTRING &path, ...@@ -870,6 +895,10 @@ int Json_table_column::set(THD *thd, enum_type ctype, const LEX_CSTRING &path,
anctual content. Not sure though if we should. anctual content. Not sure though if we should.
*/ */
m_path.s.c_str= (const uchar *) path.str; m_path.s.c_str= (const uchar *) path.str;
if (ctype == PATH)
m_format_json= m_field->type_handler() == &type_handler_json_longtext;
return 0; return 0;
} }
......
...@@ -147,6 +147,7 @@ class Json_table_column : public Sql_alloc ...@@ -147,6 +147,7 @@ class Json_table_column : public Sql_alloc
}; };
enum_type m_column_type; enum_type m_column_type;
bool m_format_json;
json_path_t m_path; json_path_t m_path;
On_response m_on_error; On_response m_on_error;
On_response m_on_empty; On_response m_on_empty;
......
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