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

plan,analyzer: Implement a TopRowsNode. #635

Merged
merged 3 commits into from Nov 18, 2021
Merged

plan,analyzer: Implement a TopRowsNode. #635

merged 3 commits into from Nov 18, 2021

Conversation

reltuk
Copy link
Contributor

@reltuk reltuk commented Nov 17, 2021

For a query like:

SELECT * FROM table ORDER BY id LIMIT 100 OFFSET 100

we do not need to materialize the entire sorted result. We can use a heap to
compute a streaming TopN and output the results when the child iterator is EOF.

  • Add a simple implementation of TopN based on container/heap and
    sql/expression.Sorter.

  • Add a TopRowsNode and topRowIter.

  • Add a simple analyzer rule to replace a Sort node with a TopRows node when it
    sits directly underneath a Limit() or directly underneath a Limit(Offset()).
    Some care is required to keep sql_calc_found_rows operating as expected.

For a query like:

`SELECT * FROM table ORDER BY id LIMIT 100 OFFSET 100`

we do not need to materialize the entire sorted result. We can use a heap to
compute a streaming TopN and output the results when the child iterator is EOF.

* Add a simple implementation of TopN based on container/heap and
  sql/expression.Sorter.

* Add a TopRowsNode and topRowIter.

* Add a simple analyzer rule to replace a Sort node with a TopRows node when it
  sits directly underneath a Limit() or directly underneath a Limit(Offset()).
  Some care is required to keep `sql_calc_found_rows` operating as expected.
Copy link
Member

@zachmu zachmu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

package analyzer

import (
//"fmt"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete

sql/plan/sort.go Outdated
return nil, sql.ErrInvalidChildrenNumber.New(n, len(exprs), len(n.SortFields))
}

var fields = make([]sql.SortField, len(n.SortFields))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use SortFields.FromExpressions for this

sql/plan/sort.go Outdated

// Expressions implements the Expressioner interface.
func (n *TopN) Expressions() []sql.Expression {
var exprs = make([]sql.Expression, len(n.SortFields))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use SortFields.ToExpressions for this

@reltuk reltuk merged commit 0ef5ada into main Nov 18, 2021
@Hydrocharged Hydrocharged deleted the aaron/plan-topn branch December 8, 2021 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants