jihoonson commented on a change in pull request #6430: Contributing Moving-Average Query to open source. URL: https://github.com/apache/incubator-druid/pull/6430#discussion_r260553468
########## File path: docs/content/development/extensions-contrib/moving-average-query.md ########## @@ -0,0 +1,357 @@ +--- +layout: doc_page +--- + +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one + ~ or more contributor license agreements. See the NOTICE file + ~ distributed with this work for additional information + ~ regarding copyright ownership. The ASF licenses this file + ~ to you under the Apache License, Version 2.0 (the + ~ "License"); you may not use this file except in compliance + ~ with the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, + ~ software distributed under the License is distributed on an + ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + ~ KIND, either express or implied. See the License for the + ~ specific language governing permissions and limitations + ~ under the License. + --> + +# 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. + +##Limitations +* movingAverage is missing support for the following groupBy properties: `subtotalsSpec`, `virtualColumns`. +* movingAverage is missing support for the following timeseries properties: `descending`. +* movingAverage is missing support for [SQL-compatible null handling](https://github.com/apache/incubator-druid/issues/4349) (So setting druid.generic.useDefaultValueForNull in configuration will give an error). + +##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. + +|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| +|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| +|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| +|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) Review comment: Oh, I think this is a cool feature. Please keep this documented. I could understand what it is from the description and the example, but the term was just not intuitive to me. Not sure what's a better name though. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
