Skip to content

Conversation

@BornChanger
Copy link
Contributor

@BornChanger BornChanger commented Feb 3, 2026

What problem does this PR solve?

Issue Number: close #xxx

What changed and how does it work?

This introduces AQSort: an adaptive sort optimized for byte-orderable keys with
long shared prefixes, inspired by US7680791B2 (“Method for sorting data using
common prefix bytes”).

Why:

  • SortExec currently relies on comparator-based sorting, which repeatedly calls
    per-type comparison functions and often re-scans shared key prefixes.
  • ORDER BY keys in TiDB are frequently byte-encodable and prefix-heavy (e.g.
    composite keys / encoded datums), making them a good fit for a
    common-prefix-aware algorithm.

What’s included:

  • New pkg/util/aqsort:

    • CPS-QS (common-prefix-skipping quicksort) + a radix partitioning step on the
      first byte after the common prefix.
    • Scratch-buffer reuse to reduce allocations.
    • Optional checkpoint callback to periodically check kill/cancel signals.
    • Introspective fallback to Go std sort when depth budget is exceeded.
    • NOTE: not stable; ordering is byte-lexicographic.
  • SortExec integration (in-memory path):

    • Encode ORDER BY keys via codec.EncodeKey per row and sort Pair{Key, Row}
      using aqsort.PairSorter.
    • DESC handling is preserved by inverting encoded key bytes for DESC columns.
    • Parallel sort workers also support AQSort and reuse an arena for encoded keys
      to avoid per-key allocations where possible.
  • User-facing enablement:

    • Add SESSION/GLOBAL sysvar tidb_enable_aqsort (default OFF) to opt into the
      AQSort execution path.
    • Keep the existing sortexec.SetAQSortEnabled toggle for benchmarking /
      experimentation; it still forces AQSort independent of the sysvar.
  • Safety / observability:

    • Safe fallback: if AQSort key encoding fails, disable AQSort for the current
      SortExec and fall back to the original comparator-based sort so the query
      still succeeds.
    • Emit a one-time WARN log on first fallback with conn_id/executor_id and
      context (rows/worker_id) to aid debugging.
    • Add failpoint AQSortForceEncodeKeyError to deterministically test fallback.

Tests:

  • Differential correctness tests ensuring AQSort and std sort produce identical
    row order for:
    • multi-type sort keys (string+collation, bytes, signed/unsigned ints, decimal,
      timestamp, duration, JSON), with NULLs and mixed ASC/DESC.
    • end-to-end SortExec (ORDER BY and window row_number sorting), including
      fallback behavior under forced encode errors.

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

Experimental: add an opt-in AQSort path for in-memory ORDER BY sorting; enable with `SET tidb_enable_aqsort=ON`.

@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-linked-issue do-not-merge/needs-tests-checked release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. labels Feb 3, 2026
@ti-chi-bot
Copy link

ti-chi-bot bot commented Feb 3, 2026

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign suzaku, yudongusa for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tiprow
Copy link

tiprow bot commented Feb 3, 2026

Hi @BornChanger. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@BornChanger BornChanger force-pushed the aqsort branch 2 times, most recently from 92f57ce to c642425 Compare February 3, 2026 15:56
Signed-off-by: BornChanger <[email protected]>
(cherry picked from commit f531a797a8400376cc4906291f3b7000a2b74294)
@ti-chi-bot
Copy link

ti-chi-bot bot commented Feb 3, 2026

[FORMAT CHECKER NOTIFICATION]

Notice: To remove the do-not-merge/needs-linked-issue label, please provide the linked issue number on one line in the PR body, for example: Issue Number: close #123 or Issue Number: ref #456.

📖 For more info, you can check the "Contribute Code" section in the development guide.

@ti-chi-bot
Copy link

ti-chi-bot bot commented Feb 3, 2026

@BornChanger: The following tests failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
idc-jenkins-ci-tidb/check_dev 3688970 link true /test check-dev
pull-build-next-gen 3688970 link true /test pull-build-next-gen
idc-jenkins-ci-tidb/build 3688970 link true /test build
pull-unit-test-next-gen 3688970 link true /test pull-unit-test-next-gen
idc-jenkins-ci-tidb/unit-test 3688970 link true /test unit-test

Full PR test history. Your PR dashboard.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do-not-merge/needs-linked-issue release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant