github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/unresolved.go (about)

     1  // Copyright 2020-2021 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 expression
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"gopkg.in/src-d/go-errors.v1"
    22  
    23  	"github.com/dolthub/go-mysql-server/sql"
    24  )
    25  
    26  // UnresolvedColumn is an expression of a column that is not yet resolved.
    27  // This is a placeholder node, so its methods Type, IsNullable and Eval are not
    28  // supposed to be called.
    29  type UnresolvedColumn struct {
    30  	name  string
    31  	table string
    32  }
    33  
    34  var _ sql.Expression = (*UnresolvedColumn)(nil)
    35  var _ sql.Expression2 = (*UnresolvedColumn)(nil)
    36  var _ sql.CollationCoercible = (*UnresolvedColumn)(nil)
    37  
    38  // NewUnresolvedColumn creates a new UnresolvedColumn expression.
    39  func NewUnresolvedColumn(name string) *UnresolvedColumn {
    40  	return &UnresolvedColumn{name: name}
    41  }
    42  
    43  // NewUnresolvedQualifiedColumn creates a new UnresolvedColumn expression
    44  // with a table qualifier.
    45  func NewUnresolvedQualifiedColumn(table, name string) *UnresolvedColumn {
    46  	return &UnresolvedColumn{name: name, table: table}
    47  }
    48  
    49  // Children implements the Expression interface.
    50  func (*UnresolvedColumn) Children() []sql.Expression {
    51  	return nil
    52  }
    53  
    54  // Resolved implements the Expression interface.
    55  func (*UnresolvedColumn) Resolved() bool {
    56  	return false
    57  }
    58  
    59  // IsNullable implements the Expression interface.
    60  func (*UnresolvedColumn) IsNullable() bool {
    61  	panic("unresolved column is a placeholder node, but IsNullable was called")
    62  }
    63  
    64  // Type implements the Expression interface.
    65  func (*UnresolvedColumn) Type() sql.Type {
    66  	panic("unresolved column is a placeholder node, but Type was called")
    67  }
    68  
    69  // CollationCoercibility implements the interface sql.CollationCoercible.
    70  func (*UnresolvedColumn) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    71  	return sql.Collation_binary, 7
    72  }
    73  
    74  func (uc *UnresolvedColumn) Eval2(ctx *sql.Context, row sql.Row2) (sql.Value, error) {
    75  	panic("unresolved column is a placeholder node, but Eval2 was called")
    76  }
    77  
    78  func (uc *UnresolvedColumn) Type2() sql.Type2 {
    79  	panic("unresolved column is a placeholder node, but Type2 was called")
    80  }
    81  
    82  // Name implements the Nameable interface.
    83  func (uc *UnresolvedColumn) Name() string { return uc.name }
    84  
    85  // Table returns the table name.
    86  func (uc *UnresolvedColumn) Table() string { return uc.table }
    87  
    88  func (uc *UnresolvedColumn) String() string {
    89  	if uc.table == "" {
    90  		return uc.name
    91  	}
    92  	return fmt.Sprintf("%s.%s", uc.table, uc.name)
    93  }
    94  
    95  // Eval implements the Expression interface.
    96  func (*UnresolvedColumn) Eval(ctx *sql.Context, r sql.Row) (interface{}, error) {
    97  	panic("unresolved column is a placeholder node, but Eval was called")
    98  }
    99  
   100  // WithChildren implements the Expression interface.
   101  func (uc *UnresolvedColumn) WithChildren(children ...sql.Expression) (sql.Expression, error) {
   102  	if len(children) != 0 {
   103  		return nil, sql.ErrInvalidChildrenNumber.New(uc, len(children), 0)
   104  	}
   105  	return uc, nil
   106  }
   107  
   108  // ErrUnresolvedTableFunction is thrown when a table function cannot be resolved
   109  var ErrUnresolvedTableFunction = errors.NewKind("unresolved table function")
   110  
   111  var _ sql.TableFunction = (*UnresolvedTableFunction)(nil)
   112  
   113  // UnresolvedTableFunction represents a table function that is not yet resolved.
   114  // This is a placeholder node, so methods such as Schema, RowIter, etc, are not
   115  // intended to be used.
   116  type UnresolvedTableFunction struct {
   117  	name      string
   118  	Alias     string
   119  	Arguments []sql.Expression
   120  	database  sql.Database
   121  }
   122  
   123  var _ sql.Node = (*UnresolvedTableFunction)(nil)
   124  var _ sql.CollationCoercible = (*UnresolvedTableFunction)(nil)
   125  var _ sql.RenameableNode = (*UnresolvedTableFunction)(nil)
   126  
   127  // NewUnresolvedTableFunction creates a new UnresolvedTableFunction node for a sql plan.
   128  func NewUnresolvedTableFunction(name string, arguments []sql.Expression) *UnresolvedTableFunction {
   129  	return &UnresolvedTableFunction{
   130  		name:      name,
   131  		Arguments: arguments,
   132  	}
   133  }
   134  
   135  func (utf *UnresolvedTableFunction) WithName(s string) sql.Node {
   136  	ret := *utf
   137  	ret.name = s
   138  	return &ret
   139  }
   140  
   141  // NewInstance implements the TableFunction interface
   142  func (utf *UnresolvedTableFunction) NewInstance(_ *sql.Context, _ sql.Database, _ []sql.Expression) (sql.Node, error) {
   143  	return nil, ErrUnresolvedTableFunction.New()
   144  }
   145  
   146  // Database implements the Databaser interface
   147  func (utf *UnresolvedTableFunction) Database() sql.Database {
   148  	return utf.database
   149  }
   150  
   151  // WithDatabase implements the Databaser interface
   152  func (utf *UnresolvedTableFunction) WithDatabase(database sql.Database) (sql.Node, error) {
   153  	utf.database = database
   154  	return utf, nil
   155  }
   156  
   157  // Name implements the TableFunction interface
   158  func (utf *UnresolvedTableFunction) Name() string {
   159  	return utf.name
   160  }
   161  
   162  // Expressions implements the Expressioner interface
   163  func (utf *UnresolvedTableFunction) Expressions() []sql.Expression {
   164  	return utf.Arguments
   165  }
   166  
   167  func (utf *UnresolvedTableFunction) IsReadOnly() bool {
   168  	return true
   169  }
   170  
   171  // WithExpressions implements the Expressioner interface
   172  func (utf *UnresolvedTableFunction) WithExpressions(expression ...sql.Expression) (sql.Node, error) {
   173  	if len(expression) != len(utf.Expressions()) {
   174  		return nil, sql.ErrInvalidExpressionNumber.New(utf, len(expression), len(utf.Expressions()))
   175  	}
   176  
   177  	nutf := *utf
   178  	nutf.Arguments = make([]sql.Expression, len(expression))
   179  	for i, _ := range expression {
   180  		nutf.Arguments[i] = expression[i]
   181  	}
   182  
   183  	return &nutf, nil
   184  }
   185  
   186  // Schema implements the Node interface
   187  func (utf *UnresolvedTableFunction) Schema() sql.Schema {
   188  	return nil
   189  }
   190  
   191  // Children implements the Node interface
   192  func (utf *UnresolvedTableFunction) Children() []sql.Node {
   193  	return nil
   194  }
   195  
   196  // RowIter implements the Node interface
   197  func (utf *UnresolvedTableFunction) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) {
   198  	return nil, ErrUnresolvedTableFunction.New()
   199  }
   200  
   201  // WithChildren implements the Node interface
   202  func (utf *UnresolvedTableFunction) WithChildren(node ...sql.Node) (sql.Node, error) {
   203  	panic("no expected children for unresolved table function")
   204  }
   205  
   206  // CheckPrivileges implements the Node interface
   207  func (utf UnresolvedTableFunction) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool {
   208  	panic("attempting to check privileges on an unresolved table function")
   209  	return false
   210  }
   211  
   212  // CollationCoercibility implements the interface sql.CollationCoercible.
   213  func (UnresolvedTableFunction) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   214  	return sql.Collation_binary, 7
   215  }
   216  
   217  // Resolved implements the Resolvable interface
   218  func (utf *UnresolvedTableFunction) Resolved() bool {
   219  	return false
   220  }
   221  
   222  // String implements the Stringer interface
   223  func (utf *UnresolvedTableFunction) String() string {
   224  	var exprs = make([]string, len(utf.Arguments))
   225  	for i, e := range utf.Arguments {
   226  		exprs[i] = e.String()
   227  	}
   228  
   229  	return fmt.Sprintf("%s(%s)", utf.name, strings.Join(exprs, ", "))
   230  }
   231  
   232  var _ sql.Expression = (*UnresolvedFunction)(nil)
   233  var _ sql.CollationCoercible = (*UnresolvedFunction)(nil)
   234  
   235  // UnresolvedFunction represents a function that is not yet resolved.
   236  // This is a placeholder node, so its methods Type, IsNullable and Eval are not
   237  // supposed to be called.
   238  type UnresolvedFunction struct {
   239  	name string
   240  	// IsAggregate or not.
   241  	IsAggregate bool
   242  	// Window is the window for this function, if present
   243  	Window *sql.WindowDefinition
   244  	// Children of the expression.
   245  	Arguments []sql.Expression
   246  }
   247  
   248  // NewUnresolvedFunction creates a new UnresolvedFunction expression.
   249  func NewUnresolvedFunction(
   250  	name string,
   251  	agg bool,
   252  	window *sql.WindowDefinition,
   253  	arguments ...sql.Expression,
   254  ) *UnresolvedFunction {
   255  	return &UnresolvedFunction{
   256  		name:        name,
   257  		IsAggregate: agg,
   258  		Window:      window,
   259  		Arguments:   arguments,
   260  	}
   261  }
   262  
   263  // Children implements the Expression interface.
   264  func (uf *UnresolvedFunction) Children() []sql.Expression {
   265  	return append(uf.Arguments, uf.Window.ToExpressions()...)
   266  }
   267  
   268  // WithWindow implements the Expression interface.
   269  func (uf *UnresolvedFunction) WithWindow(def *sql.WindowDefinition) *UnresolvedFunction {
   270  	nf := *uf
   271  	nf.Window = def
   272  	return &nf
   273  }
   274  
   275  // Resolved implements the Expression interface.
   276  func (*UnresolvedFunction) Resolved() bool {
   277  	return false
   278  }
   279  
   280  // IsNullable implements the Expression interface.
   281  func (*UnresolvedFunction) IsNullable() bool {
   282  	panic("unresolved function is a placeholder node, but IsNullable was called")
   283  }
   284  
   285  // Type implements the Expression interface.
   286  func (*UnresolvedFunction) Type() sql.Type {
   287  	panic("unresolved function is a placeholder node, but Type was called")
   288  }
   289  
   290  // CollationCoercibility implements the interface sql.CollationCoercible.
   291  func (*UnresolvedFunction) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   292  	return sql.Collation_binary, 7
   293  }
   294  
   295  // Name implements the Nameable interface.
   296  func (uf *UnresolvedFunction) Name() string { return uf.name }
   297  
   298  func (uf *UnresolvedFunction) String() string {
   299  	var exprs = make([]string, len(uf.Arguments))
   300  	for i, e := range uf.Arguments {
   301  		exprs[i] = e.String()
   302  	}
   303  
   304  	over := ""
   305  	if uf.Window != nil {
   306  		over = fmt.Sprintf(" %s", uf.Window)
   307  	}
   308  
   309  	return fmt.Sprintf("%s(%s)%s", uf.name, strings.Join(exprs, ", "), over)
   310  }
   311  
   312  func (uf *UnresolvedFunction) DebugString() string {
   313  	var exprs = make([]string, len(uf.Arguments))
   314  	for i, e := range uf.Arguments {
   315  		exprs[i] = sql.DebugString(e)
   316  	}
   317  
   318  	over := ""
   319  	if uf.Window != nil {
   320  		over = fmt.Sprintf(" %s", sql.DebugString(uf.Window))
   321  	}
   322  
   323  	return fmt.Sprintf("(unresolved)%s(%s)%s", uf.name, strings.Join(exprs, ", "), over)
   324  }
   325  
   326  // Eval implements the Expression interface.
   327  func (*UnresolvedFunction) Eval(ctx *sql.Context, r sql.Row) (interface{}, error) {
   328  	panic("unresolved function is a placeholder node, but Eval was called")
   329  }
   330  
   331  // WithChildren implements the Expression interface.
   332  func (uf *UnresolvedFunction) WithChildren(children ...sql.Expression) (sql.Expression, error) {
   333  	if len(children) != len(uf.Arguments)+len(uf.Window.ToExpressions()) {
   334  		return nil, sql.ErrInvalidChildrenNumber.New(uf, len(children), len(uf.Arguments)+len(uf.Window.ToExpressions()))
   335  	}
   336  
   337  	window, err := uf.Window.FromExpressions(children[len(uf.Arguments):])
   338  	if err != nil {
   339  		return nil, err
   340  	}
   341  
   342  	return NewUnresolvedFunction(uf.name, uf.IsAggregate, window, children[:len(uf.Arguments)]...), nil
   343  }