-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
335 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Dapper; | ||
using Pgvector; | ||
using System; | ||
using System.Data; | ||
using System.Data.SqlClient; | ||
|
||
namespace Pgvector.Dapper; | ||
|
||
public class HalfvecTypeHandler : SqlMapper.TypeHandler<HalfVector?> | ||
{ | ||
public override HalfVector? Parse(object value) | ||
=> value switch | ||
{ | ||
null or DBNull => null, | ||
HalfVector vec => vec, | ||
_ => value.ToString() is string s ? new HalfVector(s) : null | ||
}; | ||
|
||
public override void SetValue(IDbDataParameter parameter, HalfVector? value) | ||
{ | ||
parameter.Value = value is null ? DBNull.Value : value; | ||
|
||
if (parameter is SqlParameter sqlParameter) | ||
{ | ||
sqlParameter.UdtTypeName = "halfvec"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping; | ||
using NpgsqlTypes; | ||
|
||
namespace Pgvector.EntityFrameworkCore; | ||
|
||
public class HalfvecTypeMapping : RelationalTypeMapping | ||
{ | ||
public static HalfvecTypeMapping Default { get; } = new(); | ||
|
||
public HalfvecTypeMapping() : base("halfvec", typeof(HalfVector)) { } | ||
|
||
public HalfvecTypeMapping(string storeType) : base(storeType, typeof(HalfVector)) { } | ||
|
||
protected HalfvecTypeMapping(RelationalTypeMappingParameters parameters) : base(parameters) { } | ||
|
||
protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters) | ||
=> new HalfvecTypeMapping(parameters); | ||
} |
11 changes: 11 additions & 0 deletions
11
src/Pgvector.EntityFrameworkCore/HalfvecTypeMappingSourcePlugin.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
|
||
namespace Pgvector.EntityFrameworkCore; | ||
|
||
public class HalfvecTypeMappingSourcePlugin : IRelationalTypeMappingSourcePlugin | ||
{ | ||
public RelationalTypeMapping? FindMapping(in RelationalTypeMappingInfo mappingInfo) | ||
=> mappingInfo.ClrType == typeof(HalfVector) | ||
? new HalfvecTypeMapping(mappingInfo.StoreTypeName ?? "halfvec") | ||
: null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
using System.Globalization; | ||
using System.Linq; | ||
|
||
namespace Pgvector; | ||
|
||
public class HalfVector : IEquatable<HalfVector> | ||
{ | ||
public ReadOnlyMemory<Half> Memory { get; } | ||
|
||
public HalfVector(ReadOnlyMemory<Half> v) | ||
=> Memory = v; | ||
|
||
public HalfVector(string s) | ||
=> Memory = Array.ConvertAll(s.Substring(1, s.Length - 2).Split(','), v => Half.Parse(v, CultureInfo.InvariantCulture)); | ||
|
||
public override string ToString() | ||
=> string.Concat("[", string.Join(",", Memory.ToArray().Select(v => v.ToString(CultureInfo.InvariantCulture))), "]"); | ||
|
||
public Half[] ToArray() | ||
=> Memory.ToArray(); | ||
|
||
public bool Equals(HalfVector? other) | ||
=> other is not null && Memory.Span.SequenceEqual(other.Memory.Span); | ||
|
||
public override bool Equals(object? obj) | ||
=> obj is HalfVector vector && Equals(vector); | ||
|
||
public static bool operator ==(HalfVector? x, HalfVector? y) | ||
=> (x is null && y is null) || (x is not null && x.Equals(y)); | ||
|
||
public static bool operator !=(HalfVector? x, HalfVector? y) => !(x == y); | ||
|
||
public override int GetHashCode() | ||
{ | ||
var hashCode = new HashCode(); | ||
var span = Memory.Span; | ||
|
||
for (var i = 0; i < span.Length; i++) | ||
hashCode.Add(span[i]); | ||
|
||
return hashCode.ToHashCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Npgsql.Internal; | ||
|
||
namespace Pgvector.Npgsql; | ||
|
||
public class HalfvecConverter : PgStreamingConverter<HalfVector> | ||
{ | ||
public override HalfVector Read(PgReader reader) | ||
{ | ||
if (reader.ShouldBuffer(2 * sizeof(ushort))) | ||
reader.Buffer(2 * sizeof(ushort)); | ||
|
||
var dim = reader.ReadUInt16(); | ||
var unused = reader.ReadUInt16(); | ||
if (unused != 0) | ||
throw new InvalidCastException("expected unused to be 0"); | ||
|
||
var vec = new Half[dim]; | ||
for (var i = 0; i < dim; i++) | ||
{ | ||
if (reader.ShouldBuffer(sizeof(ushort))) | ||
reader.Buffer(sizeof(ushort)); | ||
vec[i] = BitConverter.UInt16BitsToHalf(reader.ReadUInt16()); | ||
} | ||
|
||
return new HalfVector(vec); | ||
} | ||
|
||
public override async ValueTask<HalfVector> ReadAsync(PgReader reader, CancellationToken cancellationToken = default) | ||
{ | ||
if (reader.ShouldBuffer(2 * sizeof(ushort))) | ||
await reader.BufferAsync(2 * sizeof(ushort), cancellationToken).ConfigureAwait(false); | ||
|
||
var dim = reader.ReadUInt16(); | ||
var unused = reader.ReadUInt16(); | ||
if (unused != 0) | ||
throw new InvalidCastException("expected unused to be 0"); | ||
|
||
var vec = new Half[dim]; | ||
for (var i = 0; i < dim; i++) | ||
{ | ||
if (reader.ShouldBuffer(sizeof(ushort))) | ||
await reader.BufferAsync(sizeof(ushort), cancellationToken).ConfigureAwait(false); | ||
vec[i] = BitConverter.UInt16BitsToHalf(reader.ReadUInt16()); | ||
} | ||
|
||
return new HalfVector(vec); | ||
} | ||
|
||
public override Size GetSize(SizeContext context, HalfVector value, ref object? writeState) | ||
=> sizeof(ushort) * 2 + sizeof(ushort) * value.ToArray().Length; | ||
|
||
public override void Write(PgWriter writer, HalfVector value) | ||
{ | ||
if (writer.ShouldFlush(sizeof(ushort) * 2)) | ||
writer.Flush(); | ||
|
||
var span = value.Memory.Span; | ||
var dim = span.Length; | ||
writer.WriteUInt16(Convert.ToUInt16(dim)); | ||
writer.WriteUInt16(0); | ||
|
||
for (int i = 0; i < dim; i++) | ||
{ | ||
if (writer.ShouldFlush(sizeof(ushort))) | ||
writer.Flush(); | ||
writer.WriteUInt16(BitConverter.HalfToUInt16Bits(span[i])); | ||
} | ||
} | ||
|
||
public override async ValueTask WriteAsync( | ||
PgWriter writer, HalfVector value, CancellationToken cancellationToken = default) | ||
{ | ||
if (writer.ShouldFlush(sizeof(ushort) * 2)) | ||
await writer.FlushAsync(cancellationToken); | ||
|
||
var memory = value.Memory; | ||
var dim = memory.Length; | ||
writer.WriteUInt16(Convert.ToUInt16(dim)); | ||
writer.WriteUInt16(0); | ||
|
||
for (int i = 0; i < dim; i++) | ||
{ | ||
if (writer.ShouldFlush(sizeof(ushort))) | ||
await writer.FlushAsync(cancellationToken); | ||
writer.WriteUInt16(BitConverter.HalfToUInt16Bits(memory.Span[i])); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 HalfvecTypeInfoResolverFactory : 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<HalfVector>("halfvec", | ||
static (options, mapping, _) => mapping.CreateInfo(options, new HalfvecConverter()), 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<HalfVector>("halfvec"); | ||
return mappings; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.