diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index ff83a4da6..e91dc1264 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -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 diff --git a/src/dialect/redshift.rs b/src/dialect/redshift.rs index aa403618f..02b3f6973 100644 --- a/src/dialect/redshift.rs +++ b/src/dialect/redshift.rs @@ -128,6 +128,10 @@ impl Dialect for RedshiftSqlDialect { true } + fn supports_approximate_percentile_disc(&self) -> bool { + true + } + fn supports_geometric_types(&self) -> bool { true } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 5edc43714..568460d44 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -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(); diff --git a/tests/sqlparser_redshift.rs b/tests/sqlparser_redshift.rs index 31cc4fe58..be7b0b1b8 100644 --- a/tests/sqlparser_redshift.rs +++ b/tests/sqlparser_redshift.rs @@ -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)", + ); +}