Search in sources :

Example 56 with Signature

use of com.facebook.presto.metadata.Signature in project presto by prestodb.

the class ParametricScalar method specialize.

@Override
public ScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, TypeManager typeManager, FunctionRegistry functionRegistry) {
    Signature boundSignature = applyBoundVariables(getSignature(), boundVariables, arity);
    if (implementations.getExactImplementations().containsKey(boundSignature)) {
        ScalarImplementation implementation = implementations.getExactImplementations().get(boundSignature);
        Optional<MethodHandleAndConstructor> methodHandleAndConstructor = implementation.specialize(boundSignature, boundVariables, typeManager, functionRegistry);
        checkCondition(methodHandleAndConstructor.isPresent(), FUNCTION_IMPLEMENTATION_ERROR, String.format("Exact implementation of %s do not match expected java types.", boundSignature.getName()));
        return new ScalarFunctionImplementation(implementation.isNullable(), implementation.getNullableArguments(), implementation.getNullFlags(), methodHandleAndConstructor.get().getMethodHandle(), methodHandleAndConstructor.get().getConstructor(), isDeterministic());
    }
    ScalarFunctionImplementation selectedImplementation = null;
    for (ScalarImplementation implementation : implementations.getSpecializedImplementations()) {
        Optional<MethodHandleAndConstructor> methodHandle = implementation.specialize(boundSignature, boundVariables, typeManager, functionRegistry);
        if (methodHandle.isPresent()) {
            checkCondition(selectedImplementation == null, AMBIGUOUS_FUNCTION_IMPLEMENTATION, "Ambiguous implementation for %s with bindings %s", getSignature(), boundVariables.getTypeVariables());
            selectedImplementation = new ScalarFunctionImplementation(implementation.isNullable(), implementation.getNullableArguments(), implementation.getNullFlags(), methodHandle.get().getMethodHandle(), methodHandle.get().getConstructor(), isDeterministic());
        }
    }
    if (selectedImplementation != null) {
        return selectedImplementation;
    }
    for (ScalarImplementation implementation : implementations.getGenericImplementations()) {
        Optional<MethodHandleAndConstructor> methodHandle = implementation.specialize(boundSignature, boundVariables, typeManager, functionRegistry);
        if (methodHandle.isPresent()) {
            checkCondition(selectedImplementation == null, AMBIGUOUS_FUNCTION_IMPLEMENTATION, "Ambiguous implementation for %s with bindings %s", getSignature(), boundVariables.getTypeVariables());
            selectedImplementation = new ScalarFunctionImplementation(implementation.isNullable(), implementation.getNullableArguments(), implementation.getNullFlags(), methodHandle.get().getMethodHandle(), methodHandle.get().getConstructor(), isDeterministic());
        }
    }
    if (selectedImplementation != null) {
        return selectedImplementation;
    }
    throw new PrestoException(FUNCTION_IMPLEMENTATION_MISSING, format("Unsupported type parameters (%s) for %s", boundVariables, getSignature()));
}
Also used : MethodHandleAndConstructor(com.facebook.presto.operator.scalar.annotations.ScalarImplementation.MethodHandleAndConstructor) Signature(com.facebook.presto.metadata.Signature) PrestoException(com.facebook.presto.spi.PrestoException) ScalarImplementation(com.facebook.presto.operator.scalar.annotations.ScalarImplementation)

Example 57 with Signature

use of com.facebook.presto.metadata.Signature in project presto by prestodb.

the class CountAggregationBenchmark method createOperatorFactories.

@Override
protected List<? extends OperatorFactory> createOperatorFactories() {
    OperatorFactory tableScanOperator = createTableScanOperator(0, new PlanNodeId("test"), "orders", "orderkey");
    InternalAggregationFunction countFunction = localQueryRunner.getMetadata().getFunctionRegistry().getAggregateFunctionImplementation(new Signature("count", AGGREGATE, BIGINT.getTypeSignature()));
    AggregationOperatorFactory aggregationOperator = new AggregationOperatorFactory(1, new PlanNodeId("test"), Step.SINGLE, ImmutableList.of(countFunction.bind(ImmutableList.of(0), Optional.empty())));
    return ImmutableList.of(tableScanOperator, aggregationOperator);
}
Also used : PlanNodeId(com.facebook.presto.sql.planner.plan.PlanNodeId) OperatorFactory(com.facebook.presto.operator.OperatorFactory) AggregationOperatorFactory(com.facebook.presto.operator.AggregationOperator.AggregationOperatorFactory) AggregationOperatorFactory(com.facebook.presto.operator.AggregationOperator.AggregationOperatorFactory) Signature(com.facebook.presto.metadata.Signature) InternalAggregationFunction(com.facebook.presto.operator.aggregation.InternalAggregationFunction)

Example 58 with Signature

use of com.facebook.presto.metadata.Signature in project presto by prestodb.

the class TestInCodeGenerator method testBigint.

@Test
public void testBigint() {
    List<RowExpression> values = new ArrayList<>();
    values.add(new ConstantExpression(Integer.MAX_VALUE + 1L, BIGINT));
    values.add(new ConstantExpression(Integer.MIN_VALUE - 1L, BIGINT));
    values.add(new ConstantExpression(3L, BIGINT));
    assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH);
    values.add(new ConstantExpression(null, BIGINT));
    assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH);
    values.add(new CallExpression(new Signature(CAST, SCALAR, BIGINT.getTypeSignature(), DOUBLE.getTypeSignature()), BIGINT, Collections.singletonList(new ConstantExpression(12345678901234.0, DOUBLE))));
    assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH);
    for (long i = 6; i <= 32; ++i) {
        values.add(new ConstantExpression(i, BIGINT));
    }
    assertEquals(checkSwitchGenerationCase(BIGINT, values), HASH_SWITCH);
    values.add(new ConstantExpression(33L, BIGINT));
    assertEquals(checkSwitchGenerationCase(BIGINT, values), SET_CONTAINS);
}
Also used : ConstantExpression(com.facebook.presto.sql.relational.ConstantExpression) Signature(com.facebook.presto.metadata.Signature) ArrayList(java.util.ArrayList) RowExpression(com.facebook.presto.sql.relational.RowExpression) CallExpression(com.facebook.presto.sql.relational.CallExpression) Test(org.testng.annotations.Test)

Example 59 with Signature

use of com.facebook.presto.metadata.Signature in project presto by prestodb.

the class InCodeGeneratorBenchmark method setup.

@Setup
public void setup() {
    Random random = new Random();
    RowExpression[] arguments = new RowExpression[1 + inListCount];
    switch(type) {
        case StandardTypes.BIGINT:
            prestoType = BIGINT;
            for (int i = 1; i <= inListCount; i++) {
                arguments[i] = constant((long) random.nextInt(), BIGINT);
            }
            break;
        case StandardTypes.DOUBLE:
            prestoType = DOUBLE;
            for (int i = 1; i <= inListCount; i++) {
                arguments[i] = constant(random.nextDouble(), DOUBLE);
            }
            break;
        case StandardTypes.VARCHAR:
            prestoType = VARCHAR;
            for (int i = 1; i <= inListCount; i++) {
                arguments[i] = constant(Slices.utf8Slice(Long.toString(random.nextLong())), VARCHAR);
            }
            break;
        default:
            throw new IllegalStateException();
    }
    arguments[0] = field(0, prestoType);
    RowExpression project = field(0, prestoType);
    PageBuilder pageBuilder = new PageBuilder(ImmutableList.of(prestoType));
    for (int i = 0; i < 10_000; i++) {
        pageBuilder.declarePosition();
        switch(type) {
            case StandardTypes.BIGINT:
                BIGINT.writeLong(pageBuilder.getBlockBuilder(0), random.nextInt());
                break;
            case StandardTypes.DOUBLE:
                DOUBLE.writeDouble(pageBuilder.getBlockBuilder(0), random.nextDouble());
                break;
            case StandardTypes.VARCHAR:
                VARCHAR.writeSlice(pageBuilder.getBlockBuilder(0), Slices.utf8Slice(Long.toString(random.nextLong())));
                break;
        }
    }
    inputPage = pageBuilder.build();
    RowExpression filter = call(new Signature(IN, SCALAR, parseTypeSignature(StandardTypes.BOOLEAN)), BOOLEAN, arguments);
    processor = new ExpressionCompiler(MetadataManager.createTestMetadataManager()).compilePageProcessor(filter, ImmutableList.of(project)).get();
}
Also used : Random(java.util.Random) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) RowExpression(com.facebook.presto.sql.relational.RowExpression) PageBuilder(com.facebook.presto.spi.PageBuilder) Setup(org.openjdk.jmh.annotations.Setup)

