|
| 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 | +``` |
0 commit comments