Skip to content

Commit

Permalink
Updated support for Npgsql 8 and Npgsql.EntityFrameworkCore.PostgreSQL 8
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Nov 22, 2023
1 parent 35c9e29 commit 6f28734
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 35 deletions.
4 changes: 2 additions & 2 deletions global.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"sdk": {
"version": "8.0.100-rc.2.23502.2",
"version": "8.0.100",
"rollForward": "latestMajor",
"allowPrerelease": "true"
"allowPrerelease": "false"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<None Include="..\..\LICENSE.txt" Pack="true" PackagePath="\" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
<ProjectReference Include="..\Pgvector\Pgvector.csproj" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0-rc.2" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ _ when ReferenceEquals(method, _methodCosineDistance) => "<=>",
{
var resultTypeMapping = _typeMappingSource.FindMapping(method.ReturnType)!;

return new PostgresUnknownBinaryExpression(
return new PgUnknownBinaryExpression(
left: _sqlExpressionFactory.ApplyDefaultTypeMapping(arguments[0]),
right: _sqlExpressionFactory.ApplyDefaultTypeMapping(arguments[1]),
binaryOperator: vectorOperator,
Expand Down
2 changes: 1 addition & 1 deletion src/Pgvector/Npgsql/VectorExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public static class VectorExtensions
{
public static INpgsqlTypeMapper UseVector(this INpgsqlTypeMapper mapper)
{
mapper.AddTypeInfoResolver(new VectorTypeInfoResolver());
mapper.AddTypeInfoResolverFactory(new VectorTypeInfoResolverFactory());
return mapper;
}
}
28 changes: 0 additions & 28 deletions src/Pgvector/Npgsql/VectorTypeInfoResolver.cs

This file was deleted.

42 changes: 42 additions & 0 deletions src/Pgvector/Npgsql/VectorTypeInfoResolverFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using Npgsql.Internal;
using Npgsql.Internal.Postgres;

namespace Pgvector.Npgsql;

public class VectorTypeInfoResolverFactory : PgTypeInfoResolverFactory
{
public override IPgTypeInfoResolver CreateResolver() => new Resolver();
public override IPgTypeInfoResolver CreateArrayResolver() => new ArrayResolver();

class Resolver : IPgTypeInfoResolver
{
TypeInfoMappingCollection? _mappings;
protected TypeInfoMappingCollection Mappings => _mappings ??= AddMappings(new());

public PgTypeInfo? GetTypeInfo(Type? type, DataTypeName? dataTypeName, PgSerializerOptions options)
=> Mappings.Find(type, dataTypeName, options);

static TypeInfoMappingCollection AddMappings(TypeInfoMappingCollection mappings)
{
mappings.AddType<Vector>("vector",
static (options, mapping, _) => mapping.CreateInfo(options, new VectorConverter()), isDefault: true);
return mappings;
}
}

sealed class ArrayResolver : Resolver, IPgTypeInfoResolver
{
TypeInfoMappingCollection? _mappings;
new TypeInfoMappingCollection Mappings => _mappings ??= AddMappings(new(base.Mappings));

public new PgTypeInfo? GetTypeInfo(Type? type, DataTypeName? dataTypeName, PgSerializerOptions options)
=> Mappings.Find(type, dataTypeName, options);

static TypeInfoMappingCollection AddMappings(TypeInfoMappingCollection mappings)
{
mappings.AddArrayType<Vector>("vector");
return mappings;
}
}
}
2 changes: 1 addition & 1 deletion src/Pgvector/Pgvector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ItemGroup>
<None Include="..\..\LICENSE.txt" Pack="true" PackagePath="\" />
<None Include="..\..\README.md" Pack="true" PackagePath="\" />
<PackageReference Include="Npgsql" Version="8.0.0-rc.2" />
<PackageReference Include="Npgsql" Version="8.0.0" />
</ItemGroup>

</Project>
4 changes: 3 additions & 1 deletion tests/Pgvector.Tests/EntityFrameworkCoreTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<Item>()
.HasIndex(i => i.Embedding)
.HasMethod("hnsw")
.HasOperators("vector_l2_ops");
.HasOperators("vector_l2_ops")
.HasStorageParameter("m", 16)
.HasStorageParameter("ef_construction", 64);
}
}

Expand Down

0 comments on commit 6f28734

Please sign in to comment.