Skip to content

Commit 6065a4d

Browse files
committed
Skip empty-array embeddings in pgvector similarity queries.
Filter out posts and requests whose embedding column is a zero-length array so legacy rows do not crash the embedding::vector cast in ORDER BY with "vector must have at least 1 dimension" across similar posts, request match, and purchase suggestion paths. Made-with: Cursor
1 parent 97067d8 commit 6065a4d

2 files changed

Lines changed: 4 additions & 0 deletions

File tree

src/repositories/PostRepository.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,7 @@ export class PostRepository extends AbstractRepository<PostModel> {
570570
.leftJoinAndSelect("post.user", "user")
571571
.where("post.id != :excludePostId", { excludePostId })
572572
.andWhere("post.embedding IS NOT NULL")
573+
.andWhere("CARDINALITY(post.embedding) > 0")
573574
.andWhere("post.archive = false")
574575
.andWhere("post.sold = false")
575576
.andWhere("user.firebaseUid != :excludeUserId", { excludeUserId })
@@ -594,6 +595,7 @@ export class PostRepository extends AbstractRepository<PostModel> {
594595
.createQueryBuilder("post")
595596
.leftJoinAndSelect("post.user", "user")
596597
.where("post.embedding IS NOT NULL")
598+
.andWhere("CARDINALITY(post.embedding) > 0")
597599
.andWhere("user.firebaseUid != :excludeUserId", { excludeUserId })
598600
.orderBy(`post.embedding::vector <-> CAST('${lit}' AS vector(512))`)
599601
.limit(limit)
@@ -717,6 +719,7 @@ export class PostRepository extends AbstractRepository<PostModel> {
717719
.createQueryBuilder("post")
718720
.leftJoinAndSelect("post.user", "user")
719721
.where("post.embedding IS NOT NULL")
722+
.andWhere("CARDINALITY(post.embedding) > 0")
720723
.andWhere("post.archive = false")
721724
.andWhere("post.sold = false")
722725
.andWhere("user.firebaseUid != :excludeUserId", { excludeUserId })

src/repositories/RequestRepository.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ export class RequestRepository extends AbstractRepository<RequestModel> {
128128
"distance",
129129
)
130130
.where("request.embedding IS NOT NULL")
131+
.andWhere("CARDINALITY(request.embedding) > 0")
131132
.andWhere("user.firebaseUid != :excludeUserId", { excludeUserId })
132133
// 3. Safely pass the embedding string as a parameter
133134
.setParameter("embedding", embeddingString)

0 commit comments

Comments
 (0)