-
-
Notifications
You must be signed in to change notification settings - Fork 39
Support for ORDER BY in array_agg #1515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
0984ac2
Tests for ordery by in array_agg
zachmu e44f9f5
Small simplification to func resolution
zachmu d20492e
Second pass at array_agg
zachmu b6ffd7b
Bug fixes
zachmu f9e920a
Mostly working order by
zachmu 6e528c4
bug fix
zachmu 06a6637
bug fixes and test fixes
zachmu 1acf7fe
Skip a couple tests
zachmu 71eef9c
New gms
zachmu 56bfe8b
Formatting
zachmu 60a43af
Update server/expression/array_agg.go
zachmu abd62ad
linting and comments
zachmu 6796c78
new gms
zachmu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| // Copyright 2025 Dolthub, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package expression | ||
|
|
||
| import ( | ||
| "sort" | ||
| "strings" | ||
|
|
||
| "github.com/dolthub/go-mysql-server/sql" | ||
| "github.com/dolthub/go-mysql-server/sql/expression" | ||
| vitess "github.com/dolthub/vitess/go/vt/sqlparser" | ||
|
|
||
| "github.com/dolthub/doltgresql/server/types" | ||
| ) | ||
|
|
||
| type ArrayAgg struct { | ||
| selectExprs []sql.Expression | ||
| orderBy sql.SortFields | ||
| id sql.ColumnId | ||
| } | ||
|
|
||
| var _ sql.Aggregation = (*ArrayAgg)(nil) | ||
| var _ vitess.Injectable = (*ArrayAgg)(nil) | ||
|
|
||
| // WithResolvedChildren returns a new ArrayAgg with the provided children as its select expressions. | ||
| // The last child is expected to be the order by expressions. | ||
| func (a *ArrayAgg) WithResolvedChildren(children []any) (any, error) { | ||
| a.selectExprs = make([]sql.Expression, len(children)-1) | ||
| for i := 0; i < len(children)-1; i++ { | ||
| a.selectExprs[i] = children[i].(sql.Expression) | ||
| } | ||
|
|
||
| a.orderBy = children[len(children)-1].(sql.SortFields) | ||
| return a, nil | ||
| } | ||
|
|
||
| func (a *ArrayAgg) Resolved() bool { | ||
| return expression.ExpressionsResolved(a.selectExprs...) && expression.ExpressionsResolved(a.orderBy.ToExpressions()...) | ||
| } | ||
|
|
||
| func (a *ArrayAgg) String() string { | ||
| sb := strings.Builder{} | ||
| sb.WriteString("group_concat(") | ||
|
|
||
| if a.selectExprs != nil { | ||
| var exprs = make([]string, len(a.selectExprs)) | ||
| for i, expr := range a.selectExprs { | ||
| exprs[i] = expr.String() | ||
| } | ||
|
|
||
| sb.WriteString(strings.Join(exprs, ", ")) | ||
| } | ||
|
|
||
| if len(a.orderBy) > 0 { | ||
| sb.WriteString(" order by ") | ||
| for i, ob := range a.orderBy { | ||
| if i > 0 { | ||
| sb.WriteString(", ") | ||
| } | ||
| sb.WriteString(ob.String()) | ||
| } | ||
| } | ||
|
|
||
| sb.WriteString(")") | ||
| return sb.String() | ||
| } | ||
|
|
||
| func (a *ArrayAgg) Type() sql.Type { | ||
| dt := a.selectExprs[0].Type().(*types.DoltgresType) | ||
| return dt.ToArrayType() | ||
| } | ||
|
|
||
| func (a *ArrayAgg) IsNullable() bool { | ||
| return true | ||
| } | ||
|
|
||
| func (a *ArrayAgg) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { | ||
| panic("eval should never be called on an aggregation function") | ||
| } | ||
|
|
||
| func (a *ArrayAgg) Children() []sql.Expression { | ||
| return append(a.selectExprs, a.orderBy.ToExpressions()...) | ||
| } | ||
|
|
||
| func (a ArrayAgg) WithChildren(children ...sql.Expression) (sql.Expression, error) { | ||
| if len(children) != len(a.selectExprs)+len(a.orderBy) { | ||
| return nil, sql.ErrInvalidChildrenNumber.New(a, len(children), len(a.selectExprs)+len(a.orderBy)) | ||
| } | ||
|
|
||
| a.selectExprs = children[:len(a.selectExprs)] | ||
| a.orderBy = a.orderBy.FromExpressions(children[len(a.selectExprs):]...) | ||
| return &a, nil | ||
| } | ||
|
|
||
| func (a *ArrayAgg) Id() sql.ColumnId { | ||
| return a.id | ||
| } | ||
|
|
||
| func (a ArrayAgg) WithId(id sql.ColumnId) sql.IdExpression { | ||
| a.id = id | ||
| return &a | ||
| } | ||
|
|
||
| func (a *ArrayAgg) NewWindowFunction() (sql.WindowFunction, error) { | ||
| panic("window functions not yet supported for array_agg") | ||
| } | ||
|
|
||
| func (a *ArrayAgg) WithWindow(window *sql.WindowDefinition) sql.WindowAdaptableExpression { | ||
| panic("window functions not yet supported for array_agg") | ||
| } | ||
|
|
||
| func (a *ArrayAgg) Window() *sql.WindowDefinition { | ||
| return nil | ||
| } | ||
|
|
||
| func (a *ArrayAgg) NewBuffer() (sql.AggregationBuffer, error) { | ||
| return &arrayAggBuffer{ | ||
| elements: make([]sql.Row, 0), | ||
| a: a, | ||
| }, nil | ||
| } | ||
|
|
||
| type arrayAggBuffer struct { | ||
| elements []sql.Row | ||
| a *ArrayAgg | ||
| } | ||
|
|
||
| func newArrayAggBuffer(a *ArrayAgg) (sql.AggregationBuffer, error) { | ||
| return &arrayAggBuffer{ | ||
| elements: make([]sql.Row, 0), | ||
| a: a, | ||
| }, nil | ||
| } | ||
|
|
||
| func (a *arrayAggBuffer) Dispose() {} | ||
|
|
||
| func (a *arrayAggBuffer) Eval(ctx *sql.Context) (interface{}, error) { | ||
| if len(a.elements) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| if a.a.orderBy != nil { | ||
| sorter := &expression.Sorter{ | ||
| SortFields: a.a.orderBy, | ||
| Rows: a.elements, | ||
| Ctx: ctx, | ||
| } | ||
|
|
||
| sort.Stable(sorter) | ||
| if sorter.LastError != nil { | ||
| return nil, sorter.LastError | ||
| } | ||
| } | ||
|
|
||
| // convert to []interface for return. The last element in each row is the one we want to return, the rest are sort fields. | ||
| result := make([]interface{}, len(a.elements)) | ||
| for i, row := range a.elements { | ||
| result[i] = row[(len(row) - 1)] | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
|
|
||
| func (a *arrayAggBuffer) Update(ctx *sql.Context, row sql.Row) error { | ||
| evalRow, err := evalExprs(ctx, a.a.selectExprs, row) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| // TODO: unwrap values as necessary | ||
| // Append the current value to the end of the row. We want to preserve the row's original structure | ||
| // for sort ordering in the final step. | ||
| a.elements = append(a.elements, append(row, nil, evalRow[0])) | ||
| return nil | ||
| } | ||
|
|
||
| func evalExprs(ctx *sql.Context, exprs []sql.Expression, row sql.Row) (sql.Row, error) { | ||
| result := make(sql.Row, len(exprs)) | ||
| for i, expr := range exprs { | ||
| var err error | ||
| result[i], err = expr.Eval(ctx, row) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
| return result, nil | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.