Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-77737

$top/$bottom gives incorrect result for sharded collection and constant expressions

    • Fully Compatible
    • ALL
    • v7.0, v6.0
    • QE 2023-06-12
    • 164

      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.

            Assignee:
            rui.liu@mongodb.com Rui Liu
            Reporter:
            rui.liu@mongodb.com Rui Liu
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: