[Commits] 346c8ab9533: MDEV-11975: SQLCOM_PREPARE of EXPLAIN & ANALYZE statement do not return correct metadata info
revision-id: 346c8ab9533a3de6a4cb348428402ffee2aa8da2 (mariadb-10.3.6-16-g346c8ab9533) parent(s): cff60be7fe159fdcb2517ce8441610ad512aa7d0 author: Oleksandr Byelkin committer: Oleksandr Byelkin timestamp: 2018-04-18 19:34:12 +0200 message: MDEV-11975: SQLCOM_PREPARE of EXPLAIN & ANALYZE statement do not return correct metadata info Added metadate info after prepare EXPLAIN/ANALYZE. --- mysql-test/main/mysql_client_test.result | 122 ++++++++++++ mysql-test/main/mysql_client_test.test | 7 +- sql/sql_class.cc | 18 +- sql/sql_class.h | 3 +- sql/sql_explain.cc | 3 +- sql/sql_parse.cc | 5 +- sql/sql_prepare.cc | 24 ++- sql/sql_prepare.h | 2 + tests/mysql_client_test.c | 323 ++++++++++++++++++++++++++++++- 9 files changed, 493 insertions(+), 14 deletions(-) diff --git a/mysql-test/main/mysql_client_test.result b/mysql-test/main/mysql_client_test.result index 83ef8d442b3..20385acfa00 100644 --- a/mysql-test/main/mysql_client_test.result +++ b/mysql-test/main/mysql_client_test.result @@ -122,5 +122,127 @@ EOF mysql_stmt_next_result(): 0; field_count: 0 # ------------------------------------ +# cat MYSQL_TMP_DIR/test_explain_meta.out.log +# ------------------------------------ +SELECT number of fields: 1 +EXPALIN number of fields: 10 + - 0: name: 'id'/''; table: ''/''; db: ''; catalog: 'def'; length: 3; max_length: 0; type: 8; decimals: 0 + - 1: name: 'select_type'/''; table: ''/''; db: ''; catalog: 'def'; length: 57; max_length: 0; type: 253; decimals: 39 + - 2: name: 'table'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 3: name: 'type'/''; table: ''/''; db: ''; catalog: 'def'; length: 30; max_length: 0; type: 253; decimals: 39 + - 4: name: 'possible_keys'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 5: name: 'key'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 6: name: 'key_len'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 7: name: 'ref'/''; table: ''/''; db: ''; catalog: 'def'; length: 6144; max_length: 0; type: 253; decimals: 39 + - 8: name: 'rows'/''; table: ''/''; db: ''; catalog: 'def'; length: 10; max_length: 0; type: 8; decimals: 0 + - 9: name: 'Extra'/''; table: ''/''; db: ''; catalog: 'def'; length: 765; max_length: 0; type: 253; decimals: 39 +EXPALIN JSON number of fields: 1 + - 0: name: 'EXPLAIN'/''; table: ''/''; db: ''; catalog: 'def'; length: 234; max_length: 0; type: 253; decimals: 39 +ANALYZE number of fields: 13 + - 0: name: 'id'/''; table: ''/''; db: ''; catalog: 'def'; length: 3; max_length: 0; type: 8; decimals: 0 + - 1: name: 'select_type'/''; table: ''/''; db: ''; catalog: 'def'; length: 57; max_length: 0; type: 253; decimals: 39 + - 2: name: 'table'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 3: name: 'type'/''; table: ''/''; db: ''; catalog: 'def'; length: 30; max_length: 0; type: 253; decimals: 39 + - 4: name: 'possible_keys'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 5: name: 'key'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 6: name: 'key_len'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 7: name: 'ref'/''; table: ''/''; db: ''; catalog: 'def'; length: 6144; max_length: 0; type: 253; decimals: 39 + - 8: name: 'rows'/''; table: ''/''; db: ''; catalog: 'def'; length: 10; max_length: 0; type: 8; decimals: 0 + - 9: name: 'r_rows'/''; table: ''/''; db: ''; catalog: 'def'; length: 4; max_length: 0; type: 5; decimals: 10 + - 10: name: 'filtered'/''; table: ''/''; db: ''; catalog: 'def'; length: 4; max_length: 0; type: 5; decimals: 2 + - 11: name: 'r_filtered'/''; table: ''/''; db: ''; catalog: 'def'; length: 4; max_length: 0; type: 5; decimals: 2 + - 12: name: 'Extra'/''; table: ''/''; db: ''; catalog: 'def'; length: 765; max_length: 0; type: 253; decimals: 39 +ANALYZE JSON number of fields: 1 + - 0: name: 'ANALYZE'/''; table: ''/''; db: ''; catalog: 'def'; length: 234; max_length: 0; type: 253; decimals: 39 +EXPALIN INSERT number of fields: 10 + - 0: name: 'id'/''; table: ''/''; db: ''; catalog: 'def'; length: 3; max_length: 0; type: 8; decimals: 0 + - 1: name: 'select_type'/''; table: ''/''; db: ''; catalog: 'def'; length: 57; max_length: 0; type: 253; decimals: 39 + - 2: name: 'table'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 3: name: 'type'/''; table: ''/''; db: ''; catalog: 'def'; length: 30; max_length: 0; type: 253; decimals: 39 + - 4: name: 'possible_keys'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 5: name: 'key'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 6: name: 'key_len'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 7: name: 'ref'/''; table: ''/''; db: ''; catalog: 'def'; length: 6144; max_length: 0; type: 253; decimals: 39 + - 8: name: 'rows'/''; table: ''/''; db: ''; catalog: 'def'; length: 10; max_length: 0; type: 8; decimals: 0 + - 9: name: 'Extra'/''; table: ''/''; db: ''; catalog: 'def'; length: 765; max_length: 0; type: 253; decimals: 39 +EXPALIN JSON INSERT number of fields: 1 + - 0: name: 'EXPLAIN'/''; table: ''/''; db: ''; catalog: 'def'; length: 234; max_length: 0; type: 253; decimals: 39 +ANALYZE INSERT number of fields: 13 + - 0: name: 'id'/''; table: ''/''; db: ''; catalog: 'def'; length: 3; max_length: 0; type: 8; decimals: 0 + - 1: name: 'select_type'/''; table: ''/''; db: ''; catalog: 'def'; length: 57; max_length: 0; type: 253; decimals: 39 + - 2: name: 'table'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 3: name: 'type'/''; table: ''/''; db: ''; catalog: 'def'; length: 30; max_length: 0; type: 253; decimals: 39 + - 4: name: 'possible_keys'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 5: name: 'key'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 6: name: 'key_len'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 7: name: 'ref'/''; table: ''/''; db: ''; catalog: 'def'; length: 6144; max_length: 0; type: 253; decimals: 39 + - 8: name: 'rows'/''; table: ''/''; db: ''; catalog: 'def'; length: 10; max_length: 0; type: 8; decimals: 0 + - 9: name: 'r_rows'/''; table: ''/''; db: ''; catalog: 'def'; length: 4; max_length: 0; type: 5; decimals: 10 + - 10: name: 'filtered'/''; table: ''/''; db: ''; catalog: 'def'; length: 4; max_length: 0; type: 5; decimals: 2 + - 11: name: 'r_filtered'/''; table: ''/''; db: ''; catalog: 'def'; length: 4; max_length: 0; type: 5; decimals: 2 + - 12: name: 'Extra'/''; table: ''/''; db: ''; catalog: 'def'; length: 765; max_length: 0; type: 253; decimals: 39 +ANALYZE JSON INSERT number of fields: 1 + - 0: name: 'ANALYZE'/''; table: ''/''; db: ''; catalog: 'def'; length: 234; max_length: 0; type: 253; decimals: 39 +EXPALIN UPDATE number of fields: 10 + - 0: name: 'id'/''; table: ''/''; db: ''; catalog: 'def'; length: 3; max_length: 0; type: 8; decimals: 0 + - 1: name: 'select_type'/''; table: ''/''; db: ''; catalog: 'def'; length: 57; max_length: 0; type: 253; decimals: 39 + - 2: name: 'table'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 3: name: 'type'/''; table: ''/''; db: ''; catalog: 'def'; length: 30; max_length: 0; type: 253; decimals: 39 + - 4: name: 'possible_keys'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 5: name: 'key'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 6: name: 'key_len'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 7: name: 'ref'/''; table: ''/''; db: ''; catalog: 'def'; length: 6144; max_length: 0; type: 253; decimals: 39 + - 8: name: 'rows'/''; table: ''/''; db: ''; catalog: 'def'; length: 10; max_length: 0; type: 8; decimals: 0 + - 9: name: 'Extra'/''; table: ''/''; db: ''; catalog: 'def'; length: 765; max_length: 0; type: 253; decimals: 39 +EXPALIN JSON UPDATE number of fields: 1 + - 0: name: 'EXPLAIN'/''; table: ''/''; db: ''; catalog: 'def'; length: 234; max_length: 0; type: 253; decimals: 39 +ANALYZE UPDATE number of fields: 13 + - 0: name: 'id'/''; table: ''/''; db: ''; catalog: 'def'; length: 3; max_length: 0; type: 8; decimals: 0 + - 1: name: 'select_type'/''; table: ''/''; db: ''; catalog: 'def'; length: 57; max_length: 0; type: 253; decimals: 39 + - 2: name: 'table'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 3: name: 'type'/''; table: ''/''; db: ''; catalog: 'def'; length: 30; max_length: 0; type: 253; decimals: 39 + - 4: name: 'possible_keys'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 5: name: 'key'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 6: name: 'key_len'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 7: name: 'ref'/''; table: ''/''; db: ''; catalog: 'def'; length: 6144; max_length: 0; type: 253; decimals: 39 + - 8: name: 'rows'/''; table: ''/''; db: ''; catalog: 'def'; length: 10; max_length: 0; type: 8; decimals: 0 + - 9: name: 'r_rows'/''; table: ''/''; db: ''; catalog: 'def'; length: 4; max_length: 0; type: 5; decimals: 10 + - 10: name: 'filtered'/''; table: ''/''; db: ''; catalog: 'def'; length: 4; max_length: 0; type: 5; decimals: 2 + - 11: name: 'r_filtered'/''; table: ''/''; db: ''; catalog: 'def'; length: 4; max_length: 0; type: 5; decimals: 2 + - 12: name: 'Extra'/''; table: ''/''; db: ''; catalog: 'def'; length: 765; max_length: 0; type: 253; decimals: 39 +ANALYZE JSON UPDATE number of fields: 1 + - 0: name: 'ANALYZE'/''; table: ''/''; db: ''; catalog: 'def'; length: 234; max_length: 0; type: 253; decimals: 39 +EXPALIN DELETE number of fields: 10 + - 0: name: 'id'/''; table: ''/''; db: ''; catalog: 'def'; length: 3; max_length: 0; type: 8; decimals: 0 + - 1: name: 'select_type'/''; table: ''/''; db: ''; catalog: 'def'; length: 57; max_length: 0; type: 253; decimals: 39 + - 2: name: 'table'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 3: name: 'type'/''; table: ''/''; db: ''; catalog: 'def'; length: 30; max_length: 0; type: 253; decimals: 39 + - 4: name: 'possible_keys'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 5: name: 'key'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 6: name: 'key_len'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 7: name: 'ref'/''; table: ''/''; db: ''; catalog: 'def'; length: 6144; max_length: 0; type: 253; decimals: 39 + - 8: name: 'rows'/''; table: ''/''; db: ''; catalog: 'def'; length: 10; max_length: 0; type: 8; decimals: 0 + - 9: name: 'Extra'/''; table: ''/''; db: ''; catalog: 'def'; length: 765; max_length: 0; type: 253; decimals: 39 +EXPALIN JSON DELETE number of fields: 1 + - 0: name: 'EXPLAIN'/''; table: ''/''; db: ''; catalog: 'def'; length: 234; max_length: 0; type: 253; decimals: 39 +ANALYZE DELETE number of fields: 13 + - 0: name: 'id'/''; table: ''/''; db: ''; catalog: 'def'; length: 3; max_length: 0; type: 8; decimals: 0 + - 1: name: 'select_type'/''; table: ''/''; db: ''; catalog: 'def'; length: 57; max_length: 0; type: 253; decimals: 39 + - 2: name: 'table'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 3: name: 'type'/''; table: ''/''; db: ''; catalog: 'def'; length: 30; max_length: 0; type: 253; decimals: 39 + - 4: name: 'possible_keys'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 5: name: 'key'/''; table: ''/''; db: ''; catalog: 'def'; length: 192; max_length: 0; type: 253; decimals: 39 + - 6: name: 'key_len'/''; table: ''/''; db: ''; catalog: 'def'; length: 12288; max_length: 0; type: 253; decimals: 39 + - 7: name: 'ref'/''; table: ''/''; db: ''; catalog: 'def'; length: 6144; max_length: 0; type: 253; decimals: 39 + - 8: name: 'rows'/''; table: ''/''; db: ''; catalog: 'def'; length: 10; max_length: 0; type: 8; decimals: 0 + - 9: name: 'r_rows'/''; table: ''/''; db: ''; catalog: 'def'; length: 4; max_length: 0; type: 5; decimals: 10 + - 10: name: 'filtered'/''; table: ''/''; db: ''; catalog: 'def'; length: 4; max_length: 0; type: 5; decimals: 2 + - 11: name: 'r_filtered'/''; table: ''/''; db: ''; catalog: 'def'; length: 4; max_length: 0; type: 5; decimals: 2 + - 12: name: 'Extra'/''; table: ''/''; db: ''; catalog: 'def'; length: 765; max_length: 0; type: 253; decimals: 39 +ANALYZE JSON DELETE number of fields: 1 + - 0: name: 'ANALYZE'/''; table: ''/''; db: ''; catalog: 'def'; length: 234; max_length: 0; type: 253; decimals: 39 +# ------------------------------------ + + SET @@global.general_log= @old_general_log; SET @@global.slow_query_log= @old_slow_query_log; diff --git a/mysql-test/main/mysql_client_test.test b/mysql-test/main/mysql_client_test.test index 260473aa0d0..79ad9f84477 100644 --- a/mysql-test/main/mysql_client_test.test +++ b/mysql-test/main/mysql_client_test.test @@ -28,6 +28,11 @@ echo ok; --cat_file $MYSQL_TMP_DIR/test_wl4435.out.log --echo # ------------------------------------ --echo - +--echo # cat MYSQL_TMP_DIR/test_explain_meta.out.log +--echo # ------------------------------------ +--cat_file $MYSQL_TMP_DIR/test_explain_meta.out.log +--echo # ------------------------------------ +--echo +--echo SET @@global.general_log= @old_general_log; SET @@global.slow_query_log= @old_slow_query_log; diff --git a/sql/sql_class.cc b/sql/sql_class.cc index 96485ed15ec..349ec576264 100644 --- a/sql/sql_class.cc +++ b/sql/sql_class.cc @@ -69,6 +69,7 @@ #include "wsrep_thd.h" #include "sql_connect.h" #include "my_atomic.h" +#include "sql_prepare.h" #ifdef HAVE_SYS_SYSCALL_H #include <sys/syscall.h> @@ -2659,18 +2660,27 @@ CHANGED_TABLE_LIST* THD::changed_table_dup(const char *key, size_t key_length) } -int THD::send_explain_fields(select_result *result, uint8 explain_flags, bool is_analyze) +int THD::send_explain_fields(select_result *result, + uint8 explain_flags, + bool is_analyze, + Prepared_statement *stmt) { List<Item> field_list; + int rc; if (lex->explain_json) make_explain_json_field_list(field_list, is_analyze); else make_explain_field_list(field_list, explain_flags, is_analyze); result->prepare(field_list, NULL); - return (result->send_result_set_metadata(field_list, - Protocol::SEND_NUM_ROWS | - Protocol::SEND_EOF)); + if (stmt) + rc= send_prep_stmt(stmt, result->field_count(field_list)) || + result->send_result_set_metadata(field_list, Protocol::SEND_EOF); + else + rc= result->send_result_set_metadata(field_list, + Protocol::SEND_NUM_ROWS | + Protocol::SEND_EOF); + return(rc); } diff --git a/sql/sql_class.h b/sql/sql_class.h index a7c33cbc504..706b72d9d06 100644 --- a/sql/sql_class.h +++ b/sql/sql_class.h @@ -2174,6 +2174,7 @@ struct QUERY_START_TIME_INFO extern "C" void my_message_sql(uint error, const char *str, myf MyFlags); +class Prepared_statement; /** @class THD For each client connection we create a separate thread with THD serving as @@ -3739,7 +3740,7 @@ class THD :public Statement, void add_changed_table(const char *key, size_t key_length); CHANGED_TABLE_LIST * changed_table_dup(const char *key, size_t key_length); int send_explain_fields(select_result *result, uint8 explain_flags, - bool is_analyze); + bool is_analyze, Prepared_statement *stmt); void make_explain_field_list(List<Item> &field_list, uint8 explain_flags, bool is_analyze); void make_explain_json_field_list(List<Item> &field_list, bool is_analyze); diff --git a/sql/sql_explain.cc b/sql/sql_explain.cc index 5d977c6d5c2..96374fc1a33 100644 --- a/sql/sql_explain.cc +++ b/sql/sql_explain.cc @@ -164,7 +164,8 @@ int Explain_query::send_explain(THD *thd) LEX *lex= thd->lex; if (!(result= new (thd->mem_root) select_send(thd)) || - thd->send_explain_fields(result, lex->describe, lex->analyze_stmt)) + thd->send_explain_fields(result, lex->describe, lex->analyze_stmt, + NULL)) return 1; int res= 0; diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 2593bac7462..fa8279d9d6f 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -6469,8 +6469,9 @@ static bool execute_sqlcom_select(THD *thd, TABLE_LIST *all_tables) */ if (!(result= new (thd->mem_root) select_send(thd))) return 1; /* purecov: inspected */ - thd->send_explain_fields(result, lex->describe, lex->analyze_stmt); - + thd->send_explain_fields(result, lex->describe, lex->analyze_stmt, + NULL); + /* This will call optimize() for all parts of query. The query plan is printed out below. diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc index 24f3cc66c6b..b9016e72410 100644 --- a/sql/sql_prepare.cc +++ b/sql/sql_prepare.cc @@ -356,7 +356,7 @@ find_prepared_statement(THD *thd, ulong id) */ #ifndef EMBEDDED_LIBRARY -static bool send_prep_stmt(Prepared_statement *stmt, uint columns) +bool send_prep_stmt(Prepared_statement *stmt, uint columns) { NET *net= &stmt->thd->net; uchar buff[12]; @@ -394,7 +394,7 @@ static bool send_prep_stmt(Prepared_statement *stmt, uint columns) DBUG_RETURN(error); } #else -static bool send_prep_stmt(Prepared_statement *stmt, +bool send_prep_stmt(Prepared_statement *stmt, uint columns __attribute__((unused))) { THD *thd= stmt->thd; @@ -2500,8 +2500,24 @@ static bool check_prepared_statement(Prepared_statement *stmt) break; } if (res == 0) - DBUG_RETURN(stmt->is_sql_prepare() ? - FALSE : (send_prep_stmt(stmt, 0) || thd->protocol->flush())); + { + if (!stmt->is_sql_prepare()) + { + if (lex->describe || lex->analyze_stmt) + { + if (!lex->result && + !(lex->result= new (stmt->mem_root) select_send(thd))) + DBUG_RETURN(TRUE); + res= thd->send_explain_fields(lex->result, lex->describe, + lex->analyze_stmt, stmt); + } + else + res= send_prep_stmt(stmt, 0); + if (!res) + thd->protocol->flush(); + } + DBUG_RETURN(FALSE); + } error: DBUG_RETURN(TRUE); } diff --git a/sql/sql_prepare.h b/sql/sql_prepare.h index ca040da341f..ce6d9df22d0 100644 --- a/sql/sql_prepare.h +++ b/sql/sql_prepare.h @@ -83,6 +83,8 @@ void mysqld_stmt_fetch(THD *thd, char *packet, uint packet_length); void mysqld_stmt_reset(THD *thd, char *packet); void mysql_stmt_get_longdata(THD *thd, char *pos, ulong packet_length); void reinit_stmt_before_use(THD *thd, LEX *lex); +class Prepared_statement; +bool send_prep_stmt(Prepared_statement *stmt, uint columns); my_bool bulk_parameters_iterations(THD *thd); my_bool bulk_parameters_set(THD *thd); diff --git a/tests/mysql_client_test.c b/tests/mysql_client_test.c index e90453411cb..b2177c660de 100644 --- a/tests/mysql_client_test.c +++ b/tests/mysql_client_test.c @@ -1056,6 +1056,7 @@ static void test_wl4435_2() rc= mysql_query(mysql, "DROP PROCEDURE p1"); myquery(rc); } + mct_close_log(); } @@ -20235,9 +20236,328 @@ static void test_proxy_header() test_proxy_header_localhost(); test_proxy_header_ignore(); } - #endif +static void print_metadata(MYSQL_RES *rs_metadata, int num_fields) +{ + MYSQL_FIELD *fields= mysql_fetch_fields(rs_metadata); + + for (int i = 0; i < num_fields; ++i) + { + mct_log(" - %d: name: '%s'/'%s'; table: '%s'/'%s'; " + "db: '%s'; catalog: '%s'; length: %d; max_length: %d; " + "type: %d; decimals: %d\n", + (int) i, + (const char *) fields[i].name, + (const char *) fields[i].org_name, + (const char *) fields[i].table, + (const char *) fields[i].org_table, + (const char *) fields[i].db, + (const char *) fields[i].catalog, + (int) fields[i].length, + (int) fields[i].max_length, + (int) fields[i].type, + (int) fields[i].decimals); + + } +} + +static void test_explain_meta() +{ + MYSQL_STMT *stmt; + int num_fields; + char query[MAX_TEST_QUERY_LENGTH]; + MYSQL_RES *rs_metadata; + int rc; + + myheader("test_explain_meta"); + mct_start_logging("test_explain_meta"); + + strmov(query, "SELECT 1"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("SELECT number of fields: %d\n", (int) num_fields); + if (num_fields != 1) + { + mct_close_log(); + DIE("num_fields != 1"); + } + mysql_stmt_close(stmt); + + strmov(query, "EXPLAIN SELECT 1"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("EXPALIN number of fields: %d\n", (int) num_fields); + if (num_fields != 10) + { + mct_close_log(); + DIE("num_fields != 10"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + strmov(query, "EXPLAIN format=json SELECT 1"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("EXPALIN JSON number of fields: %d\n", (int) num_fields); + if (num_fields != 1) + { + mct_close_log(); + DIE("num_fields != 1"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + + strmov(query, "ANALYZE SELECT 1"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("ANALYZE number of fields: %d\n", (int) num_fields); + if (num_fields != 13) + { + mct_close_log(); + DIE("num_fields != 13"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + strmov(query, "ANALYZE format=json SELECT 1"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("ANALYZE JSON number of fields: %d\n", (int) num_fields); + if (num_fields != 1) + { + mct_close_log(); + DIE("num_fields != 1"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + rc= mysql_query(mysql, "CREATE TABLE t1 (a int)"); + myquery(rc); + + strmov(query, "EXPLAIN INSERT INTO t1 values (1)"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("EXPALIN INSERT number of fields: %d\n", (int) num_fields); + if (num_fields != 10) + { + mct_close_log(); + DIE("num_fields != 10"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + strmov(query, "EXPLAIN format=json INSERT INTO t1 values(1)"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("EXPALIN JSON INSERT number of fields: %d\n", (int) num_fields); + if (num_fields != 1) + { + mct_close_log(); + DIE("num_fields != 1"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + + strmov(query, "ANALYZE INSERT INTO t1 values(1)"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("ANALYZE INSERT number of fields: %d\n", (int) num_fields); + if (num_fields != 13) + { + mct_close_log(); + DIE("num_fields != 13"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + strmov(query, "ANALYZE format=json INSERT INTO t1 values(1)"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("ANALYZE JSON INSERT number of fields: %d\n", (int) num_fields); + if (num_fields != 1) + { + mct_close_log(); + DIE("num_fields != 1"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + + strmov(query, "EXPLAIN UPDATE t1 set a=2"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("EXPALIN UPDATE number of fields: %d\n", (int) num_fields); + if (num_fields != 10) + { + mct_close_log(); + DIE("num_fields != 10"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + strmov(query, "EXPLAIN format=json UPDATE t1 set a=2"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("EXPALIN JSON UPDATE number of fields: %d\n", (int) num_fields); + if (num_fields != 1) + { + mct_close_log(); + DIE("num_fields != 1"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + + strmov(query, "ANALYZE UPDATE t1 set a=2"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("ANALYZE UPDATE number of fields: %d\n", (int) num_fields); + if (num_fields != 13) + { + mct_close_log(); + DIE("num_fields != 13"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + strmov(query, "ANALYZE format=json UPDATE t1 set a=2"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("ANALYZE JSON UPDATE number of fields: %d\n", (int) num_fields); + if (num_fields != 1) + { + mct_close_log(); + DIE("num_fields != 1"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + + strmov(query, "EXPLAIN DELETE FROM t1"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("EXPALIN DELETE number of fields: %d\n", (int) num_fields); + if (num_fields != 10) + { + mct_close_log(); + DIE("num_fields != 10"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + strmov(query, "EXPLAIN format=json DELETE FROM t1"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("EXPALIN JSON DELETE number of fields: %d\n", (int) num_fields); + if (num_fields != 1) + { + mct_close_log(); + DIE("num_fields != 1"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + + strmov(query, "ANALYZE DELETE FROM t1"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("ANALYZE DELETE number of fields: %d\n", (int) num_fields); + if (num_fields != 13) + { + mct_close_log(); + DIE("num_fields != 13"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + strmov(query, "ANALYZE format=json DELETE FROM t1"); + stmt= mysql_simple_prepare(mysql, query); + check_stmt(stmt); + + rs_metadata= mysql_stmt_result_metadata(stmt); + + num_fields= mysql_stmt_field_count(stmt); + mct_log("ANALYZE JSON DELETE number of fields: %d\n", (int) num_fields); + if (num_fields != 1) + { + mct_close_log(); + DIE("num_fields != 1"); + } + print_metadata(rs_metadata, num_fields); + mysql_stmt_close(stmt); + + rc= mysql_query(mysql, "DROP TABLE t1"); + myquery(rc); + mct_close_log(); +} + static struct my_tests_st my_tests[]= { { "disable_query_logs", disable_query_logs }, { "test_view_sp_list_fields", test_view_sp_list_fields }, @@ -20524,6 +20844,7 @@ static struct my_tests_st my_tests[]= { #ifndef EMBEDDED_LIBRARY { "test_proxy_header", test_proxy_header}, #endif + { "test_explain_meta", test_explain_meta }, { 0, 0 } };
participants (1)
-
Oleksandr Byelkin