-- ============================================================================
-- MariaDB Crash Reproduction Script
-- Date: 2026-02-01
-- Error: 2013 - Lost connection to MySQL server during query
-- Seed: 1769931178 (reproducible)
-- ============================================================================

-- ============================================================================
-- STEP 1: Environment Setup
-- ============================================================================

SET SQL_MODE = CONCAT(@@sql_mode, ',NO_ENGINE_SUBSTITUTION');
USE test2;

-- ============================================================================
-- STEP 2: Create Tables
-- ============================================================================

DROP TABLE IF EXISTS mysql_1;
CREATE TABLE `mysql_1` (
    `col_int` int,
    `col_varchar` varchar(2000),
    `col_date` date,
    `col_timestamp` timestamp,
    `col_numeric` numeric,
    KEY k2 (`col_int`,`col_date`),
    KEY k1 (`col_numeric`)
);

DROP TABLE IF EXISTS mysql_2;
CREATE TABLE `mysql_2` (
    `col_int` int,
    `col_varchar` varchar(2000),
    `col_date` date,
    `col_timestamp` timestamp,
    `col_numeric` numeric,
    KEY k2 (`col_int`,`col_date`),
    KEY k1 (`col_numeric`)
);

DROP TABLE IF EXISTS mysql_3;
CREATE TABLE `mysql_3` (
    `col_int` int,
    `col_varchar` varchar(2000),
    `col_date` date,
    `col_timestamp` timestamp,
    `col_numeric` numeric,
    KEY k2 (`col_int`,`col_date`),
    KEY k1 (`col_numeric`)
);

DROP TABLE IF EXISTS mysql_4;
CREATE TABLE `mysql_4` (
    `col_int` int,
    `col_varchar` varchar(2000),
    `col_date` date,
    `col_timestamp` timestamp,
    `col_numeric` numeric,
    KEY k2 (`col_int`,`col_date`),
    KEY k1 (`col_numeric`)
);

DROP TABLE IF EXISTS mysql_5;
CREATE TABLE `mysql_5` (
    `col_int` int,
    `col_varchar` varchar(2000),
    `col_date` date,
    `col_timestamp` timestamp,
    `col_numeric` numeric,
    KEY k2 (`col_int`,`col_date`),
    KEY k1 (`col_numeric`)
);

DROP TABLE IF EXISTS mysql_6;
CREATE TABLE `mysql_6` (
    `col_int` int,
    `col_varchar` varchar(2000),
    `col_date` date,
    `col_timestamp` timestamp,
    `col_numeric` numeric,
    KEY k2 (`col_int`,`col_date`),
    KEY k1 (`col_numeric`)
);

-- ============================================================================
-- STEP 3: Insert Test Data
-- ============================================================================

-- Data for mysql_1
INSERT IGNORE INTO mysql_1 (col_int,col_varchar,col_date,col_timestamp,col_numeric) VALUES
    (b'1', 'fbpwvp', '1973-01-05 01:02:13.048742', '2016-10-07 14:59:37.016878', b'1'),
    (0, NULL, '2017-03-17', '1981-10-06', 7),
    (7, 'bp', '1998-06-15', '1976-03-07 15:11:07.046745', 9),
    (0, 'english', '2012-09-28', '2005-04-23 13:04:53.044761', 3),
    (NULL, 'pwv', '1988-09-22', '2004-06-17 17:05:24.036792', b'1'),
    (6, 'english', '1992-12-05 10:55:55.003805', '1973-06-28 18:20:09.020486', b'0'),
    (NULL, NULL, '2004-02-06 19:37:49.038374', '1998-05-06', b'1'),
    (b'1', 'd', '1996-05-02 10:46:06.047317', '2025-02-20', 4),
    (1, NULL, '2014-06-12 04:50:38.031816', '2014-01-19 02:05:48.040820', NULL),
    (b'1', 'wvpwh', '2026-09-24', '2005-06-12', NULL);

