
com.facebook.presto.functionNamespace.testing.InMemoryFunctionNamespaceManager Maven / Gradle / Ivy
/*
* Licensed 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 com.facebook.presto.functionNamespace.testing;
import com.facebook.presto.functionNamespace.AbstractSqlInvokedFunctionNamespaceManager;
import com.facebook.presto.functionNamespace.SqlInvokedFunctionNamespaceManagerConfig;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.function.AlterRoutineCharacteristics;
import com.facebook.presto.spi.function.FunctionMetadata;
import com.facebook.presto.spi.function.QualifiedFunctionName;
import com.facebook.presto.spi.function.ScalarFunctionImplementation;
import com.facebook.presto.spi.function.SqlFunctionHandle;
import com.facebook.presto.spi.function.SqlFunctionId;
import com.facebook.presto.spi.function.SqlInvokedFunction;
import com.facebook.presto.spi.type.TypeSignature;
import javax.annotation.concurrent.ThreadSafe;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import static com.facebook.presto.spi.StandardErrorCode.GENERIC_USER_ERROR;
import static com.facebook.presto.spi.StandardErrorCode.NOT_SUPPORTED;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.MoreCollectors.onlyElement;
import static java.lang.String.format;
@ThreadSafe
public class InMemoryFunctionNamespaceManager
extends AbstractSqlInvokedFunctionNamespaceManager
{
private final Map latestFunctions = new ConcurrentHashMap<>();
public InMemoryFunctionNamespaceManager(String catalogName, SqlInvokedFunctionNamespaceManagerConfig config)
{
super(catalogName, config);
}
@Override
public synchronized void createFunction(SqlInvokedFunction function, boolean replace)
{
SqlFunctionId functionId = function.getFunctionId();
if (!replace && latestFunctions.containsKey(function.getFunctionId())) {
throw new PrestoException(GENERIC_USER_ERROR, format("Function '%s' already exists", functionId.getId()));
}
SqlInvokedFunction replacedFunction = latestFunctions.get(functionId);
long version = 1;
if (replacedFunction != null) {
version = replacedFunction.getRequiredVersion() + 1;
}
latestFunctions.put(functionId, function.withVersion(version));
}
@Override
public void alterFunction(QualifiedFunctionName functionName, Optional> parameterTypes, AlterRoutineCharacteristics alterRoutineCharacteristics)
{
throw new PrestoException(NOT_SUPPORTED, "Alter Function is not supported in InMemoryFunctionNamespaceManager");
}
@Override
public synchronized void dropFunction(QualifiedFunctionName functionName, Optional> parameterTypes, boolean exists)
{
throw new PrestoException(NOT_SUPPORTED, "Drop Function is not supported in InMemoryFunctionNamespaceManager");
}
@Override
public Collection listFunctions()
{
return latestFunctions.values();
}
@Override
public Collection fetchFunctionsDirect(QualifiedFunctionName name)
{
return latestFunctions.values().stream()
.filter(function -> function.getSignature().getName().equals(name))
.map(InMemoryFunctionNamespaceManager::copyFunction)
.collect(toImmutableList());
}
@Override
public FunctionMetadata fetchFunctionMetadataDirect(SqlFunctionHandle functionHandle)
{
return fetchFunctionsDirect(functionHandle.getFunctionId().getFunctionName()).stream()
.filter(function -> function.getRequiredFunctionHandle().equals(functionHandle))
.map(AbstractSqlInvokedFunctionNamespaceManager::sqlInvokedFunctionToMetadata)
.collect(onlyElement());
}
@Override
protected ScalarFunctionImplementation fetchFunctionImplementationDirect(SqlFunctionHandle functionHandle)
{
return fetchFunctionsDirect(functionHandle.getFunctionId().getFunctionName()).stream()
.filter(function -> function.getRequiredFunctionHandle().equals(functionHandle))
.map(AbstractSqlInvokedFunctionNamespaceManager::sqlInvokedFunctionToImplementation)
.collect(onlyElement());
}
private static SqlInvokedFunction copyFunction(SqlInvokedFunction function)
{
return new SqlInvokedFunction(
function.getSignature().getName(),
function.getParameters(),
function.getSignature().getReturnType(),
function.getDescription(),
function.getRoutineCharacteristics(),
function.getBody(),
function.getVersion());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy