diff --git a/coverage/syntax-databricks/README.md b/coverage/syntax-databricks/README.md index 9202b4982..c170b4dcf 100644 --- a/coverage/syntax-databricks/README.md +++ b/coverage/syntax-databricks/README.md @@ -605,4 +605,7 @@ alternations, groups, `PERMUTE` and quantifiers) beside the pattern's text, beca Syntax block defines the pattern as a grammar of its own rather than as a string. Spellings the language treats as one token stay one tree: the letter case of keywords and of a -raw-string prefix, the quote character around a string or binary literal, and comments. +raw-string prefix, the quote character around a string or binary literal, and comments. An +unquoted function name keeps the case it is written in, so `count` and `COUNT` give two trees, but +its case carries no meaning: the formatter writes built-in names in upper case, and its round trip +compares unquoted function names without their case. A backquoted name keeps its case. diff --git a/coverage/syntax-databricks/reviews/expressions.toml b/coverage/syntax-databricks/reviews/expressions.toml index 97ec3ffb2..6e92e466c 100644 --- a/coverage/syntax-databricks/reviews/expressions.toml +++ b/coverage/syntax-databricks/reviews/expressions.toml @@ -1292,3 +1292,54 @@ note = "Re-binds db-expressions-063, 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-066" +family = "expressions" +catalogue_sha256 = "ffc590e2daad0e5c31541285aca1a5a7d1ce7e7bedb0c1ccdc4c1cfcdd32473c" +obligations_sha256 = "697332d6178cfd5465cf1ebae8c6d98f956a76ed507c8807032c76c97f48bc11" +parser_tree = "65eb7d867d31419e4ab65ad8122999be321d3e50" +reviewed_utc = "2026-11-18T15:02:18Z" +verdict = "approved" +forms = [ + "db.expressions.object-type", +] +note = "Re-binds db-expressions-065, not a re-read: that exclusion was bound to parser tree 243ba4a2 and went stale when the parser kept the written spelling of comments, locations, paths, formats, function bodies, handlers, resource URIs and SQLSTATE values, and a function parameter's comment (parser tree 65eb7d86), 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-067" +family = "expressions" +catalogue_sha256 = "ffc590e2daad0e5c31541285aca1a5a7d1ce7e7bedb0c1ccdc4c1cfcdd32473c" +obligations_sha256 = "697332d6178cfd5465cf1ebae8c6d98f956a76ed507c8807032c76c97f48bc11" +parser_tree = "3d4fbc2eb304670ab3b8472c8a98ff374880a3f1" +reviewed_utc = "2026-11-18T15:19:01Z" +verdict = "approved" +forms = [ + "db.expressions.object-type", +] +note = "Re-binds db-expressions-066, not a re-read: that exclusion was bound to parser tree 65eb7d86 and went stale when the parser kept raw and adjacent COPY credential values and a raw string after a literal type (parser tree 3d4fbc2e), 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-068" +family = "expressions" +catalogue_sha256 = "ffc590e2daad0e5c31541285aca1a5a7d1ce7e7bedb0c1ccdc4c1cfcdd32473c" +obligations_sha256 = "697332d6178cfd5465cf1ebae8c6d98f956a76ed507c8807032c76c97f48bc11" +parser_tree = "359320d6cc062fb34f152cdb7017cbe3f81c5c30" +reviewed_utc = "2026-11-18T16:28:23Z" +verdict = "approved" +forms = [ + "db.expressions.object-type", +] +note = "Re-binds db-expressions-067, not a re-read: that exclusion was bound to parser tree 3d4fbc2e and went stale when the parser stopped recording a numeric COPY credential value as a written spelling (parser tree 359320d6), 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 2da0cd5e4..257a7ae76 100644 --- a/coverage/syntax-databricks/screen.toml +++ b/coverage/syntax-databricks/screen.toml @@ -10,8 +10,11 @@ # # 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. +# and r'x' and R'x', are one token each and give one tree. The letter case of an unquoted function +# name is allowed too: the tree keeps it as written, so count and COUNT give two trees, but Databricks +# reads unquoted function names case-insensitively, and the formatter writes built-in names in upper +# case, so its round trip compares them without case. A backquoted name keeps its case. A receipt +# that shows Databricks reading two such spellings differently makes it a finding. # # [[lexical]] # mutation = "requote" diff --git a/crates/sqlquack-semantics/src/bigquery/profile/_helpers/typing.rs b/crates/sqlquack-semantics/src/bigquery/profile/_helpers/typing.rs index 2fd87ad80..382caff65 100644 --- a/crates/sqlquack-semantics/src/bigquery/profile/_helpers/typing.rs +++ b/crates/sqlquack-semantics/src/bigquery/profile/_helpers/typing.rs @@ -91,9 +91,10 @@ pub(crate) fn bigquery_literal_type(expression: &Expression) -> Option<&'static Expression::Literal(literal) => match literal.as_ref() { Literal::Number(text) if text.contains(['.', 'e', 'E']) => Kind::Float64, Literal::Number(_) | Literal::HexNumber(_) => Kind::Int64, - Literal::String(_) | Literal::TripleQuotedString(..) | Literal::RawString(_) => { - Kind::String - } + Literal::String(_) + | Literal::TripleQuotedString(..) + | Literal::RawString(_) + | Literal::JoinedString(..) => Kind::String, Literal::ByteString(_) => Kind::Bytes, Literal::Date(_) => Kind::Date, Literal::Datetime(_) => Kind::Datetime, diff --git a/crates/sqlquack-semantics/src/bigquery/profile/tests/test_checks.rs b/crates/sqlquack-semantics/src/bigquery/profile/tests/test_checks.rs index 67dddc08a..4d70b25f4 100644 --- a/crates/sqlquack-semantics/src/bigquery/profile/tests/test_checks.rs +++ b/crates/sqlquack-semantics/src/bigquery/profile/tests/test_checks.rs @@ -314,3 +314,52 @@ fn given_a_bigquery_query_when_checking_it_then_findings_follow_googlesql() { ); } } + +#[test] +fn given_one_string_spelled_two_ways_when_grouping_then_the_expressions_match() { + let test_cases = [ + QueryFindingsTestCase { + description: "adjacent strings in the select, one string in GROUP BY", + sql: "SELECT COALESCE(status, 'a' 'b') AS x FROM orders GROUP BY COALESCE(status, 'ab')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "one string in the select, adjacent strings in GROUP BY", + sql: "SELECT COALESCE(status, 'ab') AS x FROM orders GROUP BY COALESCE(status, 'a' 'b')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "adjacent strings in a concatenation", + sql: "SELECT status || 'a' 'b' AS x FROM orders GROUP BY status || 'ab'", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a raw string in the select, a plain one in GROUP BY", + sql: "SELECT COALESCE(status, r'ab') AS x FROM orders GROUP BY COALESCE(status, 'ab')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a plain string in the select, a raw one in GROUP BY", + sql: "SELECT COALESCE(status, 'ab') AS x FROM orders GROUP BY COALESCE(status, r'ab')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "adjacent strings of another value are still a different expression", + sql: "SELECT COALESCE(status, 'a' 'c') AS x FROM orders GROUP BY COALESCE(status, 'ab')", + expected_findings: &[( + "E230", + "Column must be grouped or used in an aggregate function", + 16, + )], + }, + ]; + let schema = typed_bigquery_shop_schema(); + for test_case in test_cases { + assert_eq!( + bigquery_findings(test_case.sql, &schema), + owned_findings(test_case.expected_findings), + "{}", + test_case.description + ); + } +} diff --git a/crates/sqlquack-semantics/src/bigquery/profile/tests/test_output_types.rs b/crates/sqlquack-semantics/src/bigquery/profile/tests/test_output_types.rs index bcc9150c9..6050936bd 100644 --- a/crates/sqlquack-semantics/src/bigquery/profile/tests/test_output_types.rs +++ b/crates/sqlquack-semantics/src/bigquery/profile/tests/test_output_types.rs @@ -110,6 +110,11 @@ fn given_bigquery_queries_when_typing_outputs_then_each_has_the_analyzer_type() sql: "SELECT status || 'x' AS v FROM orders", expected_type: Some("STRING"), }, + BigqueryTypeTestCase { + description: "adjacent strings are STRING", + sql: "SELECT 'a' 'b' AS v", + expected_type: Some("STRING"), + }, BigqueryTypeTestCase { description: "COUNT(*) is INT64", sql: "SELECT COUNT(*) AS v FROM orders", 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 312fff6ed..ddc6060bb 100644 --- a/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs +++ b/crates/sqlquack-semantics/src/databricks/profile/tests/test_lineage.rs @@ -573,6 +573,50 @@ fn given_a_window_named_after_parentheses_when_checked_then_only_an_undefined_na } } +#[test] +fn given_a_typed_literal_of_a_raw_string_when_analysed_then_it_reads_as_the_plain_one() { + let test_cases = [ + SameReadingTestCase { + description: "DATE", + sql: "SELECT DATE r'2020-01-01' AS d", + expected_same_as: "SELECT DATE '2020-01-01' AS d", + }, + SameReadingTestCase { + description: "TIMESTAMP", + sql: "SELECT TIMESTAMP r'2020-01-01 10:00:00' AS d", + expected_same_as: "SELECT TIMESTAMP '2020-01-01 10:00:00' AS d", + }, + SameReadingTestCase { + description: "TIMESTAMP_NTZ", + sql: "SELECT TIMESTAMP_NTZ r'2020-01-01 10:00:00' AS d", + expected_same_as: "SELECT TIMESTAMP_NTZ '2020-01-01 10:00:00' AS d", + }, + SameReadingTestCase { + description: "a hexadecimal binary", + sql: "SELECT X r'AB' AS d", + expected_same_as: "SELECT X'AB' AS d", + }, + SameReadingTestCase { + description: "an INTERVAL with a unit", + sql: "SELECT INTERVAL r'1' DAY AS d", + expected_same_as: "SELECT INTERVAL '1' DAY AS d", + }, + SameReadingTestCase { + description: "an INTERVAL string alone", + sql: "SELECT INTERVAL r'1 day' AS d", + expected_same_as: "SELECT INTERVAL '1 day' AS d", + }, + ]; + for test_case in test_cases { + assert_eq!( + reading(test_case.sql), + reading(test_case.expected_same_as), + "{}", + test_case.description + ); + } +} + #[test] fn given_adjacent_strings_when_analysed_then_they_read_as_the_string_they_join_to() { let test_cases = [ @@ -596,3 +640,75 @@ fn given_adjacent_strings_when_analysed_then_they_read_as_the_string_they_join_t ); } } + +#[test] +fn given_one_string_spelled_two_ways_when_grouping_then_the_expressions_match() { + let test_cases = [ + QueryFindingsTestCase { + description: "adjacent strings in the select, one string in GROUP BY", + sql: "SELECT COALESCE(status, 'a' 'b') AS x FROM orders GROUP BY COALESCE(status, 'ab')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "one string in the select, adjacent strings in GROUP BY", + sql: "SELECT COALESCE(status, 'ab') AS x FROM orders GROUP BY COALESCE(status, 'a' 'b')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "adjacent strings in a concatenation", + sql: "SELECT status || 'a' 'b' AS x FROM orders GROUP BY status || 'ab'", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a raw string in the select, a plain one in GROUP BY", + sql: "SELECT COALESCE(status, r'ab') AS x FROM orders GROUP BY COALESCE(status, 'ab')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a plain string in the select, a raw one in GROUP BY", + sql: "SELECT COALESCE(status, 'ab') AS x FROM orders GROUP BY COALESCE(status, r'ab')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a raw DATE literal in the select, a plain one in GROUP BY", + sql: "SELECT datediff(ordered_at, DATE r'2020-01-01') AS x FROM orders GROUP BY datediff(ordered_at, DATE '2020-01-01')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a plain TIMESTAMP literal in the select, a raw one in GROUP BY", + sql: "SELECT COALESCE(ordered_at, TIMESTAMP '2020-01-01 00:00:00') AS x FROM orders GROUP BY COALESCE(ordered_at, TIMESTAMP r'2020-01-01 00:00:00')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a raw X literal in the select, a plain one in GROUP BY", + sql: "SELECT COALESCE(CAST(status AS BINARY), X r'AB') AS x FROM orders GROUP BY COALESCE(CAST(status AS BINARY), X'AB')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a raw DATE literal of another value is still a different expression", + sql: "SELECT datediff(ordered_at, DATE r'2020-01-02') AS x FROM orders GROUP BY datediff(ordered_at, DATE '2020-01-01')", + expected_findings: &[( + "E230", + "Column must be grouped or used in an aggregate function", + 16, + )], + }, + QueryFindingsTestCase { + description: "adjacent strings of another value are still a different expression", + sql: "SELECT COALESCE(status, 'a' 'c') AS x FROM orders GROUP BY COALESCE(status, 'ab')", + expected_findings: &[( + "E230", + "Column must be grouped or used in an aggregate function", + 16, + )], + }, + ]; + for test_case in test_cases { + assert_eq!( + databricks_findings(test_case.sql), + owned_findings(test_case.expected_findings), + "{}", + test_case.description + ); + } +} 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 cc300f5e2..20ab85452 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs @@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet}; -use serde_json::Value; +use serde_json::{Map, Value}; use sqlquack_syntax::Expression; use sqlquack_syntax::ExpressionWalk; use sqlquack_syntax::expressions::names::models::{Column, Identifier, Literal}; @@ -15,8 +15,9 @@ 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, + CAST_FIELDS, CUSTOM_TYPE_FIELDS, LITERAL_FIELD, LITERAL_TYPE_FIELD, LITERAL_VALUE_FIELD, + NAME_FIELD, RAW_STRING_KIND, RAW_TYPED_LITERALS, SCOPE_PHASE, SHAPE_COMMENT_SUFFIX, + SHAPE_COSMETIC_FIELDS, SPAN_FIELD, WINDOW_ONLY_FUNCTIONS, }; use crate::lineage::models::Engine; use crate::lineage::models::{LineageContext, LineageScope, NameFinding, ScopeSource}; @@ -804,8 +805,9 @@ fn same_group(group: &Expression, current: &Expression, level: RuleLevel<'_, '_> if group == current { return true; } - if Engine::of(level.context) != Engine::BigQuery - || std::mem::discriminant(group) != std::mem::discriminant(current) + let bigquery: bool = Engine::of(level.context) == Engine::BigQuery; + if std::mem::discriminant(group) != std::mem::discriminant(current) + || !(bigquery || spelled_strings(group) || spelled_strings(current)) { return false; } @@ -814,6 +816,10 @@ fn same_group(group: &Expression, current: &Expression, level: RuleLevel<'_, '_> else { return false; }; + let (group_shape, current_shape) = (plain_strings(group_shape), plain_strings(current_shape)); + if !bigquery { + return group_shape == current_shape; + } let (group_columns, current_columns) = (columns_in(group), columns_in(current)); without_columns(group_shape) == without_columns(current_shape) && group_columns.len() == current_columns.len() @@ -823,6 +829,82 @@ fn same_group(group: &Expression, current: &Expression, level: RuleLevel<'_, '_> .all(|(left, right)| column_key(left, level) == column_key(right, level)) } +/// Whether `expression` holds a string literal written raw or as adjacent pieces. +fn spelled_strings(expression: &Expression) -> bool { + expression.contains(|node| { + matches!( + node, + Expression::Literal(literal) + if matches!(literal.as_ref(), Literal::RawString(_) | Literal::JoinedString(..)) + ) + }) +} + +/// A serialized expression without source ranges, raw, adjacent and raw typed strings as plain. +fn plain_strings(value: Value) -> Value { + match value { + Value::Object(mut map) => { + if let Some(literal) = raw_typed_literal(&map) { + return literal; + } + let spelled = matches!( + map.get(LITERAL_TYPE_FIELD).and_then(Value::as_str), + Some("raw_string" | "joined_string") + ); + if spelled { + let text: Option = match map.remove(LITERAL_VALUE_FIELD) { + Some(Value::Array(mut parts)) if !parts.is_empty() => { + Some(parts.swap_remove(0)) + } + other => other, + }; + map.insert( + LITERAL_TYPE_FIELD.to_owned(), + Value::String("string".to_owned()), + ); + map.insert(LITERAL_VALUE_FIELD.to_owned(), text.unwrap_or(Value::Null)); + } + Value::Object( + map.into_iter() + .filter(|(key, _)| key != SPAN_FIELD) + .map(|(key, item)| (key, plain_strings(item))) + .collect(), + ) + } + Value::Array(items) => Value::Array(items.into_iter().map(plain_strings).collect()), + other => other, + } +} + +/// The typed literal a serialized cast of a raw string to `DATE`, `TIME`, `TIMESTAMP` or `X` reads as. +fn raw_typed_literal(map: &Map) -> Option { + let [cast_field, this_field, to_field] = CAST_FIELDS; + let [type_field, custom] = CUSTOM_TYPE_FIELDS; + let cast: &Map = map.get(cast_field)?.as_object()?; + let text: &Value = cast.get(this_field)?.get(LITERAL_FIELD)?; + let to: &Value = cast.get(to_field)?; + if map.len() != 1 + || text.get(LITERAL_TYPE_FIELD)?.as_str()? != RAW_STRING_KIND + || to.get(type_field)?.as_str()? != custom + { + return None; + } + let word: &str = to.get(NAME_FIELD)?.as_str()?; + let (_, kind) = RAW_TYPED_LITERALS.iter().find(|(name, _)| *name == word)?; + let mut literal: Map = Map::new(); + literal.insert( + LITERAL_TYPE_FIELD.to_owned(), + Value::String((*kind).to_owned()), + ); + literal.insert( + LITERAL_VALUE_FIELD.to_owned(), + text.get(LITERAL_VALUE_FIELD)?.clone(), + ); + let mut node: Map = Map::new(); + node.insert(LITERAL_FIELD.to_owned(), Value::Object(literal)); + Some(Value::Object(node)) +} + /// The column references of `expression`, in tree order. fn columns_in(expression: &Expression) -> Vec<&Column> { let mut found: Vec<&Column> = Vec::new(); diff --git a/crates/sqlquack-semantics/src/lineage/constants.rs b/crates/sqlquack-semantics/src/lineage/constants.rs index ad1babe0b..d0f64f659 100644 --- a/crates/sqlquack-semantics/src/lineage/constants.rs +++ b/crates/sqlquack-semantics/src/lineage/constants.rs @@ -238,6 +238,25 @@ pub(crate) const WINDOW_ONLY_FUNCTIONS: [&str; 11] = [ pub(crate) const SPAN_FIELD: &str = "span"; /// The field a serialized node records its name in. pub(crate) const NAME_FIELD: &str = "name"; +/// The field a serialized literal records its kind in. +pub(crate) const LITERAL_TYPE_FIELD: &str = "literal_type"; +/// The field a serialized literal records its value in. +pub(crate) const LITERAL_VALUE_FIELD: &str = "value"; +/// The fields a serialized cast records its operand and its type in. +pub(crate) const CAST_FIELDS: [&str; 3] = ["cast", "this", "to"]; +/// The field a serialized literal expression records its literal in. +pub(crate) const LITERAL_FIELD: &str = "literal"; +/// The field a serialized data type records its kind in, and the kind of a type named by a word. +pub(crate) const CUSTOM_TYPE_FIELDS: [&str; 2] = ["data_type", "custom"]; +/// The kind of a serialized raw string literal. +pub(crate) const RAW_STRING_KIND: &str = "raw_string"; +/// Typed literal words whose plain string makes a literal of the kind given, a raw one a cast. +pub(crate) const RAW_TYPED_LITERALS: [(&str, &str); 4] = [ + ("DATE", "date"), + ("TIME", "time"), + ("TIMESTAMP", "timestamp"), + ("X", "hex_string"), +]; /// Snowflake's VARIANT type. pub(crate) const SNOWFLAKE_VARIANT_TYPE: &str = "VARIANT"; /// The aggregates of a PIVOT whose columns DuckDB names by value alone. diff --git a/crates/sqlquack-syntax-bigquery/src/parse/_helpers/commands/creates.rs b/crates/sqlquack-syntax-bigquery/src/parse/_helpers/commands/creates.rs index c009a3ffc..20b609370 100644 --- a/crates/sqlquack-syntax-bigquery/src/parse/_helpers/commands/creates.rs +++ b/crates/sqlquack-syntax-bigquery/src/parse/_helpers/commands/creates.rs @@ -144,6 +144,7 @@ impl Parser<'_> { self.at += 1; let entries: Vec = option_entries(self.options_list()?); properties.push(Expression::OptionsProperty(Box::new(OptionsProperty { + written_keys: Vec::new(), entries, string_keys: Vec::new(), without_equals: Vec::new(), diff --git a/crates/sqlquack-syntax-bigquery/src/parse/_helpers/commands/grants.rs b/crates/sqlquack-syntax-bigquery/src/parse/_helpers/commands/grants.rs index 54b9f1456..7f4c24523 100644 --- a/crates/sqlquack-syntax-bigquery/src/parse/_helpers/commands/grants.rs +++ b/crates/sqlquack-syntax-bigquery/src/parse/_helpers/commands/grants.rs @@ -139,9 +139,9 @@ impl Parser<'_> { if self.string_at(0) { return match self.string_literal()? { Expression::Literal(literal) => match *literal { - Literal::String(text) | Literal::RawString(text) => { - Ok(principal(Identifier::new(text), None)) - } + Literal::String(text) + | Literal::RawString(text) + | Literal::JoinedString(text, _) => Ok(principal(Identifier::new(text), None)), _ => Err(self.error()), }, _ => Err(self.error()), diff --git a/crates/sqlquack-syntax-bigquery/src/parse/_helpers/values/primary.rs b/crates/sqlquack-syntax-bigquery/src/parse/_helpers/values/primary.rs index b1c5b18a0..d5040e53c 100644 --- a/crates/sqlquack-syntax-bigquery/src/parse/_helpers/values/primary.rs +++ b/crates/sqlquack-syntax-bigquery/src/parse/_helpers/values/primary.rs @@ -63,18 +63,22 @@ impl Parser<'_> { Ok(self.spanned(first, node)) } - /// `string_literal` or `bytes_literal`: its chunks of one kind joined, with whether bytes. - fn literal_chunks(&mut self) -> Parsing<(String, bool)> { + /// `string_literal` or `bytes_literal`: its chunks of one kind, each as written, with whether bytes. + fn literal_chunks(&mut self) -> Parsing<(Vec, bool)> { let bytes: bool = match self.peek().map(|token| token.kind) { Some(TokenKind::String { bytes, .. }) => bytes, _ => return Err(self.error()), }; - let mut text: String = String::new(); + let mut chunks: Vec = Vec::new(); loop { - text.push_str(&self.advance()?.value); + let token: Token = self.advance()?; + chunks.push(match token.kind { + TokenKind::String { raw: true, .. } => Literal::RawString(token.value), + _ => Literal::String(token.value), + }); let Some(TokenKind::String { bytes: next, .. }) = self.peek().map(|token| token.kind) else { - return Ok((text, bytes)); + return Ok((chunks, bytes)); }; if next != bytes { return Err(self.error_with( @@ -98,30 +102,22 @@ impl Parser<'_> { pub(crate) fn string_lit(&mut self) -> Parsing { let start: usize = self.offset(); match self.literal_chunks()? { - (text, false) => Ok(text), + (chunks, false) => Ok(chunks.iter().map(Literal::value_str).collect()), (_, true) => { Err(self.error_with("Syntax error: Unexpected bytes literal", start, true)) } } } - /// A string or bytes literal; one raw string chunk stays raw. + /// A string or bytes literal; one string chunk stays as written, several keep their chunks. pub(crate) fn string_literal(&mut self) -> Parsing { - if let Some(token) = self.peek().cloned() - && token.kind - == (TokenKind::String { - raw: true, - bytes: false, - }) - && !self.string_at(1) - { - self.at += 1; - return Ok(literal(Literal::RawString(token.value))); - } - Ok(match self.literal_chunks()? { - (text, false) => string(&text), - (text, true) => literal(Literal::ByteString(text)), - }) + let (mut chunks, bytes) = self.literal_chunks()?; + let text: String = chunks.iter().map(Literal::value_str).collect(); + Ok(literal(match (bytes, chunks.len()) { + (true, _) => Literal::ByteString(text), + (false, 1) => chunks.remove(0), + (false, _) => Literal::JoinedString(text, chunks), + })) } fn primary_base(&mut self) -> Parsing { diff --git a/crates/sqlquack-syntax-bigquery/src/parse/tests.rs b/crates/sqlquack-syntax-bigquery/src/parse/tests.rs index f6c5bd622..157698728 100644 --- a/crates/sqlquack-syntax-bigquery/src/parse/tests.rs +++ b/crates/sqlquack-syntax-bigquery/src/parse/tests.rs @@ -1,5 +1,7 @@ #[path = "tests/helpers.rs"] mod helpers; +#[path = "tests/test_grants.rs"] +mod test_grants; #[path = "tests/test_parse.rs"] mod test_parse; #[path = "tests/test_types.rs"] diff --git a/crates/sqlquack-syntax-bigquery/src/parse/tests/test_grants.rs b/crates/sqlquack-syntax-bigquery/src/parse/tests/test_grants.rs new file mode 100644 index 000000000..ed382ac71 --- /dev/null +++ b/crates/sqlquack-syntax-bigquery/src/parse/tests/test_grants.rs @@ -0,0 +1,63 @@ +//! Verdicts of the BigQuery GoogleSQL parser for GRANT and REVOKE and their grantees. + +use crate::parse::tests::helpers::verdict; +use crate::parse::tests::test_types::{ExpectedVerdict, VerdictTestCase}; + +#[test] +fn given_a_grant_or_revoke_when_parsing_then_the_verdict_is_googlesql() { + let test_cases = [ + VerdictTestCase { + description: "GRANT on a kinded object to string grantees is structured", + sql: "GRANT `roles/bigquery.dataViewer` ON VIEW d.t TO 'user:a@example.com'", + expected_verdict: ExpectedVerdict::Accepted { opaque: false }, + }, + VerdictTestCase { + description: "REVOKE takes a parameter grantee", + sql: "REVOKE x ON EXTERNAL TABLE d.t FROM @p", + expected_verdict: ExpectedVerdict::Accepted { opaque: false }, + }, + VerdictTestCase { + description: "a grantee written as adjacent strings", + sql: "GRANT `roles/bigquery.dataViewer` ON TABLE d.t TO 'user:a' '@x.com'", + expected_verdict: ExpectedVerdict::Accepted { opaque: false }, + }, + VerdictTestCase { + description: "a grantee written as a raw string beside a plain one", + sql: "GRANT `roles/bigquery.dataViewer` ON TABLE d.t TO r'user:a' '@x.com'", + expected_verdict: ExpectedVerdict::Accepted { opaque: false }, + }, + VerdictTestCase { + description: "adjacent strings as the second grantee of a list", + sql: "GRANT x ON TABLE d.t TO 'user:b@x.com', 'user:a' '@x.com'", + expected_verdict: ExpectedVerdict::Accepted { opaque: false }, + }, + VerdictTestCase { + description: "REVOKE from a grantee written as adjacent strings", + sql: "REVOKE x ON TABLE d.t FROM 'user:a' '@x.com'", + expected_verdict: ExpectedVerdict::Accepted { opaque: false }, + }, + VerdictTestCase { + description: "a grantee is no bytes literal", + sql: "GRANT x ON d.t TO b'a'", + expected_verdict: ExpectedVerdict::Rejected { syntax: true }, + }, + VerdictTestCase { + description: "GRANT has no WITH GRANT OPTION", + sql: "GRANT x ON d.t TO 'a' WITH GRANT OPTION", + expected_verdict: ExpectedVerdict::Rejected { syntax: true }, + }, + VerdictTestCase { + description: "an object takes at most two kind words", + sql: "GRANT x ON a b c d.t TO 'a'", + expected_verdict: ExpectedVerdict::Rejected { syntax: true }, + }, + ]; + for test_case in test_cases { + assert_eq!( + verdict(test_case.sql), + test_case.expected_verdict, + "{}", + test_case.description + ); + } +} diff --git a/crates/sqlquack-syntax-bigquery/src/parse/tests/test_parse.rs b/crates/sqlquack-syntax-bigquery/src/parse/tests/test_parse.rs index e4bb9ef09..c34f74641 100644 --- a/crates/sqlquack-syntax-bigquery/src/parse/tests/test_parse.rs +++ b/crates/sqlquack-syntax-bigquery/src/parse/tests/test_parse.rs @@ -562,31 +562,6 @@ fn given_sql_when_parsing_then_the_verdict_is_googlesql() { sql: "SELECT * FROM `a`-b", expected_verdict: ExpectedVerdict::Rejected { syntax: true }, }, - VerdictTestCase { - description: "GRANT on a kinded object to string grantees is structured", - sql: "GRANT `roles/bigquery.dataViewer` ON VIEW d.t TO 'user:a@example.com'", - expected_verdict: ExpectedVerdict::Accepted { opaque: false }, - }, - VerdictTestCase { - description: "REVOKE takes a parameter grantee", - sql: "REVOKE x ON EXTERNAL TABLE d.t FROM @p", - expected_verdict: ExpectedVerdict::Accepted { opaque: false }, - }, - VerdictTestCase { - description: "a grantee is no bytes literal", - sql: "GRANT x ON d.t TO b'a'", - expected_verdict: ExpectedVerdict::Rejected { syntax: true }, - }, - VerdictTestCase { - description: "GRANT has no WITH GRANT OPTION", - sql: "GRANT x ON d.t TO 'a' WITH GRANT OPTION", - expected_verdict: ExpectedVerdict::Rejected { syntax: true }, - }, - VerdictTestCase { - description: "an object takes at most two kind words", - sql: "GRANT x ON a b c d.t TO 'a'", - expected_verdict: ExpectedVerdict::Rejected { syntax: true }, - }, VerdictTestCase { description: "digits after a dot in a table path are a name part, letters following", sql: "SELECT * FROM foo.bar.25x a", @@ -766,6 +741,24 @@ fn given_sql_when_parsing_then_the_verdict_is_googlesql() { #[test] fn given_sql_when_parsing_then_the_tree_keeps_what_lineage_reads() { let test_cases = [ + ValueTestCase { + description: "adjacent strings keep their pieces", + sql: "SELECT 'a' 'b'", + pointer: "/0/select/expressions/0/literal", + expected_json: r#"{"literal_type":"joined_string","value":["ab",[{"literal_type":"string","value":"a"},{"literal_type":"string","value":"b"}]]}"#, + }, + ValueTestCase { + description: "a raw piece of adjacent strings stays raw", + sql: "SELECT r'a' \"b\"", + pointer: "/0/select/expressions/0/literal/value/1/0/literal_type", + expected_json: "\"raw_string\"", + }, + ValueTestCase { + description: "one string is a plain string", + sql: "SELECT 'ab'", + pointer: "/0/select/expressions/0/literal", + expected_json: r#"{"literal_type":"string","value":"ab"}"#, + }, ValueTestCase { description: "a backquoted project.dataset.table path splits into its parts", sql: "SELECT o.id FROM `p.d.orders` AS o", diff --git a/crates/sqlquack-syntax-clickhouse/src/parse/_helpers/commands/ddl.rs b/crates/sqlquack-syntax-clickhouse/src/parse/_helpers/commands/ddl.rs index 3034d43b2..bb07116c4 100644 --- a/crates/sqlquack-syntax-clickhouse/src/parse/_helpers/commands/ddl.rs +++ b/crates/sqlquack-syntax-clickhouse/src/parse/_helpers/commands/ddl.rs @@ -416,6 +416,8 @@ impl Parser<'_> { .parameters .iter() .map(|parameter| FunctionParameter { + comment: None, + comment_written: None, name: Some(parameter.clone()), data_type: DataType::Unknown, mode: 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 cdb3f15d9..a8eb32b15 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/alters.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/alters.rs @@ -297,10 +297,12 @@ impl Parser<'_> { )?); } else if self.kw("SET") && self.kw_at(1, "LOCATION") { self.at += 2; - let location: String = self.string_lit()?; + let (location, location_written) = self.written_string()?; node.partition = Some(partition_pairs(spec)); - node.actions - .push(AlterTableAction::SetLocation { location }); + node.actions.push(AlterTableAction::SetLocation { + location_written, + location, + }); } else if self.kw("SET") && self.kw_in(1, &["SERDE", "SERDEPROPERTIES"]) { let (key, values) = self.serde_clause()?; if deep { @@ -513,7 +515,9 @@ impl Parser<'_> { } else if self.eat_kw("DEFAULT") { column.default = Some(self.expression()?); } else if self.eat_kw("COMMENT") { - column.comment = Some(self.string_lit()?); + let (comment, written) = self.written_string()?; + column.comment = Some(comment); + column.comment_written = written; } else if self.kw_in(0, &["FIRST", "AFTER"]) { position = Some(self.column_position()?); } else if self.eat_kw("MASK") { @@ -749,14 +753,17 @@ impl Parser<'_> { altered.push(keyed); } else { match self.column_action()? { - Some(action) => altered.push(AlterTableAction::AlterColumn { - name, - action, - use_modify_keyword: false, - column_keyword, - action_words: None, - keyword: change.then(|| "CHANGE".to_owned()), - }), + Some((action, comment_written)) => { + altered.push(AlterTableAction::AlterColumn { + comment_written, + name, + action, + use_modify_keyword: false, + column_keyword, + action_words: None, + keyword: change.then(|| "CHANGE".to_owned()), + }) + } None if self.opaque => {} None if self.read_gap() => self.gap("alter_column_without_action", spec_start), None => { @@ -803,15 +810,19 @@ impl Parser<'_> { self.at += 2; self.gap("change_column_not_null", start); } - let comment: Option = match self.eat_kw("COMMENT") { - true => Some(self.string_lit()?), - false => None, + let (comment, comment_written) = match self.eat_kw("COMMENT") { + true => { + let (comment, written) = self.written_string()?; + (Some(comment), written) + } + false => (None, None), }; if self.kw_in(0, &["FIRST", "AFTER"]) { self.column_position()?; self.gap("change_column_position", start); } Ok(AlterTableAction::ChangeColumn { + comment_written, old_name, new_name, data_type: Some(data_type), @@ -821,35 +832,43 @@ impl Parser<'_> { } /// `alterColumnAction`, with Databricks' masks and identity sync; none if absent or a gap. - fn column_action(&mut self) -> Parsing> { + fn column_action(&mut self) -> Parsing)>> { if self.eat_kw("TYPE") { let data_type: DataType = self.data_type()?; - return Ok(Some(AlterColumnAction::SetDataType { - data_type, - using: None, - collate: None, - rename: None, - })); + return Ok(Some(( + AlterColumnAction::SetDataType { + data_type, + using: None, + collate: None, + rename: None, + }, + None, + ))); } if self.eat_kw("COMMENT") { - return Ok(Some(AlterColumnAction::Comment(self.string_lit()?))); + let (comment, written) = self.written_string()?; + return Ok(Some((AlterColumnAction::Comment(comment), written))); } if self.kw_in(0, &["SET", "DROP"]) && (self.kw_at(1, "NOT") || self.char_at(1, '!')) { let set: bool = self.kw("SET"); self.at += 2; self.expect_kw("NULL")?; - return Ok(Some(match set { + let action: AlterColumnAction = match set { true => AlterColumnAction::SetNotNull, false => AlterColumnAction::DropNotNull, - })); + }; + return Ok(Some((action, None))); } if self.kw("SET") && self.kw_at(1, "DEFAULT") { self.at += 2; - return Ok(Some(AlterColumnAction::SetDefault(self.expression()?))); + return Ok(Some(( + AlterColumnAction::SetDefault(self.expression()?), + None, + ))); } if self.kw("DROP") && self.kw_at(1, "DEFAULT") { self.at += 2; - return Ok(Some(AlterColumnAction::DropDefault)); + return Ok(Some((AlterColumnAction::DropDefault, None))); } Ok(None) } @@ -873,9 +892,13 @@ impl Parser<'_> { without_equals, })) } - "LOCATION" => Ok(Some(AlterTableAction::SetLocation { - location: self.string_lit()?, - })), + "LOCATION" => { + let (location, location_written) = self.written_string()?; + Ok(Some(AlterTableAction::SetLocation { + location_written, + location, + })) + } "TAGS" => { let pairs: Vec<(Expression, Expression)> = self .property_list()? @@ -1074,7 +1097,7 @@ impl Parser<'_> { loop { self.partition_spec()?; if self.eat_kw("LOCATION") { - self.string_lit()?; + self.string_spelling()?; } if !self.kw("PARTITION") { break; 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 29d2fbc74..55297dc90 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/creates.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/creates.rs @@ -74,6 +74,11 @@ fn modifiers_of(constraint: &mut TableConstraint) -> Option<&mut ConstraintModif } } +/// A comment that was given, with its written spelling, as the optional pair a node stores. +fn given((text, written): (String, Option)) -> (Option, Option) { + (Some(text), written) +} + /// Whether the written foreign key options repeat one or put MATCH between the two actions. fn order_unsaid(written: &[String]) -> bool { let distinct: std::collections::BTreeSet<&String> = written.iter().collect(); @@ -269,21 +274,26 @@ impl Parser<'_> { let clause: (&'static str, Option) = match word.as_str() { "OPTIONS" if self.char_at(1, '(') => { self.at += 1; - let spelled: Vec<(OptionEntry, bool, bool)> = + let spelled: Vec<(OptionEntry, OptionSpelling)> = self.parenthesized(|parser| parser.table_option())?; let mut entries: Vec = Vec::new(); let mut string_keys: Vec = Vec::new(); let mut without_equals: Vec = Vec::new(); - for (at, (entry, string_key, without)) in spelled.into_iter().enumerate() { - if string_key { + let mut written_keys: Vec<(usize, Literal)> = Vec::new(); + for (at, (entry, spelling)) in spelled.into_iter().enumerate() { + if let Some(written) = spelling.written { + written_keys.push((at, written)); + } + if spelling.string_key { string_keys.push(at); } - if without { + if spelling.without_equals { without_equals.push(at); } entries.push(entry); } let options = Expression::OptionsProperty(Box::new(OptionsProperty { + written_keys, entries, string_keys, without_equals, @@ -379,17 +389,27 @@ impl Parser<'_> { Ok(Some(clause)) } - /// One `OPTIONS` entry, whether its key is a string and whether its value lacks `=`. - fn table_option(&mut self) -> Parsing<(OptionEntry, bool, bool)> { + /// One `OPTIONS` entry and how it was written. + fn table_option(&mut self) -> Parsing<(OptionEntry, OptionSpelling)> { let mut strings: usize = 0; while self.string_at(strings) { strings += 1; } let string_key: bool = self.string_at(0); - let key: Identifier = match string_key { - true if !self.char_at(strings, '=') => Identifier::new(self.advance()?.value), - true => Identifier::new(self.string_lit()?), - false => joined(self.multipart_identifier()?), + let (key, written): (Identifier, Option) = match string_key { + true if !self.char_at(strings, '=') => { + let piece: Literal = self.one_string()?; + let text: String = piece.value_str().to_string(); + ( + Identifier::new(text), + (!matches!(piece, Literal::String(_))).then_some(piece), + ) + } + true => { + let (text, written) = self.written_string()?; + (Identifier::new(text), written) + } + false => (joined(self.multipart_identifier()?), None), }; let equals: bool = self.eat_char('='); if !equals && self.terminal_at(0) { @@ -403,7 +423,12 @@ impl Parser<'_> { )); } let value: Expression = self.expression()?; - Ok((OptionEntry { key, value }, string_key, !equals)) + let spelling: OptionSpelling = OptionSpelling { + string_key, + without_equals: !equals, + written, + }; + Ok((OptionEntry { key, value }, spelling)) } /// `bucketSpec`: `CLUSTERED BY (columns) [SORTED BY (columns)] INTO n BUCKETS`. @@ -640,7 +665,9 @@ impl Parser<'_> { column.nullable = Some(false); } if self.eat_kw("COMMENT") { - column.comment = Some(self.string_lit()?); + let (comment, written) = self.written_string()?; + column.comment = Some(comment); + column.comment_written = written; } Ok(Expression::ColumnDef(Box::new(column))) } @@ -659,7 +686,9 @@ impl Parser<'_> { } else if self.eat_kw("DEFAULT") { column.default = Some(self.expression()?); } else if self.eat_kw("COMMENT") { - column.comment = Some(self.string_lit()?); + let (comment, written) = self.written_string()?; + column.comment = Some(comment); + column.comment_written = written; } else if self.eat_kw("GENERATED") { column.constraints.push(self.generation_expression()?); } else if self.kw("MASK") { @@ -1017,12 +1046,12 @@ impl Parser<'_> { let mut column: ViewColumn = ViewColumn::new(String::new()); column.name = parser.ident()?; if parser.eat_kw("COMMENT") { - column.comment = Some(parser.string_lit()?); + (column.comment, column.comment_written) = given(parser.written_string()?); } Ok(column) })?; } - let mut comment: Option = None; + let mut comment: Option<(String, Option)> = None; let mut table_properties: Vec = Vec::new(); let mut copy_grants: bool = false; let mut options: Vec = Vec::new(); @@ -1036,7 +1065,7 @@ impl Parser<'_> { if comment.is_some() { return Err(self.error_with("Found duplicate clauses: COMMENT", start, true)); } - comment = Some(self.string_lit()?); + comment = Some(self.written_string()?); } else if self.eat_kw("TBLPROPERTIES") { let properties: Expression = self.properties_node(false)?; table_properties.push(properties); @@ -1052,7 +1081,7 @@ impl Parser<'_> { node.if_not_exists = if_not_exists; node.temporary = head.temporary; node.temporary_spelling = head.temporary_spelling; - node.comment = comment; + (node.comment, node.comment_written) = comment.map_or((None, None), given); node.table_properties = table_properties; node.copy_grants = copy_grants; node.options = options; @@ -1087,7 +1116,7 @@ impl Parser<'_> { node.if_not_exists = if_not_exists; node.temporary = head.temporary; node.temporary_spelling = head.temporary_spelling; - node.comment = comment; + (node.comment, node.comment_written) = comment.map_or((None, None), given); node.table_properties = table_properties; node.query_parenthesized = parenthesized; node.copy_grants = copy_grants; @@ -1146,7 +1175,9 @@ impl Parser<'_> { node.options .push(property("LANGUAGE", var(&language.value))); } else if self.eat_kw("COMMENT") { - node.comment = Some(self.string_lit()?); + let (comment, written) = self.written_string()?; + node.comment = Some(comment); + node.comment_written = written; } else if self.eat_kw("TBLPROPERTIES") { let properties: Expression = self.properties_node(false)?; node.table_properties.push(properties); @@ -1188,7 +1219,7 @@ impl Parser<'_> { parser.at += 2; } if parser.eat_kw("COMMENT") { - parser.string_lit()?; + parser.string_spelling()?; } Ok(()) })?; @@ -1292,3 +1323,10 @@ impl Parser<'_> { } } } + +/// How one `OPTIONS` entry was written: a string key, no `=`, and a raw or adjacent-string key. +pub(crate) struct OptionSpelling { + string_key: bool, + without_equals: bool, + written: Option, +} 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 84553afa9..e8bea6039 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/delta.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/delta.rs @@ -66,6 +66,7 @@ impl Parser<'_> { string_keys: Vec::new(), bare_values: Vec::new(), written_keys: Vec::new(), + written_values: Vec::new(), }; if self.eat_kw("CREDENTIAL") { match self.is_char('(') { @@ -76,6 +77,21 @@ impl Parser<'_> { credentials.quoted_keys = spellings.quoted; credentials.string_keys = spellings.strings; credentials.written_keys = spellings.written; + credentials.written_values = options + .iter() + .enumerate() + .filter_map(|(position, ((_, value), _))| match value { + Expression::Literal(literal) + if matches!( + literal.as_ref(), + Literal::RawString(_) | Literal::JoinedString(..) + ) => + { + Some((position, literal.as_ref().clone())) + } + _ => None, + }) + .collect(); for (position, ((key, value), without_equals)) in options.into_iter().enumerate() { @@ -293,7 +309,7 @@ impl Parser<'_> { if self.kw("WITH") && self.kw_at(1, "ID") { let lead: usize = self.at - first; self.at += 2; - self.string_lit()?; + self.string_spelling()?; if let Some(tree) = self.tree_within(first, self.at, lead, UNDROP_WITH_ID) { self.treed("undrop_with_id", start); return Ok(tree); @@ -316,7 +332,7 @@ impl Parser<'_> { self.expect_kw("DATA")?; let local: bool = self.eat_kw("LOCAL"); self.expect_kw("INPATH")?; - let inpath: String = self.string_lit()?; + let (inpath, inpath_written) = self.written_string()?; let overwrite: bool = self.eat_kw("OVERWRITE"); self.expect_kw("INTO")?; self.expect_kw("TABLE")?; @@ -330,6 +346,7 @@ impl Parser<'_> { false => Vec::new(), }; Ok(Expression::LoadData(Box::new(LoadData { + inpath_written, local, inpath, overwrite, 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 e1c6dd623..506ef05e1 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/dml.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/dml.rs @@ -126,12 +126,16 @@ impl Parser<'_> { let local: bool = self.eat_kw("LOCAL"); self.expect_kw("DIRECTORY")?; let path: bool = self.string_at(0); + let (path_text, path_written) = match path { + true => self.written_string()?, + false => (String::new(), None), + }; let mut node: DirectoryInsert = DirectoryInsert { + input_format_written: None, + output_format_written: None, + path_written, local, - path: match path { - true => self.string_lit()?, - false => String::new(), - }, + path: path_text, row_format: None, stored_as: None, stored_as_quoted: false, @@ -191,9 +195,13 @@ impl Parser<'_> { } self.at += 2; if self.eat_kw("INPUTFORMAT") { - node.input_format = Some(self.string_lit()?); + let (class, written) = self.written_string()?; + node.input_format = Some(class); + node.input_format_written = written; self.expect_kw("OUTPUTFORMAT")?; - node.output_format = Some(self.string_lit()?); + let (class, written) = self.written_string()?; + node.output_format = Some(class); + node.output_format_written = written; } else { let format: Identifier = self.ident()?; node.stored_as_quoted = format.quoted; 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 e069e0ccd..aae17a33d 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/routines.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/routines.rs @@ -2,7 +2,7 @@ use sqlquack_syntax::DataType; use sqlquack_syntax::Expression; -use sqlquack_syntax::expressions::names::models::Identifier; +use sqlquack_syntax::expressions::names::models::{Identifier, Literal}; use sqlquack_syntax::expressions::schema_objects::models::{ CreateFunction, CreateSchema, DropFunction, DropSchema, FunctionBody, FunctionParameter, FunctionSecurity, FunctionUsingResource, SqlDataAccess, @@ -112,13 +112,14 @@ impl Parser<'_> { let name: Identifier = self.ident()?; let data_type: DataType = self.data_type()?; let mut default: Option = None; + let mut comment: Option<(String, Option)> = None; loop { if (self.kw("NOT") || self.is_char('!')) && self.kw_at(1, "NULL") { self.at += 2; } else if self.eat_kw("DEFAULT") { default = Some(self.expression()?); } else if self.eat_kw("COMMENT") { - self.string_lit()?; + comment = Some(self.written_string()?); } else { break; } @@ -129,6 +130,8 @@ impl Parser<'_> { mode: None, default, mode_text: None, + comment_written: comment.as_ref().and_then(|(_, written)| written.clone()), + comment: comment.map(|(text, _)| text), span: SourceSpan::default(), }) } @@ -151,14 +154,18 @@ impl Parser<'_> { node.name = table(self.multipart_identifier()?); if self.eat_kw("AS") { node.has_parens = false; - node.body = Some(FunctionBody::StringLiteral(self.string_lit()?)); + let (body, written) = self.written_string()?; + node.body = Some(FunctionBody::StringLiteral(body)); + node.body_written = written; if self.eat_kw("USING") { node.using_resources = self.comma_list(|parser| { let kind: Identifier = parser.ident()?; + let (uri, uri_written) = parser.written_string()?; Ok(FunctionUsingResource { + uri_written, kind: kind.name.to_ascii_uppercase(), kind_quoted: kind.quoted, - uri: parser.string_lit()?, + uri, }) })?; } @@ -198,7 +205,9 @@ impl Parser<'_> { if foreign && self.kw("HANDLER") { let handler: usize = self.offset(); self.at += 1; - node.handler = Some(self.string_lit()?); + let (handler_text, written) = self.written_string()?; + node.handler = Some(handler_text); + node.handler_written = written; let scala: bool = node .language .as_deref() diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/scripting.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/scripting.rs index 0f541650b..4132409db 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/scripting.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/scripting.rs @@ -404,8 +404,10 @@ impl Parser<'_> { true => { self.expect_kw("SQLSTATE")?; let value_keyword: bool = self.eat_kw("VALUE"); + let (state, state_written) = self.written_string()?; Some(ScriptingSqlstate { - state: self.string_lit()?, + state_written, + state, value_keyword, }) } @@ -629,8 +631,10 @@ impl Parser<'_> { fn sqlstate(&mut self) -> Parsing { self.at += 1; let value_keyword: bool = self.eat_kw("VALUE"); + let (state, state_written) = self.written_string()?; Ok(ScriptingSqlstate { - state: self.string_lit()?, + state_written, + state, value_keyword, }) } diff --git a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/utility.rs b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/utility.rs index 1fff37871..56a2b7826 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/utility.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/utility.rs @@ -1,7 +1,7 @@ //! The auxiliary statements (SqlBaseParser.g4), such as `DESCRIBE`, `SHOW` and `SET`. use sqlquack_syntax::Expression; -use sqlquack_syntax::expressions::names::models::{Identifier, TableRef}; +use sqlquack_syntax::expressions::names::models::{Identifier, Literal, TableRef}; use sqlquack_syntax::expressions::schema_objects::models::{ Grant, GrantPrincipal, Privilege, Revoke, }; @@ -15,7 +15,7 @@ use crate::parse::_helpers::commands::alters::partition; use crate::parse::_helpers::commands::queries::limit; use crate::parse::_helpers::tree::cursor::{Parser, Parsing, identifier, is_identifier}; use crate::parse::_helpers::tree::nodes::{ - alias, call, column, column_from, dot, joined, null, property, qualified, string, table, + alias, call, column, column_from, dot, joined, null, property, qualified, table, too_deep_to_keep, tuple, utility, var, }; use crate::parse::constants::{ @@ -424,7 +424,7 @@ impl Parser<'_> { node.target = Some(Expression::Table(Box::new(self.table_reference()?))); if self.eat_char('(') { let key: Expression = match self.string_at(0) { - true => string(&self.string_lit()?), + true => self.string_literal()?, false => qualified(self.multipart_identifier()?), }; self.expect_char(')')?; @@ -528,11 +528,15 @@ impl Parser<'_> { let value: Expression = self.expression()?; return Ok(set_statement(vec![set_item("CATALOG", value, None)])); } - let parts: Vec = match string_name { - true => vec![Identifier::new(self.string_lit()?)], - false => vec![self.ident()?], + let (parts, name_written): (Vec, Option) = match string_name { + true => { + let (name, written) = self.written_string()?; + (vec![Identifier::new(name)], written) + } + false => (vec![self.ident()?], None), }; return Ok(Expression::Use(Box::new(Use { + name_written, kind: Some(UseKind::Catalog), this: joined(parts.clone()), roles: Vec::new(), @@ -706,11 +710,15 @@ impl Parser<'_> { .filter(|word| word == NAMESPACE); self.at += usize::from(kind.is_some()); let string_name: bool = kind == Some(UseKind::Catalog) && self.string_at(0); - let parts: Vec = match string_name { - true => vec![Identifier::new(self.string_lit()?)], - false => self.multipart_identifier()?, + let (parts, name_written): (Vec, Option) = match string_name { + true => { + let (name, written) = self.written_string()?; + (vec![Identifier::new(name)], written) + } + false => (self.multipart_identifier()?, None), }; Ok(Expression::Use(Box::new(Use { + name_written, kind, this: joined(parts.clone()), roles: Vec::new(), 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 ae840a246..0553a8dee 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 @@ -281,9 +281,12 @@ impl Parser<'_> { self.at += 2; options.push(var("NOT NULL")); } - let comment: Option = match self.eat_kw("COMMENT") { - true => Some(self.string_lit()?), - false => None, + let (comment, comment_written) = match self.eat_kw("COMMENT") { + true => { + let (comment, written) = self.written_string()?; + (Some(comment), written) + } + false => (None, None), }; let name: String = match name.quoted { true => format!("`{}`", name.name.replace('`', "``")), @@ -293,6 +296,7 @@ impl Parser<'_> { StructField::with_options_and_comment(name, data_type, options, comment); field.collation = collation.map(Box::new); field.colon = colon; + field.comment_written = comment_written; Ok(field) } } 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 e22870d51..09476ac8b 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/primary.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/values/primary.rs @@ -228,20 +228,8 @@ impl Parser<'_> { Ok(self.spanned(first, node)) } - /// `stringLit`: one or more adjacent string literals, joined. - pub(crate) fn string_lit(&mut self) -> Parsing { - if !self.string_at(0) { - return Err(self.error()); - } - let mut text: String = String::new(); - while self.string_at(0) { - text.push_str(&self.advance()?.value); - } - Ok(text) - } - - /// `stringLit` as a string literal: a single raw string stays raw, adjacent strings keep their pieces. - pub(crate) fn string_literal(&mut self) -> Parsing { + /// `stringLit` as one literal: a single string as written, adjacent strings joined with their pieces. + pub(crate) fn string_spelling(&mut self) -> Parsing { if !self.string_at(0) { return Err(self.error()); } @@ -250,14 +238,29 @@ impl Parser<'_> { pieces.push(self.one_string()?); } Ok(match pieces.len() { - 1 => literal(pieces.remove(0)), + 1 => pieces.remove(0), _ => { let joined: String = pieces.iter().map(Literal::value_str).collect(); - literal(Literal::JoinedString(joined, pieces)) + Literal::JoinedString(joined, pieces) } }) } + /// `stringLit` as text, with its spelling when it was a raw string or adjacent strings. + pub(crate) fn written_string(&mut self) -> Parsing<(String, Option)> { + let spelling: Literal = self.string_spelling()?; + let text: String = spelling.value_str().to_string(); + Ok(( + text, + (!matches!(spelling, Literal::String(_))).then_some(spelling), + )) + } + + /// `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(literal(self.string_spelling()?)) + } + /// One string token as a literal; a raw string stays raw. pub(crate) fn one_string(&mut self) -> Parsing { let token: Token = self.advance()?; @@ -567,6 +570,10 @@ impl Parser<'_> { literal(Literal::Number(format!("-{}", token.value))) } TokenKind::Number(_) => literal(Literal::Number(token.value)), + TokenKind::String { raw: true } if negative => Expression::Neg(Box::new(UnaryOp::new( + literal(Literal::RawString(token.value)), + ))), + TokenKind::String { raw: true } => literal(Literal::RawString(token.value)), _ if negative => Expression::Neg(Box::new(UnaryOp::new(string(&token.value)))), _ => string(&token.value), }; @@ -652,21 +659,21 @@ impl Parser<'_> { } } - /// `literalType singleStringLitWithoutMarker`: `DATE '...'`, `TIMESTAMP '...'`, `X'...'`. + /// `literalType singleStringLitWithoutMarker`, as `DATE '...'`; a raw string is cast to the word. fn typed_literal(&mut self, word: &str) -> Parsing { let at: usize = self.offset(); self.at += 1; - let value: String = self.advance()?.value; - if !is_typed_text(word, &value) { - return Err(self.invalid_typed_literal(word, &value, at)); - } - Ok(match word { - "DATE" => literal(Literal::Date(value)), - "TIME" => literal(Literal::Time(value)), - "TIMESTAMP" => literal(Literal::Timestamp(value)), - "X" => literal(Literal::HexString(value)), - _ => Expression::Cast(Box::new(cast( - string(&value), + let text: Literal = self.one_string()?; + if !is_typed_text(word, text.value_str()) { + return Err(self.invalid_typed_literal(word, text.value_str(), at)); + } + Ok(match (word, text) { + ("DATE", Literal::String(value)) => literal(Literal::Date(value)), + ("TIME", Literal::String(value)) => literal(Literal::Time(value)), + ("TIMESTAMP", Literal::String(value)) => literal(Literal::Timestamp(value)), + ("X", Literal::String(value)) => literal(Literal::HexString(value)), + (_, text) => Expression::Cast(Box::new(cast( + literal(text), DataType::Custom { name: word.to_owned(), }, diff --git a/crates/sqlquack-syntax-databricks/src/parse/tests/test_lineage_trees.rs b/crates/sqlquack-syntax-databricks/src/parse/tests/test_lineage_trees.rs index 97895ed77..aed7452f5 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/tests/test_lineage_trees.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/tests/test_lineage_trees.rs @@ -243,6 +243,12 @@ fn given_sql_when_parsing_then_the_tree_keeps_what_lineage_reads() { pointer: "/0/cache/options_without_equals", expected_json: "[0]", }, + ValueTestCase { + description: "a credential value written as a number is a bare value, not a written spelling", + sql: "COPY INTO t1 FROM 's3://b/p' WITH ( CREDENTIAL ( k = 1 ) ) FILEFORMAT = CSV", + pointer: "/0/copy/credentials", + expected_json: r#"{"bare_values":[0],"credentials":[["k","1"]],"encryption":null,"storage":null}"#, + }, ValueTestCase { description: "a table property written without = keeps its position", sql: "ALTER TABLE t1 SET TBLPROPERTIES ( 'k1' = 1 , 'k2' 2 )", 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 61240233c..9e667f9c9 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 @@ -855,6 +855,38 @@ fn given_one_form_written_in_different_ways_when_parsing_then_each_tree_keeps_wh ], expected_distinct_trees: 6, }, + DistinctTreesTestCase { + description: "a credential value as a string, a raw string and adjacent strings", + written: &[ + "COPY INTO t1 FROM 's3://b/p' WITH ( CREDENTIAL ( k = '1x' ) ) FILEFORMAT = CSV", + "COPY INTO t1 FROM 's3://b/p' WITH ( CREDENTIAL ( k = r'1x' ) ) FILEFORMAT = CSV", + "COPY INTO t1 FROM 's3://b/p' WITH ( CREDENTIAL ( k = '1' 'x' ) ) FILEFORMAT = CSV", + ], + expected_distinct_trees: 3, + }, + DistinctTreesTestCase { + description: "a typed literal of a raw string apart from a plain one and from a cast", + written: &[ + "SELECT DATE '2020-01-01'", + "SELECT DATE r'2020-01-01'", + "SELECT CAST ( r'2020-01-01' AS DATE )", + "SELECT TIME '10:00:00'", + "SELECT TIME r'10:00:00'", + "SELECT TIMESTAMP '2020-01-01'", + "SELECT TIMESTAMP r'2020-01-01'", + "SELECT TIMESTAMP_NTZ '2020-01-01'", + "SELECT TIMESTAMP_NTZ r'2020-01-01'", + "SELECT X'AB'", + "SELECT X r'AB'", + "SELECT INTERVAL '1' DAY", + "SELECT INTERVAL r'1' DAY", + "SELECT INTERVAL - '1' DAY", + "SELECT INTERVAL - r'1' DAY", + "SELECT INTERVAL '1 day'", + "SELECT INTERVAL r'1 day'", + ], + expected_distinct_trees: 17, + }, DistinctTreesTestCase { description: "adjacent strings apart from the one string they join to", written: &[ @@ -868,6 +900,68 @@ fn given_one_form_written_in_different_ways_when_parsing_then_each_tree_keeps_wh ], expected_distinct_trees: 7, }, + DistinctTreesTestCase { + description: "a comment written as adjacent strings, as a raw string and as one string", + written: &[ + "CREATE TABLE t1 ( c1 INT COMMENT 'a' 'b' )", + "CREATE TABLE t1 ( c1 INT COMMENT r'ab' )", + "CREATE TABLE t1 ( c1 INT COMMENT 'ab' )", + "CREATE VIEW v1 COMMENT 'a' 'b' AS SELECT 1", + "CREATE VIEW v1 COMMENT 'ab' AS SELECT 1", + "CREATE VIEW v1 ( c1 COMMENT 'a' 'b' ) AS SELECT 1", + "CREATE VIEW v1 ( c1 COMMENT 'ab' ) AS SELECT 1", + "SELECT CAST ( NULL AS STRUCT < c1 : INT COMMENT 'a' 'b' > )", + "SELECT CAST ( NULL AS STRUCT < c1 : INT COMMENT 'ab' > )", + "ALTER TABLE t1 ADD COLUMN c1 INT COMMENT 'a' 'b'", + "ALTER TABLE t1 ADD COLUMN c1 INT COMMENT 'ab'", + "ALTER TABLE t1 ALTER COLUMN c1 COMMENT 'a' 'b'", + "ALTER TABLE t1 ALTER COLUMN c1 COMMENT 'ab'", + "ALTER TABLE t1 CHANGE COLUMN c1 c1 INT COMMENT 'a' 'b'", + "ALTER TABLE t1 CHANGE COLUMN c1 c1 INT COMMENT 'ab'", + "CREATE FUNCTION f1 ( p1 INT COMMENT 'a' 'b' ) RETURNS INT RETURN p1", + "CREATE FUNCTION f1 ( p1 INT COMMENT r'ab' ) RETURNS INT RETURN p1", + "CREATE FUNCTION f1 ( p1 INT COMMENT 'ab' ) RETURNS INT RETURN p1", + "CREATE FUNCTION f1 ( p1 INT COMMENT 'b' ) RETURNS INT RETURN p1", + "CREATE FUNCTION f1 ( p1 INT ) RETURNS INT RETURN p1", + ], + expected_distinct_trees: 20, + }, + DistinctTreesTestCase { + description: "a path, class, URI or state written as adjacent strings and as one string", + written: &[ + "ALTER TABLE t1 SET LOCATION '/a' 'b'", + "ALTER TABLE t1 SET LOCATION '/ab'", + "ALTER TABLE t1 PARTITION ( c1 = 1 ) SET LOCATION '/a' 'b'", + "ALTER TABLE t1 PARTITION ( c1 = 1 ) SET LOCATION '/ab'", + "INSERT OVERWRITE DIRECTORY '/a' 'b' STORED AS ORC SELECT 1", + "INSERT OVERWRITE DIRECTORY '/ab' STORED AS ORC SELECT 1", + "INSERT OVERWRITE DIRECTORY '/p' STORED AS INPUTFORMAT 'a' 'b' OUTPUTFORMAT 'c' SELECT 1", + "INSERT OVERWRITE DIRECTORY '/p' STORED AS INPUTFORMAT 'ab' OUTPUTFORMAT 'c' SELECT 1", + "INSERT OVERWRITE DIRECTORY '/p' STORED AS INPUTFORMAT 'c' OUTPUTFORMAT 'a' 'b' SELECT 1", + "INSERT OVERWRITE DIRECTORY '/p' STORED AS INPUTFORMAT 'c' OUTPUTFORMAT 'ab' SELECT 1", + "LOAD DATA INPATH '/a' 'b' INTO TABLE t1", + "LOAD DATA INPATH '/ab' INTO TABLE t1", + "CREATE FUNCTION f1 AS 'a' 'b'", + "CREATE FUNCTION f1 AS 'ab'", + "CREATE FUNCTION f1 AS 'c' USING JAR '/a' 'b'", + "CREATE FUNCTION f1 AS 'c' USING JAR '/ab'", + "CREATE FUNCTION f1 ( ) RETURNS INT LANGUAGE PYTHON HANDLER 'a' 'b' AS $$ x $$", + "CREATE FUNCTION f1 ( ) RETURNS INT LANGUAGE PYTHON HANDLER 'ab' AS $$ x $$", + "BEGIN DECLARE c1 CONDITION FOR SQLSTATE '450' '00' ; END", + "BEGIN DECLARE c1 CONDITION FOR SQLSTATE '45000' ; END", + "BEGIN SIGNAL SQLSTATE '450' '00' ; END", + "BEGIN SIGNAL SQLSTATE '45000' ; END", + "USE CATALOG 'a' 'b'", + "USE CATALOG 'ab'", + "SHOW TBLPROPERTIES t1 ( 'a' 'b' )", + "SHOW TBLPROPERTIES t1 ( 'ab' )", + "CREATE TABLE t1 ( c1 INT ) USING DELTA OPTIONS ( 'a' 'b' = 1 )", + "CREATE TABLE t1 ( c1 INT ) USING DELTA OPTIONS ( 'ab' = 1 )", + "CREATE TABLE t1 ( c1 INT ) USING DELTA OPTIONS ( r'k' 'v' )", + "CREATE TABLE t1 ( c1 INT ) USING DELTA OPTIONS ( 'k' 'v' )", + ], + expected_distinct_trees: 30, + }, ]; for test_case in test_cases { assert_eq!( diff --git a/crates/sqlquack-syntax-duckdb/src/parse/_helpers/commands/alters.rs b/crates/sqlquack-syntax-duckdb/src/parse/_helpers/commands/alters.rs index 1587e2bf2..8092a784d 100644 --- a/crates/sqlquack-syntax-duckdb/src/parse/_helpers/commands/alters.rs +++ b/crates/sqlquack-syntax-duckdb/src/parse/_helpers/commands/alters.rs @@ -153,6 +153,7 @@ impl Parser<'_> { let action: AlterColumnAction = self.alter_column_entry(start)?; return Ok(match nested(path) { Ok(name) => TableAction::Tree(Box::new(AlterTableAction::AlterColumn { + comment_written: None, name, action, use_modify_keyword: false, diff --git a/crates/sqlquack-syntax-duckdb/src/parse/_helpers/commands/sessions.rs b/crates/sqlquack-syntax-duckdb/src/parse/_helpers/commands/sessions.rs index 396d694a4..1535bf4cf 100644 --- a/crates/sqlquack-syntax-duckdb/src/parse/_helpers/commands/sessions.rs +++ b/crates/sqlquack-syntax-duckdb/src/parse/_helpers/commands/sessions.rs @@ -120,6 +120,7 @@ fn use_node(parts: Vec) -> Expression { _ => parts, }; Expression::Use(Box::new(Use { + name_written: None, kind: None, this, roles: Vec::new(), diff --git a/crates/sqlquack-syntax-duckdb/src/parse/_helpers/commands/statements.rs b/crates/sqlquack-syntax-duckdb/src/parse/_helpers/commands/statements.rs index 0c2567fc7..bb7c82ece 100644 --- a/crates/sqlquack-syntax-duckdb/src/parse/_helpers/commands/statements.rs +++ b/crates/sqlquack-syntax-duckdb/src/parse/_helpers/commands/statements.rs @@ -157,6 +157,8 @@ impl Parser<'_> { false => None, }; Ok(FunctionParameter { + comment: None, + comment_written: None, name: Some(name), data_type, mode: None, diff --git a/crates/sqlquack-syntax-postgres/src/parse/_helpers/commands/routines.rs b/crates/sqlquack-syntax-postgres/src/parse/_helpers/commands/routines.rs index 83a3597ba..b00466a27 100644 --- a/crates/sqlquack-syntax-postgres/src/parse/_helpers/commands/routines.rs +++ b/crates/sqlquack-syntax-postgres/src/parse/_helpers/commands/routines.rs @@ -289,6 +289,8 @@ impl Parser<'_> { false => None, }; Ok(FunctionParameter { + comment: None, + comment_written: None, name, data_type, mode: mode.as_deref().map(|mode| match mode { 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 3c6f23dfe..c8f41a259 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/alter.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/alter.rs @@ -27,8 +27,6 @@ use sqlquack_syntax::expressions::statements::models::TruncateTableEntry; use sqlquack_syntax::expressions::statements::models::TruncateTarget; use sqlquack_syntax::expressions::statements::models::TryCatch; use sqlquack_syntax::expressions::statements::models::Uncache; -use sqlquack_syntax::expressions::statements::models::Use; -use sqlquack_syntax::expressions::statements::models::UseKind; use sqlquack_syntax::expressions::statements::models::Var; use sqlquack_syntax::expressions::tables::models::DataDeletionProperty; use sqlquack_syntax::expressions::tables::models::WithSystemVersioningProperty; @@ -195,6 +193,7 @@ impl Parser { self.match_text_seq(&["RESTRICT"]); } Ok(AlterTableAction::ChangeColumn { + comment_written: None, old_name, new_name, data_type, @@ -338,6 +337,7 @@ impl Parser { let action_words = self.written_alter_column_words(); let action = self.parse_alter_column_action()?; Ok(AlterTableAction::AlterColumn { + comment_written: None, name, action, use_modify_keyword: false, @@ -510,7 +510,10 @@ impl Parser { self.parse_alter_set_table_properties() } else if self.match_text_seq(&["LOCATION"]) { let location = self.expect_string()?; - Ok(AlterTableAction::SetLocation { location }) + Ok(AlterTableAction::SetLocation { + location_written: None, + location, + }) } else if self.match_text_seq(&["FILE", "FORMAT"]) { let format = self.expect_identifier_or_keyword()?; Ok(AlterTableAction::SetFileFormat { format }) @@ -1223,6 +1226,7 @@ impl Parser { let typed = self.parse_simple_name().and_then(|name| { let action_words = self.written_alter_column_words(); Ok(AlterTableAction::AlterColumn { + comment_written: None, name, action: self.parse_alter_column_action()?, use_modify_keyword: true, @@ -1261,6 +1265,7 @@ impl Parser { let action_words = self.written_alter_column_words(); let action = self.parse_alter_column_action()?; actions.push(AlterTableAction::AlterColumn { + comment_written: None, name, action, use_modify_keyword: modify, @@ -1587,130 +1592,6 @@ impl Parser { } } - /// Parse USE statement: USE db, USE DATABASE x, USE SCHEMA x.y, USE ROLE x, etc. - pub(crate) fn parse_use(&mut self) -> Result { - self.expect_token(TokenType::Use)?; - - if self.check(TokenType::Var) && self.peek_text().eq_ignore_ascii_case("SECONDARY") { - self.skip(); - if self.check(TokenType::Var) && self.peek_text().eq_ignore_ascii_case("ROLES") { - self.skip(); - let snowflake = self.is_snowflake(); - let mut roles: Vec = Vec::new(); - let mut listed: Vec = Vec::new(); - loop { - if snowflake && self.check(TokenType::QuotedIdentifier) { - listed.push(Identifier::quoted(self.peek_text())); - roles.push(self.parse_quoted_role()?); - if !self.match_token(TokenType::Comma) { - break; - } - } else if self.check(TokenType::Var) - || self.check(TokenType::All) - || self.check(TokenType::Identifier) - { - let role = self.advance_text()?; - listed.push(Identifier::new(role.clone())); - roles.push(role); - if !self.match_token(TokenType::Comma) { - break; - } - } else if snowflake && !roles.is_empty() { - return Err(self.parse_error("Expected a role after the comma")); - } else { - break; - } - } - if snowflake && roles.is_empty() { - return Err(self.parse_error("Expected ALL, NONE or a role after ROLES")); - } - let keyword = match roles.as_slice() { - [role] if role.eq_ignore_ascii_case("ALL") => Some(UseKind::SecondaryRolesAll), - [role] if role.eq_ignore_ascii_case("NONE") => { - Some(UseKind::SecondaryRolesNone) - } - _ => None, - }; - let first = listed.first().cloned(); - let (kind, this, roles) = match (snowflake, keyword, first) { - (true, Some(UseKind::SecondaryRolesAll), _) => ( - UseKind::SecondaryRolesAll, - Identifier::new("ALL"), - Vec::new(), - ), - (true, Some(_), _) => ( - UseKind::SecondaryRolesNone, - Identifier::new("NONE"), - Vec::new(), - ), - (true, None, Some(first)) => (UseKind::SecondaryRoles, first, listed), - _ => { - let name = match roles.is_empty() { - true => "ALL".to_string(), - false => roles.join(", "), - }; - (UseKind::SecondaryRoles, Identifier::new(name), Vec::new()) - } - }; - return Ok(Expression::Use(Box::new(Use { - kind: Some(kind), - this, - roles, - parts: Vec::new(), - string_name: false, - keyword: None, - }))); - } - } - - let kind_is_name = self.applies_snowflake_context_rules() - && matches!( - self.tokens - .get(self.current + 1) - .map(|token| token.token_type), - None | Some(TokenType::Semicolon | TokenType::Dot) - ); - let kind = if kind_is_name { - None - } else if self.match_token(TokenType::Database) { - Some(UseKind::Database) - } else if self.match_token(TokenType::Schema) { - Some(UseKind::Schema) - } else if self.match_token(TokenType::Warehouse) { - Some(UseKind::Warehouse) - } else if self.check(TokenType::Var) && self.peek_text().eq_ignore_ascii_case("ROLE") { - self.skip(); - Some(UseKind::Role) - } else if self.check(TokenType::Var) && self.peek_text().eq_ignore_ascii_case("CATALOG") { - self.skip(); - Some(UseKind::Catalog) - } else { - None - }; - - let mut ident = self.expect_identifier_or_keyword_with_quoted()?; - self.reject_reserved_name_part(&ident)?; - let mut parts: Vec = Vec::new(); - - if self.match_token(TokenType::Dot) { - let second_part = self.expect_identifier_or_keyword_with_quoted()?; - self.reject_reserved_name_part(&second_part)?; - if self.applies_snowflake_context_rules() { - parts = vec![ident.clone(), second_part.clone()]; - } - ident.name = format!("{}.{}", ident.name, second_part.name); - } - - Ok(Expression::Use(Box::new(Use { - kind, - this: ident, - roles: Vec::new(), - parts, - string_name: false, - keyword: None, - }))) - } - /// Parses BigQuery EXPORT DATA [WITH CONNECTION c] OPTIONS (...) AS SELECT ... pub(crate) fn parse_export_data(&mut self) -> Result { self.skip(); @@ -1890,6 +1771,7 @@ impl Parser { }; Ok(Expression::LoadData(Box::new(LoadData { + inpath_written: None, local, inpath, overwrite, diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/alter_table.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/alter_table.rs index 88613151c..ef4b21b0f 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/alter_table.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/alter_table.rs @@ -74,6 +74,7 @@ impl Parser { let action_words = self.written_alter_column_words(); let action = self.parse_alter_column_action()?; actions.push(AlterTableAction::AlterColumn { + comment_written: None, name, action, use_modify_keyword: false, diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/constraints.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/constraints.rs index 9920b47df..be76238eb 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/constraints.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/constraints.rs @@ -910,6 +910,7 @@ impl Parser { let has_as = self.match_view_as()?; if !has_as && !self.check(TokenType::Select) && !self.check(TokenType::With) { return Ok(Expression::CreateView(Box::new(CreateView { + comment_written: None, name, columns, query: Expression::Null(Null), @@ -975,6 +976,7 @@ impl Parser { let no_schema_binding = self.match_text_seq(&["WITH", "NO", "SCHEMA", "BINDING"]); Ok(Expression::CreateView(Box::new(CreateView { + comment_written: None, name, columns, query, @@ -1151,6 +1153,7 @@ impl Parser { None }; cols.push(ViewColumn { + comment_written: None, name: Identifier::new(col_name), comment, options, 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 83abb62f0..e18071ae3 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 @@ -62,6 +62,8 @@ impl Parser { let return_nullability = self.match_snowflake_return_nullability(); let mut function = CreateFunction { + body_written: None, + handler_written: None, name, parameters, return_type, @@ -398,6 +400,7 @@ impl Parser { let kind = self.previous_text().to_ascii_uppercase(); let uri = self.expect_string()?; function.using_resources.push(FunctionUsingResource { + uri_written: None, kind, uri, kind_quoted: false, 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 74edab71f..77f945ea9 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/scripting.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/scripting.rs @@ -924,6 +924,7 @@ impl Parser { string_keys: Vec::new(), bare_values: Vec::new(), written_keys: Vec::new(), + written_values: Vec::new(), }; credentials = Some(Box::new(creds)); } @@ -962,6 +963,7 @@ impl Parser { string_keys: Vec::new(), bare_values: Vec::new(), written_keys: Vec::new(), + written_values: Vec::new(), }; credentials = Some(Box::new(creds)); } diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/snowflake_create_properties.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/snowflake_create_properties.rs index a8149488d..6ee5716b9 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/snowflake_create_properties.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/snowflake_create_properties.rs @@ -279,6 +279,8 @@ impl Parser { false => Some(first), }; signature.push(FunctionParameter { + comment: None, + comment_written: None, name, data_type: self.parse_data_type()?, mode: None, diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/operators.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/operators.rs index 149cd6530..1b435dd4e 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/operators.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/expressions/operators.rs @@ -170,6 +170,8 @@ impl Parser { }; params.push(FunctionParameter { + comment: None, + comment_written: None, name, data_type, mode, @@ -225,6 +227,8 @@ impl Parser { None }; params.push(FunctionParameter { + comment: None, + comment_written: None, name: Some(Identifier::new(name)), data_type, mode: None, @@ -1578,6 +1582,7 @@ impl Parser { None }; cols.push(ViewColumn { + comment_written: None, name: Identifier::new(col_name), comment, options: Vec::new(), 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 3e6cb3d5c..55c35b7be 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/insert.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/queries/insert.rs @@ -357,6 +357,9 @@ impl Parser { overwrite, partition: Vec::new(), directory: Some(DirectoryInsert { + input_format_written: None, + output_format_written: None, + path_written: None, local: local_directory, path, row_format, diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/mod.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/mod.rs index 911064528..19d8b0841 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/mod.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/mod.rs @@ -7,4 +7,5 @@ pub(crate) mod procedures; pub(crate) mod scripting; pub(crate) mod sessions; pub(crate) mod snowflake_context; +pub(crate) mod use_statement; pub(crate) mod with_statement; diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/procedures.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/procedures.rs index 94e792a19..1489e89a2 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/procedures.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/procedures.rs @@ -102,6 +102,8 @@ impl Parser { None }; parameters.push(FunctionParameter { + comment: None, + comment_written: None, name: Some(name), data_type, mode: None, diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/use_statement.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/use_statement.rs new file mode 100644 index 000000000..5e9ed872f --- /dev/null +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/statements/use_statement.rs @@ -0,0 +1,137 @@ +//! Parser statements: USE. + +use crate::parser::models::Parser; +use sqlquack_syntax::expressions::names::models::Identifier; +use sqlquack_syntax::expressions::statements::models::Use; +use sqlquack_syntax::expressions::statements::models::UseKind; +use sqlquack_syntax::expressions::tree::models::Expression; +use sqlquack_syntax::tokens::models::TokenType; +use sqlquack_syntax::tokens::types::Result; + +impl Parser { + /// Parse USE statement: USE db, USE DATABASE x, USE SCHEMA x.y, USE ROLE x, etc. + pub(crate) fn parse_use(&mut self) -> Result { + self.expect_token(TokenType::Use)?; + + if self.check(TokenType::Var) && self.peek_text().eq_ignore_ascii_case("SECONDARY") { + self.skip(); + if self.check(TokenType::Var) && self.peek_text().eq_ignore_ascii_case("ROLES") { + self.skip(); + let snowflake = self.is_snowflake(); + let mut roles: Vec = Vec::new(); + let mut listed: Vec = Vec::new(); + loop { + if snowflake && self.check(TokenType::QuotedIdentifier) { + listed.push(Identifier::quoted(self.peek_text())); + roles.push(self.parse_quoted_role()?); + if !self.match_token(TokenType::Comma) { + break; + } + } else if self.check(TokenType::Var) + || self.check(TokenType::All) + || self.check(TokenType::Identifier) + { + let role = self.advance_text()?; + listed.push(Identifier::new(role.clone())); + roles.push(role); + if !self.match_token(TokenType::Comma) { + break; + } + } else if snowflake && !roles.is_empty() { + return Err(self.parse_error("Expected a role after the comma")); + } else { + break; + } + } + if snowflake && roles.is_empty() { + return Err(self.parse_error("Expected ALL, NONE or a role after ROLES")); + } + let keyword = match roles.as_slice() { + [role] if role.eq_ignore_ascii_case("ALL") => Some(UseKind::SecondaryRolesAll), + [role] if role.eq_ignore_ascii_case("NONE") => { + Some(UseKind::SecondaryRolesNone) + } + _ => None, + }; + let first = listed.first().cloned(); + let (kind, this, roles) = match (snowflake, keyword, first) { + (true, Some(UseKind::SecondaryRolesAll), _) => ( + UseKind::SecondaryRolesAll, + Identifier::new("ALL"), + Vec::new(), + ), + (true, Some(_), _) => ( + UseKind::SecondaryRolesNone, + Identifier::new("NONE"), + Vec::new(), + ), + (true, None, Some(first)) => (UseKind::SecondaryRoles, first, listed), + _ => { + let name = match roles.is_empty() { + true => "ALL".to_string(), + false => roles.join(", "), + }; + (UseKind::SecondaryRoles, Identifier::new(name), Vec::new()) + } + }; + return Ok(Expression::Use(Box::new(Use { + name_written: None, + kind: Some(kind), + this, + roles, + parts: Vec::new(), + string_name: false, + keyword: None, + }))); + } + } + + let kind_is_name = self.applies_snowflake_context_rules() + && matches!( + self.tokens + .get(self.current + 1) + .map(|token| token.token_type), + None | Some(TokenType::Semicolon | TokenType::Dot) + ); + let kind = if kind_is_name { + None + } else if self.match_token(TokenType::Database) { + Some(UseKind::Database) + } else if self.match_token(TokenType::Schema) { + Some(UseKind::Schema) + } else if self.match_token(TokenType::Warehouse) { + Some(UseKind::Warehouse) + } else if self.check(TokenType::Var) && self.peek_text().eq_ignore_ascii_case("ROLE") { + self.skip(); + Some(UseKind::Role) + } else if self.check(TokenType::Var) && self.peek_text().eq_ignore_ascii_case("CATALOG") { + self.skip(); + Some(UseKind::Catalog) + } else { + None + }; + + let mut ident = self.expect_identifier_or_keyword_with_quoted()?; + self.reject_reserved_name_part(&ident)?; + let mut parts: Vec = Vec::new(); + + if self.match_token(TokenType::Dot) { + let second_part = self.expect_identifier_or_keyword_with_quoted()?; + self.reject_reserved_name_part(&second_part)?; + if self.applies_snowflake_context_rules() { + parts = vec![ident.clone(), second_part.clone()]; + } + ident.name = format!("{}.{}", ident.name, second_part.name); + } + + Ok(Expression::Use(Box::new(Use { + name_written: None, + kind, + this: ident, + roles: Vec::new(), + parts, + string_name: false, + keyword: None, + }))) + } +} diff --git a/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/ddl.rs b/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/ddl.rs index a297f09fb..aaa9e2267 100644 --- a/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/ddl.rs +++ b/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/ddl.rs @@ -127,6 +127,8 @@ impl<'sql> Parser<'sql> { return Err(self.gap("procedure_parameter_option", self.offset())); } Ok(FunctionParameter { + comment: None, + comment_written: None, name: Some(identifier(&token)), data_type, mode: mode_text.as_ref().map(|_| ParameterMode::Out), @@ -378,6 +380,8 @@ impl<'sql> Parser<'sql> { return Err(parser.gap("function_parameter_option", parser.offset())); } Ok(FunctionParameter { + comment: None, + comment_written: None, name: Some(identifier(&token)), data_type, mode: None, @@ -625,6 +629,7 @@ impl<'sql> Parser<'sql> { return Err(self.gap("alter_column_option", start)); } let mut actions: Vec = vec![AlterTableAction::AlterColumn { + comment_written: None, name: name.clone(), action: AlterColumnAction::SetDataType { data_type, @@ -646,6 +651,7 @@ impl<'sql> Parser<'sql> { }; if let Some(action) = nullability { actions.push(AlterTableAction::AlterColumn { + comment_written: None, name, action, use_modify_keyword: false, diff --git a/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/entry.rs b/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/entry.rs index da7820753..7bef49348 100644 --- a/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/entry.rs +++ b/crates/sqlquack-syntax-sqlserver/src/parse/_helpers/statements/entry.rs @@ -205,6 +205,7 @@ impl<'sql> Parser<'sql> { .expect_token(is_any_name) .map(|token| identifier(&token))?; Ok(Expression::Use(Box::new(Use { + name_written: None, kind: None, this: name, roles: Vec::new(), diff --git a/crates/sqlquack-syntax/src/expressions/columns/models.rs b/crates/sqlquack-syntax/src/expressions/columns/models.rs index 2f017fa57..50700a50e 100644 --- a/crates/sqlquack-syntax/src/expressions/columns/models.rs +++ b/crates/sqlquack-syntax/src/expressions/columns/models.rs @@ -47,6 +47,9 @@ pub struct ColumnDef { #[serde(default, skip_serializing_if = "Option::is_none")] pub auto_random: Option, pub comment: Option, + /// Databricks: the comment as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + pub comment_written: Option, pub constraints: Vec, /// Track original order of constraints for accurate regeneration #[serde(default)] @@ -143,6 +146,7 @@ pub struct ColumnDef { impl ColumnDef { pub fn new(name: impl Into, data_type: DataType) -> Self { Self { + comment_written: None, name: Identifier::new(name), data_type, nullable: None, @@ -726,6 +730,9 @@ pub enum AlterTableAction { AlterColumn { name: Identifier, action: AlterColumnAction, + /// Databricks: the comment a `COMMENT` action sets as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + comment_written: Option, /// Whether this was parsed from MODIFY COLUMN syntax (MySQL) #[serde(default)] use_modify_keyword: bool, @@ -861,6 +868,9 @@ pub enum AlterTableAction { #[serde(default, skip_serializing_if = "Option::is_none")] data_type: Option, comment: Option, + /// Databricks: the comment as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + comment_written: Option, #[serde(default)] cascade: bool, }, @@ -896,6 +906,9 @@ pub enum AlterTableAction { /// Redshift: ALTER TABLE t SET LOCATION 's3://bucket/folder/' SetLocation { location: String, + /// Databricks: the location as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + location_written: Option, }, /// Redshift: ALTER TABLE t SET FILE FORMAT AVRO SetFileFormat { diff --git a/crates/sqlquack-syntax/src/expressions/schema_objects/models.rs b/crates/sqlquack-syntax/src/expressions/schema_objects/models.rs index 2562adff2..673952f69 100644 --- a/crates/sqlquack-syntax/src/expressions/schema_objects/models.rs +++ b/crates/sqlquack-syntax/src/expressions/schema_objects/models.rs @@ -1,5 +1,6 @@ //! Data models of expressions schema objects. +use crate::expressions::names::models::Literal; use serde::Deserialize; use serde::Serialize; @@ -24,6 +25,9 @@ use crate::tokens::models::SourceSpan; pub struct ViewColumn { pub name: Identifier, pub comment: Option, + /// Databricks: the comment as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + pub comment_written: Option, /// BigQuery: OPTIONS (key=value, ...) on column #[serde(default, skip_serializing_if = "Vec::is_empty")] pub options: Vec, @@ -32,6 +36,7 @@ pub struct ViewColumn { impl ViewColumn { pub fn new(name: impl Into) -> Self { Self { + comment_written: None, name: Identifier::new(name), comment: None, options: Vec::new(), @@ -40,6 +45,7 @@ impl ViewColumn { pub fn with_comment(name: impl Into, comment: impl Into) -> Self { Self { + comment_written: None, name: Identifier::new(name), comment: Some(comment.into()), options: Vec::new(), @@ -93,6 +99,9 @@ pub struct CreateView { /// Snowflake: COMMENT = 'text' #[serde(skip_serializing_if = "Option::is_none", default)] pub comment: Option, + /// Databricks: the comment as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + pub comment_written: Option, /// Snowflake: WITH ROW ACCESS POLICY ... clause #[serde(skip_serializing_if = "Option::is_none", default)] pub row_access_policy: Option, @@ -146,6 +155,7 @@ pub struct CreateView { impl CreateView { pub fn new(name: impl Into, query: Expression) -> Self { Self { + comment_written: None, name: TableRef::new(name), columns: Vec::new(), query, @@ -512,6 +522,9 @@ pub struct CreateFunction { pub parameters: Vec, pub return_type: Option, pub body: Option, + /// Databricks: the string body as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + pub body_written: Option, pub or_replace: bool, /// TSQL: CREATE OR ALTER #[serde(default, skip_serializing_if = "std::ops::Not::not")] @@ -567,6 +580,9 @@ pub struct CreateFunction { /// HANDLER 'handler_function' clause (Databricks) #[serde(default, skip_serializing_if = "Option::is_none")] pub handler: Option, + /// Databricks: the handler as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + pub handler_written: Option, /// True when the HANDLER clause used Snowflake-style `HANDLER = 'fn'` #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub handler_uses_eq: bool, @@ -669,6 +685,9 @@ pub enum FunctionPropertyKind { pub struct FunctionUsingResource { pub kind: String, pub uri: String, + /// Databricks: the resource URI as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + pub uri_written: Option, /// 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, @@ -684,6 +703,12 @@ pub struct FunctionParameter { /// Original text of the mode keyword for case-preserving output (e.g., "inout", "VARIADIC") #[serde(default, skip_serializing_if = "Option::is_none")] pub mode_text: Option, + /// Databricks: the parameter's COMMENT + #[serde(default, skip_serializing_if = "Option::is_none")] + pub comment: Option, + /// Databricks: the comment as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + pub comment_written: Option, /// SQLQuack: the node's source range, recorded by a parser that sets it. #[serde(default, skip_serializing_if = "SourceSpan::is_absent")] #[ast(skip)] @@ -735,6 +760,8 @@ pub enum FunctionSecurity { impl CreateFunction { pub fn new(name: impl Into) -> Self { Self { + body_written: None, + handler_written: None, name: TableRef::new(name), parameters: Vec::new(), return_type: None, diff --git a/crates/sqlquack-syntax/src/expressions/scripting/models.rs b/crates/sqlquack-syntax/src/expressions/scripting/models.rs index 3d2bb238a..aec50789a 100644 --- a/crates/sqlquack-syntax/src/expressions/scripting/models.rs +++ b/crates/sqlquack-syntax/src/expressions/scripting/models.rs @@ -1,5 +1,6 @@ //! Data models of Snowflake Scripting and Databricks SQL scripting blocks. +use crate::expressions::names::models::Literal; use crate::tokens::models::SourceSpan; use serde::Deserialize; use serde::Serialize; @@ -473,6 +474,9 @@ pub struct ScriptingCondition { #[derive(sqlquack_derive::AstNode, Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct ScriptingSqlstate { pub state: String, + /// Databricks: the state as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + pub state_written: Option, /// VALUE written before the state. #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub value_keyword: bool, diff --git a/crates/sqlquack-syntax/src/expressions/statements/models.rs b/crates/sqlquack-syntax/src/expressions/statements/models.rs index 3171a2051..ddcbef7b0 100644 --- a/crates/sqlquack-syntax/src/expressions/statements/models.rs +++ b/crates/sqlquack-syntax/src/expressions/statements/models.rs @@ -31,6 +31,9 @@ pub struct DirectoryInsert { /// The directory; empty when only Databricks' `OPTIONS ( 'path' ... )` names it #[serde(default, skip_serializing_if = "String::is_empty")] pub path: String, + /// Databricks: the path as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + pub path_written: Option, pub row_format: Option, /// STORED AS clause (e.g., TEXTFILE, ORC, PARQUET) #[serde(default)] @@ -43,10 +46,16 @@ pub struct DirectoryInsert { #[serde(default, skip_serializing_if = "Option::is_none")] #[ast(skip)] pub input_format: Option, + /// Databricks: the `INPUTFORMAT` class as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + pub input_format_written: Option, /// Hive: `STORED AS ... OUTPUTFORMAT` class #[serde(default, skip_serializing_if = "Option::is_none")] #[ast(skip)] pub output_format: Option, + /// Databricks: the `OUTPUTFORMAT` class as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + pub output_format_written: Option, /// Databricks: `USING format` #[serde(default, skip_serializing_if = "Option::is_none")] #[ast(skip)] @@ -364,6 +373,9 @@ pub struct Credentials { /// 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)>, + /// Databricks: the credential values written as a raw string or as adjacent strings, by position + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub written_values: Vec<(usize, Literal)>, } /// Historical data / Time travel (Snowflake) - BEFORE (STATEMENT => ...) or AT (TIMESTAMP => ...) @@ -606,6 +618,9 @@ pub struct Use { /// Databricks: whether the name was written as a string literal #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub string_name: bool, + /// Databricks: the string name as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + pub name_written: Option, /// Databricks: NAMESPACE written for DATABASE, or SET for `SET CATALOG` instead of `USE CATALOG` #[serde(default, skip_serializing_if = "Option::is_none")] #[ast(skip)] @@ -709,6 +724,9 @@ pub struct LoadData { pub local: bool, /// The path to load data from (INPATH value) pub inpath: String, + /// Databricks: the path as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + pub inpath_written: Option, /// Whether to overwrite existing data pub overwrite: bool, /// The target table diff --git a/crates/sqlquack-syntax/src/expressions/tables/models.rs b/crates/sqlquack-syntax/src/expressions/tables/models.rs index 8e62b3c3a..a0aa11e01 100644 --- a/crates/sqlquack-syntax/src/expressions/tables/models.rs +++ b/crates/sqlquack-syntax/src/expressions/tables/models.rs @@ -1,5 +1,6 @@ //! Data models of expressions tables. +use crate::expressions::names::models::Literal; use serde::Deserialize; use serde::Serialize; @@ -1388,6 +1389,9 @@ pub struct OptionsProperty { #[serde(default, skip_serializing_if = "Vec::is_empty")] #[ast(skip)] pub 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")] + pub written_keys: Vec<(usize, Literal)>, /// Databricks: the positions in `entries` of values written without the optional `=` #[serde(default, skip_serializing_if = "Vec::is_empty")] #[ast(skip)] diff --git a/crates/sqlquack-syntax/src/expressions/types/models.rs b/crates/sqlquack-syntax/src/expressions/types/models.rs index 39a69b7c0..e052999ed 100644 --- a/crates/sqlquack-syntax/src/expressions/types/models.rs +++ b/crates/sqlquack-syntax/src/expressions/types/models.rs @@ -1,5 +1,6 @@ //! Data models of expressions types. +use crate::expressions::names::models::Literal; use serde::Deserialize; use serde::Serialize; @@ -17,6 +18,9 @@ pub struct StructField { pub options: Vec, #[serde(default, skip_serializing_if = "Option::is_none")] pub comment: Option, + /// Databricks: the comment as written when it was a raw string or adjacent strings + #[serde(default, skip_serializing_if = "Option::is_none")] + pub comment_written: Option, /// Databricks: the COLLATE written after a string field's type #[serde(default, skip_serializing_if = "Option::is_none")] pub collation: Option>, @@ -29,6 +33,7 @@ impl StructField { /// Create a new struct field without options pub fn new(name: String, data_type: DataType) -> Self { Self { + comment_written: None, name, data_type, options: Vec::new(), @@ -41,6 +46,7 @@ impl StructField { /// Create a new struct field with options pub fn with_options(name: String, data_type: DataType, options: Vec) -> Self { Self { + comment_written: None, name, data_type, options, @@ -58,6 +64,7 @@ impl StructField { comment: Option, ) -> Self { Self { + comment_written: None, name, data_type, options,