[JAVA-3279] distinct on using non-sparse index throws NPE Created: 24/Apr/19  Updated: 28/Oct/23  Resolved: 06/Aug/19

Status: Closed
Project: Java Driver
Component/s: Async, Query Operations
Affects Version/s: None
Fix Version/s: 3.11.0

Type: Bug Priority: Minor - P4
Reporter: Simon Schenk Assignee: John Stewart (Inactive)
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

mongo-scala-driver 2.6.0
mongoDB 4.0


Issue Links:
Related
related to JAVA-3382 Observables should filter null values Closed

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



 Comments   
Comment by Githook User [ 06/Aug/19 ]

Author:

{'name': 'John Stewart', 'email': 'john.stewart@mongodb.com', 'username': 'jstewart-mongo'}

Message: Handle null results for reactive applications

JAVA-3279
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/cc1be4d952467687e6db9b8c1e5e04187d95769d

Comment by Ian Whalen (Inactive) [ 29/Apr/19 ]

Hey sschenk moving this from SCALA to JAVA because the bug is actually inside of the Java driver code.

Generated at Thu Feb 08 08:59:13 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.