问题
I was playing around with the user-defined types and found out you can do something like this:
cqlsh:test> CREATE TYPE ping(time timestamp);
cqlsh:test> CREATE TYPE pong(time timestamp, ping frozen <ping>);
cqlsh:test> ALTER TYPE ping ADD pong frozen <pong>;
cqlsh:test> DESC TYPE ping ;
CREATE TYPE test.ping (
time timestamp,
pong frozen<pong>
);
cqlsh:test> DESC TYPE pong ;
CREATE TYPE test.pong (
time timestamp,
ping frozen<ping>
);
Is this relevant for any use case?
回答1:
Just came across this while working on a closely related Java driver ticket.
The schema appears to be recursive, but that doesn't work when you actually try to insert data:
// (using int instead of time for the sake of clarity)
cqlsh:test> create type ping(pingid int);
cqlsh:test> create type pong(pongid int, ping frozen<ping>);
cqlsh:test> alter type ping ADD pong frozen<pong>;
cqlsh:test> create table foo(ping frozen<ping> primary key);
// These are OK:
cqlsh:test> insert into foo(ping) values( {pingid:1} );
cqlsh:test> insert into foo(ping) values(
{ pingid:1,
pong: { pongid:2,
ping: {pingid: 3}}} );
// But notice what happens when you nest one more level:
cqlsh:test> insert into foo(ping) values(
{ pingid:1,
pong: { pongid:2,
ping: {pingid: 3,
pong: {pongid: 4}}}} );
InvalidRequest: code=2200 [Invalid query] message="Unknown field 'pong' in value of user
defined type ping"
Looks like the ping
that was used at the time pong
was defined is a "copy" that didn't see the effect of the ALTER statement. So my guess is that recursivity is not allowed and there is a missing check. I'll update my answer when I get confirmation from the Cassandra developers.
One interesting side-effect is that you can delete neither ping
nor pong
after that :-)
EDIT: this is indeed something that ALTER TYPE
should not allow. See CASSANDRA-10339.
回答2:
Well, I haven't seen/heard of any of these use cases, but you could use such structures for storing trees or linked lists (even though Cassandra already supports lists so I'm not sure what the benefit would be).
来源:https://stackoverflow.com/questions/29037733/cassandra-2-1-recursion-by-nesting-udts