Search in sources :

Example 1 with UDAggregate

use of org.apache.cassandra.cql3.functions.UDAggregate in project cassandra by apache.

the class SchemaKeyspace method createUDAFromRow.

private static UDAggregate createUDAFromRow(UntypedResultSet.Row row, Functions functions, Types types) {
    String ksName = row.getString("keyspace_name");
    String functionName = row.getString("aggregate_name");
    FunctionName name = new FunctionName(ksName, functionName);
    List<AbstractType<?>> argTypes = row.getFrozenList("argument_types", UTF8Type.instance).stream().map(t -> CQLTypeParser.parse(ksName, t, types)).collect(toList());
    AbstractType<?> returnType = CQLTypeParser.parse(ksName, row.getString("return_type"), types);
    FunctionName stateFunc = new FunctionName(ksName, (row.getString("state_func")));
    FunctionName finalFunc = row.has("final_func") ? new FunctionName(ksName, row.getString("final_func")) : null;
    AbstractType<?> stateType = row.has("state_type") ? CQLTypeParser.parse(ksName, row.getString("state_type"), types) : null;
    ByteBuffer initcond = row.has("initcond") ? Terms.asBytes(ksName, row.getString("initcond"), stateType) : null;
    try {
        return UDAggregate.create(functions, name, argTypes, returnType, stateFunc, finalFunc, stateType, initcond);
    } catch (InvalidRequestException reason) {
        return UDAggregate.createBroken(name, argTypes, returnType, initcond, reason);
    }
}
Also used : org.apache.cassandra.config(org.apache.cassandra.config) java.util(java.util) CharacterCodingException(java.nio.charset.CharacterCodingException) org.apache.cassandra.db.marshal(org.apache.cassandra.db.marshal) MessageDigest(java.security.MessageDigest) LoggerFactory(org.slf4j.LoggerFactory) org.apache.cassandra.db(org.apache.cassandra.db) ByteBuffer(java.nio.ByteBuffer) org.apache.cassandra.cql3(org.apache.cassandra.cql3) org.apache.cassandra.db.rows(org.apache.cassandra.db.rows) View(org.apache.cassandra.db.view.View) ImmutableList(com.google.common.collect.ImmutableList) SelectStatement(org.apache.cassandra.cql3.statements.SelectStatement) ColumnFilter(org.apache.cassandra.db.filter.ColumnFilter) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) org.apache.cassandra.cql3.functions(org.apache.cassandra.cql3.functions) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) org.apache.cassandra.db.partitions(org.apache.cassandra.db.partitions) Collectors.toSet(java.util.stream.Collectors.toSet) ClusteringOrder(org.apache.cassandra.schema.ColumnMetadata.ClusteringOrder) Logger(org.slf4j.Logger) FBUtilities(org.apache.cassandra.utils.FBUtilities) ByteBufferUtil(org.apache.cassandra.utils.ByteBufferUtil) QueryProcessor.executeInternal(org.apache.cassandra.cql3.QueryProcessor.executeInternal) Maps(com.google.common.collect.Maps) String.format(java.lang.String.format) QueryProcessor.executeOnceInternal(org.apache.cassandra.cql3.QueryProcessor.executeOnceInternal) TimeUnit(java.util.concurrent.TimeUnit) MapDifference(com.google.common.collect.MapDifference) Collectors.toList(java.util.stream.Collectors.toList) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CreateTableStatement(org.apache.cassandra.cql3.statements.CreateTableStatement) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) ByteBuffer(java.nio.ByteBuffer)

Example 2 with UDAggregate

use of org.apache.cassandra.cql3.functions.UDAggregate in project cassandra by apache.

the class SchemaKeyspace method createUDAFromRow.

