Skip to content

Snowflake trailing options in CREATE TABLE #1931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
10 changes: 10 additions & 0 deletions src/ast/helpers/stmt_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,16 @@ impl CreateTableBuilder {
self
}

/// Returns true if information on the structure of the table
/// to be created was provided to the builder. If not, the
/// statement is invalid.
pub fn has_schema_info(&self) -> bool {
!self.columns.is_empty()
|| self.query.is_some()
|| self.like.is_some()
|| self.clone.is_some()
}

pub fn build(self) -> Statement {
Statement::CreateTable(CreateTable {
or_replace: self.or_replace,
Expand Down
7 changes: 2 additions & 5 deletions src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,17 +452,14 @@ pub fn parse_create_table(
Keyword::AS => {
let query = parser.parse_query()?;
builder = builder.query(Some(query));
break;
}
Keyword::CLONE => {
let clone = parser.parse_object_name(false).ok();
builder = builder.clone_clause(clone);
break;
}
Keyword::LIKE => {
let like = parser.parse_object_name(false).ok();
builder = builder.like(like);
break;
}
Keyword::CLUSTER => {
parser.expect_keyword_is(Keyword::BY)?;
Expand Down Expand Up @@ -588,7 +585,7 @@ pub fn parse_create_table(
builder = builder.columns(columns).constraints(constraints);
}
Token::EOF => {
if builder.columns.is_empty() {
if !builder.has_schema_info() {
return Err(ParserError::ParserError(
"unexpected end of input".to_string(),
));
Expand All @@ -597,7 +594,7 @@ pub fn parse_create_table(
break;
}
Token::SemiColon => {
if builder.columns.is_empty() {
if !builder.has_schema_info() {
return Err(ParserError::ParserError(
"unexpected end of input".to_string(),
));
Expand Down
15 changes: 15 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,21 @@ fn test_snowflake_create_iceberg_table_without_location() {
);
}

#[test]
fn test_snowflake_create_table_trailing_options() {
snowflake()
.parse_sql_statements(
"CREATE TEMP TABLE dst AS (SELECT * FROM src) ON COMMIT PRESERVE ROWS",
)
.unwrap();
snowflake()
.parse_sql_statements("CREATE TEMP TABLE tbl LIKE customers ON COMMIT PRESERVE ROWS;")
.unwrap();
snowflake()
.parse_sql_statements("CREATE TEMP TABLE tbl CLONE customers ON COMMIT PRESERVE ROWS;")
.unwrap();
}

#[test]
fn parse_sf_create_or_replace_view_with_comment_missing_equal() {
assert!(snowflake_and_generic()
Expand Down
Loading