SERVER-87998: Code clean-up in initCQ for CanonicalQuery creation (#20070)

GitOrigin-RevId: b04e9e238aec8c812498e5371c9d4049f09aff3d
This commit is contained in:
Matt Olma 2024-03-18 22:24:36 -07:00 committed by MongoDB Bot
parent 75d48cec2d
commit dbd98ffd19
1 changed files with 16 additions and 19 deletions

View File

@ -208,9 +208,25 @@ void CanonicalQuery::initCq(boost::intrusive_ptr<ExpressionContext> expCtx,
_proj->optimize();
}
}
_metadataDeps = _proj->metadataDeps();
uassert(ErrorCodes::BadValue,
"cannot use sortKey $meta projection without a sort",
!(_proj->metadataDeps()[DocumentMetadataFields::kSortKey] &&
_findCommand->getSort().isEmpty()));
}
if (parsedFind->sort) {
_sortPattern = std::move(parsedFind->sort);
// Be sure to track and add any metadata dependencies from the sort (e.g. text score).
_metadataDeps |= _sortPattern->metadataDeps(parsedFind->unavailableMetadata);
// If the results of this query might have to be merged on a remote node, then that node
// might need the sort key metadata. Request that the plan generates this metadata.
if (_expCtx->needsMerge) {
_metadataDeps.set(DocumentMetadataFields::kSortKey);
}
}
_cqPipeline = std::move(cqPipeline);
_isCountLike = isCountLike;
@ -239,25 +255,6 @@ void CanonicalQuery::initCq(boost::intrusive_ptr<ExpressionContext> expCtx,
uasserted(status.code(), status.reason());
}
if (_proj) {
_metadataDeps = _proj->metadataDeps();
uassert(ErrorCodes::BadValue,
"cannot use sortKey $meta projection without a sort",
!(_proj->metadataDeps()[DocumentMetadataFields::kSortKey] &&
_findCommand->getSort().isEmpty()));
}
if (_sortPattern) {
// Be sure to track and add any metadata dependencies from the sort (e.g. text score).
_metadataDeps |= _sortPattern->metadataDeps(parsedFind->unavailableMetadata);
// If the results of this query might have to be merged on a remote node, then that node
// might need the sort key metadata. Request that the plan generates this metadata.
if (_expCtx->needsMerge) {
_metadataDeps.set(DocumentMetadataFields::kSortKey);
}
}
// If the 'returnKey' option is set, then the plan should produce index key metadata.
if (_findCommand->getReturnKey()) {
_metadataDeps.set(DocumentMetadataFields::kIndexKey);