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