Post-Aggregations

Post-aggregations are specifications of processing that should happen on aggregated values as they come out of Apache Druid. If you include a post aggregation as part of a query, make sure to include all aggregators the post-aggregator requires.

There are several post-aggregators available.

Arithmetic post-aggregator

The arithmetic post-aggregator applies the provided function to the given fields from left to right. The fields can be aggregators or other post aggregators.

Supported functions are +, -, *, /, and quotient.

Note:

  • / division always returns 0 if dividing by0, regardless of the numerator.
  • quotient division behaves like regular floating point division

Arithmetic post-aggregators may also specify an ordering, which defines the order of resulting values when sorting results (this can be useful for topN queries for instance):

  • If no ordering (or null) is specified, the default floating point ordering is used.
  • numericFirst ordering always returns finite values first, followed by NaN, and infinite values last.

The grammar for an arithmetic post aggregation is:

  1. postAggregation : {
  2. "type" : "arithmetic",
  3. "name" : <output_name>,
  4. "fn" : <arithmetic_function>,
  5. "fields": [<post_aggregator>, <post_aggregator>, ...],
  6. "ordering" : <null (default), or "numericFirst">
  7. }

Field accessor post-aggregators

These post-aggregators return the value produced by the specified aggregator.

fieldName refers to the output name of the aggregator given in the aggregations portion of the query. For complex aggregators, like “cardinality” and “hyperUnique”, the type of the post-aggregator determines what the post-aggregator will return. Use type “fieldAccess” to return the raw aggregation object, or use type “finalizingFieldAccess” to return a finalized value, such as an estimated cardinality.

  1. { "type" : "fieldAccess", "name": <output_name>, "fieldName" : <aggregator_name> }

or

  1. { "type" : "finalizingFieldAccess", "name": <output_name>, "fieldName" : <aggregator_name> }

Constant post-aggregator

The constant post-aggregator always returns the specified value.

  1. { "type" : "constant", "name" : <output_name>, "value" : <numerical_value> }

Greatest / Least post-aggregators

doubleGreatest and longGreatest computes the maximum of all fields and Double.NEGATIVE_INFINITY. doubleLeast and longLeast computes the minimum of all fields and Double.POSITIVE_INFINITY.

The difference between the doubleMax aggregator and the doubleGreatest post-aggregator is that doubleMax returns the highest value of all rows for one specific column while doubleGreatest returns the highest value of multiple columns in one row. These are similar to the SQL MAX and GREATEST functions.

Example:

  1. {
  2. "type" : "doubleGreatest",
  3. "name" : <output_name>,
  4. "fields": [<post_aggregator>, <post_aggregator>, ...]
  5. }

JavaScript post-aggregator

Applies the provided JavaScript function to the given fields. Fields are passed as arguments to the JavaScript function in the given order.

  1. postAggregation : {
  2. "type": "javascript",
  3. "name": <output_name>,
  4. "fieldNames" : [<aggregator_name>, <aggregator_name>, ...],
  5. "function": <javascript function>
  6. }

Example JavaScript aggregator:

  1. {
  2. "type": "javascript",
  3. "name": "absPercent",
  4. "fieldNames": ["delta", "total"],
  5. "function": "function(delta, total) { return 100 * Math.abs(delta) / total; }"
  6. }

JavaScript-based functionality is disabled by default. Please refer to the Druid JavaScript programming guide for guidelines about using Druid’s JavaScript functionality, including instructions on how to enable it.

HyperUnique Cardinality post-aggregator

The hyperUniqueCardinality post aggregator is used to wrap a hyperUnique object such that it can be used in post aggregations.

  1. {
  2. "type" : "hyperUniqueCardinality",
  3. "name": <output name>,
  4. "fieldName" : <the name field value of the hyperUnique aggregator>
  5. }

It can be used in a sample calculation as so:

  1. "aggregations" : [{
  2. {"type" : "count", "name" : "rows"},
  3. {"type" : "hyperUnique", "name" : "unique_users", "fieldName" : "uniques"}
  4. }],
  5. "postAggregations" : [{
  6. "type" : "arithmetic",
  7. "name" : "average_users_per_row",
  8. "fn" : "/",
  9. "fields" : [
  10. { "type" : "hyperUniqueCardinality", "fieldName" : "unique_users" },
  11. { "type" : "fieldAccess", "name" : "rows", "fieldName" : "rows" }
  12. ]
  13. }]

This post-aggregator will inherit the rounding behavior of the aggregator it references. Note that this inheritance is only effective if you directly reference an aggregator. Going through another post-aggregator, for example, will cause the user-specified rounding behavior to get lost and default to “no rounding”.

Example Usage

In this example, let’s calculate a simple percentage using post aggregators. Let’s imagine our data set has a metric called “total”.

The format of the query JSON is as follows:

  1. {
  2. ...
  3. "aggregations" : [
  4. { "type" : "count", "name" : "rows" },
  5. { "type" : "doubleSum", "name" : "tot", "fieldName" : "total" }
  6. ],
  7. "postAggregations" : [{
  8. "type" : "arithmetic",
  9. "name" : "average",
  10. "fn" : "/",
  11. "fields" : [
  12. { "type" : "fieldAccess", "name" : "tot", "fieldName" : "tot" },
  13. { "type" : "fieldAccess", "name" : "rows", "fieldName" : "rows" }
  14. ]
  15. }]
  16. ...
  17. }
  1. {
  2. ...
  3. "aggregations" : [
  4. { "type" : "doubleSum", "name" : "tot", "fieldName" : "total" },
  5. { "type" : "doubleSum", "name" : "part", "fieldName" : "part" }
  6. ],
  7. "postAggregations" : [{
  8. "type" : "arithmetic",
  9. "name" : "part_percentage",
  10. "fn" : "*",
  11. "fields" : [
  12. { "type" : "arithmetic",
  13. "name" : "ratio",
  14. "fn" : "/",
  15. "fields" : [
  16. { "type" : "fieldAccess", "name" : "part", "fieldName" : "part" },
  17. { "type" : "fieldAccess", "name" : "tot", "fieldName" : "tot" }
  18. ]
  19. },
  20. { "type" : "constant", "name": "const", "value" : 100 }
  21. ]
  22. }]
  23. ...
  24. }