mariadb/mysql-test/main/subselect_union_rand.test
Dave Gosselin c728e1cd56 MDEV-32397, MDEV-32403 Crashes during join processing.
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.
2025-09-22 11:48:41 -04:00

93 lines
3.8 KiB
Text

#
# 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 VARCHAR(100), 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 VARCHAR(100), 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;