github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/sqle/lookup/cut.go (about)

     1  // Copyright 2020 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 lookup
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"strings"
    21  
    22  	"github.com/dolthub/dolt/go/store/types"
    23  )
    24  
    25  // Cut represents a position on the line of all possible values.
    26  type Cut interface {
    27  	// Compare returns an integer stating the relative position of the calling Cut to the given Cut.
    28  	Compare(Cut) (int, error)
    29  	// Equals returns whether the given Cut equals the calling Cut.
    30  	Equals(Cut) bool
    31  	// Format returns the NomsBinFormat.
    32  	Format() *types.NomsBinFormat
    33  	// Less returns whether the calling Cut represents a position smaller than the given tuple.
    34  	Less(types.Tuple) (bool, error)
    35  	// String returns the Cut as a string for debugging purposes. Will panic on errors.
    36  	String() string
    37  	// TypeAsLowerBound returns the bound type if the calling Cut is the lower bound of a range.
    38  	TypeAsLowerBound() BoundType
    39  	// TypeAsLowerBound returns the bound type if the calling Cut is the upper bound of a range.
    40  	TypeAsUpperBound() BoundType
    41  }
    42  
    43  // GetKey returns the inner tuple from the given Cut.
    44  func GetKey(c Cut) types.Tuple {
    45  	switch c := c.(type) {
    46  	case Below:
    47  		return c.key
    48  	case Above:
    49  		return c.key
    50  	default:
    51  		panic(fmt.Errorf("GetKey on '%T' should be impossible", c))
    52  	}
    53  }
    54  
    55  func cutKeyToString(tpl types.Tuple) string {
    56  	var vals []string
    57  	iter, err := tpl.Iterator()
    58  	if err != nil {
    59  		panic(err)
    60  	}
    61  	for iter.HasMore() {
    62  		_, val, err := iter.Next()
    63  		if err != nil {
    64  			panic(err)
    65  		}
    66  		str, err := types.EncodedValue(context.Background(), val)
    67  		if err != nil {
    68  			panic(err)
    69  		}
    70  		vals = append(vals, str)
    71  	}
    72  	return fmt.Sprintf("%s", strings.Join(vals, ","))
    73  }