Skip to content

Commit

Permalink
set_tblproperties (#1151)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanlehto authored Mar 1, 2024
1 parent 10cc54e commit a511c47
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
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 @@ -5124,6 +5124,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 @@ -5385,10 +5399,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 @@ -3539,6 +3539,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

0 comments on commit a511c47

Please sign in to comment.