diff --git a/Makefile b/Makefile index bd52944f1..7f3fd095f 100644 --- a/Makefile +++ b/Makefile @@ -131,12 +131,17 @@ 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 + @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/_helpers/binding.rs b/crates/sqlbabel-blocks/src/checking/_helpers/binding.rs index fe1d2750d..085094b9c 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,15 @@ pub(crate) fn bound( }) } -/// Without the engine the caller binds the SQL itself, so only the lexical checks apply. +/// 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, _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..7ac316071 100644 --- a/crates/sqlbabel-blocks/src/checking/constants.rs +++ b/crates/sqlbabel-blocks/src/checking/constants.rs @@ -12,3 +12,5 @@ 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, so nothing can bind the SQL"; diff --git a/crates/sqlbabel-blocks/src/checking/tests/helpers.rs b/crates/sqlbabel-blocks/src/checking/tests/helpers.rs index d68477ab4..7ca7f8460 100644 --- a/crates/sqlbabel-blocks/src/checking/tests/helpers.rs +++ b/crates/sqlbabel-blocks/src/checking/tests/helpers.rs @@ -34,11 +34,23 @@ pub(crate) fn failed_stage_over(sql: &str, relations: &[&str]) -> Option) -> Option { - stage.filter(|stage| { - cfg!(feature = "engine") || !matches!(stage, CheckStage::Parse | CheckStage::Bind) - }) + 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. 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();