Skip to content

Commit 73a1094

Browse files
committed
Fix the 53 receipts #68 contradicted
Snowflake receipts show the parser over-rejecting or over-accepting these statements since #68: - Routine arguments and RETURNS TABLE columns may omit their name or type; Snowflake parses them and fails semantically (90201, 90214), including for DEFINE (import 137). The routine argument check is removed and the untyped-column check of tables is limited to non-external tables. - SHOW GRANTS OF/TO/ON reads a trailing kind word as the name and takes an empty kind before ; or LIMIT. - ALTER DYNAMIC TABLE may omit the name before SET <property> =, take an empty UNSET TAG, a nameless SET TAG = '<value>', an empty row access policy column list and a string DROP SEARCH OPTIMIZATION target. - CREATE TASK takes dollar-quoted property values and name tag values, and refuses AFTER once EXECUTE AS USER was read. - CREATE EXPERIMENT may omit TYPE before FROM SPECIFICATION. - CREATE STAGE and FILE FORMAT options may be comma-separated. - Backtick-quoted identifiers are refused in faithful Snowflake modes. - CREATE OR ALTER / DEFINE SEQUENCE refuses a reserved name part.
1 parent 58b3331 commit 73a1094

13 files changed

Lines changed: 324 additions & 45 deletions

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,6 @@ impl Parser {
14451445
let (parameters, has_parens) = if self.match_token(TokenType::LParen) {
14461446
let params = self.parse_function_parameters()?;
14471447
self.expect_token(TokenType::RParen)?;
1448-
self.check_snowflake_routine_arguments(&params)?;
14491448
(params, true)
14501449
} else {
14511450
(Vec::new(), false)

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,11 @@ impl Parser {
399399
snowflake_inline_properties,
400400
)?;
401401

402-
let as_select = self.parse_create_table_column_list_query(no_column_defs, &columns)?;
402+
let as_select = self.parse_create_table_column_list_query(
403+
no_column_defs,
404+
&columns,
405+
table_modifier == Some("EXTERNAL"),
406+
)?;
403407

404408
table_properties = self.parse_create_table_partitioning(table_properties)?;
405409

@@ -564,18 +568,23 @@ impl Parser {
564568
Ok(table_properties)
565569
}
566570

567-
/// The `AS <query>` after a column list; Snowflake requires one when a listed column has no type.
571+
/// The `AS <query>` after a column list; Snowflake requires one when a non-external table's column has no type.
568572
fn parse_create_table_column_list_query(
569573
&mut self,
570574
no_column_defs: bool,
571575
columns: &[ColumnDef],
576+
external: bool,
572577
) -> Result<Option<Expression>> {
573578
let as_select = if !no_column_defs && self.match_token(TokenType::As) {
574579
Some(self.parse_statement()?)
575580
} else {
576581
None
577582
};
578-
if self.is_snowflake() && as_select.is_none() && columns.iter().any(|c| c.no_type) {
583+
if self.is_snowflake()
584+
&& !external
585+
&& as_select.is_none()
586+
&& columns.iter().any(|c| c.no_type)
587+
{
579588
return Err(self.parse_error("Expected a data type for the column"));
580589
}
581590
Ok(as_select)

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@ const ACTIONS: &[&[&str]] = &[
5151
impl Parser {
5252
/// Parse ALTER DYNAMIC TABLE after its kind.
5353
pub(super) fn parse_alter_dynamic_table(&mut self) -> Result<Expression> {
54-
let head = self.parse_alter_object_head()?;
54+
let head = if self.omits_dynamic_table_name() {
55+
(crate::expressions::names::models::TableRef::new(""), false)
56+
} else {
57+
self.parse_alter_object_head()?
58+
};
5559
let mut names: Vec<Expression> = Vec::new();
5660
while self.match_token(TokenType::Comma) {
5761
names.push(Expression::Table(Box::new(self.parse_object_name()?)));
@@ -71,6 +75,16 @@ impl Parser {
7175
))
7276
}
7377

78+
/// Whether the table name is omitted before `SET <property> =` which Snowflake parses and refuses by name check.
79+
fn omits_dynamic_table_name(&self) -> bool {
80+
let follows = |offset: usize, types: &[TokenType]| {
81+
self.tokens
82+
.get(self.current + offset)
83+
.is_some_and(|token| types.contains(&token.token_type))
84+
};
85+
(self.check_identifier("SET") || self.check(TokenType::Set)) && follows(2, &[TokenType::Eq])
86+
}
87+
7488
/// Parse what follows the words of an ALTER DYNAMIC TABLE action.
7589
fn parse_dynamic_table_action(&mut self, action: &str) -> Result<Parts> {
7690
Ok(match action {
@@ -80,7 +94,7 @@ impl Parser {
8094
self.expect_alter_words(&[&["ADD", "ROW", "ACCESS", "POLICY"]])?;
8195
let added = self.policy_operand()?;
8296
self.expect_token(TokenType::On)?;
83-
return Ok((vec![name, added, self.column_list_operand()?], vec![]));
97+
return Ok((vec![name, added, self.row_access_columns()?], vec![]));
8498
}
8599
(vec![name], vec![])
86100
}
@@ -100,19 +114,28 @@ impl Parser {
100114
let mut targets: Vec<Expression> = Vec::new();
101115
if self.match_token(TokenType::On) {
102116
loop {
103-
targets.push(self.search_target(action.starts_with("ADD"))?);
117+
if action.starts_with("DROP") && self.check(TokenType::String) {
118+
targets.push(self.parse_primary()?);
119+
} else {
120+
targets.push(self.search_target(action.starts_with("ADD"))?);
121+
}
104122
if !self.match_token(TokenType::Comma) {
105123
break;
106124
}
107125
}
108126
}
109127
(targets, vec![])
110128
}
111-
"ADD STORAGE LIFECYCLE POLICY" | "ADD ROW ACCESS POLICY" => {
129+
"ADD STORAGE LIFECYCLE POLICY" => {
112130
let policy = self.policy_operand()?;
113131
self.expect_token(TokenType::On)?;
114132
(vec![policy, self.column_list_operand()?], vec![])
115133
}
134+
"ADD ROW ACCESS POLICY" => {
135+
let policy = self.policy_operand()?;
136+
self.expect_token(TokenType::On)?;
137+
(vec![policy, self.row_access_columns()?], vec![])
138+
}
116139
"SET AGGREGATION POLICY" => {
117140
let policy = self.policy_operand()?;
118141
let mut properties: Vec<ObjectProperty> = Vec::new();
@@ -129,6 +152,15 @@ impl Parser {
129152
}
130153
(vec![policy], properties)
131154
}
155+
"UNSET TAG" if self.is_at_end() || self.check(TokenType::Semicolon) => (vec![], vec![]),
156+
"SET TAG" if self.check(TokenType::Eq) => {
157+
self.skip();
158+
let value = self.parse_object_value()?;
159+
(
160+
vec![],
161+
vec![property("TAG", Some(Identifier::new("")), Some(value))],
162+
)
163+
}
132164
"SET TAG" | "UNSET TAG" => (vec![], self.parse_tag_list(action == SET_TAG)?),
133165
"SET" | "UNSET" => (vec![], self.parse_dynamic_table_properties(action == SET)?),
134166
"ALTER" | "MODIFY" => (self.parse_dynamic_column_actions()?, vec![]),
@@ -141,6 +173,15 @@ impl Parser {
141173
Ok(Expression::Table(Box::new(self.parse_object_name()?)))
142174
}
143175

176+
/// Parse the row access policy columns, which Snowflake parses even when empty.
177+
fn row_access_columns(&mut self) -> Result<Expression> {
178+
if self.check(TokenType::LParen) && self.check_next(TokenType::RParen) {
179+
self.current += 2;
180+
return Ok(tuple(vec![]));
181+
}
182+
self.column_list_operand()
183+
}
184+
144185
/// Parse `( <col_name> [ , ... ] )`.
145186
fn column_list_operand(&mut self) -> Result<Expression> {
146187
self.expect_token(TokenType::LParen)?;

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,24 +246,32 @@ impl Parser {
246246

247247
/// Reads Snowflake CREATE TASK properties up to AS, refusing items that are not tags, contacts, assignments or clauses.
248248
pub(crate) fn check_snowflake_task_properties(&mut self, graph: bool) -> Result<()> {
249+
let mut executes_as = false;
249250
while !(graph && (self.is_at_end() || self.check(TokenType::Semicolon)))
250251
&& !(!graph && self.check(TokenType::As))
251252
{
252253
self.match_token(TokenType::With);
253254
if self.match_identifier("TAG") {
254255
self.skip_snowflake_task_assignments(|parser| {
255-
parser.expect_token(TokenType::String).map(|_| ())
256+
if parser.match_token(TokenType::String) {
257+
return Ok(());
258+
}
259+
parser.skip_snowflake_name()
256260
})?;
257261
} else if self.match_identifier("CONTACT") {
258262
self.skip_snowflake_task_assignments(Self::skip_snowflake_name)?;
259263
} else if self.match_identifier("TASK_DEFAULTS") {
260264
self.skip_snowflake_task_assignments(Self::skip_snowflake_task_value)?;
261265
} else if self.match_token(TokenType::After) {
266+
if executes_as {
267+
return Err(self.parse_error("AFTER comes before EXECUTE AS USER"));
268+
}
262269
self.skip_snowflake_name()?;
263270
while self.match_token(TokenType::Comma) {
264271
self.skip_snowflake_name()?;
265272
}
266273
} else if self.match_text_seq(&["EXECUTE", "AS", "USER"]) {
274+
executes_as = true;
267275
self.skip_snowflake_name()?;
268276
} else if self.match_token(TokenType::When) {
269277
if self.check(TokenType::As) {
@@ -311,6 +319,8 @@ impl Parser {
311319
}
312320
self.match_token(TokenType::Dash);
313321
if self.check(TokenType::String)
322+
|| self.check(TokenType::DollarString)
323+
|| self.check(TokenType::HeredocString)
314324
|| self.check(TokenType::Number)
315325
|| self.check(TokenType::True)
316326
|| self.check(TokenType::False)

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl Parser {
9696
}))))
9797
}
9898

99-
/// Parses `MODEL ... FROM MODEL`, `GATEWAY ... FROM SPECIFICATION` or a bare `EXPERIMENT`; None for other forms.
99+
/// Parses `MODEL ... FROM MODEL`, `{ GATEWAY | EXPERIMENT } ... FROM SPECIFICATION` or a bare `EXPERIMENT`; None for other forms.
100100
fn parse_snowflake_create_ml_object(&mut self, or_replace: bool) -> Result<Option<Expression>> {
101101
let kind = if self.check_identifier("MODEL") && !self.check_next_identifier("MONITOR") {
102102
"MODEL"
@@ -138,7 +138,10 @@ impl Parser {
138138
}
139139
"MODEL"
140140
}
141-
"GATEWAY" => {
141+
"GATEWAY" if !self.check(TokenType::From) => {
142+
return Err(self.parse_error("Expected FROM SPECIFICATION"));
143+
}
144+
_ if self.check(TokenType::From) => {
142145
self.expect_token(TokenType::From)?;
143146
self.expect_alter_words(&[&["SPECIFICATION"]])?;
144147
if !self.check(TokenType::String) && !self.check(TokenType::DollarString) {
@@ -215,6 +218,13 @@ impl Parser {
215218
None,
216219
Some(value),
217220
));
221+
if self.check(TokenType::Comma)
222+
&& self
223+
.peek_nth(1)
224+
.is_some_and(|token| token.token_type != TokenType::Semicolon)
225+
{
226+
self.skip();
227+
}
218228
}
219229
Ok(Some(Expression::CreateObject(Box::new(CreateObject {
220230
kind: kind.to_owned(),

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

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Snowflake function, procedure and object-name rules that the shared parsers do not apply.
22
33
use crate::expressions::names::models::TableRef;
4-
use crate::expressions::schema_objects::models::FunctionParameter;
54
use crate::parser::constants::{
65
SNOWFLAKE_RESERVED_KEYWORDS, SNOWFLAKE_ROUTINE_NAME_LIST_PROPERTIES,
76
SNOWFLAKE_ROUTINE_NAME_MAP_PROPERTIES, SNOWFLAKE_ROUTINE_NAME_PROPERTIES,
@@ -97,7 +96,7 @@ impl Parser {
9796
Ok(())
9897
}
9998

100-
/// In a Snowflake parse, refuses `TABLE ( ... )` with a reserved column name or a column lacking its type.
99+
/// In a Snowflake parse, refuses `TABLE ( ... )` with a reserved column name.
101100
pub(crate) fn check_snowflake_table_columns(&self) -> Result<()> {
102101
if !self.is_snowflake()
103102
|| !self.check(TokenType::Table)
@@ -122,31 +121,13 @@ impl Parser {
122121
.iter()
123122
.any(|word| next.text.eq_ignore_ascii_case(word))
124123
});
125-
let untyped = next.is_some_and(|next| next.token_type != TokenType::RParen)
126-
&& self
127-
.tokens
128-
.get(self.current + offset + 3)
129-
.is_some_and(|after| {
130-
matches!(after.token_type, TokenType::Comma | TokenType::RParen)
131-
});
132-
if opens_column && (reserved || untyped) {
133-
return Err(self.parse_error("Expected a column name and type in TABLE ( ... )"));
124+
if opens_column && reserved {
125+
return Err(self.parse_error("Expected a column name in TABLE ( ... )"));
134126
}
135127
}
136128
Ok(())
137129
}
138130

139-
/// In a Snowflake parse, refuses a routine argument that lacks its name or its type.
140-
pub(crate) fn check_snowflake_routine_arguments(
141-
&self,
142-
arguments: &[FunctionParameter],
143-
) -> Result<()> {
144-
if self.is_snowflake() && arguments.iter().any(|argument| argument.name.is_none()) {
145-
return Err(self.parse_error("Expected a name and a type for each argument"));
146-
}
147-
Ok(())
148-
}
149-
150131
/// In a Snowflake parse, refuses a ROW ACCESS POLICY body that is not `<name> ON ( <column> , ... )`.
151132
pub(crate) fn check_snowflake_row_access_policy(&mut self) -> Result<()> {
152133
if !self.is_snowflake() {

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,14 +261,14 @@ impl Parser {
261261
let (clause, show) = self.parse_show_grants_to(show)?;
262262
(Some(clause), show)
263263
} else if self.match_text_seq(&["OF"]) {
264-
let words = self.expect_alter_words(&[
264+
let words = self.parse_show_grants_kind(&[
265265
&["APPLICATION", "ROLE"],
266266
&["SERVICE", "ROLE"],
267267
&["DATABASE", "ROLE"],
268268
&["ROLE"],
269269
&["SHARE"],
270270
])?;
271-
(Some(format!("OF {words}")), show)
271+
(Some(format!("OF {words}").trim_end().to_owned()), show)
272272
} else if self.match_token(TokenType::In) {
273273
self.expect_alter_words(&[&["DCM", "PROJECT"]])?;
274274
(Some("IN DCM PROJECT".to_owned()), show)
@@ -319,14 +319,14 @@ impl Parser {
319319
{
320320
kind.push(self.advance_text()?.to_ascii_uppercase());
321321
}
322-
if kind.is_empty() {
322+
if kind.is_empty() && !self.ends_show_grants_clause(1) {
323323
return Err(self.parse_error("Expected an object kind after ON"));
324324
}
325325
show.scope = Some(self.parse_show_name()?);
326326
if self.check(TokenType::LParen) {
327327
show.privileges = self.parse_function_param_types()?;
328328
}
329-
Ok((format!("ON {}", kind.join(" ")), show))
329+
Ok((format!("ON {}", kind.join(" ")).trim_end().to_owned(), show))
330330
}
331331

332332
/// The grantee after SHOW GRANTS TO; a class instance role names its class as the target.
@@ -340,15 +340,41 @@ impl Parser {
340340
&["SHARE"],
341341
&["USER"],
342342
];
343-
if let Some(words) = GRANTEES.iter().find(|words| self.match_text_seq(words)) {
344-
return Ok((format!("TO {}", words.join(" ")), show));
343+
if GRANTEES.iter().any(|words| self.check_text_seq(words)) {
344+
let words = self.parse_show_grants_kind(GRANTEES)?;
345+
return Ok((format!("TO {words}").trim_end().to_owned(), show));
345346
}
346347
show.target = Some(self.parse_show_name()?);
347348
self.expect_alter_words(&[&["ROLE"]])?;
348349
show.scope = Some(self.parse_show_instance_role()?);
349350
Ok(("TO INSTANCE ROLE".to_owned(), show))
350351
}
351352

353+
/// One of `kinds`; in Snowflake its last word is the name when nothing follows and the rest is a kind, as `OF ROLE ;`.
354+
fn parse_show_grants_kind(&mut self, kinds: &[&[&str]]) -> Result<String> {
355+
if self.ends_show_grants_clause(1) {
356+
return Ok(String::new());
357+
}
358+
let words = self.expect_alter_words(kinds)?;
359+
let shorter: Vec<&str> = words.split(' ').collect();
360+
let rest: &[&str] = &shorter[..shorter.len() - 1];
361+
if self.ends_show_grants_clause(0) && kinds.contains(&rest) {
362+
self.current -= 1;
363+
return Ok(rest.join(" "));
364+
}
365+
Ok(words)
366+
}
367+
368+
/// Whether the statement or its LIMIT starts `offset` tokens on, in a faithful Snowflake parse.
369+
fn ends_show_grants_clause(&self, offset: usize) -> bool {
370+
self.applies_snowflake_context_rules()
371+
&& (self.is_statement_end_at(offset)
372+
|| self
373+
.tokens
374+
.get(self.current + offset)
375+
.is_some_and(|token| token.token_type == TokenType::Limit))
376+
}
377+
352378
/// `<instance_name>!<role_name>`, as a pair.
353379
fn parse_show_instance_role(&mut self) -> Result<Expression> {
354380
let instance = self.parse_show_name()?;

‎crates/sqlquack-syntax/src/parser/_helpers/expressions/operators.rs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ impl Parser {
283283
let (parameters, has_parens) = if self.match_token(TokenType::LParen) {
284284
let params = self.parse_function_parameters()?;
285285
self.expect_token(TokenType::RParen)?;
286-
self.check_snowflake_routine_arguments(&params)?;
287286
(params, true)
288287
} else if self.check(TokenType::Var) && !self.check(TokenType::As) {
289288
let params = self.parse_tsql_procedure_params()?;
@@ -444,7 +443,11 @@ impl Parser {
444443

445444
let if_not_exists =
446445
self.match_keywords(&[TokenType::If, TokenType::Not, TokenType::Exists]);
447-
let name = self.parse_table_ref()?;
446+
let name = if self.applies_snowflake_context_rules() {
447+
self.parse_object_name()?
448+
} else {
449+
self.parse_table_ref()?
450+
};
448451

449452
let mut seq = CreateSequence {
450453
name,

‎crates/sqlquack-syntax/src/parser/_helpers/statements/entry.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ impl Parser {
265265
/// Parses every remaining statement, split on semicolons, one `Expression` each.
266266
pub(crate) fn parse(&mut self) -> Result<Vec<Expression>> {
267267
self.ensure_complexity_guards()?;
268+
self.reject_snowflake_backticks()?;
268269
let mut statements: Vec<Expression> = Vec::new();
269270

270271
while !self.is_at_end() {

0 commit comments

Comments
 (0)