github.com/dolthub/go-mysql-server@v0.18.0/memory/point_lookup_table.go (about)

     1  package memory
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/dolthub/go-mysql-server/sql"
     7  	"github.com/dolthub/go-mysql-server/sql/expression"
     8  	"github.com/dolthub/go-mysql-server/sql/types"
     9  )
    10  
    11  var _ sql.TableFunction = PointLookupTable{}
    12  var _ sql.CollationCoercible = PointLookupTable{}
    13  var _ sql.ExecSourceRel = PointLookupTable{}
    14  var _ sql.IndexAddressable = PointLookupTable{}
    15  var _ sql.IndexedTable = PointLookupTable{}
    16  var _ sql.TableNode = PointLookupTable{}
    17  
    18  // PointLookupTable is a table whose indexes only support point lookups but not range scans.
    19  // It's used for testing optimizations on indexes.
    20  type PointLookupTable struct {
    21  	IntSequenceTable
    22  }
    23  
    24  func (s PointLookupTable) UnderlyingTable() sql.Table {
    25  	return s
    26  }
    27  
    28  func (s PointLookupTable) NewInstance(ctx *sql.Context, db sql.Database, args []sql.Expression) (sql.Node, error) {
    29  	node, err := s.IntSequenceTable.NewInstance(ctx, db, args)
    30  	return PointLookupTable{node.(IntSequenceTable)}, err
    31  }
    32  
    33  func (s PointLookupTable) String() string {
    34  	return fmt.Sprintf("pointLookup")
    35  }
    36  
    37  func (s PointLookupTable) DebugString() string {
    38  	return "pointLookup"
    39  }
    40  
    41  func (s PointLookupTable) Name() string {
    42  	return "point_lookup_table"
    43  }
    44  
    45  func (s PointLookupTable) Description() string {
    46  	return "point_lookup_table"
    47  }
    48  
    49  var _ sql.Partition = (*sequencePartition)(nil)
    50  
    51  func (s PointLookupTable) PreciseMatch() bool {
    52  	return true
    53  }
    54  
    55  func (s PointLookupTable) GetIndexes(ctx *sql.Context) (indexes []sql.Index, err error) {
    56  	return []sql.Index{
    57  		pointLookupIndex{&Index{
    58  			DB:         "",
    59  			DriverName: "",
    60  			Tbl:        nil,
    61  			TableName:  s.Name(),
    62  			Exprs: []sql.Expression{
    63  				expression.NewGetFieldWithTable(0, 0, types.Int64, s.db.Name(), s.Name(), s.name, false),
    64  			},
    65  			Name:         s.name,
    66  			Unique:       true,
    67  			Spatial:      false,
    68  			Fulltext:     false,
    69  			CommentStr:   "",
    70  			PrefixLens:   nil,
    71  			fulltextInfo: fulltextInfo{},
    72  		}},
    73  	}, nil
    74  }
    75  
    76  type pointLookupIndex struct {
    77  	sql.Index
    78  }
    79  
    80  func (i pointLookupIndex) CanSupport(ranges ...sql.Range) bool {
    81  	for _, r := range ranges {
    82  		if len(r) != 1 {
    83  			return false
    84  		}
    85  		below, ok := r[0].LowerBound.(sql.Below)
    86  		if !ok {
    87  			return false
    88  		}
    89  		belowKey, _, err := types.Int64.Convert(below.Key)
    90  		if err != nil {
    91  			return false
    92  		}
    93  
    94  		above, ok := r[0].UpperBound.(sql.Above)
    95  		if !ok {
    96  			return false
    97  		}
    98  		aboveKey, _, err := types.Int64.Convert(above.Key)
    99  		if err != nil {
   100  			return false
   101  		}
   102  
   103  		if belowKey != aboveKey {
   104  			return false
   105  		}
   106  	}
   107  	return true
   108  }