github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/alias.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  
    20  	"github.com/dolthub/go-mysql-server/sql"
    21  	"github.com/dolthub/go-mysql-server/sql/types"
    22  )
    23  
    24  // AliasReference is a named reference to an aliased expression.
    25  type AliasReference struct {
    26  	name string
    27  }
    28  
    29  // NewAliasReference creates a new AliasReference from the specified alias name.
    30  func NewAliasReference(name string) *AliasReference {
    31  	return &AliasReference{name}
    32  }
    33  
    34  func (a AliasReference) Name() string {
    35  	return a.name
    36  }
    37  
    38  func (a AliasReference) Table() string {
    39  	return ""
    40  }
    41  
    42  func (a AliasReference) String() string {
    43  	return fmt.Sprintf("(alias reference)%s", a.name)
    44  }
    45  
    46  func (a AliasReference) Resolved() bool {
    47  	return false
    48  }
    49  
    50  func (a AliasReference) IsNullable() bool {
    51  	return true
    52  }
    53  
    54  func (a AliasReference) Children() []sql.Expression {
    55  	return []sql.Expression{}
    56  }
    57  
    58  func (a AliasReference) Type() sql.Type {
    59  	return types.Null
    60  }
    61  
    62  // CollationCoercibility implements the interface sql.CollationCoercible.
    63  func (AliasReference) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    64  	return sql.Collation_binary, 7
    65  }
    66  
    67  func (a AliasReference) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
    68  	return nil, fmt.Errorf("tried to call eval on an unresolved AliasReference")
    69  }
    70  
    71  func (a AliasReference) WithChildren(children ...sql.Expression) (sql.Expression, error) {
    72  	if len(children) != 0 {
    73  		return nil, sql.ErrInvalidChildrenNumber.New(a, len(children), 0)
    74  	}
    75  	return NewAliasReference(a.name), nil
    76  }
    77  
    78  var _ sql.Expression = (*AliasReference)(nil)
    79  var _ sql.CollationCoercible = (*AliasReference)(nil)
    80  
    81  // Alias is a node that gives a name to an expression.
    82  type Alias struct {
    83  	UnaryExpression
    84  	name           string
    85  	unreferencable bool
    86  	id             sql.ColumnId
    87  }
    88  
    89  var _ sql.Expression = (*Alias)(nil)
    90  var _ sql.IdExpression = (*Alias)(nil)
    91  var _ sql.CollationCoercible = (*Alias)(nil)
    92  
    93  // NewAlias returns a new Alias node.
    94  func NewAlias(name string, expr sql.Expression) *Alias {
    95  	return &Alias{UnaryExpression{expr}, name, false, 0}
    96  }
    97  
    98  // AsUnreferencable marks the alias outside of scope referencing
    99  func (e *Alias) AsUnreferencable() *Alias {
   100  	ret := *e
   101  	ret.unreferencable = true
   102  	return &ret
   103  }
   104  
   105  func (e *Alias) Unreferencable() bool {
   106  	return e.unreferencable
   107  }
   108  
   109  func (e *Alias) WithId(id sql.ColumnId) sql.IdExpression {
   110  	ret := *e
   111  	ret.id = id
   112  	return &ret
   113  }
   114  
   115  func (e *Alias) Id() sql.ColumnId {
   116  	return e.id
   117  }
   118  
   119  // Type returns the type of the expression.
   120  func (e *Alias) Type() sql.Type {
   121  	return e.Child.Type()
   122  }
   123  
   124  // CollationCoercibility implements the interface sql.CollationCoercible.
   125  func (e *Alias) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
   126  	return sql.GetCoercibility(ctx, e.Child)
   127  }
   128  
   129  // Eval implements the Expression interface.
   130  func (e *Alias) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
   131  	return e.Child.Eval(ctx, row)
   132  }
   133  
   134  func (e *Alias) String() string {
   135  	return fmt.Sprintf("%s as %s", e.Child, e.name)
   136  }
   137  
   138  func (e *Alias) DebugString() string {
   139  	return fmt.Sprintf("%s as %s", sql.DebugString(e.Child), e.name)
   140  }
   141  
   142  // WithChildren implements the Expression interface.
   143  func (e *Alias) WithChildren(children ...sql.Expression) (sql.Expression, error) {
   144  	if len(children) != 1 {
   145  		return nil, sql.ErrInvalidChildrenNumber.New(e, len(children), 1)
   146  	}
   147  	return NewAlias(e.name, children[0]), nil
   148  }
   149  
   150  // Name implements the Nameable interface.
   151  func (e *Alias) Name() string { return e.name }