Skip to content
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
6 changes: 6 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,12 @@ pub trait Dialect: Debug + Any {
None
}

/// Does the dialect support the `APPROXIMATE PERCENTILE_DISC` function syntax?
/// See <https://docs.aws.amazon.com/redshift/latest/dg/r_APPROXIMATE_PERCENTILE_DISC.html>
fn supports_approximate_percentile_disc(&self) -> bool {
Comment thread
BenSatori marked this conversation as resolved.
false
}

/// Does the dialect support trailing commas around the query?
fn supports_trailing_commas(&self) -> bool {
false
Expand Down
5 changes: 5 additions & 0 deletions src/dialect/redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ impl Dialect for RedshiftSqlDialect {
true
}

/// See <https://docs.aws.amazon.com/redshift/latest/dg/r_APPROXIMATE_PERCENTILE_DISC.html>
fn supports_approximate_percentile_disc(&self) -> bool {
true
}

fn supports_geometric_types(&self) -> bool {
true
}
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ define_keywords!(
APPLICATION,
APPLY,
APPLYBUDGET,
APPROXIMATE,
ARCHIVE,
ARE,
ARRAY,
Expand Down
12 changes: 12 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1635,6 +1635,18 @@ impl<'a> Parser<'a> {
Keyword::MAP if *self.peek_token_ref() == Token::LBrace && self.dialect.support_map_literal_syntax() => {
Ok(Some(self.parse_duckdb_map_literal()?))
}
Keyword::APPROXIMATE
if self.dialect.supports_approximate_percentile_disc()
&& self.peek_keyword(Keyword::PERCENTILE_DISC) =>
{
self.maybe_parse(|parser| {
let function_name = parser.parse_object_name(false)?;
parser.parse_function(function_name).map(|function| Expr::Prefixed {
prefix: w.to_ident(w_span),
value: Box::new(function),
})
})
}
Keyword::LAMBDA if self.dialect.supports_lambda_functions() => {
Ok(Some(self.parse_lambda_expr()?))
}
Expand Down
16 changes: 16 additions & 0 deletions tests/sqlparser_redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,19 @@ fn parse_unpivot_expression() {
fn test_interval_as_column_name() {
redshift().verified_stmt("SELECT * FROM table_name WHERE interval = 78");
}

#[test]
fn parse_approximate_percentile_disc() {
let supporting = all_dialects_where(|d| d.supports_approximate_percentile_disc());
supporting.verified_stmt(
"SELECT APPROXIMATE PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY totalprice)",
);

// Without dialect support, `APPROXIMATE` is just a column name and
// `PERCENTILE_DISC` its implicit alias.
let non_supporting = all_dialects_where(|d| !d.supports_approximate_percentile_disc());
non_supporting.one_statement_parses_to(
"SELECT APPROXIMATE PERCENTILE_DISC FROM t",
"SELECT APPROXIMATE AS PERCENTILE_DISC FROM t",
);
}
Loading