add Int64() function and include case of int64

This commit is contained in:
Maxton Huff 2021-03-25 16:27:55 -05:00
parent d3438e8a80
commit 61beef7e5a
2 changed files with 25 additions and 6 deletions

View file

@ -1303,11 +1303,16 @@ func (e *executor) executePercentile(ctx context.Context, qcx *Qcx, index string
defer span.Finish()
// get nth
var nth float64
var nthFloat float64
if nthArg, ok := c.Args["nth"].(pql.Decimal); ok {
nth = nthArg.Float64()
if nth < 0 || nth > 100.0 {
return ValCount{}, errors.Errorf("Percentile(): invalid nth value(%f), should be >= 0 and <= 100", nth)
switch c.Args["nth"].(type) {
case pql.Decimal:
nthFloat = nthArg.Float64()
case int64:
nthFloat = float64(nthArg.Int64())
}
if nthFloat < 0 || nthFloat > 100.0 {
return ValCount{}, errors.Errorf("Percentile(): invalid nth value(%f), should be >= 0 and <= 100", nthFloat)
}
} else {
return ValCount{}, errors.New("Percentile(): nth required")
@ -1337,7 +1342,7 @@ func (e *executor) executePercentile(ctx context.Context, qcx *Qcx, index string
if err != nil {
return ValCount{}, errors.Wrap(err, "executing Min call for Percentile")
}
if nth == 0.0 {
if nthFloat == 0.0 {
return ValCount{Val: minVal.Val, Count: minVal.Count}, nil
}
@ -1365,7 +1370,7 @@ func (e *executor) executePercentile(ctx context.Context, qcx *Qcx, index string
rangeCall = intersectCall.Children[0]
}
k := (100 - nth) / nth
k := (100 - nthFloat) / nthFloat
min, max := minVal.Val, maxVal.Val
// estimate nth val, eg median when nth=0.5

View file

@ -245,6 +245,20 @@ func (d Decimal) Float64() float64 {
return ret
}
// Int64 returns d as a int64.
// TODO: this could very easily lose precision; we should audit
// its use and protect against unexpected results.
func (d Decimal) Int64() int64 {
var ret int64
if d.Scale == 0 {
ret = int64(d.Value)
} else {
temp := float64(d.Value) / math.Pow10(int(d.Scale))
ret = int64(temp)
}
return ret
}
// String returns the string representation of the decimal.
func (d Decimal) String() string {
var s string