public BoundVariables build() { return new BoundVariables(typeVariables, longVariables); } }
public BoundVariables build() { return new BoundVariables(typeVariables, longVariables); } }
@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))); }
@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))); }
@Test public void testBindLiteralForDecimal() { TypeSignature leftType = parseTypeSignature("decimal(p1,s1)", ImmutableSet.of("p1", "s1")); TypeSignature rightType = parseTypeSignature("decimal(p2,s2)", ImmutableSet.of("p2", "s2")); Signature function = functionSignature() .returnType(parseTypeSignature(StandardTypes.BOOLEAN)) .argumentTypes(leftType, rightType) .build(); assertThat(function) .boundTo("decimal(2,1)", "decimal(1,0)") .produces(new BoundVariables( ImmutableMap.of(), ImmutableMap.of( "p1", 2L, "s1", 1L, "p2", 1L, "s2", 0L))); }
@Test public void testBindLiteralForDecimal() { TypeSignature leftType = parseTypeSignature("decimal(p1,s1)", ImmutableSet.of("p1", "s1")); TypeSignature rightType = parseTypeSignature("decimal(p2,s2)", ImmutableSet.of("p2", "s2")); Signature function = functionSignature() .returnType(parseTypeSignature(StandardTypes.BOOLEAN)) .argumentTypes(leftType, rightType) .build(); assertThat(function) .boundTo("decimal(2,1)", "decimal(1,0)") .produces(new BoundVariables( ImmutableMap.of(), ImmutableMap.of( "p1", 2L, "s1", 1L, "p2", 1L, "s2", 0L))); }
@Test public void testBindUnknownToDecimal() { Signature function = functionSignature() .returnType(parseTypeSignature("boolean")) .argumentTypes(parseTypeSignature("decimal(p,s)", ImmutableSet.of("p", "s"))) .build(); assertThat(function) .boundTo("unknown") .withCoercion() .produces(new BoundVariables( ImmutableMap.of(), ImmutableMap.of( "p", 1L, "s", 0L))); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testNoVariableReuseAcrossTypes() { Set<String> literalParameters = ImmutableSet.of("p1", "p2", "s"); TypeSignature leftType = parseTypeSignature("decimal(p1,s)", literalParameters); TypeSignature rightType = parseTypeSignature("decimal(p2,s)", literalParameters); Signature function = functionSignature() .returnType(BOOLEAN.getTypeSignature()) .argumentTypes(leftType, rightType) .build(); assertThat(function) .boundTo("decimal(2,1)", "decimal(3,1)") .produces(new BoundVariables( ImmutableMap.of(), ImmutableMap.of())); }
@Test public void testBindUnknownToDecimal() { Signature function = functionSignature() .returnType(parseTypeSignature("boolean")) .argumentTypes(parseTypeSignature("decimal(p,s)", ImmutableSet.of("p", "s"))) .build(); assertThat(function) .boundTo("unknown") .withCoercion() .produces(new BoundVariables( ImmutableMap.of(), ImmutableMap.of( "p", 1L, "s", 0L))); }
@Test(expectedExceptions = UnsupportedOperationException.class) public void testNoVariableReuseAcrossTypes() { Set<String> literalParameters = ImmutableSet.of("p1", "p2", "s"); TypeSignature leftType = parseTypeSignature("decimal(p1,s)", literalParameters); TypeSignature rightType = parseTypeSignature("decimal(p2,s)", literalParameters); Signature function = functionSignature() .returnType(BOOLEAN.getTypeSignature()) .argumentTypes(leftType, rightType) .build(); assertThat(function) .boundTo("decimal(2,1)", "decimal(3,1)") .produces(new BoundVariables( ImmutableMap.of(), ImmutableMap.of())); }
@Test public void testBindUnknownToTypeParameter() { Signature function = functionSignature() .returnType(parseTypeSignature("T")) .argumentTypes(parseTypeSignature("T")) .typeVariableConstraints(ImmutableList.of(typeVariable("T"))) .build(); assertThat(function) .boundTo("unknown") .withCoercion() .produces(new BoundVariables( ImmutableMap.of("T", type("unknown")), ImmutableMap.of())); }
@Test public void testBindUnknownToTypeParameter() { Signature function = functionSignature() .returnType(parseTypeSignature("T")) .argumentTypes(parseTypeSignature("T")) .typeVariableConstraints(ImmutableList.of(typeVariable("T"))) .build(); assertThat(function) .boundTo("unknown") .withCoercion() .produces(new BoundVariables( ImmutableMap.of("T", type("unknown")), ImmutableMap.of())); }
@Test public void testBindMixedLiteralAndTypeVariables() { Signature function = functionSignature() .returnType(parseTypeSignature(StandardTypes.BOOLEAN)) .typeVariableConstraints(ImmutableList.of(typeVariable("T"))) .argumentTypes(parseTypeSignature("array(T)"), parseTypeSignature("array(decimal(p,s))", ImmutableSet.of("p", "s"))) .build(); assertThat(function) .boundTo("array(decimal(2,1))", "array(decimal(3,1))") .withCoercion() .produces(new BoundVariables( ImmutableMap.of( "T", type("decimal(2,1)")), ImmutableMap.of( "p", 3L, "s", 1L))); }
@Test public void testBindMixedLiteralAndTypeVariables() { Signature function = functionSignature() .returnType(parseTypeSignature(StandardTypes.BOOLEAN)) .typeVariableConstraints(ImmutableList.of(typeVariable("T"))) .argumentTypes(parseTypeSignature("array(T)"), parseTypeSignature("array(decimal(p,s))", ImmutableSet.of("p", "s"))) .build(); assertThat(function) .boundTo("array(decimal(2,1))", "array(decimal(3,1))") .withCoercion() .produces(new BoundVariables( ImmutableMap.of( "T", type("decimal(2,1)")), ImmutableMap.of( "p", 3L, "s", 1L))); }
@Test public void testBindVarcharTemplateStyle() { Signature function = functionSignature() .returnType(parseTypeSignature("T2")) .argumentTypes(parseTypeSignature("T1")) .typeVariableConstraints(ImmutableList.of( new TypeVariableConstraint("T1", true, false, "varchar"), new TypeVariableConstraint("T2", true, false, "varchar"))) .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())); }
@Test public void testBindTypeVariablesBasedOnTheSecondArgument() { Signature function = functionSignature() .returnType(parseTypeSignature("T")) .argumentTypes(parseTypeSignature("array(T)"), parseTypeSignature("T")) .typeVariableConstraints(ImmutableList.of(typeVariable("T"))) .build(); assertThat(function) .boundTo("unknown", "decimal(2,1)") .withCoercion() .produces(new BoundVariables( ImmutableMap.of("T", type("decimal(2,1)")), ImmutableMap.of())); }
@Test public void testBindTypeVariablesBasedOnTheSecondArgument() { Signature function = functionSignature() .returnType(parseTypeSignature("T")) .argumentTypes(parseTypeSignature("array(T)"), parseTypeSignature("T")) .typeVariableConstraints(ImmutableList.of(typeVariable("T"))) .build(); assertThat(function) .boundTo("unknown", "decimal(2,1)") .withCoercion() .produces(new BoundVariables( ImmutableMap.of("T", type("decimal(2,1)")), ImmutableMap.of())); }
@Test public void testBindVarcharTemplateStyle() { Signature function = functionSignature() .returnType(parseTypeSignature("T2")) .argumentTypes(parseTypeSignature("T1")) .typeVariableConstraints(ImmutableList.of( new TypeVariableConstraint("T1", true, false, "varchar"), new TypeVariableConstraint("T2", true, false, "varchar"))) .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())); }
@Test public void testMap() { Signature getValueFunction = functionSignature() .returnType(parseTypeSignature("V")) .argumentTypes(parseTypeSignature("map(K,V)"), parseTypeSignature("K")) .typeVariableConstraints(ImmutableList.of(typeVariable("K"), typeVariable("V"))) .build(); assertThat(getValueFunction) .boundTo("map(bigint,varchar)", "bigint") .produces(new BoundVariables( ImmutableMap.of( "K", type("bigint"), "V", type("varchar")), ImmutableMap.of())); assertThat(getValueFunction) .boundTo("map(bigint,varchar)", "varchar") .withCoercion() .fails(); }
@Test public void testMap() { Signature getValueFunction = functionSignature() .returnType(parseTypeSignature("V")) .argumentTypes(parseTypeSignature("map(K,V)"), parseTypeSignature("K")) .typeVariableConstraints(ImmutableList.of(typeVariable("K"), typeVariable("V"))) .build(); assertThat(getValueFunction) .boundTo("map(bigint,varchar)", "bigint") .produces(new BoundVariables( ImmutableMap.of( "K", type("bigint"), "V", type("varchar")), ImmutableMap.of())); assertThat(getValueFunction) .boundTo("map(bigint,varchar)", "varchar") .withCoercion() .fails(); }