@@ -102,8 +102,8 @@ namespace mongo {
102102 bool runExecute (
103103 BSONObjBuilder &result, string &errmsg,
104104 const string &ns, const string &db,
105- intrusive_ptr<Pipeline> & pPipeline,
106- intrusive_ptr<ExpressionContext> & pCtx);
105+ intrusive_ptr<Pipeline> pPipeline,
106+ intrusive_ptr<ExpressionContext> pCtx);
107107 };
108108
109109 // self-registering singleton static instance
@@ -155,8 +155,26 @@ namespace mongo {
155155 bool PipelineCommand::runExecute (
156156 BSONObjBuilder &result, string &errmsg,
157157 const string &ns, const string &db,
158- intrusive_ptr<Pipeline> &pPipeline,
159- intrusive_ptr<ExpressionContext> &pCtx) {
158+ intrusive_ptr<Pipeline> pPipeline,
159+ intrusive_ptr<ExpressionContext> pCtx) {
160+
161+ #if _DEBUG
162+ // This is outside of the if block to keep the object alive until the pipeline is finished.
163+ BSONObj parsed;
164+ if (!pCtx->getInShard ()) {
165+ // Make sure all operations round-trip through Pipeline::toBson()
166+ // correctly by reparsing every command on DEBUG builds. This is
167+ // important because sharded aggregations rely on this ability.
168+ // Skipping when inShard because this has already been through the
169+ // transformation (and this unsets pCtx->inShard).
170+ BSONObjBuilder bb;
171+ pPipeline->toBson (&bb);
172+ parsed = bb.obj ();
173+ // PRINT(parsed); // when debugging failures uncomment this and the matching one in run
174+ pPipeline = Pipeline::parseCommand (errmsg, parsed, pCtx);
175+ verify (pPipeline);
176+ }
177+ #endif
160178
161179 // The DocumentSourceCursor manages a read lock internally, see SERVER-6123.
162180 intrusive_ptr<DocumentSourceCursor> pSource (
@@ -252,6 +270,7 @@ namespace mongo {
252270 bool PipelineCommand::run (const string &db, BSONObj &cmdObj,
253271 int options, string &errmsg,
254272 BSONObjBuilder &result, bool fromRepl) {
273+ // PRINT(cmdObj); // uncomment when debugging
255274
256275 intrusive_ptr<ExpressionContext> pCtx (
257276 ExpressionContext::create (&InterruptStatusMongod::status));
0 commit comments