Registry / data / mo-sql-parsing

mo-sql-parsing

JSON →
library11.697.25301pypypi✓ verified 85d ago

mo-sql-parsing is a Python library designed to parse SQL queries into a JSON-izable parse tree. It aims to convert various SQL dialects, initially targeting MySQL, into a structured JSON format, making it easier to translate or analyze SQL for different datastores. As of version 11.697.25301, the library is actively maintained, with ongoing issue resolution and regular updates, as indicated by its high version number and recent activity on GitHub (October 2024 Project Status).

pip install mo-sql-parsing
INSTALL
IMPORT
SIG · MO-SQL-PARSING
M
mo-sql-parsing
datapythonv11.697.25301
Install
1.8s avg
Import
115ms
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v11.697.25301 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.117s · 18.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.8s · import 0.113s · 19MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
from mo_sql_parsing import parse
from moz_sql_parser import parse
The 'moz_sql_parser' library is deprecated; 'mo-sql-parsing' is an actively maintained fork.
parse_sqlserver
from mo_sql_parsing import parse_sqlserver
Use this for SQLServer-specific syntax, especially identifier handling with square brackets '[]'.
parse_mysql
from mo_sql_parsing import parse_mysql
Use this for MySQL-specific syntax, such as handling double-quotes for literal strings.

The `mo_sql_parsing` library's primary function is `parse`, which takes a SQL string and returns a JSON-like Python dictionary representing the parse tree. Specialized parsers like `parse_sqlserver` and `parse_mysql` are available for dialect-specific syntax.

from mo_sql_parsing import parse # Basic SELECT query sql_query = "select count(1) from jobs" parsed_json = parse(sql_query) print(f"Parsed basic query: {parsed_json}") # Expected: {'select': {'value': {'count': 1}}, 'from': 'jobs'} # SELECT with aliases sql_query_aliases = "select a as hello, b as world from jobs" parsed_json_aliases = parse(sql_query_aliases) print(f"Parsed query with aliases: {parsed_json_aliases}") # Expected: {'select': [{'value': 'a', 'name': 'hello'}, {'value': 'b', 'name': 'world'}], 'from': 'jobs'} # Example with SQLServer-specific parsing from mo_sql_parsing import parse_sqlserver sqlserver_query = "SELECT [Timestamp] FROM [table]" parsed_sqlserver = parse_sqlserver(sqlserver_query) print(f"Parsed SQLServer query: {parsed_sqlserver}") # Expected: {'select': 'Timestamp', 'from': 'table'}
Debug
Known issues
breakingThe `mo-sql-parsing` library is a fork of the now-archived `moz-sql-parser`. Users migrating from `moz-sql-parser` must update their package installations and import paths (`moz_sql_parser` to `mo_sql_parsing`).
fix
Uninstall `moz-sql-parser` and install `mo-sql-parsing`. Update import statements: `from mo_sql_parsing import parse`.
affects: All versions (migration from `moz-sql-parser` to `mo-sql-parsing`)
gotchaSquare brackets `[]` have different meanings in SQL dialects (e.g., identifiers in SQLServer, array constructors in BigQuery). Using the default `parse` function for SQLServer queries might lead to incorrect interpretations.
fix
For SQLServer syntax, explicitly use `from mo_sql_parsing import parse_sqlserver as parse` to ensure correct parsing of identifiers.
affects: All versions
gotchaBy default, SQL `NULL` values are parsed into `{'null':{}}` in the output JSON tree, not Python's `None`.
fix
If Python's `None` is desired for `NULL` values, pass the `null=None` argument to the `parse` function: `parse(sql_query, null=None)`.
affects: All versions
gotchaThis library relies on `mo-parsing` (a fork of `pyparsing`) internally. There are behavioral differences between `mo-parsing` and upstream `pyparsing`, notably how `add_parse_action()` creates new `ParserElement` objects that must be assigned, and `ParserElements` being static. Direct application of `pyparsing` idioms may not work.
fix
Consult the `mo-parsing` documentation for specific differences, especially regarding how parser actions modify grammar elements. For `add_parse_action()`, ensure the result is assigned back to the variable.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'moz_sql_parser'
The old `moz-sql-parser` library is not installed or the import path is incorrect. `mo-sql-parsing` is its successor.
fix
Install `mo-sql-parsing` (`pip install mo-sql-parsing`) and update import statements to `from mo_sql_parsing import parse`.
Unexpected JSON output for SQL 'NULL' as {'null': {}} instead of None.
The default behavior of `mo-sql-parsing` is to represent SQL `NULL` as a dictionary `{'null':{}}` in the parse tree.
fix
To get Python's `None` for SQL `NULL`, call the `parse` function with the `null` argument: `result = parse(sql, null=None)`.
SQL queries with square brackets like `SELECT [col] FROM [tbl]` are not parsed correctly; they might be interpreted as array literals.
The default parser often assumes a BigQuery-like dialect where `[]` denotes array constructors. This conflicts with SQLServer's use of `[]` for identifiers.
fix
Import and use the `parse_sqlserver` function explicitly for SQLServer queries: `from mo_sql_parsing import parse_sqlserver as parse`.
Upgrade
Version history
11.697.25301latest on PyPI · released Oct 28, 2025
Audit
Dependencies
mo-dotsrequiredCore utility dependency
mo-futurerequiredCore utility dependency
mo-parsingrequiredForked pyparsing for faster parsing, fundamental for SQL grammar definition
mo-importsrequiredCore utility dependency
Agent activity
2 hits · last 30 days
node
2
Resources
mo-sql-parsing — pip install mo-sql-parsing · libregistry