Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MDEV-33436 Transform OUTER JOIN to INNER JOIN in subqueries used with IN operator #3071

Open
wants to merge 2 commits into
base: 11.4
Choose a base branch
from

Conversation

Olernov
Copy link
Contributor

@Olernov Olernov commented Feb 16, 2024

  • The Jira issue number for this PR is: MDEV-_33436
MDEV-33436 Transform OUTER JOIN to INNER JOIN in subqueries used with IN operator

When there is a query in the form of
  SELECT a FROM t1 WHERE (a, b) IN
    (SELECT <item1>, <item2> FROM t2 LEFT JOIN t3 ON t2.b = t3.b)
it is obvious that the IN operator will yield FALSE when <item1> or
<item2> is evaluated to NULL. This can occur due to the case of
NULL-complemented rows in the table "t3" for the LEFT JOIN.

Item::not_null_tables() returns a map of tables which will cause
the Item be evaluated to NULL when those tables are NULL-complemented
in an OUTER JOIN. So <item1>::not_null_tables() and
<item2>::not_null_tables() may or may not contain the table "t3".
If at least one of the items contains "t3" in its not_null_tables map,
the LEFT JOIN can be transformed to INNER JOIN.

Currently, the algorithm of transforming OUTER JOIN to INNER JOIN
only collects not_null_tables for conditions and ON expressions.
However, it can benefit from cases as the one illustrated above.
This commit extends the algorithm by taking into account not_null_tables
for the items from the select list of subqueries used with IN operator

Release Notes

TODO: What should the release notes say about this change?
Include any changed system variables, status variables or behaviour. Optionally list any https://mariadb.com/kb/ pages that need changing.

How can this PR be tested?

TODO: modify the automated test suite to verify that the PR causes MariaDB to behave as intended.
Consult the documentation on "Writing good test cases".

If the changes are not amenable to automated testing, please explain why not and carefully describe how to test manually.

Basing the PR against the correct MariaDB version

  • This is a new feature and the PR is based against the latest MariaDB development branch.
  • This is a bug fix and the PR is based against the earliest maintained branch in which the bug can be reproduced.

PR quality check

  • I checked the CODING_STANDARDS.md file and my PR conforms to this where appropriate.
  • For any trivial modifications to the PR, I am ok with the reviewer making the changes themselves.

… IN operator

When there is a query in the form of
  SELECT a FROM t1 WHERE (a, b) IN
    (SELECT <item1>, <item2> FROM t2 LEFT JOIN t3 ON t2.b = t3.b)
it is obvious that the IN operator will yield FALSE when <item1> or
<item2> is evaluated to NULL. This can occur due to the case of
NULL-complemented rows in the table "t3" for the LEFT JOIN.

Item::not_null_tables() returns a map of tables which will cause
the Item be evaluated to NULL when those tables are NULL-complemented
in an OUTER JOIN. So <item1>::not_null_tables() and
<item2>::not_null_tables() may or may not contain the table "t3".
If at least one of the items contains "t3" in its not_null_tables map,
the LEFT JOIN can be transformed to INNER JOIN.

Currently, the algorithm of transforming OUTER JOIN to INNER JOIN
only collects not_null_tables for conditions and ON expressions.
However, it can benefit from cases as the one illustrated above.
This commit extends the algorithm by taking into account not_null_tables
for the items from the select list of subqueries used with IN operator
… IN operator

This commit adds logging of the transformations made to joins
to the optimizer trace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
1 participant