Bug #119305 The query results of enabling and disabling materialization are inconsistent.
Submitted: 5 Nov 2025 4:02 Modified: 16 Dec 2025 13:54
Reporter: Alice Alice Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:8.0.41 OS:Linux
Assigned to: CPU Architecture:x86

[5 Nov 2025 4:02] Alice Alice
Description:
mysql> set optimizer_switch="materialization=on";
2 left join alltypecol_t0 as ref_3 on ((ref_3.mediumtext_col is NULL)) where (ref_3.mediumblob_col is not NULL) ))  group by 1,2 order by 1,2 desc;Query OK, 0 rows affected (0.00 sec)

mysql> select ref_1.C_STREET_1 as c0, ref_1.C_SINCE as c1 from t0_range_key_subpartition as ref_0 left join t12 as ref_1 on (EXISTS ( select ref_3.varbinary_col as c0, ref_2.c_d_id as c1 from t0_hash_partition_p1 as ref_2 left join alltypecol_t0 as ref_3 on ((ref_3.mediumtext_col is NULL)) where (ref_3.mediumblob_col is not NULL) ))  group by 1,2 order by 1,2 desc;
+------+---------------------+
| c0   | c1                  |
+------+---------------------+
| NULL | 2020-11-05 09:29:49 |
| NULL | 2020-11-05 09:29:48 |
+------+---------------------+
2 rows in set (0.02 sec)

mysql> set optimizer_switch="materialization=on";
Query OK, 0 rows affected (0.00 sec)

mysql> select ref_1.C_STREET_1 as c0, ref_1.C_SINCE as c1 from t0_range_key_subpartition as ref_0 left join t12 as ref_1 on (EXISTS ( select ref_3.varbinary_col as c0, ref_2.c_d_id as c1 from t0_hash_partition_p1 as ref_2 left join alltypecol_t0 as ref_3 on ((ref_3.mediumtext_col is NULL)) where (ref_3.mediumblob_col is not NULL) ))  group by 1,2 order by 1,2 desc;
+------+---------------------+
| c0   | c1                  |
+------+---------------------+
| NULL | 2020-11-05 09:29:49 |
| NULL | 2020-11-05 09:29:48 |
+------+---------------------+
2 rows in set (0.02 sec)

mysql> set optimizer_switch="materialization=off";
Query OK, 0 rows affected (0.00 sec)

mysql> select ref_1.C_STREET_1 as c0, ref_1.C_SINCE as c1 from t0_range_key_subpartition as ref_0 left join t12 as ref_1 on (EXISTS ( select ref_3.varbinary_col as c0, ref_2.c_d_id as c1 from t0_hash_partition_p1 as ref_2 left join alltypecol_t0 as ref_3 on ((ref_3.mediumtext_col is NULL)) where (ref_3.mediumblob_col is not NULL) ))  group by 1,2 order by 1,2 desc;
+------+------+
| c0   | c1   |
+------+------+
| NULL | NULL |
+------+------+
1 row in set (0.43 sec)

How to repeat:
create database test;
use test
source test_table.sql
set optimizer_switch="materialization=on";
select ref_1.C_STREET_1 as c0, ref_1.C_SINCE as c1 from t0_range_key_subpartition as ref_0 left join t12 as ref_1 on (EXISTS ( select ref_3.varbinary_col as c0, ref_2.c_d_id as c1 from t0_hash_partition_p1 as ref_2 left join alltypecol_t0 as ref_3 on ((ref_3.mediumtext_col is NULL)) where (ref_3.mediumblob_col is not NULL) ))  group by 1,2 order by 1,2 desc;
set optimizer_switch="materialization=off";
select ref_1.C_STREET_1 as c0, ref_1.C_SINCE as c1 from t0_range_key_subpartition as ref_0 left join t12 as ref_1 on (EXISTS ( select ref_3.varbinary_col as c0, ref_2.c_d_id as c1 from t0_hash_partition_p1 as ref_2 left join alltypecol_t0 as ref_3 on ((ref_3.mediumtext_col is NULL)) where (ref_3.mediumblob_col is not NULL) ))  group by 1,2 order by 1,2 desc;
[16 Dec 2025 13:54] Knut Anders Hatlen
Thanks for the bug report. I could reproduce the bug after I changed the storage engine type from Dstore to InnoDB.

This smaller repro seems to show the same issue:

CREATE TABLE t0 (x INT);
INSERT INTO t0 VALUES (0), (1);

SET optimizer_switch='materialization=on';
SELECT * FROM t0 LEFT JOIN t0 AS t1 ON EXISTS (SELECT * FROM t0 AS t2 JOIN t0 AS t3 ON t3.x IS NULL WHERE t3.x IS NOT NULL);

SET optimizer_switch='materialization=off';
SELECT * FROM t0 LEFT JOIN t0 AS t1 ON EXISTS (SELECT * FROM t0 AS t2 JOIN t0 AS t3 ON t3.x IS NULL WHERE t3.x IS NOT NULL);

With materialization on, it returns:

+------+------+
| x    | x    |
+------+------+
|    0 |    0 |
|    0 |    1 |
|    1 |    0 |
|    1 |    1 |
+------+------+

With materialization off, it returns:

+------+------+
| x    | x    |
+------+------+
|    0 | NULL |
|    1 | NULL |
+------+------+