Skip to content

fix: MapComparator overflow causes SQL stat sort to fail#6628

Open
daguimu wants to merge 1 commit intoalibaba:masterfrom
daguimu:fix/sql-stat-sort-descending-6624
Open

fix: MapComparator overflow causes SQL stat sort to fail#6628
daguimu wants to merge 1 commit intoalibaba:masterfrom
daguimu:fix/sql-stat-sort-descending-6624

Conversation

@daguimu
Copy link
Copy Markdown

@daguimu daguimu commented Mar 25, 2026

Problem

SQL monitoring page sort doesn't work — clicking column headers toggles the ▲▼ icons but the data order doesn't change. Descending sort has no effect.

Root cause: MapComparator uses (int)(longA - longB) for Long/Date/Number comparisons. When values are large (which is common for cumulative SQL stats like ExecuteCount and TotalTime), the subtraction overflows int range, producing wrong comparison results that violate the Comparator contract.

Example: (int)(3000000000L - 1L) = -1294967297 (negative! should be positive)

This causes Collections.sort to produce unpredictable ordering that doesn't change between ascending and descending mode — exactly matching the reported behavior.

Fix

Replace all 3 overflow-prone comparisons with safe alternatives:

Before (overflow) After (safe)
(int)(long1 - long2) Long.compare(long1, long2)
(int)(double1 - double2) Double.compare(double1, double2)
(int)(date1.getTime() - date2.getTime()) Long.compare(date1.getTime(), date2.getTime())

The Double.compare fix also resolves a precision bug where small differences (e.g., 100.5 - 100.0 = 0.5) were truncated to 0 by the (int) cast, treating close values as equal.

Tests Added

7 new test cases in MapComparatorOverflowTest.java:

  • test_long_sort_ascending_large_values — ASC sort with Long.MAX_VALUE range
  • test_long_sort_descending_large_values — DESC sort with Long.MAX_VALUE range
  • test_long_sort_desc_differs_from_asc — verifies DESC produces reverse of ASC
  • test_long_overflow_values — specific overflow case: 3B vs 1
  • test_date_sort_large_time_difference — Date comparison with epoch vs now
  • test_number_small_difference_precision — 100.5 vs 100.0 precision
  • test_random_long_sort_consistency — 100 random Longs, no TimSort contract violation

All 4 existing MapComparatorTest tests continue to pass.

Impact

Fixes SQL statement monitoring page sort for all stat columns (ExecuteCount, TotalTime, MaxTimespan, etc.). The fix is minimal (3 line changes) with no behavioral change for values within int range.

Fixes #6624

The Long/Date/Number comparisons in MapComparator used (int)(a - b)
which overflows for large values, violating the Comparator contract.
This causes Collections.sort to produce unpredictable results that
don't change between ascending and descending, making the SQL
monitoring page sort appear broken.

Replace with Long.compare/Double.compare which handle all value
ranges correctly.

Fixes alibaba#6624

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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.

[BUG] sql语句监控页面,按条件排序,倒序无效

1 participant