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

clickhouse: add support for LIMIT BY #977

Merged
merged 1 commit into from
Oct 2, 2023
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: 7 additions & 0 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ pub struct Query {
pub order_by: Vec<OrderByExpr>,
/// `LIMIT { <N> | ALL }`
pub limit: Option<Expr>,

/// `LIMIT { <N> } BY { <expr>,<expr>,... } }`
pub limit_by: Vec<Expr>,

/// `OFFSET <N> [ { ROW | ROWS } ]`
pub offset: Option<Offset>,
/// `FETCH { FIRST | NEXT } <N> [ PERCENT ] { ROW | ROWS } | { ONLY | WITH TIES }`
Expand All @@ -58,6 +62,9 @@ impl fmt::Display for Query {
if let Some(ref offset) = self.offset {
write!(f, " {offset}")?;
}
if !self.limit_by.is_empty() {
write!(f, " BY {}", display_separated(&self.limit_by, ", "))?;
}
if let Some(ref fetch) = self.fetch {
write!(f, " {fetch}")?;
}
Expand Down
13 changes: 12 additions & 1 deletion src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5431,6 +5431,7 @@ impl<'a> Parser<'a> {
with,
body: Box::new(SetExpr::Insert(insert)),
limit: None,
limit_by: vec![],
order_by: vec![],
offset: None,
fetch: None,
Expand All @@ -5442,6 +5443,7 @@ impl<'a> Parser<'a> {
with,
body: Box::new(SetExpr::Update(update)),
limit: None,
limit_by: vec![],
order_by: vec![],
offset: None,
fetch: None,
Expand All @@ -5468,7 +5470,7 @@ impl<'a> Parser<'a> {
offset = Some(self.parse_offset()?)
}

if dialect_of!(self is GenericDialect | MySqlDialect)
if dialect_of!(self is GenericDialect | MySqlDialect | ClickHouseDialect)
&& limit.is_some()
&& offset.is_none()
&& self.consume_token(&Token::Comma)
Expand All @@ -5483,6 +5485,14 @@ impl<'a> Parser<'a> {
}
}

let limit_by = if dialect_of!(self is ClickHouseDialect | GenericDialect)
&& self.parse_keyword(Keyword::BY)
{
self.parse_comma_separated(Parser::parse_expr)?
} else {
vec![]
};

let fetch = if self.parse_keyword(Keyword::FETCH) {
Some(self.parse_fetch()?)
} else {
Expand All @@ -5499,6 +5509,7 @@ impl<'a> Parser<'a> {
body,
order_by,
limit,
limit_by,
offset,
fetch,
locks,
Expand Down
18 changes: 18 additions & 0 deletions tests/sqlparser_clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use sqlparser::ast::TableFactor::Table;
use sqlparser::ast::*;

use sqlparser::dialect::ClickHouseDialect;
use sqlparser::dialect::GenericDialect;

#[test]
fn parse_map_access_expr() {
Expand Down Expand Up @@ -344,9 +345,26 @@ fn parse_double_equal() {
);
}

#[test]
fn parse_limit_by() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you also please add a test for the multi-column form (e.g. LIMIT BY asset, class or similar`)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added

clickhouse_and_generic().verified_stmt(
r#"SELECT * FROM default.last_asset_runs_mv ORDER BY created_at DESC LIMIT 1 BY asset"#,
);
clickhouse_and_generic().verified_stmt(
r#"SELECT * FROM default.last_asset_runs_mv ORDER BY created_at DESC LIMIT 1 BY asset, toStartOfDay(created_at)"#,
);
}

fn clickhouse() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(ClickHouseDialect {})],
options: None,
}
}

fn clickhouse_and_generic() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(ClickHouseDialect {}), Box::new(GenericDialect {})],
options: None,
}
}
5 changes: 5 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ fn parse_update_set_from() {
}))),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down Expand Up @@ -2662,6 +2663,7 @@ fn parse_create_table_as_table() {
}))),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand All @@ -2685,6 +2687,7 @@ fn parse_create_table_as_table() {
}))),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down Expand Up @@ -3976,6 +3979,7 @@ fn parse_interval_and_or_xor() {
}))),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down Expand Up @@ -6392,6 +6396,7 @@ fn parse_merge() {
}))),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down
3 changes: 3 additions & 0 deletions tests/sqlparser_mssql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ fn parse_create_procedure() {
body: vec![Statement::Query(Box::new(Query {
with: None,
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down Expand Up @@ -493,6 +494,7 @@ fn parse_substring_in_select() {
assert_eq!(
Box::new(Query {
with: None,

body: Box::new(SetExpr::Select(Box::new(Select {
distinct: Some(Distinct::Distinct),
top: None,
Expand Down Expand Up @@ -532,6 +534,7 @@ fn parse_substring_in_select() {
}))),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down
9 changes: 9 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ fn parse_escaped_quote_identifiers_with_escape() {
}))),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down Expand Up @@ -604,6 +605,7 @@ fn parse_escaped_quote_identifiers_with_no_escape() {
}))),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down Expand Up @@ -643,6 +645,7 @@ fn parse_escaped_backticks_with_escape() {
}))),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down Expand Up @@ -682,6 +685,7 @@ fn parse_escaped_backticks_with_no_escape() {
}))),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down Expand Up @@ -956,6 +960,7 @@ fn parse_simple_insert() {
})),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down Expand Up @@ -991,6 +996,7 @@ fn parse_empty_row_insert() {
})),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down Expand Up @@ -1049,6 +1055,7 @@ fn parse_insert_with_on_duplicate_update() {
})),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down Expand Up @@ -1428,6 +1435,7 @@ fn parse_substring_in_select() {
}))),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down Expand Up @@ -1708,6 +1716,7 @@ fn parse_hex_string_introducer() {
}))),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down
2 changes: 2 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,7 @@ fn parse_copy_to() {
}))),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down Expand Up @@ -2046,6 +2047,7 @@ fn parse_array_subquery_expr() {
}),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
Expand Down
Loading