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

fix(csharp/src/Drivers/Apache/Spark): correct precision/scale handling with zeros in fractional portion #2198

Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 8 additions & 2 deletions csharp/src/Drivers/Apache/Hive2/DecimalUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal static void GetBytes(string value, int precision, int scale, int byteWi
{
throw new ArgumentOutOfRangeException(nameof(precision), precision, "precision value must be greater than zero.");
}
if (scale < 0 || scale >= precision)
if (scale < 0 || scale > precision)
{
throw new ArgumentOutOfRangeException(nameof(scale), scale, "scale value must be in the range 0 .. precision.");
}
Expand Down Expand Up @@ -165,8 +165,14 @@ private static void ProcessDecimal(ReadOnlySpan<char> value, int precision, int
frac_length = Math.Max(Math.Min(frac_length - exponent, tempSignificant.Length - int_length), 0);
}
// Reset the integer and fractional span
integerSpan = tempSignificant.Slice(0, int_length);
fractionalSpan = tempSignificant.Slice(int_length, frac_length);
integerSpan = tempSignificant.Slice(0, int_length);
// Trim leading zeros fron new integer span
while (integerSpan.Length > 0 && integerSpan[0] == AsciiZero)
{
integerSpan = integerSpan.Slice(1);
int_length -= 1;
}
}

int neededPrecision = int_length + frac_length;
Expand Down
5 changes: 5 additions & 0 deletions csharp/test/Drivers/Apache/Hive2/DecimalUtilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ public static IEnumerable<object[]> Decimal128Data()
yield return new object[] { "-0.1E0", 38, 3, 16, new byte[] { 156, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 }, new SqlDecimal(-0.1) };
yield return new object[] { "-1e-1", 38, 3, 16, new byte[] { 156, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 }, new SqlDecimal(-0.1) };
yield return new object[] { "-0.01e1", 38, 3, 16, new byte[] { 156, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 }, new SqlDecimal(-0.1) };

yield return new object[] { "0.99999999999999999999999999999999999999", 38, 38, 16, new byte[] { 255, 255, 255, 255, 63, 34, 138, 9, 122, 196, 134, 90, 168, 76, 59, 75 } };
yield return new object[] { "0.99999999999999999999999999999999999999E0", 38, 38, 16, new byte[] { 255, 255, 255, 255, 63, 34, 138, 9, 122, 196, 134, 90, 168, 76, 59, 75 } };
yield return new object[] { "9.99999999999999999999999999999999999990e-1", 38, 38, 16, new byte[] { 255, 255, 255, 255, 63, 34, 138, 9, 122, 196, 134, 90, 168, 76, 59, 75 } };
yield return new object[] { "0.0000000000000000000000000000000000000099999999999999999999999999999999999999e38", 38, 38, 16, new byte[] { 255, 255, 255, 255, 63, 34, 138, 9, 122, 196, 134, 90, 168, 76, 59, 75 } };
}

private static SqlDecimal GetSqlDecimal128(in byte[] valueBuffer, int index, int precision, int scale)
Expand Down
Loading