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 operator cache and coercion cache to reduce planning time #43

Merged
merged 1 commit into from
Mar 3, 2022
Merged
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
151 changes: 134 additions & 17 deletions presto-main/src/main/java/io/prestosql/metadata/MetadataManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.common.util.concurrent.UncheckedExecutionException;
import io.airlift.slice.Slice;
import io.prestosql.Session;
import io.prestosql.connector.CatalogName;
Expand Down Expand Up @@ -195,6 +199,9 @@ public final class MetadataManager

private final ResolvedFunctionDecoder functionDecoder;

private final LoadingCache<OperatorCacheKey, ResolvedFunction> operatorCache;
private final LoadingCache<CoercionCacheKey, ResolvedFunction> coercionCache;

@Inject
public MetadataManager(
FeaturesConfig featuresConfig,
Expand Down Expand Up @@ -240,6 +247,23 @@ public MetadataManager(
verifyTypes();

functionDecoder = new ResolvedFunctionDecoder(this::getType);

operatorCache = CacheBuilder.newBuilder()
.maximumSize(1000)
.build(CacheLoader.from(key -> {
String name = mangleOperatorName(key.getOperatorType());
return resolveFunction(QualifiedName.of(name), fromTypes(key.getArgumentTypes()));
}));

coercionCache = CacheBuilder.newBuilder()
.maximumSize(1000)
.build(CacheLoader.from(key -> {
String name = mangleOperatorName(key.getOperatorType());
Type fromType = key.getFromType();
Type toType = key.getToType();
Signature signature = new Signature(name, toType.getTypeSignature(), ImmutableList.of(fromType.getTypeSignature()));
return resolve(functionResolver.resolveCoercion(functions.get(QualifiedName.of(name)), signature));
}));
}

public static MetadataManager createTestMetadataManager()
Expand Down Expand Up @@ -1779,17 +1803,17 @@ public ResolvedFunction resolveOperator(OperatorType operatorType, List<? extend
throws OperatorNotFoundException
{
try {
return resolveFunction(QualifiedName.of(mangleOperatorName(operatorType)), fromTypes(argumentTypes));
return operatorCache.getUnchecked(new OperatorCacheKey(operatorType, argumentTypes));
}
catch (PrestoException e) {
if (e.getErrorCode().getCode() == FUNCTION_NOT_FOUND.toErrorCode().getCode()) {
OperatorNotFoundException operatorNotFound = new OperatorNotFoundException(operatorType, argumentTypes);
operatorNotFound.addSuppressed(e);
throw operatorNotFound;
}
else {
throw e;
catch (UncheckedExecutionException e) {
if (e.getCause() instanceof PrestoException) {
PrestoException cause = (PrestoException) e.getCause();
if (cause.getErrorCode().getCode() == FUNCTION_NOT_FOUND.toErrorCode().getCode()) {
throw new OperatorNotFoundException(operatorType, argumentTypes);
}
throw cause;
}
throw e;
}
}

Expand All @@ -1798,14 +1822,15 @@ public ResolvedFunction getCoercion(OperatorType operatorType, Type fromType, Ty
{
checkArgument(operatorType == OperatorType.CAST || operatorType == OperatorType.SATURATED_FLOOR_CAST);
try {
String name = mangleOperatorName(operatorType);
return resolve(functionResolver.resolveCoercion(functions.get(QualifiedName.of(name)), new Signature(name, toType.getTypeSignature(), ImmutableList.of(fromType.getTypeSignature()))));
}
catch (PrestoException e) {
if (e.getErrorCode().getCode() == FUNCTION_IMPLEMENTATION_MISSING.toErrorCode().getCode()) {
OperatorNotFoundException operatorNotFound = new OperatorNotFoundException(operatorType, ImmutableList.of(fromType), toType.getTypeSignature());
operatorNotFound.addSuppressed(e);
throw operatorNotFound;
return coercionCache.getUnchecked(new CoercionCacheKey(operatorType, fromType, toType));
}
catch (UncheckedExecutionException e) {
if (e.getCause() instanceof PrestoException) {
PrestoException cause = (PrestoException) e.getCause();
if (cause.getErrorCode().getCode() == FUNCTION_IMPLEMENTATION_MISSING.toErrorCode().getCode()) {
throw new OperatorNotFoundException(operatorType, ImmutableList.of(fromType), toType.getTypeSignature());
}
throw cause;
}
throw e;
}
Expand Down Expand Up @@ -2214,4 +2239,96 @@ private synchronized void finish()
}
}
}

private static class OperatorCacheKey
{
private final OperatorType operatorType;
private final List<? extends Type> argumentTypes;

private OperatorCacheKey(OperatorType operatorType, List<? extends Type> argumentTypes)
{
this.operatorType = requireNonNull(operatorType, "operatorType is null");
this.argumentTypes = ImmutableList.copyOf(requireNonNull(argumentTypes, "argumentTypes is null"));
}

public OperatorType getOperatorType()
{
return operatorType;
}

public List<? extends Type> getArgumentTypes()
{
return argumentTypes;
}

@Override
public int hashCode()
{
return Objects.hash(operatorType, argumentTypes);
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (!(obj instanceof OperatorCacheKey)) {
return false;
}
OperatorCacheKey other = (OperatorCacheKey) obj;
return Objects.equals(this.operatorType, other.operatorType) &&
Objects.equals(this.argumentTypes, other.argumentTypes);
}
}

private static class CoercionCacheKey
{
private final OperatorType operatorType;
private final Type fromType;
private final Type toType;

private CoercionCacheKey(OperatorType operatorType, Type fromType, Type toType)
{
this.operatorType = requireNonNull(operatorType, "operatorType is null");
this.fromType = requireNonNull(fromType, "fromType is null");
this.toType = requireNonNull(toType, "toType is null");
}

public OperatorType getOperatorType()
{
return operatorType;
}

public Type getFromType()
{
return fromType;
}

public Type getToType()
{
return toType;
}

@Override
public int hashCode()
{
return Objects.hash(operatorType, fromType, toType);
}

@Override
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (!(obj instanceof CoercionCacheKey)) {
return false;
}
CoercionCacheKey other = (CoercionCacheKey) obj;
return Objects.equals(this.operatorType, other.operatorType) &&
Objects.equals(this.fromType, other.fromType) &&
Objects.equals(this.toType, other.toType);
}
}
}