Skip to content

Commit

Permalink
Append nested list
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed May 16, 2024
1 parent bf5c1a6 commit 83377b6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 30 deletions.
13 changes: 1 addition & 12 deletions DuckDB.NET.Data/DuckDBAppenderRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public DuckDBAppenderRow AppendValue(TimeSpan? value)

#region Composite Types

public DuckDBAppenderRow AppendValue<T>(IReadOnlyCollection<T>? value) => AppendCollectionValue(value);
public DuckDBAppenderRow AppendValue<T>(IReadOnlyCollection<T>? value) => AppendValueInternal(value);

#endregion

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

private DuckDBAppenderRow AppendCollectionValue<T>(IReadOnlyCollection<T>? value)
{
CheckColumnAccess();

vectorWriters[columnIndex].AppendCollection(value, rowIndex);

columnIndex++;

return this;
}

#if NET6_0_OR_GREATER
private unsafe DuckDBAppenderRow AppendSpan(Span<byte> val)
{
Expand Down
9 changes: 6 additions & 3 deletions DuckDB.NET.Data/DuckDBDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,18 @@ private bool InitChunkData()

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

vectorReaders = new VectorDataReaderBase[fieldCount];

if (vectorReaders.Length != fieldCount)
{
vectorReaders = new VectorDataReaderBase[fieldCount];
}

for (int index = 0; index < fieldCount; index++)
{
var vector = NativeMethods.DataChunks.DuckDBDataChunkGetVector(currentChunk, index);

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

vectorReaders[index] = VectorDataReaderFactory.CreateReader(vector, logicalType,
vectorReaders[index] = VectorDataReaderFactory.CreateReader(vector, logicalType, vectorReaders[index]?.ColumnName ??
NativeMethods.Query.DuckDBColumnName(ref currentResult, index).ToManagedString(false));
}

Expand Down
17 changes: 8 additions & 9 deletions DuckDB.NET.Data/Internal/Writer/ListVectorDataWriter.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections;
using System.Collections.Generic;
using DuckDB.NET.Native;

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

internal override bool AppendCollection<T>(IReadOnlyCollection<T>? value, int rowIndex)
internal override bool AppendCollection(IList value, int rowIndex)
{
if (value == null)
{
AppendNull(rowIndex);
return true;
}

var index = 0;

foreach (var item in value)
{
listDataWriter.AppendValue(item, index++);
listDataWriter.AppendValue(item, (int)offset + (index++));
}

return AppendValueInternal(new DuckDBListEntry(offset, (ulong)value.Count), rowIndex);
var result = AppendValueInternal(new DuckDBListEntry(offset, (ulong)value.Count), rowIndex);

offset += (ulong)value.Count;

return result;
}
}
3 changes: 2 additions & 1 deletion DuckDB.NET.Data/Internal/Writer/VectorDataWriterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public unsafe void AppendValue<T>(T value, int rowIndex)
TimeOnly val => AppendTimeOnly(val, rowIndex),
#endif
DateTimeOffset val => AppendDateTimeOffset(val, rowIndex),
IList val => AppendCollection(val, rowIndex),
_ => ThrowException<T>()
};
}
Expand Down Expand Up @@ -95,7 +96,7 @@ public unsafe void AppendValue<T>(T value, int rowIndex)

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

internal virtual bool AppendCollection<T>(IReadOnlyCollection<T>? value, int rowIndex) => ThrowException<bool>();
internal virtual bool AppendCollection(IList value, int rowIndex) => ThrowException<bool>();

private bool ThrowException<T>()
{
Expand Down
11 changes: 6 additions & 5 deletions DuckDB.NET.Test/ManagedAppenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,19 +272,19 @@ public void TemporalValues()
public void ListValues()
{
Command.CommandText = "CREATE TABLE managedAppenderLists(a INTEGER, b INTEGER[]" +
//", c INTEGER[][]" +
", c INTEGER[][]" +
");";
Command.ExecuteNonQuery();

var rows = 20;
var rows = 2;
using (var appender = Connection.CreateAppender("managedAppenderLists"))
{
for (int i = 0; i < rows; i++)
{
appender.CreateRow()
.AppendValue(i)
.AppendValue(Enumerable.Range(0, i).ToList())
//.AppendValue(new List<List<int>>{ Enumerable.Range(0, 5).ToList() })
.AppendValue(Enumerable.Range(0, 2).ToList())
.AppendValue(new List<List<int>> { Enumerable.Range(0, 5).ToList(), Enumerable.Range(i + 2, 4).ToList() })
.EndRow();
}
}
Expand All @@ -296,7 +296,8 @@ public void ListValues()
while (reader.Read())
{
var ints = reader.GetFieldValue<List<int>>(1);
ints.Should().BeEquivalentTo(Enumerable.Range(0, index++).ToList());
ints.Should().BeEquivalentTo(Enumerable.Range(0, 2).ToList());
var fieldValue = reader.GetFieldValue<List<List<int>>>(2);
}
}

Expand Down

0 comments on commit 83377b6

Please sign in to comment.