Giorgi / DuckDB.NET

Bindings and ADO.NET Provider for DuckDB
https://duckdb.net
MIT License
338 stars 61 forks source link

DuckDBConnection.CreateAppender not dealing correctly when table with special characters #162

Closed jhudler closed 6 months ago

jhudler commented 6 months ago

Hello,

First of all, thank you for this library. Very simple to use and straightforward!


The Issue

The following code will fail when trying to create an appender:

using var conn = new DuckDB.NET.Data.DuckDBConnection("Data Source=:memory:");
conn.Open();
var specialTableName = "SPÉçÏÃL - TÁBLÈ_";
using (var cmdCreateTable = conn.CreateCommand())
{
    cmdCreateTable.CommandText = @$"CREATE TABLE ""{specialTableName}"" (SomeField TEXT)";
    cmdCreateTable.ExecuteNonQuery();
}

try
{
    using (var appender = conn.CreateAppender(specialTableName))
    {

    };    
    Console.WriteLine("Passed!");
}
catch (Exception e)
{
    Console.Error.WriteLine($"Failed to retrieve schema: {e.Message}");
}

Stack Trace:

Failed to retrieve schema: DuckDB.NET.Data.DuckDBException (0x80004005): Catalog Error: Table "main.SP????L - T?BL?_" could not be found
   at DuckDB.NET.Data.DuckDBAppender.ThrowLastError(DuckDBAppender appender)
   at DuckDB.NET.Data.DuckDBConnection.CreateAppender(String schema, String table)
   at DuckDB.NET.Data.DuckDBConnection.CreateAppender(String table)

It seems that the CreateAppender is not correctly converting the string to the correct encoding before calling the native API.

If you have any guidance on this, I can provide a PR.

Giorgi commented 6 months ago

In the CreateAppender method the schema and table need to be converted to utf8 with Utils.ToUnmanagedString

jhudler commented 6 months ago

Apparently easier than I thought. Just followed the pattern used in 3aea4640d9ea761ea6aab457a840bc583e440ddf. Made the PR #163 - hope is fine.