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));
}
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()));
}
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")));
}
}
Aggregations