@Test public void testVarArgs() throws Exception { TypeManager typeManager = new TypeRegistry(); Signature signature = new Signature("foo", SCALAR, ImmutableList.of(typeParameter("T")), "boolean", ImmutableList.of("T"), true); assertNotNull(signature.bindTypeParameters(ImmutableList.of(BIGINT), true, typeManager)); assertNotNull(signature.bindTypeParameters(ImmutableList.of(VARCHAR), true, typeManager)); assertNotNull(signature.bindTypeParameters(ImmutableList.of(BIGINT, BIGINT), true, typeManager)); assertNull(signature.bindTypeParameters(ImmutableList.of(VARCHAR, BIGINT), true, typeManager)); }
@Test public void testCoercion() throws Exception { TypeManager typeManager = new TypeRegistry(); Signature signature = new Signature("foo", SCALAR, ImmutableList.of(typeParameter("T")), "boolean", ImmutableList.of("T", "double"), true); assertNotNull(signature.bindTypeParameters(ImmutableList.of(DOUBLE, DOUBLE), true, typeManager)); assertNotNull(signature.bindTypeParameters(ImmutableList.of(BIGINT, BIGINT), true, typeManager)); assertNotNull(signature.bindTypeParameters(ImmutableList.of(VARCHAR, BIGINT), true, typeManager)); assertNull(signature.bindTypeParameters(ImmutableList.of(BIGINT, VARCHAR), true, typeManager)); }
@Test public void testNonParametric() throws Exception { TypeManager typeManager = new TypeRegistry(); Signature signature = new Signature("foo", SCALAR, ImmutableList.<TypeParameterRequirement>of(), "boolean", ImmutableList.of("bigint"), false); assertNotNull(signature.bindTypeParameters(ImmutableList.of(BIGINT), true, typeManager)); assertNull(signature.bindTypeParameters(ImmutableList.of(VARCHAR), true, typeManager)); assertNull(signature.bindTypeParameters(ImmutableList.of(VARCHAR, BIGINT), true, typeManager)); assertNull(signature.bindTypeParameters(ImmutableList.of(typeManager.getType(parseTypeSignature("array(bigint)"))), true, typeManager)); }
@Test public void testBasic() throws Exception { TypeManager typeManager = new TypeRegistry(); Signature signature = new Signature("foo", SCALAR, ImmutableList.of(typeParameter("T")), "T", ImmutableList.of("T"), false); assertNotNull(signature.bindTypeParameters(ImmutableList.<Type>of(BIGINT), true, typeManager)); assertNotNull(signature.bindTypeParameters(ImmutableList.of(VARCHAR), true, typeManager)); assertNull(signature.bindTypeParameters(ImmutableList.of(VARCHAR, BIGINT), true, typeManager)); assertNotNull(signature.bindTypeParameters(ImmutableList.of(typeManager.getType(parseTypeSignature("array(bigint)"))), true, typeManager)); }
@Test public void testUnknownCoercion() throws Exception { TypeManager typeManager = new TypeRegistry(); Signature signature = new Signature("foo", SCALAR, ImmutableList.of(typeParameter("T")), "boolean", ImmutableList.of("T", "T"), false); assertNotNull(signature.bindTypeParameters(ImmutableList.of(UNKNOWN, UNKNOWN), true, typeManager)); assertNotNull(signature.bindTypeParameters(ImmutableList.of(UNKNOWN, BIGINT), true, typeManager)); assertNull(signature.bindTypeParameters(ImmutableList.of(BIGINT, VARCHAR), true, typeManager)); signature = new Signature("foo", SCALAR, ImmutableList.of(comparableTypeParameter("T")), "boolean", ImmutableList.of("T", "T"), false); Map<String, Type> boundParameters = signature.bindTypeParameters(ImmutableList.of(UNKNOWN, BIGINT), true, typeManager); assertNotNull(boundParameters); assertEquals(boundParameters.get("T"), BIGINT); assertNull(signature.bindTypeParameters(ImmutableList.of(BIGINT, VARCHAR), true, typeManager)); } }
@Test public void testVariadic() throws Exception { TypeManager typeManager = new TypeRegistry(); Type mapType = typeManager.getType(parseTypeSignature("map(bigint,bigint)")); Type arrayType = typeManager.getType(parseTypeSignature("array(bigint)")); Signature signature = new Signature("foo", SCALAR, ImmutableList.of(withVariadicBound("T", "map")), "bigint", ImmutableList.of("T"), true); assertNotNull(signature.bindTypeParameters(ImmutableList.of(mapType), true, typeManager)); assertNull(signature.bindTypeParameters(ImmutableList.of(arrayType), true, typeManager)); }
private void assertFunctionBind( List<TypeParameterRequirement> typeParameterRequirements, TypeSignature returnType, List<TypeSignature> argumentTypes, Optional<Type> actualReturnType, List<? extends Type> actualArguments, boolean allowCoercion, Map<String, Type> expectedBoundParameters) { Signature function = new Signature( "function", SCALAR, typeParameterRequirements, returnType, argumentTypes, false); Map<String, Type> actualBoundParameters; if (actualReturnType.isPresent()) { actualBoundParameters = function.bindTypeParameters(actualReturnType.get(), actualArguments, allowCoercion, new TypeRegistry()); } else { actualBoundParameters = function.bindTypeParameters(actualArguments, allowCoercion, new TypeRegistry()); } assertEquals( actualBoundParameters, expectedBoundParameters); }
Map<String, Type> boundParameters = function.getSignature().bindTypeParameters(resolvedTypes, false, typeManager); if (boundParameters == null) { continue; Map<String, Type> boundParameters = function.getSignature().bindTypeParameters(resolvedTypes, true, typeManager); if (boundParameters == null) { continue; SqlFunction fieldReference = getRowFieldReference(name.getSuffix(), parameterTypes.get(0)); if (fieldReference != null) { Map<String, Type> boundParameters = fieldReference.getSignature().bindTypeParameters(resolvedTypes, true, typeManager); if (boundParameters != null) { return bindSignature(fieldReference.getSignature(), boundParameters, resolvedTypes.size());
public InternalAggregationFunction getAggregateFunctionImplementation(Signature signature) { checkArgument(signature.getKind() == AGGREGATE || signature.getKind() == APPROXIMATE_AGGREGATE, "%s is not an aggregate function", signature); checkArgument(signature.getTypeParameterRequirements().isEmpty(), "%s has unbound type parameters", signature); Iterable<SqlFunction> candidates = functions.get(QualifiedName.of(signature.getName())); // search for exact match for (SqlFunction operator : candidates) { Type returnType = typeManager.getType(signature.getReturnType()); List<Type> argumentTypes = resolveTypes(signature.getArgumentTypes(), typeManager); Map<String, Type> boundTypeParameters = operator.getSignature().bindTypeParameters(returnType, argumentTypes, false, typeManager); if (boundTypeParameters != null) { try { return specializedAggregationCache.getUnchecked(new SpecializedFunctionKey(operator, boundTypeParameters, signature.getArgumentTypes().size())); } catch (UncheckedExecutionException e) { throw Throwables.propagate(e.getCause()); } } } throw new PrestoException(FUNCTION_IMPLEMENTATION_MISSING, format("%s not found", signature)); }
@Test public void testMap() throws Exception { TypeManager typeManager = new TypeRegistry(); Signature signature = new Signature("get", SCALAR, ImmutableList.of(typeParameter("K"), typeParameter("V")), "V", ImmutableList.of("map(K,V)", "K"), false); assertNotNull(signature.bindTypeParameters(ImmutableList.of(typeManager.getType(parseTypeSignature("map(bigint,varchar)")), BIGINT), true, typeManager)); assertNull(signature.bindTypeParameters(ImmutableList.of(typeManager.getType(parseTypeSignature("map(bigint,varchar)")), VARCHAR), true, typeManager)); }
@Test public void testArray() throws Exception { TypeManager typeManager = new TypeRegistry(); Signature signature = new Signature("get", SCALAR, ImmutableList.of(typeParameter("T")), "T", ImmutableList.of("array(T)"), false); assertNotNull(signature.bindTypeParameters(ImmutableList.of(typeManager.getType(parseTypeSignature("array(bigint)"))), true, typeManager)); assertNull(signature.bindTypeParameters(ImmutableList.of(BIGINT), true, typeManager)); signature = new Signature("contains", SCALAR, ImmutableList.of(comparableTypeParameter("T")), "T", ImmutableList.of("array(T)", "T"), false); assertNotNull(signature.bindTypeParameters(ImmutableList.of(typeManager.getType(parseTypeSignature("array(bigint)")), BIGINT), true, typeManager)); assertNull(signature.bindTypeParameters(ImmutableList.of(typeManager.getType(parseTypeSignature("array(bigint)")), VARCHAR), true, typeManager)); assertNull(signature.bindTypeParameters(ImmutableList.of(typeManager.getType(parseTypeSignature("array(HyperLogLog)")), HYPER_LOG_LOG), true, typeManager)); signature = new Signature("foo", SCALAR, ImmutableList.of(typeParameter("T")), "T", ImmutableList.of("array(T)", "array(T)"), false); assertNotNull(signature.bindTypeParameters(ImmutableList.of(typeManager.getType(parseTypeSignature("array(bigint)")), typeManager.getType(parseTypeSignature("array(bigint)"))), true, typeManager)); assertNull(signature.bindTypeParameters(ImmutableList.of(typeManager.getType(parseTypeSignature("array(bigint)")), typeManager.getType(parseTypeSignature("array(varchar)"))), true, typeManager)); }
public WindowFunctionSupplier getWindowFunctionImplementation(Signature signature) { checkArgument(signature.getKind() == WINDOW || signature.getKind() == AGGREGATE, "%s is not a window function", signature); checkArgument(signature.getTypeParameterRequirements().isEmpty(), "%s has unbound type parameters", signature); Iterable<SqlFunction> candidates = functions.get(QualifiedName.of(signature.getName())); // search for exact match for (SqlFunction operator : candidates) { Type returnType = typeManager.getType(signature.getReturnType()); List<Type> argumentTypes = resolveTypes(signature.getArgumentTypes(), typeManager); Map<String, Type> boundTypeParameters = operator.getSignature().bindTypeParameters(returnType, argumentTypes, false, typeManager); if (boundTypeParameters != null) { try { return specializedWindowCache.getUnchecked(new SpecializedFunctionKey(operator, boundTypeParameters, signature.getArgumentTypes().size())); } catch (UncheckedExecutionException e) { throw Throwables.propagate(e.getCause()); } } } throw new PrestoException(FUNCTION_IMPLEMENTATION_MISSING, format("%s not found", signature)); }
List<Type> argumentTypes = resolveTypes(signature.getArgumentTypes(), typeManager); for (SqlFunction operator : candidates) { Map<String, Type> boundTypeParameters = operator.getSignature().bindTypeParameters(returnType, argumentTypes, false, typeManager); if (boundTypeParameters != null) { try { SqlFunction fieldReference = getRowFieldReference(signature.getName(), signature.getArgumentTypes().get(0)); if (fieldReference != null) { Map<String, Type> boundTypeParameters = fieldReference.getSignature().bindTypeParameters(returnType, argumentTypes, false, typeManager); return specializedScalarCache.getUnchecked(new SpecializedFunctionKey(fieldReference, boundTypeParameters, signature.getArgumentTypes().size()));