Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 7 additions & 3 deletions crates/sqlbabel-blocks/src/checking/_helpers/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(),
})
}
2 changes: 2 additions & 0 deletions crates/sqlbabel-blocks/src/checking/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
20 changes: 16 additions & 4 deletions crates/sqlbabel-blocks/src/checking/tests/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,23 @@ pub(crate) fn failed_stage_over(sql: &str, relations: &[&str]) -> Option<CheckSt
.map(|failure| failure.stage)
}

/// `stage` as this build reports it: without the engine, DuckDB neither parses nor binds the SQL.
/// `stage` as a build with the engine reports it: unchanged.
#[cfg(feature = "engine")]
pub(crate) fn built_stage(stage: Option<CheckStage>) -> Option<CheckStage> {
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<CheckStage>) -> Option<CheckStage> {
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.
Expand Down
10 changes: 6 additions & 4 deletions crates/sqlbabel-convert/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. |
Expand Down
2 changes: 2 additions & 0 deletions website/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading