Skip to content

Commit 7ae6b35

Browse files
committed
Merge remote-tracking branch 'origin/main' into feat/explain-codes
2 parents b9313b1 + 4203044 commit 7ae6b35

19 files changed

Lines changed: 1544 additions & 5 deletions

File tree

‎corpora/polyglot-check/expected_differences.toml‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,83 @@ dialects = ["Snowflake"]
912912
sqlquack_error = "Snowflake set operations take no LEFT, RIGHT, FULL, INNER or OUTER"
913913
note = "INNER UNION ... BY NAME is BigQuery's; Snowflake rejects a join side before a set operation (recorded errno 1003)"
914914

915+
[[rejection]]
916+
id = "snowflake_role_grant_without_role"
917+
sql = "GRANT developer_role TO john;"
918+
dialects = ["Snowflake"]
919+
sqlquack_error = "Expected a privilege"
920+
note = "Postgres grants role membership without ROLE; Snowflake writes GRANT ROLE <name> TO { ROLE | USER } <name> and refuses GRANT v1 TO USER v1 with syntax error 1003"
921+
922+
[[rejection]]
923+
id = "snowflake_role_grant_without_role_quoted_user"
924+
sql = "GRANT abluva TO \"kk8529p@abluva.com\";"
925+
dialects = ["Snowflake"]
926+
sqlquack_error = "Expected a privilege"
927+
note = "Postgres grants role membership without ROLE; Snowflake writes GRANT ROLE <name> TO { ROLE | USER } <name> and refuses GRANT v1 TO USER v1 with syntax error 1003"
928+
929+
[[rejection]]
930+
id = "snowflake_role_grant_without_role_admin_option"
931+
sql = "GRANT admin_role TO mary WITH ADMIN OPTION;"
932+
dialects = ["Snowflake"]
933+
sqlquack_error = "Expected a privilege"
934+
note = "Postgres grants role membership without ROLE; Snowflake writes GRANT ROLE <name> TO { ROLE | USER } <name> and refuses GRANT v1 TO USER v1 with syntax error 1003"
935+
936+
[[rejection]]
937+
id = "snowflake_role_grant_without_role_lists"
938+
sql = "GRANT role1, role2 TO user1, \"user2@example.com\";"
939+
dialects = ["Snowflake"]
940+
sqlquack_error = "Expected a privilege"
941+
note = "Postgres grants role membership without ROLE; Snowflake writes GRANT ROLE <name> TO { ROLE | USER } <name> and refuses GRANT v1 TO USER v1 with syntax error 1003"
942+
943+
[[rejection]]
944+
id = "snowflake_role_grant_without_role_user_list"
945+
sql = "GRANT purchases_reader_role TO george, maria;"
946+
dialects = ["Snowflake"]
947+
sqlquack_error = "Expected a privilege"
948+
note = "Postgres grants role membership without ROLE; Snowflake writes GRANT ROLE <name> TO { ROLE | USER } <name> and refuses GRANT v1 TO USER v1 with syntax error 1003"
949+
950+
[[rejection]]
951+
id = "snowflake_role_grant_quoted_roles"
952+
sql = "GRANT 'role1', 'role2' TO 'user1'@'localhost', 'user2'@'localhost';"
953+
dialects = ["Snowflake"]
954+
sqlquack_error = "Expected a privilege"
955+
note = "MySQL grants quoted roles to user@host accounts; Snowflake writes GRANT ROLE <name> TO { ROLE | USER } <name>, with identifiers, not strings"
956+
957+
[[rejection]]
958+
id = "snowflake_role_grant_without_role_after_select"
959+
sql = "SELECT set_config('session_authorization', 'abluva', false); GRANT abluva TO \"kk8529p@abluva.com\";"
960+
dialects = ["Snowflake"]
961+
sqlquack_error = "Expected a privilege"
962+
note = "Postgres grants role membership without ROLE; Snowflake writes GRANT ROLE <name> TO { ROLE | USER } <name> and refuses GRANT v1 TO USER v1 with syntax error 1003"
963+
964+
[[rejection]]
965+
id = "snowflake_role_grant_without_role_unterminated"
966+
sql = "GRANT abluva TO \"kk8529p@abluva.com\""
967+
dialects = ["Snowflake"]
968+
sqlquack_error = "Expected a privilege"
969+
note = "Postgres grants role membership without ROLE; Snowflake writes GRANT ROLE <name> TO { ROLE | USER } <name> and refuses GRANT v1 TO USER v1 with syntax error 1003"
970+
971+
[[rejection]]
972+
id = "snowflake_role_revoke_without_role"
973+
sql = "REVOKE developer_role FROM john;"
974+
dialects = ["Snowflake"]
975+
sqlquack_error = "Expected a privilege"
976+
note = "Postgres revokes role membership without ROLE; Snowflake writes REVOKE ROLE <name> FROM { ROLE | USER } <name>, as GRANT ROLE, which refuses the form without ROLE with syntax error 1003"
977+
978+
[[rejection]]
979+
id = "snowflake_role_revoke_admin_option_without_role"
980+
sql = "REVOKE ADMIN OPTION FOR developer_role FROM john CASCADE;"
981+
dialects = ["Snowflake"]
982+
sqlquack_error = "Expected a privilege"
983+
note = "Postgres revokes role membership without ROLE; Snowflake writes REVOKE ROLE <name> FROM { ROLE | USER } <name>, as GRANT ROLE, which refuses the form without ROLE with syntax error 1003"
984+
985+
[[rejection]]
986+
id = "snowflake_role_revoke_without_role_lists"
987+
sql = "REVOKE role1, role2 FROM user1, \"user2@example.com\" RESTRICT;"
988+
dialects = ["Snowflake"]
989+
sqlquack_error = "Expected a privilege"
990+
note = "Postgres revokes role membership without ROLE; Snowflake writes REVOKE ROLE <name> FROM { ROLE | USER } <name>, as GRANT ROLE, which refuses the form without ROLE with syntax error 1003"
991+
915992
[[rejection]]
916993
id = "snowflake_describe_bare_name"
917994
sql = "DESCRIBE x"
@@ -1137,6 +1214,12 @@ note = "a table clone's AT | BEFORE ( TIMESTAMP | OFFSET | STATEMENT => <expr> )
11371214
# The check counts an entry only when polyglot's tree is a single command, SQLQuack's tree starts
11381215
# with `sqlquack_node`, and the statement, without whitespace and ignoring case, contains `marker`.
11391216

1217+
[[structured]]
1218+
id = "snowflake_storage_integration_properties"
1219+
marker = "CREATESTORAGEINTEGRATION"
1220+
sqlquack_node = "create_object"
1221+
note = "a Snowflake CREATE STORAGE INTEGRATION parses into its name and its property list, each property's kind and value; polyglot keeps the statement as raw SQL; crates/sqlquack-syntax/tests/parser/test_snowflake_create_properties.rs pins the tree"
1222+
11401223
[[structured]]
11411224
id = "snowflake_scripting_block"
11421225
marker = "BEGIN"

‎crates/sqlquack-polyglot-check/src/expected/_helpers/nodes.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use serde_json::Value;
44

5-
use crate::expected::constants::{OK_PREFIX, THIS_KEY};
5+
use crate::expected::constants::{COMMAND_TREE, OK_PREFIX, RAW_TREE, THIS_KEY};
66

77
/// Whether both rendered parses are trees that are equal once SQLQuack's `node` wrappers go.
88
pub(crate) fn differ_only_by_kept_node(polyglot: &str, sqlquack: &str, node: &str) -> bool {
@@ -91,3 +91,10 @@ fn without_field(value: Value, field: &str) -> Value {
9191
other => other,
9292
}
9393
}
94+
95+
/// Whether polyglot's rendered tree is one opaque command or one statement kept as raw SQL.
96+
pub(crate) fn is_opaque(polyglot: &str) -> bool {
97+
[COMMAND_TREE, RAW_TREE]
98+
.iter()
99+
.any(|opaque| polyglot.starts_with(&format!("{OK_PREFIX}{opaque}")))
100+
}

‎crates/sqlquack-polyglot-check/src/expected/constants.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ pub const ERROR_PREFIX: &str = "err:";
66
pub const OK_PREFIX: &str = "ok:";
77
/// How a rendered tree of one opaque command begins after the prefix.
88
pub const COMMAND_TREE: &str = "[{\"command\":";
9+
/// How a rendered tree of one statement polyglot keeps as raw SQL begins after the prefix.
10+
pub const RAW_TREE: &str = "[{\"raw\":";
911
/// The field naming an identifier in a serialised tree.
1012
pub const NAME_KEY: &str = "name";
1113
/// The field holding the operand of a wrapper node such as unary plus.

‎crates/sqlquack-polyglot-check/src/expected/models.rs‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! Expected differences.
22
3-
use crate::expected::_helpers::nodes::{differ_only_by_kept_field, differ_only_by_kept_node};
3+
use crate::expected::_helpers::nodes::{
4+
differ_only_by_kept_field, differ_only_by_kept_node, is_opaque,
5+
};
46
use crate::expected::_helpers::quoting::{compact, differ_only_by_kept_quoting};
5-
use crate::expected::constants::{COMMAND_TREE, ERROR_PREFIX, OK_PREFIX};
7+
use crate::expected::constants::{ERROR_PREFIX, OK_PREFIX};
68
use std::collections::{BTreeMap, BTreeSet};
79

810
use serde::Deserialize;
@@ -34,7 +36,7 @@ pub struct ExpectedFile {
3436
pub structured: Vec<StructuredStatement>,
3537
}
3638

37-
/// A statement polyglot parses into an opaque command and SQLQuack parses into a structured node.
39+
/// A statement polyglot keeps as an opaque command or raw SQL and SQLQuack parses into a structured node.
3840
#[derive(Clone, Debug, Deserialize)]
3941
#[serde(deny_unknown_fields)]
4042
pub struct StructuredStatement {
@@ -225,7 +227,7 @@ impl ExpectedDifferences {
225227
) -> Option<&StructuredStatement> {
226228
let compact = compact(sql);
227229
self.structured.iter().find(|entry| {
228-
polyglot.starts_with(&format!("{OK_PREFIX}{COMMAND_TREE}"))
230+
is_opaque(polyglot)
229231
&& sqlquack.starts_with(&format!("{OK_PREFIX}[{{\"{}\":", entry.sqlquack_node))
230232
&& compact.contains(&entry.marker.to_ascii_uppercase())
231233
})

‎crates/sqlquack-polyglot-check/tests/runs/test_exact.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,32 @@ fn given_committed_corpora_when_parsed_by_both_parsers_then_results_are_identica
5151
("snowflake_show_create_table".to_owned(), 1),
5252
("snowflake_stage_reserved_name".to_owned(), 1),
5353
("snowflake_stage_quoted_blank_name".to_owned(), 1),
54+
("snowflake_role_grant_without_role".to_owned(), 1),
55+
(
56+
"snowflake_role_grant_without_role_quoted_user".to_owned(),
57+
1,
58+
),
59+
(
60+
"snowflake_role_grant_without_role_admin_option".to_owned(),
61+
1,
62+
),
63+
("snowflake_role_grant_without_role_lists".to_owned(), 1),
64+
("snowflake_role_grant_without_role_user_list".to_owned(), 1),
65+
("snowflake_role_grant_quoted_roles".to_owned(), 1),
66+
(
67+
"snowflake_role_grant_without_role_after_select".to_owned(),
68+
1,
69+
),
70+
(
71+
"snowflake_role_grant_without_role_unterminated".to_owned(),
72+
1,
73+
),
74+
("snowflake_role_revoke_without_role".to_owned(), 1),
75+
(
76+
"snowflake_role_revoke_admin_option_without_role".to_owned(),
77+
1,
78+
),
79+
("snowflake_role_revoke_without_role_lists".to_owned(), 1),
5480
]),
5581
differences: Vec::new(),
5682
tokens_lost: Vec::new(),

‎crates/sqlquack-polyglot-check/tests/runs/test_structured.rs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ fn given_a_command_polyglot_keeps_when_compared_then_only_the_listed_structure_i
1616
sqlquack: structured,
1717
expected_match: true,
1818
},
19+
StructuredTestCase {
20+
description: "raw SQL polyglot keeps, which SQLQuack parses into the listed node, is matched",
21+
sql: block,
22+
polyglot: r#"ok:[{"raw":{"sql":"BEGIN RETURN 1; END"}}]"#,
23+
sqlquack: structured,
24+
expected_match: true,
25+
},
1926
StructuredTestCase {
2027
description: "a structured polyglot tree is not matched",
2128
sql: block,

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1184,6 +1184,9 @@ pub struct CreateObject {
11841184
/// OR REPLACE clause
11851185
#[serde(default)]
11861186
pub or_replace: bool,
1187+
/// OR ALTER clause
1188+
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
1189+
pub or_alter: bool,
11871190
/// IF NOT EXISTS clause
11881191
#[serde(default)]
11891192
pub if_not_exists: bool,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,9 @@ pub struct GrantPrincipal {
647647
/// Whether prefixed with SHARE keyword (Snowflake)
648648
#[serde(default)]
649649
pub is_share: bool,
650+
/// Snowflake: the grantee kind, such as ROLE, DATABASE ROLE, APPLICATION or USER
651+
#[serde(default, skip_serializing_if = "Option::is_none")]
652+
pub kind: Option<String>,
650653
}
651654

652655
/// GRANT statement
@@ -668,6 +671,9 @@ pub struct Grant {
668671
/// TSQL: AS principal (the grantor role)
669672
#[serde(default, skip_serializing_if = "Option::is_none")]
670673
pub as_principal: Option<Identifier>,
674+
/// Snowflake GRANT OWNERSHIP: COPY or REVOKE CURRENT GRANTS
675+
#[serde(default, skip_serializing_if = "Option::is_none")]
676+
pub current_grants: Option<String>,
671677
}
672678

673679
/// REVOKE statement

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,9 @@ impl Parser {
287287
pub(crate) fn parse_grant(&mut self) -> Result<Expression> {
288288
let statement_start = self.current;
289289
self.expect_token(TokenType::Grant)?;
290+
if self.is_snowflake() {
291+
return self.parse_snowflake_grant(false);
292+
}
290293

291294
let saved_pos = self.current;
292295
let mut depth = 0i32;
@@ -363,13 +366,17 @@ impl Parser {
363366
principals,
364367
grant_option,
365368
as_principal,
369+
current_grants: None,
366370
})))
367371
}
368372

369373
/// Parses REVOKE [GRANT OPTION FOR] privileges ON [kind] object FROM principals [CASCADE].
370374
pub(crate) fn parse_revoke(&mut self) -> Result<Expression> {
371375
let statement_start = self.current;
372376
self.expect_token(TokenType::Revoke)?;
377+
if self.is_snowflake() {
378+
return self.parse_snowflake_grant(true);
379+
}
373380

374381
let saved_pos = self.current;
375382
let mut depth = 0i32;
@@ -592,6 +599,7 @@ impl Parser {
592599
is_role,
593600
is_group,
594601
is_share,
602+
kind: None,
595603
});
596604
if !self.match_token(TokenType::Comma) {
597605
break;

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ pub(crate) mod snowflake_alter_tables;
1515
pub(crate) mod snowflake_commands;
1616
pub(crate) mod snowflake_create_clone;
1717
pub(crate) mod snowflake_create_from;
18+
pub(crate) mod snowflake_create_properties;
1819
pub(crate) mod snowflake_data_metric;
20+
pub(crate) mod snowflake_grant;
1921
pub(crate) mod snowflake_show;
2022
pub(crate) mod snowflake_statements;
2123
pub(crate) mod table_options;

0 commit comments

Comments
 (0)