use of org.apache.cassandra.cql3.functions.FunctionName in project cassandra by apache.
the class UFAuthTest method functionResource.
private FunctionResource functionResource(String functionName) {
// Note that this is somewhat brittle as it assumes that function names are
// truly unique. As such, it will break in the face of overloading.
// It is here to avoid having to duplicate the functionality of CqlParser
// for transforming cql types into AbstractTypes
FunctionName fn = parseFunctionName(functionName);
Collection<Function> functions = Schema.instance.getFunctions(fn);
assertEquals(String.format("Expected a single function definition for %s, but found %s", functionName, functions.size()), 1, functions.size());
return FunctionResource.function(fn.keyspace, fn.name, functions.iterator().next().argTypes());
}
use of org.apache.cassandra.cql3.functions.FunctionName in project cassandra by apache.
the class UFJavaTest method testJavaKeyspaceFunction.
@Test
public void testJavaKeyspaceFunction() throws Throwable {
createTable("CREATE TABLE %s (key int primary key, val double)");
String functionBody = '\n' + " // parameter val is of type java.lang.Double\n" + " /* return type is of type java.lang.Double */\n" + " if (val == null) {\n" + " return null;\n" + " }\n" + " return Math.sin( val );\n";
String fName = createFunction(KEYSPACE_PER_TEST, "double", "CREATE OR REPLACE FUNCTION %s(val double) " + "CALLED ON NULL INPUT " + "RETURNS double " + "LANGUAGE JAVA " + "AS '" + functionBody + "';");
FunctionName fNameName = parseFunctionName(fName);
assertRows(execute("SELECT language, body FROM system_schema.functions WHERE keyspace_name=? AND function_name=?", fNameName.keyspace, fNameName.name), row("java", functionBody));
execute("INSERT INTO %s (key, val) VALUES (?, ?)", 1, 1d);
execute("INSERT INTO %s (key, val) VALUES (?, ?)", 2, 2d);
execute("INSERT INTO %s (key, val) VALUES (?, ?)", 3, 3d);
assertRows(execute("SELECT key, val, " + fName + "(val) FROM %s"), row(1, 1d, Math.sin(1d)), row(2, 2d, Math.sin(2d)), row(3, 3d, Math.sin(3d)));
}
use of org.apache.cassandra.cql3.functions.FunctionName in project cassandra by apache.
the class UFJavaTest method testJavaRuntimeException.
@Test
public void testJavaRuntimeException() throws Throwable {
createTable("CREATE TABLE %s (key int primary key, val double)");
String functionBody = '\n' + " throw new RuntimeException(\"oh no!\");\n";
String fName = createFunction(KEYSPACE_PER_TEST, "double", "CREATE OR REPLACE FUNCTION %s(val double) " + "RETURNS NULL ON NULL INPUT " + "RETURNS double " + "LANGUAGE JAVA\n" + "AS '" + functionBody + "';");
FunctionName fNameName = parseFunctionName(fName);
assertRows(execute("SELECT language, body FROM system_schema.functions WHERE keyspace_name=? AND function_name=?", fNameName.keyspace, fNameName.name), row("java", functionBody));
execute("INSERT INTO %s (key, val) VALUES (?, ?)", 1, 1d);
execute("INSERT INTO %s (key, val) VALUES (?, ?)", 2, 2d);
execute("INSERT INTO %s (key, val) VALUES (?, ?)", 3, 3d);
// function throws a RuntimeException which is wrapped by FunctionExecutionException
assertInvalidThrowMessage("java.lang.RuntimeException: oh no", FunctionExecutionException.class, "SELECT key, val, " + fName + "(val) FROM %s");
}
use of org.apache.cassandra.cql3.functions.FunctionName in project cassandra by apache.
the class DropAggregateStatement method announceMigration.
public Event.SchemaChange announceMigration(QueryState queryState, boolean isLocalOnly) throws RequestValidationException {
Collection<Function> olds = Schema.instance.getFunctions(functionName);
if (!argsPresent && olds != null && olds.size() > 1)
throw new InvalidRequestException(String.format("'DROP AGGREGATE %s' matches multiple function definitions; " + "specify the argument types by issuing a statement like " + "'DROP AGGREGATE %s (type, type, ...)'. Hint: use cqlsh " + "'DESCRIBE AGGREGATE %s' command to find all overloads", functionName, functionName, functionName));
Function old = null;
if (argsPresent) {
if (Schema.instance.getKeyspaceMetadata(functionName.keyspace) != null) {
List<AbstractType<?>> argTypes = new ArrayList<>(argRawTypes.size());
for (CQL3Type.Raw rawType : argRawTypes) argTypes.add(prepareType("arguments", rawType));
old = Schema.instance.findFunction(functionName, argTypes).orElse(null);
}
if (old == null || !(old instanceof AggregateFunction)) {
if (ifExists)
return null;
// just build a nicer error message
StringBuilder sb = new StringBuilder();
for (CQL3Type.Raw rawType : argRawTypes) {
if (sb.length() > 0)
sb.append(", ");
sb.append(rawType);
}
throw new InvalidRequestException(String.format("Cannot drop non existing aggregate '%s(%s)'", functionName, sb));
}
} else {
if (olds == null || olds.isEmpty() || !(olds.iterator().next() instanceof AggregateFunction)) {
if (ifExists)
return null;
throw new InvalidRequestException(String.format("Cannot drop non existing aggregate '%s'", functionName));
}
old = olds.iterator().next();
}
if (old.isNative())
throw new InvalidRequestException(String.format("Cannot drop aggregate '%s' because it is a " + "native (built-in) function", functionName));
MigrationManager.announceAggregateDrop((UDAggregate) old, isLocalOnly);
return new Event.SchemaChange(Event.SchemaChange.Change.DROPPED, Event.SchemaChange.Target.AGGREGATE, old.name().keyspace, old.name().name, AbstractType.asCQLTypeStringList(old.argTypes()));
}
use of org.apache.cassandra.cql3.functions.FunctionName in project cassandra by apache.
the class UFJavaTest method testJavaDollarQuotedFunction.
@Test
public void testJavaDollarQuotedFunction() throws Throwable {
String functionBody = '\n' + " // parameter val is of type java.lang.Double\n" + " /* return type is of type java.lang.Double */\n" + " if (input == null) {\n" + " return null;\n" + " }\n" + " return \"'\"+Math.sin(input)+'\\\'';\n";
String fName = createFunction(KEYSPACE_PER_TEST, "double", "CREATE FUNCTION %s( input double ) " + "CALLED ON NULL INPUT " + "RETURNS text " + "LANGUAGE java\n" + "AS $$" + functionBody + "$$;");
FunctionName fNameName = parseFunctionName(fName);
assertRows(execute("SELECT language, body FROM system_schema.functions WHERE keyspace_name=? AND function_name=?", fNameName.keyspace, fNameName.name), row("java", functionBody));
}
Aggregations