Example 60 with Signature

use of com.facebook.presto.metadata.Signature in project presto by prestodb.

the class TestTypeValidator method testInvalidWindowFunctionSignature.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "type of symbol 'sum(_[0-9]+)?' is expected to be double, but the actual type is bigint")
public void testInvalidWindowFunctionSignature() throws Exception {
    Symbol windowSymbol = symbolAllocator.newSymbol("sum", DOUBLE);
    Signature signature = new Signature("sum", FunctionKind.WINDOW, ImmutableList.of(), ImmutableList.of(), // should be DOUBLE
    BIGINT.getTypeSignature(), ImmutableList.of(DOUBLE.getTypeSignature()), false);
    FunctionCall functionCall = new FunctionCall(QualifiedName.of("sum"), ImmutableList.of(columnC.toSymbolReference()));
    WindowNode.Frame frame = new WindowNode.Frame(WindowFrame.Type.RANGE, FrameBound.Type.UNBOUNDED_PRECEDING, Optional.empty(), FrameBound.Type.UNBOUNDED_FOLLOWING, Optional.empty());
    WindowNode.Function function = new WindowNode.Function(functionCall, signature, frame);
    WindowNode.Specification specification = new WindowNode.Specification(ImmutableList.of(), ImmutableList.of(), ImmutableMap.of());
    PlanNode node = new WindowNode(newId(), baseTableScan, specification, ImmutableMap.of(windowSymbol, function), Optional.empty(), ImmutableSet.of(), 0);
    assertTypesValid(node);
}
Also used : WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) WindowFrame(com.facebook.presto.sql.tree.WindowFrame) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Signature(com.facebook.presto.metadata.Signature) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) Test(org.testng.annotations.Test)

Aggregations

Signature (com.facebook.presto.metadata.Signature)123 TypeSignature.parseTypeSignature (com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)96 Test (org.testng.annotations.Test)91 MapType (com.facebook.presto.type.MapType)18 ImmutableList (com.google.common.collect.ImmutableList)16 RowExpression (com.facebook.presto.sql.relational.RowExpression)12 TypeSignature (com.facebook.presto.spi.type.TypeSignature)11 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)11 Block (com.facebook.presto.spi.block.Block)10 Type (com.facebook.presto.spi.type.Type)10 InternalAggregationFunction (com.facebook.presto.operator.aggregation.InternalAggregationFunction)8 Page (com.facebook.presto.spi.Page)8 DecimalType (com.facebook.presto.spi.type.DecimalType)8 CallExpression (com.facebook.presto.sql.relational.CallExpression)8 MethodHandle (java.lang.invoke.MethodHandle)8 MetadataManager (com.facebook.presto.metadata.MetadataManager)7 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)7 PlanNodeId (com.facebook.presto.sql.planner.plan.PlanNodeId)7 ConstantExpression (com.facebook.presto.sql.relational.ConstantExpression)7 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)6