Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enums support #2: provide enums via FunctionNamespaceManagers #14828

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import com.facebook.presto.metadata.HandleResolver;
import com.facebook.presto.spi.HostAddress;
import com.facebook.presto.sql.analyzer.FeaturesConfig;
import com.facebook.presto.transaction.NoOpTransactionManager;
import com.facebook.presto.transaction.TransactionManager;
import com.facebook.presto.type.TypeDeserializer;
import com.facebook.presto.type.TypeRegistry;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -133,6 +135,8 @@ private JsonCodec<HiveSplit> getJsonCodec()
binder.install(new HandleJsonModule());
configBinder(binder).bindConfig(FeaturesConfig.class);

binder.bind(TransactionManager.class).to(NoOpTransactionManager.class).in(Scopes.SINGLETON);

binder.bind(TypeManager.class).to(TypeRegistry.class).in(Scopes.SINGLETON);
jsonBinder(binder).addDeserializerBinding(Type.class).to(TypeDeserializer.class);
newSetBinder(binder, Type.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.facebook.presto.spi.function.FunctionMetadata;
import com.facebook.presto.spi.function.FunctionMetadataManager;
import com.facebook.presto.spi.function.FunctionNamespaceManager;
import com.facebook.presto.spi.function.FunctionNamespaceManagerFactory;
import com.facebook.presto.spi.function.FunctionNamespaceTransactionHandle;
import com.facebook.presto.spi.function.ScalarFunctionImplementation;
import com.facebook.presto.spi.function.Signature;
Expand Down Expand Up @@ -64,11 +63,9 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

import static com.facebook.presto.SystemSessionProperties.isListBuiltInFunctionsOnly;
Expand Down Expand Up @@ -101,15 +98,12 @@

@ThreadSafe
public class FunctionManager
extends FunctionNamespaceLoader
implements FunctionMetadataManager
{
private final TypeManager typeManager;
private final TransactionManager transactionManager;
private final BuiltInFunctionNamespaceManager builtInFunctionNamespaceManager;
private final FunctionInvokerProvider functionInvokerProvider;
private final Map<String, FunctionNamespaceManagerFactory> functionNamespaceManagerFactories = new ConcurrentHashMap<>();
private final HandleResolver handleResolver;
private final Map<String, FunctionNamespaceManager<? extends SqlFunction>> functionNamespaceManagers = new ConcurrentHashMap<>();
private final LoadingCache<FunctionResolutionCacheKey, FunctionHandle> functionCache;
private final CacheStatsMBean cacheStatsMBean;

Expand All @@ -121,12 +115,11 @@ public FunctionManager(
FeaturesConfig featuresConfig,
HandleResolver handleResolver)
{
super(transactionManager, handleResolver);
this.typeManager = requireNonNull(typeManager, "typeManager is null");
this.transactionManager = requireNonNull(transactionManager, "transactionManager is null");
this.builtInFunctionNamespaceManager = new BuiltInFunctionNamespaceManager(typeManager, blockEncodingSerde, featuresConfig, this);
this.functionNamespaceManagers.put(DEFAULT_NAMESPACE.getCatalogName(), builtInFunctionNamespaceManager);
this.functionInvokerProvider = new FunctionInvokerProvider(this);
this.handleResolver = handleResolver;
if (typeManager instanceof TypeRegistry) {
((TypeRegistry) typeManager).setFunctionManager(this);
}
Expand All @@ -153,44 +146,11 @@ public CacheStatsMBean getFunctionResolutionCacheStats()
return cacheStatsMBean;
}

public void loadFunctionNamespaceManager(
String functionNamespaceManagerName,
String catalogName,
Map<String, String> properties)
{
requireNonNull(functionNamespaceManagerName, "functionNamespaceManagerName is null");
FunctionNamespaceManagerFactory factory = functionNamespaceManagerFactories.get(functionNamespaceManagerName);
checkState(factory != null, "No factory for function namespace manager %s", functionNamespaceManagerName);
FunctionNamespaceManager<?> functionNamespaceManager = factory.create(catalogName, properties);

transactionManager.registerFunctionNamespaceManager(catalogName, functionNamespaceManager);
if (functionNamespaceManagers.putIfAbsent(catalogName, functionNamespaceManager) != null) {
throw new IllegalArgumentException(format("Function namespace manager is already registered for catalog [%s]", catalogName));
}
}

@VisibleForTesting
public void addFunctionNamespace(String catalogName, FunctionNamespaceManager functionNamespaceManager)
{
transactionManager.registerFunctionNamespaceManager(catalogName, functionNamespaceManager);
if (functionNamespaceManagers.putIfAbsent(catalogName, functionNamespaceManager) != null) {
throw new IllegalArgumentException(format("Function namespace manager is already registered for catalog [%s]", catalogName));
}
}

public FunctionInvokerProvider getFunctionInvokerProvider()
{
return functionInvokerProvider;
}

public void addFunctionNamespaceFactory(FunctionNamespaceManagerFactory factory)
{
if (functionNamespaceManagerFactories.putIfAbsent(factory.getName(), factory) != null) {
throw new IllegalArgumentException(format("Resource group configuration manager '%s' is already registered", factory.getName()));
}
handleResolver.addFunctionNamespace(factory.getName(), factory.getHandleResolver());
}

public void registerBuiltInFunctions(List<? extends SqlFunction> functions)
{
builtInFunctionNamespaceManager.registerBuiltInFunctions(functions);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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.metadata;

import com.facebook.presto.spi.function.FunctionNamespaceManager;
import com.facebook.presto.spi.function.FunctionNamespaceManagerFactory;
import com.facebook.presto.spi.function.SqlFunction;
import com.facebook.presto.transaction.TransactionManager;
import com.google.common.annotations.VisibleForTesting;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.google.common.base.Preconditions.checkState;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public abstract class FunctionNamespaceLoader
{
private final Map<String, FunctionNamespaceManagerFactory> functionNamespaceManagerFactories = new ConcurrentHashMap<>();
private final HandleResolver handleResolver;
final TransactionManager transactionManager;

protected final Map<String, FunctionNamespaceManager<? extends SqlFunction>> functionNamespaceManagers = new ConcurrentHashMap<>();

public FunctionNamespaceLoader(
TransactionManager transactionManager,
HandleResolver handleResolver)
{
this.transactionManager = requireNonNull(transactionManager, "transactionManager is null");
this.handleResolver = handleResolver;
}

public void loadFunctionNamespaceManager(
String functionNamespaceManagerName,
String catalogName,
Map<String, String> properties)
{
requireNonNull(functionNamespaceManagerName, "functionNamespaceManagerName is null");
FunctionNamespaceManagerFactory factory = functionNamespaceManagerFactories.get(functionNamespaceManagerName);
checkState(factory != null, "No factory for function namespace manager %s", functionNamespaceManagerName);
FunctionNamespaceManager<?> functionNamespaceManager = factory.create(catalogName, properties);

transactionManager.registerFunctionNamespaceManager(catalogName, functionNamespaceManager);
if (functionNamespaceManagers.putIfAbsent(catalogName, functionNamespaceManager) != null) {
throw new IllegalArgumentException(format("Function namespace manager is already registered for catalog [%s]", catalogName));
}
}

public void addFunctionNamespaceFactory(FunctionNamespaceManagerFactory factory)
{
if (functionNamespaceManagerFactories.putIfAbsent(factory.getName(), factory) != null) {
throw new IllegalArgumentException(format("Resource group configuration manager '%s' is already registered", factory.getName()));
}
handleResolver.addFunctionNamespace(factory.getName(), factory.getHandleResolver());
}

@VisibleForTesting
public void addFunctionNamespace(String catalogName, FunctionNamespaceManager functionNamespaceManager)
{
transactionManager.registerFunctionNamespaceManager(catalogName, functionNamespaceManager);
if (functionNamespaceManagers.putIfAbsent(catalogName, functionNamespaceManager) != null) {
throw new IllegalArgumentException(format("Function namespace manager is already registered for catalog [%s]", catalogName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void addConnectorName(String name, ConnectorHandleResolver resolver)
requireNonNull(name, "name is null");
requireNonNull(resolver, "resolver is null");
MaterializedHandleResolver existingResolver = handleResolvers.putIfAbsent(name, new MaterializedHandleResolver(resolver));
checkState(existingResolver == null || existingResolver.equals(resolver),
checkState(existingResolver == null || existingResolver.equals(new MaterializedHandleResolver(resolver)),
"Connector '%s' is already assigned to resolver: %s", name, existingResolver);
}

Expand All @@ -74,7 +74,7 @@ public void addFunctionNamespace(String name, FunctionHandleResolver resolver)
requireNonNull(name, "name is null");
requireNonNull(resolver, "resolver is null");
MaterializedFunctionHandleResolver existingResolver = functionHandleResolvers.putIfAbsent(name, new MaterializedFunctionHandleResolver(resolver));
checkState(existingResolver == null || existingResolver.equals(resolver), "Name %s is already assigned to function resolver: %s", name, existingResolver);
checkState(existingResolver == null || existingResolver.equals(new MaterializedFunctionHandleResolver(resolver)), "Name %s is already assigned to function resolver: %s", name, existingResolver);
}

public String getId(ConnectorTableHandle tableHandle)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static MetadataManager createTestMetadataManager(CatalogManager catalogMa

public static MetadataManager createTestMetadataManager(TransactionManager transactionManager, FeaturesConfig featuresConfig)
{
TypeManager typeManager = new TypeRegistry(ImmutableSet.of(), featuresConfig);
TypeManager typeManager = new TypeRegistry(ImmutableSet.of(), featuresConfig, transactionManager, new HandleResolver());
return new MetadataManager(
featuresConfig,
typeManager,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.facebook.presto.metadata;

import com.facebook.airlift.log.Logger;
import com.facebook.presto.type.TypeRegistry;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableList;

Expand All @@ -35,13 +36,15 @@ public class StaticFunctionNamespaceStore
private static final Splitter SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings();

private final FunctionManager functionManager;
private final TypeRegistry typeRegistry;
private final File configDir;
private final AtomicBoolean functionNamespaceLoading = new AtomicBoolean();

@Inject
public StaticFunctionNamespaceStore(FunctionManager functionManager, StaticFunctionNamespaceStoreConfig config)
public StaticFunctionNamespaceStore(FunctionManager functionManager, StaticFunctionNamespaceStoreConfig config, TypeRegistry typeRegistry)
{
this.functionManager = functionManager;
this.typeRegistry = typeRegistry;
this.configDir = config.getFunctionNamespaceConfigurationDir();
}

Expand Down Expand Up @@ -70,6 +73,7 @@ private void loadFunctionNamespaceManager(File file)
checkState(functionNamespaceManagerName != null, "Function namespace configuration %s does not contain function-namespace-manager.name", file.getAbsoluteFile());

functionManager.loadFunctionNamespaceManager(functionNamespaceManagerName, catalogName, properties);
typeRegistry.loadFunctionNamespaceManager(functionNamespaceManagerName, catalogName, properties);
log.info("-- Added function namespace manager [%s] --", catalogName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ public void installPlugin(Plugin plugin)
for (FunctionNamespaceManagerFactory functionNamespaceManagerFactory : plugin.getFunctionNamespaceManagerFactories()) {
log.info("Registering function namespace manager %s", functionNamespaceManagerFactory.getName());
metadata.getFunctionManager().addFunctionNamespaceFactory(functionNamespaceManagerFactory);
typeRegistry.addFunctionNamespaceFactory(functionNamespaceManagerFactory);
}

for (SessionPropertyConfigurationManagerFactory sessionConfigFactory : plugin.getSessionPropertyConfigurationManagerFactories()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ public void createCatalog(String catalogName, String connectorName, Map<String,
public void loadFunctionNamespaceManager(String functionNamespaceManagerName, String catalogName, Map<String, String> properties)
{
metadata.getFunctionManager().loadFunctionNamespaceManager(functionNamespaceManagerName, catalogName, properties);
typeRegistry.loadFunctionNamespaceManager(functionNamespaceManagerName, catalogName, properties);
}

public LocalQueryRunner printPlan()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
import com.facebook.presto.common.type.TypeSignatureParameter;
import com.facebook.presto.common.type.VarcharType;
import com.facebook.presto.metadata.FunctionManager;
import com.facebook.presto.metadata.FunctionNamespaceLoader;
import com.facebook.presto.metadata.HandleResolver;
import com.facebook.presto.sql.analyzer.FeaturesConfig;
import com.facebook.presto.transaction.TransactionManager;
import com.facebook.presto.type.khyperloglog.KHyperLogLogType;
import com.facebook.presto.type.setdigest.SetDigestType;
import com.google.common.annotations.VisibleForTesting;
Expand All @@ -47,6 +50,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -76,6 +80,7 @@
import static com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType;
import static com.facebook.presto.common.type.VarcharType.createVarcharType;
import static com.facebook.presto.sql.analyzer.TypeSignatureProvider.fromTypes;
import static com.facebook.presto.transaction.InMemoryTransactionManager.createTestTransactionManager;
import static com.facebook.presto.type.ArrayParametricType.ARRAY;
import static com.facebook.presto.type.CodePointsType.CODE_POINTS;
import static com.facebook.presto.type.ColorType.COLOR;
Expand All @@ -100,6 +105,7 @@

@ThreadSafe
public final class TypeRegistry
extends FunctionNamespaceLoader
implements TypeManager
{
private final ConcurrentMap<TypeSignature, Type> types = new ConcurrentHashMap<>();
Expand All @@ -113,12 +119,13 @@ public final class TypeRegistry
@VisibleForTesting
public TypeRegistry()
{
this(ImmutableSet.of(), new FeaturesConfig());
this(ImmutableSet.of(), new FeaturesConfig(), createTestTransactionManager(), new HandleResolver());
}

@Inject
public TypeRegistry(Set<Type> types, FeaturesConfig featuresConfig)
public TypeRegistry(Set<Type> types, FeaturesConfig featuresConfig, TransactionManager transactionManager, HandleResolver handleResolver)
{
super(transactionManager, handleResolver);
requireNonNull(types, "types is null");
this.featuresConfig = requireNonNull(featuresConfig, "featuresConfig is null");

Expand Down Expand Up @@ -182,6 +189,7 @@ public void setFunctionManager(FunctionManager functionManager)
public Type getType(TypeSignature signature)
{
Type type = types.get(signature);

if (type == null) {
try {
return parametricTypeCache.getUnchecked(signature);
Expand Down Expand Up @@ -210,6 +218,13 @@ private Type instantiateParametricType(TypeSignature signature)
}

ParametricType parametricType = parametricTypes.get(signature.getBase().toLowerCase(Locale.ENGLISH));
if (parametricType == null) {
parametricType = functionNamespaceManagers.values().stream()
.map(manager -> manager.getParametricType(signature))
.filter(Objects::nonNull)
.findFirst()
.orElse(null);
}
if (parametricType == null) {
throw new IllegalArgumentException("Unknown type " + signature);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@
import com.facebook.presto.testing.TestingHandleResolver;
import com.facebook.presto.testing.TestingSplit;
import com.facebook.presto.testing.TestingTransactionHandle;
import com.facebook.presto.transaction.NoOpTransactionManager;
import com.facebook.presto.transaction.TransactionManager;
import com.facebook.presto.type.TypeDeserializer;
import com.facebook.presto.type.TypeRegistry;
import com.google.common.collect.ImmutableMultimap;
Expand Down Expand Up @@ -241,6 +243,7 @@ public void configure(Binder binder)
binder.bind(JsonMapper.class);
configBinder(binder).bindConfig(FeaturesConfig.class);
binder.bind(TypeRegistry.class).in(Scopes.SINGLETON);
binder.bind(TransactionManager.class).to(NoOpTransactionManager.class).in(Scopes.SINGLETON);
binder.bind(TypeManager.class).to(TypeRegistry.class).in(Scopes.SINGLETON);
jsonBinder(binder).addDeserializerBinding(Type.class).to(TypeDeserializer.class);
newSetBinder(binder, Type.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@
import com.facebook.presto.sql.relational.SqlToRowExpressionTranslator;
import com.facebook.presto.sql.tree.Expression;
import com.facebook.presto.sql.tree.NodeRef;
import com.facebook.presto.transaction.NoOpTransactionManager;
import com.facebook.presto.transaction.TransactionManager;
import com.facebook.presto.type.TypeDeserializer;
import com.facebook.presto.type.TypeRegistry;
import com.google.common.collect.ImmutableList;
Expand Down Expand Up @@ -247,6 +249,7 @@ private JsonCodec<RowExpression> getJsonCodec()
configBinder(binder).bindConfig(FeaturesConfig.class);

binder.bind(TypeManager.class).to(TypeRegistry.class).in(Scopes.SINGLETON);
binder.bind(TransactionManager.class).to(NoOpTransactionManager.class).in(Scopes.SINGLETON);
jsonBinder(binder).addDeserializerBinding(Type.class).to(TypeDeserializer.class);
newSetBinder(binder, Type.class);

Expand Down
Loading