Skip to content
Open
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
57 changes: 57 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4536,6 +4536,14 @@ pub enum Statement {
comment: Option<String>,
},
/// ```sql
/// CREATE [ OR REPLACE ] WAREHOUSE [ IF NOT EXISTS ] <name>
/// [ [ WITH ] <property> = <value> [ ... ] ]
/// ```
/// Snowflake-specific statement to create a virtual warehouse.
///
/// See <https://docs.snowflake.com/en/sql-reference/sql/create-warehouse>
CreateWarehouse(CreateWarehouse),
/// ```sql
/// ASSERT <condition> [AS <message>]
/// ```
Assert {
Expand Down Expand Up @@ -6261,6 +6269,7 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::CreateWarehouse(s) => write!(f, "{s}"),
Statement::CopyIntoSnowflake {
kind,
into,
Expand Down Expand Up @@ -8579,6 +8588,8 @@ pub enum ObjectType {
User,
/// A stream.
Stream,
/// A warehouse.
Warehouse,
}

impl fmt::Display for ObjectType {
Expand All @@ -8597,6 +8608,7 @@ impl fmt::Display for ObjectType {
ObjectType::Type => "TYPE",
ObjectType::User => "USER",
ObjectType::Stream => "STREAM",
ObjectType::Warehouse => "WAREHOUSE",
})
}
}
Expand Down Expand Up @@ -11602,6 +11614,45 @@ impl fmt::Display for CreateUser {
}
}

/// ```sql
/// CREATE [ OR REPLACE ] WAREHOUSE [ IF NOT EXISTS ] <name>
/// [ [ WITH ] <property> = <value> [ ... ] ]
/// ```
/// Snowflake-specific statement to create a virtual warehouse.
///
/// See <https://docs.snowflake.com/en/sql-reference/sql/create-warehouse>
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct CreateWarehouse {
/// `OR REPLACE` flag.
pub or_replace: bool,
/// `IF NOT EXISTS` flag.
pub if_not_exists: bool,
/// Warehouse name.
pub name: ObjectName,
/// Warehouse properties and parameters (e.g. `WAREHOUSE_SIZE = 'XSMALL'`).
pub options: KeyValueOptions,
}

impl fmt::Display for CreateWarehouse {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "CREATE")?;
if self.or_replace {
write!(f, " OR REPLACE")?;
}
write!(f, " WAREHOUSE")?;
if self.if_not_exists {
write!(f, " IF NOT EXISTS")?;
}
write!(f, " {}", self.name)?;
if !self.options.options.is_empty() {
write!(f, " {}", self.options)?;
}
Ok(())
}
}

/// Modifies the properties of a user
///
/// [Snowflake Syntax:](https://docs.snowflake.com/en/sql-reference/sql/alter-user)
Expand Down Expand Up @@ -12473,6 +12524,12 @@ impl From<CreateUser> for Statement {
}
}

impl From<CreateWarehouse> for Statement {
fn from(c: CreateWarehouse) -> Self {
Self::CreateWarehouse(c)
}
}

impl From<VacuumStatement> for Statement {
fn from(v: VacuumStatement) -> Self {
Self::Vacuum(v)
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ impl Spanned for Statement {
Statement::CreateMacro { .. } => Span::empty(),
Statement::CreateStage { .. } => Span::empty(),
Statement::CreateFileFormat { .. } => Span::empty(),
Statement::CreateWarehouse(..) => Span::empty(),
Statement::Assert { .. } => Span::empty(),
Statement::Grant { .. } => Span::empty(),
Statement::Deny { .. } => Span::empty(),
Expand Down
24 changes: 22 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5268,9 +5268,11 @@ impl<'a> Parser<'a> {
self.parse_create_secret(or_replace, temporary, persistent)
} else if self.parse_keyword(Keyword::USER) {
self.parse_create_user(or_replace).map(Into::into)
} else if self.parse_keyword(Keyword::WAREHOUSE) {
self.parse_create_warehouse(or_replace).map(Into::into)
} else if or_replace {
self.expected_ref(
"[EXTERNAL] TABLE or [MATERIALIZED] VIEW or FUNCTION after CREATE OR REPLACE",
"[EXTERNAL] TABLE or [MATERIALIZED] VIEW or FUNCTION or WAREHOUSE after CREATE OR REPLACE",
self.peek_token_ref(),
)
} else if self.parse_keyword(Keyword::EXTENSION) {
Expand Down Expand Up @@ -5415,6 +5417,22 @@ impl<'a> Parser<'a> {
})
}

/// Parse a `CREATE WAREHOUSE` statement.
///
/// See <https://docs.snowflake.com/en/sql-reference/sql/create-warehouse>
fn parse_create_warehouse(&mut self, or_replace: bool) -> Result<CreateWarehouse, ParserError> {
let if_not_exists = self.parse_keywords(&[Keyword::IF, Keyword::NOT, Keyword::EXISTS]);
let name = self.parse_object_name(false)?;
let _ = self.parse_keyword(Keyword::WITH);
let options = self.parse_key_value_options(false, &[])?;
Ok(CreateWarehouse {
or_replace,
if_not_exists,
name,
options,
})
}

/// See [DuckDB Docs](https://duckdb.org/docs/sql/statements/create_secret.html) for more details.
pub fn parse_create_secret(
&mut self,
Expand Down Expand Up @@ -7566,6 +7584,8 @@ impl<'a> Parser<'a> {
ObjectType::User
} else if self.parse_keyword(Keyword::STREAM) {
ObjectType::Stream
} else if self.parse_keyword(Keyword::WAREHOUSE) {
ObjectType::Warehouse
} else if self.parse_keyword(Keyword::FUNCTION) {
return self.parse_drop_function().map(Into::into);
} else if self.parse_keyword(Keyword::POLICY) {
Expand Down Expand Up @@ -7593,7 +7613,7 @@ impl<'a> Parser<'a> {
};
} else {
return self.expected_ref(
"COLLATION, CONNECTOR, DATABASE, EXTENSION, FUNCTION, INDEX, OPERATOR, POLICY, PROCEDURE, ROLE, SCHEMA, SECRET, SEQUENCE, STAGE, TABLE, TRIGGER, TYPE, VIEW, MATERIALIZED VIEW or USER after DROP",
"COLLATION, CONNECTOR, DATABASE, EXTENSION, FUNCTION, INDEX, OPERATOR, POLICY, PROCEDURE, ROLE, SCHEMA, SECRET, SEQUENCE, STAGE, TABLE, TRIGGER, TYPE, VIEW, MATERIALIZED VIEW, USER or WAREHOUSE after DROP",
self.peek_token_ref(),
);
};
Expand Down
13 changes: 13 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8839,6 +8839,19 @@ fn parse_drop_user() {
verified_stmt("DROP USER IF EXISTS u1");
}

#[test]
fn parse_create_warehouse() {
verified_stmt("CREATE WAREHOUSE my_wh");
verified_stmt("CREATE OR REPLACE WAREHOUSE IF NOT EXISTS my_wh");
verified_stmt("CREATE WAREHOUSE my_wh WAREHOUSE_SIZE='XSMALL' AUTO_SUSPEND=60");
one_statement_parses_to(
"CREATE WAREHOUSE my_wh WITH WAREHOUSE_SIZE = 'XSMALL' AUTO_SUSPEND = 60",
"CREATE WAREHOUSE my_wh WAREHOUSE_SIZE='XSMALL' AUTO_SUSPEND=60",
);
verified_stmt("DROP WAREHOUSE my_wh");
verified_stmt("DROP WAREHOUSE IF EXISTS my_wh");
}

#[test]
fn parse_invalid_subquery_without_parens() {
let res = parse_sql_statements("SELECT SELECT 1 FROM bar WHERE 1=1 FROM baz");
Expand Down
Loading