Skip to content

implement missing validation for sparse and milti-vectors in grpc - #6274

Merged
generall merged 1 commit into
devfrom
fix-upsert-vector-validation-in-grpc
Mar 31, 2025
Merged

generall merged 1 commit into
devfrom
fix-upsert-vector-validation-in-grpc

Conversation

@generall

Copy link
Copy Markdown
Member

No description provided.

@generall
generall requested a review from agourlay March 29, 2025 15:52
@coderabbitai

coderabbitai Bot commented Mar 29, 2025

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The changes update and extend validation across several modules. In the gRPC module, the vector validation logic is enhanced by incorporating new validations for an optional field and adding implementations of the Validate trait for several vector types. In the common validation module, the original multi-vector validation function is split into two: one that now validates based on a slice of vector lengths and a wrapper that collects lengths from a vector slice. A debug assertion in the vector storage module now includes a custom error message for unsorted vectors, providing clearer debugging information. Additionally, test cases have been added to the test suite to verify both valid and invalid sparse vector configurations in the API payload, ensuring proper behavior of the collection creation process.

✨ 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.

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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 1

🧹 Nitpick comments (4)
tests/basic_grpc_test.sh (1)

95-117: Valid sparse vector insertion test is effective.

It’s good to confirm that proper data passes validation. As a potential improvement, consider adding assertions on the server’s response status or the stored data to further validate correctness.

lib/api/src/grpc/validate.rs (3)

238-249: Ensure comprehensive validation when both vector and data/indices fields are present.

The optional vector field is validated if provided, and you correctly handle indices/vectors_count exclusivity. However, consider clarifying whether data is expected when vector is set, to avoid partial/ambiguous definitions.


269-280: Variant-level validations appear concise but consider specific checks.

For variants like Document or Image, you might enforce format rules or size limits. Even if they pass by default, consider adding more checks if they have known constraints.


282-295: Handle potential invalid inputs more explicitly in SparseVector and MultiDenseVector.

While validate_sparse_vector_impl and validate_multi_vector_by_length are robust, consider checks for negative or out-of-order indices, and issuance of user-friendly error messages if encountered.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between f23a992 and 9214f22.

📒 Files selected for processing (4)
  • lib/api/src/grpc/validate.rs (2 hunks)
  • lib/common/common/src/validation.rs (5 hunks)
  • lib/segment/src/vector_storage/sparse/mmap_sparse_vector_storage.rs (1 hunks)
  • tests/basic_grpc_test.sh (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (13)
  • GitHub Check: Basic TLS/HTTPS tests
  • 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: test (macos-latest)
  • GitHub Check: test-consensus-compose
  • GitHub Check: test-consensus
  • GitHub Check: test (windows-latest)
  • GitHub Check: test
  • GitHub Check: test
  • GitHub Check: test (ubuntu-latest)
  • GitHub Check: test
🔇 Additional comments (6)
tests/basic_grpc_test.sh (2)

30-38: Configuration for on-disk sparse vector indexing looks good.

Including "sparse_vectors_config" allows indexing sparse vectors on disk, which is helpful for large datasets. Ensure you’ve confirmed the performance and storage trade-offs of on-disk indexing for your use case.


68-93: Great job testing the invalid sparse vector scenario.

This negative test verifying a validation error helps ensure the API rejects malformed data. The grep -q "Validation error in body" check is a reliable pattern for CI validation.

lib/api/src/grpc/validate.rs (1)

258-262:

Details

❓ Verification inconclusive

Request verification of sparse-vector length mismatch scenarios.

When using indices alongside data, ensure no length mismatch can slip through. You might want to confirm that all relevant checks exist upstream or in validate_sparse_vector_impl.


🏁 Script executed:

#!/bin/bash
# This script checks for references to validate_sparse_vector_impl usage to ensure length mismatch is thoroughly validated.
rg "validate_sparse_vector_impl" -A 5

Length of output: 4494


Sparse Vector Length Validation Consistency

I verified that all invocations of validate_sparse_vector_impl—including the one in this GRPC validation case—are consistent with its usage elsewhere in the codebase. Please confirm that its implementation in lib/sparse/src/common/sparse_vector.rs explicitly checks for length mismatches between the provided indices and data so that no inconsistent sparse vector scenarios can slip through.

lib/common/common/src/validation.rs (3)

157-157: Function has been refactored to work with vector lengths directly

The function has been renamed from validate_multi_vector to validate_multi_vector_by_length, and its signature has changed to accept a slice of vector lengths rather than a slice of vectors. This is a good separation of concerns that allows validation to operate on lengths directly.


159-159: Implementation correctly updated to work with vector lengths

The validation logic has been properly adjusted to operate on lengths instead of the actual vectors:

  • Checking for empty input by verifying if multivec_length is empty
  • Validating that no individual length is zero
  • Computing the total size by summing the lengths
  • Ensuring consistent dimensions by comparing all lengths with the first one
  • Error message formatting updated to reflect these changes

This refactoring preserves the original validation logic while making it more flexible.

Also applies to: 168-168, 177-177, 187-188, 194-194


204-207: Good addition of a backward-compatible wrapper function

A new wrapper function validate_multi_vector has been added that maintains the original function signature. This function simply collects the lengths from the input vectors and delegates to validate_multi_vector_by_length, ensuring backward compatibility while benefiting from the refactored implementation.

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.

2 participants