Fixed zero norm detected error with IVFFlat index build - resolves #1076 [skip ci]

Co-authored-by: Song Bian <29000790+Waterpine@users.noreply.github.com>
This commit is contained in:
Andrew KaneandSong Bian committed 2026-12-03 12:01:26 -08:00
1 parent ff00e3cd70
commit 8877adc5a7
3 files changed
+23 -2

No files matched your search

+11 -2
View File
@@ -144,7 +144,7 @@ ShowMemoryUsage(MemoryContext context, Size estimatedSize)
* Compute new centers
*/
static void
ComputeNewCenters(VectorArray samples, int dimensions, float *agg, VectorArray newCenters, int *centerCounts, int *closestCenters, FmgrInfo *normprocinfo, Oid collation, const IvfflatTypeInfo * typeInfo)
ComputeNewCenters(VectorArray samples, int dimensions, float *agg, VectorArray newCenters, VectorArray prevCenters, int *centerCounts, int *closestCenters, FmgrInfo *normprocinfo, Oid collation, const IvfflatTypeInfo * typeInfo)
{
int numCenters = newCenters->length;
int numSamples = samples->length;
@@ -200,7 +200,16 @@ ComputeNewCenters(VectorArray samples, int dimensions, float *agg, VectorArray n
/* Normalize if needed */
if (normprocinfo != NULL)
{
NormCenters(typeInfo, collation, newCenters, dimensions);
for (int i = 0; i < numCenters; i++)
{
/* Use previous center if zero norm */
if (!IvfflatCheckNorm(normprocinfo, collation, PointerGetDatum(VectorArrayGet(newCenters, i))))
VectorArraySet(newCenters, i, VectorArrayGet(prevCenters, i));
}
}
}
/*
@@ -418,7 +427,7 @@ ElkanKmeans(Relation index, VectorArray samples, VectorArray centers, int dimens
}
/* Step 4: For each center c, let m(c) be mean of all points assigned */
ComputeNewCenters(samples, dimensions, agg, newCenters, centerCounts, closestCenters, normprocinfo, collation, typeInfo);
ComputeNewCenters(samples, dimensions, agg, newCenters, centers, centerCounts, closestCenters, normprocinfo, collation, typeInfo);
/* Step 5 */
for (int j = 0; j < numCenters; j++)
+5
View File
@@ -284,3 +284,8 @@ RESET client_min_messages;
RESET jit;
DROP TABLE t;
RESET work_mem;
-- k-means zero norm
CREATE TABLE t (val vector(2));
INSERT INTO t (val) VALUES ('[1,0]'), ('[-1,0]');
CREATE INDEX ON t USING ivfflat (val vector_cosine_ops) WITH (lists = 1);
DROP TABLE t;
+7
View File
@@ -154,3 +154,10 @@ RESET jit;
DROP TABLE t;
RESET work_mem;
-- k-means zero norm
CREATE TABLE t (val vector(2));
INSERT INTO t (val) VALUES ('[1,0]'), ('[-1,0]');
CREATE INDEX ON t USING ivfflat (val vector_cosine_ops) WITH (lists = 1);
DROP TABLE t;