Search in sources :

Example 71 with Signature

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

the class WindowAnnotationsParser method parse.

private static SqlWindowFunction parse(Class<? extends WindowFunction> clazz, WindowFunctionSignature window) {
    List<TypeVariableConstraint> typeVariables = ImmutableList.of();
    if (!window.typeVariable().isEmpty()) {
        typeVariables = ImmutableList.of(typeVariable(window.typeVariable()));
    }
    List<TypeSignature> argumentTypes = Stream.of(window.argumentTypes()).map(TypeSignature::parseTypeSignature).collect(toImmutableList());
    Signature signature = new Signature(window.name(), WINDOW, typeVariables, ImmutableList.of(), parseTypeSignature(window.returnType()), argumentTypes, false);
    return new SqlWindowFunction(new ReflectionWindowFunctionSupplier<>(signature, clazz));
}
Also used : TypeSignature(com.facebook.presto.spi.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) TypeSignature(com.facebook.presto.spi.type.TypeSignature) WindowFunctionSignature(com.facebook.presto.spi.function.WindowFunctionSignature) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) TypeVariableConstraint(com.facebook.presto.metadata.TypeVariableConstraint)

Example 72 with Signature

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

the class DecimalOperators method decimalAddOperator.

private static SqlScalarFunction decimalAddOperator() {
    TypeSignature decimalLeftSignature = parseTypeSignature("decimal(a_precision, a_scale)", ImmutableSet.of("a_precision", "a_scale"));
    TypeSignature decimalRightSignature = parseTypeSignature("decimal(b_precision, b_scale)", ImmutableSet.of("b_precision", "b_scale"));
    TypeSignature decimalResultSignature = parseTypeSignature("decimal(r_precision, r_scale)", ImmutableSet.of("r_precision", "r_scale"));
    Signature signature = Signature.builder().kind(SCALAR).operatorType(ADD).longVariableConstraints(longVariableExpression("r_precision", "min(38, max(a_precision - a_scale, b_precision - b_scale) + max(a_scale, b_scale) + 1)"), longVariableExpression("r_scale", "max(a_scale, b_scale)")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
    return SqlScalarFunction.builder(DecimalOperators.class).signature(signature).implementation(b -> b.methods("addShortShortShort").withExtraParameters(DecimalOperators::calculateShortRescaleParameters)).implementation(b -> b.methods("addShortShortLong", "addLongLongLong", "addShortLongLong", "addLongShortLong").withExtraParameters(DecimalOperators::calculateLongRescaleParameters)).build();
}
Also used : SpecializeContext(com.facebook.presto.metadata.SqlScalarFunctionBuilder.SpecializeContext) TypeSignature(com.facebook.presto.spi.type.TypeSignature) UnscaledDecimal128Arithmetic.rescale(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.rescale) MULTIPLY(com.facebook.presto.spi.function.OperatorType.MULTIPLY) Slice(io.airlift.slice.Slice) UnscaledDecimal128Arithmetic.divideRoundUp(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.divideRoundUp) UnscaledDecimal128Arithmetic.throwIfOverflows(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.throwIfOverflows) DIVISION_BY_ZERO(com.facebook.presto.spi.StandardErrorCode.DIVISION_BY_ZERO) HASH_CODE(com.facebook.presto.spi.function.OperatorType.HASH_CODE) Decimals.encodeUnscaledValue(com.facebook.presto.spi.type.Decimals.encodeUnscaledValue) UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong) Math.abs(java.lang.Math.abs) PrestoException(com.facebook.presto.spi.PrestoException) SCALAR(com.facebook.presto.metadata.FunctionKind.SCALAR) Signature.longVariableExpression(com.facebook.presto.metadata.Signature.longVariableExpression) MODULUS(com.facebook.presto.spi.function.OperatorType.MODULUS) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) DecimalType(com.facebook.presto.spi.type.DecimalType) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) BigInteger(java.math.BigInteger) Math.toIntExact(java.lang.Math.toIntExact) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode) UnscaledDecimal128Arithmetic(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic) Long.signum(java.lang.Long.signum) Decimals(com.facebook.presto.spi.type.Decimals) ImmutableSet(com.google.common.collect.ImmutableSet) DIVIDE(com.facebook.presto.spi.function.OperatorType.DIVIDE) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) Signature(com.facebook.presto.metadata.Signature) UnscaledDecimal128Arithmetic.isZero(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.isZero) UnscaledDecimal128Arithmetic.unscaledDecimal(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimal) NEGATION(com.facebook.presto.spi.function.OperatorType.NEGATION) Integer.max(java.lang.Integer.max) List(java.util.List) NUMERIC_VALUE_OUT_OF_RANGE(com.facebook.presto.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) UnscaledDecimal128Arithmetic.remainder(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.remainder) SqlScalarFunctionBuilder(com.facebook.presto.metadata.SqlScalarFunctionBuilder) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) SignatureBuilder(com.facebook.presto.metadata.SignatureBuilder) SUBTRACT(com.facebook.presto.spi.function.OperatorType.SUBTRACT) SqlType(com.facebook.presto.spi.function.SqlType) Decimals.longTenToNth(com.facebook.presto.spi.type.Decimals.longTenToNth) ADD(com.facebook.presto.spi.function.OperatorType.ADD) TypeSignature(com.facebook.presto.spi.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)

Example 73 with Signature

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

the class DecimalOperators method decimalMultiplyOperator.

private static SqlScalarFunction decimalMultiplyOperator() {
    TypeSignature decimalLeftSignature = parseTypeSignature("decimal(a_precision, a_scale)", ImmutableSet.of("a_precision", "a_scale"));
    TypeSignature decimalRightSignature = parseTypeSignature("decimal(b_precision, b_scale)", ImmutableSet.of("b_precision", "b_scale"));
    TypeSignature decimalResultSignature = parseTypeSignature("decimal(r_precision, r_scale)", ImmutableSet.of("r_precision", "r_scale"));
    Signature signature = Signature.builder().kind(SCALAR).operatorType(MULTIPLY).longVariableConstraints(longVariableExpression("r_precision", "min(38, a_precision + b_precision)"), longVariableExpression("r_scale", "a_scale + b_scale")).argumentTypes(decimalLeftSignature, decimalRightSignature).returnType(decimalResultSignature).build();
    return SqlScalarFunction.builder(DecimalOperators.class).signature(signature).implementation(b -> b.methods("multiplyShortShortShort", "multiplyShortShortLong", "multiplyLongLongLong", "multiplyShortLongLong", "multiplyLongShortLong")).build();
}
Also used : SpecializeContext(com.facebook.presto.metadata.SqlScalarFunctionBuilder.SpecializeContext) TypeSignature(com.facebook.presto.spi.type.TypeSignature) UnscaledDecimal128Arithmetic.rescale(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.rescale) MULTIPLY(com.facebook.presto.spi.function.OperatorType.MULTIPLY) Slice(io.airlift.slice.Slice) UnscaledDecimal128Arithmetic.divideRoundUp(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.divideRoundUp) UnscaledDecimal128Arithmetic.throwIfOverflows(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.throwIfOverflows) DIVISION_BY_ZERO(com.facebook.presto.spi.StandardErrorCode.DIVISION_BY_ZERO) HASH_CODE(com.facebook.presto.spi.function.OperatorType.HASH_CODE) Decimals.encodeUnscaledValue(com.facebook.presto.spi.type.Decimals.encodeUnscaledValue) UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimalToUnscaledLong) Math.abs(java.lang.Math.abs) PrestoException(com.facebook.presto.spi.PrestoException) SCALAR(com.facebook.presto.metadata.FunctionKind.SCALAR) Signature.longVariableExpression(com.facebook.presto.metadata.Signature.longVariableExpression) MODULUS(com.facebook.presto.spi.function.OperatorType.MODULUS) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) DecimalType(com.facebook.presto.spi.type.DecimalType) ImmutableList(com.google.common.collect.ImmutableList) Objects.requireNonNull(java.util.Objects.requireNonNull) BigInteger(java.math.BigInteger) Math.toIntExact(java.lang.Math.toIntExact) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode) UnscaledDecimal128Arithmetic(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic) Long.signum(java.lang.Long.signum) Decimals(com.facebook.presto.spi.type.Decimals) ImmutableSet(com.google.common.collect.ImmutableSet) DIVIDE(com.facebook.presto.spi.function.OperatorType.DIVIDE) SqlScalarFunction(com.facebook.presto.metadata.SqlScalarFunction) Signature(com.facebook.presto.metadata.Signature) UnscaledDecimal128Arithmetic.isZero(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.isZero) UnscaledDecimal128Arithmetic.unscaledDecimal(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.unscaledDecimal) NEGATION(com.facebook.presto.spi.function.OperatorType.NEGATION) Integer.max(java.lang.Integer.max) List(java.util.List) NUMERIC_VALUE_OUT_OF_RANGE(com.facebook.presto.spi.StandardErrorCode.NUMERIC_VALUE_OUT_OF_RANGE) UnscaledDecimal128Arithmetic.remainder(com.facebook.presto.spi.type.UnscaledDecimal128Arithmetic.remainder) SqlScalarFunctionBuilder(com.facebook.presto.metadata.SqlScalarFunctionBuilder) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) SignatureBuilder(com.facebook.presto.metadata.SignatureBuilder) SUBTRACT(com.facebook.presto.spi.function.OperatorType.SUBTRACT) SqlType(com.facebook.presto.spi.function.SqlType) Decimals.longTenToNth(com.facebook.presto.spi.type.Decimals.longTenToNth) ADD(com.facebook.presto.spi.function.OperatorType.ADD) TypeSignature(com.facebook.presto.spi.type.TypeSignature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) TypeSignature(com.facebook.presto.spi.type.TypeSignature) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature)

Example 74 with Signature

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

the class SimplifyCountOverConstant method isCountOverConstant.

private static boolean isCountOverConstant(AggregationNode.Aggregation aggregation, Assignments inputs) {
    Signature signature = aggregation.getSignature();
    if (!signature.getName().equals("count") || signature.getArgumentTypes().size() != 1) {
        return false;
    }
    Expression argument = aggregation.getCall().getArguments().get(0);
    if (argument instanceof SymbolReference) {
        argument = inputs.get(Symbol.from(argument));
    }
    return argument instanceof Literal && !(argument instanceof NullLiteral);
}
Also used : Expression(com.facebook.presto.sql.tree.Expression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) NullLiteral(com.facebook.presto.sql.tree.NullLiteral) Literal(com.facebook.presto.sql.tree.Literal) NullLiteral(com.facebook.presto.sql.tree.NullLiteral)

Example 75 with Signature

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

the class SimplifyCountOverConstant method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof AggregationNode)) {
        return Optional.empty();
    }
    AggregationNode parent = (AggregationNode) node;
    PlanNode input = lookup.resolve(parent.getSource());
    if (!(input instanceof ProjectNode)) {
        return Optional.empty();
    }
    ProjectNode child = (ProjectNode) input;
    boolean changed = false;
    Map<Symbol, AggregationNode.Aggregation> assignments = new LinkedHashMap<>(parent.getAssignments());
    for (Entry<Symbol, AggregationNode.Aggregation> entry : parent.getAssignments().entrySet()) {
        Symbol symbol = entry.getKey();
        AggregationNode.Aggregation aggregation = entry.getValue();
        if (isCountOverConstant(aggregation, child.getAssignments())) {
            changed = true;
            assignments.put(symbol, new AggregationNode.Aggregation(new FunctionCall(QualifiedName.of("count"), ImmutableList.of()), new Signature("count", AGGREGATE, parseTypeSignature(StandardTypes.BIGINT))));
        }
    }
    if (!changed) {
        return Optional.empty();
    }
    return Optional.of(new AggregationNode(node.getId(), child, assignments, parent.getGroupingSets(), parent.getStep(), parent.getHashSymbol(), parent.getGroupIdSymbol()));
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Symbol(com.facebook.presto.sql.planner.Symbol) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) LinkedHashMap(java.util.LinkedHashMap)

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