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
35 changes: 35 additions & 0 deletions benchmarks/duckshop/project/rules/quoted_aliases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
"""DuckShop house rule that keeps case-sensitive output names inside partner export models."""

from sqlbuild.rules import Finding, Model, RuleContext, rule

EXPORT_SUFFIX = "_export"


@rule(
code="XSQBRDUCK003",
message="Only partner export models may give columns quoted mixed-case names",
remediation=(
"Name the column in unquoted snake_case, or move the projection into a model whose name "
"ends in _export, which writes a partner's own feed format."
),
)
def mixed_case_names_stay_in_exports(*, model: Model, ctx: RuleContext) -> list[Finding]:
if model.path.stem.endswith(EXPORT_SUFFIX):
return []
sql = ctx.sql.for_model(model).expanded
if '"' not in sql.source:
return []
findings: list[Finding] = []
for node in sql.ast().walk():
if node.kind != "alias" or not node.quoted or node.name is None:
continue
if node.name == node.name.lower():
continue
text = sql.source[node.span[0] : node.span[1]] if node.span else node.name
findings.append(
ctx.finding(
subject=model,
message=f"Quoted mixed-case name {node.name!r} in `{text}`",
)
)
return findings
50 changes: 50 additions & 0 deletions benchmarks/duckshop/project/tests/test_rule_quoted_aliases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from rules.quoted_aliases import mixed_case_names_stay_in_exports

from sqlbuild.rules import RuleCase, RuleFile, evaluate_rule

_HEADER = 'MODEL (description "Listings");\n'
_MIXED_CASE = (
_HEADER
+ "SELECT\n"
+ ' l.listing_id AS "listingId",\n'
+ ' l.note AS "note",\n'
+ " 'isLive' AS status_label\n"
+ 'FROM __ref("dim_listings") AS l\n'
)
_LISTINGS = RuleFile(
path="models/marts/catalogue/dim_listings.sql",
source=_HEADER + "SELECT 1 AS listing_id, 'new' AS note\n",
)


def test_given_quoted_mixed_case_alias_outside_an_export_when_evaluating_then_reports_it() -> None:
result = evaluate_rule(
rule=mixed_case_names_stay_in_exports,
test_case=RuleCase(
description="a mart names a column in a partner's case",
source=_MIXED_CASE,
path="models/marts/catalogue/rpt_listings.sql",
files=(_LISTINGS,),
expected_finding_count=1,
),
)

assert result.finding_count == 1
assert result.findings[0].message == (
"Quoted mixed-case name 'listingId' in `l.listing_id AS \"listingId\"`"
)


def test_given_quoted_mixed_case_alias_in_an_export_when_evaluating_then_passes() -> None:
result = evaluate_rule(
rule=mixed_case_names_stay_in_exports,
test_case=RuleCase(
description="a partner export writes the partner's own names",
source=_MIXED_CASE,
path="models/marts/edge_cases/rpt_edge_partner_listing_export.sql",
files=(_LISTINGS,),
expected_finding_count=0,
),
)

assert result.finding_count == 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! `located_markers_as_names`: relation marker calls in code as plain names that keep every position, with where each call and name stand.

use regex::Regex;
use sqlbuild_sqltext::sql_scan::main::dialect_non_code_ranges::dialect_non_code_ranges;
use sqlbuild_sqltext::sql_scan::models::LexicalSyntax;

use crate::relation_markers::_helpers::pattern::relation_marker_pattern;
use crate::relation_markers::constants::{DBT_REF_FUNCTION, MARKER_NAME_PREFIX};
use crate::relation_markers::types::{MarkerCall, MarkerRanges};

/// `sql` with each marker call in code as `__sqb_<index>`, padded so later text keeps its line and column, the calls by index, and each call's and padded name's byte ranges.
pub fn located_markers_as_names(
sql: &str,
syntax: &LexicalSyntax,
) -> Result<(String, Vec<MarkerCall>, Vec<MarkerRanges>), String> {
let pattern: &Regex = relation_marker_pattern()?;
let mut protected: Option<Vec<(usize, usize)>> = None;
let mut named: String = String::with_capacity(sql.len());
let mut calls: Vec<MarkerCall> = Vec::new();
let mut located: Vec<MarkerRanges> = Vec::new();
let mut cursor: usize = 0;
for captures in pattern.captures_iter(sql) {
let (Some(full), Some(function), Some(first)) =
(captures.get(0), captures.get(1), captures.get(2))
else {
continue;
};
let ranges = protected.get_or_insert_with(|| dialect_non_code_ranges(sql, syntax));
let following: usize = ranges.partition_point(|(start, _)| *start <= full.start());
if following > 0 && full.start() < ranges[following - 1].1 {
continue;
}
let function_name: String = function.as_str().to_ascii_lowercase();
let referenced_name: String = match (captures.get(3), function_name == DBT_REF_FUNCTION) {
(None, _) => first.as_str().to_owned(),
(Some(second), true) => format!("{}__{}", first.as_str(), second.as_str()),
(Some(_), false) => continue,
};
named.push_str(&sql[cursor..full.start()]);
let name_start: usize = named.len();
named.push_str(&padded_name(
&format!("{MARKER_NAME_PREFIX}{}", calls.len()),
full.as_str(),
));
cursor = full.end();
calls.push((function_name, referenced_name));
located.push((full.range(), name_start..named.len()));
}
named.push_str(&sql[cursor..]);
Ok((named, calls, located))
}

/// `name`, then the call's line breaks, then spaces as wide as the call's last line.
fn padded_name(name: &str, call: &str) -> String {
let breaks: usize = call.matches('\n').count();
let last_line: &str = call.rsplit('\n').next().unwrap_or(call);
let width: usize = if breaks == 0 {
call.chars().count().saturating_sub(name.chars().count())
} else {
last_line.chars().count()
};
format!("{name}{}{}", "\n".repeat(breaks), " ".repeat(width))
}
Original file line number Diff line number Diff line change
@@ -1,60 +1,14 @@
//! `markers_as_names`: relation marker calls in code as plain names that keep every position.

use regex::Regex;
use sqlbuild_sqltext::sql_scan::main::dialect_non_code_ranges::dialect_non_code_ranges;
use sqlbuild_sqltext::sql_scan::models::LexicalSyntax;

use crate::relation_markers::_helpers::pattern::relation_marker_pattern;
use crate::relation_markers::constants::{DBT_REF_FUNCTION, MARKER_NAME_PREFIX};
use crate::relation_markers::main::located_markers_as_names::located_markers_as_names;
use crate::relation_markers::types::MarkerCall;

/// `sql` with each marker call in code as `__sqb_<index>`, padded so later text keeps its line and column, and the calls by index.
pub fn markers_as_names(
sql: &str,
syntax: &LexicalSyntax,
) -> Result<(String, Vec<MarkerCall>), String> {
let pattern: &Regex = relation_marker_pattern()?;
let mut protected: Option<Vec<(usize, usize)>> = None;
let mut named: String = String::with_capacity(sql.len());
let mut calls: Vec<MarkerCall> = Vec::new();
let mut cursor: usize = 0;
for captures in pattern.captures_iter(sql) {
let (Some(full), Some(function), Some(first)) =
(captures.get(0), captures.get(1), captures.get(2))
else {
continue;
};
let ranges = protected.get_or_insert_with(|| dialect_non_code_ranges(sql, syntax));
let following: usize = ranges.partition_point(|(start, _)| *start <= full.start());
if following > 0 && full.start() < ranges[following - 1].1 {
continue;
}
let function_name: String = function.as_str().to_ascii_lowercase();
let referenced_name: String = match (captures.get(3), function_name == DBT_REF_FUNCTION) {
(None, _) => first.as_str().to_owned(),
(Some(second), true) => format!("{}__{}", first.as_str(), second.as_str()),
(Some(_), false) => continue,
};
named.push_str(&sql[cursor..full.start()]);
named.push_str(&padded_name(
&format!("{MARKER_NAME_PREFIX}{}", calls.len()),
full.as_str(),
));
cursor = full.end();
calls.push((function_name, referenced_name));
}
named.push_str(&sql[cursor..]);
Ok((named, calls))
}

/// `name`, then the call's line breaks, then spaces as wide as the call's last line.
fn padded_name(name: &str, call: &str) -> String {
let breaks: usize = call.matches('\n').count();
let last_line: &str = call.rsplit('\n').next().unwrap_or(call);
let width: usize = if breaks == 0 {
call.chars().count().saturating_sub(name.chars().count())
} else {
last_line.chars().count()
};
format!("{name}{}{}", "\n".repeat(breaks), " ".repeat(width))
located_markers_as_names(sql, syntax).map(|(named, calls, _)| (named, calls))
}
1 change: 1 addition & 0 deletions crates/sqlbuild-analysis/src/relation_markers/main/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod lateral_start;
pub mod located_markers_as_names;
pub mod markers_as_names;
5 changes: 5 additions & 0 deletions crates/sqlbuild-analysis/src/relation_markers/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
//! Shapes of relation marker calls.

use std::ops::Range;

/// A marker call's lower-case function, such as `__ref`, and the name it references.
pub type MarkerCall = (String, String);

/// A marker call's byte range in the source, and the byte range of the padded name that stands for it.
pub type MarkerRanges = (Range<usize>, Range<usize>);
6 changes: 5 additions & 1 deletion crates/sqlbuild-analysis/src/sql_tree/_helpers/clauses.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
//! Join and CTE clauses, which SQLQuack keeps as statement fields rather than expressions.

use sqlquack_syntax::Expression;
use sqlquack_syntax::expressions::names::models::Identifier;
use sqlquack_syntax::expressions::queries::models::{Cte, Join, JoinKind, With};
use sqlquack_syntax::tokens::models::Span;

use crate::sql_tree::constants::{CTE_KIND, JOIN_KIND};

/// One clause of a statement: its kind, its span, and the expressions it owns.
/// One clause of a statement: its kind, its span, its name, and the expressions it owns.
pub(crate) struct Clause<'ast> {
pub(crate) kind: &'static str,
pub(crate) span: Option<Span>,
pub(crate) name: Option<&'ast Identifier>,
pub(crate) children: Vec<&'ast Expression>,
}

