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

Option to allow limiting auto_random maximum value/range #32508

Closed
andreifedorov-bolt opened this issue Feb 21, 2022 · 4 comments · Fixed by #36860
Closed

Option to allow limiting auto_random maximum value/range #32508

andreifedorov-bolt opened this issue Feb 21, 2022 · 4 comments · Fixed by #36860
Assignees
Labels
type/feature-request Categorizes issue or PR as related to a new feature.

Comments

@andreifedorov-bolt
Copy link

andreifedorov-bolt commented Feb 21, 2022

For some columns in databases, we use BIGINT UNSIGNED datatype. However, the following factors do not allow to use values that do not fit more than 53bits:

  1. NodeJS number datatype
  2. JSON number, only range of [-(2 ^ 53)+1, (2 ^ 53)-1] is guaranteed to work in JSON
    https://datatracker.ietf.org/doc/html/rfc7159#page-6

Examples:
% node -e 'console.log(JSON.stringify({"foobar": 18446744073709551615}))'
{"foobar":18446744073709552000}
% node -e 'console.log(JSON.parse("{"foobar": 18446744073709551615}"))'
{ foobar: 18446744073709552000 }

This causes issues if we convert autoincrement bigint unsigned to auto_random, as it almost immediately exceeds 53bit value by using the full range.
So limiting, e.g. AUTO_RANDOM(5, 6) instead of AUTO_RANDOM(5) (thus using only 6 bytes) might solve this issue.

This might be useful to allow to migrate back to auto_increment - if we limit the range when migrating to auto_random - so we have enough values free between the current and maximum.

@andreifedorov-bolt andreifedorov-bolt added the type/feature-request Categorizes issue or PR as related to a new feature. label Feb 21, 2022
@morgo
Copy link
Contributor

morgo commented Feb 25, 2022

This is a great point, thank you for the feature request.

I would actually say because it is random, there should not be a need to create an option. Not all users will be aware that numbers greater than 2^53-1 will be incorrectly handled in their apps.

@yiwen92
Copy link
Contributor

yiwen92 commented May 30, 2022

Now auto_random only support int64, it exceed the range of JSON/NodeJS data type range then cause this issue.
As discussed with @tangenta, there are 2 ways to solve this:

  1. provide int32 support auto_random, it is in the range of JSON data type;
  2. provide preserved offset to set auto_random range more flexible. And for this 53-bits limitation, it can be set to auto_random(5, 11), 5 here means shared bits default setting(timestamp), and 11 means preserve 11 bits not used for auto-random.

@yiwen92
Copy link
Contributor

yiwen92 commented May 30, 2022

@andreifedorov-bolt Hi, did you already use JSON format in TiDB? Since TiDB are also evaluting JSON support recently, could you please share your use scenario with us?

@andreifedorov-bolt
Copy link
Author

@yiwen92 Solution #1 is not acceptable for us since we did run out of int32 for some columns long time ago and migrated to bigint. Solution #2 would be perfect.
JSON usage info will be shared separately.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/feature-request Categorizes issue or PR as related to a new feature.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants