Skip to content

Commit 83377b6

Browse files
committed
Append nested list
1 parent bf5c1a6 commit 83377b6

File tree

5 files changed

+23
-30
lines changed

5 files changed

+23
-30
lines changed

DuckDB.NET.Data/DuckDBAppenderRow.cs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public DuckDBAppenderRow AppendValue(TimeSpan? value)
104104

105105
#region Composite Types
106106

107-
public DuckDBAppenderRow AppendValue<T>(IReadOnlyCollection<T>? value) => AppendCollectionValue(value);
107+
public DuckDBAppenderRow AppendValue<T>(IReadOnlyCollection<T>? value) => AppendValueInternal(value);
108108

109109
#endregion
110110

@@ -119,17 +119,6 @@ private DuckDBAppenderRow AppendValueInternal<T>(T? value)
119119
return this;
120120
}
121121

122-
private DuckDBAppenderRow AppendCollectionValue<T>(IReadOnlyCollection<T>? value)
123-
{
124-
CheckColumnAccess();
125-
126-
vectorWriters[columnIndex].AppendCollection(value, rowIndex);
127-
128-
columnIndex++;
129-
130-
return this;
131-
}
132-
133122
#if NET6_0_OR_GREATER
134123
private unsafe DuckDBAppenderRow AppendSpan(Span<byte> val)
135124
{

DuckDB.NET.Data/DuckDBDataReader.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,18 @@ private bool InitChunkData()
7676

7777
currentChunkRowCount = (ulong)NativeMethods.DataChunks.DuckDBDataChunkGetSize(currentChunk);
7878

79-
vectorReaders = new VectorDataReaderBase[fieldCount];
80-
79+
if (vectorReaders.Length != fieldCount)
80+
{
81+
vectorReaders = new VectorDataReaderBase[fieldCount];
82+
}
83+
8184
for (int index = 0; index < fieldCount; index++)
8285
{
8386
var vector = NativeMethods.DataChunks.DuckDBDataChunkGetVector(currentChunk, index);
8487

8588
using var logicalType = NativeMethods.Query.DuckDBColumnLogicalType(ref currentResult, index);
8689

87-
vectorReaders[index] = VectorDataReaderFactory.CreateReader(vector, logicalType,
90+
vectorReaders[index] = VectorDataReaderFactory.CreateReader(vector, logicalType, vectorReaders[index]?.ColumnName ??
8891
NativeMethods.Query.DuckDBColumnName(ref currentResult, index).ToManagedString(false));
8992
}
9093

DuckDB.NET.Data/Internal/Writer/ListVectorDataWriter.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using DuckDB.NET.Native;
45

@@ -16,21 +17,19 @@ public ListVectorDataWriter(IntPtr vector, void* vectorData, DuckDBType columnTy
1617
listDataWriter = VectorDataWriterFactory.CreateWriter(childVector, childType);
1718
}
1819

19-
internal override bool AppendCollection<T>(IReadOnlyCollection<T>? value, int rowIndex)
20+
internal override bool AppendCollection(IList value, int rowIndex)
2021
{
21-
if (value == null)
22-
{
23-
AppendNull(rowIndex);
24-
return true;
25-
}
26-
2722
var index = 0;
2823

2924
foreach (var item in value)
3025
{
31-
listDataWriter.AppendValue(item, index++);
26+
listDataWriter.AppendValue(item, (int)offset + (index++));
3227
}
3328

34-
return AppendValueInternal(new DuckDBListEntry(offset, (ulong)value.Count), rowIndex);
29+
var result = AppendValueInternal(new DuckDBListEntry(offset, (ulong)value.Count), rowIndex);
30+
31+
offset += (ulong)value.Count;
32+
33+
return result;
3534
}
3635
}

DuckDB.NET.Data/Internal/Writer/VectorDataWriterBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public unsafe void AppendValue<T>(T value, int rowIndex)
6161
TimeOnly val => AppendTimeOnly(val, rowIndex),
6262
#endif
6363
DateTimeOffset val => AppendDateTimeOffset(val, rowIndex),
64+
IList val => AppendCollection(val, rowIndex),
6465
_ => ThrowException<T>()
6566
};
6667
}
@@ -95,7 +96,7 @@ public unsafe void AppendValue<T>(T value, int rowIndex)
9596

9697
internal virtual bool AppendBigInteger(BigInteger value, int rowIndex) => ThrowException<BigInteger>();
9798

98-
internal virtual bool AppendCollection<T>(IReadOnlyCollection<T>? value, int rowIndex) => ThrowException<bool>();
99+
internal virtual bool AppendCollection(IList value, int rowIndex) => ThrowException<bool>();
99100

100101
private bool ThrowException<T>()
101102
{

DuckDB.NET.Test/ManagedAppenderTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,19 +272,19 @@ public void TemporalValues()
272272
public void ListValues()
273273
{
274274
Command.CommandText = "CREATE TABLE managedAppenderLists(a INTEGER, b INTEGER[]" +
275-
//", c INTEGER[][]" +
275+
", c INTEGER[][]" +
276276
");";
277277
Command.ExecuteNonQuery();
278278

279-
var rows = 20;
279+
var rows = 2;
280280
using (var appender = Connection.CreateAppender("managedAppenderLists"))
281281
{
282282
for (int i = 0; i < rows; i++)
283283
{
284284
appender.CreateRow()
285285
.AppendValue(i)
286-
.AppendValue(Enumerable.Range(0, i).ToList())
287-
//.AppendValue(new List<List<int>>{ Enumerable.Range(0, 5).ToList() })
286+
.AppendValue(Enumerable.Range(0, 2).ToList())
287+
.AppendValue(new List<List<int>> { Enumerable.Range(0, 5).ToList(), Enumerable.Range(i + 2, 4).ToList() })
288288
.EndRow();
289289
}
290290
}
@@ -296,7 +296,8 @@ public void ListValues()
296296
while (reader.Read())
297297
{
298298
var ints = reader.GetFieldValue<List<int>>(1);
299-
ints.Should().BeEquivalentTo(Enumerable.Range(0, index++).ToList());
299+
ints.Should().BeEquivalentTo(Enumerable.Range(0, 2).ToList());
300+
var fieldValue = reader.GetFieldValue<List<List<int>>>(2);
300301
}
301302
}
302303

0 commit comments

Comments
 (0)