Skip to content

Commit a283845

Browse files
committed
fix(pgvector): use the correct lowering for cosineDistance
The lowering for `cosineDistance` function used `1 - ({{self}} <=> {{arg0}})` template, which is the formula for cosine similarity, not cosine distance. `<=>` itself is the cosine distance operator in pgvector.
1 parent 2d06c5e commit a283845

6 files changed

Lines changed: 13 additions & 13 deletions

File tree

packages/2-sql/4-lanes/sql-builder-new/test/integration/extension-functions.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ import { collect, setupIntegrationTest } from './setup';
44
describe('integration: extension functions', () => {
55
const { db } = setupIntegrationTest();
66

7-
it('cosineDistance computes similarity for identical vectors', async () => {
7+
it('cosineDistance computes distance for identical vectors', async () => {
88
const row = await db()
99
.posts.select('id')
10-
.select('similarity', (f, fns) => fns.cosineDistance(f.embedding, [1, 0, 0]))
10+
.select('distance', (f, fns) => fns.cosineDistance(f.embedding, [1, 0, 0]))
1111
.where((f, fns) => fns.eq(f.id, 1))
1212
.first();
1313
expect(row).not.toBeNull();
14-
// template: 1 - (self <=> arg0), identical vectors → 1 - 0 = 1
15-
expect(row!.similarity).toBeCloseTo(1, 5);
14+
// template: self <=> arg0, identical vectors → distance = 0
15+
expect(row!.distance).toBeCloseTo(0, 5);
1616
});
1717

1818
it('cosineDistance filters in WHERE', async () => {
19-
// post 1 has embedding [1,0,0] → similarity to [1,0,0] is 1.0
20-
// post 3 has embedding [0,0,1] → similarity to [1,0,0] is ~0 (orthogonal)
19+
// post 1 has embedding [1,0,0] → distance to [1,0,0] is 0.0
20+
// post 3 has embedding [0,0,1] → distance to [1,0,0] is ~1 (orthogonal)
2121
const rows = await collect(
2222
db()
2323
.posts.select('id')
24-
.where((f, fns) => fns.gt(fns.cosineDistance(f.embedding, [1, 0, 0]), 0.5))
24+
.where((f, fns) => fns.lt(fns.cosineDistance(f.embedding, [1, 0, 0]), 0.5))
2525
.all(),
2626
);
2727
expect(rows.length).toBeGreaterThan(0);

packages/2-sql/4-lanes/sql-builder-new/test/runtime/functions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ describe('extension functions', () => {
266266
lowering: {
267267
targetFamily: 'sql' as const,
268268
strategy: 'function' as const,
269-
template: '1 - ({{self}} <=> {{arg0}})',
269+
template: '{{self}} <=> {{arg0}}',
270270
},
271271
},
272272
};

packages/3-extensions/pgvector/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ Computes the cosine distance between two vectors.
164164

165165
**Signature**: `cosineDistance(rhs: number[] | vector): number`
166166

167-
**SQL**: Uses the pgvector `<=>` operator: `1 - (vector1 <=> vector2)`
167+
**SQL**: Uses the pgvector `<=>` operator: `vector1 <=> vector2`
168168

169169
**Example**:
170170
```typescript

packages/3-extensions/pgvector/src/core/descriptor-meta.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const pgvectorTypeId = 'pg/vector@1' as const;
66
const cosineLowering = {
77
targetFamily: 'sql',
88
strategy: 'function',
9-
template: '1 - ({{self}} <=> {{arg0}})',
9+
template: '{{self}} <=> {{arg0}}',
1010
} as const;
1111

1212
const cosineDistanceOperation = Object.freeze({
@@ -32,7 +32,7 @@ export const pgvectorQueryOperations: readonly QueryOperationDescriptor[] = [
3232
lowering: {
3333
targetFamily: 'sql',
3434
strategy: 'function',
35-
template: '1 - ({{self}} <=> {{arg0}})',
35+
template: '{{self}} <=> {{arg0}}',
3636
},
3737
},
3838
];

packages/3-extensions/pgvector/test/manifest.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ describe('pgvector descriptor', () => {
4444
expect(cosineDistanceOp?.lowering).toEqual({
4545
targetFamily: 'sql',
4646
strategy: 'function',
47-
template: '1 - ({{self}} <=> {{arg0}})',
47+
template: '{{self}} <=> {{arg0}}',
4848
});
4949
});
5050

packages/3-extensions/pgvector/test/operations.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('pgvector operations', () => {
3535
expect(cosineDistanceOp?.lowering).toEqual({
3636
targetFamily: 'sql',
3737
strategy: 'function',
38-
template: '1 - ({{self}} <=> {{arg0}})',
38+
template: '{{self}} <=> {{arg0}}',
3939
});
4040
});
4141

0 commit comments

Comments
 (0)