public SignatureBuilder typeVariableConstraints(TypeVariableConstraint... typeVariableConstraints) { return typeVariableConstraints(asList(requireNonNull(typeVariableConstraints, "typeVariableConstraints is null"))); }
public SignatureBuilder typeVariableConstraints(TypeVariableConstraint... typeVariableConstraints) { return typeVariableConstraints(asList(requireNonNull(typeVariableConstraints, "typeVariableConstraints is null"))); }
private static SignatureBuilder functionSignature(List<String> arguments, String returnType, List<TypeVariableConstraint> typeVariableConstraints) { ImmutableSet<String> literalParameters = ImmutableSet.of("p", "s", "p1", "s1", "p2", "s2", "p3", "s3"); List<TypeSignature> argumentSignatures = arguments.stream() .map((signature) -> TypeSignature.parseTypeSignature(signature, literalParameters)) .collect(toImmutableList()); return new SignatureBuilder() .returnType(TypeSignature.parseTypeSignature(returnType, literalParameters)) .argumentTypes(argumentSignatures) .typeVariableConstraints(typeVariableConstraints) .kind(SCALAR); }
private static SignatureBuilder functionSignature(List<String> arguments, String returnType, List<TypeVariableConstraint> typeVariableConstraints) { ImmutableSet<String> literalParameters = ImmutableSet.of("p", "s", "p1", "s1", "p2", "s2", "p3", "s3"); List<TypeSignature> argumentSignatures = arguments.stream() .map((signature) -> TypeSignature.parseTypeSignature(signature, literalParameters)) .collect(toImmutableList()); return new SignatureBuilder() .returnType(TypeSignature.parseTypeSignature(returnType, literalParameters)) .argumentTypes(argumentSignatures) .typeVariableConstraints(typeVariableConstraints) .kind(SCALAR); }
@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 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 testBindUnknownToVariadic() { Signature rowFunction = functionSignature() .returnType(parseTypeSignature(StandardTypes.BOOLEAN)) .argumentTypes(parseTypeSignature("T"), parseTypeSignature("T")) .typeVariableConstraints(ImmutableList.of(withVariadicBound("T", "row"))) .build(); assertThat(rowFunction) .boundTo("unknown", "row(a bigint)") .withCoercion() .produces(new BoundVariables( ImmutableMap.of("T", type("row(a bigint)")), ImmutableMap.of())); Signature arrayFunction = functionSignature() .returnType(parseTypeSignature(StandardTypes.BOOLEAN)) .argumentTypes(parseTypeSignature("T"), parseTypeSignature("T")) .typeVariableConstraints(ImmutableList.of(withVariadicBound("T", "array"))) .build(); assertThat(arrayFunction) .boundTo("unknown", "array(bigint)") .withCoercion() .produces(new BoundVariables( ImmutableMap.of("T", type("array(bigint)")), ImmutableMap.of())); }
@Test public void testBindParametricTypeParameterToUnknown() { Signature function = functionSignature() .returnType(parseTypeSignature("T")) .argumentTypes(parseTypeSignature("array(T)")) .typeVariableConstraints(ImmutableList.of(typeVariable("T"))) .build(); assertThat(function) .boundTo("unknown") .fails(); assertThat(function) .withCoercion() .boundTo("unknown") .succeeds(); }
@Test public void testBasic() { Signature function = functionSignature() .typeVariableConstraints(ImmutableList.of(typeVariable("T"))) .returnType(parseTypeSignature("T")) .argumentTypes(parseTypeSignature("T")) .build(); assertThat(function) .boundTo("bigint") .produces(new BoundVariables( ImmutableMap.of("T", type("bigint")), ImmutableMap.of())); assertThat(function) .boundTo("varchar") .produces(new BoundVariables( ImmutableMap.of("T", type("varchar")), ImmutableMap.of())); assertThat(function) .boundTo("varchar", "bigint") .fails(); assertThat(function) .boundTo("array(bigint)") .produces(new BoundVariables( ImmutableMap.of("T", type("array(bigint)")), ImmutableMap.of())); }
@Test public void testBindParametricTypeParameterToUnknown() { Signature function = functionSignature() .returnType(parseTypeSignature("T")) .argumentTypes(parseTypeSignature("array(T)")) .typeVariableConstraints(ImmutableList.of(typeVariable("T"))) .build(); assertThat(function) .boundTo("unknown") .fails(); assertThat(function) .withCoercion() .boundTo("unknown") .succeeds(); }
@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(); }
@Test public void testTypeParameters() throws Throwable { Signature signature = Signature.builder() .name("foo") .kind(SCALAR) .typeVariableConstraints(comparableWithVariadicBound("V", VARCHAR)) .returnType(parseTypeSignature("V")) .argumentTypes(parseTypeSignature("V")) .build(); SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class) .signature(signature) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup.methods("varcharToVarchar"))) .build(); ScalarFunctionImplementation functionImplementation = function.specialize(BOUND_VARIABLES, 1, TYPE_REGISTRY, REGISTRY); Slice slice = (Slice) functionImplementation.getMethodHandle().invoke(INPUT_SLICE); assertEquals(slice, VARCHAR_TO_VARCHAR_RETURN_VALUE); }
@Test public void testTypeParameters() throws Throwable { Signature signature = Signature.builder() .name("foo") .kind(SCALAR) .typeVariableConstraints(comparableWithVariadicBound("V", VARCHAR)) .returnType(parseTypeSignature("V")) .argumentTypes(parseTypeSignature("V")) .build(); SqlScalarFunction function = SqlScalarFunction.builder(TestMethods.class) .signature(signature) .deterministic(true) .choice(choice -> choice .implementation(methodsGroup -> methodsGroup.methods("varcharToVarchar"))) .build(); ScalarFunctionImplementation functionImplementation = function.specialize(BOUND_VARIABLES, 1, TYPE_REGISTRY, REGISTRY); Slice slice = (Slice) functionImplementation.getMethodHandle().invoke(INPUT_SLICE); assertEquals(slice, VARCHAR_TO_VARCHAR_RETURN_VALUE); }