Conversation
Replace the stub that returned mt_rand(0, 1) — an integer 0 or 1,
not a float in [0, 1) as MySQL requires.
Unseeded RAND() compiles to a native SQLite expression:
((RANDOM() & 0x001FFFFFFFFFFFFF) / 9007199254740992.0)
The 53-bit mask matches the IEEE 754 double mantissa, so division
by 2^53 is exact and strictly less than 1.0. Matches MySQL, where
unseeded RAND() uses a thread-level state independent of RAND(N).
Seeded RAND(N) routes through a PHP UDF implementing MySQL's exact
LCG from my_rnd_init()/my_rnd() (sql/item_func.cc, mysys/my_rnd.cc),
bit-exact against MySQL 9.6. Requires 64-bit PHP.
Seed handling matches val_int(): NULL becomes 0, floats round to
nearest (RAND(3.9) == RAND(4)), numeric strings follow the same path.
The UDF keeps a single LCG state per connection, so multiple RAND(N)
call sites in one query share a stream: `SELECT RAND(1), RAND(1)`
returns (v1, v2) here vs (v, v) in MySQL. Documented divergence.
Column metadata is now reported as DOUBLE / PARAM_STR.
zaerl
reviewed
Apr 24, 2026
| return mt_rand( 0, 1 ); | ||
| public function rand( $seed ) { | ||
| // Requires 64-bit PHP. Seed * 0x10000001 can exceed PHP_INT_MAX on 32-bit. | ||
| $max_value = 0x3FFFFFFF; |
Contributor
There was a problem hiding this comment.
I really appreciate this defensive polish we have here. Despite the rarity of 32-bit systems, it is always better to handle them.
zaerl
reviewed
Apr 24, 2026
| * The UDF also handles RAND(NULL) as RAND(0), matching MySQL. | ||
| */ | ||
| if ( 0 === count( $args ) ) { | ||
| return '((RANDOM() & 0x001FFFFFFFFFFFFF) / 9007199254740992.0)'; |
Contributor
There was a problem hiding this comment.
Having some of these magic numbers declared at the class level can be useful to clean up the code a little. But feel free to ignore this; the comment above already explains it well.
Unfortunately, PHP has no constant for 2^53 - 1, which instead is Number.MAX_SAFE_INTEGER in Javascript.
zaerl
approved these changes
Apr 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Replaces the old
mt_rand(0, 1)stub — which returned an integer 0 or 1, not a float in[0, 1)as MySQL requires — with MySQL-compatibleRAND()behavior.Replaces #341.
Unseeded
RAND()Compiles to a native SQLite expression:
The 53-bit mask matches the IEEE 754 double mantissa, so division by 2^53 is exact and strictly less than 1.0. Matches MySQL, where unseeded
RAND()uses a thread-level state independent ofRAND(N).Seeded
RAND(N)Routes through a PHP UDF implementing MySQL's exact LCG from
my_rnd_init()/my_rnd()(sql/item_func.cc,mysys/my_rnd.cc), bit-exact against MySQL 9.6. Requires 64-bit PHP.Seed handling matches
val_int():NULLbecomes 0, floats round to nearest (RAND(3.9) == RAND(4)), numeric strings follow the same path.Known divergences
RAND(N)call sites in one query share a stream:SELECT RAND(1), RAND(1)returns(v1, v2)here vs(v, v)in MySQL.Both are documented in the
rand()docblock.Metadata
RAND()column metadata is now reported asDOUBLE/PARAM_STR, removing a long-standing TODO.Test plan
RAND()inWHERE,UPDATE,INSERT,ORDER BY(LIMIT+ seeded deterministic permutation).