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
Conversation
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
sql/analyzer/topn.go
Outdated
package analyzer | ||
|
||
import ( | ||
//"fmt" |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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)) |
There was a problem hiding this comment.
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
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.