diff --git a/crates/sqlquack-semantics/src/bigquery/profile/_helpers/typing.rs b/crates/sqlquack-semantics/src/bigquery/profile/_helpers/typing.rs index 382caff65..ca37f75f9 100644 --- a/crates/sqlquack-semantics/src/bigquery/profile/_helpers/typing.rs +++ b/crates/sqlquack-semantics/src/bigquery/profile/_helpers/typing.rs @@ -95,7 +95,9 @@ pub(crate) fn bigquery_literal_type(expression: &Expression) -> Option<&'static | Literal::TripleQuotedString(..) | Literal::RawString(_) | Literal::JoinedString(..) => Kind::String, - Literal::ByteString(_) => Kind::Bytes, + Literal::ByteString(_) | Literal::RawByteString(_) | Literal::JoinedBytes(..) => { + Kind::Bytes + } Literal::Date(_) => Kind::Date, Literal::Datetime(_) => Kind::Datetime, Literal::Time(_) => Kind::Time, 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 4d70b25f4..0a58c00ba 100644 --- a/crates/sqlquack-semantics/src/bigquery/profile/tests/test_checks.rs +++ b/crates/sqlquack-semantics/src/bigquery/profile/tests/test_checks.rs @@ -343,6 +343,50 @@ fn given_one_string_spelled_two_ways_when_grouping_then_the_expressions_match() sql: "SELECT COALESCE(status, 'ab') AS x FROM orders GROUP BY COALESCE(status, r'ab')", expected_findings: &[], }, + QueryFindingsTestCase { + description: "raw bytes in the select, plain ones in GROUP BY", + sql: "SELECT COALESCE(CAST(status AS BYTES), rb'ab') AS x FROM orders GROUP BY COALESCE(CAST(status AS BYTES), b'ab')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "adjacent bytes in the select, one piece in GROUP BY", + sql: "SELECT COALESCE(CAST(status AS BYTES), b'a' b'b') AS x FROM orders GROUP BY COALESCE(CAST(status AS BYTES), b'ab')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a raw DATE in the select, a plain one in GROUP BY", + sql: "SELECT COALESCE(CAST(status AS DATE), DATE r'2020-01-01') AS x FROM orders GROUP BY COALESCE(CAST(status AS DATE), DATE '2020-01-01')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "an adjacent DATETIME in the select, a plain one in GROUP BY", + sql: "SELECT COALESCE(CAST(status AS DATETIME), DATETIME '2020-01-01' ' 00:00:00') AS x FROM orders GROUP BY COALESCE(CAST(status AS DATETIME), DATETIME '2020-01-01 00:00:00')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a raw TIMESTAMP in the select, a plain one in GROUP BY", + sql: "SELECT COALESCE(CAST(status AS TIMESTAMP), TIMESTAMP r'2020-01-01 00:00:00') AS x FROM orders GROUP BY COALESCE(CAST(status AS TIMESTAMP), TIMESTAMP '2020-01-01 00:00:00')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a raw NUMERIC in the select, a plain one in GROUP BY", + sql: "SELECT COALESCE(CAST(status AS NUMERIC), NUMERIC r'1') AS x FROM orders GROUP BY COALESCE(CAST(status AS NUMERIC), NUMERIC '1')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "a raw bytes character in the select, its UTF-8 bytes escaped in GROUP BY", + sql: "SELECT COALESCE(CAST(status AS BYTES), rb'\u{ff}') AS x FROM orders GROUP BY COALESCE(CAST(status AS BYTES), b'\\xc3\\xbf')", + expected_findings: &[], + }, + QueryFindingsTestCase { + description: "raw bytes of another value are still a different expression", + sql: "SELECT COALESCE(CAST(status AS BYTES), rb'ac') AS x FROM orders GROUP BY COALESCE(CAST(status AS BYTES), b'ab')", + expected_findings: &[( + "E230", + "Column must be grouped or used in an aggregate function", + 21, + )], + }, 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')", diff --git a/crates/sqlquack-semantics/src/lineage/_helpers/checks/families.rs b/crates/sqlquack-semantics/src/lineage/_helpers/checks/families.rs index db67e9bbf..eca1fd599 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/families.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/families.rs @@ -242,7 +242,11 @@ fn literal_family(literal: &Literal) -> Family { Literal::Date(_) => Family::Date, Literal::Time(_) => Family::Time, Literal::Timestamp(_) | Literal::Datetime(_) => Family::Timestamp, - Literal::HexString(_) | Literal::BitString(_) | Literal::ByteString(_) => Family::Binary, + Literal::HexString(_) + | Literal::BitString(_) + | Literal::ByteString(_) + | Literal::RawByteString(_) + | Literal::JoinedBytes(..) => Family::Binary, _ => Family::String, } } 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 20ab85452..267cd2ead 100644 --- a/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs +++ b/crates/sqlquack-semantics/src/lineage/_helpers/checks/scope_rules.rs @@ -15,9 +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::{ - 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, + CAST_FIELDS, CUSTOM_TYPE_FIELDS, INFERRED_TYPE_FIELD, 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}; @@ -835,7 +835,13 @@ fn spelled_strings(expression: &Expression) -> bool { matches!( node, Expression::Literal(literal) - if matches!(literal.as_ref(), Literal::RawString(_) | Literal::JoinedString(..)) + if matches!( + literal.as_ref(), + Literal::RawString(_) + | Literal::JoinedString(..) + | Literal::RawByteString(_) + | Literal::JoinedBytes(..) + ) ) }) } @@ -847,11 +853,12 @@ fn plain_strings(value: Value) -> Value { 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 plain: Option<&str> = match map.get(LITERAL_TYPE_FIELD).and_then(Value::as_str) { + Some("raw_string" | "joined_string") => Some("string"), + Some("raw_byte_string" | "joined_bytes") => Some("byte_string"), + _ => None, + }; + if let Some(plain) = plain { let text: Option = match map.remove(LITERAL_VALUE_FIELD) { Some(Value::Array(mut parts)) if !parts.is_empty() => { Some(parts.swap_remove(0)) @@ -860,7 +867,7 @@ fn plain_strings(value: Value) -> Value { }; map.insert( LITERAL_TYPE_FIELD.to_owned(), - Value::String("string".to_owned()), + Value::String(plain.to_owned()), ); map.insert(LITERAL_VALUE_FIELD.to_owned(), text.unwrap_or(Value::Null)); } @@ -876,30 +883,39 @@ fn plain_strings(value: Value) -> Value { } } -/// The typed literal a serialized cast of a raw string to `DATE`, `TIME`, `TIMESTAMP` or `X` reads as. +/// The typed literal a Databricks or BigQuery typed literal of a raw or adjacent string 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 - { + let spelled: &str = text.get(LITERAL_TYPE_FIELD)?.as_str()?; + if map.len() != 1 || !matches!(spelled, RAW_STRING_KIND | "joined_string") { return None; } - let word: &str = to.get(NAME_FIELD)?.as_str()?; - let (_, kind) = RAW_TYPED_LITERALS.iter().find(|(name, _)| *name == word)?; + let kind: &str = match to.get(type_field)?.as_str()? { + name if name == custom && spelled == RAW_STRING_KIND => { + let word: &str = to.get(NAME_FIELD)?.as_str()?; + RAW_TYPED_LITERALS.iter().find(|(name, _)| *name == word)?.1 + } + _ if !cast.contains_key(INFERRED_TYPE_FIELD) => return None, + "date" => "date", + "time" => "time", + "timestamp" if to.get("timezone")?.as_bool()? => "timestamp", + "timestamp" => "datetime", + _ => return None, + }; + let value: Value = match text.get(LITERAL_VALUE_FIELD)? { + Value::Array(parts) => parts.first()?.clone(), + value => value.clone(), + }; 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(), + Value::String(kind.to_owned()), ); + literal.insert(LITERAL_VALUE_FIELD.to_owned(), value); let mut node: Map = Map::new(); node.insert(LITERAL_FIELD.to_owned(), Value::Object(literal)); Some(Value::Object(node)) diff --git a/crates/sqlquack-semantics/src/lineage/constants.rs b/crates/sqlquack-semantics/src/lineage/constants.rs index d0f64f659..d8e55647c 100644 --- a/crates/sqlquack-semantics/src/lineage/constants.rs +++ b/crates/sqlquack-semantics/src/lineage/constants.rs @@ -248,6 +248,8 @@ pub(crate) const CAST_FIELDS: [&str; 3] = ["cast", "this", "to"]; 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 field a serialized cast records the type a BigQuery typed literal gave itself in. +pub(crate) const INFERRED_TYPE_FIELD: &str = "inferred_type"; /// 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. 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 7f4c24523..9834f6eab 100644 --- a/crates/sqlquack-syntax-bigquery/src/parse/_helpers/commands/grants.rs +++ b/crates/sqlquack-syntax-bigquery/src/parse/_helpers/commands/grants.rs @@ -26,6 +26,7 @@ fn principal(name: Identifier, kind: Option<&str>) -> GrantPrincipal { is_group: false, is_share: false, kind: kind.map(str::to_owned), + written: None, } } @@ -139,9 +140,13 @@ impl Parser<'_> { if self.string_at(0) { return match self.string_literal()? { Expression::Literal(literal) => match *literal { - Literal::String(text) - | Literal::RawString(text) - | Literal::JoinedString(text, _) => Ok(principal(Identifier::new(text), None)), + Literal::String(text) => Ok(principal(Identifier::new(text), None)), + written @ (Literal::RawString(_) | Literal::JoinedString(..)) => { + Ok(GrantPrincipal { + written: Some(written.clone()), + ..principal(Identifier::new(written.value_str()), 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 d5040e53c..9bc436c6f 100644 --- a/crates/sqlquack-syntax-bigquery/src/parse/_helpers/values/primary.rs +++ b/crates/sqlquack-syntax-bigquery/src/parse/_helpers/values/primary.rs @@ -73,6 +73,11 @@ impl Parser<'_> { loop { let token: Token = self.advance()?; chunks.push(match token.kind { + TokenKind::String { + raw: true, + bytes: true, + } => Literal::RawByteString(token.value), + TokenKind::String { bytes: true, .. } => Literal::ByteString(token.value), TokenKind::String { raw: true, .. } => Literal::RawString(token.value), _ => Literal::String(token.value), }); @@ -109,17 +114,32 @@ impl Parser<'_> { } } - /// A string or bytes literal; one string chunk stays as written, several keep their chunks. + /// A string or bytes literal; one chunk stays as written, several keep their chunks. pub(crate) fn string_literal(&mut self) -> Parsing { 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), + (_, 1) => chunks.remove(0), + (true, _) => Literal::JoinedBytes(text, chunks), (false, _) => Literal::JoinedString(text, chunks), })) } + /// A string literal as written: one plain chunk as its text, else the literal it makes. + fn written_string(&mut self) -> Parsing> { + let start: usize = self.offset(); + let (mut chunks, bytes) = self.literal_chunks()?; + if bytes { + return Err(self.error_with("Syntax error: Unexpected bytes literal", start, true)); + } + let text: String = chunks.iter().map(Literal::value_str).collect(); + Ok(match (chunks.len(), chunks.first()) { + (1, Some(Literal::String(_))) => Ok(text), + (1, _) => Err(chunks.remove(0)), + _ => Err(Literal::JoinedString(text, chunks)), + }) + } + fn primary_base(&mut self) -> Parsing { let Some(token) = self.peek().cloned() else { return Err(self.error()); @@ -280,8 +300,8 @@ impl Parser<'_> { } "RANGE" if self.char_at(1, '<') => { let to: DataType = self.data_type()?; - let value: String = self.string_lit()?; - return Ok(typed(string(&value), to)); + let text: Result = self.written_string()?; + return Ok(typed(text.map_or_else(literal, |value| string(&value)), to)); } "ARRAY" if call_follows && self.starts_query_at(1) => { self.at += 2; @@ -559,22 +579,22 @@ impl Parser<'_> { }) } - /// A typed literal: a literal node for the date and time types, else a self-typed cast. + /// A typed literal: a literal node for a date or time type of one plain string, else a self-typed cast. fn typed_literal(&mut self, word: &str) -> Parsing { let type_at: usize = self.at; self.at += 1; - let value: String = self.string_lit()?; - Ok(match word { - "DATE" => literal(Literal::Date(value)), - "TIME" => literal(Literal::Time(value)), - "DATETIME" => literal(Literal::Datetime(value)), - "TIMESTAMP" => literal(Literal::Timestamp(value)), - _ => { + let text: Result = self.written_string()?; + Ok(match (word, text) { + ("DATE", Ok(value)) => literal(Literal::Date(value)), + ("TIME", Ok(value)) => literal(Literal::Time(value)), + ("DATETIME", Ok(value)) => literal(Literal::Datetime(value)), + ("TIMESTAMP", Ok(value)) => literal(Literal::Timestamp(value)), + (_, text) => { let end: usize = self.at; self.at = type_at; let to: DataType = self.data_type()?; self.at = end; - typed(string(&value), to) + typed(text.map_or_else(literal, |value| string(&value)), to) } }) } diff --git a/crates/sqlquack-syntax-bigquery/src/parse/tests.rs b/crates/sqlquack-syntax-bigquery/src/parse/tests.rs index 157698728..00a9a6399 100644 --- a/crates/sqlquack-syntax-bigquery/src/parse/tests.rs +++ b/crates/sqlquack-syntax-bigquery/src/parse/tests.rs @@ -6,3 +6,5 @@ mod test_grants; mod test_parse; #[path = "tests/test_types.rs"] mod test_types; +#[path = "tests/test_written_forms.rs"] +mod test_written_forms; 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 c34f74641..ba52a943e 100644 --- a/crates/sqlquack-syntax-bigquery/src/parse/tests/test_parse.rs +++ b/crates/sqlquack-syntax-bigquery/src/parse/tests/test_parse.rs @@ -1065,10 +1065,10 @@ fn given_bytes_literals_when_parsing_then_each_byte_is_one_unit() { expected_same: false, }, TreePairTestCase { - description: "a raw bytes literal also reads a character as its UTF-8 bytes", + description: "a raw bytes literal is kept apart from the escaped bytes it reads as", sql: "SELECT rb'\u{ff}'", other_sql: "SELECT b'\\xc3\\xbf'", - expected_same: true, + expected_same: false, }, ]; for test_case in test_cases { diff --git a/crates/sqlquack-syntax-bigquery/src/parse/tests/test_written_forms.rs b/crates/sqlquack-syntax-bigquery/src/parse/tests/test_written_forms.rs new file mode 100644 index 000000000..89cd647de --- /dev/null +++ b/crates/sqlquack-syntax-bigquery/src/parse/tests/test_written_forms.rs @@ -0,0 +1,76 @@ +//! BigQuery literals written raw, as adjacent pieces or plain: each form keeps its own tree. + +use crate::parse::main::parse::parse; +use crate::parse::tests::test_types::DistinctTreesTestCase; + +#[test] +fn given_a_literal_written_in_different_ways_when_parsing_then_each_tree_keeps_what_was_written() { + let test_cases = [ + DistinctTreesTestCase { + description: "bytes as one piece, as adjacent pieces and raw", + texts: &["SELECT b'ab'", "SELECT b'a' b'b'", "SELECT rb'ab'"], + expected_distinct: 3, + }, + DistinctTreesTestCase { + description: "a DATE of a plain, a raw and an adjacent string", + texts: &[ + "SELECT DATE '2020-01-01'", + "SELECT DATE r'2020-01-01'", + "SELECT DATE '2020-' '01-01'", + ], + expected_distinct: 3, + }, + DistinctTreesTestCase { + description: "a TIMESTAMP of a plain, a raw and an adjacent string", + texts: &[ + "SELECT TIMESTAMP '2020-01-01 00:00:00'", + "SELECT TIMESTAMP r'2020-01-01 00:00:00'", + "SELECT TIMESTAMP '2020-01-01' ' 00:00:00'", + ], + expected_distinct: 3, + }, + DistinctTreesTestCase { + description: "a JSON and a NUMERIC of a plain, a raw and an adjacent string", + texts: &[ + "SELECT JSON '{}'", + "SELECT JSON r'{}'", + "SELECT JSON '{' '}'", + "SELECT NUMERIC '1'", + "SELECT NUMERIC r'1'", + "SELECT NUMERIC '1' '0'", + ], + expected_distinct: 6, + }, + DistinctTreesTestCase { + description: "a grantee as a plain, a raw and an adjacent string", + texts: &[ + "GRANT `roles/viewer` ON TABLE d.t TO 'user:a@example.com'", + "GRANT `roles/viewer` ON TABLE d.t TO r'user:a@example.com'", + "GRANT `roles/viewer` ON TABLE d.t TO 'user:a@' 'example.com'", + ], + expected_distinct: 3, + }, + ]; + for test_case in test_cases { + let trees: Vec<_> = test_case + .texts + .iter() + .map(|text| parse(text).map(|parsed| parsed.statements)) + .collect(); + assert!( + trees.iter().all(Result::is_ok), + "{}: every form parses", + test_case.description + ); + let distinct: usize = trees + .iter() + .enumerate() + .filter(|(index, current)| !trees[..*index].contains(current)) + .count(); + assert_eq!( + distinct, test_case.expected_distinct, + "{}", + test_case.description + ); + } +} 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 56a2b7826..848d54001 100644 --- a/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/utility.rs +++ b/crates/sqlquack-syntax-databricks/src/parse/_helpers/commands/utility.rs @@ -859,6 +859,7 @@ impl Parser<'_> { is_group: false, is_share: false, kind: recipient.then(|| "RECIPIENT".to_owned()), + written: None, }) }) } diff --git a/crates/sqlquack-syntax-postgres/src/parse/_helpers/commands/grants.rs b/crates/sqlquack-syntax-postgres/src/parse/_helpers/commands/grants.rs index 66486ca49..b4dc8bf7d 100644 --- a/crates/sqlquack-syntax-postgres/src/parse/_helpers/commands/grants.rs +++ b/crates/sqlquack-syntax-postgres/src/parse/_helpers/commands/grants.rs @@ -435,6 +435,7 @@ impl Parser<'_> { is_group: false, is_share: false, kind: None, + written: None, }) } diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/commands.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/commands.rs index 54c403c57..37cff3bf4 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/commands.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/commands.rs @@ -661,6 +661,7 @@ impl Parser { is_group, is_share, kind: None, + written: None, }); if !self.match_token(TokenType::Comma) { break; diff --git a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/snowflake_grant.rs b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/snowflake_grant.rs index 990161fe6..a99c8b18d 100644 --- a/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/snowflake_grant.rs +++ b/crates/sqlquack-syntax-snowflake/src/parser/_helpers/ddl/snowflake_grant.rs @@ -338,6 +338,7 @@ impl Parser { is_group: false, is_share, kind: kind.filter(|_| !is_role && !is_share), + written: None, }) } diff --git a/crates/sqlquack-syntax/src/expressions/names/models.rs b/crates/sqlquack-syntax/src/expressions/names/models.rs index da67fe83c..4ebe74bf4 100644 --- a/crates/sqlquack-syntax/src/expressions/names/models.rs +++ b/crates/sqlquack-syntax/src/expressions/names/models.rs @@ -50,6 +50,10 @@ pub enum Literal { RawString(String), /// Adjacent string literals read as one (`'a' 'b'`): the joined value and each piece as written. JoinedString(String, Vec), + /// Raw bytes `rb'...'` (BigQuery): backslashes are literal, not escapes. + RawByteString(String), + /// Adjacent bytes literals read as one (`b'a' b'b'`): the joined value and each piece as written. + JoinedBytes(String, Vec), } impl Literal { @@ -69,8 +73,11 @@ impl Literal { | Literal::Datetime(s) | Literal::EscapeString(s) | Literal::DollarString(s) - | Literal::RawString(s) => s.as_str(), - Literal::TripleQuotedString(s, _) | Literal::JoinedString(s, _) => s.as_str(), + | Literal::RawString(s) + | Literal::RawByteString(s) => s.as_str(), + Literal::TripleQuotedString(s, _) + | Literal::JoinedString(s, _) + | Literal::JoinedBytes(s, _) => s.as_str(), } } @@ -114,7 +121,8 @@ impl fmt::Display for Literal { Literal::EscapeString(s) => write!(f, "E'{}'", s), Literal::DollarString(s) => write!(f, "$${}$$", s), Literal::RawString(s) => write!(f, "r'{}'", s), - Literal::JoinedString(_, pieces) => { + Literal::RawByteString(b) => write!(f, "rb'{}'", b), + Literal::JoinedString(_, pieces) | Literal::JoinedBytes(_, pieces) => { let written: Vec = pieces.iter().map(Literal::to_string).collect(); write!(f, "{}", written.join(" ")) } diff --git a/crates/sqlquack-syntax/src/expressions/schema_objects/models.rs b/crates/sqlquack-syntax/src/expressions/schema_objects/models.rs index 673952f69..bf578f058 100644 --- a/crates/sqlquack-syntax/src/expressions/schema_objects/models.rs +++ b/crates/sqlquack-syntax/src/expressions/schema_objects/models.rs @@ -1557,6 +1557,9 @@ pub struct GrantPrincipal { /// Snowflake: the grantee kind, such as ROLE, DATABASE ROLE, APPLICATION or USER #[serde(default, skip_serializing_if = "Option::is_none")] pub kind: Option, + /// BigQuery: a grantee string written raw or as adjacent pieces, as written; absent for one plain string + #[serde(default, skip_serializing_if = "Option::is_none")] + pub written: Option, } /// GRANT statement