/
collection.go
446 lines (346 loc) · 12 KB
/
collection.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright 2021 FerretDB 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 hana
import (
"bytes"
"context"
"fmt"
"sort"
"strings"
"github.com/SAP/go-hdb/driver"
"github.com/google/uuid"
"github.com/FerretDB/FerretDB/internal/backends"
"github.com/FerretDB/FerretDB/internal/handler/sjson"
"github.com/FerretDB/FerretDB/internal/types"
"github.com/FerretDB/FerretDB/internal/util/fsql"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
"github.com/FerretDB/FerretDB/internal/util/must"
)
// collection implements backends.Collection interface by delegating all methods to the wrapped database.
// A collection in HANA is either stored in a table or as a column of a table. The column representation
// not supported yet.
type collection struct {
hdb *fsql.DB
schema string
table string
// column string
}
// newCollection creates a new Collection.
func newCollection(hdb *fsql.DB, schema, table string) backends.Collection {
return backends.CollectionContract(&collection{
hdb: hdb,
schema: schema,
table: table,
})
}
// Helperfunction to check if database and collection exist on hana.
func (c *collection) checkSchemaAndCollectionExists(ctx context.Context) error {
s, err := schemaExists(ctx, c.hdb, c.schema)
if err != nil {
return lazyerrors.Error(err)
}
if !s {
return lazyerrors.Errorf("Database %q does not exist!", c.schema)
}
col, err := collectionExists(ctx, c.hdb, c.schema, c.table)
if err != nil {
return lazyerrors.Error(err)
}
if !col {
return lazyerrors.Errorf("Collection %q does not exist!", c.table)
}
return nil
}
func (c *collection) generateQuery(filter, sort *types.Document) (string, error) {
selectClause := prepareSelectClause(c.schema, c.table)
whereClause, err := prepareWhereClause(c.table, filter)
if err != nil {
return "", lazyerrors.Error(err)
}
sql := selectClause + whereClause
orderByClause, err := prepareOrderByClause(sort)
if err != nil {
return "", lazyerrors.Error(err)
}
sql += orderByClause
return sql, nil
}
// Query implements backends.Collection interface.
func (c *collection) Query(ctx context.Context, params *backends.QueryParams) (*backends.QueryResult, error) {
err := c.checkSchemaAndCollectionExists(ctx)
if err != nil {
return nil, lazyerrors.Error(err)
}
sql, err := c.generateQuery(params.Filter, params.Sort)
if err != nil {
return nil, lazyerrors.Error(err)
}
rows, err := c.hdb.QueryContext(ctx, sql)
if err != nil {
return nil, lazyerrors.Error(err)
}
return &backends.QueryResult{
Iter: newQueryIterator(ctx, rows),
}, nil
}
// InsertAll implements backends.Collection interface.
func (c *collection) InsertAll(ctx context.Context, params *backends.InsertAllParams) (*backends.InsertAllResult, error) {
err := createSchemaIfNotExists(ctx, c.hdb, c.schema)
if err != nil {
return nil, lazyerrors.Error(err)
}
err = createCollectionIfNotExists(ctx, c.hdb, c.schema, c.table)
if err != nil {
return nil, lazyerrors.Error(err)
}
insertSQL := "INSERT INTO %q.%q values('%s')"
for _, doc := range params.Docs {
jsonBytes, err := marshalHana(doc)
if err != nil {
return nil, lazyerrors.Error(err)
}
line := fmt.Sprintf(insertSQL, c.schema, c.table, jsonBytes)
_, err = c.hdb.ExecContext(ctx, line)
if err != nil {
return nil, lazyerrors.Error(err)
}
}
return new(backends.InsertAllResult), nil
}
// UpdateAll implements backends.Collection interface.
func (c *collection) UpdateAll(ctx context.Context, params *backends.UpdateAllParams) (*backends.UpdateAllResult, error) {
var res backends.UpdateAllResult
err := c.checkSchemaAndCollectionExists(ctx)
if err != nil {
return nil, lazyerrors.Error(err)
}
updateSQL := "UPDATE %q.%q SET %q = parse_json('%s') WHERE \"_id\" = %s"
for _, doc := range params.Docs {
jsonBytes, err := marshalHana(doc)
if err != nil {
return nil, lazyerrors.Error(err)
}
id, _ := doc.Get("_id")
must.NotBeZero(id)
id = jsonToHanaQueryString(string(must.NotFail(sjson.MarshalSingleValue(id))))
line := fmt.Sprintf(updateSQL, c.schema, c.table, c.table, jsonBytes, id)
execResult, err := c.hdb.ExecContext(ctx, line)
if err != nil {
return nil, lazyerrors.Error(err)
}
numRows, err := execResult.RowsAffected()
if err != nil {
return nil, lazyerrors.Error(err)
}
res.Updated += int32(numRows)
}
return &res, nil
}
// DeleteAll implements backends.Collection interface.
func (c *collection) DeleteAll(ctx context.Context, params *backends.DeleteAllParams) (*backends.DeleteAllResult, error) {
err := c.checkSchemaAndCollectionExists(ctx)
if err != nil {
return nil, err
}
var res backends.DeleteAllResult
deleteSQL := "DELETE FROM %q.%q WHERE \"_id\" = %s"
for _, id := range params.IDs {
idString := jsonToHanaQueryString(string(must.NotFail(sjson.MarshalSingleValue(id))))
line := fmt.Sprintf(deleteSQL, c.schema, c.table, idString)
execResult, err := c.hdb.ExecContext(ctx, line)
if err != nil {
return nil, lazyerrors.Error(err)
}
numRows, err := execResult.RowsAffected()
if err != nil {
return nil, lazyerrors.Error(err)
}
res.Deleted += int32(numRows)
}
return &res, nil
}
// Explain implements backends.Collection interface.
func (c *collection) Explain(ctx context.Context, params *backends.ExplainParams) (*backends.ExplainResult, error) {
// HANATODO HANA does not provide explain plan in json format. We need a conversion here.
err := c.checkSchemaAndCollectionExists(ctx)
if err != nil {
return nil, lazyerrors.Error(err)
}
// Generate uuid for explain plan statement name.
explainUUID := c.schema + "_" + c.table + "_" + uuid.New().String()
querySQL, err := c.generateQuery(params.Filter, params.Sort)
if err != nil {
return nil, lazyerrors.Error(err)
}
explainSQL := fmt.Sprintf("EXPLAIN PLAN SET STATEMENT_NAME = '%s' FOR %s", explainUUID, querySQL)
_, err = c.hdb.ExecContext(ctx, explainSQL)
if err != nil {
return nil, lazyerrors.Error(err)
}
selectExplainSQL := fmt.Sprintf(
"SELECT OPERATOR_NAME, OPERATOR_DETAILS, OPERATOR_ID, COALESCE(PARENT_OPERATOR_ID,0) "+
"FROM EXPLAIN_PLAN_TABLE WHERE STATEMENT_NAME = '%s'", explainUUID,
)
rows, err := c.hdb.QueryContext(ctx, selectExplainSQL)
if err != nil {
return nil, lazyerrors.Error(err)
}
defer rows.Close()
// Build a document from the explain plan.
var explainDoc types.Document
for rows.Next() {
var operatorName string
var lobDetail driver.Lob
storageDetails := new(bytes.Buffer)
lobDetail.SetWriter(storageDetails)
var operatorID, operatorParentID int32
err = rows.Scan(&operatorName, &lobDetail, &operatorID, &operatorParentID)
if err != nil {
return nil, lazyerrors.Error(err)
}
operatorDetails := storageDetails.String()
explainDoc.Set(operatorName, must.NotFail(types.NewDocument(
"operator_id", operatorID,
"parent_operator_id", operatorParentID,
"operator_details", operatorDetails,
)))
}
cleanupSQL := fmt.Sprintf("DELETE FROM EXPLAIN_PLAN_TABLE WHERE STATEMENT_NAME = '%s'", explainUUID)
_, err = c.hdb.ExecContext(ctx, cleanupSQL)
if err != nil {
return nil, lazyerrors.Error(err)
}
return &backends.ExplainResult{
QueryPlanner: &explainDoc,
}, nil
}
// Stats implements backends.Collection interface.
func (c *collection) Stats(ctx context.Context, params *backends.CollectionStatsParams) (*backends.CollectionStatsResult, error) {
var res backends.CollectionStatsResult
err := c.checkSchemaAndCollectionExists(ctx)
if err != nil {
return nil, err
}
// HANATODO Fill out collection stats
return &res, nil
}
// Compact implements backends.Collection interface.
func (c *collection) Compact(ctx context.Context, params *backends.CompactParams) (*backends.CompactResult, error) {
// HANA does not provide compact functionality.
return new(backends.CompactResult), nil
}
// Prefixes an index with the collection name to store it in hana.
//
// Reasoning:
// Hana DocStore stores indexes on a schema/database level, that may cause
// confilts for indexes of different collections having the same name (eg col1.idx and col2.idx).
func (c *collection) prefixIndexName(indexName string) string {
return fmt.Sprintf("%s__%s", c.table, indexName)
}
// Removes the prefix of an index name.
func (c *collection) removeIndexNamePrefix(prefixedIndex string) string {
prefix := fmt.Sprintf("%s__", c.table)
return strings.Replace(prefixedIndex, prefix, "", 1)
}
// ListIndexes implements backends.Collection interface.
func (c *collection) ListIndexes(ctx context.Context, params *backends.ListIndexesParams) (*backends.ListIndexesResult, error) {
err := c.checkSchemaAndCollectionExists(ctx)
if err != nil {
return nil, err
}
sql := "SELECT idx.INDEX_NAME, idx.COLUMN_NAME, idx.ASCENDING_ORDER " +
"FROM INDEX_COLUMNS idx, M_TABLES tbl " +
"WHERE idx.SCHEMA_NAME = '%s' AND idx.TABLE_NAME = '%s' AND " +
"idx.SCHEMA_NAME = tbl.SCHEMA_NAME AND idx.TABLE_NAME = tbl.TABLE_NAME AND " +
"tbl.TABLE_TYPE = 'COLLECTION'"
sql = fmt.Sprintf(sql, c.schema, c.table)
rows, err := c.hdb.QueryContext(ctx, sql)
if err != nil {
return nil, lazyerrors.Error(err)
}
defer rows.Close()
var indexes []backends.IndexInfo
for rows.Next() {
var idxName, idxColName string
var ascending bool
err = rows.Scan(&idxName, &idxColName, &ascending)
if err != nil {
return nil, lazyerrors.Error(nil)
}
indexes = append(indexes, backends.IndexInfo{
Name: c.removeIndexNamePrefix(idxName),
Unique: true, // HANATODO: Is this possible to query from HANA?
Key: []backends.IndexKeyPair{{Field: idxColName, Descending: !ascending}},
})
}
res := backends.ListIndexesResult{Indexes: indexes}
sort.Slice(res.Indexes, func(i, j int) bool {
return res.Indexes[i].Name < res.Indexes[j].Name
})
return &res, nil
}
func indexExists(indexes []backends.IndexInfo, indexToFind string) bool {
for _, idx := range indexes {
if idx.Name == indexToFind {
return true
}
}
return false
}
// CreateIndexes implements backends.Collection interface.
func (c *collection) CreateIndexes(ctx context.Context, params *backends.CreateIndexesParams) (*backends.CreateIndexesResult, error) { //nolint:lll // for readability
err := c.checkSchemaAndCollectionExists(ctx)
if err != nil {
return nil, err
}
existingIndexes, err := c.ListIndexes(ctx, new(backends.ListIndexesParams))
if err != nil {
return nil, lazyerrors.Error(err)
}
sql := "CREATE HASH INDEX %q.%q ON %q.%q(%q)"
var createStmt string
// HANATODO Can we support more than one field for indexes in HANA DocStore?
for _, index := range params.Indexes {
if !indexExists(existingIndexes.Indexes, index.Name) {
createStmt = fmt.Sprintf(sql, c.schema, c.prefixIndexName(index.Name), c.schema, c.table, index.Key[0].Field)
_, err := c.hdb.ExecContext(ctx, createStmt)
if err != nil {
return nil, lazyerrors.Error(err)
}
}
}
return new(backends.CreateIndexesResult), nil
}
// DropIndexes implements backends.Collection interface.
func (c *collection) DropIndexes(ctx context.Context, params *backends.DropIndexesParams) (*backends.DropIndexesResult, error) {
err := c.checkSchemaAndCollectionExists(ctx)
if err != nil {
return nil, err
}
// HANATODO Check if index is on this collection.
sql := "DROP INDEX %q.%q"
var droptStmt string
for _, index := range params.Indexes {
droptStmt = fmt.Sprintf(sql, c.schema, c.prefixIndexName(index))
_, err := c.hdb.ExecContext(ctx, droptStmt)
if err != nil {
return nil, lazyerrors.Error(err)
}
}
return new(backends.DropIndexesResult), nil
}
// check interfaces
var (
_ backends.Collection = (*collection)(nil)
)