-
Notifications
You must be signed in to change notification settings - Fork 34
Array fields support #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f031efe
Added Array fields support
BlackGad ae8675f
Added unit tests
BlackGad 4f7a753
Submodule update to v2.4
BlackGad fed64e5
Reverted unnecessary changes
BlackGad 5d525a5
Code style, namings changes. Removed extra array data convertion. Cod…
BlackGad 01bb6c0
Revert "Submodule update to v2.4"
BlackGad 9f1f276
Whitespace stuff
roji File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,187 @@ | ||
namespace Milvus.Client; | ||
|
||
/// <summary> | ||
/// Binary Field | ||
/// </summary> | ||
public sealed class ArrayFieldData<TElementData> : FieldData<IReadOnlyList<TElementData>> | ||
{ | ||
/// <summary> | ||
/// Construct an array field | ||
/// </summary> | ||
/// <param name="fieldName"></param> | ||
/// <param name="data"></param> | ||
/// <param name="isDynamic"></param> | ||
public ArrayFieldData(string fieldName, IEnumerable<IEnumerable<TElementData>> data, bool isDynamic) | ||
: base(fieldName, data.Select(x => x.ToArray()).ToArray(), MilvusDataType.Array, isDynamic) | ||
roji marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
ElementType = EnsureDataType<TElementData>(); | ||
} | ||
|
||
/// <summary> | ||
/// Array element type | ||
/// </summary> | ||
public MilvusDataType ElementType { get; } | ||
|
||
/// <inheritdoc /> | ||
internal override Grpc.FieldData ToGrpcFieldData() | ||
{ | ||
Check(); | ||
|
||
Grpc.FieldData fieldData = new() | ||
{ | ||
Type = Grpc.DataType.Array, | ||
IsDynamic = IsDynamic | ||
}; | ||
|
||
if (FieldName is not null) | ||
{ | ||
fieldData.FieldName = FieldName; | ||
} | ||
|
||
var arrayArray = new ArrayArray | ||
{ | ||
ElementType = (DataType) ElementType, | ||
}; | ||
|
||
fieldData.Scalars = new ScalarField | ||
{ | ||
ArrayData = arrayArray | ||
}; | ||
|
||
foreach (var array in Data) | ||
{ | ||
switch (ElementType) | ||
{ | ||
case MilvusDataType.Bool: | ||
Grpc.BoolArray boolData = new(); | ||
boolData.Data.AddRange(array as IEnumerable<bool>); | ||
arrayArray.Data.Add(new ScalarField {BoolData = boolData}); | ||
BlackGad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
break; | ||
|
||
case MilvusDataType.Int8: | ||
Grpc.IntArray int8Data = new(); | ||
var sbytes = array as IEnumerable<sbyte> ?? Enumerable.Empty<sbyte>(); | ||
int8Data.Data.AddRange(sbytes.Select(x => (int) x)); | ||
arrayArray.Data.Add(new ScalarField {IntData = int8Data}); | ||
break; | ||
|
||
case MilvusDataType.Int16: | ||
Grpc.IntArray int16Data = new(); | ||
var shorts = array as IEnumerable<short> ?? Enumerable.Empty<short>(); | ||
int16Data.Data.AddRange(shorts.Select(x=>(int)x)); | ||
arrayArray.Data.Add(new ScalarField {IntData = int16Data}); | ||
break; | ||
|
||
case MilvusDataType.Int32: | ||
Grpc.IntArray int32Data = new(); | ||
int32Data.Data.AddRange(array as IEnumerable<int>); | ||
arrayArray.Data.Add(new ScalarField {IntData = int32Data}); | ||
break; | ||
|
||
case MilvusDataType.Int64: | ||
Grpc.LongArray int64Data = new(); | ||
int64Data.Data.AddRange(array as IEnumerable<long>); | ||
arrayArray.Data.Add(new ScalarField {LongData = int64Data}); | ||
break; | ||
|
||
case MilvusDataType.Float: | ||
Grpc.FloatArray floatData = new(); | ||
floatData.Data.AddRange(array as IEnumerable<float>); | ||
arrayArray.Data.Add(new ScalarField {FloatData = floatData}); | ||
break; | ||
|
||
case MilvusDataType.Double: | ||
Grpc.DoubleArray doubleData = new(); | ||
doubleData.Data.AddRange(array as IEnumerable<double>); | ||
arrayArray.Data.Add(new ScalarField {DoubleData = doubleData}); | ||
break; | ||
|
||
case MilvusDataType.String: | ||
Grpc.StringArray stringData = new(); | ||
stringData.Data.AddRange(array as IEnumerable<string>); | ||
arrayArray.Data.Add(new ScalarField {StringData = stringData}); | ||
break; | ||
|
||
case MilvusDataType.VarChar: | ||
Grpc.StringArray varcharData = new(); | ||
varcharData.Data.AddRange(array as IEnumerable<string>); | ||
arrayArray.Data.Add(new ScalarField {StringData = varcharData}); | ||
break; | ||
|
||
case MilvusDataType.Json: | ||
Grpc.JSONArray jsonData = new(); | ||
var enumerable = array as IEnumerable<string> ?? Enumerable.Empty<string>(); | ||
jsonData.Data.AddRange(enumerable.Select(ByteString.CopyFromUtf8)); | ||
arrayArray.Data.Add(new ScalarField {JsonData = jsonData}); | ||
break; | ||
case MilvusDataType.None: | ||
throw new MilvusException($"ElementType Error:{DataType}"); | ||
default: | ||
throw new MilvusException($"ElementType Error:{DataType}, not supported"); | ||
BlackGad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
return fieldData; | ||
|
||
/* | ||
BlackGad marked this conversation as resolved.
Show resolved
Hide resolved
|
||
int dataCount = Data.Count; | ||
if (dataCount == 0) | ||
{ | ||
throw new MilvusException("The number of vectors must be positive."); | ||
} | ||
|
||
int vectorByteLength = Data[0].Length; | ||
int totalByteLength = vectorByteLength; | ||
for (int i = 1; i < dataCount; i++) | ||
{ | ||
int rowLength = Data[i].Length; | ||
if (rowLength != vectorByteLength) | ||
{ | ||
throw new MilvusException("All vectors must have the same dimensionality."); | ||
} | ||
|
||
checked { totalByteLength += rowLength; } | ||
} | ||
|
||
byte[] bytes = ArrayPool<byte>.Shared.Rent(totalByteLength); | ||
int pos = 0; | ||
for (int i = 0; i < dataCount; i++) | ||
{ | ||
ReadOnlyMemory<byte> row = Data[i]; | ||
row.Span.CopyTo(bytes.AsSpan(pos, row.Length)); | ||
pos += row.Length; | ||
} | ||
Debug.Assert(pos == totalByteLength); | ||
|
||
var result = new Grpc.FieldData | ||
{ | ||
FieldName = FieldName, | ||
Type = (Grpc.DataType)DataType, | ||
Vectors = new Grpc.VectorField | ||
{ | ||
BinaryVector = ByteString.CopyFrom(bytes.AsSpan(0, totalByteLength)), | ||
Dim = vectorByteLength * 8, | ||
} | ||
}; | ||
|
||
ArrayPool<byte>.Shared.Return(bytes); | ||
*/ | ||
} | ||
|
||
internal override object GetValueAsObject(int index) | ||
=> ElementType switch | ||
{ | ||
MilvusDataType.Bool => ((IReadOnlyList<IEnumerable<bool>>) Data)[index], | ||
MilvusDataType.Int8 => ((IReadOnlyList<IEnumerable<sbyte>>) Data)[index], | ||
MilvusDataType.Int16 => ((IReadOnlyList<IEnumerable<short>>) Data)[index], | ||
MilvusDataType.Int32 => ((IReadOnlyList<IEnumerable<int>>) Data)[index], | ||
MilvusDataType.Int64 => ((IReadOnlyList<IEnumerable<long>>) Data)[index], | ||
MilvusDataType.Float => ((IReadOnlyList<IEnumerable<float>>) Data)[index], | ||
MilvusDataType.Double => ((IReadOnlyList<IEnumerable<double>>) Data)[index], | ||
MilvusDataType.String => ((IReadOnlyList<IEnumerable<string>>) Data)[index], | ||
MilvusDataType.VarChar => ((IReadOnlyList<IEnumerable<string>>) Data)[index], | ||
|
||
MilvusDataType.None => throw new MilvusException($"DataType Error:{DataType}"), | ||
_ => throw new MilvusException($"DataType Error:{DataType}, not supported") | ||
}; | ||
} |
This file contains hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.