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
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,22 @@ pub(crate) struct SqlTestPatterns {
pub(crate) identifier: Regex,
}

/// The SQL-test patterns, compiled once; each scenario and test clones them with its syntax.
static COMPILED_PATTERNS: OnceLock<Result<SqlTestPatterns, String>> = OnceLock::new();

impl SqlTestPatterns {
pub(crate) fn new(lexical: LexicalSyntax) -> Result<Self, String> {
let compiled: &Self = COMPILED_PATTERNS
.get_or_init(|| Self::compiled(LexicalSyntax::default()))
.as_ref()
.map_err(Clone::clone)?;
Ok(Self {
lexical,
..compiled.clone()
})
}

fn compiled(lexical: LexicalSyntax) -> Result<Self, String> {
Ok(Self {
reference: compile_pattern(r#"(?i)__ref\(\"([^\"]+)\"\)"#)?,
source: compile_pattern(r#"(?i)__source\(\"([^\"]+)\"\)"#)?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! A call's argument list: split, bound to parameters and checked against their types.

use std::cell::RefCell;
use std::collections::HashMap;

use crate::sql_macros::_helpers::parsing::header::{split_top_level, unquoted};
use crate::sql_macros::_helpers::parsing::lexing::{Token, TokenKind, closing_token, tokens};
use crate::sql_macros::_helpers::rendering::slot_mappings::slot_mappings;
Expand Down Expand Up @@ -531,8 +534,34 @@ pub(crate) fn is_atomic(sql: &str) -> bool {
false
}

thread_local! {
/// Each declared type's family by adapter, since normalizing a type parses it.
static FAMILIES: RefCell<HashMap<String, HashMap<String, TypeFamily>>> =
RefCell::new(HashMap::new());
}

/// The family of a declared type, normalized as schema declaration types are for the adapter.
pub(crate) fn type_family(data_type: &str, adapter: &str) -> TypeFamily {
FAMILIES.with(|families| {
if let Some(family) = families
.borrow()
.get(adapter)
.and_then(|by_type| by_type.get(data_type))
{
return *family;
}
let family: TypeFamily = normalized_family(data_type, adapter);
families
.borrow_mut()
.entry(adapter.to_owned())
.or_default()
.insert(data_type.to_owned(), family);
family
})
}

/// The family `normalize_type` gives a declared type for the adapter's dialect.
fn normalized_family(data_type: &str, adapter: &str) -> TypeFamily {
use crate::type_system::models::TypeFamily as Normalized;
let dialect: &str = adapter_dialect(adapter);
let normalized = normalize_type(data_type, dialect)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ use crate::type_system::models::TypeNormalization;

/// Each body's type, by the probe that typed it, so a macro is typed once per process.
static BODY_TYPES: OnceLock<Mutex<HashMap<String, BodyType>>> = OnceLock::new();
/// `returns` verdicts by dialect, adapter, definition and body, so each is probed once per process.
static RETURNS_MISMATCHES: OnceLock<Mutex<HashMap<String, Option<String>>>> = OnceLock::new();
/// Each body's first type error, by the probe that found it.
static TYPE_ERRORS: OnceLock<Mutex<HashMap<String, Option<String>>>> = OnceLock::new();

Expand Down Expand Up @@ -159,27 +161,19 @@ fn remembered<T: Clone>(
verdict
}

/// Rejects an expression macro whose body's known type is not its declared `returns`.
pub(crate) fn check_body_returns(
/// The body's known type when it is not `returns`, typed over stand-in arguments.
fn returns_mismatch(
(parser, definition): (&dyn NativeDialect, &SqlMacroDefinition),
body: &ExpandedText,
(adapter, location): (&str, SqlMacroLocation),
) -> Result<(), SqlMacroError> {
if definition.form != SqlMacroForm::Expression {
return Ok(());
}
let Some(returns) = definition.returns.as_deref() else {
return Ok(());
};
let Some((probe, table)) = body_probe((parser, definition), body, false) else {
return Ok(());
};
(returns, adapter): (&str, &str),
) -> Option<String> {
let (probe, table) = body_probe((parser, definition), body, false)?;
let key: String = format!("returns\n{}\n{table:?}\n{probe}", parser.name());
let body_type: BodyType = remembered(&BODY_TYPES, key, || {
inferred_body_type(parser, &probe, &table)
});
let (shown, wrong): (String, bool) = match body_type {
BodyType::Unknown => return Ok(()),
BodyType::Unknown => return None,
BodyType::Known(inferred) if inferred.eq_ignore_ascii_case(INFERRED_TEXT_TYPE) => (
VARCHAR_TYPE.to_owned(),
disagrees(returns, VARCHAR_TYPE, adapter),
Expand All @@ -189,9 +183,28 @@ pub(crate) fn check_body_returns(
(inferred, wrong)
}
};
if !wrong {
wrong.then_some(shown)
}

/// Rejects an expression macro whose body's known type is not its declared `returns`.
pub(crate) fn check_body_returns(
(parser, definition): (&dyn NativeDialect, &SqlMacroDefinition),
body: &ExpandedText,
(adapter, location): (&str, SqlMacroLocation),
) -> Result<(), SqlMacroError> {
if definition.form != SqlMacroForm::Expression {
return Ok(());
}
let Some(returns) = definition.returns.as_deref() else {
return Ok(());
};
let key: String = format!("{}\n{adapter}\n{definition:?}\n{body:?}", parser.name());
let mismatch: Option<String> = remembered(&RETURNS_MISMATCHES, key, || {
returns_mismatch((parser, definition), body, (returns, adapter))
});
let Some(shown) = mismatch else {
return Ok(());
};
Err(SqlMacroError::new(
RETURNS_MISMATCH_CODE,
format!(
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use crate::sql_macros::_helpers::parsing::lexing::{Token, tokens};
use crate::sql_macros::constants::INVALID_DEFINITION_CODE;
use crate::sql_macros::errors::ExpansionFailure;
use crate::sql_macros::errors::SqlMacroError;
use crate::sql_macros::models::{ExpandedText, InsertedNames, SqlMacroCall, TextContext};
use crate::sql_macros::types::SqlMacroHost;

/// Each outer SQL macro call's expansion in order, or the index of the call that failed and why.
pub fn expand_sql_macro_calls<H: SqlMacroHost>(
host: &H,
context: &TextContext<'_>,
calls: &[&SqlMacroCall],
) -> Result<Vec<ExpandedText>, (usize, ExpansionFailure<H::Error>)> {
if calls.is_empty() {
return Ok(Vec::new());
}
let scanned: Vec<Token> = tokens(context.text).map_err(|_| {
(
0,
ExpansionFailure::from(SqlMacroError::new(
INVALID_DEFINITION_CODE,
"SQL has an unclosed quote or comment",
)),
)
})?;
let mut inserted: InsertedNames = InsertedNames::default();
let mut texts: Vec<ExpandedText> = Vec::with_capacity(calls.len());
for (index, call) in calls.iter().enumerate() {
let (text, updated) = crate::sql_macros::_helpers::rendering::expansion::expand_call(
host,
context,
(&scanned, inserted),
call,
)
.map_err(|failure| (index, failure))?;
texts.push(text);
inserted = updated;
}
Ok(texts)
}
2 changes: 1 addition & 1 deletion crates/sqlbuild-analysis/src/sql_macros/main/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod bare_macro_calls;
pub mod byte_text_splices;
pub mod check_sql_macro_arguments;
pub mod declaration_visible_from_macro;
pub mod expand_sql_macro_call;
pub mod expand_sql_macro_calls;
pub mod marker_free_text;
pub mod moved_typed_calls;
pub mod parse_sql_macro;
Expand Down
37 changes: 37 additions & 0 deletions crates/sqlbuild-analysis/src/sql_macros/tests/test_expansion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use sqlbuild_render::macro_calls::main::scan_macro_call_sites::scan_macro_call_s
use crate::sql_macros::tests::helpers::{adapter_parses, expanded_for};
use crate::sql_macros::tests::test_types::{
AsciiArguments, ExpansionErrorTestCase, ExpansionTestCase, PythonArgumentTestCase,
ReturnsSequenceTestCase,
};

const CENTS_TO_DOLLARS: (&str, &str) = (
Expand Down Expand Up @@ -137,6 +138,10 @@ const SHOUTED_WRONG: (&str, &str) = (
"shouted",
"MACRO (description \"Shouted\", arguments (value VARCHAR), returns BIGINT);\n\nUPPER(@value)\n",
);
const SHOUTED_TEXT: (&str, &str) = (
"shouted",
"MACRO (description \"Shouted\", arguments (value VARCHAR), returns VARCHAR);\n\nUPPER(@value)\n",
);
const SHOUTED_UNTYPED: (&str, &str) = (
"shouted",
"MACRO (description \"Shouted\", arguments (value VARCHAR));\n\nUPPER(@value)\n",
Expand Down Expand Up @@ -821,6 +826,38 @@ fn given_a_body_whose_type_breaks_its_declaration_when_expanding_then_the_error_
}
}

#[test]
fn given_one_body_under_different_returns_when_expanding_in_turn_then_each_returns_is_checked() {
let test_cases = [
ReturnsSequenceTestCase {
description: "a fitting declaration, then a breaking one, alternating",
definitions: &[SHOUTED_TEXT, SHOUTED_WRONG, SHOUTED_TEXT, SHOUTED_WRONG],
sql: "SELECT @sql.shouted(o.status) FROM orders AS o",
expected_codes: &[None, Some("P024"), None, Some("P024")],
},
ReturnsSequenceTestCase {
description: "a breaking declaration, then a fitting one",
definitions: &[SHOUTED_WRONG, SHOUTED_TEXT],
sql: "SELECT @sql.shouted(o.status) FROM orders AS o",
expected_codes: &[Some("P024"), None],
},
];

for test_case in test_cases {
let codes: Vec<Option<&str>> = test_case
.definitions
.iter()
.map(|definition| {
expanded_for("duckdb", &[*definition], test_case.sql)
.err()
.map(|error| error.code)
})
.collect();

assert_eq!(codes, test_case.expected_codes, "{}", test_case.description);
}
}

#[test]
fn given_a_sql_macro_call_in_python_macro_arguments_when_expanding_then_python_receives_its_expansion()
{
Expand Down
7 changes: 7 additions & 0 deletions crates/sqlbuild-analysis/src/sql_macros/tests/test_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ pub(super) struct ExpansionErrorTestCase {
pub(super) expected_message: &'static str,
}

pub(super) struct ReturnsSequenceTestCase {
pub(super) description: &'static str,
pub(super) definitions: &'static [(&'static str, &'static str)],
pub(super) sql: &'static str,
pub(super) expected_codes: &'static [Option<&'static str>],
}

pub(super) struct DefinitionErrorTestCase {
pub(super) description: &'static str,
pub(super) source: &'static str,
Expand Down
Loading
Loading