func (i *ifElseIter) RepresentingNode() sql.Node {
	return i.branchNode
}
RepresentingNode implements the sql.BlockRowIter interface.
package plan
import (
	"fmt"
	"github.com/dolthub/go-mysql-server/sql"
)
type IfConditional struct {
	Condition sql.Expression
	Body      sql.Node
}
var _ sql.Node = (*IfConditional)(nil)
var _ sql.DebugStringer = (*IfConditional)(nil)
var _ sql.Expressioner = (*IfConditional)(nil)
func NewIfConditional(condition sql.Expression, body sql.Node) *IfConditional {
	return &IfConditional{
		Condition: condition,
		Body:      body,
	}
}
func (ic *IfConditional) Resolved() bool {
	return ic.Condition.Resolved() && ic.Body.Resolved()
}
func (ic *IfConditional) String() string {
	p := sql.NewTreePrinter()
	_ = p.WriteNode(fmt.Sprintf("IF(%s)", ic.Condition.String()))
	_ = p.WriteChildren(ic.Body.String())
	return p.String()
}
func (ic *IfConditional) DebugString() string {
	p := sql.NewTreePrinter()
	_ = p.WriteNode(fmt.Sprintf("IF(%s)", sql.DebugString(ic.Condition)))
	_ = p.WriteChildren(sql.DebugString(ic.Body))
	return p.String()
}
func (ic *IfConditional) Schema() sql.Schema {
	return ic.Body.Schema()
}
func (ic *IfConditional) Children() []sql.Node {
	return []sql.Node{ic.Body}
}
func (ic *IfConditional) WithChildren(children ...sql.Node) (sql.Node, error) {
	if len(children) != 1 {
		return nil, sql.ErrInvalidChildrenNumber.New(ic, len(children), 1)
	}
	nic := *ic
	nic.Body = children[0]
	return &nic, nil
}
func (ic *IfConditional) Expressions() []sql.Expression {
	return []sql.Expression{ic.Condition}
}
func (ic *IfConditional) WithExpressions(exprs ...sql.Expression) (sql.Node, error) {
	if len(exprs) != 1 {
		return nil, sql.ErrInvalidChildrenNumber.New(ic, len(exprs), 1)
	}
	nic := *ic
	nic.Condition = exprs[0]
	return &nic, nil
}
func (ic *IfConditional) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
	return ic.Body.RowIter(ctx, row)
}
type IfElseBlock struct {
	IfConditionals []*IfConditional
	Else           sql.Node
}
var _ sql.Node = (*IfElseBlock)(nil)
var _ sql.DebugStringer = (*IfElseBlock)(nil)
func NewIfElse(ifConditionals []*IfConditional, elseStatement sql.Node) *IfElseBlock {
	return &IfElseBlock{
		IfConditionals: ifConditionals,
		Else:           elseStatement,
	}
}
func (ieb *IfElseBlock) Resolved() bool {
	for _, s := range ieb.IfConditionals {
		if !s.Resolved() {
			return false
		}
	}
	return ieb.Else.Resolved()
}
func (ieb *IfElseBlock) String() string {
	p := sql.NewTreePrinter()
	_ = p.WriteNode("IF BLOCK")
	var children []string
	for _, s := range ieb.IfConditionals {
		children = append(children, s.String())
	}
	_ = p.WriteChildren(children...)
	ep := sql.NewTreePrinter()
	_ = ep.WriteNode("ELSE")
	_ = ep.WriteChildren(ieb.Else.String())
	_ = p.WriteChildren(ep.String())
	return p.String()
}
func (ieb *IfElseBlock) DebugString() string {
	p := sql.NewTreePrinter()
	_ = p.WriteNode("IF BLOCK")
	var children []string
	for _, s := range ieb.IfConditionals {
		children = append(children, sql.DebugString(s))
	}
	_ = p.WriteChildren(children...)
	ep := sql.NewTreePrinter()
	_ = ep.WriteNode("ELSE")
	_ = ep.WriteChildren(sql.DebugString(ieb.Else))
	_ = p.WriteChildren(ep.String())
	return p.String()
}
func (ieb *IfElseBlock) Schema() sql.Schema {
	return nil
}
func (ieb *IfElseBlock) Children() []sql.Node {
	statements := make([]sql.Node, len(ieb.IfConditionals)+1)
	for i, ifConditional := range ieb.IfConditionals {
		statements[i] = ifConditional
	}
	statements[len(ieb.IfConditionals)] = ieb.Else
	return statements
}
func (ieb *IfElseBlock) WithChildren(children ...sql.Node) (sql.Node, error) {
	if len(children) < 2 {
		return nil, fmt.Errorf("%T: invalid children number, got %d, expected at least 2", ieb, len(children))
	}
	ifConditionals := make([]*IfConditional, len(children)-1)
	for i, child := range children[:len(children)-1] {
		ifConditional, ok := child.(*IfConditional)
		if !ok {
			return nil, fmt.Errorf("%T: expected if conditional child but got %T", ieb, child)
		}
		ifConditionals[i] = ifConditional
	}
	return NewIfElse(ifConditionals, children[len(children)-1]), nil
}
func (ieb *IfElseBlock) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
	var branchIter sql.RowIter
	var err error
	for _, ifConditional := range ieb.IfConditionals {
		condition, err := ifConditional.Condition.Eval(ctx, row)
		if err != nil {
			return nil, err
		}
		var passedCondition bool
		if condition != nil {
			passedCondition, err = sql.ConvertToBool(condition)
			if err != nil {
				return nil, err
			}
		}
		if !passedCondition {
			continue
		}
		branchIter, err = ifConditional.RowIter(ctx, row)
		if err != nil {
			return nil, err
		}
		if blockRowIter, ok := branchIter.(BlockRowIter); ok {
			return blockRowIter, nil
		}
		return &ifElseIter{
			branchIter: branchIter,
			sch:        ifConditional.Body.Schema(),
			branchNode: ifConditional.Body,
		}, nil
	}
	branchIter, err = ieb.Else.RowIter(ctx, row)
	if err != nil {
		return nil, err
	}
	if blockRowIter, ok := branchIter.(BlockRowIter); ok {
		return blockRowIter, nil
	}
	return &ifElseIter{
		branchIter: branchIter,
		sch:        ieb.Else.Schema(),
		branchNode: ieb.Else,
	}, nil
}
type ifElseIter struct {
	branchIter sql.RowIter
	sch        sql.Schema
	branchNode sql.Node
}
var _ BlockRowIter = (*ifElseIter)(nil)
func (i *ifElseIter) Next() (sql.Row, error) {
	return i.branchIter.Next()
}
func (i *ifElseIter) Close(ctx *sql.Context) error {
	return i.branchIter.Close(ctx)
}