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

spanner: JSON number decode precision error #8669

Closed
harshachinta opened this issue Oct 11, 2023 · 1 comment · Fixed by #9054
Closed

spanner: JSON number decode precision error #8669

harshachinta opened this issue Oct 11, 2023 · 1 comment · Fixed by #9054
Assignees
Labels
api: spanner Issues related to the Spanner API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@harshachinta
Copy link
Contributor

harshachinta commented Oct 11, 2023

Issue
When JSON data contains a number, then decoding it to NullJson leads to loss of precision.

Test case to reproduce

func TestDecodeJsonValue(t *testing.T) {
	nulljson := NullJSON{}
	err := decodeValue(intProto(145688415796432520), jsonType(), &nulljson)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(nulljson.Value)
	fmt.Println(nulljson)
}

The output of the above test case is

=== RUN   TestDecodeJsonValue
1.456884157964325e+17
145688415796432500

The expected output is 145688415796432520, but the returned output is 145688415796432500.

Code causing the issue
In decodeValue() function in value.go, looking at the switch case when interface is *NullJson. The code uses,

err := json.Unmarshal([]byte(x), &y)

The json package in golang by default considers the numbers in JSON as float64 values. The documentation is attached below,
image

Attaching the debugger output where the number is converted to float by the json package.
image

References
https://stackoverflow.com/questions/16946306/preserve-int64-values-when-parsing-json-in-go
https://stackoverflow.com/questions/24480835/decoding-json-int-into-string
golang/go#42246

@harshachinta harshachinta added the triage me I really want to be triaged. label Oct 11, 2023
@product-auto-label product-auto-label bot added the api: spanner Issues related to the Spanner API. label Oct 11, 2023
@harshachinta harshachinta self-assigned this Oct 11, 2023
@noahdietz noahdietz added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. priority: p2 Moderately-important priority. Fix may not be included in next release. and removed triage me I really want to be triaged. labels Oct 16, 2023
@noahdietz
Copy link
Contributor

On rotation, I took the liberty of "triaging" this issue. Update as you see fit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: spanner Issues related to the Spanner API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants
@noahdietz @rahul2393 @harshachinta and others