Skip to content

Commit 49d783b

Browse files
authored
LALR(1) parser from official MySQL grammar (#429)
> [!NOTE] > The changed line numbers are misleading—about 115,000 added lines is just a testing query corpus. > (Copied to the new `mysql-parser` package from `mysql-on-sqlite`.) ## LALR(1) parser from official MySQL grammar A new experimental **`packages/mysql-parser` package** that implements a universal **LALR(1) parser** and builds a MySQL parse table from the **official MySQL grammar**. This is the initial implementation, not used anywhere in the driver yet. A full driver migration to this new parser is AI-prototyped in #432. ### What it does - **Grammar processing pipeline:** Fetch sources → Bison → generate parse table and token data. - **Lexer:** The existing MySQL lexer was copied and adapted to the new LALR(1) grammar. - **Parser:** A new universal LALR(1) parser implementation. - **MySQL grammar:** A compacted MySQL 8.4 LTS grammar, extracted using the grammar processing pipeline. - **MySQL query corpus:** The ~70k MySQL query corpus was copied and updated to MySQL 8.4 LTS. - **Benchmark:** A no-JIT/JIT lexer + parser benchmark. - **Test suite:** New tests and a CI job. ### What it doesn't do yet - **Replace the current parser:** It's a standalone package that doesn't replace the existing parser yet. - **Multi-version:** For now, the parser only tracks MySQL 8.4 LTS. Multi-version will be done as a follow-up. ### Benchmarks Measured on MacBook Pro M4 Max on PHP 8.4, the package's 8.4.10 corpus ~70k queries, end-to-end (lex + parse), best of 5 timed passes after 2 warmups: | Metric | LL (trunk) | LALR (this) | | --- | --- | --- | | Throughput, no JIT | 11,010 QPS | **59,457 QPS** | | Throughput, warm JIT | 24,393 QPS | **112,759 QPS** | | Cold boot, no opcache | **~1.9 ms** | ~2.7 ms | | Warm boot, opcache | ~0.6 ms | **~0.3 ms** | | Memory, no opcache | **~3.4 MB** | ~5.4 MB | | Memory, opcache worker | **~1.8 MB** | ~3.1 MB | | Generated parser/table file size | **65 KB** | 177 KB | | Full size (lexer + parser + grammar) | **246 KB** | 260 KB | This parser is over **5× faster** without JIT and over **4.5× faster** with JIT. Cold boot is a bit slower; warm boot is faster. The memory footprint is a bit higher, and the overall size about 14 KB higher. #### Recognize-only The same lex+parse runs but building **no AST**, measuring only recognition without AST allocation: | Throughput | LL (trunk) | LALR (this) | | --- | --- | --- | | no JIT | 16,359 QPS | **95,374 QPS** | | warm JIT | 49,940 QPS | **210,032 QPS** | Dropping AST construction lifts both by ~1.5–2×, but the gap stays around **~4.2–5.8×**.
1 parent 4c7d586 commit 49d783b

31 files changed

Lines changed: 120989 additions & 0 deletions
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: MySQL Parser Tests
2+
3+
on:
4+
push:
5+
branches:
6+
- trunk
7+
paths:
8+
- '.github/workflows/mysql-parser-tests.yml'
9+
- 'packages/mysql-parser/**'
10+
pull_request:
11+
paths:
12+
- '.github/workflows/mysql-parser-tests.yml'
13+
- 'packages/mysql-parser/**'
14+
workflow_dispatch:
15+
16+
concurrency:
17+
group: ${{ github.workflow }}-${{ github.ref }}
18+
cancel-in-progress: true
19+
20+
# Disable permissions for all available scopes by default.
21+
# Any needed permissions should be configured at the job level.
22+
permissions: {}
23+
24+
jobs:
25+
test:
26+
# The runtime supports PHP 7.2+; test the oldest and the latest.
27+
name: PHP ${{ matrix.php }}
28+
runs-on: ubuntu-latest
29+
timeout-minutes: 10
30+
permissions:
31+
contents: read # Required to clone the repo.
32+
strategy:
33+
fail-fast: false
34+
matrix:
35+
php: [ '7.2', '8.5' ]
36+
37+
steps:
38+
- name: Checkout repository
39+
uses: actions/checkout@v4
40+
41+
- name: Set up PHP
42+
uses: shivammathur/setup-php@v2
43+
with:
44+
php-version: ${{ matrix.php }}
45+
coverage: none
46+
47+
- name: Install dependencies
48+
working-directory: packages/mysql-parser
49+
run: composer install --no-interaction --no-progress
50+
51+
- name: Run tests
52+
working-directory: packages/mysql-parser
53+
run: composer run test

‎packages/mysql-parser/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build/

‎packages/mysql-parser/README.md‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# MySQL Parser
2+
3+
A fast and complete **MySQL parser** in pure PHP with zero dependencies, generated
4+
directly from the **official MySQL grammar**.
5+
6+
The runtime requires **PHP 7.2+** with no extensions.
7+
8+
## How it works
9+
10+
The grammar is compiled ahead of time from the official MySQL grammar sources.
11+
It outputs a compact parse table and a list of MySQL tokens for the lexer:
12+
13+
```
14+
mysql-server sources
15+
sql/sql_yacc.yy ──▶ Bison 3.8.2 ──▶ automaton.xml ─┬─▶ generate-parse-table.php ──▶ mysql-parse-table.php
16+
|
17+
sql/lex.h ─────────────────────────────────────────┴─▶ generate-tokens.php ───────▶ class-wp-mysql-tokens.php
18+
```
19+
20+
The runtime ships only the compiled artifacts and a thin parser implementation.
21+
In this package, the runtime lives under `src` and the grammar tooling in `tools`.
22+
23+
A MySQL query is processed using the following pipeline:
24+
```
25+
MySQL query ──────────▶ WP_MySQL_Lexer ──────────▶ WP_Parser ──────────▶ AST
26+
string WP_MySQL_Token[] WP_Parser_Node(s)
27+
WP_MySQL_Token(s)
28+
```
29+
30+
## Usage
31+
32+
```php
33+
require_once __DIR__ . '/vendor/autoload.php';
34+
35+
$parser = WP_MySQL_Parser_Factory::create_parser();
36+
$tokens = ( new WP_MySQL_Lexer( 'SELECT 1 + 2' ) )->remaining_tokens();
37+
$ast = $parser->parse( $tokens );
38+
```
39+
40+
## Development
41+
This package includes the full grammar compilation pipeline. The generated artifacts
42+
are committed to the repository, and they only need to be regenerated on grammar
43+
changes or changes to the compilation pipeline itself. The grammar comes with a
44+
testing query corpus extracted from the MySQL server test suite.
45+
46+
### Building the grammar
47+
To regenerate the compiled MySQL artifacts from MySQL sources, use:
48+
49+
```bash
50+
composer run build-grammar
51+
```
52+
53+
This requires `bash`, `curl`, `docker`, and `php` and executes the following steps:
54+
1. Fetch the grammar from https://github.com/mysql/mysql-server/.
55+
2. Run Bison in a Docker container to generate `build/automaton.xml`.
56+
3. Extract and compact the grammar and tokens, and save them in `src`.
57+
58+
### Building the query corpus
59+
This package includes a testing corpus of about 70,000 MySQL queries extracted
60+
from the MySQL release that corresponds to the generated MySQL grammar version.
61+
To regenerate it, use:
62+
63+
```bash
64+
composer run build-corpus
65+
```
66+
67+
This requires `bash`, `git`, and `php`. It performs the following steps:
68+
1. Shallow-clone the `mysql-test` directory from MySQL server into `build/`.
69+
2. Extract SQL queries from the MySQL test files.
70+
3. Store the queries under `data/mysql-server-query-corpus/mysql-latest.csv`.
71+
72+
## Tests and benchmarks
73+
To run lexer and parser tests and benchmarks, use:
74+
75+
```bash
76+
composer run test # PHPUnit suite, including the query corpus tests
77+
composer run benchmark # Query throughput with and without JIT on the whole corpus
78+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Build the MySQL server query corpus from the MySQL server test suite.
4+
#
5+
# Runs the full pipeline end to end:
6+
# 1. Fetch the mysql-test directory from the pinned mysql-server tag.
7+
# 2. Extract the SQL queries into data/mysql-server-query-corpus/.
8+
#
9+
# Requirements: bash, git, php. Override the MySQL version with MYSQL_TAG
10+
# (default: mysql-8.4.10).
11+
#
12+
set -euo pipefail
13+
14+
package_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
15+
tools_dir="$package_dir/tools"
16+
17+
bash "$tools_dir/fetch-mysql-tests.sh"
18+
19+
echo "Generating the query corpus ..."
20+
php "$tools_dir/generate-query-corpus.php"
21+
22+
echo "Done."
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Build the MySQL parse table and token map from the official MySQL grammar.
4+
#
5+
# Runs the full pipeline end to end:
6+
# 1. Fetch sql_yacc.yy + lex.h from the pinned mysql-server tag.
7+
# 2. Run Bison 3.8.2 to produce the LALR automaton (automaton.xml).
8+
# 3. Generate src/mysql-parse-table.php from the automaton.
9+
# 4. Generate the lexer's token data block from lex.h and the automaton.
10+
#
11+
# Requirements: bash, curl, docker, php. Override the MySQL version with
12+
# MYSQL_TAG (default: mysql-8.4.10).
13+
#
14+
set -euo pipefail
15+
16+
package_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
17+
tools_dir="$package_dir/tools"
18+
build_dir="$package_dir/build"
19+
src_dir="$package_dir/src"
20+
21+
mkdir -p "$src_dir"
22+
23+
bash "$tools_dir/fetch-mysql-grammar.sh"
24+
bash "$tools_dir/run-bison.sh"
25+
26+
echo "Generating parse table ..."
27+
php "$tools_dir/generate-parse-table.php" "$build_dir/automaton.xml" "$src_dir/mysql-parse-table.php"
28+
29+
echo "Generating grammar tokens ..."
30+
php "$tools_dir/generate-tokens.php" "$build_dir/automaton.xml" "$build_dir/lex.h" "$src_dir/class-wp-mysql-lexer.php"
31+
32+
echo "Done."
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "wordpress/mysql-parser",
3+
"type": "library",
4+
"description": "A fast and complete MySQL parser with zero dependencies.",
5+
"license": "GPL-2.0-or-later",
6+
"require": {
7+
"php": ">=7.2"
8+
},
9+
"autoload": {
10+
"classmap": [
11+
"src/"
12+
]
13+
},
14+
"require-dev": {
15+
"phpunit/phpunit": "^8.5"
16+
},
17+
"scripts": {
18+
"test": "phpunit",
19+
"build-grammar": [
20+
"./bin/build-grammar"
21+
],
22+
"build-corpus": [
23+
"./bin/build-corpus"
24+
],
25+
"benchmark": [
26+
"@php tests/benchmark.php",
27+
"@php -d opcache.enable_cli=1 -d opcache.jit_buffer_size=64M -d opcache.jit=tracing tests/benchmark.php"
28+
]
29+
}
30+
}

0 commit comments

Comments
 (0)