Skip to content

Commit

Permalink
Initialize writers only once
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed Jun 14, 2024
1 parent bae8291 commit c6cc733
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
20 changes: 8 additions & 12 deletions DuckDB.NET.Data/DuckDBAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ internal unsafe DuckDBAppender(Native.DuckDBAppender appender, string qualifiedT
}

dataChunk = NativeMethods.DataChunks.DuckDBCreateDataChunk(logicalTypeHandles, columnCount);

for (long index = 0; index < vectorWriters.LongLength; index++)
{
var vector = NativeMethods.DataChunks.DuckDBDataChunkGetVector(dataChunk, index);

vectorWriters[index] = VectorDataWriterFactory.CreateWriter(vector, logicalTypes[index]);
}
}

public DuckDBAppenderRow CreateRow()
Expand All @@ -48,11 +55,10 @@ public DuckDBAppenderRow CreateRow()
throw new InvalidOperationException("Appender is already closed");
}

if (rowCount % DuckDBVectorSize == 0)
if (rowCount == DuckDBVectorSize)
{
AppendDataChunk();

InitVectorWriters();
rowCount = 0;
}

Expand Down Expand Up @@ -95,16 +101,6 @@ public void Dispose()
}
}

private unsafe void InitVectorWriters()
{
for (long index = 0; index < vectorWriters.LongLength; index++)
{
var vector = NativeMethods.DataChunks.DuckDBDataChunkGetVector(dataChunk, index);

vectorWriters[index] = VectorDataWriterFactory.CreateWriter(vector, logicalTypes[index]);
}
}

private void AppendDataChunk()
{
NativeMethods.DataChunks.DuckDBDataChunkSetSize(dataChunk, rowCount);
Expand Down
2 changes: 1 addition & 1 deletion DuckDB.NET.Test/DuckDBManagedAppenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ public void GuidValues()
Command.CommandText = "CREATE TABLE managedAppenderGuids(a UUID);";
Command.ExecuteNonQuery();

var guids = GetRandomList<Guid?>(faker => faker.Random.Guid());
var guids = GetRandomList<Guid?>(faker => faker.Random.Guid(), 5000);
guids.Add(null);

using (var appender = Connection.CreateAppender("managedAppenderGuids"))
Expand Down

0 comments on commit c6cc733

Please sign in to comment.