Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,017 changes: 505 additions & 512 deletions coverage/syntax/baseline.json

Large diffs are not rendered by default.

342 changes: 3 additions & 339 deletions coverage/syntax/receipt-disagreements.toml

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,12 @@ impl Parser {
let stmt = self.parse_statement()?;
function.body = Some(FunctionBody::Expression(stmt));
function = record_function_property(function, FunctionPropertyKind::As);
} else if self.is_snowflake()
&& self.check(TokenType::From)
&& self.check_next_identifier("EXPERIMENT")
{
let source = self.parse_experiment_run_source()?;
function.snowflake_properties.push(source);
} else if let Some(property) = self.parse_snowflake_routine_property()? {
function.snowflake_properties.push(property);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const RESOURCE_KINDS: &[&[&str]] = &[
&["DCM", "PROJECT"],
&["EVENT", "ROUTING", "TABLE"],
&["EXTERNAL", "AGENT"],
&["EXTERNAL", "CONSUMER"],
&["EXTERNAL", "TABLE"],
&["FILE", "FORMAT"],
&["GIT", "REPOSITORY"],
Expand Down Expand Up @@ -105,6 +106,14 @@ fn resource_actions(kind: &str) -> &'static [&'static [&'static str]] {
&["UNSET", "RULE"],
],
"EXTERNAL AGENT" => &[&["ADD", "VERSION"], &["SET"], &["UNSET"]],
"EXTERNAL CONSUMER" => &[
&["ADD"],
&["MODIFY"],
&["REMOVE"],
&["ROTATE"],
&["SET"],
&["UNSET"],
],
"EXTERNAL TABLE" => &[
&["ADD", "FILES"],
&["REMOVE", "FILES"],
Expand Down Expand Up @@ -366,6 +375,9 @@ impl Parser {
("DATA MOVEMENT RULE", "SET") if self.check_identifier("MAX_ROWS") => {
(vec![], vec![self.parse_max_rows_body()?])
}
("EXTERNAL CONSUMER", "ADD" | "MODIFY" | "REMOVE" | "ROTATE") => {
return self.parse_consumer_token(action);
}
(_, "SET" | "MODIFY LIVE VERSION SET") => (vec![], self.parse_resource_set()?),
(_, "UNSET") => (vec![], self.parse_resource_unset()?),
(
Expand Down Expand Up @@ -446,6 +458,29 @@ impl Parser {
Ok((action, (operands, self.parse_resource_properties()?)))
}

/// An external consumer's token action after its keyword: `{ PROGRAMMATIC ACCESS TOKEN | PAT } <name>`, then MODIFY's `SET <property> ...` or ADD and ROTATE's properties, with no IF [ NOT ] EXISTS.
fn parse_consumer_token(&mut self, mut action: String) -> Result<(String, ActionParts)> {
if !self.match_text_seq(&["PROGRAMMATIC", "ACCESS", "TOKEN"])
&& !self.match_identifier("PAT")
{
return Err(self.parse_error("Expected PROGRAMMATIC ACCESS TOKEN or PAT"));
}
action.push_str(" PROGRAMMATIC ACCESS TOKEN");
let operands = vec![self.parse_name_operand()?];
let properties = match action.as_str() {
"MODIFY PROGRAMMATIC ACCESS TOKEN" => {
if !self.match_token(TokenType::Set) {
return Err(self.parse_error("Expected SET"));
}
action.push_str(" SET");
self.parse_resource_set()?
}
"REMOVE PROGRAMMATIC ACCESS TOKEN" => Vec::new(),
_ => self.parse_resource_properties()?,
};
Ok((action, (operands, properties)))
}

/// Parse `( <expr> [ , ... ] )` as one tuple.
fn parse_expression_list_in_parens(&mut self) -> Result<Expression> {
self.expect_token(TokenType::LParen)?;
Expand Down Expand Up @@ -622,7 +657,7 @@ impl Parser {
}

/// Parse a location: a stage path or a string.
pub(super) fn parse_location_value(&mut self) -> Result<Expression> {
pub(crate) fn parse_location_value(&mut self) -> Result<Expression> {
if self.is_stage_path() {
return self.parse_stage_path();
}
Expand All @@ -638,12 +673,12 @@ impl Parser {
}

/// Whether the current token starts a stage path.
pub(super) fn is_stage_path(&self) -> bool {
pub(crate) fn is_stage_path(&self) -> bool {
!self.is_at_end() && self.peek_text().starts_with('@')
}

/// Parse a stage path, which like Snowflake's ends at the first whitespace; a quoted string that starts with @ stays a string.
pub(super) fn parse_stage_path(&mut self) -> Result<Expression> {
pub(crate) fn parse_stage_path(&mut self) -> Result<Expression> {
if self.check(TokenType::String) || self.check(TokenType::DollarString) {
let text = self.peek_text().to_owned();
self.skip();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ type ActionParts = (Vec<Expression>, Vec<ObjectProperty>);
type Action = (String, ActionParts);

impl Parser {
/// Parse ALTER USER after its kind; the name may be left out for the current user.
/// Parse ALTER USER after its kind; the name may be left out for the current user, except to rename, enroll MFA, or set or unset a policy or tags.
pub(super) fn parse_alter_user(&mut self) -> Result<Expression> {
let if_exists = self.match_keywords(&[TokenType::If, TokenType::Exists]);
let unnamed = self
Expand All @@ -62,6 +62,18 @@ impl Parser {
&["SET"],
&["UNSET"],
])?;
let tag = self.check_identifier("TAG")
&& !self.check_next(TokenType::Eq)
&& !self.is_statement_end_at(1);
let policy = USER_POLICIES.iter().any(|words| self.check_text_seq(words));
let named_only = match action.as_str() {
"RENAME TO" | "ENROLL MFA" => true,
"SET" | "UNSET" => tag || policy,
_ => false,
};
if unnamed && named_only {
return Err(self.parse_error(format!("Expected a user name before {action}")));
}
let (action, parts): Action = match action.as_str() {
"RENAME TO" => (action, (vec![self.parse_user_operand()?], vec![])),
"ADD" | "MODIFY" | "REMOVE" | "ROTATE" => self.parse_user_credential_action(action)?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::parser::_helpers::ddl::snowflake_alter::property;
use crate::parser::constants::EXECUTE_DCM_PROJECT;
use crate::parser::constants::SNOWFLAKE_DEFINE_OPAQUE_KINDS;
use crate::parser::constants::SNOWFLAKE_RESERVED_KEYWORDS;
use crate::parser::models::Parser;
use sqlquack_syntax::expressions::names::models::Identifier;
Expand All @@ -10,13 +11,88 @@ use sqlquack_syntax::expressions::schema_objects::models::ExecuteObject;
use sqlquack_syntax::expressions::schema_objects::models::ObjectProperty;
use sqlquack_syntax::expressions::statements::models::CreateTask;
use sqlquack_syntax::expressions::statements::models::ExecuteUsingPolicyContext;
use sqlquack_syntax::expressions::statements::models::Raw;
use sqlquack_syntax::expressions::tree::models::Expression;
use sqlquack_syntax::expressions::types::models::Tuple;
use sqlquack_syntax::tokens::models::TokenType;
use sqlquack_syntax::tokens::types::Result;
use sqlquack_syntax::traversal::types::ExpressionWalk;

impl Parser {
/// In Snowflake, refuse an EXECUTE IMMEDIATE, read after EXECUTE, that is neither `IMMEDIATE <source> [ USING ( <variable> [ , ... ] ) ]` nor `IMMEDIATE FROM <location> [ USING ( <name> => <value> [ , ... ] ) ] [ DRY_RUN = { TRUE | FALSE } ]`; the cursor ends where it started.
pub(crate) fn check_execute_immediate(&mut self) -> Result<()> {
if !self.applies_snowflake_context_rules() {
return Ok(());
}
let start = self.current;
let checked = self.check_execute_immediate_parts();
self.current = start;
checked
}

/// One part of the text EXECUTE IMMEDIATE runs: a string, a `$session` or `:bound` variable, or a variable's name.
fn check_execute_operand(&mut self) -> Result<()> {
if self.check(TokenType::Dollar) || self.check(TokenType::Colon) {
let end = self.peek().span.end;
self.skip();
if !self.is_identifier_token() || self.peek().span.start != end {
return Err(self.parse_error("Expected a variable's name"));
}
self.skip();
return Ok(());
}
if self.check(TokenType::String)
|| self.check(TokenType::DollarString)
|| (self.is_identifier_token()
&& !self.is_snowflake_reserved_keyword()
&& !self.peek_text().starts_with('@'))
{
self.skip();
return Ok(());
}
Err(self.parse_error("Expected the statement to execute"))
}

fn check_execute_immediate_parts(&mut self) -> Result<()> {
self.skip();
let from = self.match_token(TokenType::From);
if from {
if !self.check(TokenType::String) && !self.is_stage_path() {
return Err(self.parse_error("Expected a stage path or a string"));
}
self.parse_location_value()?;
} else {
self.check_execute_operand()?;
while self.match_token(TokenType::DPipe) {
self.check_execute_operand()?;
}
}
if self.match_token(TokenType::Using) {
self.expect_token(TokenType::LParen)?;
loop {
if from {
self.expect_identifier_or_keyword()?;
self.expect_token(TokenType::FArrow)?;
}
self.parse_expression()?;
if !self.match_token(TokenType::Comma) {
break;
}
}
self.expect_token(TokenType::RParen)?;
}
if from && self.match_identifier("DRY_RUN") {
self.expect_token(TokenType::Eq)?;
if !self.match_token(TokenType::True) && !self.match_token(TokenType::False) {
return Err(self.parse_error("Expected TRUE or FALSE"));
}
}
match self.is_statement_end_at(0) {
true => Ok(()),
false => Err(self.parse_error("Expected the end of EXECUTE IMMEDIATE")),
}
}

/// In Snowflake, refuse an unquoted reserved keyword in any dotted part of a COMMENT ON name.
pub(super) fn check_snowflake_comment_name(&self) -> Result<()> {
if !self.is_snowflake() {
Expand Down Expand Up @@ -411,7 +487,23 @@ impl Parser {

/// Snowflake DCM `DEFINE`, refused for kinds kept as opaque text or not yet checked closely; a database's or schema's properties are checked word by word before they are kept as text.
pub(crate) fn parse_snowflake_define(&mut self) -> Result<Expression> {
let start = self.current;
self.skip();
let opaque_kind = SNOWFLAKE_DEFINE_OPAQUE_KINDS
.iter()
.any(|words| self.check_text_seq(words));
self.current = start;
let definition = self.parse_create()?;
if let (true, Expression::Raw(raw)) = (opaque_kind, &definition) {
let text = raw.sql.strip_prefix("CREATE").unwrap_or(&raw.sql);
return Ok(Expression::Raw(Raw {
sql: format!("DEFINE{text}"),
}));
}
if matches!(&definition, Expression::CreateObject(create) if matches!(create.kind.as_str(), "FILE FORMAT") && create.modifier.is_some())
{
return Err(self.parse_error("DEFINE FILE FORMAT takes no TEMPORARY or VOLATILE"));
}
let unchecked_kind = matches!(definition, Expression::CreateTable(_));
let mut unchecked = definition.clone();
match &mut unchecked {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

use crate::parser::_helpers::ddl::snowflake_alter::property;
use crate::parser::constants::{
SNOWFLAKE_BODY_POLICY_KINDS, SNOWFLAKE_KIND_WORDS_ALONE, SNOWFLAKE_PROPERTY_KINDS,
SNOWFLAKE_TAGS_LAST_KINDS,
SNOWFLAKE_BODY_POLICY_KINDS, SNOWFLAKE_GROUP_NAME_PROPERTIES, SNOWFLAKE_KIND_WORDS_ALONE,
SNOWFLAKE_PROPERTY_KINDS, SNOWFLAKE_TAGS_LAST_KINDS,
};
use crate::parser::models::Parser;
use sqlquack_syntax::expressions::names::models::Identifier;
Expand Down Expand Up @@ -118,6 +118,9 @@ impl Parser {
continue;
}
let tags = self.check_identifier("TAG") && self.check_next(TokenType::LParen);
if matches!(parts.as_slice(), ["FAILOVER" | "REPLICATION", "GROUP"]) {
self.check_group_name_value()?;
}
properties.extend(self.parse_create_property_item()?);
self.match_token(TokenType::Comma);
if tags
Expand All @@ -141,6 +144,28 @@ impl Parser {
}))))
}

/// Refuse a string as the first value of a group property that names objects, and a quoted integration type.
fn check_group_name_value(&self) -> Result<()> {
let key = self.peek_text().to_ascii_uppercase();
if !self.check_next(TokenType::Eq)
|| !SNOWFLAKE_GROUP_NAME_PROPERTIES.contains(&key.as_str())
{
return Ok(());
}
let refused = matches!(
(self.peek_nth(2).map(|token| token.token_type), key.as_str()),
(Some(TokenType::String), _)
| (
Some(TokenType::QuotedIdentifier),
"ALLOWED_INTEGRATION_TYPES"
)
);
match refused {
true => Err(self.parse_error(format!("Expected a name as the value of {key}"))),
false => Ok(()),
}
}

/// A Snowflake CREATE of a body policy, `<kind> <name> AS ( <signature> ) RETURNS <type> -> <body> [ <property> ... ]`; None for any other kind.
pub(crate) fn parse_snowflake_create_body_policy(
&mut self,
Expand Down
Loading
Loading