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

$bucketAuto $mergeObject does not obey input order

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 8.1.0-rc0, 8.0.0-rc4
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Query Optimization
    • Fully Compatible
    • ALL
    • v8.0
    • QO 2024-03-18, QO 2024-04-01, QO 2024-04-15, QO 2024-04-29
    • 122

      Problem
      The same cause as SERVER-85337. Previously, $bucketAuto had incorrect implementation to assume stable-sort works well with positional accumulators like $first and $last. SERVER-85337 fixes the issue by specializing $first/$firstN/$last/$lastN. $mergeObject is a scapegoat only discovered recently after SERVER-81571 is merged and later revealed in fuzzer test failure BF-32056.

      Steps to reproduce

      db.c.insertMany([
      { "_id" : 0, "a" : 0 },
      { "_id" : 1, "a" : 1 },
      { "_id" : 2, "a" : 2 },
      { "_id" : 3, "a" : 3 },
      { "_id" : 4, "a" : 4 },
      { "_id" : 5, "a" : 5 },
      { "_id" : 6, "a" : 6 },
      { "_id" : 7, "a" : 7 },
      { "_id" : 8, "a" : 8 },
      { "_id" : 9, "a" : 9 }]);
      
      db.c.aggregate([ {$sort: {a: -1}}, {$bucket: {groupBy: "$a", boundaries: [0, 5, 10], output: {a: {$mergeObjects: "$$ROOT"}} }} ]);
      
      db.c.aggregate([ {$sort: {a: -1}}, {$bucketAuto: {groupBy: "$a", buckets: 2, output: {a: {$mergeObjects: "$$ROOT"}} }} ]);
      

      $bucket returns the results:

      { "_id" : 0, "a" : { "_id" : 0, "a" : 0 } }
      { "_id" : 5, "a" : { "_id" : 5, "a" : 5 } }
      

      Whereas $bucketAuto returns the results:

      { "_id" : { "min" : 0, "max" : 5 }, "a" : { "_id" : 4, "a" : 4 } }
      { "_id" : { "min" : 5, "max" : 9 }, "a" : { "_id" : 9, "a" : 9 } }
      

      Plan of attack
      Follow SERVER-85337 to specialize accumulator $mergeObject as well.

            Assignee:
            david.percy@mongodb.com David Percy
            Reporter:
            chii.huang@mongodb.com Chi-I Huang
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: