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

ExpressionObject does not optimize itself to a constant expression

    • Fully Compatible

      An ExpressionObject's implementation of optimize() is missing an optimization where if all of the values in the object are constant, the entire ExpressionObject can be replaced with an ExpressionConstant representing the object literal.

      This can cause an confusing error message when using the $bucket stage, since $bucket ensures all entries of the boundaries array are constants:

      > db.baz.find()
      { "_id" : ObjectId("582e293f38b7ba11ec92eda8"), "a" : { "b" : 1, "c" : 1 } }
      { "_id" : ObjectId("582e293f38b7ba11ec92eda9"), "a" : { "b" : 1, "c" : 2 } }
      
      // This fails, implying the object literals are not constants:
      > db.baz.aggregate([{$bucket: {groupBy: "$a", boundaries: [{b: 1, c: 1}, {b: 1, c: 2}, {b: 2, c: 3}]}}])
      assert: command failed: {
      	"ok" : 0,
      	"errmsg" : "The $bucket 'boundaries' field must be an array of constant values, but found value: { b: 1.0, c: 1.0 }.",
      	"code" : 40191,
      	"codeName" : "Location40191"
      } : aggregate failed
      _getErrorWithCode@src/mongo/shell/utils.js:25:13
      doassert@src/mongo/shell/assert.js:16:14
      assert.commandWorked@src/mongo/shell/assert.js:370:5
      DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
      @(shell):1:1
      
      2016-11-17T17:05:06.583-0500 E QUERY    [main] Error: command failed: {
      	"ok" : 0,
      	"errmsg" : "The $bucket 'boundaries' field must be an array of constant values, but found value: { b: 1.0, c: 1.0 }.",
      	"code" : 40191,
      	"codeName" : "Location40191"
      } : aggregate failed :
      _getErrorWithCode@src/mongo/shell/utils.js:25:13
      doassert@src/mongo/shell/assert.js:16:14
      assert.commandWorked@src/mongo/shell/assert.js:370:5
      DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1319:5
      @(shell):1:1
      
      // If the objects are wrapped in a $literal, the $bucket succeeds:
      > db.baz.aggregate([{$bucket: {groupBy: "$a", boundaries: [{$literal: {b: 1, c: 1}}, {$literal: {b: 1, c: 2}}, {$literal: {b: 2, c: 3}}]}}])
      { "_id" : { "b" : 1, "c" : 1 }, "count" : 1 }
      { "_id" : { "b" : 1, "c" : 2 }, "count" : 1 }
      

            Assignee:
            charlie.swanson@mongodb.com Charlie Swanson
            Reporter:
            charlie.swanson@mongodb.com Charlie Swanson
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: