Skip to content
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

set_tblproperties #1151

Merged
merged 4 commits into from
Mar 1, 2024
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
9 changes: 9 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,8 @@ pub enum AlterTableOperation {
///
/// Note: this is Snowflake specific <https://docs.snowflake.com/en/sql-reference/sql/alter-table>
SwapWith { table_name: ObjectName },
/// 'SET TBLPROPERTIES ( { property_key [ = ] property_val } [, ...] )'
SetTblProperties { table_properties: Vec<SqlOption> },
}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
Expand Down Expand Up @@ -283,6 +285,13 @@ impl fmt::Display for AlterTableOperation {
AlterTableOperation::SwapWith { table_name } => {
write!(f, "SWAP WITH {table_name}")
}
AlterTableOperation::SetTblProperties { table_properties } => {
write!(
f,
"SET TBLPROPERTIES({})",
display_comma_separated(table_properties)
)
}
}
}
}
Expand Down
30 changes: 26 additions & 4 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4862,6 +4862,20 @@ impl<'a> Parser<'a> {
}
}

pub fn parse_options_with_keywords(
&mut self,
keywords: &[Keyword],
) -> Result<Vec<SqlOption>, ParserError> {
if self.parse_keywords(keywords) {
self.expect_token(&Token::LParen)?;
let options = self.parse_comma_separated(Parser::parse_sql_option)?;
self.expect_token(&Token::RParen)?;
Ok(options)
} else {
Ok(vec![])
}
}

pub fn parse_index_type(&mut self) -> Result<IndexType, ParserError> {
if self.parse_keyword(Keyword::BTREE) {
Ok(IndexType::BTree)
Expand Down Expand Up @@ -5123,10 +5137,18 @@ impl<'a> Parser<'a> {
let table_name = self.parse_object_name(false)?;
AlterTableOperation::SwapWith { table_name }
} else {
return self.expected(
"ADD, RENAME, PARTITION, SWAP or DROP after ALTER TABLE",
self.peek_token(),
);
let options: Vec<SqlOption> =
self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?;
if !options.is_empty() {
AlterTableOperation::SetTblProperties {
table_properties: options,
}
} else {
return self.expected(
"ADD, RENAME, PARTITION, SWAP, DROP, or SET TBLPROPERTIES after ALTER TABLE",
self.peek_token(),
);
}
};
Ok(operation)
}
Expand Down
17 changes: 17 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3497,6 +3497,23 @@ fn parse_alter_table() {
}
_ => unreachable!(),
}

let set_table_properties = "ALTER TABLE tab SET TBLPROPERTIES('classification' = 'parquet')";
match alter_table_op(verified_stmt(set_table_properties)) {
AlterTableOperation::SetTblProperties { table_properties } => {
assert_eq!(
table_properties,
[SqlOption {
name: Ident {
value: "classification".to_string(),
quote_style: Some('\'')
},
value: Expr::Value(Value::SingleQuotedString("parquet".to_string())),
}],
);
}
_ => unreachable!(),
}
}

#[test]
Expand Down
Loading