-
Notifications
You must be signed in to change notification settings - Fork 26
Add YdbList #514
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
Open
LiamHamsters
wants to merge
19
commits into
ydb-platform:main
Choose a base branch
from
LiamHamsters:add-ydb-list
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add YdbList #514
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
4c6d452
feat: YdbList + BulkUpsertImporter.AddListAsync
LiamHamsters 2573547
feat: YdbList struct-mode (List<Struct>)
LiamHamsters 726c00b
feat: make YdbList struct-only builder for List<Struct<...>>; remove …
LiamHamsters 6c0c5ff
fix lint
LiamHamsters efbd3db
refactor: switch YdbList.Struct to proto-first and update integration…
LiamHamsters 35118f6
fix autoformat
LiamHamsters aab0035
made an issue
LiamHamsters b77d5be
Simplify tests: move temp-table lifecycle to TestBase;
LiamHamsters ddcf397
Merge branch 'main' into add-ydb-list
LiamHamsters 18e08de
fix lint
LiamHamsters cfb8a81
Merge branch 'main' into add-ydb-list
LiamHamsters ffa7ec7
Rollback Simplify tests
LiamHamsters 0583155
Merge branch 'main' into add-ydb-list
LiamHamsters e6fa60f
Restore 3 test files to aab00359
LiamHamsters 4332b98
fix lint
LiamHamsters b9c64ec
fix: ConfigureAwait(false) in AddRowAsync
LiamHamsters 8cc8b73
fix(tests): await using for connections/readers
LiamHamsters f48288e
fix(tests): await using YdbDataReader to avoid hangs
LiamHamsters a0292e9
try fix
LiamHamsters 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
using Ydb.Sdk.Value; | ||
|
||
namespace Ydb.Sdk.Ado.BulkUpsert; | ||
|
||
public interface IBulkUpsertImporter | ||
{ | ||
/// <summary>Add a single row to the batch. Values must match the importer column order.</summary> | ||
/// <param name="row">Column values in the same order as the configured <c>columns</c>.</param> | ||
LiamHamsters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ValueTask AddRowAsync(params object[] row); | ||
|
||
/// <summary> | ||
/// Add many rows from <see cref="YdbList"/> (shape: <c>List<Struct<...>></c>). | ||
/// Struct member names and order must exactly match the configured <c>columns</c>. | ||
/// </summary> | ||
ValueTask AddListAsync(YdbList list); | ||
|
||
/// <summary>Flush the current batch via BulkUpsert (no-op if empty).</summary> | ||
ValueTask FlushAsync(); | ||
} |
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,114 @@ | ||
using Google.Protobuf.WellKnownTypes; | ||
using Ydb.Sdk.Ado; | ||
using Ydb.Sdk.Ado.YdbType; | ||
|
||
namespace Ydb.Sdk.Value; | ||
|
||
/// <summary> | ||
/// Struct-only builder for YDB <c>List<Struct<...>></c>. | ||
/// Define columns (optionally YDB types) and add positional rows; no external MakeStruct is needed. | ||
/// </summary> | ||
public sealed class YdbList | ||
{ | ||
private readonly string[] _columns; | ||
private readonly YdbDbType[]? _types; | ||
private readonly List<object?[]> _rows = new(); | ||
|
||
/// <summary>Create Struct-mode list with column names; types will be inferred from the first non-null per column.</summary> | ||
public static YdbList Struct(params string[] columns) => new(columns); | ||
|
||
/// <summary>Create Struct-mode list with column names and explicit YDB types (same length as columns).</summary> | ||
public static YdbList Struct(string[] columns, YdbDbType[] types) => new(columns, types); | ||
|
||
/// <summary>Constructs Struct-mode list. If <paramref name="types"/> is null, types are inferred per column.</summary> | ||
public YdbList(string[] columns, YdbDbType[]? types = null) | ||
LiamHamsters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (columns is null || columns.Length == 0) | ||
throw new ArgumentException("Columns must be non-empty.", nameof(columns)); | ||
if (types is not null && types.Length != columns.Length) | ||
throw new ArgumentException("Length of 'types' must match length of 'columns'.", nameof(types)); | ||
|
||
_columns = columns; | ||
_types = types; | ||
} | ||
|
||
/// <summary>Add one positional row. Value count must match the number of columns.</summary> | ||
public YdbList AddRow(params object?[] values) | ||
{ | ||
if (values.Length != _columns.Length) | ||
throw new ArgumentException($"Expected {_columns.Length} values, got {values.Length}."); | ||
_rows.Add(values); | ||
return this; | ||
} | ||
|
||
/// <summary>Convert to YDB <see cref="TypedValue"/> with shape <c>List<Struct<...>></c>.</summary> | ||
internal TypedValue ToTypedValue() | ||
{ | ||
if (_rows.Count == 0 && (_types is null || _types.All(t => t == YdbDbType.Unspecified))) | ||
throw new InvalidOperationException( | ||
"Cannot infer Struct schema from an empty list without explicit YdbDbType hints."); | ||
|
||
var memberTypes = new Type[_columns.Length]; | ||
for (var i = 0; i < _columns.Length; i++) | ||
{ | ||
if (_types is not null && _types[i] != YdbDbType.Unspecified) | ||
{ | ||
memberTypes[i] = new YdbParameter { YdbDbType = _types[i] }.TypedValue.Type; | ||
continue; | ||
} | ||
|
||
object? sample = null; | ||
foreach (var r in _rows) | ||
{ | ||
var v = r[i]; | ||
if (v is not null && v != DBNull.Value) | ||
{ | ||
sample = v; | ||
break; | ||
} | ||
} | ||
|
||
if (sample is null) | ||
throw new InvalidOperationException( | ||
$"Column '{_columns[i]}' has only nulls and no explicit YdbDbType. Provide a type hint."); | ||
|
||
memberTypes[i] = new YdbParameter { Value = sample }.TypedValue.Type; | ||
} | ||
|
||
var structType = new StructType | ||
{ | ||
Members = { _columns.Select((name, idx) => new StructMember { Name = name, Type = memberTypes[idx] }) } | ||
}; | ||
|
||
var ydbRows = new List<Ydb.Value>(_rows.Count); | ||
foreach (var r in _rows) | ||
{ | ||
var fields = new List<Ydb.Value>(_columns.Length); | ||
for (var i = 0; i < _columns.Length; i++) | ||
{ | ||
var v = r[i]; | ||
if (v is null || v == DBNull.Value) | ||
{ | ||
fields.Add(new Ydb.Value { NullFlagValue = NullValue.NullValue }); | ||
continue; | ||
} | ||
|
||
var tv = v switch | ||
{ | ||
YdbValue yv => yv.GetProto(), | ||
YdbParameter p => p.TypedValue, | ||
_ => new YdbParameter { Value = v }.TypedValue | ||
}; | ||
fields.Add(tv.Value); | ||
} | ||
|
||
ydbRows.Add(new Ydb.Value { Items = { fields } }); | ||
} | ||
|
||
return new TypedValue | ||
{ | ||
Type = new Type { ListType = new ListType { Item = new Type { StructType = structType } } }, | ||
Value = new Ydb.Value { Items = { ydbRows } } | ||
}; | ||
} | ||
} |
Oops, something went wrong.
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.