Sqloxide

0.61.1 · active · verified Mon Apr 13

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.

Warnings

Install

Imports

Quickstart

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.

from sqloxide import parse_sql

sql_query = """
SELECT
    employee.first_name,
    employee.last_name,
    call.start_time,
    call.end_time,
    call_outcome.outcome_text
FROM
    employee
INNER JOIN call ON call.employee_id = employee.id
INNER JOIN call_outcome ON call.call_outcome_id = call_outcome.id
ORDER BY
    call.start_time ASC;
"""

# Parse the SQL query, specifying a dialect (e.g., 'ansi')
ast_output = parse_sql(sql=sql_query, dialect='ansi')

# The output is a Python object representing the AST
# print(ast_output) # Uncomment to see the full AST

# Example of accessing parts of the AST (structure depends on SQL and sqlparser-rs version)
if ast_output and isinstance(ast_output, list) and 'Query' in ast_output[0]:
    query_body = ast_output[0]['Query']['body']
    if 'Select' in query_body:
        projection_items = query_body['Select']['projection']
        print(f"Number of projected columns: {len(projection_items)}")
        print(f"First projected item: {projection_items[0]}")

view raw JSON →