Binding a java.util.Set to an in ? query

387 views
Skip to first unread message

Jens Teglhus Møller

unread,
Feb 2, 2018, 3:53:15 AM2/2/18
to DataStax Java Driver for Apache Cassandra User Mailing List
Hi

When I bind a Set to a "select * from mytable where colx in ?" like:

ps.bind(EnumSet.of(E1, E2))

I get the following exception:

Codec not found for requested operation: [list<varchar> <-> java.util.RegularEnumSet]: com.datastax.driver.core.exceptions.CodecNotFoundException com.datastax.driver.core.exceptions.CodecNotFoundException: Codec not found for requested operation: [list<varchar> <-> java.util.RegularEnumSet]
  at com.datastax.driver.core.CodecRegistry.notFound(CodecRegistry.java:741)
  at com.datastax.driver.core.CodecRegistry.createCodec(CodecRegistry.java:602)
  at com.datastax.driver.core.CodecRegistry.findCodec(CodecRegistry.java:582)
  at com.datastax.driver.core.CodecRegistry.codecFor(CodecRegistry.java:507)
  at com.datastax.driver.core.AbstractGettableByIndexData.codecFor(AbstractGettableByIndexData.java:77)
  at com.datastax.driver.core.BoundStatement.bind(BoundStatement.java:201)
  at com.datastax.driver.core.DefaultPreparedStatement.bind(DefaultPreparedStatement.java:126)

If I instead convert the Set to a List like:

ps.bind(new ArrayList(EnumSet.of(E1, E2))) it works as expected, but that is an ugly workaround.

Is this a bug or expected behaviour?

I'm fan of strong typing and try to use enums as much as possible and from that follows the need of using Sets in in clauses, in the above example I have registed the appropriate EnumNameCodec.

This was tested with driver version 3.3.2.

Best regards Jens

Andrew Tolbert

unread,
Feb 2, 2018, 12:04:27 PM2/2/18
to DataStax Java Driver for Apache Cassandra User Mailing List
Hi Jens,

I think this is not working because colx must be a list<varchar> is that right?   If colx is a list, it expects a java.util.List type.  The driver doesn't register a Codec that binds java.util.Set to the CQL list type.   You could consider doing one of the following:

  • Change colx to set<varchar>.  Since you want to use enums as input, you probably also want to enforce unique elements, so using set instead of list seems like a good option anyways.
  • Register a custom codec from java.util.Set <-> cql list.

Thanks,
Andy

Jens Teglhus Møller

unread,
Feb 2, 2018, 5:10:52 PM2/2/18
to DataStax Java Driver for Apache Cassandra User Mailing List
Hi Andy

Thanks for your response. 

colx is a plain varchar column, and I have registred the enum with EnumNameCodec.

Regular queries like "select * from mytable where colx = ?" where I bind a single enum value, works as expected.

When doing "where colx in ?" I would expect Set and List to be treated equally (sql semantics).

I can write up a complete example with create table statements, codec registration etc if I'm still unclear.

Best regards Jens

Andy Tolbert

unread,
Feb 2, 2018, 5:16:20 PM2/2/18
to DataStax Java Driver for Apache Cassandra User Mailing List
Hi Jens,

When doing "where colx in ?" I would expect Set and List to be treated equally (sql semantics)

The driver doesn't currently work this way.  CQL set type parameters expect java.util.Set implementations, where list type parameters expect java.util.List

If you would like this capability, please create a JIRA ticket.

Thanks!
Andy

--
You received this message because you are subscribed to the Google Groups "DataStax Java Driver for Apache Cassandra User Mailing List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-user+unsubscribe@lists.datastax.com.

Jens Teglhus Møller

unread,
Feb 6, 2018, 6:43:09 AM2/6/18
to DataStax Java Driver for Apache Cassandra User Mailing List
Hi Andy

Thanks again, I just created https://datastax-oss.atlassian.net/browse/JAVA-1743, let me know if any more clarification is needed (I can provide a github project with full code demonstration my issue, but that will probably take me a couple of hours to setup).

Best regards Jens
To unsubscribe from this group and stop receiving emails from it, send an email to java-driver-us...@lists.datastax.com.

Andrew Tolbert

unread,
Feb 6, 2018, 11:53:59 AM2/6/18
to DataStax Java Driver for Apache Cassandra User Mailing List
Thanks Jens!  The jira ticket is well explained, I do not think any additional information is needed.

Andy
Reply all
Reply to author
Forward
0 new messages