@@ -13,21 +13,21 @@ import (
1313
1414const (
1515 median = iota
16- quant_cont
17- quant_disc
16+ percentile_cont
17+ percentile_disc
1818)
1919
20- func newQuantile (kind int ) func () sqlite3.AggregateFunction {
21- return func () sqlite3.AggregateFunction { return & quantile {kind : kind } }
20+ func newPercentile (kind int ) func () sqlite3.AggregateFunction {
21+ return func () sqlite3.AggregateFunction { return & percentile {kind : kind } }
2222}
2323
24- type quantile struct {
24+ type percentile struct {
2525 nums []float64
2626 arg1 []byte
2727 kind int
2828}
2929
30- func (q * quantile ) Step (ctx sqlite3.Context , arg ... sqlite3.Value ) {
30+ func (q * percentile ) Step (ctx sqlite3.Context , arg ... sqlite3.Value ) {
3131 if a := arg [0 ]; a .NumericType () != sqlite3 .NULL {
3232 q .nums = append (q .nums , a .Float ())
3333 }
@@ -36,7 +36,12 @@ func (q *quantile) Step(ctx sqlite3.Context, arg ...sqlite3.Value) {
3636 }
3737}
3838
39- func (q * quantile ) Value (ctx sqlite3.Context ) {
39+ func (q * percentile ) Inverse (ctx sqlite3.Context , arg ... sqlite3.Value ) {
40+ // Implementing inverse allows certain queries that don't really need it to succeed.
41+ ctx .ResultError (util .ErrorString ("percentile: may not be used as a window function" ))
42+ }
43+
44+ func (q * percentile ) Value (ctx sqlite3.Context ) {
4045 if len (q .nums ) == 0 {
4146 return
4247 }
@@ -47,21 +52,21 @@ func (q *quantile) Value(ctx sqlite3.Context) {
4752 floats []float64
4853 )
4954 if q .kind == median {
50- float , err = getQuantile (q .nums , 0.5 , false )
55+ float , err = getPercentile (q .nums , 0.5 , false )
5156 ctx .ResultFloat (float )
5257 } else if err = json .Unmarshal (q .arg1 , & float ); err == nil {
53- float , err = getQuantile (q .nums , float , q .kind == quant_disc )
58+ float , err = getPercentile (q .nums , float , q .kind == percentile_disc )
5459 ctx .ResultFloat (float )
5560 } else if err = json .Unmarshal (q .arg1 , & floats ); err == nil {
56- err = getQuantiles (q .nums , floats , q .kind == quant_disc )
61+ err = getPercentiles (q .nums , floats , q .kind == percentile_disc )
5762 ctx .ResultJSON (floats )
5863 }
5964 if err != nil {
60- ctx .ResultError (fmt .Errorf ("quantile : %w" , err ))
65+ ctx .ResultError (fmt .Errorf ("percentile : %w" , err ))
6166 }
6267}
6368
64- func getQuantile (nums []float64 , pos float64 , disc bool ) (float64 , error ) {
69+ func getPercentile (nums []float64 , pos float64 , disc bool ) (float64 , error ) {
6570 if pos < 0 || pos > 1 {
6671 return 0 , util .ErrorString ("invalid pos" )
6772 }
@@ -77,9 +82,9 @@ func getQuantile(nums []float64, pos float64, disc bool) (float64, error) {
7782 return math .FMA (f , m1 , - math .FMA (f , m0 , - m0 )), nil
7883}
7984
80- func getQuantiles (nums []float64 , pos []float64 , disc bool ) error {
85+ func getPercentiles (nums []float64 , pos []float64 , disc bool ) error {
8186 for i := range pos {
82- v , err := getQuantile (nums , pos [i ], disc )
87+ v , err := getPercentile (nums , pos [i ], disc )
8388 if err != nil {
8489 return err
8590 }
0 commit comments