Search in sources :

Example 61 with PostAggregator

use of org.apache.druid.query.aggregation.PostAggregator in project druid by druid-io.

the class HllSketchEstimateOperatorConversion method toPostAggregator.

@Nullable
@Override
public PostAggregator toPostAggregator(PlannerContext plannerContext, RowSignature rowSignature, RexNode rexNode, PostAggregatorVisitor postAggregatorVisitor) {
    final List<RexNode> operands = ((RexCall) rexNode).getOperands();
    final PostAggregator firstOperand = OperatorConversions.toPostAggregator(plannerContext, rowSignature, operands.get(0), postAggregatorVisitor);
    if (firstOperand == null) {
        return null;
    }
    boolean round = HllSketchAggregatorFactory.DEFAULT_ROUND;
    if (operands.size() == 2) {
        if (!operands.get(1).isA(SqlKind.LITERAL)) {
            return null;
        }
        round = RexLiteral.booleanValue(operands.get(1));
    }
    return new HllSketchToEstimatePostAggregator(postAggregatorVisitor.getOutputNamePrefix() + postAggregatorVisitor.getAndIncrementCounter(), firstOperand, round);
}
Also used : RexCall(org.apache.calcite.rex.RexCall) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) HllSketchToEstimatePostAggregator(org.apache.druid.query.aggregation.datasketches.hll.HllSketchToEstimatePostAggregator) HllSketchToEstimatePostAggregator(org.apache.druid.query.aggregation.datasketches.hll.HllSketchToEstimatePostAggregator) RexNode(org.apache.calcite.rex.RexNode) Nullable(javax.annotation.Nullable)

Example 62 with PostAggregator

use of org.apache.druid.query.aggregation.PostAggregator in project druid by druid-io.

the class PostAveragerAggregatorCalculator method apply.

@Override
public Row apply(final Row row) {
    if (postAveragers.isEmpty()) {
        return row;
    }
    final Map<String, Object> newMap;
    newMap = Maps.newLinkedHashMap(((MapBasedRow) row).getEvent());
    for (PostAggregator postAverager : postAveragers) {
        boolean allColsPresent = postAverager.getDependentFields().stream().allMatch(c -> newMap.get(c) != null);
        newMap.put(postAverager.getName(), allColsPresent ? postAverager.compute(newMap) : null);
    }
    return new MapBasedRow(row.getTimestamp(), newMap);
}
Also used : MapBasedRow(org.apache.druid.data.input.MapBasedRow) PostAggregator(org.apache.druid.query.aggregation.PostAggregator)

Example 63 with PostAggregator

use of org.apache.druid.query.aggregation.PostAggregator in project druid by druid-io.

the class HllSketchUnionPostAggregator method compute.

@Override
public HllSketch compute(final Map<String, Object> combinedAggregators) {
    final Union union = new Union(lgK);
    for (final PostAggregator field : fields) {
        final HllSketch sketch = (HllSketch) field.compute(combinedAggregators);
        union.update(sketch);
    }
    return union.getResult(tgtHllType);
}
Also used : HllSketch(org.apache.datasketches.hll.HllSketch) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) Union(org.apache.datasketches.hll.Union)

Example 64 with PostAggregator

use of org.apache.druid.query.aggregation.PostAggregator in project druid by druid-io.

the class HllSketchEstimateWithErrorBoundsOperatorConversion method toPostAggregator.

@Nullable
@Override
public PostAggregator toPostAggregator(PlannerContext plannerContext, RowSignature rowSignature, RexNode rexNode, PostAggregatorVisitor postAggregatorVisitor) {
    final List<RexNode> operands = ((RexCall) rexNode).getOperands();
    final PostAggregator firstOperand = OperatorConversions.toPostAggregator(plannerContext, rowSignature, operands.get(0), postAggregatorVisitor);
    if (firstOperand == null) {
        return null;
    }
    Integer numStdDev = null;
    if (operands.size() == 2) {
        if (!operands.get(1).isA(SqlKind.LITERAL)) {
            return null;
        }
        numStdDev = ((Number) RexLiteral.value(operands.get(1))).intValue();
    }
    return new HllSketchToEstimateWithBoundsPostAggregator(postAggregatorVisitor.getOutputNamePrefix() + postAggregatorVisitor.getAndIncrementCounter(), firstOperand, numStdDev);
}
Also used : RexCall(org.apache.calcite.rex.RexCall) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) HllSketchToEstimateWithBoundsPostAggregator(org.apache.druid.query.aggregation.datasketches.hll.HllSketchToEstimateWithBoundsPostAggregator) HllSketchToEstimateWithBoundsPostAggregator(org.apache.druid.query.aggregation.datasketches.hll.HllSketchToEstimateWithBoundsPostAggregator) RexNode(org.apache.calcite.rex.RexNode) Nullable(javax.annotation.Nullable)

