public static Signature rowConstructorSignature(Type returnType, List<Type> argumentTypes) { return internalScalarFunction(ROW_CONSTRUCTOR, returnType.getTypeSignature(), argumentTypes.stream().map(Type::getTypeSignature).collect(toImmutableList())); }
@UsedByGeneratedCode public static Block appendElement(Type elementType, Block block, long value) { BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1); for (int i = 0; i < block.getPositionCount(); i++) { elementType.appendTo(block, i, blockBuilder); } elementType.writeLong(blockBuilder, value); return blockBuilder.build(); }
public static Block nativeValueToBlock(Type type, Object object) { if (object != null && !Primitives.wrap(type.getJavaType()).isInstance(object)) { throw new IllegalArgumentException(String.format("Object '%s' does not match type %s", object, type.getJavaType())); } BlockBuilder blockBuilder = type.createBlockBuilder(null, 1); writeNativeValue(type, blockBuilder, object); return blockBuilder.build(); }
@UsedByGeneratedCode public static Block appendElement(Type elementType, Block block, Slice value) { BlockBuilder blockBuilder = elementType.createBlockBuilder(null, block.getPositionCount() + 1); for (int i = 0; i < block.getPositionCount(); i++) { elementType.appendTo(block, i, blockBuilder); } elementType.writeSlice(blockBuilder, value); return blockBuilder.build(); }
private static List<Object> decodeRecordReaderList(Type type, List<?> list) { Type elementType = type.getTypeParameters().get(0); return list.stream() .map(element -> decodeRecordReaderValue(elementType, element)) .collect(toList()); }
@Override protected RowExpression visitLambdaExpression(LambdaExpression node, Void context) { RowExpression body = process(node.getBody(), context); Type type = getType(node); List<Type> typeParameters = type.getTypeParameters(); List<Type> argumentTypes = typeParameters.subList(0, typeParameters.size() - 1); List<String> argumentNames = node.getArguments().stream() .map(LambdaArgumentDeclaration::getName) .map(Identifier::getValue) .collect(toImmutableList()); return new LambdaDefinitionExpression(argumentTypes, argumentNames, body); }
private static List<TypeSignatureParameter> typeParameters(List<Type> argumentTypes, Type returnType) { requireNonNull(returnType, "returnType is null"); requireNonNull(argumentTypes, "argumentTypes is null"); ImmutableList.Builder<TypeSignatureParameter> builder = ImmutableList.builder(); argumentTypes.stream() .map(Type::getTypeSignature) .map(TypeSignatureParameter::of) .forEach(builder::add); builder.add(TypeSignatureParameter.of(returnType.getTypeSignature())); return builder.build(); }
@Override public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { Type type = boundVariables.getTypeVariable("E"); checkArgument(type.isOrderable(), "Type must be orderable"); MethodHandle compareMethod = functionRegistry.getScalarFunctionImplementation(internalOperator(operatorType, BOOLEAN, ImmutableList.of(type, type))).getMethodHandle(); List<Class<?>> javaTypes = IntStream.range(0, arity) .mapToObj(i -> type.getJavaType()) .collect(toImmutableList()); Class<?> clazz = generate(javaTypes, type, compareMethod); MethodHandle methodHandle = methodHandle(clazz, getSignature().getName(), javaTypes.toArray(new Class<?>[javaTypes.size()])); return new ScalarFunctionImplementation( false, nCopies(javaTypes.size(), valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), methodHandle, isDeterministic()); }
@Override public Block toBlock(Type desiredType) { checkArgument(desiredType.getTypeParameters().size() == 1 && BIGINT.equals(desiredType.getTypeParameters().get(0)), "type doesn't match: %s", desiredType); int numberOfRecords = numberOfRecords(); return ArrayBlock.fromElementBlock( numberOfRecords, Optional.of(nulls == null ? new boolean[numberOfRecords] : nulls), calculateOffsets(sizes, nulls, numberOfRecords), values != null ? values.toBlock(BIGINT) : new LongArrayBlock(0, Optional.empty(), new long[] {})); }
@Test public void testEvaluateClassifierPredictions() { metadata.addFunctions(extractFunctions(new MLPlugin().getFunctions())); InternalAggregationFunction aggregation = metadata.getFunctionRegistry().getAggregateFunctionImplementation( new Signature("evaluate_classifier_predictions", AGGREGATE, parseTypeSignature(StandardTypes.VARCHAR), parseTypeSignature(StandardTypes.BIGINT), parseTypeSignature(StandardTypes.BIGINT))); Accumulator accumulator = aggregation.bind(ImmutableList.of(0, 1), Optional.empty()).createAccumulator(); accumulator.addInput(getPage()); BlockBuilder finalOut = accumulator.getFinalType().createBlockBuilder(null, 1); accumulator.evaluateFinal(finalOut); Block block = finalOut.build(); String output = VARCHAR.getSlice(block, 0).toStringUtf8(); List<String> parts = ImmutableList.copyOf(Splitter.on('\n').omitEmptyStrings().split(output)); assertEquals(parts.size(), 7, output); assertEquals(parts.get(0), "Accuracy: 1/2 (50.00%)"); }
@Test public void testLearn() { Type mapType = typeManager.getParameterizedType("map", ImmutableList.of(TypeSignatureParameter.of(parseTypeSignature(StandardTypes.BIGINT)), TypeSignatureParameter.of(parseTypeSignature(StandardTypes.DOUBLE)))); InternalAggregationFunction aggregation = generateInternalAggregationFunction(LearnClassifierAggregation.class, ClassifierType.BIGINT_CLASSIFIER.getTypeSignature(), ImmutableList.of(BIGINT.getTypeSignature(), mapType.getTypeSignature()), typeManager); assertLearnClassifer(aggregation.bind(ImmutableList.of(0, 1), Optional.empty()).createAccumulator()); }
@Override public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { Type keyType = boundVariables.getTypeVariable("K"); Type valueType = boundVariables.getTypeVariable("V"); MapType mapType = (MapType) typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of( TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(valueType.getTypeSignature()))); return new ScalarFunctionImplementation( false, ImmutableList.of( valueTypeArgumentProperty(RETURN_NULL_ON_NULL), functionTypeArgumentProperty(BinaryFunctionInterface.class)), generateFilter(mapType), Optional.of(STATE_FACTORY.bindTo(mapType)), isDeterministic()); }
private static Type rowType(Type... fieldTypes) { ImmutableList.Builder<TypeSignatureParameter> typeSignatureParameters = ImmutableList.builder(); for (int i = 0; i < fieldTypes.length; i++) { String filedName = "field_" + i; Type fieldType = fieldTypes[i]; typeSignatureParameters.add(TypeSignatureParameter.of(new NamedTypeSignature(Optional.of(new RowFieldName(filedName, false)), fieldType.getTypeSignature()))); } return TYPE_MANAGER.getParameterizedType(StandardTypes.ROW, typeSignatureParameters.build()); } }
public Optional<BoundVariables> bindVariables(List<? extends TypeSignatureProvider> actualArgumentTypes, Type actualReturnType) { ImmutableList.Builder<TypeConstraintSolver> constraintSolvers = ImmutableList.builder(); if (!appendConstraintSolversForReturnValue(constraintSolvers, new TypeSignatureProvider(actualReturnType.getTypeSignature()))) { return Optional.empty(); } if (!appendConstraintSolversForArguments(constraintSolvers, actualArgumentTypes)) { return Optional.empty(); } return iterativeSolve(constraintSolvers.build()); }
private Type getTypeNullName(int numFields) { ImmutableList.Builder<TypeSignatureParameter> typeSignatureParameters = ImmutableList.builder(); for (int i = 0; i < numFields; i++) { typeSignatureParameters.add(TypeSignatureParameter.of(new NamedTypeSignature(Optional.empty(), TEST_DATA_TYPE.getTypeSignature()))); } return TYPE_MANAGER.getParameterizedType(StandardTypes.ROW, typeSignatureParameters.build()); } }
public void assertDecodedAs(String jsonValue, Type type, boolean expectedValue) { checkArgument(type.getJavaType() == boolean.class, "Wrong (not boolean based) presto type '%s'", type); FieldValueProvider decodedValue = decode(Optional.of(jsonValue), type); assertFalse(decodedValue.isNull(), format("expected non null when decoding %s as %s", jsonValue, type)); assertEquals(decodedValue.getBoolean(), expectedValue); }
@Override public Block toBlock(Type desiredType) { checkArgument(desiredType.getJavaType() == Slice.class, "type doesn't match: %s", desiredType); Slice values = bytes == null ? Slices.EMPTY_SLICE : Slices.wrappedBuffer(bytes); int numberOfRecords = numberOfRecords(); return new VariableWidthBlock( numberOfRecords, values, calculateOffsets(sizes, nulls, numberOfRecords), Optional.ofNullable(nulls)); }
@Test public void testEmptyStateOutputsNull() { InternalAggregationFunction bigIntAgg = metadata.getFunctionRegistry().getAggregateFunctionImplementation( new Signature("array_agg", AGGREGATE, parseTypeSignature("array(bigint)"), parseTypeSignature(StandardTypes.BIGINT))); GroupedAccumulator groupedAccumulator = bigIntAgg.bind(Ints.asList(new int[] {}), Optional.empty()) .createGroupedAccumulator(); BlockBuilder blockBuilder = groupedAccumulator.getFinalType().createBlockBuilder(null, 1000); groupedAccumulator.evaluateFinal(0, blockBuilder); assertTrue(blockBuilder.isNull(0)); }
@Override public InternalAggregationFunction specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) { Type keyType = boundVariables.getTypeVariable("K"); Type outputType = typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of( TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(BIGINT.getTypeSignature()))); return generateAggregation(NAME, keyType, outputType, groupMode); }
RowPageBuilder(Iterable<Type> types) { this.types = ImmutableList.copyOf(requireNonNull(types, "types is null")); ImmutableList.Builder<BlockBuilder> builders = ImmutableList.builder(); for (Type type : types) { builders.add(type.createBlockBuilder(null, 1)); } this.builders = builders.build(); checkArgument(!this.builders.isEmpty(), "At least one value info is required"); }