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

Add native types manager #24127

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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 @@ -21,7 +21,6 @@
import com.facebook.presto.common.block.BlockSerdeUtil;
import com.facebook.presto.common.function.OperatorType;
import com.facebook.presto.common.function.SqlFunctionResult;
import com.facebook.presto.common.type.DistinctTypeInfo;
import com.facebook.presto.common.type.ParametricType;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.common.type.TypeManager;
Expand Down Expand Up @@ -221,6 +220,7 @@
import com.facebook.presto.operator.window.RowNumberFunction;
import com.facebook.presto.operator.window.SqlWindowFunction;
import com.facebook.presto.operator.window.WindowFunctionSupplier;
import com.facebook.presto.spi.ExactTypeSignature;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.function.AggregationFunctionImplementation;
import com.facebook.presto.spi.function.AlterRoutineCharacteristics;
Expand All @@ -235,6 +235,7 @@
import com.facebook.presto.spi.function.SqlFunctionVisibility;
import com.facebook.presto.spi.function.SqlInvokedFunction;
import com.facebook.presto.spi.function.SqlInvokedScalarFunctionImplementation;
import com.facebook.presto.spi.type.TypeManagerProvider;
import com.facebook.presto.sql.analyzer.FunctionsConfig;
import com.facebook.presto.sql.analyzer.TypeSignatureProvider;
import com.facebook.presto.type.BigintOperators;
Expand Down Expand Up @@ -307,7 +308,6 @@
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -535,7 +535,7 @@

@ThreadSafe
public class BuiltInTypeAndFunctionNamespaceManager
implements FunctionNamespaceManager<SqlFunction>
implements FunctionNamespaceManager<SqlFunction>, TypeManagerProvider
{
public static final CatalogSchemaName DEFAULT_NAMESPACE = new CatalogSchemaName("presto", "default");
public static final String ID = "builtin";
Expand Down Expand Up @@ -1265,6 +1265,7 @@ public ScalarFunctionImplementation getScalarFunctionImplementation(Signature si
}
}

@Override
public Optional<Type> getType(TypeSignature typeSignature)
{
Type type = types.get(typeSignature);
Expand Down Expand Up @@ -1299,12 +1300,19 @@ public void addParametricType(ParametricType parametricType)
parametricTypes.putIfAbsent(name, parametricType);
}

public Collection<ParametricType> getParametricTypes()
@Override
public Map<String, ParametricType> getParametricTypes()
{
return parametricTypes.values();
return parametricTypes;
}

private Type instantiateParametricType(ExactTypeSignature exactSignature)
{
return instantiateParametricType(exactSignature, functionAndTypeManager, parametricTypes);
}

public Type instantiateParametricType(ExactTypeSignature exactSignature,
FunctionAndTypeManager functionAndTypeManager, Map<String, ParametricType> parametricTypes)
{
TypeSignature signature = exactSignature.getTypeSignature();
List<TypeParameter> parameters = new ArrayList<>();
Expand Down Expand Up @@ -1453,92 +1461,6 @@ public Collection<SqlFunction> get(QualifiedObjectName name)
}
}

/**
* TypeSignature but has overridden equals(). Here, we compare exact signature of any underlying distinct
* types. Some distinct types may have extra information on their lazily loaded parents, and same parent
* information is compared in equals(). This is needed to cache types in parametricTypesCache.
*/
private static class ExactTypeSignature
{
private final TypeSignature typeSignature;

public ExactTypeSignature(TypeSignature typeSignature)
{
this.typeSignature = typeSignature;
}

public TypeSignature getTypeSignature()
{
return typeSignature;
}

@Override
public int hashCode()
{
return Objects.hash(typeSignature);
}

@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

ExactTypeSignature other = (ExactTypeSignature) o;
return equals(typeSignature, other.typeSignature);
}

private static boolean equals(TypeSignature left, TypeSignature right)
{
if (!left.equals(right)) {
return false;
}

if (left.isDistinctType() && right.isDistinctType()) {
return equals(left.getDistinctTypeInfo(), right.getDistinctTypeInfo());
}
int index = 0;
for (TypeSignatureParameter leftParameter : left.getParameters()) {
TypeSignatureParameter rightParameter = right.getParameters().get(index++);
if (!leftParameter.getKind().equals(rightParameter.getKind())) {
return false;
}

switch (leftParameter.getKind()) {
case TYPE:
if (!equals(leftParameter.getTypeSignature(), rightParameter.getTypeSignature())) {
return false;
}
break;
case NAMED_TYPE:
if (!equals(leftParameter.getNamedTypeSignature().getTypeSignature(), rightParameter.getNamedTypeSignature().getTypeSignature())) {
return false;
}
break;
case DISTINCT_TYPE:
if (!equals(leftParameter.getDistinctTypeInfo(), rightParameter.getDistinctTypeInfo())) {
return false;
}
break;
}
}
return true;
}

private static boolean equals(DistinctTypeInfo left, DistinctTypeInfo right)
{
return Objects.equals(left.getName(), right.getName()) &&
Objects.equals(left.getBaseType(), right.getBaseType()) &&
Objects.equals(left.isOrderable(), right.isOrderable()) &&
Objects.equals(left.getTopMostAncestor(), right.getTopMostAncestor()) &&
Objects.equals(left.getOtherAncestors(), right.getOtherAncestors());
}
}

