Skip to content

Commit

Permalink
Encapsulate BoundVariables in SignatureBinder
Browse files Browse the repository at this point in the history
  • Loading branch information
dain committed Jul 30, 2020
1 parent a8cc411 commit e417e14
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 333 deletions.
116 changes: 26 additions & 90 deletions presto-main/src/main/java/io/prestosql/metadata/BoundVariables.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
*/
package io.prestosql.metadata;

import com.google.common.collect.ImmutableMap;
import io.prestosql.spi.type.Type;

import java.util.Map;
Expand All @@ -22,57 +21,60 @@

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableSortedMap.toImmutableSortedMap;
import static java.lang.String.CASE_INSENSITIVE_ORDER;
import static java.util.Objects.requireNonNull;

public class BoundVariables
class BoundVariables
implements TypeVariables
{
private final Map<String, Type> typeVariables;
private final Map<String, Long> longVariables;
private final Map<String, Type> typeVariables = new TreeMap<>(CASE_INSENSITIVE_ORDER);
private final Map<String, Long> longVariables = new TreeMap<>(CASE_INSENSITIVE_ORDER);

public BoundVariables(Map<String, Type> typeVariables, Map<String, Long> longVariables)
{
requireNonNull(typeVariables, "typeVariableBindings is null");
requireNonNull(longVariables, "longVariableBindings is null");

this.typeVariables = typeVariables.entrySet().stream()
.collect(toImmutableSortedMap(CASE_INSENSITIVE_ORDER, Map.Entry::getKey, Map.Entry::getValue));

this.longVariables = longVariables.entrySet().stream()
.collect(toImmutableSortedMap(CASE_INSENSITIVE_ORDER, Map.Entry::getKey, Map.Entry::getValue));
}

public Map<String, Type> getTypeVariables()
@Override
public Type getTypeVariable(String variableName)
{
return typeVariables;
return getValue(typeVariables, variableName);
}

public Type getTypeVariable(String variableName)
public BoundVariables setTypeVariable(String variableName, Type variableValue)
{
return getValue(typeVariables, variableName);
setValue(typeVariables, variableName, variableValue);
return this;
}

@Override
public boolean containsTypeVariable(String variableName)
{
return containsValue(typeVariables, variableName);
}

public Map<String, Long> getLongVariables()
public Map<String, Type> getTypeVariables()
{
return longVariables;
return typeVariables;
}

@Override
public Long getLongVariable(String variableName)
{
return getValue(longVariables, variableName);
}

public BoundVariables setLongVariable(String variableName, Long variableValue)
{
setValue(longVariables, variableName, variableValue);
return this;
}

@Override
public boolean containsLongVariable(String variableName)
{
return containsValue(longVariables, variableName);
}

public Map<String, Long> getLongVariables()
{
return longVariables;
}

private static <T> T getValue(Map<String, T> map, String variableName)
{
checkState(variableName != null, "variableName is null");
Expand Down Expand Up @@ -122,70 +124,4 @@ public String toString()
.add("longVariables", longVariables)
.toString();
}

public Map<String, Object> getBindings()
{
return ImmutableMap.<String, Object>builder()
.putAll(typeVariables)
.putAll(longVariables)
.build();
}

public static Builder builder()
{
return new Builder();
}

public static class Builder
{
private final Map<String, Type> typeVariables = new TreeMap<>(CASE_INSENSITIVE_ORDER);
private final Map<String, Long> longVariables = new TreeMap<>(CASE_INSENSITIVE_ORDER);

public Type getTypeVariable(String variableName)
{
return getValue(typeVariables, variableName);
}

public Builder setTypeVariable(String variableName, Type variableValue)
{
setValue(typeVariables, variableName, variableValue);
return this;
}

public boolean containsTypeVariable(String variableName)
{
return containsValue(typeVariables, variableName);
}

public Map<String, Type> getTypeVariables()
{
return typeVariables;
}

public Long getLongVariable(String variableName)
{
return getValue(longVariables, variableName);
}

public Builder setLongVariable(String variableName, Long variableValue)
{
setValue(longVariables, variableName, variableValue);
return this;
}

public boolean containsLongVariable(String variableName)
{
return containsValue(longVariables, variableName);
}

public Map<String, Long> getLongVariables()
{
return longVariables;
}

public BoundVariables build()
{
return new BoundVariables(typeVariables, longVariables);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1546,17 +1546,15 @@ private ResolvedFunction resolve(FunctionBinding functionBinding)
@VisibleForTesting
public ResolvedFunction resolve(FunctionBinding functionBinding, FunctionDependencyDeclaration declaration)
{
BoundVariables boundVariables = new BoundVariables(functionBinding.getTypeVariables(), functionBinding.getLongVariables());

Map<TypeSignature, Type> dependentTypes = declaration.getTypeDependencies().stream()
.map(typeSignature -> applyBoundVariables(typeSignature, boundVariables))
.collect(toImmutableMap(Function.identity(), this::getType));
.map(typeSignature -> applyBoundVariables(typeSignature, functionBinding))
.collect(toImmutableMap(Function.identity(), this::getType, (left, right) -> left));

ImmutableSet.Builder<ResolvedFunction> functions = ImmutableSet.builder();
declaration.getFunctionDependencies().stream()
.map(functionDependency -> {
try {
List<TypeSignature> argumentTypes = applyBoundVariables(functionDependency.getArgumentTypes(), boundVariables);
List<TypeSignature> argumentTypes = applyBoundVariables(functionDependency.getArgumentTypes(), functionBinding);
return resolveFunction(functionDependency.getName(), fromTypeSignatures(argumentTypes));
}
catch (PrestoException e) {
Expand All @@ -1572,7 +1570,7 @@ public ResolvedFunction resolve(FunctionBinding functionBinding, FunctionDepende
declaration.getOperatorDependencies().stream()
.map(operatorDependency -> {
try {
List<TypeSignature> argumentTypes = applyBoundVariables(operatorDependency.getArgumentTypes(), boundVariables);
List<TypeSignature> argumentTypes = applyBoundVariables(operatorDependency.getArgumentTypes(), functionBinding);
return resolveFunction(QualifiedName.of(mangleOperatorName(operatorDependency.getOperatorType())), fromTypeSignatures(argumentTypes));
}
catch (PrestoException e) {
Expand All @@ -1588,8 +1586,8 @@ public ResolvedFunction resolve(FunctionBinding functionBinding, FunctionDepende
declaration.getCastDependencies().stream()
.map(castDependency -> {
try {
Type fromType = getType(applyBoundVariables(castDependency.getFromType(), boundVariables));
Type toType = getType(applyBoundVariables(castDependency.getToType(), boundVariables));
Type fromType = getType(applyBoundVariables(castDependency.getFromType(), functionBinding));
Type toType = getType(applyBoundVariables(castDependency.getToType(), functionBinding));
return getCoercion(fromType, toType);
}
catch (PrestoException e) {
Expand Down
Loading

0 comments on commit e417e14

Please sign in to comment.