You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cassandra.apache.org by Henry M <he...@gmail.com> on 2016/04/08 04:40:45 UTC

Returning an UDT from a user defined function (UDF)

I was wondering if it is possible to create an UDT and return it within a
user defined function.

I looked at this documentation
http://docs.datastax.com/en/cql/3.3/cql/cql_using/useCreateUDF.html but the
examples are only for basic types.

This is my pseudo code I came up with... the part I think I am missing is
how to get an instance of the UserType so that I can invoke newValue to
create a UDTValue.

Has anyone done this and know how to get the keyspace in order to call
getUserType? Or know of an alternate approach?

CREATE OR REPLACE FUNCTION test_ks.transform_udt (val my_udt)
 RETURNS NULL ON NULL INPUT
 RETURNS my_other_udt
 LANGUAGE java
  AS '
    String fieldA = val.getString("field_a");

    // How do you get a reference the user type?
    UserType myUdt = ?keyspace?.getUserType("my_other_udt");

    UDTValue transformedValue = myUdt.newValue();

    transformedValue.setUUID("id", UUID.randomUUID());
    transformedValue.setString("field_a", fieldA);
    transformedValue.setString("field_b", "value b");

    return transformedValue;
  ';


Thank you,
Henry


P.S. This is the setup for my sample table and types.

drop keyspace test_ks;

create keyspace test_ks WITH REPLICATION = { 'class' :
'SimpleStrategy', 'replication_factor' : 1 };

use test_ks;

CREATE TYPE IF NOT EXISTS test_ks.my_udt (field_a text, field_b text);
CREATE TYPE IF NOT EXISTS test_ks.my_other_udt (id uuid, field_a text,
field_b text);

CREATE TABLE IF NOT EXISTS test_ks.sample_table(id uuid primary key,
col_a frozen<test_ks.my_udt>);

INSERT INTO sample_table(id, col_a) VALUES ( now() , { field_a: 'value
1', field_b: 'value 2'} );
INSERT INTO sample_table(id) VALUES ( now() );

Re: Returning an UDT from a user defined function (UDF)

Posted by Robert Stupp <sn...@snazy.de>.
Hi Henry,

there’s https://issues.apache.org/jira/browse/CASSANDRA-10818 <https://issues.apache.org/jira/browse/CASSANDRA-10818> to allow creation of UDTs and tuples.

Robert

—
Robert Stupp
@snazy

> On Apr 8, 2016, at 07:12, Henry M <he...@gmail.com> wrote:
> 
> Whatever I wanted to do does not seem to be possible (probably a limitation or a bug)... I see a way to get the KeyspaceMetadata and from that get the UserType instance (code lines 1 & 2 below).
> 
> 1.)
> org.apache.cassandra.schema.KeyspaceMetadata ksm = org.apache.cassandra.config.Schema.instance.getKSMetaData("test_ks");
> 
> 2.)
> com.datastax.driver.core.UserType myUdt = ksm.types.get("my_other_udt").get();
> 
> But this fails with the below error because Schema is not a whitelisted package. It probably should not be whitelisted but there should be a way to create and return a user defined type.
> 
> <stdin>:88:InvalidRequest: code=2200 [Invalid query] message="Could not compile function 'test_ks.transform_udt' from Java source: org.apache.cassandra.exceptions.InvalidRequestException: Java source compilation failed:
> Line 4: org.apache.cassandra.schema.KeyspaceMetadata cannot be resolved to a type
> Line 4: org.apache.cassandra.config.Schema.instance cannot be resolved to a type
> "
> <stdin>:90:InvalidRequest: code=2200 [Invalid query] message="Unknown function 'extract_text_field_sample_udt'"
> 
> My updated UDF for complete context.
> 
> CREATE OR REPLACE FUNCTION test_ks.transform_udt (val my_udt)
>  RETURNS NULL ON NULL INPUT
>  RETURNS my_other_udt
>  LANGUAGE java
>   AS '
>     String fieldA = val.getString("field_a");
> 
>     org.apache.cassandra.schema.KeyspaceMetadata ksm = org.apache.cassandra.config.Schema.instance.getKSMetaData("test_ks");
> 
>     com.datastax.driver.core.UserType myUdt = ksm.types.get("my_other_udt").get();
> 
>     com.datastax.driver.core.UDTValue transformedValue = myUdt.newValue();
> 
>     transformedValue.setUUID("id", java.util.UUID.randomUUID());
>     transformedValue.setString("field_a", fieldA);
>     transformedValue.setString("field_b", "value b");
> 
>     return transformedValue;
>   ';
> 
> 
> On Thu, Apr 7, 2016 at 7:40 PM Henry M <henrymanmail@gmail.com <ma...@gmail.com>> wrote:
> I was wondering if it is possible to create an UDT and return it within a user defined function.
> 
> I looked at this documentation http://docs.datastax.com/en/cql/3.3/cql/cql_using/useCreateUDF.html <http://docs.datastax.com/en/cql/3.3/cql/cql_using/useCreateUDF.html> but the examples are only for basic types.
> 
> This is my pseudo code I came up with... the part I think I am missing is how to get an instance of the UserType so that I can invoke newValue to create a UDTValue.
> 
> Has anyone done this and know how to get the keyspace in order to call getUserType? Or know of an alternate approach?
> 
> CREATE OR REPLACE FUNCTION test_ks.transform_udt (val my_udt)
>  RETURNS NULL ON NULL INPUT
>  RETURNS my_other_udt
>  LANGUAGE java
>   AS '
>     String fieldA = val.getString("field_a");
> 
>     // How do you get a reference the user type?
>     UserType myUdt = ?keyspace?.getUserType("my_other_udt");
> 
>     UDTValue transformedValue = myUdt.newValue();
> 
>     transformedValue.setUUID("id", UUID.randomUUID());
>     transformedValue.setString("field_a", fieldA);
>     transformedValue.setString("field_b", "value b");
> 
>     return transformedValue;
>   ';
> 
> 
> Thank you,
> Henry
> 
> 
> P.S. This is the setup for my sample table and types.
> drop keyspace test_ks;
> 
> create keyspace test_ks WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
> 
> use test_ks;
> 
> CREATE TYPE IF NOT EXISTS test_ks.my_udt (field_a text, field_b text);
> CREATE TYPE IF NOT EXISTS test_ks.my_other_udt (id uuid, field_a text, field_b text);
> 
> CREATE TABLE IF NOT EXISTS test_ks.sample_table(id uuid primary key, col_a frozen<test_ks.my_udt>);
> 
> INSERT INTO sample_table(id, col_a) VALUES ( now() , { field_a: 'value 1', field_b: 'value 2'} );
> INSERT INTO sample_table(id) VALUES ( now() );
> 
> 
> 
> 
> 


Re: Returning an UDT from a user defined function (UDF)

Posted by Henry M <he...@gmail.com>.
Whatever I wanted to do does not seem to be possible (probably a limitation
or a bug)... I see a way to get the KeyspaceMetadata and from that get the
UserType instance (code lines 1 & 2 below).

1.)

org.apache.cassandra.schema.KeyspaceMetadata ksm =
org.apache.cassandra.config.Schema.instance.getKSMetaData("test_ks");


2.)

com.datastax.driver.core.UserType myUdt = ksm.types.get("my_other_udt").get();


But this fails with the below error because Schema is not a whitelisted
package. It probably should not be whitelisted but there should be a way to
create and return a user defined type.

<stdin>:88:InvalidRequest: code=2200 [Invalid query] message="Could not
compile function 'test_ks.transform_udt' from Java source:
org.apache.cassandra.exceptions.InvalidRequestException: Java source
compilation failed:
Line 4: org.apache.cassandra.schema.KeyspaceMetadata cannot be resolved to
a type
Line 4: org.apache.cassandra.config.Schema.instance cannot be resolved to a
type
"
<stdin>:90:InvalidRequest: code=2200 [Invalid query] message="Unknown
function 'extract_text_field_sample_udt'"

My updated UDF for complete context.

CREATE OR REPLACE FUNCTION test_ks.transform_udt (val my_udt)
 RETURNS NULL ON NULL INPUT
 RETURNS my_other_udt
 LANGUAGE java
  AS '
    String fieldA = val.getString("field_a");

    org.apache.cassandra.schema.KeyspaceMetadata ksm =
org.apache.cassandra.config.Schema.instance.getKSMetaData("test_ks");

    com.datastax.driver.core.UserType myUdt =
ksm.types.get("my_other_udt").get();

    com.datastax.driver.core.UDTValue transformedValue = myUdt.newValue();

    transformedValue.setUUID("id", java.util.UUID.randomUUID());
    transformedValue.setString("field_a", fieldA);
    transformedValue.setString("field_b", "value b");

    return transformedValue;
  ';


On Thu, Apr 7, 2016 at 7:40 PM Henry M <he...@gmail.com> wrote:

> I was wondering if it is possible to create an UDT and return it within a
> user defined function.
>
> I looked at this documentation
> http://docs.datastax.com/en/cql/3.3/cql/cql_using/useCreateUDF.html but
> the examples are only for basic types.
>
> This is my pseudo code I came up with... the part I think I am missing is
> how to get an instance of the UserType so that I can invoke newValue to
> create a UDTValue.
>
> Has anyone done this and know how to get the keyspace in order to call
> getUserType? Or know of an alternate approach?
>
> CREATE OR REPLACE FUNCTION test_ks.transform_udt (val my_udt)
>  RETURNS NULL ON NULL INPUT
>  RETURNS my_other_udt
>  LANGUAGE java
>   AS '
>     String fieldA = val.getString("field_a");
>
>     // How do you get a reference the user type?
>     UserType myUdt = ?keyspace?.getUserType("my_other_udt");
>
>     UDTValue transformedValue = myUdt.newValue();
>
>     transformedValue.setUUID("id", UUID.randomUUID());
>     transformedValue.setString("field_a", fieldA);
>     transformedValue.setString("field_b", "value b");
>
>     return transformedValue;
>   ';
>
>
> Thank you,
> Henry
>
>
> P.S. This is the setup for my sample table and types.
>
> drop keyspace test_ks;
>
> create keyspace test_ks WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 1 };
>
> use test_ks;
>
> CREATE TYPE IF NOT EXISTS test_ks.my_udt (field_a text, field_b text);
> CREATE TYPE IF NOT EXISTS test_ks.my_other_udt (id uuid, field_a text, field_b text);
>
> CREATE TABLE IF NOT EXISTS test_ks.sample_table(id uuid primary key, col_a frozen<test_ks.my_udt>);
>
> INSERT INTO sample_table(id, col_a) VALUES ( now() , { field_a: 'value 1', field_b: 'value 2'} );
> INSERT INTO sample_table(id) VALUES ( now() );
>
>
>
>
>
>