private boolean returnTypeIsTheSame(List<ApplicableFunction> applicableFunctions) { Set<Type> returnTypes = applicableFunctions.stream() .map(function -> typeManager.getType(function.getBoundSignature().getReturnType())) .collect(Collectors.toSet()); return returnTypes.size() == 1; }
private boolean appendConstraintSolversForReturnValue(ImmutableList.Builder<TypeConstraintSolver> resultBuilder, TypeSignatureProvider actualReturnType) { TypeSignature formalReturnTypeSignature = declaredSignature.getReturnType(); return appendTypeRelationshipConstraintSolver(resultBuilder, formalReturnTypeSignature, actualReturnType, false) && appendConstraintSolvers(resultBuilder, formalReturnTypeSignature, actualReturnType, false); }
@VisibleForTesting public static ParametricAggregation parseFunctionDefinitionWithTypesConstraint(Class<?> clazz, TypeSignature returnType, List<TypeSignature> argumentTypes) { requireNonNull(returnType, "returnType is null"); requireNonNull(argumentTypes, "argumentTypes is null"); for (ParametricAggregation aggregation : parseFunctionDefinitions(clazz)) { if (aggregation.getSignature().getReturnType().equals(returnType) && aggregation.getSignature().getArgumentTypes().equals(argumentTypes)) { return aggregation; } } throw new IllegalArgumentException(String.format("No method with return type %s and arguments %s", returnType, argumentTypes)); }
public Signature withAlias(String name) { return new Signature(name, kind, typeVariableConstraints, longVariableConstraints, getReturnType(), getArgumentTypes(), variableArity); }
public void addImplementation(T implementation) { if (implementation.getSignature().getTypeVariableConstraints().isEmpty() && implementation.getSignature().getArgumentTypes().stream().noneMatch(TypeSignature::isCalculated) && !implementation.getSignature().getReturnType().isCalculated()) { exactImplementations.put(implementation.getSignature(), implementation); } else if (implementation.hasSpecializedTypeParameters()) { specializedImplementations.add(implementation); } else { genericImplementations.add(implementation); } }
private void checkSignature(Symbol symbol, Signature signature) { TypeSignature expectedTypeSignature = types.get(symbol).getTypeSignature(); TypeSignature actualTypeSignature = signature.getReturnType(); verifyTypeSignature(symbol, expectedTypeSignature, actualTypeSignature); }
@Override public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { if (arity < 2) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "There must be two or more concatenation arguments"); } Class<?> clazz = generateConcat(getSignature().getReturnType(), arity); MethodHandle methodHandle = methodHandle(clazz, "concat", nCopies(arity, Slice.class).toArray(new Class<?>[arity])); return new ScalarFunctionImplementation( false, nCopies(arity, valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), methodHandle, isDeterministic()); }
if (returnNativeContainerType != Object.class && returnNativeContainerType != typeManager.getType(boundSignature.getReturnType()).getJavaType()) { return Optional.empty();
public static Signature applyBoundVariables(Signature signature, BoundVariables boundVariables, int arity) { List<TypeSignature> argumentSignatures; if (signature.isVariableArity()) { argumentSignatures = expandVarargFormalTypeSignature(signature.getArgumentTypes(), arity); } else { checkArgument(signature.getArgumentTypes().size() == arity); argumentSignatures = signature.getArgumentTypes(); } List<TypeSignature> boundArgumentSignatures = applyBoundVariables(argumentSignatures, boundVariables); TypeSignature boundReturnTypeSignature = applyBoundVariables(signature.getReturnType(), boundVariables); return new Signature( signature.getName(), signature.getKind(), ImmutableList.of(), ImmutableList.of(), boundReturnTypeSignature, boundArgumentSignatures, false); }
@Override public InternalAggregationFunction specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { Type inputType = typeManager.getType(getOnlyElement(applyBoundVariables(getSignature().getArgumentTypes(), boundVariables))); Type outputType = typeManager.getType(applyBoundVariables(getSignature().getReturnType(), boundVariables)); return generateAggregation(inputType, outputType); }
rows.add(row( new StringLiteral(function.getSignature().getName()), new StringLiteral(function.getSignature().getReturnType().toString()), new StringLiteral(Joiner.on(", ").join(function.getSignature().getArgumentTypes())), new StringLiteral(getFunctionType(function)),
private ScalarImplementationChoice getScalarFunctionImplementationChoice( BoundVariables boundVariables, TypeManager typeManager, FunctionRegistry functionRegistry, PolymorphicScalarFunctionChoice choice) { List<TypeSignature> resolvedParameterTypeSignatures = applyBoundVariables(getSignature().getArgumentTypes(), boundVariables); List<Type> resolvedParameterTypes = resolveTypes(resolvedParameterTypeSignatures, typeManager); TypeSignature resolvedReturnTypeSignature = applyBoundVariables(getSignature().getReturnType(), boundVariables); Type resolvedReturnType = typeManager.getType(resolvedReturnTypeSignature); SpecializeContext context = new SpecializeContext(boundVariables, resolvedParameterTypes, resolvedReturnType, typeManager, functionRegistry); Optional<MethodAndNativeContainerTypes> matchingMethod = Optional.empty(); Optional<MethodsGroup> matchingMethodsGroup = Optional.empty(); for (MethodsGroup candidateMethodsGroup : choice.getMethodsGroups()) { for (MethodAndNativeContainerTypes candidateMethod : candidateMethodsGroup.getMethods()) { if (matchesParameterAndReturnTypes(candidateMethod, resolvedParameterTypes, resolvedReturnType, choice.getArgumentProperties(), choice.isNullableResult())) { if (matchingMethod.isPresent()) { throw new IllegalStateException("two matching methods (" + matchingMethod.get().getMethod().getName() + " and " + candidateMethod.getMethod().getName() + ") for parameter types " + resolvedParameterTypeSignatures); } matchingMethod = Optional.of(candidateMethod); matchingMethodsGroup = Optional.of(candidateMethodsGroup); } } } checkState(matchingMethod.isPresent(), "no matching method for parameter types %s", resolvedParameterTypes); List<Object> extraParameters = computeExtraParameters(matchingMethodsGroup.get(), context); MethodHandle methodHandle = applyExtraParameters(matchingMethod.get().getMethod(), extraParameters, choice.getArgumentProperties()); return new ScalarImplementationChoice(choice.isNullableResult(), choice.getArgumentProperties(), choice.getReturnPlaceConvention(), methodHandle, Optional.empty()); }
private Type getOperator(StackableAstVisitorContext<Context> context, Expression node, OperatorType operatorType, Expression... arguments) { ImmutableList.Builder<Type> argumentTypes = ImmutableList.builder(); for (Expression expression : arguments) { argumentTypes.add(process(expression, context)); } Signature operatorSignature; try { operatorSignature = functionRegistry.resolveOperator(operatorType, argumentTypes.build()); } catch (OperatorNotFoundException e) { throw new SemanticException(TYPE_MISMATCH, node, "%s", e.getMessage()); } catch (PrestoException e) { if (e.getErrorCode().getCode() == StandardErrorCode.AMBIGUOUS_FUNCTION_CALL.toErrorCode().getCode()) { throw new SemanticException(SemanticErrorCode.AMBIGUOUS_FUNCTION_CALL, node, e.getMessage()); } throw e; } for (int i = 0; i < arguments.length; i++) { Expression expression = arguments[i]; Type type = typeManager.getType(operatorSignature.getArgumentTypes().get(i)); coerceType(context, expression, type, format("Operator %s argument %d", operatorSignature, i)); } Type type = typeManager.getType(operatorSignature.getReturnType()); return setExpressionType(node, type); }
checkArgument(innerCall.getType().equals(JSON)); checkArgument(innerCall.getArguments().size() == 1); TypeSignature returnType = call.getSignature().getReturnType(); if (returnType.getBase().equals(ARRAY)) { return call(
validateOperator(operatorType, scalarSignature.getReturnType(), scalarSignature.getArgumentTypes()));
@Test public void testIdentityCast() { TypeRegistry typeManager = new TypeRegistry(); FunctionRegistry registry = new FunctionRegistry(typeManager, new BlockEncodingManager(typeManager), new FeaturesConfig()); Signature exactOperator = registry.getCoercion(HYPER_LOG_LOG, HYPER_LOG_LOG); assertEquals(exactOperator.getName(), mangleOperatorName(OperatorType.CAST.name())); assertEquals(transform(exactOperator.getArgumentTypes(), Functions.toStringFunction()), ImmutableList.of(StandardTypes.HYPER_LOG_LOG)); assertEquals(exactOperator.getReturnType().getBase(), StandardTypes.HYPER_LOG_LOG); }
@Test public void testMagicLiteralFunction() { Signature signature = getMagicLiteralFunctionSignature(TIMESTAMP_WITH_TIME_ZONE); assertEquals(signature.getName(), "$literal$timestamp with time zone"); assertEquals(signature.getArgumentTypes(), ImmutableList.of(parseTypeSignature(StandardTypes.BIGINT))); assertEquals(signature.getReturnType().getBase(), StandardTypes.TIMESTAMP_WITH_TIME_ZONE); TypeRegistry typeManager = new TypeRegistry(); FunctionRegistry registry = new FunctionRegistry(typeManager, new BlockEncodingManager(typeManager), new FeaturesConfig()); Signature function = registry.resolveFunction(QualifiedName.of(signature.getName()), fromTypeSignatures(signature.getArgumentTypes())); assertEquals(function.getArgumentTypes(), ImmutableList.of(parseTypeSignature(StandardTypes.BIGINT))); assertEquals(signature.getReturnType().getBase(), StandardTypes.TIMESTAMP_WITH_TIME_ZONE); }
@Test public void testSerializationRoundTrip() { ObjectMapperProvider objectMapperProvider = new ObjectMapperProvider(); objectMapperProvider.setJsonDeserializers(ImmutableMap.of(Type.class, new TypeDeserializer(new TypeRegistry()))); JsonCodec<Signature> codec = new JsonCodecFactory(objectMapperProvider, true).jsonCodec(Signature.class); Signature expected = new Signature( "function", SCALAR, parseTypeSignature(StandardTypes.BIGINT), ImmutableList.of(parseTypeSignature(StandardTypes.BOOLEAN), parseTypeSignature(StandardTypes.DOUBLE), parseTypeSignature(StandardTypes.VARCHAR))); String json = codec.toJson(expected); Signature actual = codec.fromJson(json); assertEquals(actual.getName(), expected.getName()); assertEquals(actual.getKind(), expected.getKind()); assertEquals(actual.getReturnType(), expected.getReturnType()); assertEquals(actual.getArgumentTypes(), expected.getArgumentTypes()); } }
makeClassName(Joiner.on("$").join("ArrayCast", elementCastSignature.getArgumentTypes().get(0), elementCastSignature.getReturnType())), type(Object.class)); Type toElementType = typeManager.getType(elementCastSignature.getReturnType()); CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(definition, binder); ArrayMapBytecodeExpression newArray = ArrayGeneratorUtils.map(scope, cachedInstanceBinder, fromElementType, toElementType, value, elementCastSignature.getName(), elementCast);
Type outputType = typeManager.getType(boundSignature.getReturnType());