Created
February 15, 2017 17:49
-
-
Save iamaleksey/a1f6f30345c119cfd79464359e9652b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Licensed to the Apache Software Foundation (ASF) under one | |
* or more contributor license agreements. See the NOTICE file | |
* distributed with this work for additional information | |
* regarding copyright ownership. The ASF licenses this file | |
* to you under the Apache License, Version 2.0 (the | |
* "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package org.apache.cassandra.cql3.statements.schema; | |
import java.util.Collection; | |
import java.util.List; | |
import java.util.function.Predicate; | |
import org.apache.cassandra.cql3.CQL3Type; | |
import org.apache.cassandra.cql3.functions.Function; | |
import org.apache.cassandra.cql3.functions.FunctionName; | |
import org.apache.cassandra.cql3.functions.ScalarFunction; | |
import org.apache.cassandra.cql3.functions.UDAggregate; | |
import org.apache.cassandra.db.marshal.AbstractType; | |
import org.apache.cassandra.schema.Functions; | |
import org.apache.cassandra.schema.KeyspaceMetadata; | |
import org.apache.cassandra.schema.Keyspaces; | |
import static java.lang.String.format; | |
import static java.lang.String.join; | |
import static java.util.stream.Collectors.toList; | |
import static com.google.common.collect.Iterables.transform; | |
public final class DropFunctionStatement extends AlterSchemaStatement | |
{ | |
private final String functionName; | |
private final Collection<CQL3Type.Raw> arguments; | |
private final boolean argumentsSpeficied; | |
private final boolean ifExists; | |
public DropFunctionStatement(String keyspaceName, | |
String functionName, | |
Collection<CQL3Type.Raw> arguments, | |
boolean argumentsSpeficied, | |
boolean ifExists) | |
{ | |
super(keyspaceName); | |
this.functionName = functionName; | |
this.arguments = arguments; | |
this.argumentsSpeficied = argumentsSpeficied; | |
this.ifExists = ifExists; | |
} | |
Keyspaces apply(Keyspaces schema) | |
{ | |
String name = | |
argumentsSpeficied | |
? format("%s.%s(%s)", keyspaceName, functionName, join(", ", transform(arguments, CQL3Type.Raw::toString))) | |
: format("%s.%s", keyspaceName, functionName); | |
KeyspaceMetadata keyspace = schema.getNullable(keyspaceName); | |
if (null == keyspace) | |
{ | |
if (ifExists) | |
return schema; | |
throw ire("Function '%s' doesn't exist", name); | |
} | |
Collection<Function> functions = keyspace.functions.get(new FunctionName(keyspaceName, functionName)); | |
if (functions.size() > 1 && !argumentsSpeficied) | |
{ | |
throw ire("'DROP FUNCTION %s' matches multiple function definitions; " + | |
"specify the argument types by issuing a statement like " + | |
"'DROP FUNCTION %s (type, type, ...)'. You can use cqlsh " + | |
"'DESCRIBE FUNCTION %s' command to find all overloads", | |
functionName, functionName, functionName); | |
} | |
arguments.stream() | |
.filter(CQL3Type.Raw::isFrozen) | |
.findFirst() | |
.ifPresent(t -> { throw ire("Argument '%s' cannot be frozen; remove frozen<> modifier from '%s'", t, t); }); | |
List<AbstractType<?>> argumentTypes = | |
arguments.stream() | |
.map(t -> t.prepare(keyspaceName, keyspace.types)) | |
.map(CQL3Type::getType) | |
.collect(toList()); | |
Predicate<Function> filter = f -> f instanceof ScalarFunction; | |
if (argumentsSpeficied) | |
filter = filter.and(f -> Functions.typesMatch(f.argTypes(), argumentTypes)); | |
Function function = functions.stream().filter(filter).findAny().orElse(null); | |
if (null == function) | |
{ | |
if (ifExists) | |
return schema; | |
throw ire("Function '%s' doesn't exist", name); | |
} | |
Collection<UDAggregate> dependentAggregates = keyspace.functions.aggregatesUsingFunction(function); | |
if (!dependentAggregates.isEmpty()) | |
throw ire("Function '%s' is still referenced by %s", name, dependentAggregates); | |
return schema.withAddedOrUpdated(keyspace.withSwapped(keyspace.functions.without(function))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment