github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/between.go (about) 1 // Copyright 2020-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 expression 16 17 import ( 18 "fmt" 19 20 "github.com/dolthub/go-mysql-server/sql" 21 "github.com/dolthub/go-mysql-server/sql/types" 22 ) 23 24 // Between checks a value is between two given values. 25 type Between struct { 26 Val sql.Expression 27 Lower sql.Expression 28 Upper sql.Expression 29 } 30 31 var _ sql.Expression = (*Between)(nil) 32 var _ sql.CollationCoercible = (*Between)(nil) 33 34 // NewBetween creates a new Between expression. 35 func NewBetween(val, lower, upper sql.Expression) *Between { 36 return &Between{val, lower, upper} 37 } 38 39 func (b *Between) String() string { 40 return fmt.Sprintf("(%s BETWEEN %s AND %s)", b.Val, b.Lower, b.Upper) 41 } 42 43 func (b *Between) DebugString() string { 44 return fmt.Sprintf("(%s BETWEEN %s AND %s)", sql.DebugString(b.Val), sql.DebugString(b.Lower), sql.DebugString(b.Upper)) 45 } 46 47 // Children implements the Expression interface. 48 func (b *Between) Children() []sql.Expression { 49 return []sql.Expression{b.Val, b.Lower, b.Upper} 50 } 51 52 // Type implements the Expression interface. 53 func (*Between) Type() sql.Type { return types.Boolean } 54 55 // CollationCoercibility implements the interface sql.CollationCoercible. 56 func (b *Between) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) { 57 return sql.GetCoercibility(ctx, b.Val) 58 } 59 60 // IsNullable implements the Expression interface. 61 func (b *Between) IsNullable() bool { 62 return b.Val.IsNullable() || b.Lower.IsNullable() || b.Upper.IsNullable() 63 } 64 65 // Resolved implements the Expression interface. 66 func (b *Between) Resolved() bool { 67 return b.Val.Resolved() && b.Lower.Resolved() && b.Upper.Resolved() 68 } 69 70 // Eval implements the Expression interface. 71 func (b *Between) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) { 72 // TODO: Delete Between expression entirely? 73 // Reuse type conversion logic in comparison expressions by creating a logically equivalent expression 74 return NewAnd(NewLessThanOrEqual(b.Lower, b.Val), NewGreaterThanOrEqual(b.Upper, b.Val)).Eval(ctx, row) 75 } 76 77 // WithChildren implements the Expression interface. 78 func (b *Between) WithChildren(children ...sql.Expression) (sql.Expression, error) { 79 if len(children) != 3 { 80 return nil, sql.ErrInvalidChildrenNumber.New(b, len(children), 3) 81 } 82 return NewBetween(children[0], children[1], children[2]), nil 83 }