Skip to content
5 changes: 5 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,11 @@ pub trait Dialect: Debug + Any {
None
}

/// Does the dialect support the `APPROXIMATE PERCENTILE_DISC` function syntax?
fn supports_approximate_percentile_disc(&self) -> bool {
false
}

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

fn supports_approximate_percentile_disc(&self) -> bool {
true
}

fn supports_geometric_types(&self) -> bool {
true
}
Expand Down
14 changes: 14 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1836,6 +1836,20 @@ impl<'a> Parser<'a> {

let dialect = self.dialect;

if dialect.supports_approximate_percentile_disc()
&& matches!(&self.peek_token_ref().token, Token::Word(word) if word.value.eq_ignore_ascii_case("approximate"))
&& matches!(&self.peek_nth_token_ref(1).token, Token::Word(word) if word.value.eq_ignore_ascii_case("percentile_disc"))
{
self.next_token();
let function_name = self.parse_object_name(false)?;
return self
.parse_function(function_name)
.map(|function| Expr::Prefixed {
prefix: "APPROXIMATE".into(),
value: Box::new(function),
});
}

self.advance_token();
let next_token_index = self.get_current_index();
let next_token = self.get_current_token();
Expand Down
7 changes: 7 additions & 0 deletions tests/sqlparser_redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,3 +557,10 @@ 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() {
redshift().verified_stmt(
"SELECT APPROXIMATE PERCENTILE_DISC(0.5) WITHIN GROUP (ORDER BY totalprice)",
);
}
Loading