github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/sqle/lookup/below.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 "fmt" 19 20 "github.com/dolthub/dolt/go/store/types" 21 ) 22 23 // Above represents the position immediately below the contained key. 24 type Below struct { 25 key types.Tuple 26 } 27 28 var _ Cut = Below{} 29 30 // Compare implements Cut. 31 func (b Below) Compare(c Cut) (int, error) { 32 switch c := c.(type) { 33 case AboveAll: 34 return -1, nil 35 case BelowAll: 36 return 1, nil 37 case Below: 38 ok, err := b.key.Less(b.key.Format(), c.key) 39 if err != nil { 40 return 0, err 41 } 42 if ok { 43 return -1, nil 44 } 45 if b.key.Equals(c.key) { 46 return 0, nil 47 } 48 return 1, nil 49 case Above: 50 ok, err := c.key.Less(c.key.Format(), b.key) 51 if err != nil { 52 return 0, err 53 } 54 if ok { 55 return 1, nil 56 } 57 return -1, nil 58 default: 59 panic(fmt.Errorf("unrecognized Cut type '%T'", c)) 60 } 61 } 62 63 // Equals implements Cut. 64 func (b Below) Equals(c Cut) bool { 65 other, ok := c.(Below) 66 if !ok { 67 return false 68 } 69 return b.key.Equals(other.key) 70 } 71 72 // Format implements Cut. 73 func (b Below) Format() *types.NomsBinFormat { 74 if b.key.Empty() { 75 return types.Format_Default 76 } 77 return b.key.Format() 78 } 79 80 // Less implements Cut. 81 func (b Below) Less(tpl types.Tuple) (bool, error) { 82 ok, err := b.key.Less(b.key.Format(), tpl) 83 if err != nil { 84 return false, err 85 } 86 if ok { 87 return true, nil 88 } 89 return b.key.Equals(tpl), nil 90 } 91 92 // String implements Cut. 93 func (b Below) String() string { 94 return fmt.Sprintf("Below[%s]", cutKeyToString(b.key)) 95 } 96 97 // TypeAsLowerBound implements Cut. 98 func (Below) TypeAsLowerBound() BoundType { 99 return Closed 100 } 101 102 // TypeAsUpperBound implements Cut. 103 func (Below) TypeAsUpperBound() BoundType { 104 return Open 105 }