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
2,576 changes: 1,791 additions & 785 deletions coverage/syntax/baseline.json

Large diffs are not rendered by default.

482 changes: 4 additions & 478 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 @@ -1461,7 +1461,7 @@ impl Parser {
};

let (parameters, has_parens) = if self.match_token(TokenType::LParen) {
let params = self.parse_function_parameters()?;
let params = self.parse_function_parameters(false)?;
self.expect_token(TokenType::RParen)?;
(params, true)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ pub(crate) mod snowflake_classes;
pub(crate) mod snowflake_commands;
pub(crate) mod snowflake_containers;
pub(crate) mod snowflake_copy;
pub(crate) mod snowflake_create_alert;
pub(crate) mod snowflake_create_clone;
pub(crate) mod snowflake_create_from;
pub(crate) mod snowflake_create_listing;
pub(crate) mod snowflake_create_properties;
pub(crate) mod snowflake_create_rules;
pub(crate) mod snowflake_create_search;
pub(crate) mod snowflake_create_service;
pub(crate) mod snowflake_create_sets;
pub(crate) mod snowflake_create_table;
pub(crate) mod snowflake_data_metric;
pub(crate) mod snowflake_event_tables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const RESOURCE_KINDS: &[&[&str]] = &[
&["ROW", "ACCESS", "POLICY"],
&["STORAGE", "LIFECYCLE", "POLICY"],
&["DATA", "MOVEMENT", "POLICY"],
&["DATA", "MOVEMENT", "RULE"],
&["AGGREGATION", "POLICY"],
&["AUTHENTICATION", "POLICY"],
&["BACKUP", "POLICY"],
Expand Down Expand Up @@ -197,6 +198,18 @@ fn resource_actions(kind: &str) -> &'static [&'static [&'static str]] {
}

impl Parser {
/// A data movement rule's `MAX_ROWS AS ( ) RETURNS <type> -> <expression>`.
pub(super) fn parse_max_rows_body(&mut self) -> Result<ObjectProperty> {
self.skip();
self.expect_token(TokenType::As)?;
self.expect_token(TokenType::LParen)?;
self.expect_token(TokenType::RParen)?;
self.expect_token(TokenType::Returns)?;
self.parse_data_type()?;
self.expect_token(TokenType::Arrow)?;
Ok(property("MAX_ROWS", None, Some(self.parse_expression()?)))
}

/// Whether `MODEL MONITOR` here starts `ALTER MODEL <name>` with a name or schema called monitor: a dot or a whole ALTER MODEL action follows.
fn alters_model_named_monitor(&self) -> bool {
if !(self.check_identifier("MODEL") && self.check_next_identifier("MONITOR")) {
Expand Down Expand Up @@ -341,6 +354,9 @@ impl Parser {
vec![property("BODY", None, Some(self.parse_expression()?))],
)
}
("DATA MOVEMENT RULE", "SET") if self.check_identifier("MAX_ROWS") => {
(vec![], vec![self.parse_max_rows_body()?])
}
(_, "SET" | "MODIFY LIVE VERSION SET") => (vec![], self.parse_resource_set()?),
(_, "UNSET") => (vec![], self.parse_resource_unset()?),
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,18 @@ impl Parser {
Ok(())
}

/// Snowflake DCM `DEFINE`, refused for kinds kept as opaque text or not yet checked closely.
/// 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 definition = self.parse_create()?;
let unchecked_kind = matches!(definition, Expression::CreateTable(_));
let mut unchecked = definition.clone();
match &mut unchecked {
Expression::CreateDatabase(database) => database.properties.clear(),
Expression::CreateSchema(schema) => schema.properties.clear(),
_ => {}
}
if unchecked_kind
|| definition
|| unchecked
.contains(|node| matches!(node, Expression::Command(_) | Expression::Raw(_)))
{
return Err(self.parse_error("Unsupported DEFINE object definition"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//! Parser ddl: Snowflake CREATE ALERT, with its condition and action or FROM TEMPLATE, checked against its grammar.

use crate::parser::_helpers::ddl::snowflake_alter::property;
use crate::parser::models::Parser;
use sqlquack_syntax::expressions::schema_objects::models::{CreateObject, ObjectProperty};
use sqlquack_syntax::expressions::tree::models::Expression;
use sqlquack_syntax::tokens::models::TokenType;
use sqlquack_syntax::tokens::types::Result;

/// The words an alert's state may be given by, without a value.
const ALERT_STATES: [&str; 2] = ["STARTED", "SUSPENDED"];

impl Parser {
/// A Snowflake CREATE ALERT other than a clone; None for any other CREATE and outside the faithful Snowflake modes.
pub(crate) fn parse_snowflake_create_alert(
&mut self,
or_replace: bool,
or_alter: bool,
) -> Result<Option<Expression>> {
if !self.applies_snowflake_context_rules() || !self.check_text_seq(&["ALERT"]) {
return Ok(None);
}
let start = self.current;
self.skip();
let if_not_exists = self.check(TokenType::If) && !self.check_next(TokenType::LParen);
if if_not_exists
&& !self.match_keywords(&[TokenType::If, TokenType::Not, TokenType::Exists])
{
return Err(self.parse_error("Expected IF NOT EXISTS"));
}
let name = self.parse_object_name()?;
if self.check_identifier("CLONE") {
self.current = start;
return Ok(None);
}
let template = self.match_token(TokenType::From);
let mut operands: Vec<Expression> = Vec::new();
if template {
self.expect_alter_words(&[&["TEMPLATE"]])?;
operands.push(Expression::Table(Box::new(self.parse_object_name()?)));
}
let properties = self.parse_alert_properties(template)?;
if !template {
operands.extend(self.parse_alert_condition_and_action()?);
}
if !self.is_at_end() && !self.check(TokenType::Semicolon) {
return Err(self.parse_error("Expected the end of the statement"));
}
Ok(Some(Expression::CreateObject(Box::new(CreateObject {
kind: "ALERT".to_owned(),
modifier: None,
or_replace,
or_alter,
if_not_exists,
name,
source: match template {
true => "FROM TEMPLATE",
false => "IF EXISTS",
}
.to_owned(),
operands,
properties,
}))))
}

/// An alert's properties, tag list and state words, up to its IF or, from a template, its end.
fn parse_alert_properties(&mut self, template: bool) -> Result<Vec<ObjectProperty>> {
let mut properties: Vec<ObjectProperty> = Vec::new();
while !self.is_at_end()
&& !self.check(TokenType::Semicolon)
&& !(!template && self.check(TokenType::If))
{
if self.check(TokenType::With) && !self.check_next(TokenType::Eq) {
self.skip();
if !self.check_identifier("TAG") {
return Err(self.parse_error("Expected TAG after WITH"));
}
}
if let Some(state) = ALERT_STATES
.iter()
.find(|word| self.check_identifier(word) && !self.check_next(TokenType::Eq))
{
self.skip();
properties.push(property(state, None, None));
continue;
}
properties.extend(self.parse_create_property_item()?);
}
Ok(properties)
}

/// `IF ( EXISTS ( <condition> ) ) THEN <action>`, as the condition and the action.
fn parse_alert_condition_and_action(&mut self) -> Result<Vec<Expression>> {
self.expect_token(TokenType::If)?;
self.expect_token(TokenType::LParen)?;
self.expect_token(TokenType::Exists)?;
self.expect_token(TokenType::LParen)?;
if self.check(TokenType::RParen) {
return Err(self.parse_error("Expected the alert's condition"));
}
let condition = self.parse_statement()?;
self.expect_token(TokenType::RParen)?;
self.expect_token(TokenType::RParen)?;
self.expect_alter_words(&[&["THEN"]])?;
if self.is_at_end() || self.check(TokenType::Semicolon) {
return Err(self.parse_error("Expected the alert's action"));
}
Ok(vec![condition, self.parse_statement()?])
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//! Parser ddl: Snowflake CREATE EXTERNAL LISTING and ORGANIZATION LISTING, for an optional share or application package, AS a manifest or FROM its stage location, and CREATE STREAMLIT from an optional source, checked against their grammars.

use crate::parser::_helpers::ddl::snowflake_alter::property;
use crate::parser::models::Parser;
use sqlquack_syntax::expressions::schema_objects::models::{CreateObject, ObjectProperty};
use sqlquack_syntax::expressions::tree::models::Expression;
use sqlquack_syntax::tokens::models::TokenType;
use sqlquack_syntax::tokens::types::Result;

/// The listing kinds this parser reads; a bare LISTING keeps its own path.
const LISTING_KINDS: [&[&str]; 2] = [&["EXTERNAL", "LISTING"], &["ORGANIZATION", "LISTING"]];

impl Parser {
/// A Snowflake CREATE EXTERNAL or ORGANIZATION LISTING; None for any other CREATE and outside the faithful Snowflake modes.
pub(crate) fn parse_snowflake_create_listing(
&mut self,
or_replace: bool,
or_alter: bool,
) -> Result<Option<Expression>> {
if !self.applies_snowflake_context_rules() {
return Ok(None);
}
let Some(kind) = LISTING_KINDS.iter().find(|kind| self.check_text_seq(kind)) else {
return Ok(None);
};
self.match_text_seq(kind);
let if_not_exists = self.check(TokenType::If);
if if_not_exists
&& !self.match_keywords(&[TokenType::If, TokenType::Not, TokenType::Exists])
{
return Err(self.parse_error("Expected IF NOT EXISTS"));
}
let name = self.parse_object_name()?;
let mut properties: Vec<ObjectProperty> = Vec::new();
if self.match_text_seq(&["SHARE"]) {
let share = Expression::Table(Box::new(self.parse_object_name()?));
properties.push(property("SHARE", None, Some(share)));
} else if self.match_text_seq(&["APPLICATION", "PACKAGE"]) {
let package = Expression::Table(Box::new(self.parse_object_name()?));
properties.push(property("APPLICATION PACKAGE", None, Some(package)));
}
let from = !self.match_token(TokenType::As);
if from {
self.expect_token(TokenType::From)?;
}
let manifest = match from && self.is_stage_path() {
true => self.parse_stage_path()?,
false => self.parse_execute_specification()?,
};
while !self.is_at_end() && !self.check(TokenType::Semicolon) {
properties.extend(self.parse_create_property_item()?);
}
Ok(Some(Expression::CreateObject(Box::new(CreateObject {
kind: kind.join(" "),
modifier: None,
or_replace,
or_alter,
if_not_exists,
name,
source: match from {
true => "FROM",
false => "AS",
}
.to_owned(),
operands: vec![manifest],
properties,
}))))
}

/// A Snowflake CREATE STREAMLIT: a name, an optional FROM <source location>, then its properties; None for any other CREATE and outside the faithful Snowflake modes.
pub(crate) fn parse_snowflake_create_streamlit(
&mut self,
or_replace: bool,
or_alter: bool,
) -> Result<Option<Expression>> {
if !self.applies_snowflake_context_rules() || !self.match_text_seq(&["STREAMLIT"]) {
return Ok(None);
}
let if_not_exists = self.check(TokenType::If);
if if_not_exists
&& !self.match_keywords(&[TokenType::If, TokenType::Not, TokenType::Exists])
{
return Err(self.parse_error("Expected IF NOT EXISTS"));
}
let name = self.parse_object_name()?;
let mut operands: Vec<Expression> = Vec::new();
if self.match_token(TokenType::From) {
operands.push(match self.is_stage_path() {
true => self.parse_stage_path()?,
false => self.parse_string_value()?,
});
}
let mut properties: Vec<ObjectProperty> = Vec::new();
while !self.is_at_end() && !self.check(TokenType::Semicolon) {
properties.extend(self.parse_create_property_item()?);
}
Ok(Some(Expression::CreateObject(Box::new(CreateObject {
kind: "STREAMLIT".to_owned(),
modifier: None,
or_replace,
or_alter,
if_not_exists,
name,
source: String::new(),
operands,
properties,
}))))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ impl Parser {
if let Some(view) = self.parse_snowflake_create_semantic_view(or_replace, or_alter)? {
return Ok(Some(view));
}
if let Some(set) = self.parse_snowflake_create_set(or_replace, or_alter)? {
return Ok(Some(set));
}
if let Some(service) = self.parse_snowflake_create_search_service(or_replace, or_alter)? {
return Ok(Some(service));
}
if let Some(service) = self.parse_snowflake_create_service(or_replace, or_alter)? {
return Ok(Some(service));
}
if let Some(listing) = self.parse_snowflake_create_listing(or_replace, or_alter)? {
return Ok(Some(listing));
}
if let Some(app) = self.parse_snowflake_create_streamlit(or_replace, or_alter)? {
return Ok(Some(app));
}
if let Some(alert) = self.parse_snowflake_create_alert(or_replace, or_alter)? {
return Ok(Some(alert));
}
if let Some(rule) = self.parse_snowflake_create_movement_rule(or_replace, or_alter)? {
return Ok(Some(rule));
}
if self.check(TokenType::Alter) || self.check(TokenType::String) {
return Err(self.parse_error("Expected an object kind after CREATE"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//! Parser ddl: Snowflake CREATE DATA MOVEMENT RULE, with its type, MAX_ROWS body and comment, checked against its grammar.

use crate::parser::_helpers::ddl::snowflake_alter::property;
use crate::parser::models::Parser;
use sqlquack_syntax::expressions::schema_objects::models::{CreateObject, ObjectProperty};
use sqlquack_syntax::expressions::tree::models::Expression;
use sqlquack_syntax::tokens::models::TokenType;
use sqlquack_syntax::tokens::types::Result;

impl Parser {
/// A Snowflake CREATE DATA MOVEMENT RULE; None for any other CREATE and outside the faithful Snowflake modes.
pub(crate) fn parse_snowflake_create_movement_rule(
&mut self,
or_replace: bool,
or_alter: bool,
) -> Result<Option<Expression>> {
if !self.applies_snowflake_context_rules()
|| !self.match_text_seq(&["DATA", "MOVEMENT", "RULE"])
{
return Ok(None);
}
let if_not_exists = self.check(TokenType::If);
if if_not_exists
&& !self.match_keywords(&[TokenType::If, TokenType::Not, TokenType::Exists])
{
return Err(self.parse_error("Expected IF NOT EXISTS"));
}
let name = self.parse_object_name()?;
let mut properties: Vec<ObjectProperty> = Vec::new();
while !self.is_at_end() && !self.check(TokenType::Semicolon) {
if self.check_identifier("MAX_ROWS") && self.check_next(TokenType::As) {
properties.push(self.parse_max_rows_body()?);
continue;
}
if self.check(TokenType::Comment) && self.check_next(TokenType::Eq) {
self.skip();
self.skip();
properties.push(property("COMMENT", None, Some(self.parse_string_value()?)));
continue;
}
properties.extend(self.parse_create_property_item()?);
}
Ok(Some(Expression::CreateObject(Box::new(CreateObject {
kind: "DATA MOVEMENT RULE".to_owned(),
modifier: None,
or_replace,
or_alter,
if_not_exists,
name,
source: String::new(),
operands: Vec::new(),
properties,
}))))
}
}
Loading
Loading