Skip to content

Commit

Permalink
Add a native type manager
Browse files Browse the repository at this point in the history
  • Loading branch information
pdabre12 authored and Pratik Joseph Dabre committed Feb 5, 2025
1 parent c1bd497 commit d5ae82c
Show file tree
Hide file tree
Showing 23 changed files with 715 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public final class StandardTypes
public static final String VARCHAR_ENUM = "VarcharEnum";
public static final String DISTINCT_TYPE = "DistinctType";
public static final String UUID = "uuid";
public static final String FUNCTION = "function";
public static final String UNKNOWN = "unknown";

private StandardTypes() {}

Expand All @@ -70,6 +72,7 @@ private StandardTypes() {}
ROW,
ARRAY,
MAP,
FUNCTION,
QDIGEST,
TDIGEST,
KLL_SKETCH,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.facebook.presto.common.type;

import java.util.List;
import java.util.Map;

public interface TypeManager
{
Expand All @@ -28,4 +29,13 @@ public interface TypeManager
Type getParameterizedType(String baseTypeName, List<TypeSignatureParameter> typeParameters);

boolean canCoerce(Type actualType, Type expectedType);

default Type instantiateParametricType(TypeSignature typeSignature)
{
throw new UnsupportedOperationException();
}

List<Type> getTypes();

Map<String, ParametricType> getParametricTypes();
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.Map;

import static com.facebook.presto.common.type.BigintType.BIGINT;
import static com.facebook.presto.common.type.BooleanType.BOOLEAN;
Expand Down Expand Up @@ -54,8 +55,15 @@ public boolean canCoerce(Type actualType, Type expectedType)
throw new UnsupportedOperationException();
}

private List<Type> getTypes()
@Override
public List<Type> getTypes()
{
return ImmutableList.of(BOOLEAN, INTEGER, BIGINT, DOUBLE, VARCHAR, VARBINARY, TIMESTAMP, DATE, ID, HYPER_LOG_LOG);
}

@Override
public Map<String, ParametricType> getParametricTypes()
{
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@

package com.facebook.presto.hudi;

import com.facebook.presto.common.type.ParametricType;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.common.type.TypeManager;
import com.facebook.presto.common.type.TypeSignature;
import com.facebook.presto.common.type.TypeSignatureParameter;
import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.Map;

import static com.facebook.presto.common.type.BigintType.BIGINT;
import static com.facebook.presto.common.type.BooleanType.BOOLEAN;
Expand Down Expand Up @@ -58,8 +60,15 @@ public boolean canCoerce(Type actualType, Type expectedType)
throw new UnsupportedOperationException();
}

private List<Type> getTypes()
@Override
public List<Type> getTypes()
{
return ImmutableList.of(BOOLEAN, INTEGER, BIGINT, DOUBLE, VARCHAR, VARBINARY, TIMESTAMP, DATE, HYPER_LOG_LOG);
}

@Override
public Map<String, ParametricType> getParametricTypes()
{
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.facebook.presto.common.Page;
import com.facebook.presto.common.type.BooleanType;
import com.facebook.presto.common.type.DoubleType;
import com.facebook.presto.common.type.ParametricType;
import com.facebook.presto.common.type.Type;
import com.facebook.presto.common.type.TypeManager;
import com.facebook.presto.common.type.TypeSignature;
Expand Down Expand Up @@ -50,6 +51,7 @@

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import static com.facebook.presto.RowPagesBuilder.rowPagesBuilder;
Expand Down Expand Up @@ -178,9 +180,16 @@ public boolean canCoerce(Type actualType, Type expectedType)
throw new UnsupportedOperationException();
}

private List<Type> getTypes()
@Override
public List<Type> getTypes()
{
return ImmutableList.of(BooleanType.BOOLEAN, INTEGER, BIGINT, DoubleType.DOUBLE, VARCHAR, VARBINARY, TIMESTAMP, DATE, HYPER_LOG_LOG);
}

@Override
public Map<String, ParametricType> getParametricTypes()
{
throw new UnsupportedOperationException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public RecordCursor cursor(ConnectorTransactionHandle transactionHandle, Connect
for (Type type : functionAndTypeManager.getTypes()) {
addTypeRow(table, type);
}
addParametricTypeRows(table, functionAndTypeManager.getParametricTypes());
addParametricTypeRows(table, functionAndTypeManager.getParametricTypes().values());
return table.build().cursor();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@

@ThreadSafe
public class BuiltInTypeAndFunctionNamespaceManager
implements FunctionNamespaceManager<SqlFunction>
implements FunctionNamespaceManager<SqlFunction>, TypeManager
{
public static final CatalogSchemaName JAVA_BUILTIN_NAMESPACE = new CatalogSchemaName("presto", "default");
public static final String ID = "builtin";
Expand Down Expand Up @@ -1262,21 +1262,34 @@ public ScalarFunctionImplementation getScalarFunctionImplementation(Signature si
}
}

public Optional<Type> getType(TypeSignature typeSignature)
@Override
public Type getType(TypeSignature typeSignature)
{
Type type = types.get(typeSignature);
if (type != null) {
return Optional.of(type);
return type;
}
try {
return Optional.ofNullable(parametricTypeCache.getUnchecked(new ExactTypeSignature(typeSignature)));
return parametricTypeCache.getUnchecked(new ExactTypeSignature(typeSignature));
}
catch (UncheckedExecutionException e) {
throwIfUnchecked(e.getCause());
throw new RuntimeException(e.getCause());
}
}

@Override
public Type getParameterizedType(String baseTypeName, List<TypeSignatureParameter> typeParameters)
{
throw new UnsupportedOperationException();
}

@Override
public boolean canCoerce(Type actualType, Type expectedType)
{
throw new UnsupportedOperationException();
}

public List<Type> getTypes()
{
return ImmutableList.copyOf(types.values());
Expand All @@ -1296,14 +1309,22 @@ public void addParametricType(ParametricType parametricType)
parametricTypes.putIfAbsent(name, parametricType);
}

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

private Type instantiateParametricType(ExactTypeSignature exactTypeSignature)
{
return parametricTypes.values();
return instantiateParametricType(exactTypeSignature.getTypeSignature(), functionAndTypeManager, parametricTypes);
}

private Type instantiateParametricType(ExactTypeSignature exactSignature)
public Type instantiateParametricType(
TypeSignature signature,
FunctionAndTypeManager functionAndTypeManager,
Map<String, ParametricType> parametricTypes)
{
TypeSignature signature = exactSignature.getTypeSignature();
List<TypeParameter> parameters = new ArrayList<>();

for (TypeSignatureParameter parameter : signature.getParameters()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
import com.facebook.presto.spi.function.SqlFunctionId;
import com.facebook.presto.spi.function.SqlFunctionSupplier;
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.sql.analyzer.FeaturesConfig;
import com.facebook.presto.sql.analyzer.FunctionAndTypeResolver;
import com.facebook.presto.sql.analyzer.FunctionsConfig;
Expand Down Expand Up @@ -128,8 +130,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, TypeManager> typeManagers = new ConcurrentHashMap<>();
private final FunctionSignatureMatcher functionSignatureMatcher;
private final TypeCoercer typeCoercer;
private final LoadingCache<FunctionResolutionCacheKey, FunctionHandle> functionCache;
Expand All @@ -151,6 +155,7 @@ public FunctionAndTypeManager(
this.builtInTypeAndFunctionNamespaceManager = new BuiltInTypeAndFunctionNamespaceManager(blockEncodingSerde, functionsConfig, types, this);
this.functionNamespaceManagers.put(JAVA_BUILTIN_NAMESPACE.getCatalogName(), builtInTypeAndFunctionNamespaceManager);
this.functionInvokerProvider = new FunctionInvokerProvider(this);
this.typeManagers.put(JAVA_BUILTIN_NAMESPACE.getCatalogName(), builtInTypeAndFunctionNamespaceManager);
this.handleResolver = requireNonNull(handleResolver, "handleResolver is null");
// TODO: Provide a more encapsulated way for TransactionManager to register FunctionNamespaceManager
transactionManager.registerFunctionNamespaceManager(JAVA_BUILTIN_NAMESPACE.getCatalogName(), builtInTypeAndFunctionNamespaceManager);
Expand Down Expand Up @@ -234,6 +239,24 @@ public SqlFunctionSupplier getSpecializedFunctionKey(Signature signature)
return FunctionAndTypeManager.this.getSpecializedFunctionKey(signature);
}

@Override
public Type instantiateParametricType(TypeSignature typeSignature)
{
return FunctionAndTypeManager.this.instantiateParametricType(typeSignature);
}

@Override
public List<Type> getTypes()
{
return FunctionAndTypeManager.this.getTypes();
}

@Override
public Map<String, ParametricType> getParametricTypes()
{
return FunctionAndTypeManager.this.getParametricTypes();
}

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

@Override
public Type instantiateParametricType(TypeSignature typeSignature)
{
return builtInTypeAndFunctionNamespaceManager.instantiateParametricType(
typeSignature,
this,
getServingTypeManager().getParametricTypes());
}

@Override
public Type getType(TypeSignature signature)
{
Expand All @@ -327,12 +359,12 @@ public Type getType(TypeSignature signature)
if (signature.isDistinctType()) {
return getDistinctType(signature.getParameters().get(0).getDistinctTypeInfo());
}
Optional<Type> type = builtInTypeAndFunctionNamespaceManager.getType(signature.getStandardTypeSignature());
if (type.isPresent()) {
Type type = getServingTypeManager().getType(signature.getStandardTypeSignature());
if (type != null) {
if (signature.getTypeSignatureBase().hasTypeName()) {
return new TypeWithName(signature.getTypeSignatureBase().getTypeName(), type.get());
return new TypeWithName(signature.getTypeSignatureBase().getTypeName(), type);
}
return type.get();
return type;
}
}

Expand Down Expand Up @@ -364,6 +396,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);
TypeManager typeManager = factory.create(new TypeManagerContext(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 @@ -509,9 +567,9 @@ public List<Type> getTypes()
return builtInTypeAndFunctionNamespaceManager.getTypes();
}

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

public Optional<Type> getCommonSuperType(Type firstType, Type secondType)
Expand Down Expand Up @@ -835,6 +893,19 @@ public CatalogSchemaName configureDefaultNamespace(String defaultNamespacePrefix
return new CatalogSchemaName(catalogSchemaNameString[0], catalogSchemaNameString[1]);
}

private TypeManager getServingTypeManager()
{
// If the NativeSidecarPlugin is loaded, typeManagerFactories is non-empty and holds an instance of NativeTypeManagerFactory.
// Check if the nativeTypeManager exists in typeManagers else use BuiltInTypeAndFunctionNamespaceManager.
for (Map.Entry<String, TypeManagerFactory> entry : typeManagerFactories.entrySet()) {
if (!typeManagers.containsKey(entry.getKey())) {
throw new PrestoException(GENERIC_USER_ERROR, format("Type manager not loaded for factory: %s", entry.getKey()));
}
return typeManagers.get(entry.getKey());
}
return builtInTypeAndFunctionNamespaceManager;
}

private static class FunctionResolutionCacheKey
{
private final QualifiedObjectName functionName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,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.expressions.ExpressionOptimizerManager;
Expand Down Expand Up @@ -402,6 +403,11 @@ public void installCoordinatorPlugin(CoordinatorPlugin plugin)
log.info("Registering expression optimizer factory %s", expressionOptimizerFactory.getName());
expressionOptimizerManager.addExpressionOptimizerFactory(expressionOptimizerFactory);
}

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
Loading

0 comments on commit d5ae82c

Please sign in to comment.