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

distinct on using non-sparse index throws NPE

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor - P4
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.11.0
    • Component/s: Asyc, Query Operations
    • Labels:
      None
    • Environment:
      mongo-scala-driver 2.6.0
      mongoDB 4.0

      Description

      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)
      ...
      ```

        Attachments

          Issue Links

            Activity

              People

              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: