8

May a Cassandra (CQL 3) map hold null values? I thought null values were permitted, but failure of my program suggests otherwise. Or is there a bug in the driver I am using?

The official documentation for CQL maps says:

A map is a typed set of key-value pairs, where keys are unique. Furthermore, note that the map are internally sorted by their keys and will thus always be returned in that order.

So the keys may not be null (otherwise sorting would be impossible), but there is no mention of a requirement that map values are not null.

I have a field that is a map<timestamp,uuid>, which I am trying to write to using values in a Java Map< Date, UUID >. One of the map values (UUIDs) is null. This seems to cause a NPE in the Cassandra client code (Cassandra version 1.2.6, called from DataStax Java driver 1.0.1) when marshalling the UUID of the map:

 java.lang.NullPointerException
    at org.apache.cassandra.utils.UUIDGen.decompose(UUIDGen.java:82)
    at org.apache.cassandra.cql.jdbc.JdbcUUID.decompose(JdbcUUID.java:55)
    at org.apache.cassandra.db.marshal.UUIDType.decompose(UUIDType.java:187)
    at org.apache.cassandra.db.marshal.UUIDType.decompose(UUIDType.java:43)
    at org.apache.cassandra.db.marshal.MapType.decompose(MapType.java:122)
    at org.apache.cassandra.db.marshal.MapType.decompose(MapType.java:29)
    at com.datastax.driver.core.BoundStatement.bind(BoundStatement.java:188)
    at [my method]

The UUIDGen.decompose(UUID) method has no special handling of a null UUID, hence the NPE. Contrast with JdbcBoolean.decompose(Boolean), which decomposes a null Boolean to an empty byte-buffer. Similarly, JdbcDate.decompose(Date) decomposes a null Date to an empty byte-buffer.


I can produce a similar problem if I have a map holding null integers (using a Java Map< Date, Integer > with a null value, for a Cassandra map<timestamp,int>), so this problem is not restricted to uuid values.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • Well, if they're going to permit nulls, it seems like the data types need to handle them consistently. So I'd say this is a bug. – Don Branson Jul 29 '14 at 14:28

1 Answers1

7

You are right, null values are not (yet?) supported inside Maps. I faced this thing before and like you couldn't find relative documentation -- In similar situation I help myself with cqlsh

A small test give you the answer

CREATE TABLE map_example (
  id text,
  m map<text, text>,
  PRIMARY KEY ((id))
)

try

insert into map_example (id, m ) VALUES ( 'a', {'key':null});

> Bad Request: null is not supported inside collections

HTH, Carlo

Carlo Bertuccini
  • 19,615
  • 3
  • 28
  • 39