Question

I am having trouble changing the type of the column in a table in cassandra.

Here is the link to the documentation which says that it is possible to change the type if the column is not part of the PRIMARY KEY and does not have an INDEX on it. I just can't get it to work.

cqlsh:test> show VERSION  
[cqlsh 3.1.8 | Cassandra 1.2.13 | CQL spec 3.0.0 | Thrift protocol 19.36.2]

CREATE TABLE mytable (
  id1 text,
  id2 text,
  col1 int,
  col2 bigint,
  col3 text,
  PRIMARY KEY (id1, id2)
) WITH CLUSTERING ORDER BY (id2 DESC);

UPDATE mytable SET col1 = 123, col2 = 1234, col3 = '12345' WHERE id1 = 'id1' AND id2 = 'id2';

SELECT * FROM mytable ;

 id1 | id2 | col1 | col2 | col3
-----+-----+------+------+-------
 id1 | id2 |  123 | 1234 | 12345

When I attempt to change column types, I get errors.

 ALTER TABLE mytable ALTER col1 TYPE text;
 Bad Request: Cannot change col1 from type int to type text: types are incompatible.

 ALTER TABLE mytable ALTER col1 TYPE text;
 Bad Request: Cannot change col1 from type int to type text: types are incompatible.

 ALTER TABLE mytable ALTER col2 TYPE text;
 Bad Request: Cannot change col2 from type bigint to type text: types are incompatible.

 ALTER TABLE mytable ALTER col3 TYPE bigint;
 Bad Request: Cannot change col3 from type text to type bigint: types are incompatible.

 ALTER TABLE mytable ALTER col3 TYPE int;
 Bad Request: Cannot change col3 from type text to type int: types are incompatible.

What am I missing or doing wrong? Any help is appreciated.

Was it helpful?

Solution

From the page you linked:

The bytes stored in values for that column remain unchanged, and if existing data cannot be [deserialized] according to the new type, your CQL driver or interface might report errors

Thus, you can only change between compatible types.

This is primarily intended for people upgrading from a Thrift schema where it was common to leave all columns defined as a blob.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top