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.
This commit is contained in:
Rucha Deodhar 2025-10-09 11:58:58 +05:30
parent 23af68bbbf
commit 85567aba45
4 changed files with 98 additions and 4 deletions

View File

@ -1216,6 +1216,7 @@ ERROR 42000: You have an error in your SQL syntax; check the manual that corresp
#
# MDEV-27898 CREATE VIEW AS SELECT FROM JSON_TABLE column requires global privileges
#
# Beginning of 10.11 tests
create view v1 as (select * from
json_table('[{"a":"1"}]', '$[*]' columns(a int path '$.a') ) as jt);
create user u1@localhost;
@ -1228,4 +1229,55 @@ connection default;
DROP VIEW v2;
DROP VIEW v1;
DROP USER u1@localhost;
# End of 10.11 tests
#
# MDEV-34081: View containing JSON_TABLE does not return JSON
#
CREATE OR REPLACE VIEW test_view AS SELECT * FROM JSON_TABLE('
[
{
"caption": "First Element",
"value": 1
},
{
"caption": "Second Element",
"value": 2
}
]
', '$[*]' COLUMNS(
caption VARCHAR(200) PATH '$.caption',
whole_block JSON PATH '$')) t;
SELECT * FROM test_view;
caption whole_block
First Element {
"caption": "First Element",
"value": 1
}
Second Element {
"caption": "Second Element",
"value": 2
}
SELECT * FROM JSON_TABLE('
[
{
"caption": "First Element",
"value": 1
},
{
"caption": "Second Element",
"value": 2
}
]
', '$[*]' COLUMNS(
caption VARCHAR(200) PATH '$.caption',
whole_block JSON PATH '$')) t;
caption whole_block
First Element {
"caption": "First Element",
"value": 1
}
Second Element {
"caption": "Second Element",
"value": 2
}
DROP VIEW test_view;
# End of 10.11 test

View File

@ -1046,6 +1046,8 @@ COLUMNS
--echo # MDEV-27898 CREATE VIEW AS SELECT FROM JSON_TABLE column requires global privileges
--echo #
--echo # Beginning of 10.11 tests
create view v1 as (select * from
json_table('[{"a":"1"}]', '$[*]' columns(a int path '$.a') ) as jt);
@ -1063,4 +1065,42 @@ DROP VIEW v2;
DROP VIEW v1;
DROP USER u1@localhost;
--echo # End of 10.11 tests
--echo #
--echo # MDEV-34081: View containing JSON_TABLE does not return JSON
--echo #
CREATE OR REPLACE VIEW test_view AS SELECT * FROM JSON_TABLE('
[
{
"caption": "First Element",
"value": 1
},
{
"caption": "Second Element",
"value": 2
}
]
', '$[*]' COLUMNS(
caption VARCHAR(200) PATH '$.caption',
whole_block JSON PATH '$')) t;
SELECT * FROM test_view;
SELECT * FROM JSON_TABLE('
[
{
"caption": "First Element",
"value": 1
},
{
"caption": "Second Element",
"value": 2
}
]
', '$[*]' COLUMNS(
caption VARCHAR(200) PATH '$.caption',
whole_block JSON PATH '$')) t;
DROP VIEW test_view;
--echo # End of 10.11 test

View File

@ -970,8 +970,9 @@ int Json_table_column::print(THD *thd, Field **f, String *str)
(*f)->sql_type(column_type);
if (str->append(column_type) ||
((*f)->has_charset() && m_explicit_cs &&
if ((m_format_json ? str->append(STRING_WITH_LEN(" JSON ")) : str->append(column_type)))
return 1;
if (((*f)->has_charset() && m_explicit_cs &&
(str->append(STRING_WITH_LEN(" CHARSET ")) ||
str->append(&m_explicit_cs->cs_name) ||
(Charset(m_explicit_cs).can_have_collate_clause() &&

View File

@ -167,6 +167,7 @@ public:
{
m_on_error.m_response= RESPONSE_NOT_SPECIFIED;
m_on_empty.m_response= RESPONSE_NOT_SPECIFIED;
m_format_json= false;
}
int print(THD *tnd, Field **f, String *str);
};