Search in sources :

Example 6 with FunctionName

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());
}
Also used : FunctionName(org.apache.cassandra.cql3.functions.FunctionName) Function(org.apache.cassandra.cql3.functions.Function)

Example 7 with FunctionName

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)));
}
Also used : FunctionName(org.apache.cassandra.cql3.functions.FunctionName) Test(org.junit.Test)

Example 8 with FunctionName

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");
}
Also used : FunctionName(org.apache.cassandra.cql3.functions.FunctionName) Test(org.junit.Test)

Example 9 with FunctionName

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()));
}
Also used : CQL3Type(org.apache.cassandra.cql3.CQL3Type) ArrayList(java.util.ArrayList) AbstractType(org.apache.cassandra.db.marshal.AbstractType) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException)

Example 10 with FunctionName

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));
}
Also used : FunctionName(org.apache.cassandra.cql3.functions.FunctionName) Test(org.junit.Test)

Aggregations

FunctionName (org.apache.cassandra.cql3.functions.FunctionName)9 Test (org.junit.Test)9 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)3 ArrayList (java.util.ArrayList)2 AbstractType (org.apache.cassandra.db.marshal.AbstractType)2 ResultMessage (org.apache.cassandra.transport.messages.ResultMessage)2 ImmutableList (com.google.common.collect.ImmutableList)1 MapDifference (com.google.common.collect.MapDifference)1 Maps (com.google.common.collect.Maps)1 String.format (java.lang.String.format)1 ByteBuffer (java.nio.ByteBuffer)1 CharacterCodingException (java.nio.charset.CharacterCodingException)1 MessageDigest (java.security.MessageDigest)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 java.util (java.util)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors.toList (java.util.stream.Collectors.toList)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 org.apache.cassandra.config (org.apache.cassandra.config)1 org.apache.cassandra.cql3 (org.apache.cassandra.cql3)1