github.com/dolthub/go-mysql-server@v0.18.0/sql/stats/func_deps.go (about)

     1  // Copyright 2023 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 stats
    16  
    17  import (
    18  	"fmt"
    19  	"strings"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql"
    22  )
    23  
    24  func IndexFds(tableName string, sch sql.Schema, idx sql.Index) (*sql.FuncDepSet, sql.ColSet, error) {
    25  	var idxCols sql.ColSet
    26  	pref := fmt.Sprintf("%s.", tableName)
    27  	for _, col := range idx.ColumnExpressionTypes() {
    28  		colName := strings.TrimPrefix(strings.ToLower(col.Expression), pref)
    29  		i := sch.IndexOfColName(colName)
    30  		if i < 0 {
    31  			return nil, idxCols, fmt.Errorf("column not found on table during stats building: %s", colName)
    32  		}
    33  		idxCols.Add(sql.ColumnId(i + 1))
    34  	}
    35  
    36  	var all sql.ColSet
    37  	var notNull sql.ColSet
    38  	for i, col := range sch {
    39  		all.Add(sql.ColumnId(i + 1))
    40  		if !col.Nullable {
    41  			notNull.Add(sql.ColumnId(i + 1))
    42  		}
    43  	}
    44  
    45  	strict := true
    46  	for i, hasNext := idxCols.Next(1); hasNext; i, hasNext = idxCols.Next(i + 1) {
    47  		if !notNull.Contains(i) {
    48  			strict = false
    49  		}
    50  	}
    51  
    52  	var strictKeys []sql.ColSet
    53  	var laxKeys []sql.ColSet
    54  	if !idx.IsUnique() {
    55  		// not an FD
    56  	} else if strict {
    57  		strictKeys = append(strictKeys, idxCols)
    58  	} else {
    59  		laxKeys = append(laxKeys, idxCols)
    60  	}
    61  	return sql.NewTablescanFDs(all, strictKeys, laxKeys, notNull), idxCols, nil
    62  }