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)));
}
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)));
}
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);
}
};
}
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);
}
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));
}
Aggregations