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

$coerceToBool cannot be serialized, causes verify assertion when some optimized expressions are sent to shards

    • Fully Compatible
    • ALL

      $coerceToBool cannot be serialized or parsed as an operator in a bson aggregation expression. And it can't be provided by a user query. However, it can be generated when another query is optimized. And because optimization happens before the pipeline is split for shards, an error can be triggered when attempting to send it to a shard.

      Test

      // Set up a sharding test.                                                                                                             
      s = new ShardingTest( "aggregation", 2, 0, 2 );
      s.adminCommand( { enablesharding:"test" } );
      s.adminCommand( { shardcollection:"test.data", key:{ _id:1 } } );
      s.stopBalancer()
      
      d = s.getDB( "test" );
      d.data.save( { a:true } );
      
      // The 'bool' part is optimized to { $coerceToBool:'$a' }.                                                                              
      result = d.data.aggregate( { $project:{ _id:0, bool:{ $and:[ '$a', true, true ] } } } );
      
      printjson( result );
      assert.eq( [ { bool:true } ], result.result );
      
      s.stop();
      

      Output

       m30999| Thu Aug 16 10:33:33 [conn1]   Assertion failure false && "not possible" src/mongo/db/pipeline/expression.cpp 531
       m30999| 0x109420a05 0x1095d458b 0x1096d3069 0x1094ae42a 0x1094b52a4 0x1091eeff2 0x10932e9bd 0x109383313 0x1094f6215 0x1094f7fd3 0x109642f9c 0x109672081 0x109641469 0x1091c72b3 0x1094ec633 0x1094edd70 0x1094eddd2 0x1094eddfd 0x10978d199 0x10a81f8bf 
       m30999|  0   mongos                              0x0000000109420a05 _ZN5mongo15printStackTraceERSo + 37
       m30999|  1   mongos                              0x00000001095d458b _ZN5mongo10logContextEPKc + 123
       m30999|  2   mongos                              0x00000001096d3069 _ZN5mongo12verifyFailedEPKcS1_j + 409
       m30999|  3   mongos                              0x00000001094ae42a _ZNK5mongo22ExpressionCoerceToBool14addToBsonArrayEPNS_16BSONArrayBuilderE + 0
       m30999|  4   mongos                              0x00000001094b52a4 _ZNK5mongo16ExpressionObject14documentToBsonEPNS_14BSONObjBuilderEb + 732
       m30999|  5   mongos                              0x00000001091eeff2 _ZNK5mongo21DocumentSourceProject12sourceToBsonEPNS_14BSONObjBuilderEb + 112
       m30999|  6   mongos                              0x000000010932e9bd _ZNK5mongo14DocumentSource14addToBsonArrayEPNS_16BSONArrayBuilderEb + 101
       m30999|  7   mongos                              0x0000000109383313 _ZNK5mongo8Pipeline6toBsonEPNS_14BSONObjBuilderE + 283
       m30999|  8   mongos                              0x00000001094f6215 _ZN5mongo15dbgrid_pub_cmds15PipelineCommand3runERKSsRNS_7BSONObjEiRSsRNS_14BSONObjBuilderEb + 1543
       m30999|  9   mongos                              0x00000001094f7fd3 _ZN5mongo7Command20runAgainstRegisteredEPKcRNS_7BSONObjERNS_14BSONObjBuilderEi + 3329
       m30999|  10  mongos                              0x0000000109642f9c _ZN5mongo14SingleStrategy7queryOpERNS_7RequestE + 1230
       m30999|  11  mongos                              0x0000000109672081 _ZN5mongo13ShardStrategy7queryOpERNS_7RequestE + 99
       m30999|  12  mongos                              0x0000000109641469 _ZN5mongo7Request7processEi + 779
       m30999|  13  mongos                              0x00000001091c72b3 _ZN5mongo21ShardedMessageHandler7processERNS_7MessageEPNS_21AbstractMessagingPortEPNS_9LastErrorE + 243
       m30999|  14  mongos                              0x00000001094ec633 _ZN5mongo3pms9threadRunEPNS_13MessagingPortE + 2275
       m30999|  15  mongos                              0x00000001094edd70 _ZN5boost3_bi5list1INS0_5valueIPN5mongo13MessagingPortEEEEclIPFvS5_ENS0_5list0EEEvNS0_4typeIvEERT_RT0_i + 78
       m30999|  16  mongos                              0x00000001094eddd2 _ZN5boost3_bi6bind_tIvPFvPN5mongo13MessagingPortEENS0_5list1INS0_5valueIS4_EEEEEclEv + 92
       m30999|  17  mongos                              0x00000001094eddfd _ZN5boost6detail11thread_dataINS_3_bi6bind_tIvPFvPN5mongo13MessagingPortEENS2_5list1INS2_5valueIS6_EEEEEEE3runEv + 37
       m30999|  18  mongos                              0x000000010978d199 thread_proxy + 169
       m30999|  19  libsystem_c.dylib                   0x000000010a81f8bf _pthread_start + 335
      {
      	"errmsg" : "exception: assertion src/mongo/db/pipeline/expression.cpp:531",
      	"code" : 0,
      	"ok" : 0
      }
      assert: [[ { "bool" : true } ]] != [undefined] are not equal : undefined
      Error("Printing Stack Trace")@:0
      ()@src/mongo/shell/utils.js:37
      ("[[ { \"bool\" : true } ]] != [undefined] are not equal : undefined")@src/mongo/shell/utils.js:58
      ([object Array],undefined)@src/mongo/shell/utils.js:88
      @test.js:13
      

            Assignee:
            mathias@mongodb.com Mathias Stearn
            Reporter:
            aaron Aaron Staple
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: