-
-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathpg_index.go
More file actions
340 lines (301 loc) · 11.4 KB
/
pg_index.go
File metadata and controls
340 lines (301 loc) · 11.4 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
// Copyright 2024 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 pgcatalog
import (
"io"
"strings"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/doltgresql/core/id"
"github.com/dolthub/doltgresql/server/functions"
"github.com/dolthub/doltgresql/server/tables"
pgtypes "github.com/dolthub/doltgresql/server/types"
)
// PgIndexName is a constant to the pg_index name.
const PgIndexName = "pg_index"
// InitPgIndex handles registration of the pg_index handler.
func InitPgIndex() {
tables.AddHandler(PgCatalogName, PgIndexName, PgIndexHandler{})
}
// PgIndexHandler is the handler for the pg_index table.
type PgIndexHandler struct{}
var _ tables.Handler = PgIndexHandler{}
var _ tables.IndexedTableHandler = PgIndexHandler{}
// Name implements the interface tables.Handler.
func (p PgIndexHandler) Name() string {
return PgIndexName
}
// RowIter implements the interface tables.Handler.
func (p PgIndexHandler) RowIter(ctx *sql.Context, partition sql.Partition) (sql.RowIter, error) {
// Use cached data from this session if it exists
pgCatalogCache, err := getPgCatalogCache(ctx)
if err != nil {
return nil, err
}
if pgCatalogCache.pgIndexes == nil {
err = cachePgIndexes(ctx, pgCatalogCache)
if err != nil {
return nil, err
}
}
if indexIdxPart, ok := partition.(inMemIndexPartition); ok {
return &inMemIndexScanIter[*pgIndex]{
lookup: indexIdxPart.lookup,
rangeConverter: p,
btreeAccess: pgCatalogCache.pgIndexes,
rowConverter: pgIndexToRow,
}, nil
}
return &pgIndexTableScanIter{
indexCache: pgCatalogCache.pgIndexes,
idx: 0,
}, nil
}
// PkSchema implements the interface tables.Handler.
func (p PgIndexHandler) PkSchema() sql.PrimaryKeySchema {
return sql.PrimaryKeySchema{
Schema: pgIndexSchema,
PkOrdinals: nil,
}
}
// Indexes implements tables.IndexedTableHandler.
func (p PgIndexHandler) Indexes() ([]sql.Index, error) {
return []sql.Index{
pgCatalogInMemIndex{
name: "pg_index_indexrelid_index",
tblName: "pg_index",
dbName: "pg_catalog",
uniq: true,
columnExprs: []sql.ColumnExpressionType{{Expression: "pg_index.indexrelid", Type: pgtypes.Oid}},
},
pgCatalogInMemIndex{
name: "pg_index_indrelid_index",
tblName: "pg_index",
dbName: "pg_catalog",
uniq: false,
columnExprs: []sql.ColumnExpressionType{{Expression: "pg_index.indrelid", Type: pgtypes.Oid}},
},
}, nil
}
// LookupPartitions implements tables.IndexedTableHandler.
func (p PgIndexHandler) LookupPartitions(context *sql.Context, lookup sql.IndexLookup) (sql.PartitionIter, error) {
return &inMemIndexPartIter{
part: inMemIndexPartition{
idxName: lookup.Index.(pgCatalogInMemIndex).name,
lookup: lookup,
},
}, nil
}
// getIndexScanRange implements the interface RangeConverter.
func (p PgIndexHandler) getIndexScanRange(rng sql.Range, index sql.Index) (*pgIndex, bool, *pgIndex, bool) {
var gte, lt *pgIndex
var hasLowerBound, hasUpperBound bool
switch index.(pgCatalogInMemIndex).name {
case "pg_index_indexrelid_index":
msrng := rng.(sql.MySQLRange)
oidRng := msrng[0]
if oidRng.HasLowerBound() {
lb := sql.GetMySQLRangeCutKey(oidRng.LowerBound)
if lb != nil {
lowerRangeCutKey := lb.(id.Id)
gte = &pgIndex{
indexOidNative: idToOid(lowerRangeCutKey),
}
hasLowerBound = true
}
}
if oidRng.HasUpperBound() {
ub := sql.GetMySQLRangeCutKey(oidRng.UpperBound)
if ub != nil {
upperRangeCutKey := ub.(id.Id)
lt = &pgIndex{
indexOidNative: idToOid(upperRangeCutKey) + 1,
}
hasUpperBound = true
}
}
case "pg_index_indrelid_index":
msrng := rng.(sql.MySQLRange)
oidRng := msrng[0]
if oidRng.HasLowerBound() {
lb := sql.GetMySQLRangeCutKey(oidRng.LowerBound)
if lb != nil {
lowerRangeCutKey := lb.(id.Id)
gte = &pgIndex{
tableOidNative: idToOid(lowerRangeCutKey),
}
hasLowerBound = true
}
}
if oidRng.HasUpperBound() {
ub := sql.GetMySQLRangeCutKey(oidRng.UpperBound)
if ub != nil {
upperRangeCutKey := ub.(id.Id)
lt = &pgIndex{
tableOidNative: idToOid(upperRangeCutKey) + 1,
}
hasUpperBound = true
}
}
default:
panic("unknown index name: " + index.(pgCatalogInMemIndex).name)
}
return gte, hasLowerBound, lt, hasUpperBound
}
// pgIndexSchema is the schema for pg_index.
var pgIndexSchema = sql.Schema{
{Name: "indexrelid", Type: pgtypes.Oid, Default: nil, Nullable: false, Source: PgIndexName},
{Name: "indrelid", Type: pgtypes.Oid, Default: nil, Nullable: false, Source: PgIndexName},
{Name: "indnatts", Type: pgtypes.Int16, Default: nil, Nullable: false, Source: PgIndexName},
{Name: "indnkeyatts", Type: pgtypes.Int16, Default: nil, Nullable: false, Source: PgIndexName},
{Name: "indisunique", Type: pgtypes.Bool, Default: nil, Nullable: false, Source: PgIndexName},
{Name: "indnullsnotdistinct", Type: pgtypes.Bool, Default: nil, Nullable: false, Source: PgIndexName},
{Name: "indisprimary", Type: pgtypes.Bool, Default: nil, Nullable: false, Source: PgIndexName},
{Name: "indisexclusion", Type: pgtypes.Bool, Default: nil, Nullable: false, Source: PgIndexName},
{Name: "indimmediate", Type: pgtypes.Bool, Default: nil, Nullable: false, Source: PgIndexName},
{Name: "indisclustered", Type: pgtypes.Bool, Default: nil, Nullable: false, Source: PgIndexName},
{Name: "indisvalid", Type: pgtypes.Bool, Default: nil, Nullable: false, Source: PgIndexName},
{Name: "indcheckxmin", Type: pgtypes.Bool, Default: nil, Nullable: false, Source: PgIndexName},
{Name: "indisready", Type: pgtypes.Bool, Default: nil, Nullable: false, Source: PgIndexName},
{Name: "indislive", Type: pgtypes.Bool, Default: nil, Nullable: false, Source: PgIndexName},
{Name: "indisreplident", Type: pgtypes.Bool, Default: nil, Nullable: false, Source: PgIndexName},
{Name: "indkey", Type: pgtypes.Int16Array, Default: nil, Nullable: false, Source: PgIndexName}, // TODO: type int2vector
{Name: "indcollation", Type: pgtypes.OidArray, Default: nil, Nullable: false, Source: PgIndexName}, // TODO: type oidvector
{Name: "indclass", Type: pgtypes.OidArray, Default: nil, Nullable: false, Source: PgIndexName}, // TODO: type oidvector
{Name: "indoption", Type: pgtypes.Int16Array, Default: nil, Nullable: false, Source: PgIndexName}, // TODO: type int2vector. Declared as the serialized form so it can be read by clients expecting text, but this is a hacky temp solution
{Name: "indexprs", Type: pgtypes.Text, Default: nil, Nullable: true, Source: PgIndexName}, // TODO: type pg_node_tree, collation C
{Name: "indpred", Type: pgtypes.Text, Default: nil, Nullable: true, Source: PgIndexName}, // TODO: type pg_node_tree, collation C
}
func extractColName(expr string) string {
// TODO: this breaks for column names that contain a `.`, but this is a problem that happens
// throughout index analysis in the engine
lastDot := strings.LastIndex(expr, ".")
return expr[lastDot+1:]
}
// pgIndex represents a row in the pg_index table.
// We store oids in their native format as well so that we can do range scans on them.
type pgIndex struct {
index sql.Index
schemaName string
indexOid id.Id
indexOidNative uint32
tableOid id.Id
tableOidNative uint32
indnatts int16
indisunique bool
indisprimary bool
indkey []any
}
// lessIndexOid is a sort function for pgIndex based on indexrelid.
func lessIndexOid(a, b *pgIndex) bool {
return a.indexOidNative < b.indexOidNative
}
// lessIndrelid is a sort function for pgIndex based on indrelid.
func lessIndrelid(a, b []*pgIndex) bool {
return a[0].tableOidNative < b[0].tableOidNative
}
// pgIndexTableScanIter is the sql.RowIter for the pg_index table.
type pgIndexTableScanIter struct {
indexCache *pgIndexCache
idx int
}
var _ sql.RowIter = (*pgIndexTableScanIter)(nil)
// Next implements the interface sql.RowIter.
func (iter *pgIndexTableScanIter) Next(ctx *sql.Context) (sql.Row, error) {
if iter.idx >= len(iter.indexCache.indexes) {
return nil, io.EOF
}
iter.idx++
index := iter.indexCache.indexes[iter.idx-1]
return pgIndexToRow(index), nil
}
// Close implements the interface sql.RowIter.
func (iter *pgIndexTableScanIter) Close(ctx *sql.Context) error {
return nil
}
// pgIndexToRow converts a pgIndex to a sql.Row.
func pgIndexToRow(index *pgIndex) sql.Row {
return sql.Row{
index.indexOid, // indexrelid
index.tableOid, // indrelid
index.indnatts, // indnatts
int16(0), // indnkeyatts
index.index.IsUnique(), // indisunique
false, // indnullsnotdistinct
index.indisprimary, // indisprimary
false, // indisexclusion
false, // indimmediate
false, // indisclustered
true, // indisvalid
false, // indcheckxmin
true, // indisready
true, // indislive
false, // indisreplident
index.indkey, // indkey
[]any{}, // indcollation
[]any{}, // indclass
[]any{int16(0)}, // indoption
nil, // indexprs
nil, // indpred
}
}
// cachePgIndexes caches the pg_index data for the current database in the session.
func cachePgIndexes(ctx *sql.Context, pgCatalogCache *pgCatalogCache) error {
var indexes []*pgIndex
indexOidIdx := NewUniqueInMemIndexStorage[*pgIndex](lessIndexOid)
indrelidIdx := NewNonUniqueInMemIndexStorage[*pgIndex](lessIndrelid)
tableSchemas := make(map[id.Id]sql.Schema)
tableNames := make(map[id.Id]string)
err := functions.IterateCurrentDatabase(ctx, functions.Callbacks{
Index: func(ctx *sql.Context, schema functions.ItemSchema, table functions.ItemTable, index functions.ItemIndex) (cont bool, err error) {
if tableSchemas[table.OID.AsId()] == nil {
tableSchemas[table.OID.AsId()] = table.Item.Schema()
}
if tableNames[table.OID.AsId()] == "" {
tableNames[table.OID.AsId()] = table.Item.Name()
}
s := tableSchemas[table.OID.AsId()]
indKey := make([]any, len(index.Item.Expressions()))
for i, expr := range index.Item.Expressions() {
colName := extractColName(expr)
indKey[i] = int16(s.IndexOfColName(colName)) + 1
}
pgIdx := &pgIndex{
index: index.Item,
schemaName: schema.Item.SchemaName(),
indexOid: index.OID.AsId(),
indexOidNative: id.Cache().ToOID(index.OID.AsId()),
tableOid: table.OID.AsId(),
tableOidNative: id.Cache().ToOID(table.OID.AsId()),
indkey: indKey,
indnatts: int16(len(index.Item.Expressions())),
indisunique: index.Item.IsUnique(),
indisprimary: strings.ToLower(index.Item.ID()) == "primary",
}
indexOidIdx.Add(pgIdx)
indrelidIdx.Add(pgIdx)
indexes = append(indexes, pgIdx)
return true, nil
},
})
if err != nil {
return err
}
pgCatalogCache.pgIndexes = &pgIndexCache{
indexes: indexes,
tableNames: tableNames,
indexOidIdx: indexOidIdx,
indrelidIdx: indrelidIdx,
}
return nil
}