-
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 6 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.
Outdated
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,150 @@ | ||
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>. | ||
/// Works directly with protobuf: | ||
/// - Each call to <see cref="AddRow(object?[])"/> converts values into protobuf cells (<see cref="Ydb.Value"/>) and stores a row immediately. | ||
/// - The struct schema (<see cref="StructType"/>) is derived from column type hints or from the first non-null sample per column. | ||
/// - If a column has at least one <c>null</c>, its type becomes <c>Optional<T></c>; individual null cells are encoded via <see cref="NullValue.NullValue"/>. | ||
/// </summary> | ||
public sealed class YdbList | ||
{ | ||
private readonly string[] _columns; | ||
private readonly YdbDbType[]? _typeHints; | ||
|
||
private readonly List<Ydb.Value> _rows = new(); | ||
|
||
private readonly Type?[] _observedBaseTypes; | ||
private readonly bool[] _sawNull; | ||
|
||
/// <summary> | ||
/// Create a struct-mode list with column names; types will be inferred from the | ||
/// first non-null value per column (columns with any nulls become Optional<T>). | ||
/// </summary> | ||
public static YdbList Struct(params string[] columns) => new(columns); | ||
|
||
/// <summary> | ||
/// Create a struct-mode list with column names and explicit YDB type hints | ||
/// (array length must match <paramref name="columns"/>). Columns with any nulls | ||
/// will be emitted as Optional<hintedType>. | ||
/// </summary> | ||
public static YdbList Struct(string[] columns, YdbDbType[] types) => new(columns, types); | ||
|
||
/// <summary> | ||
/// Construct a struct-mode list. If <paramref name="types"/> is null, schema is inferred from values. | ||
/// </summary> | ||
private YdbList(string[] columns, YdbDbType[]? types = null) | ||
{ | ||
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; | ||
_typeHints = types; | ||
_observedBaseTypes = new Type[_columns.Length]; | ||
_sawNull = new bool[_columns.Length]; | ||
} | ||
|
||
/// <summary> | ||
/// Add a single positional row. The number of values must match the number of columns. | ||
/// Values are converted to protobuf cells and the row is stored immediately. | ||
/// </summary> | ||
public YdbList AddRow(params object?[] values) | ||
{ | ||
if (values.Length != _columns.Length) | ||
throw new ArgumentException($"Expected {_columns.Length} values, got {values.Length}."); | ||
|
||
var cells = new List<Ydb.Value>(_columns.Length); | ||
|
||
for (var i = 0; i < _columns.Length; i++) | ||
{ | ||
var v = values[i]; | ||
|
||
if (v is null || v == DBNull.Value) | ||
{ | ||
_sawNull[i] = true; | ||
cells.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 | ||
}; | ||
|
||
var t = tv.Type; | ||
if (t.TypeCase == Type.TypeOneofCase.OptionalType && t.OptionalType is not null) | ||
t = t.OptionalType.Item; | ||
|
||
_observedBaseTypes[i] ??= t; | ||
cells.Add(tv.Value); | ||
} | ||
|
||
_rows.Add(new Ydb.Value { Items = { cells } }); | ||
return this; | ||
} | ||
|
||
/// <summary> | ||
/// Convert to a YDB <see cref="TypedValue"/> shaped as <c>List<Struct<...>></c>. | ||
/// Columns that observed <c>null</c> values are emitted as <c>Optional<T></c>; | ||
/// individual <c>null</c> cells are encoded via <see cref="NullValue.NullValue"/>. | ||
/// </summary> | ||
internal TypedValue ToTypedValue() | ||
{ | ||
if (_rows.Count == 0 && (_typeHints is null || _typeHints.All(t => t == YdbDbType.Unspecified))) | ||
throw new InvalidOperationException( | ||
"Cannot infer Struct schema from an empty list without explicit YdbDbType hints."); | ||
LiamHamsters marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
var n = _columns.Length; | ||
var memberTypes = new Type[n]; | ||
|
||
for (var i = 0; i < n; i++) | ||
{ | ||
Type? baseType; | ||
|
||
if (_typeHints is not null && _typeHints[i] != YdbDbType.Unspecified) | ||
{ | ||
baseType = new YdbParameter { YdbDbType = _typeHints[i], Value = DBNull.Value }.TypedValue.Type; | ||
|
||
if (baseType.TypeCase == Type.TypeOneofCase.OptionalType && baseType.OptionalType is not null) | ||
baseType = baseType.OptionalType.Item; | ||
} | ||
else | ||
{ | ||
baseType = _observedBaseTypes[i]; | ||
if (baseType is null) | ||
throw new InvalidOperationException( | ||
$"Column '{_columns[i]}' has only nulls and no explicit YdbDbType. Provide a type hint."); | ||
} | ||
LiamHamsters marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
memberTypes[i] = _sawNull[i] && baseType.TypeCase != Type.TypeOneofCase.OptionalType | ||
? new Type { OptionalType = new OptionalType { Item = baseType } } | ||
: baseType; | ||
LiamHamsters marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
var structType = new StructType | ||
{ | ||
Members = | ||
{ | ||
_columns.Select((name, idx) => new StructMember | ||
{ | ||
Name = name, | ||
Type = memberTypes[idx] | ||
}) | ||
} | ||
}; | ||
|
||
return new TypedValue | ||
{ | ||
Type = new Type { ListType = new ListType { Item = new Type { StructType = structType } } }, | ||
Value = new Ydb.Value { Items = { _rows } } | ||
}; | ||
} | ||
} |
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.