github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/aggregation/window/dense_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 DenseRank struct { 26 window *sql.WindowDefinition 27 pos int 28 id sql.ColumnId 29 } 30 31 var _ sql.FunctionExpression = (*DenseRank)(nil) 32 var _ sql.WindowAggregation = (*DenseRank)(nil) 33 var _ sql.WindowAdaptableExpression = (*DenseRank)(nil) 34 var _ sql.CollationCoercible = (*DenseRank)(nil) 35 36 func NewDenseRank() sql.Expression { 37 return &DenseRank{} 38 } 39 40 // Id implements sql.IdExpression 41 func (p *DenseRank) Id() sql.ColumnId { 42 return p.id 43 } 44 45 // WithId implements sql.IdExpression 46 func (p *DenseRank) 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 *DenseRank) Description() string { 54 return "returns rank value without gaps." 55 } 56 57 // Window implements sql.WindowExpression 58 func (p *DenseRank) Window() *sql.WindowDefinition { 59 return p.window 60 } 61 62 func (p *DenseRank) Resolved() bool { 63 return windowResolved(p.window) 64 } 65 66 func (p *DenseRank) String() string { 67 sb := strings.Builder{} 68 sb.WriteString("dense_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 *DenseRank) DebugString() string { 77 sb := strings.Builder{} 78 sb.WriteString("dense_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 *DenseRank) FunctionName() string { 88 return "DENSE_RANK" 89 } 90 91 // Type implements sql.Expression 92 func (p *DenseRank) Type() sql.Type { 93 return types.Uint64 94 } 95 96 // CollationCoercibility implements the interface sql.CollationCoercible. 97 func (*DenseRank) 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 *DenseRank) IsNullable() bool { 103 return false 104 } 105 106 // Eval implements sql.Expression 107 func (p *DenseRank) 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 *DenseRank) Children() []sql.Expression { 113 return p.window.ToExpressions() 114 } 115 116 // WithChildren implements sql.Expression 117 func (p *DenseRank) 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 *DenseRank) WithWindow(window *sql.WindowDefinition) sql.WindowAdaptableExpression { 128 nr := *p 129 nr.window = window 130 return &nr 131 } 132 133 func (p *DenseRank) NewWindowFunction() (sql.WindowFunction, error) { 134 return aggregation.NewDenseRank(p.window.OrderBy.ToExpressions()), nil 135 }