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

Sort by "randVal" is not random

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Works as Designed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Operating System:
      ALL
    • Steps To Reproduce:
      Hide

      MongoDB Enterprise > db.randval.insert([{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}])
      MongoDB Enterprise > db.randval.aggregate([{$sort: {rand: {$meta: "randVal"}}}])
      { "_id" : ObjectId("5d8d449cb417f08bc7302867"), "a" : 1 }
      { "_id" : ObjectId("5d8d449cb417f08bc7302868"), "a" : 2 }
      { "_id" : ObjectId("5d8d449cb417f08bc7302869"), "a" : 3 }
      { "_id" : ObjectId("5d8d449cb417f08bc730286a"), "a" : 4 }
      { "_id" : ObjectId("5d8d449cb417f08bc730286b"), "a" : 5 }
      

      Show
      MongoDB Enterprise > db.randval.insert([{a: 1}, {a: 2}, {a: 3}, {a: 4}, {a: 5}]) MongoDB Enterprise > db.randval.aggregate([{$sort: {rand: {$meta: "randVal"}}}]) { "_id" : ObjectId("5d8d449cb417f08bc7302867"), "a" : 1 } { "_id" : ObjectId("5d8d449cb417f08bc7302868"), "a" : 2 } { "_id" : ObjectId("5d8d449cb417f08bc7302869"), "a" : 3 } { "_id" : ObjectId("5d8d449cb417f08bc730286a"), "a" : 4 } { "_id" : ObjectId("5d8d449cb417f08bc730286b"), "a" : 5 }
    • Sprint:
      Query 2019-11-04

      Description

      The SortKeyGenerator::extractKeyPart() function gets a "randVal" from the Document metadata (by way of Expression::evaluate()) when the key pattern has {$meta: ""randVal"}. However, nowhere in the code base ever sets a random value for this piece of metadata, so the return value is always eoo. The sort deals with this lack of randomness by returning the documents in their natural order.

      I discovered this problem when I attempted to add an invariant after this line to ensure that:

      invariant(!plainKey.missing());
      

      Currently, this invariant always fails, but when we figure out a way to ensure that documents have "randVal" metadata, we should be able to add that invariant in.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              david.storch David Storch
              Reporter:
              justin.seyster Justin Seyster
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: