eerhardt commented on a change in pull request #9356: URL: https://github.com/apache/arrow/pull/9356#discussion_r589604019
########## File path: csharp/src/Apache.Arrow/DecimalUtility.cs ########## @@ -0,0 +1,144 @@ +// 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.Numerics; + +namespace Apache.Arrow +{ + /// <summary> + /// This is semi-optimised best attempt at converting to / from decimal and the buffers + /// </summary> + public 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(decimal value, int precision, int scale, int byteWidth, Span<byte> bytes) + { + // create BigInteger from decimal + byte[] bigIntBytes = new byte[12]; + int[] decimalBits = decimal.GetBits(value); + int decScale = (decimalBits[3] >> 16) & 0x7F; + for (int i = 0; i < 3; i++) + { + int bit = decimalBits[i]; + byte[] intBytes = BitConverter.GetBytes(bit); + for (int j = 0; j < intBytes.Length; j++) + { + bigIntBytes[4*i+j] =intBytes[j]; + } + } + + BigInteger bigInt = new BigInteger(bigIntBytes); + if (value < 0) + { + bigInt = -bigInt; + } + + // validate precision and scale + if (decScale > scale) + throw new OverflowException("Decimal scale can not be greater than that in the Arrow vector: " + decScale + " != " + scale); Review comment: ```suggestion throw new OverflowException($"Decimal scale cannot be greater than that in the Arrow vector: {decScale} != {scale}"); ``` ########## File path: csharp/src/Apache.Arrow/DecimalUtility.cs ########## @@ -0,0 +1,144 @@ +// 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.Numerics; + +namespace Apache.Arrow +{ + /// <summary> + /// This is semi-optimised best attempt at converting to / from decimal and the buffers + /// </summary> + public 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) Review comment: It looks like `isUnsigned` can be removed. No caller passes it in, and it doesn't appear to be used in this method. ########## File path: csharp/src/Apache.Arrow/DecimalUtility.cs ########## @@ -0,0 +1,144 @@ +// 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.Numerics; + +namespace Apache.Arrow +{ + /// <summary> + /// This is semi-optimised best attempt at converting to / from decimal and the buffers + /// </summary> + public 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(decimal value, int precision, int scale, int byteWidth, Span<byte> bytes) + { + // create BigInteger from decimal + byte[] bigIntBytes = new byte[12]; Review comment: We should be able to remove this allocation on NETCOREAPP and instead use `stackalloc byte[12]`. ########## File path: csharp/src/Apache.Arrow/DecimalUtility.cs ########## @@ -0,0 +1,144 @@ +// 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.Numerics; + +namespace Apache.Arrow +{ + /// <summary> + /// This is semi-optimised best attempt at converting to / from decimal and the buffers + /// </summary> + public static class DecimalUtility Review comment: Let's change this back to `internal`. We shouldn't be exposing APIs that don't need to be public. The reason is because if we make it public, we can never change it without potentially breaking someone. But if we don't make it public in the first place, consumers can't use it. So we can make any change we want in the future without worrying about breaking someone. I'm happy to see the unit tests, but this doesn't need to be public in order to have those tests. We can either: 1. Test it through existing public APIs (like a DecimalArray) 2. Use Reflection to call the internal methods 3. (not recommended unless absolutely necessary) use InternalsVisibleTo ########## File path: csharp/src/Apache.Arrow/DecimalUtility.cs ########## @@ -0,0 +1,144 @@ +// 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.Numerics; + +namespace Apache.Arrow +{ + /// <summary> + /// This is semi-optimised best attempt at converting to / from decimal and the buffers + /// </summary> + public 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(decimal value, int precision, int scale, int byteWidth, Span<byte> bytes) + { + // create BigInteger from decimal + byte[] bigIntBytes = new byte[12]; + int[] decimalBits = decimal.GetBits(value); + int decScale = (decimalBits[3] >> 16) & 0x7F; + for (int i = 0; i < 3; i++) + { + int bit = decimalBits[i]; + byte[] intBytes = BitConverter.GetBytes(bit); + for (int j = 0; j < intBytes.Length; j++) + { + bigIntBytes[4*i+j] =intBytes[j]; + } + } + + BigInteger bigInt = new BigInteger(bigIntBytes); + if (value < 0) + { + bigInt = -bigInt; + } + + // validate precision and scale + if (decScale > scale) + throw new OverflowException("Decimal scale can not be greater than that in the Arrow vector: " + decScale + " != " + scale); + + if (bigInt >= BigInteger.Pow(10, precision)) + throw new OverflowException("Decimal precision can not be greater than that in the Arrow vector: " + value + " has precision > " + precision); Review comment: ```suggestion throw new OverflowException($"Decimal precision cannot be greater than that in the Arrow vector: {value} has precision > {precision}"); ``` ########## File path: csharp/src/Apache.Arrow/DecimalUtility.cs ########## @@ -0,0 +1,144 @@ +// 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.Numerics; + +namespace Apache.Arrow +{ + /// <summary> + /// This is semi-optimised best attempt at converting to / from decimal and the buffers + /// </summary> + public 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(decimal value, int precision, int scale, int byteWidth, Span<byte> bytes) + { + // create BigInteger from decimal + byte[] bigIntBytes = new byte[12]; + int[] decimalBits = decimal.GetBits(value); + int decScale = (decimalBits[3] >> 16) & 0x7F; + for (int i = 0; i < 3; i++) + { + int bit = decimalBits[i]; + byte[] intBytes = BitConverter.GetBytes(bit); Review comment: Should be able to call `BitConverter.TryWriteBytes(Span<byte>, int)` on NETCOREAPP to get rid of this allocation. ---------------------------------------------------------------- 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