Skip to content

Commit

Permalink
MDEV-32397, MDEV-32403 Crashes during join processing.
Browse files Browse the repository at this point in the history
Queries having the following form may cause a crash
  SELECT t1.a FROM ( SELECT a AS a1 FROM t1 ) dt
  JOIN t1 ON a1 LIKE EXISTS ( SELECT a + RAND () FROM t1 UNION SELECT a FROM t1);
because the table t1 has some JOIN cleanup operations performed prematurely
during the subselect.

In this particular case, the presence of RAND() makes the subquery
uncacheable, necessitating the need to execute the subquery multiple
times during join record evaluation.  Each time the subquery runs, it
creates its own JOIN structure which has references to the table t1.
When the subquery completes, JOIN::cleanup and functions called by it
result in ha_end_keyread() being called on table t1.  However, we are
not done with table t1 because the upper level `select t1.a from...`
query requires table t1 to be open.  To solve this, we make the executor
aware of when we're in subqueries like this and delay JOIN cleanup
until the end of the query.
  • Loading branch information
DaveGosselin-MariaDB committed May 14, 2024
1 parent 7e65512 commit 50c275e
Show file tree
Hide file tree
Showing 3 changed files with 383 additions and 104 deletions.
185 changes: 185 additions & 0 deletions mysql-test/main/subselect_like_exists.result
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
CREATE TABLE t0 ( c38 DOUBLE ( 87 , 18 ) ) ;
INSERT INTO t0 VALUES ( DEFAULT ) , ( DEFAULT ) ;
CREATE INDEX i0 ON t0 ( c38 ) ;
INSERT INTO t0 VALUES ( 34 ) , ( -14 ) ;
SELECT t0 . c38 AS c5 FROM ( SELECT c38 AS c27 FROM t0 ) AS t1 JOIN t0 ON t0 . c38 >= LN ( c27 IS TRUE ) LIKE EXISTS ( SELECT SPACE ( c24 ) - UNHEX ( c38 ) + STD( ORD ( -26 ) & RADIANS ( 62 ) / TRIM( LEADING 110 FROM 'a4~mq;A825}$&%(C
{!}

.!
{S&pATQJb%F}

qU@UH?VHFyU4%))Gx' ) AND -19.704435 | ACOS ( -69 IS TRUE ) ) % + RAND ( CONVERT ( -75 , UNSIGNED ) IN ( -64 , 10 , -118 ) XOR -85 = MAKE_SET ( 11 , ROUND ( -89 , -20 ) SOUNDS LIKE TRIM( TRAILING FROM 34 ) AND RAND ( ) ) ) AS c17 FROM ( SELECT t2 . c38 AS c24 FROM t0 LEFT OUTER JOIN t0 AS t2 USING ( c38 ) ) AS t3 JOIN t0 ON t3 . c24 = t0 . c38 GROUP BY c38 , c24 UNION SELECT c38 + 35 AS c48 FROM t0 WHERE c38 IN ( SELECT c38 AS c12 FROM t0 ) ) ;
c5
34.000000000000000000
34.000000000000000000
Warnings:
Warning 1292 Truncated incorrect DOUBLE value: 'a4~mq;A825}$&%(C
{!}

.!
{S&pATQJb%F}

qU@UH?VHFyU4%))Gx'
Warning 1365 Division by 0
Warning 1365 Division by 0
Warning 1365 Division by 0
Warning 1292 Truncated incorrect DOUBLE value: 'a4~mq;A825}$&%(C
{!}

.!
{S&pATQJb%F}

qU@UH?VHFyU4%))Gx'
Warning 1365 Division by 0
Note 1105 Cast to unsigned converted negative integer to it's positive complement
Warning 1292 Truncated incorrect DOUBLE value: ''
Warning 1292 Truncated incorrect DOUBLE value: 'a4~mq;A825}$&%(C
{!}

.!
{S&pATQJb%F}

qU@UH?VHFyU4%))Gx'
Warning 1365 Division by 0
Note 1105 Cast to unsigned converted negative integer to it's positive complement
Warning 1292 Truncated incorrect DOUBLE value: ' '
Warning 1292 Truncated incorrect DOUBLE value: 'a4~mq;A825}$&%(C
{!}

