Skip to content

Conversation

@akcube
Copy link

@akcube akcube commented Nov 22, 2025

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 NULL as "constants" during ORDER BY simplification. While an equality predicate (e.g., col = 5) allows the optimizer to skip sorting on that column, col IS NULL which functionally also guarantees a single value for sorting purposes is ignored.

This limitation forces the engine to perform unnecessary filesort operations even when a covering index exists that could satisfy the sort order if the IS NULL column were treated as a constant.

What?
This change updates the optimizer's check_field_is_const() logic to recognize Item_func::ISNULL_FUNC as a valid constant condition. By treating IS NULL as a constant, the optimizer can now rely on the index order for subsequent columns in the ORDER BY clause, eliminating filesort in these scenarios.

How?

  1. Extended check_field_is_const: Modified this function in sql/sql_select.cc to inspect Item_func::ISNULL_FUNC alongside the existing equality checks.
  2. Sentinel Tracking: Since IS NULL does not have a literal "value" like an integer or string, the IS NULL function pointer itself is stored as a sentinel in const_item to track state across recursive calls (e.g., inside OR conditions).
  3. Conflict Resolution: Logic to ensure logical consistency within OR branches:
    • ... AND (col IS NULL OR col IS NULL) -> Recognized as Constant.
    • ... AND (col IS NULL OR col = 5) -> Recognized as Non-Constant
  4. Helper Function: Added is_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:

  • Basic Covering Index: Verified that WHERE col IS NULL ORDER BY col, next_col uses the index and removes Using filesort.
  • Compound Indices: Verified that intermediate index columns filtered by IS NULL allow the index to provide ordering for subsequent columns.
  • OR Conditions: Tested consistency checks to ensure IS NULL OR IS NULL is optimized, while IS NULL OR col=1 is correctly treated as non-constant.
  • Ordering Semantics: Verified that NULL sorting order (first in ASC, last in DESC) is preserved.
  • Optimizer Trace: Verified via INFORMATION_SCHEMA.OPTIMIZER_TRACE that the column is now flagged with "equals_constant_in_where": true.

Existing Regression Tests
Updated mysql-test/r/order_by_all.result and mysql-test/r/order_by_none.result. These tests previously expected Using filesort for IS NULL queries; they have been updated to reflect the improved execution plan (Using index).

PROBLEM:

1. The optimizer's check_field_is_const() function only recognized
   equality operators (col = value) as constants for ORDER BY
   simplification.
2. It failed to recognize that "col IS NULL" also guarantees a
   single value (NULL) for sorting purposes.
3. This caused unnecessary filesort operations even when covering
   indexes existed.
4. Example: WHERE a = 1 AND b IS NULL ORDER BY a, b, c would use
   filesort despite having index (a, b, c).

FIX:

1. Extended check_field_is_const() to handle Item_func::ISNULL_FUNC.
2. Added is_isnull_func() helper function to identify IS NULL
   conditions.
3. Properly handles OR conditions - "IS NULL OR value" remains
   non-constant, while "IS NULL OR IS NULL" is constant.
4. Stores IS NULL function as sentinel in const_item to ensure
   consistency across OR branches.
5. Preserves correct NULL ordering semantics (NULLs first in ASC,
   last in DESC).
@akcube akcube force-pushed the fix/bug-119442-order-by-isnull-optimization branch from 87df393 to c7206b4 Compare November 24, 2025 02:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant