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
  • Loading branch information
ohaibbq committed Feb 1, 2024
1 parent 961ce06 commit fde770f
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 @@ -488,13 +488,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 @@ -823,6 +823,16 @@ SELECT ARRAY_CONCAT_AGG(x) AS array_concat_agg FROM (
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 fde770f

Please sign in to comment.