Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contributing Moving-Average Query to open source. #6430

Merged
merged 55 commits into from
Apr 27, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
8abe7a5
Contributing Moving-Average Query to open source.
yurmix Oct 8, 2018
ef7e0e6
Fix failing code inspections.
yurmix Oct 9, 2018
136eb93
See if explicit types will invoke the correct comparison function.
yurmix Oct 9, 2018
1d98847
Explicitly remove support for druid.generic.useDefaultValueForNull co…
yurmix Oct 10, 2018
63cf6ee
Merge branch 'master' into moving-average-query
yurmix Jan 30, 2019
7bbfc6d
Merge branch 'master' into moving-average-query
yurmix Jan 30, 2019
7b71a26
Update styling and headers for complience.
yurmix Jan 30, 2019
6635751
Refresh code with latest master changes:
yurmix Jan 30, 2019
b731780
Small checkstyle fix.
yurmix Jan 30, 2019
95b803a
Checkstyle fixes.
yurmix Jan 31, 2019
a21a3ce
Fixing rat errors; Teamcity errors.
yurmix Jan 31, 2019
66daabf
Merge branch 'master' into moving-average-query
yurmix Feb 23, 2019
9591a9d
Removing support theta sketches. Will be added back in this pr or a f…
yurmix Feb 25, 2019
84e48dd
Implements some of the review fixes.
yurmix Feb 26, 2019
0d24960
Contributing Moving-Average Query to open source.
yurmix Oct 8, 2018
0e34829
Fix failing code inspections.
yurmix Oct 9, 2018
ae75015
See if explicit types will invoke the correct comparison function.
yurmix Oct 9, 2018
542ecd2
Explicitly remove support for druid.generic.useDefaultValueForNull co…
yurmix Oct 10, 2018
4a04cc7
Update styling and headers for complience.
yurmix Jan 30, 2019
2772206
Refresh code with latest master changes:
yurmix Jan 30, 2019
f245403
Small checkstyle fix.
yurmix Jan 30, 2019
70c14a3
Checkstyle fixes.
yurmix Jan 31, 2019
3e349f7
Fixing rat errors; Teamcity errors.
yurmix Jan 31, 2019
516d40b
Removing support theta sketches. Will be added back in this pr or a f…
yurmix Feb 25, 2019
208af0d
Implements some of the review fixes.
yurmix Feb 26, 2019
239bde9
More fixes for review.
yurmix Feb 27, 2019
291cc18
empty merge
yurmix Feb 27, 2019
8157fce
More fixes from review.
yurmix Feb 27, 2019
4f0fbf0
MapBasedRow is Unmodifiable. Create new rows instead of modifying exi…
yurmix Mar 18, 2019
5221a83
Remove more changes related to datasketches support.
yurmix Mar 18, 2019
e2a5317
Refactor BaseAverager startFrom field and add a comment.
yurmix Mar 18, 2019
4b425b2
fakeEvents field: Refactor initialization and add comment.
yurmix Mar 18, 2019
8244353
Rename parameters (tiny change).
yurmix Mar 18, 2019
f10a343
Fix variable name typo in test (JAN_4).
yurmix Mar 18, 2019
c4ad4f5
Fix styling of non camelCase fields.
yurmix Mar 18, 2019
6210196
Fix Preconditions.checkArgument for cycleSize.
yurmix Mar 18, 2019
3ffa46c
Add more documentation to RowBucketIterable and other classes.
yurmix Mar 18, 2019
5f0bbb4
key/value comment on in MovingAverageIterable.
yurmix Mar 18, 2019
0ff63a5
Fix anonymous makeColumnValueSelector returning null.
yurmix Mar 18, 2019
e304b16
Merge branch 'master' into moving-average-query
yurmix Mar 18, 2019
e313a2b
Replace IdentityYieldingAccumolator with Yielders.each().
yurmix Mar 19, 2019
6708720
* internalNext() should return null instead of throwing exception.
yurmix Mar 19, 2019
fa3fbbc
Harden MovingAverageIterableTest (Switch anyOf to exact match).
yurmix Mar 19, 2019
304c43d
Change internalNext() from recursion to iteration; Simplify next() an…
yurmix Mar 19, 2019
81d0909
Remove unused imports.
yurmix Mar 19, 2019
716665e
Merge branch 'master' into moving-average-query
yurmix Apr 10, 2019
1c577ae
Address review comments.
yurmix Apr 19, 2019
ab1ae00
Rename fakeEvents to emptyEvents.
yurmix Apr 19, 2019
295916d
Merge branch 'master' into moving-average-query
yurmix Apr 19, 2019
f26c2f6
Merge branch 'master' of github.com:apache/incubator-druid into movin…
yurmix Apr 24, 2019
7b6f56e
Remove redundant parameter key from computeMovingAverage.
yurmix Apr 25, 2019
167efa2
Merge branch 'master' of github.com:apache/incubator-druid into movin…
yurmix Apr 25, 2019
8fccf19
Check yielder as well in RowBucketIterable#hasNext()
yurmix Apr 26, 2019
001b061
Fix javadoc.
yurmix Apr 26, 2019
ac43cc0
Merge branch 'master' of github.com:apache/incubator-druid into movin…
yurmix Apr 26, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@
<argument>org.apache.druid.extensions.contrib:materialized-view-maintenance</argument>
<argument>-c</argument>
<argument>org.apache.druid.extensions.contrib:materialized-view-selection</argument>
<argument>-c</argument>
<argument>org.apache.druid.extensions.contrib:druid-moving-average-query</argument>
</arguments>
</configuration>
</execution>
Expand Down
335 changes: 335 additions & 0 deletions docs/content/development/extensions-contrib/moving-average-query.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,335 @@
---
layout: doc_page
---

# Moving Average Queries

## Overview
**Moving Average Query** is an extension which provides support for [Moving Average](https://en.wikipedia.org/wiki/Moving_average) and other Aggregate [Window Functions](https://en.wikibooks.org/wiki/Structured_Query_Language/Window_functions) in Druid queries.

These Aggregate Window Functions consume standard Druid Aggregators and outputs additional windowed aggregates called [Averagers](#averagers).

#### High level algorithm

Moving Average encapsulates the [groupBy query](../../querying/groupbyquery.html) (Or [timeseries](../../querying/timeseriesquery.html) in case of no dimensions) in order to rely on the maturity of these query types.

It runs the query in two main phases:
1. Runs an inner [groupBy](../../querying/groupbyquery.html) or [timeseries](../../querying/timeseriesquery.html) query to compute Aggregators (i.e. daily count of events).
2. Passes over aggregated results in Broker, in order to compute Averagers (i.e. moving 7 day average of the daily count).

#### Main enhancements provided by this extension:
1. Functionality: Extending druid query functionality (i.e. initial introduction of Window Functions).
2. Performance: Improving performance of such moving aggregations by eliminating multiple segment scans.

#### Further reading
[Moving Average](https://en.wikipedia.org/wiki/Moving_average)

[Window Functions](https://en.wikibooks.org/wiki/Structured_Query_Language/Window_functions)

[Analytic Functions](https://cloud.google.com/bigquery/docs/reference/standard-sql/analytic-function-concepts)


## Operations
To use this extension, make sure to [load](../../operations/including-extensions.html) `druid-moving-average-query` only to the Broker.

##Configuration
There are currently no configuration properties specific to Moving Average.

##Query spec:
* Most properties in the For the query spec derived from [groupBy query](../../querying/groupbyquery.html) / [timeseries](../../querying/timeseriesquery.html), see documentation for these query types.
yurmix marked this conversation as resolved.
Show resolved Hide resolved
* Note: movingAverage is missing support for the following groupBy properties: `subtotalsSpec`, `virtualColumns`.
* Note: movingAverage is missing support for the following timeseries properties: `descending`.

|property|description|required?|
|--------|-----------|---------|
|queryType|This String should always be "movingAverage"; this is the first thing Druid looks at to figure out how to interpret the query|yes|
|dataSource|A String or Object defining the data source to query, very similar to a table in a relational database. See [DataSource](../../querying/datasource.html) for more information |yes|
|dimensions|A JSON list of dimensions to do the groupBy over; can be ommitted for using timeseries back-engine; or see [DimensionSpec](../../querying/dimensionspecs.html) for ways to extract dimensions |no|
yurmix marked this conversation as resolved.
Show resolved Hide resolved
|limitSpec|See [LimitSpec](../../querying/limitspec.html).|no|
|having|See [Having](../../querying/having.html).|no|
|granularity|Granularity type must be:`period`; See [Granularities](../../querying/granularities.html)|yes|
yurmix marked this conversation as resolved.
Show resolved Hide resolved
|filter|See [Filters](../../querying/filters.html)|no|
|aggregations|Aggregations forms the input to Averagers; See [Aggregations](../../querying/aggregations.html)|yes|
|postAggregations|Supports only aggregations as input; See [Post Aggregations](../../querying/post-aggregations.html)|no|
|intervals|A JSON Object representing ISO-8601 Intervals. This defines the time ranges to run the query over.|yes|
|context|An additional JSON Object which can be used to specify certain flags.|no|
|averagers|Defines the moving average function; See [Aggregations](../../querying/aggregations.html)|yes|
yurmix marked this conversation as resolved.
Show resolved Hide resolved
|postAveragers|Support input of both averagers and aggregations; Syntax is identical to postAggregations (See [Post Aggregations](../../querying/post-aggregations.html))|no|

##Averagers

Averagers are used to define the Moving-Average function. Averagers are not limited to an average - they can also provide other types of window functions such as MAX()/MIN().

### Properties

These are properties which are common to all Averagers:

|property|description|required?|
|--------|-----------|---------|
|type|Averager type; See [Averager types](#averager-types)|yes|
|name|Averager name|yes|
|fieldName|Input name (An aggregation name)|yes|
|buckets|Number of lookback buckets (time periods), including current one. Must be >0|yes|
|cycleSize|Cycle size; Used to calculate day-of-week option; See [Cycle size (Day of Week)](#cycle-size-day-of-week)|no, defaults to 1|


###Averager types:

* [Standard averagers](#standard-averagers):
* doubleMean
* doubleMeanNoNulls
* doubleMax
* doubleMin
* longMean
* longMeanNoNulls
* longMax
* longMin
* [Sketch averagers](#sketch-averagers):
* sketchUnion

#### Standard averagers

These averagers offer four functions:
* Mean (Average)
* MeanNoNulls (Ignores empty buckets).
* Max
* Min

**Ignoring nulls**:
Using a MeanNoNulls averager is useful when the interval starts at the dataset beginning time.
In that case, the first records will ignore missing buckets and average won't be artificially low.
However, this also means that empty days in a sparse dataset will also be ignored.

Example of usage:
```json
{ "type" : "doubleMean", "name" : <output_name>, "fieldName": <input_name> }
```

#### Sketch averagers

Sketch averager are meant to perform merge operations on [DataSketches](../extensions-core/datasketches-extension.html) (When using a Sketch averager, please include the [DataSketches](../extensions-core/datasketches-extension.html) extension as well).

Extra properties for Sketch averagers:

|property|description|required?|
|--------|-----------|---------|
|size|Sketch size; See [DataSketches aggregator](../extensions-core/datasketches-aggregators.html)|no, defaults to 4096|

Available functions:
* sketchUnion

Example of usage:
```json
{ "type" : "sketchUnion", "name" : <output_name>, "fieldName": <input_name> }
```

### Cycle size (Day of Week)
yurmix marked this conversation as resolved.
Show resolved Hide resolved
This optional parameter is used to calculate over a single bucket within each cycle instead of all buckets.
A prime example would be weekly buckets, resulting in a Day of Week calculation. (Other examples: Month of year, Hour of day).

I.e. when using these parameters:
* *granularity*: period=P1D (daily)
* *buckets*: 28
* *cycleSize*: 7

Within each output record, the averager will compute the result over the following buckets: current (#0), #7, #14, #21.
Whereas without specifying cycleSize it would have computed over all 28 buckets.

## Examples

All examples are based on the Wikipedia dataset provided in the Druid [tutorials](../../tutorials/index.html).

### Basic example

Calculating a 7-buckets moving average for Wikipedia edit deltas.

Query syntax:
```json
{
"queryType": "movingAverage",
"dataSource": "wikipedia",
"granularity": {
"type": "period",
"period": "PT30M"
},
"intervals": [
"2015-09-12T00:00:00Z/2015-09-13T00:00:00Z"
],
"aggregations": [
{
"name": "delta30Min",
"fieldName": "delta",
"type": "longSum"
}
],
"averagers": [
{
"name": "trailing30MinChanges",
"fieldName": "delta30Min",
"type": "longMean",
"buckets": 7
}
]
}
```

Result:
```json
[ {
"version" : "v1",
"timestamp" : "2015-09-12T00:30:00.000Z",
"event" : {
"delta30Min" : 30490,
"trailing30MinChanges" : 4355.714285714285
}
}, {
"version" : "v1",
"timestamp" : "2015-09-12T01:00:00.000Z",
"event" : {
"delta30Min" : 96526,
"trailing30MinChanges" : 18145.14285714286
}
}, {
...
...
...
}, {
"version" : "v1",
"timestamp" : "2015-09-12T23:00:00.000Z",
"event" : {
"delta30Min" : 119100,
"trailing30MinChanges" : 198697.2857142857
}
}, {
"version" : "v1",
"timestamp" : "2015-09-12T23:30:00.000Z",
"event" : {
"delta30Min" : 177882,
"trailing30MinChanges" : 193890.0
}
}
```

### Post averager example

Calculating a 7-buckets moving average for Wikipedia edit deltas, plus a ratio between the current period and the moving average.

Query syntax:
```json
{
"queryType": "movingAverage",
"dataSource": "wikipedia",
"granularity": {
"type": "period",
"period": "PT30M"
},
"intervals": [
"2015-09-12T22:00:00Z/2015-09-13T00:00:00Z"
],
"aggregations": [
{
"name": "delta30Min",
"fieldName": "delta",
"type": "longSum"
}
],
"averagers": [
{
"name": "trailing30MinChanges",
"fieldName": "delta30Min",
"type": "longMean",
"buckets": 7
}
],
"postAveragers" : [
{
"name": "ratioTrailing30MinChanges",
"type": "arithmetic",
"fn": "/",
"fields": [
{
"type": "fieldAccess",
"fieldName": "delta30Min"
},
{
"type": "fieldAccess",
"fieldName": "trailing30MinChanges"
}
]
}
]
}
```

Result:
```json
[ {
"version" : "v1",
"timestamp" : "2015-09-12T22:00:00.000Z",
"event" : {
"delta30Min" : 144269,
"trailing30MinChanges" : 204088.14285714287,
"ratioTrailing30MinChanges" : 0.7068955500319539
}
}, {
"version" : "v1",
"timestamp" : "2015-09-12T22:30:00.000Z",
"event" : {
"delta30Min" : 242860,
"trailing30MinChanges" : 214031.57142857142,
"ratioTrailing30MinChanges" : 1.134692411867141
}
}, {
"version" : "v1",
"timestamp" : "2015-09-12T23:00:00.000Z",
"event" : {
"delta30Min" : 119100,
"trailing30MinChanges" : 198697.2857142857,
"ratioTrailing30MinChanges" : 0.5994042624782422
}
}, {
"version" : "v1",
"timestamp" : "2015-09-12T23:30:00.000Z",
"event" : {
"delta30Min" : 177882,
"trailing30MinChanges" : 193890.0,
"ratioTrailing30MinChanges" : 0.9174377224199288
}
} ]
```


### Cycle size example

Calculating an average of every first 10-minutes of the last 3 hours:

Query syntax:
```json
{
"queryType": "movingAverage",
"dataSource": "wikipedia",
"granularity": {
"type": "period",
"period": "PT10M"
},
"intervals": [
"2015-09-12T00:00:00Z/2015-09-13T00:00:00Z"
],
"aggregations": [
{
"name": "delta10Min",
"fieldName": "delta",
"type": "doubleSum"
}
],
"averagers": [
{
"name": "trailing10MinPerHourChanges",
"fieldName": "delta10Min",
"type": "doubleMeanNoNulls",
"buckets": 18,
"cycleSize": 6
}
]
}
```
1 change: 1 addition & 0 deletions docs/content/development/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ All of these community extensions can be downloaded using *pull-deps* with the c
|kafka-emitter|Kafka metrics emitter|[link](../development/extensions-contrib/kafka-emitter.html)|
|druid-thrift-extensions|Support thrift ingestion |[link](../development/extensions-contrib/thrift.html)|
|druid-opentsdb-emitter|OpenTSDB metrics emitter |[link](../development/extensions-contrib/opentsdb-emitter.html)|
|druid-moving-average-query|Support for [Moving Average](https://en.wikipedia.org/wiki/Moving_average) and other Aggregate [Window Functions](https://en.wikibooks.org/wiki/Structured_Query_Language/Window_functions) in Druid queries.|[link](../development/extensions-contrib/moving-average-query.html)|

## Promoting Community Extension to Core Extension

Expand Down
16 changes: 16 additions & 0 deletions extensions-contrib/moving-average-query/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
druid-moving-average-query
=============

Overview
=============
**Moving Average Query** is an extension which provides support for [Moving Average](https://en.wikipedia.org/wiki/Moving_average) and other Aggregate [Window Functions](https://en.wikibooks.org/wiki/Structured_Query_Language/Window_functions) in Druid queries.

Documentation
=============
See the druid.io website or under [Druid Github Repo](https://github.com/apache/incubator-druid/tree/master/docs/content/development/extensions-contrib/moving-average-query.md).

Thanks
===========

This module was created by [Will Lauer](https://github.com/will-lauer) and [Mangesh Pardeshi](https://github.com/mangesh-pardeshi), with additional contributions by: [Deepak Babu](https://github.com/deepakb91), [Asif Mansoor](https://github.com/asifmansoora), [Ke Wang](https://github.com/kenuiuc), [Eyal Yurman](https://github.com/yurmix).
yurmix marked this conversation as resolved.
Show resolved Hide resolved
Thanks to all contributors!
Loading