forked from microsoft/MSVBASE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhnswindex.cpp
461 lines (418 loc) · 14.6 KB
/
hnswindex.cpp
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include <iostream>
#include <string>
#include "hnswindex.hpp"
#include "hnswindex_builder.hpp"
#include "util.hpp"
extern "C"
{
#include <access/relscan.h>
#include <commands/vacuum.h>
#include <miscadmin.h>
#include <utils/elog.h>
#include <utils/rel.h>
#include <utils/selfuncs.h>
#include <access/reloptions.h>
}
extern relopt_kind hnsw_para_relopt_kind;
bytea *hnsw_para_options(Datum reloptions, bool validate) {
static const relopt_parse_elt tab[] = {
{"dimension", RELOPT_TYPE_INT, offsetof(hnsw_ParaOptions, dimension)},
{"distmethod", RELOPT_TYPE_ENUM, offsetof(hnsw_ParaOptions, distmethod)}
};
return (bytea *)build_reloptions(reloptions, validate, hnsw_para_relopt_kind, sizeof(hnsw_ParaOptions), tab, lengthof(tab));
}
int hnsw_ParaGetDimension(Relation index) {
hnsw_ParaOptions *opts = (hnsw_ParaOptions *)index->rd_options;
if (opts)
return opts->dimension;
return 1;
}
hnsw_DistCalcMethod hnsw_ParaGetDistmethod(Relation index) {
hnsw_ParaOptions *opts = (hnsw_ParaOptions *)index->rd_options;
if (opts)
return opts->distmethod;
return hnsw_Inner_Product;
}
CPP_PG_FUNCTION_INFO_V1(hnsw_handler);
Datum hnsw_handler(PG_FUNCTION_ARGS)
{
IndexAmRoutine *amroutine = makeNode(IndexAmRoutine);
amroutine->amstrategies = 0;
amroutine->amsupport = 4;
#if PG_VERSION_NUM >= 130000
amroutine->amoptsprocnum = 0;
#endif
amroutine->amcanorder = false;
amroutine->amcanorderbyop = true;
amroutine->amcanrelaxedorderbyop=true;
amroutine->amcanbackward = false; // can change direction mid-scan
amroutine->amcanunique = false;
amroutine->amcanmulticol = false;
amroutine->amoptionalkey = true;
amroutine->amsearcharray = false;
amroutine->amsearchnulls = false;
amroutine->amstorage = true;
amroutine->amclusterable = false;
amroutine->ampredlocks = false;
#if PG_VERSION_NUM >= 100000
amroutine->amcanparallel = false;
#endif
#if PG_VERSION_NUM >= 110000
amroutine->amcaninclude = false;
#endif
#if PG_VERSION_NUM >= 130000
amroutine->amusemaintenanceworkmem = false; // not used during VACUUM
amroutine->amparallelvacuumoptions =
VACUUM_OPTION_NO_PARALLEL; // FIXME: support parallel vacumm
#endif
amroutine->amkeytype = 0;
// For a brief introduction of what these functions are used for, see
// https://www.postgresql.org/docs/13/index-functions.html.
amroutine->ambuild = hnsw_build;
amroutine->ambuildempty = nullptr; // TODO: index access method
amroutine->aminsert = hnsw_insert; // TODO: index access method
amroutine->ambulkdelete = hnsw_bulkdelete; // TODO: index access method
amroutine->amvacuumcleanup =
hnsw_vacuumcleanup;
amroutine->amcanreturn = nullptr;
amroutine->amcostestimate = hnsw_costestimate;
amroutine->amoptions = hnsw_para_options; // TODO: index access method
amroutine->amproperty = nullptr; /* TODO AMPROP_DISTANCE_ORDERABLE */
#if PG_VERSION_NUM >= 120000
amroutine->ambuildphasename = nullptr;
#endif
amroutine->amvalidate = hnsw_validate;
amroutine->ambeginscan = hnsw_begin_scan;
amroutine->amrescan = hnsw_rescan;
amroutine->amgettuple = hnsw_gettuple;
amroutine->amgetbitmap = nullptr;
amroutine->amendscan = hnsw_endscan;
amroutine->ammarkpos = nullptr;
amroutine->amrestrpos = nullptr;
#if PG_VERSION_NUM >= 100000
amroutine->amestimateparallelscan = nullptr;
amroutine->aminitparallelscan = nullptr;
amroutine->amparallelrescan = nullptr;
#endif
PG_RETURN_POINTER(amroutine);
}
IndexBuildResult *hnsw_build(Relation heap,
Relation index,
IndexInfo *indexInfo)
{
IndexBuildResult *result;
try
{
auto builder = HNSWIndexBuilder(heap, index, indexInfo);
builder.ConstructInternalBuilder(DistanceMethod::L2);
builder.LoadTuples();
std::string path = std::string(DataDir) + std::string("/") +
std::string(DatabasePath) + std::string("/") +
std::string(RelationGetRelationName(index));
builder.SaveIndex(path);
result = (IndexBuildResult *) palloc(sizeof(IndexBuildResult));
result->heap_tuples = builder.m_reltuples;
result->index_tuples = builder.m_indtuples;
}
catch (std::exception &ex)
{
ereport(FATAL, errcode(0), errmsg("C++ exception: %s", ex.what()));
}
catch (...)
{
ereport(FATAL, errcode(0), errmsg("C++ exception"));
}
return result;
}
bool hnsw_insert(Relation index,
Datum* values,
bool* isnull,
ItemPointer heap_tid,
Relation heapRelation,
IndexUniqueCheck checkUnique,
IndexInfo* indexInfo)
{
std::string path = std::string(DataDir) + std::string("/") +
std::string(DatabasePath) + std::string("/") +
std::string(RelationGetRelationName(index));
int dim = hnsw_ParaGetDimension(index);
switch(hnsw_ParaGetDistmethod(index))
{
case hnsw_Inner_Product:
HNSWIndexScan::LoadIndex(path, DistanceMethod::InnerProduct, dim);
break;
case hnsw_L2_Distance:
HNSWIndexScan::LoadIndex(path, DistanceMethod::L2, dim);
break;
default:
elog(ERROR, "hnsw index parameter value error.");
}
return HNSWIndexScan::Insert(path,
values,
isnull,
heap_tid,
checkUnique,
dim);
}
IndexBulkDeleteResult* hnsw_bulkdelete(IndexVacuumInfo* info, IndexBulkDeleteResult* stats,
IndexBulkDeleteCallback callback, void* callback_state)
{
Relation index = info->index;
std::string path = std::string(DataDir) + std::string("/") +
std::string(DatabasePath) + std::string("/") +
std::string(RelationGetRelationName(index));
int dim = hnsw_ParaGetDimension(index);
switch(hnsw_ParaGetDistmethod(index))
{
case hnsw_Inner_Product:
HNSWIndexScan::LoadIndex(path, DistanceMethod::InnerProduct, dim);
break;
case hnsw_L2_Distance:
HNSWIndexScan::LoadIndex(path, DistanceMethod::L2, dim);
break;
default:
elog(ERROR, "hnsw index parameter value error.");
}
return HNSWIndexScan::BulkDelete(path,
info,
stats,
callback,
callback_state);
}
IndexScanDesc hnsw_begin_scan(Relation index, int nkeys, int norderbys)
{
std::string path = std::string(DataDir) + std::string("/") +
std::string(DatabasePath) + std::string("/") +
std::string(RelationGetRelationName(index));
//TODO(qianxi): how to get distance method and dimension
switch(hnsw_ParaGetDistmethod(index))
{
case hnsw_Inner_Product:
HNSWIndexScan::LoadIndex(path, DistanceMethod::InnerProduct, hnsw_ParaGetDimension(index));
break;
case hnsw_L2_Distance:
HNSWIndexScan::LoadIndex(path, DistanceMethod::L2, hnsw_ParaGetDimension(index));
break;
default:
elog(ERROR, "hnsw index parameter value error.");
}
IndexScanDesc scan = RelationGetIndexScan(index, nkeys, norderbys);
scan->xs_orderbyvals = (Datum *) palloc0(sizeof(Datum));
scan->xs_orderbynulls = (bool *) palloc(sizeof(bool));
HNSWScanOpaque scanState =
(HNSWScanOpaque) palloc(sizeof(HNSWScanOpaqueData));
scanState->first = true;
scanState->workSpace = nullptr;
scan->opaque = scanState;
return scan;
}
void hnsw_rescan(IndexScanDesc scan,
ScanKey keys,
int nkeys,
ScanKey orderbys,
int norderbys)
{
HNSWScanOpaque scanState = (HNSWScanOpaque) scan->opaque;
if (scanState->workSpace != nullptr)
{
HNSWIndexScan::EndScan(scanState->workSpace->resultIterator);
scanState->workSpace->resultIterator = nullptr;
delete scanState->workSpace;
scanState->workSpace = nullptr;
}
scanState->first = true;
if (keys && scan->numberOfKeys > 0)
memmove(scan->keyData, keys, scan->numberOfKeys * sizeof(ScanKeyData));
if (orderbys && scan->numberOfOrderBys > 0)
memmove(scan->orderByData,
orderbys,
scan->numberOfOrderBys * sizeof(ScanKeyData));
}
/*
* Fetch the next tuple in the given scan
*/
bool hnsw_gettuple(IndexScanDesc scan, ScanDirection dir)
{
HNSWScanOpaque scanState = (HNSWScanOpaque) scan->opaque;
/*
* Index can be used to scan backward, but Postgres doesn't support
* backward scan on operators
*/
Assert(ScanDirectionIsForward(dir));
if (scanState->first)
{
scanState->workSpace = new HNSWIndexScan::WorkSpace();
std::string path = std::string(DataDir) + std::string("/") +
std::string(DatabasePath) + std::string("/") +
std::string(RelationGetRelationName(scan->indexRelation));
if (scan->orderByData == NULL)
{
if (scan->keyData == NULL)
return false;
if (scan->keyData->sk_flags & SK_ISNULL)
return false;
Datum value = scan->keyData->sk_argument;
scanState->workSpace->array = convert_array_to_vector(value);
scanState->hasRangeFilter = true;
scanState->inRange = false;
scanState->range = scanState->workSpace->array[0];
scanState->workSpace->resultIterator =
HNSWIndexScan::BeginScan((char *)(scanState->workSpace->array.data() + 1),path);
}
else{
if (scan->orderByData->sk_flags & SK_ISNULL)
return false;
Datum value = scan->orderByData->sk_argument;
scanState->workSpace->array = convert_array_to_vector(value);
scanState->hasRangeFilter = false;
scanState->range = 86;
scanState->workSpace->resultIterator =
HNSWIndexScan::BeginScan((char *)scanState->workSpace->array.data(),path);
}
scan->xs_inorder = false;
scanState->first = false;
}
int i = 0;
//TODO(Qianxi): set parameter
int distanceThreshold = 3;
int queueThreshold = 50;
while(true)
{
hnswlib::QueryResult<float> *result =
HNSWIndexScan::GetNet(scanState->workSpace->resultIterator);
if (!result->HasResult())
{
return false;
}
if (scanState->hasRangeFilter)
{
if (!scanState->inRange)
{
if (scanState->workSpace->distanceQueue.size() <
queueThreshold ||
scanState->workSpace->distanceQueue.top() >
result->GetDistance())
{
if (scanState->workSpace->distanceQueue.size() ==
queueThreshold)
{
scanState->workSpace->distanceQueue.pop();
}
scanState->workSpace->distanceQueue.push(
result->GetDistance());
}
else
{
return false;
}
}
if(result->GetDistance() > scanState->range)
{
i++;
if (scanState->inRange && i >= distanceThreshold)
{
return false;
}
else
{
continue;
}
}
else
{
scanState->inRange = true;
}
}
else if(!scan->xs_inorder){
if (scanState->workSpace->distanceQueue.size() == scanState->range &&
scanState->workSpace->distanceQueue.top() < result->GetDistance())
{
scan->xs_inorder=true;
}
else
{
if (scanState->workSpace->distanceQueue.size() == scanState->range)
{
scanState->workSpace->distanceQueue.pop();
}
scanState->workSpace->distanceQueue.push(result->GetDistance());
}
}
std::uint64_t number = result->GetLabel();
BlockNumber blkno = (std::uint32_t) (number >> 32);
OffsetNumber offset = (std::uint32_t) number;
#if PG_VERSION_NUM >= 120000
ItemPointerSet(&scan->xs_heaptid, blkno, offset);
#else
ItemPointerSet(&scan->xs_ctup.t_self, blkno, offset);
#endif
scan->xs_orderbyvals[0] = Float4GetDatum(result->GetDistance());
scan->xs_orderbynulls[0] = false;
scan->xs_recheckorderby = false;
return true;
}
}
/*
* End a scan and release resources
*/
void hnsw_endscan(IndexScanDesc scan)
{
HNSWScanOpaque scanState = (HNSWScanOpaque) scan->opaque;
HNSWIndexScan::EndScan(scanState->workSpace->resultIterator);
scanState->workSpace->resultIterator = nullptr;
delete scanState->workSpace;
pfree(scanState);
scan->opaque = NULL;
}
/*
* Validate catalog entries for the specified operator class
*/
bool hnsw_validate(Oid opclassoid)
{
return true;
}
void hnsw_costestimate(PlannerInfo *root,
IndexPath *path,
double loop_count,
Cost *indexStartupCost,
Cost *indexTotalCost,
Selectivity *indexSelectivity,
double *indexCorrelation
#if PG_VERSION_NUM >= 100000
,
double *indexPages
#endif
)
{
IndexOptInfo *index = path->indexinfo;
List *qinfos;
GenericCosts costs;
// Do preliminary analysis of indexquals
// qinfos = deconstruct_indexquals(path);
MemSet(&costs, 0, sizeof(costs));
// We have to visit all index tuples anyway
costs.numIndexTuples = index->tuples;
// Use generic estimate
genericcostestimate(root, path, loop_count, &costs);
*indexStartupCost = costs.indexStartupCost;
*indexTotalCost = costs.indexTotalCost;
*indexSelectivity = 0.01;
*indexCorrelation = costs.indexCorrelation;
#if PG_VERSION_NUM >= 100000
*indexPages = costs.numIndexPages;
#endif
}
/*
* Clean up after a VACUUM operation
*/
IndexBulkDeleteResult *hnsw_vacuumcleanup(IndexVacuumInfo *info,
IndexBulkDeleteResult *stats)
{
Relation rel = info->index;
if (stats == NULL)
return NULL;
stats->num_pages = 0;
return stats;
}