summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVarun Gupta <varunraiko1803@gmail.com>2018-06-02 11:52:48 +0530
committerVarun Gupta <varunraiko1803@gmail.com>2018-06-02 11:52:48 +0530
commit5fb2c586f225361deae0f04dc6f72a3f0d168ac2 (patch)
tree4ac22d889103a2fe6ee456197a5a183fa2e8a77a
parent1ada4afb0a51f7283b6187a95019ec2cb80c8a0b (diff)
downloadmariadb-git-5fb2c586f225361deae0f04dc6f72a3f0d168ac2.tar.gz
MDEV-16225: wrong resultset from query with semijoin=on
For non-semi-join subquery optimization we do a cost based decision between Materialisation and IN -> EXIST transformation. The issue in this case is that for IN->EXIST transformation we run JOIN::reoptimize with the IN->EXISt conditions and we come up with a new query plan. But when we compare the cost with Materialization, we make the decision to chose Materialization so we need to restore the query plan for Materilization. The saving and restoring for keyuse array and join_tab keyuse is only done when we have atleast one element in the keyuse_array , we are now changing to do it even for 0 elements to main the generality.
-rw-r--r--mysql-test/r/subselect_sj2_mat.result57
-rw-r--r--mysql-test/t/subselect_sj2_mat.test42
-rw-r--r--sql/sql_select.cc46
3 files changed, 119 insertions, 26 deletions
diff --git a/mysql-test/r/subselect_sj2_mat.result b/mysql-test/r/subselect_sj2_mat.result
index 835742a3ff4..19aa3bd3f02 100644
--- a/mysql-test/r/subselect_sj2_mat.result
+++ b/mysql-test/r/subselect_sj2_mat.result
@@ -1601,3 +1601,60 @@ id select_type table type possible_keys key key_len ref rows Extra
1 PRIMARY <subquery2> eq_ref distinct_key distinct_key 11 func 1
2 MATERIALIZED t2 ALL NULL NULL NULL NULL 3 Using where
DROP TABLE t1,t2;
+#
+# MDEV-16225: wrong resultset from query with semijoin=on
+#
+CREATE TABLE t1 (
+`id` int(10) NOT NULL AUTO_INCREMENT,
+`local_name` varchar(64) NOT NULL,
+PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;
+insert into t1(`id`,`local_name`) values
+(1,'Cash Advance'),
+(2,'Cash Advance'),
+(3,'Rollover'),
+(4,'AL Installment'),
+(5,'AL Installment'),
+(6,'AL Installment'),
+(7,'AL Installment'),
+(8,'AL Installment'),
+(9,'AL Installment'),
+(10,'Internet Payday'),
+(11,'Rollover - Internet Payday'),
+(12,'AL Monthly Installment'),
+(13,'AL Semi-Monthly Installment');
+explain
+SELECT SQL_NO_CACHE t.id
+FROM t1 t
+WHERE (
+t.id IN (SELECT A.id FROM t1 AS A WHERE A.local_name IN (SELECT B.local_name FROM t1 AS B WHERE B.id IN (0,4,12,13,1,10,3,11)))
+OR
+(t.id IN (0,4,12,13,1,10,3,11))
+);
+id select_type table type possible_keys key key_len ref rows Extra
+1 PRIMARY t index PRIMARY PRIMARY 4 NULL 13 Using where; Using index
+2 MATERIALIZED <subquery3> ALL distinct_key NULL NULL NULL 8
+2 MATERIALIZED A ALL PRIMARY NULL NULL NULL 13 Using where; Using join buffer (flat, BNL join)
+3 MATERIALIZED B ALL PRIMARY NULL NULL NULL 13 Using where
+SELECT SQL_NO_CACHE t.id
+FROM t1 t
+WHERE (
+t.id IN (SELECT A.id FROM t1 AS A WHERE A.local_name IN (SELECT B.local_name FROM t1 AS B WHERE B.id IN (0,4,12,13,1,10,3,11)))
+OR
+(t.id IN (0,4,12,13,1,10,3,11))
+);
+id
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10
+11
+12
+13
+drop table t1;
diff --git a/mysql-test/t/subselect_sj2_mat.test b/mysql-test/t/subselect_sj2_mat.test
index cfb6c8c2819..0665cdf68fe 100644
--- a/mysql-test/t/subselect_sj2_mat.test
+++ b/mysql-test/t/subselect_sj2_mat.test
@@ -303,3 +303,45 @@ eval $q;
eval explain $q;
DROP TABLE t1,t2;
+
+--echo #
+--echo # MDEV-16225: wrong resultset from query with semijoin=on
+--echo #
+
+CREATE TABLE t1 (
+ `id` int(10) NOT NULL AUTO_INCREMENT,
+ `local_name` varchar(64) NOT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;
+
+insert into t1(`id`,`local_name`) values
+(1,'Cash Advance'),
+(2,'Cash Advance'),
+(3,'Rollover'),
+(4,'AL Installment'),
+(5,'AL Installment'),
+(6,'AL Installment'),
+(7,'AL Installment'),
+(8,'AL Installment'),
+(9,'AL Installment'),
+(10,'Internet Payday'),
+(11,'Rollover - Internet Payday'),
+(12,'AL Monthly Installment'),
+(13,'AL Semi-Monthly Installment');
+
+explain
+SELECT SQL_NO_CACHE t.id
+FROM t1 t
+WHERE (
+ t.id IN (SELECT A.id FROM t1 AS A WHERE A.local_name IN (SELECT B.local_name FROM t1 AS B WHERE B.id IN (0,4,12,13,1,10,3,11)))
+ OR
+ (t.id IN (0,4,12,13,1,10,3,11))
+);
+SELECT SQL_NO_CACHE t.id
+FROM t1 t
+WHERE (
+ t.id IN (SELECT A.id FROM t1 AS A WHERE A.local_name IN (SELECT B.local_name FROM t1 AS B WHERE B.id IN (0,4,12,13,1,10,3,11)))
+ OR
+ (t.id IN (0,4,12,13,1,10,3,11))
+);
+drop table t1;
diff --git a/sql/sql_select.cc b/sql/sql_select.cc
index d6d269a700f..016dd594e08 100644
--- a/sql/sql_select.cc
+++ b/sql/sql_select.cc
@@ -23739,21 +23739,18 @@ void JOIN::set_allowed_join_cache_types()
void JOIN::save_query_plan(Join_plan_state *save_to)
{
- if (keyuse.elements)
- {
- DYNAMIC_ARRAY tmp_keyuse;
- /* Swap the current and the backup keyuse internal arrays. */
- tmp_keyuse= keyuse;
- keyuse= save_to->keyuse; /* keyuse is reset to an empty array. */
- save_to->keyuse= tmp_keyuse;
+ DYNAMIC_ARRAY tmp_keyuse;
+ /* Swap the current and the backup keyuse internal arrays. */
+ tmp_keyuse= keyuse;
+ keyuse= save_to->keyuse; /* keyuse is reset to an empty array. */
+ save_to->keyuse= tmp_keyuse;
- for (uint i= 0; i < table_count; i++)
- {
- save_to->join_tab_keyuse[i]= join_tab[i].keyuse;
- join_tab[i].keyuse= NULL;
- save_to->join_tab_checked_keys[i]= join_tab[i].checked_keys;
- join_tab[i].checked_keys.clear_all();
- }
+ for (uint i= 0; i < table_count; i++)
+ {
+ save_to->join_tab_keyuse[i]= join_tab[i].keyuse;
+ join_tab[i].keyuse= NULL;
+ save_to->join_tab_checked_keys[i]= join_tab[i].checked_keys;
+ join_tab[i].checked_keys.clear_all();
}
memcpy((uchar*) save_to->best_positions, (uchar*) best_positions,
sizeof(POSITION) * (table_count + 1));
@@ -23791,20 +23788,17 @@ void JOIN::reset_query_plan()
void JOIN::restore_query_plan(Join_plan_state *restore_from)
{
- if (restore_from->keyuse.elements)
- {
- DYNAMIC_ARRAY tmp_keyuse;
- tmp_keyuse= keyuse;
- keyuse= restore_from->keyuse;
- restore_from->keyuse= tmp_keyuse;
-
- for (uint i= 0; i < table_count; i++)
- {
- join_tab[i].keyuse= restore_from->join_tab_keyuse[i];
- join_tab[i].checked_keys= restore_from->join_tab_checked_keys[i];
- }
+ DYNAMIC_ARRAY tmp_keyuse;
+ tmp_keyuse= keyuse;
+ keyuse= restore_from->keyuse;
+ restore_from->keyuse= tmp_keyuse;
+ for (uint i= 0; i < table_count; i++)
+ {
+ join_tab[i].keyuse= restore_from->join_tab_keyuse[i];
+ join_tab[i].checked_keys= restore_from->join_tab_checked_keys[i];
}
+
memcpy((uchar*) best_positions, (uchar*) restore_from->best_positions,
sizeof(POSITION) * (table_count + 1));
/* Restore SJM nests */