Skip to content

Commit

Permalink
adds array_agg (#1028)
Browse files Browse the repository at this point in the history
  • Loading branch information
brennanjl authored Sep 26, 2024
1 parent 4c84ae8 commit 55caa0e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/engine/integration/procedure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,17 @@ func Test_Procedures(t *testing.T) {
if array_cat($arr[:2], $arr[4:]) != [1, 2, 4] {
error('array_cat failed');
}
$count := 0;
for $row in select array_agg(a) as a2 from (select 1 as a union select 2 as a) as b {
$count := $count + 1;
if $row.a2 != [1, 2] {
error('array_agg failed');
}
}
if $count != 1 {
error('array_agg failed');
}
}`,
},
{
Expand Down
26 changes: 26 additions & 0 deletions parse/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,32 @@ var (
return fmt.Sprintf("max(%s)", inputs[0]), nil
},
},
"array_agg": {
ValidateArgs: func(args []*types.DataType) (*types.DataType, error) {
if len(args) != 1 {
return nil, wrapErrArgumentNumber(1, len(args))
}

if args[0].IsArray {
return nil, fmt.Errorf("expected argument to be a scalar, got %s", args[0].String())
}

a2 := args[0].Copy()
a2.IsArray = true
return a2, nil
},
IsAggregate: true,
PGFormat: func(inputs []string, distinct bool, star bool) (string, error) {
if star {
return "", errStar("array_agg")
}
if distinct {
return "array_agg(DISTINCT %s)", nil
}

return fmt.Sprintf("array_agg(%s ORDER BY %s)", inputs[0], inputs[0]), nil
},
},
}
)

Expand Down

0 comments on commit 55caa0e

Please sign in to comment.