Skip to content

Commit

Permalink
Ignore nulls when counting window values (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
ohaibbq committed Mar 2, 2024
1 parent 4be95a3 commit f53a6e2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
14 changes: 14 additions & 0 deletions internal/function_window.go
Expand Up @@ -72,6 +72,20 @@ func (f *WINDOW_AVG) Done(agg *WindowFuncAggregatedStatus) (Value, error) {
type WINDOW_COUNT struct {
}

func (f *WINDOW_COUNT) Step(values []Value, agg *WindowFuncAggregatedStatus) error {
if len(values) == 0 {
return nil
}

value := values[0]
if value == nil {
return nil
}

agg.Values = append(agg.Values, value)
return nil
}

func (f *WINDOW_COUNT) Done(agg *WindowFuncAggregatedStatus) (Value, error) {
values, err := agg.RelevantValues()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions query_test.go
Expand Up @@ -766,8 +766,8 @@ SELECT ARRAY_CONCAT_AGG(x) AS array_concat_agg FROM (
},
{
name: "count with null",
query: `SELECT COUNT(x) FROM UNNEST([NULL]) AS x`,
expectedRows: [][]interface{}{{int64(0)}},
query: `WITH toks AS (SELECT 1 AS x UNION ALL SELECT null) SELECT COUNT(x) FROM toks`,
expectedRows: [][]interface{}{{int64(1)}},
},
{
name: "count with if",
Expand Down

0 comments on commit f53a6e2

Please sign in to comment.