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

Incorrect $group rewrite for timeseries collection when the accumulator uses meta field

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 6.2.0-rc0, 5.0.16, 6.0.6
    • Affects Version/s: 5.0.9, 6.0.0-rc13, 6.1.0-rc0
    • Component/s: None
    • None
    • Fully Compatible
    • ALL
    • v6.0, v5.0
    • Hide
      > db.createCollection("ts", {timeseries: {timeField: "time", metaField: "metadata"}})
      { "ok" : 1 }
      > db.ts.insert({metadata: {groupKey: 1, num: 4}, time: new Date()})
      WriteResult({ "nInserted" : 1 })
      > db.ts.insert({metadata: {groupKey: 2, num: 5}, time: new Date()})
      WriteResult({ "nInserted" : 1 })
      > db.ts.insert({metadata: {groupKey: 2, num: 4}, time: new Date()})
      WriteResult({ "nInserted" : 1 })
      > db.ts.aggregate([{$group: {_id: "$metadata.groupKey", accum: {$max: "$metadata.num"}}}])
      { "_id" : 1, "accum" : null }
      { "_id" : 2, "accum" : null }
      > db.ts.aggregate([{$_internalInhibitOptimization: {}} , {$group: {_id: "$metadata.groupKey", accum: {$max: "$metadata.num"}}}])
      { "_id" : 2, "accum" : 5 }
      { "_id" : 1, "accum" : 4 }
      
      Show
      > db.createCollection( "ts" , {timeseries: {timeField: "time" , metaField: "metadata" }}) { "ok" : 1 } > db.ts.insert({metadata: {groupKey: 1, num: 4}, time: new Date()}) WriteResult({ "nInserted" : 1 }) > db.ts.insert({metadata: {groupKey: 2, num: 5}, time: new Date()}) WriteResult({ "nInserted" : 1 }) > db.ts.insert({metadata: {groupKey: 2, num: 4}, time: new Date()}) WriteResult({ "nInserted" : 1 }) > db.ts.aggregate([{$group: {_id: "$metadata.groupKey" , accum: {$max: "$metadata.num" }}}]) { "_id" : 1, "accum" : null } { "_id" : 2, "accum" : null } > db.ts.aggregate([{$_internalInhibitOptimization: {}} , {$group: {_id: "$metadata.groupKey" , accum: {$max: "$metadata.num" }}}]) { "_id" : 2, "accum" : 5 } { "_id" : 1, "accum" : 4 }
    • QO 2022-08-08, QO 2022-08-22, QO 2022-09-05
    • 157

      We currently have an optimisation to rewrite a $group with the group key on meta field, and the accumulator(s) using either $min or $max on measurements. This logic is incorrectly assuming that all non-timeField paths can be treated as measurements. We rewrite a path "$x" as either "$control.min.x" or "$control.max.x". This rewrite would be incorrect when the accumulator is using a metaField. In this case, the rewrite logic should be a path "$metaField.x" gets written into "$meta.x".

            Assignee:
            svilen.mihaylov@mongodb.com Svilen Mihaylov (Inactive)
            Reporter:
            arun.banala@mongodb.com Arun Banala
            Votes:
            0 Vote for this issue
            Watchers:
            10 Start watching this issue

              Created:
              Updated:
              Resolved: