Skip to content

Reference

Python

Check and convert warehouse SQL (Snowflake today) from Python with the sqlbabel package.

The sqlbabel Python package runs the same code as the command line. check and convert return frozen results with typed diagnostics, the text the command line renders, and, for convert, its exact --json report.

import sqlbabel
sqlbabel.check(sql="SELECT 1 QUALIFY TRUE").diagnostics[0].code # "SBL0203"
result = sqlbabel.convert(
sql="SELECT 1 AS total", options=sqlbabel.ConvertOptions(no_config=True)
)
result.sql # 'SELECT CAST(1 AS DECIMAL(1, 0)) AS "TOTAL"'

The package requires Python 3.12 or newer. One wheel loads into every CPython from 3.12, and it ships DuckDB’s library beside itself. On Linux it needs glibc 2.34 or newer. An older interpreter rejects the wheel as “not a supported wheel on this platform”, so install it into a 3.12 environment:

pip install sqlbabel
python -c "import sqlbabel; print(sqlbabel.__version__)"

The wheel also builds from the repository with uv:

uv sync --locked --no-group recorder
make python-wheel
uv venv --python 3.12 .sqlbabel-env
uv pip install --python .sqlbabel-env target/wheels/sqlbabel-*.whl
.sqlbabel-env/bin/python -c "import sqlbabel; print(sqlbabel.__version__)"

With an existing Python 3.12 or newer, pip install target/wheels/sqlbabel-*.whl works too.

check(*, sql, path="<string>", max_call_depth=None) parses sql and finds what Snowflake rejects. path names the SQL in rendered diagnostics. max_call_depth is --max-call-depth: function calls may nest that deep, 256 by default and at most 512. It returns a CheckResult:

Field Type Meaning
ok bool True when nothing was found.
diagnostics tuple[Diagnostic, ...] Every problem found.
rendered str The diagnostics as the command line prints them.

convert(*, sql, options=None, path="<string>") converts sql or refuses it, as sqlbabel convert does. It returns a Conversion:

Field Type Meaning
converted bool Whether every statement converted.
sql str The DuckDB SQL.
macros tuple[str, ...] Statements creating the DuckDB macros the SQL needs, helper macros first, to run in order first.
profile str The session profile it converted under.
diagnostics tuple[Diagnostic, ...] The refusals.
divergences Mapping[str, tuple[str, ...]] Known differences relied on, with their constructs.
assumptions tuple[str, ...] The known differences among them that are assumptions.
divergence_failures Mapping[str, str] How each known difference relied on fails: value (it can return another value) or loud (it raises instead).
notes tuple[str, ...] Notes the command line prints.
rendered str The refusals as the command line prints them.
report Mapping[str, Any] Exactly sqlbabel convert --json’s report.

ConvertOptions has one field per convert flag, with the same defaults: project, config, no_config, session, strict, python_runtime, overrides, verified_overrides, compact, max_call_depth, bind_check and dialect. project, config and overrides take a path (str or os.PathLike), not the parsed contents. session and verified_overrides take a mapping, such as session={"TIMEZONE": "UTC"}, not a list of NAME=VALUE strings.

Two fields have no flag, for a tool that keeps SQLBabel’s settings in its own configuration:

  • allow allows (True) or refuses (False) known differences, as [allow] in sqlbabel.toml does, over the configuration’s own [allow]. A name that is no known difference raises SqlBabelError, and so does project_udf, which python_runtime allows instead.
  • allow_location names where the tool keeps those allowances, such as "[testing.local] allow in project.toml". Help and the report’s allowed_by name it instead of the configuration file.
options = sqlbabel.ConvertOptions(
no_config=True,
allow={"hash": True},
allow_location="[testing.local] allow in project.toml",
)

bind_check=False is --no-bind-check: conversion skips DuckDB’s parse and bind of its output, for callers that run the SQL themselves and handle DuckDB’s errors. It defaults to True.

schema declares tables in memory, beside the project’s, as a mapping of table names to [column, Snowflake type] pairs:

options = sqlbabel.ConvertOptions(schema={"ORDERS": [["ORDER_ID", "NUMBER(38,0)"]]})

project takes a project folder, as --project does: its sources, functions, overrides and settings apply, and its sqlbabel.toml is read unless config or no_config choose otherwise. Without project, the current folder is the project, as it is on the command line.

Diagnostic has code, name, alias, severity, message, span, label, notes and help, as in the JSON output. span is a Span with start, end, line, column, end_line and end_column, or None for problems outside the SQL.

Where the command line exits 2, the functions raise SqlBabelError, a ValueError: for example, for input that holds no SQL statement, a schema that is not a valid mapping, or a max_call_depth above 512.