private static UDAggregate createUDAFromRow(UntypedResultSet.Row row, Collection<UDFunction> functions, Types types) {
    String ksName = row.getString("keyspace_name");
    String functionName = row.getString("aggregate_name");
    FunctionName name = new FunctionName(ksName, functionName);
    List<AbstractType<?>> argTypes = row.getFrozenList("argument_types", UTF8Type.instance).stream().map(t -> CQLTypeParser.parse(ksName, t, types)).collect(toList());
    AbstractType<?> returnType = CQLTypeParser.parse(ksName, row.getString("return_type"), types);
    FunctionName stateFunc = new FunctionName(ksName, (row.getString("state_func")));
    FunctionName finalFunc = row.has("final_func") ? new FunctionName(ksName, row.getString("final_func")) : null;
    AbstractType<?> stateType = row.has("state_type") ? CQLTypeParser.parse(ksName, row.getString("state_type"), types) : null;
    ByteBuffer initcond = row.has("initcond") ? Terms.asBytes(ksName, row.getString("initcond"), stateType) : null;
    return UDAggregate.create(functions, name, argTypes, returnType, stateFunc, finalFunc, stateType, initcond);
}
Also used : org.apache.cassandra.config(org.apache.cassandra.config) java.util(java.util) CharacterCodingException(java.nio.charset.CharacterCodingException) org.apache.cassandra.db.marshal(org.apache.cassandra.db.marshal) LoggerFactory(org.slf4j.LoggerFactory) org.apache.cassandra.db(org.apache.cassandra.db) ByteBuffer(java.nio.ByteBuffer) org.apache.cassandra.cql3(org.apache.cassandra.cql3) org.apache.cassandra.db.rows(org.apache.cassandra.db.rows) SchemaKeyspaceTables(org.apache.cassandra.schema.SchemaKeyspaceTables) RecognitionException(org.antlr.runtime.RecognitionException) Simulate(org.apache.cassandra.utils.Simulate) ColumnFilter(org.apache.cassandra.db.filter.ColumnFilter) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) org.apache.cassandra.cql3.functions(org.apache.cassandra.cql3.functions) KeyspacesDiff(org.apache.cassandra.schema.Keyspaces.KeyspacesDiff) com.google.common.collect(com.google.common.collect) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) SpeculativeRetryPolicy(org.apache.cassandra.service.reads.SpeculativeRetryPolicy) org.apache.cassandra.db.partitions(org.apache.cassandra.db.partitions) Collectors.toSet(java.util.stream.Collectors.toSet) ClusteringOrder(org.apache.cassandra.schema.ColumnMetadata.ClusteringOrder) Logger(org.slf4j.Logger) FBUtilities(org.apache.cassandra.utils.FBUtilities) ByteBufferUtil(org.apache.cassandra.utils.ByteBufferUtil) QueryProcessor.executeInternal(org.apache.cassandra.cql3.QueryProcessor.executeInternal) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) ReadRepairStrategy(org.apache.cassandra.service.reads.repair.ReadRepairStrategy) String.format(java.lang.String.format) QueryProcessor.executeOnceInternal(org.apache.cassandra.cql3.QueryProcessor.executeOnceInternal) CreateTableStatement(org.apache.cassandra.cql3.statements.schema.CreateTableStatement) TimeUnit(java.util.concurrent.TimeUnit) GLOBAL_CLOCK(org.apache.cassandra.utils.Simulate.With.GLOBAL_CLOCK) Collectors.toList(java.util.stream.Collectors.toList) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe) ByteBuffer(java.nio.ByteBuffer)

Example 3 with UDAggregate

use of org.apache.cassandra.cql3.functions.UDAggregate in project cassandra by apache.

the class UFJavaTest method testUDAToCqlString.

