Skip to content
/ server Public

MDEV-38819 Logic Inconsistency between Direct HAVING Query and Derived Table Relocation#4815

Open
OmarGamal10 wants to merge 1 commit intoMariaDB:10.11from
OmarGamal10:mdev-38819
Open

MDEV-38819 Logic Inconsistency between Direct HAVING Query and Derived Table Relocation#4815
OmarGamal10 wants to merge 1 commit intoMariaDB:10.11from
OmarGamal10:mdev-38819

Conversation

@OmarGamal10
Copy link

The inconsistency is caused because the dynamic range scan wrongly attempts to read from an unpopulated temporary table, fails to inject the correct value from the outer table to the inner for the dynamic scan, resulting in a TREE::IMPOSSIBLE in execution time and giving 0 rows.

Tracing through the execution showed that manually modifying the injected value to the inner table led to correct behavior, so the fix was as simple as an if condition in execution time, checking if the temp_table is empty and getting the original value instead, however this scales linearly with the number of rows since it's in execution.

Further investigation showed the root cause lies in the Optimizer's HAVING clause pushdown. When pushing conditions down to the WHERE clause, the optimizer simply shares pointers to the original Item fields. Later in the optimization phase, these original fields are mutated (their result_field is set to point to the empty temp_table, possibly to collect final results). The solution is to copy the fields instead into the pushed down condition, so that they don't undergo the mutation and thus, inject the correct values in the range scan of the inner table.

…d Table Relocation

This fix restores consistency between the two logically equivalent
queries by deep cloning the condition tree before pushing it down from
HAVING into WHERE. The issue was caused by the original condition being
shared between different optimizer and execution passes.
@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Mar 17, 2026
Copy link
Member

@gkodinov gkodinov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your contribution! This is a preliminary review.

LGTM. Stay tuned for the final review please.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

2 participants