Bringing together the Apache Cassandra experts from the community and DataStax.

Want to learn? Have a question? Want to share your expertise? You are in the right place!

Not sure where to begin? Getting Started

 

question

mishra.anurag643_153409 avatar image
mishra.anurag643_153409 asked Erick Ramirez edited

How do I update fields of a frozen UDT?

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 ?

user-defined type
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

1 Answer

Erick Ramirez avatar image
Erick Ramirez answered Erick Ramirez edited

All components of a frozen column is serialised into a single value. This applies to collections, tuples and user-defined types. This means that regardless of how many fields, elements or items there are in the value, it is treated as a single atomic value.

You are correct that you cannot update individual fields of a frozen UDT column but you can update the whole column value. Let me illustrate with an example.

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!

Share
10 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.