Skip to content

Commit 5d3eaa8

Browse files
authored
fix Expressions() of TableFunctionWrapper to make the function expression visible (#3226)
1 parent 5a790fb commit 5d3eaa8

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

sql/expression/tablefunction/table_function.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (t *TableFunctionWrapper) Expressions() []sql.Expression {
6868
if t.funcExpr == nil {
6969
return nil
7070
}
71-
return t.funcExpr.Children()
71+
return []sql.Expression{t.funcExpr}
7272
}
7373

7474
func (t *TableFunctionWrapper) IsReadOnly() bool {
@@ -130,15 +130,10 @@ func (t *TableFunctionWrapper) WithExpressions(exprs ...sql.Expression) (sql.Nod
130130
return nil, sql.ErrInvalidChildrenNumber.New(t, len(exprs), 0)
131131
}
132132
}
133-
l := len(t.funcExpr.Children())
134-
if len(exprs) != l {
135-
return nil, sql.ErrInvalidChildrenNumber.New(t, len(exprs), l)
133+
if len(exprs) != 1 {
134+
return nil, sql.ErrInvalidChildrenNumber.New(t, len(exprs), 1)
136135
}
137136
nt := *t
138-
nf, err := nt.funcExpr.WithChildren(exprs...)
139-
if err != nil {
140-
return nil, err
141-
}
142-
nt.funcExpr = nf
137+
nt.funcExpr = exprs[0]
143138
return &nt, nil
144139
}

sql/plan/values.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func NewValues(tuples [][]sql.Expression) *Values {
3636
return &Values{ExpressionTuples: tuples}
3737
}
3838

39-
// NewValuesWithAliasName creates a Values node with the given row and column aliases.
39+
// NewValuesWithAlias creates a Values node with the given row and column aliases.
4040
func NewValuesWithAlias(tableName string, columnNames map[string]string, tuples [][]sql.Expression) *Values {
4141
return &Values{ExpressionTuples: tuples, AliasName: tableName, ColumnNames: columnNames}
4242
}

sql/rows.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,47 @@ type sliceRowIter struct {
187187
idx int
188188
}
189189

190-
func (i *sliceRowIter) Next(*Context) (Row, error) {
190+
func (i *sliceRowIter) Next(ctx *Context) (Row, error) {
191191
if i.idx >= len(i.rows) {
192192
return nil, io.EOF
193193
}
194194

195-
r := i.rows[i.idx]
196-
i.idx++
197-
return r.Copy(), nil
195+
vals, hasRowIter, rowIterEnded, err := unwrapRowIterAsReturnedResult(ctx, i.rows[i.idx])
196+
if err != nil {
197+
return nil, err
198+
}
199+
200+
if !hasRowIter {
201+
i.idx++
202+
} else if rowIterEnded {
203+
return nil, io.EOF
204+
}
205+
206+
return NewRow(vals...), nil
207+
}
208+
209+
// unwrapRowIterAsReturnedResult unwraps the row if there is any RowIter in the given Row
210+
// creating multiple Rows from a single Row.
211+
func unwrapRowIterAsReturnedResult(ctx *Context, r Row) ([]any, bool, bool, error) {
212+
vals := make([]interface{}, len(r))
213+
var hasActiveRowIter = false
214+
var hasRowIter = false
215+
for i, v := range r {
216+
if ri, ok := v.(RowIter); ok {
217+
hasRowIter = true
218+
nv, err := ri.Next(ctx)
219+
if err == nil {
220+
hasActiveRowIter = true
221+
}
222+
if nv != nil && len(nv) > 0 {
223+
// TODO: can set returning iter return multiple values in the row?
224+
vals[i] = nv[0]
225+
}
226+
} else {
227+
vals[i] = v
228+
}
229+
}
230+
return vals, hasRowIter, !hasActiveRowIter && hasRowIter, nil
198231
}
199232

200233
func (i *sliceRowIter) Close(*Context) error {

0 commit comments

Comments
 (0)