Skip to content

Implement ORDER BY and LIMIT/OFFSET instead of silently ignoring them#116

Merged
mweiden merged 1 commit into
mainfrom
claude/audit-fix-6-order-by-limit
Jun 9, 2026
Merged

Implement ORDER BY and LIMIT/OFFSET instead of silently ignoring them#116
mweiden merged 1 commit into
mainfrom
claude/audit-fix-6-order-by-limit

Conversation

@mweiden

@mweiden mweiden commented Jun 9, 2026

Copy link
Copy Markdown
Owner

Problem

exec_select accepted the parsed ORDER BY and LIMIT clauses but bound them to _order/_limit and never used them. SELECT ... ORDER BY x LIMIT 10 returned every matching row in unspecified order — a silent correctness violation and an unbounded result-size footgun.

Fix

New query::apply_order_and_limit applied to SELECT results:

  • ORDER BY: multi-key, ASC/DESC, numeric-aware comparison (so 2 < 10), NULLS FIRST/LAST with the SQL-standard defaults (nulls last ascending, first descending).
  • LIMIT/OFFSET: standard LIMIT n [OFFSET m] and the MySQL LIMIT m, n form; offset past the end yields empty rather than erroring.
  • Distributed reads: the cluster layer applies the clauses after merging replica meta results, so cross-node queries honor them too (per-replica truncation before the LWW merge would be incorrect).
  • Clause forms the engine cannot honor (ORDER BY arbitrary expressions, LIMIT BY, WITH FILL, INTERPOLATE) now return QueryError::Unsupported instead of being ignored.

Tests

tests/query_order_limit_test.rs: ascending/descending, numeric ordering, multi-column sort with mixed directions, LIMIT truncation incl. LIMIT 0, OFFSET paging incl. past-the-end, missing-column rows sorting last, and rejection of unsupported ORDER BY expressions.

Found by codebase audit (medium severity: silent correctness violation).

https://claude.ai/code/session_01CJXWjB9ERGgV6B9mRqrdM9


Generated by Claude Code

exec_select accepted the parsed ORDER BY and LIMIT clauses but never
used them, so 'SELECT ... ORDER BY x LIMIT 10' returned every matching
row in unspecified order - a silent correctness violation and an
unbounded result-size footgun.

SELECT results are now sorted (numeric-aware comparison, multi-key,
ASC/DESC, NULLS FIRST/LAST with SQL-standard defaults) and bounded by
LIMIT/OFFSET. Distributed reads apply the clauses after the cluster
merges replica results so cross-node queries honor them too. Clause
forms the engine cannot honor (ORDER BY arbitrary expressions,
LIMIT BY, WITH FILL, INTERPOLATE) are rejected with an Unsupported
error rather than ignored.

https://claude.ai/code/session_01CJXWjB9ERGgV6B9mRqrdM9

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e66ecc9457

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/query.rs
// Meta results are merged across replicas by the cluster layer,
// which applies ORDER BY/LIMIT after the merge.
if let QueryOutput::Rows(ref mut rows) = out {
apply_order_and_limit(rows, order.as_ref(), limit)?;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Apply ORDER BY before dropping unselected columns

When the query projects only a subset of columns and orders by a different column, this sorts the already-projected rows, so the ORDER BY key has been removed by project_row and every row compares as None. For example, SELECT ck FROM items WHERE pk = 'p' ORDER BY score will keep storage/key order instead of score order; the clustered path has the same issue because meta rows encode the projected map. Either sort before projection or reject ORDER BY columns that are not present in the result rows.

Useful? React with 👍 / 👎.

@mweiden
mweiden merged commit 13cf7f2 into main Jun 9, 2026
1 check passed
@mweiden
mweiden deleted the claude/audit-fix-6-order-by-limit branch June 9, 2026 22:12
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.

2 participants