Skip to content

ClickHouse: create view with fields and data types #1292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ impl fmt::Display for ColumnDef {
///
/// Syntax
/// ```markdown
/// <name> [OPTIONS(option, ...)]
/// <name> [data_type][OPTIONS(option, ...)]
///
/// option: <name> = <value>
/// ```
Expand All @@ -824,18 +824,23 @@ impl fmt::Display for ColumnDef {
/// ```sql
/// name
/// age OPTIONS(description = "age column", tag = "prod")
/// created_at DateTime64
/// ```
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct ViewColumnDef {
pub name: Ident,
pub data_type: Option<DataType>,
pub options: Option<Vec<SqlOption>>,
}

impl fmt::Display for ViewColumnDef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name)?;
if let Some(data_type) = self.data_type.as_ref() {
write!(f, " {}", data_type)?;
}
if let Some(options) = self.options.as_ref() {
write!(
f,
Expand Down
11 changes: 10 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7122,7 +7122,16 @@ impl<'a> Parser<'a> {
} else {
None
};
Ok(ViewColumnDef { name, options })
let data_type = if dialect_of!(self is ClickHouseDialect) {
Some(self.parse_data_type()?)
} else {
None
};
Ok(ViewColumnDef {
name,
data_type,
options,
})
}

/// Parse a parenthesized comma-separated list of unqualified, possibly quoted identifiers
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,12 @@ fn parse_create_view_with_options() {
vec![
ViewColumnDef {
name: Ident::new("name"),
data_type: None,
options: None,
},
ViewColumnDef {
name: Ident::new("age"),
data_type: None,
options: Some(vec![SqlOption {
name: Ident::new("description"),
value: Expr::Value(Value::DoubleQuotedString("field age".to_string())),
Expand Down
41 changes: 41 additions & 0 deletions tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,47 @@ fn parse_create_table() {
);
}

#[test]
fn parse_create_view_with_fields_data_types() {
match clickhouse().verified_stmt(r#"CREATE VIEW v (i "int", f "String") AS SELECT * FROM t"#) {
Statement::CreateView { name, columns, .. } => {
assert_eq!(name, ObjectName(vec!["v".into()]));
assert_eq!(
columns,
vec![
ViewColumnDef {
name: "i".into(),
data_type: Some(DataType::Custom(
ObjectName(vec![Ident {
value: "int".into(),
quote_style: Some('"')
}]),
vec![]
)),
options: None
},
ViewColumnDef {
name: "f".into(),
data_type: Some(DataType::Custom(
ObjectName(vec![Ident {
value: "String".into(),
quote_style: Some('"')
}]),
vec![]
)),
options: None
},
]
);
}
_ => unreachable!(),
}

clickhouse()
.parse_sql_statements(r#"CREATE VIEW v (i, f) AS SELECT * FROM t"#)
.expect_err("CREATE VIEW with fields and without data types should be invalid");
}

#[test]
fn parse_double_equal() {
clickhouse().one_statement_parses_to(
Expand Down
1 change: 1 addition & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6316,6 +6316,7 @@ fn parse_create_view_with_columns() {
.into_iter()
.map(|name| ViewColumnDef {
name,
data_type: None,
options: None
})
.collect::<Vec<_>>()
Expand Down
Loading