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

Fix bigint PHP_INT_MIN/PHP_INT_MAX string to int convert #6410

Merged
merged 9 commits into from
Oct 25, 2024

Conversation

mvorisek
Copy link
Contributor

@mvorisek mvorisek commented May 28, 2024

Q A
Type bug
Fixed issues n/a

Summary

Resolve #6177 (comment) discussion and related original #6177.

Whole native php int range is guaranteed to be supported per https://www.doctrine-project.org/projects/doctrine-dbal/en/4.0/reference/types.html#bigint docs.

@mvorisek mvorisek force-pushed the fix_min_max_bigint_cast branch 3 times, most recently from 5b74171 to dcbddf3 Compare May 28, 2024 10:16
@mvorisek mvorisek marked this pull request as ready for review May 28, 2024 10:17
@mvorisek
Copy link
Contributor Author

@derrabus can you please review?

@derrabus
Copy link
Member

Please don't ping me on PRs please.

@mvorisek
Copy link
Contributor Author

I pinged you because of you authored the original PR, sorry.

@mvorisek mvorisek force-pushed the fix_min_max_bigint_cast branch 3 times, most recently from e22cc52 to dc5cd73 Compare May 28, 2024 15:16
@mvorisek mvorisek marked this pull request as draft May 28, 2024 15:18
@mvorisek mvorisek force-pushed the fix_min_max_bigint_cast branch 5 times, most recently from 347ded4 to 4c3258b Compare May 28, 2024 15:45
@mvorisek mvorisek marked this pull request as ready for review May 28, 2024 16:06
@derrabus
Copy link
Member

Please keep in mind that this piece of code will be executed on a hot path. If I hydrate thousands of entities with bigint fields, I don't want to execute preg_match() for each of them. Your implementation is too expensive.

@mvorisek
Copy link
Contributor Author

mvorisek commented May 28, 2024

Please keep in mind that this piece of code will be executed on a hot path. If I hydrate thousands of entities with bigint fields, I don't want to execute preg_match() for each of them. Your implementation is too expensive.

PCRE JIT is very fast, but yes, the regex replace is possible to be coded /wo regex and I alredy considered that option because only limited number (4096) are cached. I will rework the code.

@mvorisek mvorisek marked this pull request as draft May 28, 2024 19:57
@mvorisek mvorisek force-pushed the fix_min_max_bigint_cast branch 2 times, most recently from 70d3683 to fc86908 Compare May 28, 2024 20:15
@mvorisek mvorisek marked this pull request as ready for review May 28, 2024 20:18
@mvorisek
Copy link
Contributor Author

Please keep in mind that this piece of code will be executed on a hot path. If I hydrate thousands of entities with bigint fields, I don't want to execute preg_match() for each of them. Your implementation is too expensive.

done

@derrabus
Copy link
Member

Sorry, but that implementation is way too complicated. I don't want to maintain this.

@mvorisek
Copy link
Contributor Author

The implementation is minimal, we strip leading plus sign, zeros and trailing zeros after decimal point. If the number is then castable into int without precision loss, we cast it.

The leading/trailing zeros should be stripped because the input number can come from sources with explicit digits/decimal configured. This behaviour is tested and I do not think it can be implemented simpler. If you have an idea how to implemenet this simpler/better, I am of course ready for your ideas.

@mvorisek
Copy link
Contributor Author

If leading/trailing zeros should be supported, the impl. is minimal IMO. I would be happy if this can either be merged as is or please let me know how to fix the min/max issue differently.

@derrabus
Copy link
Member

Which DBMS formats 2^31-1 with leading zeros?

@mvorisek
Copy link
Contributor Author

I tested all DBs using https://dbfiddle.uk/6OSky-ka and none DB vendor prepend leading zeros even for DECIMAL type by default.

So you want me to remove the "leading zeros accepting" code in order to save a few lines of code?

@derrabus
Copy link
Member

🤷‍♂️

@mvorisek
Copy link
Contributor Author

@greg0ire with Alexander I got to a point when this PR is lighweight and passing the tests. Can you please clarify "I don't think this is going anywhere" into a feedback I can act on? Can you please reopen this PR?

@greg0ire
Copy link
Member

Clarification: I don't think there is anything you can do.

@mvorisek
Copy link
Contributor Author

Sorry, do you understand the problem, are you aware that valid max. int 2^64-1 (and min.) value is currently not properly casted to int type?

@greg0ire
Copy link
Member

I don't understand the problem, and I do not see an explanation about it anywhere.

@derrabus
Copy link
Member

I don't understand the problem

The problem ist that if the DB would return PHP_INT_MAX as string, we would not convert it to int although one might expect that.

@greg0ire greg0ire reopened this Jul 11, 2024
@greg0ire
Copy link
Member

I won't block this anymore but I don't get the fix, feel free to deal with this if you understand it @derrabus

@derrabus derrabus changed the base branch from 4.0.x to 4.1.x August 14, 2024 10:53
@derrabus derrabus changed the base branch from 4.1.x to 4.2.x October 10, 2024 13:13
tests/Functional/Types/BigIntTypeTest.php Outdated Show resolved Hide resolved
src/Types/BigIntType.php Show resolved Hide resolved
@derrabus
Copy link
Member

As soon as the unnecessary changes to the test have been reverted, I'm fine with the PR.

@derrabus derrabus added this to the 4.2.2 milestone Oct 21, 2024
@derrabus derrabus merged commit 516047c into doctrine:4.2.x Oct 25, 2024
91 checks passed
@mvorisek mvorisek deleted the fix_min_max_bigint_cast branch October 25, 2024 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants