[DO NOT MERGE] fix: return documents in insertion order for queries without ORDER BY#73
Draft
McNultyyy wants to merge 2 commits into
Draft
[DO NOT MERGE] fix: return documents in insertion order for queries without ORDER BY#73McNultyyy wants to merge 2 commits into
McNultyyy wants to merge 2 commits into
Conversation
Queries without ORDER BY now return documents in insertion order, matching real Cosmos DB behavior. Previously documents were returned in hash-map order due to ConcurrentDictionary enumeration. Added insertion-order tracking list to InMemoryContainer that maintains document position across create, replace, upsert, and delete operations. GetAllItemsForPartition() now iterates this ordered list instead of the dictionary directly. Fixes #72 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comprehensive edge case tests covering: - Large batch (55 docs) insertion order - Multi-partition interleaved insertion order - Repeated replaces preserving position - Mixed operations sequence (create/delete/replace/upsert) - SELECT projections maintaining order - WHERE filter preserving relative order - TOP and OFFSET/LIMIT pagination order - Empty container and single document edge cases - Delete all + recreate ordering - Upsert-only creates ordering - DISTINCT VALUE preserving first occurrence order - COUNT and SUM aggregates not crashing - Cross-partition vs single-partition order - Rapid sequential creates order - ORDER BY overriding insertion order - Range filter preserving relative order - Multiple non-consecutive deletes preserving remaining order Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
McNultyyy
added a commit
that referenced
this pull request
May 20, 2026
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.
Summary
Fixes #72 — Queries without
ORDER BYnow return documents in insertion order, matching real Cosmos DB behavior.Problem
InMemoryContainer._itemsis aConcurrentDictionarywhich does not guarantee enumeration order.GetAllItemsForPartition()iterated this dictionary directly, causing documents to be returned in hash-bucket order rather than insertion order.Solution
Added an
_insertionOrderlist (List<(string Id, string PartitionKey)>) protected by a lock that tracks the order in which documents are inserted. Key behavioral rules:GetAllItemsForPartition()now snapshots this ordered list and iterates it instead of the dictionary, ensuring queries withoutORDER BYreturn documents in insertion order.All mutation sites are updated: typed and stream variants of Create/Upsert/Delete/Replace, bulk delete by partition key, TTL eviction, state import/export, snapshot restore, and point-in-time restore.
Behavioral Source
Observed behavior on the Windows Cosmos DB Emulator (priority 6): documents returned by queries without ORDER BY are in insertion order.
Testing
Version
Bumped 4.0.17 → 4.0.18