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

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

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Major - P3 Major - P3
    • 7.1.0-rc0
    • None
    • None
    • Fully Compatible
    • ALL
    • QE 2023-06-12
    • 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.

      Attachments

        Activity

          People

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

            Dates

              Created:
              Updated:
              Resolved: