[SERVER-77737] $top/$bottom gives incorrect result for sharded collection and constant expressions Created: 02/Jun/23  Updated: 29/Oct/23  Resolved: 06/Jun/23

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 7.1.0-rc0

Type: Bug Priority: Major - P3
Reporter: Rui Liu Assignee: Rui Liu
Resolution: Fixed Votes: 0
Labels: auto-reverted
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Problem/Incident
Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: QE 2023-06-12
Participants:
Linked BF Score: 164

 Description   

For sharded collection, the following query gives incorrect results:

coll.aggregate([{$group: {_id: null, result: {$topN: {n: 3, output: "abc", sortBy: {}}}}}])

This is a pretty useless query. However, the results from both Classic engine and SBE are incorrect:

{
	"_id" : null,
	"result" : [
		{
			"output" : "abc",
			"sortFields" : [ ]
		},
		{
			"output" : "abc",
			"sortFields" : [ ]
		},
		{
			"output" : "abc",
			"sortFields" : [ ]
		}
	]
}

This is due to the erroneous logic for serializing constant argument: https://github.com/10gen/mongo/blob/c6b787ec1d8cc77b3ae09fda85e278cd6e752ee6/src/mongo/db/pipeline/accumulator_multi.cpp#L473

When the argument is constant, the serialized value will be {$const: {output: ...}} instead of

{output: ...}

, so the logic to distinguish merging group is wrong and the output field is appended incorrectly.



 Comments   
Comment by Githook User [ 06/Jun/23 ]

Author:

{'name': 'Rui Liu', 'email': 'lriuui0x0@gmail.com', 'username': 'lriuui0x0'}

Message: SERVER-77737 Fix incorrect serialization logic of $top/$bottom on constant output and sort
Branch: master
https://github.com/mongodb/mongo/commit/d16b7e170af309ba8d5d901ec011200b1438b7e7

Comment by xgen-buildbaron-user [ 06/Jun/23 ]

Ticket re-opened due to revert. aggregation began a consistent failure of jstests\aggregation\accumulators\top_bottom_top_n_bottom_n.js

Comment by Githook User [ 06/Jun/23 ]

Author:

{'name': 'auto-revert-processor', 'email': 'dev-prod-dag@mongodb.com', 'username': ''}

Message: Revert "SERVER-77737 Fix incorrect serialization logic of $top/$bottom on constant output and sort"

This reverts commit f0ecff46204b3f308b90d9f0750568f9673fd65b.
Branch: master
https://github.com/mongodb/mongo/commit/cd73317fa00de2dc3f441045ccd2afc769827be5

Comment by Githook User [ 05/Jun/23 ]

Author:

{'name': 'Rui Liu', 'email': 'lriuui0x0@gmail.com', 'username': 'lriuui0x0'}

Message: SERVER-77737 Fix incorrect serialization logic of $top/$bottom on constant output and sort
Branch: master
https://github.com/mongodb/mongo/commit/f0ecff46204b3f308b90d9f0750568f9673fd65b

Generated at Thu Feb 08 06:36:26 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.