mirror of
https://github.com/FerretDB/documentdb.git
synced 2026-11-06 11:02:50 -05:00
### Does this PR have any customer impact? ### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc) ### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes) ### Are you introducing any new config? If yes, do you have tests with and without them being set? ### ChangeLog (Refer [Template](../oss/CHANGELOG.md)) ### Description Issue1: Our lastN has some errors. When taking the last N elements, the original order is destroyed. For example: for[31,33,99,1], if want to get last3, it should be [33,99,1], our current lastN will get [1,99,33]: left is native mongo, right one is our pgmongo  Fix: When there is no sort, the result is reversed in the transit function. When there is sort, the result is reversed in the final function. Issue2: BottomN also has similar errors as mentioned above. left is native mongo, right one is our pgmongo  #### AI description (iteration 1) #### PR Classification Bug fix addressing incorrect accumulation logic for the LastN accumulator. #### PR Summary This PR fixes issues in the LastN accumulator by streamlining memory copy operations and adding robust error checking for BSON data integrity, while also correcting a misconfigured boolean flag in related functions. - `oss/pg_documentdb/src/aggregation/bson_sorted_accumulator.c`: Simplified the copy routine by replacing conditional branches with a unified memmove and added error checks to detect and report insufficient or corrupted BSON entries. - `oss/pg_documentdb/src/aggregation/bson_first_last.c`: Updated the `isLast` flag from true to false in both transition and combine functions to ensure correct accumulator behavior. <!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->