private static class MagicLiteralFunction
extends SqlScalarFunction
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.facebook.presto.common.type.TypeWithName;
import com.facebook.presto.common.type.UserDefinedType;
import com.facebook.presto.operator.window.WindowFunctionSupplier;
import com.facebook.presto.spi.ExactTypeSignature;
import com.facebook.presto.spi.PrestoException;
import com.facebook.presto.spi.function.AggregationFunctionImplementation;
import com.facebook.presto.spi.function.AlterRoutineCharacteristics;
Expand All @@ -50,6 +51,9 @@
import com.facebook.presto.spi.function.SqlFunction;
import com.facebook.presto.spi.function.SqlFunctionId;
import com.facebook.presto.spi.function.SqlInvokedFunction;
import com.facebook.presto.spi.type.TypeManagerContext;
import com.facebook.presto.spi.type.TypeManagerFactory;
import com.facebook.presto.spi.type.TypeManagerProvider;
import com.facebook.presto.sql.analyzer.FeaturesConfig;
import com.facebook.presto.sql.analyzer.FunctionAndTypeResolver;
import com.facebook.presto.sql.analyzer.FunctionsConfig;
Expand Down Expand Up @@ -122,8 +126,10 @@ public class FunctionAndTypeManager
private final BuiltInTypeAndFunctionNamespaceManager builtInTypeAndFunctionNamespaceManager;
private final FunctionInvokerProvider functionInvokerProvider;
private final Map<String, FunctionNamespaceManagerFactory> functionNamespaceManagerFactories = new ConcurrentHashMap<>();
private final Map<String, TypeManagerFactory> typeManagerFactories = new ConcurrentHashMap<>();
private final HandleResolver handleResolver;
private final Map<String, FunctionNamespaceManager<? extends SqlFunction>> functionNamespaceManagers = new ConcurrentHashMap<>();
private final Map<String, TypeManagerProvider> typeManagers = new ConcurrentHashMap<>();
private final FunctionSignatureMatcher functionSignatureMatcher;
private final TypeCoercer typeCoercer;
private final LoadingCache<FunctionResolutionCacheKey, FunctionHandle> functionCache;
Expand Down Expand Up @@ -220,6 +226,12 @@ public FunctionMetadata getFunctionMetadata(FunctionHandle functionHandle)
return FunctionAndTypeManager.this.getFunctionMetadata(functionHandle);
}

@Override
public Type instantiateParametricType(ExactTypeSignature exactSignature)
{
return FunctionAndTypeManager.this.instantiateParametricType(exactSignature);
}

@Override
public Collection<SqlFunction> listBuiltInFunctions()
{
Expand Down Expand Up @@ -290,6 +302,17 @@ public FunctionMetadata getFunctionMetadata(FunctionHandle functionHandle)
return functionNamespaceManager.get().getFunctionMetadata(functionHandle);
}

@Override
public Type instantiateParametricType(ExactTypeSignature exactSignature)
{
// This method is only used by nativeTypeManager, so check if typeManagers is empty first
checkState(!typeManagers.isEmpty());
return builtInTypeAndFunctionNamespaceManager.instantiateParametricType(
exactSignature,
this,
getServingTypeManagerProvider().getParametricTypes());
}

@Override
public Type getType(TypeSignature signature)
{
Expand All @@ -298,7 +321,7 @@ public Type getType(TypeSignature signature)
if (signature.isDistinctType()) {
return getDistinctType(signature.getParameters().get(0).getDistinctTypeInfo());
}
Optional<Type> type = builtInTypeAndFunctionNamespaceManager.getType(signature.getStandardTypeSignature());
Optional<Type> type = getServingTypeManagerProvider().getType(signature.getStandardTypeSignature());
if (type.isPresent()) {
if (signature.getTypeSignatureBase().hasTypeName()) {
return new TypeWithName(signature.getTypeSignatureBase().getTypeName(), type.get());
Expand Down Expand Up @@ -335,6 +358,32 @@ public void addFunctionNamespaceFactory(FunctionNamespaceManagerFactory factory)
handleResolver.addFunctionNamespace(factory.getName(), factory.getHandleResolver());
}

public void loadTypeManager(String typeManagerName)
{
requireNonNull(typeManagerName, "typeManagerName is null");
TypeManagerFactory factory = typeManagerFactories.get(typeManagerName);
checkState(factory != null, "No factory for type manager %s", typeManagerName);
TypeManagerProvider typeManager = factory.create(new TypeManagerContext(Optional.of(this)));

if (typeManagers.putIfAbsent(typeManagerName, typeManager) != null) {
throw new IllegalArgumentException(format("Type manager [%s] is already registered", typeManager));
}
}

public void loadTypeManagers()
{
for (String typeManagerName : typeManagerFactories.keySet()) {
loadTypeManager(typeManagerName);
}
}

public void addTypeManagerFactory(TypeManagerFactory factory)
{
if (typeManagerFactories.putIfAbsent(factory.getName(), factory) != null) {
throw new IllegalArgumentException(format("Type manager '%s' is already registered", factory.getName()));
}
}

public void registerBuiltInFunctions(List<? extends SqlFunction> functions)
{
builtInTypeAndFunctionNamespaceManager.registerBuiltInFunctions(functions);
Expand Down Expand Up @@ -488,7 +537,7 @@ public List<Type> getTypes()

public Collection<ParametricType> getParametricTypes()
{
return ImmutableList.copyOf(builtInTypeAndFunctionNamespaceManager.getParametricTypes());
return ImmutableList.copyOf(builtInTypeAndFunctionNamespaceManager.getParametricTypes().values());
}

public Optional<Type> getCommonSuperType(Type firstType, Type secondType)
Expand Down Expand Up @@ -726,6 +775,20 @@ private Optional<FunctionNamespaceManager<? extends SqlFunction>> getServingFunc
return Optional.ofNullable(functionNamespaceManagers.get(typeSignatureBase.getTypeName().getCatalogName()));
}

private TypeManagerProvider getServingTypeManagerProvider()
{
TypeManagerProvider typeManagerProvider = builtInTypeAndFunctionNamespaceManager;
for (Map.Entry<String, TypeManagerFactory> entry : typeManagerFactories.entrySet()) {
TypeManagerFactory typeManagerFactory = entry.getValue();
// Get the type manager name from the factory
String name = typeManagerFactory.getName();

// Check if the type manager exists in typeManagers else use builtInTypeAndFunctionNamespaceManager
typeManagerProvider = typeManagers.getOrDefault(name, typeManagerProvider);
}
return typeManagerProvider;
}

private static class FunctionResolutionCacheKey
{
private final QualifiedObjectName functionName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.facebook.presto.spi.tracing.TracerProvider;
import com.facebook.presto.spi.ttl.ClusterTtlProviderFactory;
import com.facebook.presto.spi.ttl.NodeTtlFetcherFactory;
import com.facebook.presto.spi.type.TypeManagerFactory;
import com.facebook.presto.sql.analyzer.AnalyzerProviderManager;
import com.facebook.presto.sql.analyzer.QueryPreparerProviderManager;
import com.facebook.presto.sql.planner.sanity.PlanCheckerProviderManager;
Expand Down Expand Up @@ -372,6 +373,11 @@ public void installCoordinatorPlugin(CoordinatorPlugin plugin)
log.info("Registering system session property provider factory %s", providerFactory.getName());
metadata.getSessionPropertyManager().addSessionPropertyProviderFactory(providerFactory);
}

for (TypeManagerFactory typeManagerFactory : plugin.getTypeManagerFactories()) {
log.info("Registering type manager factory %s", typeManagerFactory.getName());
metadata.getFunctionAndTypeManager().addTypeManagerFactory(typeManagerFactory);
}
}

private URLClassLoader buildClassLoader(String plugin)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import com.facebook.presto.execution.warnings.WarningCollectorModule;
import com.facebook.presto.metadata.Catalog;
import com.facebook.presto.metadata.CatalogManager;
import com.facebook.presto.metadata.FunctionAndTypeManager;
import com.facebook.presto.metadata.SessionPropertyManager;
import com.facebook.presto.metadata.StaticCatalogStore;
import com.facebook.presto.metadata.StaticFunctionNamespaceStore;
Expand Down Expand Up @@ -181,6 +182,7 @@ public void run()
injector.getInstance(GracefulShutdownHandler.class).loadNodeStatusNotification();
injector.getInstance(PlanCheckerProviderManager.class).loadPlanCheckerProviders();
injector.getInstance(SessionPropertyManager.class).loadSessionPropertyProviders();
injector.getInstance(FunctionAndTypeManager.class).loadTypeManagers();
startAssociatedProcesses(injector);

injector.getInstance(Announcer.class).start();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ default void loadSessionPropertyProvider(String sessionPropertyProviderName)
throw new UnsupportedOperationException();
}

default void loadTypeManager(String typeManagerName)
{
throw new UnsupportedOperationException();
}

Lock getExclusiveLock();

class MaterializedResultWithPlan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
package com.facebook.presto.sidecar;

import com.facebook.presto.sidecar.sessionpropertyproviders.NativeSystemSessionPropertyProviderFactory;
import com.facebook.presto.sidecar.type.NativeTypeManagerFactory;
import com.facebook.presto.spi.CoordinatorPlugin;
import com.facebook.presto.spi.session.WorkerSessionPropertyProviderFactory;
import com.facebook.presto.spi.type.TypeManagerFactory;
import com.google.common.collect.ImmutableList;

public class NativeSidecarPlugin
Expand All @@ -26,4 +28,10 @@ public Iterable<WorkerSessionPropertyProviderFactory> getWorkerSessionPropertyPr
{
return ImmutableList.of(new NativeSystemSessionPropertyProviderFactory());
}

@Override
public Iterable<TypeManagerFactory> getTypeManagerFactories()
{
return ImmutableList.of(new NativeTypeManagerFactory());
}
}
Loading
Loading