Merged PR 1573978: Improve Let with Lookup

### Does this PR have any customer impact?
No

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

### 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))
Improve Perf in $lookup with let stage with nested join pipeline

### Description
Today the pipeline `$match` stage is not inlined with lookup right collection query in case it has `$expr` and let expression. Move this to right query if `$match` is the first non-inlined stage.

This improves the JOIN criteria and fetches lesser documents from right collection. We have seen cx cases where very small set of docucments can breach the 100 MB document limit otherwise.

----
#### AI description  (iteration 1)
#### PR Classification
Code optimization and cleanup.

#### PR Summary
This pull request improves the handling of `$let` with `$lookup` in the aggregation pipeline by simplifying and optimizing the code.
- Removed the `QueryDocumentHasExpr` function and simplified the `CanInlineLookupStageMatch` function in `bson_aggregation_pipeline.c`.
- Enhanced the `OptimizeLookup` function to build and use `$let` expressions more efficiently in `bson_aggregation_nested_pipeline.c`.
- Simplified the `ProcessLookupCoreWithLet` function to directly use variables from the left query in `bson_aggregation_nested_pipeline.c`.
<!-- GitOpsUserAgent=GitOps.Apps.Server.pullrequestcopilot -->
This commit is contained in:
Deepak Bisht authored and Shuai Tian committed 2025-02-19 17:00:35 +08:00
1 parent 4dee5a8d5e
commit 83e22d92f3
11 files changed
+740 -282

No files matched your search

@@ -19,6 +19,8 @@
#ifndef BSON_AGGREGATION_PIPELINE_PRIVATE_H
#define BSON_AGGREGATION_PIPELINE_PRIVATE_H
typedef struct AggregationStageDefinition AggregationStageDefinition;
/*
* for nested stage, use this to record its parent stage name
*/
@@ -32,6 +34,63 @@ typedef enum ParentStageName
} ParentStageName;
/*
* Enums to represent all kind of aggregations stages.
* Please keep the list sorted within their groups for easier readability
*/
typedef enum
{
Stage_Invalid = 0,
/* Start internal stages Mongo */
Stage_Internal_InhibitOptimization = 1,
/* Start Mongo Public stages */
Stage_AddFields = 10,
Stage_Bucket,
Stage_BucketAuto,
Stage_ChangeStream,
Stage_CollStats,
Stage_Count,
Stage_CurrentOp,
Stage_Densify,
Stage_Documents,
Stage_Facet,
Stage_Fill,
Stage_GeoNear,
Stage_GraphLookup,
Stage_Group,
Stage_IndexStats,
Stage_Limit,
Stage_ListLocalSessions,
Stage_ListSessions,
Stage_Lookup,
Stage_Match,
Stage_Merge,
Stage_Out,
Stage_Project,
Stage_Redact,
Stage_ReplaceRoot,
Stage_ReplaceWith,
Stage_Sample,
Stage_Search,
Stage_SearchMeta,
Stage_Set,
Stage_SetWindowFields,
Stage_Skip,
Stage_Sort,
Stage_SortByCount,
Stage_UnionWith,
Stage_Unset,
Stage_Unwind,
Stage_VectorSearch,
/* Start of pg_documentdb Custom or internal stages */
Stage_InverseMatch = 100,
Stage_LookupUnwind
} Stage;
/*
* Shared context during aggregation pipeline build phase.
*/
@@ -120,6 +179,16 @@ typedef struct
} AggregationPipelineBuildContext;
typedef struct
{
/* The bson value of the pipeline spec */
bson_value_t stageValue;
/* Definition of internal handlers */
AggregationStageDefinition *stageDefinition;
} AggregationStage;
/* Core Infra exports */
Query * MutateQueryWithPipeline(Query *query, List *aggregationStages,
AggregationPipelineBuildContext *context);
@@ -219,6 +288,7 @@ bool IsPartitionByFieldsOnShardKey(const pgbson *partitionByFields,
const MongoCollection *collection);
Expr * GenerateMultiExpressionRepathExpression(List *repathArgs);
Stage GetAggregationStageAtPosition(const List *aggregationStages, int position);
/* Helper methods */