-- Data for mysql_2
INSERT IGNORE INTO mysql_2 (col_int,col_varchar,col_date,col_timestamp,col_numeric) VALUES
    (8, NULL, '2032-10-10 15:11:31.019266', '2004-02-24', 8),
    (NULL, 'vpw', '1988-08-22 21:11:35.034917', '2008-12-13', b'1'),
    (b'1', 'q', '2007-12-19 12:57:18.044537', '2008-07-18 23:27:56.040461', NULL),
    (b'1', NULL, '2019-09-02', '1994-07-02', b'0'),
    (5, 'p', '2016-03-17 03:04:32.060779', '2028-07-06', NULL),
    (b'0', 'whng', '2002-04-25 11:29:14.019118', '2000-09-13', b'0'),
    (b'1', 'hngfs', '2022-06-13', '1985-05-07 06:48:43.039843', NULL),
    (8, NULL, '1977-05-07 21:30:42.044358', '1986-08-28', NULL),
    (b'1', NULL, '2030-01-19', '1989-06-21 11:59:34.024698', 1),
    (2, 'english', '2009-01-18', '2030-04-24 11:10:46.059635', NULL);

-- Data for mysql_3
INSERT IGNORE INTO mysql_3 (col_int,col_varchar,col_date,col_timestamp,col_numeric) VALUES
    (4, 'ngf', '2016-11-21', '2002-09-25', b'0'),
    (b'1', 'english', '2016-10-16 00:20:14.005989', '1998-06-05', 0),
    (b'1', 'english', '1993-04-08', '1997-04-22', 7),
    (9, 'english', '1991-10-02', '2030-01-25 20:36:29.057171', b'0'),
    (1, 'r', '1979-03-05 19:51:28.007175', '2018-11-12', NULL),
    (b'0', 'g', '2035-06-17', '1984-07-17', 7),
    (b'0', 'h', '2021-02-21 03:17:43.034676', '1988-02-12 12:27:13.032858', NULL),
    (b'0', 'english', '2005-09-12', '1979-03-16', b'0'),
    (b'1', 'fsg', '1982-09-20', '2004-04-01 20:40:15.050137', 6),
    (1, 'sg', '1971-11-12', '1974-04-23 01:09:31.007231', NULL);

-- Data for mysql_4
INSERT IGNORE INTO mysql_4 (col_int,col_varchar,col_date,col_timestamp,col_numeric) VALUES
    (7, 'y', '2021-09-23', '2002-10-12', 5),
    (NULL, 'b', '2023-01-02', '2024-08-19 23:08:28.045492', 5),
    (b'0', 'english', '1977-01-23', '2006-11-04', 2),
    (3, 'j', '2015-04-03', '2005-09-14', 4),
    (1, NULL, '2008-11-13', '2028-03-18', b'0'),
    (1, 'f', '1999-02-12 10:21:24.049762', '1993-05-02 11:44:00.065149', NULL),
    (4, 'english', '1972-03-22 01:53:18.033362', '2028-10-04', 4),
    (7, 'i', '2031-02-01', '1998-09-19', 4),
    (2, 'r', '2009-02-25', '2016-10-26 05:33:20.039875', NULL),
    (3, NULL, '2001-11-12 04:46:55.056336', '1985-07-15 16:28:38.013945', NULL);

-- Data for mysql_5
INSERT IGNORE INTO mysql_5 (col_int,col_varchar,col_date,col_timestamp,col_numeric) VALUES
    (b'1', 'gwslnl', '2004-12-10', '1976-05-17 15:37:40.034430', NULL),
    (NULL, 'j', '1980-10-17 23:17:14.042699', '1988-12-02 17:18:39.049653', b'1'),
    (5, 'b', '2009-07-11', '1983-07-14 07:35:17.005167', 3),
    (2, 'd', '1997-04-17', '2024-07-02 18:18:38.049242', b'0'),
    (4, NULL, '1998-02-01', '2020-09-05', NULL),
    (2, NULL, '1976-03-08 03:02:01.064985', '1988-04-24 15:30:11.004013', 4),
    (6, 'h', '1988-10-10', '2024-10-20', 0),
    (6, 'english', '2014-11-13 18:04:16.029187', '2010-10-21', b'0'),
    (8, 'w', '1993-04-16 03:09:07.026324', '1976-08-05 23:51:26.004257', 9),
    (6, 'english', '1986-12-01', '2024-01-14', b'0');

