[SERVER-31098] Wrong ns in system.profile for aggregation query Created: 15/Sep/17  Updated: 30/Oct/23  Resolved: 20/Nov/18

Status: Closed
Project: Core Server
Component/s: Aggregation Framework, Diagnostics
Affects Version/s: 3.4.5, 3.5.13
Fix Version/s: 3.6.11, 4.0.7, 4.1.6

Type: Bug Priority: Major - P3
Reporter: Henrik Ingo (Inactive) Assignee: Devin Hilly (Inactive)
Resolution: Fixed Votes: 0
Labels: QFB, diagnosibility
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Backports
Depends
Related
related to SERVER-30864 Query from system.profile errors when... Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Backport Requested:
v4.0, v3.6, v3.4
Sprint: Query 2018-11-19, Query 2018-12-03
Participants:
Case:

 Description   

Using the tpch denormalized dataset: http://noexpire.s3.amazonaws.com/sqlproxy/data/tpch_full_denormalized.bson.gz

I run the following aggregation pipeline:

db.setProfilingLevel(2)
 
db.mongo_part.aggregate(
[
			{
				"$match" : {
					"p_suppliers" : {
						"$elemMatch" : {
							"ps_s_r_name" : "EUROPE"
						}
					}
				}
			},
			{
				"$unwind" : {
					"includeArrayIndex" : "p_suppliers_idx",
					"path" : "$p_suppliers"
				}
			},
			{
				"$match" : {
					"p_suppliers.ps_s_r_name" : "EUROPE"
				}
			},
			{
				"$match" : {
					"$and" : [
						{
							"p_size" : NumberLong(15)
						},
						{
							"p_type" : {
								"$regex" : /^.*BRASS$/i
							}
						}
					]
				}
			},
			{
				"$group" : {
					"min(mongo_partsupp_DOT_ps_supplycost)" : {
						"$min" : "$p_suppliers.ps_supplycost"
					},
					"_id" : {
						"mongo_part_DOT_p_partkey" : "$_id"
					}
				}
			},
			{
				"$project" : {
					"mongo_part_DOT_p_partkey" : "$_id.mongo_part_DOT_p_partkey",
					"min(mongo_partsupp_DOT_ps_supplycost)" : "$min(mongo_partsupp_DOT_ps_supplycost)"
				}
			},
			{
				"$match" : {
					"mongo_part_DOT_p_partkey" : {
						"$ne" : null
					}
				}
			},
			{
				"$lookup" : {
					"as" : "__joined_mongo_part",
					"from" : "mongo_part",
					"localField" : "mongo_part_DOT_p_partkey",
					"foreignField" : "_id"
				}
			},
			{
				"$unwind" : {
					"path" : "$__joined_mongo_part",
					"preserveNullAndEmptyArrays" : false
				}
			},
			{
				"$match" : {
					"__joined_mongo_part._id" : {
						"$ne" : null
					}
				}
			},
			{
				"$lookup" : {
					"from" : "mongo_part",
					"localField" : "__joined_mongo_part._id",
					"foreignField" : "_id",
					"as" : "__joined_mongo_partsupp"
				}
			},
			{
				"$unwind" : {
					"path" : "$__joined_mongo_partsupp",
					"preserveNullAndEmptyArrays" : false
				}
			},
			{
				"$unwind" : {
					"path" : "$__joined_mongo_partsupp.p_suppliers",
					"includeArrayIndex" : "__joined_mongo_partsupp.p_suppliers_idx",
					"preserveNullAndEmptyArrays" : false
				}
			},
			{
				"$addFields" : {
					"__predicate" : {
						"$let" : {
							"vars" : {
								"predicate" : {
									"$cond" : [
										{
											"$or" : [
												{
													"$eq" : [
														{
															"$ifNull" : [
																"$__joined_mongo_partsupp.p_suppliers.ps_supplycost",
																null
															]
														},
														null
													]
												},
												{
													"$eq" : [
														{
															"$ifNull" : [
																"$min(mongo_partsupp_DOT_ps_supplycost)",
																null
															]
														},
														null
													]
												}
											]
										},
										null,
										{
											"$eq" : [
												"$__joined_mongo_partsupp.p_suppliers.ps_supplycost",
												"$min(mongo_partsupp_DOT_ps_supplycost)"
											]
										}
									]
								}
							},
							"in" : {
								"$cond" : [
									{
										"$or" : [
											{
												"$eq" : [
													"$$predicate",
													false
												]
											},
											{
												"$eq" : [
													"$$predicate",
													0
												]
											},
											{
												"$eq" : [
													"$$predicate",
													"0"
												]
											},
											{
												"$eq" : [
													"$$predicate",
													"-0"
												]
											},
											{
												"$eq" : [
													"$$predicate",
													"0.0"
												]
											},
											{
												"$eq" : [
													"$$predicate",
													"-0.0"
												]
											},
											{
												"$eq" : [
													"$$predicate",
													null
												]
											}
										]
									},
									false,
									true
								]
							}
						}
					}
				}
			},
			{
				"$match" : {
					"__predicate" : true
				}
			},
			{
				"$match" : {
					"__joined_mongo_partsupp.p_suppliers.ps_suppkey" : {
						"$ne" : null
					}
				}
			},
			{
				"$lookup" : {
					"from" : "mongo_supplier",
					"localField" : "__joined_mongo_partsupp.p_suppliers.ps_suppkey",
					"foreignField" : "_id",
					"as" : "__joined_mongo_supplier"
				}
			},
			{
				"$unwind" : {
					"path" : "$__joined_mongo_supplier",
					"preserveNullAndEmptyArrays" : false
				}
			},
			{
				"$project" : {
					"mongo_partsupp_DOT_ps_suppkey" : "$__joined_mongo_partsupp.p_suppliers.ps_suppkey",
					"mongo_supplier_DOT_s_comment" : "$__joined_mongo_supplier.s_comment",
					"mongo_supplier_DOT_s_n_name" : "$__joined_mongo_supplier.s_n_name",
					"mongo_part_DOT_p_mfgr" : "$__joined_mongo_part.p_mfgr",
					"mongo_part_DOT_p_partkey" : "$__joined_mongo_part._id",
					"mongo_supplier_DOT_s_address" : "$__joined_mongo_supplier.s_address",
					"mongo_supplier_DOT_s_name" : "$__joined_mongo_supplier.s_name",
					"mongo_supplier_DOT_s_phone" : "$__joined_mongo_supplier.s_phone",
					"mongo_supplier_DOT_s_acctbal" : "$__joined_mongo_supplier.s_acctbal"
				}
			}
		]
)

