Search code examples
c++cassandranesteddatastaxuser-defined-types

DataStax C++ Cassandra Nested UDTs


In the DataStax C++ Driver, I attempt to set a user-type to another user-type (nested UDT).

However, for some reason I get an error when trying to set the field of a user-defined-type with another user-defined-type.

CassDataType* outerDataType = cass_data_type_new_udt(1);
cass_data_type_add_sub_value_type_by_name(outerDataType, "OUTER_FIELD", CASS_VALUE_TYPE_UDT);
CassUserType* outerUserType = cass_user_type_new_from_data_type(outerDataType);

CassDataType* innerDataType = cass_data_type_new_udt(1);
cass_data_type_add_sub_value_type_by_name(innerDataType, "INNER_FIELD", CASS_VALUE_TYPE_INT);
CassUserType* innerUserType = cass_user_type_new_from_data_type(innerDataType);

// Bind values to user type fields 

// No error, CASS_OK
CassError err = cass_user_type_set_int32_by_name(innerUserType, "INNER_FIELD", 32);

// CASS_ERROR_LIB_INVALID_VALUE_TYPE
err = cass_user_type_set_user_type_by_name(outerUserType, "OUTER_FIELD", innerUserType);

// Error: Invalid value type
printf(cass_error_desc(err)); 

cass_data_type_free(innerDataType);
cass_data_type_free(outerDataType);

cass_user_type_free(innerUserType);
cass_user_type_free(outerUserType);

Does anyone know why this is the case? Cassandra itself appears to allow for nested UDTs.

  • Is there something wrong with how I'm setting the UDT fields?
  • Is nesting UDTs not possible in the C++ driver? What would the function cass_user_type_set_user_type() be for if this is the case?

Solution

  • I changed the above to get the user-defined-type from the Cassandra schema instead. My original problem involved using such a type.

    The problem ended up being an issue of case-sensitivity.

    For some reason, certain calls in the DataStax C++ driver are case sensitive, like cass_keyspace_meta_user_type_by_name, while some are not such as cass_user_type_set_user_type_by_name.

    DataStax C++ does indeed allow for nested UDTs. Just be careful with their names upon defining them in Cassandra.