summaryrefslogtreecommitdiff
path: root/sql
diff options
context:
space:
mode:
Diffstat (limited to 'sql')
-rw-r--r--sql/handler.h3
-rw-r--r--sql/sql_base.cc2
-rw-r--r--sql/sql_explain.cc5
-rw-r--r--sql/sql_explain.h2
-rw-r--r--sql/sql_lex.cc7
-rw-r--r--sql/sql_lex.h2
-rw-r--r--sql/sql_show.cc32
-rw-r--r--sql/sql_show.h3
-rw-r--r--sql/sql_yacc.yy14
9 files changed, 53 insertions, 17 deletions
diff --git a/sql/handler.h b/sql/handler.h
index fe61666bf20..f0ccb048a21 100644
--- a/sql/handler.h
+++ b/sql/handler.h
@@ -1006,7 +1006,8 @@ enum enum_schema_tables
SCH_ENABLED_ROLES,
SCH_ENGINES,
SCH_EVENTS,
- SCH_EXPLAIN,
+ SCH_EXPLAIN_TABULAR,
+ SCH_EXPLAIN_JSON,
SCH_FILES,
SCH_GLOBAL_STATUS,
SCH_GLOBAL_VARIABLES,
diff --git a/sql/sql_base.cc b/sql/sql_base.cc
index d983fe9a332..7ddff6d627b 100644
--- a/sql/sql_base.cc
+++ b/sql/sql_base.cc
@@ -8837,6 +8837,8 @@ fill_record(THD *thd, TABLE *table, Field **ptr, List<Item> &values,
continue;
value=v++;
+ /* Ensure the end of the list of values is not reached */
+ DBUG_ASSERT(value);
bool vers_sys_field= table->versioned() && field->vers_sys_field();
diff --git a/sql/sql_explain.cc b/sql/sql_explain.cc
index 4fd4e6d3b77..722fcd8a151 100644
--- a/sql/sql_explain.cc
+++ b/sql/sql_explain.cc
@@ -215,7 +215,7 @@ int Explain_query::print_explain(select_result_sink *output,
}
-void Explain_query::print_explain_json(select_result_sink *output,
+int Explain_query::print_explain_json(select_result_sink *output,
bool is_analyze)
{
Json_writer writer;
@@ -230,7 +230,7 @@ void Explain_query::print_explain_json(select_result_sink *output,
/* Start printing from node with id=1 */
Explain_node *node= get_node(1);
if (!node)
- return; /* No query plan */
+ return 1; /* No query plan */
node->print_explain_json(this, &writer, is_analyze);
}
@@ -243,6 +243,7 @@ void Explain_query::print_explain_json(select_result_sink *output,
Item_string(thd, buf->ptr(), buf->length(), cs),
thd->mem_root);
output->send_data(item_list);
+ return 0;
}
diff --git a/sql/sql_explain.h b/sql/sql_explain.h
index 88fae9d4f1a..94d0f667cf9 100644
--- a/sql/sql_explain.h
+++ b/sql/sql_explain.h
@@ -478,7 +478,7 @@ public:
/* Return tabular EXPLAIN output as a text string */
bool print_explain_str(THD *thd, String *out_str, bool is_analyze);
- void print_explain_json(select_result_sink *output, bool is_analyze);
+ int print_explain_json(select_result_sink *output, bool is_analyze);
/* If true, at least part of EXPLAIN can be printed */
bool have_query_plan() { return insert_plan || upd_del_plan|| get_node(1) != NULL; }
diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc
index b5f8cf4a886..0bbe5b02460 100644
--- a/sql/sql_lex.cc
+++ b/sql/sql_lex.cc
@@ -5825,12 +5825,15 @@ bool st_select_lex::is_merged_child_of(st_select_lex *ancestor)
*/
int LEX::print_explain(select_result_sink *output, uint8 explain_flags,
- bool is_analyze, bool *printed_anything)
+ bool is_analyze, bool is_json_format, bool *printed_anything)
{
int res;
if (explain && explain->have_query_plan())
{
- res= explain->print_explain(output, explain_flags, is_analyze);
+ if (is_json_format)
+ res= explain->print_explain_json(output, is_analyze);
+ else
+ res= explain->print_explain(output, explain_flags, is_analyze);
*printed_anything= true;
}
else
diff --git a/sql/sql_lex.h b/sql/sql_lex.h
index 14cf90caa04..1ec855253a6 100644
--- a/sql/sql_lex.h
+++ b/sql/sql_lex.h
@@ -3792,7 +3792,7 @@ public:
bool save_prep_leaf_tables();
int print_explain(select_result_sink *output, uint8 explain_flags,
- bool is_analyze, bool *printed_anything);
+ bool is_analyze, bool is_json_format, bool *printed_anything);
void restore_set_statement_var();
void init_last_field(Column_definition *field, const LEX_CSTRING *name,
diff --git a/sql/sql_show.cc b/sql/sql_show.cc
index d01f84fe7d1..0247b11fbfa 100644
--- a/sql/sql_show.cc
+++ b/sql/sql_show.cc
@@ -2998,7 +2998,8 @@ void Show_explain_request::call_in_target_thread()
DBUG_ASSERT(current_thd == target_thd);
set_current_thd(request_thd);
if (target_thd->lex->print_explain(explain_buf, 0 /* explain flags*/,
- false /*TODO: analyze? */, &printed_anything))
+ false /*TODO: analyze? */,
+ is_json_format, &printed_anything))
{
failed_to_produce= TRUE;
}
@@ -3119,7 +3120,8 @@ void select_result_text_buffer::save_to(String *res)
Store the SHOW EXPLAIN output in the temporary table.
*/
-int fill_show_explain(THD *thd, TABLE_LIST *table, COND *cond)
+int fill_show_explain(THD *thd, TABLE_LIST *table, COND *cond,
+ bool json_format)
{
const char *calling_user;
THD *tmp;
@@ -3164,6 +3166,7 @@ int fill_show_explain(THD *thd, TABLE_LIST *table, COND *cond)
bool timed_out;
int timeout_sec= 30;
Show_explain_request explain_req;
+ explain_req.is_json_format= json_format;
select_result_explain_buffer *explain_buf;
explain_buf= new select_result_explain_buffer(thd, table->table);
@@ -3226,6 +3229,18 @@ int fill_show_explain(THD *thd, TABLE_LIST *table, COND *cond)
}
+int fill_show_explain_tabular(THD *thd, TABLE_LIST *table, COND *cond)
+{
+ return fill_show_explain(thd, table, cond, false /*json_format*/);
+}
+
+
+int fill_show_explain_json(THD *thd, TABLE_LIST *table, COND *cond)
+{
+ return fill_show_explain(thd, table, cond, true /*json_format*/);
+}
+
+
struct processlist_callback_arg
{
processlist_callback_arg(THD *thd_arg, TABLE *table_arg):
@@ -9615,7 +9630,7 @@ ST_FIELD_INFO keycache_fields_info[]=
};
-ST_FIELD_INFO show_explain_fields_info[]=
+ST_FIELD_INFO show_explain_tabular_fields_info[]=
{
Column("id", SLonglong(3), NULLABLE, "id"),
Column("select_type", Varchar(19), NOT_NULL, "select_type"),
@@ -9631,6 +9646,13 @@ ST_FIELD_INFO show_explain_fields_info[]=
};
+ST_FIELD_INFO show_explain_json_fields_info[]=
+{
+ Column("EXPLAIN", Longtext(MAX_FIELD_VARCHARLENGTH), NOT_NULL, "EXPLAIN"),
+ CEnd()
+};
+
+
ST_FIELD_INFO check_constraints_fields_info[]=
{
Column("CONSTRAINT_CATALOG", Catalog(), NOT_NULL, OPEN_FULL_TABLE),
@@ -9691,8 +9713,10 @@ ST_SCHEMA_TABLE schema_tables[]=
{"EVENTS", Show::events_fields_info, 0,
0, make_old_format, 0, -1, -1, 0, 0},
#endif
- {"EXPLAIN", Show::show_explain_fields_info, 0, fill_show_explain,
+ {"EXPLAIN", Show::show_explain_tabular_fields_info, 0, fill_show_explain_tabular,
make_old_format, 0, -1, -1, TRUE /*hidden*/ , 0},
+ {"EXPLAIN_JSON", Show::show_explain_json_fields_info, 0, fill_show_explain_json,
+ make_old_format, 0, -1, -1, TRUE /*hidden*/, 0},
{"FILES", Show::files_fields_info, 0,
hton_fill_schema_table, 0, 0, -1, -1, 0, 0},
{"GLOBAL_STATUS", Show::variables_fields_info, 0,
diff --git a/sql/sql_show.h b/sql/sql_show.h
index 3d7a4d1146c..8c8ae416699 100644
--- a/sql/sql_show.h
+++ b/sql/sql_show.h
@@ -164,6 +164,9 @@ public:
THD *target_thd; /* thd that we're running SHOW EXPLAIN for */
THD *request_thd; /* thd that run SHOW EXPLAIN command */
+ bool is_json_format= false; /* set to TRUE if you need the result in JSON
+ format, FALSE - in traditional tabular */
+
/* If true, there was some error when producing EXPLAIN output. */
bool failed_to_produce;
diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy
index 74757970411..6df21338881 100644
--- a/sql/sql_yacc.yy
+++ b/sql/sql_yacc.yy
@@ -13945,12 +13945,13 @@ show_param:
Lex->spname= $3;
Lex->sql_command = SQLCOM_SHOW_CREATE_EVENT;
}
- | describe_command FOR_SYM expr
+ | describe_command opt_format_json FOR_SYM expr
{
Lex->sql_command= SQLCOM_SHOW_EXPLAIN;
- if (unlikely(prepare_schema_table(thd, Lex, 0, SCH_EXPLAIN)))
+ if (unlikely(prepare_schema_table(thd, Lex, 0,
+ Lex->explain_json ? SCH_EXPLAIN_JSON : SCH_EXPLAIN_TABULAR)))
MYSQL_YYABORT;
- add_value_to_list(thd, $3);
+ add_value_to_list(thd, $4);
}
| IDENT_sys remember_tok_start wild_and_where
{
@@ -14123,12 +14124,13 @@ opt_describe_column:
;
explain_for_connection:
- describe_command FOR_SYM CONNECTION_SYM expr
+ describe_command opt_format_json FOR_SYM CONNECTION_SYM expr
{
Lex->sql_command= SQLCOM_SHOW_EXPLAIN;
- if (unlikely(prepare_schema_table(thd, Lex, 0, SCH_EXPLAIN)))
+ if (unlikely(prepare_schema_table(thd, Lex, 0,
+ Lex->explain_json ? SCH_EXPLAIN_JSON : SCH_EXPLAIN_TABULAR)))
MYSQL_YYABORT;
- add_value_to_list(thd, $4);
+ add_value_to_list(thd, $5);
}
;