Skip to content

Commit

Permalink
Extract data reading code
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed Sep 28, 2023
1 parent 75bd7c1 commit 50a4879
Showing 1 changed file with 34 additions and 43 deletions.
77 changes: 34 additions & 43 deletions DuckDB.NET.Data/DuckDBDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,23 +82,25 @@ private void InitChunkData()
}
}

public override unsafe bool GetBoolean(int ordinal)
private unsafe T GetFieldData<T>(int ordinal) where T: unmanaged
{
var data = (byte*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
var b = *data;
return b != 0;
var data = (T*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
return *data;
}

public override unsafe byte GetByte(int ordinal)
public override bool GetBoolean(int ordinal)
{
var data = (byte*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
return *data;
return GetByte(ordinal) != 0;
}

private unsafe sbyte GetSByte(int ordinal)
public override byte GetByte(int ordinal)
{
var data = (sbyte*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
return *data;
return GetFieldData<byte>(ordinal);
}

private sbyte GetSByte(int ordinal)
{
return GetFieldData<sbyte>(ordinal);
}

public override long GetBytes(int ordinal, long dataOffset, byte[] buffer, int bufferOffset, int length)
Expand Down Expand Up @@ -127,17 +129,15 @@ public override unsafe DateTime GetDateTime(int ordinal)
return data->ToDateTime();
}

private unsafe DuckDBDateOnly GetDateOnly(int ordinal)
private DuckDBDateOnly GetDateOnly(int ordinal)
{
var data = (DuckDBDate*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
var date = *data;
var date = GetFieldData<DuckDBDate>(ordinal);
return NativeMethods.DateTime.DuckDBFromDate(date);
}

private unsafe DuckDBTimeOnly GetTimeOnly(int ordinal)
private DuckDBTimeOnly GetTimeOnly(int ordinal)
{
var data = (DuckDBTime*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
var time = *data;
var time = GetFieldData<DuckDBTime>(ordinal);
return NativeMethods.DateTime.DuckDBFromTime(time);
}

Expand Down Expand Up @@ -176,10 +176,9 @@ public override decimal GetDecimal(int ordinal)
return result;
}

public override unsafe double GetDouble(int ordinal)
public override double GetDouble(int ordinal)
{
var data = (double*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
return *data;
return GetFieldData<double>(ordinal);
}

public override Type GetFieldType(int ordinal)
Expand Down Expand Up @@ -210,51 +209,44 @@ public override Type GetFieldType(int ordinal)
};
}

public override unsafe float GetFloat(int ordinal)
public override float GetFloat(int ordinal)
{
var data = (float*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
return *data;
return GetFieldData<float>(ordinal);
}

public override Guid GetGuid(int ordinal)
{
return new Guid(GetString(ordinal));
}

public override unsafe short GetInt16(int ordinal)
public override short GetInt16(int ordinal)
{
var data = (short*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
return *data;
return GetFieldData<short>(ordinal);
}

public override unsafe int GetInt32(int ordinal)
public override int GetInt32(int ordinal)
{
var data = (int*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
return *data;
return GetFieldData<int>(ordinal);
}

public override unsafe long GetInt64(int ordinal)
public override long GetInt64(int ordinal)
{
var data = (long*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
return *data;
return GetFieldData<long>(ordinal);
}

private unsafe ushort GetUInt16(int ordinal)
private ushort GetUInt16(int ordinal)
{
var data = (ushort*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
return *data;
return GetFieldData<ushort>(ordinal);
}

private unsafe uint GetUInt32(int ordinal)
private uint GetUInt32(int ordinal)
{
var data = (uint*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
return *data;
return GetFieldData<uint>(ordinal);
}

private unsafe ulong GetUInt64(int ordinal)
private ulong GetUInt64(int ordinal)
{
var data = (ulong*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
return *data;
return GetFieldData<ulong>(ordinal);
}

private unsafe BigInteger GetBigInteger(int ordinal)
Expand Down Expand Up @@ -329,10 +321,9 @@ public override object GetValue(int ordinal)
};
}

private unsafe DuckDBInterval GetDuckDBInterval(int ordinal)
private DuckDBInterval GetDuckDBInterval(int ordinal)
{
var data = (DuckDBInterval*)vectors[ordinal] + rowsReadFromCurrentChunk - 1;
return *data;
return GetFieldData<DuckDBInterval>(ordinal);
}

public override int GetValues(object[] values)
Expand Down

0 comments on commit 50a4879

Please sign in to comment.