Skip to content

Support PREWHERE condition for ClickHouse dialect #1328

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 3 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ pub struct Select {
pub from: Vec<TableWithJoins>,
/// LATERAL VIEWs
pub lateral_views: Vec<LateralView>,
/// ClickHouse syntax: `PREWHERE a = 1 WHERE b = 2`,
/// and it can be used together with WHERE selection.
///
/// [ClickHouse](https://clickhouse.com/docs/en/sql-reference/statements/select/prewhere)
pub prewhere: Option<Expr>,
/// WHERE
pub selection: Option<Expr>,
/// GROUP BY
Expand Down Expand Up @@ -295,6 +300,9 @@ impl fmt::Display for Select {
write!(f, "{lv}")?;
}
}
if let Some(ref prewhere) = self.prewhere {
write!(f, " PREWHERE {prewhere}")?;
}
if let Some(ref selection) = self.selection {
write!(f, " WHERE {selection}")?;
}
Expand Down
3 changes: 3 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,7 @@ define_keywords!(
PRECISION,
PREPARE,
PRESERVE,
PREWHERE,
PRIMARY,
PRIOR,
PRIVILEGES,
Expand Down Expand Up @@ -850,6 +851,8 @@ pub const RESERVED_FOR_TABLE_ALIAS: &[Keyword] = &[
Keyword::FOR,
// for MYSQL PARTITION SELECTION
Keyword::PARTITION,
// for Clickhouse PREWHERE
Keyword::PREWHERE,
// for Snowflake START WITH .. CONNECT BY
Keyword::START,
Keyword::CONNECT,
Expand Down
9 changes: 9 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8312,6 +8312,14 @@ impl<'a> Parser<'a> {
}
}

let prewhere = if dialect_of!(self is ClickHouseDialect|GenericDialect)
&& self.parse_keyword(Keyword::PREWHERE)
{
Some(self.parse_expr()?)
} else {
None
};

let selection = if self.parse_keyword(Keyword::WHERE) {
Some(self.parse_expr()?)
} else {
Expand Down Expand Up @@ -8423,6 +8431,7 @@ impl<'a> Parser<'a> {
into,
from,
lateral_views,
prewhere,
selection,
group_by,
cluster_by,
Expand Down
51 changes: 51 additions & 0 deletions tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ fn parse_map_access_expr() {
joins: vec![],
}],
lateral_views: vec![],
prewhere: None,
selection: Some(BinaryOp {
left: Box::new(BinaryOp {
left: Box::new(Identifier(Ident::new("id"))),
Expand Down Expand Up @@ -681,6 +682,56 @@ fn parse_group_by_with_modifier() {
}
}

#[test]
fn test_prewhere() {
match clickhouse().verified_stmt("SELECT * FROM t PREWHERE x = 1 WHERE y = 2") {
Statement::Query(query) => {
let prewhere = query.body.as_select().unwrap().prewhere.as_ref();
assert_eq!(
prewhere,
Some(&BinaryOp {
left: Box::new(Identifier(Ident::new("x"))),
op: BinaryOperator::Eq,
right: Box::new(Expr::Value(Value::Number("1".parse().unwrap(), false))),
})
);
let selection = query.as_ref().body.as_select().unwrap().selection.as_ref();
assert_eq!(
selection,
Some(&BinaryOp {
left: Box::new(Identifier(Ident::new("y"))),
op: BinaryOperator::Eq,
right: Box::new(Expr::Value(Value::Number("2".parse().unwrap(), false))),
})
);
}
_ => unreachable!(),
}

match clickhouse().verified_stmt("SELECT * FROM t PREWHERE x = 1 AND y = 2") {
Statement::Query(query) => {
let prewhere = query.body.as_select().unwrap().prewhere.as_ref();
assert_eq!(
prewhere,
Some(&BinaryOp {
left: Box::new(BinaryOp {
left: Box::new(Identifier(Ident::new("x"))),
op: BinaryOperator::Eq,
right: Box::new(Expr::Value(Value::Number("1".parse().unwrap(), false))),
}),
op: BinaryOperator::And,
right: Box::new(BinaryOp {
left: Box::new(Identifier(Ident::new("y"))),
op: BinaryOperator::Eq,
right: Box::new(Expr::Value(Value::Number("2".parse().unwrap(), false))),
}),
})
);
}
_ => unreachable!(),
}
}

fn clickhouse() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(ClickHouseDialect {})],
Expand Down
8 changes: 8 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ fn parse_update_set_from() {
joins: vec![],
}],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(
vec![Expr::Identifier(Ident::new("id"))],
Expand Down Expand Up @@ -4548,6 +4549,7 @@ fn test_parse_named_window() {
joins: vec![],
}],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down Expand Up @@ -4929,6 +4931,7 @@ fn parse_interval_and_or_xor() {
joins: vec![],
}],
lateral_views: vec![],
prewhere: None,
selection: Some(Expr::BinaryOp {
left: Box::new(Expr::BinaryOp {
left: Box::new(Expr::Identifier(Ident {
Expand Down Expand Up @@ -6911,6 +6914,7 @@ fn lateral_function() {
}],
}],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down Expand Up @@ -7630,6 +7634,7 @@ fn parse_merge() {
joins: vec![],
}],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down Expand Up @@ -9136,6 +9141,7 @@ fn parse_unload() {
joins: vec![],
}],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down Expand Up @@ -9279,6 +9285,7 @@ fn parse_connect_by() {
}],
into: None,
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down Expand Up @@ -9363,6 +9370,7 @@ fn parse_connect_by() {
}],
into: None,
lateral_views: vec![],
prewhere: None,
selection: Some(Expr::BinaryOp {
left: Box::new(Expr::Identifier(Ident::new("employee_id"))),
op: BinaryOperator::NotEq,
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_duckdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ fn test_select_union_by_name() {
joins: vec![],
}],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down Expand Up @@ -208,6 +209,7 @@ fn test_select_union_by_name() {
joins: vec![],
}],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ fn parse_create_procedure() {
into: None,
from: vec![],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down Expand Up @@ -527,6 +528,7 @@ fn parse_substring_in_select() {
joins: vec![]
}],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down
8 changes: 8 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ fn parse_escaped_quote_identifiers_with_escape() {
into: None,
from: vec![],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down Expand Up @@ -953,6 +954,7 @@ fn parse_escaped_quote_identifiers_with_no_escape() {
into: None,
from: vec![],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down Expand Up @@ -997,6 +999,7 @@ fn parse_escaped_backticks_with_escape() {
into: None,
from: vec![],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down Expand Up @@ -1041,6 +1044,7 @@ fn parse_escaped_backticks_with_no_escape() {
into: None,
from: vec![],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down Expand Up @@ -1702,6 +1706,7 @@ fn parse_select_with_numeric_prefix_column_name() {
joins: vec![]
}],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down Expand Up @@ -1755,6 +1760,7 @@ fn parse_select_with_concatenation_of_exp_number_and_numeric_prefix_column() {
joins: vec![]
}],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down Expand Up @@ -2254,6 +2260,7 @@ fn parse_substring_in_select() {
joins: vec![]
}],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down Expand Up @@ -2558,6 +2565,7 @@ fn parse_hex_string_introducer() {
})],
from: vec![],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down
3 changes: 3 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,6 +1074,7 @@ fn parse_copy_to() {
into: None,
from: vec![],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
having: None,
Expand Down Expand Up @@ -2382,6 +2383,7 @@ fn parse_array_subquery_expr() {
into: None,
from: vec![],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand All @@ -2401,6 +2403,7 @@ fn parse_array_subquery_expr() {
into: None,
from: vec![],
lateral_views: vec![],
prewhere: None,
selection: None,
group_by: GroupByExpr::Expressions(vec![], vec![]),
cluster_by: vec![],
Expand Down
Loading