Skip to content

Fix RAND() implementation and add plugin compatibility layer#340

Open
wp-fuse wants to merge 1 commit intoWordPress:trunkfrom
wp-fuse:fix/plugin-compatibility
Open

Fix RAND() implementation and add plugin compatibility layer#340
wp-fuse wants to merge 1 commit intoWordPress:trunkfrom
wp-fuse:fix/plugin-compatibility

Conversation

@wp-fuse
Copy link
Copy Markdown

@wp-fuse wp-fuse commented Mar 29, 2026

Fix RAND() implementation and add plugin compatibility layer

Summary

This PR fixes bugs in the RAND() implementation and adds a new plugin compatibility integration (following the existing integrations/query-monitor/ pattern) to handle third-party plugin queries that are incompatible with SQLite.

Changes

RAND() fixes (wp-includes/sqlite-ast/class-wp-pdo-mysql-on-sqlite.php)

  • Translate parameter-less RAND() natively to (ABS(RANDOM()) / 9223372036854775808.0) for better performance (avoids PHP UDF overhead per row).
  • Seeded RAND(N) calls correctly fall through to the PHP UDF via translate_sequence(), fixing a TypeError where the function returned no value.

UDF fix (wp-includes/sqlite/class-wp-sqlite-pdo-user-defined-functions.php)

  • Updated rand() to accept an optional $seed parameter and return a float (0.0–1.0) instead of an int, matching MySQL behavior.

Plugin compatibility layer (integrations/plugin-compatibility/)

New integration module (same pattern as integrations/query-monitor/) that sanitizes third-party plugin queries before they reach the AST parser:

  • Locking clauses: Strips FOR UPDATE, SKIP LOCKED, and NOWAIT globally (including inside subqueries), since SQLite doesn't support row-level locking.
  • Action Scheduler: Rewrites UPDATE ... JOIN syntax to UPDATE ... WHERE IN (...), escapes the unquoted group keyword, and fixes missing INTO in INSERT statements.

Test updates (tests/WP_SQLite_Driver_Tests.php)

  • Updated column metadata expectations for RAND() to reflect the correct DOUBLE return type.

Boot integration (wp-includes/sqlite/db.php)

  • Added require_once for the new plugin compatibility boot file.

Testing

  • All 795 unit tests pass locally.
  • Verified Action Scheduler claim execution works correctly in production with FluentForm.

- Translate parameter-less RAND() natively to SQLite for better performance
- Fix seeded RAND(N) to correctly delegate to the PHP UDF, resolving a TypeError
- Update rand() UDF to return a float (0.0-1.0) and support optional seeds
- Add plugin compatibility integration (integrations/plugin-compatibility/)
  to strip FOR UPDATE/SKIP LOCKED/NOWAIT and rewrite Action Scheduler queries
- Update RAND() test metadata to reflect correct DOUBLE return type
@wp-fuse wp-fuse force-pushed the fix/plugin-compatibility branch from 90063a9 to e835807 Compare March 30, 2026 02:06
@wp-fuse wp-fuse changed the title Fix: Enhance native AST translation and add plugin compatibility layer Fix RAND() implementation and add plugin compatibility layer Mar 30, 2026
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.

1 participant