diff --git a/coverage/syntax-databricks/README.md b/coverage/syntax-databricks/README.md index 641811058..9202b4982 100644 --- a/coverage/syntax-databricks/README.md +++ b/coverage/syntax-databricks/README.md @@ -569,6 +569,8 @@ others: - `ADD COLUMN` against `ADD COLUMNS` and `DROP COLUMN` against `DROP COLUMNS`, because the ALTER TABLE page writes `{ COLUMN | COLUMNS }` as alternatives of one clause; - the sign written before an interval value, including `+`; +- a raw string (`r'c'` or `R'c'`) against a plain one, and adjacent strings (`'c' 'c'`) against + the one string they join to, in a `joined_string` literal that keeps each piece; - the operator written for a construct another documented form shares: `==` against `=`, `!=` against `<>`, `IS [NOT] DISTINCT FROM` against `<=>`, `REGEXP` and `RLIKE` against the `rlike` call, and `>>>` against the `shiftrightunsigned` call, in a `written_as` field that analysis ignores; diff --git a/coverage/syntax-databricks/reviews/expressions.toml b/coverage/syntax-databricks/reviews/expressions.toml index eb05a9894..97ec3ffb2 100644 --- a/coverage/syntax-databricks/reviews/expressions.toml +++ b/coverage/syntax-databricks/reviews/expressions.toml @@ -1241,3 +1241,54 @@ note = "Re-binds db-expressions-061, not a re-read: that exclusion was bound to [[review.exclusion]] form = "db.expressions.object-type" reason = "output-only type. The page (doc.data-types.object-type, page_sha256 bb2353b8cf2213122635372bfbe0177c7a19da940f0138ce38766f0368c5546a) says: \"The OBJECT cannot be stored in a table column. It is only exposed when calling schema_of_variant or schema_of_variant_agg.\" The pinned engine (2026.45) and the local parser both refuse OBJECT as input. Reconsider if the page changes or the engine starts accepting OBJECT as input." + +[[review]] +id = "db-expressions-063" +family = "expressions" +catalogue_sha256 = "ffc590e2daad0e5c31541285aca1a5a7d1ce7e7bedb0c1ccdc4c1cfcdd32473c" +obligations_sha256 = "697332d6178cfd5465cf1ebae8c6d98f956a76ed507c8807032c76c97f48bc11" +parser_tree = "e41321e692d3aa300122b5affc29d38a151bed13" +reviewed_utc = "2026-11-18T10:11:35Z" +verdict = "approved" +forms = [ + "db.expressions.object-type", +] +note = "Re-binds db-expressions-062, not a re-read: that exclusion was bound to parser tree dafbc1c0 and went stale when the parser kept raw and adjacent string spellings, the parentheses of an INSERT query and a WINDOW written after a parenthesized query (parser tree e41321e6), so object-type again left the excluded count. The catalogue and obligation files are byte-identical to the ones it bound (catalogue ffc590e2, obligations 697332d6), and the local parser still refuses OBJECT as input in all three cases, with the same error as before (unsupported data type OBJECT, position 22). The exclusion and its reason are repeated unchanged." + +[[review.exclusion]] +form = "db.expressions.object-type" +reason = "output-only type. The page (doc.data-types.object-type, page_sha256 bb2353b8cf2213122635372bfbe0177c7a19da940f0138ce38766f0368c5546a) says: \"The OBJECT cannot be stored in a table column. It is only exposed when calling schema_of_variant or schema_of_variant_agg.\" The pinned engine (2026.45) and the local parser both refuse OBJECT as input. Reconsider if the page changes or the engine starts accepting OBJECT as input." + +[[review]] +id = "db-expressions-064" +family = "expressions" +parser_tree = "e41321e692d3aa300122b5affc29d38a151bed13" +reviewed_utc = "2026-11-18T10:11:35Z" +verdict = "approved" +forms = [ + "db.expressions.string-type.subscript-dot", +] +note = "Re-read on 18 Nov 2026 after the adjacent-strings fix, at parser tree e41321e6, from the existing 2026.45 receipts; 4 of 4 required obligations are met, and the warehouse accepts all four cases. The gated tree screen (drop, synonym swap, backquote, string, double quote, requote, unquote, insert AS, insert = and paren pair) finds no same-tree variant of any accepted case in 128 variants; the 3 requote variants it allows rewrite one string literal in the other quote style, which the lexer reads as the same token. string-type.subscript-dot: db-expressions-022's (d) finding, held again in db-expressions-040, is answered: SELECT ( 'c' 'c' ) (kcba339110891, o010) is now a joined_string literal that keeps the value cc and both pieces, so it no longer shares the tree of SELECT ( 'cc' ), and a dropped piece gives a plain string. db-expressions-022's (a) finding stays answered by the [[reading]] row. r'c' and R'c' (o008, o009) are raw_string literals apart from 'c' (o007); the two prefixes give one tree, as a keyword's letter case does, because the lexer reads the prefix case-insensitively." + +[review.fingerprints] +"db.expressions.string-type.subscript-dot" = "8e1d8018e9410c999219c14a399ccf33865df765423a52e95a27b90d1da18a23" + +[review.definitions] +"db.expressions.string-type.subscript-dot" = "6e0e70498e32940a8ecb5d77d0d34cfc3c73896223f0d392bac3582b9b4442b9" + +[[review]] +id = "db-expressions-065" +family = "expressions" +catalogue_sha256 = "ffc590e2daad0e5c31541285aca1a5a7d1ce7e7bedb0c1ccdc4c1cfcdd32473c" +obligations_sha256 = "697332d6178cfd5465cf1ebae8c6d98f956a76ed507c8807032c76c97f48bc11" +parser_tree = "243ba4a29f6b3b1da6889bd32bb4efedd9b1c712" +reviewed_utc = "2026-11-18T13:52:45Z" +verdict = "approved" +forms = [ + "db.expressions.object-type", +] +note = "Re-binds db-expressions-063, not a re-read: that exclusion was bound to parser tree e41321e6 and went stale when the parser kept a WINDOW written after a parenthesized query on the node after the parentheses, even when the query inside could take it (parser tree 243ba4a2), so object-type again left the excluded count. The catalogue and obligation files are byte-identical to the ones it bound (catalogue ffc590e2, obligations 697332d6), and the local parser still refuses OBJECT as input in all three cases, with the same error as before (unsupported data type OBJECT, position 22). The exclusion and its reason are repeated unchanged." + +[[review.exclusion]] +form = "db.expressions.object-type" +reason = "output-only type. The page (doc.data-types.object-type, page_sha256 bb2353b8cf2213122635372bfbe0177c7a19da940f0138ce38766f0368c5546a) says: \"The OBJECT cannot be stored in a table column. It is only exposed when calling schema_of_variant or schema_of_variant_agg.\" The pinned engine (2026.45) and the local parser both refuse OBJECT as input. Reconsider if the page changes or the engine starts accepting OBJECT as input." diff --git a/coverage/syntax-databricks/screen.toml b/coverage/syntax-databricks/screen.toml index 5a7c8b31c..2da0cd5e4 100644 --- a/coverage/syntax-databricks/screen.toml +++ b/coverage/syntax-databricks/screen.toml @@ -8,6 +8,11 @@ # covers one token written in another spelling that the lexer reads as the same token, in every # form; only `requote` ('x' and "x", one string literal) qualifies. # +# Letter case is allowed without an entry, because the screen makes no case variants: the lexer +# reads a keyword and the r or R prefix of a raw string case-insensitively, so SELECT and select, +# and r'x' and R'x', are one token each and give one tree. A receipt that shows Databricks reading +# two such spellings differently makes it a finding. +# # [[lexical]] # mutation = "requote" # reason = "" diff --git a/crates/sqlquack-semantics/src/databricks/profile/_helpers/typing.rs b/crates/sqlquack-semantics/src/databricks/profile/_helpers/typing.rs index 3d81cabae..4ab2f096f 100644 --- a/crates/sqlquack-semantics/src/databricks/profile/_helpers/typing.rs +++ b/crates/sqlquack-semantics/src/databricks/profile/_helpers/typing.rs @@ -20,7 +20,9 @@ const MAX_DECIMAL_PRECISION: usize = 38; pub(crate) fn databricks_literal_type(literal: &Literal) -> Option { match literal { Literal::Number(text) => number_type(text, false), - Literal::String(_) => Some(STRING_TYPE.to_owned()), + Literal::String(_) | Literal::RawString(_) | Literal::JoinedString(..) => { + Some(STRING_TYPE.to_owned()) + } _ => None, } } diff --git a/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs b/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs index 412590d84..312fff6ed 100644 --- a/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs +++ b/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs @@ -136,7 +136,7 @@ fn given_a_databricks_query_when_naming_and_typing_outputs_then_they_are_the_war DatabricksOutputsTestCase { description: "literals typed as typeof reports them", sql: "SELECT 1 AS a, 2147483648 AS b, 9223372036854775808 AS c, 1.50 AS d, \ - 0.05 AS e, 1e5 AS f, 'x' AS g, -2147483648 AS h", + 0.05 AS e, 1e5 AS f, 'x' AS g, -2147483648 AS h, r'x' AS i", expected_outputs: &[ ("a", Some("INT")), ("b", Some("BIGINT")), @@ -146,6 +146,7 @@ fn given_a_databricks_query_when_naming_and_typing_outputs_then_they_are_the_war ("f", Some("DOUBLE")), ("g", Some("STRING")), ("h", Some("INT")), + ("i", Some("STRING")), ], }, DatabricksOutputsTestCase { @@ -388,6 +389,66 @@ fn given_a_relation_or_query_in_extra_parentheses_when_analysed_then_it_reads_as sql: "( SELECT id FROM orders ) UNION ( SELECT id FROM customers )", expected_same_as: "SELECT id FROM orders UNION SELECT id FROM customers", }, + SameReadingTestCase { + description: "an insert's query in parentheses", + sql: "INSERT INTO orders ( SELECT id, nope FROM customers )", + expected_same_as: "INSERT INTO orders SELECT id, nope FROM customers", + }, + ]; + for test_case in test_cases { + assert_eq!( + reading(test_case.sql), + reading(test_case.expected_same_as), + "{}", + test_case.description + ); + } +} + +#[test] +fn given_a_join_in_parentheses_when_analysed_then_its_conditions_and_items_are_checked_as_without() +{ + let test_cases = [ + SameReadingTestCase { + description: "an unknown column in a parenthesised join's ON", + sql: "SELECT o.id FROM ( orders o JOIN customers c ON o.nope = c.id )", + expected_same_as: "SELECT o.id FROM orders o JOIN customers c ON o.nope = c.id", + }, + SameReadingTestCase { + description: "the same join in a second pair of parentheses", + sql: "SELECT o.id FROM ( ( orders o JOIN customers c ON o.nope = c.id ) )", + expected_same_as: "SELECT o.id FROM orders o JOIN customers c ON o.nope = c.id", + }, + SameReadingTestCase { + description: "a USING key one side lacks", + sql: "SELECT o.id FROM ( orders o JOIN customers c USING ( nope ) )", + expected_same_as: "SELECT o.id FROM orders o JOIN customers c USING ( nope )", + }, + SameReadingTestCase { + description: "a derived table inside the parenthesised join", + sql: "SELECT o.id FROM ( orders o JOIN ( SELECT nope FROM customers ) d ON o.id = d.nope )", + expected_same_as: "SELECT o.id FROM orders o JOIN ( SELECT nope FROM customers ) d ON o.id = d.nope", + }, + SameReadingTestCase { + description: "a parenthesised join joined to an earlier table", + sql: "SELECT o.id FROM customers c2 JOIN ( orders o JOIN customers c ON o.nope = c.id ) ON c2.id = o.id", + expected_same_as: "SELECT o.id FROM customers c2 JOIN orders o ON c2.id = o.id JOIN customers c ON o.nope = c.id", + }, + SameReadingTestCase { + description: "a LATERAL item reading the table before it inside the parentheses", + sql: "SELECT l.n FROM ( orders o JOIN LATERAL ( SELECT o.id AS n ) l ON true )", + expected_same_as: "SELECT l.n FROM orders o JOIN LATERAL ( SELECT o.id AS n ) l ON true", + }, + SameReadingTestCase { + description: "USING inside the parentheses, its left side only the tables there", + sql: "SELECT 1 FROM orders JOIN ( customers JOIN orders y USING ( quantity ) ) ON true", + expected_same_as: "SELECT 1 FROM customers JOIN orders y USING ( quantity )", + }, + SameReadingTestCase { + description: "USING after the parentheses, its right side every table there", + sql: "SELECT 1 FROM orders JOIN ( customers JOIN orders y ON true ) USING ( quantity )", + expected_same_as: "SELECT 1 FROM orders JOIN orders y USING ( quantity )", + }, ]; for test_case in test_cases { assert_eq!( @@ -402,6 +463,11 @@ fn given_a_relation_or_query_in_extra_parentheses_when_analysed_then_it_reads_as #[test] fn given_a_parenthesized_query_with_clauses_after_it_when_analysed_then_they_read_its_sources() { let test_cases = [ + SameReadingTestCase { + description: "a named window written after the parentheses", + sql: "( SELECT sum ( quantity ) OVER w AS x FROM orders LIMIT 1 ) WINDOW w AS ( ORDER BY quantity )", + expected_same_as: "SELECT sum ( quantity ) OVER w AS x FROM orders WINDOW w AS ( ORDER BY quantity ) LIMIT 1", + }, SameReadingTestCase { description: "ORDER BY a column the query does not output", sql: "( SELECT id FROM orders ) ORDER BY quantity", @@ -482,3 +548,51 @@ fn given_a_parenthesized_query_with_clauses_after_it_when_analysed_then_they_rea ); } } + +#[test] +fn given_a_window_named_after_parentheses_when_checked_then_only_an_undefined_name_is_found() { + let test_cases = [ + QueryFindingsTestCase { + description: "the window function inside reads the window named after the parentheses", + sql: "( SELECT sum ( quantity ) OVER w AS x FROM orders LIMIT 1 ) WINDOW w AS ( ORDER BY quantity )", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a window no WINDOW clause names is still unknown", + sql: "( SELECT sum ( quantity ) OVER w2 AS x FROM orders LIMIT 1 ) WINDOW w AS ( ORDER BY quantity )", + expected_findings: &[("E232", "Unknown named window 'w2'", 15)], + }, + ]; + for test_case in test_cases { + assert_eq!( + databricks_findings(test_case.sql), + owned_findings(test_case.expected_findings), + "{}", + test_case.description + ); + } +} + +#[test] +fn given_adjacent_strings_when_analysed_then_they_read_as_the_string_they_join_to() { + let test_cases = [ + SameReadingTestCase { + description: "an output's type", + sql: "SELECT 'a' 'b' AS x", + expected_same_as: "SELECT 'ab' AS x", + }, + SameReadingTestCase { + description: "a number column compared with a string that is not a number", + sql: "SELECT id FROM orders WHERE quantity = 'x' 'y'", + expected_same_as: "SELECT id FROM orders WHERE quantity = 'xy'", + }, + ]; + for test_case in test_cases { + assert_eq!( + reading(test_case.sql), + reading(test_case.expected_same_as), + "{}", + test_case.description + ); + } +} diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/checks/coercion.rs b/crates/sqlquack-semantics/src/lineage/_helpers/checks/coercion.rs index 5c99870d3..6f5625ac1 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/coercion.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/coercion.rs @@ -102,7 +102,7 @@ pub(crate) fn predicate(engine: Engine, family: Family) -> bool { fn string_literal_text(expression: &Expression) -> Option<&str> { match expression { Expression::Literal(literal) => match literal.as_ref() { - Literal::String(value) => Some(value), + Literal::String(value) | Literal::JoinedString(value, _) => Some(value), _ => None, }, Expression::Alias(alias) => string_literal_text(&alias.this), @@ -372,10 +372,12 @@ pub(crate) fn invalid_literal_for(engine: Engine, expression: &Expression, targe Expression::Alias(alias) => invalid_literal_for(engine, &alias.this, target), Expression::Paren(paren) => invalid_literal_for(engine, &paren.this, target), Expression::Literal(literal) => match literal.as_ref() { - Literal::String(value) if engine == Engine::Postgres => { + Literal::String(value) | Literal::JoinedString(value, _) + if engine == Engine::Postgres => + { invalid_postgres_literal(value, target) } - Literal::String(value) => match target { + Literal::String(value) | Literal::JoinedString(value, _) => match target { Family::Integer | Family::Numeric => value.trim().parse::().is_err(), Family::Date | Family::Timestamp => invalid_temporal_literal(value), Family::Boolean => !matches!( diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/checks/node_checks.rs b/crates/sqlquack-semantics/src/lineage/_helpers/checks/node_checks.rs index c12ca91c8..496e4afb5 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/node_checks.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/node_checks.rs @@ -22,6 +22,7 @@ use crate::lineage::_helpers::checks::signatures::{ argument_finding, check_call, dispatch_name, has_signature, located, unify, }; use crate::lineage::_helpers::tracing::expressions::expression_lineage; +use crate::lineage::_helpers::tracing::sources::every_join; use crate::lineage::constants::{ DUCKDB_KEYWORD_ONLY_FUNCTIONS, NAME_PHASE, RUNTIME_WARNING, TYPE_PHASE, }; @@ -214,7 +215,7 @@ fn clause_predicates( .iter() .map(|clause| ("QUALIFY", &clause.this)), ); - for join in &select.joins { + for join in every_join(select) { clauses.extend(join.on.iter().map(|on| ("JOIN ON", on))); clauses.extend( join.match_condition @@ -338,7 +339,7 @@ fn expression_checks(node: &Expression, level: NodeLevel<'_, '_>) -> Vec { if let Some(Expression::Literal(literal)) = &interval.this - && let Literal::String(value) = literal.as_ref() + && let Literal::String(value) | Literal::JoinedString(value, _) = literal.as_ref() && !value.chars().any(|character| character.is_ascii_digit()) { let code: Option<&'static str> = match engine { @@ -653,7 +654,11 @@ fn scalar_subqueries(node: &Expression, level: NodeLevel<'_, '_>) -> Vec, LineageContext<'_>), ) -> Vec { - let single_from: bool = select - .from - .as_ref() - .is_some_and(|from| from.expressions.len() == 1); - let [join] = select.joins.as_slice() else { + let [(_, None), (_, Some(join))] = from_parts(select)[..] else { return Vec::new(); }; let [left_source, right_source] = scope.sources.as_slice() else { return Vec::new(); }; - if !single_from || join.using.is_empty() { + if join.using.is_empty() { return Vec::new(); } let engine: Engine = Engine::of(context); diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs b/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs index 50599d109..cc300f5e2 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs @@ -13,6 +13,7 @@ use crate::lineage::_helpers::checks::issues::{in_scope, issue, rule_location}; use crate::lineage::_helpers::findings::checks::keyword_function; use crate::lineage::_helpers::tracing::expressions::is_aggregate; use crate::lineage::_helpers::tracing::resolution::key; +use crate::lineage::_helpers::tracing::sources::{every_join, from_parts}; use crate::lineage::constants::{ NAME_FIELD, SCOPE_PHASE, SHAPE_COMMENT_SUFFIX, SHAPE_COSMETIC_FIELDS, SPAN_FIELD, WINDOW_ONLY_FUNCTIONS, @@ -161,9 +162,8 @@ pub(crate) fn select_rules( .map(|clause| (&clause.this, (false, false), usable)), ); clauses.extend( - select - .joins - .iter() + every_join(select) + .into_iter() .filter_map(|join| join.on.as_ref()) .map(|on| (on, (false, false), &empty)), ); @@ -318,7 +318,7 @@ fn structure_rules( } } let mut names: HashSet = HashSet::new(); - for item in from_items(select) + for (item, _) in from_parts(select) .into_iter() .filter(|_| !matches!(engine, Engine::Sqlite | Engine::Databricks)) { @@ -333,18 +333,18 @@ fn structure_rules( )); } } - let windows: HashSet = select + let windows: HashSet<&str> = level + .scope .windows .iter() - .flatten() - .map(|window| level.key(&window.name)) + .map(|(name, _)| name.as_str()) .collect(); let mut has_window: bool = false; for expression in in_scope(node) { if let Expression::WindowFunction(window) = expression { has_window = true; if let Some(name) = &window.over.window_name - && !windows.contains(&level.key(name)) + && !windows.contains(level.key(name).as_str()) { findings.push(rule( "E232", @@ -394,15 +394,6 @@ fn structure_rules( findings } -fn from_items(select: &Select) -> Vec<&Expression> { - let mut items: Vec<&Expression> = Vec::new(); - if let Some(from) = &select.from { - items.extend(from.expressions.iter()); - } - items.extend(select.joins.iter().map(|join| &join.this)); - items -} - /// The name a FROM item is known by: its alias, else its table's name. fn item_name(item: &Expression) -> Option<&Identifier> { match item { @@ -422,7 +413,7 @@ fn invalid_bound(value: &Expression) -> bool { Literal::Number(number) => number .parse::() .is_ok_and(|number| number < f64::default()), - Literal::String(text) => text + Literal::String(text) | Literal::JoinedString(text, _) => text .parse::() .map_or(true, |number| number < f64::default()), _ => false, diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs index 54dfdd0bd..68aa6d30b 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/checks.rs @@ -11,7 +11,7 @@ use crate::lineage::_helpers::checks::scope_rules::output_width; use crate::lineage::_helpers::tracing::queries::empty_scope; use crate::lineage::_helpers::tracing::resolution::key; use crate::lineage::_helpers::tracing::reuse::holds_unpivot; -use crate::lineage::_helpers::tracing::sources::item_sources; +use crate::lineage::_helpers::tracing::sources::{from_parts, item_sources, sided_parts}; use crate::lineage::constants::{ ALIAS_COUNT_CODE, AMBIGUOUS_COLUMN_CODE, COLUMNS_NO_MATCH, CONNECT_BY_LEVEL, NAME_PHASE, UNKNOWN_COLUMN_CODE, UNKNOWN_QUALIFIER_CODE, @@ -348,7 +348,7 @@ pub(crate) fn columns_pattern_finding( let Expression::Literal(literal) = pattern else { return None; }; - let (Literal::String(pattern), true) = ( + let (Literal::String(pattern) | Literal::JoinedString(pattern, _), true) = ( literal.as_ref(), dialect_rules(context.profile).checks_columns_patterns(), ) else { @@ -400,11 +400,19 @@ pub(crate) fn using_findings( ) -> Vec { let at = select.expressions.first().and_then(Expression::source_span); let mut findings: Vec = Vec::new(); - let leading: usize = select.from.iter().map(|from| from.expressions.len()).sum(); - for (position, join) in select.joins.iter().enumerate() { - let before: usize = leading + position; - let (left, right) = scope.sources.split_at(before.min(scope.sources.len())); - let right: &[ScopeSource] = &right[..right.len().min(1)]; + let sources = |parts: std::ops::Range| { + let at = |part: usize| { + scope.starts.get(part).map_or(scope.sources.len(), |start| { + *start.min(&scope.sources.len()) + }) + }; + &scope.sources[at(parts.start)..at(parts.end)] + }; + for part in sided_parts(select) { + let Some(join) = part.join else { + continue; + }; + let (left, right) = (sources(part.left), sources(part.right)); for name in &join.using { let wanted: String = key(name, context); if lacks(left, &wanted, context) || lacks(right, &wanted, context) { @@ -536,13 +544,8 @@ pub(crate) fn alias_count_findings( scope: &LineageScope<'_>, context: LineageContext<'_>, ) -> Vec { - let items = select - .from - .iter() - .flat_map(|from| from.expressions.iter()) - .chain(select.joins.iter().map(|join| &join.this)); let mut findings: Vec = Vec::new(); - for item in items { + for (item, _) in from_parts(select) { let (relation, aliases, width) = match item { Expression::Subquery(subquery) => ( "Derived-table", diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs index f94f82f85..90acfc2c5 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs @@ -4,7 +4,9 @@ use std::collections::HashMap; use sqlquack_syntax::Expression; use sqlquack_syntax::ExpressionWalk; -use sqlquack_syntax::expressions::queries::models::{JoinKind, OrderBy, Select, With}; +use sqlquack_syntax::expressions::queries::models::{ + JoinKind, NamedWindow, OrderBy, Select, Subquery, With, +}; use crate::dialects::main::dialect_rules::dialect_rules; use crate::lineage::_helpers::checks::issues::in_scope; @@ -19,15 +21,16 @@ use crate::lineage::_helpers::findings::checks::{ }; use crate::lineage::_helpers::tracing::expressions::date_part_argument; use crate::lineage::_helpers::tracing::queries::{ - clauses_within, empty_scope, level_scope, output_key, takes_clauses, unannotated, - with_ctes_visiting, + after_windows, clauses_within, empty_scope, level_scope, output_key, takes_clauses, + unannotated, with_ctes_visiting, }; use crate::lineage::_helpers::tracing::resolution::key; use crate::lineage::_helpers::tracing::reuse::{Reach, Reuse, reusing}; +use crate::lineage::_helpers::tracing::sources::{every_join, from_parts}; use crate::lineage::constants::{COLUMNS_FUNCTION, OUTER_RIGHT_JOINS}; use crate::lineage::models::Engine; use crate::lineage::models::NameFinding; -use crate::lineage::models::{LineageContext, LineageScope, NamedQuery}; +use crate::lineage::models::{Lineage, LineageContext, LineageScope, NamedQuery}; use crate::lineage::types::DialectRules; /// The findings of `query` and its inner queries; `aliases` are enclosing levels' output aliases. @@ -74,7 +77,7 @@ fn query_findings_level( .collect(); match inner { Expression::Select(select) => { - select_findings((inner, select, &orders), (ctes, outer, aliases), context) + select_findings((inner, select, &after), (ctes, outer, aliases), context) } _ => { let mut found: Vec = @@ -185,7 +188,7 @@ fn unread_ctes(owner: &Expression, with: &With, context: LineageContext<'_>) -> .collect() } -/// The items before the `index`th FROM item, then `outer`; none when no item comes before it. +/// The parts before the `index`th FROM part, then `outer`; none when no part comes before it. fn left_scope<'a>( scope: &LineageScope<'_>, index: usize, @@ -233,7 +236,7 @@ fn faulted_cte(name: &str, reach: &[NamedQuery], context: LineageContext<'_>) -> } fn select_findings( - (node, select, after): (&Expression, &Select, &[&OrderBy]), + (node, select, after): (&Expression, &Select, &[&Subquery]), (ctes, outer, aliases): (&[NamedQuery], Option<&LineageScope<'_>>, &[String]), context: LineageContext<'_>, ) -> Vec { @@ -244,6 +247,12 @@ fn select_findings( ); let _reuse: Reuse = reusing(&ctes); let mut scope: LineageScope<'_> = level_scope(select, &ctes, outer, context); + let found: Vec<(String, Lineage)> = after_windows(after, &scope, context); + scope.windows.extend(found); + let orders: Vec<&OrderBy> = after + .iter() + .filter_map(|node| node.order_by.as_ref()) + .collect(); let level = NodeLevel { scope: &scope, context, @@ -256,7 +265,12 @@ fn select_findings( context, }, )); - let after_keys = after.iter().flat_map(|order| order_keys(order)); + let after_orders = orders.iter().flat_map(|order| order_keys(order)); + let after_windows = after + .iter() + .flat_map(|node| node.windows.iter().flatten()) + .flat_map(window_keys); + let after_keys = after_orders.chain(after_windows); for checked in in_scope(node) .into_iter() .chain(after_keys.flat_map(in_scope)) @@ -270,17 +284,10 @@ fn select_findings( let rules: &dyn DialectRules = dialect_rules(context.profile); let anywhere: bool = rules.outputs_read_anywhere(); let left_reading: bool = rules.derived_tables_read_left_items(); - let listed: usize = select - .from - .as_ref() - .map_or(0, |from| from.expressions.len()); - for (index, item) in from_items(select).into_iter().enumerate() { + for (index, (item, join)) in from_parts(select).into_iter().enumerate() { let implicit: bool = left_reading && derived_key(unannotated(item), context).is_some() - && index - .checked_sub(listed) - .and_then(|join| select.joins.get(join)) - .is_none_or(|join| !OUTER_RIGHT_JOINS.contains(&join.kind)); + && join.is_none_or(|join| !OUTER_RIGHT_JOINS.contains(&join.kind)); let left: Option> = (lateral(item) || implicit) .then(|| left_scope(&scope, index, (&ctes, outer))) .flatten(); @@ -308,7 +315,7 @@ fn select_findings( own: &own_outputs, hidden: None, connect: select.connect.is_some(), - filtering_join: select.joins.iter().any(|join| { + filtering_join: every_join(select).into_iter().any(|join| { matches!( join.kind, JoinKind::Semi @@ -340,9 +347,9 @@ fn select_findings( nested.extend(inner); } let mut read: Vec<&Expression> = clauses(select); - read.extend(after.iter().flat_map(|order| order_keys(order))); + read.extend(orders.iter().flat_map(|order| order_keys(order))); for clause in read { - let named_place: bool = ordered_name_place(select, after, clause) + let named_place: bool = ordered_name_place(select, &orders, clause) || (rules.group_keys_read_outputs() && group_key_place(select, clause)); let read = match (anywhere, named_place) { (true, _) => level, @@ -361,16 +368,6 @@ fn select_findings( own } -/// The FROM items and joined items of `select`, in order. -fn from_items(select: &Select) -> Vec<&Expression> { - let mut items: Vec<&Expression> = Vec::new(); - if let Some(from) = &select.from { - items.extend(from.expressions.iter()); - } - items.extend(select.joins.iter().map(|join| &join.this)); - items -} - /// The names of `select`'s aliased outputs, which later clauses may read. fn aliased_outputs(select: &Select, context: LineageContext<'_>) -> Vec { select @@ -405,6 +402,12 @@ fn ordered_name_place(select: &Select, after: &[&OrderBy], clause: &Expression) .any(|ordered| std::ptr::eq(&ordered.this, clause)) } +/// The partition and order keys of `window`. +fn window_keys(window: &NamedWindow) -> impl Iterator { + let orders = window.spec.order_by.iter().map(|ordered| &ordered.this); + window.spec.partition_by.iter().chain(orders) +} + /// The keys of `order`. fn order_keys(order: &OrderBy) -> impl Iterator { order.expressions.iter().map(|ordered| &ordered.this) @@ -423,7 +426,7 @@ fn lateral(item: &Expression) -> bool { /// Every expression of `select` after its projections: filters, keys and orders. fn clauses(select: &Select) -> Vec<&Expression> { let mut read: Vec<&Expression> = Vec::new(); - for join in &select.joins { + for join in every_join(select) { read.extend(join.on.iter()); read.extend(join.match_condition.iter()); } diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/queries.rs b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/queries.rs index e6cf98436..6b4a857e1 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/queries.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/queries.rs @@ -6,7 +6,9 @@ use std::rc::Rc; use sqlquack_syntax::Expression; use sqlquack_syntax::ExpressionWalk; use sqlquack_syntax::expressions::names::models::{Identifier, Literal, Star}; -use sqlquack_syntax::expressions::queries::models::{Cte, OrderBy, Select, Subquery, Union, With}; +use sqlquack_syntax::expressions::queries::models::{ + Cte, NamedWindow, OrderBy, Select, Subquery, Union, With, +}; use sqlquack_syntax::tokens::models::Span; use crate::dialects::main::dialect_rules::dialect_rules; @@ -561,7 +563,7 @@ fn level_scope_and_rows<'a>( starts, ..empty_scope(ctes, outer) }; - scope.windows = named_windows(select, &scope, context); + scope.windows = named_windows(select.windows.iter().flatten(), &scope, context); scope.non_null = filtered_non_null(select); scope.traced.get_mut().clear(); if outer.is_none() { @@ -578,6 +580,8 @@ fn select_query( ) -> ResolvedQuery { let ctes = with_ctes(select.with.as_ref(), ctes, outer, context); let (mut scope, rows) = level_scope_and_rows(select, &ctes, outer, context); + let found: Vec<(String, Lineage)> = after_windows(after, &scope, context); + scope.windows.extend(found); let mut query = ResolvedQuery { rows, ..ResolvedQuery::default() @@ -727,7 +731,8 @@ pub(crate) fn takes_clauses(node: &Subquery) -> bool { || node.offset.is_some() || node.sort_by.is_some() || node.cluster_by.is_some() - || node.distribute_by.is_some()) + || node.distribute_by.is_some() + || node.windows.is_some()) } /// The query within `query`'s parentheses and unaliased subquery nodes, and those nodes, innermost first. @@ -823,13 +828,13 @@ fn row_filters(select: &Select) -> Vec<&Expression> { } /// What each named window's partitions and orders read. -fn named_windows( - select: &Select, +fn named_windows<'w>( + named: impl IntoIterator, scope: &LineageScope<'_>, context: LineageContext<'_>, ) -> Vec<(String, Lineage)> { let mut windows: Vec<(String, Lineage)> = Vec::new(); - for window in select.windows.iter().flatten() { + for window in named { let mut read: Vec = Vec::new(); for partition in &window.spec.partition_by { read.push(expression_lineage(partition, scope, context)); @@ -845,6 +850,16 @@ fn named_windows( windows } +/// What each named window written after a query's parentheses reads, in `scope`. +pub(crate) fn after_windows( + after: &[&Subquery], + scope: &LineageScope<'_>, + context: LineageContext<'_>, +) -> Vec<(String, Lineage)> { + let windows = after.iter().flat_map(|node| node.windows.iter().flatten()); + named_windows(windows, scope, context) +} + /// The columns a star selects (a bare star's merged join keys once, first), EXCLUDE etc. applied. pub(crate) fn expanded_star( star: &Star, diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs index a0cde2537..a98021351 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/sources.rs @@ -24,8 +24,8 @@ use crate::lineage::constants::{ STRUCT_FUNCTION, TABLE_FUNCTION_COLUMNS, TABLE_WRAPPER, UNNEST_FUNCTION, }; use crate::lineage::models::{ - FromItems, Lineage, LineageContext, LineageScope, NamedQuery, Nullability, ResolvedQuery, - RowReads, SchemaColumn, ScopeSource, ScopedColumn, TransformKind, + FromItems, FromPart, Lineage, LineageContext, LineageScope, NamedQuery, Nullability, + ResolvedQuery, RowReads, SchemaColumn, ScopeSource, ScopedColumn, TransformKind, }; /// The FROM items of `select` and its joins; a later item may read earlier ones (LATERAL). @@ -35,18 +35,9 @@ pub(crate) fn from_sources( outer: Option<&LineageScope<'_>>, context: LineageContext<'_>, ) -> FromItems { - let mut items: Vec<(&Expression, Option<&Join>, bool)> = Vec::new(); - for item in select.from.iter().flat_map(|from| from.expressions.iter()) { - items.extend(starting(flattened(item, None))); - } - for join in &select.joins { - items.extend(starting(flattened(&join.this, Some(join)))); - } let mut from = FromItems::default(); - for (item, join, starts) in items { - if starts { - from.starts.push(from.sources.len()); - } + for (item, join) in from_parts(select) { + from.starts.push(from.sources.len()); if join.is_some_and(|join| OUTER_RIGHT_JOINS.contains(&join.kind)) { for key in &mut from.merged { key.lineage.nullability = Nullability::Nullable; @@ -80,6 +71,7 @@ pub(crate) fn from_sources( } from.sources.extend(added); } + from.starts.push(from.sources.len()); if dialect_rules(context.profile).has_lateral_views() { for view in &select.lateral_views { let scope = LineageScope { @@ -96,14 +88,40 @@ pub(crate) fn from_sources( from } -/// One FROM item's flattened parts, the first marked as where the item starts. -fn starting<'a>( - parts: Vec<(&'a Expression, Option<&'a Join>)>, -) -> impl Iterator, bool)> { +/// The FROM items of `select`, then each join's item, with the join that adds it. +fn top_items(select: &Select) -> impl Iterator)> { + select + .from + .iter() + .flat_map(|from| from.expressions.iter()) + .map(|item| (item, None)) + .chain(select.joins.iter().map(|join| (&join.this, Some(join)))) +} + +/// Every FROM item and joined item of `select`, a parenthesised join's tables and joins in order. +pub(crate) fn from_parts(select: &Select) -> Vec<(&Expression, Option<&Join>)> { + sided_parts(select) + .into_iter() + .map(|part| (part.item, part.join)) + .collect() +} + +/// `from_parts`, each with the parts on either side of the join that adds it. +pub(crate) fn sided_parts(select: &Select) -> Vec> { + let mut parts: Vec> = Vec::new(); + for (item, join) in top_items(select) { + let added = sided(item, join, 0, parts.len()); + parts.extend(added); + } parts +} + +/// Every join of `select`, including those written inside a parenthesised join. +pub(crate) fn every_join(select: &Select) -> Vec<&Join> { + from_parts(select) .into_iter() - .enumerate() - .map(|(index, (item, join))| (item, join, index == 0)) + .filter_map(|(_, join)| join) + .collect() } /// A LATERAL VIEW's columns under its alias, untyped; open when it names no columns. @@ -154,23 +172,42 @@ fn outer_nullable(mut source: ScopeSource) -> ScopeSource { source } -/// A FROM item, with a parenthesised join's tables and joins listed in order. -fn flattened<'a>( +/// The parts of a FROM item numbered from `start`, the first added by `join` after `group_start`. +fn sided<'a>( item: &'a Expression, join: Option<&'a Join>, -) -> Vec<(&'a Expression, Option<&'a Join>)> { - match item { + group_start: usize, + start: usize, +) -> Vec> { + let mut parts: Vec> = match item { Expression::JoinedTable(joined) if joined.alias.is_none() => { - let mut items = flattened(&joined.left, join); + let mut parts: Vec> = sided(&joined.left, join, group_start, start); for inner in &joined.joins { - items.extend(flattened(&inner.this, Some(inner))); + let added = sided(&inner.this, Some(inner), start, start + parts.len()); + parts.extend(added); } - items + parts } - Expression::Paren(paren) if matches!(paren.this, Expression::JoinedTable(_)) => { - flattened(&paren.this, join) + Expression::Paren(paren) if holds_join(&paren.this) => { + sided(&paren.this, join, group_start, start) } - _ => vec![(item, join)], + _ => vec![FromPart { + item, + join, + left: group_start..start, + right: start..start, + }], + }; + parts[0].right = start..start + parts.len(); + parts +} + +/// Whether `item` is a join, in any number of parentheses. +fn holds_join(item: &Expression) -> bool { + match item { + Expression::JoinedTable(_) => true, + Expression::Paren(paren) => holds_join(&paren.this), + _ => false, } } @@ -848,8 +885,8 @@ fn pivoted_name(value: &Expression, aggregates: usize, context: LineageContext<' } match value { Expression::Literal(literal) => match literal.as_ref() { - Literal::String(text) if bare => text.clone(), - Literal::String(text) => format!("'{text}'"), + Literal::String(text) | Literal::JoinedString(text, _) if bare => text.clone(), + Literal::String(text) | Literal::JoinedString(text, _) => format!("'{text}'"), Literal::Number(text) => text.clone(), _ => String::new(), }, diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/typing/rules.rs b/crates/sqlquack-semantics/src/lineage/_helpers/typing/rules.rs index 9e844ba63..7ca9bd0f8 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/typing/rules.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/typing/rules.rs @@ -29,7 +29,7 @@ fn generic_literal_type(expression: &Expression) -> ValueType { Expression::Literal(literal) => match literal.as_ref() { Literal::Number(text) if text.contains(['.', 'e', 'E']) => Some(DECIMAL_LITERAL_TYPE), Literal::Number(_) => Some(INTEGER_LITERAL_TYPE), - Literal::String(_) => Some(TEXT_TYPE), + Literal::String(_) | Literal::JoinedString(..) => Some(TEXT_TYPE), _ => None, }, Expression::Boolean(_) => Some(BOOLEAN_TYPE), diff --git a/crates/sqlquack-semantics/src/lineage/main/string_literal.rs b/crates/sqlquack-semantics/src/lineage/main/string_literal.rs index f10d7d52a..14580bd11 100644 --- a/crates/sqlquack-semantics/src/lineage/main/string_literal.rs +++ b/crates/sqlquack-semantics/src/lineage/main/string_literal.rs @@ -6,7 +6,10 @@ use sqlquack_syntax::expressions::names::models::Literal; /// Whether `expression` is a string literal, past aliases, parentheses and comments. pub(crate) fn string_literal(expression: &Expression) -> bool { match expression { - Expression::Literal(literal) => matches!(literal.as_ref(), Literal::String(_)), + Expression::Literal(literal) => matches!( + literal.as_ref(), + Literal::String(_) | Literal::JoinedString(..) + ), Expression::Alias(alias) => string_literal(&alias.this), Expression::Paren(paren) => string_literal(&paren.this), Expression::Annotated(annotated) diff --git a/crates/sqlquack-semantics/src/lineage/models.rs b/crates/sqlquack-semantics/src/lineage/models.rs index 271784f12..b9dc89b9b 100644 --- a/crates/sqlquack-semantics/src/lineage/models.rs +++ b/crates/sqlquack-semantics/src/lineage/models.rs @@ -7,7 +7,9 @@ use std::rc::Rc; use rustc_hash::FxHashMap; +use sqlquack_syntax::Expression; use sqlquack_syntax::expressions::names::models::Column; +use sqlquack_syntax::expressions::queries::models::Join; use sqlquack_syntax::tokens::models::Span; use crate::dialects::types::DialectProfile; @@ -186,7 +188,7 @@ pub(crate) struct LineageScope<'a> { pub(crate) traced: RefCell>, /// Lineages that read no SELECT alias, shared by every scope of this level analysed alike. pub(crate) shared: Option>>>, - /// How many of `sources` the items before each FROM item give, in FROM order. + /// How many of `sources` come before each FROM part, in `from_parts` order, then the total. pub(crate) starts: Vec, } @@ -223,7 +225,7 @@ pub(crate) struct FromItems { pub(crate) sources: Vec, pub(crate) merged: Vec, pub(crate) rows: RowReads, - /// How many sources the items before each FROM item give, in FROM order. + /// How many sources come before each FROM part, in `from_parts` order, then the total. pub(crate) starts: Vec, } @@ -487,3 +489,14 @@ impl Family { } } } + +/// A FROM part, the join that adds it, and the parts on each side of that join, by `from_parts` index. +#[derive(Debug)] +pub(crate) struct FromPart<'a> { + pub(crate) item: &'a Expression, + pub(crate) join: Option<&'a Join>, + /// The parts before it in its own parentheses, or every earlier part outside any. + pub(crate) left: Range, + /// The part itself, or the whole parenthesised join it starts when the join adds that. + pub(crate) right: Range, +} diff --git a/crates/sqlquack-semantics/src/postgres/profile/tests/test_statement_findings.rs b/crates/sqlquack-semantics/src/postgres/profile/tests/test_statement_findings.rs index ebff80d1d..789beaaba 100644 --- a/crates/sqlquack-semantics/src/postgres/profile/tests/test_statement_findings.rs +++ b/crates/sqlquack-semantics/src/postgres/profile/tests/test_statement_findings.rs @@ -167,3 +167,101 @@ fn given_from_items_that_read_each_other_when_resolving_names_then_each_reads_on ); } } + +#[test] +fn given_a_join_in_parentheses_when_resolving_names_then_its_conditions_and_items_are_checked() { + let test_cases = [ + QueryFindingsTestCase { + description: "an unknown column in a parenthesised join's ON", + sql: "SELECT o.id FROM (orders o JOIN customers c ON o.nope = c.id)", + expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 49)], + }, + QueryFindingsTestCase { + description: "a USING key one side lacks", + sql: "SELECT o.id FROM (orders o JOIN customers c USING (nope))", + expected_findings: &[( + "E201", + "JOIN USING column 'nope' must exist on both sides", + 7, + )], + }, + QueryFindingsTestCase { + description: "a scalar subquery of two columns in a parenthesised join's ON", + sql: "SELECT o.id FROM (orders o JOIN customers c ON o.id = (SELECT id, name FROM customers))", + expected_findings: &[("E216", "Scalar subquery must return one column", 47)], + }, + QueryFindingsTestCase { + description: "an alias inside the parentheses repeated after them", + sql: "SELECT o.id FROM (orders o JOIN customers c ON true) JOIN customers c ON true", + expected_findings: &[("E233", "Duplicate table alias 'c'", 0)], + }, + QueryFindingsTestCase { + description: "a derived table in a parenthesised join joined to an earlier table", + sql: "SELECT o.id FROM customers c2 JOIN (orders o JOIN (SELECT nope FROM customers) d \ + ON o.id = d.nope) ON c2.id = o.id", + expected_findings: &[("E201", "Unknown column 'nope' in table 'customers'", 58)], + }, + QueryFindingsTestCase { + description: "a LATERAL item reading the table before it inside the parentheses", + sql: "SELECT l.n FROM (orders o JOIN LATERAL (SELECT o.id AS n) l ON true)", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a derived table without LATERAL reading the table before it inside the parentheses", + sql: "SELECT l.n FROM (orders o JOIN (SELECT o.id AS n) l ON true)", + expected_findings: &[( + "E222", + "Unknown table or alias 'o' referenced by column 'id'", + 39, + )], + }, + QueryFindingsTestCase { + description: "USING inside the parentheses, its left side only the tables there", + sql: "SELECT y.id FROM orders JOIN (customers JOIN orders y USING (customer_id)) ON true", + expected_findings: &[( + "E201", + "JOIN USING column 'customer_id' must exist on both sides", + 7, + )], + }, + QueryFindingsTestCase { + description: "USING after the parentheses, its right side every table there", + sql: "SELECT y.id FROM orders JOIN (customers JOIN orders y ON true) USING (customer_id)", + expected_findings: &[], + }, + ]; + let schema = duckdb_shop_schema(); + for test_case in test_cases { + let statement = sqlquack_syntax_postgres::parse_strict(test_case.sql) + .unwrap_or_else(|failure| panic!("{}: {failure:?}", test_case.description)) + .statements + .remove(0); + let context = LineageContext { + profile: &POSTGRES, + schema: &schema, + sql: Some(test_case.sql), + }; + let native: Vec<(&'static str, String, usize)> = statement_findings(&statement, context) + .into_iter() + .map(|finding| { + ( + finding.code, + finding.message, + finding.span.map_or(0, |span| span.start), + ) + }) + .collect(); + assert_eq!( + found_findings(test_case.sql, (Dialect::PostgreSQL, &POSTGRES, &schema)), + owned_findings(test_case.expected_findings), + "{}", + test_case.description + ); + assert_eq!( + native, + owned_findings(test_case.expected_findings), + "{}, PostgreSQL's parser", + test_case.description + ); + } +} diff --git a/crates/sqlquack-syntax-bigquery/src/parse/_helpers/tree/nodes.rs b/crates/sqlquack-syntax-bigquery/src/parse/_helpers/tree/nodes.rs index e191e9b3c..066ee242e 100644 --- a/crates/sqlquack-syntax-bigquery/src/parse/_helpers/tree/nodes.rs +++ b/crates/sqlquack-syntax-bigquery/src/parse/_helpers/tree/nodes.rs @@ -394,6 +394,7 @@ pub(crate) fn subquery(this: Expression, alias: Option, lateral: boo table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } diff --git a/crates/sqlquack-syntax-clickhouse/src/parse/_helpers/tree/nodes.rs b/crates/sqlquack-syntax-clickhouse/src/parse/_helpers/tree/nodes.rs index 051d3563b..a71c56883 100644 --- a/crates/sqlquack-syntax-clickhouse/src/parse/_helpers/tree/nodes.rs +++ b/crates/sqlquack-syntax-clickhouse/src/parse/_helpers/tree/nodes.rs @@ -387,6 +387,7 @@ pub(crate) fn subquery(this: Expression, alias: Option, lateral: boo table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/alters.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/alters.rs index f8ea11ffe..cdb3f15d9 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/alters.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/alters.rs @@ -6,7 +6,7 @@ use sqlquack_syntax::expressions::columns::models::{ AlterColumnAction, AlterTable, AlterTableAction, ColumnDef, ColumnPosition, MaskingPolicyColumnConstraint, }; -use sqlquack_syntax::expressions::names::models::{Identifier, TableRef}; +use sqlquack_syntax::expressions::names::models::{Identifier, Literal, TableRef}; use sqlquack_syntax::expressions::schema_objects::models::{ AlterObject, AlterView, AlterViewAction, }; @@ -42,19 +42,30 @@ pub(crate) fn property_text(expression: &Expression) -> String { } } -/// The positions of the property `keys` written in backquotes, and of those written as strings. -pub(crate) fn key_spellings<'a>( - keys: impl IntoIterator, -) -> (Vec, Vec) { - let (mut quoted, mut strings) = (Vec::new(), Vec::new()); +/// Where the property `keys` were backquoted or strings, and each raw or adjacent-string key as written. +pub(crate) fn key_spellings<'a>(keys: impl IntoIterator) -> KeySpellings { + let mut spellings: KeySpellings = KeySpellings::default(); for (position, key) in keys.into_iter().enumerate() { match key { - Expression::Literal(_) => strings.push(position), - Expression::Identifier(name) if name.quoted => quoted.push(position), + Expression::Literal(literal) => { + spellings.strings.push(position); + if !matches!(literal.as_ref(), Literal::String(_)) { + spellings.written.push((position, literal.as_ref().clone())); + } + } + Expression::Identifier(name) if name.quoted => spellings.quoted.push(position), _ => {} } } - (quoted, strings) + spellings +} + +/// How property keys were written, by position. +#[derive(Default)] +pub(crate) struct KeySpellings { + pub(crate) quoted: Vec, + pub(crate) strings: Vec, + pub(crate) written: Vec<(usize, Literal)>, } /// A property as its key and value; a key without a value has a null value. @@ -871,15 +882,16 @@ impl Parser<'_> { .into_iter() .map(property_pair) .collect(); - let (quoted_keys, string_keys) = key_spellings(pairs.iter().map(|(key, _)| key)); + let spellings: KeySpellings = key_spellings(pairs.iter().map(|(key, _)| key)); let expressions: Vec<(String, Expression)> = pairs .into_iter() .map(|(key, value)| (property_text(&key), value)) .collect(); Ok(Some(AlterTableAction::SetTag { expressions, - quoted_keys, - string_keys, + quoted_keys: spellings.quoted, + string_keys: spellings.strings, + written_keys: spellings.written, })) } "OWNER" => { @@ -940,11 +952,12 @@ impl Parser<'_> { } if self.eat_kw("TAGS") { let keys: Vec = self.property_list()?; - let (quoted_keys, string_keys) = key_spellings(&keys); + let spellings: KeySpellings = key_spellings(&keys); return Ok(Some(AlterTableAction::UnsetTag { names: keys.iter().map(property_text).collect(), - quoted_keys, - string_keys, + quoted_keys: spellings.quoted, + string_keys: spellings.strings, + written_keys: spellings.written, })); } self.expect_kw("MANAGED")?; diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/delta.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/delta.rs index 8a8d498c2..84553afa9 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/delta.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/delta.rs @@ -8,7 +8,7 @@ use sqlquack_syntax::expressions::statements::models::{ }; use sqlquack_syntax::expressions::tables::models::Undrop; -use crate::parse::_helpers::commands::alters::{key_spellings, property_text}; +use crate::parse::_helpers::commands::alters::{KeySpellings, key_spellings, property_text}; use crate::parse::_helpers::tree::cursor::{Parser, Parsing}; use crate::parse::_helpers::tree::nodes::{ literal, null, property, subquery, too_deep_to_keep, utility, var, @@ -65,13 +65,17 @@ impl Parser<'_> { quoted_keys: Vec::new(), string_keys: Vec::new(), bare_values: Vec::new(), + written_keys: Vec::new(), }; if self.eat_kw("CREDENTIAL") { match self.is_char('(') { true => { let options = self.spelled_valued_property_list()?; - (credentials.quoted_keys, credentials.string_keys) = + let spellings: KeySpellings = key_spellings(options.iter().map(|((key, _), _)| key)); + credentials.quoted_keys = spellings.quoted; + credentials.string_keys = spellings.strings; + credentials.written_keys = spellings.written; for (position, ((key, value), without_equals)) in options.into_iter().enumerate() { diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/dml.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/dml.rs index 76a395489..e1c6dd623 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/dml.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/dml.rs @@ -12,8 +12,8 @@ use crate::parse::_helpers::tree::cursor::{ Parser, Parsing, is_identifier, is_strict_identifier, is_word, }; use crate::parse::_helpers::tree::nodes::{ - TableAlias, binary, boolean, call, column_from, default_value, delete, insert, joined, star, - subquery, table, tuple, update, var, + TableAlias, binary, boolean, call, column_from, default_value, delete, insert, joined, paren, + star, subquery, table, tuple, update, var, }; use crate::parse::constants::{IDENTIFIER_CLAUSE, REPLACE_ON_QUERY_TAKERS}; use crate::scan::models::{Token, TokenKind}; @@ -273,6 +273,7 @@ impl Parser<'_> { } let query: Expression = match self.is_char('(') && self.starts_query_at(1) { true => { + let first: usize = self.at; self.at += 1; let query: Expression = self.query()?; self.expect_char(')')?; @@ -280,7 +281,7 @@ impl Parser<'_> { match alias { Some(alias) => subquery(query, Some(alias), false), None if node.replace_on.is_some() => subquery(query, None, false), - None => query, + None => self.spanned(first, paren(query)), } } false => self.query()?, diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/from.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/from.rs index 6fbeb7d03..dc44dca49 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/from.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/from.rs @@ -854,8 +854,7 @@ impl Parser<'_> { } let key: Expression = match self.string_at(0) { true if !self.char_at(strings, '=') => { - let token = self.advance()?; - crate::parse::_helpers::tree::nodes::string(&token.value) + Expression::Literal(Box::new(self.one_string()?)) } true => self.string_literal()?, false => crate::parse::_helpers::tree::nodes::qualified(self.multipart_identifier()?), diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/queries.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/queries.rs index 7a4330a01..20b3617ed 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/queries.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/queries.rs @@ -86,11 +86,23 @@ fn is_plain_star(select: &Select) -> bool { && select.sample.is_none() } +/// Whether `select` is a written `SELECT * FROM ( query )`, the shape a pipe step wraps. +fn written_wrapper(select: &Select) -> bool { + select.pipe_operators.is_empty() + && select.joins.is_empty() + && matches!( + select.from.as_ref().map(|from| from.expressions.as_slice()), + Some([Expression::Subquery(subquery)]) if subquery.alias.is_none() + ) +} + /// The select a pipe operator extends: `left` itself, or else `SELECT * FROM (left)`. fn pipe_input(left: Expression, bare: bool) -> Select { match left { Expression::Select(select) - if is_plain_star(&select) && !(bare && select.where_clause.is_some()) => + if is_plain_star(&select) + && !(bare && select.where_clause.is_some()) + && !written_wrapper(&select) => { *select } @@ -142,41 +154,18 @@ fn organized_parenthesized(this: Expression, organization: Organization) -> Expr subquery.sort_by = organization.sort.map(|expressions| SortBy { expressions }); subquery.limit = organization.limit.map(limit); subquery.offset = organization.offset.map(|this| Offset { this, rows: None }); + subquery.windows = organization.windows; } node } -/// Whether the query within any parentheses of `body` can take every clause of `organization`. -fn takes_within(body: &Expression, organization: &Organization) -> bool { - match body { - Expression::Paren(paren) => takes_within(&paren.this, organization), - other => takes(other, organization), - } -} - -/// `body` with `organization` applied to the query within its parentheses, which stay. -fn organized_within(body: Expression, organization: Organization) -> Expression { - match body { - Expression::Paren(mut paren) => { - paren.this = organized_within(paren.this, organization); - Expression::Paren(paren) - } - other => organized(other, organization), - } -} - /// `body` with `organization` applied, wrapped first when it cannot take it as it is. fn organized(body: Expression, organization: Organization) -> Expression { if organization.is_empty() { return body; } let body: Expression = match body { - Expression::Paren(paren) if organization.windows.is_none() => { - return organized_parenthesized(paren.this, organization); - } - Expression::Paren(paren) if takes_within(&paren.this, &organization) => { - return subquery(organized_within(paren.this, organization), None, false); - } + Expression::Paren(paren) => return organized_parenthesized(paren.this, organization), other => other, }; let mut body: Expression = match takes(&body, &organization) { diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/tree/nodes.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/tree/nodes.rs index d3a9969ba..0fec975cf 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/tree/nodes.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/tree/nodes.rs @@ -470,6 +470,7 @@ pub(crate) fn subquery(this: Expression, alias: Option, lateral: boo table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/data_types.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/data_types.rs index 14f5e09fd..ae840a246 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/data_types.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/data_types.rs @@ -144,7 +144,7 @@ impl Parser<'_> { "TIMESTAMP" => { let precision: Option = self.optional_length()?; let without: bool = !self.kw("WITH") && self.eat_kws(&["WITHOUT", "TIME", "ZONE"]); - let local: bool = self.eat_kws(&["WITH", "LOCAL", "TIME", "ZONE"]); + let local: bool = !without && self.eat_kws(&["WITH", "LOCAL", "TIME", "ZONE"]); DataType::Timestamp { precision, timezone: local || !without, diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/primary.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/primary.rs index 6edb83569..e22870d51 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/primary.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/primary.rs @@ -240,9 +240,31 @@ impl Parser<'_> { Ok(text) } - /// `stringLit` as a string literal. + /// `stringLit` as a string literal: a single raw string stays raw, adjacent strings keep their pieces. pub(crate) fn string_literal(&mut self) -> Parsing { - Ok(string(&self.string_lit()?)) + if !self.string_at(0) { + return Err(self.error()); + } + let mut pieces: Vec = Vec::new(); + while self.string_at(0) { + pieces.push(self.one_string()?); + } + Ok(match pieces.len() { + 1 => literal(pieces.remove(0)), + _ => { + let joined: String = pieces.iter().map(Literal::value_str).collect(); + literal(Literal::JoinedString(joined, pieces)) + } + }) + } + + /// One string token as a literal; a raw string stays raw. + pub(crate) fn one_string(&mut self) -> Parsing { + let token: Token = self.advance()?; + Ok(match token.kind { + TokenKind::String { raw: true } => Literal::RawString(token.value), + _ => Literal::String(token.value), + }) } fn primary_base(&mut self) -> Parsing { @@ -254,10 +276,6 @@ impl Parser<'_> { self.at += 1; Ok(number_literal(&token, kind)) } - TokenKind::String { raw: true } if !self.string_at(1) => { - self.at += 1; - Ok(literal(Literal::RawString(token.value))) - } TokenKind::String { .. } => self.string_literal(), TokenKind::Char('(') => self.parenthesized_primary(), TokenKind::Char('*') => { diff --git a/crates/sqlquack-syntax-databricks/src/parse/tests/test_parse.rs b/crates/sqlquack-syntax-databricks/src/parse/tests/test_parse.rs index e7e2afdc4..f42527539 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/tests/test_parse.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/tests/test_parse.rs @@ -18,6 +18,21 @@ fn given_sql_when_parsing_then_the_verdict_is_databricks() { sql: "", expected_verdict: ExpectedVerdict::Rejected { syntax: true }, }, + VerdictTestCase { + description: "TIMESTAMP takes one time zone clause, not WITHOUT and then WITH LOCAL", + sql: "SELECT CAST ( v1 AS TIMESTAMP WITHOUT TIME ZONE WITH LOCAL TIME ZONE )", + expected_verdict: ExpectedVerdict::Rejected { syntax: true }, + }, + VerdictTestCase { + description: "TIMESTAMP WITHOUT TIME ZONE alone", + sql: "SELECT CAST ( v1 AS TIMESTAMP WITHOUT TIME ZONE )", + expected_verdict: ExpectedVerdict::Accepted { opaque: false }, + }, + VerdictTestCase { + description: "TIMESTAMP WITH LOCAL TIME ZONE alone", + sql: "SELECT CAST ( v1 AS TIMESTAMP WITH LOCAL TIME ZONE )", + expected_verdict: ExpectedVerdict::Accepted { opaque: false }, + }, VerdictTestCase { description: "STREAM's parentheses hold a table name or a table function call, with no alias", sql: "SELECT * FROM STREAM ( t1 a1 )", diff --git a/crates/sqlquack-syntax-databricks/src/parse/tests/test_written_forms.rs b/crates/sqlquack-syntax-databricks/src/parse/tests/test_written_forms.rs index df7f5c3c1..61240233c 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/tests/test_written_forms.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/tests/test_written_forms.rs @@ -437,6 +437,40 @@ fn given_one_form_written_in_different_ways_when_parsing_then_each_tree_keeps_wh ], expected_distinct_trees: 3, }, + DistinctTreesTestCase { + description: "an insert's query in no, one and two pairs of parentheses", + written: &[ + "INSERT INTO t1 SELECT 1", + "INSERT INTO t1 ( SELECT 1 )", + "INSERT INTO t1 ( ( SELECT 1 ) )", + "INSERT INTO t1 VALUES ( 1 )", + "INSERT INTO t1 ( VALUES ( 1 ) )", + "INSERT OVERWRITE t1 SELECT 1", + "INSERT OVERWRITE t1 ( SELECT 1 )", + "INSERT INTO t1 ( c1 ) SELECT 1", + "INSERT INTO t1 ( c1 ) ( SELECT 1 )", + "INSERT INTO t1 ( c1 ) ( ( SELECT 1 ) )", + "INSERT INTO t1 ( c1 ) VALUES ( 1 )", + "INSERT INTO t1 ( c1 ) ( VALUES ( 1 ) )", + ], + expected_distinct_trees: 12, + }, + DistinctTreesTestCase { + description: "a pipe operator after a query in no, one and two pairs of parentheses", + written: &[ + "SELECT 1 |> WHERE 1 = 1", + "( SELECT 1 ) |> WHERE 1 = 1", + "( ( SELECT 1 ) ) |> WHERE 1 = 1", + "SELECT * FROM ( SELECT 1 ) |> WHERE 1 = 1", + "SELECT * FROM ( ( SELECT 1 ) ) |> WHERE 1 = 1", + "( SELECT 1 ) |> TABLESAMPLE ( 1 ROWS )", + "SELECT * FROM ( ( SELECT 1 ) ) |> TABLESAMPLE ( 1 ROWS )", + "SELECT * FROM ( SELECT 1 ) WHERE 1 = 1 |> WHERE 2 = 2", + "SELECT 1 |> WHERE 1 = 1 |> WHERE 2 = 2", + "SELECT * FROM ( SELECT 1 ) a1 |> WHERE 1 = 1", + ], + expected_distinct_trees: 10, + }, DistinctTreesTestCase { description: "REPLACE ON's query with and without parentheses", written: &[ @@ -784,17 +818,56 @@ fn given_one_form_written_in_different_ways_when_parsing_then_each_tree_keeps_wh expected_distinct_trees: 6, }, DistinctTreesTestCase { - description: "a tag key written as a name, backquoted and as a string", + description: "a tag key written as a name, backquoted, as a string and as a raw string", written: &[ "ALTER TABLE t1 UNSET TAGS ( k )", "ALTER TABLE t1 UNSET TAGS ( `k` )", "ALTER TABLE t1 UNSET TAGS ( 'k' )", + "ALTER TABLE t1 UNSET TAGS ( r'k' )", "ALTER TABLE t1 SET TAGS ( k = 'v' )", "ALTER TABLE t1 SET TAGS ( `k` = 'v' )", "ALTER TABLE t1 SET TAGS ( 'k' = 'v' )", + "ALTER TABLE t1 SET TAGS ( r'k' = 'v' )", + ], + expected_distinct_trees: 8, + }, + DistinctTreesTestCase { + description: "WINDOW after a parenthesized query apart from WINDOW over a star of it", + written: &[ + "( SELECT 1 LIMIT 1 ) WINDOW w AS ( )", + "SELECT * FROM ( ( SELECT 1 LIMIT 1 ) ) WINDOW w AS ( )", + "( SELECT 1 ) WINDOW w AS ( )", + "SELECT * FROM ( ( SELECT 1 ) ) WINDOW w AS ( )", + "( SELECT 1 WINDOW w AS ( ) )", + ], + expected_distinct_trees: 5, + }, + DistinctTreesTestCase { + description: "WINDOW after a parenthesized query apart from WINDOW inside it, the query \ + able to take it", + written: &[ + "( SELECT 1 ) ORDER BY 1 WINDOW w AS ( )", + "( SELECT 1 ORDER BY 1 ) WINDOW w AS ( )", + "WITH c AS ( SELECT 1 ) ( SELECT 1 ) WINDOW w AS ( )", + "WITH c AS ( SELECT 1 ) ( SELECT 1 WINDOW w AS ( ) )", + "WITH c AS ( SELECT 1 ) ( ( SELECT 1 ) ) WINDOW w AS ( )", + "WITH c AS ( SELECT 1 ) ( ( SELECT 1 WINDOW w AS ( ) ) )", ], expected_distinct_trees: 6, }, + DistinctTreesTestCase { + description: "adjacent strings apart from the one string they join to", + written: &[ + "SELECT 'a' 'b'", + "SELECT 'ab'", + "SELECT r'a' 'b'", + "SELECT 'a' 'b' 'c'", + "SELECT 'ab' 'c'", + "ALTER TABLE t1 SET TAGS ( 'a' 'b' = 'v' )", + "ALTER TABLE t1 SET TAGS ( 'ab' = 'v' )", + ], + expected_distinct_trees: 7, + }, ]; for test_case in test_cases { assert_eq!( diff --git a/crates/sqlquack-syntax-duckdb/src/parse/_helpers/tree/nodes.rs b/crates/sqlquack-syntax-duckdb/src/parse/_helpers/tree/nodes.rs index 40e39e0d9..5aaa954af 100644 --- a/crates/sqlquack-syntax-duckdb/src/parse/_helpers/tree/nodes.rs +++ b/crates/sqlquack-syntax-duckdb/src/parse/_helpers/tree/nodes.rs @@ -371,6 +371,7 @@ pub(crate) fn subquery(this: Expression, alias: Option, lateral: boo table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } diff --git a/crates/sqlquack-syntax-postgres/src/parse/_helpers/tree/nodes.rs b/crates/sqlquack-syntax-postgres/src/parse/_helpers/tree/nodes.rs index 16d04a076..dd6c7289a 100644 --- a/crates/sqlquack-syntax-postgres/src/parse/_helpers/tree/nodes.rs +++ b/crates/sqlquack-syntax-postgres/src/parse/_helpers/tree/nodes.rs @@ -362,6 +362,7 @@ pub(crate) fn subquery(this: Expression, alias: Option, lateral: boo table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/scripting.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/scripting.rs index 4d24724fc..74edab71f 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/scripting.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/scripting.rs @@ -923,6 +923,7 @@ impl Parser { quoted_keys: Vec::new(), string_keys: Vec::new(), bare_values: Vec::new(), + written_keys: Vec::new(), }; credentials = Some(Box::new(creds)); } @@ -960,6 +961,7 @@ impl Parser { quoted_keys: Vec::new(), string_keys: Vec::new(), bare_values: Vec::new(), + written_keys: Vec::new(), }; credentials = Some(Box::new(creds)); } diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/grouping.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/grouping.rs index eb5e82bc0..ef7f313c4 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/grouping.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/grouping.rs @@ -686,6 +686,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, }; let explicit_as = self.match_token(TokenType::As); if explicit_as || self.is_identifier_token() || self.is_safe_keyword_as_identifier() { diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/paths.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/paths.rs index df6ad1e24..73623f351 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/paths.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/paths.rs @@ -693,6 +693,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, }))) } @@ -748,6 +749,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } else { Expression::Subquery(Box::new(Subquery { @@ -770,6 +772,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) }; @@ -830,6 +833,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } else { set_result @@ -911,6 +915,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, }))) } _ => None, @@ -963,6 +968,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, }))); } } diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/snowflake_calls.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/snowflake_calls.rs index 75282ad56..f1944b84a 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/snowflake_calls.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/snowflake_calls.rs @@ -130,6 +130,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })))) } diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/typed.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/typed.rs index c4f2796d0..1535e613d 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/typed.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/typed.rs @@ -685,6 +685,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })); return Ok(Expression::Function(Box::new(Function { diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/clauses.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/clauses.rs index cc76db9f1..8aca194eb 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/clauses.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/clauses.rs @@ -703,6 +703,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, }, }; subquery @@ -759,6 +760,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, }))) } else { Ok(Expression::Subquery(Box::new(Subquery { @@ -781,6 +783,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, }))) } } else { diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/modifiers.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/modifiers.rs index 77935b899..457b7fb4a 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/modifiers.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/modifiers.rs @@ -850,6 +850,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, }))) } else if self.check(TokenType::LParen) { let inner = self.parse_select_or_paren_select()?; @@ -880,6 +881,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, }))) } else { Err(self.parse_error("Expected SELECT, VALUES, or ( after (")) diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/select.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/select.rs index 4c13fa7cb..a31534f63 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/select.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/select.rs @@ -631,6 +631,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } else { self.parse_table_expression_primary()? @@ -1329,6 +1330,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } else { let table_expr = self.parse_table_expression()?; @@ -1353,6 +1355,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } } else { @@ -1528,6 +1531,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } else if self.check(TokenType::Select) || self.check(TokenType::With) @@ -1567,6 +1571,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })); self.parse_set_operation(left)? } else { @@ -1592,6 +1597,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } else if self.check(TokenType::LParen) { let inner = self.parse_table_expression()?; @@ -1689,6 +1695,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } else if self.check(TokenType::Limit) || self.check(TokenType::Offset) { let limit = if self.match_token(TokenType::Limit) { @@ -1728,6 +1735,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } else { result @@ -1789,6 +1797,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } }) diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/dml.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/dml.rs index 4244c6965..42ffb197e 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/dml.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/dml.rs @@ -300,6 +300,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, }, _ => return Err(self.parse_error("Expected a query in DELETE USING ( )")), }; diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/entry.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/entry.rs index 3d6a958a4..e8e41c366 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/entry.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/entry.rs @@ -839,6 +839,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })); let result = self.parse_set_operation(subquery)?; self.parse_query_modifiers(result) @@ -867,6 +868,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })); let result = self.parse_set_operation(subquery)?; let pre_alias_comments = self.previous_trailing_comments().to_vec(); @@ -1110,6 +1112,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })); let result = self.parse_set_operation(subquery)?; self.parse_query_modifiers(result) @@ -1138,6 +1141,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })); let result = self.parse_set_operation(subquery)?; let pre_alias_comments = self.previous_trailing_comments().to_vec(); @@ -1412,6 +1416,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } else { self.parse_expression()? @@ -1466,6 +1471,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } else { self.parse_expression()? diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/support/names.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/support/names.rs index adab7bef9..26369b5d9 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/support/names.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/support/names.rs @@ -1064,6 +1064,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, }))) } else { self.current -= 1; diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/types/data_types.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/types/data_types.rs index 958557dd6..37ac0870d 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/types/data_types.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/types/data_types.rs @@ -1323,6 +1323,7 @@ impl Parser { table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } else { inner diff --git a/crates/sqlquack-syntax-sqlite/src/parse/_helpers/tree/nodes.rs b/crates/sqlquack-syntax-sqlite/src/parse/_helpers/tree/nodes.rs index a9d50d056..bb64ae7b4 100644 --- a/crates/sqlquack-syntax-sqlite/src/parse/_helpers/tree/nodes.rs +++ b/crates/sqlquack-syntax-sqlite/src/parse/_helpers/tree/nodes.rs @@ -287,6 +287,7 @@ pub(crate) fn subquery(this: Expression, alias: Option, lateral: boo table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } diff --git a/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/tree/nodes.rs b/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/tree/nodes.rs index 29f5f65b7..b52989426 100644 --- a/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/tree/nodes.rs +++ b/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/tree/nodes.rs @@ -300,6 +300,7 @@ pub(crate) fn subquery(this: Expression, alias: Option, lateral: boo table_sample: None, watermark: None, ctes_outside: false, + windows: None, })) } diff --git a/crates/sqlquack-syntax/src/expressions/columns/models.rs b/crates/sqlquack-syntax/src/expressions/columns/models.rs index 57e514d1c..2f017fa57 100644 --- a/crates/sqlquack-syntax/src/expressions/columns/models.rs +++ b/crates/sqlquack-syntax/src/expressions/columns/models.rs @@ -4,6 +4,7 @@ use serde::Deserialize; use serde::Serialize; use crate::expressions::names::models::Identifier; +use crate::expressions::names::models::Literal; use crate::expressions::names::models::TableRef; use crate::expressions::tables::models::ConstraintType; use crate::expressions::tables::models::IndexKeyPart; @@ -806,6 +807,9 @@ pub enum AlterTableAction { /// Databricks: the positions of the keys written as strings #[serde(default, skip_serializing_if = "Vec::is_empty")] string_keys: Vec, + /// Databricks: the keys written as a raw string or as adjacent strings, by position, as written + #[serde(default, skip_serializing_if = "Vec::is_empty")] + written_keys: Vec<(usize, Literal)>, }, /// UNSET TAG action (Snowflake): ALTER TABLE t UNSET TAG key1, key2 UnsetTag { @@ -816,6 +820,9 @@ pub enum AlterTableAction { /// Databricks: the positions of the names written as strings #[serde(default, skip_serializing_if = "Vec::is_empty")] string_keys: Vec, + /// Databricks: the keys written as a raw string or as adjacent strings, by position, as written + #[serde(default, skip_serializing_if = "Vec::is_empty")] + written_keys: Vec<(usize, Literal)>, }, /// SET with parenthesized options (TSQL): ALTER TABLE t SET (SYSTEM_VERSIONING=ON, ...) SetOptions { @@ -934,6 +941,7 @@ impl AlterTableAction { expressions, quoted_keys: Vec::new(), string_keys: Vec::new(), + written_keys: Vec::new(), } } @@ -943,6 +951,7 @@ impl AlterTableAction { names, quoted_keys: Vec::new(), string_keys: Vec::new(), + written_keys: Vec::new(), } } } diff --git a/crates/sqlquack-syntax/src/expressions/names/models.rs b/crates/sqlquack-syntax/src/expressions/names/models.rs index 968714e14..da67fe83c 100644 --- a/crates/sqlquack-syntax/src/expressions/names/models.rs +++ b/crates/sqlquack-syntax/src/expressions/names/models.rs @@ -48,6 +48,8 @@ pub enum Literal { DollarString(String), /// Raw string r"..." (BigQuery, Spark, Databricks): backslashes are literal, not escapes. RawString(String), + /// Adjacent string literals read as one (`'a' 'b'`): the joined value and each piece as written. + JoinedString(String, Vec), } impl Literal { @@ -68,7 +70,7 @@ impl Literal { | Literal::EscapeString(s) | Literal::DollarString(s) | Literal::RawString(s) => s.as_str(), - Literal::TripleQuotedString(s, _) => s.as_str(), + Literal::TripleQuotedString(s, _) | Literal::JoinedString(s, _) => s.as_str(), } } @@ -82,6 +84,7 @@ impl Literal { | Literal::DollarString(_) | Literal::RawString(_) | Literal::TripleQuotedString(_, _) + | Literal::JoinedString(_, _) ) } @@ -111,6 +114,10 @@ impl fmt::Display for Literal { Literal::EscapeString(s) => write!(f, "E'{}'", s), Literal::DollarString(s) => write!(f, "$${}$$", s), Literal::RawString(s) => write!(f, "r'{}'", s), + Literal::JoinedString(_, pieces) => { + let written: Vec = pieces.iter().map(Literal::to_string).collect(); + write!(f, "{}", written.join(" ")) + } } } } diff --git a/crates/sqlquack-syntax/src/expressions/queries/models.rs b/crates/sqlquack-syntax/src/expressions/queries/models.rs index 957290118..311f56532 100644 --- a/crates/sqlquack-syntax/src/expressions/queries/models.rs +++ b/crates/sqlquack-syntax/src/expressions/queries/models.rs @@ -529,6 +529,9 @@ pub struct Subquery { /// Databricks: the query's `WITH` was written before these parentheses, not inside them #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub ctes_outside: bool, + /// Databricks: a WINDOW clause written after these parentheses that the query inside could not take + #[serde(default, skip_serializing_if = "Option::is_none")] + pub windows: Option>, /// SQLQuack: the node's source range, recorded by a located parse. #[serde(default, skip_serializing_if = "SourceSpan::is_absent")] #[ast(skip)] diff --git a/crates/sqlquack-syntax/src/expressions/statements/models.rs b/crates/sqlquack-syntax/src/expressions/statements/models.rs index c4ebab583..3171a2051 100644 --- a/crates/sqlquack-syntax/src/expressions/statements/models.rs +++ b/crates/sqlquack-syntax/src/expressions/statements/models.rs @@ -5,6 +5,7 @@ use serde::Deserialize; use serde::Serialize; use crate::expressions::names::models::Identifier; +use crate::expressions::names::models::Literal; use crate::expressions::names::models::TableRef; use crate::expressions::queries::models::From; use crate::expressions::queries::models::Hint; @@ -360,6 +361,9 @@ pub struct Credentials { /// Databricks: the positions of the credential options whose value was not a string, as in `k = 1` #[serde(default, skip_serializing_if = "Vec::is_empty")] pub bare_values: Vec, + /// Databricks: the credential keys written as a raw string or as adjacent strings, by position, as written + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub written_keys: Vec<(usize, Literal)>, } /// Historical data / Time travel (Snowflake) - BEFORE (STATEMENT => ...) or AT (TIMESTAMP => ...)