github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/treewindow/constants.go (about)

     1  // Copyright 2022 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package treewindow
    12  
    13  import (
    14  	"github.com/cockroachdb/errors"
    15  	"github.com/cockroachdb/redact"
    16  )
    17  
    18  // WindowFrameMode indicates which mode of framing is used.
    19  type WindowFrameMode int
    20  
    21  const (
    22  	// RANGE is the mode of specifying frame in terms of logical range (e.g. 100 units cheaper).
    23  	RANGE WindowFrameMode = iota
    24  	// ROWS is the mode of specifying frame in terms of physical offsets (e.g. 1 row before etc).
    25  	ROWS
    26  	// GROUPS is the mode of specifying frame in terms of peer groups.
    27  	GROUPS
    28  )
    29  
    30  // Name returns a string representation of the window frame mode to be used in
    31  // struct names for generated code.
    32  func (m WindowFrameMode) Name() string {
    33  	switch m {
    34  	case RANGE:
    35  		return "Range"
    36  	case ROWS:
    37  		return "Rows"
    38  	case GROUPS:
    39  		return "Groups"
    40  	}
    41  	return ""
    42  }
    43  
    44  func (m WindowFrameMode) String() string {
    45  	switch m {
    46  	case RANGE:
    47  		return "RANGE"
    48  	case ROWS:
    49  		return "ROWS"
    50  	case GROUPS:
    51  		return "GROUPS"
    52  	}
    53  	return ""
    54  }
    55  
    56  // WindowFrameBoundType indicates which type of boundary is used.
    57  type WindowFrameBoundType int
    58  
    59  const (
    60  	// UnboundedPreceding represents UNBOUNDED PRECEDING type of boundary.
    61  	UnboundedPreceding WindowFrameBoundType = iota
    62  	// OffsetPreceding represents 'value' PRECEDING type of boundary.
    63  	OffsetPreceding
    64  	// CurrentRow represents CURRENT ROW type of boundary.
    65  	CurrentRow
    66  	// OffsetFollowing represents 'value' FOLLOWING type of boundary.
    67  	OffsetFollowing
    68  	// UnboundedFollowing represents UNBOUNDED FOLLOWING type of boundary.
    69  	UnboundedFollowing
    70  )
    71  
    72  // IsOffset returns true if the WindowFrameBoundType is an offset.
    73  func (ft WindowFrameBoundType) IsOffset() bool {
    74  	return ft == OffsetPreceding || ft == OffsetFollowing
    75  }
    76  
    77  // Name returns a string representation of the bound type to be used in struct
    78  // names for generated code.
    79  func (ft WindowFrameBoundType) Name() string {
    80  	switch ft {
    81  	case UnboundedPreceding:
    82  		return "UnboundedPreceding"
    83  	case OffsetPreceding:
    84  		return "OffsetPreceding"
    85  	case CurrentRow:
    86  		return "CurrentRow"
    87  	case OffsetFollowing:
    88  		return "OffsetFollowing"
    89  	case UnboundedFollowing:
    90  		return "UnboundedFollowing"
    91  	}
    92  	return ""
    93  }
    94  
    95  func (ft WindowFrameBoundType) String() string {
    96  	switch ft {
    97  	case UnboundedPreceding:
    98  		return "UNBOUNDED PRECEDING"
    99  	case OffsetPreceding:
   100  		return "OFFSET PRECEDING"
   101  	case CurrentRow:
   102  		return "CURRENT ROW"
   103  	case OffsetFollowing:
   104  		return "OFFSET FOLLOWING"
   105  	case UnboundedFollowing:
   106  		return "UNBOUNDED FOLLOWING"
   107  	}
   108  	return ""
   109  }
   110  
   111  // WindowFrameExclusion indicates which mode of exclusion is used.
   112  type WindowFrameExclusion int
   113  
   114  const (
   115  	// NoExclusion represents an omitted frame exclusion clause.
   116  	NoExclusion WindowFrameExclusion = iota
   117  	// ExcludeCurrentRow represents EXCLUDE CURRENT ROW mode of frame exclusion.
   118  	ExcludeCurrentRow
   119  	// ExcludeGroup represents EXCLUDE GROUP mode of frame exclusion.
   120  	ExcludeGroup
   121  	// ExcludeTies represents EXCLUDE TIES mode of frame exclusion.
   122  	ExcludeTies
   123  )
   124  
   125  func (node WindowFrameExclusion) String() string {
   126  	switch node {
   127  	case NoExclusion:
   128  		return "EXCLUDE NO ROWS"
   129  	case ExcludeCurrentRow:
   130  		return "EXCLUDE CURRENT ROW"
   131  	case ExcludeGroup:
   132  		return "EXCLUDE GROUP"
   133  	case ExcludeTies:
   134  		return "EXCLUDE TIES"
   135  	default:
   136  		panic(errors.AssertionFailedf("unhandled case: %d", redact.Safe(node)))
   137  	}
   138  }
   139  
   140  // Name returns a string representation of the exclusion type to be used in
   141  // struct names for generated code.
   142  func (node WindowFrameExclusion) Name() string {
   143  	switch node {
   144  	case NoExclusion:
   145  		return "NoExclusion"
   146  	case ExcludeCurrentRow:
   147  		return "ExcludeCurrentRow"
   148  	case ExcludeGroup:
   149  		return "ExcludeGroup"
   150  	case ExcludeTies:
   151  		return "ExcludeTies"
   152  	}
   153  	return ""
   154  }
   155  
   156  // WindowModeName returns the name of the window frame mode.
   157  func WindowModeName(mode WindowFrameMode) string {
   158  	switch mode {
   159  	case RANGE:
   160  		return "RANGE"
   161  	case ROWS:
   162  		return "ROWS"
   163  	case GROUPS:
   164  		return "GROUPS"
   165  	default:
   166  		panic(errors.AssertionFailedf("unhandled case: %d", redact.Safe(mode)))
   167  	}
   168  }