Skip to content

Cosmos: Add cross-partition vector queries - #5130

Open
Simon Moreno (simorenoh) wants to merge 7 commits into
mainfrom
simorenoh-cosmos-vector-query-validation
Open

Cosmos: Add cross-partition vector queries#5130
Simon Moreno (simorenoh) wants to merge 7 commits into
mainfrom
simorenoh-cosmos-vector-query-validation

Conversation

@simorenoh

@simorenoh Simon Moreno (simorenoh) commented Aug 25, 2026

Copy link
Copy Markdown
Member

Adds finite-window, cross-partition ORDER BY VectorDistance(...) execution to the Cosmos DB Rust driver. Plans marked hasNonStreamingOrderBy previously failed because the SDK could not combine per-partition results into a global ranking.

Design

NonStreamingOrderedMerge drains the Gateway/native provider's rewritten query across every target range and retains the best K rows in a max-heap, where K is TOP or OFFSET + LIMIT. Routing trusts authoritative query-plan metadata rather than reparsing SQL, matching .NET, Python, Java, and JavaScript. The merge supports plan-provided sort-key tuples and preserves stable service arrival order for ties.

Query shape Behavior
Finite non-streaming TOP or OFFSET/LIMIT ordering supported
Indexed/brute-force vector distance, filters, and bound parameters supported
Unbounded, resumed, DISTINCT, aggregate, GROUP BY, or hybrid plans rejected
Continuation creation or resume rejected

The requested window is fully buffered before the first result page. The SDK imposes no additional item ceiling; callers control memory through the finite window and projected row size. max_item_count controls emitted pages after the merge, not the buffered window.

Native query planning always requests the formattable, non-continuation shape used by .NET. The driver replaces the Gateway's {documentdb-formattableorderbyquery-filter} placeholder before execution. Request charges, session tokens, and diagnostics retain the existing page-aggregation behavior; complete cross-page query/index metrics are tracked separately in #5170.

Validation

The live suite covers single- and cross-partition vector queries with filters, bound vectors, indexed and brute-force execution, TOP, OFFSET/LIMIT, small output pages, and duplicate/missing-row detection. It also validates Euclidean, Cosine, and DotProduct ranking against nine valid 300-dimensional embeddings adapted from .NET's NonStreamingOrderByQueryTests; the malformed 279-dimensional year_NOUN fixture is documented and omitted.

Add live-only coverage for parameterized vector distance queries scoped to one logical partition. Document raw SQL usage and verify float-vector serialization.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Add a bounded non-streaming merge pipeline for finite pure-vector ORDER BY queries across Cosmos partitions. Reject unsupported continuation, hybrid, unbounded, and oversized query shapes before fan-out.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 26e6962d-c1cb-4663-955f-f3427bcfe2b7
Copilot AI balanced review requested due to automatic review settings August 25, 2026 22:02
@simorenoh
Simon Moreno (simorenoh) requested a review from a team as a code owner August 25, 2026 22:02
@simorenoh Simon Moreno (simorenoh) added the Cosmos The azure_cosmos crate label Aug 25, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
3 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Adds bounded cross-partition vector query execution to the Cosmos SDK and driver.

Changes:

  • Adds SDK and driver buffer-limit configuration APIs.
  • Implements non-streaming vector ordering, pagination, metrics aggregation, and typed errors.
  • Adds unit, documentation, and live-account coverage.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
azure_data_cosmos/tests/emulator_tests/mod.rs Registers vector tests.
azure_data_cosmos/tests/emulator_tests/cosmos_vector_query.rs Tests vector query scenarios.
azure_data_cosmos/src/options/feed.rs Exposes buffer-limit options.
azure_data_cosmos/src/feed/query.rs Documents vector parameters.
azure_data_cosmos/src/clients/container_client.rs Documents cross-partition vectors.
azure_data_cosmos/CHANGELOG.md Records SDK support.
azure_data_cosmos_driver/src/query/mod.rs Classifies vector query shapes.
azure_data_cosmos_driver/src/query_plan_native/mod.rs Requests formattable rewrites.
azure_data_cosmos_driver/src/options/plan_options.rs Defines driver buffer limits.
azure_data_cosmos_driver/src/options/mod.rs Exports new options.
azure_data_cosmos_driver/src/error/cosmos_status.rs Adds typed statuses.
azure_data_cosmos_driver/src/driver/dataflow/query_response.rs Aggregates response metadata.
azure_data_cosmos_driver/src/driver/dataflow/planner.rs Builds buffered merge plans.
azure_data_cosmos_driver/src/driver/dataflow/non_streaming_ordered_merge.rs Implements bounded ordered merge.
azure_data_cosmos_driver/src/driver/dataflow/mod.rs Registers the merge stage.
azure_data_cosmos_driver/src/driver/cosmos_driver.rs Routes non-streaming plans.
azure_data_cosmos_driver/CHANGELOG.md Records driver support.

💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.

Comment thread sdk/cosmos/azure_data_cosmos_driver/src/query/mod.rs Outdated
Comment thread sdk/cosmos/azure_data_cosmos_driver/src/driver/dataflow/query_response.rs Outdated
Port the valid 300-dimensional .NET vector corpus into live cross-partition coverage for all supported distance functions and both indexed and brute-force execution.

Remove the unreleased public buffer-ceiling option while retaining mandatory finite TOP or OFFSET/LIMIT windows.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 26e6962d-c1cb-4663-955f-f3427bcfe2b7
Cap retained query and index metrics by entry count and byte size, and mark truncated output explicitly so buffered fan-out diagnostics cannot grow with backend page count.

Make the vector parameter doctest compare against the serialized f32 source values, avoiding architecture-independent precision mismatches.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 26e6962d-c1cb-4663-955f-f3427bcfe2b7
Keep the vector-search PR focused by restoring PageAggregator query and index metrics handling exactly to main. Cross-page metrics aggregation and synthetic truncation markers belong in a separate design change.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 26e6962d-c1cb-4663-955f-f3427bcfe2b7
@simorenoh

Copy link
Copy Markdown
Member Author

/azp run rust - cosmos - weekly

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@simorenoh Simon Moreno (simorenoh) changed the title Add cross-partition vector queries Cosmos: Add cross-partition vector queries Aug 26, 2026
@simorenoh
Simon Moreno (simorenoh) marked this pull request as ready for review August 27, 2026 14:13
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
3 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Comment thread sdk/cosmos/azure_data_cosmos/src/clients/container_client.rs Outdated
Comment thread sdk/cosmos/azure_data_cosmos_driver/src/driver/dataflow/planner.rs Outdated
Extract comparator-driven binary heap operations for the streaming and buffered ordered merges so both pipelines share the same mechanics while retaining their runtime ordering rules.

Update query_items documentation to describe the current cross-partition query surface and distinguish finite buffered vector ordering from streaming features.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 26e6962d-c1cb-4663-955f-f3427bcfe2b7
Route buffered ORDER BY queries from authoritative plan metadata instead of reparsing SQL locally. Match peer SDK native plan options while retaining the existing finite-window and unsupported-feature guards.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 26e6962d-c1cb-4663-955f-f3427bcfe2b7
@simorenoh

Copy link
Copy Markdown
Member Author

/azp run rust - cosmos - weekly

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

//! Comparator-driven binary-heap operations used by ordered merge stages.

/// Pushes `item` while preserving the heap defined by `precedes`.
pub(crate) fn push_by<T>(heap: &mut Vec<T>, item: T, precedes: impl Fn(&T, &T) -> bool) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could we use std::collections::BinaryHeap instead of a full custom implementation


TestClient::run_with_unique_db(
async |run_context, db_client| {
let container = seed_vector_container(run_context, db_client).await?;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

from what I can tell this is creating a container with one partition, so it is not exercising the cross partition portion. Same with this test precomputed_pure_vector_search

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Cosmos The azure_cosmos crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants