From e2a6e8803c626bbe273d72b993c8c8eb935b5873 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Mon, 30 Nov 2026 13:52:47 +0000 Subject: [PATCH 1/3] fix(blocks): refuse a requested bind check when built without DuckDB Without the engine feature, the printed-SQL check passed every statement that passed its lexical checks, so a conversion that asked for the bind check got none and was not told. The Databricks, PostgreSQL and SQLite pipelines always ask for it. The check now fails at the engine stage, and the statement is refused naming the missing engine. The browser playground, which runs the SQL in DuckDB-WASM, now skips the check explicitly. make engine-free runs the checking tests without the engine, which no other build does. --- Makefile | 5 ++++- .../sqlbabel-blocks/src/checking/_helpers/binding.rs | 11 ++++++++--- crates/sqlbabel-blocks/src/checking/constants.rs | 3 +++ crates/sqlbabel-blocks/src/checking/tests/helpers.rs | 11 +++++++---- crates/sqlbabel-convert/Cargo.toml | 10 ++++++---- .../analysis/tests/test_analyze_function.rs | 12 +++++++++--- docs/architecture.md | 2 +- website/wasm/src/lib.rs | 2 ++ 8 files changed, 40 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index bd52944f1..5af31691c 100644 --- a/Makefile +++ b/Makefile @@ -131,12 +131,15 @@ evidence-pin-check: clutter-guard: uv run python -m scripts.check_clutter_guard -# Fails when the conversion pipeline crate, which builds without DuckDB's engine, reaches it. +# Fails when the conversion pipeline crate, which builds without DuckDB's engine, reaches it, or when +# the printed-SQL check built without the engine passes SQL it was asked to bind. The workspace's +# other builds always enable the engine, so only this run checks the check without it. engine-free: @tree="$$(cargo tree --locked -p sqlbabel-convert -e normal --prefix none)" || exit 1; \ if grep -E '^(duckdb|libduckdb-sys) ' <<<"$$tree"; then \ echo "sqlbabel-convert reaches DuckDB's engine" >&2; exit 1; \ fi + cargo test --locked -q -p sqlbabel-blocks --lib checking:: # Fails when DuckDB compiled into a distribution lacks its json extension, which the checking # engine's helper macros need: the sqlbabel wheel and the sqlbabel-cli wheel both bundle DuckDB. diff --git a/crates/sqlbabel-blocks/src/checking/_helpers/binding.rs b/crates/sqlbabel-blocks/src/checking/_helpers/binding.rs index fe1d2750d..69f98edd1 100644 --- a/crates/sqlbabel-blocks/src/checking/_helpers/binding.rs +++ b/crates/sqlbabel-blocks/src/checking/_helpers/binding.rs @@ -4,9 +4,10 @@ use crate::checking::_helpers::engine::with_engine; #[cfg(feature = "engine")] use crate::checking::_helpers::tables::{function_definitions, table_definitions}; +#[cfg(not(feature = "engine"))] +use crate::checking::constants::ENGINE_ABSENT; #[cfg(feature = "engine")] use crate::checking::constants::PARSER_ERROR; -#[cfg(feature = "engine")] use crate::checking::errors::CheckStage; use crate::checking::errors::PrintedSqlFailure; use crate::checking::types::{FunctionLookup, RelationLookup}; @@ -36,12 +37,16 @@ pub(crate) fn bound( }) } -/// Without the engine the caller binds the SQL itself, so only the lexical checks apply. +/// Without the engine nothing binds the SQL, so a request for the bind fails rather than passing +/// on the lexical checks alone. A caller that binds the SQL itself skips this check instead. #[cfg(not(feature = "engine"))] pub(crate) fn bound( _sql: &str, _names: &[String], _lookups: (RelationLookup<'_>, FunctionLookup<'_>), ) -> Result<(), PrintedSqlFailure> { - Ok(()) + Err(PrintedSqlFailure { + stage: CheckStage::Engine, + message: ENGINE_ABSENT.to_owned(), + }) } diff --git a/crates/sqlbabel-blocks/src/checking/constants.rs b/crates/sqlbabel-blocks/src/checking/constants.rs index 7dd49d505..1fec13164 100644 --- a/crates/sqlbabel-blocks/src/checking/constants.rs +++ b/crates/sqlbabel-blocks/src/checking/constants.rs @@ -12,3 +12,6 @@ pub const MACRO_PREFIX: &str = "CREATE OR REPLACE MACRO"; /// Turns off DuckDB's optimiser: the check parses and binds, and optimising long SQL is slow. pub const DISABLE_OPTIMIZER: &str = "PRAGMA disable_optimizer"; +/// Why a build without the engine refuses a requested bind: nothing in it can bind the SQL. +pub const ENGINE_ABSENT: &str = + "this build has no DuckDB engine to bind the SQL; skip the bind check and bind it yourself"; diff --git a/crates/sqlbabel-blocks/src/checking/tests/helpers.rs b/crates/sqlbabel-blocks/src/checking/tests/helpers.rs index d68477ab4..4b248dec6 100644 --- a/crates/sqlbabel-blocks/src/checking/tests/helpers.rs +++ b/crates/sqlbabel-blocks/src/checking/tests/helpers.rs @@ -34,11 +34,14 @@ pub(crate) fn failed_stage_over(sql: &str, relations: &[&str]) -> Option) -> Option { - stage.filter(|stage| { - cfg!(feature = "engine") || !matches!(stage, CheckStage::Parse | CheckStage::Bind) - }) + match stage { + _ if cfg!(feature = "engine") => stage, + None | Some(CheckStage::Parse | CheckStage::Bind) => Some(CheckStage::Engine), + lexical => lexical, + } } /// `depth` selects joined by `UNION ALL`, each pair in its own parentheses. diff --git a/crates/sqlbabel-convert/Cargo.toml b/crates/sqlbabel-convert/Cargo.toml index 7fab81fe0..f71aca1d4 100644 --- a/crates/sqlbabel-convert/Cargo.toml +++ b/crates/sqlbabel-convert/Cargo.toml @@ -9,8 +9,9 @@ homepage.workspace = true publish.workspace = true rust-version.workspace = true -# `engine`: DuckDB parses and binds every converted statement. Off, the converter skips that check, -# and the caller binds the SQL itself (the browser playground does, in DuckDB-WASM). +# `engine`: DuckDB parses and binds every converted statement. Off, nothing can bind it, so a +# conversion that asks for the bind check is refused; a caller that binds the SQL itself, as the +# browser playground does in DuckDB-WASM, skips the check. # `snowflake`: the Snowflake-to-DuckDB rules and pipeline. # `databricks`: the Databricks-to-DuckDB rules and pipeline. # `postgres`: the PostgreSQL-to-DuckDB rules and pipeline. @@ -40,8 +41,9 @@ sqlbabel-syntax.workspace = true sqlbabel-tokenize.workspace = true [dev-dependencies] -# The tests run the Snowflake, Databricks, PostgreSQL and SQLite rules and pipelines. -sqlbabel-convert = { workspace = true, features = ["databricks", "postgres", "snowflake", "sqlite"] } +# The tests run the Snowflake, Databricks, PostgreSQL and SQLite rules and pipelines, and bind what +# they emit, as the workspace's other crates make every build of them do. +sqlbabel-convert = { workspace = true, features = ["databricks", "engine", "postgres", "snowflake", "sqlite"] } [lints] workspace = true diff --git a/crates/sqlbabel-convert/src/snowflake/analysis/tests/test_analyze_function.rs b/crates/sqlbabel-convert/src/snowflake/analysis/tests/test_analyze_function.rs index 15e31bba9..cc9ce705e 100644 --- a/crates/sqlbabel-convert/src/snowflake/analysis/tests/test_analyze_function.rs +++ b/crates/sqlbabel-convert/src/snowflake/analysis/tests/test_analyze_function.rs @@ -17,8 +17,8 @@ use crate::snowflake::analysis::tests::test_types::{ }; #[test] -fn given_a_sql_function_body_when_converted_without_the_engine_then_it_is_a_macro_body_or_refused() -{ +fn given_a_sql_function_body_when_converted_without_the_bind_check_then_it_is_a_macro_body_or_refused() + { let amount = SnowflakeType::Number { precision: 10, scale: 2, @@ -56,7 +56,13 @@ fn given_a_sql_function_body_when_converted_without_the_engine_then_it_is_a_macr let outcome = analyze_function( (test_case.body, &spec), &builtin_catalogue(), - (&Schema::default(), &ConversionOptions::default()), + ( + &Schema::default(), + &ConversionOptions { + skip_bind_check: true, + ..ConversionOptions::default() + }, + ), ); let parameters = outcome .as_ref() diff --git a/docs/architecture.md b/docs/architecture.md index 3f4cf1e2d..ba5eeaa16 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -35,7 +35,7 @@ a parameter of every entry point; Snowflake is the only one implemented. | `sqlbabel-parse` | The one way the other crates reach a parser. The Snowflake parser's entry points are always there; each native parser is a module behind the feature of its dialect's name. | | `sqlbabel-diagnostics` | Diagnostics, the failure taxonomy, refusals, constructs and conversion provenance. Snowflake's error taxonomy and rejections sit in `src/snowflake/` behind the `snowflake` feature. | | `sqlbabel-semantics` | SQL semantics apart from conversion: dialect profiles (Snowflake and DuckDB), relation and column name resolution in query scopes, column types with the marks VARIANT values carry, and lineage (each output's value sources, plus the query's `row_sources`: the columns filters, join keys, groups, CONNECT BY, PIVOT FOR, ORDER BY under a limit and table-function inputs decide rows by, with `rows_resolved` false when one of those could not be traced). Snowflake's typing layer is in `src/snowflake/`: polyglot leaves many Snowflake expressions untyped, so exact types (`NUMBER(p, s)`, timestamp kinds, semi-structured values) are computed there. It builds without the conversion, DuckDB and evidence crates. | -| `sqlbabel-convert` | Conversion to DuckDB: the rule catalogue, the rules with their type conditions and declared refusals (`src/snowflake/`), and the pipeline itself, `analyze`: parse, classify, convert each statement or refuse it. It builds without DuckDB's engine, so the bind check in the last step is skipped where it is linked alone; every crate that enables the engine gets the check, and `make engine-free` fails when this crate reaches DuckDB. | +| `sqlbabel-convert` | Conversion to DuckDB: the rule catalogue, the rules with their type conditions and declared refusals (`src/snowflake/`), and the pipeline itself, `analyze`: parse, classify, convert each statement or refuse it. It builds without DuckDB's engine. Linked alone, it refuses a statement whose bind check is asked for, since nothing can bind it, and a caller that binds the SQL itself, such as the browser playground, skips the check. Every crate that enables the engine gets the check, and `make engine-free` fails when this crate reaches DuckDB or when the check built without the engine passes SQL. | | `sqlbabel-blocks` | DuckDB building blocks shared by rules, and, behind the `engine` feature, DuckDB's own parse-and-bind check of every converted statement. | | `sqlbabel-policy` | The policy file `sqlbabel.toml`: schema, validation and type-pattern matching. | | `sqlbabel-format` | The SQL formatter: lays SQL out over lines, changing only the whitespace between tokens and keyword case. A layout guide prints the authored tokens as a parse-tree pretty printer would; the output is refused unless it keeps every token, keeps comments attached and is idempotent. It reaches parsers through `sqlbabel-parse`: for SQL Server the Transact-SQL scanner supplies the tokens and its parser validates the input; for BigQuery the GoogleSQL scanner does the same, as the Databricks scanner does for Databricks; and for PostgreSQL the native scanner decides keywords and its parser validates the input. | diff --git a/website/wasm/src/lib.rs b/website/wasm/src/lib.rs index 9fe719d5c..76fc5c623 100644 --- a/website/wasm/src/lib.rs +++ b/website/wasm/src/lib.rs @@ -363,8 +363,10 @@ fn stages(sql: &str, catalog: &Catalog, settings: &str) -> Value { return json!({ "parse": parse, "checks": checks, "translate": translate }); } }; + // The page runs the SQL in DuckDB-WASM and reports its errors, and this build has no engine. let options = ConversionOptions { quoted_exclusions: quoted_exclusions(sql), + skip_bind_check: true, ..options }; let mut refusals = Vec::new(); From aeb906cebf16b31a9b1939f8da037c5196a9afc1 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Mon, 30 Nov 2026 14:00:00 +0000 Subject: [PATCH 2/3] fix(blocks): fail engine-free when no checking test runs, and drop skip advice The engine-free target now fails if its filter matches no test. The refusal message no longer tells the reader to skip the bind check, which the Databricks, PostgreSQL and SQLite pipelines do not offer. --- Makefile | 4 +++- crates/sqlbabel-blocks/src/checking/constants.rs | 3 +-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 5af31691c..7f3fd095f 100644 --- a/Makefile +++ b/Makefile @@ -139,7 +139,9 @@ engine-free: if grep -E '^(duckdb|libduckdb-sys) ' <<<"$$tree"; then \ echo "sqlbabel-convert reaches DuckDB's engine" >&2; exit 1; \ fi - cargo test --locked -q -p sqlbabel-blocks --lib checking:: + @out="$$(cargo test --locked -q -p sqlbabel-blocks --lib checking:: 2>&1)"; status=$$?; \ + echo "$$out"; [ "$$status" -eq 0 ] || exit 1; \ + grep -q '^running [1-9]' <<<"$$out" || { echo "no checking test ran without the engine" >&2; exit 1; } # Fails when DuckDB compiled into a distribution lacks its json extension, which the checking # engine's helper macros need: the sqlbabel wheel and the sqlbabel-cli wheel both bundle DuckDB. diff --git a/crates/sqlbabel-blocks/src/checking/constants.rs b/crates/sqlbabel-blocks/src/checking/constants.rs index 1fec13164..7ac316071 100644 --- a/crates/sqlbabel-blocks/src/checking/constants.rs +++ b/crates/sqlbabel-blocks/src/checking/constants.rs @@ -13,5 +13,4 @@ pub const MACRO_PREFIX: &str = "CREATE OR REPLACE MACRO"; /// Turns off DuckDB's optimiser: the check parses and binds, and optimising long SQL is slow. pub const DISABLE_OPTIMIZER: &str = "PRAGMA disable_optimizer"; /// Why a build without the engine refuses a requested bind: nothing in it can bind the SQL. -pub const ENGINE_ABSENT: &str = - "this build has no DuckDB engine to bind the SQL; skip the bind check and bind it yourself"; +pub const ENGINE_ABSENT: &str = "this build has no DuckDB engine, so nothing can bind the SQL"; From 9b5a9ceeb64d719fc1b31484c586aba2b72d28b2 Mon Sep 17 00:00:00 2001 From: Kevin Longe <273747162+kvlonge@users.noreply.github.com> Date: Mon, 30 Nov 2026 14:15:05 +0000 Subject: [PATCH 3/3] fix(blocks): keep the engine-free stage helper branch-free Split the test helper by feature instead of matching on the stage, and keep each doc comment to one line, as the layout rules require. --- .../src/checking/_helpers/binding.rs | 3 +-- .../src/checking/tests/helpers.rs | 23 +++++++++++++------ 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/crates/sqlbabel-blocks/src/checking/_helpers/binding.rs b/crates/sqlbabel-blocks/src/checking/_helpers/binding.rs index 69f98edd1..085094b9c 100644 --- a/crates/sqlbabel-blocks/src/checking/_helpers/binding.rs +++ b/crates/sqlbabel-blocks/src/checking/_helpers/binding.rs @@ -37,8 +37,7 @@ pub(crate) fn bound( }) } -/// Without the engine nothing binds the SQL, so a request for the bind fails rather than passing -/// on the lexical checks alone. A caller that binds the SQL itself skips this check instead. +/// Without the engine nothing can bind the SQL, so a requested bind fails instead of passing. #[cfg(not(feature = "engine"))] pub(crate) fn bound( _sql: &str, diff --git a/crates/sqlbabel-blocks/src/checking/tests/helpers.rs b/crates/sqlbabel-blocks/src/checking/tests/helpers.rs index 4b248dec6..7ca7f8460 100644 --- a/crates/sqlbabel-blocks/src/checking/tests/helpers.rs +++ b/crates/sqlbabel-blocks/src/checking/tests/helpers.rs @@ -34,14 +34,23 @@ pub(crate) fn failed_stage_over(sql: &str, relations: &[&str]) -> Option) -> Option { - match stage { - _ if cfg!(feature = "engine") => stage, - None | Some(CheckStage::Parse | CheckStage::Bind) => Some(CheckStage::Engine), - lexical => lexical, - } + stage +} + +/// `stage` as a build without the engine reports it: a lexical failure, or else the engine's. +#[cfg(not(feature = "engine"))] +pub(crate) fn built_stage(stage: Option) -> Option { + stage + .filter(|stage| { + matches!( + stage, + CheckStage::StatementCount | CheckStage::CapturedParameter + ) + }) + .or(Some(CheckStage::Engine)) } /// `depth` selects joined by `UNION ALL`, each pair in its own parentheses.