From aa242f4a1d78ded81a987087647613b6ca673779 Mon Sep 17 00:00:00 2001 From: icefery Date: Tue, 1 Dec 2026 16:47:08 +0800 Subject: [PATCH] PostgreSQL: Support DROP STATISTICS - Reuse the DROP statement AST for extended statistics objects. - Preserve qualified names and drop behavior without accepting unrelated modifiers. --- .../postgres_3081_drop_statistics.sql | 1 + src/ast/mod.rs | 3 + src/dialect/generic.rs | 4 ++ src/dialect/mod.rs | 7 +++ src/dialect/postgresql.rs | 4 ++ src/parser/mod.rs | 11 +++- tests/sqlparser_postgres.rs | 57 +++++++++++++++++++ 7 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 fuzz/fuzz_seeds/postgres_3081_drop_statistics.sql diff --git a/fuzz/fuzz_seeds/postgres_3081_drop_statistics.sql b/fuzz/fuzz_seeds/postgres_3081_drop_statistics.sql new file mode 100644 index 0000000000..1f602ba1e8 --- /dev/null +++ b/fuzz/fuzz_seeds/postgres_3081_drop_statistics.sql @@ -0,0 +1 @@ +DROP STATISTICS IF EXISTS app.stats, other RESTRICT; diff --git a/src/ast/mod.rs b/src/ast/mod.rs index c61c32cf32..9a47fe0e00 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -8955,6 +8955,8 @@ pub enum ObjectType { Stream, /// A warehouse. Warehouse, + /// An extended statistics object. + Statistics, } impl fmt::Display for ObjectType { @@ -8976,6 +8978,7 @@ impl fmt::Display for ObjectType { ObjectType::User => "USER", ObjectType::Stream => "STREAM", ObjectType::Warehouse => "WAREHOUSE", + ObjectType::Statistics => "STATISTICS", }) } } diff --git a/src/dialect/generic.rs b/src/dialect/generic.rs index fdf3529054..2aab24376b 100644 --- a/src/dialect/generic.rs +++ b/src/dialect/generic.rs @@ -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 } diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index b975805b5f..969b1f21cc 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -430,6 +430,13 @@ pub trait Dialect: Debug + Any { false } + /// Returns true if the dialect supports `DROP STATISTICS`. + /// + /// + 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 diff --git a/src/dialect/postgresql.rs b/src/dialect/postgresql.rs index 4e7265cb02..214aeafe9b 100644 --- a/src/dialect/postgresql.rs +++ b/src/dialect/postgresql.rs @@ -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 } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 2e653c7983..0e2f331c77 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -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) { @@ -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 ); } diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 9c59720aa8..183d15d13f 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -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:?}" + ); + } +}