[JAVA-2386] Exception when a null value is included in the results of the distinct command Created: 16/Nov/16  Updated: 21/Nov/16  Resolved: 21/Nov/16

Status: Closed
Project: Java Driver
Component/s: Query Operations
Affects Version/s: 3.3.0
Fix Version/s: 3.4.0

Type: Bug Priority: Major - P3
Reporter: Jim Dunne Assignee: Ross Lawley
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

MongoDB server 3.2.8

Windows 7 x64
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)



 Description   

Using the async Java driver and running a `distinct` query over a string-typed field that may contain null values results in `org.bson.BsonInvalidOperationException` being thrown. Example code using the `distinct` query operation, sanitized and simplified:

mongoCollection
  .distinct("stringFieldName", String.class)
  .into(new ArrayList<String>(), asyncCallback)

A workaround is available to filter out null values:

mongoCollection
  .distinct("stringFieldName", Filters.ne("stringFieldName", new BsonNull()), String.class)
  .into(new ArrayList<String>(), asyncCallback)

Exception:

org.bson.BsonInvalidOperationException: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is NULL.
	at org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:655)
	at org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:687)
	at org.bson.AbstractBsonReader.readString(AbstractBsonReader.java:428)
	at org.bson.codecs.StringCodec.decode(StringCodec.java:39)
	at org.bson.codecs.StringCodec.decode(StringCodec.java:28)
	at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:48)
	at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:53)
	at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
	at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
	at com.mongodb.connection.ReplyMessage.<init>(ReplyMessage.java:57)
	at com.mongodb.connection.CommandProtocol.getResponseDocument(CommandProtocol.java:139)



 Comments   
Comment by Ross Lawley [ 21/Nov/16 ]

Thanks for the ticket jim.dunne, this is fixed in master and will be released as part of 3.4.0

Comment by Githook User [ 21/Nov/16 ]

Author:

{u'username': u'rozza', u'name': u'Ross Lawley', u'email': u'ross.lawley@gmail.com'}

Message: Explicitly handle null values in the CommandResultArrayCodec

Allows null values to be handled when using distinct
JAVA-2386
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/aa0adf06f553bba5db1698fa9b2543b8dc5e53d0

Comment by Jeffrey Yemin [ 17/Nov/16 ]

Yes, you could use the BsonValue class, in which case you will get back a series of BsonString instances plus one BsonNull instance.

Comment by Jim Dunne [ 17/Nov/16 ]

I get the same error when resultClass is BsonString.class. Is there a resultClass that would make this work without the not-null filter query?

Comment by Ross Lawley [ 17/Nov/16 ]

Hi jim.dunne,

Thats an interesting corner case for distinct as the results returned are strictly typed according to the resultClass and variants to that type are expected to fail. However, as always `null` is exceptional in Java! We'll look into a fix for the distinct method.

Ross

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