diff --git a/mysql-test/r/analyze_format_json.result b/mysql-test/r/analyze_format_json.result index fcab200df27..26625cbb75e 100644 --- a/mysql-test/r/analyze_format_json.result +++ b/mysql-test/r/analyze_format_json.result @@ -210,3 +210,32 @@ EXPLAIN } } drop table t1,t2; +create table t1(a int); +insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); +# +# MDEV-7679: ANALYZE crashes when printing WHERE when no default db +# +select database(); +database() +test +select database(); +database() +NULL +analyze format=json select * from test.t1 where t1.a<5; +EXPLAIN +{ + "query_block": { + "select_id": 1, + "table": { + "table_name": "t1", + "access_type": "ALL", + "r_loops": 1, + "rows": 10, + "r_rows": 10, + "filtered": 100, + "r_filtered": 50, + "attached_condition": "(test.t1.a < 5)" + } + } +} +drop table t1; diff --git a/mysql-test/t/analyze_format_json.test b/mysql-test/t/analyze_format_json.test index 1adb693c51b..f57db1d76af 100644 --- a/mysql-test/t/analyze_format_json.test +++ b/mysql-test/t/analyze_format_json.test @@ -51,3 +51,19 @@ analyze format=json select * from t1 straight_join t2 force index(a) where t2.a= drop table t1,t2; +create table t1(a int); +insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); + +--echo # +--echo # MDEV-7679: ANALYZE crashes when printing WHERE when no default db +--echo # + +select database(); +connect (con1,localhost,root,,*NO-ONE*); +connection con1; +select database(); +analyze format=json select * from test.t1 where t1.a<5; +disconnect con1; +connection default; +drop table t1; + diff --git a/sql/item.cc b/sql/item.cc index 5bc127336bd..85279b2abf1 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -2380,7 +2380,8 @@ void Item_ident::print(String *str, enum_query_type query_type) 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); + bool skip_db= (query_type & QT_EXPLAIN) && thd->db && + !strcmp(thd->db, db_name); if (!skip_db && !(cached_table && cached_table->belong_to_view && cached_table->belong_to_view->compact_view_format))