|
| 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 = &TableFunction{} |
| 25 | +var _ sql.ExecSourceRel = &TableFunction{} |
| 26 | + |
| 27 | +type TableFunction struct { |
| 28 | + underlyingFunc sql.Function |
| 29 | + |
| 30 | + args []sql.Expression |
| 31 | + database sql.Database |
| 32 | + funcExpr sql.Expression |
| 33 | +} |
| 34 | + |
| 35 | +func NewTableFunction(f sql.Function) sql.TableFunction { |
| 36 | + return &TableFunction{ |
| 37 | + underlyingFunc: f, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func (t *TableFunction) NewInstance(ctx *sql.Context, db sql.Database, args []sql.Expression) (sql.Node, error) { |
| 42 | + nt := *t |
| 43 | + nt.database = db |
| 44 | + nt.args = args |
| 45 | + f, err := nt.underlyingFunc.NewInstance(args) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + nt.funcExpr = f |
| 50 | + return &nt, nil |
| 51 | +} |
| 52 | + |
| 53 | +func (t *TableFunction) Children() []sql.Node { |
| 54 | + return nil |
| 55 | +} |
| 56 | + |
| 57 | +func (t *TableFunction) Database() sql.Database { |
| 58 | + return t.database |
| 59 | +} |
| 60 | + |
| 61 | +func (t *TableFunction) Expressions() []sql.Expression { |
| 62 | + return t.funcExpr.Children() |
| 63 | +} |
| 64 | + |
| 65 | +func (t *TableFunction) IsReadOnly() bool { |
| 66 | + return true |
| 67 | +} |
| 68 | + |
| 69 | +func (t *TableFunction) Name() string { |
| 70 | + return t.underlyingFunc.FunctionName() |
| 71 | +} |
| 72 | + |
| 73 | +func (t *TableFunction) RowIter(ctx *sql.Context, r sql.Row) (sql.RowIter, error) { |
| 74 | + v, err := t.funcExpr.Eval(ctx, r) |
| 75 | + if err != nil { |
| 76 | + return nil, err |
| 77 | + } |
| 78 | + return sql.RowsToRowIter(sql.Row{v}), nil |
| 79 | +} |
| 80 | + |
| 81 | +func (t *TableFunction) Resolved() bool { |
| 82 | + for _, expr := range t.args { |
| 83 | + return expr.Resolved() |
| 84 | + } |
| 85 | + return true |
| 86 | +} |
| 87 | + |
| 88 | +func (t *TableFunction) Schema() sql.Schema { |
| 89 | + return sql.Schema{&sql.Column{Name: t.underlyingFunc.FunctionName(), Type: t.funcExpr.Type()}} |
| 90 | +} |
| 91 | + |
| 92 | +func (t *TableFunction) String() string { |
| 93 | + var args []string |
| 94 | + for _, expr := range t.args { |
| 95 | + args = append(args, expr.String()) |
| 96 | + } |
| 97 | + return fmt.Sprintf("%s(%s)", t.underlyingFunc.FunctionName(), strings.Join(args, ", ")) |
| 98 | +} |
| 99 | + |
| 100 | +func (t *TableFunction) WithChildren(children ...sql.Node) (sql.Node, error) { |
| 101 | + if len(children) != 0 { |
| 102 | + return nil, fmt.Errorf("unexpected children") |
| 103 | + } |
| 104 | + return t, nil |
| 105 | +} |
| 106 | + |
| 107 | +func (t *TableFunction) WithDatabase(database sql.Database) (sql.Node, error) { |
| 108 | + nt := *t |
| 109 | + nt.database = database |
| 110 | + return &nt, nil |
| 111 | +} |
| 112 | + |
| 113 | +func (t *TableFunction) WithExpressions(exprs ...sql.Expression) (sql.Node, error) { |
| 114 | + l := len(t.funcExpr.Children()) |
| 115 | + if len(exprs) != l { |
| 116 | + return nil, sql.ErrInvalidChildrenNumber.New(t, len(exprs), l) |
| 117 | + } |
| 118 | + nt := *t |
| 119 | + nf, err := nt.funcExpr.WithChildren(exprs...) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + nt.funcExpr = nf |
| 124 | + return &nt, nil |
| 125 | +} |
0 commit comments