The entry in system.profile now contains the wrong ns:

db.system.profile.findOne().ns
tpch.mongo_supplier

Note that if the same aggregation is executed against an empty database, it actually does result in the right ns (emptydb.mongo_part) logged in system.profile.



 Comments   
Comment by Githook User [ 20/Feb/19 ]

Author:

{'name': 'Devin Hilly', 'email': 'devin.hilly@mongodb.com', 'username': 'dmhilly'}

Message: SERVER-31098 Wrong ns in system.profile for aggregation query

(cherry picked from commit 1862b00862a6ea9c3fdd08bba52ea6a016eccbc5)
(cherry picked from commit 93f8e8b2df10c2ec28090d9bbfd533b45f3b58e2)
Branch: v3.6
https://github.com/mongodb/mongo/commit/18caaa34aecc860adf797e712127b639292d8903

Comment by Githook User [ 06/Feb/19 ]

Author:

{'name': 'Devin Hilly', 'email': 'devin.hilly@mongodb.com', 'username': 'dmhilly'}

Message: SERVER-31098 Wrong ns in system.profile for aggregation query

(cherry picked from commit 1862b00862a6ea9c3fdd08bba52ea6a016eccbc5)
Branch: v4.0
https://github.com/mongodb/mongo/commit/93f8e8b2df10c2ec28090d9bbfd533b45f3b58e2

Comment by Charlie Swanson [ 20/Nov/18 ]

Flagging this for backport consideration. Based on devin.hilly's patch it doesn't seem like it will be terribly complex, but almost certainly will not backport cleanly. For one thing, AutoStatsTracker did not exist in 3.4 - this functionality was rolled into AutoGetcollectionForRead.

Comment by Githook User [ 20/Nov/18 ]

Author:

{'name': 'Devin Hilly', 'email': 'devin.hilly@mongodb.com', 'username': 'dmhilly'}

Message: SERVER-31098 Wrong ns in system.profile for aggregation query
Branch: master
https://github.com/mongodb/mongo/commit/1862b00862a6ea9c3fdd08bba52ea6a016eccbc5

Comment by Asya Kamsky [ 01/Mar/18 ]

The wrong namespace is also written to the logs, not just to the profiler collection.

The namespace is the last collection that $lookup uses as "from" field.

Comment by James Wahlin [ 15/Sep/17 ]

We are writing the namespace from the last $lookup performed to the profiler rather than the local collection. This is the case on master as well. The following can be used to repro:

db.dropDatabase();
db.local.insert({a: 1});
db.foreign.insert({a: 1});
db.setProfilingLevel(2);
db.local.aggregate([{$lookup: {from: "foreign", as: "result", localField: "a", foreignField: "a"}}], {comment: "ns_test"});
db.setProfilingLevel(0);
let profileDoc = db.system.profile.findOne({"command.comment": "ns_test"},{ns: 1});
assert.eq("test.local", profileDoc.ns);

Generated at Thu Feb 08 04:25:59 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.