Search in sources :

Example 1 with ADD

use of com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator.ADD in project presto by prestodb.

the class TestValuesNodeStats method testStatsForValuesNodeWithJustNulls.

@Test
public void testStatsForValuesNodeWithJustNulls() {
    FunctionResolution resolution = new FunctionResolution(tester().getMetadata().getFunctionAndTypeManager());
    PlanNodeStatsEstimate bigintNullAStats = PlanNodeStatsEstimate.builder().setOutputRowCount(1).setConfident(true).addVariableStatistics(new VariableReferenceExpression(Optional.empty(), "a", BIGINT), VariableStatsEstimate.zero()).build();
    tester().assertStatsFor(pb -> pb.values(ImmutableList.of(pb.variable("a", BIGINT)), ImmutableList.of(ImmutableList.of(call(ADD.name(), resolution.arithmeticFunction(ADD, BIGINT, BIGINT), BIGINT, constant(3L, BIGINT), constantNull(BIGINT)))))).check(outputStats -> outputStats.equalTo(bigintNullAStats));
    tester().assertStatsFor(pb -> pb.values(ImmutableList.of(pb.variable("a", BIGINT)), ImmutableList.of(ImmutableList.of(constantNull(BIGINT))))).check(outputStats -> outputStats.equalTo(bigintNullAStats));
    PlanNodeStatsEstimate unknownNullAStats = PlanNodeStatsEstimate.builder().setOutputRowCount(1).setConfident(true).addVariableStatistics(new VariableReferenceExpression(Optional.empty(), "a", UNKNOWN), VariableStatsEstimate.zero()).build();
    tester().assertStatsFor(pb -> pb.values(ImmutableList.of(pb.variable("a", UNKNOWN)), ImmutableList.of(ImmutableList.of(constantNull(UNKNOWN))))).check(outputStats -> outputStats.equalTo(unknownNullAStats));
}
Also used : BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) VARCHAR(com.facebook.presto.common.type.VarcharType.VARCHAR) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Test(org.testng.annotations.Test) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) Expressions.call(com.facebook.presto.sql.relational.Expressions.call) Expressions.constantNull(com.facebook.presto.sql.relational.Expressions.constantNull) Expressions.constant(com.facebook.presto.sql.relational.Expressions.constant) PlanBuilder.constantExpressions(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder.constantExpressions) UNKNOWN(com.facebook.presto.common.type.UnknownType.UNKNOWN) ImmutableList(com.google.common.collect.ImmutableList) ADD(com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator.ADD) Optional(java.util.Optional) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) Test(org.testng.annotations.Test)

Example 2 with ADD

use of com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator.ADD in project presto by prestodb.

the class TestValuesNodeStats method testStatsForValuesNode.

@Test
public void testStatsForValuesNode() {
    FunctionResolution resolution = new FunctionResolution(tester().getMetadata().getFunctionAndTypeManager());
    tester().assertStatsFor(pb -> pb.values(ImmutableList.of(pb.variable("a", BIGINT), pb.variable("b", DOUBLE)), ImmutableList.of(ImmutableList.of(call(ADD.name(), resolution.arithmeticFunction(ADD, BIGINT, BIGINT), BIGINT, constantExpressions(BIGINT, 3L, 3L)), constant(13.5, DOUBLE)), ImmutableList.of(constant(55L, BIGINT), constantNull(DOUBLE)), ImmutableList.of(constant(6L, BIGINT), constant(13.5, DOUBLE))))).check(outputStats -> outputStats.equalTo(PlanNodeStatsEstimate.builder().setOutputRowCount(3).setConfident(true).addVariableStatistics(new VariableReferenceExpression(Optional.empty(), "a", BIGINT), VariableStatsEstimate.builder().setNullsFraction(0).setLowValue(6).setHighValue(55).setDistinctValuesCount(2).build()).addVariableStatistics(new VariableReferenceExpression(Optional.empty(), "b", DOUBLE), VariableStatsEstimate.builder().setNullsFraction(0.33333333333333333).setLowValue(13.5).setHighValue(13.5).setDistinctValuesCount(1).build()).build()));
    tester().assertStatsFor(pb -> pb.values(ImmutableList.of(pb.variable("v", createVarcharType(30))), ImmutableList.of(constantExpressions(VARCHAR, utf8Slice("Alice")), constantExpressions(VARCHAR, utf8Slice("has")), constantExpressions(VARCHAR, utf8Slice("a cat")), ImmutableList.of(constantNull(VARCHAR))))).check(outputStats -> outputStats.equalTo(PlanNodeStatsEstimate.builder().setOutputRowCount(4).setConfident(true).addVariableStatistics(new VariableReferenceExpression(Optional.empty(), "v", createVarcharType(30)), VariableStatsEstimate.builder().setNullsFraction(0.25).setDistinctValuesCount(3).build()).build()));
}
Also used : BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) VARCHAR(com.facebook.presto.common.type.VarcharType.VARCHAR) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Test(org.testng.annotations.Test) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) Expressions.call(com.facebook.presto.sql.relational.Expressions.call) Expressions.constantNull(com.facebook.presto.sql.relational.Expressions.constantNull) Expressions.constant(com.facebook.presto.sql.relational.Expressions.constant) PlanBuilder.constantExpressions(com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder.constantExpressions) UNKNOWN(com.facebook.presto.common.type.UnknownType.UNKNOWN) ImmutableList(com.google.common.collect.ImmutableList) ADD(com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator.ADD) Optional(java.util.Optional) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) FunctionResolution(com.facebook.presto.sql.relational.FunctionResolution) Test(org.testng.annotations.Test)

Example 3 with ADD

use of com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator.ADD in project presto by prestodb.

the class GroupingOperationRewriter method rewriteGroupingOperation.

public static Expression rewriteGroupingOperation(GroupingOperation expression, List<Set<Integer>> groupingSets, Multimap<NodeRef<Expression>, FieldId> columnReferenceFields, Optional<VariableReferenceExpression> groupIdVariable) {
    requireNonNull(groupIdVariable, "groupIdVariable is null");
    // See SQL:2011:4.16.2 and SQL:2011:6.9.10.
    if (groupingSets.size() == 1) {
        return new LongLiteral("0");
    } else {
        checkState(groupIdVariable.isPresent(), "groupId symbol is missing");
        RelationId relationId = checkAndGetColumnReferenceField(expression.getGroupingColumns().get(0), columnReferenceFields).getRelationId();
        List<Integer> columns = expression.getGroupingColumns().stream().map(NodeRef::of).peek(groupingColumn -> checkState(columnReferenceFields.containsKey(groupingColumn), "the grouping column is not in the columnReferencesField map")).map(columnReferenceFields::get).flatMap(Collection::stream).map(fieldId -> translateFieldToInteger(fieldId, relationId)).collect(toImmutableList());
        List<Expression> groupingResults = groupingSets.stream().map(groupingSet -> String.valueOf(calculateGrouping(groupingSet, columns))).map(LongLiteral::new).collect(toImmutableList());
        // It is necessary to add a 1 to the groupId because the underlying array is indexed starting at 1
        return new SubscriptExpression(new ArrayConstructor(groupingResults), new ArithmeticBinaryExpression(ADD, createSymbolReference(groupIdVariable.get()), new GenericLiteral("BIGINT", "1")));
    }
}
Also used : SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) RelationId(com.facebook.presto.sql.analyzer.RelationId) GroupingOperation(com.facebook.presto.sql.tree.GroupingOperation) Collection(java.util.Collection) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ExpressionTreeUtils.checkAndGetColumnReferenceField(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.checkAndGetColumnReferenceField) Set(java.util.Set) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Multimap(com.google.common.collect.Multimap) Preconditions.checkState(com.google.common.base.Preconditions.checkState) NodeRef(com.facebook.presto.sql.tree.NodeRef) List(java.util.List) ArrayConstructor(com.facebook.presto.sql.tree.ArrayConstructor) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral) Expression(com.facebook.presto.sql.tree.Expression) ADD(com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator.ADD) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) Objects.requireNonNull(java.util.Objects.requireNonNull) FieldId(com.facebook.presto.sql.analyzer.FieldId) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) Optional(java.util.Optional) ExpressionTreeUtils.createSymbolReference(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Expression(com.facebook.presto.sql.tree.Expression) ArithmeticBinaryExpression(com.facebook.presto.sql.tree.ArithmeticBinaryExpression) RelationId(com.facebook.presto.sql.analyzer.RelationId) Collection(java.util.Collection) SubscriptExpression(com.facebook.presto.sql.tree.SubscriptExpression) ArrayConstructor(com.facebook.presto.sql.tree.ArrayConstructor) GenericLiteral(com.facebook.presto.sql.tree.GenericLiteral)

Aggregations

VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)3 ADD (com.facebook.presto.sql.tree.ArithmeticBinaryExpression.Operator.ADD)3 Optional (java.util.Optional)3 BIGINT (com.facebook.presto.common.type.BigintType.BIGINT)2 DOUBLE (com.facebook.presto.common.type.DoubleType.DOUBLE)2 UNKNOWN (com.facebook.presto.common.type.UnknownType.UNKNOWN)2 VARCHAR (com.facebook.presto.common.type.VarcharType.VARCHAR)2 VarcharType.createVarcharType (com.facebook.presto.common.type.VarcharType.createVarcharType)2 PlanBuilder.constantExpressions (com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder.constantExpressions)2 Expressions.call (com.facebook.presto.sql.relational.Expressions.call)2 Expressions.constant (com.facebook.presto.sql.relational.Expressions.constant)2 Expressions.constantNull (com.facebook.presto.sql.relational.Expressions.constantNull)2 FunctionResolution (com.facebook.presto.sql.relational.FunctionResolution)2 ImmutableList (com.google.common.collect.ImmutableList)2 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)2 Test (org.testng.annotations.Test)2 ExpressionTreeUtils.checkAndGetColumnReferenceField (com.facebook.presto.sql.analyzer.ExpressionTreeUtils.checkAndGetColumnReferenceField)1 ExpressionTreeUtils.createSymbolReference (com.facebook.presto.sql.analyzer.ExpressionTreeUtils.createSymbolReference)1 FieldId (com.facebook.presto.sql.analyzer.FieldId)1 RelationId (com.facebook.presto.sql.analyzer.RelationId)1