From dc60ae7b1d72fd4fab870a5c97ffec0cb86f85e0 Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Fri, 7 Aug 2026 10:49:40 -0700 Subject: [PATCH] Fixed error with avg aggregate when no matching rows - resolves #1012 Co-authored-by: Vaibhav Jain --- CHANGELOG.md | 4 ++++ src/vector.c | 3 ++- test/expected/vector_type.out | 6 +++++- test/t/018_aggregates.pl | 8 ++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b95541..d4121a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.8.7 (unreleased) + +- Fixed error with `avg` aggregate when no matching rows + ## 0.8.6 (2026-07-29) - Fixed buffer overflow with IVFFlat index build on 32-bit systems - [more info](https://github.com/pgvector/pgvector/issues/1006) diff --git a/src/vector.c b/src/vector.c index b75cfc4..5135be3 100644 --- a/src/vector.c +++ b/src/vector.c @@ -1233,7 +1233,8 @@ vector_combine(PG_FUNCTION_ARGS) { n = n2; dim = STATE_DIMS(statearray2); - CheckDim(dim); + if (dim != 0) + CheckDim(dim); statedatums = CreateStateDatums(dim); for (int i = 0; i < dim; i++) statedatums[i + 1] = Float8GetDatum(statevalues2[i + 1]); diff --git a/test/expected/vector_type.out b/test/expected/vector_type.out index b6dc9bc..67fce2a 100644 --- a/test/expected/vector_type.out +++ b/test/expected/vector_type.out @@ -713,7 +713,11 @@ ERROR: vector_combine: expected state array SELECT vector_combine('{0}', '{}'); ERROR: vector_combine: expected state array SELECT vector_combine('{0}', '{0}'); -ERROR: vector must have at least 1 dimension + vector_combine +---------------- + {0} +(1 row) + SELECT vector_combine('{0}', (SELECT array_agg(n) FROM generate_series(1, 16002) n)); ERROR: vector cannot have more than 16000 dimensions SELECT vector_combine((SELECT array_agg(n) FROM generate_series(1, 16002) n), '{0}'); diff --git a/test/t/018_aggregates.pl b/test/t/018_aggregates.pl index a8dd786..6ab4de3 100644 --- a/test/t/018_aggregates.pl +++ b/test/t/018_aggregates.pl @@ -42,6 +42,10 @@ sub test_aggregate my $explain = $node->safe_psql("postgres", "EXPLAIN SELECT $agg(v) FROM tst;"); like($explain, qr/Partial Aggregate/); + # Test no matching rows + $res = $node->safe_psql("postgres", "SELECT $agg(v) FROM tst WHERE r1 < 0;"); + is($res, ''); + # Test halfvec $res = $node->safe_psql("postgres", "SELECT $agg(v::halfvec) FROM tst;"); if ($agg eq 'avg') @@ -55,6 +59,10 @@ sub test_aggregate # Does not raise overflow error in this instance due to loss of precision is($res, "[24576,24576,49152]"); } + + # Test halfvec no matching rows + $res = $node->safe_psql("postgres", "SELECT $agg(v::halfvec) FROM tst WHERE r1 < 0;"); + is($res, ''); } test_aggregate('avg');