Commit 9e0d046
committed
Fix RAND() function behavior
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.1 parent 1e4a19a commit 9e0d046
3 files changed
Lines changed: 414 additions & 21 deletions
File tree
- packages/mysql-on-sqlite
- src/sqlite
- tests
Lines changed: 28 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
436 | 436 | | |
437 | 437 | | |
438 | 438 | | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
439 | 446 | | |
440 | 447 | | |
441 | 448 | | |
| |||
724 | 731 | | |
725 | 732 | | |
726 | 733 | | |
727 | | - | |
| 734 | + | |
728 | 735 | | |
729 | 736 | | |
730 | 737 | | |
| |||
4395 | 4402 | | |
4396 | 4403 | | |
4397 | 4404 | | |
| 4405 | + | |
| 4406 | + | |
| 4407 | + | |
| 4408 | + | |
| 4409 | + | |
| 4410 | + | |
| 4411 | + | |
| 4412 | + | |
| 4413 | + | |
| 4414 | + | |
| 4415 | + | |
| 4416 | + | |
| 4417 | + | |
| 4418 | + | |
| 4419 | + | |
| 4420 | + | |
| 4421 | + | |
| 4422 | + | |
| 4423 | + | |
4398 | 4424 | | |
4399 | 4425 | | |
4400 | 4426 | | |
| |||
6609 | 6635 | | |
6610 | 6636 | | |
6611 | 6637 | | |
| 6638 | + | |
6612 | 6639 | | |
6613 | 6640 | | |
6614 | 6641 | | |
| |||
Lines changed: 97 additions & 9 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
98 | 131 | | |
99 | 132 | | |
100 | 133 | | |
| |||
167 | 200 | | |
168 | 201 | | |
169 | 202 | | |
170 | | - | |
| 203 | + | |
171 | 204 | | |
172 | | - | |
173 | | - | |
174 | | - | |
| 205 | + | |
| 206 | + | |
175 | 207 | | |
176 | | - | |
177 | | - | |
| 208 | + | |
178 | 209 | | |
179 | | - | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
180 | 231 | | |
181 | | - | |
182 | | - | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
183 | 271 | | |
184 | 272 | | |
185 | 273 | | |
| |||
0 commit comments