.!
{S&pATQJb%F}

qU@UH?VHFyU4%))Gx'
Warning 1365 Division by 0
Note 1105 Cast to unsigned converted negative integer to it's positive complement
Warning 1292 Truncated incorrect DOUBLE value: ''
Warning 1292 Truncated incorrect DOUBLE value: 'a4~mq;A825}$&%(C
{!}

.!
{S&pATQJb%F}

qU@UH?VHFyU4%))Gx'
Warning 1365 Division by 0
Note 1105 Cast to unsigned converted negative integer to it's positive complement
Warning 1292 Truncated incorrect DOUBLE value: ' '
Warning 1365 Division by 0
Warning 1365 Division by 0
Warning 1292 Truncated incorrect DOUBLE value: 'a4~mq;A825}$&%(C
{!}

.!
{S&pATQJb%F}

qU@UH?VHFyU4%))Gx'
Warning 1365 Division by 0
Note 1105 Cast to unsigned converted negative integer to it's positive complement
Warning 1292 Truncated incorrect DOUBLE value: ''
Warning 1292 Truncated incorrect DOUBLE value: 'a4~mq;A825}$&%(C
{!}

.!
{S&pATQJb%F}

qU@UH?VHFyU4%))Gx'
Warning 1365 Division by 0
Note 1105 Cast to unsigned converted negative integer to it's positive complement
Warning 1292 Truncated incorrect DOUBLE value: ' '
Warning 1292 Truncated incorrect DOUBLE value: 'a4~mq;A825}$&%(C
{!}

.!
{S&pATQJb%F}

qU@UH?VHFyU4%))Gx'
Warning 1365 Division by 0
Note 1105 Cast to unsigned converted negative integer to it's positive complement
Warning 1292 Truncated incorrect DOUBLE value: ''
Warning 1292 Truncated incorrect DOUBLE value: 'a4~mq;A825}$&%(C
{!}

.!
{S&pATQJb%F}

qU@UH?VHFyU4%))Gx'
Warning 1365 Division by 0
Note 1105 Cast to unsigned converted negative integer to it's positive complement
Warning 1292 Truncated incorrect DOUBLE value: ' '
CREATE TABLE t1 ( a double, key (a)) ;
INSERT INTO t1 VALUES (1),(2),(-3);
SELECT t1.a FROM ( SELECT a AS a1 FROM t1 ) dt
JOIN t1 ON a1 LIKE EXISTS ( SELECT a + RAND () FROM t1 UNION SELECT a FROM t1) ;
drop table t0, t1;
CREATE TEMPORARY TABLE t0 ( c22 TEXT , c57 BOOL ) ;
INSERT INTO t0 VALUES ( -101 , -87 ) , ( -95 , 59 ) ;
CREATE INDEX i0 ON t0 ( c22 ) ;
Warnings:
Note 1071 Specified key was too long; max key length is 1000 bytes
INSERT INTO t0 ( c57 ) SELECT 48 AS c48 ;
SELECT t4 . c16 AS c37 FROM ( WITH t1 AS ( SELECT 2743131056066857905 AS c24 , MIN( 3762089500126409270 ) AS c62 ) SELECT c22 = ( SELECT t0 . c22 AS c20 FROM ( SELECT t1 . c62 AS c27 FROM t1 LEFT OUTER JOIN t0 AS t2 ON TRUE HAVING RAND ( ) IS NOT UNKNOWN ) AS t3 JOIN t0 ON t0 . c22 = t3 . c27 EXCEPT SELECT c22 + -94 AS c36 FROM t0 WHERE c57 IN ( SELECT c22 AS c25 FROM t0 ) LIMIT 1 ) AS c16 FROM t0 ) AS t4 JOIN t0 ON t0 . c22 = t0 . c22 ;
c37
NULL
NULL
NULL
NULL
NULL
NULL
CREATE TABLE t1 ( a TEXT, b bool, index (a)) ;
Warnings:
Note 1071 Specified key was too long; max key length is 1000 bytes
INSERT INTO t1 VALUES ('-101',-87),('-95',59),(NULL,48);
SELECT
(SELECT 1 FROM (SELECT 1 HAVING rand() ) dt1
union
SELECT a FROM t1 WHERE b IN (SELECT a FROM t1) LIMIT 1)
FROM t1;
SELECT
(SELECT 1 FROM (SELECT 1 HAVING TRUE ) dt1
union
SELECT a FROM t1 WHERE b IN (SELECT a FROM t1) LIMIT 1)
FROM t1;
(SELECT 1 FROM (SELECT 1 HAVING TRUE ) dt1
union
SELECT a FROM t1 WHERE b IN (SELECT a FROM t1) LIMIT 1)
1
1
1
DROP TABLE t1;
CREATE TABLE t1 ( a TEXT, b bool) ;
INSERT INTO t1 VALUES ('-101',-87),('-95',59),(NULL,48);
SELECT
(SELECT 1 FROM (SELECT 1 HAVING rand() ) dt1
union
SELECT a FROM t1 WHERE b IN (SELECT a FROM t1) LIMIT 1)
FROM t1;
SELECT
(SELECT 1 FROM (SELECT 1 HAVING TRUE ) dt1
union
SELECT a FROM t1 WHERE b IN (SELECT a FROM t1) LIMIT 1)
FROM t1;
(SELECT 1 FROM (SELECT 1 HAVING TRUE ) dt1
union
SELECT a FROM t1 WHERE b IN (SELECT a FROM t1) LIMIT 1)
1
1
1
DROP TABLE t1;
CREATE TABLE t1 ( a int, b bool, index (a)) ;
INSERT INTO t1 VALUES ('-101',-87),('-95',59),(NULL,48);
SELECT
(SELECT 1 FROM (SELECT 1 HAVING rand() ) dt1
union
SELECT a FROM t1 WHERE b IN (SELECT a FROM t1) LIMIT 1)
FROM t1;
SELECT
(SELECT 1 FROM (SELECT 1 HAVING TRUE ) dt1
union
SELECT a FROM t1 WHERE b IN (SELECT a FROM t1) LIMIT 1)
FROM t1;
(SELECT 1 FROM (SELECT 1 HAVING TRUE ) dt1
union
SELECT a FROM t1 WHERE b IN (SELECT a FROM t1) LIMIT 1)
1
1
1
DROP TABLE t1;
93 changes: 93 additions & 0 deletions mysql-test/main/subselect_like_exists.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#
# MDEV-32397 join_read_first, keyread SEGV crash
#

CREATE TABLE t0 ( c38 DOUBLE ( 87 , 18 ) ) ;
INSERT INTO t0 VALUES ( DEFAULT ) , ( DEFAULT ) ;
CREATE INDEX i0 ON t0 ( c38 ) ;
INSERT INTO t0 VALUES ( 34 ) , ( -14 ) ;
SELECT t0 . c38 AS c5 FROM ( SELECT c38 AS c27 FROM t0 ) AS t1 JOIN t0 ON t0 . c38 >= LN ( c27 IS TRUE ) LIKE EXISTS ( SELECT SPACE ( c24 ) - UNHEX ( c38 ) + STD( ORD ( -26 ) & RADIANS ( 62 ) / TRIM( LEADING 110 FROM 'a4~mq;A825}$&%(C
{!}

.!
{S&pATQJb%F}

qU@UH?VHFyU4%))Gx' ) AND -19.704435 | ACOS ( -69 IS TRUE ) ) % + RAND ( CONVERT ( -75 , UNSIGNED ) IN ( -64 , 10 , -118 ) XOR -85 = MAKE_SET ( 11 , ROUND ( -89 , -20 ) SOUNDS LIKE TRIM( TRAILING FROM 34 ) AND RAND ( ) ) ) AS c17 FROM ( SELECT t2 . c38 AS c24 FROM t0 LEFT OUTER JOIN t0 AS t2 USING ( c38 ) ) AS t3 JOIN t0 ON t3 . c24 = t0 . c38 GROUP BY c38 , c24 UNION SELECT c38 + 35 AS c48 FROM t0 WHERE c38 IN ( SELECT c38 AS c12 FROM t0 ) ) ;


CREATE TABLE t1 ( a double, key (a)) ;
INSERT INTO t1 VALUES (1),(2),(-3);
# We disable the result log because RAND() is unpredictable and seeding RAND
# doesn't make it stable when using the PS protocol.
--disable_result_log
SELECT t1.a FROM ( SELECT a AS a1 FROM t1 ) dt
JOIN t1 ON a1 LIKE EXISTS ( SELECT a + RAND () FROM t1 UNION SELECT a FROM t1) ;
--enable_result_log

drop table t0, t1;

#
# MDEV-32403 test_if_quick_select: Segv
#
CREATE TEMPORARY TABLE t0 ( c22 TEXT , c57 BOOL ) ;
INSERT INTO t0 VALUES ( -101 , -87 ) , ( -95 , 59 ) ;
CREATE INDEX i0 ON t0 ( c22 ) ;
INSERT INTO t0 ( c57 ) SELECT 48 AS c48 ;
SELECT t4 . c16 AS c37 FROM ( WITH t1 AS ( SELECT 2743131056066857905 AS c24 , MIN( 3762089500126409270 ) AS c62 ) SELECT c22 = ( SELECT t0 . c22 AS c20 FROM ( SELECT t1 . c62 AS c27 FROM t1 LEFT OUTER JOIN t0 AS t2 ON TRUE HAVING RAND ( ) IS NOT UNKNOWN ) AS t3 JOIN t0 ON t0 . c22 = t3 . c27 EXCEPT SELECT c22 + -94 AS c36 FROM t0 WHERE c57 IN ( SELECT c22 AS c25 FROM t0 ) LIMIT 1 ) AS c16 FROM t0 ) AS t4 JOIN t0 ON t0 . c22 = t0 . c22 ;

CREATE TABLE t1 ( a TEXT, b bool, index (a)) ;
INSERT INTO t1 VALUES ('-101',-87),('-95',59),(NULL,48);
# We disable the result log because RAND() is unpredictable and seeding RAND
# doesn't make it stable when using the PS protocol.
--disable_result_log
SELECT
(SELECT 1 FROM (SELECT 1 HAVING rand() ) dt1
union
SELECT a FROM t1 WHERE b IN (SELECT a FROM t1) LIMIT 1)
FROM t1;
--enable_result_log
# Another example without rand() to show what the expected result is.
SELECT
(SELECT 1 FROM (SELECT 1 HAVING TRUE ) dt1
union
SELECT a FROM t1 WHERE b IN (SELECT a FROM t1) LIMIT 1)
FROM t1;
DROP TABLE t1;

CREATE TABLE t1 ( a TEXT, b bool) ;
INSERT INTO t1 VALUES ('-101',-87),('-95',59),(NULL,48);
# We disable the result log because RAND() is unpredictable and seeding RAND
# doesn't make it stable when using the PS protocol.
--disable_result_log
SELECT
(SELECT 1 FROM (SELECT 1 HAVING rand() ) dt1
union
SELECT a FROM t1 WHERE b IN (SELECT a FROM t1) LIMIT 1)
FROM t1;
--enable_result_log
# Another example without rand() to show what the expected result is.
SELECT
(SELECT 1 FROM (SELECT 1 HAVING TRUE ) dt1
union
SELECT a FROM t1 WHERE b IN (SELECT a FROM t1) LIMIT 1)
FROM t1;
DROP TABLE t1;

CREATE TABLE t1 ( a int, b bool, index (a)) ;
INSERT INTO t1 VALUES ('-101',-87),('-95',59),(NULL,48);
# We disable the result log because RAND() is unpredictable and seeding RAND
# doesn't make it stable when using the PS protocol.
--disable_result_log
SELECT
(SELECT 1 FROM (SELECT 1 HAVING rand() ) dt1
union
SELECT a FROM t1 WHERE b IN (SELECT a FROM t1) LIMIT 1)
FROM t1;
--enable_result_log
# Another example without rand() to show what the expected result is.
SELECT
(SELECT 1 FROM (SELECT 1 HAVING TRUE ) dt1
union
SELECT a FROM t1 WHERE b IN (SELECT a FROM t1) LIMIT 1)
FROM t1;
DROP TABLE t1;

0 comments on commit 50c275e

Please sign in to comment.