-- Data for mysql_6
INSERT IGNORE INTO mysql_6 (col_int,col_varchar,col_date,col_timestamp,col_numeric) VALUES
    (NULL, NULL, '1990-01-20 14:27:48.028705', '2017-07-07 20:11:39.021096', b'1'),
    (6, 'sln', '1983-12-03', '1984-07-02', b'1'),
    (NULL, NULL, '2034-01-02', '2033-09-27', 0),
    (7, 'english', '2015-12-01 02:20:54.036463', '1992-09-15', 1),
    (NULL, NULL, '2015-06-07 21:01:34.058173', '2035-11-12 03:57:17.047420', NULL),
    (NULL, 'x', '1975-08-21', '2033-09-03 08:22:30.035803', 6),
    (5, 'english', '2001-03-25 22:45:41.003458', '2025-02-26', 5),
    (b'0', 'g', '1983-07-08', '1982-05-19', 6),
    (NULL, 'english', '1971-07-19 03:23:07.019233', '2008-03-06 09:19:55.011540', 2),
    (NULL, NULL, '1978-09-06 06:29:12.020976', '1982-03-17 09:39:49.059409', 2);

COMMIT;

-- ============================================================================
-- STEP 4: Execute Crash-Inducing Query
-- WARNING: This query will crash the MariaDB server!
-- ============================================================================

SELECT t11.*
FROM mysql_4 t11
STRAIGHT_JOIN mysql_1 t12 ON '2016-01-10' < '2012-11-01'
INNER JOIN mysql_1 t13 ON 803930112 < 362610688
CROSS JOIN mysql_1 t14 ON 0 < CASE WHEN FALSE THEN t13.col_numeric ELSE 0 END
RIGHT JOIN mysql_5 t15 ON CASE WHEN FALSE THEN NULLIF(0, 0) ELSE 0 END IS NULL

EXCEPT

SELECT t11.*
FROM mysql_6 t11
STRAIGHT_JOIN mysql_5 t12 ON t12.col_date < '1976-07-28'
INNER JOIN mysql_5 t13 ON t13.col_int < t12.col_int
CROSS JOIN mysql_1 t14 ON cast((LEAST(0, 0)) as decimal) < 0
RIGHT JOIN mysql_2 t15 ON unix_timestamp() IS NULL

INTERSECT

SELECT t11.*
FROM mysql_4 t11
STRAIGHT_JOIN mysql_3 t12 ON t12.col_date < '1979-08-18'
INNER JOIN mysql_3 t13 ON t12.col_int < -1717764096
CROSS JOIN mysql_5 t14 ON unix_timestamp() < 0
RIGHT JOIN mysql_1 t15 ON 0 IS NULL

INTERSECT ALL

SELECT t11.*
FROM mysql_3 t11
STRAIGHT_JOIN mysql_2 t12 ON '2011-08-03' < '2001-11-12'
INNER JOIN mysql_6 t13 ON GREATEST(LEAST(0, 10), 20) < -1596522496
CROSS JOIN mysql_2 t14 ON 0 < 0
RIGHT JOIN mysql_4 t15 ON 0 IS NULL

UNION ALL

SELECT t11.*
FROM mysql_4 t11
STRAIGHT_JOIN mysql_1 t12 ON '2019-05-19' < '2009-04-03'
INNER JOIN mysql_6 t13 ON GREATEST(LEAST(0, 10), 20) < LEAST(cast(GREATEST(LEAST(t11.col_int, 10), 20) as SIGNED), NULLIF(2140078080, 748683264))
CROSS JOIN mysql_2 t14 ON cast((LEAST(0, 0)) as decimal) < CASE WHEN FALSE THEN CASE WHEN TRUE THEN t13.col_numeric ELSE 0 END ELSE case when FALSE then NULLIF(CASE WHEN FALSE THEN t14.col_numeric ELSE 0 END, case when TRUE then 0 else t14.col_numeric end) else 0 end END
RIGHT JOIN mysql_4 t15 ON 0 IS NULL

EXCEPT ALL

SELECT t11.*
FROM mysql_2 t11
STRAIGHT_JOIN mysql_5 t12 ON '1995-07-09' < '2028-05-23'
INNER JOIN mysql_5 t13 ON -516161536 < length(substr('u', 7, 2))
CROSS JOIN mysql_4 t14 ON 0 < CASE WHEN TRUE THEN case when TRUE then NULLIF(0, rand()) else 0 end ELSE CASE WHEN FALSE THEN LEAST(NULLIF(COALESCE(unix_timestamp(), 0), NULLIF(t11.col_numeric, 0)), cast((0) as decimal)) ELSE sqrt(abs('0.551803588867188')) END END
RIGHT JOIN mysql_4 t15 ON t12.col_numeric IS NULL

ORDER BY 1
LIMIT 1;

-- ============================================================================
-- Expected Result: Error 2013 - Lost connection to MySQL server during query
-- ============================================================================
