Skip to content

CLI commands

sqlbabel convert

Convert Snowflake SQL into DuckDB SQL, or refuse it with a reason.

sqlbabel convert prints the DuckDB SQL for Snowflake SQL, or refuses it. Every statement it prints has passed DuckDB’s own parse-and-bind check.

sqlbabel convert [OPTIONS] [PATH]
sqlbabel convert query.sql --no-config
echo "SELECT 1 AS total" | sqlbabel convert --no-config --json

PATH is a SQL file. Without one, or with -, convert reads standard input. The tables the SQL reads come from the project in the current folder. The DuckDB SQL goes to standard output, laid out with each clause on its own line, select items one per line, and nested queries and long CASE expressions indented; short statements stay on one line. Progress, the known differences and assumptions it relies on, and refusals go to standard error.

$ sqlbabel convert revenue.sql --no-config
SELECT
"ORDERS"."CUSTOMER_ID" AS "CUSTOMER_ID",
CAST(sum("ORDERS"."TOTAL") AS DECIMAL(24, 2)) AS "REVENUE"
FROM "ORDERS"
WHERE ("ORDERS"."STATUS" = 'shipped')
GROUP BY "ORDERS"."CUSTOMER_ID"
sqlbabel convert: done, converted

--dialect databricks reads Databricks SQL instead. Databricks support is partial: convert converts only what recorded Databricks results prove and refuses the rest with a coded diagnostic. It reads no project, and --project, --session, --overrides, --verified-override and --python-runtime exit 2.

$ echo "SELECT abs(-7) AS total" | sqlbabel convert --dialect databricks
SELECT abs(-7) AS "total"
sqlbabel convert: done, converted
$ echo "SELECT sin(1.0) AS total" | sqlbabel convert --dialect databricks
error[SBL0002]: SQLBabel can't convert SIN yet
--> <stdin>
|
= note: no rule converts the function SIN
sqlbabel convert: refused, 1 refusal

A refused statement prints its refusals instead, each as a coded diagnostic, and convert exits 1. Each refusal is one of these:

Code Why
SBL0002 SQLBabel can’t convert this yet.
SBL0003 SQLBabel can’t convert this for these types.
SBL0004 SQLBabel doesn’t know this name: the project’s sources don’t declare it.
SBL0005 DuckDB can’t do what Snowflake does here.
SBL0006 Converting this took too long or produced too much SQL.
SBL0007 SQLBabel can’t convert for these session settings.
SBL0008 This needs a known difference that isn’t allowed.
SBL0009 This column needs an alias, because SQLBabel can’t be sure of the name Snowflake gives it.

SQL that does not parse, or that Snowflake rejects, is refused with the same codes as check. A VALUES list of more than 1,000 rows is refused.

Option Meaning
--dialect snowflake|databricks The SQL’s dialect. Default snowflake. See Converting Databricks SQL.
--project <folder> The project declaring the tables and functions the SQL reads. Default the current folder.
--session NAME=VALUE A Snowflake session parameter of your session, replacing the project’s. Repeatable. See Sessions.
--overrides <path> Replacements: a FUNCTION() file, or a directory of them, each with replaces and dialect duckdb. Replaces the project’s overrides/ folder.
--verified-override NAME=SHA256 An override verified for the body with that hash, as convert --json reports.
--compact Print each statement on one line instead, in the JSON report too.
--no-bind-check Skip DuckDB’s parse and bind of the converted SQL, often the larger part of the conversion time, for callers that run the SQL and handle DuckDB’s errors. The check only catches SQLBabel bugs, converted SQL that DuckDB cannot bind; without it, such SQL is reported as converted and fails when run. The other checks and refusals are unchanged.
--max-call-depth <n> Refuse function calls nested more than n deep, as for check. Default 256, at most 512. run, function and census take it too.
--format text|json, --json Print the report as text or JSON.
--color auto|always|never Highlight the SQL and colour diagnostics, as for check. JSON is never coloured.

convert also takes the configuration flags: --config, --no-config, --strict and --python-runtime. See Refuse, allow or replace.

With --json, convert prints one object:

{
"converted": true,
"profile": "utc-v1",
"sql": "SELECT CAST(1 AS DECIMAL(1, 0)) AS \"TOTAL\"",
"refusals": [],
"divergences": {},
"assumptions": [],
"divergence_failures": {},
"known_differences": [],
"notes": [],
"warnings": [],
"python_functions": [],
"macros": [],
"overrides": []
}

sql is laid out as the text output prints it, or on one line with --compact. macros lists the statements to run first, in order: those creating the helper macros the SQL calls, then your override macros. The text output prints them ahead of the SQL. A refusal in refusals carries the same diagnostic fields.

Code Meaning
0 Converted.
1 Something was refused; the reasons are on standard error.
2 It could not run: bad arguments, an unreadable file, a missing --config file or an invalid sqlbabel.toml, or input without SQL.