Skip to content

Commit a511c47

Browse files
set_tblproperties (#1151)
1 parent 10cc54e commit a511c47

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

src/ast/ddl.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ pub enum AlterTableOperation {
143143
///
144144
/// Note: this is Snowflake specific <https://docs.snowflake.com/en/sql-reference/sql/alter-table>
145145
SwapWith { table_name: ObjectName },
146+
/// 'SET TBLPROPERTIES ( { property_key [ = ] property_val } [, ...] )'
147+
SetTblProperties { table_properties: Vec<SqlOption> },
146148
}
147149

148150
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
@@ -283,6 +285,13 @@ impl fmt::Display for AlterTableOperation {
283285
AlterTableOperation::SwapWith { table_name } => {
284286
write!(f, "SWAP WITH {table_name}")
285287
}
288+
AlterTableOperation::SetTblProperties { table_properties } => {
289+
write!(
290+
f,
291+
"SET TBLPROPERTIES({})",
292+
display_comma_separated(table_properties)
293+
)
294+
}
286295
}
287296
}
288297
}

src/parser/mod.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5124,6 +5124,20 @@ impl<'a> Parser<'a> {
51245124
}
51255125
}
51265126

5127+
pub fn parse_options_with_keywords(
5128+
&mut self,
5129+
keywords: &[Keyword],
5130+
) -> Result<Vec<SqlOption>, ParserError> {
5131+
if self.parse_keywords(keywords) {
5132+
self.expect_token(&Token::LParen)?;
5133+
let options = self.parse_comma_separated(Parser::parse_sql_option)?;
5134+
self.expect_token(&Token::RParen)?;
5135+
Ok(options)
5136+
} else {
5137+
Ok(vec![])
5138+
}
5139+
}
5140+
51275141
pub fn parse_index_type(&mut self) -> Result<IndexType, ParserError> {
51285142
if self.parse_keyword(Keyword::BTREE) {
51295143
Ok(IndexType::BTree)
@@ -5385,10 +5399,18 @@ impl<'a> Parser<'a> {
53855399
let table_name = self.parse_object_name(false)?;
53865400
AlterTableOperation::SwapWith { table_name }
53875401
} else {
5388-
return self.expected(
5389-
"ADD, RENAME, PARTITION, SWAP or DROP after ALTER TABLE",
5390-
self.peek_token(),
5391-
);
5402+
let options: Vec<SqlOption> =
5403+
self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?;
5404+
if !options.is_empty() {
5405+
AlterTableOperation::SetTblProperties {
5406+
table_properties: options,
5407+
}
5408+
} else {
5409+
return self.expected(
5410+
"ADD, RENAME, PARTITION, SWAP, DROP, or SET TBLPROPERTIES after ALTER TABLE",
5411+
self.peek_token(),
5412+
);
5413+
}
53925414
};
53935415
Ok(operation)
53945416
}

tests/sqlparser_common.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3539,6 +3539,23 @@ fn parse_alter_table() {
35393539
}
35403540
_ => unreachable!(),
35413541
}
3542+
3543+
let set_table_properties = "ALTER TABLE tab SET TBLPROPERTIES('classification' = 'parquet')";
3544+
match alter_table_op(verified_stmt(set_table_properties)) {
3545+
AlterTableOperation::SetTblProperties { table_properties } => {
3546+
assert_eq!(
3547+
table_properties,
3548+
[SqlOption {
3549+
name: Ident {
3550+
value: "classification".to_string(),
3551+
quote_style: Some('\'')
3552+
},
3553+
value: Expr::Value(Value::SingleQuotedString("parquet".to_string())),
3554+
}],
3555+
);
3556+
}
3557+
_ => unreachable!(),
3558+
}
35423559
}
35433560

35443561
#[test]

0 commit comments

Comments
 (0)