Expand Down Expand Up @@ -45,6 +47,7 @@ fn cte_clause(cte: &Cte) -> Clause<'_> {
Clause {
kind: CTE_KIND,
span: cte.span.0,
name: Some(&cte.alias),
children: vec![&cte.this],
}
}
Expand All @@ -57,6 +60,7 @@ fn join_clause(join: &Join) -> Clause<'_> {
Clause {
kind: JOIN_KIND,
span: join.span.0,
name: None,
children,
}
}
Expand Down
4 changes: 3 additions & 1 deletion crates/sqlbuild-analysis/src/sql_tree/_helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
//! Parser selection, parsing, the tree walk, clause nodes, source positions and table wrapping.
//! Parser selection, parsing, the tree walk, clause nodes, node facts, source positions and table wrapping.

pub(crate) mod clauses;
pub(crate) mod node_facts;
pub(crate) mod parse;
pub(crate) mod positions;
pub(crate) mod rules_tree_parser;
pub(crate) mod source_map;
pub(crate) mod table_wrapped;
pub(crate) mod walk;
100 changes: 100 additions & 0 deletions crates/sqlbuild-analysis/src/sql_tree/_helpers/node_facts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//! What a node names or holds: identifier, column, alias, table and CTE names, and literal values.

use sqlquack_syntax::Expression;
use sqlquack_syntax::expressions::names::models::{Identifier, Literal, TableRef};
use sqlquack_syntax::tokens::models::Span;

use crate::relation_markers::constants::MARKER_NAME_PREFIX;
use crate::sql_tree::_helpers::source_map::SourceMap;
use crate::sql_tree::models::{NodeLiteral, NodeName};

/// The name `expression` carries, its qualifier outermost first; a relation marker's name is its call.
pub(crate) fn node_name(expression: &Expression, map: &SourceMap<'_>) -> Option<NodeName> {
let (name, qualifier): (&Identifier, Vec<&Identifier>) = match expression {
Expression::Identifier(identifier) => (identifier, Vec::new()),
Expression::Column(column) => (&column.name, column.table.iter().collect()),
Expression::Alias(alias) => (&alias.alias, Vec::new()),
Expression::Table(table) => (
&table.name,
table.catalog.iter().chain(table.schema.iter()).collect(),
),
_ => return None,
};
Some(named(name, &qualifier, map))
}

/// `name` and its qualifier as written, marker names read back as their calls.
pub(crate) fn named(name: &Identifier, qualifier: &[&Identifier], map: &SourceMap<'_>) -> NodeName {
let (text, quoted) = written(name, map);
NodeName {
name: text,
quoted,
qualifier: qualifier.iter().map(|part| written(part, map).0).collect(),
}
}

/// The value and snake_case type of a literal node.
pub(crate) fn node_literal(expression: &Expression) -> Option<NodeLiteral> {
let Expression::Literal(literal) = expression else {
return None;
};
Some(NodeLiteral {
value: literal.value_str().to_owned(),
literal_type: literal_type(literal),
})
}

/// A table's source range: from its outermost qualifier to the end of its name, without its alias.
pub(crate) fn table_span(table: &TableRef) -> Option<Span> {
let end: Span = table.name.span?;
let first: &Identifier = table
.catalog
.as_ref()
.or(table.schema.as_ref())
.unwrap_or(&table.name);
let qualified = [table.catalog.as_ref(), table.schema.as_ref()];
if qualified.iter().flatten().any(|part| part.span.is_none()) {
return None;
}
first.span.map(|start| start.through(end))
}

/// An identifier's text and quoting; a marker name reads as the call it stands for, unquoted.
fn written(identifier: &Identifier, map: &SourceMap<'_>) -> (String, bool) {
match marker_call(identifier, map) {
Some(call) => (call.to_owned(), false),
None => (identifier.name.clone(), identifier.quoted),
}
}

/// The call an unquoted marker name stands for.
fn marker_call<'source>(identifier: &Identifier, map: &SourceMap<'source>) -> Option<&'source str> {
if identifier.quoted {
return None;
}
let index: &str = identifier.name.strip_prefix(MARKER_NAME_PREFIX)?;
match index.parse::<usize>() {
Ok(index) => map.calls.get(index).copied(),
Err(_not_a_marker) => None,
}
}

fn literal_type(literal: &Literal) -> &'static str {
match literal {
Literal::String(_) => "string",
Literal::Number(_) => "number",
Literal::HexString(_) => "hex_string",
Literal::HexNumber(_) => "hex_number",
Literal::BitString(_) => "bit_string",
Literal::ByteString(_) => "byte_string",
Literal::NationalString(_) => "national_string",
Literal::Date(_) => "date",
Literal::Time(_) => "time",
Literal::Timestamp(_) => "timestamp",
Literal::Datetime(_) => "datetime",
Literal::TripleQuotedString(_, _) => "triple_quoted_string",
Literal::EscapeString(_) => "escape_string",
Literal::DollarString(_) => "dollar_string",
Literal::RawString(_) => "raw_string",
}
}
Loading
Loading