Files
documentdb/internal/pg_documentdb_distributed
Deepak Bisht 1fd2daa2e6 Merged PR 1672196: Fix Lookup Unwind Crash with Inner join
### Does this PR have any customer impact?
Yes

### Type (Feature, Refactoring, Bugfix, DevOps, Testing, Perf, etc)
BugFix

### Does it involve schema level changes? (Table, Column, Index, UDF, etc level changes)
No

### Are you introducing any new config? If yes, do you have tests with and without them being set?
No

### ChangeLog (Refer [Template](../oss/CHANGELOG.md))

### Description

- When unwind is used with preserveNullAndEmptyArrays: false, the join type is used as INNER JOIN, but we have the join defined as LATERAL which means it is dependent on results of left query. The bug seems to be intermittent based on the cost of the joins, sometimes the join can be evaluated as LEFT JOIN RIGHT where things work, but when PG decided to go to the path of RIGHT JOIN LEFT it will crash as RIGHT is dependent on LEFT

The solution is to always use LEFT JOIN, and for case where `preserveNullAndEmptyArrays: false` add an `IS NOT NULL` check to filter out the right documents that are not a match.

----
#### AI description  (iteration 1)
#### PR Classification
This pull request fixes a bug in lookup unwind operations by correcting the join type used in the aggregation pipeline.

#### PR Summary
The changes resolve a crash caused by using an inner join during lookup unwind by refactoring the join logic and lookup context. The update enforces LEFT JOIN behavior when preserving null and empty arrays and updates test expectations accordingly.
- `oss/pg_documentdb/src/aggregation/bson_aggregation_nested_pipeline.c`: Refactored the lookup join logic by replacing the `useInnerJoin` flag with the `preserveNullAndEmptyArrays` flag and always using LEFT JOIN.
- `pgmongo/src/test/docdb_compat/expected/bson_aggregation_stage_lookup_tests.out` and `oss/internal/pg_documentdb_distributed/src/test/regress/expected/bson_aggregation_stage_lookup_tests.out`: Updated expected outputs for BSON aggregation lookup tests.
- `oss/internal/pg_documentdb_distributed/src/test/regress/sql/bson_aggregation_stage_lookup_tests.sql`: Added new SQL tests to verify the corrected lookup behavior using LEFT JOIN.
<!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->

Related work items: #4209808
2025-05-29 12:12:31 +08:00
..