Skip to content
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b6bf0d5
Add BulkUpsert implementation
LiamHamsters Jul 23, 2025
261d342
hot fix
LiamHamsters Jul 23, 2025
8173dd0
hot fix 2
LiamHamsters Jul 23, 2025
8d170b8
fix issues
LiamHamsters Jul 24, 2025
5ac5127
feat: make BulkUpsert API more user-friendly
LiamHamsters Jul 24, 2025
3db706d
Implement simple BulkUpsert in YdbConnection and YdbDataSource (delet…
LiamHamsters Jul 25, 2025
a2aa86b
refactor: use array-based rows for BulkUpsert, remove dictionary
LiamHamsters Jul 25, 2025
bff5137
fix: format
LiamHamsters Jul 25, 2025
924da51
last hot fix
LiamHamsters Jul 25, 2025
5e52cc1
fix issues
LiamHamsters Jul 25, 2025
3c219ef
rebase + SessionImpl -> Session
LiamHamsters Jul 25, 2025
4af5c80
fast fix
LiamHamsters Jul 25, 2025
18060cd
BulkUpsert: forbid usage inside transaction, add test
LiamHamsters Jul 25, 2025
f207b69
fix + edit CHANGELOG.md
LiamHamsters Jul 25, 2025
816dc9c
add BulkUpsertProtoImporter
LiamHamsters Jul 29, 2025
7ffa2ee
hot fix
LiamHamsters Jul 29, 2025
1879bca
bulk upsert: add object[] support, simplify implementation
LiamHamsters Jul 29, 2025
14a2bfe
fix: handle exceptions in BulkUpsertImporter DisposeAsync, update tests
LiamHamsters Jul 29, 2025
5cee465
fix
LiamHamsters Jul 30, 2025
b203781
hot fix
LiamHamsters Jul 30, 2025
eec3d12
remove Type argument
LiamHamsters Jul 30, 2025
8c32732
hot fix
LiamHamsters Jul 30, 2025
c9fdbb5
fix inspections and autoformat
LiamHamsters Jul 30, 2025
6463d36
feat: switch BulkUpsertImporter to plain proto, remove extra wrappers…
LiamHamsters Jul 31, 2025
bfc6cf5
refactor: correct batch bytes check and flush logic in BulkUpsertImpo…
LiamHamsters Jul 31, 2025
1c28862
fix
LiamHamsters Jul 31, 2025
0bb791c
last fix
LiamHamsters Jul 31, 2025
beec0e0
Finally
LiamHamsters Jul 31, 2025
b08ebd5
change ct in Flush
LiamHamsters Jul 31, 2025
96fef4a
edit: tablePath calculation in YdbConnection
LiamHamsters Aug 1, 2025
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
1 change: 1 addition & 0 deletions src/Ydb.Sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- ADO.NET: Added `BulkUpsertAsync` for batch upsert operations with transaction checks and integration tests.
- Optimization: On BadSession, do not invoke the `DeleteSession()` method.
- Canceling AttachStream after calling the `DeleteSession` method.
- Fixed bug: fixed issue where session was not deleted (`ClientTransportTimeout`).
Expand Down
7 changes: 7 additions & 0 deletions src/Ydb.Sdk/src/Ado/BulkUpsert/BulkUpsertFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Ydb.Sdk.Ado.BulkUpsert;

public enum BulkUpsertFormat
{
Proto = 0
// Arrow = 1
}
7 changes: 7 additions & 0 deletions src/Ydb.Sdk/src/Ado/BulkUpsert/IBulkUpsertImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Ydb.Sdk.Ado.BulkUpsert;

public interface IBulkUpsertImporter : IAsyncDisposable
{
ValueTask AddRowAsync(params Ydb.Value[] values);
ValueTask FlushAsync(CancellationToken cancellationToken = default);
}
116 changes: 116 additions & 0 deletions src/Ydb.Sdk/src/Ado/BulkUpsert/YdbBulkUpsertProtoImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using Ydb.Sdk.Value;

namespace Ydb.Sdk.Ado.BulkUpsert;

public sealed class YdbBulkUpsertProtoImporter : IAsyncDisposable
{
private readonly YdbConnection _connection;
private readonly string _tablePath;
private readonly List<string> _columns;
private readonly List<Type> _types;
private readonly int _maxBytes;

private readonly List<Ydb.Value> _rows = new();
private bool _disposed;

public YdbBulkUpsertProtoImporter(
YdbConnection connection,
string tablePath,
IReadOnlyList<string> columns,
IReadOnlyList<Type> types,
int maxBytes = 1024 * 1024)
{
_connection = connection;
_tablePath = tablePath;
_columns = columns is List<string> colList ? colList : new List<string>(columns);
_types = types is List<Type> typList ? typList : new List<Type>(types);
_maxBytes = maxBytes;
}

public async ValueTask AddRowAsync(params YdbValue[] values)
{
ThrowIfDisposed();

if (values.Length != _columns.Count)
throw new ArgumentException("Values count must match columns count", nameof(values));

var dict = _columns.Zip(values, (name, value) => new KeyValuePair<string, YdbValue>(name, value))
.ToDictionary(x => x.Key, x => x.Value);

var structValue = YdbValue.MakeStruct(dict).GetProto().Value;
_rows.Add(structValue);

var totalSize = _rows.Sum(r => r.CalculateSize());

if (totalSize >= _maxBytes)
await FlushAsync();
}

public async ValueTask AddRowsAsync(IEnumerable<YdbValue[]> rows, CancellationToken cancellationToken = default)
{
ThrowIfDisposed();

foreach (var values in rows)
{
if (values.Length != _columns.Count)
throw new ArgumentException("Values count must match columns count", nameof(values));

var dict = _columns.Zip(values, (name, value) => new KeyValuePair<string, YdbValue>(name, value))
.ToDictionary(x => x.Key, x => x.Value);

var structValue = YdbValue.MakeStruct(dict).GetProto().Value;
_rows.Add(structValue);

var totalSize = _rows.Sum(r => r.CalculateSize());

if (totalSize >= _maxBytes)
await FlushAsync(cancellationToken);
}
}

public async ValueTask DisposeAsync()
{
if (_disposed) return;
await FlushAsync();
_disposed = true;
}

public async ValueTask FlushAsync(CancellationToken cancellationToken = default)
{
ThrowIfDisposed();

if (_rows.Count == 0)
return;

await _connection.BulkUpsertProtoAsync(
_tablePath,
GetStructType(),
new List<Ydb.Value>(_rows),
cancellationToken);

_rows.Clear();
}

private Type GetStructType()
{
var structType = new Type { StructType = new StructType() };
for (var i = 0; i < _columns.Count; i++)
{
structType.StructType.Members.Add(new StructMember
{
Name = _columns[i],
Type = _types[i]
});
}

return structType;
}

private void ThrowIfDisposed()
{
if (_disposed)
throw new ObjectDisposedException(nameof(YdbBulkUpsertProtoImporter));
}

public IReadOnlyList<Ydb.Value> GetBufferedRows() => _rows;
}
90 changes: 90 additions & 0 deletions src/Ydb.Sdk/src/Ado/YdbConnection.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
using System.Data;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using Ydb.Sdk.Ado.Internal;
using Ydb.Sdk.Ado.Session;
using Ydb.Sdk.Services.Query;
using Ydb.Sdk.Value;
using Ydb.Table;
using Ydb.Table.V1;
using static System.Data.IsolationLevel;

namespace Ydb.Sdk.Ado;
Expand Down Expand Up @@ -52,6 +56,92 @@ public YdbConnection(YdbConnectionStringBuilder connectionStringBuilder)
ConnectionStringBuilder = connectionStringBuilder;
}

public async Task BulkUpsertAsync(
string tablePath,
IReadOnlyList<string> columns,
IReadOnlyList<IReadOnlyList<object?>> rows,
CancellationToken cancellationToken = default)
{
if (CurrentTransaction is { Completed: false })
throw new InvalidOperationException("BulkUpsert cannot be used inside an active transaction.");

if (columns == null || columns.Count == 0)
throw new ArgumentException("Columns must not be empty", nameof(columns));
if (rows == null || rows.Count == 0)
throw new ArgumentException("Rows collection is empty", nameof(rows));

var structs = rows.Select(row =>
{
if (row.Count != columns.Count)
throw new ArgumentException("Each row must have the same number of elements as columns");
var members = columns
.Select((col, i) =>
new KeyValuePair<string, YdbValue>(col, new YdbParameter { Value = row[i] }.YdbValue))
.ToDictionary(x => x.Key, x => x.Value);
return YdbValue.MakeStruct(members);
}).ToList();

var list = YdbValue.MakeList(structs);

var req = new BulkUpsertRequest
{
Table = tablePath,
Rows = list.GetProto()
};

var resp = await Session.Driver.UnaryCall(
TableService.BulkUpsertMethod,
req,
new GrpcRequestSettings { CancellationToken = cancellationToken }
).ConfigureAwait(false);

var operation = resp.Operation;
if (operation.Status.IsNotSuccess())
{
throw YdbException.FromServer(operation.Status, operation.Issues);
}
}

public async Task BulkUpsertProtoAsync(
string tablePath,
Type structType,
IReadOnlyList<Ydb.Value> chunk,
CancellationToken cancellationToken = default,
int retryCount = 3)
{
if (CurrentTransaction is { Completed: false })
throw new InvalidOperationException("BulkUpsertProto cannot be used inside an active transaction.");

var listValue = new Ydb.Value();
listValue.Items.AddRange(chunk);

var typedValue = new TypedValue { Type = structType, Value = listValue };
var req = new BulkUpsertRequest { Table = tablePath, Rows = typedValue };

var attempt = 0;
while (true)
{
try
{
var resp = await Session.Driver.UnaryCall(
TableService.BulkUpsertMethod,
req,
new GrpcRequestSettings { CancellationToken = cancellationToken }
).ConfigureAwait(false);

var operation = resp.Operation;
if (operation.Status.IsNotSuccess())
throw YdbException.FromServer(operation.Status, operation.Issues);
return;
}
catch (Exception) when (attempt < retryCount)
{
attempt++;
await Task.Delay(100 * attempt, cancellationToken);
}
}
}

protected override YdbTransaction BeginDbTransaction(IsolationLevel isolationLevel)
{
ThrowIfConnectionClosed();
Expand Down
10 changes: 10 additions & 0 deletions src/Ydb.Sdk/src/Ado/YdbDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ protected override async ValueTask DisposeAsyncCore() =>
await PoolManager.ClearPool(_ydbConnectionStringBuilder.ConnectionString);

protected override void Dispose(bool disposing) => DisposeAsyncCore().AsTask().GetAwaiter().GetResult();

public async Task BulkUpsertAsync(
string tablePath,
IReadOnlyList<string> columns,
IReadOnlyList<IReadOnlyList<object?>> rows,
CancellationToken cancellationToken = default)
{
await using var conn = await OpenConnectionAsync(cancellationToken);
await conn.BulkUpsertAsync(tablePath, columns, rows, cancellationToken);
}
}

#endif
Loading
Loading