mirror of
https://github.com/pgvector/pgvector.git
synced 2026-07-14 00:26:55 +08:00
optimize: defer sample normalization when building ivfflat
This commit is contained in:
@@ -63,15 +63,13 @@ AddSample(Datum *values, IvfflatBuildState * buildstate)
|
|||||||
Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
|
Datum value = PointerGetDatum(PG_DETOAST_DATUM(values[0]));
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Normalize with KMEANS_NORM_PROC since spherical distance function
|
* Check with KMEANS_NORM_PROC that the value can be normalized
|
||||||
* expects unit vectors
|
* since spherical distance function expects unit vectors
|
||||||
*/
|
*/
|
||||||
if (buildstate->kmeansnormprocinfo != NULL)
|
if (buildstate->kmeansnormprocinfo != NULL)
|
||||||
{
|
{
|
||||||
if (!IvfflatCheckNorm(buildstate->kmeansnormprocinfo, buildstate->collation, value))
|
if (!IvfflatCheckNorm(buildstate->kmeansnormprocinfo, buildstate->collation, value))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
value = IvfflatNormValue(buildstate->typeInfo, buildstate->collation, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (samples->length < targsamples)
|
if (samples->length < targsamples)
|
||||||
@@ -150,6 +148,22 @@ SampleRows(IvfflatBuildState * buildstate)
|
|||||||
table_index_build_range_scan(buildstate->heap, buildstate->index, buildstate->indexInfo,
|
table_index_build_range_scan(buildstate->heap, buildstate->index, buildstate->indexInfo,
|
||||||
false, true, false, targblock, 1, SampleCallback, (void *) buildstate, NULL);
|
false, true, false, targblock, 1, SampleCallback, (void *) buildstate, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Normalize if needed */
|
||||||
|
if (buildstate->kmeansnormprocinfo)
|
||||||
|
{
|
||||||
|
VectorArray samples = buildstate->samples;
|
||||||
|
Datum value;
|
||||||
|
Datum nvalue;
|
||||||
|
|
||||||
|
for (int i = 0; i < samples->length; i++)
|
||||||
|
{
|
||||||
|
value = PointerGetDatum(VectorArrayGet(samples, i));
|
||||||
|
nvalue = IvfflatNormValue(buildstate->typeInfo, buildstate->collation, value);
|
||||||
|
VectorArraySet(samples, i, DatumGetPointer(nvalue));
|
||||||
|
pfree(DatumGetPointer(nvalue));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user