diff --git a/sql/expression/tablefunction/table_function.go b/sql/expression/tablefunction/table_function.go index e54a1fccd4..27ff0c7d2d 100644 --- a/sql/expression/tablefunction/table_function.go +++ b/sql/expression/tablefunction/table_function.go @@ -68,7 +68,7 @@ func (t *TableFunctionWrapper) Expressions() []sql.Expression { if t.funcExpr == nil { return nil } - return t.funcExpr.Children() + return []sql.Expression{t.funcExpr} } func (t *TableFunctionWrapper) IsReadOnly() bool { @@ -130,15 +130,10 @@ func (t *TableFunctionWrapper) WithExpressions(exprs ...sql.Expression) (sql.Nod return nil, sql.ErrInvalidChildrenNumber.New(t, len(exprs), 0) } } - l := len(t.funcExpr.Children()) - if len(exprs) != l { - return nil, sql.ErrInvalidChildrenNumber.New(t, len(exprs), l) + if len(exprs) != 1 { + return nil, sql.ErrInvalidChildrenNumber.New(t, len(exprs), 1) } nt := *t - nf, err := nt.funcExpr.WithChildren(exprs...) - if err != nil { - return nil, err - } - nt.funcExpr = nf + nt.funcExpr = exprs[0] return &nt, nil } diff --git a/sql/plan/values.go b/sql/plan/values.go index 9368daafbc..b0a55eec27 100644 --- a/sql/plan/values.go +++ b/sql/plan/values.go @@ -36,7 +36,7 @@ func NewValues(tuples [][]sql.Expression) *Values { return &Values{ExpressionTuples: tuples} } -// NewValuesWithAliasName creates a Values node with the given row and column aliases. +// NewValuesWithAlias creates a Values node with the given row and column aliases. func NewValuesWithAlias(tableName string, columnNames map[string]string, tuples [][]sql.Expression) *Values { return &Values{ExpressionTuples: tuples, AliasName: tableName, ColumnNames: columnNames} } diff --git a/sql/rows.go b/sql/rows.go index bd89fd7cd4..a9e5f55d5c 100644 --- a/sql/rows.go +++ b/sql/rows.go @@ -187,14 +187,47 @@ type sliceRowIter struct { idx int } -func (i *sliceRowIter) Next(*Context) (Row, error) { +func (i *sliceRowIter) Next(ctx *Context) (Row, error) { if i.idx >= len(i.rows) { return nil, io.EOF } - r := i.rows[i.idx] - i.idx++ - return r.Copy(), nil + vals, hasRowIter, rowIterEnded, err := unwrapRowIterAsReturnedResult(ctx, i.rows[i.idx]) + if err != nil { + return nil, err + } + + if !hasRowIter { + i.idx++ + } else if rowIterEnded { + return nil, io.EOF + } + + return NewRow(vals...), nil +} + +// unwrapRowIterAsReturnedResult unwraps the row if there is any RowIter in the given Row +// creating multiple Rows from a single Row. +func unwrapRowIterAsReturnedResult(ctx *Context, r Row) ([]any, bool, bool, error) { + vals := make([]interface{}, len(r)) + var hasActiveRowIter = false + var hasRowIter = false + for i, v := range r { + if ri, ok := v.(RowIter); ok { + hasRowIter = true + nv, err := ri.Next(ctx) + if err == nil { + hasActiveRowIter = true + } + if nv != nil && len(nv) > 0 { + // TODO: can set returning iter return multiple values in the row? + vals[i] = nv[0] + } + } else { + vals[i] = v + } + } + return vals, hasRowIter, !hasActiveRowIter && hasRowIter, nil } func (i *sliceRowIter) Close(*Context) error {