Skip to content

[fix](show) apply ORDER BY and LIMIT correctly to SHOW TABLETS (#65871) - #66116

Open
SudharsanK2308 wants to merge 5 commits into
apache:masterfrom
SudharsanK2308:fix/issue-65871-SHOW-TABLE-ORDER-LIMIT
Open

[fix](show) apply ORDER BY and LIMIT correctly to SHOW TABLETS (#65871)#66116
SudharsanK2308 wants to merge 5 commits into
apache:masterfrom
SudharsanK2308:fix/issue-65871-SHOW-TABLE-ORDER-LIMIT

Conversation

@SudharsanK2308

Copy link
Copy Markdown

What problem does this PR solve?

Issue Number: close #65871

Related PR: #xxx

Problem Summary:

Release note

None

Check List (For Author)

  • Test

    • Regression test
    • Unit Test
    • Manual test (add detailed scripts or steps below)
    • No need to test or manual test. Explain why:
      • This is a refactor/code format and no logic has been changed.
      • Previous test can cover this change.
      • No code files have been changed.
      • Other reason
  • Behavior changed:

    • No.
    • Yes.
  • Does this need documentation?

    • No.
    • Yes.

Check List (For Reviewer who merge this PR)

  • Confirm the release note
  • Confirm test cases
  • Confirm document
  • Add branch pick label

sudharsan-k-3668 and others added 5 commits July 23, 2026 18:23
### What problem does this PR solve?

Issue Number: None

Related PR: None

Problem Summary: RecordPickerUtils used a top-K heap before returning sorted records. Replace the heap selection with a stable full lexicographic sort before truncation so mixed-direction keys such as LocalDataSize DESC and TabletId ASC are applied in sequence. Negative no-limit values continue to sort the complete result.

### Release note

Fix multi-column ordering for SHOW TABLETS results.

### Check List (For Author)

- Test: Manual test
    - Verified LocalDataSize DESC, TabletId ASC with equal-size groups and a limit
- Behavior changed: Yes, all ORDER BY keys are applied before limiting results
- Does this need documentation: No

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
### What problem does this PR solve?

Issue Number: close apache#65871

Related PR: None

Problem Summary: SHOW TABLETS represented both an absent LIMIT and LIMIT 0 as zero. The command consequently converted explicit LIMIT 0 to an unlimited request, returning all records. Preserve absent LIMIT as -1, pass it to RecordPickerUtils as Optional.empty(), retain explicit zero as Optional.of(0), and apply offset after the complete multi-column ordering.

### Release note

SHOW TABLETS now honors LIMIT 0 and applies LIMIT/OFFSET after multi-column ordering.

### Check List (For Author)

- Test: Manual test
    - Verified LIMIT 0 returns no records and unlimited LocalDataSize DESC, TabletId ASC ordering
- Behavior changed: Yes, explicit LIMIT 0 no longer behaves as unlimited
- Does this need documentation: No

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@hello-stephen

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

@SudharsanK2308

Copy link
Copy Markdown
Author

run buildall

Optional<Integer> sizeLimit = Optional.empty();
if (offset > 0 && limit > 0) {
sizeLimit = offset + limit;
sizeLimit = Optional.of((int) (offset + limit));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

offset and limit should be long. check overflow by org.apache.doris.nereids.util.Utils#addOverflows

@SudharsanK2308 SudharsanK2308 Jul 28, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@morrySnow Since it is already typecast in the original version, I preserved the same. Also,

List<List<Comparable>> tabletInfos

's size always returns int.

}
Collections.sort(tabletInfos, comparator);
if (sizeLimit > -1) {
tabletInfos = tabletInfos.subList((int) offset,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Narrowing Type Casting in the existing version

@SudharsanK2308
SudharsanK2308 requested a review from morrySnow July 28, 2026 07:07
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] ORDER BY does not work in SHOW TABLETS

4 participants