Skip to content
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

support clickhouse parameterized views #1181

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
14 changes: 14 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,17 @@ pub enum Expr {
///
/// See <https://docs.snowflake.com/en/sql-reference/constructs/where#joins-in-the-where-clause>.
OuterJoin(Box<Expr>),

/// ClickHouse supported parameterized view
/// for example
/// ```sql
/// CREATE VIEW view AS SELECT * FROM TABLE WHERE Column1={column1:datatype1} and Column2={column2:datatype2}
/// ```
/// see doc <https://clickhouse.com/docs/en/sql-reference/statements/create/view#parameterized-view>
ParameterizedViewArg {
column: Ident,
data_type: DataType,
},
}

impl fmt::Display for CastFormat {
Expand Down Expand Up @@ -1251,6 +1262,9 @@ impl fmt::Display for Expr {
Expr::OuterJoin(expr) => {
write!(f, "{expr} (+)")
}
Expr::ParameterizedViewArg { column, data_type } => {
write!(f, "{{{}:{}}}", column, data_type)
}
}
}
}
Expand Down
34 changes: 32 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,14 +928,38 @@ impl<'a> Parser<'a> {

Ok(Statement::ReleaseSavepoint { name })
}

/// Parse clickhouse view parameter style `column1:data_type`
pub fn parse_column_with_data_type(&mut self) -> Result<Expr, ParserError> {
Lordworms marked this conversation as resolved.
Show resolved Hide resolved
self.expect_token(&Token::LBrace)?;
let name = self.parse_identifier(false)?;
self.expect_token(&Token::Colon)?;
let data_type = self.parse_data_type()?;
self.expect_token(&Token::RBrace)?;
Ok(Expr::ParameterizedViewArg {
column: name,
data_type,
})
}
/// Parse an expression prefix
pub fn parse_prefix(&mut self) -> Result<Expr, ParserError> {
// allow the dialect to override prefix parsing
if let Some(prefix) = self.dialect.parse_prefix(self) {
return prefix;
}

// ClickHouse support using {column:Datatype} format in its creation of parametrised views
match self.peek_token().token {
// check if last token is WHERE and if it is do it, else do the following logic
Token::LBrace if dialect_of!(self is ClickHouseDialect | GenericDialect) => {
self.prev_token();
// prev token is EQ
if self.consume_token(&Token::Eq) {
return self.parse_column_with_data_type();
} else {
self.next_token();
}
}
_ => {}
}
// PostgreSQL allows any string literal to be preceded by a type name, indicating that the
// string literal represents a literal of that type. Some examples:
//
Expand Down Expand Up @@ -2542,6 +2566,12 @@ impl<'a> Parser<'a> {
}
self.parse_map_access(expr)
} else if Token::Colon == tok {
if dialect_of!(self is ClickHouseDialect) && self.index() > 0 {
return Ok(Expr::ParameterizedViewArg {
column: Ident::new(expr.to_string()),
data_type: self.parse_data_type()?,
});
}
Ok(Expr::JsonAccess {
left: Box::new(expr),
operator: JsonOperator::Colon,
Expand Down
5 changes: 5 additions & 0 deletions tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,8 @@ fn clickhouse_and_generic() -> TestedDialects {
options: None,
}
}

#[test]
fn parse_create_parametrised_views() {
clickhouse().verified_stmt("CREATE VIEW view AS SELECT * FROM A WHERE Column1 = {column1:datatype1} AND Column2 = {column2:datatype2}");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also please test:

  1. In generic dialect
  2. A single column definition
  3. Error case (like where Column1 = {column1})
  4. Error case (like where) (no column definition)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll add these tommorrow

}
25 changes: 25 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8685,3 +8685,28 @@ fn parse_map_access_expr() {
let _ = dialects.verified_expr(sql);
}
}
fn general_dialect() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(GenericDialect {})],
options: None,
}
}
#[test]
fn parse_create_parametrised_views() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I may be mistaken, but thinking about this PR some more and I am not sure about the change to parse_prefix

Specifically, by changing that I think it now allows {column:data_type} to appear in any expression (not just a parameterized view).

For example, wouldn't these queries now parse SELECT * FROM foo WHERE x = {column:int} or SELECT x={column:int} FROM foo?

Can you add test coverage for those cases?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure

// 1. verify this
general_dialect().verified_stmt("CREATE VIEW view AS SELECT * FROM A WHERE Column1 = {column1:datatype1} AND Column2 = {column2:datatype2}");

// 2. verify single column def
general_dialect()
.verified_stmt("CREATE VIEW view AS SELECT * FROM A WHERE Column1 = {column1:datatype1}");

// 3. Error case I : column =
let mut res = general_dialect()
.parse_sql_statements("CREATE VIEW view AS SELECT * FROM A WHERE Column1 = {column1}");
assert!(res.is_err());
// 4. No column def
res = general_dialect().parse_sql_statements(
"CREATE VIEW view AS SELECT * FROM A WHERE Column1 = {column1:} AND Column2 = {column2:}",
);
assert!(res.is_err());
}
Loading