Skip to content

ARROW-11422: [C#] add decimal support #9356

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

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion csharp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ This implementation is under development and may not be suitable for use in prod
- Timestamp
- Date32
- Date64
- Decimal
- Time32
- Time64
- Binary (fixed-length)
Expand Down Expand Up @@ -124,7 +125,6 @@ This implementation is under development and may not be suitable for use in prod
- Dense
- Sparse
- Half-Float
- Decimal
- Dictionary
- Array Operations
- Equality / Comparison
Expand Down
7 changes: 5 additions & 2 deletions csharp/src/Apache.Arrow/Arrays/ArrowArrayBuilderFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
Expand Down Expand Up @@ -58,9 +58,12 @@ internal static IArrowArrayBuilder<IArrowArray, IArrowArrayBuilder<IArrowArray>>
return new Date32Array.Builder();
case ArrowTypeId.List:
return new ListArray.Builder(dataType as ListType);
case ArrowTypeId.Decimal128:
return new Decimal128Array.Builder(dataType as Decimal128Type);
case ArrowTypeId.Decimal256:
return new Decimal256Array.Builder(dataType as Decimal256Type);
case ArrowTypeId.Struct:
case ArrowTypeId.Union:
case ArrowTypeId.Decimal:
case ArrowTypeId.Dictionary:
case ArrowTypeId.FixedSizedBinary:
case ArrowTypeId.HalfFloat:
Expand Down
5 changes: 4 additions & 1 deletion csharp/src/Apache.Arrow/Arrays/ArrowArrayFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ public static IArrowArray BuildArray(ArrayData data)
return new Date64Array(data);
case ArrowTypeId.Date32:
return new Date32Array(data);
case ArrowTypeId.Decimal:
case ArrowTypeId.Decimal128:
return new Decimal128Array(data);
case ArrowTypeId.Decimal256:
return new Decimal256Array(data);
case ArrowTypeId.Dictionary:
case ArrowTypeId.FixedSizedBinary:
case ArrowTypeId.HalfFloat:
Expand Down
95 changes: 95 additions & 0 deletions csharp/src/Apache.Arrow/Arrays/Decimal128Array.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Apache.Arrow.Arrays;
using Apache.Arrow.Types;

namespace Apache.Arrow
{
public class Decimal128Array : FixedSizeBinaryArray
{
public class Builder : BuilderBase<Decimal128Array, Builder>
{
public Builder(Decimal128Type type) : base(type, 16)
{
DataType = type;
}

protected new Decimal128Type DataType { get; }

protected override Decimal128Array Build(ArrayData data)
{
return new Decimal128Array(data);
}

public Builder Append(decimal value)
{
Span<byte> bytes = stackalloc byte[DataType.ByteWidth];
DecimalUtility.GetBytes(value, DataType.Precision, DataType.Scale, DataType.ByteWidth, bytes);

return Append(bytes);
}

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

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

return Instance;
}

public Builder Set(int index, decimal value)
{
Span<byte> bytes = stackalloc byte[DataType.ByteWidth];
DecimalUtility.GetBytes(value, DataType.Precision, DataType.Scale, DataType.ByteWidth, bytes);

return Set(index, bytes);
}
}

public Decimal128Array(ArrayData data)
: base(ArrowTypeId.Decimal128, data)
{
data.EnsureDataType(ArrowTypeId.Decimal128);
data.EnsureBufferCount(2);
Debug.Assert(Data.DataType is Decimal128Type);
}
public override void Accept(IArrowArrayVisitor visitor) => Accept(this, visitor);

public int Scale => ((Decimal128Type)Data.DataType).Scale;
public int Precision => ((Decimal128Type)Data.DataType).Precision;
public int ByteWidth => ((Decimal128Type)Data.DataType).ByteWidth;

public decimal? GetValue(int index)
{
if (IsNull(index))
{
return null;
}
return DecimalUtility.GetDecimal(ValueBuffer, index, Scale, ByteWidth);
}
}
}
96 changes: 96 additions & 0 deletions csharp/src/Apache.Arrow/Arrays/Decimal256Array.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Apache.Arrow.Arrays;
using Apache.Arrow.Types;

namespace Apache.Arrow
{
public class Decimal256Array : FixedSizeBinaryArray
{
public class Builder : BuilderBase<Decimal256Array, Builder>
{
public Builder(Decimal256Type type) : base(type, 32)
{
DataType = type;
}

protected new Decimal256Type DataType { get; }

protected override Decimal256Array Build(ArrayData data)
{
return new Decimal256Array(data);
}

public Builder Append(decimal value)
{
Span<byte> bytes = stackalloc byte[DataType.ByteWidth];
DecimalUtility.GetBytes(value, DataType.Precision, DataType.Scale, DataType.ByteWidth, bytes);

return Append(bytes);
}

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

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

return Instance;
}

public Builder Set(int index, decimal value)
{
Span<byte> bytes = stackalloc byte[DataType.ByteWidth];
DecimalUtility.GetBytes(value, DataType.Precision, DataType.Scale, DataType.ByteWidth, bytes);

return Set(index, bytes);
}
}

public Decimal256Array(ArrayData data)
: base(ArrowTypeId.Decimal256, data)
{
data.EnsureDataType(ArrowTypeId.Decimal256);
data.EnsureBufferCount(2);
Debug.Assert(Data.DataType is Decimal256Type);
}
public override void Accept(IArrowArrayVisitor visitor) => Accept(this, visitor);

public int Scale => ((Decimal256Type)Data.DataType).Scale;
public int Precision => ((Decimal256Type)Data.DataType).Precision;
public int ByteWidth => ((Decimal256Type)Data.DataType).ByteWidth;

public decimal? GetValue(int index)
{
if (IsNull(index))
{
return null;
}

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