@@ -418,7 +418,7 @@ static void store_json_in_field(Field *f, const json_engine_t *je)
418418}
419419
420420
421- static int store_json_in_json (Field *f, json_engine_t *je)
421+ static int store_json_in_field_as_json (Field *f, json_engine_t *je)
422422{
423423 const uchar *from= je->value_begin ;
424424 const uchar *to;
@@ -617,7 +617,7 @@ int ha_json_table::fill_column_values(THD *thd, uchar * buf, uchar *pos)
617617 {
618618 if (jc->m_format_json )
619619 {
620- if (!(error= store_json_in_json (*f, &je)))
620+ if (!(error= store_json_in_field_as_json (*f, &je)))
621621 error= er_handler.errors ;
622622 }
623623 else if (!(error= !json_value_scalar (&je)))
@@ -931,6 +931,21 @@ TABLE *create_table_for_function(THD *thd, TABLE_LIST *sql_table)
931931}
932932
933933
934+ bool Json_table_column::enable_format_json ()
935+ {
936+ if (!m_field->type_handler ()->is_general_purpose_string_type ())
937+ {
938+ my_error (ER_WRONG_USAGE, MYF (0 ), " FORMAT JSON" ,
939+ " non-string JSON_TABLE column" );
940+ return true ;
941+ }
942+
943+ m_format_json= true ;
944+ m_explicit_format_json= true ;
945+ return false ;
946+ }
947+
948+
934949int Json_table_column::set (THD *thd, enum_type ctype, const LEX_CSTRING &path,
935950 CHARSET_INFO *cs)
936951{
@@ -955,8 +970,11 @@ int Json_table_column::set(THD *thd, enum_type ctype, const LEX_CSTRING &path,
955970 */
956971 m_path.s .c_str = (const uchar *) path.str ;
957972
958- if (ctype == PATH)
959- m_format_json= m_field->type_handler () == &type_handler_long_blob_json;
973+ if (ctype == PATH &&
974+ Type_handler_json_common::is_json_type_handler (m_field->type_handler ()))
975+ {
976+ m_format_json= true ;
977+ }
960978
961979 return 0 ;
962980}
@@ -1018,17 +1036,22 @@ int Json_table_column::print(THD *thd, Field **f, String *str)
10181036 {
10191037 static const LEX_CSTRING path= { STRING_WITH_LEN (" PATH " ) };
10201038 static const LEX_CSTRING exists_path= { STRING_WITH_LEN (" EXISTS PATH " ) };
1039+ bool is_json_type=
1040+ Type_handler_json_common::is_json_type_handler ((*f)->type_handler ());
10211041
10221042 (*f)->sql_type (column_type);
10231043
1024- if ((m_format_json ? str->append (STRING_WITH_LEN (" JSON " )) : str->append (column_type)))
1044+ if (is_json_type ? str->append (STRING_WITH_LEN (" JSON" )) :
1045+ str->append (column_type))
10251046 return 1 ;
10261047 if (((*f)->has_charset () && m_explicit_cs &&
10271048 (str->append (STRING_WITH_LEN (" CHARSET " )) ||
10281049 str->append (&m_explicit_cs->cs_name ) ||
10291050 (Charset (m_explicit_cs).can_have_collate_clause () &&
10301051 (str->append (STRING_WITH_LEN (" COLLATE " )) ||
10311052 str->append (&m_explicit_cs->coll_name ))))) ||
1053+ (m_explicit_format_json &&
1054+ str->append (STRING_WITH_LEN (" FORMAT JSON" ))) ||
10321055 str->append (m_column_type == PATH ? &path : &exists_path) ||
10331056 print_path (str, &m_path))
10341057 return 1 ;
0 commit comments