forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
152 lines (133 loc) · 4.98 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2020-2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package sql
import "fmt"
type IndexDef struct {
Name string
Columns []IndexColumn
Constraint IndexConstraint
Storage IndexUsing
Comment string
}
// IndexColumn is the column by which to add to an index.
type IndexColumn struct {
Name string
// Length represents the index prefix length. If zero, then no length was specified.
Length int64
}
// IndexConstraint represents any constraints that should be applied to the index.
type IndexConstraint byte
const (
IndexConstraint_None IndexConstraint = iota
IndexConstraint_Unique
IndexConstraint_Fulltext
IndexConstraint_Spatial
IndexConstraint_Primary
)
// IndexUsing is the desired storage type.
type IndexUsing byte
const (
IndexUsing_Default IndexUsing = iota
IndexUsing_BTree
IndexUsing_Hash
)
// Index is the representation of an index, and also creates an IndexLookup when given a collection of ranges.
type Index interface {
// ID returns the identifier of the index.
ID() string
// Database returns the database name this index belongs to.
Database() string
// Table returns the table name this index belongs to.
Table() string
// Expressions returns the indexed expressions. If the result is more than
// one expression, it means the index has multiple columns indexed. If it's
// just one, it means it may be an expression or a column.
Expressions() []string
// IsUnique returns whether this index is unique
IsUnique() bool
// IsSpatial returns whether this index is a spatial index
IsSpatial() bool
// Comment returns the comment for this index
Comment() string
// IndexType returns the type of this index, e.g. BTREE
IndexType() string
// IsGenerated returns whether this index was generated. Generated indexes
// are used for index access, but are not displayed (such as with SHOW INDEXES).
IsGenerated() bool
// ColumnExpressionTypes returns each expression and its associated Type.
// Each expression string should exactly match the string returned from
// Index.Expressions().
ColumnExpressionTypes() []ColumnExpressionType
// CanSupport returns whether this index supports lookups on the given
// range filters.
CanSupport(...Range) bool
// PrefixLengths returns the prefix lengths for each column in this index
PrefixLengths() []uint16
}
// IndexLookup is the implementation-specific definition of an index lookup. The IndexLookup must contain all necessary
// information to retrieve exactly the rows in the table as specified by the ranges given to their parent index.
// Implementors are responsible for all semantics of correctly returning rows that match an index lookup.
type IndexLookup struct {
Index Index
Ranges RangeCollection
// IsPointLookup is true if the lookup will return one or zero
// values; the range is null safe, the index is unique, every index
// column has a range expression, and every range expression is an
// exact equality.
IsPointLookup bool
IsEmptyRange bool
IsSpatialLookup bool
}
var emptyLookup = IndexLookup{}
func (il IndexLookup) IsEmpty() bool {
return il.Index == nil
}
func (il IndexLookup) String() string {
pr := NewTreePrinter()
_ = pr.WriteNode("IndexLookup")
pr.WriteChildren(fmt.Sprintf("index: %s", il.Index), fmt.Sprintf("ranges: %s", il.Ranges.String()))
return pr.String()
}
func (il IndexLookup) DebugString() string {
pr := NewTreePrinter()
_ = pr.WriteNode("IndexLookup")
pr.WriteChildren(fmt.Sprintf("index: %s", il.Index), fmt.Sprintf("ranges: %s", il.Ranges.DebugString()))
return pr.String()
}
// FilteredIndex is an extension of |Index| that allows an index to declare certain filter predicates handled,
// allowing them to be removed from the overall plan for greater execution efficiency
type FilteredIndex interface {
Index
// HandledFilters returns a subset of |filters| that are satisfied
// by index lookups to this index.
HandledFilters(filters []Expression) (handled []Expression)
}
type IndexOrder byte
const (
IndexOrderNone IndexOrder = iota
IndexOrderAsc
IndexOrderDesc
)
// OrderedIndex is an extension of |Index| that allows indexes to declare their return order. The query engine can
// optimize certain queries if the order of an index is guaranteed, e.g. removing a sort operation.
type OrderedIndex interface {
Index
// Order returns the order of results for reads from this index
Order() IndexOrder
}
// ColumnExpressionType returns a column expression along with its Type.
type ColumnExpressionType struct {
Expression string
Type Type
}