Search code examples
databasecassandrauser-defined-typesnosql

User defined type including itself


Oke, maybe I am trying something stupid here and the errors I get already gave me a hint that I am. But is it possible to have a user defined type within itself in C*?

So this would be the result.

CREATE TYPE udt (
    id uuid,
    child frozen <udt>,
    childeren list <frozen <udt>>
);

InvalidRequest: code=2200 [Invalid query] message="Unknown type space.udt"

I can imagine that the type has to be declared. Before it can be used inside itself. So naturally I'd try something like this.

CREATE TYPE udt (
    id uuid
);

ALTER TYPE udt ADD child frozen <udt>;
--ALTER TYPE udt ADD children list <frozen <udt>>;

InvalidRequest: code=2200 [Invalid query] message="Cannot add new field child of type space.udt to type space.udt as this would create a circular reference"

This error message is introduced due to CASSANDRA-10339 which was reported after this post Cassandra 2.1: Recursion by nesting UDT's

In a relational database I understand this is a problem but why can't C* handle this. The data is not referencing itself in anyway so I would expect that "circular referencing" is happening within C* itself. But why is it a problem here? It only describes how the data should be structured. To me it would be like a json object where the object structure is repeated within itself.

If somebody would ask me this question I'd probably respond with "Why would you want this?"

So a real world example would be a kind of decision tree. So I'd have a table questionaire that has a user defined type question. This question holds the questions to ask next. This could be two attributes, in case of yes and no kind of questions, of type question. This hopefully clarifies what I'm trying to do.


Solution

  • If Cassandra does not support such operations then probably you would like to store your data structure as a json string within a regular text column (instead of UDT).