Skip to content
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

Use ordinals to force stable TopN heap sort #1690

Merged
merged 3 commits into from
Apr 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions enginetest/queries/script_queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,39 @@ type ScriptTestAssertion struct {
// Unlike other engine tests, ScriptTests must be self-contained. No other tables are created outside the definition of
// the tests.
var ScriptTests = []ScriptTest{
{
Name: "topN stable output",
SetUpScript: []string{
"create table xy (x int primary key, y int)",
"insert into xy values (1,0),(2,0),(3,0),(4,0)",
},
Assertions: []ScriptTestAssertion{
{
Query: "select * from xy order by y asc limit 1",
Expected: []sql.Row{{1, 0}},
},
{
Query: "select * from xy order by y asc limit 1 offset 1",
Expected: []sql.Row{{2, 0}},
},
{
Query: "select * from xy order by y asc limit 1 offset 2",
Expected: []sql.Row{{3, 0}},
},
{
Query: "select * from xy order by y asc limit 1 offset 3",
Expected: []sql.Row{{4, 0}},
},
{
Query: "(select * from xy order by y asc limit 1 offset 1) union (select * from xy order by y asc limit 1 offset 2)",
Expected: []sql.Row{{2, 0}, {3, 0}},
},
{
Query: "with recursive cte as ((select * from xy order by y asc limit 1 offset 1) union (select * from xy order by y asc limit 1 offset 2)) select * from cte",
Expected: []sql.Row{{2, 0}, {3, 0}},
},
},
},
{
Name: "enums with default, case-sensitive collation (utf8mb4_0900_bin)",
SetUpScript: []string{
Expand Down
2 changes: 1 addition & 1 deletion sql/plan/recursive_cte.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (r *RecursiveCte) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, erro
if err != nil {
return nil, err
}
iter = newTopRowsIter(r.union.SortFields, limit, false, iter)
iter = newTopRowsIter(r.union.SortFields, limit, false, iter, len(r.union.Schema()))
} else if r.union.Limit != nil {
limit, err := getInt64Value(ctx, r.union.Limit)
if err != nil {
Expand Down
12 changes: 8 additions & 4 deletions sql/plan/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"sort"
"strings"

"github.com/dolthub/go-mysql-server/sql/types"

"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/expression"
)
Expand Down Expand Up @@ -320,7 +322,7 @@ func (n *TopN) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
if err != nil {
return nil, err
}
return sql.NewSpanIter(span, newTopRowsIter(n.Fields, limit, n.CalcFoundRows, i)), nil
return sql.NewSpanIter(span, newTopRowsIter(n.Fields, limit, n.CalcFoundRows, i, len(n.Child.Schema()))), nil
}

func (n *TopN) String() string {
Expand Down Expand Up @@ -397,9 +399,9 @@ type topRowsIter struct {
idx int
}

func newTopRowsIter(s sql.SortFields, limit int64, calcFoundRows bool, child sql.RowIter) *topRowsIter {
func newTopRowsIter(s sql.SortFields, limit int64, calcFoundRows bool, child sql.RowIter, childSchemaLen int) *topRowsIter {
return &topRowsIter{
sortFields: s,
sortFields: append(s, sql.SortField{Column: expression.NewGetField(childSchemaLen, types.Int64, "order", false)}),
limit: limit,
calcFoundRows: calcFoundRows,
childIter: child,
Expand All @@ -421,7 +423,7 @@ func (i *topRowsIter) Next(ctx *sql.Context) (sql.Row, error) {
}
row := i.topRows[i.idx]
i.idx++
return row, nil
return row[:len(row)-1], nil
}

func (i *topRowsIter) Close(ctx *sql.Context) error {
Expand Down Expand Up @@ -453,6 +455,8 @@ func (i *topRowsIter) computeTopRows(ctx *sql.Context) error {
}
i.numFoundRows++

row = append(row, i.numFoundRows)

heap.Push(topRowsHeap, row)
if int64(topRowsHeap.Len()) > i.limit {
heap.Pop(topRowsHeap)
Expand Down
2 changes: 1 addition & 1 deletion sql/plan/union.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (u *Union) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
if err != nil {
return nil, err
}
iter = newTopRowsIter(u.SortFields, limit, false, iter)
iter = newTopRowsIter(u.SortFields, limit, false, iter, len(u.Schema()))
} else if u.Limit != nil {
limit, err := getInt64Value(ctx, u.Limit)
if err != nil {
Expand Down