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);
}
}
}
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);
}
}
}
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"));
}
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"));
}
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));
}
Aggregations