Bug #119442: Fix ORDER BY optimization doesn't recognize IS NULL as constant #637
+534
−24
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What changes are proposed in this pull request?
Why?
This PR addresses Bug#119442. The MySQL optimizer currently fails to treat columns filtered by
IS NULLas "constants" duringORDER BYsimplification. While an equality predicate (e.g.,col = 5) allows the optimizer to skip sorting on that column,col IS NULLwhich functionally also guarantees a single value for sorting purposes is ignored.This limitation forces the engine to perform unnecessary
filesortoperations even when a covering index exists that could satisfy the sort order if theIS NULLcolumn were treated as a constant.What?
This change updates the optimizer's
check_field_is_const()logic to recognizeItem_func::ISNULL_FUNCas a valid constant condition. By treatingIS NULLas a constant, the optimizer can now rely on the index order for subsequent columns in theORDER BYclause, eliminatingfilesortin these scenarios.How?
check_field_is_const: Modified this function insql/sql_select.ccto inspectItem_func::ISNULL_FUNCalongside the existing equality checks.IS NULLdoes not have a literal "value" like an integer or string, theIS NULLfunction pointer itself is stored as a sentinel inconst_itemto track state across recursive calls (e.g., insideORconditions).ORbranches:... AND (col IS NULL OR col IS NULL)-> Recognized as Constant.... AND (col IS NULL OR col = 5)-> Recognized as Non-Constantis_isnull_func()to clean up some code.How is this tested?
New MTR Tests (
mysql-test/t/order_by_isnull.test)The suite covers the following scenarios:
WHERE col IS NULL ORDER BY col, next_coluses the index and removesUsing filesort.IS NULLallow the index to provide ordering for subsequent columns.IS NULL OR IS NULLis optimized, whileIS NULL OR col=1is correctly treated as non-constant.NULLsorting order (first inASC, last inDESC) is preserved.INFORMATION_SCHEMA.OPTIMIZER_TRACEthat the column is now flagged with"equals_constant_in_where": true.Existing Regression Tests
Updated
mysql-test/r/order_by_all.resultandmysql-test/r/order_by_none.result. These tests previously expectedUsing filesortforIS NULLqueries; they have been updated to reflect the improved execution plan (Using index).