From 85567aba450dcd5efe2506c67222ef7b82b6ceda Mon Sep 17 00:00:00 2001 From: Rucha Deodhar Date: Thu, 9 Oct 2025 11:58:58 +0530 Subject: [PATCH] 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. --- mysql-test/suite/json/r/json_table.result | 54 ++++++++++++++++++++++- mysql-test/suite/json/t/json_table.test | 42 +++++++++++++++++- sql/json_table.cc | 5 ++- sql/json_table.h | 1 + 4 files changed, 98 insertions(+), 4 deletions(-) diff --git a/mysql-test/suite/json/r/json_table.result b/mysql-test/suite/json/r/json_table.result index 3bee1e06abb..9f874d6264e 100644 --- a/mysql-test/suite/json/r/json_table.result +++ b/mysql-test/suite/json/r/json_table.result @@ -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 diff --git a/mysql-test/suite/json/t/json_table.test b/mysql-test/suite/json/t/json_table.test index bbb6db9cc7b..aac9005cbd6 100644 --- a/mysql-test/suite/json/t/json_table.test +++ b/mysql-test/suite/json/t/json_table.test @@ -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 diff --git a/sql/json_table.cc b/sql/json_table.cc index bb126f36b85..d3a81cc61d1 100644 --- a/sql/json_table.cc +++ b/sql/json_table.cc @@ -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() && diff --git a/sql/json_table.h b/sql/json_table.h index f588b6e08b0..01b97377e4a 100644 --- a/sql/json_table.h +++ b/sql/json_table.h @@ -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); };