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

Timeseries last-point query is incorrect for pre-1970 dates

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • Query Integration
    • ALL

      Timeseries "last point" queries are those that pick out the most recent event in each series. For example:

      {$group: {
        _id: "$meta.sensor_id",
        last_time: {$top: {sortBy: {time: 1}, output: "$time"}},
        last_value: {$top: {sortBy: {time: 1}, output: "$value"}},
      }}
      

      We rewrite the query to pick out the latest bucket within each series, before unpacking. This assumes that the latest bucket contains the latest event, which is true when control.max.time is a tight upper bound.

      However, for events before 1970 control.max.time is not a tight bound:

      > db.createCollection('ts', {timeseries:{timeField:'t',metaField:'m'}})
      { "ok" : 1 }
      > db.ts.insert({t: ISODate('1969-01-01T00:00:12.345')})
      WriteResult({ "nInserted" : 1 })
      > db.system.buckets.ts.find().pretty()
      {
              "_id" : ObjectId("fe1eccbc6f22cc96f1dd557f"),
              "control" : {
                      "min" : {
                              // Tight lower bound, not rounded.
                              "t" : ISODate("1969-01-01T00:00:12.345Z")
                      },
                      "max" : {
                              // Rounded upper bound, not tight.
                              "t" : ISODate("1969-01-01T00:01:00Z")
                      }
              },
              "data" : ...
      }
      

      This means if a series contains only pre-1970 dates, the query can return a wrong event.

      A simple / coarse way to fix this would be to disable this rewrite when the collection contains extended-range dates.

            Assignee:
            backlog-query-integration [DO NOT USE] Backlog - Query Integration
            Reporter:
            david.percy@mongodb.com David Percy
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated: