Skip to content

Commit 375742d

Browse files
author
Aleksei Piianin
authored
ClickHouse: create view with fields and data types (#1292)
1 parent 029a999 commit 375742d

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

src/ast/ddl.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -815,7 +815,7 @@ impl fmt::Display for ColumnDef {
815815
///
816816
/// Syntax
817817
/// ```markdown
818-
/// <name> [OPTIONS(option, ...)]
818+
/// <name> [data_type][OPTIONS(option, ...)]
819819
///
820820
/// option: <name> = <value>
821821
/// ```
@@ -824,18 +824,23 @@ impl fmt::Display for ColumnDef {
824824
/// ```sql
825825
/// name
826826
/// age OPTIONS(description = "age column", tag = "prod")
827+
/// created_at DateTime64
827828
/// ```
828829
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
829830
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
830831
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
831832
pub struct ViewColumnDef {
832833
pub name: Ident,
834+
pub data_type: Option<DataType>,
833835
pub options: Option<Vec<SqlOption>>,
834836
}
835837

836838
impl fmt::Display for ViewColumnDef {
837839
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
838840
write!(f, "{}", self.name)?;
841+
if let Some(data_type) = self.data_type.as_ref() {
842+
write!(f, " {}", data_type)?;
843+
}
839844
if let Some(options) = self.options.as_ref() {
840845
write!(
841846
f,

src/parser/mod.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7282,7 +7282,16 @@ impl<'a> Parser<'a> {
72827282
} else {
72837283
None
72847284
};
7285-
Ok(ViewColumnDef { name, options })
7285+
let data_type = if dialect_of!(self is ClickHouseDialect) {
7286+
Some(self.parse_data_type()?)
7287+
} else {
7288+
None
7289+
};
7290+
Ok(ViewColumnDef {
7291+
name,
7292+
data_type,
7293+
options,
7294+
})
72867295
}
72877296

72887297
/// Parse a parenthesized comma-separated list of unqualified, possibly quoted identifiers

tests/sqlparser_bigquery.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,10 +261,12 @@ fn parse_create_view_with_options() {
261261
vec![
262262
ViewColumnDef {
263263
name: Ident::new("name"),
264+
data_type: None,
264265
options: None,
265266
},
266267
ViewColumnDef {
267268
name: Ident::new("age"),
269+
data_type: None,
268270
options: Some(vec![SqlOption {
269271
name: Ident::new("description"),
270272
value: Expr::Value(Value::DoubleQuotedString("field age".to_string())),

tests/sqlparser_clickhouse.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,47 @@ fn parse_create_table() {
220220
);
221221
}
222222

223+
#[test]
224+
fn parse_create_view_with_fields_data_types() {
225+
match clickhouse().verified_stmt(r#"CREATE VIEW v (i "int", f "String") AS SELECT * FROM t"#) {
226+
Statement::CreateView { name, columns, .. } => {
227+
assert_eq!(name, ObjectName(vec!["v".into()]));
228+
assert_eq!(
229+
columns,
230+
vec![
231+
ViewColumnDef {
232+
name: "i".into(),
233+
data_type: Some(DataType::Custom(
234+
ObjectName(vec![Ident {
235+
value: "int".into(),
236+
quote_style: Some('"')
237+
}]),
238+
vec![]
239+
)),
240+
options: None
241+
},
242+
ViewColumnDef {
243+
name: "f".into(),
244+
data_type: Some(DataType::Custom(
245+
ObjectName(vec![Ident {
246+
value: "String".into(),
247+
quote_style: Some('"')
248+
}]),
249+
vec![]
250+
)),
251+
options: None
252+
},
253+
]
254+
);
255+
}
256+
_ => unreachable!(),
257+
}
258+
259+
clickhouse()
260+
.parse_sql_statements(r#"CREATE VIEW v (i, f) AS SELECT * FROM t"#)
261+
.expect_err("CREATE VIEW with fields and without data types should be invalid");
262+
}
263+
223264
#[test]
224265
fn parse_double_equal() {
225266
clickhouse().one_statement_parses_to(

tests/sqlparser_common.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6319,6 +6319,7 @@ fn parse_create_view_with_columns() {
63196319
.into_iter()
63206320
.map(|name| ViewColumnDef {
63216321
name,
6322+
data_type: None,
63226323
options: None
63236324
})
63246325
.collect::<Vec<_>>()

0 commit comments

Comments
 (0)