Search in sources :

Example 36 with Function

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

the class DropTypeStatement method apply.

// TODO: expand types into tuples in all dropped columns of all tables
public Keyspaces apply(Keyspaces schema) {
    ByteBuffer name = bytes(typeName);
    KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);
    UserType type = null == keyspace ? null : keyspace.types.getNullable(name);
    if (null == type) {
        if (ifExists)
            return schema;
        throw ire("Type '%s.%s' doesn't exist", keyspaceName, typeName);
    }
    /*
         * We don't want to drop a type unless it's not used anymore (mainly because
         * if someone drops a type and recreates one with the same name but different
         * definition with the previous name still in use, things can get messy).
         * We have three places to check:
         * 1) UDFs and UDAs using the type
         * 2) other user type that can nest the one we drop and
         * 3) existing tables referencing the type (maybe in a nested way).
         */
    Iterable<Function> functions = keyspace.functions.referencingUserType(name);
    if (!isEmpty(functions)) {
        throw ire("Cannot drop user type '%s.%s' as it is still used by functions %s", keyspaceName, typeName, join(", ", transform(functions, f -> f.name().toString())));
    }
    Iterable<UserType> types = keyspace.types.referencingUserType(name);
    if (!isEmpty(types)) {
        throw ire("Cannot drop user type '%s.%s' as it is still used by user types %s", keyspaceName, typeName, join(", ", transform(types, UserType::getNameAsString)));
    }
    Iterable<TableMetadata> tables = keyspace.tables.referencingUserType(name);
    if (!isEmpty(tables)) {
        throw ire("Cannot drop user type '%s.%s' as it is still used by tables %s", keyspaceName, typeName, join(", ", transform(tables, t -> t.name)));
    }
    return schema.withAddedOrUpdated(keyspace.withSwapped(keyspace.types.without(type)));
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) Function(org.apache.cassandra.cql3.functions.Function) ByteBuffer(java.nio.ByteBuffer) KeyspaceMetadata(org.apache.cassandra.schema.KeyspaceMetadata) UserType(org.apache.cassandra.db.marshal.UserType)

Example 37 with Function

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

the class DropAggregateStatement method apply.

public Keyspaces apply(Keyspaces schema) {
    String name = argumentsSpeficied ? format("%s.%s(%s)", keyspaceName, aggregateName, join(", ", transform(arguments, CQL3Type.Raw::toString))) : format("%s.%s", keyspaceName, aggregateName);
    KeyspaceMetadata keyspace = schema.getNullable(keyspaceName);
    if (null == keyspace) {
        if (ifExists)
            return schema;
        throw ire("Aggregate '%s' doesn't exist", name);
    }
    Collection<Function> aggregates = keyspace.functions.get(new FunctionName(keyspaceName, aggregateName));
    if (aggregates.size() > 1 && !argumentsSpeficied) {
        throw ire("'DROP AGGREGATE %s' matches multiple function definitions; " + "specify the argument types by issuing a statement like " + "'DROP AGGREGATE %s (type, type, ...)'. You can use cqlsh " + "'DESCRIBE AGGREGATE %s' command to find all overloads", aggregateName, aggregateName, aggregateName);
    }
    arguments.stream().filter(raw -> !raw.isTuple() && raw.isFrozen()).findFirst().ifPresent(t -> {
        throw ire("Argument '%s' cannot be frozen; remove frozen<> modifier from '%s'", t, t);
    });
    List<AbstractType<?>> argumentTypes = prepareArgumentTypes(keyspace.types);
    Predicate<Function> filter = Functions.Filter.UDA;
    if (argumentsSpeficied)
        filter = filter.and(f -> Functions.typesMatch(f.argTypes(), argumentTypes));
    Function aggregate = aggregates.stream().filter(filter).findAny().orElse(null);
    if (null == aggregate) {
        if (ifExists)
            return schema;
        throw ire("Aggregate '%s' doesn't exist", name);
    }
    return schema.withAddedOrUpdated(keyspace.withSwapped(keyspace.functions.without(aggregate)));
}
Also used : CQL3Type(org.apache.cassandra.cql3.CQL3Type) Function(org.apache.cassandra.cql3.functions.Function) FunctionName(org.apache.cassandra.cql3.functions.FunctionName) AbstractType(org.apache.cassandra.db.marshal.AbstractType)

Example 38 with Function

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

the class AbstractFunctionSelector method newFactory.

public static Factory newFactory(final Function fun, final SelectorFactories factories) throws InvalidRequestException {
    if (fun.isAggregate()) {
        if (factories.doesAggregation())
            throw new InvalidRequestException("aggregate functions cannot be used as arguments of aggregate functions");
    }
    return new Factory() {

        protected String getColumnName() {
            return fun.columnName(factories.getColumnNames());
        }

        protected AbstractType<?> getReturnType() {
            return fun.returnType();
        }

        protected void addColumnMapping(SelectionColumnMapping mapping, ColumnSpecification resultsColumn) {
            SelectionColumnMapping tmpMapping = SelectionColumnMapping.newMapping();
            for (Factory factory : factories) factory.addColumnMapping(tmpMapping, resultsColumn);
            if (tmpMapping.getMappings().get(resultsColumn).isEmpty())
                // add a null mapping for cases where there are no
                // further selectors, such as no-arg functions and count
                mapping.addMapping(resultsColumn, (ColumnMetadata) null);
            else
                // collate the mapped columns from the child factories & add those
                mapping.addMapping(resultsColumn, tmpMapping.getMappings().values());
        }

        public void addFunctionsTo(List<Function> functions) {
            fun.addFunctionsTo(functions);
            factories.addFunctionsTo(functions);
        }

        public Selector newInstance(QueryOptions options) throws InvalidRequestException {
            return fun.isAggregate() ? new AggregateFunctionSelector(fun, factories.newInstances(options)) : new ScalarFunctionSelector(fun, factories.newInstances(options));
        }

        public boolean isWritetimeSelectorFactory() {
            return factories.containsWritetimeSelectorFactory();
        }

        public boolean isTTLSelectorFactory() {
            return factories.containsTTLSelectorFactory();
        }

        public boolean isAggregateSelectorFactory() {
            return fun.isAggregate() || factories.doesAggregation();
        }

        @Override
        public boolean areAllFetchedColumnsKnown() {
            return Iterables.all(factories, f -> f.areAllFetchedColumnsKnown());
        }

        @Override
        public void addFetchedColumns(ColumnFilter.Builder builder) {
            for (Selector.Factory factory : factories) factory.addFetchedColumns(builder);
        }
    };
}
Also used : ColumnSpecification(org.apache.cassandra.cql3.ColumnSpecification) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) StrBuilder(org.apache.commons.lang3.text.StrBuilder) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) List(java.util.List) QueryOptions(org.apache.cassandra.cql3.QueryOptions)

Example 39 with Function

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

the class ModificationStatement method authorize.

public void authorize(ClientState state) throws InvalidRequestException, UnauthorizedException {
    state.ensureTablePermission(metadata, Permission.MODIFY);
    // CAS updates can be used to simulate a SELECT query, so should require Permission.SELECT as well.
    if (hasConditions())
        state.ensureTablePermission(metadata, Permission.SELECT);
    // MV updates need to get the current state from the table, and might update the views
    // Require Permission.SELECT on the base table, and Permission.MODIFY on the views
    Iterator<ViewMetadata> views = View.findAll(keyspace(), table()).iterator();
    if (views.hasNext()) {
        state.ensureTablePermission(metadata, Permission.SELECT);
        do {
            state.ensureTablePermission(views.next().metadata, Permission.MODIFY);
        } while (views.hasNext());
    }
    for (Function function : getFunctions()) state.ensurePermission(Permission.EXECUTE, function);
}
Also used : Function(org.apache.cassandra.cql3.functions.Function) ViewMetadata(org.apache.cassandra.schema.ViewMetadata)

Example 40 with Function

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

Test (org.junit.Test)34 Function (io.reactivex.rxjava3.functions.Function)20 FunctionName (org.apache.cassandra.cql3.functions.FunctionName)13 ByteBuffer (java.nio.ByteBuffer)7 Function (org.apache.cassandra.cql3.functions.Function)7 AbstractType (org.apache.cassandra.db.marshal.AbstractType)6 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)6 KeyspaceMetadata (org.apache.cassandra.schema.KeyspaceMetadata)6 TableMetadata (org.apache.cassandra.schema.TableMetadata)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)5 ProtocolVersion (org.apache.cassandra.transport.ProtocolVersion)5 InOrder (org.mockito.InOrder)5 List (java.util.List)4 UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)4 UDFunction (org.apache.cassandra.cql3.functions.UDFunction)4 ViewMetadata (org.apache.cassandra.schema.ViewMetadata)4 TestException (io.reactivex.rxjava3.exceptions.TestException)3 java.util (java.util)3 ArrayList (java.util.ArrayList)3