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

GH-38483: [C#] Add support for more decimal conversions #38508

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
40 changes: 40 additions & 0 deletions csharp/src/Apache.Arrow/Arrays/Decimal128Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,37 @@ public Builder AppendRange(IEnumerable<decimal> values)
return Instance;
}

public Builder Append(string value)
{
if (value == null)
{
AppendNull();
}
else
{
Span<byte> bytes = stackalloc byte[DataType.ByteWidth];
DecimalUtility.GetBytes(value, DataType.Precision, DataType.Scale, ByteWidth, bytes);
Append(bytes);
}

return Instance;
}

public Builder AppendRange(IEnumerable<string> values)
{
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}

foreach (string s in values)
{
Append(s);
}

return Instance;
}

#if !NETSTANDARD1_3
public Builder Append(SqlDecimal value)
{
Expand Down Expand Up @@ -120,6 +151,15 @@ public Decimal128Array(ArrayData data)
return DecimalUtility.GetDecimal(ValueBuffer, index, Scale, ByteWidth);
}

public string GetString(int index)
{
if (IsNull(index))
{
return null;
}
return DecimalUtility.GetString(ValueBuffer, index, Precision, Scale, ByteWidth);
}

#if !NETSTANDARD1_3
public SqlDecimal? GetSqlDecimal(int index)
{
Expand Down
98 changes: 97 additions & 1 deletion csharp/src/Apache.Arrow/Arrays/Decimal256Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

using System;
using System.Collections.Generic;
#if !NETSTANDARD1_3
using System.Data.SqlTypes;
#endif
using System.Diagnostics;
using System.Numerics;
using Apache.Arrow.Arrays;
using Apache.Arrow.Types;

Expand Down Expand Up @@ -61,6 +63,68 @@ public Builder AppendRange(IEnumerable<decimal> values)
return Instance;
}

public Builder Append(string value)
{
if (value == null)
{
AppendNull();
}
else
{
Span<byte> bytes = stackalloc byte[DataType.ByteWidth];
DecimalUtility.GetBytes(value, DataType.Precision, DataType.Scale, ByteWidth, bytes);
Append(bytes);
}

return Instance;
}

public Builder AppendRange(IEnumerable<string> values)
{
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}

foreach (string s in values)
{
Append(s);
}

return Instance;
}

#if !NETSTANDARD1_3
public Builder Append(SqlDecimal value)
{
Span<byte> bytes = stackalloc byte[DataType.ByteWidth];
DecimalUtility.GetBytes(value, DataType.Precision, DataType.Scale, bytes);
if (!value.IsPositive)
{
var span = bytes.CastTo<long>();
span[2] = -1;
span[3] = -1;
}

return Append(bytes);
}

public Builder AppendRange(IEnumerable<SqlDecimal> values)
{
if (values == null)
{
throw new ArgumentNullException(nameof(values));
}

foreach (SqlDecimal d in values)
{
Append(d);
}

return Instance;
}
#endif

public Builder Set(int index, decimal value)
{
Span<byte> bytes = stackalloc byte[DataType.ByteWidth];
Expand Down Expand Up @@ -92,5 +156,37 @@ public Decimal256Array(ArrayData data)

return DecimalUtility.GetDecimal(ValueBuffer, index, Scale, ByteWidth);
}

public string GetString(int index)
{
if (IsNull(index))
{
return null;
}
return DecimalUtility.GetString(ValueBuffer, index, Precision, Scale, ByteWidth);
}

#if !NETSTANDARD1_3
public bool TryGetSqlDecimal(int index, out SqlDecimal? value)
{
if (IsNull(index))
{
value = null;
return true;
}

const int longWidth = 4;
var span = ValueBuffer.Span.CastTo<long>().Slice(index * longWidth);
if ((span[2] == 0 && span[3] == 0) ||
(span[2] == -1 && span[3] == -1))
{
value = DecimalUtility.GetSqlDecimal128(ValueBuffer, 2 * index, Precision, Scale);
return true;
}

value = null;
return false;
}
#endif
}
}
Loading
Loading