Sqloxide provides Python bindings for the high-performance `sqlparser-rs` Rust library. It enables fast, efficient, and accurate parsing of SQL queries into a structured Abstract Syntax Tree (AST) in Python, making it suitable for tasks like building data lineage graphs, especially across complex or auto-generated SQL codebases that include deeply nested queries, sub-selects, and table aliases. The library is currently at version 0.61.1 and its minor version now tracks the underlying `sqlparser-rs` library's minor version, indicating a responsive release cadence.
pip install sqloxideVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse a SQL query using `sqloxide.parse_sql`. The function returns a Python object which is a typed Abstract Syntax Tree (AST) that mirrors the `sqlparser-rs` AST schema. You can specify different SQL dialects (e.g., 'ansi', 'mysql', 'postgres', 'sqlite', 'snowflake', 'bigquery', 'hive', 'generic') for accurate parsing.
Upgrade your Python environment to 3.9 or higher.
If your code relies on directly inspecting or manipulating the AST structure for these statement types, you will need to update your logic to match the new JSON/Python object shape. Refer to the `sqlparser-rs` 0.57-0.61 release notes for specific structural changes.
Be aware that minor version bumps in sqloxide may now correspond to significant updates and potential breaking changes in the underlying `sqlparser-rs` library. Review `sqlparser-rs` changelogs in addition to sqloxide's for detailed impact analysis during upgrades.
Upgrade to sqloxide v0.61.1 or later to ensure proper exception propagation from mutation callbacks. If on an older version, add robust error checking within and around your mutation callbacks.
pip install sqloxide
import sqloxide
ast = sqloxide.parse_sql_to_ast("SELECT 1 FROM my_table")Review and correct the SQL query string to ensure it is valid, for example: `sqloxide.parse_sql_to_ast("SELECT column_name FROM my_table")`Iterate through the returned list or access elements by index, and then access dictionary keys using `['key']` notation. For example:
import sqloxide
ast = sqloxide.parse_sql_to_ast("SELECT 1; SELECT 2;")
for statement_dict in ast:
print(statement_dict.keys())
# Or for a single statement:
single_statement_ast = sqloxide.parse_sql_to_ast("SELECT 1")
first_statement = single_statement_ast[0]
print(first_statement.get('Select'))No dependency data recorded yet.