Skip to content

Commit

Permalink
Assert reading numeric data as other types
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed Nov 10, 2023
1 parent 878abe2 commit e3f952d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
18 changes: 16 additions & 2 deletions DuckDB.NET.Data/Internal/Reader/NumericVectorDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,14 @@ internal override object GetValue(ulong offset, Type? targetType = null)

if (targetType.IsNumeric())
{
return Convert.ChangeType(value, targetType);
try
{
return Convert.ChangeType(value, targetType);
}
catch (OverflowException)
{
throw new InvalidCastException($"Cannot cast from {value.GetType().Name} to {targetType.Name} in column {ColumnName}");
}
}

throw new InvalidCastException($"Cannot cast from {value.GetType().Name} to {targetType.Name} in column {ColumnName}");
Expand Down Expand Up @@ -127,6 +134,13 @@ private TResult GetUnmanagedTypeValue<TQuery, TResult>(ulong offset) where TQuer
return Unsafe.As<TQuery, TResult>(ref value);
}

return (TResult)Convert.ChangeType(value, typeof(TResult));
try
{
return (TResult)Convert.ChangeType(value, typeof(TResult));
}
catch (OverflowException)
{
throw new InvalidCastException($"Cannot cast from {value.GetType().Name} to {typeof(TResult).Name} in column {ColumnName}");
}
}
}
29 changes: 24 additions & 5 deletions DuckDB.NET.Test/Parameters/IntegerParametersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ namespace DuckDB.NET.Test.Parameters;

public class IntegerParametersTests
{
private static void TestBind<TValue>(DuckDBConnection connection, TValue expectedValue,
DuckDBParameter parameter, Func<DuckDBDataReader, TValue> getValue)
private static void TestBind<TValue>(DuckDBConnection connection, TValue expectedValue, DuckDBParameter parameter, Func<DuckDBDataReader, TValue> getValue)
{
var command = connection.CreateCommand();
command.CommandText = "SELECT ?;";
Expand All @@ -24,8 +23,7 @@ private static void TestBind<TValue>(DuckDBConnection connection, TValue expecte
value.Should().Be(expectedValue);
}

private static void TestSimple<TValue>(DuckDBConnection connection, string duckDbType, TValue expectedValue,
Func<DuckDBDataReader, TValue> getValue)
private static void TestSimple<TValue>(DuckDBConnection connection, string duckDbType, TValue expectedValue, Func<DuckDBDataReader, TValue> getValue)
{
var command = connection.CreateCommand();
command.CommandText = $"CREATE TABLE {duckDbType}_test (a {duckDbType});";
Expand All @@ -48,14 +46,35 @@ private static void TestSimple<TValue>(DuckDBConnection connection, string duckD
value.Should().Be(expectedValue);

reader.Invoking(r => r.GetFieldValue<string>(0)).Should().Throw<InvalidCastException>();

reader.GetFieldType(0).Should().Match(type => type == typeof(TValue) || type == Nullable.GetUnderlyingType(typeof(TValue)));

TestReadValueAs<byte>(reader);
TestReadValueAs<sbyte>(reader);
TestReadValueAs<ushort>(reader);
TestReadValueAs<short>(reader);
TestReadValueAs<uint>(reader);
TestReadValueAs<int>(reader);
TestReadValueAs<ulong>(reader);
TestReadValueAs<long>(reader);
}
finally
{
command.CommandText = $"DROP TABLE {duckDbType}_test;";
command.ExecuteNonQuery();
}

void TestReadValueAs<T>(DuckDBDataReader reader)
{
try
{
var convertedExpectedValue = (T)Convert.ChangeType(expectedValue, typeof(T));
convertedExpectedValue.Should().Be(reader.GetFieldValue<T>(0));
}
catch (Exception)
{
reader.Invoking(dataReader => dataReader.GetFieldValue<T>(0)).Should().Throw<InvalidCastException>();
}
}
}

[Theory]
Expand Down

0 comments on commit e3f952d

Please sign in to comment.