public BoundVariables build() { return new BoundVariables(typeVariables, longVariables); } }
@VisibleForTesting public static InternalAggregationFunction generateInternalAggregationFunction(Class<?> clazz, TypeSignature outputType, List<TypeSignature> inputTypes, TypeManager typeManager) { return generateInternalAggregationFunction(clazz, outputType, inputTypes, typeManager, BoundVariables.builder().build(), inputTypes.size()); }
public Type getType(String name) { return boundVariables.getTypeVariable(name); }
public static TypeSignature applyBoundVariables(TypeSignature typeSignature, BoundVariables boundVariables) { String baseType = typeSignature.getBase(); if (boundVariables.containsTypeVariable(baseType)) { checkState(typeSignature.getParameters().isEmpty(), "Type parameters cannot have parameters"); return boundVariables.getTypeVariable(baseType).getTypeSignature(); } List<TypeSignatureParameter> parameters = typeSignature.getParameters().stream() .map(typeSignatureParameter -> applyBoundVariables(typeSignatureParameter, boundVariables)) .collect(toList()); return new TypeSignature(baseType, parameters); }
@Override public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { checkArgument(boundVariables.getTypeVariables().size() == 1, "Expected only one type"); Type type = boundVariables.getTypeVariable("T"); MethodHandle identity = MethodHandles.identity(type.getJavaType()); return new ScalarFunctionImplementation( false, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), identity, isDeterministic()); } }
private static TypeSignatureParameter applyBoundVariables(TypeSignatureParameter parameter, BoundVariables boundVariables) { ParameterKind parameterKind = parameter.getKind(); switch (parameterKind) { case TYPE: { TypeSignature typeSignature = parameter.getTypeSignature(); return TypeSignatureParameter.of(applyBoundVariables(typeSignature, boundVariables)); } case NAMED_TYPE: { NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature(); TypeSignature typeSignature = namedTypeSignature.getTypeSignature(); return TypeSignatureParameter.of(new NamedTypeSignature( namedTypeSignature.getFieldName(), applyBoundVariables(typeSignature, boundVariables))); } case VARIABLE: { String variableName = parameter.getVariable(); checkState(boundVariables.containsLongVariable(variableName), "Variable is not bound: %s", variableName); Long variableValue = boundVariables.getLongVariable(variableName); return TypeSignatureParameter.of(variableValue); } case LONG: { return parameter; } default: throw new IllegalStateException("Unknown parameter kind: " + parameter.getKind()); } }
@Override public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { return specializeArrayJoin(boundVariables.getTypeVariables(), functionRegistry, ImmutableList.of(false, false, false), METHOD_HANDLE); } }
@Override public Long resolve(BoundVariables boundVariables, TypeManager typeManager, FunctionRegistry functionRegistry) { return boundVariables.getLongVariable(literalName); } }
public Long getLongVariable(String variableName) { return getValue(longVariables, variableName); }
public boolean containsTypeVariable(String variableName) { return containsValue(typeVariables, variableName); }
@Override public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) checkArgument(boundVariables.getTypeVariables().size() == 1, "Expected one type, got %s", boundVariables.getTypeVariables()); Type elementType = boundVariables.getTypeVariable("E");
private boolean allTypeVariablesBound(BoundVariables boundVariables) { return boundVariables.getTypeVariables().keySet().equals(typeVariableConstraints.keySet()); }
public Long getLiteral(String name) { return boundVariables.getLongVariable(name); }
public Type getTypeVariable(String variableName) { return getValue(typeVariables, variableName); }
public boolean containsLongVariable(String variableName) { return containsValue(longVariables, variableName); }
@Override public InternalAggregationFunction specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { Type keyType = boundVariables.getTypeVariable("K"); Type valueType = boundVariables.getTypeVariable("V"); return generateAggregation(valueType, keyType); }
@Test public void testBindUnparametrizedVarchar() { Signature function = functionSignature() .returnType(parseTypeSignature("boolean")) .argumentTypes(parseTypeSignature("varchar(x)", ImmutableSet.of("x"))) .build(); assertThat(function) .boundTo("varchar") .produces(new BoundVariables( ImmutableMap.of(), ImmutableMap.of("x", (long) Integer.MAX_VALUE))); }
private Optional<BoundVariables> iterativeSolve(List<TypeConstraintSolver> constraints) BoundVariables.Builder boundVariablesBuilder = BoundVariables.builder(); for (int i = 0; true; i++) { if (i == SOLVE_ITERATION_LIMIT) {
@Override public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { return specializeArrayJoin(boundVariables.getTypeVariables(), functionRegistry, ImmutableList.of(false, false), METHOD_HANDLE); }
@Override public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { return new ScalarFunctionImplementation( false, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), insertArguments(METHOD_HANDLE, 0, dfaStatesLimit, dfaRetries, padSpaces, boundVariables.getLongVariable("x")), true); }