@@ -13,21 +13,21 @@ import (
13
13
14
14
const (
15
15
median = iota
16
- quant_cont
17
- quant_disc
16
+ percentile_cont
17
+ percentile_disc
18
18
)
19
19
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 } }
22
22
}
23
23
24
- type quantile struct {
24
+ type percentile struct {
25
25
nums []float64
26
26
arg1 []byte
27
27
kind int
28
28
}
29
29
30
- func (q * quantile ) Step (ctx sqlite3.Context , arg ... sqlite3.Value ) {
30
+ func (q * percentile ) Step (ctx sqlite3.Context , arg ... sqlite3.Value ) {
31
31
if a := arg [0 ]; a .NumericType () != sqlite3 .NULL {
32
32
q .nums = append (q .nums , a .Float ())
33
33
}
@@ -36,7 +36,12 @@ func (q *quantile) Step(ctx sqlite3.Context, arg ...sqlite3.Value) {
36
36
}
37
37
}
38
38
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 ) {
40
45
if len (q .nums ) == 0 {
41
46
return
42
47
}
@@ -47,21 +52,21 @@ func (q *quantile) Value(ctx sqlite3.Context) {
47
52
floats []float64
48
53
)
49
54
if q .kind == median {
50
- float , err = getQuantile (q .nums , 0.5 , false )
55
+ float , err = getPercentile (q .nums , 0.5 , false )
51
56
ctx .ResultFloat (float )
52
57
} 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 )
54
59
ctx .ResultFloat (float )
55
60
} 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 )
57
62
ctx .ResultJSON (floats )
58
63
}
59
64
if err != nil {
60
- ctx .ResultError (fmt .Errorf ("quantile : %w" , err ))
65
+ ctx .ResultError (fmt .Errorf ("percentile : %w" , err ))
61
66
}
62
67
}
63
68
64
- func getQuantile (nums []float64 , pos float64 , disc bool ) (float64 , error ) {
69
+ func getPercentile (nums []float64 , pos float64 , disc bool ) (float64 , error ) {
65
70
if pos < 0 || pos > 1 {
66
71
return 0 , util .ErrorString ("invalid pos" )
67
72
}
@@ -77,9 +82,9 @@ func getQuantile(nums []float64, pos float64, disc bool) (float64, error) {
77
82
return math .FMA (f , m1 , - math .FMA (f , m0 , - m0 )), nil
78
83
}
79
84
80
- func getQuantiles (nums []float64 , pos []float64 , disc bool ) error {
85
+ func getPercentiles (nums []float64 , pos []float64 , disc bool ) error {
81
86
for i := range pos {
82
- v , err := getQuantile (nums , pos [i ], disc )
87
+ v , err := getPercentile (nums , pos [i ], disc )
83
88
if err != nil {
84
89
return err
85
90
}
0 commit comments