handle empty avg agg (#2316)

* handle empty avg agg
This commit is contained in:
Jacob Brinlee 2023-03-13 12:59:38 -05:00 committed by GitHub
parent c79cc3b7db
commit cd32cd7696
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View file

@ -269,7 +269,9 @@ func (i *pqlAggregateRowIter) Next(ctx context.Context) (types.Row, error) {
case *parser.DataTypeDecimal:
_, isAvg := i.aggregate.(*avgPlanExpression)
if isAvg {
if actualResult.DecimalVal == nil {
if actualResult.Count == 0 {
i.resultValue = nil
} else if actualResult.DecimalVal == nil {
average := float64(actualResult.Val) / float64(actualResult.Count)
daverage, err := pql.FromFloat64WithScale(average, int(t.Scale))
if err != nil {

View file

@ -394,6 +394,38 @@ var avgTests = TableTest{
),
Compare: CompareExactUnordered,
},
{
SQLs: sqls(
"SELECT avg(i1) AS avg_rows FROM avg_test WHERE i1 > 100",
),
ExpHdrs: hdrs(
hdr("avg_rows", featurebase.WireQueryField{
Type: dax.BaseTypeDecimal + "(4)",
BaseType: dax.BaseTypeDecimal,
TypeInfo: map[string]interface{}{"scale": int64(4)},
}),
),
ExpRows: rows(
row(nil),
),
Compare: CompareExactUnordered,
},
{
SQLs: sqls(
"SELECT avg(d1) AS avg_rows FROM avg_test WHERE d1 > 100.0",
),
ExpHdrs: hdrs(
hdr("avg_rows", featurebase.WireQueryField{
Type: dax.BaseTypeDecimal + "(4)",
BaseType: dax.BaseTypeDecimal,
TypeInfo: map[string]interface{}{"scale": int64(4)},
}),
),
ExpRows: rows(
row(nil),
),
Compare: CompareExactUnordered,
},
},
}