Example 65 with PostAggregator

use of org.apache.druid.query.aggregation.PostAggregator in project druid by druid-io.

the class HllSketchSetUnionOperatorConversion method toPostAggregator.

@Nullable
@Override
public PostAggregator toPostAggregator(PlannerContext plannerContext, RowSignature rowSignature, RexNode rexNode, PostAggregatorVisitor postAggregatorVisitor) {
    final List<RexNode> operands = ((RexCall) rexNode).getOperands();
    final List<PostAggregator> inputPostAggs = new ArrayList<>();
    Integer lgK = null;
    String tgtHllType = null;
    int operandCounter = 0;
    for (RexNode operand : operands) {
        final PostAggregator convertedPostAgg = OperatorConversions.toPostAggregator(plannerContext, rowSignature, operand, postAggregatorVisitor);
        if (convertedPostAgg == null) {
            if (operandCounter == 0) {
                if (!operand.isA(SqlKind.LITERAL)) {
                    return null;
                }
                try {
                    lgK = RexLiteral.intValue(operand);
                } catch (ClassCastException re) {
                    return null;
                }
            } else if (operandCounter == 1) {
                if (!operand.isA(SqlKind.LITERAL)) {
                    return null;
                }
                // both lgK and tgtHllType must be specified
                if (lgK == null) {
                    return null;
                }
                try {
                    tgtHllType = RexLiteral.stringValue(operand);
                } catch (ClassCastException re) {
                    return null;
                }
            } else {
                return null;
            }
        } else {
            inputPostAggs.add(convertedPostAgg);
            operandCounter++;
        }
    }
    return new HllSketchUnionPostAggregator(postAggregatorVisitor.getOutputNamePrefix() + postAggregatorVisitor.getAndIncrementCounter(), inputPostAggs, lgK, tgtHllType);
}
Also used : RexCall(org.apache.calcite.rex.RexCall) HllSketchUnionPostAggregator(org.apache.druid.query.aggregation.datasketches.hll.HllSketchUnionPostAggregator) PostAggregator(org.apache.druid.query.aggregation.PostAggregator) ArrayList(java.util.ArrayList) HllSketchUnionPostAggregator(org.apache.druid.query.aggregation.datasketches.hll.HllSketchUnionPostAggregator) RexNode(org.apache.calcite.rex.RexNode) Nullable(javax.annotation.Nullable)

Aggregations

PostAggregator (org.apache.druid.query.aggregation.PostAggregator)135 Test (org.junit.Test)98 FieldAccessPostAggregator (org.apache.druid.query.aggregation.post.FieldAccessPostAggregator)48 ConstantPostAggregator (org.apache.druid.query.aggregation.post.ConstantPostAggregator)41 HashMap (java.util.HashMap)29 DefaultObjectMapper (org.apache.druid.jackson.DefaultObjectMapper)21 AggregatorFactory (org.apache.druid.query.aggregation.AggregatorFactory)20 Nullable (javax.annotation.Nullable)16 Aggregator (org.apache.druid.query.aggregation.Aggregator)13 Comparator (java.util.Comparator)12 RexCall (org.apache.calcite.rex.RexCall)11 RexNode (org.apache.calcite.rex.RexNode)11 CountAggregator (org.apache.druid.query.aggregation.CountAggregator)10 ArrayList (java.util.ArrayList)9 TestDoubleColumnSelectorImpl (org.apache.druid.query.aggregation.TestDoubleColumnSelectorImpl)9 Map (java.util.Map)8 DimensionSpec (org.apache.druid.query.dimension.DimensionSpec)8 Function (com.google.common.base.Function)7 InitializedNullHandlingTest (org.apache.druid.testing.InitializedNullHandlingTest)7 List (java.util.List)6