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

Problem: float values loses precision after SQL transformation #631

Closed
wants to merge 1 commit into from

Conversation

arajkumar
Copy link
Contributor

Solution: Use format string %.15f which would retain 15 digits after the decimal point.

@arajkumar
Copy link
Contributor Author

arajkumar commented Jan 12, 2024

@dimitri I'm not sure this is a right solution, but I wanted to understand why we settled on %f which outputs floating point values with low resolution.?

I had gone through [1][2] and I agree that [1][2] will be the right fix for the problem, I would still like to understand why not just bump up the resolution as proposed in this fix? I can add tests if required.

The test cdc-wal2json seem to fail because of bumping up the resolution, but that is not a problem and I can fix expected file.

[1] eulerto/wal2json#255
[2] ebe3f33 (edited)

Solution: Use format string `%.15f` which would retain 15 digits after
the decimal point.

Signed-off-by: Arunprasad Rajkumar <ar.arunprasad@gmail.com>
@arajkumar arajkumar force-pushed the use-higher-precision branch from 3a4cf01 to 53b1aad Compare January 12, 2024 19:02
@arajkumar arajkumar changed the title Problem: float values looses precision after SQL transformation Problem: float values loses precision after SQL transformation Jan 12, 2024
@shubhamdhama
Copy link
Contributor

@arajkumar The actual problem is here,

			case JSONNumber:
			{
				double x = json_value_get_number(jsval);

				valueColumn->oid = FLOAT8OID;
				valueColumn->val.float8 = x;
				valueColumn->isNull = false;
				break;
			}

I believe JSON doesn't have limit on the length of numeric numbers. But when it's converted to a parson JSON object it detects its type as number. Hence the precision of numeric data type, which is pretty much endless, is lost when stored in double of C. Long time back I tried to patch parson to return numeric data type as string as well which worked fine (or maybe there is a way out of box in parson to read numeric as string in the first place). But then you realize Postgres supports +/-NaN and +/- Inf which comes as null from wal2json. That would lead to loss of data integrity. I guess you already know this from linked discussion.

That's why for wal2json can't be used reliable without numeric-data-types-as-string so the code path you pointed above is not used practically (and perhaps we can even delete it).

@arajkumar
Copy link
Contributor Author

Thanks @shubhamdhama.

@arajkumar arajkumar closed this Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants