github.com/dolthub/go-mysql-server@v0.18.0/memory/table_function.go (about)

     1  package memory
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  
     7  	"github.com/dolthub/go-mysql-server/sql"
     8  	"github.com/dolthub/go-mysql-server/sql/expression"
     9  	"github.com/dolthub/go-mysql-server/sql/types"
    10  )
    11  
    12  var _ sql.TableFunction = TableFunc{}
    13  var _ sql.ExecSourceRel = TableFunc{}
    14  
    15  // TableFunc a simple table function that returns the instantiated value.
    16  type TableFunc struct {
    17  	db    sql.Database
    18  	name  string
    19  	value int64
    20  }
    21  
    22  func (s TableFunc) NewInstance(_ *sql.Context, db sql.Database, args []sql.Expression) (sql.Node, error) {
    23  	if len(args) != 2 {
    24  		return nil, fmt.Errorf("table_func table expects 2 arguments: (name, len)")
    25  	}
    26  	nameExp, ok := args[0].(*expression.Literal)
    27  	if !ok {
    28  		return nil, fmt.Errorf("table_func table expects arguments to be literal expressions")
    29  	}
    30  	name, ok := nameExp.Value().(string)
    31  	if !ok {
    32  		return nil, fmt.Errorf("table_func table expects 1st argument to be column name")
    33  	}
    34  	valueExpr, ok := args[1].(*expression.Literal)
    35  	if !ok {
    36  		return nil, fmt.Errorf("table_func table expects arguments to be literal expressions")
    37  	}
    38  	value, _, err := types.Int64.Convert(valueExpr.Value())
    39  	if !ok {
    40  		return nil, fmt.Errorf("%w; table_func table expects 2nd argument to be a table_func length integer", err)
    41  	}
    42  	return TableFunc{db: db, name: name, value: value.(int64)}, nil
    43  }
    44  
    45  func (s TableFunc) Resolved() bool {
    46  	return true
    47  }
    48  
    49  func (s TableFunc) IsReadOnly() bool {
    50  	return true
    51  }
    52  
    53  func (s TableFunc) String() string {
    54  	return fmt.Sprintf("table_func(%s, %d)", s.name, s.value)
    55  }
    56  
    57  func (s TableFunc) DebugString() string {
    58  	pr := sql.NewTreePrinter()
    59  	_ = pr.WriteNode("table_func")
    60  	children := []string{
    61  		fmt.Sprintf("name: %s", s.name),
    62  		fmt.Sprintf("len: %d", s.value),
    63  	}
    64  	_ = pr.WriteChildren(children...)
    65  	return pr.String()
    66  }
    67  
    68  func (s TableFunc) Schema() sql.Schema {
    69  	schema := []*sql.Column{
    70  		{
    71  			DatabaseSource: s.db.Name(),
    72  			Source:         s.Name(),
    73  			Name:           s.name,
    74  			Type:           types.Int64,
    75  		},
    76  	}
    77  
    78  	return schema
    79  }
    80  
    81  func (s TableFunc) Children() []sql.Node {
    82  	return []sql.Node{}
    83  }
    84  
    85  func (s TableFunc) RowIter(_ *sql.Context, _ sql.Row) (sql.RowIter, error) {
    86  	rowIter := &TableFunctionRowIter{val: s.value}
    87  	return rowIter, nil
    88  }
    89  
    90  func (s TableFunc) WithChildren(_ ...sql.Node) (sql.Node, error) {
    91  	return s, nil
    92  }
    93  
    94  func (s TableFunc) CheckPrivileges(_ *sql.Context, _ sql.PrivilegedOperationChecker) bool {
    95  	return true
    96  }
    97  
    98  // CollationCoercibility implements the interface sql.CollationCoercible.
    99  func (TableFunc) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   100  	return sql.Collation_binary, 5
   101  }
   102  
   103  // Collation implements the sql.Table interface.
   104  func (TableFunc) Collation() sql.CollationID {
   105  	return sql.Collation_Default
   106  }
   107  
   108  func (s TableFunc) Expressions() []sql.Expression {
   109  	return []sql.Expression{}
   110  }
   111  
   112  func (s TableFunc) WithExpressions(e ...sql.Expression) (sql.Node, error) {
   113  	return s, nil
   114  }
   115  
   116  func (s TableFunc) Database() sql.Database {
   117  	return s.db
   118  }
   119  
   120  func (s TableFunc) WithDatabase(_ sql.Database) (sql.Node, error) {
   121  	return s, nil
   122  }
   123  
   124  func (s TableFunc) Name() string {
   125  	return "table_func"
   126  }
   127  
   128  func (s TableFunc) Description() string {
   129  	return "table function"
   130  }
   131  
   132  var _ sql.RowIter = (*TableFunctionRowIter)(nil)
   133  
   134  type TableFunctionRowIter struct {
   135  	val  interface{}
   136  	done bool
   137  }
   138  
   139  func (i *TableFunctionRowIter) Next(_ *sql.Context) (sql.Row, error) {
   140  	if i.done {
   141  		return nil, io.EOF
   142  	}
   143  	ret := sql.Row{i.val}
   144  	i.done = true
   145  	return ret, nil
   146  }
   147  
   148  func (i *TableFunctionRowIter) Close(_ *sql.Context) error {
   149  	return nil
   150  }