Skip to content

Commit

Permalink
Refactor decimal reader. Make numeric reader sealed
Browse files Browse the repository at this point in the history
  • Loading branch information
Giorgi committed Jul 6, 2024
1 parent 9c0dbf8 commit efea40d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
14 changes: 11 additions & 3 deletions DuckDB.NET.Data/Internal/Reader/DecimalVectorDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@

namespace DuckDB.NET.Data.Internal.Reader;

internal sealed class DecimalVectorDataReader : NumericVectorDataReader
internal sealed class DecimalVectorDataReader : VectorDataReaderBase
{
private readonly DuckDBType decimalType;
private readonly NumericVectorDataReader numericVectorDataReader;

internal unsafe DecimalVectorDataReader(IntPtr vector, void* dataPointer, ulong* validityMaskPointer, DuckDBType columnType, string columnName) : base(dataPointer, validityMaskPointer, columnType, columnName)
{
using var logicalType = NativeMethods.Vectors.DuckDBVectorGetColumnType(vector);
Scale = NativeMethods.LogicalType.DuckDBDecimalScale(logicalType);
Precision = NativeMethods.LogicalType.DuckDBDecimalWidth(logicalType);
decimalType = NativeMethods.LogicalType.DuckDBDecimalInternalType(logicalType);

numericVectorDataReader = new NumericVectorDataReader(dataPointer, validityMaskPointer, columnType, columnName);
}

internal byte Scale { get; }
Expand All @@ -22,7 +25,7 @@ internal unsafe DecimalVectorDataReader(IntPtr vector, void* dataPointer, ulong*

protected override T GetValidValue<T>(ulong offset, Type targetType)
{
if (DuckDBType!= DuckDBType.Decimal)
if (DuckDBType != DuckDBType.Decimal)
{
return base.GetValidValue<T>(offset, targetType);
}
Expand Down Expand Up @@ -54,7 +57,7 @@ private decimal GetDecimal(ulong offset)
return decimal.Divide(GetFieldData<long>(offset), pow);
case DuckDBType.HugeInt:
{
var hugeInt = GetBigInteger(offset, false);
var hugeInt = numericVectorDataReader.GetBigInteger(offset, false);

var result = (decimal)BigInteger.DivRem(hugeInt, (BigInteger)pow, out var remainder);

Expand All @@ -65,4 +68,9 @@ private decimal GetDecimal(ulong offset)
}
}

public override void Dispose()
{
numericVectorDataReader.Dispose();
base.Dispose();
}
}
6 changes: 3 additions & 3 deletions DuckDB.NET.Data/Internal/Reader/NumericVectorDataReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace DuckDB.NET.Data.Internal.Reader;

internal class NumericVectorDataReader : VectorDataReaderBase
internal sealed class NumericVectorDataReader : VectorDataReaderBase
{
internal unsafe NumericVectorDataReader(void* dataPointer, ulong* validityMaskPointer, DuckDBType columnType, string columnName) : base(dataPointer, validityMaskPointer, columnType, columnName)
{
Expand Down Expand Up @@ -85,7 +85,7 @@ internal override object GetValue(ulong offset, Type targetType)
throw new InvalidCastException($"Cannot cast from {value.GetType().Name} to {targetType.Name} in column {ColumnName}");
}

protected unsafe BigInteger GetBigInteger(ulong offset, bool unsigned)
internal unsafe BigInteger GetBigInteger(ulong offset, bool unsigned)
{
if (unsigned)
{
Expand All @@ -99,7 +99,7 @@ protected unsafe BigInteger GetBigInteger(ulong offset, bool unsigned)
}
}

protected T GetBigInteger<T>(ulong offset, bool unsigned)
private T GetBigInteger<T>(ulong offset, bool unsigned)
{
var bigInteger = GetBigInteger(offset, unsigned);

Expand Down

0 comments on commit efea40d

Please sign in to comment.