Skip to content
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

.Net: Optimize DuckDb GetBatchAsync to retrieve multiple entries with a single query #8014

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions dotnet/src/Connectors/Connectors.Memory.DuckDB/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,53 @@ ORDER BY score DESC
return null;
}

public async IAsyncEnumerable<DatabaseEntry> ReadAllAsync(DuckDBConnection conn,
string collectionName,
string[] keys,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
if (keys.Length == 0)
{
yield break;
}

using var cmd = conn.CreateCommand();
var keyPlaceholders = string.Join(", ", keys.Select((k) => "?"));

#pragma warning disable CA2100 // Review SQL queries for security vulnerabilities
cmd.CommandText = $@"
SELECT key, metadata, timestamp, embedding
FROM {TableName}
WHERE collection=?
AND key IN ({keyPlaceholders});";
#pragma warning restore CA2100 // Review SQL queries for security vulnerabilities

cmd.Parameters.Add(new DuckDBParameter { Value = collectionName });
// Add the key parameters
foreach (var key in keys)
{
cmd.Parameters.Add(new DuckDBParameter { Value = key });
}

using var dataReader = await cmd.ExecuteReaderAsync(cancellationToken).ConfigureAwait(false);

while (await dataReader.ReadAsync(cancellationToken).ConfigureAwait(false))
{
string key = dataReader.GetFieldValue<string>("key");
string metadata = dataReader.GetFieldValue<string>("metadata");
string timestamp = dataReader.GetFieldValue<string>("timestamp");
var embeddingFromSearch = dataReader.GetFieldValue<List<float>>("embedding").ToArray();

yield return new DatabaseEntry
{
Key = key,
MetadataString = metadata,
Embedding = embeddingFromSearch,
Timestamp = timestamp
};
}
}

public async Task DeleteCollectionAsync(DuckDBConnection conn, string collectionName, CancellationToken cancellationToken = default)
{
using var cmd = conn.CreateCommand();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,9 @@ public async IAsyncEnumerable<string> UpsertBatchAsync(string collectionName, IE
public async IAsyncEnumerable<MemoryRecord> GetBatchAsync(string collectionName, IEnumerable<string> keys, bool withEmbeddings = false,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
foreach (var key in keys)
await foreach (var memoryRecord in this.InternalGetBatchAsync(this._dbConnection, collectionName, keys.ToArray(), withEmbeddings, cancellationToken).ConfigureAwait(false))
{
var result = await this.InternalGetAsync(this._dbConnection, collectionName, key, withEmbeddings, cancellationToken).ConfigureAwait(false);
if (result is not null)
{
yield return result;
}
else
{
yield break;
}
yield return memoryRecord;
}
}

Expand Down Expand Up @@ -320,5 +312,30 @@ await this._dbConnector.UpdateOrInsertAsync(conn: connection,
ParseTimestamp(entry.Value.Timestamp));
}

private async IAsyncEnumerable<MemoryRecord> InternalGetBatchAsync(
DuckDBConnection connection,
string collectionName,
string[] keys,
bool withEmbedding,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
await foreach (var entry in this._dbConnector.ReadAllAsync(connection, collectionName, keys, cancellationToken).ConfigureAwait(false))
{
var memoryRecord = withEmbedding
? MemoryRecord.FromJsonMetadata(
json: entry.MetadataString,
entry.Embedding,
entry.Key,
ParseTimestamp(entry.Timestamp))
: MemoryRecord.FromJsonMetadata(
json: entry.MetadataString,
ReadOnlyMemory<float>.Empty,
entry.Key,
ParseTimestamp(entry.Timestamp));

yield return memoryRecord;
}
}

#endregion
}
Loading