Search in sources :

Example 1 with LongVariableConstraint

use of com.facebook.presto.spi.function.LongVariableConstraint in project presto by prestodb.

the class SignatureBinder method calculateVariableValuesForLongConstraints.

private void calculateVariableValuesForLongConstraints(BoundVariables.Builder variableBinder) {
    for (LongVariableConstraint longVariableConstraint : declaredSignature.getLongVariableConstraints()) {
        String calculation = longVariableConstraint.getExpression();
        String variableName = longVariableConstraint.getName();
        Long calculatedValue = calculateLiteralValue(calculation, variableBinder.getLongVariables());
        if (variableBinder.containsLongVariable(variableName)) {
            Long currentValue = variableBinder.getLongVariable(variableName);
            checkState(Objects.equals(currentValue, calculatedValue), "variable '%s' is already set to %s when trying to set %s", variableName, currentValue, calculatedValue);
        }
        variableBinder.setLongVariable(variableName, calculatedValue);
    }
}
Also used : LongVariableConstraint(com.facebook.presto.spi.function.LongVariableConstraint)

Example 2 with LongVariableConstraint

use of com.facebook.presto.spi.function.LongVariableConstraint in project presto by prestodb.

the class TestAnnotationEngineForAggregates method testLongConstraintAggregateFunctionParse.

@Test
public void testLongConstraintAggregateFunctionParse() {
    Signature expectedSignature = new Signature(QualifiedObjectName.valueOf(DEFAULT_NAMESPACE, "parametric_aggregate_long_constraint"), FunctionKind.AGGREGATE, ImmutableList.of(), ImmutableList.of(new LongVariableConstraint("z", "x + y")), parseTypeSignature("varchar(z)", ImmutableSet.of("z")), ImmutableList.of(parseTypeSignature("varchar(x)", ImmutableSet.of("x")), parseTypeSignature("varchar(y)", ImmutableSet.of("y"))), false);
    ParametricAggregation aggregation = parseFunctionDefinition(LongConstraintAggregateFunction.class);
    assertEquals(aggregation.getDescription(), "Parametric aggregate with parametric type returned");
    assertTrue(aggregation.isDeterministic());
    assertEquals(aggregation.getSignature(), expectedSignature);
    ParametricImplementationsGroup<AggregationImplementation> implementations = aggregation.getImplementations();
    assertEquals(implementations.getGenericImplementations().size(), 1);
    AggregationImplementation implementation = implementations.getGenericImplementations().get(0);
    assertTrue(!implementation.getStateSerializerFactory().isPresent());
    assertEquals(implementation.getDefinitionClass(), LongConstraintAggregateFunction.class);
    assertDependencyCount(implementation, 0, 0, 0);
    assertEquals(implementation.getStateSerializerFactoryDependencies().size(), 0);
    assertFalse(implementation.hasSpecializedTypeParameters());
    List<AggregationMetadata.ParameterMetadata.ParameterType> expectedMetadataTypes = ImmutableList.of(AggregationMetadata.ParameterMetadata.ParameterType.STATE, AggregationMetadata.ParameterMetadata.ParameterType.INPUT_CHANNEL, AggregationMetadata.ParameterMetadata.ParameterType.INPUT_CHANNEL);
    assertTrue(implementation.getInputParameterMetadataTypes().equals(expectedMetadataTypes));
    InternalAggregationFunction specialized = aggregation.specialize(BoundVariables.builder().setLongVariable("x", 17L).setLongVariable("y", 13L).setLongVariable("z", 30L).build(), 2, FUNCTION_AND_TYPE_MANAGER);
    assertEquals(specialized.getFinalType(), VarcharType.createVarcharType(30));
    assertTrue(specialized.isDecomposable());
    assertEquals(specialized.name(), "parametric_aggregate_long_constraint");
}
Also used : AggregationImplementation(com.facebook.presto.operator.aggregation.AggregationImplementation) LongVariableConstraint(com.facebook.presto.spi.function.LongVariableConstraint) TypeSignature(com.facebook.presto.common.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.common.type.TypeSignature.parseTypeSignature) Signature(com.facebook.presto.spi.function.Signature) AggregationMetadata(com.facebook.presto.operator.aggregation.AggregationMetadata) ParametricAggregation(com.facebook.presto.operator.aggregation.ParametricAggregation) InternalAggregationFunction(com.facebook.presto.operator.aggregation.InternalAggregationFunction) Test(org.testng.annotations.Test)

Aggregations

LongVariableConstraint (com.facebook.presto.spi.function.LongVariableConstraint)2 TypeSignature (com.facebook.presto.common.type.TypeSignature)1 TypeSignature.parseTypeSignature (com.facebook.presto.common.type.TypeSignature.parseTypeSignature)1 AggregationImplementation (com.facebook.presto.operator.aggregation.AggregationImplementation)1 AggregationMetadata (com.facebook.presto.operator.aggregation.AggregationMetadata)1 InternalAggregationFunction (com.facebook.presto.operator.aggregation.InternalAggregationFunction)1 ParametricAggregation (com.facebook.presto.operator.aggregation.ParametricAggregation)1 Signature (com.facebook.presto.spi.function.Signature)1 Test (org.testng.annotations.Test)1