Skip to content

Commit

Permalink
Changing the database cursor to return default for DBNull (#4070)
Browse files Browse the repository at this point in the history
* Changing the database cursor to return default for DBNull

* Fixing float/double to be NaN for null values in the database loader.

* Fixing the DatabaseLoaderTests mock to return false for IsDBNull
  • Loading branch information
tannergooding authored and Eric Erhardt committed Aug 7, 2019
1 parent a802127 commit bbb6b15
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private Delegate CreateGetterDelegate<TValue>(int col)
private ValueGetter<bool> CreateBooleanGetterDelegate(ColInfo colInfo)
{
int columnIndex = GetColumnIndex(colInfo);
return (ref bool value) => value = DataReader.GetBoolean(columnIndex);
return (ref bool value) => value = DataReader.IsDBNull(columnIndex) ? default : DataReader.GetBoolean(columnIndex);
}

private ValueGetter<byte> CreateByteGetterDelegate(ColInfo colInfo)
Expand All @@ -254,61 +254,61 @@ private ValueGetter<DateTime> CreateDateTimeGetterDelegate(ColInfo colInfo)
private ValueGetter<double> CreateDoubleGetterDelegate(ColInfo colInfo)
{
int columnIndex = GetColumnIndex(colInfo);
return (ref double value) => value = DataReader.GetDouble(columnIndex);
return (ref double value) => value = DataReader.IsDBNull(columnIndex) ? double.NaN : DataReader.GetDouble(columnIndex);
}

private ValueGetter<short> CreateInt16GetterDelegate(ColInfo colInfo)
{
int columnIndex = GetColumnIndex(colInfo);
return (ref short value) => value = DataReader.GetInt16(columnIndex);
return (ref short value) => value = DataReader.IsDBNull(columnIndex) ? default : DataReader.GetInt16(columnIndex);
}

private ValueGetter<int> CreateInt32GetterDelegate(ColInfo colInfo)
{
int columnIndex = GetColumnIndex(colInfo);
return (ref int value) => value = DataReader.GetInt32(columnIndex);
return (ref int value) => value = DataReader.IsDBNull(columnIndex) ? default : DataReader.GetInt32(columnIndex);
}

private ValueGetter<long> CreateInt64GetterDelegate(ColInfo colInfo)
{
int columnIndex = GetColumnIndex(colInfo);
return (ref long value) => value = DataReader.GetInt64(columnIndex);
return (ref long value) => value = DataReader.IsDBNull(columnIndex) ? default : DataReader.GetInt64(columnIndex);
}

private ValueGetter<sbyte> CreateSByteGetterDelegate(ColInfo colInfo)
{
int columnIndex = GetColumnIndex(colInfo);
return (ref sbyte value) => value = (sbyte)DataReader.GetByte(columnIndex);
return (ref sbyte value) => value = DataReader.IsDBNull(columnIndex) ? default : (sbyte)DataReader.GetByte(columnIndex);
}

private ValueGetter<float> CreateSingleGetterDelegate(ColInfo colInfo)
{
int columnIndex = GetColumnIndex(colInfo);
return (ref float value) => value = DataReader.GetFloat(columnIndex);
return (ref float value) => value = DataReader.IsDBNull(columnIndex) ? float.NaN : DataReader.GetFloat(columnIndex);
}

private ValueGetter<ReadOnlyMemory<char>> CreateStringGetterDelegate(ColInfo colInfo)
{
int columnIndex = GetColumnIndex(colInfo);
return (ref ReadOnlyMemory<char> value) => value = DataReader.GetString(columnIndex).AsMemory();
return (ref ReadOnlyMemory<char> value) => value = DataReader.IsDBNull(columnIndex) ? default : DataReader.GetString(columnIndex).AsMemory();
}

private ValueGetter<ushort> CreateUInt16GetterDelegate(ColInfo colInfo)
{
int columnIndex = GetColumnIndex(colInfo);
return (ref ushort value) => value = (ushort)DataReader.GetInt16(columnIndex);
return (ref ushort value) => value = DataReader.IsDBNull(columnIndex) ? default : (ushort)DataReader.GetInt16(columnIndex);
}

private ValueGetter<uint> CreateUInt32GetterDelegate(ColInfo colInfo)
{
int columnIndex = GetColumnIndex(colInfo);
return (ref uint value) => value = (uint)DataReader.GetInt32(columnIndex);
return (ref uint value) => value = DataReader.IsDBNull(columnIndex) ? default : (uint)DataReader.GetInt32(columnIndex);
}

private ValueGetter<ulong> CreateUInt64GetterDelegate(ColInfo colInfo)
{
int columnIndex = GetColumnIndex(colInfo);
return (ref ulong value) => value = (ulong)DataReader.GetInt64(columnIndex);
return (ref ulong value) => value = DataReader.IsDBNull(columnIndex) ? default : (ulong)DataReader.GetInt64(columnIndex);
}

private int GetColumnIndex(ColInfo colInfo)
Expand Down
2 changes: 1 addition & 1 deletion test/Microsoft.ML.Tests/DatabaseLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public override int GetOrdinal(string name)

public override int GetValues(object[] values) => throw new NotImplementedException();

public override bool IsDBNull(int ordinal) => throw new NotImplementedException();
public override bool IsDBNull(int ordinal) => false;

public override bool NextResult() => throw new NotImplementedException();

Expand Down

0 comments on commit bbb6b15

Please sign in to comment.