blob: 65aecbde805b96dc2781291516b671785d367b56 [file] [log] [blame]
"use strict";(self.webpackChunk=self.webpackChunk||[]).push([[3499],{2528:(e,n,i)=>{i.r(n),i.d(n,{assets:()=>l,contentTitle:()=>d,default:()=>h,frontMatter:()=>s,metadata:()=>a,toc:()=>o});const a=JSON.parse('{"id":"development/extensions-core/stats","title":"Stats aggregator","description":"\x3c!--","source":"@site/docs/32.0.0/development/extensions-core/stats.md","sourceDirName":"development/extensions-core","slug":"/development/extensions-core/stats","permalink":"/docs/32.0.0/development/extensions-core/stats","draft":false,"unlisted":false,"tags":[],"version":"current","frontMatter":{"id":"stats","title":"Stats aggregator"}}');var r=i(74848),t=i(28453);const s={id:"stats",title:"Stats aggregator"},d=void 0,l={},o=[{value:"Variance aggregator",id:"variance-aggregator",level:2},{value:"Variance and Standard Deviation SQL Aggregators",id:"variance-and-standard-deviation-sql-aggregators",level:3},{value:"Pre-aggregating variance at ingestion time",id:"pre-aggregating-variance-at-ingestion-time",level:3},{value:"Standard deviation post-aggregator",id:"standard-deviation-post-aggregator",level:3},{value:"Query examples:",id:"query-examples",level:2},{value:"Timeseries query",id:"timeseries-query",level:3},{value:"Druid SQL",id:"druid-sql",level:4},{value:"Native Query",id:"native-query",level:4},{value:"TopN query",id:"topn-query",level:3},{value:"Druid SQL",id:"druid-sql-1",level:4},{value:"Native Query",id:"native-query-1",level:4},{value:"GroupBy query",id:"groupby-query",level:3},{value:"Druid SQL",id:"druid-sql-2",level:4},{value:"Native Query",id:"native-query-2",level:4}];function c(e){const n={a:"a",admonition:"admonition",code:"code",h2:"h2",h3:"h3",h4:"h4",li:"li",p:"p",pre:"pre",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",ul:"ul",...(0,t.R)(),...e.components};return(0,r.jsxs)(r.Fragment,{children:[(0,r.jsxs)(n.p,{children:["This Apache Druid extension includes stat-related aggregators, including variance and standard deviations, etc. Make sure to ",(0,r.jsx)(n.a,{href:"/docs/32.0.0/configuration/extensions#loading-extensions",children:"include"})," ",(0,r.jsx)(n.code,{children:"druid-stats"})," in the extensions load list."]}),"\n",(0,r.jsx)(n.h2,{id:"variance-aggregator",children:"Variance aggregator"}),"\n",(0,r.jsx)(n.p,{children:"Algorithm of the aggregator is the same with that of apache hive. This is the description in GenericUDAFVariance in hive."}),"\n",(0,r.jsx)(n.p,{children:'Evaluate the variance using the algorithm described by Chan, Golub, and LeVeque in\n"Algorithms for computing the sample variance: analysis and recommendations"\nThe American Statistician, 37 (1983) pp. 242--247.'}),"\n",(0,r.jsx)(n.p,{children:"variance = variance1 + variance2 + n/(m*(m+n)) * pow(((m/n)*t1 - t2),2)"}),"\n",(0,r.jsx)(n.p,{children:"where:"}),"\n",(0,r.jsxs)(n.ul,{children:["\n",(0,r.jsx)(n.li,{children:"variance is sum(x-avg^2) (this is actually n times the variance)\nand is updated at every step."}),"\n",(0,r.jsx)(n.li,{children:"n is the count of elements in chunk1"}),"\n",(0,r.jsx)(n.li,{children:"m is the count of elements in chunk2"}),"\n",(0,r.jsx)(n.li,{children:"t1 is the sum of elements in chunk1"}),"\n",(0,r.jsx)(n.li,{children:"t2 is the sum of elements in chunk2"}),"\n"]}),"\n",(0,r.jsx)(n.p,{children:'This algorithm was proven to be numerically stable by J.L. Barlow in\n"Error analysis of a pairwise summation algorithm to compute sample variance"\nNumer. Math, 58 (1991) pp. 583--590'}),"\n",(0,r.jsxs)(n.admonition,{type:"info",children:[(0,r.jsxs)(n.p,{children:["As with all ",(0,r.jsx)(n.a,{href:"/docs/32.0.0/querying/sql-aggregations",children:"aggregators"}),', the order of operations across segments is\nnon-deterministic. This means that if this aggregator operates with an input type of "float" or "double", the result\nof the aggregation may not be precisely the same across multiple runs of the query.']}),(0,r.jsx)(n.p,{children:"To produce consistent results, round the variance to a fixed number of decimal places so that the results are\nprecisely the same across query runs."})]}),"\n",(0,r.jsx)(n.h3,{id:"variance-and-standard-deviation-sql-aggregators",children:"Variance and Standard Deviation SQL Aggregators"}),"\n",(0,r.jsx)(n.p,{children:"You can use the variance and standard deviation aggregation functions in the SELECT clause of any Druid SQL query."}),"\n",(0,r.jsxs)(n.table,{children:[(0,r.jsx)(n.thead,{children:(0,r.jsxs)(n.tr,{children:[(0,r.jsx)(n.th,{children:"Function"}),(0,r.jsx)(n.th,{children:"Notes"}),(0,r.jsx)(n.th,{children:"Default"})]})}),(0,r.jsxs)(n.tbody,{children:[(0,r.jsxs)(n.tr,{children:[(0,r.jsx)(n.td,{children:(0,r.jsx)(n.code,{children:"VAR_POP(expr)"})}),(0,r.jsxs)(n.td,{children:["Computes variance population of ",(0,r.jsx)(n.code,{children:"expr"}),"."]}),(0,r.jsx)(n.td,{children:(0,r.jsx)(n.code,{children:"null"})})]}),(0,r.jsxs)(n.tr,{children:[(0,r.jsx)(n.td,{children:(0,r.jsx)(n.code,{children:"VAR_SAMP(expr)"})}),(0,r.jsxs)(n.td,{children:["Computes variance sample of ",(0,r.jsx)(n.code,{children:"expr"}),"."]}),(0,r.jsx)(n.td,{children:(0,r.jsx)(n.code,{children:"null"})})]}),(0,r.jsxs)(n.tr,{children:[(0,r.jsx)(n.td,{children:(0,r.jsx)(n.code,{children:"VARIANCE(expr)"})}),(0,r.jsxs)(n.td,{children:["Computes variance sample of ",(0,r.jsx)(n.code,{children:"expr"}),"."]}),(0,r.jsx)(n.td,{children:(0,r.jsx)(n.code,{children:"null"})})]}),(0,r.jsxs)(n.tr,{children:[(0,r.jsx)(n.td,{children:(0,r.jsx)(n.code,{children:"STDDEV_POP(expr)"})}),(0,r.jsxs)(n.td,{children:["Computes standard deviation population of ",(0,r.jsx)(n.code,{children:"expr"}),"."]}),(0,r.jsx)(n.td,{children:(0,r.jsx)(n.code,{children:"null"})})]}),(0,r.jsxs)(n.tr,{children:[(0,r.jsx)(n.td,{children:(0,r.jsx)(n.code,{children:"STDDEV_SAMP(expr)"})}),(0,r.jsxs)(n.td,{children:["Computes standard deviation sample of ",(0,r.jsx)(n.code,{children:"expr"}),"."]}),(0,r.jsx)(n.td,{children:(0,r.jsx)(n.code,{children:"null"})})]}),(0,r.jsxs)(n.tr,{children:[(0,r.jsx)(n.td,{children:(0,r.jsx)(n.code,{children:"STDDEV(expr)"})}),(0,r.jsxs)(n.td,{children:["Computes standard deviation sample of ",(0,r.jsx)(n.code,{children:"expr"}),"."]}),(0,r.jsx)(n.td,{children:(0,r.jsx)(n.code,{children:"null"})})]})]})]}),"\n",(0,r.jsx)(n.h3,{id:"pre-aggregating-variance-at-ingestion-time",children:"Pre-aggregating variance at ingestion time"}),"\n",(0,r.jsx)(n.p,{children:'To use this feature, an "variance" aggregator must be included at indexing time.\nThe ingestion aggregator can only apply to numeric values. If you use "variance"\nthen any input rows missing the value will be considered to have a value of 0.'}),"\n",(0,r.jsx)(n.p,{children:'User can specify expected input type as one of "float", "double", "long", "variance" for ingestion, which is by default "float".'}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-json",children:'{\n "type" : "variance",\n "name" : <output_name>,\n "fieldName" : <metric_name>,\n "inputType" : <input_type>,\n "estimator" : <string>\n}\n'})}),"\n",(0,r.jsx)(n.p,{children:'To query for results, "variance" aggregator with "variance" input type or simply a "varianceFold" aggregator must be included in the query.'}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-json",children:'{\n "type" : "varianceFold",\n "name" : <output_name>,\n "fieldName" : <metric_name>,\n "estimator" : <string>\n}\n'})}),"\n",(0,r.jsxs)(n.table,{children:[(0,r.jsx)(n.thead,{children:(0,r.jsxs)(n.tr,{children:[(0,r.jsx)(n.th,{children:"Property"}),(0,r.jsx)(n.th,{children:"Description"}),(0,r.jsx)(n.th,{children:"Default"})]})}),(0,r.jsx)(n.tbody,{children:(0,r.jsxs)(n.tr,{children:[(0,r.jsx)(n.td,{children:(0,r.jsx)(n.code,{children:"estimator"})}),(0,r.jsx)(n.td,{children:'Set "population" to get variance_pop rather than variance_sample, which is default.'}),(0,r.jsx)(n.td,{children:"null"})]})})]}),"\n",(0,r.jsx)(n.h3,{id:"standard-deviation-post-aggregator",children:"Standard deviation post-aggregator"}),"\n",(0,r.jsx)(n.p,{children:'To acquire standard deviation from variance, user can use "stddev" post aggregator.'}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-json",children:'{\n "type": "stddev",\n "name": "<output_name>",\n "fieldName": "<aggregator_name>",\n "estimator": <string>\n}\n'})}),"\n",(0,r.jsx)(n.h2,{id:"query-examples",children:"Query examples:"}),"\n",(0,r.jsx)(n.h3,{id:"timeseries-query",children:"Timeseries query"}),"\n",(0,r.jsx)(n.h4,{id:"druid-sql",children:"Druid SQL"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-SQL",children:"SELECT \n DATE_TRUNC('day', __time),\n VARIANCE(\"index_var\") AS index_var\nFROM \"testing\"\nWHERE TIME_IN_INTERVAL(__time, '2013-03-01/2016-03-20')\nGROUP BY 1\n"})}),"\n",(0,r.jsx)(n.h4,{id:"native-query",children:"Native Query"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-json",children:'{\n "queryType": "timeseries",\n "dataSource": "testing",\n "granularity": "day",\n "aggregations": [\n {\n "type": "variance",\n "name": "index_var",\n "fieldName": "index_var"\n }\n ],\n "intervals": [\n "2016-03-01/2013-03-20"\n ]\n}\n'})}),"\n",(0,r.jsx)(n.h3,{id:"topn-query",children:"TopN query"}),"\n",(0,r.jsx)(n.h4,{id:"druid-sql-1",children:"Druid SQL"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-SQL",children:'SELECT\n alias,\n VARIANCE("index") AS index_var\nFROM "testing"\nWHERE TIME_IN_INTERVAL(__time, \'2016-03-06/2016-03-07\')\nGROUP BY 1\nORDER BY 2\nLIMIT 5\n'})}),"\n",(0,r.jsx)(n.h4,{id:"native-query-1",children:"Native Query"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-json",children:'{\n "queryType": "topN",\n "dataSource": "testing",\n "dimensions": ["alias"],\n "threshold": 5,\n "granularity": "all",\n "aggregations": [\n {\n "type": "variance",\n "name": "index_var",\n "fieldName": "index"\n }\n ],\n "postAggregations": [\n {\n "type": "stddev",\n "name": "index_stddev",\n "fieldName": "index_var"\n }\n ],\n "intervals": [\n "2016-03-06/2016-03-07"\n ]\n}\n'})}),"\n",(0,r.jsx)(n.h3,{id:"groupby-query",children:"GroupBy query"}),"\n",(0,r.jsx)(n.h4,{id:"druid-sql-2",children:"Druid SQL"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-SQL",children:'SELECT\n alias,\n VARIANCE("index") AS index_var\nFROM "testing"\nWHERE TIME_IN_INTERVAL(__time, \'2016-03-06/2016-03-07\')\nGROUP BY alias\n'})}),"\n",(0,r.jsx)(n.h4,{id:"native-query-2",children:"Native Query"}),"\n",(0,r.jsx)(n.pre,{children:(0,r.jsx)(n.code,{className:"language-json",children:'{\n "queryType": "groupBy",\n "dataSource": "testing",\n "dimensions": ["alias"],\n "granularity": "all",\n "aggregations": [\n {\n "type": "variance",\n "name": "index_var",\n "fieldName": "index"\n }\n ],\n "postAggregations": [\n {\n "type": "stddev",\n "name": "index_stddev",\n "fieldName": "index_var"\n }\n ],\n "intervals": [\n "2016-03-06/2016-03-07"\n ]\n}\n'})})]})}function h(e={}){const{wrapper:n}={...(0,t.R)(),...e.components};return n?(0,r.jsx)(n,{...e,children:(0,r.jsx)(c,{...e})}):c(e)}},28453:(e,n,i)=>{i.d(n,{R:()=>s,x:()=>d});var a=i(96540);const r={},t=a.createContext(r);function s(e){const n=a.useContext(t);return a.useMemo((function(){return"function"==typeof e?e(n):{...n,...e}}),[n,e])}function d(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(r):e.components||r:s(e.components),a.createElement(t.Provider,{value:n},e.children)}}}]);