|
| 1 | +// Copyright 2024 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 dtablefunctions |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + "strings" |
| 20 | + |
| 21 | + "github.com/dolthub/go-mysql-server/sql" |
| 22 | +) |
| 23 | + |
| 24 | +var _ sql.TableFunction = &TableFunctionWrapper{} |
| 25 | +var _ sql.ExecSourceRel = &TableFunctionWrapper{} |
| 26 | + |
| 27 | +// TableFunctionWrapper represents a table function with underlying |
| 28 | +// regular function. It allows using regular function as table function. |
| 29 | +type TableFunctionWrapper struct { |
| 30 | + underlyingFunc sql.Function |
| 31 | + |
| 32 | + args []sql.Expression |
| 33 | + database sql.Database |
| 34 | + funcExpr sql.Expression |
| 35 | +} |
| 36 | + |
| 37 | +// NewTableFunctionWrapper creates new TableFunction |
| 38 | +// with given Function as underlying function. |
| 39 | +func NewTableFunctionWrapper(f sql.Function) sql.TableFunction { |
| 40 | + return &TableFunctionWrapper{ |
| 41 | + underlyingFunc: f, |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +func (t *TableFunctionWrapper) NewInstance(ctx *sql.Context, db sql.Database, args []sql.Expression) (sql.Node, error) { |
| 46 | + nt := *t |
| 47 | + nt.database = db |
| 48 | + nt.args = args |
| 49 | + f, err := nt.underlyingFunc.NewInstance(args) |
| 50 | + if err != nil { |
| 51 | + return nil, err |
| 52 | + } |
| 53 | + nt.funcExpr = f |
| 54 | + return &nt, nil |
| 55 | +} |
| 56 | + |
| 57 | +func (t *TableFunctionWrapper) Children() []sql.Node { |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +func (t *TableFunctionWrapper) Database() sql.Database { |
| 62 | + return t.database |
| 63 | +} |
| 64 | + |
| 65 | +func (t *TableFunctionWrapper) Expressions() []sql.Expression { |
| 66 | + if t.funcExpr == nil { |
| 67 | + return nil |
| 68 | + } |
| 69 | + return t.funcExpr.Children() |
| 70 | +} |
| 71 | + |
| 72 | +func (t *TableFunctionWrapper) IsReadOnly() bool { |
| 73 | + return true |
| 74 | +} |
| 75 | + |
| 76 | +func (t *TableFunctionWrapper) Name() string { |
| 77 | + return t.underlyingFunc.FunctionName() |
| 78 | +} |
| 79 | + |
| 80 | +func (t *TableFunctionWrapper) RowIter(ctx *sql.Context, r sql.Row) (sql.RowIter, error) { |
| 81 | + v, err := t.funcExpr.Eval(ctx, r) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + return sql.RowsToRowIter(sql.Row{v}), nil |
| 86 | +} |
| 87 | + |
| 88 | +func (t *TableFunctionWrapper) Resolved() bool { |
| 89 | + for _, expr := range t.args { |
| 90 | + if !expr.Resolved() { |
| 91 | + return false |
| 92 | + } |
| 93 | + } |
| 94 | + return true |
| 95 | +} |
| 96 | + |
| 97 | +func (t *TableFunctionWrapper) Schema() sql.Schema { |
| 98 | + return sql.Schema{&sql.Column{Name: t.underlyingFunc.FunctionName(), Type: t.funcExpr.Type()}} |
| 99 | +} |
| 100 | + |
| 101 | +func (t *TableFunctionWrapper) String() string { |
| 102 | + var args []string |
| 103 | + for _, expr := range t.args { |
| 104 | + args = append(args, expr.String()) |
| 105 | + } |
| 106 | + return fmt.Sprintf("%s(%s)", t.underlyingFunc.FunctionName(), strings.Join(args, ", ")) |
| 107 | +} |
| 108 | + |
| 109 | +func (t *TableFunctionWrapper) WithChildren(children ...sql.Node) (sql.Node, error) { |
| 110 | + if len(children) != 0 { |
| 111 | + return nil, sql.ErrInvalidChildrenNumber.New(t, len(children), 0) |
| 112 | + } |
| 113 | + return t, nil |
| 114 | +} |
| 115 | + |
| 116 | +func (t *TableFunctionWrapper) WithDatabase(database sql.Database) (sql.Node, error) { |
| 117 | + nt := *t |
| 118 | + nt.database = database |
| 119 | + return &nt, nil |
| 120 | +} |
| 121 | + |
| 122 | +func (t *TableFunctionWrapper) WithExpressions(exprs ...sql.Expression) (sql.Node, error) { |
| 123 | + if t.funcExpr == nil { |
| 124 | + if len(exprs) != 0 { |
| 125 | + return nil, sql.ErrInvalidChildrenNumber.New(t, len(exprs), 0) |
| 126 | + } |
| 127 | + } |
| 128 | + l := len(t.funcExpr.Children()) |
| 129 | + if len(exprs) != l { |
| 130 | + return nil, sql.ErrInvalidChildrenNumber.New(t, len(exprs), l) |
| 131 | + } |
| 132 | + nt := *t |
| 133 | + nf, err := nt.funcExpr.WithChildren(exprs...) |
| 134 | + if err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + nt.funcExpr = nf |
| 138 | + return &nt, nil |
| 139 | +} |
0 commit comments