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

sql/pgwire: fix encoding of decimals with trailing 0s #64022

Merged
merged 1 commit into from
Apr 22, 2021

Conversation

rafiss
Copy link
Collaborator

@rafiss rafiss commented Apr 21, 2021

fixes #56907

The dscale is defined as number of digits (in base 10)
visible after the decimal separator," so a negative
dscale makes no sense and is not valid.

This was preventing NPGSQL from being able to decode the binary decimals
that CockroachDB was sending.

Release note (bug fix): The binary encoding of decimals no longer will
have negative dscale values. This was preventing Npgsql from being
able to read some binary decimals from CockroachDB.

@rafiss rafiss requested review from otan and a team April 21, 2021 22:04
@cockroach-teamcity
Copy link
Member

This change is Reviewable

----
{"Type":"ParseComplete"}
{"Type":"BindComplete"}
{"Type":"DataRow","Values":[{"text":"10000"},{"text":"10000"}]}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add one for 10E4 too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean add an expected return value of 10E4? i don't think i understand -- postgres will never return a decimal value with that text format (and this is the noncrdb_only expected result)

is 10E4 what we would get if we add 0000 to the encoding above? sorry i don't understand!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I meant to parse 10E4

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done! i think that shows why CRDB uses scientific notation -- it lets us preserve the number of significant digits (as detailed in #17029)


send
Parse {"Name": "s1", "Query": "SELECT 10000::decimal, $1::decimal"}
Bind {"DestinationPortal": "p1", "PreparedStatement": "s1", "ParameterFormatCodes": [1], "Parameters": [[0, 1, 0, 1, 0, 0, 0, 0, 0, 1]]}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also add a test that sends an extra 0000 at the end? (see my second bullet point for #56907 (comment))

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i may not understand -- where should i add the 0000?

When CockroachDB decodes the value NPGSQL provides, I think we will return 10000 instead of 1E+4 if you make add extra 00 00 at the end (other drivers do this so perhaps this is the problem). I think 10000 0000 is broken too, etc. for any value with 0000 as the last 4 digits.

Meaning add 00 00 to the end of the binary parameter passed in?

Copy link
Contributor

@otan otan Apr 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah sorry I mean if we do introduce the extra 0000 to read but maybe I misread my old comment

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm yeah if i add 0000 to the end of the binary param, it is rejected as invalid binary data

@rafiss rafiss force-pushed the fix-decimal-0000-encoding branch from 08e040c to 3f92175 Compare April 22, 2021 02:00
@blathers-crl blathers-crl bot requested a review from otan April 22, 2021 02:00
@rafiss rafiss force-pushed the fix-decimal-0000-encoding branch from 3f92175 to 1a35cc6 Compare April 22, 2021 06:27
The dscale is defined as number of digits (in base 10)
visible after the decimal separator," so a negative
dscale makes no sense and is not valid.

This was preventing NPGSQL from being able to decode the binary decimals
that CockroachDB was sending.

Release note (bug fix): The binary encoding of decimals no longer will
have negative `dscale` values. This was preventing Npgsql from being
able to read some binary decimals from CockroachDB.
@rafiss rafiss force-pushed the fix-decimal-0000-encoding branch from 1a35cc6 to f7fe860 Compare April 22, 2021 14:52
@rafiss rafiss requested a review from a team April 22, 2021 14:52
@rafiss
Copy link
Collaborator Author

rafiss commented Apr 22, 2021

tftr!

bors r=otan

@craig
Copy link
Contributor

craig bot commented Apr 22, 2021

Build failed (retrying...):

@craig
Copy link
Contributor

craig bot commented Apr 22, 2021

Build succeeded:

@craig craig bot merged commit 21e954f into cockroachdb:master Apr 22, 2021
@rafiss rafiss deleted the fix-decimal-0000-encoding branch April 29, 2021 16:32
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.

NUMERIC data type not decoding properly in C# (NPGSQL)
3 participants