Cassandra 2.1: Recursion by nesting UDT's

假装没事ソ 提交于 2019-12-11 01:58:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!