forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsequence_table.go
246 lines (208 loc) · 6.42 KB
/
sequence_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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package memory
import (
"encoding/binary"
"fmt"
"io"
"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 = IntSequenceTable{}
var _ sql.CollationCoercible = IntSequenceTable{}
var _ sql.ExecSourceRel = IntSequenceTable{}
var _ sql.IndexAddressable = IntSequenceTable{}
var _ sql.IndexedTable = IntSequenceTable{}
var _ sql.TableNode = IntSequenceTable{}
// IntSequenceTable a simple table function that returns a sequence
// of integers.
type IntSequenceTable struct {
db sql.Database
name string
Len int64
}
func (s IntSequenceTable) UnderlyingTable() sql.Table {
return s
}
func (s IntSequenceTable) NewInstance(_ *sql.Context, db sql.Database, args []sql.Expression) (sql.Node, error) {
if len(args) != 2 {
return nil, fmt.Errorf("sequence table expects 2 arguments: (name, len)")
}
nameExp, ok := args[0].(*expression.Literal)
if !ok {
return nil, fmt.Errorf("sequence table expects arguments to be literal expressions")
}
name, ok := nameExp.Value().(string)
if !ok {
return nil, fmt.Errorf("sequence table expects 1st argument to be column name")
}
lenExp, ok := args[1].(*expression.Literal)
if !ok {
return nil, fmt.Errorf("sequence table expects arguments to be literal expressions")
}
length, _, err := types.Int64.Convert(lenExp.Value())
if !ok {
return nil, fmt.Errorf("%w; sequence table expects 2nd argument to be a sequence length integer", err)
}
return IntSequenceTable{db: db, name: name, Len: length.(int64)}, nil
}
func (s IntSequenceTable) Resolved() bool {
return true
}
func (s IntSequenceTable) IsReadOnly() bool {
return true
}
func (s IntSequenceTable) String() string {
return fmt.Sprintf("sequence(%s, %d)", s.name, s.Len)
}
func (s IntSequenceTable) DebugString() string {
pr := sql.NewTreePrinter()
_ = pr.WriteNode("sequence")
children := []string{
fmt.Sprintf("name: %s", s.name),
fmt.Sprintf("len: %d", s.Len),
}
_ = pr.WriteChildren(children...)
return pr.String()
}
func (s IntSequenceTable) Schema() sql.Schema {
schema := []*sql.Column{
{
DatabaseSource: s.db.Name(),
Source: s.Name(),
Name: s.name,
Type: types.Int64,
},
}
return schema
}
func (s IntSequenceTable) Children() []sql.Node {
return []sql.Node{}
}
func (s IntSequenceTable) RowIter(_ *sql.Context, _ sql.Row) (sql.RowIter, error) {
rowIter := &SequenceTableFnRowIter{i: 0, n: s.Len}
return rowIter, nil
}
func (s IntSequenceTable) WithChildren(_ ...sql.Node) (sql.Node, error) {
return s, nil
}
func (s IntSequenceTable) CheckPrivileges(_ *sql.Context, _ sql.PrivilegedOperationChecker) bool {
return true
}
// CollationCoercibility implements the interface sql.CollationCoercible.
func (IntSequenceTable) CollationCoercibility(ctx *sql.Context) (collation sql.CollationID, coercibility byte) {
return sql.Collation_binary, 5
}
// Collation implements the sql.Table interface.
func (IntSequenceTable) Collation() sql.CollationID {
return sql.Collation_Default
}
func (s IntSequenceTable) Expressions() []sql.Expression {
return []sql.Expression{}
}
func (s IntSequenceTable) WithExpressions(e ...sql.Expression) (sql.Node, error) {
return s, nil
}
func (s IntSequenceTable) Database() sql.Database {
return s.db
}
func (s IntSequenceTable) WithDatabase(_ sql.Database) (sql.Node, error) {
return s, nil
}
func (s IntSequenceTable) Name() string {
return "sequence_table"
}
func (s IntSequenceTable) Description() string {
return "sequence"
}
var _ sql.RowIter = (*SequenceTableFnRowIter)(nil)
type SequenceTableFnRowIter struct {
n int64
i int64
}
func (i *SequenceTableFnRowIter) Next(_ *sql.Context) (sql.Row, error) {
if i.i >= i.n {
return nil, io.EOF
}
ret := sql.Row{i.i}
i.i++
return ret, nil
}
func (i *SequenceTableFnRowIter) Close(_ *sql.Context) error {
return nil
}
var _ sql.Partition = (*sequencePartition)(nil)
type sequencePartition struct {
min, max int64
}
func (s sequencePartition) Key() []byte {
return binary.LittleEndian.AppendUint64(binary.LittleEndian.AppendUint64(nil, uint64(s.min)), uint64(s.max))
}
// Partitions is a sql.Table interface function that returns a partition of the data. This data has a single partition.
func (s IntSequenceTable) Partitions(ctx *sql.Context) (sql.PartitionIter, error) {
return sql.PartitionsToPartitionIter(&sequencePartition{min: 0, max: int64(s.Len) - 1}), nil
}
// PartitionRows is a sql.Table interface function that takes a partition and returns all rows in that partition.
// This table has a partition for just schema changes, one for just data changes, and one for both.
func (s IntSequenceTable) PartitionRows(ctx *sql.Context, partition sql.Partition) (sql.RowIter, error) {
sp, ok := partition.(*sequencePartition)
if !ok {
return &SequenceTableFnRowIter{i: 0, n: s.Len}, nil
}
min := int64(0)
if sp.min > min {
min = sp.min
}
max := int64(s.Len) - 1
if sp.max < max {
max = sp.max
}
return &SequenceTableFnRowIter{i: min, n: max + 1}, nil
}
// LookupPartitions is a sql.IndexedTable interface function that takes an index lookup and returns the set of corresponding partitions.
func (s IntSequenceTable) LookupPartitions(context *sql.Context, lookup sql.IndexLookup) (sql.PartitionIter, error) {
lowerBound := lookup.Ranges[0][0].LowerBound
below, ok := lowerBound.(sql.Below)
if !ok {
return s.Partitions(context)
}
upperBound := lookup.Ranges[0][0].UpperBound
above, ok := upperBound.(sql.Above)
if !ok {
return s.Partitions(context)
}
min, _, err := s.Schema()[0].Type.Convert(below.Key)
if err != nil {
return nil, err
}
max, _, err := s.Schema()[0].Type.Convert(above.Key)
if err != nil {
return nil, err
}
return sql.PartitionsToPartitionIter(&sequencePartition{min: min.(int64), max: max.(int64)}), nil
}
func (s IntSequenceTable) IndexedAccess(lookup sql.IndexLookup) sql.IndexedTable {
return s
}
func (s IntSequenceTable) PreciseMatch() bool {
return true
}
func (s IntSequenceTable) GetIndexes(ctx *sql.Context) ([]sql.Index, error) {
return []sql.Index{
&Index{
DB: s.db.Name(),
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
}