github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/aggregation/window/last_value.go (about) 1 // Copyright 2022 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 "fmt" 19 "strings" 20 21 "github.com/dolthub/go-mysql-server/sql/transform" 22 23 "github.com/dolthub/go-mysql-server/sql" 24 "github.com/dolthub/go-mysql-server/sql/expression" 25 "github.com/dolthub/go-mysql-server/sql/expression/function/aggregation" 26 ) 27 28 type LastValue struct { 29 window *sql.WindowDefinition 30 expression.UnaryExpression 31 pos int 32 id sql.ColumnId 33 } 34 35 var _ sql.FunctionExpression = (*LastValue)(nil) 36 var _ sql.WindowAggregation = (*LastValue)(nil) 37 var _ sql.WindowAdaptableExpression = (*LastValue)(nil) 38 var _ sql.CollationCoercible = (*LastValue)(nil) 39 40 func NewLastValue(e sql.Expression) sql.Expression { 41 return &LastValue{window: nil, UnaryExpression: expression.UnaryExpression{Child: e}} 42 } 43 44 // Id implements sql.IdExpression 45 func (f *LastValue) Id() sql.ColumnId { 46 return f.id 47 } 48 49 // WithId implements sql.IdExpression 50 func (f *LastValue) WithId(id sql.ColumnId) sql.IdExpression { 51 ret := *f 52 ret.id = id 53 return &ret 54 } 55 56 // Description implements sql.FunctionExpression 57 func (f *LastValue) Description() string { 58 return "returns value of argument from last row of window frame." 59 } 60 61 // Window implements sql.WindowExpression 62 func (f *LastValue) Window() *sql.WindowDefinition { 63 return f.window 64 } 65 66 // IsNullable implements sql.Expression 67 func (f *LastValue) Resolved() bool { 68 return windowResolved(f.window) 69 } 70 71 func (f *LastValue) String() string { 72 sb := strings.Builder{} 73 sb.WriteString(fmt.Sprintf("last_value(%s)", f.Child.String())) 74 if f.window != nil { 75 sb.WriteString(" ") 76 sb.WriteString(f.window.String()) 77 } 78 return sb.String() 79 } 80 81 func (f *LastValue) DebugString() string { 82 sb := strings.Builder{} 83 sb.WriteString(fmt.Sprintf("last_value(%s)", f.Child.String())) 84 if f.window != nil { 85 sb.WriteString(" ") 86 sb.WriteString(sql.DebugString(f.window)) 87 } 88 return sb.String() 89 } 90 91 // FunctionName implements sql.FunctionExpression 92 func (f *LastValue) FunctionName() string { 93 return "LAST_VALUE" 94 } 95 96 // Type implements sql.Expression 97 func (f *LastValue) Type() sql.Type { 98 return f.Child.Type() 99 } 100 101 // CollationCoercibility implements the interface sql.CollationCoercible. 102 func (f *LastValue) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 103 return sql.GetCoercibility(ctx, f.Child) 104 } 105 106 // IsNullable implements sql.Expression 107 func (f *LastValue) IsNullable() bool { 108 return false 109 } 110 111 // Eval implements sql.Expression 112 func (f *LastValue) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 113 return nil, sql.ErrWindowUnsupported.New(f.FunctionName()) 114 } 115 116 // Children implements sql.Expression 117 func (f *LastValue) Children() []sql.Expression { 118 if f == nil { 119 return nil 120 } 121 return append(f.window.ToExpressions(), f.Child) 122 } 123 124 // WithChildren implements sql.Expression 125 func (f *LastValue) WithChildren(children ...sql.Expression) (sql.Expression, error) { 126 if len(children) < 2 { 127 return nil, sql.ErrInvalidChildrenNumber.New(f, len(children), 2) 128 } 129 130 nf := *f 131 window, err := f.window.FromExpressions(children[:len(children)-1]) 132 if err != nil { 133 return nil, err 134 } 135 136 nf.Child = children[len(children)-1] 137 nf.window = window 138 139 return &nf, nil 140 } 141 142 // WithWindow implements sql.WindowAggregation 143 func (f *LastValue) WithWindow(window *sql.WindowDefinition) sql.WindowAdaptableExpression { 144 nr := *f 145 nr.window = window 146 return &nr 147 } 148 149 func (f *LastValue) NewWindowFunction() (sql.WindowFunction, error) { 150 c, err := transform.Clone(f.Child) 151 if err != nil { 152 return nil, err 153 } 154 return aggregation.NewLastAgg(c).WithWindow(f.window) 155 }