Skip to content

Conversation

agourlay
Copy link
Member

@agourlay agourlay commented Apr 30, 2025

This PR implements fully the strict mode for search_allow_exact to make sure no search query can be issued against a disabled HNSW index.

The check takes into account the presence of prefetches to still allow the rescoring use case anyway.

Copy link
Contributor

coderabbitai bot commented Apr 30, 2025

📝 Walkthrough

Walkthrough

This change updates the strict mode verification logic for queries in the collection module. The check_strict_mode method on the Query object is converted from a synchronous to an asynchronous function. A new asynchronous method, check_fullscan, is introduced to ensure that fullscan queries are restricted when strict mode disallows unindexed retrieval, specifically for vector queries. The method reads the collection configuration asynchronously, skips sparse vectors, and inspects the HNSW indexing parameter m. If m is zero (indicating vector indexing is disabled), it returns an error forbidding fullscan unless vector indexing is enabled or a prefetch query is used.

Implementations of StrictModeVerification for CollectionQueryRequest, CollectionPrefetch, and CollectionQueryGroupsRequest are updated to await the asynchronous check_strict_mode method. Additionally, check_fullscan is invoked asynchronously in relevant query verification flows depending on the presence of prefetch queries. A new test, test_strict_mode_full_scan, is added to verify strict mode enforcement on fullscan queries, particularly when HNSW indexing is disabled, ensuring correct error handling and behavior under strict mode.

Possibly related PRs

  • [strict mode | score boosting] Strict mode for formula queries #6317: The main PR extends the existing asynchronous strict mode verification in Query by adding fullscan checks for vector queries and integrates these checks into query and prefetch flows, while the retrieved PR focuses on adding strict mode checks for unindexed fields in formula expressions and refactoring related modules; both modify strict mode verification but target different aspects (fullscan vector queries vs. formula expressions) and different methods, so their changes are related but address distinct strict mode concerns.

📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6962b12 and 8e4f6ca.

📒 Files selected for processing (2)
  • lib/collection/src/operations/verification/query.rs (5 hunks)
  • tests/openapi/test_strictmode.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/openapi/test_strictmode.py
  • lib/collection/src/operations/verification/query.rs
⏰ Context from checks skipped due to timeout of 90000ms (13)
  • GitHub Check: test-snapshot-operations-s3-minio
  • GitHub Check: test-shard-snapshot-api-s3-minio
  • GitHub Check: test-low-resources
  • GitHub Check: test-consistency
  • GitHub Check: Basic TLS/HTTPS tests
  • GitHub Check: test-consensus-compose
  • GitHub Check: rust-tests (macos-latest)
  • GitHub Check: integration-tests-consensus
  • GitHub Check: rust-tests (windows-latest)
  • GitHub Check: integration-tests
  • GitHub Check: storage-compat-test
  • GitHub Check: rust-tests (ubuntu-latest)
  • GitHub Check: lint
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (3)
lib/collection/src/operations/verification/query.rs (2)

91-94: Redundant Box::pin around already-pinned future

prefetch.check_strict_mode(...) returns a Pin<Box<…>> (per the trait).
Wrapping it again with Box::pin allocates a second box and needlessly complicates the type:

-            Box::pin(prefetch.check_strict_mode(collection, strict_mode_config)).await?;
+            prefetch.check_strict_mode(collection, strict_mode_config).await?;

Same issue appears in the CollectionPrefetch implementation.


72-77: Error message could expose little context – include the vector name

The current message:

Fullscan forbidden on '{using}'

is helpful but omits why (index disabled). Consider:

return Err(CollectionError::strict_mode(
    format!("Fullscan forbidden on '{using}' – vector indexing is disabled (m = 0 or payload_m = 0)"),
    "Enable vector indexing or use a prefetch query before rescoring",
));

This makes troubleshooting easier, especially when multiple vectors are configured.

tests/openapi/test_strictmode.py (1)

1813-1864: Test is brittle – relies on exact error string & skips status-code check

The assertions check the full, hard-coded error message. Any wording change will break the test although behaviour is still correct.

-    assert not response.ok
-    assert "Fullscan forbidden on 'dense-multi'. Help: Enable vector indexing or use a prefetch query before rescoring" in response.json()['status']['error']
+    assert response.status_code == 400  # or the exact code your API returns
+    assert "Fullscan forbidden" in response.json()['status']['error']
+    assert "dense-multi" in response.json()['status']['error']

Using substrings and explicit status codes makes the test less fragile while still validating the contract.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d177fd5 and 49d62dc.

📒 Files selected for processing (2)
  • lib/collection/src/operations/verification/query.rs (5 hunks)
  • tests/openapi/test_strictmode.py (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
tests/openapi/test_strictmode.py (2)
tests/openapi/conftest.py (1)
  • collection_name (50-51)
tests/openapi/helpers/helpers.py (1)
  • request_with_validation (41-116)
⏰ Context from checks skipped due to timeout of 90000ms (13)
  • GitHub Check: test-snapshot-operations-s3-minio
  • GitHub Check: test-shard-snapshot-api-s3-minio
  • GitHub Check: test-low-resources
  • GitHub Check: test-consensus-compose
  • GitHub Check: Basic TLS/HTTPS tests
  • GitHub Check: rust-tests (macos-latest)
  • GitHub Check: test-consistency
  • GitHub Check: rust-tests (windows-latest)
  • GitHub Check: integration-tests-consensus
  • GitHub Check: rust-tests (ubuntu-latest)
  • GitHub Check: integration-tests
  • GitHub Check: storage-compat-test
  • GitHub Check: lint
🔇 Additional comments (1)
lib/collection/src/operations/verification/query.rs (1)

48-61:

❌ Incorrect review comment

Full-scan guard ignores ID-based & multivector queries

check_fullscan short-circuits unless the variant is Query::Vector(_).
However, full scans are equally possible when the query is:

  • a point-ID query that falls back to an un-indexed scan, or
  • a multivector / hybrid variant.

The new test uses an ID ("query": 2) and therefore relies on other logic to catch the violation. For defence-in-depth (and future-proofing), consider extending the match arm or adding a separate check for those variants.


We need to inspect the full check_fullscan implementation to see which Query variants it handles. Let’s dump the entire function body:


🏁 Script executed:

#!/bin/bash
rg -n "fn check_fullscan" -A200 lib/collection/src/operations/verification/query.rs

Length of output: 6041


🏁 Script executed:

#!/bin/bash
# Locate the Query enum definition to inspect its variants
rg -n "enum Query" -A30 lib/collection/src/operations/verification/query.rs

Length of output: 77


🏁 Script executed:

#!/bin/bash
# Display the top of the file to locate the Query enum definition and its variants
sed -n '1,200p' lib/collection/src/operations/verification/query.rs

Length of output: 6642


🏁 Script executed:

#!/bin/bash
# Locate and display the Query enum definition with its variants
file_path=$(fd --full-path "collection_query.rs" lib/collection/src/operations/universal_query)
echo "Using file: $file_path"
sed -n '1,200p' "$file_path"

Length of output: 7602


Ignore full-scan guard suggestion for ID-based or multivector queries

The Query::Vector(_) arm already handles both pure vectors and ID-based lookups, since VectorInputInternal can be an Id. There is no separate “ID” or “hybrid” variant to match, and other Query variants (e.g. Fusion, Formula, OrderBy, Sample) do not perform vector full-scans. No change required.

Likely an incorrect or invalid review comment.

strict_mode_config: &StrictModeConfig,
) -> CollectionResult<()> {
// Check only applies on `unindexed_filtering_retrieve`
if strict_mode_config.unindexed_filtering_retrieve == Some(false) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit but maybe we can early return here to reduce amount of indents.

Copy link
Member Author

Choose a reason for hiding this comment

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

I prefer keeping it this to make the scope clearer when add new checks in the custom checker.

// Check only applies on `unindexed_filtering_retrieve`
if strict_mode_config.unindexed_filtering_retrieve == Some(false) {
if let Query::Vector(_) = &self {
let config = collection.collection_config.read().await;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe a bit too cautious but are you sure that this block call is not too slow for getting called in every request?
I don't know if there is a proper workaround though.

Copy link
Member Author

Choose a reason for hiding this comment

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

In my experience the async lock on the collection configuration is not an issue.

collection: &Collection,
strict_mode_config: &StrictModeConfig,
) -> CollectionResult<()> {
// Check only applies on `unindexed_filtering_retrieve`
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't it be search_allow_exact instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch!
You are right, got confused with my ongoing work on group by filtering 🤦

) -> CollectionResult<()> {
// Check only applies on `unindexed_filtering_retrieve`
if strict_mode_config.unindexed_filtering_retrieve == Some(false) {
if let Query::Vector(_) = &self {
Copy link
Member

Choose a reason for hiding this comment

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

Let's use match here, explicitly handling each case

.and_then(|param| param.hnsw_config.as_ref());

let vector_hnsw_m = vector_hnsw_config.and_then(|hnsw| hnsw.m);
let vector_hnsw_payload_m = vector_hnsw_config.and_then(|hnsw| hnsw.payload_m);
Copy link
Member

Choose a reason for hiding this comment

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

Payload_m is only an excuse if there is a filter by tenant/principal

Copy link
Member Author

Choose a reason for hiding this comment

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

Got it, removing it from the check and will tackle multitenancy in a different PR.

// check for unindexed fields in formula
query.check_strict_mode(collection, strict_mode_config)?
query
.check_strict_mode(collection, strict_mode_config)
Copy link
Member

Choose a reason for hiding this comment

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

Function naming in this case becomes confusing.
check_fullscan is followed by check_strict_mode. Isn't check_fullscan part of strict mode?

Copy link
Member Author

Choose a reason for hiding this comment

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

check_strict_mode on the Query does not have the knowledge whether there are prefetches.
This information is necessary to know if it is a rescoring use case.

For CollectionPrefetch it should always be checked.
But for CollectionQueryRequest only if there are no prefetches.

This is why the calls are not unified.

Copy link
Member

@generall generall left a comment

Choose a reason for hiding this comment

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

Please address commends

@agourlay agourlay requested a review from generall May 2, 2025 11:00
@agourlay agourlay force-pushed the strict-mode-fullscan branch from 8e63b77 to 6962b12 Compare May 5, 2025 12:35
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
lib/collection/src/operations/verification/query.rs (1)

39-85: Well-structured implementation of fullscan prevention in strict mode.

This new method correctly implements the strict mode check for fullscan queries when search_allow_exact is set to false. The method:

  1. Only applies the check when appropriate (line 47)
  2. Uses pattern matching for different query types (line 48)
  3. Properly handles sparse vectors which are always indexed (lines 53-62)
  4. Correctly identifies disabled vector indexing via the HNSW m parameter (lines 64-73)
  5. Provides a helpful error message with a clear solution (lines 74-79)

The TODO comment on line 72 could be more descriptive about what the payload_m check will entail and its purpose in the multi-tenancy context.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8e63b77 and 6962b12.

📒 Files selected for processing (2)
  • lib/collection/src/operations/verification/query.rs (5 hunks)
  • tests/openapi/test_strictmode.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/openapi/test_strictmode.py
🧰 Additional context used
🧬 Code Graph Analysis (1)
lib/collection/src/operations/verification/query.rs (6)
lib/collection/src/operations/verification/discovery.rs (1)
  • check_strict_mode (30-42)
lib/collection/src/operations/verification/search.rs (1)
  • check_strict_mode (53-65)
lib/collection/src/operations/verification/mod.rs (1)
  • check_strict_mode (150-162)
src/common/update.rs (1)
  • check_strict_mode (125-172)
lib/collection/src/collection/collection_ops.rs (1)
  • strict_mode_config (277-283)
lib/collection/src/operations/types.rs (1)
  • strict_mode (1120-1123)
🔇 Additional comments (4)
lib/collection/src/operations/verification/query.rs (4)

11-37: The conversion to async is correct and well-implemented.

The method has been properly converted from synchronous to asynchronous. This change is essential for allowing the collection configuration to be read asynchronously later in the check_fullscan method. The existing logic for checking unindexed expressions remains intact.


96-112: Correct implementation of strict mode checks for CollectionQueryRequest.

The implementation properly:

  1. Awaits the async check_strict_mode for prefetches
  2. Only applies the fullscan check when not in a rescoring scenario (empty prefetch)
  3. Correctly calls both checks on the query object

This approach aligns with the PR objective of allowing rescoring use cases to proceed despite strict mode restrictions.


150-157: Appropriate strict mode checks for CollectionPrefetch.

For prefetch queries, the implementation correctly:

  1. Always checks if the prefetch can perform a fullscan (since prefetches are the first query in a rescoring pipeline)
  2. Properly awaits the async checks
  3. Maintains the existing check for unindexed fields in the formula

191-200: Consistent implementation for CollectionQueryGroupsRequest.

The implementation correctly:

  1. Only applies the fullscan check when not rescoring (empty prefetch)
  2. Properly awaits the async checks
  3. Maintains the check for unindexed fields in the formula

This implementation is consistent with the approach used for CollectionQueryRequest, which is good for code maintainability and understanding.

@agourlay agourlay force-pushed the strict-mode-fullscan branch from 6962b12 to 8e4f6ca Compare May 5, 2025 12:54
@agourlay
Copy link
Member Author

agourlay commented May 5, 2025

More review is necessary here 🙏

.and_then(|param| param.hnsw_config.as_ref());

let vector_hnsw_m = vector_hnsw_config.and_then(|hnsw| hnsw.m);
// TODO(strict-mode) check also payload_m if if there is a filter by tenant/principal
Copy link
Member

Choose a reason for hiding this comment

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

Just FYI: we can't do releases without this todo implemented. Otherwise it is too restrictive

@agourlay agourlay merged commit 1bdf1f3 into dev May 5, 2025
17 checks passed
@agourlay agourlay deleted the strict-mode-fullscan branch May 5, 2025 13: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.

3 participants