From 645191aa1338caa397124e5c02002d93a07a628b Mon Sep 17 00:00:00 2001 From: Igor Babaev Date: Thu, 20 Jun 2019 18:50:20 -0700 Subject: [PATCH 01/21] MDEV-19778 Wrong Result on Left Outer Join with Subquery right on true and WHERE filter afterwards This patch complements the patch fixing the bug MDEV-6892. The latter properly handled queries that used mergeable views returning constant columns as inner tables of outer joins and whose where clause contained predicates referring to these columns if the predicates of happened not to be equality predicates. Otherwise the server still could return wrong result sets for such queries. Besides the fix for MDEV-6892 prevented some possible conversions of outer joins to inner joins for such queries. This patch corrected the function check_simple_equality() to handle properly conjunctive equalities of the where clause that refer to the constant columns of mergeable views used as inner tables of an outer join. The patch also changed the code of Item_direct_view_ref::not_null_tables(). This change allowed to take into account predicates containing references to constant columns of mergeable views when converting outer joins into inner joins. --- mysql-test/r/derived.result | 9 +++++ mysql-test/r/derived_view.result | 57 +++++++++++++++++++++++++++++ mysql-test/r/func_group.result | 2 + mysql-test/r/subselect_cache.result | 2 + mysql-test/t/derived.test | 4 ++ mysql-test/t/derived_view.test | 34 +++++++++++++++++ sql/item.cc | 13 ++++--- sql/item.h | 1 + sql/item_func.cc | 1 + sql/sql_select.cc | 9 ++++- 10 files changed, 126 insertions(+), 6 deletions(-) diff --git a/mysql-test/r/derived.result b/mysql-test/r/derived.result index 2c0d009567837..97149633269c5 100644 --- a/mysql-test/r/derived.result +++ b/mysql-test/r/derived.result @@ -578,6 +578,15 @@ select x.id, message from (select id from t1) x left join (select id, 1 as message from t2) y on x.id=y.id where coalesce(message,0) <> 0; id message +explain extended +select x.id, message from (select id from t1) x left join +(select id, 1 as message from t2) y on x.id=y.id +where message <> 0; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`id` AS `id`,1 AS `message` from `test`.`t1` join `test`.`t2` where (`test`.`t2`.`id` = `test`.`t1`.`id`) drop table t1,t2; # # MDEV-7827: Assertion `!table || (!table->read_set || diff --git a/mysql-test/r/derived_view.result b/mysql-test/r/derived_view.result index f8cf919012673..412786c48e20e 100644 --- a/mysql-test/r/derived_view.result +++ b/mysql-test/r/derived_view.result @@ -3041,3 +3041,60 @@ id select_type table type possible_keys key key_len ref rows Extra 7 DERIVED p9 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join) 7 DERIVED p10 ALL NULL NULL NULL NULL 550 Using where; Using join buffer (incremental, BNL join) DROP TABLE t1, t2; +# +# MDEV-19778: equality condition for mergeable view returning constants +# in its columns and used as inner table of outer join +# +create table t1 (pk int, a int); +insert into t1 values (1,7), (2,3), (3,2), (4,3); +create table t2 (b int); +insert into t2 values (5), (1), (NULL), (3); +create table t3 (c int); +insert into t3 values (1), (8); +create view v1 as +select 3 as d, t2.b from t2; +select * from t1 left join v1 on t1.pk <= 2 where t1.a=v1.d; +pk a d b +2 3 3 5 +2 3 3 1 +2 3 3 NULL +2 3 3 3 +explain extended select * from t1 left join v1 on t1.pk <= 2 where t1.a=v1.d; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 4 100.00 Using where +1 SIMPLE t2 ALL NULL NULL NULL NULL 4 100.00 Using join buffer (flat, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`pk` AS `pk`,`test`.`t1`.`a` AS `a`,3 AS `d`,`test`.`t2`.`b` AS `b` from `test`.`t1` join `test`.`t2` where ((`test`.`t1`.`a` = 3) and (`test`.`t1`.`pk` <= 2)) +select * from t1 left join (select 3 as d, t2.b from t2) dt on t1.pk <= 2 +where t1.a=dt.d; +pk a d b +2 3 3 5 +2 3 3 1 +2 3 3 NULL +2 3 3 3 +explain extended select * from t1 left join (select 3 as d, t2.b from t2) dt on t1.pk <= 2 +where t1.a=dt.d; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 4 100.00 Using where +1 SIMPLE t2 ALL NULL NULL NULL NULL 4 100.00 Using join buffer (flat, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`pk` AS `pk`,`test`.`t1`.`a` AS `a`,3 AS `d`,`test`.`t2`.`b` AS `b` from `test`.`t1` join `test`.`t2` where ((`test`.`t1`.`a` = 3) and (`test`.`t1`.`pk` <= 2)) +select * from t1 left join (v1,t3) on t1.pk <= 2 where t1.a=v1.d; +pk a d b c +2 3 3 5 1 +2 3 3 5 8 +2 3 3 1 1 +2 3 3 1 8 +2 3 3 NULL 1 +2 3 3 NULL 8 +2 3 3 3 1 +2 3 3 3 8 +explain extended select * from t1 left join (v1,t3) on t1.pk <= 2 where t1.a=v1.d; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t3 ALL NULL NULL NULL NULL 2 100.00 +1 SIMPLE t1 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 4 100.00 Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`pk` AS `pk`,`test`.`t1`.`a` AS `a`,3 AS `d`,`test`.`t2`.`b` AS `b`,`test`.`t3`.`c` AS `c` from `test`.`t1` join `test`.`t2` join `test`.`t3` where ((`test`.`t1`.`a` = 3) and (`test`.`t1`.`pk` <= 2)) +drop view v1; +drop table t1,t2,t3; diff --git a/mysql-test/r/func_group.result b/mysql-test/r/func_group.result index 38fae2f0a4f20..c1de5d935371d 100644 --- a/mysql-test/r/func_group.result +++ b/mysql-test/r/func_group.result @@ -1981,6 +1981,7 @@ NULL Warnings: Warning 1292 Truncated incorrect INTEGER value: 'j' Warning 1292 Truncated incorrect INTEGER value: 'j' +Warning 1292 Truncated incorrect INTEGER value: 'j' EXPLAIN SELECT MIN(t2.pk) @@ -1995,6 +1996,7 @@ id select_type table type possible_keys key key_len ref rows Extra Warnings: Warning 1292 Truncated incorrect INTEGER value: 'j' Warning 1292 Truncated incorrect INTEGER value: 'j' +Warning 1292 Truncated incorrect INTEGER value: 'j' # # 2) Test that subquery materialization is setup for query with diff --git a/mysql-test/r/subselect_cache.result b/mysql-test/r/subselect_cache.result index 95f935981c294..0bdef7a8e71dd 100644 --- a/mysql-test/r/subselect_cache.result +++ b/mysql-test/r/subselect_cache.result @@ -3130,6 +3130,7 @@ WHERE table1 .`col_varchar_key` ) field10 Warnings: Warning 1292 Truncated incorrect DOUBLE value: 'f' Warning 1292 Truncated incorrect DOUBLE value: 'f' +Warning 1292 Truncated incorrect DOUBLE value: 'f' SET @@optimizer_switch = 'subquery_cache=on'; /* cache is on */ SELECT COUNT( DISTINCT table2 .`col_int_key` ) , ( SELECT SUBQUERY2_t1 .`col_int_key` @@ -3146,6 +3147,7 @@ WHERE table1 .`col_varchar_key` ) field10 Warnings: Warning 1292 Truncated incorrect DOUBLE value: 'f' Warning 1292 Truncated incorrect DOUBLE value: 'f' +Warning 1292 Truncated incorrect DOUBLE value: 'f' drop table t1,t2,t3,t4; set @@optimizer_switch= default; #launchpad BUG#611625 diff --git a/mysql-test/t/derived.test b/mysql-test/t/derived.test index 62b2c43ca121f..38ab28f2e2aec 100644 --- a/mysql-test/t/derived.test +++ b/mysql-test/t/derived.test @@ -502,6 +502,10 @@ insert into t2 values(4),(5),(6); select x.id, message from (select id from t1) x left join (select id, 1 as message from t2) y on x.id=y.id where coalesce(message,0) <> 0; +explain extended +select x.id, message from (select id from t1) x left join +(select id, 1 as message from t2) y on x.id=y.id +where message <> 0; drop table t1,t2; --echo # diff --git a/mysql-test/t/derived_view.test b/mysql-test/t/derived_view.test index 61c4278b43fce..34aaa27ccf983 100644 --- a/mysql-test/t/derived_view.test +++ b/mysql-test/t/derived_view.test @@ -2194,3 +2194,37 @@ JOIN ) gp_20 ON gp_20.id=t2.id ; DROP TABLE t1, t2; + +--echo # +--echo # MDEV-19778: equality condition for mergeable view returning constants +--echo # in its columns and used as inner table of outer join +--echo # + +create table t1 (pk int, a int); +insert into t1 values (1,7), (2,3), (3,2), (4,3); +create table t2 (b int); +insert into t2 values (5), (1), (NULL), (3); +create table t3 (c int); +insert into t3 values (1), (8); + +create view v1 as +select 3 as d, t2.b from t2; + +let $q= +select * from t1 left join v1 on t1.pk <= 2 where t1.a=v1.d; +eval $q; +eval explain extended $q; + +let $q= +select * from t1 left join (select 3 as d, t2.b from t2) dt on t1.pk <= 2 + where t1.a=dt.d; +eval $q; +eval explain extended $q; + +let $q= +select * from t1 left join (v1,t3) on t1.pk <= 2 where t1.a=v1.d; +eval $q; +eval explain extended $q; + +drop view v1; +drop table t1,t2,t3; diff --git a/sql/item.cc b/sql/item.cc index 719bdcfaf898a..ffd899ef82016 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -9814,11 +9814,14 @@ table_map Item_direct_view_ref::used_tables() const table_map Item_direct_view_ref::not_null_tables() const { - return get_depended_from() ? - 0 : - ((view->is_merged_derived() || view->merged || !view->table) ? - (*ref)->not_null_tables() : - view->table->map); + if (get_depended_from()) + return 0; + if (!( view->merged || !view->table)) + return view->table->map; + TABLE *tab= get_null_ref_table(); + if (tab == NO_NULL_TABLE || (*ref)->used_tables()) + return (*ref)->not_null_tables(); + return get_null_ref_table()->map; } /* diff --git a/sql/item.h b/sql/item.h index f6c3c20516baf..6dc999709146b 100644 --- a/sql/item.h +++ b/sql/item.h @@ -3382,6 +3382,7 @@ class Item_direct_view_ref :public Item_direct_ref void update_used_tables(); table_map not_null_tables() const; bool const_item() const { return used_tables() == 0; } + TABLE *get_null_ref_table() const { return null_ref_table; } bool walk(Item_processor processor, bool walk_subquery, uchar *arg) { return (*ref)->walk(processor, walk_subquery, arg) || diff --git a/sql/item_func.cc b/sql/item_func.cc index 22d7e8c7bb6d0..dad4b89b3bcd4 100644 --- a/sql/item_func.cc +++ b/sql/item_func.cc @@ -226,6 +226,7 @@ Item_func::fix_fields(THD *thd, Item **ref) with_field= with_field || item->with_field; used_tables_cache|= item->used_tables(); const_item_cache&= item->const_item(); + not_null_tables_cache|= item->not_null_tables(); with_subselect|= item->has_subquery(); } } diff --git a/sql/sql_select.cc b/sql/sql_select.cc index 0d94ba2f293bc..3d78000b3d415 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -1030,7 +1030,6 @@ JOIN::optimize() DBUG_RETURN(1); /* purecov: inspected */ /* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */ select_lex->update_used_tables(); - } eval_select_list_used_tables(); @@ -1092,6 +1091,8 @@ JOIN::optimize() sel->where= conds; + select_lex->update_used_tables(); + if (arena) thd->restore_active_arena(arena, &backup); } @@ -11763,6 +11764,9 @@ static bool check_simple_equality(Item *left_item, Item *right_item, { if (((Item_ref*)left_item)->get_depended_from()) return FALSE; + if (((Item_direct_view_ref*)left_item)->get_null_ref_table() != + NO_NULL_TABLE && !left_item->real_item()->used_tables()) + return FALSE; left_item= left_item->real_item(); } if (right_item->type() == Item::REF_ITEM && @@ -11770,6 +11774,9 @@ static bool check_simple_equality(Item *left_item, Item *right_item, { if (((Item_ref*)right_item)->get_depended_from()) return FALSE; + if (((Item_direct_view_ref*)right_item)->get_null_ref_table() != + NO_NULL_TABLE && !right_item->real_item()->used_tables()) + return FALSE; right_item= right_item->real_item(); } if (left_item->type() == Item::FIELD_ITEM && From 69be8f3c5bdd507fa924194f8b15f6bc90a01f3b Mon Sep 17 00:00:00 2001 From: Anel Husakovic Date: Fri, 14 Jun 2019 00:33:01 -0700 Subject: [PATCH 02/21] Apply valid parameter type for test case in i_s_parameters.test --- mysql-test/r/information_schema_parameters.result | 4 ++-- mysql-test/t/information_schema_parameters.test | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mysql-test/r/information_schema_parameters.result b/mysql-test/r/information_schema_parameters.result index 93c7634a91024..1a17c7c3a5e84 100644 --- a/mysql-test/r/information_schema_parameters.result +++ b/mysql-test/r/information_schema_parameters.result @@ -392,7 +392,7 @@ SPECIFIC_CATALOG SPECIFIC_SCHEMA SPECIFIC_NAME ORDINAL_POSITION PARAMETER_MODE P DROP DATABASE IF EXISTS i_s_parameters_test; CREATE DATABASE i_s_parameters_test; USE i_s_parameters_test; -CREATE PROCEDURE testproc (OUT param1 INT) +CREATE PROCEDURE testproc (IN param1 INT) BEGIN SELECT 2+2 as param1; END; @@ -400,7 +400,7 @@ END; SELECT * FROM INFORMATION_SCHEMA.PARAMETERS WHERE SPECIFIC_SCHEMA = 'i_s_parameters_test' AND SPECIFIC_NAME = 'testproc'; SPECIFIC_CATALOG SPECIFIC_SCHEMA SPECIFIC_NAME ORDINAL_POSITION PARAMETER_MODE PARAMETER_NAME DATA_TYPE CHARACTER_MAXIMUM_LENGTH CHARACTER_OCTET_LENGTH NUMERIC_PRECISION NUMERIC_SCALE DATETIME_PRECISION CHARACTER_SET_NAME COLLATION_NAME DTD_IDENTIFIER ROUTINE_TYPE -def i_s_parameters_test testproc 1 OUT param1 int NULL NULL 10 0 NULL NULL NULL int(11) PROCEDURE +def i_s_parameters_test testproc 1 IN param1 int NULL NULL 10 0 NULL NULL NULL int(11) PROCEDURE # ========== parameters.5 ========== DROP DATABASE IF EXISTS i_s_parameters_test; CREATE DATABASE i_s_parameters_test; diff --git a/mysql-test/t/information_schema_parameters.test b/mysql-test/t/information_schema_parameters.test index 3f0b11cba5fc4..0563941c17c9d 100644 --- a/mysql-test/t/information_schema_parameters.test +++ b/mysql-test/t/information_schema_parameters.test @@ -92,7 +92,7 @@ CREATE DATABASE i_s_parameters_test; USE i_s_parameters_test; delimiter //; -CREATE PROCEDURE testproc (OUT param1 INT) +CREATE PROCEDURE testproc (IN param1 INT) BEGIN SELECT 2+2 as param1; END; From d890c45b62c0d841b464a0d3539f2c99f241aacf Mon Sep 17 00:00:00 2001 From: Daniel Black Date: Mon, 15 Apr 2019 09:13:47 +1000 Subject: [PATCH 03/21] mysql_install_db: assume builddir is dirname0 The assumption in the original commit for --builddir (648d3cedbc09), was to assume that without a --builddir, and when --srcdir is specified, that the builddir is the same as the srcdir. The problem is that this assumption does not hold for out-of-source builds and we can figure out the builddir by looking for where mysql_install_db script is. As mysql_install_db is in the builddir, we use dirname0 as the builddir after checking that my_print_defaults is also located from dirname0, otherwise default to old behavior. --- scripts/mysql_install_db.sh | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/scripts/mysql_install_db.sh b/scripts/mysql_install_db.sh index e3b4ad88105f9..27256994ef060 100644 --- a/scripts/mysql_install_db.sh +++ b/scripts/mysql_install_db.sh @@ -245,7 +245,12 @@ if test -n "$srcdir" then if test -z "$builddir" then - builddir="$srcdir" + if test -x "$dirname0/extra/my_print_defaults" + then + builddir="$dirname0" + else + builddir="$srcdir" + fi fi print_defaults="$builddir/extra/my_print_defaults" elif test -n "$basedir" From ecd8f44844c84b7ab0865eeb4e6e0ddd15836e16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vicen=C8=9Biu=20Ciorbaru?= Date: Mon, 1 Jul 2019 13:55:05 +0300 Subject: [PATCH 04/21] Extra comment to mysql_install_db Explain why it makes sense to not consider builddir == srcdir directly, for cases when we do out-of-source builds. --- scripts/mysql_install_db.sh | 2 ++ 1 file changed, 2 insertions(+) diff --git a/scripts/mysql_install_db.sh b/scripts/mysql_install_db.sh index 27256994ef060..fb1663d9d697f 100644 --- a/scripts/mysql_install_db.sh +++ b/scripts/mysql_install_db.sh @@ -243,6 +243,8 @@ then fi if test -n "$srcdir" then + # In an out-of-source build, builddir is not srcdir. Try to guess where + # builddir is by looking for my_print_defaults. if test -z "$builddir" then if test -x "$dirname0/extra/my_print_defaults" From 399d012c81b8f830a19ccf03d156cdd6194380c3 Mon Sep 17 00:00:00 2001 From: Vladislav Vaintroub Date: Fri, 5 Jul 2019 15:14:52 +0200 Subject: [PATCH 05/21] MDEV-19922 Old version of heidisql in mariadb installer Upgrade HeidiSQL to 10.2 --- win/packaging/heidisql.cmake | 9 +-------- win/packaging/heidisql.wxi.in | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/win/packaging/heidisql.cmake b/win/packaging/heidisql.cmake index 29ecdd8eb4fc4..1b9fb3d110c09 100644 --- a/win/packaging/heidisql.cmake +++ b/win/packaging/heidisql.cmake @@ -1,4 +1,4 @@ -SET(HEIDISQL_BASE_NAME "HeidiSQL_9.5_Portable") +SET(HEIDISQL_BASE_NAME "HeidiSQL_10.2_32_Portable") SET(HEIDISQL_ZIP "${HEIDISQL_BASE_NAME}.zip") SET(HEIDISQL_URL "http://www.heidisql.com/downloads/releases/${HEIDISQL_ZIP}") SET(HEIDISQL_DOWNLOAD_DIR ${THIRD_PARTY_DOWNLOAD_LOCATION}/${HEIDISQL_BASE_NAME}) @@ -12,12 +12,5 @@ IF(NOT EXISTS ${HEIDISQL_DOWNLOAD_DIR}/${HEIDISQL_ZIP}) ) ENDIF() -SET(LIBMYSQLDLL_SOURCE ${HEIDISQL_DOWNLOAD_DIR}/libmysql.dll) -IF(CMAKE_SIZEOF_VOID_P EQUAL 4) - # Use our libmysql if it is 32 bit. - IF(LIBMYSQL_LOCATION) - SET(LIBMYSQLDLL_SOURCE "${LIBMYSQL_LOCATION}") - ENDIF() -ENDIF() CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/heidisql.wxi.in ${CMAKE_CURRENT_BINARY_DIR}/heidisql.wxi) CONFIGURE_FILE(${CMAKE_CURRENT_SOURCE_DIR}/heidisql_feature.wxi.in ${CMAKE_CURRENT_BINARY_DIR}/heidisql_feature.wxi) diff --git a/win/packaging/heidisql.wxi.in b/win/packaging/heidisql.wxi.in index 1d5c4430cd24f..e8e017000326e 100644 --- a/win/packaging/heidisql.wxi.in +++ b/win/packaging/heidisql.wxi.in @@ -8,6 +8,7 @@ Win64="no" /> + @@ -35,7 +36,9 @@ - + + + @@ -53,12 +56,11 @@ - - - - - + + + + @@ -73,13 +75,15 @@ + - - + + + From 8997f20f12309d2660988f254415a343d6328835 Mon Sep 17 00:00:00 2001 From: Mostafa Hussein Date: Sat, 29 Sep 2018 11:30:27 +0200 Subject: [PATCH 06/21] use -f with pgrep pgrep will not be able to get th pid using the full path which is $libexec/mysqld unless -f is being used --- support-files/mysql.server.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/support-files/mysql.server.sh b/support-files/mysql.server.sh index f3990967b8769..97d3059c2eaf4 100644 --- a/support-files/mysql.server.sh +++ b/support-files/mysql.server.sh @@ -386,7 +386,7 @@ case "$mode" in fi else # Try to find appropriate mysqld process - mysqld_pid=`pgrep $libexecdir/mysqld` + mysqld_pid=`pgrep -f $libexecdir/mysqld` # test if multiple pids exist pid_count=`echo $mysqld_pid | wc -w` From 8540fa83bb34df6a3d489a4e85c77692f36e3e26 Mon Sep 17 00:00:00 2001 From: Igor Babaev Date: Sun, 30 Jun 2019 13:16:12 -0700 Subject: [PATCH 07/21] MDEV-19421 Basic 3-way join queries are not parsed. The parser returned a syntax error message for the queries with join expressions like this t1 JOIN t2 [LEFT | RIGHT] JOIN t3 ON ... ON ... when the second operand of the outer JOIN operation with ON clause was another join expression with ON clause. In this expression the JOIN operator is right-associative, i.e. expression has to be parsed as the expression t1 JOIN (t2 [LEFT | RIGHT] JOIN t3 ON ... ) ON ... Such join expressions are hard to parse because the outer JOIN is left-associative if there is no ON clause for the first outer JOIN operator. The patch implements the solution when the JOIN operator is always parsed as right-associative and builds first the right-associative tree. If it happens that there is no corresponding ON clause for this operator the tree is converted to left-associative. The idea of the solution was taken from the patch by Martin Hansson "WL#8083: Fixed the join_table rule" from MySQL-8.0 code line. As the grammar rules related to join expressions in MySQL-8.0 and MariaDB-5.5+ are quite different MariaDB solution could not borrow any code from the MySQL-8.0 solution. --- mysql-test/r/join.result | 1384 +++++++++++++++++++++++++ mysql-test/r/join_outer_innodb.result | 2 +- mysql-test/t/join.test | 404 ++++++++ mysql-test/t/join_outer_innodb.test | 2 +- sql/sql_lex.h | 6 +- sql/sql_parse.cc | 235 +++++ sql/sql_yacc.yy | 49 +- sql/table.h | 24 + 8 files changed, 2078 insertions(+), 28 deletions(-) diff --git a/mysql-test/r/join.result b/mysql-test/r/join.result index 2e5ee30b54e52..6b051acc10825 100644 --- a/mysql-test/r/join.result +++ b/mysql-test/r/join.result @@ -1537,5 +1537,1389 @@ ERROR 23000: Column 'c' in field list is ambiguous DROP PROCEDURE p1; DROP TABLE t1,t2,t3,t4,t5; # +# MDEV-19421: Embedding inner joins +# +create table t1 (a int); +insert into t1 values (7), (5), (3); +create table s1 (b int); +insert into s1 values (7), (5), (3); +create table t2 (a int); +insert into t2 values (5), (1), (7); +create table s2 (b int); +insert into s2 values (5), (1), (7); +create table t3 (a int); +insert into t3 values (2), (7), (3); +create table t4 (a int); +insert into t4 values (4), (7), (9), (5); +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3 on t2.a=t3.a on t1.a=t2.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t1` join `test`.`t2` join `test`.`t3` where ((`test`.`t2`.`a` = `test`.`t1`.`a`) and (`test`.`t3`.`a` = `test`.`t1`.`a`)) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3 on t2.a=t3.a on t1.a=t2.a; +t1_a t2_a t3_a +7 7 7 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 left join t3 on t2.a=t3.a on t1.a=t2.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t1` join `test`.`t2` left join `test`.`t3` on((`test`.`t3`.`a` = `test`.`t1`.`a`)) where (`test`.`t2`.`a` = `test`.`t1`.`a`) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 left join t3 on t2.a=t3.a on t1.a=t2.a; +t1_a t2_a t3_a +7 7 7 +5 5 NULL +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 right join t3 on t2.a=t3.a on t1.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t1` join `test`.`t3` left join `test`.`t2` on((`test`.`t2`.`a` = `test`.`t1`.`a`)) where (`test`.`t3`.`a` = `test`.`t1`.`a`) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 right join t3 on t2.a=t3.a on t1.a=t3.a; +t1_a t2_a t3_a +7 7 7 +3 NULL 3 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3 using(a) using(a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t1` join `test`.`t2` join `test`.`t3` where ((`test`.`t2`.`a` = `test`.`t1`.`a`) and (`test`.`t3`.`a` = `test`.`t1`.`a`)) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3 using(a) using(a); +t1_a t2_a t3_a +7 7 7 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 left join t3 using(a) using(a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t1` join `test`.`t2` left join `test`.`t3` on((`test`.`t3`.`a` = `test`.`t1`.`a`)) where (`test`.`t2`.`a` = `test`.`t1`.`a`) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 left join t3 using(a) using(a); +t1_a t2_a t3_a +7 7 7 +5 5 NULL +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 right join t3 using(a) using(a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t1` join `test`.`t3` left join `test`.`t2` on((`test`.`t2`.`a` = `test`.`t1`.`a`)) where (`test`.`t3`.`a` = `test`.`t1`.`a`) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 right join t3 using(a) using(a); +t1_a t2_a t3_a +7 7 7 +3 NULL 3 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3 on t2.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t1` join `test`.`t2` join `test`.`t3` where (`test`.`t3`.`a` = `test`.`t2`.`a`) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3 on t2.a=t3.a; +t1_a t2_a t3_a +7 7 7 +5 7 7 +3 7 7 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 left join t3 on t2.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t1` join `test`.`t2` left join `test`.`t3` on((`test`.`t3`.`a` = `test`.`t2`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 left join t3 on t2.a=t3.a; +t1_a t2_a t3_a +7 7 7 +5 7 7 +3 7 7 +7 5 NULL +5 5 NULL +3 5 NULL +7 1 NULL +5 1 NULL +3 1 NULL +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 right join t3 on t2.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t3` left join (`test`.`t1` join `test`.`t2`) on((`test`.`t2`.`a` = `test`.`t3`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 right join t3 on t2.a=t3.a; +t1_a t2_a t3_a +7 7 7 +5 7 7 +3 7 7 +NULL NULL 2 +NULL NULL 3 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3 on t1.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t1` join `test`.`t2` join `test`.`t3` where (`test`.`t3`.`a` = `test`.`t1`.`a`) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3 on t1.a=t3.a; +t1_a t2_a t3_a +7 5 7 +7 1 7 +7 7 7 +3 5 3 +3 1 3 +3 7 3 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 left join t3 on t1.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t1` join `test`.`t2` left join `test`.`t3` on((`test`.`t3`.`a` = `test`.`t1`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 left join t3 on t1.a=t3.a; +t1_a t2_a t3_a +7 5 7 +7 1 7 +7 7 7 +3 5 3 +3 1 3 +3 7 3 +5 5 NULL +5 1 NULL +5 7 NULL +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 right join t3 on t1.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t3` left join (`test`.`t1` join `test`.`t2`) on((`test`.`t1`.`a` = `test`.`t3`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 right join t3 on t1.a=t3.a; +t1_a t2_a t3_a +7 5 7 +3 5 3 +7 1 7 +3 1 3 +7 7 7 +3 7 3 +NULL NULL 2 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join (t2 join t3 on t2.a=t3.a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t1` join `test`.`t2` join `test`.`t3` where (`test`.`t3`.`a` = `test`.`t2`.`a`) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join (t2 join t3 on t2.a=t3.a); +t1_a t2_a t3_a +7 7 7 +5 7 7 +3 7 7 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join (t2 left join t3 on t2.a=t3.a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t1` join `test`.`t2` left join `test`.`t3` on((`test`.`t3`.`a` = `test`.`t2`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join (t2 left join t3 on t2.a=t3.a); +t1_a t2_a t3_a +7 7 7 +5 7 7 +3 7 7 +7 5 NULL +5 5 NULL +3 5 NULL +7 1 NULL +5 1 NULL +3 1 NULL +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join (t2 right join t3 on t2.a=t3.a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t1` join `test`.`t3` left join `test`.`t2` on((`test`.`t2`.`a` = `test`.`t3`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join (t2 right join t3 on t2.a=t3.a); +t1_a t2_a t3_a +7 7 7 +5 7 7 +3 7 7 +7 NULL 2 +5 NULL 2 +3 NULL 2 +7 NULL 3 +5 NULL 3 +3 NULL 3 +explain extended select * +from s1 join t2 join t3 using(a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t2`.`a` AS `a`,`test`.`s1`.`b` AS `b` from `test`.`s1` join `test`.`t2` join `test`.`t3` where (`test`.`t3`.`a` = `test`.`t2`.`a`) +select * +from s1 join t2 join t3 using(a); +a b +7 7 +7 5 +7 3 +explain extended select * +from s1 join t2 left join t3 using(a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t2`.`a` AS `a`,`test`.`s1`.`b` AS `b` from `test`.`s1` join `test`.`t2` left join `test`.`t3` on((`test`.`t3`.`a` = `test`.`t2`.`a`)) where 1 +select * +from s1 join t2 left join t3 using(a); +a b +7 7 +7 5 +7 3 +5 7 +5 5 +5 3 +1 7 +1 5 +1 3 +explain extended select * +from s1 join t2 right join t3 using(a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t3`.`a` AS `a`,`test`.`s1`.`b` AS `b` from `test`.`t3` left join (`test`.`s1` join `test`.`t2`) on((`test`.`t2`.`a` = `test`.`t3`.`a`)) where 1 +select * +from s1 join t2 right join t3 using(a); +a b +7 7 +7 5 +7 3 +2 NULL +3 NULL +explain extended select s1.b, t2.a as t2_a, t3.a as t3_a +from s1 join t2 join t3 using(a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `b`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`s1` join `test`.`t2` join `test`.`t3` where (`test`.`t3`.`a` = `test`.`t2`.`a`) +select s1.b, t2.a as t2_a, t3.a as t3_a +from s1 join t2 join t3 using(a); +b t2_a t3_a +7 7 7 +5 7 7 +3 7 7 +explain extended select s1.b, t2.a as t2_a, t3.a as t3_a +from s1 join t2 left join t3 using(a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `b`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`s1` join `test`.`t2` left join `test`.`t3` on((`test`.`t3`.`a` = `test`.`t2`.`a`)) where 1 +select s1.b, t2.a as t2_a, t3.a as t3_a +from s1 join t2 left join t3 using(a); +b t2_a t3_a +7 7 7 +5 7 7 +3 7 7 +7 5 NULL +5 5 NULL +3 5 NULL +7 1 NULL +5 1 NULL +3 1 NULL +explain extended select s1.b, t2.a as t2_a, t3.a as t3_a +from s1 join t2 right join t3 using(a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `b`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t3` left join (`test`.`s1` join `test`.`t2`) on((`test`.`t2`.`a` = `test`.`t3`.`a`)) where 1 +select s1.b, t2.a as t2_a, t3.a as t3_a +from s1 join t2 right join t3 using(a); +b t2_a t3_a +7 7 7 +5 7 7 +3 7 7 +NULL NULL 2 +NULL NULL 3 +explain extended select s1.b, t2.a as t2_a, t3.a as t3_a +from (s1 join t2) right join t3 using(a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `b`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t3` left join (`test`.`s1` join `test`.`t2`) on((`test`.`t2`.`a` = `test`.`t3`.`a`)) where 1 +select s1.b, t2.a as t2_a, t3.a as t3_a +from (s1 join t2) right join t3 using(a); +b t2_a t3_a +7 7 7 +5 7 7 +3 7 7 +NULL NULL 2 +NULL NULL 3 +explain extended select * +from s1 join t2 natural join t3; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t2`.`a` AS `a`,`test`.`s1`.`b` AS `b` from `test`.`s1` join `test`.`t2` join `test`.`t3` where (`test`.`t3`.`a` = `test`.`t2`.`a`) +select * +from s1 join t2 natural join t3; +a b +7 7 +7 5 +7 3 +explain extended select * +from s1 join t2 natural left join t3; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t2`.`a` AS `a`,`test`.`s1`.`b` AS `b` from `test`.`s1` join `test`.`t2` left join `test`.`t3` on((`test`.`t3`.`a` = `test`.`t2`.`a`)) where 1 +select * +from s1 join t2 natural left join t3; +a b +7 7 +7 5 +7 3 +5 7 +5 5 +5 3 +1 7 +1 5 +1 3 +explain extended select * +from s1 join t2 natural right join t3; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t3`.`a` AS `a`,`test`.`s1`.`b` AS `b` from `test`.`t3` left join (`test`.`s1` join `test`.`t2`) on((`test`.`t2`.`a` = `test`.`t3`.`a`)) where 1 +select * +from s1 join t2 natural right join t3; +a b +7 7 +7 5 +7 3 +2 NULL +3 NULL +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t1` join `test`.`t2` join `test`.`t3` +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3; +t1_a t2_a t3_a +7 5 2 +5 5 2 +3 5 2 +7 1 2 +5 1 2 +3 1 2 +7 7 2 +5 7 2 +3 7 2 +7 5 7 +5 5 7 +3 5 7 +7 1 7 +5 1 7 +3 1 7 +7 7 7 +5 7 7 +3 7 7 +7 5 3 +5 5 3 +3 5 3 +7 1 3 +5 1 3 +3 1 3 +7 7 3 +5 7 3 +3 7 3 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3 +where t1.a=t2.a and t2.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t1` join `test`.`t2` join `test`.`t3` where ((`test`.`t2`.`a` = `test`.`t1`.`a`) and (`test`.`t3`.`a` = `test`.`t1`.`a`)) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3 +where t1.a=t2.a and t2.a=t3.a; +t1_a t2_a t3_a +7 7 7 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 join t3 join t4 on t3.a=t4.a on t2.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t1` join `test`.`t2` join `test`.`t3` join `test`.`t4` where ((`test`.`t3`.`a` = `test`.`t2`.`a`) and (`test`.`t4`.`a` = `test`.`t2`.`a`)) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 join t3 join t4 on t3.a=t4.a on t2.a=t3.a; +t1_a t2_a t3_a t4_a +7 7 7 7 +5 7 7 7 +3 7 7 7 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 left join t3 join t4 on t3.a=t4.a on t2.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t1` join `test`.`t2` left join (`test`.`t3` join `test`.`t4`) on(((`test`.`t3`.`a` = `test`.`t2`.`a`) and (`test`.`t4`.`a` = `test`.`t2`.`a`))) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 left join t3 join t4 on t3.a=t4.a on t2.a=t3.a; +t1_a t2_a t3_a t4_a +7 7 7 7 +5 7 7 7 +3 7 7 7 +7 5 NULL NULL +5 5 NULL NULL +3 5 NULL NULL +7 1 NULL NULL +5 1 NULL NULL +3 1 NULL NULL +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 left join t3 left join t4 on t3.a=t4.a on t2.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t1` join `test`.`t2` left join (`test`.`t3` left join `test`.`t4` on((`test`.`t4`.`a` = `test`.`t2`.`a`))) on((`test`.`t3`.`a` = `test`.`t2`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 left join t3 left join t4 on t3.a=t4.a on t2.a=t3.a; +t1_a t2_a t3_a t4_a +7 7 7 7 +5 7 7 7 +3 7 7 7 +7 5 NULL NULL +5 5 NULL NULL +3 5 NULL NULL +7 1 NULL NULL +5 1 NULL NULL +3 1 NULL NULL +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 left join t3 right join t4 on t3.a=t4.a on t2.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t1` join `test`.`t2` left join (`test`.`t4` join `test`.`t3`) on(((`test`.`t3`.`a` = `test`.`t2`.`a`) and (`test`.`t4`.`a` = `test`.`t2`.`a`))) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 left join t3 right join t4 on t3.a=t4.a on t2.a=t3.a; +t1_a t2_a t3_a t4_a +7 7 7 7 +5 7 7 7 +3 7 7 7 +7 5 NULL NULL +5 5 NULL NULL +3 5 NULL NULL +7 1 NULL NULL +5 1 NULL NULL +3 1 NULL NULL +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 right join t3 join t4 on t3.a=t4.a on t2.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t3` join `test`.`t4` left join (`test`.`t1` join `test`.`t2`) on((`test`.`t2`.`a` = `test`.`t3`.`a`)) where (`test`.`t4`.`a` = `test`.`t3`.`a`) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 right join t3 join t4 on t3.a=t4.a on t2.a=t3.a; +t1_a t2_a t3_a t4_a +7 7 7 7 +5 7 7 7 +3 7 7 7 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 right join t3 left join t4 on t3.a=t4.a on t2.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t3` left join `test`.`t4` on((`test`.`t4`.`a` = `test`.`t3`.`a`)) left join (`test`.`t1` join `test`.`t2`) on((`test`.`t2`.`a` = `test`.`t3`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 right join t3 left join t4 on t3.a=t4.a on t2.a=t3.a; +t1_a t2_a t3_a t4_a +7 7 7 7 +5 7 7 7 +3 7 7 7 +NULL NULL 2 NULL +NULL NULL 3 NULL +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 right join t3 right join t4 on t3.a=t4.a on t2.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t4` left join `test`.`t3` on((`test`.`t3`.`a` = `test`.`t4`.`a`)) left join (`test`.`t1` join `test`.`t2`) on((`test`.`t2`.`a` = `test`.`t3`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 right join t3 right join t4 on t3.a=t4.a on t2.a=t3.a; +t1_a t2_a t3_a t4_a +7 7 7 7 +5 7 7 7 +3 7 7 7 +NULL NULL NULL 4 +NULL NULL NULL 9 +NULL NULL NULL 5 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 join t3 join t4 on t3.a=t4.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t1` join `test`.`t2` join `test`.`t3` join `test`.`t4` where (`test`.`t4`.`a` = `test`.`t3`.`a`) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 join t3 join t4 on t3.a=t4.a; +t1_a t2_a t3_a t4_a +7 5 7 7 +5 5 7 7 +3 5 7 7 +7 1 7 7 +5 1 7 7 +3 1 7 7 +7 7 7 7 +5 7 7 7 +3 7 7 7 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 join t3 left join t4 on t3.a=t4.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t1` join `test`.`t2` join `test`.`t3` left join `test`.`t4` on((`test`.`t4`.`a` = `test`.`t3`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 join t3 left join t4 on t3.a=t4.a; +t1_a t2_a t3_a t4_a +7 5 7 7 +5 5 7 7 +3 5 7 7 +7 1 7 7 +5 1 7 7 +3 1 7 7 +7 7 7 7 +5 7 7 7 +3 7 7 7 +7 5 2 NULL +5 5 2 NULL +3 5 2 NULL +7 1 2 NULL +5 1 2 NULL +3 1 2 NULL +7 7 2 NULL +5 7 2 NULL +3 7 2 NULL +7 5 3 NULL +5 5 3 NULL +3 5 3 NULL +7 1 3 NULL +5 1 3 NULL +3 1 3 NULL +7 7 3 NULL +5 7 3 NULL +3 7 3 NULL +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 join t3 right join t4 on t3.a=t4.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t4` left join (`test`.`t1` join `test`.`t2` join `test`.`t3`) on((`test`.`t3`.`a` = `test`.`t4`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 join t3 right join t4 on t3.a=t4.a; +t1_a t2_a t3_a t4_a +7 5 7 7 +5 5 7 7 +3 5 7 7 +7 1 7 7 +5 1 7 7 +3 1 7 7 +7 7 7 7 +5 7 7 7 +3 7 7 7 +NULL NULL NULL 4 +NULL NULL NULL 9 +NULL NULL NULL 5 +explain extended select s1.b as s1_b, s2.b as s2_b, t3.a as t3_a, t4.a as t4_a +from s1 join s2 join t3 join t4 using(a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE s2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `s1_b`,`test`.`s2`.`b` AS `s2_b`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`s1` join `test`.`s2` join `test`.`t3` join `test`.`t4` where (`test`.`t4`.`a` = `test`.`t3`.`a`) +select s1.b as s1_b, s2.b as s2_b, t3.a as t3_a, t4.a as t4_a +from s1 join s2 join t3 join t4 using(a); +s1_b s2_b t3_a t4_a +7 5 7 7 +5 5 7 7 +3 5 7 7 +7 1 7 7 +5 1 7 7 +3 1 7 7 +7 7 7 7 +5 7 7 7 +3 7 7 7 +explain extended select s1.b as s1_b, s2.b as s2_b, t3.a as t3_a, t4.a as t4_a +from s1 join s2 join t3 left join t4 using(a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE s2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `s1_b`,`test`.`s2`.`b` AS `s2_b`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`s1` join `test`.`s2` join `test`.`t3` left join `test`.`t4` on((`test`.`t4`.`a` = `test`.`t3`.`a`)) where 1 +select s1.b as s1_b, s2.b as s2_b, t3.a as t3_a, t4.a as t4_a +from s1 join s2 join t3 left join t4 using(a); +s1_b s2_b t3_a t4_a +7 5 7 7 +5 5 7 7 +3 5 7 7 +7 1 7 7 +5 1 7 7 +3 1 7 7 +7 7 7 7 +5 7 7 7 +3 7 7 7 +7 5 2 NULL +5 5 2 NULL +3 5 2 NULL +7 1 2 NULL +5 1 2 NULL +3 1 2 NULL +7 7 2 NULL +5 7 2 NULL +3 7 2 NULL +7 5 3 NULL +5 5 3 NULL +3 5 3 NULL +7 1 3 NULL +5 1 3 NULL +3 1 3 NULL +7 7 3 NULL +5 7 3 NULL +3 7 3 NULL +explain extended select s1.b as s1_b, s2.b as s2_b, t3.a as t3_a, t4.a as t4_a +from s1 join s2 join t3 right join t4 using(a); +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE s2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `s1_b`,`test`.`s2`.`b` AS `s2_b`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t4` left join (`test`.`s1` join `test`.`s2` join `test`.`t3`) on((`test`.`t3`.`a` = `test`.`t4`.`a`)) where 1 +select s1.b as s1_b, s2.b as s2_b, t3.a as t3_a, t4.a as t4_a +from s1 join s2 join t3 right join t4 using(a); +s1_b s2_b t3_a t4_a +7 5 7 7 +5 5 7 7 +3 5 7 7 +7 1 7 7 +5 1 7 7 +3 1 7 7 +7 7 7 7 +5 7 7 7 +3 7 7 7 +NULL NULL NULL 4 +NULL NULL NULL 9 +NULL NULL NULL 5 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 join t3 on t2.a=t3.a join t4; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t1` join `test`.`t2` join `test`.`t3` join `test`.`t4` where (`test`.`t3`.`a` = `test`.`t2`.`a`) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 join t3 on t2.a=t3.a join t4; +t1_a t2_a t3_a t4_a +7 7 7 4 +5 7 7 4 +3 7 7 4 +7 7 7 7 +5 7 7 7 +3 7 7 7 +7 7 7 9 +5 7 7 9 +3 7 7 9 +7 7 7 5 +5 7 7 5 +3 7 7 5 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 left join t3 on t2.a=t3.a join t4; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t1` join `test`.`t2` left join `test`.`t3` on((`test`.`t3`.`a` = `test`.`t2`.`a`)) join `test`.`t4` where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 left join t3 on t2.a=t3.a join t4; +t1_a t2_a t3_a t4_a +7 7 7 4 +5 7 7 4 +3 7 7 4 +7 5 NULL 4 +5 5 NULL 4 +3 5 NULL 4 +7 1 NULL 4 +5 1 NULL 4 +3 1 NULL 4 +7 7 7 7 +5 7 7 7 +3 7 7 7 +7 5 NULL 7 +5 5 NULL 7 +3 5 NULL 7 +7 1 NULL 7 +5 1 NULL 7 +3 1 NULL 7 +7 7 7 9 +5 7 7 9 +3 7 7 9 +7 5 NULL 9 +5 5 NULL 9 +3 5 NULL 9 +7 1 NULL 9 +5 1 NULL 9 +3 1 NULL 9 +7 7 7 5 +5 7 7 5 +3 7 7 5 +7 5 NULL 5 +5 5 NULL 5 +3 5 NULL 5 +7 1 NULL 5 +5 1 NULL 5 +3 1 NULL 5 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 right join t3 on t2.a=t3.a join t4; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t3` left join (`test`.`t1` join `test`.`t2`) on((`test`.`t2`.`a` = `test`.`t3`.`a`)) join `test`.`t4` where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 right join t3 on t2.a=t3.a join t4; +t1_a t2_a t3_a t4_a +7 7 7 4 +5 7 7 4 +3 7 7 4 +7 7 7 7 +5 7 7 7 +3 7 7 7 +7 7 7 9 +5 7 7 9 +3 7 7 9 +7 7 7 5 +5 7 7 5 +3 7 7 5 +NULL NULL 2 4 +NULL NULL 3 4 +NULL NULL 2 7 +NULL NULL 3 7 +NULL NULL 2 9 +NULL NULL 3 9 +NULL NULL 2 5 +NULL NULL 3 5 +explain extended select s1.b as s1_b, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t2 join t3 using(a) join t4; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `s1_b`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`s1` join `test`.`t2` join `test`.`t3` join `test`.`t4` where (`test`.`t3`.`a` = `test`.`t2`.`a`) +select s1.b as s1_b, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t2 join t3 using(a) join t4; +s1_b t2_a t3_a t4_a +7 7 7 4 +5 7 7 4 +3 7 7 4 +7 7 7 7 +5 7 7 7 +3 7 7 7 +7 7 7 9 +5 7 7 9 +3 7 7 9 +7 7 7 5 +5 7 7 5 +3 7 7 5 +explain extended select s1.b as s1_b, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t2 left join t3 using(a) join t4; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `s1_b`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`s1` join `test`.`t2` left join `test`.`t3` on((`test`.`t3`.`a` = `test`.`t2`.`a`)) join `test`.`t4` where 1 +select s1.b as s1_b, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t2 left join t3 using(a) join t4; +s1_b t2_a t3_a t4_a +7 7 7 4 +5 7 7 4 +3 7 7 4 +7 5 NULL 4 +5 5 NULL 4 +3 5 NULL 4 +7 1 NULL 4 +5 1 NULL 4 +3 1 NULL 4 +7 7 7 7 +5 7 7 7 +3 7 7 7 +7 5 NULL 7 +5 5 NULL 7 +3 5 NULL 7 +7 1 NULL 7 +5 1 NULL 7 +3 1 NULL 7 +7 7 7 9 +5 7 7 9 +3 7 7 9 +7 5 NULL 9 +5 5 NULL 9 +3 5 NULL 9 +7 1 NULL 9 +5 1 NULL 9 +3 1 NULL 9 +7 7 7 5 +5 7 7 5 +3 7 7 5 +7 5 NULL 5 +5 5 NULL 5 +3 5 NULL 5 +7 1 NULL 5 +5 1 NULL 5 +3 1 NULL 5 +explain extended select s1.b as s1_b, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t2 right join t3 using(a) join t4; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `s1_b`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t3` left join (`test`.`s1` join `test`.`t2`) on((`test`.`t2`.`a` = `test`.`t3`.`a`)) join `test`.`t4` where 1 +select s1.b as s1_b, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t2 right join t3 using(a) join t4; +s1_b t2_a t3_a t4_a +7 7 7 4 +5 7 7 4 +3 7 7 4 +7 7 7 7 +5 7 7 7 +3 7 7 7 +7 7 7 9 +5 7 7 9 +3 7 7 9 +7 7 7 5 +5 7 7 5 +3 7 7 5 +NULL NULL 2 4 +NULL NULL 3 4 +NULL NULL 2 7 +NULL NULL 3 7 +NULL NULL 2 9 +NULL NULL 3 9 +NULL NULL 2 5 +NULL NULL 3 5 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 on t1.a=t2.a join t3 join t4 on t3.a=t4.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t1` join `test`.`t2` join `test`.`t3` join `test`.`t4` where ((`test`.`t4`.`a` = `test`.`t3`.`a`) and (`test`.`t2`.`a` = `test`.`t1`.`a`)) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 on t1.a=t2.a join t3 join t4 on t3.a=t4.a; +t1_a t2_a t3_a t4_a +5 5 7 7 +7 7 7 7 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 on t1.a=t2.a join t3 left join t4 on t3.a=t4.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t1` join `test`.`t2` join `test`.`t3` left join `test`.`t4` on((`test`.`t4`.`a` = `test`.`t3`.`a`)) where (`test`.`t2`.`a` = `test`.`t1`.`a`) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 on t1.a=t2.a join t3 left join t4 on t3.a=t4.a; +t1_a t2_a t3_a t4_a +5 5 7 7 +7 7 7 7 +5 5 2 NULL +7 7 2 NULL +5 5 3 NULL +7 7 3 NULL +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 on t1.a=t2.a join t3 right join t4 on t3.a=t4.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t4` left join (`test`.`t1` join `test`.`t2` join `test`.`t3`) on(((`test`.`t3`.`a` = `test`.`t4`.`a`) and (`test`.`t2`.`a` = `test`.`t1`.`a`))) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 on t1.a=t2.a join t3 right join t4 on t3.a=t4.a; +t1_a t2_a t3_a t4_a +5 5 7 7 +7 7 7 7 +NULL NULL NULL 4 +NULL NULL NULL 9 +NULL NULL NULL 5 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 left join t2 on t1.a=t2.a join t3 join t4 on t3.a=t4.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t1` left join `test`.`t2` on((`test`.`t2`.`a` = `test`.`t1`.`a`)) join `test`.`t3` join `test`.`t4` where (`test`.`t4`.`a` = `test`.`t3`.`a`) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 left join t2 on t1.a=t2.a join t3 join t4 on t3.a=t4.a; +t1_a t2_a t3_a t4_a +5 5 7 7 +7 7 7 7 +3 NULL 7 7 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 left join t2 on t1.a=t2.a join t3 left join t4 on t3.a=t4.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t1` left join `test`.`t2` on((`test`.`t2`.`a` = `test`.`t1`.`a`)) join `test`.`t3` left join `test`.`t4` on((`test`.`t4`.`a` = `test`.`t3`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 left join t2 on t1.a=t2.a join t3 left join t4 on t3.a=t4.a; +t1_a t2_a t3_a t4_a +5 5 7 7 +7 7 7 7 +3 NULL 7 7 +5 5 2 NULL +7 7 2 NULL +3 NULL 2 NULL +5 5 3 NULL +7 7 3 NULL +3 NULL 3 NULL +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 left join t2 on t1.a=t2.a join t3 right join t4 on t3.a=t4.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t4` left join (`test`.`t1` left join `test`.`t2` on((`test`.`t2`.`a` = `test`.`t1`.`a`)) join `test`.`t3`) on((`test`.`t3`.`a` = `test`.`t4`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 left join t2 on t1.a=t2.a join t3 right join t4 on t3.a=t4.a; +t1_a t2_a t3_a t4_a +5 5 7 7 +7 7 7 7 +3 NULL 7 7 +NULL NULL NULL 4 +NULL NULL NULL 9 +NULL NULL NULL 5 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 right join t2 on t1.a=t2.a join t3 join t4 on t3.a=t4.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t2` left join `test`.`t1` on((`test`.`t1`.`a` = `test`.`t2`.`a`)) join `test`.`t3` join `test`.`t4` where (`test`.`t4`.`a` = `test`.`t3`.`a`) +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 right join t2 on t1.a=t2.a join t3 join t4 on t3.a=t4.a; +t1_a t2_a t3_a t4_a +7 7 7 7 +5 5 7 7 +NULL 1 7 7 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 right join t2 on t1.a=t2.a join t3 left join t4 on t3.a=t4.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t2` left join `test`.`t1` on((`test`.`t1`.`a` = `test`.`t2`.`a`)) join `test`.`t3` left join `test`.`t4` on((`test`.`t4`.`a` = `test`.`t3`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 right join t2 on t1.a=t2.a join t3 left join t4 on t3.a=t4.a; +t1_a t2_a t3_a t4_a +7 7 7 7 +5 5 7 7 +NULL 1 7 7 +7 7 2 NULL +5 5 2 NULL +NULL 1 2 NULL +7 7 3 NULL +5 5 3 NULL +NULL 1 3 NULL +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 right join t2 on t1.a=t2.a join t3 right join t4 on t3.a=t4.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t4` left join (`test`.`t2` left join `test`.`t1` on((`test`.`t1`.`a` = `test`.`t2`.`a`)) join `test`.`t3`) on((`test`.`t3`.`a` = `test`.`t4`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 right join t2 on t1.a=t2.a join t3 right join t4 on t3.a=t4.a; +t1_a t2_a t3_a t4_a +7 7 7 7 +5 5 7 7 +NULL 1 7 7 +NULL NULL NULL 4 +NULL NULL NULL 9 +NULL NULL NULL 5 +explain extended select s1.b as s1_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t1 left join t2 on s1.b=t2.a join t3 join t4 on t4.a=s1.b; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `s1_b`,`test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`s1` join `test`.`t1` left join `test`.`t2` on((`test`.`t2`.`a` = `test`.`s1`.`b`)) join `test`.`t3` join `test`.`t4` where (`test`.`t4`.`a` = `test`.`s1`.`b`) +select s1.b as s1_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t1 left join t2 on s1.b=t2.a join t3 join t4 on t4.a=s1.b; +s1_b t1_a t2_a t3_a t4_a +7 7 7 2 7 +7 5 7 2 7 +7 3 7 2 7 +7 7 7 7 7 +7 5 7 7 7 +7 3 7 7 7 +7 7 7 3 7 +7 5 7 3 7 +7 3 7 3 7 +5 7 5 2 5 +5 5 5 2 5 +5 3 5 2 5 +5 7 5 7 5 +5 5 5 7 5 +5 3 5 7 5 +5 7 5 3 5 +5 5 5 3 5 +5 3 5 3 5 +explain extended select s1.b as s1_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t1 left join t2 on s1.b=t2.a join t3 left join t4 on t4.a=s1.b; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `s1_b`,`test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`s1` join `test`.`t1` left join `test`.`t2` on((`test`.`t2`.`a` = `test`.`s1`.`b`)) join `test`.`t3` left join `test`.`t4` on((`test`.`t4`.`a` = `test`.`s1`.`b`)) where 1 +select s1.b as s1_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t1 left join t2 on s1.b=t2.a join t3 left join t4 on t4.a=s1.b; +s1_b t1_a t2_a t3_a t4_a +7 7 7 2 7 +7 5 7 2 7 +7 3 7 2 7 +7 7 7 7 7 +7 5 7 7 7 +7 3 7 7 7 +7 7 7 3 7 +7 5 7 3 7 +7 3 7 3 7 +5 7 5 2 5 +5 5 5 2 5 +5 3 5 2 5 +5 7 5 7 5 +5 5 5 7 5 +5 3 5 7 5 +5 7 5 3 5 +5 5 5 3 5 +5 3 5 3 5 +3 7 NULL 2 NULL +3 5 NULL 2 NULL +3 3 NULL 2 NULL +3 7 NULL 7 NULL +3 5 NULL 7 NULL +3 3 NULL 7 NULL +3 7 NULL 3 NULL +3 5 NULL 3 NULL +3 3 NULL 3 NULL +explain extended select s1.b as s1_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t1 left join t2 on s1.b=t2.a join t3 right join t4 on t4.a=s1.b; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `s1_b`,`test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t4` left join (`test`.`s1` join `test`.`t1` left join `test`.`t2` on((`test`.`t2`.`a` = `test`.`t4`.`a`)) join `test`.`t3`) on((`test`.`s1`.`b` = `test`.`t4`.`a`)) where 1 +select s1.b as s1_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t1 left join t2 on s1.b=t2.a join t3 right join t4 on t4.a=s1.b; +s1_b t1_a t2_a t3_a t4_a +5 7 5 2 5 +5 5 5 2 5 +5 3 5 2 5 +7 7 7 2 7 +7 5 7 2 7 +7 3 7 2 7 +5 7 5 7 5 +5 5 5 7 5 +5 3 5 7 5 +7 7 7 7 7 +7 5 7 7 7 +7 3 7 7 7 +5 7 5 3 5 +5 5 5 3 5 +5 3 5 3 5 +7 7 7 3 7 +7 5 7 3 7 +7 3 7 3 7 +NULL NULL NULL NULL 4 +NULL NULL NULL NULL 9 +explain extended select s1.b as s1_b, s2.b as s2_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from s1 join s2 on s1.b=s2.b join t1 right join t2 on t1.a=t2.a join t3; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE s2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `s1_b`,`test`.`s2`.`b` AS `s2_b`,`test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t2` left join (`test`.`s1` join `test`.`s2` join `test`.`t1`) on(((`test`.`t1`.`a` = `test`.`t2`.`a`) and (`test`.`s2`.`b` = `test`.`s1`.`b`))) join `test`.`t3` where 1 +select s1.b as s1_b, s2.b as s2_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from s1 join s2 on s1.b=s2.b join t1 right join t2 on t1.a=t2.a join t3; +s1_b s2_b t1_a t2_a t3_a +5 5 7 7 2 +7 7 7 7 2 +5 5 5 5 2 +7 7 5 5 2 +5 5 7 7 7 +7 7 7 7 7 +5 5 5 5 7 +7 7 5 5 7 +5 5 7 7 3 +7 7 7 7 3 +5 5 5 5 3 +7 7 5 5 3 +NULL NULL NULL 1 2 +NULL NULL NULL 1 7 +NULL NULL NULL 1 3 +explain extended select s1.b as s1_b, s2.b as s2_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from s1 left join s2 on s1.b=s2.b join t1 right join t2 on t1.a=t2.a join t3; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE s2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `s1_b`,`test`.`s2`.`b` AS `s2_b`,`test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t2` left join (`test`.`s1` left join `test`.`s2` on((`test`.`s2`.`b` = `test`.`s1`.`b`)) join `test`.`t1`) on((`test`.`t1`.`a` = `test`.`t2`.`a`)) join `test`.`t3` where 1 +select s1.b as s1_b, s2.b as s2_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from s1 left join s2 on s1.b=s2.b join t1 right join t2 on t1.a=t2.a join t3; +s1_b s2_b t1_a t2_a t3_a +5 5 7 7 2 +7 7 7 7 2 +3 NULL 7 7 2 +5 5 5 5 2 +7 7 5 5 2 +3 NULL 5 5 2 +5 5 7 7 7 +7 7 7 7 7 +3 NULL 7 7 7 +5 5 5 5 7 +7 7 5 5 7 +3 NULL 5 5 7 +5 5 7 7 3 +7 7 7 7 3 +3 NULL 7 7 3 +5 5 5 5 3 +7 7 5 5 3 +3 NULL 5 5 3 +NULL NULL NULL 1 2 +NULL NULL NULL 1 7 +NULL NULL NULL 1 3 +explain extended select s1.b as s1_b, s2.b as s2_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from s1 right join s2 on s1.b=s2.b join t1 right join t2 on t1.a=t2.a join t3; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 +1 SIMPLE s2 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (flat, BNL join) +1 SIMPLE s1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`s1`.`b` AS `s1_b`,`test`.`s2`.`b` AS `s2_b`,`test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a` from `test`.`t2` left join (`test`.`s2` left join `test`.`s1` on((`test`.`s1`.`b` = `test`.`s2`.`b`)) join `test`.`t1`) on((`test`.`t1`.`a` = `test`.`t2`.`a`)) join `test`.`t3` where 1 +select s1.b as s1_b, s2.b as s2_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from s1 right join s2 on s1.b=s2.b join t1 right join t2 on t1.a=t2.a join t3; +s1_b s2_b t1_a t2_a t3_a +7 7 7 7 2 +5 5 7 7 2 +NULL 1 7 7 2 +7 7 5 5 2 +5 5 5 5 2 +NULL 1 5 5 2 +7 7 7 7 7 +5 5 7 7 7 +NULL 1 7 7 7 +7 7 5 5 7 +5 5 5 5 7 +NULL 1 5 5 7 +7 7 7 7 3 +5 5 7 7 3 +NULL 1 7 7 3 +7 7 5 5 3 +5 5 5 5 3 +NULL 1 5 5 3 +NULL NULL NULL 1 2 +NULL NULL NULL 1 7 +NULL NULL NULL 1 3 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 right join t3 right join t4 on t3.a=t4.a on t2.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t4` left join `test`.`t3` on((`test`.`t3`.`a` = `test`.`t4`.`a`)) left join (`test`.`t1` join `test`.`t2`) on((`test`.`t2`.`a` = `test`.`t3`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 right join t3 right join t4 on t3.a=t4.a on t2.a=t3.a; +t1_a t2_a t3_a t4_a +7 7 7 7 +5 7 7 7 +3 7 7 7 +NULL NULL NULL 4 +NULL NULL NULL 9 +NULL NULL NULL 5 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from (t1 join t2) right join t3 right join t4 on t3.a=t4.a on t2.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t4` left join `test`.`t3` on((`test`.`t3`.`a` = `test`.`t4`.`a`)) left join (`test`.`t1` join `test`.`t2`) on((`test`.`t2`.`a` = `test`.`t3`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from (t1 join t2) right join t3 right join t4 on t3.a=t4.a on t2.a=t3.a; +t1_a t2_a t3_a t4_a +7 7 7 7 +5 7 7 7 +3 7 7 7 +NULL NULL NULL 4 +NULL NULL NULL 9 +NULL NULL NULL 5 +explain extended select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from (t1, t2) right join t3 right join t4 on t3.a=t4.a on t2.a=t3.a; +id select_type table type possible_keys key key_len ref rows filtered Extra +1 SIMPLE t4 ALL NULL NULL NULL NULL 4 100.00 +1 SIMPLE t3 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (flat, BNL join) +1 SIMPLE t1 ALL NULL NULL NULL NULL 3 100.00 Using join buffer (incremental, BNL join) +1 SIMPLE t2 ALL NULL NULL NULL NULL 3 100.00 Using where; Using join buffer (incremental, BNL join) +Warnings: +Note 1003 select `test`.`t1`.`a` AS `t1_a`,`test`.`t2`.`a` AS `t2_a`,`test`.`t3`.`a` AS `t3_a`,`test`.`t4`.`a` AS `t4_a` from `test`.`t4` left join `test`.`t3` on((`test`.`t3`.`a` = `test`.`t4`.`a`)) left join (`test`.`t1` join `test`.`t2`) on((`test`.`t2`.`a` = `test`.`t3`.`a`)) where 1 +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from (t1, t2) right join t3 right join t4 on t3.a=t4.a on t2.a=t3.a; +t1_a t2_a t3_a t4_a +7 7 7 7 +5 7 7 7 +3 7 7 7 +NULL NULL NULL 4 +NULL NULL NULL 9 +NULL NULL NULL 5 +drop table t1,t2,t3,t4,s1,s2; +# # End of MariaDB 5.5 tests # diff --git a/mysql-test/r/join_outer_innodb.result b/mysql-test/r/join_outer_innodb.result index 336fb5ee6b6bd..5c14b3c25510e 100644 --- a/mysql-test/r/join_outer_innodb.result +++ b/mysql-test/r/join_outer_innodb.result @@ -486,7 +486,7 @@ Note 1051 Unknown table 't2' Note 1051 Unknown table 't3' create table t2(a int,unique key (a)) engine=innodb; create table t3(b int) engine=innodb; -create table t1(a int,b int)engine=innodb; +create table t1(c int,b int)engine=innodb; set @mdev4270_opl= @@optimizer_prune_level; set @mdev4270_osd= @@optimizer_search_depth; set optimizer_prune_level=0; diff --git a/mysql-test/t/join.test b/mysql-test/t/join.test index 5a29fe7204967..168cfe4e4778c 100644 --- a/mysql-test/t/join.test +++ b/mysql-test/t/join.test @@ -1209,6 +1209,410 @@ CALL p1; DROP PROCEDURE p1; DROP TABLE t1,t2,t3,t4,t5; +--echo # +--echo # MDEV-19421: Embedding inner joins +--echo # + +create table t1 (a int); +insert into t1 values (7), (5), (3); +create table s1 (b int); +insert into s1 values (7), (5), (3); +create table t2 (a int); +insert into t2 values (5), (1), (7); +create table s2 (b int); +insert into s2 values (5), (1), (7); +create table t3 (a int); +insert into t3 values (2), (7), (3); +create table t4 (a int); +insert into t4 values (4), (7), (9), (5); + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3 on t2.a=t3.a on t1.a=t2.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 left join t3 on t2.a=t3.a on t1.a=t2.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 right join t3 on t2.a=t3.a on t1.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3 using(a) using(a); +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 left join t3 using(a) using(a); +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 right join t3 using(a) using(a); +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3 on t2.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 left join t3 on t2.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 right join t3 on t2.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3 on t1.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 left join t3 on t1.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 right join t3 on t1.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join (t2 join t3 on t2.a=t3.a); +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join (t2 left join t3 on t2.a=t3.a); +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join (t2 right join t3 on t2.a=t3.a); +eval explain extended $q; +eval $q; + +let $q= +select * +from s1 join t2 join t3 using(a); +eval explain extended $q; +eval $q; + +let $q= +select * +from s1 join t2 left join t3 using(a); +eval explain extended $q; +eval $q; + +let $q= +select * +from s1 join t2 right join t3 using(a); +eval explain extended $q; +eval $q; + +let $q= +select s1.b, t2.a as t2_a, t3.a as t3_a +from s1 join t2 join t3 using(a); +eval explain extended $q; +eval $q; + +let $q= +select s1.b, t2.a as t2_a, t3.a as t3_a +from s1 join t2 left join t3 using(a); +eval explain extended $q; +eval $q; + +let $q= +select s1.b, t2.a as t2_a, t3.a as t3_a +from s1 join t2 right join t3 using(a); +eval explain extended $q; +eval $q; + +let $q= +select s1.b, t2.a as t2_a, t3.a as t3_a +from (s1 join t2) right join t3 using(a); +eval explain extended $q; +eval $q; + +let $q= +select * +from s1 join t2 natural join t3; +eval explain extended $q; +eval $q; + +let $q= +select * +from s1 join t2 natural left join t3; +eval explain extended $q; +eval $q; + +let $q= +select * +from s1 join t2 natural right join t3; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from t1 join t2 join t3 +where t1.a=t2.a and t2.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 join t3 join t4 on t3.a=t4.a on t2.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 left join t3 join t4 on t3.a=t4.a on t2.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 left join t3 left join t4 on t3.a=t4.a on t2.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 left join t3 right join t4 on t3.a=t4.a on t2.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 right join t3 join t4 on t3.a=t4.a on t2.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 right join t3 left join t4 on t3.a=t4.a on t2.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 right join t3 right join t4 on t3.a=t4.a on t2.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 join t3 join t4 on t3.a=t4.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 join t3 left join t4 on t3.a=t4.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 join t3 right join t4 on t3.a=t4.a; +eval explain extended $q; +eval $q; + +let $q= +select s1.b as s1_b, s2.b as s2_b, t3.a as t3_a, t4.a as t4_a +from s1 join s2 join t3 join t4 using(a); +eval explain extended $q; +eval $q; + +let $q= +select s1.b as s1_b, s2.b as s2_b, t3.a as t3_a, t4.a as t4_a +from s1 join s2 join t3 left join t4 using(a); +eval explain extended $q; +eval $q; + +let $q= +select s1.b as s1_b, s2.b as s2_b, t3.a as t3_a, t4.a as t4_a +from s1 join s2 join t3 right join t4 using(a); +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 join t3 on t2.a=t3.a join t4; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 left join t3 on t2.a=t3.a join t4; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 right join t3 on t2.a=t3.a join t4; +eval explain extended $q; +eval $q; + +let $q= +select s1.b as s1_b, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t2 join t3 using(a) join t4; +eval explain extended $q; +eval $q; + +let $q= +select s1.b as s1_b, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t2 left join t3 using(a) join t4; +eval explain extended $q; +eval $q; + +let $q= +select s1.b as s1_b, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t2 right join t3 using(a) join t4; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 on t1.a=t2.a join t3 join t4 on t3.a=t4.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 on t1.a=t2.a join t3 left join t4 on t3.a=t4.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 on t1.a=t2.a join t3 right join t4 on t3.a=t4.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 left join t2 on t1.a=t2.a join t3 join t4 on t3.a=t4.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 left join t2 on t1.a=t2.a join t3 left join t4 on t3.a=t4.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 left join t2 on t1.a=t2.a join t3 right join t4 on t3.a=t4.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 right join t2 on t1.a=t2.a join t3 join t4 on t3.a=t4.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 right join t2 on t1.a=t2.a join t3 left join t4 on t3.a=t4.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 right join t2 on t1.a=t2.a join t3 right join t4 on t3.a=t4.a; +eval explain extended $q; +eval $q; + +let $q= +select s1.b as s1_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t1 left join t2 on s1.b=t2.a join t3 join t4 on t4.a=s1.b; +eval explain extended $q; +eval $q; + +let $q= +select s1.b as s1_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t1 left join t2 on s1.b=t2.a join t3 left join t4 on t4.a=s1.b; +eval explain extended $q; +eval $q; + +let $q= +select s1.b as s1_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from s1 join t1 left join t2 on s1.b=t2.a join t3 right join t4 on t4.a=s1.b; +eval explain extended $q; +eval $q; + +let $q= +select s1.b as s1_b, s2.b as s2_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from s1 join s2 on s1.b=s2.b join t1 right join t2 on t1.a=t2.a join t3; +eval explain extended $q; +eval $q; + +let $q= +select s1.b as s1_b, s2.b as s2_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from s1 left join s2 on s1.b=s2.b join t1 right join t2 on t1.a=t2.a join t3; +eval explain extended $q; +eval $q; + +let $q= +select s1.b as s1_b, s2.b as s2_b, t1.a as t1_a, t2.a as t2_a, t3.a as t3_a +from s1 right join s2 on s1.b=s2.b join t1 right join t2 on t1.a=t2.a join t3; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from t1 join t2 right join t3 right join t4 on t3.a=t4.a on t2.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from (t1 join t2) right join t3 right join t4 on t3.a=t4.a on t2.a=t3.a; +eval explain extended $q; +eval $q; + +let $q= +select t1.a as t1_a, t2.a as t2_a, t3.a as t3_a, t4.a as t4_a +from (t1, t2) right join t3 right join t4 on t3.a=t4.a on t2.a=t3.a; +eval explain extended $q; +eval $q; + +drop table t1,t2,t3,t4,s1,s2; + --echo # --echo # End of MariaDB 5.5 tests --echo # diff --git a/mysql-test/t/join_outer_innodb.test b/mysql-test/t/join_outer_innodb.test index 539d85a8b119f..365c78183eb8a 100644 --- a/mysql-test/t/join_outer_innodb.test +++ b/mysql-test/t/join_outer_innodb.test @@ -358,7 +358,7 @@ drop table t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16; drop table if exists t1,t2,t3; create table t2(a int,unique key (a)) engine=innodb; create table t3(b int) engine=innodb; -create table t1(a int,b int)engine=innodb; +create table t1(c int,b int)engine=innodb; set @mdev4270_opl= @@optimizer_prune_level; set @mdev4270_osd= @@optimizer_search_depth; diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 615b583897bde..8f629750198ae 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -957,6 +957,8 @@ class st_select_lex: public st_select_lex_node TABLE_LIST *end_nested_join(THD *thd); TABLE_LIST *nest_last_join(THD *thd); void add_joined_table(TABLE_LIST *table); + bool add_cross_joined_table(TABLE_LIST *left_op, TABLE_LIST *right_op, + bool straight_fl); TABLE_LIST *convert_right_join(); List* get_item_list(); ulong get_table_join_options(); @@ -2745,9 +2747,9 @@ struct LEX: public Query_tables_list return context_stack.push_front(context); } - void pop_context() + Name_resolution_context *pop_context() { - context_stack.pop(); + return context_stack.pop(); } bool copy_db_to(char **p_db, size_t *p_db_length) const; diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc index 1d5733a646bf8..346f8ad5e8b1f 100644 --- a/sql/sql_parse.cc +++ b/sql/sql_parse.cc @@ -6390,6 +6390,7 @@ TABLE_LIST *st_select_lex::end_nested_join(THD *thd) join_list= ptr->join_list; embedding= ptr->embedding; nested_join= ptr->nested_join; + nested_join->nest_type= 0; if (nested_join->join_list.elements == 1) { TABLE_LIST *embedded= nested_join->join_list.head(); @@ -6399,6 +6400,8 @@ TABLE_LIST *st_select_lex::end_nested_join(THD *thd) join_list->push_front(embedded); ptr= embedded; embedded->lifted= 1; + if (embedded->nested_join) + embedded->nested_join->nest_type= 0; } else if (nested_join->join_list.elements == 0) { @@ -6429,6 +6432,16 @@ TABLE_LIST *st_select_lex::nest_last_join(THD *thd) List *embedded_list; DBUG_ENTER("nest_last_join"); + TABLE_LIST *head= join_list->head(); + if (head->nested_join && head->nested_join->nest_type & REBALANCED_NEST) + { + List_iterator li(*join_list); + li++; + while (li++) + li.remove(); + DBUG_RETURN(head); + } + if (!(ptr= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+ sizeof(NESTED_JOIN)))) DBUG_RETURN(0); @@ -6440,6 +6453,7 @@ TABLE_LIST *st_select_lex::nest_last_join(THD *thd) ptr->alias= (char*) "(nest_last_join)"; embedded_list= &nested_join->join_list; embedded_list->empty(); + nested_join->nest_type= JOIN_OP_NEST; for (uint i=0; i < 2; i++) { @@ -6490,6 +6504,227 @@ void st_select_lex::add_joined_table(TABLE_LIST *table) } +/** + @brief + Create a node for JOIN/INNER JOIN/CROSS JOIN/STRAIGHT_JOIN operation + + @param left_op the node for the left operand constructed by the parser + @param right_op the node for the right operand constructed by the parser + @param straight_fl TRUE if STRAIGHT_JOIN is used + + @retval + false on success + true otherwise + + @details + + JOIN operator can be left-associative with other join operators in one + context and right-associative in another context. + + In this query + SELECT * FROM t1 JOIN t2 LEFT JOIN t3 ON t2.a=t3.a (Q1) + JOIN is left-associative and the query Q1 is interpreted as + SELECT * FROM (t1 JOIN t2) LEFT JOIN t3 ON t2.a=t3.a. + While in this query + SELECT * FROM t1 JOIN t2 LEFT JOIN t3 ON t2.a=t3.a ON t1.b=t2.b (Q2) + JOIN is right-associative and the query Q2 is interpreted as + SELECT * FROM t1 JOIN (t2 LEFT JOIN t3 ON t2.a=t3.a) ON t1.b=t2.b + + JOIN is right-associative if it is used with ON clause or with USING clause. + Otherwise it is left-associative. + When parsing a join expression with JOIN operator we can't determine + whether this operation left or right associative until either we read the + corresponding ON clause or we reach the end of the expression. This creates + a problem for the parser to build a proper internal representation of the + used join expression. + + For Q1 and Q2 the trees representing the used join expressions look like + + LJ - ON J - ON + / \ / \ + J t3 (TQ1) t1 LJ - ON (TQ2) + / \ / \ + t1 t2 t2 t3 + + To build TQ1 the parser has to reduce the expression for JOIN right after + it has read the reference to t2. To build TQ2 the parser reduces JOIN + when he has read the whole join expression. There is no way to determine + whether an early reduction is needed until the whole join expression is + read. + A solution here is always to do a late reduction. In this case the parser + first builds an incorrect tree TQ1* that has to be rebalanced right after + it has been constructed. + + J LJ - ON + / \ / \ + t1 LJ - ON (TQ1*) => J t3 + / \ / \ + t2 t3 t1 t2 + + Actually the transformation is performed over the nodes t1 and LJ before the + node for J is created in the function st_select_lex::add_cross_joined_table. + The function creates a node for J which replaces the node t2. Then it + attaches the nodes t1 and t2 to this newly created node. The node LJ becomes + the top node of the tree. + + For the query + SELECT * FROM t1 JOIN t2 RIGHT JOIN t3 ON t2.a=t3.a (Q3) + the transformation looks slightly differently because the parser + replaces the RIGHT JOIN tree for an equivalent LEFT JOIN tree. + + J LJ - ON + / \ / \ + t1 LJ - ON (TQ3*) => J t2 + / \ / \ + t3 t2 t1 t3 + + With several left associative JOINs + SELECT * FROM t1 JOIN t2 JOIN t3 LEFT JOIN t4 ON t3.a=t4.a (Q4) + the newly created node for JOIN replaces the left most node of the tree: + + J1 LJ - ON + / \ / \ + t1 LJ - ON J2 t4 + / \ => / \ + J2 t4 J1 t3 + / \ / \ + t2 t3 t1 t2 + + Here's another example: + SELECT * + FROM t1 JOIN t2 LEFT JOIN t3 JOIN t4 ON t3.a=t4.a ON t2.b=t3.b (Q5) + + J LJ - ON + / \ / \ + t1 LJ - ON J J - ON + / \ => / \ / \ + t2 J - ON t1 t2 t3 t4 + / \ + t3 t4 + + If the transformed nested join node node is a natural join node like in + the following query + SELECT * FROM t1 JOIN t2 LEFT JOIN t3 USING(a) (Q6) + the transformation additionally has to take care about setting proper + references in the field natural_join for both operands of the natural + join operation. + The function also has to change the name resolution context for ON + expressions used in the transformed join expression to take into + account the tables of the left_op node. +*/ + +bool st_select_lex::add_cross_joined_table(TABLE_LIST *left_op, + TABLE_LIST *right_op, + bool straight_fl) +{ + DBUG_ENTER("add_cross_joined_table"); + THD *thd= parent_lex->thd; + if (!(right_op->nested_join && + (right_op->nested_join->nest_type & JOIN_OP_NEST))) + { + /* + This handles the cases when the right operand is not a nested join. + like in queries + SELECT * FROM t1 JOIN t2; + SELECT * FROM t1 LEFT JOIN t2 ON t1.a=t2.a JOIN t3 + */ + right_op->straight= straight_fl; + DBUG_RETURN(false); + } + + TABLE_LIST *tbl; + List *jl= &right_op->nested_join->join_list; + TABLE_LIST *cj_nest; + + /* + Create the node NJ for a new nested join for the future inclusion + of left_op in it. Initially the nest is empty. + */ + if (unlikely(!(cj_nest= + (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+ + sizeof(NESTED_JOIN))))) + DBUG_RETURN(true); + cj_nest->nested_join= + ((NESTED_JOIN*) ((uchar*) cj_nest + ALIGN_SIZE(sizeof(TABLE_LIST)))); + cj_nest->nested_join->nest_type= JOIN_OP_NEST; + List *cjl= &cj_nest->nested_join->join_list; + cjl->empty(); + + /* Look for the left most node tbl of the right_op tree */ + for ( ; ; ) + { + TABLE_LIST *pair_tbl= 0; /* useful only for operands of natural joins */ + + List_iterator li(*jl); + tbl= li++; + + /* Expand name resolution context */ + Name_resolution_context *on_context; + if ((on_context= tbl->on_context)) + { + on_context->first_name_resolution_table= + left_op->first_leaf_for_name_resolution(); + } + + if (!(tbl->outer_join & JOIN_TYPE_RIGHT)) + { + pair_tbl= tbl; + tbl= li++; + } + if (tbl->nested_join && + tbl->nested_join->nest_type & JOIN_OP_NEST) + { + jl= &tbl->nested_join->join_list; + continue; + } + + /* Replace the tbl node in the tree for the newly created NJ node */ + cj_nest->outer_join= tbl->outer_join; + cj_nest->on_expr= tbl->on_expr; + cj_nest->embedding= tbl->embedding; + cj_nest->join_list= jl; + cj_nest->alias= (char*) "(nest_last_join)"; + li.replace(cj_nest); + + /* + If tbl is an operand of a natural join set properly the references + in the fields natural_join for both operands of the operation. + */ + if(tbl->embedding && tbl->embedding->is_natural_join) + { + if (!pair_tbl) + pair_tbl= li++; + pair_tbl->natural_join= cj_nest; + cj_nest->natural_join= pair_tbl; + } + break; + } + + /* Attach tbl as the right operand of NJ */ + if (unlikely(cjl->push_back(tbl, thd->mem_root))) + DBUG_RETURN(true); + tbl->outer_join= 0; + tbl->on_expr= 0; + tbl->straight= straight_fl; + tbl->natural_join= 0; + tbl->embedding= cj_nest; + tbl->join_list= cjl; + + /* Add left_op as the left operand of NJ */ + if (unlikely(cjl->push_back(left_op, thd->mem_root))) + DBUG_RETURN(true); + left_op->embedding= cj_nest; + left_op->join_list= cjl; + + /* + Mark right_op as a rebalanced nested join in order not to + create a new top level nested join node. + */ + right_op->nested_join->nest_type|= REBALANCED_NEST; + DBUG_RETURN(false); +} + + /** Convert a right join into equivalent left join. diff --git a/sql/sql_yacc.yy b/sql/sql_yacc.yy index 93ac55592bf9d..a0e9497f2177b 100644 --- a/sql/sql_yacc.yy +++ b/sql/sql_yacc.yy @@ -1428,9 +1428,9 @@ bool my_yyoverflow(short **a, YYSTYPE **b, ulong *yystacksize); %token IMPOSSIBLE_ACTION /* To avoid warning for yyerrlab1 */ -%left JOIN_SYM INNER_SYM STRAIGHT_JOIN CROSS LEFT RIGHT /* A dummy token to force the priority of table_ref production in a join. */ -%left TABLE_REF_PRIORITY +%left CONDITIONLESS_JOIN +%left JOIN_SYM INNER_SYM STRAIGHT_JOIN CROSS LEFT RIGHT ON_SYM USING %left SET_VAR %left OR_OR_SYM OR_SYM OR2_SYM %left XOR @@ -9607,9 +9607,9 @@ join_table_list: and are ignored. */ esc_table_ref: - table_ref { $$=$1; } - | '{' ident table_ref '}' { $$=$3; } - ; + table_ref { $$=$1; } + | '{' ident table_ref '}' { $$=$3; } + ; /* Equivalent to in the SQL:2003 standard. */ /* Warning - may return NULL in case of incomplete SELECT */ @@ -9622,23 +9622,24 @@ derived_table_list: ; /* - Notice that JOIN is a left-associative operation, and it must be parsed - as such, that is, the parser must process first the left join operand - then the right one. Such order of processing ensures that the parser - produces correct join trees which is essential for semantic analysis - and subsequent optimization phases. + Notice that JOIN can be a left-associative operator in one context and + a right-associative operator in another context (see the comment for + st_select_lex::add_cross_joined_table). */ join_table: /* INNER JOIN variants */ - /* - Use %prec to evaluate production 'table_ref' before 'normal_join' - so that [INNER | CROSS] JOIN is properly nested as other - left-associative joins. - */ - table_ref normal_join table_ref %prec TABLE_REF_PRIORITY - { MYSQL_YYABORT_UNLESS($1 && ($$=$3)); } - | table_ref STRAIGHT_JOIN table_factor - { MYSQL_YYABORT_UNLESS($1 && ($$=$3)); $3->straight=1; } + table_ref normal_join table_ref %prec CONDITIONLESS_JOIN + { + MYSQL_YYABORT_UNLESS($1 && ($$=$3)); + if (unlikely(Select->add_cross_joined_table($1, $3, false))) + MYSQL_YYABORT; + } + | table_ref STRAIGHT_JOIN table_ref %prec CONDITIONLESS_JOIN + { + MYSQL_YYABORT_UNLESS($1 && ($$=$3)); + if (unlikely(Select->add_cross_joined_table($1, $3, true))) + MYSQL_YYABORT; + } | table_ref normal_join table_ref ON { @@ -9651,10 +9652,10 @@ join_table: expr { add_join_on($3,$6); - Lex->pop_context(); + $3->on_context= Lex->pop_context(); Select->parsing_place= NO_MATTER; } - | table_ref STRAIGHT_JOIN table_factor + | table_ref STRAIGHT_JOIN table_ref ON { MYSQL_YYABORT_UNLESS($1 && $3); @@ -9667,7 +9668,7 @@ join_table: { $3->straight=1; add_join_on($3,$6); - Lex->pop_context(); + $3->on_context= Lex->pop_context(); Select->parsing_place= NO_MATTER; } | table_ref normal_join table_ref @@ -9696,7 +9697,7 @@ join_table: expr { add_join_on($5,$8); - Lex->pop_context(); + $5->on_context= Lex->pop_context(); $5->outer_join|=JOIN_TYPE_LEFT; $$=$5; Select->parsing_place= NO_MATTER; @@ -9735,7 +9736,7 @@ join_table: if (!($$= lex->current_select->convert_right_join())) MYSQL_YYABORT; add_join_on($$, $8); - Lex->pop_context(); + $1->on_context= Lex->pop_context(); Select->parsing_place= NO_MATTER; } | table_ref RIGHT opt_outer JOIN_SYM table_factor diff --git a/sql/table.h b/sql/table.h index c81ee68b0a6e5..66a51b9d4f568 100644 --- a/sql/table.h +++ b/sql/table.h @@ -46,6 +46,7 @@ struct TABLE_LIST; class ACL_internal_schema_access; class ACL_internal_table_access; class Field; +struct Name_resolution_context; /* Used to identify NESTED_JOIN structures within a join (applicable only to @@ -1618,6 +1619,7 @@ struct TABLE_LIST char *db, *alias, *table_name, *schema_table_name; char *option; /* Used by cache index */ Item *on_expr; /* Used with outer join */ + Name_resolution_context *on_context; /* For ON expressions */ Item *sj_on_expr; /* @@ -2332,9 +2334,31 @@ class Field_iterator_table_ref: public Field_iterator }; +#define JOIN_OP_NEST 1 +#define REBALANCED_NEST 2 + typedef struct st_nested_join { List join_list; /* list of elements in the nested join */ + /* + Currently the valid values for nest type are: + JOIN_OP_NEST - for nest created for JOIN operation used as an operand in + a join expression, contains 2 elements; + JOIN_OP_NEST | REBALANCED_NEST - nest created after tree re-balancing + in st_select_lex::add_cross_joined_table(), contains 1 element; + 0 - for all other nests. + Examples: + 1. SELECT * FROM t1 JOIN t2 LEFT JOIN t3 ON t2.a=t3.a; + Here the nest created for LEFT JOIN at first has nest_type==JOIN_OP_NEST. + After re-balancing in st_select_lex::add_cross_joined_table() this nest + has nest_type==JOIN_OP_NEST | REBALANCED_NEST. The nest for JOIN created + in st_select_lex::add_cross_joined_table() has nest_type== JOIN_OP_NEST. + 2. SELECT * FROM t1 JOIN (t2 LEFT JOIN t3 ON t2.a=t3.a) + Here the nest created for LEFT JOIN has nest_type==0, because it's not + an operand in a join expression. The nest created for JOIN has nest_type + set to JOIN_OP_NEST. + */ + uint nest_type; /* Bitmap of tables within this nested join (including those embedded within its children), including tables removed by table elimination. From 1a79a29c87eca4dffa393796b7dc4deb62eb0865 Mon Sep 17 00:00:00 2001 From: Oleksandr Byelkin Date: Fri, 12 Jul 2019 10:03:33 +0200 Subject: [PATCH 08/21] MDEV-17042: prepared statement does not return error with SQL_MODE STRICT_TRANS_TABLES. Use for parameters value conversion functions which issue warnings. --- mysql-test/r/ps.result | 63 +++++++++++++++++++++++++++ mysql-test/r/ps_innodb.result | 64 ++++++++++++++++++++++++++++ mysql-test/t/ps.test | 77 +++++++++++++++++++++++++++++++++ mysql-test/t/ps_innodb.test | 80 +++++++++++++++++++++++++++++++++++ sql/item.cc | 20 ++++----- 5 files changed, 293 insertions(+), 11 deletions(-) create mode 100644 mysql-test/r/ps_innodb.result create mode 100644 mysql-test/t/ps_innodb.test diff --git a/mysql-test/r/ps.result b/mysql-test/r/ps.result index 209d3d85108ea..1e909c8a53c34 100644 --- a/mysql-test/r/ps.result +++ b/mysql-test/r/ps.result @@ -4334,4 +4334,67 @@ LINE1 1 LINE2 2 LINE3 3 drop table t1; +# +# MDEV-17042: prepared statement does not return error with +# SQL_MODE STRICT_TRANS_TABLES. (Part 1) +# +set @save_sql_mode=@@sql_mode; +set sql_mode='STRICT_ALL_TABLES'; +CREATE TABLE t1 (id int, count int); +insert into t1 values (1,1),(0,2); +update t1 set count = count + 1 where id = '1bad'; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +execute stmt; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +deallocate prepare stmt; +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +execute stmt using @a; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +deallocate prepare stmt; +drop table t1; +CREATE TABLE t1 (id decimal(10,5), count int); +insert into t1 values (1,1),(0,2); +update t1 set count = count + 1 where id = '1bad'; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +execute stmt; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +deallocate prepare stmt; +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +execute stmt using @a; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +deallocate prepare stmt; +drop table t1; +CREATE TABLE t1 (id double, count int); +insert into t1 values (1,1),(0,2); +update t1 set count = count + 1 where id = '1bad'; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +execute stmt; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +deallocate prepare stmt; +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +execute stmt using @a; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +deallocate prepare stmt; +drop table t1; +CREATE TABLE t1 (id date, count int); +insert into t1 values ("2019-06-11",1),("2019-06-12",2); +update t1 set count = count + 1 where id = '1bad'; +ERROR 22007: Incorrect datetime value: '1bad' +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +execute stmt; +ERROR 22007: Incorrect datetime value: '1bad' +deallocate prepare stmt; +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +execute stmt using @a; +ERROR 22007: Incorrect datetime value: '1bad' +deallocate prepare stmt; +drop table t1; +set sql_mode=@save_sql_mode; # End of 5.5 tests diff --git a/mysql-test/r/ps_innodb.result b/mysql-test/r/ps_innodb.result new file mode 100644 index 0000000000000..c55f5e4900184 --- /dev/null +++ b/mysql-test/r/ps_innodb.result @@ -0,0 +1,64 @@ +# +# MDEV-17042: prepared statement does not return error with +# SQL_MODE STRICT_TRANS_TABLES. (Part 2) +# +set @save_sql_mode=@@sql_mode; +set sql_mode='STRICT_TRANS_TABLES'; +CREATE TABLE t1 (id int, count int) engine=innodb; +insert into t1 values (1,1),(0,2); +update t1 set count = count + 1 where id = '1bad'; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +execute stmt; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +deallocate prepare stmt; +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +execute stmt using @a; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +deallocate prepare stmt; +drop table t1; +CREATE TABLE t1 (id decimal(10,5), count int) engine=innodb; +insert into t1 values (1,1),(0,2); +update t1 set count = count + 1 where id = '1bad'; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +execute stmt; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +deallocate prepare stmt; +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +execute stmt using @a; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +deallocate prepare stmt; +drop table t1; +CREATE TABLE t1 (id double, count int) engine=innodb; +insert into t1 values (1,1),(0,2); +update t1 set count = count + 1 where id = '1bad'; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +execute stmt; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +deallocate prepare stmt; +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +execute stmt using @a; +ERROR 22007: Truncated incorrect DOUBLE value: '1bad' +deallocate prepare stmt; +drop table t1; +CREATE TABLE t1 (id date, count int) engine=innodb; +insert into t1 values ("2019-06-11",1),("2019-06-12",2); +update t1 set count = count + 1 where id = '1bad'; +ERROR 22007: Incorrect datetime value: '1bad' +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +execute stmt; +ERROR 22007: Incorrect datetime value: '1bad' +deallocate prepare stmt; +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +execute stmt using @a; +ERROR 22007: Incorrect datetime value: '1bad' +deallocate prepare stmt; +drop table t1; +set sql_mode=@save_sql_mode; +# End of 5.5 tests diff --git a/mysql-test/t/ps.test b/mysql-test/t/ps.test index ea67e1074f94e..b5d3416d1ca7f 100644 --- a/mysql-test/t/ps.test +++ b/mysql-test/t/ps.test @@ -3858,4 +3858,81 @@ FROM FROM t1 A, (SELECT @cnt := 0) C) T ) X; drop table t1; + +--echo # +--echo # MDEV-17042: prepared statement does not return error with +--echo # SQL_MODE STRICT_TRANS_TABLES. (Part 1) +--echo # + +set @save_sql_mode=@@sql_mode; +set sql_mode='STRICT_ALL_TABLES'; + +CREATE TABLE t1 (id int, count int); +insert into t1 values (1,1),(0,2); +--error ER_TRUNCATED_WRONG_VALUE +update t1 set count = count + 1 where id = '1bad'; + +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt; +deallocate prepare stmt; + +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt using @a; +deallocate prepare stmt; +drop table t1; + +CREATE TABLE t1 (id decimal(10,5), count int); +insert into t1 values (1,1),(0,2); +--error ER_TRUNCATED_WRONG_VALUE +update t1 set count = count + 1 where id = '1bad'; + +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt; +deallocate prepare stmt; + +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt using @a; +deallocate prepare stmt; +drop table t1; + +CREATE TABLE t1 (id double, count int); +insert into t1 values (1,1),(0,2); +--error ER_TRUNCATED_WRONG_VALUE +update t1 set count = count + 1 where id = '1bad'; + +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt; +deallocate prepare stmt; + +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt using @a; +deallocate prepare stmt; +drop table t1; + +CREATE TABLE t1 (id date, count int); +insert into t1 values ("2019-06-11",1),("2019-06-12",2); +--error ER_TRUNCATED_WRONG_VALUE +update t1 set count = count + 1 where id = '1bad'; + +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt; +deallocate prepare stmt; + +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt using @a; +deallocate prepare stmt; +drop table t1; +set sql_mode=@save_sql_mode; --echo # End of 5.5 tests diff --git a/mysql-test/t/ps_innodb.test b/mysql-test/t/ps_innodb.test new file mode 100644 index 0000000000000..6f56af352718d --- /dev/null +++ b/mysql-test/t/ps_innodb.test @@ -0,0 +1,80 @@ +--source include/have_innodb.inc + +--echo # +--echo # MDEV-17042: prepared statement does not return error with +--echo # SQL_MODE STRICT_TRANS_TABLES. (Part 2) +--echo # + +set @save_sql_mode=@@sql_mode; +set sql_mode='STRICT_TRANS_TABLES'; + +CREATE TABLE t1 (id int, count int) engine=innodb; +insert into t1 values (1,1),(0,2); +--error ER_TRUNCATED_WRONG_VALUE +update t1 set count = count + 1 where id = '1bad'; + +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt; +deallocate prepare stmt; + +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt using @a; +deallocate prepare stmt; +drop table t1; + +CREATE TABLE t1 (id decimal(10,5), count int) engine=innodb; +insert into t1 values (1,1),(0,2); +--error ER_TRUNCATED_WRONG_VALUE +update t1 set count = count + 1 where id = '1bad'; + +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt; +deallocate prepare stmt; + +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt using @a; +deallocate prepare stmt; +drop table t1; + +CREATE TABLE t1 (id double, count int) engine=innodb; +insert into t1 values (1,1),(0,2); +--error ER_TRUNCATED_WRONG_VALUE +update t1 set count = count + 1 where id = '1bad'; + +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt; +deallocate prepare stmt; + +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt using @a; +deallocate prepare stmt; +drop table t1; + +CREATE TABLE t1 (id date, count int) engine=innodb; +insert into t1 values ("2019-06-11",1),("2019-06-12",2); +--error ER_TRUNCATED_WRONG_VALUE +update t1 set count = count + 1 where id = '1bad'; + +prepare stmt from "update t1 set count = count + 1 where id = '1bad'"; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt; +deallocate prepare stmt; + +prepare stmt from 'update t1 set count = count + 1 where id = ?'; +set @a = '1bad'; +--error ER_TRUNCATED_WRONG_VALUE +execute stmt using @a; +deallocate prepare stmt; +drop table t1; +set sql_mode=@save_sql_mode; + +--echo # End of 5.5 tests diff --git a/sql/item.cc b/sql/item.cc index ffd899ef82016..14a703f462013 100644 --- a/sql/item.cc +++ b/sql/item.cc @@ -3668,10 +3668,10 @@ double Item_param::val_real() case STRING_VALUE: case LONG_DATA_VALUE: { - int dummy_err; - char *end_not_used; - return my_strntod(str_value.charset(), (char*) str_value.ptr(), - str_value.length(), &end_not_used, &dummy_err); + return double_from_string_with_check(str_value.charset(), + str_value.ptr(), + str_value.ptr() + + str_value.length()); } case TIME_VALUE: /* @@ -3706,11 +3706,10 @@ longlong Item_param::val_int() } case STRING_VALUE: case LONG_DATA_VALUE: - { - int dummy_err; - return my_strntoll(str_value.charset(), str_value.ptr(), - str_value.length(), 10, (char**) 0, &dummy_err); - } + return longlong_from_string_with_check(str_value.charset(), + str_value.ptr(), + str_value.ptr() + + str_value.length()); case TIME_VALUE: return (longlong) TIME_to_ulonglong(&value.time); case NULL_VALUE: @@ -3735,8 +3734,7 @@ my_decimal *Item_param::val_decimal(my_decimal *dec) return dec; case STRING_VALUE: case LONG_DATA_VALUE: - string2my_decimal(E_DEC_FATAL_ERROR, &str_value, dec); - return dec; + return val_decimal_from_string(dec); case TIME_VALUE: { longlong i= (longlong) TIME_to_ulonglong(&value.time); From cc86a0bd11a436dfe4df0622481c571f83b1680a Mon Sep 17 00:00:00 2001 From: Oleksandr Byelkin Date: Thu, 11 Jul 2019 14:50:50 +0200 Subject: [PATCH 09/21] MDEV-15572: view.test, server crash with --big-tables=1 Check that table is really opened before cleanup using handler. --- mysql-test/r/view.result | 15 +++++++++++++++ mysql-test/t/view.test | 20 ++++++++++++++++++++ sql/sql_insert.cc | 6 +++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/view.result b/mysql-test/r/view.result index 7fc3c48c3a0ae..98692a0b8eebc 100644 --- a/mysql-test/r/view.result +++ b/mysql-test/r/view.result @@ -5732,6 +5732,21 @@ t37, t38, t39, t40, t41, t42, t43, t44, t45, t46, t47, t48, t49, t50, t51, t52, t53, t54, t55, t56, t57, t58, t59,t60; drop view v60; +# +# MDEV-15572: view.test, server crash with --big-tables=1 +# +set @save_big_tables=@@big_tables; +set big_tables=ON; +CREATE TABLE t1 ( f1 int , f2 int , f3 int , f4 int); +CREATE TABLE t2 ( f1 int , f2 int , f3 int , f4 int); +CREATE VIEW v1 AS +SELECT t2.f1, t1.f2, t2.f3, t2.f4 FROM (t1 JOIN t2); +REPLACE INTO v1 (f1, f2, f3, f4) +SELECT f1, f2, f3, f4 FROM t1; +ERROR HY000: Can not modify more than one base table through a join view 'test.v1' +drop view v1; +drop table t1, t2; +set big_tables=@save_big_tables; # ----------------------------------------------------------------- # -- End of 5.5 tests. # ----------------------------------------------------------------- diff --git a/mysql-test/t/view.test b/mysql-test/t/view.test index 835f12957d438..0c4680eb5d2c8 100644 --- a/mysql-test/t/view.test +++ b/mysql-test/t/view.test @@ -5674,6 +5674,26 @@ t46, t47, t48, t49, t50, t51, t52, t53, t54, t55, t56, t57, t58, t59,t60; drop view v60; +--echo # +--echo # MDEV-15572: view.test, server crash with --big-tables=1 +--echo # + +set @save_big_tables=@@big_tables; +set big_tables=ON; +CREATE TABLE t1 ( f1 int , f2 int , f3 int , f4 int); +CREATE TABLE t2 ( f1 int , f2 int , f3 int , f4 int); + +CREATE VIEW v1 AS + SELECT t2.f1, t1.f2, t2.f3, t2.f4 FROM (t1 JOIN t2); + +--error ER_VIEW_MULTIUPDATE +REPLACE INTO v1 (f1, f2, f3, f4) + SELECT f1, f2, f3, f4 FROM t1; + +drop view v1; +drop table t1, t2; +set big_tables=@save_big_tables; + --echo # ----------------------------------------------------------------- --echo # -- End of 5.5 tests. --echo # ----------------------------------------------------------------- diff --git a/sql/sql_insert.cc b/sql/sql_insert.cc index 8f5ef0b531c69..ddf9bd155a911 100644 --- a/sql/sql_insert.cc +++ b/sql/sql_insert.cc @@ -3743,8 +3743,12 @@ void select_insert::abort_result_set() { example), no table will have been opened and therefore 'table' will be NULL. In that case, we still need to execute the rollback and the end of the function. + + If it fail due to inability to insert in multi-table view for example, + table will be assigned with view table structure, but that table will + not be opened really (it is dummy to check fields types & Co). */ - if (table) + if (table && table->file->get_table()) { bool changed, transactional_table; /* From f90040fd9ccb99cb4541d181a7052c37dc38decb Mon Sep 17 00:00:00 2001 From: Oleksandr Byelkin Date: Wed, 17 Jul 2019 12:31:45 +0200 Subject: [PATCH 10/21] MDEV-19429: Wrong query result with EXISTS and LIMIT 0 Check EXISTS LIMIT before rewriting. --- mysql-test/r/derived_view.result | 2 +- mysql-test/r/subselect.result | 41 ++++++++++++++++++++--- mysql-test/r/subselect_mat.result | 2 +- mysql-test/r/subselect_no_mat.result | 41 ++++++++++++++++++++--- mysql-test/r/subselect_no_opts.result | 41 ++++++++++++++++++++--- mysql-test/r/subselect_no_scache.result | 41 ++++++++++++++++++++--- mysql-test/r/subselect_no_semijoin.result | 41 ++++++++++++++++++++--- mysql-test/r/subselect_sj_mat.result | 2 +- mysql-test/r/view_alias.result | 10 +++--- mysql-test/t/subselect.test | 24 +++++++++++++ sql/item_subselect.cc | 21 ++++++++---- sql/sql_lex.cc | 5 ++- 12 files changed, 228 insertions(+), 43 deletions(-) diff --git a/mysql-test/r/derived_view.result b/mysql-test/r/derived_view.result index 412786c48e20e..73a9306c37bfb 100644 --- a/mysql-test/r/derived_view.result +++ b/mysql-test/r/derived_view.result @@ -1288,7 +1288,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 3 DEPENDENT SUBQUERY t3 ALL NULL NULL NULL NULL 2 100.00 Using where Warnings: Note 1276 Field or reference 't.a' of SELECT #3 was resolved in SELECT #1 -Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where <`test`.`t1`.`a`>(exists(select 28 from `test`.`t3` where ('j' < `test`.`t1`.`a`))) +Note 1003 select `test`.`t1`.`a` AS `a` from `test`.`t1` where <`test`.`t1`.`a`>(exists(select 28 from `test`.`t3` where ('j' < `test`.`t1`.`a`) limit 1)) SELECT * FROM (SELECT * FROM t1) AS t WHERE EXISTS (SELECT t2.a FROM t3 RIGHT JOIN t2 ON (t3.a = t2.a) WHERE t2.b < t.a); diff --git a/mysql-test/r/subselect.result b/mysql-test/r/subselect.result index 2999f4cfc8f38..0aa8a8192f04c 100644 --- a/mysql-test/r/subselect.result +++ b/mysql-test/r/subselect.result @@ -341,7 +341,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t7 eq_ref PRIMARY PRIMARY 4 test.t6.clinic_uq 1 100.00 Using index Warnings: Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <`test`.`t6`.`clinic_uq`>(exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`))) +Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <`test`.`t6`.`clinic_uq`>(exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`) limit 1)) select * from t1 where a= (select a from t2,t4 where t2.b=t4.b); ERROR 23000: Column 'a' in field list is ambiguous drop table t1,t2,t3; @@ -1887,7 +1887,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index Warnings: Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(<`test`.`tt`.`id`>(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null))))) +Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(<`test`.`tt`.`id`>(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null) limit 1)))) insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001'); create table t2 (id int not null, text varchar(20) not null default '', primary key (id)); insert into t2 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text1'), (12, 'text2'), (13, 'text3'), (14, 'text4'), (15, 'text5'), (16, 'text6'), (17, 'text7'), (18, 'text8'), (19, 'text9'), (20, 'text10'),(21, 'text1'), (22, 'text2'), (23, 'text3'), (24, 'text4'), (25, 'text5'), (26, 'text6'), (27, 'text7'), (28, 'text8'), (29, 'text9'), (30, 'text10'), (31, 'text1'), (32, 'text2'), (33, 'text3'), (34, 'text4'), (35, 'text5'), (36, 'text6'), (37, 'text7'), (38, 'text8'), (39, 'text9'), (40, 'text10'), (41, 'text1'), (42, 'text2'), (43, 'text3'), (44, 'text4'), (45, 'text5'), (46, 'text6'), (47, 'text7'), (48, 'text8'), (49, 'text9'), (50, 'text10'); @@ -2423,7 +2423,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where Warnings: Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <`test`.`up`.`a`>(exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`))) +Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <`test`.`up`.`a`>(exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`) limit 1)) drop table t1; CREATE TABLE t1 (t1_a int); INSERT INTO t1 VALUES (1); @@ -4432,7 +4432,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where Warnings: Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 -Note 1003 select 2 AS `2` from `test`.`t1` where <`test`.`t1`.`a`>(exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`))) +Note 1003 select 2 AS `2` from `test`.`t1` where <`test`.`t1`.`a`>(exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`) limit 1)) EXPLAIN EXTENDED SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION (SELECT 1 FROM t2 WHERE t1.a = t2.a)); @@ -4444,7 +4444,7 @@ NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Warnings: Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1 -Note 1003 select 2 AS `2` from `test`.`t1` where <`test`.`t1`.`a`>(exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)))) +Note 1003 select 2 AS `2` from `test`.`t1` where <`test`.`t1`.`a`>(exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) limit 1)) DROP TABLE t1,t2; create table t0(a int); insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); @@ -7187,4 +7187,35 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away drop table t1,t2; +# +# MDEV-19429: Wrong query result with EXISTS and LIMIT 0 +# +create table t10 (a int); +insert into t10 values (1),(2),(3); +create table t12 (a int); +insert into t12 values (1),(2),(3); +select * from t10 where exists (select * from t12 order by a limit 0); +a +explain select * from t10 where exists (select * from t12 order by a limit 0); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Zero limit +prepare stmt1 from "select * from t10 where exists (select * from t12 order by a limit ?)"; +set @l=1; +execute stmt1 using @l; +a +1 +2 +3 +set @l=2; +execute stmt1 using @l; +a +1 +2 +3 +set @l=0; +execute stmt1 using @l; +a +deallocate prepare stmt1; +drop table t10, t12; End of 5.5 tests diff --git a/mysql-test/r/subselect_mat.result b/mysql-test/r/subselect_mat.result index 0e10800331a6d..79d5fa37f162f 100644 --- a/mysql-test/r/subselect_mat.result +++ b/mysql-test/r/subselect_mat.result @@ -2219,7 +2219,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 3 MATERIALIZED t3 ALL NULL NULL NULL NULL 8 100.00 Warnings: Note 1276 Field or reference 'test.t1.f1' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where <`test`.`t1`.`f1`>(exists(select 1 from `test`.`t2` semi join (`test`.`t3`) join `test`.`t3` where ((`test`.`t3`.`i3` = `test`.`t2`.`i2`) and (`test`.`t1`.`f1` = `test`.`t3`.`f3`)))) +Note 1003 select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where <`test`.`t1`.`f1`>(exists(select 1 from `test`.`t2` semi join (`test`.`t3`) join `test`.`t3` where ((`test`.`t3`.`i3` = `test`.`t2`.`i2`) and (`test`.`t1`.`f1` = `test`.`t3`.`f3`)) limit 1)) # this checks the result set above set @save_optimizer_switch= @@optimizer_switch; set optimizer_switch= 'materialization=off,semijoin=off'; diff --git a/mysql-test/r/subselect_no_mat.result b/mysql-test/r/subselect_no_mat.result index 5c21e746b60a4..7662e416a6679 100644 --- a/mysql-test/r/subselect_no_mat.result +++ b/mysql-test/r/subselect_no_mat.result @@ -348,7 +348,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t7 eq_ref PRIMARY PRIMARY 4 test.t6.clinic_uq 1 100.00 Using index Warnings: Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <`test`.`t6`.`clinic_uq`>(exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`))) +Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <`test`.`t6`.`clinic_uq`>(exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`) limit 1)) select * from t1 where a= (select a from t2,t4 where t2.b=t4.b); ERROR 23000: Column 'a' in field list is ambiguous drop table t1,t2,t3; @@ -1894,7 +1894,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index Warnings: Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(<`test`.`tt`.`id`>(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null))))) +Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(<`test`.`tt`.`id`>(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null) limit 1)))) insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001'); create table t2 (id int not null, text varchar(20) not null default '', primary key (id)); insert into t2 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text1'), (12, 'text2'), (13, 'text3'), (14, 'text4'), (15, 'text5'), (16, 'text6'), (17, 'text7'), (18, 'text8'), (19, 'text9'), (20, 'text10'),(21, 'text1'), (22, 'text2'), (23, 'text3'), (24, 'text4'), (25, 'text5'), (26, 'text6'), (27, 'text7'), (28, 'text8'), (29, 'text9'), (30, 'text10'), (31, 'text1'), (32, 'text2'), (33, 'text3'), (34, 'text4'), (35, 'text5'), (36, 'text6'), (37, 'text7'), (38, 'text8'), (39, 'text9'), (40, 'text10'), (41, 'text1'), (42, 'text2'), (43, 'text3'), (44, 'text4'), (45, 'text5'), (46, 'text6'), (47, 'text7'), (48, 'text8'), (49, 'text9'), (50, 'text10'); @@ -2430,7 +2430,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where Warnings: Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <`test`.`up`.`a`>(exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`))) +Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <`test`.`up`.`a`>(exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`) limit 1)) drop table t1; CREATE TABLE t1 (t1_a int); INSERT INTO t1 VALUES (1); @@ -4436,7 +4436,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where Warnings: Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 -Note 1003 select 2 AS `2` from `test`.`t1` where <`test`.`t1`.`a`>(exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`))) +Note 1003 select 2 AS `2` from `test`.`t1` where <`test`.`t1`.`a`>(exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`) limit 1)) EXPLAIN EXTENDED SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION (SELECT 1 FROM t2 WHERE t1.a = t2.a)); @@ -4448,7 +4448,7 @@ NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Warnings: Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1 -Note 1003 select 2 AS `2` from `test`.`t1` where <`test`.`t1`.`a`>(exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)))) +Note 1003 select 2 AS `2` from `test`.`t1` where <`test`.`t1`.`a`>(exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) limit 1)) DROP TABLE t1,t2; create table t0(a int); insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); @@ -7184,6 +7184,37 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away drop table t1,t2; +# +# MDEV-19429: Wrong query result with EXISTS and LIMIT 0 +# +create table t10 (a int); +insert into t10 values (1),(2),(3); +create table t12 (a int); +insert into t12 values (1),(2),(3); +select * from t10 where exists (select * from t12 order by a limit 0); +a +explain select * from t10 where exists (select * from t12 order by a limit 0); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Zero limit +prepare stmt1 from "select * from t10 where exists (select * from t12 order by a limit ?)"; +set @l=1; +execute stmt1 using @l; +a +1 +2 +3 +set @l=2; +execute stmt1 using @l; +a +1 +2 +3 +set @l=0; +execute stmt1 using @l; +a +deallocate prepare stmt1; +drop table t10, t12; End of 5.5 tests set optimizer_switch=default; select @@optimizer_switch like '%materialization=on%'; diff --git a/mysql-test/r/subselect_no_opts.result b/mysql-test/r/subselect_no_opts.result index 8ab1f1bd3f96f..0242eac3eddc2 100644 --- a/mysql-test/r/subselect_no_opts.result +++ b/mysql-test/r/subselect_no_opts.result @@ -344,7 +344,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t7 eq_ref PRIMARY PRIMARY 4 test.t6.clinic_uq 1 100.00 Using index Warnings: Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`)) +Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`) limit 1) select * from t1 where a= (select a from t2,t4 where t2.b=t4.b); ERROR 23000: Column 'a' in field list is ambiguous drop table t1,t2,t3; @@ -1890,7 +1890,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index Warnings: Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null)))) +Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null) limit 1))) insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001'); create table t2 (id int not null, text varchar(20) not null default '', primary key (id)); insert into t2 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text1'), (12, 'text2'), (13, 'text3'), (14, 'text4'), (15, 'text5'), (16, 'text6'), (17, 'text7'), (18, 'text8'), (19, 'text9'), (20, 'text10'),(21, 'text1'), (22, 'text2'), (23, 'text3'), (24, 'text4'), (25, 'text5'), (26, 'text6'), (27, 'text7'), (28, 'text8'), (29, 'text9'), (30, 'text10'), (31, 'text1'), (32, 'text2'), (33, 'text3'), (34, 'text4'), (35, 'text5'), (36, 'text6'), (37, 'text7'), (38, 'text8'), (39, 'text9'), (40, 'text10'), (41, 'text1'), (42, 'text2'), (43, 'text3'), (44, 'text4'), (45, 'text5'), (46, 'text6'), (47, 'text7'), (48, 'text8'), (49, 'text9'), (50, 'text10'); @@ -2426,7 +2426,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where Warnings: Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`)) +Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`) limit 1) drop table t1; CREATE TABLE t1 (t1_a int); INSERT INTO t1 VALUES (1); @@ -4432,7 +4432,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where Warnings: Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 -Note 1003 select 2 AS `2` from `test`.`t1` where exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) +Note 1003 select 2 AS `2` from `test`.`t1` where exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`) limit 1) EXPLAIN EXTENDED SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION (SELECT 1 FROM t2 WHERE t1.a = t2.a)); @@ -4444,7 +4444,7 @@ NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Warnings: Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1 -Note 1003 select 2 AS `2` from `test`.`t1` where exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`))) +Note 1003 select 2 AS `2` from `test`.`t1` where exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) limit 1) DROP TABLE t1,t2; create table t0(a int); insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); @@ -7182,5 +7182,36 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away drop table t1,t2; +# +# MDEV-19429: Wrong query result with EXISTS and LIMIT 0 +# +create table t10 (a int); +insert into t10 values (1),(2),(3); +create table t12 (a int); +insert into t12 values (1),(2),(3); +select * from t10 where exists (select * from t12 order by a limit 0); +a +explain select * from t10 where exists (select * from t12 order by a limit 0); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Zero limit +prepare stmt1 from "select * from t10 where exists (select * from t12 order by a limit ?)"; +set @l=1; +execute stmt1 using @l; +a +1 +2 +3 +set @l=2; +execute stmt1 using @l; +a +1 +2 +3 +set @l=0; +execute stmt1 using @l; +a +deallocate prepare stmt1; +drop table t10, t12; End of 5.5 tests set @optimizer_switch_for_subselect_test=null; diff --git a/mysql-test/r/subselect_no_scache.result b/mysql-test/r/subselect_no_scache.result index f0afa7ef19c9d..15f29c55d104b 100644 --- a/mysql-test/r/subselect_no_scache.result +++ b/mysql-test/r/subselect_no_scache.result @@ -347,7 +347,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t7 eq_ref PRIMARY PRIMARY 4 test.t6.clinic_uq 1 100.00 Using index Warnings: Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`)) +Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`) limit 1) select * from t1 where a= (select a from t2,t4 where t2.b=t4.b); ERROR 23000: Column 'a' in field list is ambiguous drop table t1,t2,t3; @@ -1893,7 +1893,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index Warnings: Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null)))) +Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null) limit 1))) insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001'); create table t2 (id int not null, text varchar(20) not null default '', primary key (id)); insert into t2 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text1'), (12, 'text2'), (13, 'text3'), (14, 'text4'), (15, 'text5'), (16, 'text6'), (17, 'text7'), (18, 'text8'), (19, 'text9'), (20, 'text10'),(21, 'text1'), (22, 'text2'), (23, 'text3'), (24, 'text4'), (25, 'text5'), (26, 'text6'), (27, 'text7'), (28, 'text8'), (29, 'text9'), (30, 'text10'), (31, 'text1'), (32, 'text2'), (33, 'text3'), (34, 'text4'), (35, 'text5'), (36, 'text6'), (37, 'text7'), (38, 'text8'), (39, 'text9'), (40, 'text10'), (41, 'text1'), (42, 'text2'), (43, 'text3'), (44, 'text4'), (45, 'text5'), (46, 'text6'), (47, 'text7'), (48, 'text8'), (49, 'text9'), (50, 'text10'); @@ -2429,7 +2429,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where Warnings: Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`)) +Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`) limit 1) drop table t1; CREATE TABLE t1 (t1_a int); INSERT INTO t1 VALUES (1); @@ -4438,7 +4438,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where Warnings: Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 -Note 1003 select 2 AS `2` from `test`.`t1` where exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) +Note 1003 select 2 AS `2` from `test`.`t1` where exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`) limit 1) EXPLAIN EXTENDED SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION (SELECT 1 FROM t2 WHERE t1.a = t2.a)); @@ -4450,7 +4450,7 @@ NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Warnings: Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1 -Note 1003 select 2 AS `2` from `test`.`t1` where exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`))) +Note 1003 select 2 AS `2` from `test`.`t1` where exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) limit 1) DROP TABLE t1,t2; create table t0(a int); insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); @@ -7193,6 +7193,37 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away drop table t1,t2; +# +# MDEV-19429: Wrong query result with EXISTS and LIMIT 0 +# +create table t10 (a int); +insert into t10 values (1),(2),(3); +create table t12 (a int); +insert into t12 values (1),(2),(3); +select * from t10 where exists (select * from t12 order by a limit 0); +a +explain select * from t10 where exists (select * from t12 order by a limit 0); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Zero limit +prepare stmt1 from "select * from t10 where exists (select * from t12 order by a limit ?)"; +set @l=1; +execute stmt1 using @l; +a +1 +2 +3 +set @l=2; +execute stmt1 using @l; +a +1 +2 +3 +set @l=0; +execute stmt1 using @l; +a +deallocate prepare stmt1; +drop table t10, t12; End of 5.5 tests set optimizer_switch=default; select @@optimizer_switch like '%subquery_cache=on%'; diff --git a/mysql-test/r/subselect_no_semijoin.result b/mysql-test/r/subselect_no_semijoin.result index 0d5fbc3498a0e..c75f335c6eff8 100644 --- a/mysql-test/r/subselect_no_semijoin.result +++ b/mysql-test/r/subselect_no_semijoin.result @@ -344,7 +344,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t7 eq_ref PRIMARY PRIMARY 4 test.t6.clinic_uq 1 100.00 Using index Warnings: Note 1276 Field or reference 'test.t6.clinic_uq' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <`test`.`t6`.`clinic_uq`>(exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`))) +Note 1003 select `test`.`t6`.`patient_uq` AS `patient_uq`,`test`.`t6`.`clinic_uq` AS `clinic_uq` from `test`.`t6` where <`test`.`t6`.`clinic_uq`>(exists(select 1 from `test`.`t7` where (`test`.`t7`.`uq` = `test`.`t6`.`clinic_uq`) limit 1)) select * from t1 where a= (select a from t2,t4 where t2.b=t4.b); ERROR 23000: Column 'a' in field list is ambiguous drop table t1,t2,t3; @@ -1890,7 +1890,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 eq_ref PRIMARY PRIMARY 4 test.tt.id 1 100.00 Using where; Using index Warnings: Note 1276 Field or reference 'test.tt.id' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(<`test`.`tt`.`id`>(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null))))) +Note 1003 select `test`.`tt`.`id` AS `id`,`test`.`tt`.`text` AS `text` from `test`.`t1` `tt` where (not(<`test`.`tt`.`id`>(exists(select `test`.`t1`.`id` from `test`.`t1` where ((`test`.`t1`.`id` < 8) and (`test`.`t1`.`id` = `test`.`tt`.`id`)) having (`test`.`t1`.`id` is not null) limit 1)))) insert into t1 (id, text) values (1000, 'text1000'), (1001, 'text1001'); create table t2 (id int not null, text varchar(20) not null default '', primary key (id)); insert into t2 (id, text) values (1, 'text1'), (2, 'text2'), (3, 'text3'), (4, 'text4'), (5, 'text5'), (6, 'text6'), (7, 'text7'), (8, 'text8'), (9, 'text9'), (10, 'text10'), (11, 'text1'), (12, 'text2'), (13, 'text3'), (14, 'text4'), (15, 'text5'), (16, 'text6'), (17, 'text7'), (18, 'text8'), (19, 'text9'), (20, 'text10'),(21, 'text1'), (22, 'text2'), (23, 'text3'), (24, 'text4'), (25, 'text5'), (26, 'text6'), (27, 'text7'), (28, 'text8'), (29, 'text9'), (30, 'text10'), (31, 'text1'), (32, 'text2'), (33, 'text3'), (34, 'text4'), (35, 'text5'), (36, 'text6'), (37, 'text7'), (38, 'text8'), (39, 'text9'), (40, 'text10'), (41, 'text1'), (42, 'text2'), (43, 'text3'), (44, 'text4'), (45, 'text5'), (46, 'text6'), (47, 'text7'), (48, 'text8'), (49, 'text9'), (50, 'text10'); @@ -2426,7 +2426,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t1 ALL NULL NULL NULL NULL 2 100.00 Using where Warnings: Note 1276 Field or reference 'test.up.a' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <`test`.`up`.`a`>(exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`))) +Note 1003 select `test`.`up`.`a` AS `a`,`test`.`up`.`b` AS `b` from `test`.`t1` `up` where <`test`.`up`.`a`>(exists(select 1 from `test`.`t1` where (`test`.`t1`.`a` = `test`.`up`.`a`) limit 1)) drop table t1; CREATE TABLE t1 (t1_a int); INSERT INTO t1 VALUES (1); @@ -4432,7 +4432,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 2 DEPENDENT SUBQUERY t2 ALL NULL NULL NULL NULL 2 100.00 Using where Warnings: Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 -Note 1003 select 2 AS `2` from `test`.`t1` where <`test`.`t1`.`a`>(exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`))) +Note 1003 select 2 AS `2` from `test`.`t1` where <`test`.`t1`.`a`>(exists(select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`) limit 1)) EXPLAIN EXTENDED SELECT 2 FROM t1 WHERE EXISTS ((SELECT 1 FROM t2 WHERE t1.a=t2.a) UNION (SELECT 1 FROM t2 WHERE t1.a = t2.a)); @@ -4444,7 +4444,7 @@ NULL UNION RESULT ALL NULL NULL NULL NULL NULL NULL Warnings: Note 1276 Field or reference 'test.t1.a' of SELECT #2 was resolved in SELECT #1 Note 1276 Field or reference 'test.t1.a' of SELECT #3 was resolved in SELECT #1 -Note 1003 select 2 AS `2` from `test`.`t1` where <`test`.`t1`.`a`>(exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)))) +Note 1003 select 2 AS `2` from `test`.`t1` where <`test`.`t1`.`a`>(exists((select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) union (select 1 from `test`.`t2` where (`test`.`t1`.`a` = `test`.`t2`.`a`)) limit 1)) DROP TABLE t1,t2; create table t0(a int); insert into t0 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9); @@ -7182,6 +7182,37 @@ id select_type table type possible_keys key key_len ref rows Extra 1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE 2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Select tables optimized away drop table t1,t2; +# +# MDEV-19429: Wrong query result with EXISTS and LIMIT 0 +# +create table t10 (a int); +insert into t10 values (1),(2),(3); +create table t12 (a int); +insert into t12 values (1),(2),(3); +select * from t10 where exists (select * from t12 order by a limit 0); +a +explain select * from t10 where exists (select * from t12 order by a limit 0); +id select_type table type possible_keys key key_len ref rows Extra +1 PRIMARY NULL NULL NULL NULL NULL NULL NULL Impossible WHERE +2 SUBQUERY NULL NULL NULL NULL NULL NULL NULL Zero limit +prepare stmt1 from "select * from t10 where exists (select * from t12 order by a limit ?)"; +set @l=1; +execute stmt1 using @l; +a +1 +2 +3 +set @l=2; +execute stmt1 using @l; +a +1 +2 +3 +set @l=0; +execute stmt1 using @l; +a +deallocate prepare stmt1; +drop table t10, t12; End of 5.5 tests set @optimizer_switch_for_subselect_test=null; set @join_cache_level_for_subselect_test=NULL; diff --git a/mysql-test/r/subselect_sj_mat.result b/mysql-test/r/subselect_sj_mat.result index cb4b63047e528..1e123f17cd31b 100644 --- a/mysql-test/r/subselect_sj_mat.result +++ b/mysql-test/r/subselect_sj_mat.result @@ -2259,7 +2259,7 @@ id select_type table type possible_keys key key_len ref rows filtered Extra 3 MATERIALIZED t3 ALL NULL NULL NULL NULL 8 100.00 Warnings: Note 1276 Field or reference 'test.t1.f1' of SELECT #2 was resolved in SELECT #1 -Note 1003 select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where <`test`.`t1`.`f1`>(exists(select 1 from `test`.`t2` semi join (`test`.`t3`) join `test`.`t3` where ((`test`.`t3`.`i3` = `test`.`t2`.`i2`) and (`test`.`t1`.`f1` = `test`.`t3`.`f3`)))) +Note 1003 select `test`.`t1`.`f1` AS `f1` from `test`.`t1` where <`test`.`t1`.`f1`>(exists(select 1 from `test`.`t2` semi join (`test`.`t3`) join `test`.`t3` where ((`test`.`t3`.`i3` = `test`.`t2`.`i2`) and (`test`.`t1`.`f1` = `test`.`t3`.`f3`)) limit 1)) # this checks the result set above set @save_optimizer_switch= @@optimizer_switch; set optimizer_switch= 'materialization=off,semijoin=off'; diff --git a/mysql-test/r/view_alias.result b/mysql-test/r/view_alias.result index 72c4bf29f253f..fc05e4f227605 100644 --- a/mysql-test/r/view_alias.result +++ b/mysql-test/r/view_alias.result @@ -90,22 +90,22 @@ CREATE TABLE t2 LIKE t1; # Test alias in subquery CREATE VIEW v1 AS SELECT a FROM t1 WHERE EXISTS (SELECT 1 FROM t2 AS b WHERE b.a = 0); DROP VIEW v1; -CREATE VIEW v1 AS select `test`.`t1`.`a` AS `a` from `test`.`t1` where exists(select 1 from `test`.`t2` `b` where (`b`.`a` = 0)); +CREATE VIEW v1 AS select `test`.`t1`.`a` AS `a` from `test`.`t1` where exists(select 1 from `test`.`t2` `b` where (`b`.`a` = 0) limit 1); DROP VIEW v1; # Test column alias in subquery CREATE VIEW v1 AS SELECT a FROM t1 WHERE EXISTS (SELECT a AS alias FROM t1 GROUP BY alias); SHOW CREATE VIEW v1; View Create View character_set_client collation_connection -v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1` where exists(select `t1`.`a` AS `alias` from `t1` group by `t1`.`a`) latin1 latin1_swedish_ci +v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1` where exists(select `t1`.`a` AS `alias` from `t1` group by `t1`.`a` limit 1) latin1 latin1_swedish_ci DROP VIEW v1; -CREATE VIEW v1 AS select `test`.`t1`.`a` AS `a` from `test`.`t1` where exists(select `test`.`t1`.`a` AS `alias` from `test`.`t1` group by `test`.`t1`.`a`); +CREATE VIEW v1 AS select `test`.`t1`.`a` AS `a` from `test`.`t1` where exists(select `test`.`t1`.`a` AS `alias` from `test`.`t1` group by `test`.`t1`.`a` limit 1); DROP VIEW v1; # Alias as the expression column name. CREATE VIEW v1 AS SELECT a FROM t1 WHERE EXISTS (SELECT ' a ' AS alias FROM t1 GROUP BY alias); SHOW CREATE VIEW v1; View Create View character_set_client collation_connection -v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1` where exists(select ' a ' AS `alias` from `t1` group by ' a ') latin1 latin1_swedish_ci +v1 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `v1` AS select `t1`.`a` AS `a` from `t1` where exists(select ' a ' AS `alias` from `t1` group by ' a ' limit 1) latin1 latin1_swedish_ci DROP VIEW v1; -CREATE VIEW v1 AS select `test`.`t1`.`a` AS `a` from `test`.`t1` where exists(select ' a ' AS `alias` from `test`.`t1` group by ' a '); +CREATE VIEW v1 AS select `test`.`t1`.`a` AS `a` from `test`.`t1` where exists(select ' a ' AS `alias` from `test`.`t1` group by ' a ' limit 1); DROP VIEW v1; DROP TABLE t1, t2; diff --git a/mysql-test/t/subselect.test b/mysql-test/t/subselect.test index 4e35032a789d6..387935c2d5d1d 100644 --- a/mysql-test/t/subselect.test +++ b/mysql-test/t/subselect.test @@ -6075,4 +6075,28 @@ explain select * from t1 where (select max(b) from t2) = 10 and t1.a > 3; drop table t1,t2; + +--echo # +--echo # MDEV-19429: Wrong query result with EXISTS and LIMIT 0 +--echo # +create table t10 (a int); +insert into t10 values (1),(2),(3); +create table t12 (a int); +insert into t12 values (1),(2),(3); +select * from t10 where exists (select * from t12 order by a limit 0); +explain select * from t10 where exists (select * from t12 order by a limit 0); + +prepare stmt1 from "select * from t10 where exists (select * from t12 order by a limit ?)"; + +set @l=1; +execute stmt1 using @l; +set @l=2; +execute stmt1 using @l; +set @l=0; +execute stmt1 using @l; + +deallocate prepare stmt1; + +drop table t10, t12; + --echo End of 5.5 tests diff --git a/sql/item_subselect.cc b/sql/item_subselect.cc index afc42dc08d5e0..9d6fe5ac5aba0 100644 --- a/sql/item_subselect.cc +++ b/sql/item_subselect.cc @@ -1432,13 +1432,20 @@ void Item_exists_subselect::fix_length_and_dec() { DBUG_ENTER("Item_exists_subselect::fix_length_and_dec"); init_length_and_dec(); - /* - We need only 1 row to determine existence (i.e. any EXISTS that is not - an IN always requires LIMIT 1) - */ - thd->change_item_tree(&unit->global_parameters->select_limit, - new Item_int((int32) 1)); - DBUG_PRINT("info", ("Set limit to 1")); + // If limit is not set or it is constant more than 1 + if (!unit->global_parameters->select_limit || + (unit->global_parameters->select_limit->basic_const_item() && + unit->global_parameters->select_limit->val_int() > 1)) + { + /* + We need only 1 row to determine existence (i.e. any EXISTS that is not + an IN always requires LIMIT 1) + */ + thd->change_item_tree(&unit->global_parameters->select_limit, + new Item_int((int32) 1)); + unit->global_parameters->explicit_limit= 1; // we set the limit + DBUG_PRINT("info", ("Set limit to 1")); + } DBUG_VOID_RETURN; } diff --git a/sql/sql_lex.cc b/sql/sql_lex.cc index 28f56282bad14..fe4dcfd1524d1 100644 --- a/sql/sql_lex.cc +++ b/sql/sql_lex.cc @@ -2456,14 +2456,13 @@ void st_select_lex::print_limit(THD *thd, if (item && unit->global_parameters == this) { Item_subselect::subs_type subs_type= item->substype(); - if (subs_type == Item_subselect::EXISTS_SUBS || - subs_type == Item_subselect::IN_SUBS || + if (subs_type == Item_subselect::IN_SUBS || subs_type == Item_subselect::ALL_SUBS) { return; } } - if (explicit_limit) + if (explicit_limit && select_limit) { str->append(STRING_WITH_LEN(" limit ")); if (offset_limit) From 82563c5fc0a40d64d8e8e9de2bf6f904fa6c0dc6 Mon Sep 17 00:00:00 2001 From: Sergei Golubchik Date: Sun, 21 Jul 2019 12:09:17 +0200 Subject: [PATCH 11/21] MDEV-20110 don't try to load client plugins with invalid names reported by lixtelnis --- mysql-test/r/connect_debug.result | 5 +++++ mysql-test/t/connect_debug.test | 11 +++++++++++ sql-common/client_plugin.c | 8 +++++++- sql/sql_acl.cc | 1 + 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/mysql-test/r/connect_debug.result b/mysql-test/r/connect_debug.result index 0452b238db9be..3151166a0e6cc 100644 --- a/mysql-test/r/connect_debug.result +++ b/mysql-test/r/connect_debug.result @@ -3,3 +3,8 @@ set global debug_dbug='+d,auth_disconnect'; create user 'bad' identified by 'worse'; set global debug_dbug=@old_dbug; drop user bad; +set global debug_dbug='+d,auth_invalid_plugin'; +create user 'bad' identified by 'worse'; +ERROR 2059 (HY000): Authentication plugin 'foo/bar' cannot be loaded: invalid plugin name +set global debug_dbug=@old_dbug; +drop user bad; diff --git a/mysql-test/t/connect_debug.test b/mysql-test/t/connect_debug.test index 299b605b2cd08..7a2f2872b795f 100644 --- a/mysql-test/t/connect_debug.test +++ b/mysql-test/t/connect_debug.test @@ -1,3 +1,4 @@ +source include/not_embedded.inc; source include/have_debug.inc; set @old_dbug=@@global.debug_dbug; @@ -10,3 +11,13 @@ create user 'bad' identified by 'worse'; --exec $MYSQL --default-auth=mysql_old_password --user=bad --password=worse set global debug_dbug=@old_dbug; drop user bad; + +# +# malicious server, invalid plugin name +# +set global debug_dbug='+d,auth_invalid_plugin'; +create user 'bad' identified by 'worse'; +--error 1 +--exec $MYSQL --default-auth=mysql_old_password --user=bad --password=worse 2>&1 +set global debug_dbug=@old_dbug; +drop user bad; diff --git a/sql-common/client_plugin.c b/sql-common/client_plugin.c index f5e1ffbbf5c40..812cefe03f585 100644 --- a/sql-common/client_plugin.c +++ b/sql-common/client_plugin.c @@ -362,7 +362,13 @@ mysql_load_plugin_v(MYSQL *mysql, const char *name, int type, mysql->options.extension && mysql->options.extension->plugin_dir ? mysql->options.extension->plugin_dir : PLUGINDIR, "/", name, SO_EXT, NullS); - + + if (strpbrk(name, "()[]!@#$%^&/*;.,'?")) + { + errmsg= "invalid plugin name"; + goto err; + } + DBUG_PRINT ("info", ("dlopeninig %s", dlpath)); /* Open new dll handle */ if (!(dlhandle= dlopen(dlpath, RTLD_NOW))) diff --git a/sql/sql_acl.cc b/sql/sql_acl.cc index ef5fed1fc1982..8670f5b390d0c 100644 --- a/sql/sql_acl.cc +++ b/sql/sql_acl.cc @@ -8256,6 +8256,7 @@ static bool send_plugin_request_packet(MPVIO_EXT *mpvio, ((st_mysql_auth *) (plugin_decl(mpvio->plugin)->info))->client_auth_plugin; DBUG_EXECUTE_IF("auth_disconnect", { vio_close(net->vio); DBUG_RETURN(1); }); + DBUG_EXECUTE_IF("auth_invalid_plugin", client_auth_plugin="foo/bar"; ); DBUG_ASSERT(client_auth_plugin); /* From 07ba5560da805c766da68811692272dd9eebdaf7 Mon Sep 17 00:00:00 2001 From: Thirunarayanan Balathandayuthapani Date: Fri, 26 Aug 2016 11:00:44 +0530 Subject: [PATCH 12/21] Bug #20989615 INNODB AUTO_INCREMENT PRODUCES SAME VALUE TWICE Problem: ======= Autoincrement value gives duplicate values because of the following reasons. (1) In InnoDB handler function, current autoincrement value is not changed based on newly set auto_increment_increment or auto_increment_offset variable. (2) Handler function does the rounding logic and changes the current autoincrement value and InnoDB doesn't aware of the change in current autoincrement value. Solution: ======== Fix the problem(1), InnoDB always respect the auto_increment_increment and auto_increment_offset value in case of current autoincrement value. By fixing the problem (2), handler layer won't change any current autoincrement value. Reviewed-by: Jimmy Yang RB: 13748 --- .../suite/innodb/r/autoinc_debug.result | 95 +++++++++++++++++++ mysql-test/suite/innodb/t/autoinc_debug.test | 91 ++++++++++++++++++ storage/innobase/handler/ha_innodb.cc | 32 +++++++ storage/innobase/row/row0mysql.c | 2 + storage/xtradb/handler/ha_innodb.cc | 31 ++++++ storage/xtradb/row/row0mysql.c | 2 + 6 files changed, 253 insertions(+) create mode 100644 mysql-test/suite/innodb/r/autoinc_debug.result create mode 100644 mysql-test/suite/innodb/t/autoinc_debug.test diff --git a/mysql-test/suite/innodb/r/autoinc_debug.result b/mysql-test/suite/innodb/r/autoinc_debug.result new file mode 100644 index 0000000000000..505e9f4dfc993 --- /dev/null +++ b/mysql-test/suite/innodb/r/autoinc_debug.result @@ -0,0 +1,95 @@ +CREATE TABLE t1 (id INT AUTO_INCREMENT PRIMARY KEY)ENGINE=INNODB; +# SETTING auto_increment_increment IN CONNECTION DEFAULT +SET AUTO_INCREMENT_INCREMENT = 1; +INSERT INTO t1 VALUES(NULL); +SELECT * FROM t1; +id +1 +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 +# SETTING auto_increment_increment IN CONNECTION1 +SET AUTO_INCREMENT_INCREMENT = 2; +SET DEBUG_SYNC= 'ib_after_row_insert SIGNAL opened WAIT_FOR flushed1'; +INSERT INTO t1 VALUES(NULL); +SET AUTO_INCREMENT_INCREMENT = 2; +SET DEBUG_SYNC= 'now WAIT_FOR opened'; +SET DEBUG_SYNC= 'ib_after_row_insert_step SIGNAL flushed1 WAIT_FOR opened1'; +insert into t1 values(NULL); +# CONNECTION default +SELECT * FROM t1; +id +1 +3 +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1 +SET DEBUG_SYNC= 'now SIGNAL opened1'; +# CONNECTION con1 +SELECT * FROM t1; +id +1 +3 +5 +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1 +DROP TABLE t1; +CREATE TABLE t1(id INT AUTO_INCREMENT PRIMARY KEY)ENGINE=INNODB; +# SETTING auto_increment_increment IN CONNECTION DEFAULT +SET AUTO_INCREMENT_INCREMENT = 1; +INSERT INTO t1 VALUES(NULL); +SELECT * FROM t1; +id +1 +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1 +SET DEBUG_SYNC = 'now SIGNAL flushed'; +# SETTING auto_increment_increment in connection1 +SET AUTO_INCREMENT_INCREMENT = 2; +SET DEBUG_SYNC= 'now WAIT_FOR flushed'; +SET DEBUG_SYNC= 'ib_after_row_insert SIGNAL opened WAIT_FOR flushed1'; +INSERT INTO t1 values(NULL); +# CONNECTION DEFAULT +SET DEBUG_SYNC= 'now WAIT_FOR opened'; +SET DEBUG_SYNC= 'ib_after_row_insert_step SIGNAL flushed1 WAIT_FOR opened1'; +INSERT INTO t1 VALUES(NULL); +# CONNECTION con1 +SELECT * FROM t1; +id +1 +3 +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 +SET DEBUG_SYNC= 'now SIGNAL opened1'; +# CONNECTION default +SELECT * FROM t1; +id +1 +3 +5 +SHOW CREATE TABLE t1; +Table Create Table +t1 CREATE TABLE `t1` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + PRIMARY KEY (`id`) +) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 +SET DEBUG_SYNC= 'RESET'; +DROP TABLE t1; diff --git a/mysql-test/suite/innodb/t/autoinc_debug.test b/mysql-test/suite/innodb/t/autoinc_debug.test new file mode 100644 index 0000000000000..c3f9dbffecfc5 --- /dev/null +++ b/mysql-test/suite/innodb/t/autoinc_debug.test @@ -0,0 +1,91 @@ +--source include/have_innodb.inc +--source include/have_debug.inc +--source include/have_debug_sync.inc +--source include/not_embedded.inc + +# Two parallel connection with autoinc column after restart. + +CREATE TABLE t1 (id INT AUTO_INCREMENT PRIMARY KEY)ENGINE=INNODB; + +--echo # SETTING auto_increment_increment IN CONNECTION DEFAULT +SET AUTO_INCREMENT_INCREMENT = 1; +INSERT INTO t1 VALUES(NULL); +SELECT * FROM t1; +SHOW CREATE TABLE t1; + +--source include/restart_mysqld.inc + +--echo # SETTING auto_increment_increment IN CONNECTION1 +SET AUTO_INCREMENT_INCREMENT = 2; + +SET DEBUG_SYNC= 'ib_after_row_insert SIGNAL opened WAIT_FOR flushed1'; + +SEND INSERT INTO t1 VALUES(NULL); + +connect(con1, localhost, root,,); +SET AUTO_INCREMENT_INCREMENT = 2; +SET DEBUG_SYNC= 'now WAIT_FOR opened'; +SET DEBUG_SYNC= 'ib_after_row_insert_step SIGNAL flushed1 WAIT_FOR opened1'; +send insert into t1 values(NULL); + +--echo # CONNECTION default +connection default; +reap; +SELECT * FROM t1; +SHOW CREATE TABLE t1; +SET DEBUG_SYNC= 'now SIGNAL opened1'; + +--echo # CONNECTION con1 +connection con1; +reap; +SELECT * FROM t1; +SHOW CREATE TABLE t1; +connection default; +disconnect con1; + +DROP TABLE t1; + +# Two parallel connection with autoinc column without restart. + +CREATE TABLE t1(id INT AUTO_INCREMENT PRIMARY KEY)ENGINE=INNODB; + +--echo # SETTING auto_increment_increment IN CONNECTION DEFAULT +SET AUTO_INCREMENT_INCREMENT = 1; +INSERT INTO t1 VALUES(NULL); +SELECT * FROM t1; +SHOW CREATE TABLE t1; +SET DEBUG_SYNC = 'now SIGNAL flushed'; + +connect(con1, localhost, root,,); + +--echo # SETTING auto_increment_increment in connection1 +SET AUTO_INCREMENT_INCREMENT = 2; + +SET DEBUG_SYNC= 'now WAIT_FOR flushed'; +SET DEBUG_SYNC= 'ib_after_row_insert SIGNAL opened WAIT_FOR flushed1'; + +send INSERT INTO t1 values(NULL); + +--echo # CONNECTION DEFAULT +connection default; + +SET DEBUG_SYNC= 'now WAIT_FOR opened'; +SET DEBUG_SYNC= 'ib_after_row_insert_step SIGNAL flushed1 WAIT_FOR opened1'; + +send INSERT INTO t1 VALUES(NULL); + +--echo # CONNECTION con1 +connection con1; +reap; +SELECT * FROM t1; +SHOW CREATE TABLE t1; +SET DEBUG_SYNC= 'now SIGNAL opened1'; + +--echo # CONNECTION default +connection default; +reap; +SELECT * FROM t1; +SHOW CREATE TABLE t1; +disconnect con1; +SET DEBUG_SYNC= 'RESET'; +DROP TABLE t1; diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc index 287eae5681c65..8b48552eed4f4 100644 --- a/storage/innobase/handler/ha_innodb.cc +++ b/storage/innobase/handler/ha_innodb.cc @@ -5350,6 +5350,7 @@ ha_innobase::write_row( innodb_srv_conc_enter_innodb(prebuilt->trx); error = row_insert_for_mysql((byte*) record, prebuilt); + DEBUG_SYNC(user_thd, "ib_after_row_insert"); /* Handle duplicate key errors */ if (auto_inc_used) { @@ -10461,6 +10462,37 @@ ha_innobase::get_auto_increment( ulonglong col_max_value = innobase_get_int_col_max_value( table->next_number_field); + /** The following logic is needed to avoid duplicate key error + for autoincrement column. + + (1) InnoDB gives the current autoincrement value with respect + to increment and offset value. + + (2) Basically it does compute_next_insert_id() logic inside InnoDB + to avoid the current auto increment value changed by handler layer. + + (3) It is restricted only for insert operations. */ + + if (increment > 1 && thd_sql_command(user_thd) != SQLCOM_ALTER_TABLE + && autoinc < col_max_value) { + + ulint prev_auto_inc = autoinc; + + autoinc = ((autoinc - 1) + increment - offset)/ increment; + + autoinc = autoinc * increment + offset; + + /* If autoinc exceeds the col_max_value then reset + to old autoinc value. Because in case of non-strict + sql mode, boundary value is not considered as error. */ + + if (autoinc >= col_max_value) { + autoinc = prev_auto_inc; + } + + ut_ad(autoinc > 0); + } + /* Called for the first time ? */ if (trx->n_autoinc_rows == 0) { diff --git a/storage/innobase/row/row0mysql.c b/storage/innobase/row/row0mysql.c index 2c39ec4587f87..ad07ff7709dee 100644 --- a/storage/innobase/row/row0mysql.c +++ b/storage/innobase/row/row0mysql.c @@ -1241,6 +1241,8 @@ row_insert_for_mysql( row_ins_step(thr); + DEBUG_SYNC_C("ib_after_row_insert_step"); + err = trx->error_state; if (err != DB_SUCCESS) { diff --git a/storage/xtradb/handler/ha_innodb.cc b/storage/xtradb/handler/ha_innodb.cc index d2079585b94e9..d148ab8c8d898 100644 --- a/storage/xtradb/handler/ha_innodb.cc +++ b/storage/xtradb/handler/ha_innodb.cc @@ -11731,6 +11731,37 @@ ha_innobase::get_auto_increment( ulonglong col_max_value = innobase_get_int_col_max_value( table->next_number_field); + /** The following logic is needed to avoid duplicate key error + for autoincrement column. + + (1) InnoDB gives the current autoincrement value with respect + to increment and offset value. + + (2) Basically it does compute_next_insert_id() logic inside InnoDB + to avoid the current auto increment value changed by handler layer. + + (3) It is restricted only for insert operations. */ + + if (increment > 1 && thd_sql_command(user_thd) != SQLCOM_ALTER_TABLE + && autoinc < col_max_value) { + + ulint prev_auto_inc = autoinc; + + autoinc = ((autoinc - 1) + increment - offset)/ increment; + + autoinc = autoinc * increment + offset; + + /* If autoinc exceeds the col_max_value then reset + to old autoinc value. Because in case of non-strict + sql mode, boundary value is not considered as error. */ + + if (autoinc >= col_max_value) { + autoinc = prev_auto_inc; + } + + ut_ad(autoinc > 0); + } + /* Called for the first time ? */ if (trx->n_autoinc_rows == 0) { diff --git a/storage/xtradb/row/row0mysql.c b/storage/xtradb/row/row0mysql.c index 278a287325eb8..cb156ff6e4674 100644 --- a/storage/xtradb/row/row0mysql.c +++ b/storage/xtradb/row/row0mysql.c @@ -1254,6 +1254,8 @@ row_insert_for_mysql( row_ins_step(thr); + DEBUG_SYNC_C("ib_after_row_insert_step"); + err = trx->error_state; if (err != DB_SUCCESS) { From 7153e155424de4b42bc0f96ceb6433e4ed1f827a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20M=C3=A4kel=C3=A4?= Date: Tue, 23 Jul 2019 11:01:44 +0300 Subject: [PATCH 13/21] Revert "MDEV-8827 Duplicate key with auto increment" This reverts commit ef47b62551b0f37770e5d174ea028150c5b71fd8. The parent commit 07ba5560da805c766da68811692272dd9eebdaf7 which is a backport of mysql/mysql-server@1198267c331b045b9cad26be72b1a5b4f8930a79 fixes the issue differently. --- mysql-test/suite/innodb/r/innodb-autoinc.result | 4 ++-- mysql-test/suite/innodb/t/innodb-autoinc.test | 2 +- storage/innobase/handler/ha_innodb.cc | 12 +++++++----- storage/xtradb/handler/ha_innodb.cc | 12 +++++++----- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/mysql-test/suite/innodb/r/innodb-autoinc.result b/mysql-test/suite/innodb/r/innodb-autoinc.result index 2ea6869291ec8..d6f7a93034032 100644 --- a/mysql-test/suite/innodb/r/innodb-autoinc.result +++ b/mysql-test/suite/innodb/r/innodb-autoinc.result @@ -567,7 +567,7 @@ Variable_name Value auto_increment_increment 65535 auto_increment_offset 65535 INSERT INTO t1 VALUES (NULL),(NULL); -ERROR HY000: Failed to read auto-increment value from storage engine +ERROR 22003: Out of range value for column 'c1' at row 1 SELECT * FROM t1; c1 1 @@ -660,7 +660,7 @@ t2 CREATE TABLE `t2` ( `n` int(10) unsigned NOT NULL, `o` enum('FALSE','TRUE') DEFAULT NULL, PRIMARY KEY (`m`) -) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1 +) ENGINE=InnoDB AUTO_INCREMENT=19 DEFAULT CHARSET=latin1 INSERT INTO t1 (b,c) SELECT n,o FROM t2 ; SHOW CREATE TABLE t1; Table Create Table diff --git a/mysql-test/suite/innodb/t/innodb-autoinc.test b/mysql-test/suite/innodb/t/innodb-autoinc.test index 362be2e055bad..5e5a2b49a7d09 100644 --- a/mysql-test/suite/innodb/t/innodb-autoinc.test +++ b/mysql-test/suite/innodb/t/innodb-autoinc.test @@ -349,7 +349,7 @@ INSERT INTO t1 VALUES (18446744073709551610); #-- 2^64 - 2 SELECT * FROM t1; SET @@SESSION.AUTO_INCREMENT_INCREMENT=1152921504606846976, @@SESSION.AUTO_INCREMENT_OFFSET=1152921504606846976; SHOW VARIABLES LIKE "%auto_inc%"; ---error 1467 +--error 167 INSERT INTO t1 VALUES (NULL),(NULL); SELECT * FROM t1; DROP TABLE t1; diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc index 8b48552eed4f4..dab43efbf94cb 100644 --- a/storage/innobase/handler/ha_innodb.cc +++ b/storage/innobase/handler/ha_innodb.cc @@ -1504,11 +1504,10 @@ innobase_next_autoinc( if (next_value == 0) { ulonglong next; - if (current >= offset) { + if (current > offset) { next = (current - offset) / step; } else { - next = 0; - block -= step; + next = (offset - current) / step; } ut_a(max_value > next); @@ -10530,12 +10529,15 @@ ha_innobase::get_auto_increment( current = *first_value; - if (prebuilt->autoinc_increment != increment) { + /* If the increment step of the auto increment column + decreases then it is not affecting the immediate + next value in the series. */ + if (prebuilt->autoinc_increment > increment) { current = autoinc - prebuilt->autoinc_increment; current = innobase_next_autoinc( - current, 1, increment, offset, col_max_value); + current, 1, increment, 1, col_max_value); dict_table_autoinc_initialize(prebuilt->table, current); diff --git a/storage/xtradb/handler/ha_innodb.cc b/storage/xtradb/handler/ha_innodb.cc index d148ab8c8d898..5f3639a8ea9b7 100644 --- a/storage/xtradb/handler/ha_innodb.cc +++ b/storage/xtradb/handler/ha_innodb.cc @@ -1774,11 +1774,10 @@ innobase_next_autoinc( if (next_value == 0) { ulonglong next; - if (current >= offset) { + if (current > offset) { next = (current - offset) / step; } else { - next = 0; - block -= step; + next = (offset - current) / step; } ut_a(max_value > next); @@ -11799,12 +11798,15 @@ ha_innobase::get_auto_increment( current = *first_value; - if (prebuilt->autoinc_increment != increment) { + /* If the increment step of the auto increment column + decreases then it is not affecting the immediate + next value in the series. */ + if (prebuilt->autoinc_increment > increment) { current = autoinc - prebuilt->autoinc_increment; current = innobase_next_autoinc( - current, 1, increment, offset, col_max_value); + current, 1, increment, 1, col_max_value); dict_table_autoinc_initialize(prebuilt->table, current); From 739f5239f12904247d2a61f9880ea1fafbedc332 Mon Sep 17 00:00:00 2001 From: Rahul Malik Date: Thu, 2 May 2019 10:43:17 +0530 Subject: [PATCH 14/21] Bug#15851528 DUPLICATE KEY ERROR ON AUTO-INC PK WITH MIXED AUTO_INCREMENT_INCREMENT CLIENTS Problem: Clients running different values for auto_increment_increment and doing concurrent inserts leads to "Duplicate key error" in one of them. Analysis: When auto_increment_increment value is reduced in a session, InnoDB uses last auto_increment_increment value to recalculate the autoinc value. In case, some other session has inserted a value with different auto_increment_increment, InnoDB recalculate autoinc values based on current session previous auto_increment_increment instead of considering the auto_increment_increment used for last insert across all session Fix: revert 7acdf29cb4f90498af143430e3bf0e9fd3bd39f5 a.k.a. 7c12a9e5c3200688612d59160e8f45b1c8451635 as it causing the bug. Reviewed By: Bin Kevin RB#21777 Note: In MariaDB Server, earlier changes in ae5bc059880c395ccf2cc51d5db1895dffc4f5f0 for MDEV-533 require that the original test in mysql/mysql-server@1ccd472d63a042d3237a55f5827239164219ef7e be adjusted for MariaDB. Also, ef47b62551b0f37770e5d174ea028150c5b71fd8 (MDEV-8827) had to be reverted after the upstream fix had been backported. --- .../suite/innodb/r/innodb-autoinc.result | 132 +++++++++++++++++- mysql-test/suite/innodb/t/innodb-autoinc.test | 60 ++++++++ storage/innobase/handler/ha_innodb.cc | 18 +-- storage/xtradb/handler/ha_innodb.cc | 18 +-- 4 files changed, 194 insertions(+), 34 deletions(-) diff --git a/mysql-test/suite/innodb/r/innodb-autoinc.result b/mysql-test/suite/innodb/r/innodb-autoinc.result index d6f7a93034032..d98a539f968ef 100644 --- a/mysql-test/suite/innodb/r/innodb-autoinc.result +++ b/mysql-test/suite/innodb/r/innodb-autoinc.result @@ -1340,11 +1340,139 @@ SELECT * FROM t; i 1 301 -351 +601 SHOW CREATE TABLE t; Table Create Table t CREATE TABLE `t` ( `i` int(11) NOT NULL AUTO_INCREMENT, KEY `i` (`i`) -) ENGINE=InnoDB AUTO_INCREMENT=401 DEFAULT CHARSET=latin1 +) ENGINE=InnoDB AUTO_INCREMENT=651 DEFAULT CHARSET=latin1 +DROP TABLE t; +# +# Bug#15851528 DUPLICATE KEY ERROR ON AUTO-INC PK WITH MIXED AUTO_INCREMENT_INCREMENT CLIENTS +# +# This test shows that the next record to be inserted is not affected +# by a change in auto_increment_increment. +# In addition, current value of auto_increment_increment by the client +# that uses the existing autoinc value with be used to set next autoinc +# value, which will be used by next client reguardless of its own session +# setting for auto_increment_increment. +# +# Client 1: Insert a record with auto_increment_increment=2 +CREATE TABLE t( +a SERIAL PRIMARY KEY, +b VARCHAR(200)) ENGINE=InnoDB; +SET SESSION auto_increment_increment=2; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + `b` varchar(200) DEFAULT NULL, + PRIMARY KEY (`a`), + UNIQUE KEY `a` (`a`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 +INSERT INTO t(b) VALUES('S1'); +SELECT a,b FROM t; +a b +1 S1 +# Client 2: Insert records with auto_increment_increment 2,1 +SET SESSION auto_increment_increment=2; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + `b` varchar(200) DEFAULT NULL, + PRIMARY KEY (`a`), + UNIQUE KEY `a` (`a`) +) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1 +INSERT INTO t(b) VALUES('S2'); +SELECT a,b FROM t; +a b +1 S1 +3 S2 +SET SESSION auto_increment_increment=1; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + `b` varchar(200) DEFAULT NULL, + PRIMARY KEY (`a`), + UNIQUE KEY `a` (`a`) +) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 +INSERT INTO t(b) VALUES('S2'); +SELECT a,b FROM t; +a b +1 S1 +3 S2 +5 S2 +# Client 1: Insert a record with auto_increment_increment=1 +SET SESSION auto_increment_increment=1; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + `b` varchar(200) DEFAULT NULL, + PRIMARY KEY (`a`), + UNIQUE KEY `a` (`a`) +) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 +INSERT INTO t(b) VALUES('S1'); +SELECT a,b FROM t; +a b +1 S1 +3 S2 +5 S2 +6 S1 +DROP TABLE t; +# Autoincrement behaviour with mixed insert. +CREATE TABLE t( +a TINYINT AUTO_INCREMENT PRIMARY KEY, +b VARCHAR(200)) ENGINE=InnoDB; +SET SESSION auto_increment_increment=10; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` tinyint(4) NOT NULL AUTO_INCREMENT, + `b` varchar(200) DEFAULT NULL, + PRIMARY KEY (`a`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 +INSERT INTO t(b) VALUES('S0'),('S1'); +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` tinyint(4) NOT NULL AUTO_INCREMENT, + `b` varchar(200) DEFAULT NULL, + PRIMARY KEY (`a`) +) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=latin1 +INSERT INTO t(a,b) VALUES(28,'S2'); +SET SESSION auto_increment_increment=1; +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` tinyint(4) NOT NULL AUTO_INCREMENT, + `b` varchar(200) DEFAULT NULL, + PRIMARY KEY (`a`) +) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=latin1 +INSERT INTO t(b) VALUES('S3'); +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` tinyint(4) NOT NULL AUTO_INCREMENT, + `b` varchar(200) DEFAULT NULL, + PRIMARY KEY (`a`) +) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=latin1 +INSERT INTO t(b) VALUES('S4'); +SELECT * FROM t; +a b +1 S0 +11 S1 +28 S2 +31 S3 +32 S4 +SHOW CREATE TABLE t; +Table Create Table +t CREATE TABLE `t` ( + `a` tinyint(4) NOT NULL AUTO_INCREMENT, + `b` varchar(200) DEFAULT NULL, + PRIMARY KEY (`a`) +) ENGINE=InnoDB AUTO_INCREMENT=33 DEFAULT CHARSET=latin1 DROP TABLE t; diff --git a/mysql-test/suite/innodb/t/innodb-autoinc.test b/mysql-test/suite/innodb/t/innodb-autoinc.test index 5e5a2b49a7d09..b311e161a0f06 100644 --- a/mysql-test/suite/innodb/t/innodb-autoinc.test +++ b/mysql-test/suite/innodb/t/innodb-autoinc.test @@ -680,3 +680,63 @@ INSERT INTO t VALUES (NULL); SELECT * FROM t; SHOW CREATE TABLE t; DROP TABLE t; + +--echo # +--echo # Bug#15851528 DUPLICATE KEY ERROR ON AUTO-INC PK WITH MIXED AUTO_INCREMENT_INCREMENT CLIENTS +--echo # +--echo # This test shows that the next record to be inserted is not affected +--echo # by a change in auto_increment_increment. +--echo # In addition, current value of auto_increment_increment by the client +--echo # that uses the existing autoinc value with be used to set next autoinc +--echo # value, which will be used by next client reguardless of its own session +--echo # setting for auto_increment_increment. +--echo # + +--connection default +--echo # Client 1: Insert a record with auto_increment_increment=2 +CREATE TABLE t( + a SERIAL PRIMARY KEY, + b VARCHAR(200)) ENGINE=InnoDB; +SET SESSION auto_increment_increment=2; +SHOW CREATE TABLE t; +INSERT INTO t(b) VALUES('S1'); +SELECT a,b FROM t; +--connect(con1,localhost,root,,) + +--connection con1 +--echo # Client 2: Insert records with auto_increment_increment 2,1 +SET SESSION auto_increment_increment=2; +SHOW CREATE TABLE t; +INSERT INTO t(b) VALUES('S2'); +SELECT a,b FROM t; +SET SESSION auto_increment_increment=1; +SHOW CREATE TABLE t; +INSERT INTO t(b) VALUES('S2'); +SELECT a,b FROM t; +disconnect con1; + +--connection default +--echo # Client 1: Insert a record with auto_increment_increment=1 +SET SESSION auto_increment_increment=1; +SHOW CREATE TABLE t; +INSERT INTO t(b) VALUES('S1'); +SELECT a,b FROM t; +DROP TABLE t; + +--echo # Autoincrement behaviour with mixed insert. +CREATE TABLE t( + a TINYINT AUTO_INCREMENT PRIMARY KEY, + b VARCHAR(200)) ENGINE=InnoDB; +SET SESSION auto_increment_increment=10; +SHOW CREATE TABLE t; +INSERT INTO t(b) VALUES('S0'),('S1'); +SHOW CREATE TABLE t; +INSERT INTO t(a,b) VALUES(28,'S2'); +SET SESSION auto_increment_increment=1; +SHOW CREATE TABLE t; +INSERT INTO t(b) VALUES('S3'); +SHOW CREATE TABLE t; +INSERT INTO t(b) VALUES('S4'); +SELECT * FROM t; +SHOW CREATE TABLE t; +DROP TABLE t; diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc index dab43efbf94cb..5b9c9dc9123ea 100644 --- a/storage/innobase/handler/ha_innodb.cc +++ b/storage/innobase/handler/ha_innodb.cc @@ -1,8 +1,9 @@ /***************************************************************************** -Copyright (c) 2000, 2018, Oracle and/or its affiliates. All Rights Reserved. +Copyright (c) 2000, 2019, Oracle and/or its affiliates. All Rights Reserved. Copyright (c) 2008, 2009 Google Inc. Copyright (c) 2009, Percona Inc. +Copyright (c) 2010, 2019, MariaDB Corporation. Portions of this file contain modifications contributed and copyrighted by Google, Inc. Those modifications are gratefully acknowledged and are described @@ -10529,21 +10530,6 @@ ha_innobase::get_auto_increment( current = *first_value; - /* If the increment step of the auto increment column - decreases then it is not affecting the immediate - next value in the series. */ - if (prebuilt->autoinc_increment > increment) { - - current = autoinc - prebuilt->autoinc_increment; - - current = innobase_next_autoinc( - current, 1, increment, 1, col_max_value); - - dict_table_autoinc_initialize(prebuilt->table, current); - - *first_value = current; - } - /* Compute the last value in the interval */ next_value = innobase_next_autoinc( current, *nb_reserved_values, increment, offset, diff --git a/storage/xtradb/handler/ha_innodb.cc b/storage/xtradb/handler/ha_innodb.cc index 5f3639a8ea9b7..18d4bd5b4d595 100644 --- a/storage/xtradb/handler/ha_innodb.cc +++ b/storage/xtradb/handler/ha_innodb.cc @@ -1,8 +1,9 @@ /***************************************************************************** -Copyright (c) 2000, 2018, Oracle and/or its affiliates. All Rights Reserved. +Copyright (c) 2000, 2019, Oracle and/or its affiliates. All Rights Reserved. Copyright (c) 2008, 2009 Google Inc. Copyright (c) 2009, Percona Inc. +Copyright (c) 2010, 2019, MariaDB Corporation. Portions of this file contain modifications contributed and copyrighted by Google, Inc. Those modifications are gratefully acknowledged and are described @@ -11798,21 +11799,6 @@ ha_innobase::get_auto_increment( current = *first_value; - /* If the increment step of the auto increment column - decreases then it is not affecting the immediate - next value in the series. */ - if (prebuilt->autoinc_increment > increment) { - - current = autoinc - prebuilt->autoinc_increment; - - current = innobase_next_autoinc( - current, 1, increment, 1, col_max_value); - - dict_table_autoinc_initialize(prebuilt->table, current); - - *first_value = current; - } - /* Compute the last value in the interval */ next_value = innobase_next_autoinc( current, *nb_reserved_values, increment, offset, From 9d93f9dac4d144e8b08d2ba175c10b068daf9235 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marko=20M=C3=A4kel=C3=A4?= Date: Tue, 23 Jul 2019 15:00:53 +0300 Subject: [PATCH 15/21] MDEV-8827: Fix the 32-bit build Follow-up to 07ba5560da805c766da68811692272dd9eebdaf7: Use the correct 64-bit type name ulonglong instead of ulint, like in mysql/mysql-server@4e0100d86b1b46be0107ebd46a98a0c2dbb0fab4 --- storage/innobase/handler/ha_innodb.cc | 2 +- storage/xtradb/handler/ha_innodb.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/storage/innobase/handler/ha_innodb.cc b/storage/innobase/handler/ha_innodb.cc index 5b9c9dc9123ea..e0a47e1a5ead0 100644 --- a/storage/innobase/handler/ha_innodb.cc +++ b/storage/innobase/handler/ha_innodb.cc @@ -10476,7 +10476,7 @@ ha_innobase::get_auto_increment( if (increment > 1 && thd_sql_command(user_thd) != SQLCOM_ALTER_TABLE && autoinc < col_max_value) { - ulint prev_auto_inc = autoinc; + ulonglong prev_auto_inc = autoinc; autoinc = ((autoinc - 1) + increment - offset)/ increment; diff --git a/storage/xtradb/handler/ha_innodb.cc b/storage/xtradb/handler/ha_innodb.cc index 18d4bd5b4d595..23e3f2fd135b3 100644 --- a/storage/xtradb/handler/ha_innodb.cc +++ b/storage/xtradb/handler/ha_innodb.cc @@ -11745,7 +11745,7 @@ ha_innobase::get_auto_increment( if (increment > 1 && thd_sql_command(user_thd) != SQLCOM_ALTER_TABLE && autoinc < col_max_value) { - ulint prev_auto_inc = autoinc; + ulonglong prev_auto_inc = autoinc; autoinc = ((autoinc - 1) + increment - offset)/ increment; From 9c6777c03c6bdeb329865fe56a9662d15058ff9f Mon Sep 17 00:00:00 2001 From: Anushree Prakash B Date: Thu, 28 Feb 2019 09:29:45 +0530 Subject: [PATCH 16/21] Bug#27259654 - ISSUES FOUND BY PVS-STUDIO STATIC ANALYZER DESCRIPTION =========== PVS-Studio static code analyzer found several suspicious fragments of code across various files. i) sizeof() is using the pointer ii) memcpy() doesn't copy the whole string. iii) enumeration constant 'wkb_multilinestring' is used as a variable of a Boolean-type. iv) 'throw' keyword is missing from std::runtime_error() FIX === i) Use sizeof({actual object/data type}) ii) Use strncpy() and set last char as '\0' iii) N/A (Issue has already been fixed) iv) Add 'throw' before the exception. RB: 21502 --- plugin/win_auth_client/common.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/win_auth_client/common.cc b/plugin/win_auth_client/common.cc index 0cfbb2e802c74..27f1685e200cb 100644 --- a/plugin/win_auth_client/common.cc +++ b/plugin/win_auth_client/common.cc @@ -1,4 +1,4 @@ -/* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. +/* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -504,7 +504,7 @@ const char* get_last_error_message(Error_message_buf buf) buf[0]= '\0'; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR)buf, sizeof(buf), NULL ); + (LPTSTR)buf, sizeof(Error_message_buf), NULL); return buf; } From c5e967430059212a06b1e3764030f704ca910393 Mon Sep 17 00:00:00 2001 From: Georgi Kodinov Date: Thu, 7 Mar 2019 14:08:19 +0100 Subject: [PATCH 17/21] Bug #27312862: ASAN: HEAP-USE-AFTER-FREE: UPDATEXML RB#21666 RB#21666 The xpath parsing function was using a local string buffer that was deallocated when going out of scope. However references to it are preserved in the XPATH parse tree. This was causing read-after-free. Fixed by making the xpath buffer a local variable inside the Item class for the relevant xpath function, thus being preserved for the duration of the query. --- sql/item_xmlfunc.cc | 8 ++++---- sql/item_xmlfunc.h | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/sql/item_xmlfunc.cc b/sql/item_xmlfunc.cc index 90d1f598b5b01..6978218fb90d4 100644 --- a/sql/item_xmlfunc.cc +++ b/sql/item_xmlfunc.cc @@ -1,5 +1,5 @@ -/* Copyright (c) 2005, 2016, Oracle and/or its affiliates. - Copyright (c) 2009, 2017, MariaDB +/* Copyright (c) 2005, 2019, Oracle and/or its affiliates. + Copyright (c) 2009, 2019, MariaDB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -2601,7 +2601,7 @@ my_xpath_parse(MY_XPATH *xpath, const char *str, const char *strend) void Item_xml_str_func::fix_length_and_dec() { - String *xp, tmp; + String *xp; MY_XPATH xpath; int rc; @@ -2628,7 +2628,7 @@ void Item_xml_str_func::fix_length_and_dec() return; } - if (!(xp= args[1]->val_str(&tmp))) + if (!(xp= args[1]->val_str(&xpath_tmp_value))) return; my_xpath_init(&xpath); xpath.cs= collation.collation; diff --git a/sql/item_xmlfunc.h b/sql/item_xmlfunc.h index c5cc986348d1d..5cac858ee7643 100644 --- a/sql/item_xmlfunc.h +++ b/sql/item_xmlfunc.h @@ -1,8 +1,7 @@ #ifndef ITEM_XMLFUNC_INCLUDED #define ITEM_XMLFUNC_INCLUDED -/* Copyright (c) 2000-2007 MySQL AB, 2009 Sun Microsystems, Inc. - Use is subject to license terms. +/* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -31,6 +30,7 @@ class Item_xml_str_func: public Item_str_func protected: String tmp_value, pxml; Item *nodeset_func; + String xpath_tmp_value; public: Item_xml_str_func(Item *a, Item *b): Item_str_func(a,b) From 5e8ab9b7af159cee6e954f62b6304c2c33b6f6e2 Mon Sep 17 00:00:00 2001 From: Sergei Golubchik Date: Tue, 16 Apr 2019 09:33:02 +0530 Subject: [PATCH 18/21] Bug#27302459: EMPTY VALUE IN MYSQL.PLUGIN TABLE CAUSES SERVER TO EXIT ON STARTUP Description:- During server startup, the server exits if the 'mysql.plugin' system table has any rows with empty value for the field 'name' (plugin name). --- mysql-test/r/plugin.result | 2 ++ mysql-test/t/plugin.test | 6 ++++++ sql/sql_plugin.cc | 3 +++ 3 files changed, 11 insertions(+) diff --git a/mysql-test/r/plugin.result b/mysql-test/r/plugin.result index 4fb4986d0fd11..68ee682e56259 100644 --- a/mysql-test/r/plugin.result +++ b/mysql-test/r/plugin.result @@ -277,3 +277,5 @@ UNUSABLE uninstall soname 'ha_example'; select plugin_name from information_schema.plugins where plugin_library like 'ha_example%'; plugin_name +insert mysql.plugin values (); +delete from mysql.plugin where name = ''; diff --git a/mysql-test/t/plugin.test b/mysql-test/t/plugin.test index c61a923921822..0cb25c6249dd3 100644 --- a/mysql-test/t/plugin.test +++ b/mysql-test/t/plugin.test @@ -224,3 +224,9 @@ select plugin_name from information_schema.plugins where plugin_library like 'ha uninstall soname 'ha_example'; select plugin_name from information_schema.plugins where plugin_library like 'ha_example%'; +# +# Bug#27302459: EMPTY VALUE IN MYSQL.PLUGIN TABLE CAUSES SERVER TO EXIT ON STARTUP +# +insert mysql.plugin values (); +source include/restart_mysqld.inc; +delete from mysql.plugin where name = ''; diff --git a/sql/sql_plugin.cc b/sql/sql_plugin.cc index 46df991639e11..91d0a4393c5bc 100644 --- a/sql/sql_plugin.cc +++ b/sql/sql_plugin.cc @@ -1781,6 +1781,9 @@ static void plugin_load(MEM_ROOT *tmp_root) LEX_STRING name= {(char *)str_name.ptr(), str_name.length()}; LEX_STRING dl= {(char *)str_dl.ptr(), str_dl.length()}; + if (!name.length || !dl.length) + continue; + /* there're no other threads running yet, so we don't need a mutex. but plugin_add() before is designed to work in multi-threaded From 8ddb7e3eb71010decd5acc99aa98c82bbe0139aa Mon Sep 17 00:00:00 2001 From: Sergei Golubchik Date: Wed, 24 Jul 2019 13:14:03 +0200 Subject: [PATCH 19/21] Bug#27167197 USING ? IN INSTALL PLUGIN QUERY ABORTS DEBUG, AND HANGS OPTIMIZED SERVER check_valid_path() uses my_strcspn() that cannot handle invalid characters properly. This is fixed by a big refactoring in 10.2 (MDEV-6353). For 5.5, let's simply swap tests, because check_string_char_length() rejects invalid characters just fine. --- mysql-test/r/plugin.result | 8 ++++++++ mysql-test/t/plugin.test | 9 +++++++++ sql/sql_plugin.cc | 4 ++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/mysql-test/r/plugin.result b/mysql-test/r/plugin.result index 68ee682e56259..65e993b27665b 100644 --- a/mysql-test/r/plugin.result +++ b/mysql-test/r/plugin.result @@ -277,5 +277,13 @@ UNUSABLE uninstall soname 'ha_example'; select plugin_name from information_schema.plugins where plugin_library like 'ha_example%'; plugin_name +set names utf8; +select convert('admin𝌆' using utf8); +convert('admin𝌆' using utf8) +admin +Warnings: +Warning 1300 Invalid utf8 character string: 'F09D8C' +install plugin foo soname 'admin𝌆'; +ERROR HY000: No paths allowed for shared library insert mysql.plugin values (); delete from mysql.plugin where name = ''; diff --git a/mysql-test/t/plugin.test b/mysql-test/t/plugin.test index 0cb25c6249dd3..05347d221a0f0 100644 --- a/mysql-test/t/plugin.test +++ b/mysql-test/t/plugin.test @@ -224,9 +224,18 @@ select plugin_name from information_schema.plugins where plugin_library like 'ha uninstall soname 'ha_example'; select plugin_name from information_schema.plugins where plugin_library like 'ha_example%'; +# +# +# +set names utf8; +select convert('admin𝌆' using utf8); +--error ER_UDF_NO_PATHS +install plugin foo soname 'admin𝌆'; + # # Bug#27302459: EMPTY VALUE IN MYSQL.PLUGIN TABLE CAUSES SERVER TO EXIT ON STARTUP # insert mysql.plugin values (); source include/restart_mysqld.inc; delete from mysql.plugin where name = ''; + diff --git a/sql/sql_plugin.cc b/sql/sql_plugin.cc index 91d0a4393c5bc..a90c755804532 100644 --- a/sql/sql_plugin.cc +++ b/sql/sql_plugin.cc @@ -736,9 +736,9 @@ static st_plugin_dl *plugin_dl_add(const LEX_STRING *dl, int report) This is done to ensure that only approved libraries from the plugin directory are used (to make this even remotely secure). */ - if (check_valid_path(dl->str, dl->length) || - check_string_char_length((LEX_STRING *) dl, "", NAME_CHAR_LEN, + if (check_string_char_length((LEX_STRING *) dl, "", NAME_CHAR_LEN, system_charset_info, 1) || + check_valid_path(dl->str, dl->length) || plugin_dir_len + dl->length + 1 >= FN_REFLEN) { report_error(report, ER_UDF_NO_PATHS); From 7473a71a282c47a1e95359c575089c8ef51caf56 Mon Sep 17 00:00:00 2001 From: Gleb Shchepa Date: Mon, 20 May 2019 10:53:00 +0400 Subject: [PATCH 20/21] Bug #29419820: MEMORY LEAK IN MY_YYOVERFLOW() Note: this patch is for 5.6. Detected by ASAN. The patch fixes the cleanup of parser stack pointers. Reviewed-by: Guilhem Bichot --- sql/sql_lex.h | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/sql/sql_lex.h b/sql/sql_lex.h index 8f629750198ae..2410f6edc239f 100644 --- a/sql/sql_lex.h +++ b/sql/sql_lex.h @@ -1,5 +1,5 @@ -/* Copyright (c) 2000, 2015, Oracle and/or its affiliates. - Copyright (c) 2010, 2018, MariaDB Corporation +/* Copyright (c) 2000, 2019, Oracle and/or its affiliates. + Copyright (c) 2010, 2019, MariaDB Corporation This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -2858,15 +2858,18 @@ class Set_signal_information class Yacc_state { public: - Yacc_state() - { - reset(); - } + Yacc_state() : yacc_yyss(NULL), yacc_yyvs(NULL) { reset(); } void reset() { - yacc_yyss= NULL; - yacc_yyvs= NULL; + if (yacc_yyss != NULL) { + my_free(yacc_yyss); + yacc_yyss = NULL; + } + if (yacc_yyvs != NULL) { + my_free(yacc_yyvs); + yacc_yyvs = NULL; + } m_set_signal_info.clear(); m_lock_type= TL_READ_DEFAULT; m_mdl_type= MDL_SHARED_READ; From 2536c0b1ebf6c5012ae34435d82fb2f5fa54aea5 Mon Sep 17 00:00:00 2001 From: Nisha Gopalakrishnan Date: Wed, 17 Apr 2019 15:36:29 +0530 Subject: [PATCH 21/21] BUG#28642318: POINT IN TIME RECOVERY USING MYSQLBINLOG BROKEN WITH TEMPORARY TABLE -> ERRORS Analysis ======== Point in time recovery using mysqlbinlog containing queries operating on temporary tables results in an error. While writing the query log event in the binary log, the thread id used for execution of DROP TABLE and DELETE commands were incorrect. The thread variable 'thread_specific_used' is used to determine whether a specific thread id is to used while executing the statements i.e using 'SET @@session.pseudo_thread_id'. This variable was not set correctly for DROP TABLE query and was never set for DELETE query. The thread id is important for temporary tables since the tables are session specific. DROP TABLE and DELETE queries executed using a wrong thread id resulted in errors while applying the queries generated by mysqlbinlog utility. Fix === Set the 'thread_specific_used' THD variable for DROP TABLE and DELETE queries. ReviewBoard: 21833 --- .../extra/binlog_tests/drop_temp_table.test | 69 +++++++++++++++++++ .../binlog/r/binlog_row_drop_tmp_tbl.result | 27 ++++++++ .../binlog/r/binlog_stm_drop_tmp_tbl.result | 27 ++++++++ sql/sql_delete.cc | 5 +- sql/sql_table.cc | 7 +- 5 files changed, 129 insertions(+), 6 deletions(-) diff --git a/mysql-test/extra/binlog_tests/drop_temp_table.test b/mysql-test/extra/binlog_tests/drop_temp_table.test index c852ee4c8a078..b53b41292d80b 100644 --- a/mysql-test/extra/binlog_tests/drop_temp_table.test +++ b/mysql-test/extra/binlog_tests/drop_temp_table.test @@ -100,3 +100,72 @@ RESET MASTER; DROP TABLE t1; # End of 4.1 tests + + +--echo # +--echo # BUG#28642318: POINT IN TIME RECOVERY USING MYSQLBINLOG BROKEN +--echo # WITH TEMPORARY TABLE -> ERRORS + +--echo # Test case for DELETE query. + +RESET MASTER; +connect (con1,localhost,root,,); + +--echo # Set up. +--connection default +--disable_warnings +SET @save_binlog_format= @@session.binlog_format; +SET @@session.binlog_format=STATEMENT; +let $MYSQLD_DATADIR= `select @@datadir`; +CREATE TABLE t1 (a INT) ENGINE=INNODB; + +--connection con1 +SET @@session.binlog_format=STATEMENT; +CREATE TEMPORARY TABLE t1 (b BLOB) ENGINE=INNODB; + +--connection default +DELETE d1, d2 FROM t1 AS d1, t1 AS d2 WHERE d1.a<>d2.a; + +--exec $MYSQL_BINLOG --force-if-open $MYSQLD_DATADIR/master-bin.000001 > $MYSQLTEST_VARDIR/tmp/bug28642318.sql + +--connection default +DROP TABLE t1; + +--echo # DELETE query fails with table re-open error without patch. +--exec $MYSQL < $MYSQLTEST_VARDIR/tmp/bug28642318.sql + +--echo # Clean up. +--connection con1 +DROP TABLE IF EXISTS t1; + +--connection default +DROP TABLE IF EXISTS t1; +RESET MASTER; + +--echo # Test case for DROP query. + +--connection default +CREATE TABLE t1 (a INT) ENGINE=INNODB; + +--connection con1 +CREATE TEMPORARY TABLE t1 (b BLOB) ENGINE=INNODB; + +--connection default +DROP TABLE t1; + +--connection con1 +DROP TABLE t1; + +--connection default +--exec $MYSQL_BINLOG --force-if-open $MYSQLD_DATADIR/master-bin.000001 > $MYSQLTEST_VARDIR/tmp/bug28642318.sql + +--echo # DROP table query fails with unknown table error without patch. +--exec $MYSQL < $MYSQLTEST_VARDIR/tmp/bug28642318.sql + +--echo # Clean up +--connection default +SET @@session.binlog_format= @save_binlog_format; +RESET MASTER; + +--disconnect con1 +--enable_warnings diff --git a/mysql-test/suite/binlog/r/binlog_row_drop_tmp_tbl.result b/mysql-test/suite/binlog/r/binlog_row_drop_tmp_tbl.result index ca5181f010676..1641072c930b7 100644 --- a/mysql-test/suite/binlog/r/binlog_row_drop_tmp_tbl.result +++ b/mysql-test/suite/binlog/r/binlog_row_drop_tmp_tbl.result @@ -55,3 +55,30 @@ master-bin.000001 # Query # # COMMIT master-bin.000001 # Query # # use `test`; DROP /*!40005 TEMPORARY */ TABLE IF EXISTS `ttmp1` RESET MASTER; DROP TABLE t1; +# +# BUG#28642318: POINT IN TIME RECOVERY USING MYSQLBINLOG BROKEN +# WITH TEMPORARY TABLE -> ERRORS +# Test case for DELETE query. +RESET MASTER; +# Set up. +SET @save_binlog_format= @@session.binlog_format; +SET @@session.binlog_format=STATEMENT; +CREATE TABLE t1 (a INT) ENGINE=INNODB; +SET @@session.binlog_format=STATEMENT; +CREATE TEMPORARY TABLE t1 (b BLOB) ENGINE=INNODB; +DELETE d1, d2 FROM t1 AS d1, t1 AS d2 WHERE d1.a<>d2.a; +DROP TABLE t1; +# DELETE query fails with table re-open error without patch. +# Clean up. +DROP TABLE IF EXISTS t1; +DROP TABLE IF EXISTS t1; +RESET MASTER; +# Test case for DROP query. +CREATE TABLE t1 (a INT) ENGINE=INNODB; +CREATE TEMPORARY TABLE t1 (b BLOB) ENGINE=INNODB; +DROP TABLE t1; +DROP TABLE t1; +# DROP table query fails with unknown table error without patch. +# Clean up +SET @@session.binlog_format= @save_binlog_format; +RESET MASTER; diff --git a/mysql-test/suite/binlog/r/binlog_stm_drop_tmp_tbl.result b/mysql-test/suite/binlog/r/binlog_stm_drop_tmp_tbl.result index 027bf1eaada90..35d5d1e51a19b 100644 --- a/mysql-test/suite/binlog/r/binlog_stm_drop_tmp_tbl.result +++ b/mysql-test/suite/binlog/r/binlog_stm_drop_tmp_tbl.result @@ -63,3 +63,30 @@ master-bin.000001 # Query # # COMMIT master-bin.000001 # Query # # use `test`; DROP /*!40005 TEMPORARY */ TABLE IF EXISTS `ttmp1` RESET MASTER; DROP TABLE t1; +# +# BUG#28642318: POINT IN TIME RECOVERY USING MYSQLBINLOG BROKEN +# WITH TEMPORARY TABLE -> ERRORS +# Test case for DELETE query. +RESET MASTER; +# Set up. +SET @save_binlog_format= @@session.binlog_format; +SET @@session.binlog_format=STATEMENT; +CREATE TABLE t1 (a INT) ENGINE=INNODB; +SET @@session.binlog_format=STATEMENT; +CREATE TEMPORARY TABLE t1 (b BLOB) ENGINE=INNODB; +DELETE d1, d2 FROM t1 AS d1, t1 AS d2 WHERE d1.a<>d2.a; +DROP TABLE t1; +# DELETE query fails with table re-open error without patch. +# Clean up. +DROP TABLE IF EXISTS t1; +DROP TABLE IF EXISTS t1; +RESET MASTER; +# Test case for DROP query. +CREATE TABLE t1 (a INT) ENGINE=INNODB; +CREATE TEMPORARY TABLE t1 (b BLOB) ENGINE=INNODB; +DROP TABLE t1; +DROP TABLE t1; +# DROP table query fails with unknown table error without patch. +# Clean up +SET @@session.binlog_format= @save_binlog_format; +RESET MASTER; diff --git a/sql/sql_delete.cc b/sql/sql_delete.cc index 27dd4260ea2f8..df1489fbd93a2 100644 --- a/sql/sql_delete.cc +++ b/sql/sql_delete.cc @@ -1,5 +1,5 @@ -/* - Copyright (c) 2000, 2010, Oracle and/or its affiliates. +/* Copyright (c) 2000, 2019, Oracle and/or its affiliates. + Copyright (c) 2009, 2019, MariaDB Corporation This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -1054,6 +1054,7 @@ bool multi_delete::send_eof() thd->clear_error(); else errcode= query_error_code(thd, killed_status == NOT_KILLED); + thd->thread_specific_used= TRUE; if (thd->binlog_query(THD::ROW_QUERY_TYPE, thd->query(), thd->query_length(), transactional_tables, FALSE, FALSE, errcode) && diff --git a/sql/sql_table.cc b/sql/sql_table.cc index 99f2ce580c05e..9e80350f3abc9 100644 --- a/sql/sql_table.cc +++ b/sql/sql_table.cc @@ -1,6 +1,6 @@ /* - Copyright (c) 2000, 2016, Oracle and/or its affiliates. - Copyright (c) 2010, 2018, MariaDB + Copyright (c) 2000, 2019, Oracle and/or its affiliates. + Copyright (c) 2010, 2019, MariaDB This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -2358,8 +2358,7 @@ int mysql_rm_table_no_locks(THD *thd, TABLE_LIST *tables, bool if_exists, table->table_name);); } DEBUG_SYNC(thd, "rm_table_no_locks_before_binlog"); - thd->thread_specific_used|= (trans_tmp_table_deleted || - non_trans_tmp_table_deleted); + thd->thread_specific_used= TRUE; error= 0; err: if (wrong_tables.length())