Skip to content

Commit 85567ab

Browse files
MDEV-34081: View containing JSON_TABLE does not return JSON
Analysis: While writing the view to .FRM file, we check the datatype of each column and append the appropriate type to the string (which will be written to the frm). This is where the conversion from JSON to longtext happens because that is how it is stored internally. Now, while SELECT, when the frm is read it has longtext instead of JSON which also results in changing the handler type. Since the handler types dont match, m_format_json becomes false for that specific column. Now, when filling the values, since the format is not json, it does not get added in the result. Hence the output is NULL. Fix: Before writing the view to the FRM file, check if the datatype for the column is JSON (which means the m_format_json will be true). If it is JSON append JSON.
1 parent 23af68b commit 85567ab

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed

mysql-test/suite/json/r/json_table.result

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,7 @@ ERROR 42000: You have an error in your SQL syntax; check the manual that corresp
12161216
#
12171217
# MDEV-27898 CREATE VIEW AS SELECT FROM JSON_TABLE column requires global privileges
12181218
#
1219+
# Beginning of 10.11 tests
12191220
create view v1 as (select * from
12201221
json_table('[{"a":"1"}]', '$[*]' columns(a int path '$.a') ) as jt);
12211222
create user u1@localhost;
@@ -1228,4 +1229,55 @@ connection default;
12281229
DROP VIEW v2;
12291230
DROP VIEW v1;
12301231
DROP USER u1@localhost;
1231-
# End of 10.11 tests
1232+
#
1233+
# MDEV-34081: View containing JSON_TABLE does not return JSON
1234+
#
1235+
CREATE OR REPLACE VIEW test_view AS SELECT * FROM JSON_TABLE('
1236+
[
1237+
{
1238+
"caption": "First Element",
1239+
"value": 1
1240+
},
1241+
{
1242+
"caption": "Second Element",
1243+
"value": 2
1244+
}
1245+
]
1246+
', '$[*]' COLUMNS(
1247+
caption VARCHAR(200) PATH '$.caption',
1248+
whole_block JSON PATH '$')) t;
1249+
SELECT * FROM test_view;
1250+
caption whole_block
1251+
First Element {
1252+
"caption": "First Element",
1253+
"value": 1
1254+
}
1255+
Second Element {
1256+
"caption": "Second Element",
1257+
"value": 2
1258+
}
1259+
SELECT * FROM JSON_TABLE('
1260+
[
1261+
{
1262+
"caption": "First Element",
1263+
"value": 1
1264+
},
1265+
{
1266+
"caption": "Second Element",
1267+
"value": 2
1268+
}
1269+
]
1270+
', '$[*]' COLUMNS(
1271+
caption VARCHAR(200) PATH '$.caption',
1272+
whole_block JSON PATH '$')) t;
1273+
caption whole_block
1274+
First Element {
1275+
"caption": "First Element",
1276+
"value": 1
1277+
}
1278+
Second Element {
1279+
"caption": "Second Element",
1280+
"value": 2
1281+
}
1282+
DROP VIEW test_view;
1283+
# End of 10.11 test

mysql-test/suite/json/t/json_table.test

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,8 @@ COLUMNS
10461046
--echo # MDEV-27898 CREATE VIEW AS SELECT FROM JSON_TABLE column requires global privileges
10471047
--echo #
10481048

1049+
--echo # Beginning of 10.11 tests
1050+
10491051
create view v1 as (select * from
10501052
json_table('[{"a":"1"}]', '$[*]' columns(a int path '$.a') ) as jt);
10511053

@@ -1063,4 +1065,42 @@ DROP VIEW v2;
10631065
DROP VIEW v1;
10641066
DROP USER u1@localhost;
10651067

1066-
--echo # End of 10.11 tests
1068+
--echo #
1069+
--echo # MDEV-34081: View containing JSON_TABLE does not return JSON
1070+
--echo #
1071+
1072+
CREATE OR REPLACE VIEW test_view AS SELECT * FROM JSON_TABLE('
1073+
[
1074+
{
1075+
"caption": "First Element",
1076+
"value": 1
1077+
},
1078+
{
1079+
"caption": "Second Element",
1080+
"value": 2
1081+
}
1082+
]
1083+
', '$[*]' COLUMNS(
1084+
caption VARCHAR(200) PATH '$.caption',
1085+
whole_block JSON PATH '$')) t;
1086+
1087+
SELECT * FROM test_view;
1088+
1089+
SELECT * FROM JSON_TABLE('
1090+
[
1091+
{
1092+
"caption": "First Element",
1093+
"value": 1
1094+
},
1095+
{
1096+
"caption": "Second Element",
1097+
"value": 2
1098+
}
1099+
]
1100+
', '$[*]' COLUMNS(
1101+
caption VARCHAR(200) PATH '$.caption',
1102+
whole_block JSON PATH '$')) t;
1103+
1104+
DROP VIEW test_view;
1105+
1106+
--echo # End of 10.11 test

sql/json_table.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,8 +970,9 @@ int Json_table_column::print(THD *thd, Field **f, String *str)
970970

971971
(*f)->sql_type(column_type);
972972

973-
if (str->append(column_type) ||
974-
((*f)->has_charset() && m_explicit_cs &&
973+
if ((m_format_json ? str->append(STRING_WITH_LEN(" JSON ")) : str->append(column_type)))
974+
return 1;
975+
if (((*f)->has_charset() && m_explicit_cs &&
975976
(str->append(STRING_WITH_LEN(" CHARSET ")) ||
976977
str->append(&m_explicit_cs->cs_name) ||
977978
(Charset(m_explicit_cs).can_have_collate_clause() &&

sql/json_table.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ class Json_table_column : public Sql_alloc
167167
{
168168
m_on_error.m_response= RESPONSE_NOT_SPECIFIED;
169169
m_on_empty.m_response= RESPONSE_NOT_SPECIFIED;
170+
m_format_json= false;
170171
}
171172
int print(THD *tnd, Field **f, String *str);
172173
};

0 commit comments

Comments
 (0)