Skip to content

Commit

Permalink
fix(c/driver/postgresql): fix numeric to str
Browse files Browse the repository at this point in the history
- conversion from numeric to string had two bugs due to deviations from the original PostgreSQL code
- leading zero would always be dropped
- in some cases, the numbers after decimal would not be incomplete and instead replaced with 'garbage'
  • Loading branch information
lupko committed Jan 31, 2024
1 parent d6694a2 commit 0460b7b
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions c/driver/postgresql/copy/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -352,14 +352,16 @@ class PostgresCopyNumericFieldReader : public PostgresCopyFieldReader {
// To strip leading zeroes
int append = (d > 0);

for (const auto pow10 : {1000, 100, 10, 1}) {
for (const auto pow10 : {1000, 100, 10}) {
d1 = dig / pow10;
dig -= d1 * pow10;
append |= (d1 > 0);
if (append) {
*out++ = d1 + '0';
}
}

*out++ = dig + '0';
}
}

Expand All @@ -372,18 +374,20 @@ class PostgresCopyNumericFieldReader : public PostgresCopyFieldReader {
*out++ = '.';
actual_chars_required += dscale + 1;

for (int i = 0; i < dscale; i++, d++, i += kDecDigits) {
for (int i = 0; i < dscale; d++, i += kDecDigits) {
if (d >= 0 && d < ndigits) {
dig = digits_[d];
} else {
dig = 0;
}

for (const auto pow10 : {1000, 100, 10, 1}) {
for (const auto pow10 : {1000, 100, 10}) {
d1 = dig / pow10;
dig -= d1 * pow10;
*out++ = d1 + '0';
}

*out++ = dig + '0';
}
}

Expand Down

0 comments on commit 0460b7b

Please sign in to comment.