@Override public Type getType() { return new FunctionType(argumentTypes, body.getType()); }
@Override public String getDisplayName() { ImmutableList<String> names = getTypeParameters().stream() .map(Type::getDisplayName) .collect(toImmutableList()); return "function<" + Joiner.on(",").join(names) + ">"; }
@Override public final Class<?> getJavaType() { throw new UnsupportedOperationException(getTypeSignature() + " type does not have Java type"); }
@Override protected Type visitBindExpression(BindExpression node, StackableAstVisitorContext<Context> context) { verify(context.getContext().isExpectingLambda(), "bind expression found when lambda is not expected"); StackableAstVisitorContext<Context> innerContext = new StackableAstVisitorContext<>(context.getContext().notExpectingLambda()); ImmutableList.Builder<Type> functionInputTypesBuilder = ImmutableList.builder(); for (Expression value : node.getValues()) { functionInputTypesBuilder.add(process(value, innerContext)); } functionInputTypesBuilder.addAll(context.getContext().getFunctionInputTypes()); List<Type> functionInputTypes = functionInputTypesBuilder.build(); FunctionType functionType = (FunctionType) process(node.getFunction(), new StackableAstVisitorContext<>(context.getContext().expectingLambda(functionInputTypes))); List<Type> argumentTypes = functionType.getArgumentTypes(); int numCapturedValues = node.getValues().size(); verify(argumentTypes.size() == functionInputTypes.size()); for (int i = 0; i < numCapturedValues; i++) { verify(functionInputTypes.get(i).equals(argumentTypes.get(i))); } FunctionType result = new FunctionType(argumentTypes.subList(numCapturedValues, argumentTypes.size()), functionType.getReturnType()); return setExpressionType(node, result); }
.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() .boundTo("integer", new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, SMALLINT).getTypeSignature())) .fails(); assertThat(varcharApply) .withCoercion() .boundTo("varchar(10)", new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, createVarcharType(1)).getTypeSignature())) .succeeds(); .build(); assertThat(sortByKey) .boundTo("array(integer)", new TypeSignatureProvider(paramTypes -> new FunctionType(paramTypes, VARCHAR).getTypeSignature())) .produces(BoundVariables.builder() .setTypeVariable("T", INTEGER)
public FunctionType(List<Type> argumentTypes, Type returnType) { this.signature = new TypeSignature(NAME, typeParameters(argumentTypes, returnType)); this.returnType = requireNonNull(returnType, "returnType is null"); this.argumentTypes = ImmutableList.copyOf(requireNonNull(argumentTypes, "argumentTypes is null")); }
@Override protected Object visitLambdaExpression(LambdaExpression node, Object context) { if (optimize) { // TODO: enable optimization related to lambda expression // A mechanism to convert function type back into lambda expression need to exist to enable optimization return node; } Expression body = node.getBody(); List<String> argumentNames = node.getArguments().stream() .map(LambdaArgumentDeclaration::getName) .map(Identifier::getValue) .collect(toImmutableList()); FunctionType functionType = (FunctionType) expressionTypes.get(NodeRef.<Expression>of(node)); checkArgument(argumentNames.size() == functionType.getArgumentTypes().size()); return generateVarArgsToMapAdapter( Primitives.wrap(functionType.getReturnType().getJavaType()), functionType.getArgumentTypes().stream() .map(Type::getJavaType) .map(Primitives::wrap) .collect(toImmutableList()), argumentNames, map -> process(body, new LambdaSymbolResolver(map))); }
verify(lambdaExpression.getArguments().size() == functionType.getArgumentTypes().size()); Map<NodeRef<Expression>, Type> lambdaArgumentExpressionTypes = new HashMap<>(); Map<Symbol, Type> lambdaArgumentSymbolTypes = new HashMap<>(); for (int j = 0; j < lambdaExpression.getArguments().size(); j++) { LambdaArgumentDeclaration argument = lambdaExpression.getArguments().get(j); Type type = functionType.getArgumentTypes().get(j); lambdaArgumentExpressionTypes.put(NodeRef.of(argument), type); lambdaArgumentSymbolTypes.put(new Symbol(argument.getName().getValue()), type);
Type actualReturnType = ((FunctionType) actualLambdaType).getReturnType();
public FunctionType(List<Type> argumentTypes, Type returnType) { super(parameterizedTypeName(NAME, typeParameters(argumentTypes, returnType)), MethodHandle.class); this.returnType = requireNonNull(returnType, "returnType is null"); this.argumentTypes = ImmutableList.copyOf(requireNonNull(argumentTypes, "argumentTypes is null")); }
process(expression, new StackableAstVisitorContext<>(context.getContext().expectingLambda(expectedFunctionType.getArgumentTypes())));
@Override public Type createType(TypeManager typeManager, List<TypeParameter> parameters) { checkArgument(parameters.size() >= 1, "Function type must have at least one parameter, got %s", parameters); checkArgument( parameters.stream().allMatch(parameter -> parameter.getKind() == ParameterKind.TYPE), "Expected only types as a parameters, got %s", parameters); List<Type> types = parameters.stream().map(TypeParameter::getType).collect(toList()); return new FunctionType(types.subList(0, types.size() - 1), types.get(types.size() - 1)); } }
@Override public boolean equalTo(Block leftBlock, int leftPosition, Block rightBlock, int rightPosition) { throw new UnsupportedOperationException(getTypeSignature() + " type is not comparable"); }
@Override public String getDisplayName() { ImmutableList<String> names = getTypeParameters().stream() .map(Type::getDisplayName) .collect(toImmutableList()); return "function<" + Joiner.on(",").join(names) + ">"; } }
FunctionType functionType = new FunctionType(types, returnType); return setExpressionType(node, functionType);
@Override public long hash(Block block, int position) { throw new UnsupportedOperationException(getTypeSignature() + " type is not comparable"); }
@Override public Type createType(List<TypeParameter> parameters) { checkArgument(parameters.size() >= 1, "Function type must have at least one parameter, got %s", parameters); checkArgument( parameters.stream().allMatch(parameter -> parameter.getKind() == ParameterKind.TYPE), "Expected only types as a parameters, got %s", parameters); List<Type> types = parameters.stream().map(parameter -> parameter.getType()).collect(toList()); return new FunctionType(types.subList(0, types.size() - 1), types.get(types.size() - 1)); } }
@Override public int compareTo(Block leftBlock, int leftPosition, Block rightBlock, int rightPosition) { throw new UnsupportedOperationException(getTypeSignature() + " type is not orderable"); }