@Test
public void testUDAToCqlString() throws Throwable {
    // we have to create this function in DB otherwise UDAggregate creation below fails
    String stateFunctionName = createFunction(KEYSPACE, "int,int", "CREATE OR REPLACE FUNCTION %s(state int, val int)\n" + "    CALLED ON NULL INPUT\n" + "    RETURNS int\n" + "    LANGUAGE java\n" + "    AS $$\n" + "        return state + val;\n" + "    $$;");
    // Java representation of state function so we can construct aggregate programmatically
    UDFunction stateFunction = UDFunction.create(new FunctionName(KEYSPACE, stateFunctionName.split("\\.")[1]), Arrays.asList(ColumnIdentifier.getInterned("state", false), ColumnIdentifier.getInterned("val", false)), Arrays.asList(Int32Type.instance, Int32Type.instance), Int32Type.instance, true, "java", "return state + val;");
    UDAggregate aggregate = UDAggregate.create(Collections.singleton(stateFunction), new FunctionName(KEYSPACE, "my_aggregate"), Collections.singletonList(Int32Type.instance), Int32Type.instance, new FunctionName(KEYSPACE, stateFunctionName.split("\\.")[1]), null, Int32Type.instance, null);
    Assert.assertTrue(aggregate.toCqlString(true, true).contains("CREATE AGGREGATE IF NOT EXISTS"));
    Assert.assertFalse(aggregate.toCqlString(true, false).contains("CREATE AGGREGATE IF NOT EXISTS"));
    Assert.assertEquals(aggregate.toCqlString(true, true), aggregate.toCqlString(false, true));
    Assert.assertEquals(aggregate.toCqlString(true, false), aggregate.toCqlString(false, false));
}
Also used : FunctionName(org.apache.cassandra.cql3.functions.FunctionName) UDAggregate(org.apache.cassandra.cql3.functions.UDAggregate) UDFunction(org.apache.cassandra.cql3.functions.UDFunction) Test(org.junit.Test)

Example 4 with UDAggregate

use of org.apache.cassandra.cql3.functions.UDAggregate 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 5 with UDAggregate

use of org.apache.cassandra.cql3.functions.UDAggregate in project cassandra by apache.

the class AggregationTest method testBrokenAggregate.

@Test
public void testBrokenAggregate() throws Throwable {
    createTable("CREATE TABLE %s (key int primary key, val int)");
    execute("INSERT INTO %s (key, val) VALUES (?, ?)", 1, 1);
    String fState = createFunction(KEYSPACE, "int, int", "CREATE FUNCTION %s(a int, b int) " + "CALLED ON NULL INPUT " + "RETURNS int " + "LANGUAGE javascript " + "AS 'a + b;'");
    String a = createAggregate(KEYSPACE, "int", "CREATE AGGREGATE %s(int) " + "SFUNC " + shortFunctionName(fState) + " " + "STYPE int ");
    KeyspaceMetadata ksm = Schema.instance.getKeyspaceMetadata(keyspace());
    UDAggregate f = (UDAggregate) ksm.functions.get(parseFunctionName(a)).iterator().next();
    UDAggregate broken = UDAggregate.createBroken(f.name(), f.argTypes(), f.returnType(), null, new InvalidRequestException("foo bar is broken"));
    Schema.instance.load(ksm.withSwapped(ksm.functions.without(f.name(), f.argTypes()).with(broken)));
    assertInvalidThrowMessage("foo bar is broken", InvalidRequestException.class, "SELECT " + a + "(val) FROM %s");
}
Also used : UDAggregate(org.apache.cassandra.cql3.functions.UDAggregate) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) Test(org.junit.Test)

Aggregations

InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)4 String.format (java.lang.String.format)3 ByteBuffer (java.nio.ByteBuffer)3 Collectors.toList (java.util.stream.Collectors.toList)3 org.apache.cassandra.cql3 (org.apache.cassandra.cql3)3 org.apache.cassandra.cql3.functions (org.apache.cassandra.cql3.functions)3 ProtocolVersion (org.apache.cassandra.transport.ProtocolVersion)3 Maps (com.google.common.collect.Maps)2 CharacterCodingException (java.nio.charset.CharacterCodingException)2 java.util (java.util)2 TimeUnit (java.util.concurrent.TimeUnit)2 Collectors.toSet (java.util.stream.Collectors.toSet)2 org.apache.cassandra.config (org.apache.cassandra.config)2 QueryProcessor.executeInternal (org.apache.cassandra.cql3.QueryProcessor.executeInternal)2 QueryProcessor.executeOnceInternal (org.apache.cassandra.cql3.QueryProcessor.executeOnceInternal)2 UDAggregate (org.apache.cassandra.cql3.functions.UDAggregate)2 org.apache.cassandra.db (org.apache.cassandra.db)2 ColumnFilter (org.apache.cassandra.db.filter.ColumnFilter)2 org.apache.cassandra.db.marshal (org.apache.cassandra.db.marshal)2 AbstractType (org.apache.cassandra.db.marshal.AbstractType)2