Skip to content

Commit

Permalink
Fix AccessViolation in appender
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed Jul 9, 2024
1 parent efea40d commit c074b18
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
28 changes: 20 additions & 8 deletions DuckDB.NET.Data/DuckDBAppender.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ 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 @@ -55,10 +48,12 @@ public DuckDBAppenderRow CreateRow()
throw new InvalidOperationException("Appender is already closed");
}

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

InitVectorWriters();

rowCount = 0;
}

Expand Down Expand Up @@ -101,6 +96,23 @@ public void Dispose()
}
}

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

if (vectorWriters[index] == null)
{
vectorWriters[index] = VectorDataWriterFactory.CreateWriter(vector, logicalTypes[index]);
}
else
{
vectorWriters[index].InitializerWriter();
}
}
}

private void AppendDataChunk()
{
NativeMethods.DataChunks.DuckDBDataChunkSetSize(dataChunk, rowCount);
Expand Down
35 changes: 34 additions & 1 deletion DuckDB.NET.Test/DuckDBManagedAppenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.IO;
using System.Linq;
using System.Numerics;
using Bogus;
using Xunit;

namespace DuckDB.NET.Test;
Expand Down Expand Up @@ -406,7 +407,6 @@ public void TableWithSchema()
Command.CommandText = "SELECT * FROM managedAppenderTestSchema.managedAppenderTest";
using (var reader = Command.ExecuteReader())
{

var readRowIndex = 0;
while (reader.Read())
{
Expand All @@ -426,6 +426,39 @@ public void TableWithSchema()
}
}

[Fact]
public void ManagedAppenderOnTableAndColumns()
{
var table = "CREATE TABLE managedAppenderTest2(a INTEGER, b VARCHAR, c DateTime);";
Command.CommandText = table;
Command.ExecuteNonQuery();

var rows = 5000;
var date = DateTime.Today;

var categories = Enumerable.Range(0, rows)
.Select(i => new { id = i, name = Faker.Lorem.Word().OrNull(Faker), date = date.AddDays(i) })
.ToList();

using (var appender = Connection.CreateAppender("managedAppenderTest2"))
{
foreach (var item in categories)
{
var row = appender.CreateRow();
row
.AppendValue(item.id)
.AppendValue(item.name)
.AppendValue(item.date)
.EndRow();
}
}

var list = Connection.Query<(int id, string name, DateTime date)>("SELECT a, b, c FROM managedAppenderTest2").Select(tuple => new { tuple.id, tuple.name, tuple.date}).ToList();

list.Should().HaveCount(rows);
list.Should().BeEquivalentTo(categories);
}

[Theory]
[InlineData("")]
[InlineData("MY # SÇHËMÁ")]
Expand Down
2 changes: 1 addition & 1 deletion DuckDB.NET.Test/Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ItemGroup>
<PackageReference Include="Bogus" Version="35.5.1" />
<PackageReference Include="coverlet.msbuild" Version="6.0.0" PrivateAssets="all" />
<PackageReference Include="Dapper" Version="2.1.24" />
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="FluentAssertions" Version="6.8.0" />
<PackageReference Include="coverlet.collector" Version="6.0.0" PrivateAssets="all" />
<PackageReference Include="GitHubActionsTestLogger" Version="2.3.3" PrivateAssets="all" />
Expand Down

0 comments on commit c074b18

Please sign in to comment.