Giorgi / DuckDB.NET

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

GetInt64 reads an int128 column incorrectly #142

Closed DanCory closed 11 months ago

DanCory commented 12 months ago

I'm not sure if you will consider this a bug, but it certainly confused me. SUM() of an int16 field in DuckDB returns an int128. I tried to read it with GetInt64 and got the wrong results

using DuckDB.NET.Data;
using DuckDBConnection duckDBConnection = new("Data Source=test.db");
duckDBConnection.Open();
DuckDbCommand command = duckDBConnection.CreateCommand();
command.CommandText = "DROP TABLE IF EXISTS intint;";
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE intint(foo INTEGER, bar INTEGER);";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO intint VALUES (3, 4), (5, 4), (7, 8),(9,8),(11,12),(13,14);";
command.ExecuteNonQuery();
command.CommandText = "SELECT sum(foo) FROM intint group by bar";
DuckDBDataReader reader = command.ExecuteReader();
while (reader.Read())
{
  for (int c = 0; c < reader.FieldCount; c++)
  {
    Console.Write(reader.GetInt64(c));
  }
  Console.WriteLine();
}
reader.Close();

should return

8
16
11
13

returns

8
0
16
0

Using GetValue is correct

Giorgi commented 11 months ago

I tested Microsoft.Data.SqlClient, Microsoft.Data.Sqlite, and Npgsql and only Microsoft.Data.SqlClient requires GetXXX methods to match the underlying column type. The other two allow GetInt32 to work for other numeric types and vice versa (as long as data fits in the type)

I created a new issue to track this and will close this one.