Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Column.ToArray<T> typecast error (fixes #4) #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion FeatherDotNet/Column.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,11 @@ public void GetRange<T>(long rowSourceIndex, int length, ref T[] array, int dest

if (CanBeBlitted<T>())
{
Parent.UnsafeFastGetRowRange<T>(translatedIndex, TranslatedColumnIndex, array, destinationIndex, length);
if (typeof(T) == Type) {
Parent.UnsafeFastGetRowRange<T>(translatedIndex, TranslatedColumnIndex, array, destinationIndex, length);
} else {
Parent.GetRowRangeWithTypeCast<T>(translatedIndex, TranslatedColumnIndex, array, destinationIndex, length);
}
return;
}

Expand Down
32 changes: 32 additions & 0 deletions FeatherDotNet/DataFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,38 @@ internal void UnsafeFastGetRowRange<T>(long translatedRowIndex, long translatedC
UnsafeArrayReader<T>.ReadArray(View, byteIndex, array, 0, length);
}

private Array ReadNativeArrayUnsafe<T>(long byteIndex, int length) {
T[] array = new T[length];
UnsafeArrayReader<T>.ReadArray(View, byteIndex, array, 0, length);
return array;
}

internal void GetRowRangeWithTypeCast<T>(long translatedRowIndex, long translatedColumnIndex, T[] array, int destinationIndex, int length)
{
var columnMetadata = Metadata.Columns[translatedColumnIndex];
var entrySize = columnMetadata.Type.GetAlignment();
var dataStart = columnMetadata.DataOffset;
var byteOffset = translatedRowIndex * entrySize;

var byteIndex = dataStart + byteOffset;

Array uncastedArray;
switch (columnMetadata.Type) {
case ColumnType.Int8: uncastedArray = ReadNativeArrayUnsafe<System.SByte> (byteIndex, length); break;
case ColumnType.Int16: uncastedArray = ReadNativeArrayUnsafe<System.Int16> (byteIndex, length); break;
case ColumnType.Int32: uncastedArray = ReadNativeArrayUnsafe<System.Int32> (byteIndex, length); break;
case ColumnType.Int64: uncastedArray = ReadNativeArrayUnsafe<System.Int64> (byteIndex, length); break;
case ColumnType.Uint8: uncastedArray = ReadNativeArrayUnsafe<System.Byte> (byteIndex, length); break;
case ColumnType.Uint16: uncastedArray = ReadNativeArrayUnsafe<System.UInt16>(byteIndex, length); break;
case ColumnType.Uint32: uncastedArray = ReadNativeArrayUnsafe<System.UInt32>(byteIndex, length); break;
case ColumnType.Uint64: uncastedArray = ReadNativeArrayUnsafe<System.UInt64>(byteIndex, length); break;
case ColumnType.Float: uncastedArray = ReadNativeArrayUnsafe<System.Single>(byteIndex, length); break;
case ColumnType.Double: uncastedArray = ReadNativeArrayUnsafe<System.Double>(byteIndex, length); break;
default: throw new InvalidOperationException($"Invalid cast {columnMetadata.Name}: {columnMetadata.Type} -> {typeof(T)}");
}
Array.Copy(uncastedArray, 0, array, destinationIndex, length);
}

internal bool IsNullTranslated(long translatedRowIndex, long translatedColumnIndex)
{
var columnMetadata = Metadata.Columns[translatedColumnIndex];
Expand Down