Giorgi / DuckDB.NET

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

Queries with Decimal type fail ("DuckDBQuery failed") #22

Closed siganakis closed 2 years ago

siganakis commented 2 years ago

Description

When querying tables with a decimal data type, duckdb.net returns an error "DuckDBQuery failed". This occurs where the decimal values in the table are NULL or has a value.

Expected result

The query should return the decimal values stored in the table.

Minimal example:

using System;
using System.Data.Common;
using DuckDB.NET.Data;
namespace DuckDbDecimalIssue
{
    internal class Program
    {
        static void Main(string[] args)
        {
            // Using duckdb.dll from:
            //  https://github.com/duckdb/duckdb/releases/download/v0.3.1/libduckdb-windows-amd64.zip
            var connectionString = "Data Source=db.duckdb;";
            using (var cn = new DuckDBConnection(connectionString))
            {
                cn.Open();
                Execute(cn, @"DROP TABLE IF EXISTS test;");
                Execute(cn, @"CREATE TABLE test (dec DECIMAL(18, 4) NULL, value FLOAT NULL);");
                Execute(cn, @"INSERT INTO test (dec, value) SELECT NULL, 10");

                var query = "SELECT * FROM test";
                using (var reader = ExecuteReader(cn, query))
                {
                    while (reader.Read())
                    {
                        Console.WriteLine($"{reader[0]}\t{reader[1]}");
                    }
                }

            }
        }

        static void Execute(DuckDBConnection cn, string sql)
        {
            var createTableCommand = cn.CreateCommand();
            createTableCommand.CommandText = sql;
            createTableCommand.ExecuteNonQuery();
        }

        static DbDataReader ExecuteReader(DuckDBConnection cn, string sql)
        {
            var createTableCommand = cn.CreateCommand();
            createTableCommand.CommandText = sql;
            return createTableCommand.ExecuteReader();
        }
    }
}

If you change the column type of "dec" above to INT, then the code works properly.

siganakis commented 2 years ago

I should also note the executing the queries above in the CLI works as expected:

λ duckdb "db.duckdb"
v0.3.1 88aa81c6b
Enter ".help" for usage hints.
D DROP TABLE IF EXISTS test;
D CREATE TABLE test (dec DECIMAL(19,14) NULL, value FLOAT NULL);
D INSERT INTO test (dec, value) SELECT NULL, 10;
D SELECT * FROM test;
┌─────┬───────┐
│ dec │ value │
├─────┼───────┤
│     │ 10.0  │
└─────┴───────┘
Giorgi commented 2 years ago

Filed a bug: https://github.com/duckdb/duckdb/issues/2858

Giorgi commented 2 years ago

This is now implemented in release 0.4.0