github.com/dolthub/go-mysql-server@v0.18.0/sql/window.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 sql
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/cespare/xxhash/v2"
    22  )
    23  
    24  func NewWindowDefinition(partitionBy []Expression, orderBy SortFields, frame WindowFrame, ref, name string) *WindowDefinition {
    25  	return &WindowDefinition{
    26  		PartitionBy: partitionBy,
    27  		OrderBy:     orderBy,
    28  		Frame:       frame,
    29  		Ref:         ref,
    30  		Name:        name,
    31  	}
    32  }
    33  
    34  // A WindowDefinition specifies the window parameters of a window function
    35  type WindowDefinition struct {
    36  	PartitionBy []Expression
    37  	OrderBy     SortFields
    38  	Frame       WindowFrame
    39  	Ref         string
    40  	Name        string
    41  	id          uint64
    42  }
    43  
    44  // ToExpressions converts the PartitionBy and OrderBy expressions to a single slice of expressions suitable for
    45  // manipulation by analyzer rules.
    46  func (w *WindowDefinition) ToExpressions() []Expression {
    47  	if w == nil {
    48  		return nil
    49  	}
    50  	return append(w.OrderBy.ToExpressions(), w.PartitionBy...)
    51  }
    52  
    53  // FromExpressions returns copy of this window with the given expressions taken to stand in for the partition and order
    54  // by fields. An error is returned if the lengths or types of these expressions are incompatible with this window.
    55  func (w *WindowDefinition) FromExpressions(children []Expression) (*WindowDefinition, error) {
    56  	if w == nil {
    57  		return nil, nil
    58  	}
    59  
    60  	if len(children) != len(w.OrderBy)+len(w.PartitionBy) {
    61  		return nil, ErrInvalidChildrenNumber.New(w, len(children), len(w.OrderBy)+len(w.PartitionBy))
    62  	}
    63  
    64  	nw := *w
    65  	nw.OrderBy = nw.OrderBy.FromExpressions(children[:len(nw.OrderBy)]...)
    66  	nw.PartitionBy = children[len(nw.OrderBy):]
    67  	return &nw, nil
    68  }
    69  
    70  func (w *WindowDefinition) String() string {
    71  	if w == nil {
    72  		return ""
    73  	}
    74  	sb := strings.Builder{}
    75  	sb.WriteString("over (")
    76  	if len(w.PartitionBy) > 0 {
    77  		sb.WriteString(" partition by ")
    78  		for i, expression := range w.PartitionBy {
    79  			if i > 0 {
    80  				sb.WriteString(", ")
    81  			}
    82  			sb.WriteString(expression.String())
    83  		}
    84  	}
    85  	if len(w.OrderBy) > 0 {
    86  		sb.WriteString(" order by ")
    87  		for i, ob := range w.OrderBy {
    88  			if i > 0 {
    89  				sb.WriteString(", ")
    90  			}
    91  			sb.WriteString(ob.String())
    92  		}
    93  	}
    94  	if w.Frame != nil {
    95  		sb.WriteString(fmt.Sprintf(" %s", w.Frame.String()))
    96  	}
    97  	sb.WriteString(")")
    98  	return sb.String()
    99  }
   100  
   101  func (w *WindowDefinition) PartitionId() (uint64, error) {
   102  	if w == nil {
   103  		return 0, nil
   104  	}
   105  	if w.id != uint64(0) {
   106  		return w.id, nil
   107  	}
   108  	sb := strings.Builder{}
   109  	if len(w.PartitionBy) > 0 {
   110  		for _, expression := range w.PartitionBy {
   111  			sb.WriteString(expression.String())
   112  		}
   113  	}
   114  	if len(w.OrderBy) > 0 {
   115  		for _, ob := range w.OrderBy {
   116  			sb.WriteString(ob.String())
   117  		}
   118  	}
   119  	hash := xxhash.New()
   120  	_, err := hash.Write([]byte(sb.String()))
   121  	if err != nil {
   122  		return 0, err
   123  	}
   124  	w.id = hash.Sum64()
   125  	return w.id, nil
   126  }
   127  
   128  func (w *WindowDefinition) DebugString() string {
   129  	if w == nil {
   130  		return ""
   131  	}
   132  	return w.String()
   133  }