Skip to content

Refinement keeps the view's written order_by - #3136

Merged
mtoy-googly-moogly merged 1 commit into
mainfrom
refinement-keeps-ordering
Oct 7, 2026
Merged

mtoy-googly-moogly merged 1 commit into
mainfrom
refinement-keeps-ordering

Conversation

@mtoy-googly-moogly

Copy link
Copy Markdown
Collaborator

view + { limit: N } threw away the view's order_by, so the query silently returned a different top N, ordered by the default instead. We overlooked this in #2022: when top: N by was removed, the guard in refineFrom() that keeps the view's ordering changed from "unless the refinement has its own order_by" to "unless it has its own limit". Wrong since 0.0.218.

Refinement is computed two ways, chosen by how the right side is written, and this also fixes an ordering bug in the other one. Since the two are easy to confuse, here is how x + y works:

  • y inline (x + { ... }): a query builder re-enters x's fields and runs y's statements as though they were written inside x. When both set limit: or order_by:, y wins. finalize() then computes a default ordering if none was written.
  • y named (x + w): refine() in refine-utils.ts merges two finished segments. When both set limit:, or both have a written order_by:, it is an error (WN-0006 Lenses #1444, Lenses), which keeps combining named views independent of their order. A named view must have fields, so a bare limit: or order_by: can only be inline; in practice inline means "refine this view" and named means "combine pieces".

A segment's orderBy is either written by the author or computed by the translator and marked defaultOrderBy (#2022). Both paths must carry a written ordering forward and recompute a computed one. The named path copied a written ordering over a computed one but left the flag set, so a later inline refinement of the result threw it away.

The named path also never recomputes a computed ordering for the merged fields. That is #3135; the test showing it is checked in skipped.

An inline refinement that sets only a limit dropped the view's order_by,
and a named-view refinement left a written ordering marked as a computed
default, so a later inline refinement dropped it.
@mtoy-googly-moogly
mtoy-googly-moogly merged commit b90c216 into main Oct 7, 2026
21 checks passed
@mtoy-googly-moogly
mtoy-googly-moogly deleted the refinement-keeps-ordering branch October 7, 2026 23:59
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.

1 participant