Added sorting [skip ci]

This commit is contained in:
Andrew Kane
2024-09-21 22:56:03 -07:00
parent e07d17fe38
commit f1c7494c35

View File

@@ -141,6 +141,24 @@ hnswrescan(IndexScanDesc scan, ScanKey keys, int nkeys, ScanKey orderbys, int no
memmove(scan->orderByData, orderbys, scan->numberOfOrderBys * sizeof(ScanKeyData));
}
/*
* Compare search candidate distances
*/
static int
CompareSearchCandidateDistances(const ListCell *a, const ListCell *b)
{
HnswSearchCandidate *hca = lfirst(a);
HnswSearchCandidate *hcb = lfirst(b);
if (hca->distance < hcb->distance)
return 1;
if (hca->distance > hcb->distance)
return -1;
return 0;
}
/*
* Fetch the next tuple in the given scan
*/
@@ -216,10 +234,12 @@ hnswgettuple(IndexScanDesc scan, ScanDirection dir)
break;
}
/* Return remaining tuples and exit */
/* TODO sort */
/* Return remaining tuples */
so->w = so->discarded;
so->discarded = NIL;
/* Sort in reverse order since results are removed from end */
list_sort(so->w, CompareSearchCandidateDistances);
}
else
{
@@ -245,6 +265,8 @@ hnswgettuple(IndexScanDesc scan, ScanDirection dir)
hc = llast(so->w);
element = HnswPtrAccess(base, hc->element);
elog(INFO, "distance = %f", hc->distance);
/* Move to next element if no valid heap TIDs */
if (element->heaptidsLength == 0)
{