eerhardt commented on a change in pull request #9356: URL: https://github.com/apache/arrow/pull/9356#discussion_r584834740
########## File path: csharp/src/Apache.Arrow/DecimalUtility.cs ########## @@ -0,0 +1,163 @@ +// 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.Linq; +using System.Numerics; +using System.Runtime.InteropServices; + +namespace Apache.Arrow +{ + /// <summary> + /// This is semi-optimised best attempt at converting to / from decimal and the buffers + /// </summary> + internal static class DecimalUtility + { + private static readonly BigInteger _maxDecimal = new BigInteger(decimal.MaxValue); + private static readonly BigInteger _minDecimal = new BigInteger(decimal.MinValue); + private static readonly ulong[] s_powersOfTen = + { + 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, + 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, + 1000000000000000000, 10000000000000000000 + }; + private static int PowersOfTenLength => s_powersOfTen.Length - 1; + + public static decimal GetDecimal(in ArrowBuffer valueBuffer, int index, int scale, int byteWidth, + bool isUnsigned = false) + { + int startIndex = index * byteWidth; + ReadOnlySpan<byte> value = valueBuffer.Span.Slice(startIndex, byteWidth); + BigInteger integerValue; + +#if NETCOREAPP + integerValue = new BigInteger(value); +#else + integerValue = new BigInteger(value.ToArray()); +#endif + + if (integerValue > _maxDecimal || integerValue < _minDecimal) + { + BigInteger scaleBy = BigInteger.Pow(10, scale); + BigInteger integerPart = BigInteger.DivRem(integerValue, scaleBy, out BigInteger fractionalPart); + if (integerPart > _maxDecimal || integerPart < _minDecimal) // decimal overflow, not much we can do here - C# needs a BigDecimal + { + throw new OverflowException("Value: " + integerPart + " too big or too small to be represented as a decimal"); + } + return (decimal)integerPart + DivideByScale(fractionalPart, scale); + } + else + { + return DivideByScale(integerValue, scale); + } + } + + private static decimal DivideByScale(BigInteger integerValue, int scale) + { + decimal result = (decimal)integerValue; // this cast is safe here + int drop = scale; + while (drop > PowersOfTenLength) + { + result /= s_powersOfTen[PowersOfTenLength]; + drop -= PowersOfTenLength; + } + + result /= s_powersOfTen[drop]; + return result; + } + + public static void GetBytes(BigInteger integerValue, int byteWidth, ref Span<byte> bytes) + { + if (bytes.Length != byteWidth) + { + throw new OverflowException("ValueBuffer size not equal to " + byteWidth + " byte width: " + bytes.Length); + } + + Span<byte> integerBytes = integerValue.ToByteArray().AsSpan(); + if (integerBytes.Length > byteWidth) + { + throw new OverflowException("Decimal size greater than " + byteWidth + " bytes: " + integerBytes.Length); + } + + if (integerBytes.Length == byteWidth) + { + bytes = integerBytes; + return; + } + + if (integerValue.Sign == -1) + { + integerBytes.CopyTo(bytes); + for (int i = integerBytes.Length; i < byteWidth; i++) + { + bytes[i] = 255; + } + } + else + { + integerBytes.CopyTo(bytes); + } + } + + public static bool CheckPrecisionAndScale(decimal value, int precision, int scale, out BigInteger integerValue) + { + DecimalLayout layout = new DecimalLayout(value); // use in place of decimal.GetBits(value) to avoid an allocation + integerValue = new BigInteger(BitConverter.GetBytes(layout.Lo).Concat(BitConverter.GetBytes(layout.Mid)).Concat(BitConverter.GetBytes(layout.Hi)).ToArray()); Review comment: This comment and code doesn't really make sense together. The comment says it is using `DecimalLayout` instead of `decimal.GetBits(value)` to avoid an allocation. But then the next line allocates 3 separate byte arrays, and then 2 Concat enumerables, and then a final byte array to put them all together. You might as well have just called `decimal.GetBits(value)`. Also - on .NET Core, you can call https://docs.microsoft.com/en-us/dotnet/api/system.decimal.getbits?view=net-5.0#System_Decimal_GetBits_System_Decimal_System_Span_System_Int32__, which takes a `Span<int>`. Also, one last thought - it may make sense to combine `DecimalUtility.CheckPrecisionAndScale` and `GetBytes` into one method. They are only ever called together, and that way you don't necessarily need to go through an in-between structure if not needed. ########## File path: csharp/src/Apache.Arrow/DecimalUtility.cs ########## @@ -0,0 +1,163 @@ +// 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.Linq; +using System.Numerics; +using System.Runtime.InteropServices; + +namespace Apache.Arrow +{ + /// <summary> + /// This is semi-optimised best attempt at converting to / from decimal and the buffers + /// </summary> + internal static class DecimalUtility + { + private static readonly BigInteger _maxDecimal = new BigInteger(decimal.MaxValue); + private static readonly BigInteger _minDecimal = new BigInteger(decimal.MinValue); + private static readonly ulong[] s_powersOfTen = + { + 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, + 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, + 1000000000000000000, 10000000000000000000 + }; + private static int PowersOfTenLength => s_powersOfTen.Length - 1; + + public static decimal GetDecimal(in ArrowBuffer valueBuffer, int index, int scale, int byteWidth, + bool isUnsigned = false) + { + int startIndex = index * byteWidth; + ReadOnlySpan<byte> value = valueBuffer.Span.Slice(startIndex, byteWidth); + BigInteger integerValue; + +#if NETCOREAPP + integerValue = new BigInteger(value); +#else + integerValue = new BigInteger(value.ToArray()); +#endif + + if (integerValue > _maxDecimal || integerValue < _minDecimal) + { + BigInteger scaleBy = BigInteger.Pow(10, scale); + BigInteger integerPart = BigInteger.DivRem(integerValue, scaleBy, out BigInteger fractionalPart); + if (integerPart > _maxDecimal || integerPart < _minDecimal) // decimal overflow, not much we can do here - C# needs a BigDecimal + { + throw new OverflowException("Value: " + integerPart + " too big or too small to be represented as a decimal"); + } + return (decimal)integerPart + DivideByScale(fractionalPart, scale); + } + else + { + return DivideByScale(integerValue, scale); + } + } + + private static decimal DivideByScale(BigInteger integerValue, int scale) + { + decimal result = (decimal)integerValue; // this cast is safe here + int drop = scale; + while (drop > PowersOfTenLength) + { + result /= s_powersOfTen[PowersOfTenLength]; + drop -= PowersOfTenLength; + } + + result /= s_powersOfTen[drop]; + return result; + } + + public static void GetBytes(BigInteger integerValue, int byteWidth, ref Span<byte> bytes) + { + if (bytes.Length != byteWidth) + { + throw new OverflowException("ValueBuffer size not equal to " + byteWidth + " byte width: " + bytes.Length); + } + + Span<byte> integerBytes = integerValue.ToByteArray().AsSpan(); Review comment: Instead of `ToByteArray()`, on .NET Core you can use `TryWriteBytes`, which will write directly to the `Span<byte> bytes`. This will save an allocation and a copy. See https://docs.microsoft.com/en-us/dotnet/api/system.numerics.biginteger.trywritebytes?view=net-5.0 ########## File path: csharp/src/Apache.Arrow/DecimalUtility.cs ########## @@ -0,0 +1,163 @@ +// 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.Linq; +using System.Numerics; +using System.Runtime.InteropServices; + +namespace Apache.Arrow +{ + /// <summary> + /// This is semi-optimised best attempt at converting to / from decimal and the buffers + /// </summary> + internal static class DecimalUtility + { + private static readonly BigInteger _maxDecimal = new BigInteger(decimal.MaxValue); + private static readonly BigInteger _minDecimal = new BigInteger(decimal.MinValue); + private static readonly ulong[] s_powersOfTen = + { + 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, + 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, + 1000000000000000000, 10000000000000000000 + }; + private static int PowersOfTenLength => s_powersOfTen.Length - 1; + + public static decimal GetDecimal(in ArrowBuffer valueBuffer, int index, int scale, int byteWidth, + bool isUnsigned = false) + { + int startIndex = index * byteWidth; + ReadOnlySpan<byte> value = valueBuffer.Span.Slice(startIndex, byteWidth); + BigInteger integerValue; + +#if NETCOREAPP + integerValue = new BigInteger(value); +#else + integerValue = new BigInteger(value.ToArray()); +#endif + + if (integerValue > _maxDecimal || integerValue < _minDecimal) + { + BigInteger scaleBy = BigInteger.Pow(10, scale); + BigInteger integerPart = BigInteger.DivRem(integerValue, scaleBy, out BigInteger fractionalPart); + if (integerPart > _maxDecimal || integerPart < _minDecimal) // decimal overflow, not much we can do here - C# needs a BigDecimal + { + throw new OverflowException("Value: " + integerPart + " too big or too small to be represented as a decimal"); + } + return (decimal)integerPart + DivideByScale(fractionalPart, scale); + } + else + { + return DivideByScale(integerValue, scale); + } + } + + private static decimal DivideByScale(BigInteger integerValue, int scale) + { + decimal result = (decimal)integerValue; // this cast is safe here + int drop = scale; + while (drop > PowersOfTenLength) + { + result /= s_powersOfTen[PowersOfTenLength]; + drop -= PowersOfTenLength; + } + + result /= s_powersOfTen[drop]; + return result; + } + + public static void GetBytes(BigInteger integerValue, int byteWidth, ref Span<byte> bytes) + { + if (bytes.Length != byteWidth) + { + throw new OverflowException("ValueBuffer size not equal to " + byteWidth + " byte width: " + bytes.Length); + } + + Span<byte> integerBytes = integerValue.ToByteArray().AsSpan(); + if (integerBytes.Length > byteWidth) + { + throw new OverflowException("Decimal size greater than " + byteWidth + " bytes: " + integerBytes.Length); + } + + if (integerBytes.Length == byteWidth) + { + bytes = integerBytes; + return; + } + + if (integerValue.Sign == -1) + { + integerBytes.CopyTo(bytes); + for (int i = integerBytes.Length; i < byteWidth; i++) + { + bytes[i] = 255; + } + } + else + { + integerBytes.CopyTo(bytes); + } + } + + public static bool CheckPrecisionAndScale(decimal value, int precision, int scale, out BigInteger integerValue) + { + DecimalLayout layout = new DecimalLayout(value); // use in place of decimal.GetBits(value) to avoid an allocation + integerValue = new BigInteger(BitConverter.GetBytes(layout.Lo).Concat(BitConverter.GetBytes(layout.Mid)).Concat(BitConverter.GetBytes(layout.Hi)).ToArray()); + + if (layout.Scale > scale) + throw new OverflowException("Decimal scale can not be greater than that in the Arrow vector: " + layout.Scale + " != " + scale); + + if(integerValue >= BigInteger.Pow(10, precision)) + throw new OverflowException("Decimal precision can not be greater than that in the Arrow vector: " + value + " has precision > " + precision); + + if (layout.Scale < scale) // pad with trailing zeros + { + integerValue *= BigInteger.Pow(10, scale - layout.Scale); + } + + if (value < 0) // sign the big int + integerValue = -integerValue; + + return true; + } + + [StructLayout(LayoutKind.Explicit)] + private readonly struct DecimalLayout + { + public DecimalLayout(decimal value) + { + this = default; + d = value; + } + + [FieldOffset(0)] private readonly decimal d; + + [FieldOffset(0)] private readonly int flags; + [FieldOffset(4)] private readonly int hi; +#if BIGENDIAN Review comment: This isn't going to work well because no one builds this assembly with `BIGENDIAN` enabled. How about using: ```C# private readonly int _flags; private readonly uint _hi32; private readonly ulong _lo64; ``` Which is what System.Decimal uses? To get the Low and the Mid, you can do: ```C# internal uint Low => (uint)_lo64; internal uint Mid => (uint)(_lo64 >> 32); ``` ########## File path: csharp/src/Apache.Arrow/DecimalUtility.cs ########## @@ -0,0 +1,163 @@ +// 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.Linq; +using System.Numerics; +using System.Runtime.InteropServices; + +namespace Apache.Arrow +{ + /// <summary> + /// This is semi-optimised best attempt at converting to / from decimal and the buffers + /// </summary> + internal static class DecimalUtility + { + private static readonly BigInteger _maxDecimal = new BigInteger(decimal.MaxValue); + private static readonly BigInteger _minDecimal = new BigInteger(decimal.MinValue); + private static readonly ulong[] s_powersOfTen = + { + 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, + 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, + 1000000000000000000, 10000000000000000000 + }; + private static int PowersOfTenLength => s_powersOfTen.Length - 1; + + public static decimal GetDecimal(in ArrowBuffer valueBuffer, int index, int scale, int byteWidth, + bool isUnsigned = false) + { + int startIndex = index * byteWidth; + ReadOnlySpan<byte> value = valueBuffer.Span.Slice(startIndex, byteWidth); + BigInteger integerValue; + +#if NETCOREAPP + integerValue = new BigInteger(value); +#else + integerValue = new BigInteger(value.ToArray()); +#endif + + if (integerValue > _maxDecimal || integerValue < _minDecimal) + { + BigInteger scaleBy = BigInteger.Pow(10, scale); + BigInteger integerPart = BigInteger.DivRem(integerValue, scaleBy, out BigInteger fractionalPart); + if (integerPart > _maxDecimal || integerPart < _minDecimal) // decimal overflow, not much we can do here - C# needs a BigDecimal + { + throw new OverflowException("Value: " + integerPart + " too big or too small to be represented as a decimal"); + } + return (decimal)integerPart + DivideByScale(fractionalPart, scale); + } + else + { + return DivideByScale(integerValue, scale); + } + } + + private static decimal DivideByScale(BigInteger integerValue, int scale) + { + decimal result = (decimal)integerValue; // this cast is safe here + int drop = scale; + while (drop > PowersOfTenLength) + { + result /= s_powersOfTen[PowersOfTenLength]; + drop -= PowersOfTenLength; + } + + result /= s_powersOfTen[drop]; + return result; + } + + public static void GetBytes(BigInteger integerValue, int byteWidth, ref Span<byte> bytes) Review comment: ```suggestion public static void GetBytes(BigInteger integerValue, int byteWidth, Span<byte> bytes) ``` There isn't a need to use `ref` here. ########## File path: csharp/src/Apache.Arrow/DecimalUtility.cs ########## @@ -0,0 +1,163 @@ +// 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.Linq; +using System.Numerics; +using System.Runtime.InteropServices; + +namespace Apache.Arrow +{ + /// <summary> + /// This is semi-optimised best attempt at converting to / from decimal and the buffers + /// </summary> + internal static class DecimalUtility + { + private static readonly BigInteger _maxDecimal = new BigInteger(decimal.MaxValue); + private static readonly BigInteger _minDecimal = new BigInteger(decimal.MinValue); + private static readonly ulong[] s_powersOfTen = + { + 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, + 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, + 1000000000000000000, 10000000000000000000 + }; + private static int PowersOfTenLength => s_powersOfTen.Length - 1; + + public static decimal GetDecimal(in ArrowBuffer valueBuffer, int index, int scale, int byteWidth, + bool isUnsigned = false) + { + int startIndex = index * byteWidth; + ReadOnlySpan<byte> value = valueBuffer.Span.Slice(startIndex, byteWidth); + BigInteger integerValue; + +#if NETCOREAPP + integerValue = new BigInteger(value); +#else + integerValue = new BigInteger(value.ToArray()); +#endif + + if (integerValue > _maxDecimal || integerValue < _minDecimal) + { + BigInteger scaleBy = BigInteger.Pow(10, scale); + BigInteger integerPart = BigInteger.DivRem(integerValue, scaleBy, out BigInteger fractionalPart); + if (integerPart > _maxDecimal || integerPart < _minDecimal) // decimal overflow, not much we can do here - C# needs a BigDecimal + { + throw new OverflowException("Value: " + integerPart + " too big or too small to be represented as a decimal"); + } + return (decimal)integerPart + DivideByScale(fractionalPart, scale); + } + else + { + return DivideByScale(integerValue, scale); + } + } + + private static decimal DivideByScale(BigInteger integerValue, int scale) + { + decimal result = (decimal)integerValue; // this cast is safe here + int drop = scale; + while (drop > PowersOfTenLength) + { + result /= s_powersOfTen[PowersOfTenLength]; + drop -= PowersOfTenLength; + } + + result /= s_powersOfTen[drop]; + return result; + } + + public static void GetBytes(BigInteger integerValue, int byteWidth, ref Span<byte> bytes) + { + if (bytes.Length != byteWidth) + { + throw new OverflowException("ValueBuffer size not equal to " + byteWidth + " byte width: " + bytes.Length); + } + + Span<byte> integerBytes = integerValue.ToByteArray().AsSpan(); + if (integerBytes.Length > byteWidth) + { + throw new OverflowException("Decimal size greater than " + byteWidth + " bytes: " + integerBytes.Length); + } + + if (integerBytes.Length == byteWidth) + { + bytes = integerBytes; + return; + } + + if (integerValue.Sign == -1) + { + integerBytes.CopyTo(bytes); + for (int i = integerBytes.Length; i < byteWidth; i++) + { + bytes[i] = 255; + } + } + else + { + integerBytes.CopyTo(bytes); + } + } + + public static bool CheckPrecisionAndScale(decimal value, int precision, int scale, out BigInteger integerValue) + { + DecimalLayout layout = new DecimalLayout(value); // use in place of decimal.GetBits(value) to avoid an allocation + integerValue = new BigInteger(BitConverter.GetBytes(layout.Lo).Concat(BitConverter.GetBytes(layout.Mid)).Concat(BitConverter.GetBytes(layout.Hi)).ToArray()); + + if (layout.Scale > scale) + throw new OverflowException("Decimal scale can not be greater than that in the Arrow vector: " + layout.Scale + " != " + scale); + + if(integerValue >= BigInteger.Pow(10, precision)) + throw new OverflowException("Decimal precision can not be greater than that in the Arrow vector: " + value + " has precision > " + precision); + + if (layout.Scale < scale) // pad with trailing zeros + { + integerValue *= BigInteger.Pow(10, scale - layout.Scale); + } + + if (value < 0) // sign the big int + integerValue = -integerValue; + + return true; + } + + [StructLayout(LayoutKind.Explicit)] + private readonly struct DecimalLayout + { + public DecimalLayout(decimal value) + { + this = default; Review comment: Why set `this = default`, when we set `d = value` right afterwards? ########## File path: csharp/src/Apache.Arrow/DecimalUtility.cs ########## @@ -0,0 +1,163 @@ +// 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.Linq; +using System.Numerics; +using System.Runtime.InteropServices; + +namespace Apache.Arrow +{ + /// <summary> + /// This is semi-optimised best attempt at converting to / from decimal and the buffers + /// </summary> + internal static class DecimalUtility + { + private static readonly BigInteger _maxDecimal = new BigInteger(decimal.MaxValue); + private static readonly BigInteger _minDecimal = new BigInteger(decimal.MinValue); + private static readonly ulong[] s_powersOfTen = + { + 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, + 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 10000000000000000, 100000000000000000, + 1000000000000000000, 10000000000000000000 + }; + private static int PowersOfTenLength => s_powersOfTen.Length - 1; + + public static decimal GetDecimal(in ArrowBuffer valueBuffer, int index, int scale, int byteWidth, + bool isUnsigned = false) + { + int startIndex = index * byteWidth; + ReadOnlySpan<byte> value = valueBuffer.Span.Slice(startIndex, byteWidth); + BigInteger integerValue; + +#if NETCOREAPP + integerValue = new BigInteger(value); +#else + integerValue = new BigInteger(value.ToArray()); +#endif + + if (integerValue > _maxDecimal || integerValue < _minDecimal) + { + BigInteger scaleBy = BigInteger.Pow(10, scale); + BigInteger integerPart = BigInteger.DivRem(integerValue, scaleBy, out BigInteger fractionalPart); + if (integerPart > _maxDecimal || integerPart < _minDecimal) // decimal overflow, not much we can do here - C# needs a BigDecimal + { + throw new OverflowException("Value: " + integerPart + " too big or too small to be represented as a decimal"); + } + return (decimal)integerPart + DivideByScale(fractionalPart, scale); + } + else + { + return DivideByScale(integerValue, scale); + } + } + + private static decimal DivideByScale(BigInteger integerValue, int scale) + { + decimal result = (decimal)integerValue; // this cast is safe here + int drop = scale; + while (drop > PowersOfTenLength) + { + result /= s_powersOfTen[PowersOfTenLength]; + drop -= PowersOfTenLength; + } + + result /= s_powersOfTen[drop]; + return result; + } + + public static void GetBytes(BigInteger integerValue, int byteWidth, ref Span<byte> bytes) + { + if (bytes.Length != byteWidth) + { + throw new OverflowException("ValueBuffer size not equal to " + byteWidth + " byte width: " + bytes.Length); + } + + Span<byte> integerBytes = integerValue.ToByteArray().AsSpan(); + if (integerBytes.Length > byteWidth) + { + throw new OverflowException("Decimal size greater than " + byteWidth + " bytes: " + integerBytes.Length); + } + + if (integerBytes.Length == byteWidth) + { + bytes = integerBytes; + return; + } + + if (integerValue.Sign == -1) + { + integerBytes.CopyTo(bytes); + for (int i = integerBytes.Length; i < byteWidth; i++) + { + bytes[i] = 255; + } + } + else + { + integerBytes.CopyTo(bytes); + } + } + + public static bool CheckPrecisionAndScale(decimal value, int precision, int scale, out BigInteger integerValue) + { + DecimalLayout layout = new DecimalLayout(value); // use in place of decimal.GetBits(value) to avoid an allocation + integerValue = new BigInteger(BitConverter.GetBytes(layout.Lo).Concat(BitConverter.GetBytes(layout.Mid)).Concat(BitConverter.GetBytes(layout.Hi)).ToArray()); + + if (layout.Scale > scale) + throw new OverflowException("Decimal scale can not be greater than that in the Arrow vector: " + layout.Scale + " != " + scale); Review comment: Do we have tests for these overflow scenarios? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org