use of org.apache.cassandra.transport.ProtocolVersion in project cassandra by apache.
the class UFPureScriptTupleCollectionTest method testJavascriptTupleTypeCollection.
// 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!
// See CASSANDRA-10141
@Test
public void testJavascriptTupleTypeCollection() throws Throwable {
String tupleTypeDef = "tuple<double, list<double>, set<text>, map<int, boolean>>";
createTable("CREATE TABLE %s (key int primary key, tup frozen<" + tupleTypeDef + ">)");
String fTup1 = createFunction(KEYSPACE_PER_TEST, tupleTypeDef, "CREATE FUNCTION %s( tup " + tupleTypeDef + " ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS tuple<double, list<double>, set<text>, map<int, boolean>> " + "LANGUAGE javascript\n" + "AS $$" + " tup;$$;");
String fTup2 = createFunction(KEYSPACE_PER_TEST, tupleTypeDef, "CREATE FUNCTION %s( tup " + tupleTypeDef + " ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS double " + "LANGUAGE javascript\n" + "AS $$" + " tup.getDouble(0);$$;");
String fTup3 = createFunction(KEYSPACE_PER_TEST, tupleTypeDef, "CREATE FUNCTION %s( tup " + tupleTypeDef + " ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS list<double> " + "LANGUAGE javascript\n" + "AS $$" + " tup.getList(1, java.lang.Double.class);$$;");
String fTup4 = createFunction(KEYSPACE_PER_TEST, tupleTypeDef, "CREATE FUNCTION %s( tup " + tupleTypeDef + " ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS set<text> " + "LANGUAGE javascript\n" + "AS $$" + " tup.getSet(2, java.lang.String.class);$$;");
String fTup5 = createFunction(KEYSPACE_PER_TEST, tupleTypeDef, "CREATE FUNCTION %s( tup " + tupleTypeDef + " ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS map<int, boolean> " + "LANGUAGE javascript\n" + "AS $$" + " tup.getMap(3, java.lang.Integer.class, java.lang.Boolean.class);$$;");
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);
Object t = tuple(1d, list, set, map);
execute("INSERT INTO %s (key, tup) VALUES (1, ?)", t);
assertRows(execute("SELECT " + fTup1 + "(tup) FROM %s WHERE key = 1"), row(t));
assertRows(execute("SELECT " + fTup2 + "(tup) FROM %s WHERE key = 1"), row(1d));
assertRows(execute("SELECT " + fTup3 + "(tup) FROM %s WHERE key = 1"), row(list));
assertRows(execute("SELECT " + fTup4 + "(tup) FROM %s WHERE key = 1"), row(set));
assertRows(execute("SELECT " + fTup5 + "(tup) FROM %s WHERE key = 1"), row(map));
// same test - but via native protocol
// we use protocol V3 here to encode the expected version because the server
// always serializes Collections using V3 - see CollectionSerializer's
// serialize and deserialize methods.
TupleType tType = tupleTypeOf(ProtocolVersion.V3, DataType.cdouble(), DataType.list(DataType.cdouble()), DataType.set(DataType.text()), DataType.map(DataType.cint(), DataType.cboolean()));
TupleValue tup = tType.newValue(1d, list, set, map);
for (ProtocolVersion version : PROTOCOL_VERSIONS) {
assertRowsNet(version, executeNet(version, "SELECT " + fTup1 + "(tup) FROM %s WHERE key = 1"), row(tup));
assertRowsNet(version, executeNet(version, "SELECT " + fTup2 + "(tup) FROM %s WHERE key = 1"), row(1d));
assertRowsNet(version, executeNet(version, "SELECT " + fTup3 + "(tup) FROM %s WHERE key = 1"), row(list));
assertRowsNet(version, executeNet(version, "SELECT " + fTup4 + "(tup) FROM %s WHERE key = 1"), row(set));
assertRowsNet(version, executeNet(version, "SELECT " + fTup5 + "(tup) FROM %s WHERE key = 1"), row(map));
}
}
use of org.apache.cassandra.transport.ProtocolVersion in project cassandra by apache.
the class UFJavaTest method testJavaUserType.
@Test
public void testJavaUserType() throws Throwable {
String type = KEYSPACE + '.' + createType("CREATE TYPE %s (txt text, i int)");
createTable("CREATE TABLE %s (key int primary key, udt frozen<" + type + ">)");
String fUdt0 = createFunction(KEYSPACE, type, "CREATE FUNCTION %s( udt " + type + " ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS " + type + " " + "LANGUAGE java " + "AS $$return " + " udt;$$;");
String fUdt1 = createFunction(KEYSPACE, type, "CREATE FUNCTION %s( udt " + type + ") " + "RETURNS NULL ON NULL INPUT " + "RETURNS text " + "LANGUAGE java " + "AS $$return " + " udt.getString(\"txt\");$$;");
String fUdt2 = createFunction(KEYSPACE, type, "CREATE FUNCTION %s( udt " + type + ") " + "CALLED ON NULL INPUT " + "RETURNS int " + "LANGUAGE java " + "AS $$return " + " Integer.valueOf(udt.getInt(\"i\"));$$;");
execute("INSERT INTO %s (key, udt) VALUES (1, {txt: 'one', i:1})");
UntypedResultSet rows = execute("SELECT " + fUdt0 + "(udt) FROM %s WHERE key = 1");
Assert.assertEquals(1, rows.size());
assertRows(execute("SELECT " + fUdt1 + "(udt) FROM %s WHERE key = 1"), row("one"));
assertRows(execute("SELECT " + fUdt2 + "(udt) FROM %s WHERE key = 1"), row(1));
for (ProtocolVersion version : PROTOCOL_VERSIONS) {
List<Row> rowsNet = executeNet(version, "SELECT " + fUdt0 + "(udt) FROM %s WHERE key = 1").all();
Assert.assertEquals(1, rowsNet.size());
UDTValue udtVal = rowsNet.get(0).getUDTValue(0);
Assert.assertEquals("one", udtVal.getString("txt"));
Assert.assertEquals(1, udtVal.getInt("i"));
assertRowsNet(version, executeNet(version, "SELECT " + fUdt1 + "(udt) FROM %s WHERE key = 1"), row("one"));
assertRowsNet(version, executeNet(version, "SELECT " + fUdt2 + "(udt) FROM %s WHERE key = 1"), row(1));
}
}
use of org.apache.cassandra.transport.ProtocolVersion in project cassandra by apache.
the class UFJavaTest method testJavaTupleTypeCollection.
@Test
public void testJavaTupleTypeCollection() throws Throwable {
String tupleTypeDef = "tuple<double, list<double>, set<text>, map<int, boolean>>";
createTable("CREATE TABLE %s (key int primary key, tup frozen<" + tupleTypeDef + ">)");
String fTup0 = createFunction(KEYSPACE_PER_TEST, tupleTypeDef, "CREATE FUNCTION %s( tup " + tupleTypeDef + " ) " + "CALLED ON NULL INPUT " + "RETURNS " + tupleTypeDef + ' ' + "LANGUAGE java\n" + "AS $$return " + " tup;$$;");
String fTup1 = createFunction(KEYSPACE_PER_TEST, tupleTypeDef, "CREATE FUNCTION %s( tup " + tupleTypeDef + " ) " + "CALLED ON NULL INPUT " + "RETURNS double " + "LANGUAGE java\n" + "AS $$return " + " Double.valueOf(tup.getDouble(0));$$;");
String fTup2 = createFunction(KEYSPACE_PER_TEST, tupleTypeDef, "CREATE FUNCTION %s( tup " + tupleTypeDef + " ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS list<double> " + "LANGUAGE java\n" + "AS $$return " + " tup.getList(1, Double.class);$$;");
String fTup3 = createFunction(KEYSPACE_PER_TEST, tupleTypeDef, "CREATE FUNCTION %s( tup " + tupleTypeDef + " ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS set<text> " + "LANGUAGE java\n" + "AS $$return " + " tup.getSet(2, String.class);$$;");
String fTup4 = createFunction(KEYSPACE_PER_TEST, tupleTypeDef, "CREATE FUNCTION %s( tup " + tupleTypeDef + " ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS map<int, boolean> " + "LANGUAGE java\n" + "AS $$return " + " tup.getMap(3, Integer.class, Boolean.class);$$;");
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);
Object t = tuple(1d, list, set, map);
execute("INSERT INTO %s (key, tup) VALUES (1, ?)", t);
assertRows(execute("SELECT " + fTup0 + "(tup) FROM %s WHERE key = 1"), row(t));
assertRows(execute("SELECT " + fTup1 + "(tup) FROM %s WHERE key = 1"), row(1d));
assertRows(execute("SELECT " + fTup2 + "(tup) FROM %s WHERE key = 1"), row(list));
assertRows(execute("SELECT " + fTup3 + "(tup) FROM %s WHERE key = 1"), row(set));
assertRows(execute("SELECT " + fTup4 + "(tup) FROM %s WHERE key = 1"), row(map));
// same test - but via native protocol
// we use protocol V3 here to encode the expected version because the server
// always serializes Collections using V3 - see CollectionSerializer's
// serialize and deserialize methods.
TupleType tType = tupleTypeOf(ProtocolVersion.V3, DataType.cdouble(), DataType.list(DataType.cdouble()), DataType.set(DataType.text()), DataType.map(DataType.cint(), DataType.cboolean()));
TupleValue tup = tType.newValue(1d, list, set, map);
for (ProtocolVersion version : PROTOCOL_VERSIONS) {
assertRowsNet(version, executeNet(version, "SELECT " + fTup0 + "(tup) FROM %s WHERE key = 1"), row(tup));
assertRowsNet(version, executeNet(version, "SELECT " + fTup1 + "(tup) FROM %s WHERE key = 1"), row(1d));
assertRowsNet(version, executeNet(version, "SELECT " + fTup2 + "(tup) FROM %s WHERE key = 1"), row(list));
assertRowsNet(version, executeNet(version, "SELECT " + fTup3 + "(tup) FROM %s WHERE key = 1"), row(set));
assertRowsNet(version, executeNet(version, "SELECT " + fTup4 + "(tup) FROM %s WHERE key = 1"), row(map));
}
}
use of org.apache.cassandra.transport.ProtocolVersion in project cassandra by apache.
the class UFJavaTest method testJavaSimpleCollections.
@Test
public void testJavaSimpleCollections() throws Throwable {
createTable("CREATE TABLE %s (key int primary key, lst list<double>, st set<text>, mp map<int, boolean>)");
String fList = createFunction(KEYSPACE_PER_TEST, "list<double>", "CREATE FUNCTION %s( lst list<double> ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS list<double> " + "LANGUAGE java\n" + "AS $$return lst;$$;");
String fSet = createFunction(KEYSPACE_PER_TEST, "set<text>", "CREATE FUNCTION %s( st set<text> ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS set<text> " + "LANGUAGE java\n" + "AS $$return st;$$;");
String fMap = createFunction(KEYSPACE_PER_TEST, "map<int, boolean>", "CREATE FUNCTION %s( mp map<int, boolean> ) " + "RETURNS NULL ON NULL INPUT " + "RETURNS map<int, boolean> " + "LANGUAGE java\n" + "AS $$return 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 " + fList + "(lst), " + fSet + "(st), " + fMap + "(mp) FROM %s WHERE key = 1"), row(list, set, map));
// same test - but via native protocol
for (ProtocolVersion version : PROTOCOL_VERSIONS) assertRowsNet(version, executeNet(version, "SELECT " + fList + "(lst), " + fSet + "(st), " + fMap + "(mp) FROM %s WHERE key = 1"), row(list, set, map));
}
use of org.apache.cassandra.transport.ProtocolVersion in project cassandra by apache.
the class TimeFcts method toDate.
/**
* Creates a function that convert a value of the specified type into a <code>DATE</code>.
* @param type the temporal type
* @return a function that convert a value of the specified type into a <code>DATE</code>.
*/
public static final NativeScalarFunction toDate(final TemporalType<?> type) {
return new NativeScalarFunction("todate", SimpleDateType.instance, type) {
public ByteBuffer execute(ProtocolVersion protocolVersion, List<ByteBuffer> parameters) {
ByteBuffer bb = parameters.get(0);
if (bb == null || !bb.hasRemaining())
return null;
long millis = type.toTimeInMillis(bb);
return SimpleDateType.instance.fromTimeInMillis(millis);
}
};
}
Aggregations