[Commits] 331034cb6e1: MDEV-11975: SQLCOM_PREPARE of EXPLAIN & ANALYZE statement do not return correct metadata info

Oleksandr Byelkin sanja at mariadb.com
Fri Apr 20 15:41:41 EEST 2018


revision-id: 331034cb6e1552e1ab7c12b6f7341f557f76b807 (mariadb-10.3.6-16-g331034cb6e1)
parent(s): cff60be7fe159fdcb2517ce8441610ad512aa7d0
author: Oleksandr Byelkin
committer: Oleksandr Byelkin
timestamp: 2018-04-20 14:40:14 +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                         |  30 ++-
 sql/sql_class.h                          |   2 +
 sql/sql_prepare.cc                       |  24 ++-
 tests/mysql_client_test.c                | 323 +++++++++++++++++++++++++++++++
 6 files changed, 498 insertions(+), 10 deletions(-)

diff --git a/mysql-test/main/mysql_client_test.result b/mysql-test/main/mysql_client_test.result
index 83ef8d442b3..6f65979517b 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..2c6febd8ffa 100644
--- a/mysql-test/main/mysql_client_test.test
+++ b/mysql-test/main/mysql_client_test.test
@@ -29,5 +29,12 @@ echo ok;
 --echo # ------------------------------------
 --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
+
 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..0ec95e11829 100644
--- a/sql/sql_class.cc
+++ b/sql/sql_class.cc
@@ -2659,18 +2659,32 @@ 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)
+void THD::prepare_explain_fields(select_result *result,
+                                 List<Item> *field_list,
+                                 uint8 explain_flags,
+                                 bool is_analyze)
 {
-  List<Item> field_list;
+  ;
   if (lex->explain_json)
-    make_explain_json_field_list(field_list, is_analyze);
+    make_explain_json_field_list(*field_list, is_analyze);
   else
-    make_explain_field_list(field_list, explain_flags, is_analyze);
+    make_explain_field_list(*field_list, explain_flags, is_analyze);
+
+  result->prepare(*field_list, NULL);
+}
 
-  result->prepare(field_list, NULL);
-  return (result->send_result_set_metadata(field_list,
-                                           Protocol::SEND_NUM_ROWS | 
-                                           Protocol::SEND_EOF));
+
+int THD::send_explain_fields(select_result *result,
+                             uint8 explain_flags,
+                             bool is_analyze)
+{
+  List<Item> field_list;
+  int rc;
+  prepare_explain_fields(result, &field_list, explain_flags, is_analyze);
+  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..67bcb639954 100644
--- a/sql/sql_class.h
+++ b/sql/sql_class.h
@@ -3738,6 +3738,8 @@ class THD :public Statement,
   void add_changed_table(TABLE *table);
   void add_changed_table(const char *key, size_t key_length);
   CHANGED_TABLE_LIST * changed_table_dup(const char *key, size_t key_length);
+  void prepare_explain_fields(select_result *result, List<Item> *field_list,
+                              uint8 explain_flags, bool is_analyze);
   int send_explain_fields(select_result *result, uint8 explain_flags,
                           bool is_analyze);
   void make_explain_field_list(List<Item> &field_list, uint8 explain_flags,
diff --git a/sql/sql_prepare.cc b/sql/sql_prepare.cc
index 24f3cc66c6b..e37c2006cc6 100644
--- a/sql/sql_prepare.cc
+++ b/sql/sql_prepare.cc
@@ -2500,8 +2500,28 @@ 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);
+         List<Item> field_list;
+         thd->prepare_explain_fields(lex->result, &field_list,
+                                     lex->describe, lex->analyze_stmt);
+         res= send_prep_stmt(stmt, lex->result->field_count(field_list)) ||
+              lex->result->send_result_set_metadata(field_list,
+                                                    Protocol::SEND_EOF);
+       }
+       else
+         res= send_prep_stmt(stmt, 0);
+       if (!res)
+         thd->protocol->flush();
+    }
+    DBUG_RETURN(FALSE);
+  }
 error:
   DBUG_RETURN(TRUE);
 }
diff --git a/tests/mysql_client_test.c b/tests/mysql_client_test.c
index e90453411cb..ae4b2ead99d 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();
 }
 
 
@@ -20238,6 +20239,327 @@ static void test_proxy_header()
 
 #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 +20846,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 }
 };
 


More information about the commits mailing list