diff --git a/runtime/vam/expr/agg/count.go b/runtime/vam/expr/agg/count.go index d25f9d4fbb..8652fc9cd9 100644 --- a/runtime/vam/expr/agg/count.go +++ b/runtime/vam/expr/agg/count.go @@ -10,25 +10,20 @@ type count struct { } func (a *count) Consume(vec vector.Any) { - if c, ok := vec.(*vector.Const); ok { - val := c.Value() - if !val.IsNull() && !val.IsError() { - a.count += uint64(vec.Len()) - } + if c, ok := vec.(*vector.Const); ok && c.Value().IsNull() { return } if _, ok := vector.Under(vec).Type().(*super.TypeError); ok { return } - nulls := vector.NullsOf(vec) - if nulls == nil { - a.count += uint64(vec.Len()) - return - } - for i := range vec.Len() { - if !nulls.Value(i) { - a.count++ + if nulls := vector.NullsOf(vec); nulls != nil { + for i := range vec.Len() { + if !nulls.Value(i) { + a.count++ + } } + } else { + a.count += uint64(vec.Len()) } } diff --git a/runtime/vam/expr/aggregator.go b/runtime/vam/expr/aggregator.go index 4392c3fce4..27a5aa4b37 100644 --- a/runtime/vam/expr/aggregator.go +++ b/runtime/vam/expr/aggregator.go @@ -41,15 +41,19 @@ func (a *Aggregator) Eval(this vector.Any) vector.Any { func (a *Aggregator) apply(args ...vector.Any) vector.Any { vec, where := args[0], args[1] - var tags []uint32 - // If type is not bool then we want to filter everything. - if where.Type().ID() == super.IDBool { - for slot := uint32(0); slot < where.Len(); slot++ { - // XXX Feels like we should have a optimzed version of this. - if vector.BoolValue(where, slot) { - tags = append(tags, slot) - } + bools, _ := BoolMask(where) + if bools.IsEmpty() { + // everything is filtered. + return vector.NewConst(super.NewValue(vec.Type(), nil), vec.Len(), nil) + } + bools.Flip(0, uint64(vec.Len())) + if !bools.IsEmpty() { + nulls := vector.NewBoolEmpty(vec.Len(), nil) + bools.WriteDenseTo(nulls.Bits) + if origNulls := vector.NullsOf(vec); origNulls != nil { + nulls = vector.Or(nulls, origNulls) } + vector.SetNulls(vec, nulls) } - return vector.NewView(vec, tags) + return vec } diff --git a/runtime/ztests/op/summarize/count-where.yaml b/runtime/ztests/op/summarize/count-where.yaml new file mode 100644 index 0000000000..41b0246993 --- /dev/null +++ b/runtime/ztests/op/summarize/count-where.yaml @@ -0,0 +1,17 @@ +zed: | + summarize + num_requests := count() + where log_time >= 2012-10-01T00:00:00Z + by client_ip + | sort client_ip + +vector: true + +input: | + {log_time:2012-01-01T00:00:44Z,client_ip:249.92.17.134} + {log_time:2012-10-01T00:24:30Z,client_ip:249.92.17.134} + {log_time:2012-05-12T10:23:22Z,client_ip:251.58.48.137} + +output: | + {client_ip:249.92.17.134,num_requests:1(uint64)} + {client_ip:251.58.48.137,num_requests:0(uint64)}