Skip to content

Commit 1bbc05c

Browse files
Snowflake: support multiple column options in CREATE VIEW (#1891)
1 parent b2ab006 commit 1bbc05c

File tree

10 files changed

+98
-40
lines changed

10 files changed

+98
-40
lines changed

src/ast/ddl.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,24 @@ impl fmt::Display for ColumnDef {
14261426
pub struct ViewColumnDef {
14271427
pub name: Ident,
14281428
pub data_type: Option<DataType>,
1429-
pub options: Option<Vec<ColumnOption>>,
1429+
pub options: Option<ColumnOptions>,
1430+
}
1431+
1432+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
1433+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
1434+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
1435+
pub enum ColumnOptions {
1436+
CommaSeparated(Vec<ColumnOption>),
1437+
SpaceSeparated(Vec<ColumnOption>),
1438+
}
1439+
1440+
impl ColumnOptions {
1441+
pub fn as_slice(&self) -> &[ColumnOption] {
1442+
match self {
1443+
ColumnOptions::CommaSeparated(options) => options.as_slice(),
1444+
ColumnOptions::SpaceSeparated(options) => options.as_slice(),
1445+
}
1446+
}
14301447
}
14311448

14321449
impl fmt::Display for ViewColumnDef {
@@ -1436,7 +1453,14 @@ impl fmt::Display for ViewColumnDef {
14361453
write!(f, " {}", data_type)?;
14371454
}
14381455
if let Some(options) = self.options.as_ref() {
1439-
write!(f, " {}", display_comma_separated(options.as_slice()))?;
1456+
match options {
1457+
ColumnOptions::CommaSeparated(column_options) => {
1458+
write!(f, " {}", display_comma_separated(column_options.as_slice()))?;
1459+
}
1460+
ColumnOptions::SpaceSeparated(column_options) => {
1461+
write!(f, " {}", display_separated(column_options.as_slice(), " "))?
1462+
}
1463+
}
14401464
}
14411465
Ok(())
14421466
}

src/ast/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,14 @@ pub use self::ddl::{
6161
AlterColumnOperation, AlterConnectorOwner, AlterIndexOperation, AlterPolicyOperation,
6262
AlterTableAlgorithm, AlterTableLock, AlterTableOperation, AlterType, AlterTypeAddValue,
6363
AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue,
64-
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnPolicy, ColumnPolicyProperty,
65-
ConstraintCharacteristics, CreateConnector, CreateDomain, CreateFunction, Deduplicate,
66-
DeferrableInitial, DropBehavior, GeneratedAs, GeneratedExpressionMode, IdentityParameters,
67-
IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder,
68-
IndexOption, IndexType, KeyOrIndexDisplay, NullsDistinctOption, Owner, Partition,
69-
ProcedureParam, ReferentialAction, ReplicaIdentity, TableConstraint, TagsColumnOption,
70-
UserDefinedTypeCompositeAttributeDef, UserDefinedTypeRepresentation, ViewColumnDef,
64+
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy,
65+
ColumnPolicyProperty, ConstraintCharacteristics, CreateConnector, CreateDomain, CreateFunction,
66+
Deduplicate, DeferrableInitial, DropBehavior, GeneratedAs, GeneratedExpressionMode,
67+
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
68+
IdentityPropertyOrder, IndexOption, IndexType, KeyOrIndexDisplay, NullsDistinctOption, Owner,
69+
Partition, ProcedureParam, ReferentialAction, ReplicaIdentity, TableConstraint,
70+
TagsColumnOption, UserDefinedTypeCompositeAttributeDef, UserDefinedTypeRepresentation,
71+
ViewColumnDef,
7172
};
7273
pub use self::dml::{CreateIndex, CreateTable, Delete, IndexColumn, Insert};
7374
pub use self::operator::{BinaryOperator, UnaryOperator};

src/ast/spans.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
use crate::ast::query::SelectItemQualifiedWildcardKind;
18+
use crate::ast::{query::SelectItemQualifiedWildcardKind, ColumnOptions};
1919
use core::iter;
2020

2121
use crate::tokenizer::Span;
@@ -991,10 +991,13 @@ impl Spanned for ViewColumnDef {
991991
options,
992992
} = self;
993993

994-
union_spans(
995-
core::iter::once(name.span)
996-
.chain(options.iter().flat_map(|i| i.iter().map(|k| k.span()))),
997-
)
994+
name.span.union_opt(&options.as_ref().map(|o| o.span()))
995+
}
996+
}
997+
998+
impl Spanned for ColumnOptions {
999+
fn span(&self) -> Span {
1000+
union_spans(self.as_slice().iter().map(|i| i.span()))
9981001
}
9991002
}
10001003

@@ -1055,7 +1058,9 @@ impl Spanned for CreateTableOptions {
10551058
match self {
10561059
CreateTableOptions::None => Span::empty(),
10571060
CreateTableOptions::With(vec) => union_spans(vec.iter().map(|i| i.span())),
1058-
CreateTableOptions::Options(vec) => union_spans(vec.iter().map(|i| i.span())),
1061+
CreateTableOptions::Options(vec) => {
1062+
union_spans(vec.as_slice().iter().map(|i| i.span()))
1063+
}
10591064
CreateTableOptions::Plain(vec) => union_spans(vec.iter().map(|i| i.span())),
10601065
CreateTableOptions::TableProperties(vec) => union_spans(vec.iter().map(|i| i.span())),
10611066
}

src/dialect/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,10 @@ pub trait Dialect: Debug + Any {
10281028
fn supports_set_names(&self) -> bool {
10291029
false
10301030
}
1031+
1032+
fn supports_space_separated_column_options(&self) -> bool {
1033+
false
1034+
}
10311035
}
10321036

10331037
/// This represents the operators for which precedence must be defined

src/dialect/snowflake.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,10 @@ impl Dialect for SnowflakeDialect {
356356
fn get_reserved_keywords_for_select_item_operator(&self) -> &[Keyword] {
357357
&RESERVED_KEYWORDS_FOR_SELECT_ITEM_OPERATOR
358358
}
359+
360+
fn supports_space_separated_column_options(&self) -> bool {
361+
true
362+
}
359363
}
360364

361365
fn parse_file_staging_command(kw: Keyword, parser: &mut Parser) -> Result<Statement, ParserError> {

src/parser/mod.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10579,17 +10579,7 @@ impl<'a> Parser<'a> {
1057910579
/// Parses a column definition within a view.
1058010580
fn parse_view_column(&mut self) -> Result<ViewColumnDef, ParserError> {
1058110581
let name = self.parse_identifier()?;
10582-
let options = if (dialect_of!(self is BigQueryDialect | GenericDialect)
10583-
&& self.parse_keyword(Keyword::OPTIONS))
10584-
|| (dialect_of!(self is SnowflakeDialect | GenericDialect)
10585-
&& self.parse_keyword(Keyword::COMMENT))
10586-
{
10587-
self.prev_token();
10588-
self.parse_optional_column_option()?
10589-
.map(|option| vec![option])
10590-
} else {
10591-
None
10592-
};
10582+
let options = self.parse_view_column_options()?;
1059310583
let data_type = if dialect_of!(self is ClickHouseDialect) {
1059410584
Some(self.parse_data_type()?)
1059510585
} else {
@@ -10602,6 +10592,25 @@ impl<'a> Parser<'a> {
1060210592
})
1060310593
}
1060410594

10595+
fn parse_view_column_options(&mut self) -> Result<Option<ColumnOptions>, ParserError> {
10596+
let mut options = Vec::new();
10597+
loop {
10598+
let option = self.parse_optional_column_option()?;
10599+
if let Some(option) = option {
10600+
options.push(option);
10601+
} else {
10602+
break;
10603+
}
10604+
}
10605+
if options.is_empty() {
10606+
Ok(None)
10607+
} else if self.dialect.supports_space_separated_column_options() {
10608+
Ok(Some(ColumnOptions::SpaceSeparated(options)))
10609+
} else {
10610+
Ok(Some(ColumnOptions::CommaSeparated(options)))
10611+
}
10612+
}
10613+
1060510614
/// Parses a parenthesized comma-separated list of unqualified, possibly quoted identifiers.
1060610615
/// For example: `(col1, "col 2", ...)`
1060710616
pub fn parse_parenthesized_column_list(

tests/sqlparser_bigquery.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -355,14 +355,16 @@ fn parse_create_view_with_options() {
355355
ViewColumnDef {
356356
name: Ident::new("age"),
357357
data_type: None,
358-
options: Some(vec![ColumnOption::Options(vec![SqlOption::KeyValue {
359-
key: Ident::new("description"),
360-
value: Expr::Value(
361-
Value::DoubleQuotedString("field age".to_string()).with_span(
362-
Span::new(Location::new(1, 42), Location::new(1, 52))
363-
)
364-
),
365-
}])]),
358+
options: Some(ColumnOptions::CommaSeparated(vec![ColumnOption::Options(
359+
vec![SqlOption::KeyValue {
360+
key: Ident::new("description"),
361+
value: Expr::Value(
362+
Value::DoubleQuotedString("field age".to_string()).with_span(
363+
Span::new(Location::new(1, 42), Location::new(1, 52))
364+
)
365+
),
366+
}]
367+
)])),
366368
},
367369
],
368370
columns

tests/sqlparser_clickhouse.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ fn parse_create_view_with_fields_data_types() {
914914
}]),
915915
vec![]
916916
)),
917-
options: None
917+
options: None,
918918
},
919919
ViewColumnDef {
920920
name: "f".into(),
@@ -926,7 +926,7 @@ fn parse_create_view_with_fields_data_types() {
926926
}]),
927927
vec![]
928928
)),
929-
options: None
929+
options: None,
930930
},
931931
]
932932
);

