Skip to content

Commit a38ef65

Browse files
committed
Keep USE SCHEMA name parts with their own quoting
USE SCHEMA "v1 Q" . v1 and USE SCHEMA "v1 Q.v1" parsed to the same tree, since the two parts were joined into one name with a single quoted flag. In faithful Snowflake modes the Use node now also lists the parts of a qualified name, each with its quoting (D118 on 58b3331).
1 parent 73a1094 commit a38ef65

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

‎crates/sqlquack-syntax/src/expressions/statements/models.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ pub struct Use {
503503
/// Snowflake's secondary roles one by one, quoting kept; empty for ALL, NONE and other kinds.
504504
#[serde(default, skip_serializing_if = "Vec::is_empty")]
505505
pub roles: Vec<Identifier>,
506+
/// Snowflake's qualified name part by part, quoting kept; empty for a one-part name.
507+
#[serde(default, skip_serializing_if = "Vec::is_empty")]
508+
pub parts: Vec<Identifier>,
506509
}
507510

508511
/// Kind of USE statement

‎crates/sqlquack-syntax/src/parser/_helpers/ddl/alter.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,7 @@ impl Parser {
14861486
true => listed,
14871487
false => Vec::new(),
14881488
},
1489+
parts: Vec::new(),
14891490
})));
14901491
}
14911492
}
@@ -1517,17 +1518,22 @@ impl Parser {
15171518

15181519
let mut ident = self.expect_identifier_or_keyword_with_quoted()?;
15191520
self.reject_reserved_name_part(&ident)?;
1521+
let mut parts: Vec<Identifier> = Vec::new();
15201522

15211523
if self.match_token(TokenType::Dot) {
15221524
let second_part = self.expect_identifier_or_keyword_with_quoted()?;
15231525
self.reject_reserved_name_part(&second_part)?;
1526+
if self.applies_snowflake_context_rules() {
1527+
parts = vec![ident.clone(), second_part.clone()];
1528+
}
15241529
ident.name = format!("{}.{}", ident.name, second_part.name);
15251530
}
15261531

15271532
Ok(Expression::Use(Box::new(Use {
15281533
kind,
15291534
this: ident,
15301535
roles: Vec::new(),
1536+
parts,
15311537
})))
15321538
}
15331539

‎crates/sqlquack-syntax/tests/parser/test_snowflake_recorded.rs‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,24 @@ fn given_snowflake_create_and_begin_name_when_parsing_then_the_tree_keeps_the_el
749749
pointer: "/0/select/expressions/0/cast/to/name",
750750
expected_value: json!("PERIOD ( INT )"),
751751
},
752+
ProjectionTestCase {
753+
description: "USE SCHEMA keeps the database part's quoting",
754+
sql: "USE SCHEMA \"v1 Q\" . v1",
755+
pointer: "/0/use/parts/0/quoted",
756+
expected_value: json!(true),
757+
},
758+
ProjectionTestCase {
759+
description: "USE SCHEMA keeps the schema part apart from the database",
760+
sql: "USE SCHEMA \"v1 Q\" . v1",
761+
pointer: "/0/use/parts/1/name",
762+
expected_value: json!("v1"),
763+
},
764+
ProjectionTestCase {
765+
description: "USE SCHEMA with a quoted dotted name has one part",
766+
sql: "USE SCHEMA \"v1 Q.v1\"",
767+
pointer: "/0/use/parts",
768+
expected_value: json!(null),
769+
},
752770
];
753771

754772
for test_case in test_cases {

0 commit comments

Comments
 (0)