mirror of
https://github.com/MariaDB/server.git
synced 2025-07-29 05:21:33 +03:00
MDEV-6109: EXPLAIN JSON
- Add first testcases - Don't overquote when printing conditions - Other small output fixes
This commit is contained in:
41
mysql-test/r/explain_json.result
Normal file
41
mysql-test/r/explain_json.result
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
drop table if exists t0,t1;
|
||||||
|
create table t0(a int);
|
||||||
|
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
||||||
|
explain format=json select * from t0;
|
||||||
|
EXPLAIN
|
||||||
|
{
|
||||||
|
"query_block": {
|
||||||
|
"select_id": 1,
|
||||||
|
"table": {
|
||||||
|
"table_name": "t0",
|
||||||
|
"access_type": "ALL",
|
||||||
|
"rows": 10,
|
||||||
|
"filtered": 100
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
explain format=json select * from t0 where 1>2;
|
||||||
|
EXPLAIN
|
||||||
|
{
|
||||||
|
"query_block": {
|
||||||
|
"select_id": 1,
|
||||||
|
"table": {
|
||||||
|
"message": "Impossible WHERE"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
explain format=json select * from t0 where a<3;
|
||||||
|
EXPLAIN
|
||||||
|
{
|
||||||
|
"query_block": {
|
||||||
|
"select_id": 1,
|
||||||
|
"table": {
|
||||||
|
"table_name": "t0",
|
||||||
|
"access_type": "ALL",
|
||||||
|
"rows": 10,
|
||||||
|
"filtered": 100,
|
||||||
|
"attached_condition": "(t0.a < 3)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
drop table t0;
|
27
mysql-test/t/explain_json.test
Normal file
27
mysql-test/t/explain_json.test
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#
|
||||||
|
# EXPLAIN FORMAT=JSON tests. These are tests developed for MariaDB.
|
||||||
|
#
|
||||||
|
--disable_warnings
|
||||||
|
drop table if exists t0,t1;
|
||||||
|
--enable_warnings
|
||||||
|
|
||||||
|
create table t0(a int);
|
||||||
|
insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
||||||
|
|
||||||
|
explain format=json select * from t0;
|
||||||
|
|
||||||
|
explain format=json select * from t0 where 1>2;
|
||||||
|
|
||||||
|
explain format=json select * from t0 where a<3;
|
||||||
|
|
||||||
|
#create table t1 (a int, b int, filler char(32), key(a));
|
||||||
|
#insert into t1
|
||||||
|
#select
|
||||||
|
# A.a + B.a* 10 + C.a * 100,
|
||||||
|
# A.a + B.a* 10 + C.a * 100,
|
||||||
|
# 'filler'
|
||||||
|
#from t0 A, t0 B, t0 C;
|
||||||
|
#
|
||||||
|
#explain format=json select * from t0,t1 where t1.a=t0.a;
|
||||||
|
|
||||||
|
drop table t0;
|
@ -2583,7 +2583,13 @@ void Item_ident::print(String *str, enum_query_type query_type)
|
|||||||
}
|
}
|
||||||
if (db_name && db_name[0] && !alias_name_used)
|
if (db_name && db_name[0] && !alias_name_used)
|
||||||
{
|
{
|
||||||
if (!(cached_table && cached_table->belong_to_view &&
|
/*
|
||||||
|
When printing EXPLAIN, don't print database name when it's the same as
|
||||||
|
current database.
|
||||||
|
*/
|
||||||
|
bool skip_db= (query_type & QT_EXPLAIN) && !strcmp(thd->db, db_name);
|
||||||
|
if (!skip_db &&
|
||||||
|
!(cached_table && cached_table->belong_to_view &&
|
||||||
cached_table->belong_to_view->compact_view_format))
|
cached_table->belong_to_view->compact_view_format))
|
||||||
{
|
{
|
||||||
append_identifier(thd, str, d_name, (uint)strlen(d_name));
|
append_identifier(thd, str, d_name, (uint)strlen(d_name));
|
||||||
|
@ -100,7 +100,7 @@ void Json_writer::add_double(double val)
|
|||||||
start_element();
|
start_element();
|
||||||
|
|
||||||
char buf[64];
|
char buf[64];
|
||||||
my_snprintf(buf, sizeof(buf), "%lf", val);
|
my_snprintf(buf, sizeof(buf), "%lg", val);
|
||||||
output.append(buf);
|
output.append(buf);
|
||||||
element_started= false;
|
element_started= false;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
/* Todo: SkySQL copyrights */
|
/* Todo: SkySQL copyrights */
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
A class to write well-formed JSON documents. The documents are also formatted
|
||||||
|
for human readability.
|
||||||
|
*/
|
||||||
|
|
||||||
class Json_writer
|
class Json_writer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -25,7 +31,7 @@ public:
|
|||||||
first_child(true)
|
first_child(true)
|
||||||
{}
|
{}
|
||||||
private:
|
private:
|
||||||
// stack of (name, bool is_object_or_array) elements.
|
// TODO: a stack of (name, bool is_object_or_array) elements.
|
||||||
int indent_level;
|
int indent_level;
|
||||||
enum { INDENT_SIZE = 2 };
|
enum { INDENT_SIZE = 2 };
|
||||||
|
|
||||||
|
@ -611,9 +611,13 @@ enum enum_query_type
|
|||||||
/// Without character set introducers.
|
/// Without character set introducers.
|
||||||
QT_WITHOUT_INTRODUCERS= (1 << 1),
|
QT_WITHOUT_INTRODUCERS= (1 << 1),
|
||||||
/// view internal representation (like QT_ORDINARY except ORDER BY clause)
|
/// view internal representation (like QT_ORDINARY except ORDER BY clause)
|
||||||
QT_VIEW_INTERNAL= (1 << 2)
|
QT_VIEW_INTERNAL= (1 << 2),
|
||||||
|
/// This value means focus on readability, not on ability to parse back, etc.
|
||||||
|
QT_EXPLAIN= (1 << 4)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* query_id */
|
/* query_id */
|
||||||
typedef int64 query_id_t;
|
typedef int64 query_id_t;
|
||||||
extern query_id_t global_query_id;
|
extern query_id_t global_query_id;
|
||||||
|
@ -774,10 +774,17 @@ int Explain_table_access::print_explain(select_result_sink *output, uint8 explai
|
|||||||
|
|
||||||
static void write_item(Json_writer *writer, Item *item)
|
static void write_item(Json_writer *writer, Item *item)
|
||||||
{
|
{
|
||||||
|
THD *thd= current_thd;
|
||||||
char item_buf[256];
|
char item_buf[256];
|
||||||
String str(item_buf, sizeof(item_buf), &my_charset_bin);
|
String str(item_buf, sizeof(item_buf), &my_charset_bin);
|
||||||
str.length(0);
|
str.length(0);
|
||||||
item->print(&str ,QT_ORDINARY);
|
|
||||||
|
ulonglong save_option_bits= thd->variables.option_bits;
|
||||||
|
thd->variables.option_bits &= ~OPTION_QUOTE_SHOW_CREATE;
|
||||||
|
|
||||||
|
item->print(&str, QT_EXPLAIN);
|
||||||
|
|
||||||
|
thd->variables.option_bits= save_option_bits;
|
||||||
writer->add_str(str.c_ptr_safe());
|
writer->add_str(str.c_ptr_safe());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -848,7 +855,8 @@ void Explain_table_access::print_explain_json(Json_writer *writer,
|
|||||||
writer->add_member("key").add_str(key_str);
|
writer->add_member("key").add_str(key_str);
|
||||||
|
|
||||||
/* `used_key_parts` */
|
/* `used_key_parts` */
|
||||||
writer->add_member("used_key_parts").add_str("TODO");
|
if (key_str.length())
|
||||||
|
writer->add_member("used_key_parts").add_str("TODO");
|
||||||
|
|
||||||
StringBuffer<64> key_len_str;
|
StringBuffer<64> key_len_str;
|
||||||
fill_key_len_str(&key_len_str);
|
fill_key_len_str(&key_len_str);
|
||||||
|
@ -5269,21 +5269,9 @@ static bool execute_sqlcom_select(THD *thd, TABLE_LIST *all_tables)
|
|||||||
top-level LIMIT
|
top-level LIMIT
|
||||||
*/
|
*/
|
||||||
result->reset_offset_limit();
|
result->reset_offset_limit();
|
||||||
if (thd->lex->explain_json)
|
if (lex->explain_json)
|
||||||
{
|
{
|
||||||
/*
|
lex->explain->print_explain_json(result, lex->analyze_stmt);
|
||||||
Json_writer writer;
|
|
||||||
writer.start_object();
|
|
||||||
thd->lex->explain->print_explain_json(&writer, thd->lex->analyze_stmt);
|
|
||||||
writer.end_object();
|
|
||||||
|
|
||||||
const CHARSET_INFO *cs= system_charset_info;
|
|
||||||
List<Item> item_list;
|
|
||||||
String *buf= &writer.output;
|
|
||||||
item_list.push_back(new Item_string(buf->ptr(), buf->length(), cs));
|
|
||||||
result->send_data(item_list);
|
|
||||||
*/
|
|
||||||
thd->lex->explain->print_explain_json(result, thd->lex->analyze_stmt);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user