github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/function/aggregation/window/lead.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 Lead struct { 29 window *sql.WindowDefinition 30 expression.NaryExpression 31 offset int 32 pos int 33 id sql.ColumnId 34 } 35 36 var _ sql.FunctionExpression = (*Lead)(nil) 37 var _ sql.WindowAggregation = (*Lead)(nil) 38 var _ sql.WindowAdaptableExpression = (*Lead)(nil) 39 var _ sql.CollationCoercible = (*Lead)(nil) 40 41 // NewLead accepts variadic arguments to create a new Lead node: 42 // If 1 expression, use default values for [default] and [offset] 43 // If 2 expressions, use default value for [default] 44 // 3 input expression match to [child], [offset], and [default] arguments 45 // The offset is constrained to a non-negative integer expression.Literal. 46 // TODO: support user-defined variable offset 47 func NewLead(e ...sql.Expression) (*Lead, error) { 48 switch len(e) { 49 case 1: 50 return &Lead{NaryExpression: expression.NaryExpression{ChildExpressions: e[:1]}, offset: 1}, nil 51 case 2: 52 offset, err := expression.LiteralToInt(e[1]) 53 if err != nil { 54 return nil, err 55 } 56 return &Lead{NaryExpression: expression.NaryExpression{ChildExpressions: e[:1]}, offset: offset}, nil 57 case 3: 58 offset, err := expression.LiteralToInt(e[1]) 59 if err != nil { 60 return nil, err 61 } 62 return &Lead{NaryExpression: expression.NaryExpression{ChildExpressions: []sql.Expression{e[0], e[2]}}, offset: offset}, nil 63 } 64 return nil, sql.ErrInvalidArgumentNumber.New("LEAD", "1, 2, or 3", len(e)) 65 } 66 67 // Id implements sql.IdExpression 68 func (l *Lead) Id() sql.ColumnId { 69 return l.id 70 } 71 72 // WithId implements sql.IdExpression 73 func (l *Lead) WithId(id sql.ColumnId) sql.IdExpression { 74 ret := *l 75 ret.id = id 76 return &ret 77 } 78 79 // Description implements sql.FunctionExpression 80 func (l *Lead) Description() string { 81 return "returns the value of the expression evaluated at the lead offset row" 82 } 83 84 // Window implements sql.WindowExpression 85 func (l *Lead) Window() *sql.WindowDefinition { 86 return l.window 87 } 88 89 // IsNullable implements sql.Expression 90 func (l *Lead) Resolved() bool { 91 childrenResolved := true 92 for _, c := range l.ChildExpressions { 93 childrenResolved = childrenResolved && c.Resolved() 94 } 95 return childrenResolved && windowResolved(l.window) 96 } 97 98 func (l *Lead) String() string { 99 sb := strings.Builder{} 100 if len(l.ChildExpressions) > 1 { 101 sb.WriteString(fmt.Sprintf("lead(%s, %d, %s)", l.ChildExpressions[0].String(), l.offset, l.ChildExpressions[1])) 102 } else { 103 sb.WriteString(fmt.Sprintf("lead(%s, %d)", l.ChildExpressions[0].String(), l.offset)) 104 } 105 if l.window != nil { 106 sb.WriteString(" ") 107 sb.WriteString(l.window.String()) 108 } 109 return sb.String() 110 } 111 112 func (l *Lead) DebugString() string { 113 sb := strings.Builder{} 114 if len(l.ChildExpressions) > 1 { 115 sb.WriteString(fmt.Sprintf("lead(%s, %d, %s)", l.ChildExpressions[0].String(), l.offset, l.ChildExpressions[1])) 116 } else { 117 sb.WriteString(fmt.Sprintf("lead(%s, %d)", l.ChildExpressions[0].String(), l.offset)) 118 } 119 if l.window != nil { 120 sb.WriteString(" ") 121 sb.WriteString(sql.DebugString(l.window)) 122 } 123 return sb.String() 124 } 125 126 // FunctionName implements sql.FunctionExpression 127 func (l *Lead) FunctionName() string { 128 return "LEAD" 129 } 130 131 // Type implements sql.Expression 132 func (l *Lead) Type() sql.Type { 133 return l.ChildExpressions[0].Type() 134 } 135 136 // CollationCoercibility implements the interface sql.CollationCoercible. 137 func (l *Lead) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 138 // We use the first child for the Type, so we'll use the first child for the coercibility as well 139 if l == nil || len(l.ChildExpressions) == 0 { 140 return sql.Collation_binary, 6 141 } 142 return sql.GetCoercibility(ctx, l.ChildExpressions[0]) 143 } 144 145 // IsNullable implements sql.Expression 146 func (l *Lead) IsNullable() bool { 147 return true 148 } 149 150 // Eval implements sql.Expression 151 func (l *Lead) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 152 return nil, sql.ErrWindowUnsupported.New(l.FunctionName()) 153 } 154 155 // Children implements sql.Expression 156 func (l *Lead) Children() []sql.Expression { 157 if l == nil { 158 return nil 159 } 160 return append(l.window.ToExpressions(), l.ChildExpressions...) 161 } 162 163 // WithChildren implements sql.Expression 164 func (l *Lead) WithChildren(children ...sql.Expression) (sql.Expression, error) { 165 if len(children) < 2 { 166 return nil, sql.ErrInvalidChildrenNumber.New(l, len(children), 2) 167 } 168 169 nl := *l 170 numWindowExpr := len(children) - len(l.ChildExpressions) 171 window, err := l.window.FromExpressions(children[:numWindowExpr]) 172 if err != nil { 173 return nil, err 174 } 175 176 nl.ChildExpressions = children[numWindowExpr:] 177 nl.window = window 178 179 return &nl, nil 180 } 181 182 // WithWindow implements sql.WindowAggregation 183 func (l *Lead) WithWindow(window *sql.WindowDefinition) sql.WindowAdaptableExpression { 184 nl := *l 185 nl.window = window 186 return &nl 187 } 188 189 func (l *Lead) NewWindowFunction() (sql.WindowFunction, error) { 190 c, err := transform.Clone(l.ChildExpressions[0]) 191 if err != nil { 192 return nil, err 193 } 194 var def sql.Expression 195 if len(l.ChildExpressions) > 1 { 196 def, err = transform.Clone(l.ChildExpressions[1]) 197 if err != nil { 198 return nil, err 199 } 200 } 201 return aggregation.NewLead(c, def, l.offset), nil 202 }