func (r *RowNumber) EvalRow(i int, buffer sql.Row) (interface{}, error) {
	rows := buffer[0].([]sql.Row)
	return rows[i][len(rows[i])-2], nil
}
EvalRow implements sql.WindowAggregation
package window
import (
	"sort"
	"strings"
	"github.com/dolthub/go-mysql-server/sql/expression"
	"github.com/dolthub/go-mysql-server/sql"
)
type RowNumber struct {
	window *sql.Window
	pos    int
}
var _ sql.FunctionExpression = (*RowNumber)(nil)
var _ sql.WindowAggregation = (*RowNumber)(nil)
func NewRowNumber() sql.Expression {
	return &RowNumber{}
}
func (r *RowNumber) Window() *sql.Window {
	return r.window
}
func (r *RowNumber) Resolved() bool {
	return windowResolved(r.window)
}
func (r *RowNumber) NewBuffer() sql.Row {
	return sql.NewRow(make([]sql.Row, 0))
}
func (r *RowNumber) String() string {
	sb := strings.Builder{}
	sb.WriteString("row_number()")
	if r.window != nil {
		sb.WriteString(" ")
		sb.WriteString(r.window.String())
	}
	return sb.String()
}
func (r *RowNumber) DebugString() string {
	sb := strings.Builder{}
	sb.WriteString("row_number()")
	if r.window != nil {
		sb.WriteString(" ")
		sb.WriteString(sql.DebugString(r.window))
	}
	return sb.String()
}
func (r *RowNumber) FunctionName() string {
	return "ROW_NUMBER"
}
func (r *RowNumber) Type() sql.Type {
	return sql.Int64
}
func (r *RowNumber) IsNullable() bool {
	return false
}
func (r *RowNumber) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
	panic("eval called on window function")
}
func (r *RowNumber) Children() []sql.Expression {
	return r.window.ToExpressions()
}
func (r *RowNumber) WithChildren(children ...sql.Expression) (sql.Expression, error) {
	window, err := r.window.FromExpressions(children)
	if err != nil {
		return nil, err
	}
	return r.WithWindow(window)
}
func (r *RowNumber) WithWindow(window *sql.Window) (sql.WindowAggregation, error) {
	nr := *r
	nr.window = window
	return &nr, nil
}
func (r *RowNumber) Add(ctx *sql.Context, buffer, row sql.Row) error {
	rows := buffer[0].([]sql.Row)
	buffer[0] = append(rows, append(row, nil, r.pos))
	r.pos++
	return nil
}
func (r *RowNumber) Finish(ctx *sql.Context, buffer sql.Row) error {
	rows := buffer[0].([]sql.Row)
	if len(rows) > 0 && r.window != nil && r.window.OrderBy != nil {
		sorter := &expression.Sorter{
			SortFields: append(partitionsToSortFields(r.Window().PartitionBy), r.Window().OrderBy...),
			Rows:       rows,
			Ctx:        ctx,
		}
		sort.Stable(sorter)
		if sorter.LastError != nil {
			return sorter.LastError
		}
		rowNumIdx := len(rows[0]) - 2
		originalOrderIdx := len(rows[0]) - 1
		var last sql.Row
		var rowNum int
		for _, row := range rows {
			isNew, err := isNewPartition(ctx, r.window.PartitionBy, last, row)
			if err != nil {
				return err
			}
			if isNew {
				rowNum = 1
			}
			row[rowNumIdx] = rowNum
			rowNum++
			last = row
		}
		sort.SliceStable(rows, func(i, j int) bool {
			return rows[i][originalOrderIdx].(int) < rows[j][originalOrderIdx].(int)
		})
	}
	return nil
}