Skip to content

Commit

Permalink
MDEV-30540 Wrong result with IN list length reaching IN_PREDICATE_CON…
Browse files Browse the repository at this point in the history
…VERSION_THRESHOLD

The problem was the mysql_derived_prepare() did not correctly set
'distinct' when creating a temporary derivated table.

Fixed by separating checking for distinct for queries with and without
UNION.

Other things:
- Fixed bug in generate_derived_keys_for_table() where we set the wrong
  bit for join_tab->keys
- Cleaned up JOIN::drop_unused_derived_keys()
- Changed TABLE::use_index() to keep unique keys and update
  share->key_parts

Author: Sergei Petrunia <sergey@mariadb.com>, monty@mariadb.org
  • Loading branch information
montywi committed Mar 2, 2023
1 parent eb441f6 commit bd9ca2a
Show file tree
Hide file tree
Showing 16 changed files with 388 additions and 155 deletions.
40 changes: 39 additions & 1 deletion mysql-test/main/derived.result
Original file line number Diff line number Diff line change
Expand Up @@ -1315,7 +1315,7 @@ a a
analyze select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a from t2 order by c) except(select t3.a from t3 order by b))q where t1.a=q.a;
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00 Using where
1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 1 0.50 100.00 100.00
1 PRIMARY <derived2> eq_ref distinct_key distinct_key 5 test.t1.a 1 0.50 100.00 100.00
2 DERIVED t2 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00
3 UNION t2 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00
4 EXCEPT t3 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00
Expand All @@ -1325,6 +1325,23 @@ a a
3 3
4 4
6 6
analyze select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a from t2 order by c) except ALL (select t3.a from t3 order by b))q where t1.a=q.a;
id select_type table type possible_keys key key_len ref rows r_rows filtered r_filtered Extra
1 PRIMARY t1 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00 Using where
1 PRIMARY <derived2> ref key0 key0 5 test.t1.a 1 1.17 100.00 100.00
2 DERIVED t2 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00
3 UNION t2 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00
4 EXCEPT t3 ALL NULL NULL NULL NULL 6 6.00 100.00 100.00
NULL UNIT RESULT <unit2,3,4> ALL NULL NULL NULL NULL NULL 7.00 NULL NULL
select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a from t2 order by c) except ALL (select t3.a from t3 order by b))q where t1.a=q.a;
a a
3 3
3 3
4 4
4 4
5 5
6 6
6 6
drop table t1,t2,t3;
#
# MDEV-16549: Server crashes in Item_field::fix_fields on query with
Expand Down Expand Up @@ -1438,5 +1455,26 @@ SET @@IN_PREDICATE_CONVERSION_THRESHOLD=@@global.IN_PREDICATE_CONVERSION_THRESHO
SET @@OPTIMIZER_SWITCH=@@global.OPTIMIZER_SWITCH;
DROP TABLE t1, t2;
#
# MDEV-30540 Wrong result with IN list length reaching
# IN_PREDICATE_CONVERSION_THRESHOLD
#
CREATE TABLE t1 (a INT PRIMARY KEY);
INSERT INTO t1 SELECT seq FROM seq_1_to_30;
ANALYZE TABLE t1 PERSISTENT FOR ALL;
Table Op Msg_type Msg_text
test.t1 analyze status Engine-independent statistics collected
test.t1 analyze status OK
SET IN_PREDICATE_CONVERSION_THRESHOLD=4;
SELECT a FROM t1 WHERE a IN ( 1, 1, 2, 194 );
a
1
2
SET IN_PREDICATE_CONVERSION_THRESHOLD=100;
SELECT a FROM t1 WHERE a IN ( 1, 1, 2, 194 );
a
1
2
drop table t1;
#
# End of 11.0 tests
#
19 changes: 19 additions & 0 deletions mysql-test/main/derived.test
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,11 @@ analyze select * from t1 , ( (select t2.a from t2 order by c) union all (select

select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a from t2 order by c) except(select t3.a from t3 order by b))q where t1.a=q.a;

analyze select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a from t2 order by c) except ALL (select t3.a from t3 order by b))q where t1.a=q.a;

select * from t1 , ( (select t2.a from t2 order by c) union all (select t2.a from t2 order by c) except ALL (select t3.a from t3 order by b))q where t1.a=q.a;


drop table t1,t2,t3;

--echo #
Expand Down Expand Up @@ -1224,6 +1229,20 @@ SET @@IN_PREDICATE_CONVERSION_THRESHOLD=@@global.IN_PREDICATE_CONVERSION_THRESHO
SET @@OPTIMIZER_SWITCH=@@global.OPTIMIZER_SWITCH;
DROP TABLE t1, t2;

--echo #
--echo # MDEV-30540 Wrong result with IN list length reaching
--echo # IN_PREDICATE_CONVERSION_THRESHOLD
--echo #

CREATE TABLE t1 (a INT PRIMARY KEY);
INSERT INTO t1 SELECT seq FROM seq_1_to_30;
ANALYZE TABLE t1 PERSISTENT FOR ALL;
SET IN_PREDICATE_CONVERSION_THRESHOLD=4;
SELECT a FROM t1 WHERE a IN ( 1, 1, 2, 194 );
SET IN_PREDICATE_CONVERSION_THRESHOLD=100;
SELECT a FROM t1 WHERE a IN ( 1, 1, 2, 194 );
drop table t1;

--echo #
--echo # End of 11.0 tests
--echo #
Loading

0 comments on commit bd9ca2a

Please sign in to comment.