use of com.facebook.presto.spi.function.Signature in project presto by prestodb.
the class TestSignatureBinder method testBindLiteralForRepeatedVarchar.
@Test
public void testBindLiteralForRepeatedVarchar() {
Set<String> literalParameters = ImmutableSet.of("x");
TypeSignature leftType = parseTypeSignature("varchar(x)", literalParameters);
TypeSignature rightType = parseTypeSignature("varchar(x)", literalParameters);
TypeSignature returnType = parseTypeSignature("varchar(x)", literalParameters);
Signature function = functionSignature().returnType(returnType).argumentTypes(leftType, rightType).build();
assertThat(function).withCoercion().boundTo(ImmutableList.of("varchar(3)", "varchar(5)"), "varchar(5)").produces(new BoundVariables(ImmutableMap.of(), ImmutableMap.of("x", 5L)));
assertThat(function).withCoercion().boundTo(ImmutableList.of("varchar(3)", "varchar(5)"), "varchar(6)").produces(new BoundVariables(ImmutableMap.of(), ImmutableMap.of("x", 6L)));
}
use of com.facebook.presto.spi.function.Signature in project presto by prestodb.
the class TestSignatureBinder method testBindUnparametrizedVarchar.
@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)));
}
use of com.facebook.presto.spi.function.Signature in project presto by prestodb.
the class TestSignatureBinder method testBindVarcharTemplateStyle.
@Test
public void testBindVarcharTemplateStyle() {
Signature function = functionSignature().returnType(parseTypeSignature("T2")).argumentTypes(parseTypeSignature("T1")).typeVariableConstraints(ImmutableList.of(new TypeVariableConstraint("T1", true, false, "varchar", false), new TypeVariableConstraint("T2", true, false, "varchar", false))).build();
assertThat(function).boundTo(ImmutableList.of("varchar(42)"), "varchar(1)").produces(new BoundVariables(ImmutableMap.of("T1", type("varchar(42)"), "T2", type("varchar(1)")), ImmutableMap.of()));
}
use of com.facebook.presto.spi.function.Signature in project presto by prestodb.
the class TestSignatureBinder method testFunction.
@Test
public void testFunction() {
Signature simple = functionSignature().returnType(parseTypeSignature("boolean")).argumentTypes(parseTypeSignature("function(integer,integer)")).build();
assertThat(simple).boundTo("integer").fails();
assertThat(simple).boundTo("function(integer,integer)").succeeds();
// TODO: Support coercion of return type of lambda
assertThat(simple).boundTo("function(integer,smallint)").withCoercion().fails();
assertThat(simple).boundTo("function(integer,bigint)").withCoercion().fails();
Signature applyTwice = functionSignature().returnType(parseTypeSignature("V")).argumentTypes(parseTypeSignature("T"), parseTypeSignature("function(T,U)"), parseTypeSignature("function(U,V)")).typeVariableConstraints(typeVariable("T"), typeVariable("U"), typeVariable("V")).build();
assertThat(applyTwice).boundTo("integer", "integer", "integer").fails();
assertThat(applyTwice).boundTo("integer", "function(integer,varchar)", "function(varchar,double)").produces(BoundVariables.builder().setTypeVariable("T", INTEGER).setTypeVariable("U", VARCHAR).setTypeVariable("V", DOUBLE).build());
assertThat(applyTwice).boundTo("integer", new TypeSignatureProvider(functionArgumentTypes -> TypeSignature.parseTypeSignature("function(integer,varchar)")), new TypeSignatureProvider(functionArgumentTypes -> TypeSignature.parseTypeSignature("function(varchar,double)"))).produces(BoundVariables.builder().setTypeVariable("T", INTEGER).setTypeVariable("U", VARCHAR).setTypeVariable("V", DOUBLE).build());
assertThat(applyTwice).boundTo(// pass function argument to non-function position of a function
new TypeSignatureProvider(functionArgumentTypes -> TypeSignature.parseTypeSignature("function(integer,varchar)")), new TypeSignatureProvider(functionArgumentTypes -> TypeSignature.parseTypeSignature("function(integer,varchar)")), new TypeSignatureProvider(functionArgumentTypes -> TypeSignature.parseTypeSignature("function(varchar,double)"))).fails();
assertThat(applyTwice).boundTo(new TypeSignatureProvider(functionArgumentTypes -> TypeSignature.parseTypeSignature("function(integer,varchar)")), // pass non-function argument to function position of a function
"integer", new TypeSignatureProvider(functionArgumentTypes -> TypeSignature.parseTypeSignature("function(varchar,double)"))).fails();
Signature flatMap = functionSignature().returnType(parseTypeSignature("array(T)")).argumentTypes(parseTypeSignature("array(T)"), parseTypeSignature("function(T, array(T))")).typeVariableConstraints(typeVariable("T")).build();
assertThat(flatMap).boundTo("array(integer)", "function(integer, array(integer))").produces(BoundVariables.builder().setTypeVariable("T", INTEGER).build());
Signature varargApply = functionSignature().returnType(parseTypeSignature("T")).argumentTypes(parseTypeSignature("T"), parseTypeSignature("function(T, T)")).typeVariableConstraints(typeVariable("T")).setVariableArity(true).build();
assertThat(varargApply).boundTo("integer", "function(integer, integer)", "function(integer, integer)", "function(integer, integer)").produces(BoundVariables.builder().setTypeVariable("T", INTEGER).build());
assertThat(varargApply).boundTo("integer", "function(integer, integer)", "function(integer, double)", "function(double, double)").fails();
Signature loop = functionSignature().returnType(parseTypeSignature("T")).argumentTypes(parseTypeSignature("T"), parseTypeSignature("function(T, T)")).typeVariableConstraints(typeVariable("T")).build();
assertThat(loop).boundTo("integer", new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, BIGINT).getTypeSignature())).fails();
assertThat(loop).boundTo("integer", new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, BIGINT).getTypeSignature())).withCoercion().produces(BoundVariables.builder().setTypeVariable("T", BIGINT).build());
// TODO: Support coercion of return type of lambda
assertThat(loop).withCoercion().boundTo("integer", new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, SMALLINT).getTypeSignature())).fails();
// TODO: Support coercion of return type of lambda
// Without coercion support for return type of lambda, the return type of lambda must be `varchar(x)` to avoid need for coercions.
Signature varcharApply = functionSignature().returnType(parseTypeSignature("varchar")).argumentTypes(parseTypeSignature("varchar"), parseTypeSignature("function(varchar, varchar(x))", ImmutableSet.of("x"))).build();
assertThat(varcharApply).withCoercion().boundTo("varchar(10)", new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, createVarcharType(1)).getTypeSignature())).succeeds();
Signature sortByKey = functionSignature().returnType(parseTypeSignature("array(T)")).argumentTypes(parseTypeSignature("array(T)"), parseTypeSignature("function(T,E)")).typeVariableConstraints(typeVariable("T"), orderableTypeParameter("E")).build();
assertThat(sortByKey).boundTo("array(integer)", new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, VARCHAR).getTypeSignature())).produces(BoundVariables.builder().setTypeVariable("T", INTEGER).setTypeVariable("E", VARCHAR).build());
}
use of com.facebook.presto.spi.function.Signature in project presto by prestodb.
the class TestSignatureBinder method testBindLiteralForRepeatedDecimal.
@Test
public void testBindLiteralForRepeatedDecimal() {
TypeSignature leftType = parseTypeSignature("decimal(p,s)", ImmutableSet.of("p", "s"));
TypeSignature rightType = parseTypeSignature("decimal(p,s)", ImmutableSet.of("p", "s"));
Signature function = functionSignature().returnType(parseTypeSignature(StandardTypes.BOOLEAN)).argumentTypes(leftType, rightType).build();
assertThat(function).boundTo("decimal(10,5)", "decimal(10,5)").produces(new BoundVariables(ImmutableMap.of(), ImmutableMap.of("p", 10L, "s", 5L)));
assertThat(function).boundTo("decimal(10,8)", "decimal(9,8)").withCoercion().produces(new BoundVariables(ImmutableMap.of(), ImmutableMap.of("p", 10L, "s", 8L)));
assertThat(function).boundTo("decimal(10,2)", "decimal(10,8)").withCoercion().produces(new BoundVariables(ImmutableMap.of(), ImmutableMap.of("p", 16L, "s", 8L)));
assertThat(function).boundTo("unknown", "decimal(10,5)").withCoercion().produces(new BoundVariables(ImmutableMap.of(), ImmutableMap.of("p", 10L, "s", 5L)));
}
Aggregations