github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/dtables/expression.go (about)

     1  // Copyright 2022 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 dtables
    16  
    17  import (
    18  	"strings"
    19  
    20  	"github.com/dolthub/go-mysql-server/sql"
    21  	"github.com/dolthub/go-mysql-server/sql/expression"
    22  
    23  	"github.com/dolthub/dolt/go/libraries/utils/set"
    24  )
    25  
    26  type Predicate func(sql.Expression) bool
    27  
    28  // ColumnPredicate returns a predicate function for expressions on the column names given
    29  func ColumnPredicate(colNameSet *set.StrSet) Predicate {
    30  	return func(filter sql.Expression) bool {
    31  		isCommitFilter := true
    32  		sql.Inspect(filter, func(e sql.Expression) (cont bool) {
    33  			if e == nil {
    34  				return true
    35  			}
    36  
    37  			switch val := e.(type) {
    38  			case *expression.GetField:
    39  				if !colNameSet.Contains(strings.ToLower(val.Name())) {
    40  					isCommitFilter = false
    41  					return false
    42  				}
    43  			}
    44  
    45  			return true
    46  		})
    47  
    48  		return isCommitFilter
    49  	}
    50  }
    51  
    52  // FilterFilters returns the subset of the expressions given that match the given predicate
    53  func FilterFilters(filters []sql.Expression, predicate func(filter sql.Expression) bool) []sql.Expression {
    54  	matching := make([]sql.Expression, 0, len(filters))
    55  	for _, f := range filters {
    56  		if predicate(f) {
    57  			matching = append(matching, f)
    58  		}
    59  	}
    60  	return matching
    61  }