Skip to content
Open
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
1 change: 1 addition & 0 deletions fuzz/fuzz_seeds/postgres_3081_drop_statistics.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP STATISTICS IF EXISTS app.stats, other RESTRICT;
3 changes: 3 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8955,6 +8955,8 @@ pub enum ObjectType {
Stream,
/// A warehouse.
Warehouse,
/// An extended statistics object.
Statistics,
}

impl fmt::Display for ObjectType {
Expand All @@ -8976,6 +8978,7 @@ impl fmt::Display for ObjectType {
ObjectType::User => "USER",
ObjectType::Stream => "STREAM",
ObjectType::Warehouse => "WAREHOUSE",
ObjectType::Statistics => "STATISTICS",
})
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ impl Dialect for GenericDialect {
true
}

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

fn supports_sql_option_without_value(&self) -> bool {
true
}
Expand Down
7 changes: 7 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,13 @@ pub trait Dialect: Debug + Any {
false
}

/// Returns true if the dialect supports `DROP STATISTICS`.
///
/// <https://www.postgresql.org/docs/18/sql-dropstatistics.html>
fn supports_drop_statistics(&self) -> bool {
false
}

/// Returns true if the dialect treats `ALTER USER` as a synonym for `ALTER ROLE`.
///
/// In PostgreSQL, `ALTER USER` and `ALTER ROLE` are synonyms that accept the same
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,10 @@ impl Dialect for PostgreSqlDialect {
true
}

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

fn supports_alter_user_as_alter_role(&self) -> bool {
true
}
Expand Down
11 changes: 9 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8409,6 +8409,9 @@ impl<'a> Parser<'a> {
ObjectType::Sequence
} else if self.parse_keyword(Keyword::STAGE) {
ObjectType::Stage
} else if self.dialect.supports_drop_statistics() && self.parse_keyword(Keyword::STATISTICS)
{
ObjectType::Statistics
} else if self.parse_keyword(Keyword::TYPE) {
ObjectType::Type
} else if self.parse_keyword(Keyword::USER) {
Expand Down Expand Up @@ -8475,9 +8478,13 @@ impl<'a> Parser<'a> {
} else {
None
};
if object_type == ObjectType::ForeignTable && (temporary || purge || table.is_some()) {
if matches!(
object_type,
ObjectType::ForeignTable | ObjectType::Statistics
) && (temporary || purge || table.is_some())
{
return parser_err!(
"Cannot specify TEMPORARY, PURGE, or ON in DROP FOREIGN TABLE",
format_args!("Cannot specify TEMPORARY, PURGE, or ON in DROP {object_type}"),
loc
);
}
Expand Down
57 changes: 57 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14874,3 +14874,60 @@ fn parse_create_function_transform() {
);
}
}

#[test]
fn parse_drop_statistics() {
assert_eq!(
pg_and_generic().verified_stmt(
r#"DROP STATISTICS IF EXISTS accounting.users_uid_creation, public."RoleStats" CASCADE"#,
),
Statement::Drop {
object_type: ObjectType::Statistics,
if_exists: true,
names: vec![
ObjectName::from(vec![Ident::new("accounting"), Ident::new("users_uid_creation")]),
ObjectName::from(vec![Ident::new("public"), Ident::with_quote('"', "RoleStats")]),
],
cascade: true,
restrict: false,
purge: false,
temporary: false,
table: None,
}
);
pg_and_generic().verified_stmt("DROP STATISTICS stats RESTRICT");
pg_and_generic().verified_stmt("DROP STATISTICS stats");
}

#[test]
fn parse_drop_statistics_rejects_invalid_syntax() {
for dialect in pg_and_generic().dialects {
for sql in [
"DROP STATISTICS",
"DROP STATISTICS IF EXISTS",
"DROP STATISTICS stats CASCADE RESTRICT",
"DROP STATISTICS stats RESTRICT CASCADE",
"DROP TEMPORARY STATISTICS stats",
"DROP STATISTICS stats PURGE",
"DROP STATISTICS stats ON t",
] {
let result = Parser::parse_sql(dialect.as_ref(), sql);
assert!(
matches!(result, Err(ParserError::ParserError(_))),
"{dialect:?}: {sql}: {result:?}"
);
}
}
}

#[test]
fn parse_drop_statistics_is_dialect_gated() {
let sql = "DROP STATISTICS stats";
for dialect in all_dialects_where(|d| !d.supports_drop_statistics()).dialects {
let result = Parser::parse_sql(dialect.as_ref(), sql);
assert!(
matches!(result, Err(ParserError::ParserError(_))),
"{dialect:?}: {sql}: {result:?}"
);
}
}
Loading