Skip to content

Commit 84348d4

Browse files
author
Aleksei Piianin
authored
Snowflake: support of views column comment (#1441)
1 parent 8ccb87a commit 84348d4

File tree

5 files changed

+56
-17
lines changed

5 files changed

+56
-17
lines changed

src/ast/ddl.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,7 @@ impl fmt::Display for ColumnDef {
10401040
/// ```sql
10411041
/// name
10421042
/// age OPTIONS(description = "age column", tag = "prod")
1043+
/// amount COMMENT 'The total amount for the order line'
10431044
/// created_at DateTime64
10441045
/// ```
10451046
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
@@ -1048,7 +1049,7 @@ impl fmt::Display for ColumnDef {
10481049
pub struct ViewColumnDef {
10491050
pub name: Ident,
10501051
pub data_type: Option<DataType>,
1051-
pub options: Option<Vec<SqlOption>>,
1052+
pub options: Option<Vec<ColumnOption>>,
10521053
}
10531054

10541055
impl fmt::Display for ViewColumnDef {
@@ -1058,11 +1059,7 @@ impl fmt::Display for ViewColumnDef {
10581059
write!(f, " {}", data_type)?;
10591060
}
10601061
if let Some(options) = self.options.as_ref() {
1061-
write!(
1062-
f,
1063-
" OPTIONS({})",
1064-
display_comma_separated(options.as_slice())
1065-
)?;
1062+
write!(f, " {}", display_comma_separated(options.as_slice()))?;
10661063
}
10671064
Ok(())
10681065
}

src/ast/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -3930,19 +3930,19 @@ impl fmt::Display for Statement {
39303930
.map(|to| format!(" TO {to}"))
39313931
.unwrap_or_default()
39323932
)?;
3933+
if !columns.is_empty() {
3934+
write!(f, " ({})", display_comma_separated(columns))?;
3935+
}
3936+
if matches!(options, CreateTableOptions::With(_)) {
3937+
write!(f, " {options}")?;
3938+
}
39333939
if let Some(comment) = comment {
39343940
write!(
39353941
f,
39363942
" COMMENT = '{}'",
39373943
value::escape_single_quote_string(comment)
39383944
)?;
39393945
}
3940-
if matches!(options, CreateTableOptions::With(_)) {
3941-
write!(f, " {options}")?;
3942-
}
3943-
if !columns.is_empty() {
3944-
write!(f, " ({})", display_comma_separated(columns))?;
3945-
}
39463946
if !cluster_by.is_empty() {
39473947
write!(f, " CLUSTER BY ({})", display_comma_separated(cluster_by))?;
39483948
}

src/parser/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -8361,11 +8361,14 @@ impl<'a> Parser<'a> {
83618361
/// Parses a column definition within a view.
83628362
fn parse_view_column(&mut self) -> Result<ViewColumnDef, ParserError> {
83638363
let name = self.parse_identifier(false)?;
8364-
let options = if dialect_of!(self is BigQueryDialect | GenericDialect)
8365-
&& self.parse_keyword(Keyword::OPTIONS)
8364+
let options = if (dialect_of!(self is BigQueryDialect | GenericDialect)
8365+
&& self.parse_keyword(Keyword::OPTIONS))
8366+
|| (dialect_of!(self is SnowflakeDialect | GenericDialect)
8367+
&& self.parse_keyword(Keyword::COMMENT))
83668368
{
83678369
self.prev_token();
8368-
Some(self.parse_options(Keyword::OPTIONS)?)
8370+
self.parse_optional_column_option()?
8371+
.map(|option| vec![option])
83698372
} else {
83708373
None
83718374
};

tests/sqlparser_bigquery.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,10 @@ fn parse_create_view_with_options() {
272272
ViewColumnDef {
273273
name: Ident::new("age"),
274274
data_type: None,
275-
options: Some(vec![SqlOption::KeyValue {
275+
options: Some(vec![ColumnOption::Options(vec![SqlOption::KeyValue {
276276
key: Ident::new("description"),
277277
value: Expr::Value(Value::DoubleQuotedString("field age".to_string())),
278-
}])
278+
}])]),
279279
},
280280
],
281281
columns

tests/sqlparser_snowflake.rs

+39
Original file line numberDiff line numberDiff line change
@@ -2405,3 +2405,42 @@ fn parse_use() {
24052405
);
24062406
}
24072407
}
2408+
2409+
#[test]
2410+
fn view_comment_option_should_be_after_column_list() {
2411+
for sql in [
2412+
"CREATE OR REPLACE VIEW v (a) COMMENT = 'Comment' AS SELECT a FROM t",
2413+
"CREATE OR REPLACE VIEW v (a COMMENT 'a comment', b, c COMMENT 'c comment') COMMENT = 'Comment' AS SELECT a FROM t",
2414+
"CREATE OR REPLACE VIEW v (a COMMENT 'a comment', b, c COMMENT 'c comment') WITH (foo = bar) COMMENT = 'Comment' AS SELECT a FROM t",
2415+
] {
2416+
snowflake_and_generic()
2417+
.verified_stmt(sql);
2418+
}
2419+
}
2420+
2421+
#[test]
2422+
fn parse_view_column_descriptions() {
2423+
let sql = "CREATE OR REPLACE VIEW v (a COMMENT 'Comment', b) AS SELECT a, b FROM table1";
2424+
2425+
match snowflake_and_generic().verified_stmt(sql) {
2426+
Statement::CreateView { name, columns, .. } => {
2427+
assert_eq!(name.to_string(), "v");
2428+
assert_eq!(
2429+
columns,
2430+
vec![
2431+
ViewColumnDef {
2432+
name: Ident::new("a"),
2433+
data_type: None,
2434+
options: Some(vec![ColumnOption::Comment("Comment".to_string())]),
2435+
},
2436+
ViewColumnDef {
2437+
name: Ident::new("b"),
2438+
data_type: None,
2439+
options: None,
2440+
}
2441+
]
2442+
);
2443+
}
2444+
_ => unreachable!(),
2445+
};
2446+
}

0 commit comments

Comments
 (0)