Skip to content

Commit b39883a

Browse files
fotiniAlvanakiEvergreen Agent
authored andcommitted
SERVER-83490 Do not push to SBE $addFields stage created as part of $setWindowFields
1 parent 976ce50 commit b39883a

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

src/mongo/db/pipeline/document_source_add_fields.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,17 @@ intrusive_ptr<DocumentSource> DocumentSourceAddFields::create(
8181
intrusive_ptr<DocumentSource> DocumentSourceAddFields::create(
8282
const FieldPath& fieldPath,
8383
const intrusive_ptr<Expression>& expr,
84-
const intrusive_ptr<ExpressionContext>& expCtx) {
84+
const intrusive_ptr<ExpressionContext>& expCtx,
85+
bool createdBySetWindowFields) {
8586

8687
const bool isIndependentOfAnyCollection = false;
87-
return make_intrusive<DocumentSourceSingleDocumentTransformation>(
88+
auto docSrc = make_intrusive<DocumentSourceSingleDocumentTransformation>(
8889
expCtx,
8990
projection_executor::AddFieldsProjectionExecutor::create(expCtx, fieldPath, expr),
9091
kStageName,
9192
isIndependentOfAnyCollection);
93+
docSrc->setCreatedBySetWindowFields(createdBySetWindowFields);
94+
return docSrc;
9295
}
9396

9497
intrusive_ptr<DocumentSource> DocumentSourceAddFields::createFromBson(

src/mongo/db/pipeline/document_source_add_fields.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ class DocumentSourceAddFields final {
6969
static boost::intrusive_ptr<DocumentSource> create(
7070
const FieldPath& fieldPath,
7171
const boost::intrusive_ptr<Expression>& expr,
72-
const boost::intrusive_ptr<ExpressionContext>& expCtx);
72+
const boost::intrusive_ptr<ExpressionContext>& expCtx,
73+
bool createdBySetWindowFields);
7374

7475
/**
7576
* Parses a $addFields stage from the user-supplied BSON.

src/mongo/db/pipeline/document_source_set_window_fields.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ list<intrusive_ptr<DocumentSource>> document_source_set_window_fields::create(
244244
// $set
245245
if (complexPartitionBy) {
246246
result.push_back(
247-
DocumentSourceAddFields::create(*simplePartitionBy, *complexPartitionBy, expCtx));
247+
DocumentSourceAddFields::create(*simplePartitionBy, *complexPartitionBy, expCtx, true));
248248
}
249249

250250
// $sort

src/mongo/db/pipeline/document_source_single_document_transformation.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,14 @@ class DocumentSourceSingleDocumentTransformation final : public DocumentSource {
163163
newName);
164164
}
165165

166+
void setCreatedBySetWindowFields(bool val) {
167+
createdBySetWindowFields = val;
168+
}
169+
170+
bool isCreatedBySetWindowFields() const {
171+
return createdBySetWindowFields;
172+
}
173+
166174
protected:
167175
GetNextResult doGetNext() final;
168176
void doDispose() final;
@@ -182,6 +190,11 @@ class DocumentSourceSingleDocumentTransformation final : public DocumentSource {
182190
// Cached stage options in case this DocumentSource is disposed before serialized (e.g. explain
183191
// with a sort which will auto-dispose of the pipeline).
184192
Document _cachedStageOptions;
193+
194+
// We set the createdBySetWindowFields flag to prevent an addFields stage from being pushed to
195+
// SBE. TODO (SERVER-75103) : Once setWindowFields has been pushed to SBE, this should be
196+
// removed.
197+
bool createdBySetWindowFields = false;
185198
};
186199

187200
} // namespace mongo

src/mongo/db/pipeline/pipeline_d.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,13 @@ bool pushDownPipelineStageIfCompatible(
295295
return true;
296296
} else if (auto transformStage =
297297
dynamic_cast<DocumentSourceSingleDocumentTransformation*>(stage.get())) {
298+
// We do not push to SBE an addFields that has been created as part of a setWindowFields
299+
// stage because it causes a performance regression. TODO (SERVER-75103) : Once
300+
// setWindowFields has been pushed to SBE, this should be removed.
301+
if (transformStage->isCreatedBySetWindowFields()) {
302+
return false;
303+
}
304+
298305
if (!allowedStages.transform) {
299306
return false;
300307
}

0 commit comments

Comments
 (0)