tests/sqlparser_common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7990,7 +7990,7 @@ fn parse_create_view_with_columns() {
79907990
.map(|name| ViewColumnDef {
79917991
name,
79927992
data_type: None,
7993-
options: None
7993+
options: None,
79947994
})
79957995
.collect::<Vec<_>>()
79967996
);

tests/sqlparser_snowflake.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3124,7 +3124,7 @@ fn view_comment_option_should_be_after_column_list() {
31243124
"CREATE OR REPLACE VIEW v (a COMMENT 'a comment', b, c COMMENT 'c comment') COMMENT = 'Comment' AS SELECT a FROM t",
31253125
"CREATE OR REPLACE VIEW v (a COMMENT 'a comment', b, c COMMENT 'c comment') WITH (foo = bar) COMMENT = 'Comment' AS SELECT a FROM t",
31263126
] {
3127-
snowflake_and_generic()
3127+
snowflake()
31283128
.verified_stmt(sql);
31293129
}
31303130
}
@@ -3133,7 +3133,7 @@ fn view_comment_option_should_be_after_column_list() {
31333133
fn parse_view_column_descriptions() {
31343134
let sql = "CREATE OR REPLACE VIEW v (a COMMENT 'Comment', b) AS SELECT a, b FROM table1";
31353135

3136-
match snowflake_and_generic().verified_stmt(sql) {
3136+
match snowflake().verified_stmt(sql) {
31373137
Statement::CreateView { name, columns, .. } => {
31383138
assert_eq!(name.to_string(), "v");
31393139
assert_eq!(
@@ -3142,7 +3142,9 @@ fn parse_view_column_descriptions() {
31423142
ViewColumnDef {
31433143
name: Ident::new("a"),
31443144
data_type: None,
3145-
options: Some(vec![ColumnOption::Comment("Comment".to_string())]),
3145+
options: Some(ColumnOptions::SpaceSeparated(vec![ColumnOption::Comment(
3146+
"Comment".to_string()
3147+
)])),
31463148
},
31473149
ViewColumnDef {
31483150
name: Ident::new("b"),
@@ -4165,3 +4167,10 @@ fn test_snowflake_fetch_clause_syntax() {
41654167
canonical,
41664168
);
41674169
}
4170+
4171+
#[test]
4172+
fn test_snowflake_create_view_with_multiple_column_options() {
4173+
let create_view_with_tag =
4174+
r#"CREATE VIEW X (COL WITH TAG (pii='email') COMMENT 'foobar') AS SELECT * FROM Y"#;
4175+
snowflake().verified_stmt(create_view_with_tag);
4176+
}

0 commit comments

Comments
 (0)