func (s *Subquery) EvalMultiple(ctx *sql.Context, row sql.Row) ([]interface{}, error) {
	s.cacheMu.Lock()
	cached := s.resultsCached
	s.cacheMu.Unlock()
	if cached {
		return s.cache, nil
	}
	result, err := s.evalMultiple(ctx, row)
	if err != nil {
		return nil, err
	}
	if s.canCacheResults {
		s.cacheMu.Lock()
		if s.resultsCached == false {
			s.cache, s.resultsCached = result, true
		}
		s.cacheMu.Unlock()
	}
	return result, nil
}
EvalMultiple returns all rows returned by a subquery.
package plan
import (
	"fmt"
	"io"
	"sync"
	"github.com/dolthub/go-mysql-server/sql"
)
type Subquery struct {
	Query sql.Node
	QueryString string
	canCacheResults bool
	resultsCached bool
	cache []interface{}
	hashCache sql.KeyValueCache
	disposeFunc sql.DisposeFunc
	cacheMu sync.Mutex
}
func NewSubquery(node sql.Node, queryString string) *Subquery {
	return &Subquery{Query: node, QueryString: queryString}
}
var _ sql.NonDeterministicExpression = (*Subquery)(nil)
type StripRowNode struct {
	UnaryNode
	numCols int
}
type stripRowIter struct {
	sql.RowIter
	numCols int
}
func (sri *stripRowIter) Next() (sql.Row, error) {
	r, err := sri.RowIter.Next()
	if err != nil {
		return nil, err
	}
	return r[sri.numCols:], nil
}
func NewStripRowNode(child sql.Node, numCols int) sql.Node {
	return &StripRowNode{UnaryNode: UnaryNode{child}, numCols: numCols}
}
func (srn *StripRowNode) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
	childIter, err := srn.Child.RowIter(ctx, row)
	if err != nil {
		return nil, err
	}
	return &stripRowIter{
		childIter,
		srn.numCols,
	}, nil
}
func (srn *StripRowNode) String() string {
	return srn.Child.String()
}
func (srn *StripRowNode) DebugString() string {
	tp := sql.NewTreePrinter()
	_ = tp.WriteNode("StripRowNode(%d)", srn.numCols)
	_ = tp.WriteChildren(sql.DebugString(srn.Child))
	return tp.String()
}
func (srn *StripRowNode) WithChildren(children ...sql.Node) (sql.Node, error) {
	if len(children) != 1 {
		return nil, sql.ErrInvalidChildrenNumber.New(srn, len(children), 1)
	}
	return &StripRowNode{
		UnaryNode: UnaryNode{Child: children[0]},
		numCols:   srn.numCols,
	}, nil
}
type prependNode struct {
	UnaryNode
	row sql.Row
}
type prependRowIter struct {
	row       sql.Row
	childIter sql.RowIter
}
func (p *prependRowIter) Next() (sql.Row, error) {
	next, err := p.childIter.Next()
	if err != nil {
		return next, err
	}
	return p.row.Append(next), nil
}
func (p *prependRowIter) Close(ctx *sql.Context) error {
	return p.childIter.Close(ctx)
}
func (p *prependNode) String() string {
	return p.Child.String()
}
func (p *prependNode) DebugString() string {
	tp := sql.NewTreePrinter()
	_ = tp.WriteNode("Prepend(%s)", sql.FormatRow(p.row))
	_ = tp.WriteChildren(sql.DebugString(p.Child))
	return tp.String()
}
func (p *prependNode) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
	childIter, err := p.Child.RowIter(ctx, row)
	if err != nil {
		return nil, err
	}
	return &prependRowIter{
		row:       p.row,
		childIter: childIter,
	}, nil
}
func (p *prependNode) WithChildren(children ...sql.Node) (sql.Node, error) {
	if len(children) != 1 {
		return nil, sql.ErrInvalidChildrenNumber.New(p, len(children), 1)
	}
	return &prependNode{
		UnaryNode: UnaryNode{Child: children[0]},
		row:       p.row,
	}, nil
}
func (s *Subquery) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
	s.cacheMu.Lock()
	cached := s.resultsCached
	s.cacheMu.Unlock()
	if cached {
		if len(s.cache) == 0 {
			return nil, nil
		}
		return s.cache[0], nil
	}
	rows, err := s.evalMultiple(ctx, row)
	if err != nil {
		return nil, err
	}
	if len(rows) > 1 {
		return nil, sql.ErrExpectedSingleRow.New()
	}
	if s.canCacheResults {
		s.cacheMu.Lock()
		if !s.resultsCached {
			s.cache, s.resultsCached = rows, true
		}
		s.cacheMu.Unlock()
	}
	if len(rows) == 0 {
		return nil, nil
	}
	return rows[0], nil
}
func prependRowInPlan(row sql.Row) func(n sql.Node) (sql.Node, error) {
	return func(n sql.Node) (sql.Node, error) {
		switch n := n.(type) {
		case *Project, *GroupBy, *Having, *SubqueryAlias, *Window, sql.Table, *ValueDerivedTable, *Union:
			return &prependNode{
				UnaryNode: UnaryNode{Child: n},
				row:       row,
			}, nil
		default:
			return n, nil
		}
	}
}