Simplify sort-clause de-duplication and document single-key invariant#1263
Open
jwils wants to merge 1 commit into
Open
Simplify sort-clause de-duplication and document single-key invariant#1263jwils wants to merge 1 commit into
jwils wants to merge 1 commit into
Conversation
`remove_duplicate_sort_clauses` used `clause.keys.all? { |key| seen_fields.add?(key) }`,
which relied on `Set#add?`'s side effect inside `all?`. That conflated filtering
with mutation and would have left some keys unrecorded for a multi-key clause due to
`all?`'s short-circuiting. Sort clauses are in fact always single-key (enforced upstream
by `DecodedCursor::Factory.from_sort_list`, which raises `InvalidSortFieldsError` for
anything else), so the multi-key branch was both unreachable and subtly wrong.
Simplified to operate directly on the single key and added a comment documenting the
invariant, per the project's 'avoid defensive code for impossible cases' guideline.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
DatastoreQuery#remove_duplicate_sort_clausesused:This relied on
Set#add?'s side effect insideall?, conflating filtering with mutation. For a multi-key clause,all?short-circuits as soon as a previously-seen key is encountered, leaving the remaining keys of that clause unrecorded inseen_fields— a subtle correctness bug.In practice this branch is unreachable: sort clauses are always single-key, enforced upstream by
DecodedCursor::Factory.from_sort_list, which raisesErrors::InvalidSortFieldsError("Each must be a flat hash with one entry") for anything else.Changes
seen_fields.add?(clause.keys.first).add?correctly drops duplicates.This follows the project guideline to avoid defensive code for impossible cases rather than adding multi-key handling for a scenario that can't occur.
Testing