From 8746444cdbfc037a37b7f051fbe4ae39213c7b31 Mon Sep 17 00:00:00 2001 From: Maxton Huff Date: Thu, 25 Mar 2021 16:57:08 -0500 Subject: [PATCH] add error message for invalid type --- executor.go | 7 +++++-- pql/decimal.go | 14 -------------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/executor.go b/executor.go index e4e41abbb..832ce3f0f 100644 --- a/executor.go +++ b/executor.go @@ -20,6 +20,7 @@ import ( "fmt" "math" "math/bits" + "reflect" "sort" "strings" "sync" @@ -1309,10 +1310,12 @@ func (e *executor) executePercentile(ctx context.Context, qcx *Qcx, index string case pql.Decimal: nthFloat = nthArg.Float64() case int64: - nthFloat = float64(nthArg.Int64()) + nthFloat = float64(nthArg.ToInt64(0)) + default: + return ValCount{}, errors.Errorf("Percentile(): invalid nth type (%f), should be int64 or pql.Decimal", reflect.TypeOf(nthArg)) } if nthFloat < 0 || nthFloat > 100.0 { - return ValCount{}, errors.Errorf("Percentile(): invalid nth value(%f), should be >= 0 and <= 100", nthFloat) + return ValCount{}, errors.Errorf("Percentile(): invalid nth value (%f), should be >= 0 and <= 100", nthFloat) } } else { return ValCount{}, errors.New("Percentile(): nth required") diff --git a/pql/decimal.go b/pql/decimal.go index b9d1e3749..61843f580 100644 --- a/pql/decimal.go +++ b/pql/decimal.go @@ -245,20 +245,6 @@ 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