Bug #117155 drived_merge optimization error
Submitted: 9 Jan 2:29 Modified: 9 Jan 4:47
Reporter: gang chen (OCA) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S3 (Non-critical)
Version:8.0.32, 8.0.40 OS:Any
Assigned to: CPU Architecture:Any

[9 Jan 2:29] gang chen
Description:
When nondeterministicfunctions are used in derived tables, the drived_merge configuration will result in incorrect query results

but user variables is fixed (#104918)

How to repeat:
with a as (
select sysdate() as val
from (values row(1)) a)
select 
a.val,sleep(1),a.val
from a;

with a as (
select uuid() as val
from (values row(1)) a)
select 
a.val,a.val
from a;

with a as (
select rand() as val
from (values row(1)) a)
select 
a.val,a.val
from a;

The two val values in the above query are not equal

but after adding set_var hint, the result is correct.
like this:
with a as (
select uuid() as val
from (values row(1)) a)
select  /*+ set_var(optimizer_switch='derived_merge=off')*/
a.val,a.val
from a;

I think the official should prevent drived_merge when non deterministic functions are included in the derived table, rather than requiring developers to write hints

Suggested fix:
Do not automatically merge when there are nondeterministicfunctions in the derived table
[9 Jan 4:47] MySQL Verification Team
Hello gang chen,

Thank you for the report and test case.

regards,
Umesh