Skip to content
Draft
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
37 changes: 37 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4536,6 +4536,23 @@ 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 {
/// `OR REPLACE` flag.
or_replace: bool,
/// `IF NOT EXISTS` flag.
if_not_exists: bool,
/// Warehouse name.
name: ObjectName,
/// Warehouse properties and parameters (e.g. `WAREHOUSE_SIZE = 'XSMALL'`).
options: KeyValueOptions,
},
/// ```sql
/// ASSERT <condition> [AS <message>]
/// ```
Assert {
Expand Down Expand Up @@ -6261,6 +6278,23 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::CreateWarehouse {
or_replace,
if_not_exists,
name,
options,
} => {
write!(
f,
"CREATE {or_replace}WAREHOUSE {if_not_exists}{name}",
or_replace = if *or_replace { "OR REPLACE " } else { "" },
if_not_exists = if *if_not_exists { "IF NOT EXISTS " } else { "" },
)?;
if !options.options.is_empty() {
write!(f, " {options}")?;
}
Ok(())
}
Statement::CopyIntoSnowflake {
kind,
into,
Expand Down Expand Up @@ -8579,6 +8613,8 @@ pub enum ObjectType {
User,
/// A stream.
Stream,
/// A warehouse.
Warehouse,
}

impl fmt::Display for ObjectType {
Expand All @@ -8597,6 +8633,7 @@ impl fmt::Display for ObjectType {
ObjectType::Type => "TYPE",
ObjectType::User => "USER",
ObjectType::Stream => "STREAM",
ObjectType::Warehouse => "WAREHOUSE",
})
}
}
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)
} 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<Statement, 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(Statement::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