github.com/dolthub/go-mysql-server@v0.18.0/sql/expression/filter-range.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  // NewRangeFilterExpr builds an expression that filters with the list of sql ranges and exprs.
    25  //
    26  // Exprs is the list of expressions to filter by.
    27  // Ranges is the list of ranges to check against each expr.
    28  //
    29  // The length of each range must match the length of the exprs slice.
    30  func NewRangeFilterExpr(exprs []sql.Expression, ranges []sql.Range) (sql.Expression, error) {
    31  	if len(ranges) == 0 {
    32  		return nil, nil
    33  	}
    34  
    35  	var rangeCollectionExpr sql.Expression
    36  	for rangIdx, rang := range ranges {
    37  		if len(exprs) < len(rang) {
    38  			return nil, fmt.Errorf("expected different key count: exprs(%d) < (ranges[%d])(%d)", len(exprs), rangIdx, len(rang))
    39  		}
    40  		var rangeExpr sql.Expression
    41  		for i, rce := range rang {
    42  			var rangeColumnExpr sql.Expression
    43  			switch rce.Type() {
    44  			// Both Empty and All may seem like strange inclusions, but if only one range is given we need some
    45  			// expression to evaluate, otherwise our expression would be a nil expression which would panic.
    46  			case sql.RangeType_Empty:
    47  				rangeColumnExpr = NewEquals(NewLiteral(1, types.Int8), NewLiteral(2, types.Int8))
    48  			case sql.RangeType_All:
    49  				rangeColumnExpr = NewEquals(NewLiteral(1, types.Int8), NewLiteral(1, types.Int8))
    50  			case sql.RangeType_EqualNull:
    51  				rangeColumnExpr = NewIsNull(exprs[i])
    52  			case sql.RangeType_GreaterThan:
    53  				if sql.RangeCutIsBinding(rce.LowerBound) {
    54  					rangeColumnExpr = NewGreaterThan(exprs[i], NewLiteral(sql.GetRangeCutKey(rce.LowerBound), rce.Typ.Promote()))
    55  				} else {
    56  					rangeColumnExpr = NewNot(NewIsNull(exprs[i]))
    57  				}
    58  			case sql.RangeType_GreaterOrEqual:
    59  				rangeColumnExpr = NewGreaterThanOrEqual(exprs[i], NewLiteral(sql.GetRangeCutKey(rce.LowerBound), rce.Typ.Promote()))
    60  			case sql.RangeType_LessThanOrNull:
    61  				rangeColumnExpr = JoinOr(
    62  					NewLessThan(exprs[i], NewLiteral(sql.GetRangeCutKey(rce.UpperBound), rce.Typ.Promote())),
    63  					NewIsNull(exprs[i]),
    64  				)
    65  			case sql.RangeType_LessOrEqualOrNull:
    66  				rangeColumnExpr = JoinOr(
    67  					NewLessThanOrEqual(exprs[i], NewLiteral(sql.GetRangeCutKey(rce.UpperBound), rce.Typ.Promote())),
    68  					NewIsNull(exprs[i]),
    69  				)
    70  			case sql.RangeType_ClosedClosed:
    71  				rangeColumnExpr = JoinAnd(
    72  					NewGreaterThanOrEqual(exprs[i], NewLiteral(sql.GetRangeCutKey(rce.LowerBound), rce.Typ.Promote())),
    73  					NewLessThanOrEqual(exprs[i], NewLiteral(sql.GetRangeCutKey(rce.UpperBound), rce.Typ.Promote())),
    74  				)
    75  			case sql.RangeType_OpenOpen:
    76  				if sql.RangeCutIsBinding(rce.LowerBound) {
    77  					rangeColumnExpr = JoinAnd(
    78  						NewGreaterThan(exprs[i], NewLiteral(sql.GetRangeCutKey(rce.LowerBound), rce.Typ.Promote())),
    79  						NewLessThan(exprs[i], NewLiteral(sql.GetRangeCutKey(rce.UpperBound), rce.Typ.Promote())),
    80  					)
    81  				} else {
    82  					// Lower bound is (NULL, ...)
    83  					rangeColumnExpr = NewLessThan(exprs[i], NewLiteral(sql.GetRangeCutKey(rce.UpperBound), rce.Typ.Promote()))
    84  				}
    85  			case sql.RangeType_OpenClosed:
    86  				if sql.RangeCutIsBinding(rce.LowerBound) {
    87  					rangeColumnExpr = JoinAnd(
    88  						NewGreaterThan(exprs[i], NewLiteral(sql.GetRangeCutKey(rce.LowerBound), rce.Typ.Promote())),
    89  						NewLessThanOrEqual(exprs[i], NewLiteral(sql.GetRangeCutKey(rce.UpperBound), rce.Typ.Promote())),
    90  					)
    91  				} else {
    92  					// Lower bound is (NULL, ...]
    93  					rangeColumnExpr = NewLessThanOrEqual(exprs[i], NewLiteral(sql.GetRangeCutKey(rce.UpperBound), rce.Typ.Promote()))
    94  				}
    95  			case sql.RangeType_ClosedOpen:
    96  				rangeColumnExpr = JoinAnd(
    97  					NewGreaterThanOrEqual(exprs[i], NewLiteral(sql.GetRangeCutKey(rce.LowerBound), rce.Typ.Promote())),
    98  					NewLessThan(exprs[i], NewLiteral(sql.GetRangeCutKey(rce.UpperBound), rce.Typ.Promote())),
    99  				)
   100  			}
   101  			rangeExpr = JoinAnd(rangeExpr, rangeColumnExpr)
   102  		}
   103  		rangeCollectionExpr = JoinOr(rangeCollectionExpr, rangeExpr)
   104  	}
   105  	return rangeCollectionExpr, nil
   106  }