Cassandra Compatible CQL Data Types

August 9, 2015

Recently, I stumbled across this question on StackOverflow: Cassandra alter column type: which types are compatible?

I had to admit that the OP had me stumped on that one (at the time). I really didn't have an answer for him. But as a Cassandra MVP, this is one of those things that I should "just know." So, I set out to compile a comprehensive list of compatible Cassandra data types.


tl;dr;

List of compatible Cassandra data types:

(given as starting type, and types that type is allowed to be ALTERed into)

 starting type  compatible type(s) 
 ascii  blob, text, varchar 
 bigint  blob, timestamp, varint 
 int  blob, varint 
 text  blob, varchar 
 timestamp  bigint, blob, varint 
 timeuuid  blob, UUID 
 varchar  blob, text 

To solve this, I created a table for each Cassandra type (using Cassandra 2.2.0), each with enough columns for every other type. I inserted the number 1 into every column, and then attempted to convert that column to every possible type. Ex:

aploetz@cqlsh:typeconversion> ALTER TABLE varintconvert ALTER c3 TYPE int;

Observations:
  • blob seems to be the only ubiquitous type to convert to. You can change any column type to a blob.
  • varint successfully converts to the new date type, but the table is unqueryable after that. In fact, it kills my cqlsh connection and causes some additional weirdness to the table. Appears to be a bug in Cassandra (CASSANDRA-10027). If you have a varint column, I DO NOT RECOMMEND attempting to convert it to a date.
  • varchar and text are essentially interchangeable. In fact, describing a table consisting of varchar columns shows that they are in fact text columns.
  • timeuuid can be converted to a UUID, but the opposite is not true. This is because while all timeuuids are UUIDs, not all UUIDs are timeuuids (which is a version 1 UUID).
  • I did not expect it to work, but (wanting to be thorough) I did try changing columns into lists and sets of their same type...and it didn't work. I also attempted to change a set<t> to a list<t>, but that was not allowed, either.

Hope this helps!


Aaron Ploetz
Copyright © Aaron Ploetz 2010 -
All corporate trademarks are property of their respective owners, and are shown here for reference only.