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