taozhi8833998 / node-sql-parser

Parse simple SQL statements into an abstract syntax tree (AST) with the visited tableList and convert it back to SQL
https://taozhi8833998.github.io/node-sql-parser/
Apache License 2.0
823 stars 184 forks source link

PegJS : MOD operator not supported for MySQL / MariaDB #2146

Closed TonyGravagno closed 1 month ago

TonyGravagno commented 1 month ago
`Int11` int(11) DEFAULT NULL CHECK (`Int11` MOD 5 = 0),

[SyntaxError]: Expected "!=", "#", ... , or [ \t\n\r] but "M" found.

That seems to be tripping on the MOD operator.

NSP 5.3.2 Node 22

MOD and % are synonym operators - but only in MySQL/MariaDB, not in most other databases. I checked documentation for PostgreSQL and SQLite and it doesn't look like they support the MOD synonym. This "seems" to be an issue that is limited in scope.

Both "%" and "MOD" are in a comment in mysql.pegjs but MOD is not in the list of processed operators.

https://github.com/taozhi8833998/node-sql-parser/blob/feca67f1f086cd0c99b8a8f80fca1786fabab9fe/pegjs/mysql.pegjs#L2856

I believe adding it here will fix the issue for MySQL (similar for MariaDB).

https://github.com/taozhi8833998/node-sql-parser/blob/feca67f1f086cd0c99b8a8f80fca1786fabab9fe/pegjs/mysql.pegjs#L3058

I made an attempt to fix the issue and have submitted a PR.