Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new aggregation operators in builder #2514

Merged
merged 16 commits into from
Mar 28, 2023

Conversation

alcaeus
Copy link
Member

@alcaeus alcaeus commented Mar 22, 2023

Q A
Type feature
BC Break no
Fixed issues

Summary

This PR adds the following new aggregation pipeline operators to the builder: $accumulator, $binarySize, $bottom, $bottomN, $bsonSize, $dateAdd, $dateDiff, $dateFromParts, $dateFromString, $dateSubtract, $dateToParts, $dateTrunc, $firstN, $function, $getField, $lastsN, $maxN, $mergeObjects, $minN, $rand, $regexFind, $regexFindAll, $regexMatch, $replaceAll, $replaceOne, $sampleRate, $setField, $sortArray, $top, $topN, $tsIncrement, $tsSecond.

This covers all aggregation pipeline operators currently supported in MongoDB, with the exception of accumulator operators only supported in the $setWindowFields stage, which will be introduced in a separate pull request.

I've also taken liberty to organise the code a bit. We now have interfaces for the various operator groups instead of one generic operator interface, and I've cleaned up the logic around accumulator stages (such as $sum) which have two different forms depending on the stage they're being used in. In the $group, $bucket, and $bucketAuto stages they take a single argument, whereas they take multiple arguments in other stages such as $project. This is now properly accounted for and easier to handle due to a trait that handles this more gracefully.

Note: GitHub Copilot was instrumental in getting this work done as it automated away certain repetitive tasks (such as adding proxy methods to the Operator class. I've also marked that class as @internal as we may want to do away with it in the future in favour of multiple traits that add specific operator groups, similar to how we have interfaces for the groups.

@alcaeus alcaeus added this to the 2.6.0 milestone Mar 22, 2023
@alcaeus alcaeus requested a review from jmikola March 22, 2023 11:18
@alcaeus alcaeus self-assigned this Mar 22, 2023
@alcaeus alcaeus force-pushed the new-aggregation-operators branch 2 times, most recently from 6e1575f to 75ab4da Compare March 22, 2023 11:36
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.

Not done with the review, but I'm publishing what comments I have so far.

I'm intentionally not reviewing the interfaces in detail and am instead focusing on the implementations.

lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php Outdated Show resolved Hide resolved
lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php Outdated Show resolved Hide resolved
lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php Show resolved Hide resolved
lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php Outdated Show resolved Hide resolved
lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php Show resolved Hide resolved
lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php Show resolved Hide resolved
lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php Outdated Show resolved Hide resolved
lib/Doctrine/ODM/MongoDB/Aggregation/Expr.php Outdated Show resolved Hide resolved
@alcaeus
Copy link
Member Author

alcaeus commented Mar 24, 2023

Note: test failures on PHP 7.4 are due to self not being sufficient for what we want to do. Using a static return type will fix the problem but requires PHP 8. We'll most likely drop support for PHP 7.4 as a result.

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.

Feel free to make the change to accumulator() arg order on your own.

Everything else LGTM.

@alcaeus
Copy link
Member Author

alcaeus commented Mar 28, 2023

Ignoring PHPStan as those failures are the same we're already seeing due to psalm types.

@alcaeus alcaeus merged commit 21a66c2 into doctrine:2.6.x Mar 28, 2023
@alcaeus alcaeus deleted the new-aggregation-operators branch March 28, 2023 09:54
notrix pushed a commit to notrix/mongodb-odm that referenced this pull request Apr 6, 2023
* Add interfaces for aggregation pipeline operators

* Implement new aggregation pipeline operators in stages

* Update static analysis baselines

* Use consistent formatting for operators with optional arguments

* Use consistent capitalisation for "date object"

* Wrap docblock comments

* Rename ensureArray to something more descriptive

* Remove duplicated docblocks

* Use func_get_args when proxying expression methods

* Fix wrong return types for internal expr methods

* Use func_get_args for operators where possible

* Disambiguate closure param in tests

* Fix docblock errors

Note: ProvidesGroupAccumulatorOperators errors were ignored as the trait is designed to be used in conjunction with the corresponding interface.

* Make initArgs parameter for $accumulator optional

* Use static return types across aggregation builder

* Fix handling of optional parameters for $range and $slice
alcaeus added a commit that referenced this pull request Apr 11, 2023
* 2.6.x:
  Add $search stage to aggregation pipeline builder (#2516)
  Support new aggregation operators in builder (#2514)
  Drop support for PHP 7.4 (#2515)
  Support new aggregation pipeline stages in builder (#2513)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants