Search in sources :

Example 16 with ProtocolVersion

use of org.apache.cassandra.transport.ProtocolVersion in project cassandra by apache.

the class CQL3TypeLiteralTest method testTupleWithNatives.

@Test
public void testTupleWithNatives() {
    for (ProtocolVersion version : ProtocolVersion.SUPPORTED) {
        for (int n = 0; n < 100; n++) {
            Value value = generateTupleValue(version, randomTupleType(0), true);
            compareCqlLiteral(version, value);
        }
    }
}
Also used : ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) Test(org.junit.Test)

Example 17 with ProtocolVersion

use of org.apache.cassandra.transport.ProtocolVersion in project cassandra by apache.

the class CQL3TypeLiteralTest method testCollectionNullAndEmpty.

@Test
public void testCollectionNullAndEmpty() {
    // An empty collection is one with a size of 0 (note that rely on the fact that protocol version < 3 are not
    // supported anymore and so the size of a collection is always on 4 bytes).
    ByteBuffer emptyCollection = ByteBufferUtil.bytes(0);
    for (ProtocolVersion version : ProtocolVersion.SUPPORTED) {
        for (boolean frozen : Arrays.asList(true, false)) {
            // empty
            Value value = new Value("[]", ListType.getInstance(UTF8Type.instance, frozen).asCQL3Type(), emptyCollection);
            compareCqlLiteral(version, value);
            value = new Value("{}", SetType.getInstance(UTF8Type.instance, frozen).asCQL3Type(), emptyCollection);
            compareCqlLiteral(version, value);
            value = new Value("{}", MapType.getInstance(UTF8Type.instance, UTF8Type.instance, frozen).asCQL3Type(), emptyCollection);
            compareCqlLiteral(version, value);
            // null
            value = new Value("null", ListType.getInstance(UTF8Type.instance, frozen).asCQL3Type(), null);
            compareCqlLiteral(version, value);
            value = new Value("null", SetType.getInstance(UTF8Type.instance, frozen).asCQL3Type(), null);
            compareCqlLiteral(version, value);
            value = new Value("null", MapType.getInstance(UTF8Type.instance, UTF8Type.instance, frozen).asCQL3Type(), null);
            compareCqlLiteral(version, value);
        }
    }
}
Also used : ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Example 18 with ProtocolVersion

use of org.apache.cassandra.transport.ProtocolVersion in project cassandra by apache.

the class UFJavaTest method testJavaUTCollections.

@Test
public void testJavaUTCollections() throws Throwable {
    String type = KEYSPACE + '.' + createType("CREATE TYPE %s (txt text, i int)");
    createTable(String.format("CREATE TABLE %%s " + "(key int primary key, lst list<frozen<%s>>, st set<frozen<%s>>, mp map<int, frozen<%s>>)", type, type, type));
    String fName1 = createFunction(KEYSPACE, "list<frozen<" + type + ">>", "CREATE FUNCTION %s( lst list<frozen<" + type + ">> ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS text " + "LANGUAGE java\n" + "AS $$" + "     com.datastax.driver.core.UDTValue udtVal = (com.datastax.driver.core.UDTValue)lst.get(1);" + "     return udtVal.getString(\"txt\");$$;");
    String fName2 = createFunction(KEYSPACE, "set<frozen<" + type + ">>", "CREATE FUNCTION %s( st set<frozen<" + type + ">> ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS text " + "LANGUAGE java\n" + "AS $$" + "     com.datastax.driver.core.UDTValue udtVal = (com.datastax.driver.core.UDTValue)st.iterator().next();" + "     return udtVal.getString(\"txt\");$$;");
    String fName3 = createFunction(KEYSPACE, "map<int, frozen<" + type + ">>", "CREATE FUNCTION %s( mp map<int, frozen<" + type + ">> ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS text " + "LANGUAGE java\n" + "AS $$" + "     com.datastax.driver.core.UDTValue udtVal = (com.datastax.driver.core.UDTValue)mp.get(Integer.valueOf(3));" + "     return udtVal.getString(\"txt\");$$;");
    execute("INSERT INTO %s (key, lst, st, mp) values (1, " + "[ {txt: 'one', i:1}, {txt: 'three', i:1}, {txt: 'one', i:1} ] , " + "{ {txt: 'one', i:1}, {txt: 'three', i:3}, {txt: 'two', i:2} }, " + "{ 1: {txt: 'one', i:1}, 2: {txt: 'one', i:3}, 3: {txt: 'two', i:2} })");
    assertRows(execute("SELECT " + fName1 + "(lst), " + fName2 + "(st), " + fName3 + "(mp) FROM %s WHERE key = 1"), row("three", "one", "two"));
    for (ProtocolVersion version : PROTOCOL_VERSIONS) assertRowsNet(version, executeNet(version, "SELECT " + fName1 + "(lst), " + fName2 + "(st), " + fName3 + "(mp) FROM %s WHERE key = 1"), row("three", "one", "two"));
}
Also used : ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) Test(org.junit.Test)

Example 19 with ProtocolVersion

use of org.apache.cassandra.transport.ProtocolVersion in project cassandra by apache.

the class UFScriptTest method testJavascriptUTCollections.

@Test
public void testJavascriptUTCollections() throws Throwable {
    String type = createType("CREATE TYPE %s (txt text, i int)");
    createTable(String.format("CREATE TABLE %%s " + "(key int primary key, lst list<frozen<%s>>, st set<frozen<%s>>, mp map<int, frozen<%s>>)", type, type, type));
    String fName = createFunction(KEYSPACE, "list<frozen<" + type + ">>", "CREATE FUNCTION %s( lst list<frozen<" + type + ">> ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS text " + "LANGUAGE javascript\n" + "AS $$" + "        lst.get(1).getString(\"txt\");$$;");
    createFunctionOverload(fName, "set<frozen<" + type + ">>", "CREATE FUNCTION %s( st set<frozen<" + type + ">> ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS text " + "LANGUAGE javascript\n" + "AS $$" + "        st.iterator().next().getString(\"txt\");$$;");
    createFunctionOverload(fName, "map<int, frozen<" + type + ">>", "CREATE FUNCTION %s( mp map<int, frozen<" + type + ">> ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS text " + "LANGUAGE javascript\n" + "AS $$" + "        mp.get(java.lang.Integer.valueOf(3)).getString(\"txt\");$$;");
    execute("INSERT INTO %s (key, lst, st, mp) values (1, " + // list<frozen<UDT>>
    "[ {txt: 'one', i:1}, {txt: 'three', i:1}, {txt: 'one', i:1} ] , " + // set<frozen<UDT>>
    "{ {txt: 'one', i:1}, {txt: 'three', i:3}, {txt: 'two', i:2} }, " + // map<int, frozen<UDT>>
    "{ 1: {txt: 'one', i:1}, 2: {txt: 'one', i:3}, 3: {txt: 'two', i:2} })");
    assertRows(execute("SELECT " + fName + "(lst) FROM %s WHERE key = 1"), row("three"));
    assertRows(execute("SELECT " + fName + "(st) FROM %s WHERE key = 1"), row("one"));
    assertRows(execute("SELECT " + fName + "(mp) FROM %s WHERE key = 1"), row("two"));
    String cqlSelect = "SELECT " + fName + "(lst), " + fName + "(st), " + fName + "(mp) FROM %s WHERE key = 1";
    assertRows(execute(cqlSelect), row("three", "one", "two"));
    // same test - but via native protocol
    for (ProtocolVersion version : PROTOCOL_VERSIONS) assertRowsNet(version, executeNet(version, cqlSelect), row("three", "one", "two"));
}
Also used : ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) Test(org.junit.Test)

Example 20 with ProtocolVersion

use of org.apache.cassandra.transport.ProtocolVersion in project cassandra by apache.

the class UFScriptTest method testJavascriptSimpleCollections.

// Just JavaScript UDFs to check how UDF - especially security/class-loading/sandboxing stuff -
// behaves, if no Java UDF has been executed before.
// Do not add any other test here - especially none using Java UDFs
@Test
public void testJavascriptSimpleCollections() throws Throwable {
    createTable("CREATE TABLE %s (key int primary key, lst list<double>, st set<text>, mp map<int, boolean>)");
    String fName1 = createFunction(KEYSPACE_PER_TEST, "list<double>", "CREATE FUNCTION %s( lst list<double> ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS list<double> " + "LANGUAGE javascript\n" + "AS 'lst;';");
    String fName2 = createFunction(KEYSPACE_PER_TEST, "set<text>", "CREATE FUNCTION %s( st set<text> ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS set<text> " + "LANGUAGE javascript\n" + "AS 'st;';");
    String fName3 = createFunction(KEYSPACE_PER_TEST, "map<int, boolean>", "CREATE FUNCTION %s( mp map<int, boolean> ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS map<int, boolean> " + "LANGUAGE javascript\n" + "AS 'mp;';");
    List<Double> list = Arrays.asList(1d, 2d, 3d);
    Set<String> set = new TreeSet<>(Arrays.asList("one", "three", "two"));
    Map<Integer, Boolean> map = new TreeMap<>();
    map.put(1, true);
    map.put(2, false);
    map.put(3, true);
    execute("INSERT INTO %s (key, lst, st, mp) VALUES (1, ?, ?, ?)", list, set, map);
    assertRows(execute("SELECT lst, st, mp FROM %s WHERE key = 1"), row(list, set, map));
    assertRows(execute("SELECT " + fName1 + "(lst), " + fName2 + "(st), " + fName3 + "(mp) FROM %s WHERE key = 1"), row(list, set, map));
    for (ProtocolVersion version : PROTOCOL_VERSIONS) assertRowsNet(version, executeNet(version, "SELECT " + fName1 + "(lst), " + fName2 + "(st), " + fName3 + "(mp) FROM %s WHERE key = 1"), row(list, set, map));
}
Also used : BigInteger(java.math.BigInteger) TreeSet(java.util.TreeSet) TreeMap(java.util.TreeMap) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) Test(org.junit.Test)

Aggregations

ProtocolVersion (org.apache.cassandra.transport.ProtocolVersion)22 Test (org.junit.Test)15 ByteBuffer (java.nio.ByteBuffer)7 TreeMap (java.util.TreeMap)5 TreeSet (java.util.TreeSet)5 Row (com.datastax.driver.core.Row)3 List (java.util.List)3 TupleType (com.datastax.driver.core.TupleType)2 TupleValue (com.datastax.driver.core.TupleValue)2 UDTValue (com.datastax.driver.core.UDTValue)2 ImmutableList (com.google.common.collect.ImmutableList)2 UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)2 InvalidQueryException (com.datastax.driver.core.exceptions.InvalidQueryException)1 BigInteger (java.math.BigInteger)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 MultiItemTerminal (org.apache.cassandra.cql3.Term.MultiItemTerminal)1 Terminal (org.apache.cassandra.cql3.Term.Terminal)1 Function (org.apache.cassandra.cql3.functions.Function)1 org.apache.cassandra.db.marshal (org.apache.cassandra.db.marshal)1