Skip to content

Commit efac015

Browse files
committed
Improvements to EF type mapping
* Support scaffolding of vector types * Consolidated different mappins and plugins into the same files * A bit of test code cleanup Closes pgvector#44
1 parent 4de753b commit efac015

8 files changed

Lines changed: 81 additions & 88 deletions

‎src/Pgvector.EntityFrameworkCore/HalfvecTypeMapping.cs‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎src/Pgvector.EntityFrameworkCore/HalfvecTypeMappingSourcePlugin.cs‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎src/Pgvector.EntityFrameworkCore/SparsevecTypeMapping.cs‎

Lines changed: 0 additions & 19 deletions
This file was deleted.

‎src/Pgvector.EntityFrameworkCore/SparsevecTypeMappingSourcePlugin.cs‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎src/Pgvector.EntityFrameworkCore/VectorDbContextOptionsExtension.cs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public void ApplyServices(IServiceCollection services)
1717
.TryAdd<IMethodCallTranslatorPlugin, VectorDbFunctionsTranslatorPlugin>();
1818

1919
services.AddSingleton<IRelationalTypeMappingSourcePlugin, VectorTypeMappingSourcePlugin>();
20-
services.AddSingleton<IRelationalTypeMappingSourcePlugin, HalfvecTypeMappingSourcePlugin>();
21-
services.AddSingleton<IRelationalTypeMappingSourcePlugin, SparsevecTypeMappingSourcePlugin>();
2220
}
2321

2422
public void Validate(IDbContextOptions options) { }

‎src/Pgvector.EntityFrameworkCore/VectorTypeMapping.cs‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ namespace Pgvector.EntityFrameworkCore;
66

77
public class VectorTypeMapping : RelationalTypeMapping
88
{
9-
public static VectorTypeMapping Default { get; } = new();
9+
public static VectorTypeMapping Default { get; } = new("vector", typeof(Vector));
1010

11-
public VectorTypeMapping() : base("vector", typeof(Vector)) { }
12-
13-
public VectorTypeMapping(string storeType) : base(storeType, typeof(Vector)) { }
11+
public VectorTypeMapping(string storeType, Type clrType) : base(storeType, clrType) { }
1412

1513
protected VectorTypeMapping(RelationalTypeMappingParameters parameters) : base(parameters) { }
1614

‎src/Pgvector.EntityFrameworkCore/VectorTypeMappingSourcePlugin.cs‎

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,35 @@ namespace Pgvector.EntityFrameworkCore;
44

55
public class VectorTypeMappingSourcePlugin : IRelationalTypeMappingSourcePlugin
66
{
7+
private static readonly VectorTypeMapping _vectorMapping = new("vector", typeof(Vector));
8+
private static readonly VectorTypeMapping _halfVectorMapping = new("halfvec", typeof(HalfVector));
9+
private static readonly VectorTypeMapping _sparseVectorMapping = new("sparsevec", typeof(SparseVector));
10+
711
public RelationalTypeMapping? FindMapping(in RelationalTypeMappingInfo mappingInfo)
8-
=> mappingInfo.ClrType == typeof(Vector)
9-
? new VectorTypeMapping(mappingInfo.StoreTypeName ?? "vector")
10-
: null;
12+
{
13+
if (mappingInfo.StoreTypeName is not null)
14+
{
15+
var mapping = mappingInfo.StoreTypeName switch
16+
{
17+
"vector" => _vectorMapping,
18+
"halfvec" => _halfVectorMapping,
19+
"sparsevec" => _sparseVectorMapping,
20+
_ => null,
21+
};
22+
23+
// If the caller hasn't specified a CLR type (this is scaffolding), or if the user has specified
24+
// the one matching the store type, return the mapping.
25+
return mappingInfo.ClrType is null || mappingInfo.ClrType == mapping?.ClrType
26+
? mapping : null;
27+
}
28+
29+
// No store type specified, look up by the CLR type only
30+
return mappingInfo.ClrType switch
31+
{
32+
var t when t == typeof(Vector) => _vectorMapping,
33+
var t when t == typeof(HalfVector) => _halfVectorMapping,
34+
var t when t == typeof(SparseVector) => _sparseVectorMapping,
35+
_ => null,
36+
};
37+
}
1138
}

‎tests/Pgvector.CSharp.Tests/EntityFrameworkCoreTests.cs‎

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,79 +66,109 @@ public async Task Main()
6666

6767
var embedding = new Vector(new float[] { 1, 1, 1 });
6868
var items = await ctx.Items.FromSql($"SELECT * FROM efcore_items ORDER BY embedding <-> {embedding} LIMIT 5").ToListAsync();
69-
Assert.Equal(new int[] { 1, 3, 2 }, items.Select(v => v.Id).ToArray());
70-
Assert.Equal(new float[] { 1, 1, 1 }, items[0].Embedding!.ToArray());
71-
Assert.Equal(new Half[] { (Half)1, (Half)1, (Half)1 }, items[0].HalfEmbedding!.ToArray());
69+
Assert.Equal([1, 3, 2], items.Select(v => v.Id).ToArray());
70+
Assert.Equal([1, 1, 1], items[0].Embedding!.ToArray());
71+
Assert.Equal([(Half)1, (Half)1, (Half)1], items[0].HalfEmbedding!.ToArray());
7272
Assert.Equal(new BitArray(new bool[] { false, false, false }), items[0].BinaryEmbedding!);
73-
Assert.Equal(new float[] { 1, 1, 1 }, items[0].SparseEmbedding!.ToArray());
73+
Assert.Equal([1, 1, 1], items[0].SparseEmbedding!.ToArray());
7474

7575
// vector distance functions
7676

7777
items = await ctx.Items.OrderBy(x => x.Embedding!.L2Distance(embedding)).Take(5).ToListAsync();
78-
Assert.Equal(new int[] { 1, 3, 2 }, items.Select(v => v.Id).ToArray());
79-
Assert.Equal(new float[] { 1, 1, 1 }, items[0].Embedding!.ToArray());
78+
Assert.Equal([1, 3, 2], items.Select(v => v.Id).ToArray());
79+
Assert.Equal([1, 1, 1], items[0].Embedding!.ToArray());
8080

8181
items = await ctx.Items.OrderBy(x => x.Embedding!.MaxInnerProduct(embedding)).Take(5).ToListAsync();
82-
Assert.Equal(new int[] { 2, 3, 1 }, items.Select(v => v.Id).ToArray());
82+
Assert.Equal([2, 3, 1], items.Select(v => v.Id).ToArray());
8383

8484
items = await ctx.Items.OrderBy(x => x.Embedding!.CosineDistance(embedding)).Take(5).ToListAsync();
8585
Assert.Equal(3, items[2].Id);
8686

8787
items = await ctx.Items.OrderBy(x => x.Embedding!.L1Distance(embedding)).Take(5).ToListAsync();
88-
Assert.Equal(new int[] { 1, 3, 2 }, items.Select(v => v.Id).ToArray());
88+
Assert.Equal([1, 3, 2], items.Select(v => v.Id).ToArray());
8989

9090
// halfvec distance functions
9191

9292
var halfEmbedding = new HalfVector(new Half[] { (Half)1, (Half)1, (Half)1 });
9393
items = await ctx.Items.OrderBy(x => x.HalfEmbedding!.L2Distance(halfEmbedding)).Take(5).ToListAsync();
94-
Assert.Equal(new int[] { 1, 3, 2 }, items.Select(v => v.Id).ToArray());
94+
Assert.Equal([1, 3, 2], items.Select(v => v.Id).ToArray());
9595

9696
items = await ctx.Items.OrderBy(x => x.HalfEmbedding!.MaxInnerProduct(halfEmbedding)).Take(5).ToListAsync();
97-
Assert.Equal(new int[] { 2, 3, 1 }, items.Select(v => v.Id).ToArray());
97+
Assert.Equal([2, 3, 1], items.Select(v => v.Id).ToArray());
9898

9999
items = await ctx.Items.OrderBy(x => x.HalfEmbedding!.CosineDistance(halfEmbedding)).Take(5).ToListAsync();
100100
Assert.Equal(3, items[2].Id);
101101

102102
items = await ctx.Items.OrderBy(x => x.HalfEmbedding!.L1Distance(halfEmbedding)).Take(5).ToListAsync();
103-
Assert.Equal(new int[] { 1, 3, 2 }, items.Select(v => v.Id).ToArray());
103+
Assert.Equal([1, 3, 2], items.Select(v => v.Id).ToArray());
104104

105105
// sparsevec distance functions
106106

107107
var sparseEmbedding = new SparseVector(new float[] { 1, 1, 1 });
108108
items = await ctx.Items.OrderBy(x => x.SparseEmbedding!.L2Distance(sparseEmbedding)).Take(5).ToListAsync();
109-
Assert.Equal(new int[] { 1, 3, 2 }, items.Select(v => v.Id).ToArray());
109+
Assert.Equal([1, 3, 2], items.Select(v => v.Id).ToArray());
110110

111111
items = await ctx.Items.OrderBy(x => x.SparseEmbedding!.MaxInnerProduct(sparseEmbedding)).Take(5).ToListAsync();
112-
Assert.Equal(new int[] { 2, 3, 1 }, items.Select(v => v.Id).ToArray());
112+
Assert.Equal([2, 3, 1], items.Select(v => v.Id).ToArray());
113113

114114
items = await ctx.Items.OrderBy(x => x.SparseEmbedding!.CosineDistance(sparseEmbedding)).Take(5).ToListAsync();
115115
Assert.Equal(3, items[2].Id);
116116

117117
items = await ctx.Items.OrderBy(x => x.SparseEmbedding!.L1Distance(sparseEmbedding)).Take(5).ToListAsync();
118-
Assert.Equal(new int[] { 1, 3, 2 }, items.Select(v => v.Id).ToArray());
118+
Assert.Equal([1, 3, 2], items.Select(v => v.Id).ToArray());
119119

120120
// bit distance functions
121121

122122
var binaryEmbedding = new BitArray(new bool[] { true, false, true });
123123
items = await ctx.Items.OrderBy(x => x.BinaryEmbedding!.HammingDistance(binaryEmbedding)).Take(5).ToListAsync();
124-
Assert.Equal(new int[] { 2, 3, 1 }, items.Select(v => v.Id).ToArray());
124+
Assert.Equal([2, 3, 1], items.Select(v => v.Id).ToArray());
125125

126126
items = await ctx.Items.OrderBy(x => x.BinaryEmbedding!.JaccardDistance(binaryEmbedding)).Take(5).ToListAsync();
127-
Assert.Equal(new int[] { 2, 3, 1 }, items.Select(v => v.Id).ToArray());
127+
Assert.Equal([2, 3, 1], items.Select(v => v.Id).ToArray());
128128

129129
// additional
130130

131131
items = await ctx.Items
132132
.OrderBy(x => x.Id)
133133
.Where(x => x.Embedding!.L2Distance(embedding) < 1.5)
134134
.ToListAsync();
135-
Assert.Equal(new int[] { 1, 3 }, items.Select(v => v.Id).ToArray());
135+
Assert.Equal([1, 3], items.Select(v => v.Id).ToArray());
136136

137137
var neighbors = await ctx.Items
138138
.OrderBy(x => x.Embedding!.L2Distance(embedding))
139139
.Select(x => new { Entity = x, Distance = x.Embedding!.L2Distance(embedding) })
140140
.ToListAsync();
141-
Assert.Equal(new int[] { 1, 3, 2 }, neighbors.Select(v => v.Entity.Id).ToArray());
142-
Assert.Equal(new double[] { 0, 1, Math.Sqrt(3) }, neighbors.Select(v => v.Distance).ToArray());
141+
Assert.Equal([1, 3, 2], neighbors.Select(v => v.Entity.Id).ToArray());
142+
Assert.Equal([0, 1, Math.Sqrt(3)], neighbors.Select(v => v.Distance).ToArray());
143+
}
144+
145+
[Fact]
146+
public void TypeMappingTests()
147+
{
148+
var typeMappingSource = new VectorTypeMappingSourcePlugin();
149+
150+
var vectorTypeMapping = typeMappingSource.FindMapping(new RelationalTypeMappingInfo { ClrType = typeof(Vector) })!;
151+
Assert.Equal("vector", vectorTypeMapping.StoreType);
152+
Assert.Same(typeof(Vector), vectorTypeMapping.ClrType);
153+
154+
vectorTypeMapping = typeMappingSource.FindMapping(new RelationalTypeMappingInfo { StoreTypeName = "vector" })!;
155+
Assert.Equal("vector", vectorTypeMapping.StoreType);
156+
Assert.Same(typeof(Vector), vectorTypeMapping.ClrType);
157+
158+
var halfVectorTypeMapping = typeMappingSource.FindMapping(new RelationalTypeMappingInfo { ClrType = typeof(HalfVector) })!;
159+
Assert.Equal("halfvec", halfVectorTypeMapping.StoreType);
160+
Assert.Same(typeof(HalfVector), halfVectorTypeMapping.ClrType);
161+
162+
halfVectorTypeMapping = typeMappingSource.FindMapping(new RelationalTypeMappingInfo { StoreTypeName = "halfvec" })!;
163+
Assert.Equal("halfvec", halfVectorTypeMapping.StoreType);
164+
Assert.Same(typeof(HalfVector), halfVectorTypeMapping.ClrType);
165+
166+
var sparseVectorTypeMapping = typeMappingSource.FindMapping(new RelationalTypeMappingInfo { ClrType = typeof(SparseVector) })!;
167+
Assert.Equal("sparsevec", sparseVectorTypeMapping.StoreType);
168+
Assert.Same(typeof(SparseVector), sparseVectorTypeMapping.ClrType);
169+
170+
sparseVectorTypeMapping = typeMappingSource.FindMapping(new RelationalTypeMappingInfo { StoreTypeName = "sparsevec" })!;
171+
Assert.Equal("sparsevec", sparseVectorTypeMapping.StoreType);
172+
Assert.Same(typeof(SparseVector), sparseVectorTypeMapping.ClrType);
143173
}
144174
}

0 commit comments

Comments
 (0)