github.com/dolthub/go-mysql-server@v0.18.0/sql/plan/namedwindows.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 plan 16 17 import ( 18 "fmt" 19 "strings" 20 21 "github.com/dolthub/go-mysql-server/sql" 22 ) 23 24 // NamedWindows is a list of WINDOW clause definitions 25 // to be resolved and merged into OVER clause sql.Window 26 // nodes. 27 type NamedWindows struct { 28 UnaryNode 29 WindowDefs map[string]*sql.WindowDefinition 30 } 31 32 var _ sql.Node = (*NamedWindows)(nil) 33 var _ sql.CollationCoercible = (*NamedWindows)(nil) 34 35 func NewNamedWindows(windowDefs map[string]*sql.WindowDefinition, child sql.Node) *NamedWindows { 36 return &NamedWindows{ 37 UnaryNode: UnaryNode{Child: child}, 38 WindowDefs: windowDefs, 39 } 40 } 41 42 func (n *NamedWindows) IsReadOnly() bool { 43 return n.Child.IsReadOnly() 44 } 45 46 // String implements sql.Node 47 func (n *NamedWindows) String() string { 48 var sb strings.Builder 49 sb.WriteString("NamedWindows(") 50 var sep string 51 for n, def := range n.WindowDefs { 52 sb.WriteString(strings.ReplaceAll(fmt.Sprintf("%s%s %s", sep, n, def), "over", "as")) 53 sep = ", " 54 } 55 pr := sql.NewTreePrinter() 56 sb.WriteString(")") 57 _ = pr.WriteNode(sb.String()) 58 _ = pr.WriteChildren(n.Child.String()) 59 return pr.String() 60 } 61 62 // DebugString implements sql.Node 63 func (n *NamedWindows) DebugString() string { 64 var sb strings.Builder 65 sb.WriteString("NamedWindows(") 66 var sep string 67 for n, def := range n.WindowDefs { 68 sb.WriteString(strings.ReplaceAll(fmt.Sprintf("%s%s %s", sep, n, def.DebugString()), "over", "as")) 69 sep = ", " 70 } 71 pr := sql.NewTreePrinter() 72 sb.WriteString(")") 73 _ = pr.WriteNode(sb.String()) 74 _ = pr.WriteChildren(sql.DebugString(n.Child)) 75 return pr.String() 76 } 77 78 // RowIter implements sql.Node 79 func (n *NamedWindows) RowIter(ctx *sql.Context, row sql.Row) (sql.RowIter, error) { 80 panic("cannot iterate *plan.NamedWindows") 81 } 82 83 // WithChildren implements sql.Node 84 func (n *NamedWindows) WithChildren(nodes ...sql.Node) (sql.Node, error) { 85 if len(nodes) != 1 { 86 return nil, sql.ErrInvalidChildrenNumber.New(n, len(nodes), 1) 87 } 88 return NewNamedWindows(n.WindowDefs, nodes[0]), nil 89 } 90 91 // CheckPrivileges implements sql.Node 92 func (n *NamedWindows) CheckPrivileges(ctx *sql.Context, opChecker sql.PrivilegedOperationChecker) bool { 93 return n.Child.CheckPrivileges(ctx, opChecker) 94 } 95 96 // CollationCoercibility implements the interface sql.CollationCoercible. 97 func (n *NamedWindows) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 98 return sql.GetCoercibility(ctx, n.Child) 99 }