github.com/dolthub/go-mysql-server@v0.18.0/sql/sort_field.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  
    20  	"gopkg.in/src-d/go-errors.v1"
    21  )
    22  
    23  // SortField is a field by which a query will be sorted.
    24  type SortField struct {
    25  	// Column to order by.
    26  	Column Expression
    27  	// Column Expression2 to order by. This is always the same value as Column, but avoids a type cast
    28  	Column2 Expression2
    29  	// Order type.
    30  	Order SortOrder
    31  	// NullOrdering defining how nulls will be ordered.
    32  	NullOrdering NullOrdering
    33  }
    34  
    35  type SortFields []SortField
    36  
    37  func (sf SortFields) ToExpressions() []Expression {
    38  	es := make([]Expression, len(sf))
    39  	for i, f := range sf {
    40  		es[i] = f.Column
    41  	}
    42  	return es
    43  }
    44  
    45  func (sf SortFields) FromExpressions(exprs ...Expression) SortFields {
    46  	var fields = make(SortFields, len(sf))
    47  
    48  	if len(exprs) != len(fields) {
    49  		panic(fmt.Sprintf("Invalid expression slice. Wanted %d elements, got %d", len(fields), len(exprs)))
    50  	}
    51  
    52  	for i, expr := range exprs {
    53  		expr2, _ := expr.(Expression2)
    54  		fields[i] = SortField{
    55  			Column:       expr,
    56  			Column2:      expr2,
    57  			NullOrdering: sf[i].NullOrdering,
    58  			Order:        sf[i].Order,
    59  		}
    60  	}
    61  	return fields
    62  }
    63  
    64  func (s SortField) String() string {
    65  	return fmt.Sprintf("%s %s", s.Column, s.Order)
    66  }
    67  
    68  func (s SortField) DebugString() string {
    69  	nullOrdering := "nullsFirst"
    70  	if s.NullOrdering == NullsLast {
    71  		nullOrdering = "nullsLast"
    72  	}
    73  	return fmt.Sprintf("%s %s %s", DebugString(s.Column), DebugString(s.Order), nullOrdering)
    74  }
    75  
    76  // ErrUnableSort is thrown when something happens on sorting
    77  var ErrUnableSort = errors.NewKind("unable to sort")
    78  
    79  // SortOrder represents the order of the sort (ascending or descending).
    80  type SortOrder byte
    81  
    82  const (
    83  	// Ascending order.
    84  	Ascending SortOrder = 1
    85  	// Descending order.
    86  	Descending SortOrder = 2
    87  )
    88  
    89  func (s SortOrder) String() string {
    90  	switch s {
    91  	case Ascending:
    92  		return "ASC"
    93  	case Descending:
    94  		return "DESC"
    95  	default:
    96  		return "invalid SortOrder"
    97  	}
    98  }
    99  
   100  // NullOrdering represents how to order based on null values.
   101  type NullOrdering byte
   102  
   103  const (
   104  	// NullsFirst puts the null values before any other values.
   105  	NullsFirst NullOrdering = iota
   106  	// NullsLast puts the null values after all other values.
   107  	NullsLast NullOrdering = 2
   108  )