Skip to content

Commit

Permalink
[Aggreagate] Poroperly return nil for STRING_AGG when called with…
Browse files Browse the repository at this point in the history
… null or 0-length array (#154)
  • Loading branch information
ohaibbq authored Mar 9, 2024
1 parent 6a17ac7 commit 4a17cdc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/function_aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,13 +494,19 @@ func (f *STRING_AGG) Done() (Value, error) {
f.values = f.values[:minLen]
}
values := make([]string, 0, len(f.values))

foundNotNilValue := false
for _, v := range f.values {
text, err := v.Value.ToString()
if err != nil {
return nil, err
}
foundNotNilValue = true
values = append(values, text)
}
if !foundNotNilValue {
return nil, nil
}
return StringValue(strings.Join(values, f.delim)), nil
}

Expand Down
10 changes: 10 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,16 @@ SELECT LOGICAL_OR(x) AS logical_or FROM toks`,
query: `SELECT STRING_AGG(fruit) AS string_agg FROM UNNEST(["apple", NULL, "pear", "banana", "pear"]) AS fruit`,
expectedRows: [][]interface{}{{"apple,pear,banana,pear"}},
},
{
name: "string_agg with length 0",
query: `SELECT STRING_AGG(fruit) FROM UNNEST(ARRAY<STRING>[]) fruit;`,
expectedRows: [][]interface{}{{nil}},
},
{
name: "string_agg with null",
query: `SELECT STRING_AGG(null) FROM UNNEST(ARRAY<STRING>[]);`,
expectedRows: [][]interface{}{{nil}},
},
{
name: "string_agg with delimiter",
query: `SELECT STRING_AGG(fruit, " & ") AS string_agg FROM UNNEST(["apple", "pear", "banana", "pear"]) AS fruit`,
Expand Down

0 comments on commit 4a17cdc

Please sign in to comment.