github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/aggregation/window/rank.go (about)

     1  // Copyright 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 window
    16  
    17  import (
    18  	"strings"
    19  
    20  	"github.com/dolthub/go-mysql-server/sql"
    21  	"github.com/dolthub/go-mysql-server/sql/expression/function/aggregation"
    22  	"github.com/dolthub/go-mysql-server/sql/types"
    23  )
    24  
    25  type Rank struct {
    26  	window *sql.WindowDefinition
    27  	pos    int
    28  	id     sql.ColumnId
    29  }
    30  
    31  var _ sql.FunctionExpression = (*Rank)(nil)
    32  var _ sql.WindowAggregation = (*Rank)(nil)
    33  var _ sql.WindowAdaptableExpression = (*Rank)(nil)
    34  var _ sql.CollationCoercible = (*Rank)(nil)
    35  
    36  func NewRank() sql.Expression {
    37  	return &Rank{}
    38  }
    39  
    40  // Id implements sql.IdExpression
    41  func (p *Rank) Id() sql.ColumnId {
    42  	return p.id
    43  }
    44  
    45  // WithId implements sql.IdExpression
    46  func (p *Rank) WithId(id sql.ColumnId) sql.IdExpression {
    47  	ret := *p
    48  	ret.id = id
    49  	return &ret
    50  }
    51  
    52  // Description implements sql.FunctionExpression
    53  func (p *Rank) Description() string {
    54  	return "returns rank value."
    55  }
    56  
    57  // Window implements sql.WindowExpression
    58  func (p *Rank) Window() *sql.WindowDefinition {
    59  	return p.window
    60  }
    61  
    62  func (p *Rank) Resolved() bool {
    63  	return windowResolved(p.window)
    64  }
    65  
    66  func (p *Rank) String() string {
    67  	sb := strings.Builder{}
    68  	sb.WriteString("rank()")
    69  	if p.window != nil {
    70  		sb.WriteString(" ")
    71  		sb.WriteString(p.window.String())
    72  	}
    73  	return sb.String()
    74  }
    75  
    76  func (p *Rank) DebugString() string {
    77  	sb := strings.Builder{}
    78  	sb.WriteString("rank()")
    79  	if p.window != nil {
    80  		sb.WriteString(" ")
    81  		sb.WriteString(sql.DebugString(p.window))
    82  	}
    83  	return sb.String()
    84  }
    85  
    86  // FunctionName implements sql.FunctionExpression
    87  func (p *Rank) FunctionName() string {
    88  	return "RANK"
    89  }
    90  
    91  // Type implements sql.Expression
    92  func (p *Rank) Type() sql.Type {
    93  	return types.Uint64
    94  }
    95  
    96  // CollationCoercibility implements the interface sql.CollationCoercible.
    97  func (*Rank) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
    98  	return sql.Collation_binary, 5
    99  }
   100  
   101  // IsNullable implements sql.Expression
   102  func (p *Rank) IsNullable() bool {
   103  	return false
   104  }
   105  
   106  // Eval implements sql.Expression
   107  func (p *Rank) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
   108  	return nil, sql.ErrWindowUnsupported.New(p.FunctionName())
   109  }
   110  
   111  // Children implements sql.Expression
   112  func (p *Rank) Children() []sql.Expression {
   113  	return p.window.ToExpressions()
   114  }
   115  
   116  // WithChildren implements sql.Expression
   117  func (p *Rank) WithChildren(children ...sql.Expression) (sql.Expression, error) {
   118  	window, err := p.window.FromExpressions(children)
   119  	if err != nil {
   120  		return nil, err
   121  	}
   122  
   123  	return p.WithWindow(window), nil
   124  }
   125  
   126  // WithWindow implements sql.WindowAggregation
   127  func (p *Rank) WithWindow(window *sql.WindowDefinition) sql.WindowAdaptableExpression {
   128  	nr := *p
   129  	nr.window = window
   130  	return &nr
   131  }
   132  
   133  func (p *Rank) NewWindowFunction() (sql.WindowFunction, error) {
   134  	return aggregation.NewRank(p.window.OrderBy.ToExpressions()), nil
   135  }