-
Notifications
You must be signed in to change notification settings - Fork 3
fix(sql-parser): preserve SQL top-k secondary ordering semantics #682
Description
Parent
What to build
Prevent SQL heap top-k detection from changing the meaning of queries with additional ORDER BY terms. The current detector validates only the primary aggregate ordering, while heap top-k execution bypasses normal SQL post-processing.
Either reject unsupported secondary ordering during top-k detection or implement the complete ordering behavior, including ties.
Acceptance criteria
- Queries with unsupported secondary ORDER BY terms are rejected or handled correctly.
- Aggregate descending ordering remains supported.
- Tie ordering is deterministic and matches the documented SQL behavior.
- Tests cover secondary ordering, ties, and LIMIT interaction.
Blocked by
None - can start immediately.
Concrete example
This query has a meaningful secondary ordering:
SELECT srcip, SUM(bytes) AS total FROM netflow GROUP BY srcip ORDER BY total DESC, srcip ASC LIMIT 2;
The detector currently accepts it because the first ordering item is total DESC. The execution path then skips normal SQL post-processing, so ties are ordered by the heap/result implementation rather than by the requested srcip ASC.
A minimal fix could reject any top-k query where order_by.len() > 1; a broader fix would carry all ordering terms through execution.