Files
documentdb/internal
Deepak Bisht 79927cae0d Merged PR 1694933: [Oss] [documentdb] - Fix match filters not applied on lookup unwind aggregation stage
### 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))
- Fix $match filters not applied after preserving null on $lookup and $unwind combinations

### Description
This fixes the OSS issue: https://github.com/microsoft/documentdb/issues/198
- Starting PG 16 the the right join query that will have NULL results need to assign `varnullingrels` index of RTE which can replace the NULL values for outer JOIN conditions, if the VAR expression is a result of JOIN.

This is seen as the diff in the parse plan of not working and working case of the same query in PG 16
![image.png](https://msdata.visualstudio.com/ba574a88-a171-48e0-8fcb-5fef6d23739c/_apis/git/repositories/501a5e7e-ffdc-4d1e-a5e6-910f73c6eb9a/pullRequests/1694933/attachments/image.png)

The final query described in the bug has below format and the last `WHERE` filter can merge with the inner right query if `varnullingrels` is not specified which yields incorrect results
```SQL
SELECT document
   FROM ( WITT debug_print_parse to on;
             LECT document
   FROM ( WITH "lookupLeftCte_0" AS (
                 SELECT collection.document,
                    bson_dollar_lookup_extract_filter_expression(collection.document, '{ "name" : "author" }'::bson) AS lookup_filter
                   FROM documentdb_data.documents_3 collection
                  WHERE collection.shard_key_value = '3'::bigint
                )
         SELECT bson_dollar_merge_documents_at_path(lookup_stage_1.document, COALESCE("lookupRight_stage_1".document, '{ }'::bson), 'author-names'::text) AS document
           FROM "lookupLeftCte_0" lookup_stage_1
             LEFT JOIN LATERAL ( WITH lookup_right_query AS (
                         SELECT collection_0_1.document
                           FROM documentdb_data.documents_2 collection_0_1
                          WHERE collection_0_1.shard_key_value = '2'::bigint
                        )
                 SELECT lookup_right_query_stage_0.document
                   FROM lookup_right_query lookup_right_query_stage_0
                  WHERE bson_dollar_lookup_join_filter(lookup_right_query_stage_0.document, lookup_stage_1.lookup_filter, 'name'::text)) "lookupRight_stage_1" ON true) agg_stage_1
  WHERE document #= '{ "title" : "Emma" }'::bsonquery;
```

----
#### AI description  (iteration 1)
#### PR Classification
Bug fix addressing the incorrect application of match filters in aggregation pipelines combining $lookup and $unwind stages.

#### PR Summary
This pull request fixes the issue by updating the aggregation pipeline processing logic for null-preserving $lookup/$unwind operations and enhancing test coverage to verify correct filtering.
- **`oss/p...
2025-06-13 15:13:47 +08:00
..