|
| 1 | +// Copyright 2026 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package functions |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "io" |
| 20 | + "strings" |
| 21 | + |
| 22 | + "github.com/cockroachdb/errors" |
| 23 | + "github.com/dolthub/go-mysql-server/sql" |
| 24 | + dtablefunctions "github.com/dolthub/go-mysql-server/sql/expression/tablefunction" |
| 25 | + |
| 26 | + "github.com/dolthub/doltgresql/server/functions/framework" |
| 27 | + pgtypes "github.com/dolthub/doltgresql/server/types" |
| 28 | +) |
| 29 | + |
| 30 | +// UnnestTableFunction is the FROM-clause form of unnest, returning one column per array and padding shorter arrays |
| 31 | +// with NULL. |
| 32 | +type UnnestTableFunction struct { |
| 33 | + database sql.Database |
| 34 | + arrays []sql.Expression |
| 35 | +} |
| 36 | + |
| 37 | +var _ sql.TableFunction = (*UnnestTableFunction)(nil) |
| 38 | +var _ sql.ExecSourceRel = (*UnnestTableFunction)(nil) |
| 39 | + |
| 40 | +// NewInstance implements the interface sql.TableFunction. |
| 41 | +func (u *UnnestTableFunction) NewInstance(ctx *sql.Context, database sql.Database, args []sql.Expression) (sql.Node, error) { |
| 42 | + if len(args) == 1 { |
| 43 | + unnest := sql.FunctionN{Name: u.Name(), Fn: func(ctx *sql.Context, args ...sql.Expression) (sql.Expression, error) { |
| 44 | + compiledFunction, _, err := framework.GetFunction(ctx, u.Name(), args...) |
| 45 | + return compiledFunction, err |
| 46 | + }} |
| 47 | + return dtablefunctions.NewTableFunctionWrapper(unnest).NewInstance(ctx, database, args) |
| 48 | + } |
| 49 | + return (&UnnestTableFunction{database: database}).WithExpressions(ctx, args...) |
| 50 | +} |
| 51 | + |
| 52 | +// Name implements the interface sql.TableFunction. |
| 53 | +func (u *UnnestTableFunction) Name() string { |
| 54 | + return "unnest" |
| 55 | +} |
| 56 | + |
| 57 | +// Database implements the interface sql.Databaser. |
| 58 | +func (u *UnnestTableFunction) Database() sql.Database { |
| 59 | + return u.database |
| 60 | +} |
| 61 | + |
| 62 | +// WithDatabase implements the interface sql.Databaser. |
| 63 | +func (u *UnnestTableFunction) WithDatabase(database sql.Database) (sql.Node, error) { |
| 64 | + nu := *u |
| 65 | + nu.database = database |
| 66 | + return &nu, nil |
| 67 | +} |
| 68 | + |
| 69 | +// Expressions implements the interface sql.Expressioner. |
| 70 | +func (u *UnnestTableFunction) Expressions() []sql.Expression { |
| 71 | + return u.arrays |
| 72 | +} |
| 73 | + |
| 74 | +// WithExpressions implements the interface sql.Expressioner. |
| 75 | +func (u *UnnestTableFunction) WithExpressions(ctx *sql.Context, exprs ...sql.Expression) (sql.Node, error) { |
| 76 | + for _, expr := range exprs { |
| 77 | + typ, ok := expr.Type(ctx).(*pgtypes.DoltgresType) |
| 78 | + if !ok { |
| 79 | + typ = pgtypes.FromGmsType(expr.Type(ctx)) |
| 80 | + } |
| 81 | + if !typ.IsArrayType() { |
| 82 | + return nil, framework.ErrFunctionDoesNotExist.New(fmt.Sprintf("pg_catalog.unnest(%s)", typ)) |
| 83 | + } |
| 84 | + } |
| 85 | + nu := *u |
| 86 | + nu.arrays = exprs |
| 87 | + return &nu, nil |
| 88 | +} |
| 89 | + |
| 90 | +// Schema implements the interface sql.Node. |
| 91 | +func (u *UnnestTableFunction) Schema(ctx *sql.Context) sql.Schema { |
| 92 | + schema := make(sql.Schema, len(u.arrays)) |
| 93 | + for i, array := range u.arrays { |
| 94 | + schema[i] = &sql.Column{Name: u.Name(), Type: array.Type(ctx).(*pgtypes.DoltgresType).ArrayBaseType(), Nullable: true} |
| 95 | + } |
| 96 | + return schema |
| 97 | +} |
| 98 | + |
| 99 | +// Children implements the interface sql.Node. |
| 100 | +func (u *UnnestTableFunction) Children() []sql.Node { |
| 101 | + return nil |
| 102 | +} |
| 103 | + |
| 104 | +// WithChildren implements the interface sql.Node. |
| 105 | +func (u *UnnestTableFunction) WithChildren(ctx *sql.Context, children ...sql.Node) (sql.Node, error) { |
| 106 | + if len(children) != 0 { |
| 107 | + return nil, errors.Errorf("unexpected children") |
| 108 | + } |
| 109 | + return u, nil |
| 110 | +} |
| 111 | + |
| 112 | +// Resolved implements the interface sql.Resolvable. |
| 113 | +func (u *UnnestTableFunction) Resolved() bool { |
| 114 | + for _, array := range u.arrays { |
| 115 | + if !array.Resolved() { |
| 116 | + return false |
| 117 | + } |
| 118 | + } |
| 119 | + return true |
| 120 | +} |
| 121 | + |
| 122 | +// IsReadOnly implements the interface sql.Node. |
| 123 | +func (u *UnnestTableFunction) IsReadOnly() bool { |
| 124 | + return true |
| 125 | +} |
| 126 | + |
| 127 | +// String implements the interface fmt.Stringer. |
| 128 | +func (u *UnnestTableFunction) String() string { |
| 129 | + arrays := make([]string, len(u.arrays)) |
| 130 | + for i, array := range u.arrays { |
| 131 | + arrays[i] = array.String() |
| 132 | + } |
| 133 | + return fmt.Sprintf("unnest(%s)", strings.Join(arrays, ", ")) |
| 134 | +} |
| 135 | + |
| 136 | +// RowIter implements the interface sql.ExecSourceRel. |
| 137 | +func (u *UnnestTableFunction) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) { |
| 138 | + arrays := make([][]any, len(u.arrays)) |
| 139 | + rowCount := 0 |
| 140 | + for i, array := range u.arrays { |
| 141 | + val, err := array.Eval(ctx, row) |
| 142 | + if err != nil { |
| 143 | + return nil, err |
| 144 | + } |
| 145 | + arrays[i], _ = val.([]any) |
| 146 | + rowCount = max(rowCount, len(arrays[i])) |
| 147 | + } |
| 148 | + |
| 149 | + var i = 0 |
| 150 | + return pgtypes.NewSetReturningFunctionRowIter(func(ctx *sql.Context) (sql.Row, error) { |
| 151 | + defer func() { i++ }() |
| 152 | + |
| 153 | + if i >= rowCount { |
| 154 | + return nil, io.EOF |
| 155 | + } |
| 156 | + result := make(sql.Row, len(arrays)) |
| 157 | + for j, array := range arrays { |
| 158 | + if i < len(array) { |
| 159 | + result[j] = array[i] |
| 160 | + } |
| 161 | + } |
| 162 | + return result, nil |
| 163 | + }), nil |
| 164 | +} |
0 commit comments