Files
Duan Pei c550bfba32 Merged PR 1688875: [Fix] LastN and BottomN Accumulator
### 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
![image.png](https://msdata.visualstudio.com/ba574a88-a171-48e0-8fcb-5fef6d23739c/_apis/git/repositories/501a5e7e-ffdc-4d1e-a5e6-910f73c6eb9a/pullRequests/1688875/attachments/image.png)

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
![image (2).png](https://msdata.visualstudio.com/ba574a88-a171-48e0-8fcb-5fef6d23739c/_apis/git/repositories/501a5e7e-ffdc-4d1e-a5e6-910f73c6eb9a/pullRequests/1688875/attachments/image%20%282%29.png)

#### 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 -->
2025-07-23 15:56:08 +08:00
..