From 7498978e6af867c3351f8a1392a0de66d2dfae24 Mon Sep 17 00:00:00 2001 From: Oleg Smirnov Date: Wed, 13 Apr 2022 19:56:34 +0700 Subject: [PATCH] MDEV-27699 ANALYZE FORMAT=JSON fields are incorrect for UNION ALL queries UNION ALL queries are a subject of optimization introduced in MDEV-334 when creation of a temporary table is skipped. While there is a check for this optimization in Explain_union::print_explain() there was no such in Explain_union::print_explain_json(). This resulted in printing irrelevant data like: "union_result": { "table_name": "", "access_type": "ALL", "r_loops": 0, "r_rows": null in case when creation of the temporary table was actually optimized out. This commits adds a check whether the temporary table was actually created during the UNION ALL processing and eliminates printing of the irrelevant data. --- mysql-test/main/cte_recursive.result | 4 -- mysql-test/main/derived_cond_pushdown.result | 6 --- mysql-test/main/explain_json.result | 2 - mysql-test/main/table_value_constr.result | 24 ------------ .../compat/oracle/r/table_value_constr.result | 24 ------------ sql/sql_explain.cc | 38 ++++++++++--------- 6 files changed, 20 insertions(+), 78 deletions(-) diff --git a/mysql-test/main/cte_recursive.result b/mysql-test/main/cte_recursive.result index 993fad4f34d..0c98530c9bf 100644 --- a/mysql-test/main/cte_recursive.result +++ b/mysql-test/main/cte_recursive.result @@ -4050,10 +4050,6 @@ ANALYZE { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", - "r_loops": 0, - "r_rows": null, "query_specifications": [ { "query_block": { diff --git a/mysql-test/main/derived_cond_pushdown.result b/mysql-test/main/derived_cond_pushdown.result index e5cf14f64b5..ebe0c2298fd 100644 --- a/mysql-test/main/derived_cond_pushdown.result +++ b/mysql-test/main/derived_cond_pushdown.result @@ -10742,8 +10742,6 @@ EXPLAIN "materialized": { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", "query_specifications": [ { "query_block": { @@ -14718,8 +14716,6 @@ EXPLAIN "materialized": { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", "query_specifications": [ { "query_block": { @@ -14940,8 +14936,6 @@ EXPLAIN "materialized": { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", "query_specifications": [ { "query_block": { diff --git a/mysql-test/main/explain_json.result b/mysql-test/main/explain_json.result index ef6b70aff71..277b9ee3ba3 100644 --- a/mysql-test/main/explain_json.result +++ b/mysql-test/main/explain_json.result @@ -247,8 +247,6 @@ EXPLAIN { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", "query_specifications": [ { "query_block": { diff --git a/mysql-test/main/table_value_constr.result b/mysql-test/main/table_value_constr.result index 7ff5fb7fa5e..d2d961dabd0 100644 --- a/mysql-test/main/table_value_constr.result +++ b/mysql-test/main/table_value_constr.result @@ -1304,8 +1304,6 @@ EXPLAIN { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", "query_specifications": [ { "query_block": { @@ -1525,8 +1523,6 @@ EXPLAIN { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", "query_specifications": [ { "query_block": { @@ -1589,8 +1585,6 @@ EXPLAIN { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", "query_specifications": [ { "query_block": { @@ -1633,8 +1627,6 @@ EXPLAIN { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", "query_specifications": [ { "query_block": { @@ -1677,10 +1669,6 @@ ANALYZE { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", - "r_loops": 0, - "r_rows": null, "query_specifications": [ { "query_block": { @@ -1908,10 +1896,6 @@ ANALYZE { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", - "r_loops": 0, - "r_rows": null, "query_specifications": [ { "query_block": { @@ -1976,10 +1960,6 @@ ANALYZE { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", - "r_loops": 0, - "r_rows": null, "query_specifications": [ { "query_block": { @@ -2022,10 +2002,6 @@ ANALYZE { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", - "r_loops": 0, - "r_rows": null, "query_specifications": [ { "query_block": { diff --git a/mysql-test/suite/compat/oracle/r/table_value_constr.result b/mysql-test/suite/compat/oracle/r/table_value_constr.result index 539329554d5..daea0eccb19 100644 --- a/mysql-test/suite/compat/oracle/r/table_value_constr.result +++ b/mysql-test/suite/compat/oracle/r/table_value_constr.result @@ -1302,8 +1302,6 @@ EXPLAIN { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", "query_specifications": [ { "query_block": { @@ -1523,8 +1521,6 @@ EXPLAIN { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", "query_specifications": [ { "query_block": { @@ -1587,8 +1583,6 @@ EXPLAIN { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", "query_specifications": [ { "query_block": { @@ -1631,8 +1625,6 @@ EXPLAIN { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", "query_specifications": [ { "query_block": { @@ -1675,10 +1667,6 @@ ANALYZE { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", - "r_loops": 0, - "r_rows": null, "query_specifications": [ { "query_block": { @@ -1906,10 +1894,6 @@ ANALYZE { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", - "r_loops": 0, - "r_rows": null, "query_specifications": [ { "query_block": { @@ -1974,10 +1958,6 @@ ANALYZE { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", - "r_loops": 0, - "r_rows": null, "query_specifications": [ { "query_block": { @@ -2020,10 +2000,6 @@ ANALYZE { "query_block": { "union_result": { - "table_name": "", - "access_type": "ALL", - "r_loops": 0, - "r_rows": null, "query_specifications": [ { "query_block": { diff --git a/sql/sql_explain.cc b/sql/sql_explain.cc index 3a29826ee2a..8690a4a38bb 100644 --- a/sql/sql_explain.cc +++ b/sql/sql_explain.cc @@ -581,27 +581,29 @@ void Explain_union::print_explain_json(Explain_query *query, else writer->add_member("union_result").start_object(); - // using_temporary_table - make_union_table_name(table_name_buffer); - writer->add_member("table_name").add_str(table_name_buffer); - writer->add_member("access_type").add_str("ALL"); // not very useful - - /* r_loops (not present in tabular output) */ - if (is_analyze) + if (using_tmp) { - writer->add_member("r_loops").add_ll(fake_select_lex_tracker.get_loops()); - } + make_union_table_name(table_name_buffer); + writer->add_member("table_name").add_str(table_name_buffer); + writer->add_member("access_type").add_str("ALL"); // not very useful - /* `r_rows` */ - if (is_analyze) - { - writer->add_member("r_rows"); - if (fake_select_lex_tracker.has_scans()) - writer->add_double(fake_select_lex_tracker.get_avg_rows()); - else - writer->add_null(); - } + /* r_loops (not present in tabular output) */ + if (is_analyze) + { + writer->add_member("r_loops").add_ll( + fake_select_lex_tracker.get_loops()); + } + /* `r_rows` */ + if (is_analyze) + { + writer->add_member("r_rows"); + if (fake_select_lex_tracker.has_scans()) + writer->add_double(fake_select_lex_tracker.get_avg_rows()); + else + writer->add_null(); + } + } writer->add_member("query_specifications").start_array(); for (int i= 0; i < (int) union_members.elements(); i++)