How to update fields in cassandra frozen UDT column?

Viewed 963

I am aware of fact that fields in frozen UDT column is not possible and entire records needs to update , in that case does it imply update on frozen UDT column is not possible and if there is scenario of field update of frozen UDT column , in that case one has to insert new record and delete older one ?

2 Answers

You are correct that you cannot update individual fields of a frozen UDT column but you can update the whole column value. You do not need to delete the previous record. It's fine to update the fields directly. Let me illustrate with an example I created on Astra.

Here is a user-defined type that stores a user's address:

CREATE TYPE address (
  number int,
  street text,
  city text,
  zip int
)

and here is the definition for the table of users:

CREATE TABLE users (
  name text PRIMARY KEY,
  address frozen<address>
)

In this table, there is one user with their address stored as:

cqlsh> SELECT * FROM users ;

 name  | address
-------+----------------------------------------------------------------
 alice | {number: 100, street: 'Main Rd', city: 'Melbourne', zip: 3000}

Let's say that the street number is incorrect. If we try to update just the street number field with:

cqlsh> UPDATE users SET address = {number: 456} WHERE name = 'alice';

We'll end up with an address that only has the street number and nothing else:

cqlsh> SELECT * FROM users ;

 name  | address
-------+----------------------------------------------------
 alice | {number: 456, street: null, city: null, zip: null}

This is because the whole value (not just the street number field) got overwritten by the update. The correct way to update the street number is to explicitly set a value for all the fields of the address with:

cqlsh> UPDATE users SET address = {number: 456, street: 'Main Rd', city: 'Melbourne', zip: 3000} WHERE name = 'alice';

so we end up with:

cqlsh> SELECT * FROM users ;

 name  | address
-------+----------------------------------------------------------------
 alice | {number: 456, street: 'Main Rd', city: 'Melbourne', zip: 3000}

Cheers!

You can update column that is frozen UDT, but you'll need to insert all values for fields inside that UDT. So you can just do normal update of that column only

UPDATE table SET udt_col = new_value WHERE pk = ....

without need to delete something first, etc.

Basically, frozen value is just blob obtained by serializing UDT or collection, and stored as one cell inside row and having the single timestamp. That's different from the non-frozen value, where different pieces of UDT/collection could be stored in different places, and having different timestamps.

Related