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

Investigate an issue that we can't generate sort key for non parallel arrays for multi-key path sort pattern

    • Query Execution
    • ALL

      See the following repro:

      > db.adminCommand({setParameter: 1, internalQueryFrameworkControl: "forceClassicEngine"});
      > db.ds.insert({x: [{a: 1, b: 2}, {a: 2, b: 3}]});
      > # $sort + $group w/ $first
      > let pipeline1 = [{$sort: {"x.a": 1, "x.b": 1}}, {$group: {_id: "$x.a", o: {$first: "$x.b"}}}];
      > # $group w/ $top is supposed to return the same result
      > let pipeline2 = [{$group: {_id: "$x.a", o: {$top: {sortBy: {"x.a": 1, "x.b": 1}, output: "$x.b"}}}}];
      > db.ds.aggregate(pipeline1);
      { "_id" : [ 1, 2 ], "o" : [ 2, 3 ] }
      > db.ds.aggregate(pipeline2);
      uncaught exception: Error: command failed: {
              "ok" : 0,
              "errmsg" : "PlanExecutor error during aggregation :: caused by :: cannot sort with keys that are parallel arrays",
              "code" : 2,
              "codeName" : "BadValue"
      }
      

      Even though "$x.a" & "$x.b" from {x: [{a: 1, b: 2}, {a: 2, b: 3}]} are not truly parallel arrays, we refuse to generate sort key, saying "cannot sort with keys that are parallel arrays". One example of true parallel arrays is {x: {a: [1, 2], b: [0, -1]}}. For parallel arrays, ordering by one array may not agree with order by the other array.

      Interestingly enough, in SBE, both pipelines succeed but 6.0 & 7.0 show the same behavior as the above: pipeline1 succeeds and pipeline2 fails with the same error.

      This issue occurs in the 8.0 classic pipeline and 6.0 and 7.0 for $group w/ $top. The same issue would occur for $bottom/$topN/$bottomN.

            Assignee:
            Unassigned Unassigned
            Reporter:
            yoonsoo.kim@mongodb.com Yoon Soo Kim
            Votes:
            0 Vote for this issue
            Watchers:
            9 Start watching this issue

              Created:
              Updated: