Skip to content

feat(sql-utilities,query-engine): add SQL ORDER BY and LIMIT support#364

Merged
milindsrivastava1997 merged 4 commits into
mainfrom
363-featsql-utilities-add-sql-order-by-and-limit-support
May 25, 2026
Merged

feat(sql-utilities,query-engine): add SQL ORDER BY and LIMIT support#364
milindsrivastava1997 merged 4 commits into
mainfrom
363-featsql-utilities-add-sql-order-by-and-limit-support

Conversation

@akanksha-akkihal

Copy link
Copy Markdown
Collaborator

Summary

Add parser and post-processing support for SQL ORDER BY and LIMIT on supported grouped aggregate queries.

Queries such as:

SELECT g1, g2, agg(v) AS p99
FROM t
GROUP BY g1, g2
ORDER BY p99 DESC
LIMIT 10

now parse and execute successfully where they previously fell through to:

destination=none_unsupported

Changes

  • Capture:

    • ORDER BY items
    • LIMIT N
    • aggregate aliases

during SQL parsing and store them on SQLQueryData.

  • Apply sorting and truncation after execute_query_pipeline() returns.

  • Add a new SqlPostProcessing side-channel returned from build_sql_query_pieces() alongside QueryExecutionContext:

    • keeps the shared execution context unchanged
    • avoids impact to PromQL and Elastic paths
  • matches_sql_pattern() now ignores:

    • ORDER BY
    • LIMIT
    • aggregate aliases

so existing registered templates continue matching queries that add presentation-only clauses.

  • SqlPostProcessing::apply() short-circuits when no ordering or limit is present, making the fast path effectively zero-cost.

Validation Rules

ORDER BY items must reference either:

  • the aggregate alias
  • a GROUP BY column

Unknown identifiers are rejected at parse time to avoid silently returning unsorted results for typo'd columns.

Supported

  • single or multi-column ORDER BY
  • mixed ASC / DESC
  • ordering by aggregate alias
  • ordering by GROUP BY columns
  • LIMIT N

Still Unsupported

The following remain rejected:

  • positional ORDER BY
  • expressions in ORDER BY
  • OFFSET
  • LIMIT BY
  • MySQL LIMIT a, b
  • WITH FILL
  • NULLS FIRST/LAST
  • ClickHouse ORDER BY ALL

Tests Added

Added 16 tests covering:

  • aggregate alias ordering with LIMIT

  • default ascending order

  • multi-column mixed directions

  • ordering by group-by keys

  • limit-only queries

  • tie-breaking behavior

  • NaN handling

  • matches_sql_pattern() compatibility

  • no-op fast path

  • end-to-end SqlPostProcessing::apply

  • rejection of:

    • unknown columns
    • expressions
    • OFFSET

Comment thread asap-query-engine/src/engines/simple_engine/sql.rs Outdated
@milindsrivastava1997 milindsrivastava1997 merged commit f51cec2 into main May 25, 2026
10 checks passed
@milindsrivastava1997 milindsrivastava1997 deleted the 363-featsql-utilities-add-sql-order-by-and-limit-support branch May 25, 2026 15: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.

feat(sql-utilities, query-engine): add SQL ORDER BY and LIMIT support for grouped aggregate queries.

2 participants