Skip to content

Validate filter and queryVector parameter in $vectorSearch stage builder#2857

Merged
GromNaN merged 1 commit intodoctrine:2.13.xfrom
GromNaN:validate-binary-vector
Oct 16, 2025
Merged

Validate filter and queryVector parameter in $vectorSearch stage builder#2857
GromNaN merged 1 commit intodoctrine:2.13.xfrom
GromNaN:validate-binary-vector

Conversation

@GromNaN
Copy link
Member

@GromNaN GromNaN commented Oct 15, 2025

Q A
Type improvement
BC Break no
Fixed issues -

Summary

  • Give early feedback when an invalid value is set as queryVector.
  • Allow using an array for the filter option.

@GromNaN GromNaN requested review from Copilot and paulinevos October 15, 2025 12:56
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds validation to the $vectorSearch stage builder to provide early feedback for invalid parameters and extends the filter option to accept arrays in addition to expressions.

  • Adds validation for queryVector parameter to ensure Binary types use the correct vector type and arrays are lists rather than associative arrays
  • Extends the filter method to accept both arrays and Expr objects instead of only Expr objects
  • Adds comprehensive test coverage for the new validation logic

Reviewed Changes

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

File Description
lib/Doctrine/ODM/MongoDB/Aggregation/Stage/VectorSearch.php Implements validation logic for queryVector and extends filter method to accept arrays
tests/Doctrine/ODM/MongoDB/Tests/Aggregation/Stage/VectorSearchTest.php Adds test cases for array filter usage and queryVector validation
phpstan-baseline.neon Adds PHPStan baseline entry for the new mixed array type in filter property

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@GromNaN GromNaN force-pushed the validate-binary-vector branch from f8a8073 to 83b7a0e Compare October 15, 2025 13:05
@GromNaN GromNaN force-pushed the validate-binary-vector branch from 83b7a0e to 99ba67a Compare October 15, 2025 13:06
@GromNaN GromNaN requested a review from jmikola October 15, 2025 15:50
@GromNaN GromNaN added this to the 2.13.0 milestone Oct 15, 2025

#[TestWith([new Binary("\x03\x00\x01\x02\x03", Binary::TYPE_GENERIC), 'Binary query vector must be of type 9 (Vector), got 0.'])]
#[TestWith([[1 => 1, 2 => 3], 'Query vector must be a list of numbers, got an associative array.'])]
#[TestWith([[], 'Query vector cannot be an empty array.'])]
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh cool, I had not seen these attributes before.

Copy link
Member Author

Choose a reason for hiding this comment

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

Before attributes, it was the @testWith annotation but the syntax was awful.

Copy link
Member

@jmikola jmikola left a comment

Choose a reason for hiding this comment

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

A couple of questions which might lead to some small changes, but defer to you.

private ?int $numCandidates = null;
private ?string $path = null;
/** @see Binary::TYPE_VECTOR introduced in ext-mongodb 2.2 */
private const BINARY_TYPE_VECTOR = 9;
Copy link
Member

Choose a reason for hiding this comment

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

Just to confirm, will this stick around even after the ext-mongodb 2.2.0 release? I assume you don't want to raise the driver dependency just for this feature.

Copy link
Member Author

Choose a reason for hiding this comment

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

Exactly, we don't want to update the required version for this feature. But someone could receive a binary vector from the server and use in a query. That's why we have this constant duplicated here.

}

if (is_array($queryVector) && ! array_is_list($queryVector)) {
throw new InvalidArgumentException('Query vector must be a list of numbers, got an associative array.');
Copy link
Member

Choose a reason for hiding this comment

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

Do you actually intend to validate the contents of the list, or will you rely on the server to report that error?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this is a partial validation. I don't know what vector type is expected so iterating over the values would just cost time.

@GromNaN GromNaN merged commit d820746 into doctrine:2.13.x Oct 16, 2025
23 checks passed
@GromNaN GromNaN deleted the validate-binary-vector branch October 16, 2025 13:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants