How to create an update statement where a UDT value need to be updated using QueryBuilder

Author: Manu Chadha

Originally Sourced from: https://stackoverflow.com/questions/57315604/how-to-create-an-update-statement-where-a-udt-value-need-to-be-updated-using-que

I have the following udt type

CREATE TYPE tag_partitions(
  year bigint,
  month bigint);

and the following table

CREATE TABLE ${tableName} (
  tag text,
  partition_info set<FROZEN<tag_partitions>>,
  PRIMARY KEY ((tag))
)

The table schema is mapped using the following model

case class TagPartitionsInfo(year:Long, month:Long)
case class TagPartitions(tag:String, partition_info:Set[TagPartitionsInfo])

I have written a function which should create an Update.IfExists query: But I don't know how I should update the udt value. I tried to use set but it isn't working.

def updateValues(tableName:String, model:TagPartitions, id:TagPartitionKeys):Update.IfExists = {
   val partitionInfoType:UserType = session.getCluster().getMetadata
       .getKeyspace("codingjedi").getUserType("tag_partitions")
   //create value
   //the logic below assumes that there is only one element in the set
   val partitionsInfoSet:Set[UDTValue] = model.partition_info.map((partitionInfo:TagPartitionsInfo) =>{
      partitionInfoType.newValue()
        .setLong("year",partitionInfo.year)
        .setLong("month",partitionInfo.month)
   })
   println("partition info converted to UDTValue: "+partitionsInfoSet)

   QueryBuilder.update(tableName).
      `with`(QueryBuilder.WHAT_TO_DO_HERE_TO_UPDATE_UDT("partition_info",partitionsInfoSet))
      .where(QueryBuilder.eq("tag", id.tag)).ifExists()
}