From 1b976673fea14376a5963c48ad09ac2af47b8b96 Mon Sep 17 00:00:00 2001 From: jonathanlehto Date: Thu, 29 Feb 2024 09:39:29 -0500 Subject: [PATCH 1/4] set_tblproperties --- src/ast/ddl.rs | 9 +++++++++ src/parser/mod.rs | 24 ++++++++++++++++++++---- tests/sqlparser_common.rs | 17 +++++++++++++++++ 3 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index 7d87d88de2..9e3137d944 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -143,6 +143,8 @@ pub enum AlterTableOperation { /// /// Note: this is Snowflake specific SwapWith { table_name: ObjectName }, + /// 'SET TBLPROPERTIES ( { property_key [ = ] property_val } [, ...] )' + SetTblProperties { table_properties: Vec }, } #[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] @@ -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) + ) + } } } } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 36ac2fd28c..d89a239749 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -4862,6 +4862,17 @@ impl<'a> Parser<'a> { } } + pub fn parse_options_with_keywords(&mut self, keywords: &[Keyword]) -> Result, 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 { if self.parse_keyword(Keyword::BTREE) { Ok(IndexType::BTree) @@ -5123,10 +5134,15 @@ 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 = self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?; + if options.len() > 0 { + 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) } diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index fcacb34596..81a59052c9 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -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] From c78cebfe0a09fbb3bca0b0ca54e13c013eb5c3a7 Mon Sep 17 00:00:00 2001 From: jonathanlehto Date: Thu, 29 Feb 2024 15:29:58 -0500 Subject: [PATCH 2/4] fixing lint and code style errors --- src/parser/mod.rs | 12 +++++++++--- tests/sqlparser_common.rs | 13 ++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index d89a239749..dc0f5a407f 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -4862,7 +4862,10 @@ impl<'a> Parser<'a> { } } - pub fn parse_options_with_keywords(&mut self, keywords: &[Keyword]) -> Result, ParserError> { + pub fn parse_options_with_keywords( + &mut self, + keywords: &[Keyword], + ) -> Result, ParserError> { if self.parse_keywords(keywords) { self.expect_token(&Token::LParen)?; let options = self.parse_comma_separated(Parser::parse_sql_option)?; @@ -5134,9 +5137,12 @@ impl<'a> Parser<'a> { let table_name = self.parse_object_name(false)?; AlterTableOperation::SwapWith { table_name } } else { - let options: Vec = self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?; + let options: Vec = + self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?; if options.len() > 0 { - AlterTableOperation::SetTblProperties { table_properties: options } + AlterTableOperation::SetTblProperties { + table_properties: options + } } else { return self.expected( "ADD, RENAME, PARTITION, SWAP, DROP, or SET TBLPROPERTIES after ALTER TABLE", diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 81a59052c9..a9ccec4b58 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -3500,15 +3500,14 @@ fn parse_alter_table() { let set_table_properties = "ALTER TABLE tab SET TBLPROPERTIES('classification' = 'parquet')"; match alter_table_op(verified_stmt(set_table_properties)) { - AlterTableOperation::SetTblProperties { - 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() - )), + name: Ident { + value: "classification".to_string(), + quote_style: Some('\'') + }, + value: Expr::Value(Value::SingleQuotedString("parquet".to_string())), } ]); } From f55618d808173fdb15bfb3a4b71282b7dd88e1d3 Mon Sep 17 00:00:00 2001 From: jonathanlehto Date: Thu, 29 Feb 2024 15:31:26 -0500 Subject: [PATCH 3/4] fixing lint error --- src/parser/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index dc0f5a407f..065767b75a 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -5139,7 +5139,7 @@ impl<'a> Parser<'a> { } else { let options: Vec = self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?; - if options.len() > 0 { + if !options.is_empty() { AlterTableOperation::SetTblProperties { table_properties: options } From 6ec4b9059d858ca1acd312eff87bde3f828c5f58 Mon Sep 17 00:00:00 2001 From: jonathanlehto Date: Thu, 29 Feb 2024 15:34:32 -0500 Subject: [PATCH 4/4] fixing codestyle errors --- src/parser/mod.rs | 2 +- tests/sqlparser_common.rs | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 065767b75a..b766cefbb5 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -5141,7 +5141,7 @@ impl<'a> Parser<'a> { self.parse_options_with_keywords(&[Keyword::SET, Keyword::TBLPROPERTIES])?; if !options.is_empty() { AlterTableOperation::SetTblProperties { - table_properties: options + table_properties: options, } } else { return self.expected( diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index a9ccec4b58..b4c743cca4 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -3501,15 +3501,16 @@ fn parse_alter_table() { 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 { + assert_eq!( + table_properties, + [SqlOption { name: Ident { value: "classification".to_string(), quote_style: Some('\'') }, value: Expr::Value(Value::SingleQuotedString("parquet".to_string())), - } - ]); + }], + ); } _ => unreachable!(), }