Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-3279

distinct on using non-sparse index throws NPE

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 3.11.0
    • Affects Version/s: None
    • Component/s: Async, Query Operations
    • None
    • Environment:
      mongo-scala-driver 2.6.0
      mongoDB 4.0

      Calling `.distinct` on a collection with a non-sparse index on a field, that is missing from at least one document, results in a NullPointerException. The reason is that due to the index not being sparse, null is added to the distinct values for the missing field. `AbstractSubscription` checks if the entire returned array is null (which it isn't), but not the individual elements. `resultsQueue.addAll(results)` then fails with an NPE.

      Steps to reproduce:

      ```scala
      val db = client.getDatabase("foo")
      val coll = db.getCollection[Document]("bar")
      for

      { _ <- coll.createIndex(ascending("a")).toFuture() _ <- coll.insertOne(Document("b" -> 1)).toFuture() dist <- coll.distinct[String]("a").toFuture() }

      yield dist should contain theSameElementsAs Seq(null)
      ```

      ```
      10:27:42.110 [zio-default-async-6-352707738] DEBUG o.m.d.p.command - Sending command '{"distinct": "bar", "key": "a", "$db": "foo", "$readPreference": {"mode": "primaryPreferred"}}' with request id 10 to database foo on connection [connectionId

      {localValue:2, serverValue:2}

      ] to server localhost:12345
      10:27:42.111 [Thread-20] DEBUG o.m.d.p.command - Execution of command with request id 10 completed successfully in 1.78 ms on connection [connectionId

      {localValue:2, serverValue:2}

      ] to server localhost:12345
      10:27:42.115 [Thread-20] ERROR o.m.d.client - Callback onResult call produced an error
      java.lang.NullPointerException: null
      at java.util.concurrent.ConcurrentLinkedQueue.checkNotNull(ConcurrentLinkedQueue.java:920)
      at java.util.concurrent.ConcurrentLinkedQueue.addAll(ConcurrentLinkedQueue.java:531)
      at com.mongodb.async.client.AbstractSubscription.addToQueue(AbstractSubscription.java:116)
      at com.mongodb.async.client.MongoIterableSubscription$2.onResult(MongoIterableSubscription.java:95)
      at com.mongodb.async.client.MongoIterableSubscription$2.onResult(MongoIterableSubscription.java:85)
      ...
      ```

            Assignee:
            john.stewart@mongodb.com John Stewart (Inactive)
            Reporter:
            simon.schenk@risk42.com Simon Schenk
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: