diff --git a/coverage/syntax-databricks/reviews/ddl.toml b/coverage/syntax-databricks/reviews/ddl.toml index 35a0e2300..768902686 100644 --- a/coverage/syntax-databricks/reviews/ddl.toml +++ b/coverage/syntax-databricks/reviews/ddl.toml @@ -2065,3 +2065,20 @@ note = "Re-read on 18 Nov 2026 after the optional-parentheses fix, at parser tre [review.definitions] "db.ddl.create-recipient" = "4b313315e6c043fc5164e925ceb412cf511fd3172d08797d36374e16bd47622b" + +[[review]] +id = "db-ddl-106" +family = "ddl" +parser_tree = "dafbc1c0aa3e303fd9bbf21b7a8f4d1842d3661e" +reviewed_utc = "2026-11-18T05:46:48Z" +verdict = "changes_requested" +forms = [ + "db.ddl.alter-table", +] +note = "Re-read on 18 Nov 2026 at parser tree dafbc1c0 from the existing 2026.45 receipts, because the parser change that keeps tag key spellings changed two of this form's trees: kb4fe2aa33752 ALTER TABLE t1 UNSET TAGS ( 'k' ) and kcba4490330d8 ALTER TABLE t1 SET TAGS ( 'k' = 'v' ) now record that the key was written as a string (string_keys); nothing else in them changed, and no other case's tree or verdict changed. Of db-ddl-073's findings on this form, SET TBLPROPERTIES without = and REFERENCES ... ON DELETE or ON UPDATE NO ACTION now give trees of their own, and a tag key written as a name, backquoted or as a string now gives three trees. One finding still holds: SET TAGS ( 'k' 'v' ) without = gives the same tree as kcba4490330d8 although the grammar requires =, and no receipt shows what the warehouse does. Held until one exists; the text is in the pending recording bundle, and the parser is not changed on a guess." + +[review.fingerprints] +"db.ddl.alter-table" = "93fd4219816042c99f96fb384a36b9f46b302cf7437e5cef6d3fe18623dfc7ce" + +[review.definitions] +"db.ddl.alter-table" = "45f8d3c275e44a178729e82ee86a7ddf4b83d6b1a93ca41cd0a895c42ad47906" diff --git a/coverage/syntax-databricks/reviews/expressions.toml b/coverage/syntax-databricks/reviews/expressions.toml index 9c984505d..eb05a9894 100644 --- a/coverage/syntax-databricks/reviews/expressions.toml +++ b/coverage/syntax-databricks/reviews/expressions.toml @@ -1224,3 +1224,20 @@ note = "Re-binds db-expressions-057, 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-062" +family = "expressions" +catalogue_sha256 = "ffc590e2daad0e5c31541285aca1a5a7d1ce7e7bedb0c1ccdc4c1cfcdd32473c" +obligations_sha256 = "697332d6178cfd5465cf1ebae8c6d98f956a76ed507c8807032c76c97f48bc11" +parser_tree = "dafbc1c0aa3e303fd9bbf21b7a8f4d1842d3661e" +reviewed_utc = "2026-11-18T05:39:30Z" +verdict = "approved" +forms = [ + "db.expressions.object-type", +] +note = "Re-binds db-expressions-061, not a re-read: that exclusion was bound to parser tree a367f5c7 and went stale when the parser kept backquoted names, tag key spellings, credential value types and every pair of parentheses (parser tree dafbc1c0), 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. 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/crates/sqlquack-lint/src/quality/_helpers/cte_columns.rs b/crates/sqlquack-lint/src/quality/_helpers/cte_columns.rs index d6abb93df..1f96417a8 100644 --- a/crates/sqlquack-lint/src/quality/_helpers/cte_columns.rs +++ b/crates/sqlquack-lint/src/quality/_helpers/cte_columns.rs @@ -18,8 +18,8 @@ use crate::quality::models::QualityRequest; use crate::quality::projection_spans::projection_spans; use crate::quality::removal_safety::{expression_refusal, reads_whole_rows, select_refusal}; use crate::quality::syntax::{ - Projection, columns, cte_select_tokens, lower, projection_removal, projections, qualifier, - sources, + Projection, clauses_around, columns, cte_select_tokens, lower, projection_removal, projections, + qualifier, query_within, sources, }; use rustc_hash::{FxHashMap, FxHashSet}; @@ -58,7 +58,8 @@ pub(crate) fn diagnostics(request: &QualityRequest<'_>) -> Vec { return Vec::new(); } let mut found: Vec = Vec::new(); - for statement in request.statements { + for written in request.statements { + let statement: &Expression = query_within(written); let Expression::Select(root) = statement else { continue; }; @@ -66,8 +67,13 @@ pub(crate) fn diagnostics(request: &QualityRequest<'_>) -> Vec { continue; }; let names: Vec = with.ctes.iter().map(|cte| lower(&cte.alias.name)).collect(); - let used: FxHashMap = - reads(&with.ctes, &names, terminal_nodes(statement, with)); + let mut terminal: Vec<&Expression> = terminal_nodes(statement, with); + terminal.extend( + clauses_around(written) + .into_iter() + .flat_map(|clause| clause.dfs()), + ); + let used: FxHashMap = reads(&with.ctes, &names, terminal); let select_tokens: FxHashMap = cte_select_tokens(request.tokens); for (index, cte) in with .ctes @@ -152,7 +158,7 @@ fn reads<'a>( /// The output names of a CTE, or None when a star or unnamed projection hides them. fn output_names(cte: &Cte) -> Option> { - let Expression::Select(select) = &cte.this else { + let Expression::Select(select) = query_within(&cte.this) else { return None; }; projections(select) @@ -333,7 +339,7 @@ fn clause_roots(select: &Select) -> Vec<&Expression> { /// Lateral alias reuse: a later projection or a clause of the CTE reading an earlier output. fn self_reads(body: &Expression) -> FxHashSet { let mut reads: FxHashSet = FxHashSet::default(); - let Expression::Select(select) = body else { + let Expression::Select(select) = query_within(body) else { return reads; }; let mut earlier: FxHashSet = FxHashSet::default(); @@ -360,6 +366,7 @@ fn self_reads(body: &Expression) -> FxHashSet { if let Some(order) = &select.order_by { clauses.extend(order.expressions.iter().map(|ordered| &ordered.this)); } + clauses.extend(clauses_around(body)); for clause in clauses { reads.extend( unqualified_reads(clause) @@ -384,7 +391,7 @@ fn unused_outputs( outputs: CteOutputs<'_>, ) -> Vec { let CteOutputs { cte, name, usage } = outputs; - let Expression::Select(select) = &cte.this else { + let Expression::Select(select) = query_within(&cte.this) else { return Vec::new(); }; let Some(usage) = usage.filter(|usage| !usage.everything && usage.read) else { diff --git a/crates/sqlquack-lint/src/quality/_helpers/keys.rs b/crates/sqlquack-lint/src/quality/_helpers/keys.rs index 263e5adc2..74781f4df 100644 --- a/crates/sqlquack-lint/src/quality/_helpers/keys.rs +++ b/crates/sqlquack-lint/src/quality/_helpers/keys.rs @@ -9,7 +9,9 @@ use sqlquack_syntax::expressions::queries::models::Select; use crate::quality::constants::FIRST_ROW; use crate::quality::models::QualityRequest; -use crate::quality::syntax::{Projection, columns, lower, projections, qualifier, sources}; +use crate::quality::syntax::{ + Projection, columns, lower, projections, qualifier, query_within, sources, +}; use crate::quality::types::{InputKey, Keys}; use crate::tree::main::is_row_number::is_row_number; @@ -362,13 +364,13 @@ pub(crate) fn key_environment(request: &QualityRequest<'_>) -> KeyEnvironment { row_numbers: HashMap::new(), }; for statement in request.statements { - let Expression::Select(select) = statement else { + let Expression::Select(select) = query_within(statement) else { continue; }; for cte in select.with.iter().flat_map(|with| with.ctes.iter()) { let keys = environment.query_keys(&cte.this); environment.ctes.insert(lower(&cte.alias.name), keys); - if let Expression::Select(body) = &cte.this { + if let Expression::Select(body) = query_within(&cte.this) { environment .row_numbers .insert(lower(&cte.alias.name), row_number_outputs(body)); diff --git a/crates/sqlquack-lint/src/quality/_helpers/syntax.rs b/crates/sqlquack-lint/src/quality/_helpers/syntax.rs index d05e26abc..dc4e3b0d9 100644 --- a/crates/sqlquack-lint/src/quality/_helpers/syntax.rs +++ b/crates/sqlquack-lint/src/quality/_helpers/syntax.rs @@ -2,13 +2,62 @@ use sqlquack_syntax::expressions::names::models::Column; use sqlquack_syntax::expressions::names::models::Star; -use sqlquack_syntax::expressions::queries::models::Select; +use sqlquack_syntax::expressions::queries::models::{Select, Subquery}; use sqlquack_syntax::tokens::models::{Token, TokenType}; use sqlquack_syntax::{Expression, ExpressionWalk}; use crate::quality::constants::MIN_REMOVABLE_OUTPUTS; use rustc_hash::FxHashMap; +/// The query within `expression`'s parentheses and unaliased subquery nodes. +pub(super) fn query_within(expression: &Expression) -> &Expression { + match expression { + Expression::Paren(paren) => query_within(&paren.this), + Expression::Subquery(subquery) if around(subquery) => query_within(&subquery.this), + other => other, + } +} + +/// The clause expressions of the unaliased subquery nodes around `expression`'s query. +pub(super) fn clauses_around(expression: &Expression) -> Vec<&Expression> { + let mut clauses: Vec<&Expression> = Vec::new(); + let mut node: &Expression = expression; + loop { + node = match node { + Expression::Paren(paren) => &paren.this, + Expression::Subquery(subquery) if around(subquery) => { + let ordered = subquery + .order_by + .iter() + .flat_map(|order| &order.expressions) + .chain(subquery.sort_by.iter().flat_map(|sort| &sort.expressions)) + .chain( + subquery + .cluster_by + .iter() + .flat_map(|cluster| &cluster.expressions), + ); + clauses.extend(ordered.map(|ordered| &ordered.this)); + clauses.extend( + subquery + .distribute_by + .iter() + .flat_map(|distribute| &distribute.expressions), + ); + clauses.extend(subquery.limit.iter().map(|limit| &limit.this)); + clauses.extend(subquery.offset.iter().map(|offset| &offset.this)); + &subquery.this + } + _ => return clauses, + }; + } +} + +/// Whether `subquery` only wraps a query: unaliased, without a sample or watermark. +fn around(subquery: &Subquery) -> bool { + subquery.alias.is_none() && subquery.table_sample.is_none() && subquery.watermark.is_none() +} + /// One projection of a select list. pub(super) enum Projection<'a> { Named { @@ -83,23 +132,30 @@ pub(super) fn sources(select: &Select) -> Vec<(String, Option, &Expressi found } -/// Token index of the SELECT opening each `name AS (SELECT` CTE body, by lower-cased name. +/// Token index of the SELECT opening each `name AS ( [( ...] SELECT` CTE body, by lower-cased name. pub(super) fn cte_select_tokens(tokens: &[Token]) -> FxHashMap { let significant: Vec = (0..tokens.len()) .filter(|&index| !is_trivia(&tokens[index])) .collect(); let mut found: FxHashMap = FxHashMap::default(); - for window in significant.windows(4) { - let [name_index, as_index, paren_index, select_index] = window else { + for (position, window) in significant.windows(3).enumerate() { + let [name_index, as_index, paren_index] = window else { continue; }; - if tokens[*as_index].token_type == TokenType::As - && tokens[*paren_index].token_type == TokenType::LParen - && tokens[*select_index].token_type == TokenType::Select + if tokens[*as_index].token_type != TokenType::As + || tokens[*paren_index].token_type != TokenType::LParen + { + continue; + } + let select_index: Option<&usize> = significant[position + 3..] + .iter() + .find(|&&index| tokens[index].token_type != TokenType::LParen); + if let Some(&select_index) = select_index + && tokens[select_index].token_type == TokenType::Select { found .entry(lower(&tokens[*name_index].text)) - .or_insert(*select_index); + .or_insert(select_index); } } found diff --git a/crates/sqlquack-lint/src/quality/tests/test_quality_rules.rs b/crates/sqlquack-lint/src/quality/tests/test_quality_rules.rs index aaf6ea1b4..57eb6d04a 100644 --- a/crates/sqlquack-lint/src/quality/tests/test_quality_rules.rs +++ b/crates/sqlquack-lint/src/quality/tests/test_quality_rules.rs @@ -470,3 +470,119 @@ fn given_threshold_findings_when_linting_then_remediation_shows_the_exact_settin } Ok(()) } + +#[test] +fn given_a_query_or_cte_body_in_parentheses_when_linting_then_its_ctes_are_checked_as_without() +-> Result<(), String> { + let test_cases = [ + QualityRuleTestCase { + description: "an unused CTE output, the query bare", + sql: "WITH orders AS (SELECT o.order_id, o.status, o.note FROM raw_orders AS o), final AS (SELECT r.order_id, r.status FROM orders AS r) SELECT f.order_id, f.status FROM final AS f", + rule: "SQLCLEAN042", + relation_keys: "{}", + expected_anchors: &["o.note"], + expected_fixed_sql: Some( + "WITH orders AS (SELECT o.order_id, o.status FROM raw_orders AS o), final AS (SELECT r.order_id, r.status FROM orders AS r) SELECT f.order_id, f.status FROM final AS f", + ), + }, + QualityRuleTestCase { + description: "an unused CTE output, the whole query in parentheses", + sql: "(WITH orders AS (SELECT o.order_id, o.status, o.note FROM raw_orders AS o), final AS (SELECT r.order_id, r.status FROM orders AS r) SELECT f.order_id, f.status FROM final AS f)", + rule: "SQLCLEAN042", + relation_keys: "{}", + expected_anchors: &["o.note"], + expected_fixed_sql: Some( + "(WITH orders AS (SELECT o.order_id, o.status FROM raw_orders AS o), final AS (SELECT r.order_id, r.status FROM orders AS r) SELECT f.order_id, f.status FROM final AS f)", + ), + }, + QualityRuleTestCase { + description: "an unused CTE output, its CTE body in a second pair", + sql: "WITH orders AS ((SELECT o.order_id, o.status, o.note FROM raw_orders AS o)), final AS (SELECT r.order_id, r.status FROM orders AS r) SELECT f.order_id, f.status FROM final AS f", + rule: "SQLCLEAN042", + relation_keys: "{}", + expected_anchors: &["o.note"], + expected_fixed_sql: Some( + "WITH orders AS ((SELECT o.order_id, o.status FROM raw_orders AS o)), final AS (SELECT r.order_id, r.status FROM orders AS r) SELECT f.order_id, f.status FROM final AS f", + ), + }, + QualityRuleTestCase { + description: "an unused CTE output, WITH before a parenthesized query", + sql: "WITH orders AS (SELECT o.order_id, o.status, o.note FROM raw_orders AS o), final AS (SELECT r.order_id, r.status FROM orders AS r) (SELECT f.order_id, f.status FROM final AS f)", + rule: "SQLCLEAN042", + relation_keys: "{}", + expected_anchors: &["o.note"], + expected_fixed_sql: Some( + "WITH orders AS (SELECT o.order_id, o.status FROM raw_orders AS o), final AS (SELECT r.order_id, r.status FROM orders AS r) (SELECT f.order_id, f.status FROM final AS f)", + ), + }, + QualityRuleTestCase { + description: "an unused CTE output, WITH before a parenthesized query with LIMIT inside and ORDER BY after", + sql: "WITH orders AS (SELECT o.order_id, o.status, o.note FROM raw_orders AS o), final AS (SELECT r.order_id, r.status FROM orders AS r) (SELECT f.order_id, f.status FROM final AS f LIMIT 5) ORDER BY 1", + rule: "SQLCLEAN042", + relation_keys: "{}", + expected_anchors: &["o.note"], + expected_fixed_sql: Some( + "WITH orders AS (SELECT o.order_id, o.status FROM raw_orders AS o), final AS (SELECT r.order_id, r.status FROM orders AS r) (SELECT f.order_id, f.status FROM final AS f LIMIT 5) ORDER BY 1", + ), + }, + QualityRuleTestCase { + description: "a CTE column read only by an ORDER BY after the parentheses stays", + sql: "WITH orders AS (SELECT o.order_id, o.status, o.note FROM raw_orders AS o), other AS (SELECT 1 AS one) (SELECT r.order_id FROM orders AS r LIMIT 5) ORDER BY r.note", + rule: "SQLCLEAN042", + relation_keys: "{}", + expected_anchors: &["o.status"], + expected_fixed_sql: Some( + "WITH orders AS (SELECT o.order_id, o.note FROM raw_orders AS o), other AS (SELECT 1 AS one) (SELECT r.order_id FROM orders AS r LIMIT 5) ORDER BY r.note", + ), + }, + QualityRuleTestCase { + description: "GROUP BY grain is a key, the query bare", + sql: "WITH totals AS (SELECT o.customer_id, SUM(o.amount) AS amount FROM orders AS o GROUP BY o.customer_id), final AS (SELECT t.customer_id, ROW_NUMBER() OVER (ORDER BY t.amount, t.customer_id) AS rn FROM totals AS t) SELECT f.customer_id, f.rn FROM final AS f", + rule: "SQLCLEAN018", + relation_keys: r#"{}"#, + expected_anchors: &[], + expected_fixed_sql: None, + }, + QualityRuleTestCase { + description: "GROUP BY grain is a key, the whole query in parentheses", + sql: "(WITH totals AS (SELECT o.customer_id, SUM(o.amount) AS amount FROM orders AS o GROUP BY o.customer_id), final AS (SELECT t.customer_id, ROW_NUMBER() OVER (ORDER BY t.amount, t.customer_id) AS rn FROM totals AS t) SELECT f.customer_id, f.rn FROM final AS f)", + rule: "SQLCLEAN018", + relation_keys: r#"{}"#, + expected_anchors: &[], + expected_fixed_sql: None, + }, + QualityRuleTestCase { + description: "a later rn = 1 filter makes the partition a key, the query bare", + sql: "WITH ranked AS (SELECT o.order_id, o.customer_id, ROW_NUMBER() OVER (PARTITION BY o.customer_id ORDER BY o.order_id) AS rn FROM orders AS o), latest AS (SELECT r.customer_id, r.order_id FROM ranked AS r WHERE r.rn = 1), final AS (SELECT l.customer_id, ROW_NUMBER() OVER (ORDER BY l.customer_id) AS position FROM latest AS l) SELECT f.customer_id, f.position FROM final AS f", + rule: "SQLCLEAN018", + relation_keys: r#"{"orders": [["order_id"]]}"#, + expected_anchors: &[], + expected_fixed_sql: None, + }, + QualityRuleTestCase { + description: "a later rn = 1 filter makes the partition a key, the ranking CTE body in a second pair", + sql: "WITH ranked AS ((SELECT o.order_id, o.customer_id, ROW_NUMBER() OVER (PARTITION BY o.customer_id ORDER BY o.order_id) AS rn FROM orders AS o)), latest AS (SELECT r.customer_id, r.order_id FROM ranked AS r WHERE r.rn = 1), final AS (SELECT l.customer_id, ROW_NUMBER() OVER (ORDER BY l.customer_id) AS position FROM latest AS l) SELECT f.customer_id, f.position FROM final AS f", + rule: "SQLCLEAN018", + relation_keys: r#"{"orders": [["order_id"]]}"#, + expected_anchors: &[], + expected_fixed_sql: None, + }, + ]; + for test_case in &test_cases { + let diagnostics: Vec = + lint_with_host_fields(test_case, r#"{"dialect": "databricks"}"#)?; + assert_eq!( + anchors(test_case.sql, &diagnostics), + test_case.expected_anchors, + "{}", + test_case.description + ); + assert_eq!( + fixed(test_case.sql, &diagnostics).as_deref(), + test_case.expected_fixed_sql, + "{}", + test_case.description + ); + } + Ok(()) +} diff --git a/crates/sqlquack-semantics/src/databricks/profile/tests/helpers.rs b/crates/sqlquack-semantics/src/databricks/profile/tests/helpers.rs index 303f670db..4e2646874 100644 --- a/crates/sqlquack-semantics/src/databricks/profile/tests/helpers.rs +++ b/crates/sqlquack-semantics/src/databricks/profile/tests/helpers.rs @@ -69,3 +69,15 @@ pub(crate) fn databricks_shop_schema() -> TypedShopSchema { ..TypedShopSchema::default() } } + +/// A query's outputs with their types, and its findings' codes and messages. +type Reading = (Vec<(String, Option)>, Vec<(&'static str, String)>); + +/// The outputs and the findings without their positions, which differ between spellings. +pub(crate) fn reading(sql: &str) -> Reading { + let findings: Vec<(&str, String)> = databricks_findings(sql) + .into_iter() + .map(|(code, message, _)| (code, message)) + .collect(); + (databricks_outputs(sql), findings) +} 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 0c6b117bb..412590d84 100644 --- a/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs +++ b/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs @@ -1,5 +1,6 @@ use crate::databricks::profile::tests::helpers::databricks_findings; use crate::databricks::profile::tests::helpers::databricks_outputs; +use crate::databricks::profile::tests::helpers::reading; use crate::databricks::profile::tests::test_types::DatabricksOutputsTestCase; use crate::databricks::profile::tests::test_types::SameReadingTestCase; use crate::dialects::constants::DATABRICKS; @@ -199,21 +200,33 @@ fn given_a_databricks_query_when_naming_and_typing_outputs_then_they_are_the_war #[test] fn given_a_strict_parse_when_tracing_lineage_then_each_output_has_its_items_byte_range() { - let test_cases = [StrictRangeTestCase { - description: "a literal aliased after a two-byte character, a qualified column, a labelled call, a bare column and a qualified star", - sql: "SELECT 'é' AS e, w.status, UPPER(w.status) loud, id, w.* FROM orders AS w", - expected_ranges: &[ - 7..16, - 18..26, - 28..48, - 50..52, - 54..57, - 54..57, - 54..57, - 54..57, - 54..57, - ], - }]; + let test_cases = [ + StrictRangeTestCase { + description: "a literal aliased after a two-byte character, a qualified column, a labelled call, a bare column and a qualified star", + sql: "SELECT 'é' AS e, w.status, UPPER(w.status) loud, id, w.* FROM orders AS w", + expected_ranges: &[ + 7..16, + 18..26, + 28..48, + 50..52, + 54..57, + 54..57, + 54..57, + 54..57, + 54..57, + ], + }, + StrictRangeTestCase { + description: "a pipe ORDER BY after a parenthesized query", + sql: "( SELECT id, quantity FROM orders ) |> ORDER BY status", + expected_ranges: &[9..11, 13..21], + }, + StrictRangeTestCase { + description: "a pipe LIMIT after a parenthesized query", + sql: "( SELECT id, quantity FROM orders ) |> LIMIT 1", + expected_ranges: &[9..11, 13..21], + }, + ]; for test_case in test_cases { let statement = sqlquack_syntax_databricks::parse_strict(test_case.sql) .unwrap_or_else(|failure| panic!("{}: {failure:?}", test_case.description)) @@ -322,17 +335,148 @@ fn given_synonymous_operator_spellings_when_analysed_then_each_reads_alike() { expected_same_as: "SELECT shiftrightunsigned(quantity, 1) AS a FROM orders WHERE shiftrightunsigned(quantity, 1)", }, ]; - let read = |sql: &str| { - let findings: Vec<(&str, String)> = databricks_findings(sql) - .into_iter() - .map(|(code, message, _)| (code, message)) - .collect(); - (databricks_outputs(sql), findings) - }; for test_case in test_cases { assert_eq!( - read(test_case.sql), - read(test_case.expected_same_as), + reading(test_case.sql), + reading(test_case.expected_same_as), + "{}", + test_case.description + ); + } +} + +#[test] +fn given_a_relation_or_query_in_extra_parentheses_when_analysed_then_it_reads_as_without() { + let test_cases = [ + SameReadingTestCase { + description: "a table in two pairs of parentheses", + sql: "SELECT id, nope FROM ( ( orders ) )", + expected_same_as: "SELECT id, nope FROM orders", + }, + SameReadingTestCase { + description: "an aliased table in two pairs of parentheses", + sql: "SELECT o.id, o.nope FROM ( ( orders ) ) o", + expected_same_as: "SELECT o.id, o.nope FROM orders o", + }, + SameReadingTestCase { + description: "a table function in parentheses", + sql: "SELECT r.id FROM ( range ( 3 ) ) r", + expected_same_as: "SELECT r.id FROM range ( 3 ) r", + }, + SameReadingTestCase { + description: "a join in a second pair of parentheses", + sql: "SELECT o.id, c.name FROM ( ( orders o JOIN customers c ON o.id = c.id ) )", + expected_same_as: "SELECT o.id, c.name FROM ( orders o JOIN customers c ON o.id = c.id )", + }, + SameReadingTestCase { + description: "a derived table's query in a second pair of parentheses", + sql: "SELECT x FROM ( ( SELECT id AS x FROM orders ) ) d", + expected_same_as: "SELECT x FROM ( SELECT id AS x FROM orders ) d", + }, + SameReadingTestCase { + description: "IN and EXISTS subqueries in a second pair of parentheses", + sql: "SELECT id FROM orders WHERE id IN ( ( SELECT nope FROM customers ) ) AND EXISTS ( ( SELECT nope FROM customers ) )", + expected_same_as: "SELECT id FROM orders WHERE id IN ( SELECT nope FROM customers ) AND EXISTS ( SELECT nope FROM customers )", + }, + SameReadingTestCase { + description: "a whole query in parentheses", + sql: "( SELECT id, nope FROM orders )", + expected_same_as: "SELECT id, nope FROM orders", + }, + SameReadingTestCase { + description: "a set operation's operands in parentheses", + sql: "( SELECT id FROM orders ) UNION ( SELECT id FROM customers )", + expected_same_as: "SELECT id FROM orders UNION SELECT id 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_parenthesized_query_with_clauses_after_it_when_analysed_then_they_read_its_sources() { + let test_cases = [ + SameReadingTestCase { + description: "ORDER BY a column the query does not output", + sql: "( SELECT id FROM orders ) ORDER BY quantity", + expected_same_as: "SELECT id FROM orders ORDER BY quantity", + }, + SameReadingTestCase { + description: "ORDER BY through the query's table alias", + sql: "( SELECT id FROM orders o ) ORDER BY o.quantity", + expected_same_as: "SELECT id FROM orders o ORDER BY o.quantity", + }, + SameReadingTestCase { + description: "ORDER BY through the table name, then LIMIT", + sql: "( SELECT id FROM orders ) ORDER BY orders.quantity LIMIT 1", + expected_same_as: "SELECT id FROM orders ORDER BY orders.quantity LIMIT 1", + }, + SameReadingTestCase { + description: "an unknown ORDER BY column is still found", + sql: "( SELECT id FROM orders ) ORDER BY nope", + expected_same_as: "SELECT id FROM orders ORDER BY nope", + }, + SameReadingTestCase { + description: "two pairs of parentheses", + sql: "( ( SELECT id FROM orders ) ) ORDER BY nope, quantity", + expected_same_as: "SELECT id FROM orders ORDER BY nope, quantity", + }, + SameReadingTestCase { + description: "inside an IN subquery", + sql: "SELECT id FROM orders WHERE id IN ( ( SELECT id FROM customers ) ORDER BY name )", + expected_same_as: "SELECT id FROM orders WHERE id IN ( SELECT id FROM customers ORDER BY name )", + }, + SameReadingTestCase { + description: "inside a derived table", + sql: "SELECT x FROM ( ( SELECT id AS x FROM orders ) ORDER BY quantity ) d", + expected_same_as: "SELECT x FROM ( SELECT id AS x FROM orders ORDER BY quantity ) d", + }, + SameReadingTestCase { + description: "a pipe ORDER BY after the parentheses", + sql: "( SELECT id FROM orders ) |> ORDER BY id", + expected_same_as: "SELECT id FROM orders |> ORDER BY id", + }, + SameReadingTestCase { + description: "common table expressions before the parentheses", + sql: "WITH c AS ( SELECT id FROM orders ) ( SELECT id, nope FROM c )", + expected_same_as: "WITH c AS ( SELECT id FROM orders ) SELECT id, nope FROM c", + }, + SameReadingTestCase { + description: "common table expressions before the parentheses and a clause after", + sql: "WITH c AS ( SELECT id, quantity FROM orders ) ( SELECT id FROM c ) ORDER BY quantity", + expected_same_as: "WITH c AS ( SELECT id, quantity FROM orders ) SELECT id FROM c ORDER BY quantity", + }, + SameReadingTestCase { + description: "common table expressions before the parentheses, a LIMIT inside and a clause after", + sql: "WITH c AS ( SELECT id, quantity FROM orders ) ( SELECT id, nope FROM c LIMIT 1 ) ORDER BY id", + expected_same_as: "( WITH c AS ( SELECT id, quantity FROM orders ) SELECT id, nope FROM c LIMIT 1 ) ORDER BY id", + }, + SameReadingTestCase { + description: "a LIMIT inside and an unknown ORDER BY column after", + sql: "( SELECT id FROM orders LIMIT 1 ) ORDER BY nope", + expected_same_as: "SELECT id FROM orders ORDER BY nope LIMIT 1", + }, + SameReadingTestCase { + description: "common table expressions before the parentheses, a LIMIT inside and an unknown column after", + sql: "WITH c AS ( SELECT id FROM orders ) ( SELECT id FROM c LIMIT 1 ) ORDER BY nope", + expected_same_as: "WITH c AS ( SELECT id FROM orders ) SELECT id FROM c ORDER BY nope LIMIT 1", + }, + SameReadingTestCase { + description: "common table expressions inside the parentheses, a LIMIT inside and an unknown column after", + sql: "( WITH c AS ( SELECT id FROM orders ) SELECT id FROM c LIMIT 1 ) ORDER BY nope", + expected_same_as: "WITH c AS ( SELECT id FROM orders ) SELECT id FROM c ORDER BY nope LIMIT 1", + }, + ]; + 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/issues.rs b/crates/sqlquack-semantics/src/lineage/_helpers/checks/issues.rs index 4bb997c0a..aa1fb4455 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/issues.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/issues.rs @@ -4,6 +4,7 @@ use sqlquack_syntax::Expression; use sqlquack_syntax::ExpressionWalk; use sqlquack_syntax::tokens::models::Span; +use crate::lineage::_helpers::tracing::queries::takes_clauses; use crate::lineage::models::NameFinding; /// A finding about `node`, at `span`. @@ -72,7 +73,7 @@ pub(crate) fn query_boundary(node: &Expression) -> bool { | Expression::Intersect(_) | Expression::Except(_) | Expression::Cte(_) => true, - Expression::Subquery(subquery) => subquery.alias.is_some(), + Expression::Subquery(subquery) => subquery.alias.is_some() || takes_clauses(subquery), _ => false, } } 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 7fa930215..50599d109 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs @@ -1157,10 +1157,25 @@ fn distinct_order( /// A set operation's ORDER BY column that is not one of its outputs. pub(crate) fn set_order(query: &Expression, context: LineageContext<'_>) -> Vec { - let (order, left) = match query { - Expression::Union(set) => (set.order_by.as_ref(), &set.left), - Expression::Intersect(set) => (set.order_by.as_ref(), &set.left), - Expression::Except(set) => (set.order_by.as_ref(), &set.left), + let order: Option<&OrderBy> = match query { + Expression::Union(set) => set.order_by.as_ref(), + Expression::Intersect(set) => set.order_by.as_ref(), + Expression::Except(set) => set.order_by.as_ref(), + _ => return Vec::new(), + }; + set_order_by(query, order, context) +} + +/// `order`'s bare names that are not outputs of the set operation `query`, which it follows. +pub(crate) fn set_order_by( + query: &Expression, + order: Option<&OrderBy>, + context: LineageContext<'_>, +) -> Vec { + let left: &Expression = match query { + Expression::Union(set) => &set.left, + Expression::Intersect(set) => &set.left, + Expression::Except(set) => &set.left, _ => return Vec::new(), }; let Some(outputs) = output_names(left, context) else { diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs index d9847850d..f94f82f85 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/findings/levels.rs @@ -4,12 +4,14 @@ use std::collections::HashMap; use sqlquack_syntax::Expression; use sqlquack_syntax::ExpressionWalk; -use sqlquack_syntax::expressions::queries::models::{JoinKind, Select, With}; +use sqlquack_syntax::expressions::queries::models::{JoinKind, OrderBy, Select, With}; use crate::dialects::main::dialect_rules::dialect_rules; use crate::lineage::_helpers::checks::issues::in_scope; use crate::lineage::_helpers::checks::node_checks::{NodeLevel, node_findings}; -use crate::lineage::_helpers::checks::scope_rules::{RuleLevel, select_rules, set_order}; +use crate::lineage::_helpers::checks::scope_rules::{ + RuleLevel, select_rules, set_order, set_order_by, +}; use crate::lineage::_helpers::checks::sets::{Branches, set_findings}; use crate::lineage::_helpers::findings::checks::{ Level, alias_count_findings, column_finding, columns_pattern_finding, level_outputs, @@ -17,7 +19,8 @@ use crate::lineage::_helpers::findings::checks::{ }; use crate::lineage::_helpers::tracing::expressions::date_part_argument; use crate::lineage::_helpers::tracing::queries::{ - empty_scope, level_scope, output_key, unannotated, with_ctes_visiting, + 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}; @@ -61,7 +64,29 @@ fn query_findings_level( }; match query { Expression::Select(select) => { - select_findings((query, select), (ctes, outer, aliases), context) + select_findings((query, select, &[]), (ctes, outer, aliases), context) + } + Expression::Subquery(subquery) if takes_clauses(subquery) => { + let (inner, after) = clauses_within(query); + let orders: Vec<&OrderBy> = after + .iter() + .filter_map(|node| node.order_by.as_ref()) + .collect(); + match inner { + Expression::Select(select) => { + select_findings((inner, select, &orders), (ctes, outer, aliases), context) + } + _ => { + let mut found: Vec = + query_findings(inner, (ctes, outer, aliases), context); + if typed_rules(context) { + for order in orders { + found.extend(set_order_by(inner, Some(order), context)); + } + } + found + } + } } Expression::Union(union) => branches(union.with.as_ref(), &union.left, &union.right), Expression::Intersect(intersect) => { @@ -208,7 +233,7 @@ fn faulted_cte(name: &str, reach: &[NamedQuery], context: LineageContext<'_>) -> } fn select_findings( - (node, select): (&Expression, &Select), + (node, select, after): (&Expression, &Select, &[&OrderBy]), (ctes, outer, aliases): (&[NamedQuery], Option<&LineageScope<'_>>, &[String]), context: LineageContext<'_>, ) -> Vec { @@ -231,7 +256,11 @@ fn select_findings( context, }, )); - for checked in in_scope(node) { + let after_keys = after.iter().flat_map(|order| order_keys(order)); + for checked in in_scope(node) + .into_iter() + .chain(after_keys.flat_map(in_scope)) + { nested.extend(node_findings(checked, level)); } } @@ -310,8 +339,10 @@ fn select_findings( own.extend(found); nested.extend(inner); } - for clause in clauses(select) { - let named_place: bool = ordered_name_place(select, clause) + let mut read: Vec<&Expression> = clauses(select); + read.extend(after.iter().flat_map(|order| order_keys(order))); + for clause in read { + let named_place: bool = ordered_name_place(select, after, clause) || (rules.group_keys_read_outputs() && group_key_place(select, clause)); let read = match (anywhere, named_place) { (true, _) => level, @@ -359,8 +390,8 @@ fn group_key_place(select: &Select, clause: &Expression) -> bool { .any(|candidate| std::ptr::eq(candidate, clause)) } -/// Whether `clause` is an ORDER BY key of `select` that is a bare name. -fn ordered_name_place(select: &Select, clause: &Expression) -> bool { +/// Whether `clause` is a bare-name ORDER BY key of `select` or of the clauses `after` it. +fn ordered_name_place(select: &Select, after: &[&OrderBy], clause: &Expression) -> bool { let mut bare: &Expression = unannotated(clause); while let Expression::Paren(paren) = bare { bare = unannotated(&paren.this); @@ -369,10 +400,16 @@ fn ordered_name_place(select: &Select, clause: &Expression) -> bool { && select .order_by .iter() + .chain(after.iter().copied()) .flat_map(|order| &order.expressions) .any(|ordered| std::ptr::eq(&ordered.this, clause)) } +/// The keys of `order`. +fn order_keys(order: &OrderBy) -> impl Iterator { + order.expressions.iter().map(|ordered| &ordered.this) +} + /// Whether a FROM item is LATERAL, reading the items before it. fn lateral(item: &Expression) -> bool { match unannotated(item) { @@ -414,6 +451,15 @@ fn clause_findings( let mut bound: Vec = Vec::new(); while let Some(node) = pending.pop() { match node { + Expression::Subquery(subquery) if takes_clauses(subquery) => { + let aliases: Vec = + level_outputs(*level, context).map(str::to_owned).collect(); + nested.extend(query_findings( + node, + (level.scope.ctes, Some(level.scope), &aliases), + context, + )); + } Expression::Select(_) | Expression::Union(_) | Expression::Intersect(_) diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/expressions.rs b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/expressions.rs index 54cffce0d..44d2824e9 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/expressions.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/expressions.rs @@ -13,7 +13,9 @@ use sqlquack_syntax::{DataType, Expression}; use crate::dialects::main::canonical_type::canonical_type; use crate::dialects::main::dialect_rules::dialect_rules; use crate::lineage::_helpers::tracing::lineages::{combined, constant, traced_if, unresolved}; -use crate::lineage::_helpers::tracing::queries::{empty_scope, expanded_star, resolved_query}; +use crate::lineage::_helpers::tracing::queries::{ + empty_scope, expanded_star, resolved_query, takes_clauses, +}; use crate::lineage::_helpers::tracing::resolution::{column_lineage, key, positional_column}; use crate::lineage::_helpers::tracing::reuse::{alias_read, alias_reads, worked}; use crate::lineage::_helpers::typing::rules::{ @@ -152,6 +154,9 @@ fn expression_lineage_level( ..constant() } } + Expression::Subquery(subquery) if takes_clauses(subquery) => { + query_value(expression, scope, context) + } Expression::Subquery(subquery) => query_value(&subquery.this, scope, context), Expression::Select(_) | Expression::Union(_) diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/queries.rs b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/queries.rs index 3b7b89365..e6cf98436 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/tracing/queries.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/tracing/queries.rs @@ -6,7 +6,7 @@ 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, Union, With}; +use sqlquack_syntax::expressions::queries::models::{Cte, OrderBy, Select, Subquery, Union, With}; use sqlquack_syntax::tokens::models::Span; use crate::dialects::main::dialect_rules::dialect_rules; @@ -57,7 +57,35 @@ fn resolved_query_level( context: LineageContext<'_>, ) -> ResolvedQuery { match query { - Expression::Select(select) => select_query(select, ctes, outer, context), + Expression::Select(select) => select_query((select, &[]), ctes, outer, context), + Expression::Subquery(subquery) if takes_clauses(subquery) => match clauses_within(query) { + (Expression::Select(select), after) => { + select_query((select, &after), ctes, outer, context) + } + (inner, after) => { + let mut query: ResolvedQuery = resolved_query(inner, ctes, outer, context); + let own: Option<&OrderBy> = match inner { + Expression::Union(node) if node.limit.is_none() && node.offset.is_none() => { + node.order_by.as_ref() + } + Expression::Intersect(node) + if node.limit.is_none() && node.offset.is_none() => + { + node.order_by.as_ref() + } + Expression::Except(node) if node.limit.is_none() && node.offset.is_none() => { + node.order_by.as_ref() + } + _ => None, + }; + let mut levels: Vec<(Option<&OrderBy>, bool)> = vec![(own, false)]; + levels.extend(after.iter().map(|node| clause_level(node))); + for read in output_reads(&deciding(&levels), &query.columns, context) { + query.rows.read(&read); + } + query + } + }, Expression::Union(union) if union.by_name => { let ctes = with_ctes(union.with.as_ref(), ctes, outer, context); let sides: (&Expression, &Expression) = (&union.left, &union.right); @@ -543,7 +571,7 @@ fn level_scope_and_rows<'a>( } fn select_query( - select: &Select, + (select, after): (&Select, &[&Subquery]), ctes: &[NamedQuery], outer: Option<&LineageScope<'_>>, context: LineageContext<'_>, @@ -620,7 +648,7 @@ fn select_query( .group_by .iter() .flat_map(|group| group.expressions.iter()); - for item in grouping.chain(ordering(select)) { + for item in grouping.chain(ordering(select, after)) { let read: Lineage = match ordinal(item) { Some(position) => output_at(&query.columns, position), None => expression_lineage(item, &scope, context), @@ -662,20 +690,62 @@ fn unwrapped_projection(mut projection: &Expression) -> &Expression { } } -/// The ORDER BY keys that decide rows: those of a level with LIMIT, OFFSET, TOP or FETCH. -fn ordering(select: &Select) -> Vec<&Expression> { +/// The ORDER BY keys that decide rows of `select` with the clauses of the nodes `after` it. +fn ordering<'a>(select: &'a Select, after: &[&'a Subquery]) -> Vec<&'a Expression> { let limited: bool = select.limit.is_some() || select.offset.is_some() || select.top.is_some() || select.fetch.is_some(); - match (limited, &select.order_by) { - (true, Some(order)) => order - .expressions - .iter() - .map(|ordered| &ordered.this) - .collect(), - _ => Vec::new(), + let mut levels: Vec<(Option<&OrderBy>, bool)> = vec![(select.order_by.as_ref(), limited)]; + levels.extend(after.iter().map(|node| clause_level(node))); + deciding(&levels) +} + +/// The ORDER BY keys of `levels`, innermost first, that a LIMIT or OFFSET at or outside them follows. +fn deciding<'a>(levels: &[(Option<&'a OrderBy>, bool)]) -> Vec<&'a Expression> { + let outermost: Option = levels.iter().rposition(|(_, limited)| *limited); + levels + .iter() + .take(outermost.map_or(0, |index| index + 1)) + .flat_map(|(order, _)| order_keys(*order)) + .collect() +} + +/// A subquery node's ORDER BY, and whether a LIMIT or OFFSET follows it. +fn clause_level(node: &Subquery) -> (Option<&OrderBy>, bool) { + ( + node.order_by.as_ref(), + node.limit.is_some() || node.offset.is_some(), + ) +} + +/// Whether `node` is an unaliased subquery node holding clauses written after its parentheses. +pub(crate) fn takes_clauses(node: &Subquery) -> bool { + node.alias.is_none() + && (node.order_by.is_some() + || node.limit.is_some() + || node.offset.is_some() + || node.sort_by.is_some() + || node.cluster_by.is_some() + || node.distribute_by.is_some()) +} + +/// The query within `query`'s parentheses and unaliased subquery nodes, and those nodes, innermost first. +pub(crate) fn clauses_within(query: &Expression) -> (&Expression, Vec<&Subquery>) { + let mut nodes: Vec<&Subquery> = Vec::new(); + let mut inner: &Expression = query; + loop { + inner = match inner { + Expression::Paren(paren) => &paren.this, + Expression::Subquery(node) if node.alias.is_none() => { + nodes.push(node); + &node.this + } + _ => break, + }; } + nodes.reverse(); + (inner, nodes) } /// What ORDER BY keys read: an ordinal or output name reads that output, else nothing traced. diff --git a/crates/sqlquack-semantics/src/snowflake/profile/tests/test_statement_findings.rs b/crates/sqlquack-semantics/src/snowflake/profile/tests/test_statement_findings.rs index b5552b3a2..5fb348421 100644 --- a/crates/sqlquack-semantics/src/snowflake/profile/tests/test_statement_findings.rs +++ b/crates/sqlquack-semantics/src/snowflake/profile/tests/test_statement_findings.rs @@ -21,6 +21,16 @@ fn given_a_query_when_resolving_its_names_then_unknown_ambiguous_and_unqualifiab sql: "SELECT qty FROM orders", expected_findings: &[("E201", "Unknown column 'qty' in table 'orders'", 7)], }, + QueryFindingsTestCase { + description: "an ORDER BY column after a parenthesized query that its relation lacks", + sql: "(SELECT id FROM orders) ORDER BY nope", + expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 33)], + }, + QueryFindingsTestCase { + description: "an ORDER BY column after a parenthesized query with LIMIT that its relation lacks", + sql: "(SELECT id FROM orders LIMIT 1) ORDER BY nope", + expected_findings: &[("E201", "Unknown column 'nope' in table 'orders'", 41)], + }, QueryFindingsTestCase { description: "an unqualified column no relation has", sql: "SELECT qty FROM orders, customers", 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 9c8c1c597..e191e9b3c 100644 --- a/crates/sqlquack-syntax-bigquery/src/parse/_helpers/tree/nodes.rs +++ b/crates/sqlquack-syntax-bigquery/src/parse/_helpers/tree/nodes.rs @@ -393,6 +393,7 @@ pub(crate) fn subquery(this: Expression, alias: Option, lateral: boo span: SourceSpan(None), table_sample: None, watermark: None, + ctes_outside: false, })) } 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 e451dd46d..051d3563b 100644 --- a/crates/sqlquack-syntax-clickhouse/src/parse/_helpers/tree/nodes.rs +++ b/crates/sqlquack-syntax-clickhouse/src/parse/_helpers/tree/nodes.rs @@ -386,6 +386,7 @@ pub(crate) fn subquery(this: Expression, alias: Option, lateral: boo span: SourceSpan(None), table_sample: None, watermark: None, + ctes_outside: false, })) } 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 4d7fdcb69..f8ea11ffe 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/alters.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/alters.rs @@ -42,6 +42,21 @@ 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()); + for (position, key) in keys.into_iter().enumerate() { + match key { + Expression::Literal(_) => strings.push(position), + Expression::Identifier(name) if name.quoted => quoted.push(position), + _ => {} + } + } + (quoted, strings) +} + /// A property as its key and value; a key without a value has a null value. pub(crate) fn property_pair(property: Expression) -> (Expression, Expression) { match property { @@ -851,13 +866,21 @@ impl Parser<'_> { location: self.string_lit()?, })), "TAGS" => { - let expressions: Vec<(String, Expression)> = self + let pairs: Vec<(Expression, Expression)> = self .property_list()? .into_iter() .map(property_pair) + .collect(); + let (quoted_keys, string_keys) = 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 })) + Ok(Some(AlterTableAction::SetTag { + expressions, + quoted_keys, + string_keys, + })) } "OWNER" => { self.expect_kw("TO")?; @@ -916,8 +939,13 @@ impl Parser<'_> { })); } if self.eat_kw("TAGS") { - let names: Vec = self.property_list()?.iter().map(property_text).collect(); - return Ok(Some(AlterTableAction::UnsetTag { names })); + let keys: Vec = self.property_list()?; + let (quoted_keys, string_keys) = key_spellings(&keys); + return Ok(Some(AlterTableAction::UnsetTag { + names: keys.iter().map(property_text).collect(), + quoted_keys, + string_keys, + })); } self.expect_kw("MANAGED")?; let truncate: bool = self.eat_kw("TRUNCATE"); diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/creates.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/creates.rs index 7f7e31d54..29d2fbc74 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/creates.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/creates.rs @@ -229,8 +229,8 @@ impl Parser<'_> { )); } if as_query || self.starts_query_at(0) { - let parenthesized: bool = self.is_char('('); - node.as_select = Some(self.query()?); + let (query, parenthesized) = self.flagged_query()?; + node.as_select = Some(query); node.as_select_parenthesized = parenthesized; node.as_select_without_as = !as_query; } @@ -1079,8 +1079,7 @@ impl Parser<'_> { "Operation not allowed: TBLPROPERTIES can't coexist with CREATE TEMPORARY VIEW"; return Err(self.error_with(message, start_offset, true)); } - let parenthesized: bool = self.is_char('('); - let query: Expression = self.query()?; + let (query, parenthesized) = self.flagged_query()?; let mut node: CreateView = CreateView::new(String::new(), query); node.name = name; node.columns = columns; 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 2eaf00542..8a8d498c2 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::property_text; +use crate::parse::_helpers::commands::alters::{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, @@ -64,23 +64,22 @@ impl Parser<'_> { without_equals: Vec::new(), quoted_keys: Vec::new(), string_keys: Vec::new(), + bare_values: 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) = + key_spellings(options.iter().map(|((key, _), _)| key)); for (position, ((key, value), without_equals)) in options.into_iter().enumerate() { if without_equals { credentials.without_equals.push(position); } - match &key { - Expression::Literal(_) => credentials.string_keys.push(position), - Expression::Identifier(name) if name.quoted => { - credentials.quoted_keys.push(position) - } - _ => {} + if !matches!(&value, Expression::Literal(literal) if literal.is_string()) { + credentials.bare_values.push(position); } credentials .credentials 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 446de7f71..76a395489 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/dml.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/dml.rs @@ -134,6 +134,7 @@ impl Parser<'_> { }, row_format: None, stored_as: None, + stored_as_quoted: false, input_format: None, output_format: None, using: None, @@ -194,7 +195,9 @@ impl Parser<'_> { self.expect_kw("OUTPUTFORMAT")?; node.output_format = Some(self.string_lit()?); } else { - node.stored_as = Some(self.ident()?.name); + let format: Identifier = self.ident()?; + node.stored_as_quoted = format.quoted; + node.stored_as = Some(format.name); } Ok(node) } 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 276526f2e..6fbeb7d03 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/from.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/from.rs @@ -25,14 +25,14 @@ fn collapse(item: Expression, joins: Vec) -> Expression { } } -/// A relation written alone in parentheses, a table marked so that `( t )` and `t` differ. +/// A relation alone in parentheses: a table's first pair marks it, any other pair is a paren node. fn parenthesized(item: Expression) -> Expression { match item { - Expression::Table(mut relation) => { + Expression::Table(mut relation) if !relation.parenthesized => { relation.parenthesized = true; Expression::Table(relation) } - other => other, + other => paren(other), } } 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 c315f0999..7a4330a01 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/queries.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/queries.rs @@ -5,12 +5,12 @@ use sqlquack_syntax::expressions::names::models::Identifier; use sqlquack_syntax::expressions::operators::models::{Alias, GroupingSets}; use sqlquack_syntax::expressions::queries::models::{ ClusterBy, Cte, DistributeBy, From, GroupBy, Having, Hint, HintExpression, Limit, NamedWindow, - OrderBy, Ordered, Qualify, Select, SortBy, Where, With, + Offset, OrderBy, Ordered, Qualify, Select, SortBy, Where, With, }; use crate::parse::_helpers::tree::cursor::{Parser, Parsing, is_identifier}; use crate::parse::_helpers::tree::nodes::{ - TableAlias, binary, call, cte, null, set_operation, star, subquery, tuple, var, + TableAlias, binary, call, cte, null, paren, set_operation, star, subquery, tuple, var, }; use crate::parse::constants::{CUBE, GROUPING_SETS, ROLLUP}; use crate::scan::models::TokenKind; @@ -128,11 +128,57 @@ fn takes(body: &Expression, organization: &Organization) -> bool { } } +/// A parenthesized query with `organization` after it, as an unaliased subquery node holding both. +fn organized_parenthesized(this: Expression, organization: Organization) -> Expression { + let mut node: Expression = subquery(this, None, false); + if let Expression::Subquery(subquery) = &mut node { + subquery.order_by = organization.order.map(order_by); + subquery.cluster_by = organization.cluster.map(|expressions| ClusterBy { + expressions: expressions.into_iter().map(Ordered::asc).collect(), + }); + subquery.distribute_by = organization + .distribute + .map(|expressions| DistributeBy { expressions }); + 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 }); + } + 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); + } + other => other, + }; let mut body: Expression = match takes(&body, &organization) { true => body, false => Expression::Select(Box::new(wrapped(body))), @@ -173,13 +219,7 @@ fn organized(body: Expression, organization: Organization) -> Expression { select.sort_by = sort.or(select.sort_by.take()); select.windows = windows.or(select.windows.take()); select.limit = count.map(limit); - select.offset = - offset.map( - |this| sqlquack_syntax::expressions::queries::models::Offset { - this, - rows: None, - }, - ); + select.offset = offset.map(|this| Offset { this, rows: None }); } Expression::Union(node) => set_operation_organized!(node), Expression::Intersect(node) => set_operation_organized!(node), @@ -189,8 +229,49 @@ fn organized(body: Expression, organization: Organization) -> Expression { body } -/// `body` with `with` as its common table expressions, wrapped when it has its own already. +/// Whether the select within any parentheses of `query` took the pipe `marked`, which it then records. +fn marked_within(query: &mut Expression, marked: String) -> bool { + match query { + Expression::Paren(paren) => marked_within(&mut paren.this, marked), + Expression::Select(select) => { + select.pipe_operators.push(marked); + true + } + _ => false, + } +} + +/// `body` with `with` as its CTEs; a parenthesized query takes them inside, flagged as written outside. pub(crate) fn with_ctes(body: Expression, with: With) -> Expression { + let mut node: Expression = match body { + Expression::Paren(paren) => subquery(paren.this, None, false), + Expression::Subquery(node) if node.alias.is_none() => Expression::Subquery(node), + other => return with_query(other, with), + }; + if let Expression::Subquery(subquery) = &mut node { + subquery.this = with_inside(std::mem::replace(&mut subquery.this, null()), with); + subquery.ctes_outside = true; + } + node +} + +/// `body` with `with` on the query within any parentheses and unaliased subquery nodes. +fn with_inside(body: Expression, with: With) -> Expression { + match body { + Expression::Paren(mut paren) => { + paren.this = with_inside(paren.this, with); + Expression::Paren(paren) + } + Expression::Subquery(mut node) if node.alias.is_none() => { + node.this = with_inside(node.this, with); + Expression::Subquery(node) + } + other => with_query(other, with), + } +} + +/// A query with `with` as its CTEs, wrapped first when it already has some. +fn with_query(body: Expression, with: With) -> Expression { let has_with: bool = match &body { Expression::Select(select) => select.with.is_some(), Expression::Union(node) => node.with.is_some(), @@ -226,6 +307,15 @@ impl Parser<'_> { }) } + /// A query, and whether it is one parenthesized query term, whose outer pair the flag then records. + pub(crate) fn flagged_query(&mut self) -> Parsing<(Expression, bool)> { + let first: usize = self.at; + Ok(match self.query()? { + Expression::Paren(node) => (self.respanned(first, node.this), true), + query => (query, false), + }) + } + /// A query after its common table expressions. pub(crate) fn query_rest(&mut self, with: Option) -> Parsing { let body: Expression = self.query_term(0)?; @@ -319,13 +409,14 @@ impl Parser<'_> { } } - /// `queryPrimary`: a `SELECT`, `FROM ...`, `TABLE t`, `VALUES ...` or `( query )`. + /// `queryPrimary`: a `SELECT`, `FROM ...`, `TABLE t`, `VALUES ...` or `( query )` as a paren node. pub(crate) fn query_primary(&mut self) -> Parsing { if self.is_char('(') { + let first: usize = self.at; self.at += 1; let query: Expression = self.query()?; self.expect_char(')')?; - return Ok(query); + return Ok(self.spanned(first, paren(query))); } match self.word_at(0).as_deref() { Some("SELECT") => self.query_specification(), @@ -659,6 +750,12 @@ impl Parser<'_> { } match organized(left, organization) { Expression::Select(select) => *select, + Expression::Subquery(mut node) => { + if marked_within(&mut node.this, mark(&word)) { + return Ok(Expression::Subquery(node)); + } + wrapped(Expression::Subquery(node)) + } other => wrapped(other), } } diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/routines.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/routines.rs index 61d881e94..e069e0ccd 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/routines.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/routines.rs @@ -157,6 +157,7 @@ impl Parser<'_> { let kind: Identifier = parser.ident()?; Ok(FunctionUsingResource { kind: kind.name.to_ascii_uppercase(), + kind_quoted: kind.quoted, uri: parser.string_lit()?, }) })?; 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 ed2c2cc33..d3a9969ba 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/tree/nodes.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/tree/nodes.rs @@ -469,6 +469,7 @@ pub(crate) fn subquery(this: Expression, alias: Option, lateral: boo span: SourceSpan(None), table_sample: None, watermark: None, + ctes_outside: false, })) } 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 8e9b476f2..14f5e09fd 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,11 +144,15 @@ impl Parser<'_> { "TIMESTAMP" => { let precision: Option = self.optional_length()?; let without: bool = !self.kw("WITH") && self.eat_kws(&["WITHOUT", "TIME", "ZONE"]); - let timezone: bool = self.eat_kws(&["WITH", "LOCAL", "TIME", "ZONE"]) || !without; + let local: bool = self.eat_kws(&["WITH", "LOCAL", "TIME", "ZONE"]); DataType::Timestamp { precision, - timezone, - spelling: without.then_some(TypeSpelling::WithoutTimeZone), + timezone: local || !without, + spelling: match (without, local) { + (true, _) => Some(TypeSpelling::WithoutTimeZone), + (false, true) => Some(TypeSpelling::WithLocalTimeZone), + (false, false) => None, + }, } } "TIME" => { 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 ec133fc8f..6edb83569 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/primary.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/primary.rs @@ -681,6 +681,7 @@ impl Parser<'_> { parser.expect_char(')')?; Ok(Expression::Extract(Box::new(ExtractFunc { this: source, + field_quoted: field.kind == TokenKind::Quoted, field: DateTimeField::Custom(field.value), }))) }), 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 18e667535..df7f5c3c1 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 @@ -640,6 +640,161 @@ fn given_one_form_written_in_different_ways_when_parsing_then_each_tree_keeps_wh ], expected_distinct_trees: 3, }, + DistinctTreesTestCase { + description: "a table in one, two and three pairs of parentheses", + written: &[ + "SELECT * FROM t1", + "SELECT * FROM ( t1 )", + "SELECT * FROM ( ( t1 ) )", + "SELECT * FROM ( ( ( t1 ) ) )", + ], + expected_distinct_trees: 4, + }, + DistinctTreesTestCase { + description: "a table function, a join and an inline table, bare and in parentheses", + written: &[ + "SELECT * FROM range ( 1 ) a1", + "SELECT * FROM ( range ( 1 ) a1 )", + "SELECT * FROM ( range ( 1 ) ) a1", + "SELECT * FROM ( t1 JOIN t2 )", + "SELECT * FROM ( ( t1 JOIN t2 ) )", + "SELECT * FROM VALUES ( 1 ) a1", + "SELECT * FROM ( VALUES ( 1 ) ) a1", + "SELECT * FROM ( ( VALUES ( 1 ) ) ) a1", + ], + expected_distinct_trees: 8, + }, + DistinctTreesTestCase { + description: "a query in one and two pairs of parentheses", + written: &[ + "SELECT * FROM ( SELECT 1 )", + "SELECT * FROM ( ( SELECT 1 ) )", + "SELECT * FROM abs ( TABLE ( SELECT 1 ) )", + "SELECT * FROM abs ( TABLE ( ( SELECT 1 ) ) )", + "VACUUM t1 USING INVENTORY ( SELECT 1 )", + "VACUUM t1 USING INVENTORY ( ( SELECT 1 ) )", + "SELECT 1 UNION SELECT 2", + "( SELECT 1 ) UNION SELECT 2", + "CREATE TABLE t1 AS ( SELECT 1 )", + "CREATE TABLE t1 AS ( ( SELECT 1 ) )", + "CREATE VIEW v1 AS ( SELECT 1 )", + "CREATE VIEW v1 AS ( ( SELECT 1 ) )", + ], + expected_distinct_trees: 12, + }, + DistinctTreesTestCase { + description: "a parenthesized query with a clause after it and a star over it", + written: &[ + "( SELECT 1 ) ORDER BY 1", + "SELECT * FROM ( ( SELECT 1 ) ) ORDER BY 1", + "SELECT 1 ORDER BY 1", + "( SELECT 1 ) LIMIT 1", + "SELECT * FROM ( ( SELECT 1 ) ) LIMIT 1", + "( ( SELECT 1 ) ) LIMIT 1", + "( SELECT 1 LIMIT 1 )", + ], + expected_distinct_trees: 7, + }, + DistinctTreesTestCase { + description: "CREATE TABLE AS a parenthesized query with LIMIT, and a star over it", + written: &[ + "CREATE TABLE t1 AS ( SELECT 1 ) LIMIT 1", + "CREATE TABLE t1 AS SELECT * FROM ( ( SELECT 1 ) ) LIMIT 1", + "CREATE TABLE t1 AS SELECT 1 LIMIT 1", + "CREATE TABLE t1 AS ( SELECT 1 LIMIT 1 )", + ], + expected_distinct_trees: 4, + }, + DistinctTreesTestCase { + description: "WITH before a parenthesized query, inside it, and over a star", + written: &[ + "WITH c AS ( SELECT 1 ) ( SELECT * FROM c )", + "WITH c AS ( SELECT 1 ) SELECT * FROM ( ( SELECT * FROM c ) )", + "WITH c AS ( SELECT 1 ) SELECT * FROM c", + "( WITH c AS ( SELECT 1 ) SELECT * FROM c )", + "WITH c AS ( SELECT 1 ) ( ( SELECT * FROM c ) )", + "WITH c AS ( SELECT 1 ) ( SELECT * FROM c ) ORDER BY 1", + "( WITH c AS ( SELECT 1 ) SELECT * FROM c ) ORDER BY 1", + "WITH c AS ( SELECT 1 ) ( SELECT * FROM c LIMIT 1 ) ORDER BY 1", + "( WITH c AS ( SELECT 1 ) SELECT * FROM c LIMIT 1 ) ORDER BY 1", + "WITH c AS ( SELECT 1 ) ( SELECT * FROM c ) |> WHERE 1 = 1", + "( WITH c AS ( SELECT 1 ) SELECT * FROM c ) |> WHERE 1 = 1", + ], + expected_distinct_trees: 11, + }, + DistinctTreesTestCase { + description: "clauses after a parenthesized query apart from the same clauses inside it", + written: &[ + "( SELECT 1 ) ORDER BY 1 LIMIT 1", + "( SELECT 1 ORDER BY 1 ) LIMIT 1", + "( ( SELECT 1 ) ) ORDER BY 1 LIMIT 1", + "( ( SELECT 1 ORDER BY 1 ) ) LIMIT 1", + "( ( ( SELECT 1 ) ) ) ORDER BY 1 LIMIT 1", + "( ( ( SELECT 1 ORDER BY 1 ) ) ) LIMIT 1", + "CREATE TABLE t1 AS ( SELECT 1 ) ORDER BY 1 LIMIT 1", + "CREATE TABLE t1 AS ( SELECT 1 ORDER BY 1 ) LIMIT 1", + "SELECT 1 WHERE 1 IN ( ( SELECT 1 ) ORDER BY 1 LIMIT 1 )", + "SELECT 1 WHERE 1 IN ( ( SELECT 1 ORDER BY 1 ) LIMIT 1 )", + "SELECT ( ( SELECT 1 ) ORDER BY 1 LIMIT 1 )", + "SELECT ( ( SELECT 1 ORDER BY 1 ) LIMIT 1 )", + "( WITH c AS ( SELECT 1 ) SELECT 1 ) ORDER BY 1", + "WITH c AS ( SELECT 1 ) ( SELECT 1 ORDER BY 1 )", + ], + expected_distinct_trees: 14, + }, + DistinctTreesTestCase { + description: "WITH before a parenthesized query with a clause after it apart from one more pair", + written: &[ + "WITH c AS ( SELECT 1 ) ( SELECT 1 ) ORDER BY 1", + "WITH c AS ( SELECT 1 ) ( ( SELECT 1 ORDER BY 1 ) )", + "WITH c AS ( SELECT 1 ) ( SELECT 1 ) LIMIT 1", + "WITH c AS ( SELECT 1 ) ( ( SELECT 1 LIMIT 1 ) )", + ], + expected_distinct_trees: 4, + }, + DistinctTreesTestCase { + description: "TIMESTAMP bare, WITH LOCAL TIME ZONE and WITHOUT TIME ZONE", + written: &[ + "SELECT CAST ( v1 AS TIMESTAMP )", + "SELECT CAST ( v1 AS TIMESTAMP WITH LOCAL TIME ZONE )", + "SELECT CAST ( v1 AS TIMESTAMP WITHOUT TIME ZONE )", + ], + expected_distinct_trees: 3, + }, + DistinctTreesTestCase { + description: "a COPY credential value written as a string, a number and a boolean", + written: &[ + "COPY INTO t1 FROM 's3://b/p' WITH ( CREDENTIAL ( k = '1' ) ) FILEFORMAT = CSV", + "COPY INTO t1 FROM 's3://b/p' WITH ( CREDENTIAL ( k = 1 ) ) FILEFORMAT = CSV", + "COPY INTO t1 FROM 's3://b/p' WITH ( CREDENTIAL ( k = 'true' ) ) FILEFORMAT = CSV", + "COPY INTO t1 FROM 's3://b/p' WITH ( CREDENTIAL ( k = true ) ) FILEFORMAT = CSV", + ], + expected_distinct_trees: 4, + }, + DistinctTreesTestCase { + description: "a resource kind, a stored format and an EXTRACT field, bare and backquoted", + written: &[ + "CREATE FUNCTION f1 AS 'a.B' USING JAR 'x.jar'", + "CREATE FUNCTION f1 AS 'a.B' USING `JAR` 'x.jar'", + "INSERT OVERWRITE DIRECTORY '/p' STORED AS TEXTFILE SELECT 1", + "INSERT OVERWRITE DIRECTORY '/p' STORED AS `TEXTFILE` SELECT 1", + "SELECT EXTRACT ( YEAR FROM v1 )", + "SELECT EXTRACT ( `YEAR` FROM v1 )", + ], + expected_distinct_trees: 6, + }, + DistinctTreesTestCase { + description: "a tag key written as a name, backquoted and as a string", + written: &[ + "ALTER TABLE t1 UNSET TAGS ( k )", + "ALTER TABLE t1 UNSET TAGS ( `k` )", + "ALTER TABLE t1 UNSET TAGS ( 'k' )", + "ALTER TABLE t1 SET TAGS ( k = 'v' )", + "ALTER TABLE t1 SET TAGS ( `k` = 'v' )", + "ALTER TABLE t1 SET TAGS ( 'k' = 'v' )", + ], + expected_distinct_trees: 6, + }, ]; 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 4d4b0b017..40e39e0d9 100644 --- a/crates/sqlquack-syntax-duckdb/src/parse/_helpers/tree/nodes.rs +++ b/crates/sqlquack-syntax-duckdb/src/parse/_helpers/tree/nodes.rs @@ -370,6 +370,7 @@ pub(crate) fn subquery(this: Expression, alias: Option, lateral: boo span: SourceSpan(None), table_sample: None, watermark: None, + ctes_outside: false, })) } 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 3bb2539e7..16d04a076 100644 --- a/crates/sqlquack-syntax-postgres/src/parse/_helpers/tree/nodes.rs +++ b/crates/sqlquack-syntax-postgres/src/parse/_helpers/tree/nodes.rs @@ -361,6 +361,7 @@ pub(crate) fn subquery(this: Expression, alias: Option, lateral: boo span: SourceSpan(None), table_sample: None, watermark: None, + ctes_outside: false, })) } diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/alter.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/alter.rs index b756d2bd3..3c6f23dfe 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/alter.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/alter.rs @@ -362,7 +362,7 @@ impl Parser { break; } } - Ok(AlterTableAction::UnsetTag { names }) + Ok(AlterTableAction::unset_tag(names)) } else if self.peek_nth(1).map(|t| { t.token_type != TokenType::Comma && t.token_type != TokenType::Semicolon @@ -561,7 +561,7 @@ impl Parser { break; } } - Ok(AlterTableAction::SetTag { expressions: tags }) + Ok(AlterTableAction::set_tag(tags)) } fn parse_alter_set_properties(&mut self) -> Result { diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/create_function.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/create_function.rs index 92418f97c..83abb62f0 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/create_function.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/create_function.rs @@ -397,9 +397,11 @@ impl Parser { { let kind = self.previous_text().to_ascii_uppercase(); let uri = self.expect_string()?; - function - .using_resources - .push(FunctionUsingResource { kind, uri }); + function.using_resources.push(FunctionUsingResource { + kind, + uri, + kind_quoted: false, + }); let _ = self.match_token(TokenType::Comma); } function = record_function_property(function, FunctionPropertyKind::Using); 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 81e15a340..4d24724fc 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/scripting.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/scripting.rs @@ -922,6 +922,7 @@ impl Parser { without_equals: Vec::new(), quoted_keys: Vec::new(), string_keys: Vec::new(), + bare_values: Vec::new(), }; credentials = Some(Box::new(creds)); } @@ -958,6 +959,7 @@ impl Parser { without_equals: Vec::new(), quoted_keys: Vec::new(), string_keys: Vec::new(), + bare_values: 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 1dfefc557..eb5e82bc0 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/grouping.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/grouping.rs @@ -685,6 +685,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, }; 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 51f494318..df6ad1e24 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/paths.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/paths.rs @@ -692,6 +692,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, }))) } @@ -746,6 +747,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })) } else { Expression::Subquery(Box::new(Subquery { @@ -767,6 +769,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })) }; @@ -826,6 +829,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })) } else { set_result @@ -906,6 +910,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, }))) } _ => None, @@ -957,6 +962,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, }))); } } diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/registry_functions.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/registry_functions.rs index 48fe6561c..7cacdbb39 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/registry_functions.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/registry_functions.rs @@ -814,6 +814,7 @@ impl Parser { Ok(Some(Expression::Extract(Box::new(ExtractFunc { this, field, + field_quoted: false, })))) } 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 ac39b517f..75282ad56 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 @@ -129,6 +129,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })))) } 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 a0882dfd3..c4f2796d0 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/typed.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/functions/typed.rs @@ -684,6 +684,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })); 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 cd0da6eb0..cc76db9f1 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/clauses.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/clauses.rs @@ -702,6 +702,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, }, }; subquery @@ -757,6 +758,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, }))) } else { Ok(Expression::Subquery(Box::new(Subquery { @@ -778,6 +780,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, }))) } } else { diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/insert.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/insert.rs index 69ad5f349..3e6cb3d5c 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/insert.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/insert.rs @@ -361,6 +361,7 @@ impl Parser { path, row_format, stored_as, + stored_as_quoted: false, input_format: None, output_format: None, using: None, 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 11d5d9227..77935b899 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/modifiers.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/modifiers.rs @@ -849,6 +849,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, }))) } else if self.check(TokenType::LParen) { let inner = self.parse_select_or_paren_select()?; @@ -878,6 +879,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, }))) } 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 3ae180c35..4c13fa7cb 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/select.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/select.rs @@ -630,6 +630,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })) } else { self.parse_table_expression_primary()? @@ -1327,6 +1328,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })) } else { let table_expr = self.parse_table_expression()?; @@ -1350,6 +1352,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })) } } else { @@ -1524,6 +1527,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })) } else if self.check(TokenType::Select) || self.check(TokenType::With) @@ -1562,6 +1566,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })); self.parse_set_operation(left)? } else { @@ -1586,6 +1591,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })) } else if self.check(TokenType::LParen) { let inner = self.parse_table_expression()?; @@ -1682,6 +1688,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })) } else if self.check(TokenType::Limit) || self.check(TokenType::Offset) { let limit = if self.match_token(TokenType::Limit) { @@ -1720,6 +1727,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })) } else { result @@ -1780,6 +1788,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })) } }) 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 d9aaa8bfe..4244c6965 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/dml.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/dml.rs @@ -299,6 +299,7 @@ impl Parser { span: values.span, table_sample: None, watermark: None, + ctes_outside: false, }, _ => 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 3409d6d93..3d6a958a4 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/entry.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/entry.rs @@ -838,6 +838,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })); let result = self.parse_set_operation(subquery)?; self.parse_query_modifiers(result) @@ -865,6 +866,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })); let result = self.parse_set_operation(subquery)?; let pre_alias_comments = self.previous_trailing_comments().to_vec(); @@ -1107,6 +1109,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })); let result = self.parse_set_operation(subquery)?; self.parse_query_modifiers(result) @@ -1134,6 +1137,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })); let result = self.parse_set_operation(subquery)?; let pre_alias_comments = self.previous_trailing_comments().to_vec(); @@ -1407,6 +1411,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })) } else { self.parse_expression()? @@ -1460,6 +1465,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })) } 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 f0841f128..adab7bef9 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/support/names.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/support/names.rs @@ -1063,6 +1063,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, }))) } 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 7c7dea4c7..958557dd6 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 @@ -1322,6 +1322,7 @@ impl Parser { span: SourceSpan::default(), table_sample: None, watermark: None, + ctes_outside: false, })) } 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 d0dd45590..a9d50d056 100644 --- a/crates/sqlquack-syntax-sqlite/src/parse/_helpers/tree/nodes.rs +++ b/crates/sqlquack-syntax-sqlite/src/parse/_helpers/tree/nodes.rs @@ -286,6 +286,7 @@ pub(crate) fn subquery(this: Expression, alias: Option, lateral: boo span: SourceSpan(None), table_sample: None, watermark: None, + ctes_outside: false, })) } 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 c6ad84274..29f5f65b7 100644 --- a/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/tree/nodes.rs +++ b/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/tree/nodes.rs @@ -299,6 +299,7 @@ pub(crate) fn subquery(this: Expression, alias: Option, lateral: boo span: SourceSpan(None), table_sample: None, watermark: None, + ctes_outside: false, })) } diff --git a/crates/sqlquack-syntax/src/expressions/columns/models.rs b/crates/sqlquack-syntax/src/expressions/columns/models.rs index e745d2492..57e514d1c 100644 --- a/crates/sqlquack-syntax/src/expressions/columns/models.rs +++ b/crates/sqlquack-syntax/src/expressions/columns/models.rs @@ -800,10 +800,22 @@ pub enum AlterTableAction { /// SET TAG action (Snowflake): ALTER TABLE t SET TAG key='value' SetTag { expressions: Vec<(String, Expression)>, + /// Databricks: the positions of the keys written in backquotes + #[serde(default, skip_serializing_if = "Vec::is_empty")] + quoted_keys: Vec, + /// Databricks: the positions of the keys written as strings + #[serde(default, skip_serializing_if = "Vec::is_empty")] + string_keys: Vec, }, /// UNSET TAG action (Snowflake): ALTER TABLE t UNSET TAG key1, key2 UnsetTag { names: Vec, + /// Databricks: the positions of the names written in backquotes + #[serde(default, skip_serializing_if = "Vec::is_empty")] + quoted_keys: Vec, + /// Databricks: the positions of the names written as strings + #[serde(default, skip_serializing_if = "Vec::is_empty")] + string_keys: Vec, }, /// SET with parenthesized options (TSQL): ALTER TABLE t SET (SYSTEM_VERSIONING=ON, ...) SetOptions { @@ -915,6 +927,26 @@ pub enum AlterTableAction { }, } +impl AlterTableAction { + /// `SET TAG` with keys written plainly. + pub fn set_tag(expressions: Vec<(String, Expression)>) -> Self { + Self::SetTag { + expressions, + quoted_keys: Vec::new(), + string_keys: Vec::new(), + } + } + + /// `UNSET TAG` with names written plainly. + pub fn unset_tag(names: Vec) -> Self { + Self::UnsetTag { + names, + quoted_keys: Vec::new(), + string_keys: Vec::new(), + } + } +} + /// Actions for ALTER COLUMN #[derive(sqlquack_derive::AstNode, Debug, Clone, PartialEq, Serialize, Deserialize)] pub enum AlterColumnAction { diff --git a/crates/sqlquack-syntax/src/expressions/queries/models.rs b/crates/sqlquack-syntax/src/expressions/queries/models.rs index 40004bbb1..957290118 100644 --- a/crates/sqlquack-syntax/src/expressions/queries/models.rs +++ b/crates/sqlquack-syntax/src/expressions/queries/models.rs @@ -526,6 +526,9 @@ pub struct Subquery { /// Databricks: `WATERMARK ... DELAY OF ...` after the parenthesized query #[serde(default, skip_serializing_if = "Option::is_none")] pub watermark: Option>, + /// 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, /// 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/schema_objects/models.rs b/crates/sqlquack-syntax/src/expressions/schema_objects/models.rs index 4c301dedf..2562adff2 100644 --- a/crates/sqlquack-syntax/src/expressions/schema_objects/models.rs +++ b/crates/sqlquack-syntax/src/expressions/schema_objects/models.rs @@ -669,6 +669,9 @@ pub enum FunctionPropertyKind { pub struct FunctionUsingResource { pub kind: String, pub uri: String, + /// Databricks: the resource kind was written in backquotes, as in ``USING `JAR` 'uri'`` + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub kind_quoted: bool, } /// Function parameter diff --git a/crates/sqlquack-syntax/src/expressions/statements/models.rs b/crates/sqlquack-syntax/src/expressions/statements/models.rs index 984ca15a7..c4ebab583 100644 --- a/crates/sqlquack-syntax/src/expressions/statements/models.rs +++ b/crates/sqlquack-syntax/src/expressions/statements/models.rs @@ -34,6 +34,10 @@ pub struct DirectoryInsert { /// STORED AS clause (e.g., TEXTFILE, ORC, PARQUET) #[serde(default)] pub stored_as: Option, + /// Databricks: the `STORED AS` format was written in backquotes + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + #[ast(skip)] + pub stored_as_quoted: bool, /// Hive: `STORED AS INPUTFORMAT` class #[serde(default, skip_serializing_if = "Option::is_none")] #[ast(skip)] @@ -353,6 +357,9 @@ pub struct Credentials { /// Databricks: the positions of the credential options whose key was written as a string #[serde(default, skip_serializing_if = "Vec::is_empty")] pub string_keys: Vec, + /// 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, } /// Historical data / Time travel (Snowflake) - BEFORE (STATEMENT => ...) or AT (TIMESTAMP => ...) diff --git a/crates/sqlquack-syntax/src/expressions/typed_functions/models.rs b/crates/sqlquack-syntax/src/expressions/typed_functions/models.rs index 4f403e64c..2e34dad1f 100644 --- a/crates/sqlquack-syntax/src/expressions/typed_functions/models.rs +++ b/crates/sqlquack-syntax/src/expressions/typed_functions/models.rs @@ -294,6 +294,10 @@ pub struct DateTruncFunc { pub struct ExtractFunc { pub this: Expression, pub field: DateTimeField, + /// Databricks: the field was written in backquotes, as in ``EXTRACT ( `YEAR` FROM d )`` + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + #[ast(skip)] + pub field_quoted: bool, } #[derive(sqlquack_derive::AstNode, Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] diff --git a/crates/sqlquack-syntax/src/expressions/types/models.rs b/crates/sqlquack-syntax/src/expressions/types/models.rs index 8a4f4a55f..39a69b7c0 100644 --- a/crates/sqlquack-syntax/src/expressions/types/models.rs +++ b/crates/sqlquack-syntax/src/expressions/types/models.rs @@ -161,6 +161,9 @@ pub enum TypeSpelling { /// Databricks: `WITHOUT TIME ZONE` written after `TIMESTAMP` or `TIME`. #[serde(rename = "WITHOUT TIME ZONE")] WithoutTimeZone, + /// Databricks: `WITH LOCAL TIME ZONE` written after `TIMESTAMP`. + #[serde(rename = "WITH LOCAL TIME ZONE")] + WithLocalTimeZone, } /// Every data type the parser recognizes, standard and dialect-specific.